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