1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code for a function. This class contains a list of
11 // MachineBasicBlock instances that make up the current compiled function.
13 // This class also contains pointers to various classes which hold
14 // target-specific information about the generated code.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
19 #define LLVM_CODEGEN_MACHINEFUNCTION_H
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/ilist.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/Analysis/EHPersonalities.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Support/Allocator.h"
32 #include "llvm/Support/ArrayRecycler.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Recycler.h"
41 class MachineRegisterInfo;
42 class MachineFrameInfo;
43 class MachineConstantPool;
44 class MachineJumpTableInfo;
45 class MachineModuleInfo;
48 class PseudoSourceValueManager;
50 class TargetSubtargetInfo;
51 class TargetRegisterClass;
52 struct MachinePointerInfo;
55 template <> struct ilist_alloc_traits<MachineBasicBlock> {
56 void deleteNode(MachineBasicBlock *MBB);
59 template <> struct ilist_callback_traits<MachineBasicBlock> {
60 void addNodeToList(MachineBasicBlock* MBB);
61 void removeNodeFromList(MachineBasicBlock* MBB);
63 template <class Iterator>
64 void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator) {
65 llvm_unreachable("Never transfer between lists");
69 /// MachineFunctionInfo - This class can be derived from and used by targets to
70 /// hold private target-specific information for each MachineFunction. Objects
71 /// of type are accessed/created with MF::getInfo and destroyed when the
72 /// MachineFunction is destroyed.
73 struct MachineFunctionInfo {
74 virtual ~MachineFunctionInfo();
76 /// \brief Factory function: default behavior is to call new using the
77 /// supplied allocator.
79 /// This function can be overridden in a derive class.
81 static Ty *create(BumpPtrAllocator &Allocator, MachineFunction &MF) {
82 return new (Allocator.Allocate<Ty>()) Ty(MF);
86 /// Properties which a MachineFunction may have at a given point in time.
87 /// Each of these has checking code in the MachineVerifier, and passes can
88 /// require that a property be set.
89 class MachineFunctionProperties {
90 // Possible TODO: Allow targets to extend this (perhaps by allowing the
91 // constructor to specify the size of the bit vector)
92 // Possible TODO: Allow requiring the negative (e.g. VRegsAllocated could be
93 // stated as the negative of "has vregs"
96 // The properties are stated in "positive" form; i.e. a pass could require
97 // that the property hold, but not that it does not hold.
99 // Property descriptions:
100 // IsSSA: True when the machine function is in SSA form and virtual registers
101 // have a single def.
102 // NoPHIs: The machine function does not contain any PHI instruction.
103 // TracksLiveness: True when tracking register liveness accurately.
104 // While this property is set, register liveness information in basic block
105 // live-in lists and machine instruction operands (e.g. kill flags, implicit
106 // defs) is accurate. This means it can be used to change the code in ways
107 // that affect the values in registers, for example by the register
109 // When this property is clear, liveness is no longer reliable.
110 // NoVRegs: The machine function does not use any virtual registers.
111 // Legalized: In GlobalISel: the MachineLegalizer ran and all pre-isel generic
112 // instructions have been legalized; i.e., all instructions are now one of:
113 // - generic and always legal (e.g., COPY)
115 // - legal pre-isel generic instructions.
116 // RegBankSelected: In GlobalISel: the RegBankSelect pass ran and all generic
117 // virtual registers have been assigned to a register bank.
118 // Selected: In GlobalISel: the InstructionSelect pass ran and all pre-isel
119 // generic instructions have been eliminated; i.e., all instructions are now
120 // target-specific or non-pre-isel generic instructions (e.g., COPY).
121 // Since only pre-isel generic instructions can have generic virtual register
122 // operands, this also means that all generic virtual registers have been
123 // constrained to virtual registers (assigned to register classes) and that
124 // all sizes attached to them have been eliminated.
125 enum class Property : unsigned {
134 LastProperty = Selected,
137 bool hasProperty(Property P) const {
138 return Properties[static_cast<unsigned>(P)];
140 MachineFunctionProperties &set(Property P) {
141 Properties.set(static_cast<unsigned>(P));
144 MachineFunctionProperties &reset(Property P) {
145 Properties.reset(static_cast<unsigned>(P));
148 /// Reset all the properties.
149 MachineFunctionProperties &reset() {
153 MachineFunctionProperties &set(const MachineFunctionProperties &MFP) {
154 Properties |= MFP.Properties;
157 MachineFunctionProperties &reset(const MachineFunctionProperties &MFP) {
158 Properties.reset(MFP.Properties);
161 // Returns true if all properties set in V (i.e. required by a pass) are set
163 bool verifyRequiredProperties(const MachineFunctionProperties &V) const {
164 return !V.Properties.test(Properties);
167 /// Print the MachineFunctionProperties in human-readable form.
168 void print(raw_ostream &OS) const;
171 BitVector Properties =
172 BitVector(static_cast<unsigned>(Property::LastProperty)+1);
176 /// Filter or finally function. Null indicates a catch-all.
177 const Function *FilterOrFinally;
179 /// Address of block to recover at. Null for a finally handler.
180 const BlockAddress *RecoverBA;
184 /// This structure is used to retain landing pad info for the current function.
185 struct LandingPadInfo {
186 MachineBasicBlock *LandingPadBlock; // Landing pad block.
187 SmallVector<MCSymbol *, 1> BeginLabels; // Labels prior to invoke.
188 SmallVector<MCSymbol *, 1> EndLabels; // Labels after invoke.
189 SmallVector<SEHHandler, 1> SEHHandlers; // SEH handlers active at this lpad.
190 MCSymbol *LandingPadLabel; // Label at beginning of landing pad.
191 std::vector<int> TypeIds; // List of type ids (filters negative).
193 explicit LandingPadInfo(MachineBasicBlock *MBB)
194 : LandingPadBlock(MBB), LandingPadLabel(nullptr) {}
197 class MachineFunction {
199 const TargetMachine &Target;
200 const TargetSubtargetInfo *STI;
202 MachineModuleInfo &MMI;
204 // RegInfo - Information about each register in use in the function.
205 MachineRegisterInfo *RegInfo;
207 // Used to keep track of target-specific per-machine function information for
208 // the target implementation.
209 MachineFunctionInfo *MFInfo;
211 // Keep track of objects allocated on the stack.
212 MachineFrameInfo *FrameInfo;
214 // Keep track of constants which are spilled to memory
215 MachineConstantPool *ConstantPool;
217 // Keep track of jump tables for switch instructions
218 MachineJumpTableInfo *JumpTableInfo;
220 // Keeps track of Windows exception handling related data. This will be null
221 // for functions that aren't using a funclet-based EH personality.
222 WinEHFuncInfo *WinEHInfo = nullptr;
224 // Function-level unique numbering for MachineBasicBlocks. When a
225 // MachineBasicBlock is inserted into a MachineFunction is it automatically
226 // numbered and this vector keeps track of the mapping from ID's to MBB's.
227 std::vector<MachineBasicBlock*> MBBNumbering;
229 // Pool-allocate MachineFunction-lifetime and IR objects.
230 BumpPtrAllocator Allocator;
232 // Allocation management for instructions in function.
233 Recycler<MachineInstr> InstructionRecycler;
235 // Allocation management for operand arrays on instructions.
236 ArrayRecycler<MachineOperand> OperandRecycler;
238 // Allocation management for basic blocks in function.
239 Recycler<MachineBasicBlock> BasicBlockRecycler;
241 // List of machine basic blocks in function
242 typedef ilist<MachineBasicBlock> BasicBlockListType;
243 BasicBlockListType BasicBlocks;
245 /// FunctionNumber - This provides a unique ID for each function emitted in
246 /// this translation unit.
248 unsigned FunctionNumber;
250 /// Alignment - The alignment of the function.
253 /// ExposesReturnsTwice - True if the function calls setjmp or related
254 /// functions with attribute "returns twice", but doesn't have
255 /// the attribute itself.
256 /// This is used to limit optimizations which cannot reason
257 /// about the control flow of such functions.
258 bool ExposesReturnsTwice = false;
260 /// True if the function includes any inline assembly.
261 bool HasInlineAsm = false;
263 /// True if any WinCFI instruction have been emitted in this function.
264 Optional<bool> HasWinCFI;
266 /// Current high-level properties of the IR of the function (e.g. is in SSA
267 /// form or whether registers have been allocated)
268 MachineFunctionProperties Properties;
270 // Allocation management for pseudo source values.
271 std::unique_ptr<PseudoSourceValueManager> PSVManager;
273 /// List of moves done by a function's prolog. Used to construct frame maps
274 /// by debug and exception handling consumers.
275 std::vector<MCCFIInstruction> FrameInstructions;
277 /// \name Exception Handling
280 /// List of LandingPadInfo describing the landing pad information.
281 std::vector<LandingPadInfo> LandingPads;
283 /// Map a landing pad's EH symbol to the call site indexes.
284 DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap;
286 /// Map of invoke call site index values to associated begin EH_LABEL.
287 DenseMap<MCSymbol*, unsigned> CallSiteMap;
289 bool CallsEHReturn = false;
290 bool CallsUnwindInit = false;
291 bool HasEHFunclets = false;
293 /// List of C++ TypeInfo used.
294 std::vector<const GlobalValue *> TypeInfos;
296 /// List of typeids encoding filters used.
297 std::vector<unsigned> FilterIds;
299 /// List of the indices in FilterIds corresponding to filter terminators.
300 std::vector<unsigned> FilterEnds;
302 EHPersonality PersonalityTypeCache = EHPersonality::Unknown;
306 MachineFunction(const MachineFunction &) = delete;
307 void operator=(const MachineFunction&) = delete;
309 /// Clear all the members of this MachineFunction, but the ones used
310 /// to initialize again the MachineFunction.
311 /// More specifically, this deallocates all the dynamically allocated
312 /// objects and get rid of all the XXXInfo data structure, but keep
313 /// unchanged the references to Fn, Target, MMI, and FunctionNumber.
315 /// Allocate and initialize the different members.
316 /// In particular, the XXXInfo data structure.
317 /// \pre Fn, Target, MMI, and FunctionNumber are properly set.
321 struct VariableDbgInfo {
322 const DILocalVariable *Var;
323 const DIExpression *Expr;
325 const DILocation *Loc;
327 VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
328 unsigned Slot, const DILocation *Loc)
329 : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {}
331 typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
332 VariableDbgInfoMapTy VariableDbgInfos;
334 MachineFunction(const Function *Fn, const TargetMachine &TM,
335 unsigned FunctionNum, MachineModuleInfo &MMI);
338 /// Reset the instance as if it was just created.
344 MachineModuleInfo &getMMI() const { return MMI; }
345 MCContext &getContext() const { return Ctx; }
347 PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }
349 /// Return the DataLayout attached to the Module associated to this MF.
350 const DataLayout &getDataLayout() const;
352 /// getFunction - Return the LLVM function that this machine code represents
354 const Function *getFunction() const { return Fn; }
356 /// getName - Return the name of the corresponding LLVM function.
358 StringRef getName() const;
360 /// getFunctionNumber - Return a unique ID for the current function.
362 unsigned getFunctionNumber() const { return FunctionNumber; }
364 /// getTarget - Return the target machine this machine code is compiled with
366 const TargetMachine &getTarget() const { return Target; }
368 /// getSubtarget - Return the subtarget for which this machine code is being
370 const TargetSubtargetInfo &getSubtarget() const { return *STI; }
371 void setSubtarget(const TargetSubtargetInfo *ST) { STI = ST; }
373 /// getSubtarget - This method returns a pointer to the specified type of
374 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
375 /// returned is of the correct type.
376 template<typename STC> const STC &getSubtarget() const {
377 return *static_cast<const STC *>(STI);
380 /// getRegInfo - Return information about the registers currently in use.
382 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
383 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
385 /// getFrameInfo - Return the frame info object for the current function.
386 /// This object contains information about objects allocated on the stack
387 /// frame of the current function in an abstract way.
389 MachineFrameInfo &getFrameInfo() { return *FrameInfo; }
390 const MachineFrameInfo &getFrameInfo() const { return *FrameInfo; }
392 /// getJumpTableInfo - Return the jump table info object for the current
393 /// function. This object contains information about jump tables in the
394 /// current function. If the current function has no jump tables, this will
396 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
397 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
399 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
400 /// does already exist, allocate one.
401 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
403 /// getConstantPool - Return the constant pool object for the current
406 MachineConstantPool *getConstantPool() { return ConstantPool; }
407 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
409 /// getWinEHFuncInfo - Return information about how the current function uses
410 /// Windows exception handling. Returns null for functions that don't use
411 /// funclets for exception handling.
412 const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; }
413 WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; }
415 /// getAlignment - Return the alignment (log2, not bytes) of the function.
417 unsigned getAlignment() const { return Alignment; }
419 /// setAlignment - Set the alignment (log2, not bytes) of the function.
421 void setAlignment(unsigned A) { Alignment = A; }
423 /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
424 void ensureAlignment(unsigned A) {
425 if (Alignment < A) Alignment = A;
428 /// exposesReturnsTwice - Returns true if the function calls setjmp or
429 /// any other similar functions with attribute "returns twice" without
430 /// having the attribute itself.
431 bool exposesReturnsTwice() const {
432 return ExposesReturnsTwice;
435 /// setCallsSetJmp - Set a flag that indicates if there's a call to
436 /// a "returns twice" function.
437 void setExposesReturnsTwice(bool B) {
438 ExposesReturnsTwice = B;
441 /// Returns true if the function contains any inline assembly.
442 bool hasInlineAsm() const {
446 /// Set a flag that indicates that the function contains inline assembly.
447 void setHasInlineAsm(bool B) {
451 bool hasWinCFI() const {
452 assert(HasWinCFI.hasValue() && "HasWinCFI not set yet!");
455 void setHasWinCFI(bool v) { HasWinCFI = v; }
457 /// Get the function properties
458 const MachineFunctionProperties &getProperties() const { return Properties; }
459 MachineFunctionProperties &getProperties() { return Properties; }
461 /// getInfo - Keep track of various per-function pieces of information for
462 /// backends that would like to do so.
464 template<typename Ty>
467 MFInfo = Ty::template create<Ty>(Allocator, *this);
468 return static_cast<Ty*>(MFInfo);
471 template<typename Ty>
472 const Ty *getInfo() const {
473 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
476 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
477 /// are inserted into the machine function. The block number for a machine
478 /// basic block can be found by using the MBB::getBlockNumber method, this
479 /// method provides the inverse mapping.
481 MachineBasicBlock *getBlockNumbered(unsigned N) const {
482 assert(N < MBBNumbering.size() && "Illegal block number");
483 assert(MBBNumbering[N] && "Block was removed from the machine function!");
484 return MBBNumbering[N];
487 /// Should we be emitting segmented stack stuff for the function
488 bool shouldSplitStack() const;
490 /// getNumBlockIDs - Return the number of MBB ID's allocated.
492 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
494 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
495 /// recomputes them. This guarantees that the MBB numbers are sequential,
496 /// dense, and match the ordering of the blocks within the function. If a
497 /// specific MachineBasicBlock is specified, only that block and those after
498 /// it are renumbered.
499 void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
501 /// print - Print out the MachineFunction in a format suitable for debugging
502 /// to the specified stream.
504 void print(raw_ostream &OS, const SlotIndexes* = nullptr) const;
506 /// viewCFG - This function is meant for use from the debugger. You can just
507 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
508 /// program, displaying the CFG of the current function with the code for each
509 /// basic block inside. This depends on there being a 'dot' and 'gv' program
512 void viewCFG() const;
514 /// viewCFGOnly - This function is meant for use from the debugger. It works
515 /// just like viewCFG, but it does not include the contents of basic blocks
516 /// into the nodes, just the label. If you are only interested in the CFG
517 /// this can make the graph smaller.
519 void viewCFGOnly() const;
521 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
525 /// Run the current MachineFunction through the machine code verifier, useful
526 /// for debugger use.
527 /// \returns true if no problems were found.
528 bool verify(Pass *p = nullptr, const char *Banner = nullptr,
529 bool AbortOnError = true) const;
531 // Provide accessors for the MachineBasicBlock list...
532 typedef BasicBlockListType::iterator iterator;
533 typedef BasicBlockListType::const_iterator const_iterator;
534 typedef BasicBlockListType::const_reverse_iterator const_reverse_iterator;
535 typedef BasicBlockListType::reverse_iterator reverse_iterator;
537 /// Support for MachineBasicBlock::getNextNode().
538 static BasicBlockListType MachineFunction::*
539 getSublistAccess(MachineBasicBlock *) {
540 return &MachineFunction::BasicBlocks;
543 /// addLiveIn - Add the specified physical register as a live-in value and
544 /// create a corresponding virtual register for it.
545 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
547 //===--------------------------------------------------------------------===//
548 // BasicBlock accessor functions.
550 iterator begin() { return BasicBlocks.begin(); }
551 const_iterator begin() const { return BasicBlocks.begin(); }
552 iterator end () { return BasicBlocks.end(); }
553 const_iterator end () const { return BasicBlocks.end(); }
555 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
556 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
557 reverse_iterator rend () { return BasicBlocks.rend(); }
558 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
560 unsigned size() const { return (unsigned)BasicBlocks.size();}
561 bool empty() const { return BasicBlocks.empty(); }
562 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
563 MachineBasicBlock &front() { return BasicBlocks.front(); }
564 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
565 MachineBasicBlock & back() { return BasicBlocks.back(); }
567 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
568 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
569 void insert(iterator MBBI, MachineBasicBlock *MBB) {
570 BasicBlocks.insert(MBBI, MBB);
572 void splice(iterator InsertPt, iterator MBBI) {
573 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
575 void splice(iterator InsertPt, MachineBasicBlock *MBB) {
576 BasicBlocks.splice(InsertPt, BasicBlocks, MBB);
578 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
579 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
582 void remove(iterator MBBI) { BasicBlocks.remove(MBBI); }
583 void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(MBBI); }
584 void erase(iterator MBBI) { BasicBlocks.erase(MBBI); }
585 void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(MBBI); }
587 template <typename Comp>
588 void sort(Comp comp) {
589 BasicBlocks.sort(comp);
592 //===--------------------------------------------------------------------===//
593 // Internal functions used to automatically number MachineBasicBlocks
596 /// \brief Adds the MBB to the internal numbering. Returns the unique number
597 /// assigned to the MBB.
599 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
600 MBBNumbering.push_back(MBB);
601 return (unsigned)MBBNumbering.size()-1;
604 /// removeFromMBBNumbering - Remove the specific machine basic block from our
605 /// tracker, this is only really to be used by the MachineBasicBlock
607 void removeFromMBBNumbering(unsigned N) {
608 assert(N < MBBNumbering.size() && "Illegal basic block #");
609 MBBNumbering[N] = nullptr;
612 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
613 /// of `new MachineInstr'.
615 MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, const DebugLoc &DL,
618 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
619 /// 'Orig' instruction, identical in all ways except the instruction
620 /// has no parent, prev, or next.
622 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
624 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
626 /// DeleteMachineInstr - Delete the given MachineInstr.
628 void DeleteMachineInstr(MachineInstr *MI);
630 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
631 /// instead of `new MachineBasicBlock'.
633 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = nullptr);
635 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
637 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
639 /// getMachineMemOperand - Allocate a new MachineMemOperand.
640 /// MachineMemOperands are owned by the MachineFunction and need not be
641 /// explicitly deallocated.
642 MachineMemOperand *getMachineMemOperand(
643 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
644 unsigned base_alignment, const AAMDNodes &AAInfo = AAMDNodes(),
645 const MDNode *Ranges = nullptr,
646 SynchronizationScope SynchScope = CrossThread,
647 AtomicOrdering Ordering = AtomicOrdering::NotAtomic,
648 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic);
650 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
651 /// an existing one, adjusting by an offset and using the given size.
652 /// MachineMemOperands are owned by the MachineFunction and need not be
653 /// explicitly deallocated.
654 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
655 int64_t Offset, uint64_t Size);
657 typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
659 /// Allocate an array of MachineOperands. This is only intended for use by
660 /// internal MachineInstr functions.
661 MachineOperand *allocateOperandArray(OperandCapacity Cap) {
662 return OperandRecycler.allocate(Cap, Allocator);
665 /// Dellocate an array of MachineOperands and recycle the memory. This is
666 /// only intended for use by internal MachineInstr functions.
667 /// Cap must be the same capacity that was used to allocate the array.
668 void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
669 OperandRecycler.deallocate(Cap, Array);
672 /// \brief Allocate and initialize a register mask with @p NumRegister bits.
673 uint32_t *allocateRegisterMask(unsigned NumRegister) {
674 unsigned Size = (NumRegister + 31) / 32;
675 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
676 for (unsigned i = 0; i != Size; ++i)
681 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
682 /// pointers. This array is owned by the MachineFunction.
683 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
685 /// extractLoadMemRefs - Allocate an array and populate it with just the
686 /// load information from the given MachineMemOperand sequence.
687 std::pair<MachineInstr::mmo_iterator,
688 MachineInstr::mmo_iterator>
689 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
690 MachineInstr::mmo_iterator End);
692 /// extractStoreMemRefs - Allocate an array and populate it with just the
693 /// store information from the given MachineMemOperand sequence.
694 std::pair<MachineInstr::mmo_iterator,
695 MachineInstr::mmo_iterator>
696 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
697 MachineInstr::mmo_iterator End);
699 /// Allocate a string and populate it with the given external symbol name.
700 const char *createExternalSymbolName(StringRef Name);
702 //===--------------------------------------------------------------------===//
703 // Label Manipulation.
706 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
707 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
708 /// normal 'L' label is returned.
709 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
710 bool isLinkerPrivate = false) const;
712 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
714 MCSymbol *getPICBaseSymbol() const;
716 /// Returns a reference to a list of cfi instructions in the function's
717 /// prologue. Used to construct frame maps for debug and exception handling
719 const std::vector<MCCFIInstruction> &getFrameInstructions() const {
720 return FrameInstructions;
723 LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst) {
724 FrameInstructions.push_back(Inst);
725 return FrameInstructions.size() - 1;
728 /// \name Exception Handling
731 bool callsEHReturn() const { return CallsEHReturn; }
732 void setCallsEHReturn(bool b) { CallsEHReturn = b; }
734 bool callsUnwindInit() const { return CallsUnwindInit; }
735 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
737 bool hasEHFunclets() const { return HasEHFunclets; }
738 void setHasEHFunclets(bool V) { HasEHFunclets = V; }
740 /// Find or create an LandingPadInfo for the specified MachineBasicBlock.
741 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
743 /// Remap landing pad labels and remove any deleted landing pads.
744 void tidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr);
746 /// Return a reference to the landing pad info for the current function.
747 const std::vector<LandingPadInfo> &getLandingPads() const {
751 /// Provide the begin and end labels of an invoke style call and associate it
752 /// with a try landing pad block.
753 void addInvoke(MachineBasicBlock *LandingPad,
754 MCSymbol *BeginLabel, MCSymbol *EndLabel);
756 /// Add a new panding pad. Returns the label ID for the landing pad entry.
757 MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
759 /// Provide the catch typeinfo for a landing pad.
760 void addCatchTypeInfo(MachineBasicBlock *LandingPad,
761 ArrayRef<const GlobalValue *> TyInfo);
763 /// Provide the filter typeinfo for a landing pad.
764 void addFilterTypeInfo(MachineBasicBlock *LandingPad,
765 ArrayRef<const GlobalValue *> TyInfo);
767 /// Add a cleanup action for a landing pad.
768 void addCleanup(MachineBasicBlock *LandingPad);
770 void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter,
771 const BlockAddress *RecoverLabel);
773 void addSEHCleanupHandler(MachineBasicBlock *LandingPad,
774 const Function *Cleanup);
776 /// Return the type id for the specified typeinfo. This is function wide.
777 unsigned getTypeIDFor(const GlobalValue *TI);
779 /// Return the id of the filter encoded by TyIds. This is function wide.
780 int getFilterIDFor(std::vector<unsigned> &TyIds);
782 /// Map the landing pad's EH symbol to the call site indexes.
783 void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);
785 /// Get the call site indexes for a landing pad EH symbol.
786 SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) {
787 assert(hasCallSiteLandingPad(Sym) &&
788 "missing call site number for landing pad!");
789 return LPadToCallSiteMap[Sym];
792 /// Return true if the landing pad Eh symbol has an associated call site.
793 bool hasCallSiteLandingPad(MCSymbol *Sym) {
794 return !LPadToCallSiteMap[Sym].empty();
797 /// Map the begin label for a call site.
798 void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) {
799 CallSiteMap[BeginLabel] = Site;
802 /// Get the call site number for a begin label.
803 unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) const {
804 assert(hasCallSiteBeginLabel(BeginLabel) &&
805 "Missing call site number for EH_LABEL!");
806 return CallSiteMap.lookup(BeginLabel);
809 /// Return true if the begin label has a call site number associated with it.
810 bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) const {
811 return CallSiteMap.count(BeginLabel);
814 /// Return a reference to the C++ typeinfo for the current function.
815 const std::vector<const GlobalValue *> &getTypeInfos() const {
819 /// Return a reference to the typeids encoding filters used in the current
821 const std::vector<unsigned> &getFilterIds() const {
827 /// Collect information used to emit debugging information of a variable.
828 void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr,
829 unsigned Slot, const DILocation *Loc) {
830 VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc);
833 VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; }
834 const VariableDbgInfoMapTy &getVariableDbgInfo() const {
835 return VariableDbgInfos;
839 /// \name Exception Handling
842 /// Extract the exception handling information from the landingpad instruction
843 /// and add them to the specified machine module info.
844 void addLandingPadInfo(const LandingPadInst &I, MachineBasicBlock &MBB);
848 //===--------------------------------------------------------------------===//
849 // GraphTraits specializations for function basic block graphs (CFGs)
850 //===--------------------------------------------------------------------===//
852 // Provide specializations of GraphTraits to be able to treat a
853 // machine function as a graph of machine basic blocks... these are
854 // the same as the machine basic block iterators, except that the root
855 // node is implicitly the first node of the function.
857 template <> struct GraphTraits<MachineFunction*> :
858 public GraphTraits<MachineBasicBlock*> {
859 static NodeRef getEntryNode(MachineFunction *F) { return &F->front(); }
861 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
862 typedef pointer_iterator<MachineFunction::iterator> nodes_iterator;
863 static nodes_iterator nodes_begin(MachineFunction *F) {
864 return nodes_iterator(F->begin());
866 static nodes_iterator nodes_end(MachineFunction *F) {
867 return nodes_iterator(F->end());
869 static unsigned size (MachineFunction *F) { return F->size(); }
871 template <> struct GraphTraits<const MachineFunction*> :
872 public GraphTraits<const MachineBasicBlock*> {
873 static NodeRef getEntryNode(const MachineFunction *F) { return &F->front(); }
875 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
876 typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
877 static nodes_iterator nodes_begin(const MachineFunction *F) {
878 return nodes_iterator(F->begin());
880 static nodes_iterator nodes_end (const MachineFunction *F) {
881 return nodes_iterator(F->end());
883 static unsigned size (const MachineFunction *F) {
889 // Provide specializations of GraphTraits to be able to treat a function as a
890 // graph of basic blocks... and to walk it in inverse order. Inverse order for
891 // a function is considered to be when traversing the predecessor edges of a BB
892 // instead of the successor edges.
894 template <> struct GraphTraits<Inverse<MachineFunction*> > :
895 public GraphTraits<Inverse<MachineBasicBlock*> > {
896 static NodeRef getEntryNode(Inverse<MachineFunction *> G) {
897 return &G.Graph->front();
900 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
901 public GraphTraits<Inverse<const MachineBasicBlock*> > {
902 static NodeRef getEntryNode(Inverse<const MachineFunction *> G) {
903 return &G.Graph->front();
907 } // End llvm namespace