1 //===- llvm/CodeGen/MachineFunction.h ---------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Collect native machine code for a function.  This class contains a list of
10 // MachineBasicBlock instances that make up the current compiled function.
11 //
12 // This class also contains pointers to various classes which hold
13 // target-specific information about the generated code.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
18 #define LLVM_CODEGEN_MACHINEFUNCTION_H
19 
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/ilist.h"
26 #include "llvm/ADT/iterator.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/IR/EHPersonalities.h"
31 #include "llvm/Support/Allocator.h"
32 #include "llvm/Support/ArrayRecycler.h"
33 #include "llvm/Support/AtomicOrdering.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Recycler.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include <cassert>
38 #include <cstdint>
39 #include <memory>
40 #include <utility>
41 #include <variant>
42 #include <vector>
43 
44 namespace llvm {
45 
46 class BasicBlock;
47 class BlockAddress;
48 class DataLayout;
49 class DebugLoc;
50 struct DenormalMode;
51 class DIExpression;
52 class DILocalVariable;
53 class DILocation;
54 class Function;
55 class GISelChangeObserver;
56 class GlobalValue;
57 class LLVMTargetMachine;
58 class MachineConstantPool;
59 class MachineFrameInfo;
60 class MachineFunction;
61 class MachineJumpTableInfo;
62 class MachineModuleInfo;
63 class MachineRegisterInfo;
64 class MCContext;
65 class MCInstrDesc;
66 class MCSymbol;
67 class MCSection;
68 class Pass;
69 class PseudoSourceValueManager;
70 class raw_ostream;
71 class SlotIndexes;
72 class StringRef;
73 class TargetRegisterClass;
74 class TargetSubtargetInfo;
75 struct WasmEHFuncInfo;
76 struct WinEHFuncInfo;
77 
78 template <> struct ilist_alloc_traits<MachineBasicBlock> {
79   void deleteNode(MachineBasicBlock *MBB);
80 };
81 
82 template <> struct ilist_callback_traits<MachineBasicBlock> {
83   void addNodeToList(MachineBasicBlock* N);
84   void removeNodeFromList(MachineBasicBlock* N);
85 
86   template <class Iterator>
87   void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator) {
88     assert(this == &OldList && "never transfer MBBs between functions");
89   }
90 };
91 
92 /// MachineFunctionInfo - This class can be derived from and used by targets to
93 /// hold private target-specific information for each MachineFunction.  Objects
94 /// of type are accessed/created with MF::getInfo and destroyed when the
95 /// MachineFunction is destroyed.
96 struct MachineFunctionInfo {
97   virtual ~MachineFunctionInfo();
98 
99   /// Factory function: default behavior is to call new using the
100   /// supplied allocator.
101   ///
102   /// This function can be overridden in a derive class.
103   template <typename FuncInfoTy, typename SubtargetTy = TargetSubtargetInfo>
104   static FuncInfoTy *create(BumpPtrAllocator &Allocator, const Function &F,
105                             const SubtargetTy *STI) {
106     return new (Allocator.Allocate<FuncInfoTy>()) FuncInfoTy(F, STI);
107   }
108 
109   template <typename Ty>
110   static Ty *create(BumpPtrAllocator &Allocator, const Ty &MFI) {
111     return new (Allocator.Allocate<Ty>()) Ty(MFI);
112   }
113 
114   /// Make a functionally equivalent copy of this MachineFunctionInfo in \p MF.
115   /// This requires remapping MachineBasicBlock references from the original
116   /// parent to values in the new function. Targets may assume that virtual
117   /// register and frame index values are preserved in the new function.
118   virtual MachineFunctionInfo *
119   clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
120         const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
121       const {
122     return nullptr;
123   }
124 };
125 
126 /// Properties which a MachineFunction may have at a given point in time.
127 /// Each of these has checking code in the MachineVerifier, and passes can
128 /// require that a property be set.
129 class MachineFunctionProperties {
130   // Possible TODO: Allow targets to extend this (perhaps by allowing the
131   // constructor to specify the size of the bit vector)
132   // Possible TODO: Allow requiring the negative (e.g. VRegsAllocated could be
133   // stated as the negative of "has vregs"
134 
135 public:
136   // The properties are stated in "positive" form; i.e. a pass could require
137   // that the property hold, but not that it does not hold.
138 
139   // Property descriptions:
140   // IsSSA: True when the machine function is in SSA form and virtual registers
141   //  have a single def.
142   // NoPHIs: The machine function does not contain any PHI instruction.
143   // TracksLiveness: True when tracking register liveness accurately.
144   //  While this property is set, register liveness information in basic block
145   //  live-in lists and machine instruction operands (e.g. implicit defs) is
146   //  accurate, kill flags are conservatively accurate (kill flag correctly
147   //  indicates the last use of a register, an operand without kill flag may or
148   //  may not be the last use of a register). This means it can be used to
149   //  change the code in ways that affect the values in registers, for example
150   //  by the register scavenger.
151   //  When this property is cleared at a very late time, liveness is no longer
152   //  reliable.
153   // NoVRegs: The machine function does not use any virtual registers.
154   // Legalized: In GlobalISel: the MachineLegalizer ran and all pre-isel generic
155   //  instructions have been legalized; i.e., all instructions are now one of:
156   //   - generic and always legal (e.g., COPY)
157   //   - target-specific
158   //   - legal pre-isel generic instructions.
159   // RegBankSelected: In GlobalISel: the RegBankSelect pass ran and all generic
160   //  virtual registers have been assigned to a register bank.
161   // Selected: In GlobalISel: the InstructionSelect pass ran and all pre-isel
162   //  generic instructions have been eliminated; i.e., all instructions are now
163   //  target-specific or non-pre-isel generic instructions (e.g., COPY).
164   //  Since only pre-isel generic instructions can have generic virtual register
165   //  operands, this also means that all generic virtual registers have been
166   //  constrained to virtual registers (assigned to register classes) and that
167   //  all sizes attached to them have been eliminated.
168   // TiedOpsRewritten: The twoaddressinstruction pass will set this flag, it
169   //  means that tied-def have been rewritten to meet the RegConstraint.
170   // FailsVerification: Means that the function is not expected to pass machine
171   //  verification. This can be set by passes that introduce known problems that
172   //  have not been fixed yet.
173   // TracksDebugUserValues: Without this property enabled, debug instructions
174   // such as DBG_VALUE are allowed to reference virtual registers even if those
175   // registers do not have a definition. With the property enabled virtual
176   // registers must only be used if they have a definition. This property
177   // allows earlier passes in the pipeline to skip updates of `DBG_VALUE`
178   // instructions to save compile time.
179   enum class Property : unsigned {
180     IsSSA,
181     NoPHIs,
182     TracksLiveness,
183     NoVRegs,
184     FailedISel,
185     Legalized,
186     RegBankSelected,
187     Selected,
188     TiedOpsRewritten,
189     FailsVerification,
190     TracksDebugUserValues,
191     LastProperty = TracksDebugUserValues,
192   };
193 
194   bool hasProperty(Property P) const {
195     return Properties[static_cast<unsigned>(P)];
196   }
197 
198   MachineFunctionProperties &set(Property P) {
199     Properties.set(static_cast<unsigned>(P));
200     return *this;
201   }
202 
203   MachineFunctionProperties &reset(Property P) {
204     Properties.reset(static_cast<unsigned>(P));
205     return *this;
206   }
207 
208   /// Reset all the properties.
209   MachineFunctionProperties &reset() {
210     Properties.reset();
211     return *this;
212   }
213 
214   MachineFunctionProperties &set(const MachineFunctionProperties &MFP) {
215     Properties |= MFP.Properties;
216     return *this;
217   }
218 
219   MachineFunctionProperties &reset(const MachineFunctionProperties &MFP) {
220     Properties.reset(MFP.Properties);
221     return *this;
222   }
223 
224   // Returns true if all properties set in V (i.e. required by a pass) are set
225   // in this.
226   bool verifyRequiredProperties(const MachineFunctionProperties &V) const {
227     return !V.Properties.test(Properties);
228   }
229 
230   /// Print the MachineFunctionProperties in human-readable form.
231   void print(raw_ostream &OS) const;
232 
233 private:
234   BitVector Properties =
235       BitVector(static_cast<unsigned>(Property::LastProperty)+1);
236 };
237 
238 struct SEHHandler {
239   /// Filter or finally function. Null indicates a catch-all.
240   const Function *FilterOrFinally;
241 
242   /// Address of block to recover at. Null for a finally handler.
243   const BlockAddress *RecoverBA;
244 };
245 
246 /// This structure is used to retain landing pad info for the current function.
247 struct LandingPadInfo {
248   MachineBasicBlock *LandingPadBlock;      // Landing pad block.
249   SmallVector<MCSymbol *, 1> BeginLabels;  // Labels prior to invoke.
250   SmallVector<MCSymbol *, 1> EndLabels;    // Labels after invoke.
251   SmallVector<SEHHandler, 1> SEHHandlers;  // SEH handlers active at this lpad.
252   MCSymbol *LandingPadLabel = nullptr;     // Label at beginning of landing pad.
253   std::vector<int> TypeIds;                // List of type ids (filters negative).
254 
255   explicit LandingPadInfo(MachineBasicBlock *MBB)
256       : LandingPadBlock(MBB) {}
257 };
258 
259 class LLVM_EXTERNAL_VISIBILITY MachineFunction {
260   Function &F;
261   const LLVMTargetMachine &Target;
262   const TargetSubtargetInfo *STI;
263   MCContext &Ctx;
264   MachineModuleInfo &MMI;
265 
266   // RegInfo - Information about each register in use in the function.
267   MachineRegisterInfo *RegInfo;
268 
269   // Used to keep track of target-specific per-machine function information for
270   // the target implementation.
271   MachineFunctionInfo *MFInfo;
272 
273   // Keep track of objects allocated on the stack.
274   MachineFrameInfo *FrameInfo;
275 
276   // Keep track of constants which are spilled to memory
277   MachineConstantPool *ConstantPool;
278 
279   // Keep track of jump tables for switch instructions
280   MachineJumpTableInfo *JumpTableInfo;
281 
282   // Keep track of the function section.
283   MCSection *Section = nullptr;
284 
285   // Catchpad unwind destination info for wasm EH.
286   // Keeps track of Wasm exception handling related data. This will be null for
287   // functions that aren't using a wasm EH personality.
288   WasmEHFuncInfo *WasmEHInfo = nullptr;
289 
290   // Keeps track of Windows exception handling related data. This will be null
291   // for functions that aren't using a funclet-based EH personality.
292   WinEHFuncInfo *WinEHInfo = nullptr;
293 
294   // Function-level unique numbering for MachineBasicBlocks.  When a
295   // MachineBasicBlock is inserted into a MachineFunction is it automatically
296   // numbered and this vector keeps track of the mapping from ID's to MBB's.
297   std::vector<MachineBasicBlock*> MBBNumbering;
298 
299   // Pool-allocate MachineFunction-lifetime and IR objects.
300   BumpPtrAllocator Allocator;
301 
302   // Allocation management for instructions in function.
303   Recycler<MachineInstr> InstructionRecycler;
304 
305   // Allocation management for operand arrays on instructions.
306   ArrayRecycler<MachineOperand> OperandRecycler;
307 
308   // Allocation management for basic blocks in function.
309   Recycler<MachineBasicBlock> BasicBlockRecycler;
310 
311   // List of machine basic blocks in function
312   using BasicBlockListType = ilist<MachineBasicBlock>;
313   BasicBlockListType BasicBlocks;
314 
315   /// FunctionNumber - This provides a unique ID for each function emitted in
316   /// this translation unit.
317   ///
318   unsigned FunctionNumber;
319 
320   /// Alignment - The alignment of the function.
321   Align Alignment;
322 
323   /// ExposesReturnsTwice - True if the function calls setjmp or related
324   /// functions with attribute "returns twice", but doesn't have
325   /// the attribute itself.
326   /// This is used to limit optimizations which cannot reason
327   /// about the control flow of such functions.
328   bool ExposesReturnsTwice = false;
329 
330   /// True if the function includes any inline assembly.
331   bool HasInlineAsm = false;
332 
333   /// True if any WinCFI instruction have been emitted in this function.
334   bool HasWinCFI = false;
335 
336   /// Current high-level properties of the IR of the function (e.g. is in SSA
337   /// form or whether registers have been allocated)
338   MachineFunctionProperties Properties;
339 
340   // Allocation management for pseudo source values.
341   std::unique_ptr<PseudoSourceValueManager> PSVManager;
342 
343   /// List of moves done by a function's prolog.  Used to construct frame maps
344   /// by debug and exception handling consumers.
345   std::vector<MCCFIInstruction> FrameInstructions;
346 
347   /// List of basic blocks immediately following calls to _setjmp. Used to
348   /// construct a table of valid longjmp targets for Windows Control Flow Guard.
349   std::vector<MCSymbol *> LongjmpTargets;
350 
351   /// List of basic blocks that are the target of catchrets. Used to construct
352   /// a table of valid targets for Windows EHCont Guard.
353   std::vector<MCSymbol *> CatchretTargets;
354 
355   /// \name Exception Handling
356   /// \{
357 
358   /// List of LandingPadInfo describing the landing pad information.
359   std::vector<LandingPadInfo> LandingPads;
360 
361   /// Map a landing pad's EH symbol to the call site indexes.
362   DenseMap<MCSymbol*, SmallVector<unsigned, 4>> LPadToCallSiteMap;
363 
364   /// Map a landing pad to its index.
365   DenseMap<const MachineBasicBlock *, unsigned> WasmLPadToIndexMap;
366 
367   /// Map of invoke call site index values to associated begin EH_LABEL.
368   DenseMap<MCSymbol*, unsigned> CallSiteMap;
369 
370   /// CodeView label annotations.
371   std::vector<std::pair<MCSymbol *, MDNode *>> CodeViewAnnotations;
372 
373   bool CallsEHReturn = false;
374   bool CallsUnwindInit = false;
375   bool HasEHCatchret = false;
376   bool HasEHScopes = false;
377   bool HasEHFunclets = false;
378   bool IsOutlined = false;
379 
380   /// BBID to assign to the next basic block of this function.
381   unsigned NextBBID = 0;
382 
383   /// Section Type for basic blocks, only relevant with basic block sections.
384   BasicBlockSection BBSectionsType = BasicBlockSection::None;
385 
386   /// List of C++ TypeInfo used.
387   std::vector<const GlobalValue *> TypeInfos;
388 
389   /// List of typeids encoding filters used.
390   std::vector<unsigned> FilterIds;
391 
392   /// List of the indices in FilterIds corresponding to filter terminators.
393   std::vector<unsigned> FilterEnds;
394 
395   EHPersonality PersonalityTypeCache = EHPersonality::Unknown;
396 
397   /// \}
398 
399   /// Clear all the members of this MachineFunction, but the ones used
400   /// to initialize again the MachineFunction.
401   /// More specifically, this deallocates all the dynamically allocated
402   /// objects and get rid of all the XXXInfo data structure, but keep
403   /// unchanged the references to Fn, Target, MMI, and FunctionNumber.
404   void clear();
405   /// Allocate and initialize the different members.
406   /// In particular, the XXXInfo data structure.
407   /// \pre Fn, Target, MMI, and FunctionNumber are properly set.
408   void init();
409 
410 public:
411   /// Description of the location of a variable whose Address is valid and
412   /// unchanging during function execution. The Address may be:
413   /// * A stack index, which can be negative for fixed stack objects.
414   /// * A MCRegister, whose entry value contains the address of the variable.
415   class VariableDbgInfo {
416     std::variant<int, MCRegister> Address;
417 
418   public:
419     const DILocalVariable *Var;
420     const DIExpression *Expr;
421     const DILocation *Loc;
422 
423     VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
424                     int Slot, const DILocation *Loc)
425         : Address(Slot), Var(Var), Expr(Expr), Loc(Loc) {}
426 
427     VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
428                     MCRegister EntryValReg, const DILocation *Loc)
429         : Address(EntryValReg), Var(Var), Expr(Expr), Loc(Loc) {}
430 
431     /// Return true if this variable is in a stack slot.
432     bool inStackSlot() const { return std::holds_alternative<int>(Address); }
433 
434     /// Return true if this variable is in the entry value of a register.
435     bool inEntryValueRegister() const {
436       return std::holds_alternative<MCRegister>(Address);
437     }
438 
439     /// Returns the stack slot of this variable, assuming `inStackSlot()` is
440     /// true.
441     int getStackSlot() const { return std::get<int>(Address); }
442 
443     /// Returns the MCRegister of this variable, assuming
444     /// `inEntryValueRegister()` is true.
445     MCRegister getEntryValueRegister() const {
446       return std::get<MCRegister>(Address);
447     }
448 
449     /// Updates the stack slot of this variable, assuming `inStackSlot()` is
450     /// true.
451     void updateStackSlot(int NewSlot) {
452       assert(inStackSlot());
453       Address = NewSlot;
454     }
455   };
456 
457   class Delegate {
458     virtual void anchor();
459 
460   public:
461     virtual ~Delegate() = default;
462     /// Callback after an insertion. This should not modify the MI directly.
463     virtual void MF_HandleInsertion(MachineInstr &MI) = 0;
464     /// Callback before a removal. This should not modify the MI directly.
465     virtual void MF_HandleRemoval(MachineInstr &MI) = 0;
466   };
467 
468   /// Structure used to represent pair of argument number after call lowering
469   /// and register used to transfer that argument.
470   /// For now we support only cases when argument is transferred through one
471   /// register.
472   struct ArgRegPair {
473     Register Reg;
474     uint16_t ArgNo;
475     ArgRegPair(Register R, unsigned Arg) : Reg(R), ArgNo(Arg) {
476       assert(Arg < (1 << 16) && "Arg out of range");
477     }
478   };
479   /// Vector of call argument and its forwarding register.
480   using CallSiteInfo = SmallVector<ArgRegPair, 1>;
481   using CallSiteInfoImpl = SmallVectorImpl<ArgRegPair>;
482 
483 private:
484   Delegate *TheDelegate = nullptr;
485   GISelChangeObserver *Observer = nullptr;
486 
487   using CallSiteInfoMap = DenseMap<const MachineInstr *, CallSiteInfo>;
488   /// Map a call instruction to call site arguments forwarding info.
489   CallSiteInfoMap CallSitesInfo;
490 
491   /// A helper function that returns call site info for a give call
492   /// instruction if debug entry value support is enabled.
493   CallSiteInfoMap::iterator getCallSiteInfo(const MachineInstr *MI);
494 
495   // Callbacks for insertion and removal.
496   void handleInsertion(MachineInstr &MI);
497   void handleRemoval(MachineInstr &MI);
498   friend struct ilist_traits<MachineInstr>;
499 
500 public:
501   using VariableDbgInfoMapTy = SmallVector<VariableDbgInfo, 4>;
502   VariableDbgInfoMapTy VariableDbgInfos;
503 
504   /// A count of how many instructions in the function have had numbers
505   /// assigned to them. Used for debug value tracking, to determine the
506   /// next instruction number.
507   unsigned DebugInstrNumberingCount = 0;
508 
509   /// Set value of DebugInstrNumberingCount field. Avoid using this unless
510   /// you're deserializing this data.
511   void setDebugInstrNumberingCount(unsigned Num);
512 
513   /// Pair of instruction number and operand number.
514   using DebugInstrOperandPair = std::pair<unsigned, unsigned>;
515 
516   /// Replacement definition for a debug instruction reference. Made up of a
517   /// source instruction / operand pair, destination pair, and a qualifying
518   /// subregister indicating what bits in the operand make up the substitution.
519   // For example, a debug user
520   /// of %1:
521   ///    %0:gr32 = someinst, debug-instr-number 1
522   ///    %1:gr16 = %0.some_16_bit_subreg, debug-instr-number 2
523   /// Would receive the substitution {{2, 0}, {1, 0}, $subreg}, where $subreg is
524   /// the subregister number for some_16_bit_subreg.
525   class DebugSubstitution {
526   public:
527     DebugInstrOperandPair Src;  ///< Source instruction / operand pair.
528     DebugInstrOperandPair Dest; ///< Replacement instruction / operand pair.
529     unsigned Subreg;            ///< Qualifier for which part of Dest is read.
530 
531     DebugSubstitution(const DebugInstrOperandPair &Src,
532                       const DebugInstrOperandPair &Dest, unsigned Subreg)
533         : Src(Src), Dest(Dest), Subreg(Subreg) {}
534 
535     /// Order only by source instruction / operand pair: there should never
536     /// be duplicate entries for the same source in any collection.
537     bool operator<(const DebugSubstitution &Other) const {
538       return Src < Other.Src;
539     }
540   };
541 
542   /// Debug value substitutions: a collection of DebugSubstitution objects,
543   /// recording changes in where a value is defined. For example, when one
544   /// instruction is substituted for another. Keeping a record allows recovery
545   /// of variable locations after compilation finishes.
546   SmallVector<DebugSubstitution, 8> DebugValueSubstitutions;
547 
548   /// Location of a PHI instruction that is also a debug-info variable value,
549   /// for the duration of register allocation. Loaded by the PHI-elimination
550   /// pass, and emitted as DBG_PHI instructions during VirtRegRewriter, with
551   /// maintenance applied by intermediate passes that edit registers (such as
552   /// coalescing and the allocator passes).
553   class DebugPHIRegallocPos {
554   public:
555     MachineBasicBlock *MBB; ///< Block where this PHI was originally located.
556     Register Reg;           ///< VReg where the control-flow-merge happens.
557     unsigned SubReg;        ///< Optional subreg qualifier within Reg.
558     DebugPHIRegallocPos(MachineBasicBlock *MBB, Register Reg, unsigned SubReg)
559         : MBB(MBB), Reg(Reg), SubReg(SubReg) {}
560   };
561 
562   /// Map of debug instruction numbers to the position of their PHI instructions
563   /// during register allocation. See DebugPHIRegallocPos.
564   DenseMap<unsigned, DebugPHIRegallocPos> DebugPHIPositions;
565 
566   /// Flag for whether this function contains DBG_VALUEs (false) or
567   /// DBG_INSTR_REF (true).
568   bool UseDebugInstrRef = false;
569 
570   /// Create a substitution between one <instr,operand> value to a different,
571   /// new value.
572   void makeDebugValueSubstitution(DebugInstrOperandPair, DebugInstrOperandPair,
573                                   unsigned SubReg = 0);
574 
575   /// Create substitutions for any tracked values in \p Old, to point at
576   /// \p New. Needed when we re-create an instruction during optimization,
577   /// which has the same signature (i.e., def operands in the same place) but
578   /// a modified instruction type, flags, or otherwise. An example: X86 moves
579   /// are sometimes transformed into equivalent LEAs.
580   /// If the two instructions are not the same opcode, limit which operands to
581   /// examine for substitutions to the first N operands by setting
582   /// \p MaxOperand.
583   void substituteDebugValuesForInst(const MachineInstr &Old, MachineInstr &New,
584                                     unsigned MaxOperand = UINT_MAX);
585 
586   /// Find the underlying  defining instruction / operand for a COPY instruction
587   /// while in SSA form. Copies do not actually define values -- they move them
588   /// between registers. Labelling a COPY-like instruction with an instruction
589   /// number is to be avoided as it makes value numbers non-unique later in
590   /// compilation. This method follows the definition chain for any sequence of
591   /// COPY-like instructions to find whatever non-COPY-like instruction defines
592   /// the copied value; or for parameters, creates a DBG_PHI on entry.
593   /// May insert instructions into the entry block!
594   /// \p MI The copy-like instruction to salvage.
595   /// \p DbgPHICache A container to cache already-solved COPYs.
596   /// \returns An instruction/operand pair identifying the defining value.
597   DebugInstrOperandPair
598   salvageCopySSA(MachineInstr &MI,
599                  DenseMap<Register, DebugInstrOperandPair> &DbgPHICache);
600 
601   DebugInstrOperandPair salvageCopySSAImpl(MachineInstr &MI);
602 
603   /// Finalise any partially emitted debug instructions. These are DBG_INSTR_REF
604   /// instructions where we only knew the vreg of the value they use, not the
605   /// instruction that defines that vreg. Once isel finishes, we should have
606   /// enough information for every DBG_INSTR_REF to point at an instruction
607   /// (or DBG_PHI).
608   void finalizeDebugInstrRefs();
609 
610   /// Determine whether, in the current machine configuration, we should use
611   /// instruction referencing or not.
612   bool shouldUseDebugInstrRef() const;
613 
614   /// Returns true if the function's variable locations are tracked with
615   /// instruction referencing.
616   bool useDebugInstrRef() const;
617 
618   /// Set whether this function will use instruction referencing or not.
619   void setUseDebugInstrRef(bool UseInstrRef);
620 
621   /// A reserved operand number representing the instructions memory operand,
622   /// for instructions that have a stack spill fused into them.
623   const static unsigned int DebugOperandMemNumber;
624 
625   MachineFunction(Function &F, const LLVMTargetMachine &Target,
626                   const TargetSubtargetInfo &STI, unsigned FunctionNum,
627                   MachineModuleInfo &MMI);
628   MachineFunction(const MachineFunction &) = delete;
629   MachineFunction &operator=(const MachineFunction &) = delete;
630   ~MachineFunction();
631 
632   /// Reset the instance as if it was just created.
633   void reset() {
634     clear();
635     init();
636   }
637 
638   /// Reset the currently registered delegate - otherwise assert.
639   void resetDelegate(Delegate *delegate) {
640     assert(TheDelegate == delegate &&
641            "Only the current delegate can perform reset!");
642     TheDelegate = nullptr;
643   }
644 
645   /// Set the delegate. resetDelegate must be called before attempting
646   /// to set.
647   void setDelegate(Delegate *delegate) {
648     assert(delegate && !TheDelegate &&
649            "Attempted to set delegate to null, or to change it without "
650            "first resetting it!");
651 
652     TheDelegate = delegate;
653   }
654 
655   void setObserver(GISelChangeObserver *O) { Observer = O; }
656 
657   GISelChangeObserver *getObserver() const { return Observer; }
658 
659   MachineModuleInfo &getMMI() const { return MMI; }
660   MCContext &getContext() const { return Ctx; }
661 
662   /// Returns the Section this function belongs to.
663   MCSection *getSection() const { return Section; }
664 
665   /// Indicates the Section this function belongs to.
666   void setSection(MCSection *S) { Section = S; }
667 
668   PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }
669 
670   /// Return the DataLayout attached to the Module associated to this MF.
671   const DataLayout &getDataLayout() const;
672 
673   /// Return the LLVM function that this machine code represents
674   Function &getFunction() { return F; }
675 
676   /// Return the LLVM function that this machine code represents
677   const Function &getFunction() const { return F; }
678 
679   /// getName - Return the name of the corresponding LLVM function.
680   StringRef getName() const;
681 
682   /// getFunctionNumber - Return a unique ID for the current function.
683   unsigned getFunctionNumber() const { return FunctionNumber; }
684 
685   /// Returns true if this function has basic block sections enabled.
686   bool hasBBSections() const {
687     return (BBSectionsType == BasicBlockSection::All ||
688             BBSectionsType == BasicBlockSection::List ||
689             BBSectionsType == BasicBlockSection::Preset);
690   }
691 
692   /// Returns true if basic block labels are to be generated for this function.
693   bool hasBBLabels() const {
694     return BBSectionsType == BasicBlockSection::Labels;
695   }
696 
697   void setBBSectionsType(BasicBlockSection V) { BBSectionsType = V; }
698 
699   /// Assign IsBeginSection IsEndSection fields for basic blocks in this
700   /// function.
701   void assignBeginEndSections();
702 
703   /// getTarget - Return the target machine this machine code is compiled with
704   const LLVMTargetMachine &getTarget() const { return Target; }
705 
706   /// getSubtarget - Return the subtarget for which this machine code is being
707   /// compiled.
708   const TargetSubtargetInfo &getSubtarget() const { return *STI; }
709 
710   /// getSubtarget - This method returns a pointer to the specified type of
711   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
712   /// returned is of the correct type.
713   template<typename STC> const STC &getSubtarget() const {
714     return *static_cast<const STC *>(STI);
715   }
716 
717   /// getRegInfo - Return information about the registers currently in use.
718   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
719   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
720 
721   /// getFrameInfo - Return the frame info object for the current function.
722   /// This object contains information about objects allocated on the stack
723   /// frame of the current function in an abstract way.
724   MachineFrameInfo &getFrameInfo() { return *FrameInfo; }
725   const MachineFrameInfo &getFrameInfo() const { return *FrameInfo; }
726 
727   /// getJumpTableInfo - Return the jump table info object for the current
728   /// function.  This object contains information about jump tables in the
729   /// current function.  If the current function has no jump tables, this will
730   /// return null.
731   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
732   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
733 
734   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
735   /// does already exist, allocate one.
736   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
737 
738   /// getConstantPool - Return the constant pool object for the current
739   /// function.
740   MachineConstantPool *getConstantPool() { return ConstantPool; }
741   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
742 
743   /// getWasmEHFuncInfo - Return information about how the current function uses
744   /// Wasm exception handling. Returns null for functions that don't use wasm
745   /// exception handling.
746   const WasmEHFuncInfo *getWasmEHFuncInfo() const { return WasmEHInfo; }
747   WasmEHFuncInfo *getWasmEHFuncInfo() { return WasmEHInfo; }
748 
749   /// getWinEHFuncInfo - Return information about how the current function uses
750   /// Windows exception handling. Returns null for functions that don't use
751   /// funclets for exception handling.
752   const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; }
753   WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; }
754 
755   /// getAlignment - Return the alignment of the function.
756   Align getAlignment() const { return Alignment; }
757 
758   /// setAlignment - Set the alignment of the function.
759   void setAlignment(Align A) { Alignment = A; }
760 
761   /// ensureAlignment - Make sure the function is at least A bytes aligned.
762   void ensureAlignment(Align A) {
763     if (Alignment < A)
764       Alignment = A;
765   }
766 
767   /// exposesReturnsTwice - Returns true if the function calls setjmp or
768   /// any other similar functions with attribute "returns twice" without
769   /// having the attribute itself.
770   bool exposesReturnsTwice() const {
771     return ExposesReturnsTwice;
772   }
773 
774   /// setCallsSetJmp - Set a flag that indicates if there's a call to
775   /// a "returns twice" function.
776   void setExposesReturnsTwice(bool B) {
777     ExposesReturnsTwice = B;
778   }
779 
780   /// Returns true if the function contains any inline assembly.
781   bool hasInlineAsm() const {
782     return HasInlineAsm;
783   }
784 
785   /// Set a flag that indicates that the function contains inline assembly.
786   void setHasInlineAsm(bool B) {
787     HasInlineAsm = B;
788   }
789 
790   bool hasWinCFI() const {
791     return HasWinCFI;
792   }
793   void setHasWinCFI(bool v) { HasWinCFI = v; }
794 
795   /// True if this function needs frame moves for debug or exceptions.
796   bool needsFrameMoves() const;
797 
798   /// Get the function properties
799   const MachineFunctionProperties &getProperties() const { return Properties; }
800   MachineFunctionProperties &getProperties() { return Properties; }
801 
802   /// getInfo - Keep track of various per-function pieces of information for
803   /// backends that would like to do so.
804   ///
805   template<typename Ty>
806   Ty *getInfo() {
807     return static_cast<Ty*>(MFInfo);
808   }
809 
810   template<typename Ty>
811   const Ty *getInfo() const {
812     return static_cast<const Ty *>(MFInfo);
813   }
814 
815   template <typename Ty> Ty *cloneInfo(const Ty &Old) {
816     assert(!MFInfo);
817     MFInfo = Ty::template create<Ty>(Allocator, Old);
818     return static_cast<Ty *>(MFInfo);
819   }
820 
821   /// Initialize the target specific MachineFunctionInfo
822   void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI);
823 
824   MachineFunctionInfo *cloneInfoFrom(
825       const MachineFunction &OrigMF,
826       const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) {
827     assert(!MFInfo && "new function already has MachineFunctionInfo");
828     if (!OrigMF.MFInfo)
829       return nullptr;
830     return OrigMF.MFInfo->clone(Allocator, *this, Src2DstMBB);
831   }
832 
833   /// Returns the denormal handling type for the default rounding mode of the
834   /// function.
835   DenormalMode getDenormalMode(const fltSemantics &FPType) const;
836 
837   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
838   /// are inserted into the machine function.  The block number for a machine
839   /// basic block can be found by using the MBB::getNumber method, this method
840   /// provides the inverse mapping.
841   MachineBasicBlock *getBlockNumbered(unsigned N) const {
842     assert(N < MBBNumbering.size() && "Illegal block number");
843     assert(MBBNumbering[N] && "Block was removed from the machine function!");
844     return MBBNumbering[N];
845   }
846 
847   /// Should we be emitting segmented stack stuff for the function
848   bool shouldSplitStack() const;
849 
850   /// getNumBlockIDs - Return the number of MBB ID's allocated.
851   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
852 
853   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
854   /// recomputes them.  This guarantees that the MBB numbers are sequential,
855   /// dense, and match the ordering of the blocks within the function.  If a
856   /// specific MachineBasicBlock is specified, only that block and those after
857   /// it are renumbered.
858   void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
859 
860   /// print - Print out the MachineFunction in a format suitable for debugging
861   /// to the specified stream.
862   void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
863 
864   /// viewCFG - This function is meant for use from the debugger.  You can just
865   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
866   /// program, displaying the CFG of the current function with the code for each
867   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
868   /// in your path.
869   void viewCFG() const;
870 
871   /// viewCFGOnly - This function is meant for use from the debugger.  It works
872   /// just like viewCFG, but it does not include the contents of basic blocks
873   /// into the nodes, just the label.  If you are only interested in the CFG
874   /// this can make the graph smaller.
875   ///
876   void viewCFGOnly() const;
877 
878   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
879   void dump() const;
880 
881   /// Run the current MachineFunction through the machine code verifier, useful
882   /// for debugger use.
883   /// \returns true if no problems were found.
884   bool verify(Pass *p = nullptr, const char *Banner = nullptr,
885               bool AbortOnError = true) const;
886 
887   // Provide accessors for the MachineBasicBlock list...
888   using iterator = BasicBlockListType::iterator;
889   using const_iterator = BasicBlockListType::const_iterator;
890   using const_reverse_iterator = BasicBlockListType::const_reverse_iterator;
891   using reverse_iterator = BasicBlockListType::reverse_iterator;
892 
893   /// Support for MachineBasicBlock::getNextNode().
894   static BasicBlockListType MachineFunction::*
895   getSublistAccess(MachineBasicBlock *) {
896     return &MachineFunction::BasicBlocks;
897   }
898 
899   /// addLiveIn - Add the specified physical register as a live-in value and
900   /// create a corresponding virtual register for it.
901   Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC);
902 
903   //===--------------------------------------------------------------------===//
904   // BasicBlock accessor functions.
905   //
906   iterator                 begin()       { return BasicBlocks.begin(); }
907   const_iterator           begin() const { return BasicBlocks.begin(); }
908   iterator                 end  ()       { return BasicBlocks.end();   }
909   const_iterator           end  () const { return BasicBlocks.end();   }
910 
911   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
912   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
913   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
914   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
915 
916   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
917   bool                     empty() const { return BasicBlocks.empty(); }
918   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
919         MachineBasicBlock &front()       { return BasicBlocks.front(); }
920   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
921         MachineBasicBlock & back()       { return BasicBlocks.back(); }
922 
923   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
924   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
925   void insert(iterator MBBI, MachineBasicBlock *MBB) {
926     BasicBlocks.insert(MBBI, MBB);
927   }
928   void splice(iterator InsertPt, iterator MBBI) {
929     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
930   }
931   void splice(iterator InsertPt, MachineBasicBlock *MBB) {
932     BasicBlocks.splice(InsertPt, BasicBlocks, MBB);
933   }
934   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
935     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
936   }
937 
938   void remove(iterator MBBI) { BasicBlocks.remove(MBBI); }
939   void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(MBBI); }
940   void erase(iterator MBBI) { BasicBlocks.erase(MBBI); }
941   void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(MBBI); }
942 
943   template <typename Comp>
944   void sort(Comp comp) {
945     BasicBlocks.sort(comp);
946   }
947 
948   /// Return the number of \p MachineInstrs in this \p MachineFunction.
949   unsigned getInstructionCount() const {
950     unsigned InstrCount = 0;
951     for (const MachineBasicBlock &MBB : BasicBlocks)
952       InstrCount += MBB.size();
953     return InstrCount;
954   }
955 
956   //===--------------------------------------------------------------------===//
957   // Internal functions used to automatically number MachineBasicBlocks
958 
959   /// Adds the MBB to the internal numbering. Returns the unique number
960   /// assigned to the MBB.
961   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
962     MBBNumbering.push_back(MBB);
963     return (unsigned)MBBNumbering.size()-1;
964   }
965 
966   /// removeFromMBBNumbering - Remove the specific machine basic block from our
967   /// tracker, this is only really to be used by the MachineBasicBlock
968   /// implementation.
969   void removeFromMBBNumbering(unsigned N) {
970     assert(N < MBBNumbering.size() && "Illegal basic block #");
971     MBBNumbering[N] = nullptr;
972   }
973 
974   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
975   /// of `new MachineInstr'.
976   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, DebugLoc DL,
977                                    bool NoImplicit = false);
978 
979   /// Create a new MachineInstr which is a copy of \p Orig, identical in all
980   /// ways except the instruction has no parent, prev, or next. Bundling flags
981   /// are reset.
982   ///
983   /// Note: Clones a single instruction, not whole instruction bundles.
984   /// Does not perform target specific adjustments; consider using
985   /// TargetInstrInfo::duplicate() instead.
986   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
987 
988   /// Clones instruction or the whole instruction bundle \p Orig and insert
989   /// into \p MBB before \p InsertBefore.
990   ///
991   /// Note: Does not perform target specific adjustments; consider using
992   /// TargetInstrInfo::duplicate() intead.
993   MachineInstr &
994   cloneMachineInstrBundle(MachineBasicBlock &MBB,
995                           MachineBasicBlock::iterator InsertBefore,
996                           const MachineInstr &Orig);
997 
998   /// DeleteMachineInstr - Delete the given MachineInstr.
999   void deleteMachineInstr(MachineInstr *MI);
1000 
1001   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
1002   /// instead of `new MachineBasicBlock'.
1003   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = nullptr);
1004 
1005   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
1006   void deleteMachineBasicBlock(MachineBasicBlock *MBB);
1007 
1008   /// getMachineMemOperand - Allocate a new MachineMemOperand.
1009   /// MachineMemOperands are owned by the MachineFunction and need not be
1010   /// explicitly deallocated.
1011   MachineMemOperand *getMachineMemOperand(
1012       MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
1013       Align base_alignment, const AAMDNodes &AAInfo = AAMDNodes(),
1014       const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1015       AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1016       AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1017 
1018   MachineMemOperand *getMachineMemOperand(
1019       MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy,
1020       Align base_alignment, const AAMDNodes &AAInfo = AAMDNodes(),
1021       const MDNode *Ranges = nullptr, SyncScope::ID SSID = SyncScope::System,
1022       AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
1023       AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
1024 
1025   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1026   /// an existing one, adjusting by an offset and using the given size.
1027   /// MachineMemOperands are owned by the MachineFunction and need not be
1028   /// explicitly deallocated.
1029   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1030                                           int64_t Offset, LLT Ty);
1031   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1032                                           int64_t Offset, uint64_t Size) {
1033     return getMachineMemOperand(
1034         MMO, Offset, Size == ~UINT64_C(0) ? LLT() : LLT::scalar(8 * Size));
1035   }
1036 
1037   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
1038   /// an existing one, replacing only the MachinePointerInfo and size.
1039   /// MachineMemOperands are owned by the MachineFunction and need not be
1040   /// explicitly deallocated.
1041   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1042                                           const MachinePointerInfo &PtrInfo,
1043                                           uint64_t Size);
1044   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1045                                           const MachinePointerInfo &PtrInfo,
1046                                           LLT Ty);
1047 
1048   /// Allocate a new MachineMemOperand by copying an existing one,
1049   /// replacing only AliasAnalysis information. MachineMemOperands are owned
1050   /// by the MachineFunction and need not be explicitly deallocated.
1051   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1052                                           const AAMDNodes &AAInfo);
1053 
1054   /// Allocate a new MachineMemOperand by copying an existing one,
1055   /// replacing the flags. MachineMemOperands are owned
1056   /// by the MachineFunction and need not be explicitly deallocated.
1057   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
1058                                           MachineMemOperand::Flags Flags);
1059 
1060   using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity;
1061 
1062   /// Allocate an array of MachineOperands. This is only intended for use by
1063   /// internal MachineInstr functions.
1064   MachineOperand *allocateOperandArray(OperandCapacity Cap) {
1065     return OperandRecycler.allocate(Cap, Allocator);
1066   }
1067 
1068   /// Dellocate an array of MachineOperands and recycle the memory. This is
1069   /// only intended for use by internal MachineInstr functions.
1070   /// Cap must be the same capacity that was used to allocate the array.
1071   void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
1072     OperandRecycler.deallocate(Cap, Array);
1073   }
1074 
1075   /// Allocate and initialize a register mask with @p NumRegister bits.
1076   uint32_t *allocateRegMask();
1077 
1078   ArrayRef<int> allocateShuffleMask(ArrayRef<int> Mask);
1079 
1080   /// Allocate and construct an extra info structure for a `MachineInstr`.
1081   ///
1082   /// This is allocated on the function's allocator and so lives the life of
1083   /// the function.
1084   MachineInstr::ExtraInfo *createMIExtraInfo(
1085       ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol = nullptr,
1086       MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr,
1087       MDNode *PCSections = nullptr, uint32_t CFIType = 0);
1088 
1089   /// Allocate a string and populate it with the given external symbol name.
1090   const char *createExternalSymbolName(StringRef Name);
1091 
1092   //===--------------------------------------------------------------------===//
1093   // Label Manipulation.
1094 
1095   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
1096   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
1097   /// normal 'L' label is returned.
1098   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
1099                          bool isLinkerPrivate = false) const;
1100 
1101   /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
1102   /// base.
1103   MCSymbol *getPICBaseSymbol() const;
1104 
1105   /// Returns a reference to a list of cfi instructions in the function's
1106   /// prologue.  Used to construct frame maps for debug and exception handling
1107   /// comsumers.
1108   const std::vector<MCCFIInstruction> &getFrameInstructions() const {
1109     return FrameInstructions;
1110   }
1111 
1112   [[nodiscard]] unsigned addFrameInst(const MCCFIInstruction &Inst);
1113 
1114   /// Returns a reference to a list of symbols immediately following calls to
1115   /// _setjmp in the function. Used to construct the longjmp target table used
1116   /// by Windows Control Flow Guard.
1117   const std::vector<MCSymbol *> &getLongjmpTargets() const {
1118     return LongjmpTargets;
1119   }
1120 
1121   /// Add the specified symbol to the list of valid longjmp targets for Windows
1122   /// Control Flow Guard.
1123   void addLongjmpTarget(MCSymbol *Target) { LongjmpTargets.push_back(Target); }
1124 
1125   /// Returns a reference to a list of symbols that we have catchrets.
1126   /// Used to construct the catchret target table used by Windows EHCont Guard.
1127   const std::vector<MCSymbol *> &getCatchretTargets() const {
1128     return CatchretTargets;
1129   }
1130 
1131   /// Add the specified symbol to the list of valid catchret targets for Windows
1132   /// EHCont Guard.
1133   void addCatchretTarget(MCSymbol *Target) {
1134     CatchretTargets.push_back(Target);
1135   }
1136 
1137   /// \name Exception Handling
1138   /// \{
1139 
1140   bool callsEHReturn() const { return CallsEHReturn; }
1141   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
1142 
1143   bool callsUnwindInit() const { return CallsUnwindInit; }
1144   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
1145 
1146   bool hasEHCatchret() const { return HasEHCatchret; }
1147   void setHasEHCatchret(bool V) { HasEHCatchret = V; }
1148 
1149   bool hasEHScopes() const { return HasEHScopes; }
1150   void setHasEHScopes(bool V) { HasEHScopes = V; }
1151 
1152   bool hasEHFunclets() const { return HasEHFunclets; }
1153   void setHasEHFunclets(bool V) { HasEHFunclets = V; }
1154 
1155   bool isOutlined() const { return IsOutlined; }
1156   void setIsOutlined(bool V) { IsOutlined = V; }
1157 
1158   /// Find or create an LandingPadInfo for the specified MachineBasicBlock.
1159   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1160 
1161   /// Return a reference to the landing pad info for the current function.
1162   const std::vector<LandingPadInfo> &getLandingPads() const {
1163     return LandingPads;
1164   }
1165 
1166   /// Provide the begin and end labels of an invoke style call and associate it
1167   /// with a try landing pad block.
1168   void addInvoke(MachineBasicBlock *LandingPad,
1169                  MCSymbol *BeginLabel, MCSymbol *EndLabel);
1170 
1171   /// Add a new panding pad, and extract the exception handling information from
1172   /// the landingpad instruction. Returns the label ID for the landing pad
1173   /// entry.
1174   MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
1175 
1176   /// Return the type id for the specified typeinfo.  This is function wide.
1177   unsigned getTypeIDFor(const GlobalValue *TI);
1178 
1179   /// Return the id of the filter encoded by TyIds.  This is function wide.
1180   int getFilterIDFor(ArrayRef<unsigned> TyIds);
1181 
1182   /// Map the landing pad's EH symbol to the call site indexes.
1183   void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
1184 
1185   /// Return if there is any wasm exception handling.
1186   bool hasAnyWasmLandingPadIndex() const {
1187     return !WasmLPadToIndexMap.empty();
1188   }
1189 
1190   /// Map the landing pad to its index. Used for Wasm exception handling.
1191   void setWasmLandingPadIndex(const MachineBasicBlock *LPad, unsigned Index) {
1192     WasmLPadToIndexMap[LPad] = Index;
1193   }
1194 
1195   /// Returns true if the landing pad has an associate index in wasm EH.
1196   bool hasWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1197     return WasmLPadToIndexMap.count(LPad);
1198   }
1199 
1200   /// Get the index in wasm EH for a given landing pad.
1201   unsigned getWasmLandingPadIndex(const MachineBasicBlock *LPad) const {
1202     assert(hasWasmLandingPadIndex(LPad));
1203     return WasmLPadToIndexMap.lookup(LPad);
1204   }
1205 
1206   bool hasAnyCallSiteLandingPad() const {
1207     return !LPadToCallSiteMap.empty();
1208   }
1209 
1210   /// Get the call site indexes for a landing pad EH symbol.
1211   SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
1212     assert(hasCallSiteLandingPad(Sym) &&
1213            "missing call site number for landing pad!");
1214     return LPadToCallSiteMap[Sym];
1215   }
1216 
1217   /// Return true if the landing pad Eh symbol has an associated call site.
1218   bool hasCallSiteLandingPad(MCSymbol *Sym) {
1219     return !LPadToCallSiteMap[Sym].empty();
1220   }
1221 
1222   bool hasAnyCallSiteLabel() const {
1223     return !CallSiteMap.empty();
1224   }
1225 
1226   /// Map the begin label for a call site.
1227   void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
1228     CallSiteMap[BeginLabel] = Site;
1229   }
1230 
1231   /// Get the call site number for a begin label.
1232   unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1233     assert(hasCallSiteBeginLabel(BeginLabel) &&
1234            "Missing call site number for EH_LABEL!");
1235     return CallSiteMap.lookup(BeginLabel);
1236   }
1237 
1238   /// Return true if the begin label has a call site number associated with it.
1239   bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) const {
1240     return CallSiteMap.count(BeginLabel);
1241   }
1242 
1243   /// Record annotations associated with a particular label.
1244   void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD) {
1245     CodeViewAnnotations.push_back({Label, MD});
1246   }
1247 
1248   ArrayRef<std::pair<MCSymbol *, MDNode *>> getCodeViewAnnotations() const {
1249     return CodeViewAnnotations;
1250   }
1251 
1252   /// Return a reference to the C++ typeinfo for the current function.
1253   const std::vector<const GlobalValue *> &getTypeInfos() const {
1254     return TypeInfos;
1255   }
1256 
1257   /// Return a reference to the typeids encoding filters used in the current
1258   /// function.
1259   const std::vector<unsigned> &getFilterIds() const {
1260     return FilterIds;
1261   }
1262 
1263   /// \}
1264 
1265   /// Collect information used to emit debugging information of a variable in a
1266   /// stack slot.
1267   void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1268                           int Slot, const DILocation *Loc) {
1269     VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
1270   }
1271 
1272   /// Collect information used to emit debugging information of a variable in
1273   /// the entry value of a register.
1274   void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
1275                           MCRegister Reg, const DILocation *Loc) {
1276     VariableDbgInfos.emplace_back(Var, Expr, Reg, Loc);
1277   }
1278 
1279   VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
1280   const VariableDbgInfoMapTy &getVariableDbgInfo() const {
1281     return VariableDbgInfos;
1282   }
1283 
1284   /// Returns the collection of variables for which we have debug info and that
1285   /// have been assigned a stack slot.
1286   auto getInStackSlotVariableDbgInfo() {
1287     return make_filter_range(getVariableDbgInfo(), [](auto &VarInfo) {
1288       return VarInfo.inStackSlot();
1289     });
1290   }
1291 
1292   /// Returns the collection of variables for which we have debug info and that
1293   /// have been assigned a stack slot.
1294   auto getInStackSlotVariableDbgInfo() const {
1295     return make_filter_range(getVariableDbgInfo(), [](const auto &VarInfo) {
1296       return VarInfo.inStackSlot();
1297     });
1298   }
1299 
1300   /// Returns the collection of variables for which we have debug info and that
1301   /// have been assigned an entry value register.
1302   auto getEntryValueVariableDbgInfo() const {
1303     return make_filter_range(getVariableDbgInfo(), [](const auto &VarInfo) {
1304       return VarInfo.inEntryValueRegister();
1305     });
1306   }
1307 
1308   /// Start tracking the arguments passed to the call \p CallI.
1309   void addCallArgsForwardingRegs(const MachineInstr *CallI,
1310                                  CallSiteInfoImpl &&CallInfo) {
1311     assert(CallI->isCandidateForCallSiteEntry());
1312     bool Inserted =
1313         CallSitesInfo.try_emplace(CallI, std::move(CallInfo)).second;
1314     (void)Inserted;
1315     assert(Inserted && "Call site info not unique");
1316   }
1317 
1318   const CallSiteInfoMap &getCallSitesInfo() const {
1319     return CallSitesInfo;
1320   }
1321 
1322   /// Following functions update call site info. They should be called before
1323   /// removing, replacing or copying call instruction.
1324 
1325   /// Erase the call site info for \p MI. It is used to remove a call
1326   /// instruction from the instruction stream.
1327   void eraseCallSiteInfo(const MachineInstr *MI);
1328   /// Copy the call site info from \p Old to \ New. Its usage is when we are
1329   /// making a copy of the instruction that will be inserted at different point
1330   /// of the instruction stream.
1331   void copyCallSiteInfo(const MachineInstr *Old,
1332                         const MachineInstr *New);
1333 
1334   /// Move the call site info from \p Old to \New call site info. This function
1335   /// is used when we are replacing one call instruction with another one to
1336   /// the same callee.
1337   void moveCallSiteInfo(const MachineInstr *Old,
1338                         const MachineInstr *New);
1339 
1340   unsigned getNewDebugInstrNum() {
1341     return ++DebugInstrNumberingCount;
1342   }
1343 };
1344 
1345 //===--------------------------------------------------------------------===//
1346 // GraphTraits specializations for function basic block graphs (CFGs)
1347 //===--------------------------------------------------------------------===//
1348 
1349 // Provide specializations of GraphTraits to be able to treat a
1350 // machine function as a graph of machine basic blocks... these are
1351 // the same as the machine basic block iterators, except that the root
1352 // node is implicitly the first node of the function.
1353 //
1354 template <> struct GraphTraits<MachineFunction*> :
1355   public GraphTraits<MachineBasicBlock*> {
1356   static NodeRef getEntryNode(MachineFunction *F) { return &F->front(); }
1357 
1358   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1359   using nodes_iterator = pointer_iterator<MachineFunction::iterator>;
1360 
1361   static nodes_iterator nodes_begin(MachineFunction *F) {
1362     return nodes_iterator(F->begin());
1363   }
1364 
1365   static nodes_iterator nodes_end(MachineFunction *F) {
1366     return nodes_iterator(F->end());
1367   }
1368 
1369   static unsigned       size       (MachineFunction *F) { return F->size(); }
1370 };
1371 template <> struct GraphTraits<const MachineFunction*> :
1372   public GraphTraits<const MachineBasicBlock*> {
1373   static NodeRef getEntryNode(const MachineFunction *F) { return &F->front(); }
1374 
1375   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
1376   using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
1377 
1378   static nodes_iterator nodes_begin(const MachineFunction *F) {
1379     return nodes_iterator(F->begin());
1380   }
1381 
1382   static nodes_iterator nodes_end  (const MachineFunction *F) {
1383     return nodes_iterator(F->end());
1384   }
1385 
1386   static unsigned       size       (const MachineFunction *F)  {
1387     return F->size();
1388   }
1389 };
1390 
1391 // Provide specializations of GraphTraits to be able to treat a function as a
1392 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
1393 // a function is considered to be when traversing the predecessor edges of a BB
1394 // instead of the successor edges.
1395 //
1396 template <> struct GraphTraits<Inverse<MachineFunction*>> :
1397   public GraphTraits<Inverse<MachineBasicBlock*>> {
1398   static NodeRef getEntryNode(Inverse<MachineFunction *> G) {
1399     return &G.Graph->front();
1400   }
1401 };
1402 template <> struct GraphTraits<Inverse<const MachineFunction*>> :
1403   public GraphTraits<Inverse<const MachineBasicBlock*>> {
1404   static NodeRef getEntryNode(Inverse<const MachineFunction *> G) {
1405     return &G.Graph->front();
1406   }
1407 };
1408 
1409 class MachineFunctionAnalysisManager;
1410 void verifyMachineFunction(MachineFunctionAnalysisManager *,
1411                            const std::string &Banner,
1412                            const MachineFunction &MF);
1413 
1414 } // end namespace llvm
1415 
1416 #endif // LLVM_CODEGEN_MACHINEFUNCTION_H
1417