Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:30

0001 //===- llvm/CodeGen/MachineFunction.h ---------------------------*- 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 // Collect native machine code for a function.  This class contains a list of
0010 // MachineBasicBlock instances that make up the current compiled function.
0011 //
0012 // This class also contains pointers to various classes which hold
0013 // target-specific information about the generated code.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
0018 #define LLVM_CODEGEN_MACHINEFUNCTION_H
0019 
0020 #include "llvm/ADT/ArrayRef.h"
0021 #include "llvm/ADT/DenseMap.h"
0022 #include "llvm/ADT/GraphTraits.h"
0023 #include "llvm/ADT/SmallVector.h"
0024 #include "llvm/ADT/ilist.h"
0025 #include "llvm/ADT/iterator.h"
0026 #include "llvm/CodeGen/MachineBasicBlock.h"
0027 #include "llvm/CodeGen/MachineInstr.h"
0028 #include "llvm/CodeGen/MachineMemOperand.h"
0029 #include "llvm/IR/EHPersonalities.h"
0030 #include "llvm/Support/Allocator.h"
0031 #include "llvm/Support/ArrayRecycler.h"
0032 #include "llvm/Support/AtomicOrdering.h"
0033 #include "llvm/Support/Compiler.h"
0034 #include "llvm/Support/Recycler.h"
0035 #include "llvm/Target/TargetOptions.h"
0036 #include <bitset>
0037 #include <cassert>
0038 #include <cstdint>
0039 #include <memory>
0040 #include <utility>
0041 #include <variant>
0042 #include <vector>
0043 
0044 namespace llvm {
0045 
0046 class BasicBlock;
0047 class BlockAddress;
0048 class DataLayout;
0049 class DebugLoc;
0050 struct DenormalMode;
0051 class DIExpression;
0052 class DILocalVariable;
0053 class DILocation;
0054 class Function;
0055 class GISelChangeObserver;
0056 class GlobalValue;
0057 class TargetMachine;
0058 class MachineConstantPool;
0059 class MachineFrameInfo;
0060 class MachineFunction;
0061 class MachineJumpTableInfo;
0062 class MachineRegisterInfo;
0063 class MCContext;
0064 class MCInstrDesc;
0065 class MCSymbol;
0066 class MCSection;
0067 class Pass;
0068 class PseudoSourceValueManager;
0069 class raw_ostream;
0070 class SlotIndexes;
0071 class StringRef;
0072 class TargetRegisterClass;
0073 class TargetSubtargetInfo;
0074 struct WasmEHFuncInfo;
0075 struct WinEHFuncInfo;
0076 
0077 template <> struct ilist_alloc_traits<MachineBasicBlock> {
0078   void deleteNode(MachineBasicBlock *MBB);
0079 };
0080 
0081 template <> struct ilist_callback_traits<MachineBasicBlock> {
0082   void addNodeToList(MachineBasicBlock* N);
0083   void removeNodeFromList(MachineBasicBlock* N);
0084 
0085   template <class Iterator>
0086   void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator) {
0087     assert(this == &OldList && "never transfer MBBs between functions");
0088   }
0089 };
0090 
0091 // The hotness of static data tracked by a MachineFunction and not represented
0092 // as a global object in the module IR / MIR. Typical examples are
0093 // MachineJumpTableInfo and MachineConstantPool.
0094 enum class MachineFunctionDataHotness {
0095   Unknown,
0096   Cold,
0097   Hot,
0098 };
0099 
0100 /// MachineFunctionInfo - This class can be derived from and used by targets to
0101 /// hold private target-specific information for each MachineFunction.  Objects
0102 /// of type are accessed/created with MF::getInfo and destroyed when the
0103 /// MachineFunction is destroyed.
0104 struct MachineFunctionInfo {
0105   virtual ~MachineFunctionInfo();
0106 
0107   /// Factory function: default behavior is to call new using the
0108   /// supplied allocator.
0109   ///
0110   /// This function can be overridden in a derive class.
0111   template <typename FuncInfoTy, typename SubtargetTy = TargetSubtargetInfo>
0112   static FuncInfoTy *create(BumpPtrAllocator &Allocator, const Function &F,
0113                             const SubtargetTy *STI) {
0114     return new (Allocator.Allocate<FuncInfoTy>()) FuncInfoTy(F, STI);
0115   }
0116 
0117   template <typename Ty>
0118   static Ty *create(BumpPtrAllocator &Allocator, const Ty &MFI) {
0119     return new (Allocator.Allocate<Ty>()) Ty(MFI);
0120   }
0121 
0122   /// Make a functionally equivalent copy of this MachineFunctionInfo in \p MF.
0123   /// This requires remapping MachineBasicBlock references from the original
0124   /// parent to values in the new function. Targets may assume that virtual
0125   /// register and frame index values are preserved in the new function.
0126   virtual MachineFunctionInfo *
0127   clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
0128         const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
0129       const {
0130     return nullptr;
0131   }
0132 };
0133 
0134 /// Properties which a MachineFunction may have at a given point in time.
0135 /// Each of these has checking code in the MachineVerifier, and passes can
0136 /// require that a property be set.
0137 class MachineFunctionProperties {
0138   // Possible TODO: Allow targets to extend this (perhaps by allowing the
0139   // constructor to specify the size of the bit vector)
0140   // Possible TODO: Allow requiring the negative (e.g. VRegsAllocated could be
0141   // stated as the negative of "has vregs"
0142 
0143 public:
0144   // The properties are stated in "positive" form; i.e. a pass could require
0145   // that the property hold, but not that it does not hold.
0146 
0147   // Property descriptions:
0148   // IsSSA: True when the machine function is in SSA form and virtual registers
0149   //  have a single def.
0150   // NoPHIs: The machine function does not contain any PHI instruction.
0151   // TracksLiveness: True when tracking register liveness accurately.
0152   //  While this property is set, register liveness information in basic block
0153   //  live-in lists and machine instruction operands (e.g. implicit defs) is
0154   //  accurate, kill flags are conservatively accurate (kill flag correctly
0155   //  indicates the last use of a register, an operand without kill flag may or
0156   //  may not be the last use of a register). This means it can be used to
0157   //  change the code in ways that affect the values in registers, for example
0158   //  by the register scavenger.
0159   //  When this property is cleared at a very late time, liveness is no longer
0160   //  reliable.
0161   // NoVRegs: The machine function does not use any virtual registers.
0162   // Legalized: In GlobalISel: the MachineLegalizer ran and all pre-isel generic
0163   //  instructions have been legalized; i.e., all instructions are now one of:
0164   //   - generic and always legal (e.g., COPY)
0165   //   - target-specific
0166   //   - legal pre-isel generic instructions.
0167   // RegBankSelected: In GlobalISel: the RegBankSelect pass ran and all generic
0168   //  virtual registers have been assigned to a register bank.
0169   // Selected: In GlobalISel: the InstructionSelect pass ran and all pre-isel
0170   //  generic instructions have been eliminated; i.e., all instructions are now
0171   //  target-specific or non-pre-isel generic instructions (e.g., COPY).
0172   //  Since only pre-isel generic instructions can have generic virtual register
0173   //  operands, this also means that all generic virtual registers have been
0174   //  constrained to virtual registers (assigned to register classes) and that
0175   //  all sizes attached to them have been eliminated.
0176   // TiedOpsRewritten: The twoaddressinstruction pass will set this flag, it
0177   //  means that tied-def have been rewritten to meet the RegConstraint.
0178   // FailsVerification: Means that the function is not expected to pass machine
0179   //  verification. This can be set by passes that introduce known problems that
0180   //  have not been fixed yet.
0181   // TracksDebugUserValues: Without this property enabled, debug instructions
0182   // such as DBG_VALUE are allowed to reference virtual registers even if those
0183   // registers do not have a definition. With the property enabled virtual
0184   // registers must only be used if they have a definition. This property
0185   // allows earlier passes in the pipeline to skip updates of `DBG_VALUE`
0186   // instructions to save compile time.
0187   enum class Property : unsigned {
0188     IsSSA,
0189     NoPHIs,
0190     TracksLiveness,
0191     NoVRegs,
0192     FailedISel,
0193     Legalized,
0194     RegBankSelected,
0195     Selected,
0196     TiedOpsRewritten,
0197     FailsVerification,
0198     FailedRegAlloc,
0199     TracksDebugUserValues,
0200     LastProperty = TracksDebugUserValues,
0201   };
0202 
0203   bool hasProperty(Property P) const {
0204     return Properties[static_cast<unsigned>(P)];
0205   }
0206 
0207   MachineFunctionProperties &set(Property P) {
0208     Properties.set(static_cast<unsigned>(P));
0209     return *this;
0210   }
0211 
0212   MachineFunctionProperties &reset(Property P) {
0213     Properties.reset(static_cast<unsigned>(P));
0214     return *this;
0215   }
0216 
0217   /// Reset all the properties.
0218   MachineFunctionProperties &reset() {
0219     Properties.reset();
0220     return *this;
0221   }
0222 
0223   MachineFunctionProperties &set(const MachineFunctionProperties &MFP) {
0224     Properties |= MFP.Properties;
0225     return *this;
0226   }
0227 
0228   MachineFunctionProperties &reset(const MachineFunctionProperties &MFP) {
0229     Properties &= ~MFP.Properties;
0230     return *this;
0231   }
0232 
0233   // Returns true if all properties set in V (i.e. required by a pass) are set
0234   // in this.
0235   bool verifyRequiredProperties(const MachineFunctionProperties &V) const {
0236     return (Properties | ~V.Properties).all();
0237   }
0238 
0239   /// Print the MachineFunctionProperties in human-readable form.
0240   void print(raw_ostream &OS) const;
0241 
0242 private:
0243   std::bitset<static_cast<unsigned>(Property::LastProperty) + 1> Properties;
0244 };
0245 
0246 struct SEHHandler {
0247   /// Filter or finally function. Null indicates a catch-all.
0248   const Function *FilterOrFinally;
0249 
0250   /// Address of block to recover at. Null for a finally handler.
0251   const BlockAddress *RecoverBA;
0252 };
0253 
0254 /// This structure is used to retain landing pad info for the current function.
0255 struct LandingPadInfo {
0256   MachineBasicBlock *LandingPadBlock;      // Landing pad block.
0257   SmallVector<MCSymbol *, 1> BeginLabels;  // Labels prior to invoke.
0258   SmallVector<MCSymbol *, 1> EndLabels;    // Labels after invoke.
0259   SmallVector<SEHHandler, 1> SEHHandlers;  // SEH handlers active at this lpad.
0260   MCSymbol *LandingPadLabel = nullptr;     // Label at beginning of landing pad.
0261   std::vector<int> TypeIds;                // List of type ids (filters negative).
0262 
0263   explicit LandingPadInfo(MachineBasicBlock *MBB)
0264       : LandingPadBlock(MBB) {}
0265 };
0266 
0267 class LLVM_ABI MachineFunction {
0268   Function &F;
0269   const TargetMachine &Target;
0270   const TargetSubtargetInfo *STI;
0271   MCContext &Ctx;
0272 
0273   // RegInfo - Information about each register in use in the function.
0274   MachineRegisterInfo *RegInfo;
0275 
0276   // Used to keep track of target-specific per-machine-function information for
0277   // the target implementation.
0278   MachineFunctionInfo *MFInfo;
0279 
0280   // Keep track of objects allocated on the stack.
0281   MachineFrameInfo *FrameInfo;
0282 
0283   // Keep track of constants which are spilled to memory
0284   MachineConstantPool *ConstantPool;
0285 
0286   // Keep track of jump tables for switch instructions
0287   MachineJumpTableInfo *JumpTableInfo;
0288 
0289   // Keep track of the function section.
0290   MCSection *Section = nullptr;
0291 
0292   // Catchpad unwind destination info for wasm EH.
0293   // Keeps track of Wasm exception handling related data. This will be null for
0294   // functions that aren't using a wasm EH personality.
0295   WasmEHFuncInfo *WasmEHInfo = nullptr;
0296 
0297   // Keeps track of Windows exception handling related data. This will be null
0298   // for functions that aren't using a funclet-based EH personality.
0299   WinEHFuncInfo *WinEHInfo = nullptr;
0300 
0301   // Function-level unique numbering for MachineBasicBlocks.  When a
0302   // MachineBasicBlock is inserted into a MachineFunction is it automatically
0303   // numbered and this vector keeps track of the mapping from ID's to MBB's.
0304   std::vector<MachineBasicBlock*> MBBNumbering;
0305 
0306   // MBBNumbering epoch, incremented after renumbering to detect use of old
0307   // block numbers.
0308   unsigned MBBNumberingEpoch = 0;
0309 
0310   // Pool-allocate MachineFunction-lifetime and IR objects.
0311   BumpPtrAllocator Allocator;
0312 
0313   // Allocation management for instructions in function.
0314   Recycler<MachineInstr> InstructionRecycler;
0315 
0316   // Allocation management for operand arrays on instructions.
0317   ArrayRecycler<MachineOperand> OperandRecycler;
0318 
0319   // Allocation management for basic blocks in function.
0320   Recycler<MachineBasicBlock> BasicBlockRecycler;
0321 
0322   // List of machine basic blocks in function
0323   using BasicBlockListType = ilist<MachineBasicBlock>;
0324   BasicBlockListType BasicBlocks;
0325 
0326   /// FunctionNumber - This provides a unique ID for each function emitted in
0327   /// this translation unit.
0328   ///
0329   unsigned FunctionNumber;
0330 
0331   /// Alignment - The alignment of the function.
0332   Align Alignment;
0333 
0334   /// ExposesReturnsTwice - True if the function calls setjmp or related
0335   /// functions with attribute "returns twice", but doesn't have
0336   /// the attribute itself.
0337   /// This is used to limit optimizations which cannot reason
0338   /// about the control flow of such functions.
0339   bool ExposesReturnsTwice = false;
0340 
0341   /// True if the function includes any inline assembly.
0342   bool HasInlineAsm = false;
0343 
0344   /// True if any WinCFI instruction have been emitted in this function.
0345   bool HasWinCFI = false;
0346 
0347   /// Current high-level properties of the IR of the function (e.g. is in SSA
0348   /// form or whether registers have been allocated)
0349   MachineFunctionProperties Properties;
0350 
0351   // Allocation management for pseudo source values.
0352   std::unique_ptr<PseudoSourceValueManager> PSVManager;
0353 
0354   /// List of moves done by a function's prolog.  Used to construct frame maps
0355   /// by debug and exception handling consumers.
0356   std::vector<MCCFIInstruction> FrameInstructions;
0357 
0358   /// List of basic blocks immediately following calls to _setjmp. Used to
0359   /// construct a table of valid longjmp targets for Windows Control Flow Guard.
0360   std::vector<MCSymbol *> LongjmpTargets;
0361 
0362   /// List of basic blocks that are the target of catchrets. Used to construct
0363   /// a table of valid targets for Windows EHCont Guard.
0364   std::vector<MCSymbol *> CatchretTargets;
0365 
0366   /// \name Exception Handling
0367   /// \{
0368 
0369   /// List of LandingPadInfo describing the landing pad information.
0370   std::vector<LandingPadInfo> LandingPads;
0371 
0372   /// Map a landing pad's EH symbol to the call site indexes.
0373   DenseMap<MCSymbol*, SmallVector<unsigned, 4>> LPadToCallSiteMap;
0374 
0375   /// Map a landing pad to its index.
0376   DenseMap<const MachineBasicBlock *, unsigned> WasmLPadToIndexMap;
0377 
0378   /// Map of invoke call site index values to associated begin EH_LABEL.
0379   DenseMap<MCSymbol*, unsigned> CallSiteMap;
0380 
0381   /// CodeView label annotations.
0382   std::vector<std::pair<MCSymbol *, MDNode *>> CodeViewAnnotations;
0383 
0384   bool CallsEHReturn = false;
0385   bool CallsUnwindInit = false;
0386   bool HasEHCatchret = false;
0387   bool HasEHScopes = false;
0388   bool HasEHFunclets = false;
0389   bool HasFakeUses = false;
0390   bool IsOutlined = false;
0391 
0392   /// BBID to assign to the next basic block of this function.
0393   unsigned NextBBID = 0;
0394 
0395   /// Section Type for basic blocks, only relevant with basic block sections.
0396   BasicBlockSection BBSectionsType = BasicBlockSection::None;
0397 
0398   /// List of C++ TypeInfo used.
0399   std::vector<const GlobalValue *> TypeInfos;
0400 
0401   /// List of typeids encoding filters used.
0402   std::vector<unsigned> FilterIds;
0403 
0404   /// List of the indices in FilterIds corresponding to filter terminators.
0405   std::vector<unsigned> FilterEnds;
0406 
0407   EHPersonality PersonalityTypeCache = EHPersonality::Unknown;
0408 
0409   /// \}
0410 
0411   /// Clear all the members of this MachineFunction, but the ones used to
0412   /// initialize again the MachineFunction.  More specifically, this deallocates
0413   /// all the dynamically allocated objects and get rids of all the XXXInfo data
0414   /// structure, but keeps unchanged the references to Fn, Target, and
0415   /// FunctionNumber.
0416   void clear();
0417   /// Allocate and initialize the different members.
0418   /// In particular, the XXXInfo data structure.
0419   /// \pre Fn, Target, and FunctionNumber are properly set.
0420   void init();
0421 
0422 public:
0423   /// Description of the location of a variable whose Address is valid and
0424   /// unchanging during function execution. The Address may be:
0425   /// * A stack index, which can be negative for fixed stack objects.
0426   /// * A MCRegister, whose entry value contains the address of the variable.
0427   class VariableDbgInfo {
0428     std::variant<int, MCRegister> Address;
0429 
0430   public:
0431     const DILocalVariable *Var;
0432     const DIExpression *Expr;
0433     const DILocation *Loc;
0434 
0435     VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
0436                     int Slot, const DILocation *Loc)
0437         : Address(Slot), Var(Var), Expr(Expr), Loc(Loc) {}
0438 
0439     VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
0440                     MCRegister EntryValReg, const DILocation *Loc)
0441         : Address(EntryValReg), Var(Var), Expr(Expr), Loc(Loc) {}
0442 
0443     /// Return true if this variable is in a stack slot.
0444     bool inStackSlot() const { return std::holds_alternative<int>(Address); }
0445 
0446     /// Return true if this variable is in the entry value of a register.
0447     bool inEntryValueRegister() const {
0448       return std::holds_alternative<MCRegister>(Address);
0449     }
0450 
0451     /// Returns the stack slot of this variable, assuming `inStackSlot()` is
0452     /// true.
0453     int getStackSlot() const { return std::get<int>(Address); }
0454 
0455     /// Returns the MCRegister of this variable, assuming
0456     /// `inEntryValueRegister()` is true.
0457     MCRegister getEntryValueRegister() const {
0458       return std::get<MCRegister>(Address);
0459     }
0460 
0461     /// Updates the stack slot of this variable, assuming `inStackSlot()` is
0462     /// true.
0463     void updateStackSlot(int NewSlot) {
0464       assert(inStackSlot());
0465       Address = NewSlot;
0466     }
0467   };
0468 
0469   class Delegate {
0470     virtual void anchor();
0471 
0472   public:
0473     virtual ~Delegate() = default;
0474     /// Callback after an insertion. This should not modify the MI directly.
0475     virtual void MF_HandleInsertion(MachineInstr &MI) = 0;
0476     /// Callback before a removal. This should not modify the MI directly.
0477     virtual void MF_HandleRemoval(MachineInstr &MI) = 0;
0478     /// Callback before changing MCInstrDesc. This should not modify the MI
0479     /// directly.
0480     virtual void MF_HandleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID) {
0481     }
0482   };
0483 
0484   /// Structure used to represent pair of argument number after call lowering
0485   /// and register used to transfer that argument.
0486   /// For now we support only cases when argument is transferred through one
0487   /// register.
0488   struct ArgRegPair {
0489     Register Reg;
0490     uint16_t ArgNo;
0491     ArgRegPair(Register R, unsigned Arg) : Reg(R), ArgNo(Arg) {
0492       assert(Arg < (1 << 16) && "Arg out of range");
0493     }
0494   };
0495 
0496   struct CallSiteInfo {
0497     /// Vector of call argument and its forwarding register.
0498     SmallVector<ArgRegPair, 1> ArgRegPairs;
0499   };
0500 
0501   struct CalledGlobalInfo {
0502     const GlobalValue *Callee;
0503     unsigned TargetFlags;
0504   };
0505 
0506 private:
0507   Delegate *TheDelegate = nullptr;
0508   GISelChangeObserver *Observer = nullptr;
0509 
0510   using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;
0511   /// Map a call instruction to call site arguments forwarding info.
0512   CallSiteInfoMap CallSitesInfo;
0513 
0514   /// A helper function that returns call site info for a give call
0515   /// instruction if debug entry value support is enabled.
0516   CallSiteInfoMap::iterator getCallSiteInfo(const MachineInstr *MI);
0517 
0518   using CalledGlobalsMap = DenseMap<const MachineInstr *, CalledGlobalInfo>;
0519   /// Mapping of call instruction to the global value and target flags that it
0520   /// calls, if applicable.
0521   CalledGlobalsMap CalledGlobalsInfo;
0522 
0523   // Callbacks for insertion and removal.
0524   void handleInsertion(MachineInstr &MI);
0525   void handleRemoval(MachineInstr &MI);
0526   friend struct ilist_traits<MachineInstr>;
0527 
0528 public:
0529   // Need to be accessed from MachineInstr::setDesc.
0530   void handleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID);
0531 
0532   using VariableDbgInfoMapTy = SmallVector<VariableDbgInfo, 4>;
0533   VariableDbgInfoMapTy VariableDbgInfos;
0534 
0535   /// A count of how many instructions in the function have had numbers
0536   /// assigned to them. Used for debug value tracking, to determine the
0537   /// next instruction number.
0538   unsigned DebugInstrNumberingCount = 0;
0539 
0540   /// Set value of DebugInstrNumberingCount field. Avoid using this unless
0541   /// you're deserializing this data.
0542   void setDebugInstrNumberingCount(unsigned Num);
0543 
0544   /// Pair of instruction number and operand number.
0545   using DebugInstrOperandPair = std::pair<unsigned, unsigned>;
0546 
0547   /// Replacement definition for a debug instruction reference. Made up of a
0548   /// source instruction / operand pair, destination pair, and a qualifying
0549   /// subregister indicating what bits in the operand make up the substitution.
0550   // For example, a debug user
0551   /// of %1:
0552   ///    %0:gr32 = someinst, debug-instr-number 1
0553   ///    %1:gr16 = %0.some_16_bit_subreg, debug-instr-number 2
0554   /// Would receive the substitution {{2, 0}, {1, 0}, $subreg}, where $subreg is
0555   /// the subregister number for some_16_bit_subreg.
0556   class DebugSubstitution {
0557   public:
0558     DebugInstrOperandPair Src;  ///< Source instruction / operand pair.
0559     DebugInstrOperandPair Dest; ///< Replacement instruction / operand pair.
0560     unsigned Subreg;            ///< Qualifier for which part of Dest is read.
0561 
0562     DebugSubstitution(const DebugInstrOperandPair &Src,
0563                       const DebugInstrOperandPair &Dest, unsigned Subreg)
0564         : Src(Src), Dest(Dest), Subreg(Subreg) {}
0565 
0566     /// Order only by source instruction / operand pair: there should never
0567     /// be duplicate entries for the same source in any collection.
0568     bool operator<(const DebugSubstitution &Other) const {
0569       return Src < Other.Src;
0570     }
0571   };
0572 
0573   /// Debug value substitutions: a collection of DebugSubstitution objects,
0574   /// recording changes in where a value is defined. For example, when one
0575   /// instruction is substituted for another. Keeping a record allows recovery
0576   /// of variable locations after compilation finishes.
0577   SmallVector<DebugSubstitution, 8> DebugValueSubstitutions;
0578 
0579   /// Location of a PHI instruction that is also a debug-info variable value,
0580   /// for the duration of register allocation. Loaded by the PHI-elimination
0581   /// pass, and emitted as DBG_PHI instructions during VirtRegRewriter, with
0582   /// maintenance applied by intermediate passes that edit registers (such as
0583   /// coalescing and the allocator passes).
0584   class DebugPHIRegallocPos {
0585   public:
0586     MachineBasicBlock *MBB; ///< Block where this PHI was originally located.
0587     Register Reg;           ///< VReg where the control-flow-merge happens.
0588     unsigned SubReg;        ///< Optional subreg qualifier within Reg.
0589     DebugPHIRegallocPos(MachineBasicBlock *MBB, Register Reg, unsigned SubReg)
0590         : MBB(MBB), Reg(Reg), SubReg(SubReg) {}
0591   };
0592 
0593   /// Map of debug instruction numbers to the position of their PHI instructions
0594   /// during register allocation. See DebugPHIRegallocPos.
0595   DenseMap<unsigned, DebugPHIRegallocPos> DebugPHIPositions;
0596 
0597   /// Flag for whether this function contains DBG_VALUEs (false) or
0598   /// DBG_INSTR_REF (true).
0599   bool UseDebugInstrRef = false;
0600 
0601   /// Create a substitution between one <instr,operand> value to a different,
0602   /// new value.
0603   void makeDebugValueSubstitution(DebugInstrOperandPair, DebugInstrOperandPair,
0604                                   unsigned SubReg = 0);
0605 
0606   /// Create substitutions for any tracked values in \p Old, to point at
0607   /// \p New. Needed when we re-create an instruction during optimization,
0608   /// which has the same signature (i.e., def operands in the same place) but
0609   /// a modified instruction type, flags, or otherwise. An example: X86 moves
0610   /// are sometimes transformed into equivalent LEAs.
0611   /// If the two instructions are not the same opcode, limit which operands to
0612   /// examine for substitutions to the first N operands by setting
0613   /// \p MaxOperand.
0614   void substituteDebugValuesForInst(const MachineInstr &Old, MachineInstr &New,
0615                                     unsigned MaxOperand = UINT_MAX);
0616 
0617   /// Find the underlying  defining instruction / operand for a COPY instruction
0618   /// while in SSA form. Copies do not actually define values -- they move them
0619   /// between registers. Labelling a COPY-like instruction with an instruction
0620   /// number is to be avoided as it makes value numbers non-unique later in
0621   /// compilation. This method follows the definition chain for any sequence of
0622   /// COPY-like instructions to find whatever non-COPY-like instruction defines
0623   /// the copied value; or for parameters, creates a DBG_PHI on entry.
0624   /// May insert instructions into the entry block!
0625   /// \p MI The copy-like instruction to salvage.
0626   /// \p DbgPHICache A container to cache already-solved COPYs.
0627   /// \returns An instruction/operand pair identifying the defining value.
0628   DebugInstrOperandPair
0629   salvageCopySSA(MachineInstr &MI,
0630                  DenseMap<Register, DebugInstrOperandPair> &DbgPHICache);
0631 
0632   DebugInstrOperandPair salvageCopySSAImpl(MachineInstr &MI);
0633 
0634   /// Finalise any partially emitted debug instructions. These are DBG_INSTR_REF
0635   /// instructions where we only knew the vreg of the value they use, not the
0636   /// instruction that defines that vreg. Once isel finishes, we should have
0637   /// enough information for every DBG_INSTR_REF to point at an instruction
0638   /// (or DBG_PHI).
0639   void finalizeDebugInstrRefs();
0640 
0641   /// Determine whether, in the current machine configuration, we should use
0642   /// instruction referencing or not.
0643   bool shouldUseDebugInstrRef() const;
0644 
0645   /// Returns true if the function's variable locations are tracked with
0646   /// instruction referencing.
0647   bool useDebugInstrRef() const;
0648 
0649   /// Set whether this function will use instruction referencing or not.
0650   void setUseDebugInstrRef(bool UseInstrRef);
0651 
0652   /// A reserved operand number representing the instructions memory operand,
0653   /// for instructions that have a stack spill fused into them.
0654   const static unsigned int DebugOperandMemNumber;
0655 
0656   MachineFunction(Function &F, const TargetMachine &Target,
0657                   const TargetSubtargetInfo &STI, MCContext &Ctx,
0658                   unsigned FunctionNum);
0659   MachineFunction(const MachineFunction &) = delete;
0660   MachineFunction &operator=(const MachineFunction &) = delete;
0661   ~MachineFunction();
0662 
0663   /// Reset the instance as if it was just created.
0664   void reset() {
0665     clear();
0666     init();
0667   }
0668 
0669   /// Reset the currently registered delegate - otherwise assert.
0670   void resetDelegate(Delegate *delegate) {
0671     assert(TheDelegate == delegate &&
0672            "Only the current delegate can perform reset!");
0673     TheDelegate = nullptr;
0674   }
0675 
0676   /// Set the delegate. resetDelegate must be called before attempting
0677   /// to set.
0678   void setDelegate(Delegate *delegate) {
0679     assert(delegate && !TheDelegate &&
0680            "Attempted to set delegate to null, or to change it without "
0681            "first resetting it!");
0682 
0683     TheDelegate = delegate;
0684   }
0685 
0686   void setObserver(GISelChangeObserver *O) { Observer = O; }
0687 
0688   GISelChangeObserver *getObserver() const { return Observer; }
0689 
0690   MCContext &getContext() const { return Ctx; }
0691 
0692   /// Returns the Section this function belongs to.
0693   MCSection *getSection() const { return Section; }
0694 
0695   /// Indicates the Section this function belongs to.
0696   void setSection(MCSection *S) { Section = S; }
0697 
0698   PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }
0699 
0700   /// Return the DataLayout attached to the Module associated to this MF.
0701   const DataLayout &getDataLayout() const;
0702 
0703   /// Return the LLVM function that this machine code represents
0704   Function &getFunction() { return F; }
0705 
0706   /// Return the LLVM function that this machine code represents
0707   const Function &getFunction() const { return F; }
0708 
0709   /// getName - Return the name of the corresponding LLVM function.
0710   StringRef getName() const;
0711 
0712   /// getFunctionNumber - Return a unique ID for the current function.
0713   unsigned getFunctionNumber() const { return FunctionNumber; }
0714 
0715   /// Returns true if this function has basic block sections enabled.
0716   bool hasBBSections() const {
0717     return (BBSectionsType == BasicBlockSection::All ||
0718             BBSectionsType == BasicBlockSection::List ||
0719             BBSectionsType == BasicBlockSection::Preset);
0720   }
0721 
0722   void setBBSectionsType(BasicBlockSection V) { BBSectionsType = V; }
0723 
0724   /// Assign IsBeginSection IsEndSection fields for basic blocks in this
0725   /// function.
0726   void assignBeginEndSections();
0727 
0728   /// getTarget - Return the target machine this machine code is compiled with
0729   const TargetMachine &getTarget() const { return Target; }
0730 
0731   /// getSubtarget - Return the subtarget for which this machine code is being
0732   /// compiled.
0733   const TargetSubtargetInfo &getSubtarget() const { return *STI; }
0734 
0735   /// getSubtarget - This method returns a pointer to the specified type of
0736   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
0737   /// returned is of the correct type.
0738   template<typename STC> const STC &getSubtarget() const {
0739     return *static_cast<const STC *>(STI);
0740   }
0741 
0742   /// getRegInfo - Return information about the registers currently in use.
0743   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
0744   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
0745 
0746   /// getFrameInfo - Return the frame info object for the current function.
0747   /// This object contains information about objects allocated on the stack
0748   /// frame of the current function in an abstract way.
0749   MachineFrameInfo &getFrameInfo() { return *FrameInfo; }
0750   const MachineFrameInfo &getFrameInfo() const { return *FrameInfo; }
0751 
0752   /// getJumpTableInfo - Return the jump table info object for the current
0753   /// function.  This object contains information about jump tables in the
0754   /// current function.  If the current function has no jump tables, this will
0755   /// return null.
0756   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
0757   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
0758 
0759   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
0760   /// does already exist, allocate one.
0761   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
0762 
0763   /// getConstantPool - Return the constant pool object for the current
0764   /// function.
0765   MachineConstantPool *getConstantPool() { return ConstantPool; }
0766   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
0767 
0768   /// getWasmEHFuncInfo - Return information about how the current function uses
0769   /// Wasm exception handling. Returns null for functions that don't use wasm
0770   /// exception handling.
0771   const WasmEHFuncInfo *getWasmEHFuncInfo() const { return WasmEHInfo; }
0772   WasmEHFuncInfo *getWasmEHFuncInfo() { return WasmEHInfo; }
0773 
0774   /// getWinEHFuncInfo - Return information about how the current function uses
0775   /// Windows exception handling. Returns null for functions that don't use
0776   /// funclets for exception handling.
0777   const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; }
0778   WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; }
0779 
0780   /// getAlignment - Return the alignment of the function.
0781   Align getAlignment() const { return Alignment; }
0782 
0783   /// setAlignment - Set the alignment of the function.
0784   void setAlignment(Align A) { Alignment = A; }
0785 
0786   /// ensureAlignment - Make sure the function is at least A bytes aligned.
0787   void ensureAlignment(Align A) {
0788     if (Alignment < A)
0789       Alignment = A;
0790   }
0791 
0792   /// exposesReturnsTwice - Returns true if the function calls setjmp or
0793   /// any other similar functions with attribute "returns twice" without
0794   /// having the attribute itself.
0795   bool exposesReturnsTwice() const {
0796     return ExposesReturnsTwice;
0797   }
0798 
0799   /// setCallsSetJmp - Set a flag that indicates if there's a call to
0800   /// a "returns twice" function.
0801   void setExposesReturnsTwice(bool B) {
0802     ExposesReturnsTwice = B;
0803   }
0804 
0805   /// Returns true if the function contains any inline assembly.
0806   bool hasInlineAsm() const {
0807     return HasInlineAsm;
0808   }
0809 
0810   /// Set a flag that indicates that the function contains inline assembly.
0811   void setHasInlineAsm(bool B) {
0812     HasInlineAsm = B;
0813   }
0814 
0815   bool hasWinCFI() const {
0816     return HasWinCFI;
0817   }
0818   void setHasWinCFI(bool v) { HasWinCFI = v; }
0819 
0820   /// True if this function needs frame moves for debug or exceptions.
0821   bool needsFrameMoves() const;
0822 
0823   /// Get the function properties
0824   const MachineFunctionProperties &getProperties() const { return Properties; }
0825   MachineFunctionProperties &getProperties() { return Properties; }
0826 
0827   /// getInfo - Keep track of various per-function pieces of information for
0828   /// backends that would like to do so.
0829   ///
0830   template<typename Ty>
0831   Ty *getInfo() {
0832     return static_cast<Ty*>(MFInfo);
0833   }
0834 
0835   template<typename Ty>
0836   const Ty *getInfo() const {
0837     return static_cast<const Ty *>(MFInfo);
0838   }
0839 
0840   template <typename Ty> Ty *cloneInfo(const Ty &Old) {
0841     assert(!MFInfo);
0842     MFInfo = Ty::template create<Ty>(Allocator, Old);
0843     return static_cast<Ty *>(MFInfo);
0844   }
0845 
0846   /// Initialize the target specific MachineFunctionInfo
0847   void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI);
0848 
0849   MachineFunctionInfo *cloneInfoFrom(
0850       const MachineFunction &OrigMF,
0851       const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) {
0852     assert(!MFInfo && "new function already has MachineFunctionInfo");
0853     if (!OrigMF.MFInfo)
0854       return nullptr;
0855     return OrigMF.MFInfo->clone(Allocator, *this, Src2DstMBB);
0856   }
0857 
0858   /// Returns the denormal handling type for the default rounding mode of the
0859   /// function.
0860   DenormalMode getDenormalMode(const fltSemantics &FPType) const;
0861 
0862   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
0863   /// are inserted into the machine function.  The block number for a machine
0864   /// basic block can be found by using the MBB::getNumber method, this method
0865   /// provides the inverse mapping.
0866   MachineBasicBlock *getBlockNumbered(unsigned N) const {
0867     assert(N < MBBNumbering.size() && "Illegal block number");
0868     assert(MBBNumbering[N] && "Block was removed from the machine function!");
0869     return MBBNumbering[N];
0870   }
0871 
0872   /// Should we be emitting segmented stack stuff for the function
0873   bool shouldSplitStack() const;
0874 
0875   /// getNumBlockIDs - Return the number of MBB ID's allocated.
0876   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
0877 
0878   /// Return the numbering "epoch" of block numbers, incremented after each
0879   /// numbering. Intended for asserting that no renumbering was performed when
0880   /// used by, e.g., preserved analyses.
0881   unsigned getBlockNumberEpoch() const { return MBBNumberingEpoch; }
0882 
0883   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
0884   /// recomputes them.  This guarantees that the MBB numbers are sequential,
0885   /// dense, and match the ordering of the blocks within the function.  If a
0886   /// specific MachineBasicBlock is specified, only that block and those after
0887   /// it are renumbered.
0888   void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
0889 
0890   /// Return an estimate of the function's code size,
0891   /// taking into account block and function alignment
0892   int64_t estimateFunctionSizeInBytes();
0893 
0894   /// print - Print out the MachineFunction in a format suitable for debugging
0895   /// to the specified stream.
0896   void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
0897 
0898   /// viewCFG - This function is meant for use from the debugger.  You can just
0899   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
0900   /// program, displaying the CFG of the current function with the code for each
0901   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
0902   /// in your path.
0903   void viewCFG() const;
0904 
0905   /// viewCFGOnly - This function is meant for use from the debugger.  It works
0906   /// just like viewCFG, but it does not include the contents of basic blocks
0907   /// into the nodes, just the label.  If you are only interested in the CFG
0908   /// this can make the graph smaller.
0909   ///
0910   void viewCFGOnly() const;
0911 
0912   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
0913   void dump() const;
0914 
0915   /// Run the current MachineFunction through the machine code verifier, useful
0916   /// for debugger use.
0917   /// \returns true if no problems were found.
0918   bool verify(Pass *p = nullptr, const char *Banner = nullptr,
0919               raw_ostream *OS = nullptr, bool AbortOnError = true) const;
0920 
0921   /// Run the current MachineFunction through the machine code verifier, useful
0922   /// for debugger use.
0923   /// \returns true if no problems were found.
0924   bool verify(LiveIntervals *LiveInts, SlotIndexes *Indexes,
0925               const char *Banner = nullptr, raw_ostream *OS = nullptr,
0926               bool AbortOnError = true) const;
0927 
0928   // Provide accessors for the MachineBasicBlock list...
0929   using iterator = BasicBlockListType::iterator;
0930   using const_iterator = BasicBlockListType::const_iterator;
0931   using const_reverse_iterator = BasicBlockListType::const_reverse_iterator;
0932   using reverse_iterator = BasicBlockListType::reverse_iterator;
0933 
0934   /// Support for MachineBasicBlock::getNextNode().
0935   static BasicBlockListType MachineFunction::*
0936   getSublistAccess(MachineBasicBlock *) {
0937     return &MachineFunction::BasicBlocks;
0938   }
0939 
0940   /// addLiveIn - Add the specified physical register as a live-in value and
0941   /// create a corresponding virtual register for it.
0942   Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC);
0943 
0944   //===--------------------------------------------------------------------===//
0945   // BasicBlock accessor functions.
0946   //
0947   iterator                 begin()       { return BasicBlocks.begin(); }
0948   const_iterator           begin() const { return BasicBlocks.begin(); }
0949   iterator                 end  ()       { return BasicBlocks.end();   }
0950   const_iterator           end  () const { return BasicBlocks.end();   }
0951 
0952   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
0953   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
0954   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
0955   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
0956 
0957   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
0958   bool                     empty() const { return BasicBlocks.empty(); }
0959   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
0960         MachineBasicBlock &front()       { return BasicBlocks.front(); }
0961   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
0962         MachineBasicBlock & back()       { return BasicBlocks.back(); }
0963 
0964   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
0965   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
0966   void insert(iterator MBBI, MachineBasicBlock *MBB) {
0967     BasicBlocks.insert(MBBI, MBB);
0968   }
0969   void splice(iterator InsertPt, iterator MBBI) {
0970     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
0971   }
0972   void splice(iterator InsertPt, MachineBasicBlock *MBB) {
0973     BasicBlocks.splice(InsertPt, BasicBlocks, MBB);
0974   }
0975   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
0976     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
0977   }
0978 
0979   void remove(iterator MBBI) { BasicBlocks.remove(MBBI); }
0980   void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(MBBI); }
0981   void erase(iterator MBBI) { BasicBlocks.erase(MBBI); }
0982   void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(MBBI); }
0983 
0984   template <typename Comp>
0985   void sort(Comp comp) {
0986     BasicBlocks.sort(comp);
0987   }
0988 
0989   /// Return the number of \p MachineInstrs in this \p MachineFunction.
0990   unsigned getInstructionCount() const {
0991     unsigned InstrCount = 0;
0992     for (const MachineBasicBlock &MBB : BasicBlocks)
0993       InstrCount += MBB.size();
0994     return InstrCount;
0995   }
0996 
0997   //===--------------------------------------------------------------------===//
0998   // Internal functions used to automatically number MachineBasicBlocks
0999 
1000   /// Adds the MBB to the internal numbering. Returns the unique number
1001   /// assigned to the MBB.
1002   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
1003     MBBNumbering.push_back(MBB);
1004     return (unsigned)MBBNumbering.size()-1;
1005   }
1006 
1007   /// removeFromMBBNumbering - Remove the specific machine basic block from our
1008   /// tracker, this is only really to be used by the MachineBasicBlock
1009   /// implementation.
1010   void removeFromMBBNumbering(unsigned N) {
1011     assert(N < MBBNumbering.size() && "Illegal basic block #");
1012     MBBNumbering[N] = nullptr;
1013   }
1014 
1015   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
1016   /// of `new MachineInstr'.
1017   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, DebugLoc DL,
1018                                    bool NoImplicit = false);
1019 
1020   /// Create a new MachineInstr which is a copy of \p Orig, identical in all
1021   /// ways except the instruction has no parent, prev, or next. Bundling flags
1022   /// are reset.
1023   ///
1024   /// Note: Clones a single instruction, not whole instruction bundles.
1025   /// Does not perform target specific adjustments; consider using
1026   /// TargetInstrInfo::duplicate() instead.
1027   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
1028 
1029   /// Clones instruction or the whole instruction bundle \p Orig and insert
1030   /// into \p MBB before \p InsertBefore.
1031   ///
1032   /// Note: Does not perform target specific adjustments; consider using
1033   /// TargetInstrInfo::duplicate() instead.
1034   MachineInstr &
1035   cloneMachineInstrBundle(MachineBasicBlock &MBB,
1036                           MachineBasicBlock::iterator InsertBefore,
1037                           const MachineInstr &Orig);
1038 
1039   /// DeleteMachineInstr - Delete the given MachineInstr.
1040   void deleteMachineInstr(MachineInstr *MI);
1041 
1042   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
1043   /// instead of `new MachineBasicBlock'. Sets `MachineBasicBlock::BBID` if
1044   /// basic-block-sections is enabled for the function.
1045   MachineBasicBlock *
1046   CreateMachineBasicBlock(const BasicBlock *BB = nullptr,
1047                           std::optional<UniqueBBID> BBID = std::nullopt);
1048 
1049   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
1050   void deleteMachineBasicBlock(MachineBasicBlock *MBB);
1051 
1052   /// getMachineMemOperand - Allocate a new MachineMemOperand.
1053   /// MachineMemOperands are owned by the MachineFunction and need not be
1054   /// explicitly deallocated.
1055   MachineMemOperand *getMachineMemOperand(
1056       MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy,
1057       Align base_alignment, const AAMDNodes &AAInfo = AAMDNodes(),
1058       const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1059       AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1060       AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1061   MachineMemOperand *getMachineMemOperand(
1062       MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LocationSize Size,
1063       Align BaseAlignment, const AAMDNodes &AAInfo = AAMDNodes(),
1064       const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1065       AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1066       AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1067   MachineMemOperand *getMachineMemOperand(
1068       MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, uint64_t Size,
1069       Align BaseAlignment, const AAMDNodes &AAInfo = AAMDNodes(),
1070       const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1071       AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1072       AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic) {
1073     return getMachineMemOperand(PtrInfo, F, LocationSize::precise(Size),
1074                                 BaseAlignment, AAInfo, Ranges, SSID, Ordering,
1075                                 FailureOrdering);
1076   }
1077 
1078   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1079   /// an existing one, adjusting by an offset and using the given size.
1080   /// MachineMemOperands are owned by the MachineFunction and need not be
1081   /// explicitly deallocated.
1082   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1083                                           int64_t Offset, LLT Ty);
1084   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1085                                           int64_t Offset, LocationSize Size) {
1086     return getMachineMemOperand(
1087         MMO, Offset,
1088         !Size.hasValue() ? LLT()
1089         : Size.isScalable()
1090             ? LLT::scalable_vector(1, 8 * Size.getValue().getKnownMinValue())
1091             : LLT::scalar(8 * Size.getValue().getKnownMinValue()));
1092   }
1093   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1094                                           int64_t Offset, uint64_t Size) {
1095     return getMachineMemOperand(MMO, Offset, LocationSize::precise(Size));
1096   }
1097 
1098   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1099   /// an existing one, replacing only the MachinePointerInfo and size.
1100   /// MachineMemOperands are owned by the MachineFunction and need not be
1101   /// explicitly deallocated.
1102   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1103                                           const MachinePointerInfo &PtrInfo,
1104                                           LocationSize Size);
1105   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1106                                           const MachinePointerInfo &PtrInfo,
1107                                           LLT Ty);
1108   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1109                                           const MachinePointerInfo &PtrInfo,
1110                                           uint64_t Size) {
1111     return getMachineMemOperand(MMO, PtrInfo, LocationSize::precise(Size));
1112   }
1113 
1114   /// Allocate a new MachineMemOperand by copying an existing one,
1115   /// replacing only AliasAnalysis information. MachineMemOperands are owned
1116   /// by the MachineFunction and need not be explicitly deallocated.
1117   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1118                                           const AAMDNodes &AAInfo);
1119 
1120   /// Allocate a new MachineMemOperand by copying an existing one,
1121   /// replacing the flags. MachineMemOperands are owned
1122   /// by the MachineFunction and need not be explicitly deallocated.
1123   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1124                                           MachineMemOperand::Flags Flags);
1125 
1126   using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity;
1127 
1128   /// Allocate an array of MachineOperands. This is only intended for use by
1129   /// internal MachineInstr functions.
1130   MachineOperand *allocateOperandArray(OperandCapacity Cap) {
1131     return OperandRecycler.allocate(Cap, Allocator);
1132   }
1133 
1134   /// Dellocate an array of MachineOperands and recycle the memory. This is
1135   /// only intended for use by internal MachineInstr functions.
1136   /// Cap must be the same capacity that was used to allocate the array.
1137   void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
1138     OperandRecycler.deallocate(Cap, Array);
1139   }
1140 
1141   /// Allocate and initialize a register mask with @p NumRegister bits.
1142   uint32_t *allocateRegMask();
1143 
1144   ArrayRef<int> allocateShuffleMask(ArrayRef<int> Mask);
1145 
1146   /// Allocate and construct an extra info structure for a `MachineInstr`.
1147   ///
1148   /// This is allocated on the function's allocator and so lives the life of
1149   /// the function.
1150   MachineInstr::ExtraInfo *createMIExtraInfo(
1151       ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol = nullptr,
1152       MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr,
1153       MDNode *PCSections = nullptr, uint32_t CFIType = 0,
1154       MDNode *MMRAs = nullptr);
1155 
1156   /// Allocate a string and populate it with the given external symbol name.
1157   const char *createExternalSymbolName(StringRef Name);
1158 
1159   //===--------------------------------------------------------------------===//
1160   // Label Manipulation.
1161 
1162   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
1163   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
1164   /// normal 'L' label is returned.
1165   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
1166                          bool isLinkerPrivate = false) const;
1167 
1168   /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
1169   /// base.
1170   MCSymbol *getPICBaseSymbol() const;
1171 
1172   /// Returns a reference to a list of cfi instructions in the function's
1173   /// prologue.  Used to construct frame maps for debug and exception handling
1174   /// comsumers.
1175   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
1176     return FrameInstructions;
1177   }
1178 
1179   [[nodiscard]] unsigned addFrameInst(const MCCFIInstruction &Inst);
1180 
1181   /// Returns a reference to a list of symbols immediately following calls to
1182   /// _setjmp in the function. Used to construct the longjmp target table used
1183   /// by Windows Control Flow Guard.
1184   const std::vector<MCSymbol *> &getLongjmpTargets() const {
1185     return LongjmpTargets;
1186   }
1187 
1188   /// Add the specified symbol to the list of valid longjmp targets for Windows
1189   /// Control Flow Guard.
1190   void addLongjmpTarget(MCSymbol *Target) { LongjmpTargets.push_back(Target); }
1191 
1192   /// Returns a reference to a list of symbols that we have catchrets.
1193   /// Used to construct the catchret target table used by Windows EHCont Guard.
1194   const std::vector<MCSymbol *> &getCatchretTargets() const {
1195     return CatchretTargets;
1196   }
1197 
1198   /// Add the specified symbol to the list of valid catchret targets for Windows
1199   /// EHCont Guard.
1200   void addCatchretTarget(MCSymbol *Target) {
1201     CatchretTargets.push_back(Target);
1202   }
1203 
1204   /// Tries to get the global and target flags for a call site, if the
1205   /// instruction is a call to a global.
1206   CalledGlobalInfo tryGetCalledGlobal(const MachineInstr *MI) const {
1207     return CalledGlobalsInfo.lookup(MI);
1208   }
1209 
1210   /// Notes the global and target flags for a call site.
1211   void addCalledGlobal(const MachineInstr *MI, CalledGlobalInfo Details) {
1212     assert(MI && "MI must not be null");
1213     assert(Details.Callee && "Global must not be null");
1214     CalledGlobalsInfo.insert({MI, Details});
1215   }
1216 
1217   /// Iterates over the full set of call sites and their associated globals.
1218   auto getCalledGlobals() const {
1219     return llvm::make_range(CalledGlobalsInfo.begin(), CalledGlobalsInfo.end());
1220   }
1221 
1222   /// \name Exception Handling
1223   /// \{
1224 
1225   bool callsEHReturn() const { return CallsEHReturn; }
1226   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
1227 
1228   bool callsUnwindInit() const { return CallsUnwindInit; }
1229   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
1230 
1231   bool hasEHCatchret() const { return HasEHCatchret; }
1232   void setHasEHCatchret(bool V) { HasEHCatchret = V; }
1233 
1234   bool hasEHScopes() const { return HasEHScopes; }
1235   void setHasEHScopes(bool V) { HasEHScopes = V; }
1236 
1237   bool hasEHFunclets() const { return HasEHFunclets; }
1238   void setHasEHFunclets(bool V) { HasEHFunclets = V; }
1239 
1240   bool hasFakeUses() const { return HasFakeUses; }
1241   void setHasFakeUses(bool V) { HasFakeUses = V; }
1242 
1243   bool isOutlined() const { return IsOutlined; }
1244   void setIsOutlined(bool V) { IsOutlined = V; }
1245 
1246   /// Find or create an LandingPadInfo for the specified MachineBasicBlock.
1247   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1248 
1249   /// Return a reference to the landing pad info for the current function.
1250   const std::vector<LandingPadInfo> &getLandingPads() const {
1251     return LandingPads;
1252   }
1253 
1254   /// Provide the begin and end labels of an invoke style call and associate it
1255   /// with a try landing pad block.
1256   void addInvoke(MachineBasicBlock *LandingPad,
1257                  MCSymbol *BeginLabel, MCSymbol *EndLabel);
1258 
1259   /// Add a new panding pad, and extract the exception handling information from
1260   /// the landingpad instruction. Returns the label ID for the landing pad
1261   /// entry.
1262   MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
1263 
1264   /// Return the type id for the specified typeinfo.  This is function wide.
1265   unsigned getTypeIDFor(const GlobalValue *TI);
1266 
1267   /// Return the id of the filter encoded by TyIds.  This is function wide.
1268   int getFilterIDFor(ArrayRef<unsigned> TyIds);
1269 
1270   /// Map the landing pad's EH symbol to the call site indexes.
1271   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
1272 
1273   /// Return if there is any wasm exception handling.
1274   bool hasAnyWasmLandingPadIndex() const {
1275     return !WasmLPadToIndexMap.empty();
1276   }
1277 
1278   /// Map the landing pad to its index. Used for Wasm exception handling.
1279   void setWasmLandingPadIndex(const MachineBasicBlock *LPad, unsigned Index) {
1280     WasmLPadToIndexMap[LPad] = Index;
1281   }
1282 
1283   /// Returns true if the landing pad has an associate index in wasm EH.
1284   bool hasWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1285     return WasmLPadToIndexMap.count(LPad);
1286   }
1287 
1288   /// Get the index in wasm EH for a given landing pad.
1289   unsigned getWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1290     assert(hasWasmLandingPadIndex(LPad));
1291     return WasmLPadToIndexMap.lookup(LPad);
1292   }
1293 
1294   bool hasAnyCallSiteLandingPad() const {
1295     return !LPadToCallSiteMap.empty();
1296   }
1297 
1298   /// Get the call site indexes for a landing pad EH symbol.
1299   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
1300     assert(hasCallSiteLandingPad(Sym) &&
1301            "missing call site number for landing pad!");
1302     return LPadToCallSiteMap[Sym];
1303   }
1304 
1305   /// Return true if the landing pad Eh symbol has an associated call site.
1306   bool hasCallSiteLandingPad(MCSymbol *Sym) {
1307     return !LPadToCallSiteMap[Sym].empty();
1308   }
1309 
1310   bool hasAnyCallSiteLabel() const {
1311     return !CallSiteMap.empty();
1312   }
1313 
1314   /// Map the begin label for a call site.
1315   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
1316     CallSiteMap[BeginLabel] = Site;
1317   }
1318 
1319   /// Get the call site number for a begin label.
1320   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1321     assert(hasCallSiteBeginLabel(BeginLabel) &&
1322            "Missing call site number for EH_LABEL!");
1323     return CallSiteMap.lookup(BeginLabel);
1324   }
1325 
1326   /// Return true if the begin label has a call site number associated with it.
1327   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1328     return CallSiteMap.count(BeginLabel);
1329   }
1330 
1331   /// Record annotations associated with a particular label.
1332   void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD) {
1333     CodeViewAnnotations.push_back({Label, MD});
1334   }
1335 
1336   ArrayRef<std::pair<MCSymbol *, MDNode *>> getCodeViewAnnotations() const {
1337     return CodeViewAnnotations;
1338   }
1339 
1340   /// Return a reference to the C++ typeinfo for the current function.
1341   const std::vector<const GlobalValue *> &getTypeInfos() const {
1342     return TypeInfos;
1343   }
1344 
1345   /// Return a reference to the typeids encoding filters used in the current
1346   /// function.
1347   const std::vector<unsigned> &getFilterIds() const {
1348     return FilterIds;
1349   }
1350 
1351   /// \}
1352 
1353   /// Collect information used to emit debugging information of a variable in a
1354   /// stack slot.
1355   void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1356                           int Slot, const DILocation *Loc) {
1357     VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
1358   }
1359 
1360   /// Collect information used to emit debugging information of a variable in
1361   /// the entry value of a register.
1362   void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1363                           MCRegister Reg, const DILocation *Loc) {
1364     VariableDbgInfos.emplace_back(Var, Expr, Reg, Loc);
1365   }
1366 
1367   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
1368   const VariableDbgInfoMapTy &getVariableDbgInfo() const {
1369     return VariableDbgInfos;
1370   }
1371 
1372   /// Returns the collection of variables for which we have debug info and that
1373   /// have been assigned a stack slot.
1374   auto getInStackSlotVariableDbgInfo() {
1375     return make_filter_range(getVariableDbgInfo(), [](auto &VarInfo) {
1376       return VarInfo.inStackSlot();
1377     });
1378   }
1379 
1380   /// Returns the collection of variables for which we have debug info and that
1381   /// have been assigned a stack slot.
1382   auto getInStackSlotVariableDbgInfo() const {
1383     return make_filter_range(getVariableDbgInfo(), [](const auto &VarInfo) {
1384       return VarInfo.inStackSlot();
1385     });
1386   }
1387 
1388   /// Returns the collection of variables for which we have debug info and that
1389   /// have been assigned an entry value register.
1390   auto getEntryValueVariableDbgInfo() const {
1391     return make_filter_range(getVariableDbgInfo(), [](const auto &VarInfo) {
1392       return VarInfo.inEntryValueRegister();
1393     });
1394   }
1395 
1396   /// Start tracking the arguments passed to the call \p CallI.
1397   void addCallSiteInfo(const MachineInstr *CallI, CallSiteInfo &&CallInfo) {
1398     assert(CallI->isCandidateForAdditionalCallInfo());
1399     bool Inserted =
1400         CallSitesInfo.try_emplace(CallI, std::move(CallInfo)).second;
1401     (void)Inserted;
1402     assert(Inserted && "Call site info not unique");
1403   }
1404 
1405   const CallSiteInfoMap &getCallSitesInfo() const {
1406     return CallSitesInfo;
1407   }
1408 
1409   /// Following functions update call site info. They should be called before
1410   /// removing, replacing or copying call instruction.
1411 
1412   /// Erase the call site info for \p MI. It is used to remove a call
1413   /// instruction from the instruction stream.
1414   void eraseAdditionalCallInfo(const MachineInstr *MI);
1415   /// Copy the call site info from \p Old to \ New. Its usage is when we are
1416   /// making a copy of the instruction that will be inserted at different point
1417   /// of the instruction stream.
1418   void copyAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New);
1419 
1420   /// Move the call site info from \p Old to \New call site info. This function
1421   /// is used when we are replacing one call instruction with another one to
1422   /// the same callee.
1423   void moveAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New);
1424 
1425   unsigned getNewDebugInstrNum() {
1426     return ++DebugInstrNumberingCount;
1427   }
1428 };
1429 
1430 //===--------------------------------------------------------------------===//
1431 // GraphTraits specializations for function basic block graphs (CFGs)
1432 //===--------------------------------------------------------------------===//
1433 
1434 // Provide specializations of GraphTraits to be able to treat a
1435 // machine function as a graph of machine basic blocks... these are
1436 // the same as the machine basic block iterators, except that the root
1437 // node is implicitly the first node of the function.
1438 //
1439 template <> struct GraphTraits<MachineFunction*> :
1440   public GraphTraits<MachineBasicBlock*> {
1441   static NodeRef getEntryNode(MachineFunction *F) { return &F->front(); }
1442 
1443   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1444   using nodes_iterator = pointer_iterator<MachineFunction::iterator>;
1445 
1446   static nodes_iterator nodes_begin(MachineFunction *F) {
1447     return nodes_iterator(F->begin());
1448   }
1449 
1450   static nodes_iterator nodes_end(MachineFunction *F) {
1451     return nodes_iterator(F->end());
1452   }
1453 
1454   static unsigned       size       (MachineFunction *F) { return F->size(); }
1455 
1456   static unsigned getMaxNumber(MachineFunction *F) {
1457     return F->getNumBlockIDs();
1458   }
1459   static unsigned getNumberEpoch(MachineFunction *F) {
1460     return F->getBlockNumberEpoch();
1461   }
1462 };
1463 template <> struct GraphTraits<const MachineFunction*> :
1464   public GraphTraits<const MachineBasicBlock*> {
1465   static NodeRef getEntryNode(const MachineFunction *F) { return &F->front(); }
1466 
1467   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1468   using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
1469 
1470   static nodes_iterator nodes_begin(const MachineFunction *F) {
1471     return nodes_iterator(F->begin());
1472   }
1473 
1474   static nodes_iterator nodes_end  (const MachineFunction *F) {
1475     return nodes_iterator(F->end());
1476   }
1477 
1478   static unsigned       size       (const MachineFunction *F)  {
1479     return F->size();
1480   }
1481 
1482   static unsigned getMaxNumber(const MachineFunction *F) {
1483     return F->getNumBlockIDs();
1484   }
1485   static unsigned getNumberEpoch(const MachineFunction *F) {
1486     return F->getBlockNumberEpoch();
1487   }
1488 };
1489 
1490 // Provide specializations of GraphTraits to be able to treat a function as a
1491 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
1492 // a function is considered to be when traversing the predecessor edges of a BB
1493 // instead of the successor edges.
1494 //
1495 template <> struct GraphTraits<Inverse<MachineFunction*>> :
1496   public GraphTraits<Inverse<MachineBasicBlock*>> {
1497   static NodeRef getEntryNode(Inverse<MachineFunction *> G) {
1498     return &G.Graph->front();
1499   }
1500 
1501   static unsigned getMaxNumber(MachineFunction *F) {
1502     return F->getNumBlockIDs();
1503   }
1504   static unsigned getNumberEpoch(MachineFunction *F) {
1505     return F->getBlockNumberEpoch();
1506   }
1507 };
1508 template <> struct GraphTraits<Inverse<const MachineFunction*>> :
1509   public GraphTraits<Inverse<const MachineBasicBlock*>> {
1510   static NodeRef getEntryNode(Inverse<const MachineFunction *> G) {
1511     return &G.Graph->front();
1512   }
1513 
1514   static unsigned getMaxNumber(const MachineFunction *F) {
1515     return F->getNumBlockIDs();
1516   }
1517   static unsigned getNumberEpoch(const MachineFunction *F) {
1518     return F->getBlockNumberEpoch();
1519   }
1520 };
1521 
1522 void verifyMachineFunction(const std::string &Banner,
1523                            const MachineFunction &MF);
1524 
1525 } // end namespace llvm
1526 
1527 #endif // LLVM_CODEGEN_MACHINEFUNCTION_H