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/ilist.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/IR/DebugLoc.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/ArrayRecycler.h"
27 #include "llvm/Support/Recycler.h"
34 class MachineRegisterInfo;
35 class MachineFrameInfo;
36 class MachineConstantPool;
37 class MachineJumpTableInfo;
38 class MachineModuleInfo;
41 class PseudoSourceValueManager;
43 class TargetSubtargetInfo;
44 class TargetRegisterClass;
45 struct MachinePointerInfo;
49 struct ilist_traits<MachineBasicBlock>
50 : public ilist_default_traits<MachineBasicBlock> {
51 mutable ilist_half_node<MachineBasicBlock> Sentinel;
53 MachineBasicBlock *createSentinel() const {
54 return static_cast<MachineBasicBlock*>(&Sentinel);
56 void destroySentinel(MachineBasicBlock *) const {}
58 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
59 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
60 return createSentinel();
62 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
64 void addNodeToList(MachineBasicBlock* MBB);
65 void removeNodeFromList(MachineBasicBlock* MBB);
66 void deleteNode(MachineBasicBlock *MBB);
68 void createNode(const MachineBasicBlock &);
71 /// MachineFunctionInfo - This class can be derived from and used by targets to
72 /// hold private target-specific information for each MachineFunction. Objects
73 /// of type are accessed/created with MF::getInfo and destroyed when the
74 /// MachineFunction is destroyed.
75 struct MachineFunctionInfo {
76 virtual ~MachineFunctionInfo();
78 /// \brief Factory function: default behavior is to call new using the
79 /// supplied allocator.
81 /// This function can be overridden in a derive class.
83 static Ty *create(BumpPtrAllocator &Allocator, MachineFunction &MF) {
84 return new (Allocator.Allocate<Ty>()) Ty(MF);
88 class MachineFunction {
90 const TargetMachine &Target;
91 const TargetSubtargetInfo *STI;
93 MachineModuleInfo &MMI;
95 // RegInfo - Information about each register in use in the function.
96 MachineRegisterInfo *RegInfo;
98 // Used to keep track of target-specific per-machine function information for
99 // the target implementation.
100 MachineFunctionInfo *MFInfo;
102 // Keep track of objects allocated on the stack.
103 MachineFrameInfo *FrameInfo;
105 // Keep track of constants which are spilled to memory
106 MachineConstantPool *ConstantPool;
108 // Keep track of jump tables for switch instructions
109 MachineJumpTableInfo *JumpTableInfo;
111 // Keeps track of Windows exception handling related data. This will be null
112 // for functions that aren't using a funclet-based EH personality.
113 WinEHFuncInfo *WinEHInfo = nullptr;
115 // Function-level unique numbering for MachineBasicBlocks. When a
116 // MachineBasicBlock is inserted into a MachineFunction is it automatically
117 // numbered and this vector keeps track of the mapping from ID's to MBB's.
118 std::vector<MachineBasicBlock*> MBBNumbering;
120 // Pool-allocate MachineFunction-lifetime and IR objects.
121 BumpPtrAllocator Allocator;
123 // Allocation management for instructions in function.
124 Recycler<MachineInstr> InstructionRecycler;
126 // Allocation management for operand arrays on instructions.
127 ArrayRecycler<MachineOperand> OperandRecycler;
129 // Allocation management for basic blocks in function.
130 Recycler<MachineBasicBlock> BasicBlockRecycler;
132 // List of machine basic blocks in function
133 typedef ilist<MachineBasicBlock> BasicBlockListType;
134 BasicBlockListType BasicBlocks;
136 /// FunctionNumber - This provides a unique ID for each function emitted in
137 /// this translation unit.
139 unsigned FunctionNumber;
141 /// Alignment - The alignment of the function.
144 /// ExposesReturnsTwice - True if the function calls setjmp or related
145 /// functions with attribute "returns twice", but doesn't have
146 /// the attribute itself.
147 /// This is used to limit optimizations which cannot reason
148 /// about the control flow of such functions.
149 bool ExposesReturnsTwice;
151 /// True if the function includes any inline assembly.
154 // Allocation management for pseudo source values.
155 std::unique_ptr<PseudoSourceValueManager> PSVManager;
157 MachineFunction(const MachineFunction &) = delete;
158 void operator=(const MachineFunction&) = delete;
160 MachineFunction(const Function *Fn, const TargetMachine &TM,
161 unsigned FunctionNum, MachineModuleInfo &MMI);
164 MachineModuleInfo &getMMI() const { return MMI; }
165 MCContext &getContext() const { return Ctx; }
167 PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }
169 /// Return the DataLayout attached to the Module associated to this MF.
170 const DataLayout &getDataLayout() const;
172 /// getFunction - Return the LLVM function that this machine code represents
174 const Function *getFunction() const { return Fn; }
176 /// getName - Return the name of the corresponding LLVM function.
178 StringRef getName() const;
180 /// getFunctionNumber - Return a unique ID for the current function.
182 unsigned getFunctionNumber() const { return FunctionNumber; }
184 /// getTarget - Return the target machine this machine code is compiled with
186 const TargetMachine &getTarget() const { return Target; }
188 /// getSubtarget - Return the subtarget for which this machine code is being
190 const TargetSubtargetInfo &getSubtarget() const { return *STI; }
191 void setSubtarget(const TargetSubtargetInfo *ST) { STI = ST; }
193 /// getSubtarget - This method returns a pointer to the specified type of
194 /// TargetSubtargetInfo. In debug builds, it verifies that the object being
195 /// returned is of the correct type.
196 template<typename STC> const STC &getSubtarget() const {
197 return *static_cast<const STC *>(STI);
200 /// getRegInfo - Return information about the registers currently in use.
202 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
203 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
205 /// getFrameInfo - Return the frame info object for the current function.
206 /// This object contains information about objects allocated on the stack
207 /// frame of the current function in an abstract way.
209 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
210 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
212 /// getJumpTableInfo - Return the jump table info object for the current
213 /// function. This object contains information about jump tables in the
214 /// current function. If the current function has no jump tables, this will
216 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
217 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
219 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
220 /// does already exist, allocate one.
221 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
223 /// getConstantPool - Return the constant pool object for the current
226 MachineConstantPool *getConstantPool() { return ConstantPool; }
227 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
229 /// getWinEHFuncInfo - Return information about how the current function uses
230 /// Windows exception handling. Returns null for functions that don't use
231 /// funclets for exception handling.
232 const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; }
233 WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; }
235 /// getAlignment - Return the alignment (log2, not bytes) of the function.
237 unsigned getAlignment() const { return Alignment; }
239 /// setAlignment - Set the alignment (log2, not bytes) of the function.
241 void setAlignment(unsigned A) { Alignment = A; }
243 /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
244 void ensureAlignment(unsigned A) {
245 if (Alignment < A) Alignment = A;
248 /// exposesReturnsTwice - Returns true if the function calls setjmp or
249 /// any other similar functions with attribute "returns twice" without
250 /// having the attribute itself.
251 bool exposesReturnsTwice() const {
252 return ExposesReturnsTwice;
255 /// setCallsSetJmp - Set a flag that indicates if there's a call to
256 /// a "returns twice" function.
257 void setExposesReturnsTwice(bool B) {
258 ExposesReturnsTwice = B;
261 /// Returns true if the function contains any inline assembly.
262 bool hasInlineAsm() const {
266 /// Set a flag that indicates that the function contains inline assembly.
267 void setHasInlineAsm(bool B) {
271 /// getInfo - Keep track of various per-function pieces of information for
272 /// backends that would like to do so.
274 template<typename Ty>
277 MFInfo = Ty::template create<Ty>(Allocator, *this);
278 return static_cast<Ty*>(MFInfo);
281 template<typename Ty>
282 const Ty *getInfo() const {
283 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
286 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
287 /// are inserted into the machine function. The block number for a machine
288 /// basic block can be found by using the MBB::getBlockNumber method, this
289 /// method provides the inverse mapping.
291 MachineBasicBlock *getBlockNumbered(unsigned N) const {
292 assert(N < MBBNumbering.size() && "Illegal block number");
293 assert(MBBNumbering[N] && "Block was removed from the machine function!");
294 return MBBNumbering[N];
297 /// Should we be emitting segmented stack stuff for the function
298 bool shouldSplitStack();
300 /// getNumBlockIDs - Return the number of MBB ID's allocated.
302 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
304 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
305 /// recomputes them. This guarantees that the MBB numbers are sequential,
306 /// dense, and match the ordering of the blocks within the function. If a
307 /// specific MachineBasicBlock is specified, only that block and those after
308 /// it are renumbered.
309 void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
311 /// print - Print out the MachineFunction in a format suitable for debugging
312 /// to the specified stream.
314 void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
316 /// viewCFG - This function is meant for use from the debugger. You can just
317 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
318 /// program, displaying the CFG of the current function with the code for each
319 /// basic block inside. This depends on there being a 'dot' and 'gv' program
322 void viewCFG() const;
324 /// viewCFGOnly - This function is meant for use from the debugger. It works
325 /// just like viewCFG, but it does not include the contents of basic blocks
326 /// into the nodes, just the label. If you are only interested in the CFG
327 /// this can make the graph smaller.
329 void viewCFGOnly() const;
331 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
335 /// verify - Run the current MachineFunction through the machine code
336 /// verifier, useful for debugger use.
337 void verify(Pass *p = nullptr, const char *Banner = nullptr) const;
339 // Provide accessors for the MachineBasicBlock list...
340 typedef BasicBlockListType::iterator iterator;
341 typedef BasicBlockListType::const_iterator const_iterator;
342 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
343 typedef std::reverse_iterator<iterator> reverse_iterator;
345 /// Support for MachineBasicBlock::getNextNode().
346 static BasicBlockListType MachineFunction::*
347 getSublistAccess(MachineBasicBlock *) {
348 return &MachineFunction::BasicBlocks;
351 /// addLiveIn - Add the specified physical register as a live-in value and
352 /// create a corresponding virtual register for it.
353 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
355 //===--------------------------------------------------------------------===//
356 // BasicBlock accessor functions.
358 iterator begin() { return BasicBlocks.begin(); }
359 const_iterator begin() const { return BasicBlocks.begin(); }
360 iterator end () { return BasicBlocks.end(); }
361 const_iterator end () const { return BasicBlocks.end(); }
363 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
364 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
365 reverse_iterator rend () { return BasicBlocks.rend(); }
366 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
368 unsigned size() const { return (unsigned)BasicBlocks.size();}
369 bool empty() const { return BasicBlocks.empty(); }
370 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
371 MachineBasicBlock &front() { return BasicBlocks.front(); }
372 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
373 MachineBasicBlock & back() { return BasicBlocks.back(); }
375 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
376 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
377 void insert(iterator MBBI, MachineBasicBlock *MBB) {
378 BasicBlocks.insert(MBBI, MBB);
380 void splice(iterator InsertPt, iterator MBBI) {
381 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
383 void splice(iterator InsertPt, MachineBasicBlock *MBB) {
384 BasicBlocks.splice(InsertPt, BasicBlocks, MBB);
386 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
387 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
390 void remove(iterator MBBI) { BasicBlocks.remove(MBBI); }
391 void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(MBBI); }
392 void erase(iterator MBBI) { BasicBlocks.erase(MBBI); }
393 void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(MBBI); }
395 template <typename Comp>
396 void sort(Comp comp) {
397 BasicBlocks.sort(comp);
400 //===--------------------------------------------------------------------===//
401 // Internal functions used to automatically number MachineBasicBlocks
404 /// \brief Adds the MBB to the internal numbering. Returns the unique number
405 /// assigned to the MBB.
407 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
408 MBBNumbering.push_back(MBB);
409 return (unsigned)MBBNumbering.size()-1;
412 /// removeFromMBBNumbering - Remove the specific machine basic block from our
413 /// tracker, this is only really to be used by the MachineBasicBlock
415 void removeFromMBBNumbering(unsigned N) {
416 assert(N < MBBNumbering.size() && "Illegal basic block #");
417 MBBNumbering[N] = nullptr;
420 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
421 /// of `new MachineInstr'.
423 MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
427 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
428 /// 'Orig' instruction, identical in all ways except the instruction
429 /// has no parent, prev, or next.
431 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
433 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
435 /// DeleteMachineInstr - Delete the given MachineInstr.
437 void DeleteMachineInstr(MachineInstr *MI);
439 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
440 /// instead of `new MachineBasicBlock'.
442 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = nullptr);
444 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
446 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
448 /// getMachineMemOperand - Allocate a new MachineMemOperand.
449 /// MachineMemOperands are owned by the MachineFunction and need not be
450 /// explicitly deallocated.
451 MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
452 unsigned f, uint64_t s,
453 unsigned base_alignment,
454 const AAMDNodes &AAInfo = AAMDNodes(),
455 const MDNode *Ranges = nullptr);
457 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
458 /// an existing one, adjusting by an offset and using the given size.
459 /// MachineMemOperands are owned by the MachineFunction and need not be
460 /// explicitly deallocated.
461 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
462 int64_t Offset, uint64_t Size);
464 typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
466 /// Allocate an array of MachineOperands. This is only intended for use by
467 /// internal MachineInstr functions.
468 MachineOperand *allocateOperandArray(OperandCapacity Cap) {
469 return OperandRecycler.allocate(Cap, Allocator);
472 /// Dellocate an array of MachineOperands and recycle the memory. This is
473 /// only intended for use by internal MachineInstr functions.
474 /// Cap must be the same capacity that was used to allocate the array.
475 void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
476 OperandRecycler.deallocate(Cap, Array);
479 /// \brief Allocate and initialize a register mask with @p NumRegister bits.
480 uint32_t *allocateRegisterMask(unsigned NumRegister) {
481 unsigned Size = (NumRegister + 31) / 32;
482 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
483 for (unsigned i = 0; i != Size; ++i)
488 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
489 /// pointers. This array is owned by the MachineFunction.
490 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
492 /// extractLoadMemRefs - Allocate an array and populate it with just the
493 /// load information from the given MachineMemOperand sequence.
494 std::pair<MachineInstr::mmo_iterator,
495 MachineInstr::mmo_iterator>
496 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
497 MachineInstr::mmo_iterator End);
499 /// extractStoreMemRefs - Allocate an array and populate it with just the
500 /// store information from the given MachineMemOperand sequence.
501 std::pair<MachineInstr::mmo_iterator,
502 MachineInstr::mmo_iterator>
503 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
504 MachineInstr::mmo_iterator End);
506 /// Allocate a string and populate it with the given external symbol name.
507 const char *createExternalSymbolName(StringRef Name);
509 //===--------------------------------------------------------------------===//
510 // Label Manipulation.
513 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
514 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
515 /// normal 'L' label is returned.
516 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
517 bool isLinkerPrivate = false) const;
519 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
521 MCSymbol *getPICBaseSymbol() const;
524 //===--------------------------------------------------------------------===//
525 // GraphTraits specializations for function basic block graphs (CFGs)
526 //===--------------------------------------------------------------------===//
528 // Provide specializations of GraphTraits to be able to treat a
529 // machine function as a graph of machine basic blocks... these are
530 // the same as the machine basic block iterators, except that the root
531 // node is implicitly the first node of the function.
533 template <> struct GraphTraits<MachineFunction*> :
534 public GraphTraits<MachineBasicBlock*> {
535 static NodeType *getEntryNode(MachineFunction *F) {
539 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
540 typedef MachineFunction::iterator nodes_iterator;
541 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
542 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
543 static unsigned size (MachineFunction *F) { return F->size(); }
545 template <> struct GraphTraits<const MachineFunction*> :
546 public GraphTraits<const MachineBasicBlock*> {
547 static NodeType *getEntryNode(const MachineFunction *F) {
551 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
552 typedef MachineFunction::const_iterator nodes_iterator;
553 static nodes_iterator nodes_begin(const MachineFunction *F) {
556 static nodes_iterator nodes_end (const MachineFunction *F) {
559 static unsigned size (const MachineFunction *F) {
565 // Provide specializations of GraphTraits to be able to treat a function as a
566 // graph of basic blocks... and to walk it in inverse order. Inverse order for
567 // a function is considered to be when traversing the predecessor edges of a BB
568 // instead of the successor edges.
570 template <> struct GraphTraits<Inverse<MachineFunction*> > :
571 public GraphTraits<Inverse<MachineBasicBlock*> > {
572 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
573 return &G.Graph->front();
576 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
577 public GraphTraits<Inverse<const MachineBasicBlock*> > {
578 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
579 return &G.Graph->front();
583 } // End llvm namespace