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
;
42 class TargetSubtargetInfo
;
43 class TargetRegisterClass
;
44 struct MachinePointerInfo
;
47 struct ilist_traits
<MachineBasicBlock
>
48 : public ilist_default_traits
<MachineBasicBlock
> {
49 mutable ilist_half_node
<MachineBasicBlock
> Sentinel
;
51 MachineBasicBlock
*createSentinel() const {
52 return static_cast<MachineBasicBlock
*>(&Sentinel
);
54 void destroySentinel(MachineBasicBlock
*) const {}
56 MachineBasicBlock
*provideInitialHead() const { return createSentinel(); }
57 MachineBasicBlock
*ensureHead(MachineBasicBlock
*) const {
58 return createSentinel();
60 static void noteHead(MachineBasicBlock
*, MachineBasicBlock
*) {}
62 void addNodeToList(MachineBasicBlock
* MBB
);
63 void removeNodeFromList(MachineBasicBlock
* MBB
);
64 void deleteNode(MachineBasicBlock
*MBB
);
66 void createNode(const MachineBasicBlock
&);
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 class MachineFunction
{
88 const TargetMachine
&Target
;
89 const TargetSubtargetInfo
*STI
;
91 MachineModuleInfo
&MMI
;
93 // RegInfo - Information about each register in use in the function.
94 MachineRegisterInfo
*RegInfo
;
96 // Used to keep track of target-specific per-machine function information for
97 // the target implementation.
98 MachineFunctionInfo
*MFInfo
;
100 // Keep track of objects allocated on the stack.
101 MachineFrameInfo
*FrameInfo
;
103 // Keep track of constants which are spilled to memory
104 MachineConstantPool
*ConstantPool
;
106 // Keep track of jump tables for switch instructions
107 MachineJumpTableInfo
*JumpTableInfo
;
109 // Function-level unique numbering for MachineBasicBlocks. When a
110 // MachineBasicBlock is inserted into a MachineFunction is it automatically
111 // numbered and this vector keeps track of the mapping from ID's to MBB's.
112 std::vector
<MachineBasicBlock
*> MBBNumbering
;
114 // Pool-allocate MachineFunction-lifetime and IR objects.
115 BumpPtrAllocator Allocator
;
117 // Allocation management for instructions in function.
118 Recycler
<MachineInstr
> InstructionRecycler
;
120 // Allocation management for operand arrays on instructions.
121 ArrayRecycler
<MachineOperand
> OperandRecycler
;
123 // Allocation management for basic blocks in function.
124 Recycler
<MachineBasicBlock
> BasicBlockRecycler
;
126 // List of machine basic blocks in function
127 typedef ilist
<MachineBasicBlock
> BasicBlockListType
;
128 BasicBlockListType BasicBlocks
;
130 /// FunctionNumber - This provides a unique ID for each function emitted in
131 /// this translation unit.
133 unsigned FunctionNumber
;
135 /// Alignment - The alignment of the function.
138 /// ExposesReturnsTwice - True if the function calls setjmp or related
139 /// functions with attribute "returns twice", but doesn't have
140 /// the attribute itself.
141 /// This is used to limit optimizations which cannot reason
142 /// about the control flow of such functions.
143 bool ExposesReturnsTwice
;
145 /// True if the function includes any inline assembly.
148 MachineFunction(const MachineFunction
&) LLVM_DELETED_FUNCTION
;
149 void operator=(const MachineFunction
&) LLVM_DELETED_FUNCTION
;
151 MachineFunction(const Function
*Fn
, const TargetMachine
&TM
,
152 unsigned FunctionNum
, MachineModuleInfo
&MMI
);
155 MachineModuleInfo
&getMMI() const { return MMI
; }
156 MCContext
&getContext() const { return Ctx
; }
158 /// getFunction - Return the LLVM function that this machine code represents
160 const Function
*getFunction() const { return Fn
; }
162 /// getName - Return the name of the corresponding LLVM function.
164 StringRef
getName() const;
166 /// getFunctionNumber - Return a unique ID for the current function.
168 unsigned getFunctionNumber() const { return FunctionNumber
; }
170 /// getTarget - Return the target machine this machine code is compiled with
172 const TargetMachine
&getTarget() const { return Target
; }
174 /// getSubtarget - Return the subtarget for which this machine code is being
176 const TargetSubtargetInfo
&getSubtarget() const { return *STI
; }
177 void setSubtarget(const TargetSubtargetInfo
*ST
) { STI
= ST
; }
179 /// getRegInfo - Return information about the registers currently in use.
181 MachineRegisterInfo
&getRegInfo() { return *RegInfo
; }
182 const MachineRegisterInfo
&getRegInfo() const { return *RegInfo
; }
184 /// getFrameInfo - Return the frame info object for the current function.
185 /// This object contains information about objects allocated on the stack
186 /// frame of the current function in an abstract way.
188 MachineFrameInfo
*getFrameInfo() { return FrameInfo
; }
189 const MachineFrameInfo
*getFrameInfo() const { return FrameInfo
; }
191 /// getJumpTableInfo - Return the jump table info object for the current
192 /// function. This object contains information about jump tables in the
193 /// current function. If the current function has no jump tables, this will
195 const MachineJumpTableInfo
*getJumpTableInfo() const { return JumpTableInfo
; }
196 MachineJumpTableInfo
*getJumpTableInfo() { return JumpTableInfo
; }
198 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
199 /// does already exist, allocate one.
200 MachineJumpTableInfo
*getOrCreateJumpTableInfo(unsigned JTEntryKind
);
203 /// getConstantPool - Return the constant pool object for the current
206 MachineConstantPool
*getConstantPool() { return ConstantPool
; }
207 const MachineConstantPool
*getConstantPool() const { return ConstantPool
; }
209 /// getAlignment - Return the alignment (log2, not bytes) of the function.
211 unsigned getAlignment() const { return Alignment
; }
213 /// setAlignment - Set the alignment (log2, not bytes) of the function.
215 void setAlignment(unsigned A
) { Alignment
= A
; }
217 /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
218 void ensureAlignment(unsigned A
) {
219 if (Alignment
< A
) Alignment
= A
;
222 /// exposesReturnsTwice - Returns true if the function calls setjmp or
223 /// any other similar functions with attribute "returns twice" without
224 /// having the attribute itself.
225 bool exposesReturnsTwice() const {
226 return ExposesReturnsTwice
;
229 /// setCallsSetJmp - Set a flag that indicates if there's a call to
230 /// a "returns twice" function.
231 void setExposesReturnsTwice(bool B
) {
232 ExposesReturnsTwice
= B
;
235 /// Returns true if the function contains any inline assembly.
236 bool hasInlineAsm() const {
240 /// Set a flag that indicates that the function contains inline assembly.
241 void setHasInlineAsm(bool B
) {
245 /// getInfo - Keep track of various per-function pieces of information for
246 /// backends that would like to do so.
248 template<typename Ty
>
251 MFInfo
= Ty::template create
<Ty
>(Allocator
, *this);
252 return static_cast<Ty
*>(MFInfo
);
255 template<typename Ty
>
256 const Ty
*getInfo() const {
257 return const_cast<MachineFunction
*>(this)->getInfo
<Ty
>();
260 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
261 /// are inserted into the machine function. The block number for a machine
262 /// basic block can be found by using the MBB::getBlockNumber method, this
263 /// method provides the inverse mapping.
265 MachineBasicBlock
*getBlockNumbered(unsigned N
) const {
266 assert(N
< MBBNumbering
.size() && "Illegal block number");
267 assert(MBBNumbering
[N
] && "Block was removed from the machine function!");
268 return MBBNumbering
[N
];
271 /// Should we be emitting segmented stack stuff for the function
272 bool shouldSplitStack();
274 /// getNumBlockIDs - Return the number of MBB ID's allocated.
276 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering
.size(); }
278 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
279 /// recomputes them. This guarantees that the MBB numbers are sequential,
280 /// dense, and match the ordering of the blocks within the function. If a
281 /// specific MachineBasicBlock is specified, only that block and those after
282 /// it are renumbered.
283 void RenumberBlocks(MachineBasicBlock
*MBBFrom
= nullptr);
285 /// print - Print out the MachineFunction in a format suitable for debugging
286 /// to the specified stream.
288 void print(raw_ostream
&OS
, SlotIndexes
* = nullptr) const;
290 /// viewCFG - This function is meant for use from the debugger. You can just
291 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
292 /// program, displaying the CFG of the current function with the code for each
293 /// basic block inside. This depends on there being a 'dot' and 'gv' program
296 void viewCFG() const;
298 /// viewCFGOnly - This function is meant for use from the debugger. It works
299 /// just like viewCFG, but it does not include the contents of basic blocks
300 /// into the nodes, just the label. If you are only interested in the CFG
301 /// this can make the graph smaller.
303 void viewCFGOnly() const;
305 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
309 /// verify - Run the current MachineFunction through the machine code
310 /// verifier, useful for debugger use.
311 void verify(Pass
*p
= nullptr, const char *Banner
= nullptr) const;
313 // Provide accessors for the MachineBasicBlock list...
314 typedef BasicBlockListType::iterator iterator
;
315 typedef BasicBlockListType::const_iterator const_iterator
;
316 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
317 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
319 /// addLiveIn - Add the specified physical register as a live-in value and
320 /// create a corresponding virtual register for it.
321 unsigned addLiveIn(unsigned PReg
, const TargetRegisterClass
*RC
);
323 //===--------------------------------------------------------------------===//
324 // BasicBlock accessor functions.
326 iterator
begin() { return BasicBlocks
.begin(); }
327 const_iterator
begin() const { return BasicBlocks
.begin(); }
328 iterator
end () { return BasicBlocks
.end(); }
329 const_iterator
end () const { return BasicBlocks
.end(); }
331 reverse_iterator
rbegin() { return BasicBlocks
.rbegin(); }
332 const_reverse_iterator
rbegin() const { return BasicBlocks
.rbegin(); }
333 reverse_iterator
rend () { return BasicBlocks
.rend(); }
334 const_reverse_iterator
rend () const { return BasicBlocks
.rend(); }
336 unsigned size() const { return (unsigned)BasicBlocks
.size();}
337 bool empty() const { return BasicBlocks
.empty(); }
338 const MachineBasicBlock
&front() const { return BasicBlocks
.front(); }
339 MachineBasicBlock
&front() { return BasicBlocks
.front(); }
340 const MachineBasicBlock
& back() const { return BasicBlocks
.back(); }
341 MachineBasicBlock
& back() { return BasicBlocks
.back(); }
343 void push_back (MachineBasicBlock
*MBB
) { BasicBlocks
.push_back (MBB
); }
344 void push_front(MachineBasicBlock
*MBB
) { BasicBlocks
.push_front(MBB
); }
345 void insert(iterator MBBI
, MachineBasicBlock
*MBB
) {
346 BasicBlocks
.insert(MBBI
, MBB
);
348 void splice(iterator InsertPt
, iterator MBBI
) {
349 BasicBlocks
.splice(InsertPt
, BasicBlocks
, MBBI
);
351 void splice(iterator InsertPt
, iterator MBBI
, iterator MBBE
) {
352 BasicBlocks
.splice(InsertPt
, BasicBlocks
, MBBI
, MBBE
);
355 void remove(iterator MBBI
) {
356 BasicBlocks
.remove(MBBI
);
358 void erase(iterator MBBI
) {
359 BasicBlocks
.erase(MBBI
);
362 //===--------------------------------------------------------------------===//
363 // Internal functions used to automatically number MachineBasicBlocks
366 /// \brief Adds the MBB to the internal numbering. Returns the unique number
367 /// assigned to the MBB.
369 unsigned addToMBBNumbering(MachineBasicBlock
*MBB
) {
370 MBBNumbering
.push_back(MBB
);
371 return (unsigned)MBBNumbering
.size()-1;
374 /// removeFromMBBNumbering - Remove the specific machine basic block from our
375 /// tracker, this is only really to be used by the MachineBasicBlock
377 void removeFromMBBNumbering(unsigned N
) {
378 assert(N
< MBBNumbering
.size() && "Illegal basic block #");
379 MBBNumbering
[N
] = nullptr;
382 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
383 /// of `new MachineInstr'.
385 MachineInstr
*CreateMachineInstr(const MCInstrDesc
&MCID
,
389 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
390 /// 'Orig' instruction, identical in all ways except the instruction
391 /// has no parent, prev, or next.
393 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
395 MachineInstr
*CloneMachineInstr(const MachineInstr
*Orig
);
397 /// DeleteMachineInstr - Delete the given MachineInstr.
399 void DeleteMachineInstr(MachineInstr
*MI
);
401 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
402 /// instead of `new MachineBasicBlock'.
404 MachineBasicBlock
*CreateMachineBasicBlock(const BasicBlock
*bb
= nullptr);
406 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
408 void DeleteMachineBasicBlock(MachineBasicBlock
*MBB
);
410 /// getMachineMemOperand - Allocate a new MachineMemOperand.
411 /// MachineMemOperands are owned by the MachineFunction and need not be
412 /// explicitly deallocated.
413 MachineMemOperand
*getMachineMemOperand(MachinePointerInfo PtrInfo
,
414 unsigned f
, uint64_t s
,
415 unsigned base_alignment
,
416 const AAMDNodes
&AAInfo
= AAMDNodes(),
417 const MDNode
*Ranges
= nullptr);
419 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
420 /// an existing one, adjusting by an offset and using the given size.
421 /// MachineMemOperands are owned by the MachineFunction and need not be
422 /// explicitly deallocated.
423 MachineMemOperand
*getMachineMemOperand(const MachineMemOperand
*MMO
,
424 int64_t Offset
, uint64_t Size
);
426 typedef ArrayRecycler
<MachineOperand
>::Capacity OperandCapacity
;
428 /// Allocate an array of MachineOperands. This is only intended for use by
429 /// internal MachineInstr functions.
430 MachineOperand
*allocateOperandArray(OperandCapacity Cap
) {
431 return OperandRecycler
.allocate(Cap
, Allocator
);
434 /// Dellocate an array of MachineOperands and recycle the memory. This is
435 /// only intended for use by internal MachineInstr functions.
436 /// Cap must be the same capacity that was used to allocate the array.
437 void deallocateOperandArray(OperandCapacity Cap
, MachineOperand
*Array
) {
438 OperandRecycler
.deallocate(Cap
, Array
);
441 /// \brief Allocate and initialize a register mask with @p NumRegister bits.
442 uint32_t *allocateRegisterMask(unsigned NumRegister
) {
443 unsigned Size
= (NumRegister
+ 31) / 32;
444 uint32_t *Mask
= Allocator
.Allocate
<uint32_t>(Size
);
445 for (unsigned i
= 0; i
!= Size
; ++i
)
450 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
451 /// pointers. This array is owned by the MachineFunction.
452 MachineInstr::mmo_iterator
allocateMemRefsArray(unsigned long Num
);
454 /// extractLoadMemRefs - Allocate an array and populate it with just the
455 /// load information from the given MachineMemOperand sequence.
456 std::pair
<MachineInstr::mmo_iterator
,
457 MachineInstr::mmo_iterator
>
458 extractLoadMemRefs(MachineInstr::mmo_iterator Begin
,
459 MachineInstr::mmo_iterator End
);
461 /// extractStoreMemRefs - Allocate an array and populate it with just the
462 /// store information from the given MachineMemOperand sequence.
463 std::pair
<MachineInstr::mmo_iterator
,
464 MachineInstr::mmo_iterator
>
465 extractStoreMemRefs(MachineInstr::mmo_iterator Begin
,
466 MachineInstr::mmo_iterator End
);
468 //===--------------------------------------------------------------------===//
469 // Label Manipulation.
472 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
473 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
474 /// normal 'L' label is returned.
475 MCSymbol
*getJTISymbol(unsigned JTI
, MCContext
&Ctx
,
476 bool isLinkerPrivate
= false) const;
478 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
480 MCSymbol
*getPICBaseSymbol() const;
483 //===--------------------------------------------------------------------===//
484 // GraphTraits specializations for function basic block graphs (CFGs)
485 //===--------------------------------------------------------------------===//
487 // Provide specializations of GraphTraits to be able to treat a
488 // machine function as a graph of machine basic blocks... these are
489 // the same as the machine basic block iterators, except that the root
490 // node is implicitly the first node of the function.
492 template <> struct GraphTraits
<MachineFunction
*> :
493 public GraphTraits
<MachineBasicBlock
*> {
494 static NodeType
*getEntryNode(MachineFunction
*F
) {
498 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
499 typedef MachineFunction::iterator nodes_iterator
;
500 static nodes_iterator
nodes_begin(MachineFunction
*F
) { return F
->begin(); }
501 static nodes_iterator
nodes_end (MachineFunction
*F
) { return F
->end(); }
502 static unsigned size (MachineFunction
*F
) { return F
->size(); }
504 template <> struct GraphTraits
<const MachineFunction
*> :
505 public GraphTraits
<const MachineBasicBlock
*> {
506 static NodeType
*getEntryNode(const MachineFunction
*F
) {
510 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
511 typedef MachineFunction::const_iterator nodes_iterator
;
512 static nodes_iterator
nodes_begin(const MachineFunction
*F
) {
515 static nodes_iterator
nodes_end (const MachineFunction
*F
) {
518 static unsigned size (const MachineFunction
*F
) {
524 // Provide specializations of GraphTraits to be able to treat a function as a
525 // graph of basic blocks... and to walk it in inverse order. Inverse order for
526 // a function is considered to be when traversing the predecessor edges of a BB
527 // instead of the successor edges.
529 template <> struct GraphTraits
<Inverse
<MachineFunction
*> > :
530 public GraphTraits
<Inverse
<MachineBasicBlock
*> > {
531 static NodeType
*getEntryNode(Inverse
<MachineFunction
*> G
) {
532 return &G
.Graph
->front();
535 template <> struct GraphTraits
<Inverse
<const MachineFunction
*> > :
536 public GraphTraits
<Inverse
<const MachineBasicBlock
*> > {
537 static NodeType
*getEntryNode(Inverse
<const MachineFunction
*> G
) {
538 return &G
.Graph
->front();
542 } // End llvm namespace