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/Support/Allocator.h"
24 #include "llvm/Support/ArrayRecycler.h"
25 #include "llvm/Support/DebugLoc.h"
26 #include "llvm/Support/Recycler.h"
33 class MachineRegisterInfo;
34 class MachineFrameInfo;
35 class MachineConstantPool;
36 class MachineJumpTableInfo;
37 class MachineModuleInfo;
41 class TargetRegisterClass;
42 struct MachinePointerInfo;
45 struct ilist_traits<MachineBasicBlock>
46 : public ilist_default_traits<MachineBasicBlock> {
47 mutable ilist_half_node<MachineBasicBlock> Sentinel;
49 MachineBasicBlock *createSentinel() const {
50 return static_cast<MachineBasicBlock*>(&Sentinel);
52 void destroySentinel(MachineBasicBlock *) const {}
54 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
55 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
56 return createSentinel();
58 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
60 void addNodeToList(MachineBasicBlock* MBB);
61 void removeNodeFromList(MachineBasicBlock* MBB);
62 void deleteNode(MachineBasicBlock *MBB);
64 void createNode(const MachineBasicBlock &);
67 /// MachineFunctionInfo - This class can be derived from and used by targets to
68 /// hold private target-specific information for each MachineFunction. Objects
69 /// of type are accessed/created with MF::getInfo and destroyed when the
70 /// MachineFunction is destroyed.
71 struct MachineFunctionInfo {
72 virtual ~MachineFunctionInfo();
75 class MachineFunction {
77 const TargetMachine &Target;
79 MachineModuleInfo &MMI;
82 // RegInfo - Information about each register in use in the function.
83 MachineRegisterInfo *RegInfo;
85 // Used to keep track of target-specific per-machine function information for
86 // the target implementation.
87 MachineFunctionInfo *MFInfo;
89 // Keep track of objects allocated on the stack.
90 MachineFrameInfo *FrameInfo;
92 // Keep track of constants which are spilled to memory
93 MachineConstantPool *ConstantPool;
95 // Keep track of jump tables for switch instructions
96 MachineJumpTableInfo *JumpTableInfo;
98 // Function-level unique numbering for MachineBasicBlocks. When a
99 // MachineBasicBlock is inserted into a MachineFunction is it automatically
100 // numbered and this vector keeps track of the mapping from ID's to MBB's.
101 std::vector<MachineBasicBlock*> MBBNumbering;
103 // Pool-allocate MachineFunction-lifetime and IR objects.
104 BumpPtrAllocator Allocator;
106 // Allocation management for instructions in function.
107 Recycler<MachineInstr> InstructionRecycler;
109 // Allocation management for operand arrays on instructions.
110 ArrayRecycler<MachineOperand> OperandRecycler;
112 // Allocation management for basic blocks in function.
113 Recycler<MachineBasicBlock> BasicBlockRecycler;
115 // List of machine basic blocks in function
116 typedef ilist<MachineBasicBlock> BasicBlockListType;
117 BasicBlockListType BasicBlocks;
119 /// FunctionNumber - This provides a unique ID for each function emitted in
120 /// this translation unit.
122 unsigned FunctionNumber;
124 /// Alignment - The alignment of the function.
127 /// ExposesReturnsTwice - True if the function calls setjmp or related
128 /// functions with attribute "returns twice", but doesn't have
129 /// the attribute itself.
130 /// This is used to limit optimizations which cannot reason
131 /// about the control flow of such functions.
132 bool ExposesReturnsTwice;
134 /// True if the function includes MS-style inline assembly.
137 MachineFunction(const MachineFunction &) LLVM_DELETED_FUNCTION;
138 void operator=(const MachineFunction&) LLVM_DELETED_FUNCTION;
140 MachineFunction(const Function *Fn, const TargetMachine &TM,
141 unsigned FunctionNum, MachineModuleInfo &MMI,
145 MachineModuleInfo &getMMI() const { return MMI; }
146 GCModuleInfo *getGMI() const { return GMI; }
147 MCContext &getContext() const { return Ctx; }
149 /// getFunction - Return the LLVM function that this machine code represents
151 const Function *getFunction() const { return Fn; }
153 /// getName - Return the name of the corresponding LLVM function.
155 StringRef getName() const;
157 /// getFunctionNumber - Return a unique ID for the current function.
159 unsigned getFunctionNumber() const { return FunctionNumber; }
161 /// getTarget - Return the target machine this machine code is compiled with
163 const TargetMachine &getTarget() const { return Target; }
165 /// getRegInfo - Return information about the registers currently in use.
167 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
168 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
170 /// getFrameInfo - Return the frame info object for the current function.
171 /// This object contains information about objects allocated on the stack
172 /// frame of the current function in an abstract way.
174 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
175 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
177 /// getJumpTableInfo - Return the jump table info object for the current
178 /// function. This object contains information about jump tables in the
179 /// current function. If the current function has no jump tables, this will
181 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
182 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
184 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
185 /// does already exist, allocate one.
186 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
189 /// getConstantPool - Return the constant pool object for the current
192 MachineConstantPool *getConstantPool() { return ConstantPool; }
193 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
195 /// getAlignment - Return the alignment (log2, not bytes) of the function.
197 unsigned getAlignment() const { return Alignment; }
199 /// setAlignment - Set the alignment (log2, not bytes) of the function.
201 void setAlignment(unsigned A) { Alignment = A; }
203 /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
204 void ensureAlignment(unsigned A) {
205 if (Alignment < A) Alignment = A;
208 /// exposesReturnsTwice - Returns true if the function calls setjmp or
209 /// any other similar functions with attribute "returns twice" without
210 /// having the attribute itself.
211 bool exposesReturnsTwice() const {
212 return ExposesReturnsTwice;
215 /// setCallsSetJmp - Set a flag that indicates if there's a call to
216 /// a "returns twice" function.
217 void setExposesReturnsTwice(bool B) {
218 ExposesReturnsTwice = B;
221 /// Returns true if the function contains any MS-style inline assembly.
222 bool hasMSInlineAsm() const {
223 return HasMSInlineAsm;
226 /// Set a flag that indicates that the function contains MS-style inline
228 void setHasMSInlineAsm(bool B) {
232 /// getInfo - Keep track of various per-function pieces of information for
233 /// backends that would like to do so.
235 template<typename Ty>
238 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
239 // that apparently breaks GCC 3.3.
240 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
241 AlignOf<Ty>::Alignment));
242 MFInfo = new (Loc) Ty(*this);
244 return static_cast<Ty*>(MFInfo);
247 template<typename Ty>
248 const Ty *getInfo() const {
249 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
252 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
253 /// are inserted into the machine function. The block number for a machine
254 /// basic block can be found by using the MBB::getBlockNumber method, this
255 /// method provides the inverse mapping.
257 MachineBasicBlock *getBlockNumbered(unsigned N) const {
258 assert(N < MBBNumbering.size() && "Illegal block number");
259 assert(MBBNumbering[N] && "Block was removed from the machine function!");
260 return MBBNumbering[N];
263 /// getNumBlockIDs - Return the number of MBB ID's allocated.
265 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
267 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
268 /// recomputes them. This guarantees that the MBB numbers are sequential,
269 /// dense, and match the ordering of the blocks within the function. If a
270 /// specific MachineBasicBlock is specified, only that block and those after
271 /// it are renumbered.
272 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
274 /// print - Print out the MachineFunction in a format suitable for debugging
275 /// to the specified stream.
277 void print(raw_ostream &OS, SlotIndexes* = 0) const;
279 /// viewCFG - This function is meant for use from the debugger. You can just
280 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
281 /// program, displaying the CFG of the current function with the code for each
282 /// basic block inside. This depends on there being a 'dot' and 'gv' program
285 void viewCFG() const;
287 /// viewCFGOnly - This function is meant for use from the debugger. It works
288 /// just like viewCFG, but it does not include the contents of basic blocks
289 /// into the nodes, just the label. If you are only interested in the CFG
290 /// this can make the graph smaller.
292 void viewCFGOnly() const;
294 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
298 /// verify - Run the current MachineFunction through the machine code
299 /// verifier, useful for debugger use.
300 void verify(Pass *p = NULL, const char *Banner = NULL) const;
302 // Provide accessors for the MachineBasicBlock list...
303 typedef BasicBlockListType::iterator iterator;
304 typedef BasicBlockListType::const_iterator const_iterator;
305 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
306 typedef std::reverse_iterator<iterator> reverse_iterator;
308 /// addLiveIn - Add the specified physical register as a live-in value and
309 /// create a corresponding virtual register for it.
310 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
312 //===--------------------------------------------------------------------===//
313 // BasicBlock accessor functions.
315 iterator begin() { return BasicBlocks.begin(); }
316 const_iterator begin() const { return BasicBlocks.begin(); }
317 iterator end () { return BasicBlocks.end(); }
318 const_iterator end () const { return BasicBlocks.end(); }
320 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
321 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
322 reverse_iterator rend () { return BasicBlocks.rend(); }
323 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
325 unsigned size() const { return (unsigned)BasicBlocks.size();}
326 bool empty() const { return BasicBlocks.empty(); }
327 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
328 MachineBasicBlock &front() { return BasicBlocks.front(); }
329 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
330 MachineBasicBlock & back() { return BasicBlocks.back(); }
332 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
333 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
334 void insert(iterator MBBI, MachineBasicBlock *MBB) {
335 BasicBlocks.insert(MBBI, MBB);
337 void splice(iterator InsertPt, iterator MBBI) {
338 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
340 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
341 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
344 void remove(iterator MBBI) {
345 BasicBlocks.remove(MBBI);
347 void erase(iterator MBBI) {
348 BasicBlocks.erase(MBBI);
351 //===--------------------------------------------------------------------===//
352 // Internal functions used to automatically number MachineBasicBlocks
355 /// \brief Adds the MBB to the internal numbering. Returns the unique number
356 /// assigned to the MBB.
358 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
359 MBBNumbering.push_back(MBB);
360 return (unsigned)MBBNumbering.size()-1;
363 /// removeFromMBBNumbering - Remove the specific machine basic block from our
364 /// tracker, this is only really to be used by the MachineBasicBlock
366 void removeFromMBBNumbering(unsigned N) {
367 assert(N < MBBNumbering.size() && "Illegal basic block #");
371 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
372 /// of `new MachineInstr'.
374 MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
378 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
379 /// 'Orig' instruction, identical in all ways except the instruction
380 /// has no parent, prev, or next.
382 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
384 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
386 /// DeleteMachineInstr - Delete the given MachineInstr.
388 void DeleteMachineInstr(MachineInstr *MI);
390 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
391 /// instead of `new MachineBasicBlock'.
393 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
395 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
397 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
399 /// getMachineMemOperand - Allocate a new MachineMemOperand.
400 /// MachineMemOperands are owned by the MachineFunction and need not be
401 /// explicitly deallocated.
402 MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
403 unsigned f, uint64_t s,
404 unsigned base_alignment,
405 const MDNode *TBAAInfo = 0,
406 const MDNode *Ranges = 0);
408 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
409 /// an existing one, adjusting by an offset and using the given size.
410 /// MachineMemOperands are owned by the MachineFunction and need not be
411 /// explicitly deallocated.
412 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
413 int64_t Offset, uint64_t Size);
415 typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
417 /// Allocate an array of MachineOperands. This is only intended for use by
418 /// internal MachineInstr functions.
419 MachineOperand *allocateOperandArray(OperandCapacity Cap) {
420 return OperandRecycler.allocate(Cap, Allocator);
423 /// Dellocate an array of MachineOperands and recycle the memory. This is
424 /// only intended for use by internal MachineInstr functions.
425 /// Cap must be the same capacity that was used to allocate the array.
426 void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
427 OperandRecycler.deallocate(Cap, Array);
430 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
431 /// pointers. This array is owned by the MachineFunction.
432 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
434 /// extractLoadMemRefs - Allocate an array and populate it with just the
435 /// load information from the given MachineMemOperand sequence.
436 std::pair<MachineInstr::mmo_iterator,
437 MachineInstr::mmo_iterator>
438 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
439 MachineInstr::mmo_iterator End);
441 /// extractStoreMemRefs - Allocate an array and populate it with just the
442 /// store information from the given MachineMemOperand sequence.
443 std::pair<MachineInstr::mmo_iterator,
444 MachineInstr::mmo_iterator>
445 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
446 MachineInstr::mmo_iterator End);
448 //===--------------------------------------------------------------------===//
449 // Label Manipulation.
452 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
453 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
454 /// normal 'L' label is returned.
455 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
456 bool isLinkerPrivate = false) const;
458 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
460 MCSymbol *getPICBaseSymbol() const;
463 //===--------------------------------------------------------------------===//
464 // GraphTraits specializations for function basic block graphs (CFGs)
465 //===--------------------------------------------------------------------===//
467 // Provide specializations of GraphTraits to be able to treat a
468 // machine function as a graph of machine basic blocks... these are
469 // the same as the machine basic block iterators, except that the root
470 // node is implicitly the first node of the function.
472 template <> struct GraphTraits<MachineFunction*> :
473 public GraphTraits<MachineBasicBlock*> {
474 static NodeType *getEntryNode(MachineFunction *F) {
478 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
479 typedef MachineFunction::iterator nodes_iterator;
480 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
481 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
482 static unsigned size (MachineFunction *F) { return F->size(); }
484 template <> struct GraphTraits<const MachineFunction*> :
485 public GraphTraits<const MachineBasicBlock*> {
486 static NodeType *getEntryNode(const MachineFunction *F) {
490 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
491 typedef MachineFunction::const_iterator nodes_iterator;
492 static nodes_iterator nodes_begin(const MachineFunction *F) {
495 static nodes_iterator nodes_end (const MachineFunction *F) {
498 static unsigned size (const MachineFunction *F) {
504 // Provide specializations of GraphTraits to be able to treat a function as a
505 // graph of basic blocks... and to walk it in inverse order. Inverse order for
506 // a function is considered to be when traversing the predecessor edges of a BB
507 // instead of the successor edges.
509 template <> struct GraphTraits<Inverse<MachineFunction*> > :
510 public GraphTraits<Inverse<MachineBasicBlock*> > {
511 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
512 return &G.Graph->front();
515 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
516 public GraphTraits<Inverse<const MachineBasicBlock*> > {
517 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
518 return &G.Graph->front();
522 } // End llvm namespace