Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:13

0001 //===- llvm/MC/MCInstrAnalysis.h - InstrDesc target hooks -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the MCInstrAnalysis class which the MCTargetDescs can
0010 // derive from to give additional information to MC.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_MC_MCINSTRANALYSIS_H
0015 #define LLVM_MC_MCINSTRANALYSIS_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/MC/MCInst.h"
0019 #include "llvm/MC/MCInstrDesc.h"
0020 #include "llvm/MC/MCInstrInfo.h"
0021 #include "llvm/MC/MCRegisterInfo.h"
0022 #include <cstdint>
0023 #include <vector>
0024 
0025 namespace llvm {
0026 
0027 class MCRegisterInfo;
0028 class Triple;
0029 
0030 class MCInstrAnalysis {
0031 protected:
0032   friend class Target;
0033 
0034   const MCInstrInfo *Info;
0035 
0036 public:
0037   MCInstrAnalysis(const MCInstrInfo *Info) : Info(Info) {}
0038   virtual ~MCInstrAnalysis() = default;
0039 
0040   /// Clear the internal state. See updateState for more information.
0041   virtual void resetState() {}
0042 
0043   /// Update internal state with \p Inst at \p Addr.
0044   ///
0045   /// For some types of analyses, inspecting a single instruction is not
0046   /// sufficient. Some examples are auipc/jalr pairs on RISC-V or adrp/ldr pairs
0047   /// on AArch64. To support inspecting multiple instructions, targets may keep
0048   /// track of an internal state while analysing instructions. Clients should
0049   /// call updateState for every instruction which allows later calls to one of
0050   /// the analysis functions to take previous instructions into account.
0051   /// Whenever state becomes irrelevant (e.g., when starting to disassemble a
0052   /// new function), clients should call resetState to clear it.
0053   virtual void updateState(const MCInst &Inst, uint64_t Addr) {}
0054 
0055   virtual bool isBranch(const MCInst &Inst) const {
0056     return Info->get(Inst.getOpcode()).isBranch();
0057   }
0058 
0059   virtual bool isConditionalBranch(const MCInst &Inst) const {
0060     return Info->get(Inst.getOpcode()).isConditionalBranch();
0061   }
0062 
0063   virtual bool isUnconditionalBranch(const MCInst &Inst) const {
0064     return Info->get(Inst.getOpcode()).isUnconditionalBranch();
0065   }
0066 
0067   virtual bool isIndirectBranch(const MCInst &Inst) const {
0068     return Info->get(Inst.getOpcode()).isIndirectBranch();
0069   }
0070 
0071   virtual bool isCall(const MCInst &Inst) const {
0072     return Info->get(Inst.getOpcode()).isCall();
0073   }
0074 
0075   virtual bool isReturn(const MCInst &Inst) const {
0076     return Info->get(Inst.getOpcode()).isReturn();
0077   }
0078 
0079   virtual bool isTerminator(const MCInst &Inst) const {
0080     return Info->get(Inst.getOpcode()).isTerminator();
0081   }
0082 
0083   virtual bool mayAffectControlFlow(const MCInst &Inst,
0084                                     const MCRegisterInfo &MCRI) const {
0085     if (isBranch(Inst) || isCall(Inst) || isReturn(Inst) ||
0086         isIndirectBranch(Inst))
0087       return true;
0088     MCRegister PC = MCRI.getProgramCounter();
0089     if (!PC)
0090       return false;
0091     return Info->get(Inst.getOpcode()).hasDefOfPhysReg(Inst, PC, MCRI);
0092   }
0093 
0094   /// Returns true if at least one of the register writes performed by
0095   /// \param Inst implicitly clears the upper portion of all super-registers.
0096   ///
0097   /// Example: on X86-64, a write to EAX implicitly clears the upper half of
0098   /// RAX. Also (still on x86) an XMM write perfomed by an AVX 128-bit
0099   /// instruction implicitly clears the upper portion of the correspondent
0100   /// YMM register.
0101   ///
0102   /// This method also updates an APInt which is used as mask of register
0103   /// writes. There is one bit for every explicit/implicit write performed by
0104   /// the instruction. If a write implicitly clears its super-registers, then
0105   /// the corresponding bit is set (vic. the corresponding bit is cleared).
0106   ///
0107   /// The first bits in the APint are related to explicit writes. The remaining
0108   /// bits are related to implicit writes. The sequence of writes follows the
0109   /// machine operand sequence. For implicit writes, the sequence is defined by
0110   /// the MCInstrDesc.
0111   ///
0112   /// The assumption is that the bit-width of the APInt is correctly set by
0113   /// the caller. The default implementation conservatively assumes that none of
0114   /// the writes clears the upper portion of a super-register.
0115   virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI,
0116                                     const MCInst &Inst,
0117                                     APInt &Writes) const;
0118 
0119   /// Returns true if MI is a dependency breaking zero-idiom for the given
0120   /// subtarget.
0121   ///
0122   /// Mask is used to identify input operands that have their dependency
0123   /// broken. Each bit of the mask is associated with a specific input operand.
0124   /// Bits associated with explicit input operands are laid out first in the
0125   /// mask; implicit operands come after explicit operands.
0126   ///
0127   /// Dependencies are broken only for operands that have their corresponding bit
0128   /// set. Operands that have their bit cleared, or that don't have a
0129   /// corresponding bit in the mask don't have their dependency broken.  Note
0130   /// that Mask may not be big enough to describe all operands.  The assumption
0131   /// for operands that don't have a correspondent bit in the mask is that those
0132   /// are still data dependent.
0133   ///
0134   /// The only exception to the rule is for when Mask has all zeroes.
0135   /// A zero mask means: dependencies are broken for all explicit register
0136   /// operands.
0137   virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask,
0138                            unsigned CPUID) const {
0139     return false;
0140   }
0141 
0142   /// Returns true if MI is a dependency breaking instruction for the
0143   /// subtarget associated with CPUID .
0144   ///
0145   /// The value computed by a dependency breaking instruction is not dependent
0146   /// on the inputs. An example of dependency breaking instruction on X86 is
0147   /// `XOR %eax, %eax`.
0148   ///
0149   /// If MI is a dependency breaking instruction for subtarget CPUID, then Mask
0150   /// can be inspected to identify independent operands.
0151   ///
0152   /// Essentially, each bit of the mask corresponds to an input operand.
0153   /// Explicit operands are laid out first in the mask; implicit operands follow
0154   /// explicit operands. Bits are set for operands that are independent.
0155   ///
0156   /// Note that the number of bits in Mask may not be equivalent to the sum of
0157   /// explicit and implicit operands in MI. Operands that don't have a
0158   /// corresponding bit in Mask are assumed "not independente".
0159   ///
0160   /// The only exception is for when Mask is all zeroes. That means: explicit
0161   /// input operands of MI are independent.
0162   virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask,
0163                                     unsigned CPUID) const {
0164     return isZeroIdiom(MI, Mask, CPUID);
0165   }
0166 
0167   /// Returns true if MI is a candidate for move elimination.
0168   ///
0169   /// Different subtargets may apply different constraints to optimizable
0170   /// register moves. For example, on most X86 subtargets, a candidate for move
0171   /// elimination cannot specify the same register for both source and
0172   /// destination.
0173   virtual bool isOptimizableRegisterMove(const MCInst &MI,
0174                                          unsigned CPUID) const {
0175     return false;
0176   }
0177 
0178   /// Given a branch instruction try to get the address the branch
0179   /// targets. Return true on success, and the address in Target.
0180   virtual bool
0181   evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
0182                  uint64_t &Target) const;
0183 
0184   /// Given an instruction tries to get the address of a memory operand. Returns
0185   /// the address on success.
0186   virtual std::optional<uint64_t>
0187   evaluateMemoryOperandAddress(const MCInst &Inst, const MCSubtargetInfo *STI,
0188                                uint64_t Addr, uint64_t Size) const;
0189 
0190   /// Given an instruction with a memory operand that could require relocation,
0191   /// returns the offset within the instruction of that relocation.
0192   virtual std::optional<uint64_t>
0193   getMemoryOperandRelocationOffset(const MCInst &Inst, uint64_t Size) const;
0194 
0195   /// Returns (PLT virtual address, GOT virtual address) pairs for PLT entries.
0196   virtual std::vector<std::pair<uint64_t, uint64_t>>
0197   findPltEntries(uint64_t PltSectionVA, ArrayRef<uint8_t> PltContents,
0198                  const Triple &TargetTriple) const {
0199     return {};
0200   }
0201 };
0202 
0203 } // end namespace llvm
0204 
0205 #endif // LLVM_MC_MCINSTRANALYSIS_H