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/alist.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/Support/Annotation.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Recycler.h"
31 class MachineRegisterInfo;
32 class MachineFrameInfo;
33 class MachineConstantPool;
34 class MachineJumpTableInfo;
37 class alist_traits<MachineBasicBlock, MachineBasicBlock> {
38 typedef alist_iterator<MachineBasicBlock> iterator;
40 void addNodeToList(MachineBasicBlock* MBB);
41 void removeNodeFromList(MachineBasicBlock* MBB);
42 void transferNodesFromList(alist_traits<MachineBasicBlock> &,
45 void deleteNode(MachineBasicBlock *MBB);
48 /// MachineFunctionInfo - This class can be derived from and used by targets to
49 /// hold private target-specific information for each MachineFunction. Objects
50 /// of type are accessed/created with MF::getInfo and destroyed when the
51 /// MachineFunction is destroyed.
52 struct MachineFunctionInfo {
53 virtual ~MachineFunctionInfo() {}
56 class MachineFunction : private Annotation {
58 const TargetMachine &Target;
60 // RegInfo - Information about each register in use in the function.
61 MachineRegisterInfo *RegInfo;
63 // Used to keep track of target-specific per-machine function information for
64 // the target implementation.
65 MachineFunctionInfo *MFInfo;
67 // Keep track of objects allocated on the stack.
68 MachineFrameInfo *FrameInfo;
70 // Keep track of constants which are spilled to memory
71 MachineConstantPool *ConstantPool;
73 // Keep track of jump tables for switch instructions
74 MachineJumpTableInfo *JumpTableInfo;
76 // Function-level unique numbering for MachineBasicBlocks. When a
77 // MachineBasicBlock is inserted into a MachineFunction is it automatically
78 // numbered and this vector keeps track of the mapping from ID's to MBB's.
79 std::vector<MachineBasicBlock*> MBBNumbering;
81 // Pool-allocate MachineFunction-lifetime and IR objects.
82 BumpPtrAllocator Allocator;
84 // Allocation management for instructions in function.
85 Recycler<MachineInstr> InstructionRecycler;
87 // Allocation management for basic blocks in function.
88 Recycler<MachineBasicBlock> BasicBlockRecycler;
90 // Allocation management for memoperands in function.
91 Recycler<MachineMemOperand> MemOperandRecycler;
93 // List of machine basic blocks in function
94 typedef alist<MachineBasicBlock> BasicBlockListType;
95 BasicBlockListType BasicBlocks;
98 MachineFunction(const Function *Fn, const TargetMachine &TM);
101 /// getFunction - Return the LLVM function that this machine code represents
103 const Function *getFunction() const { return Fn; }
105 /// getTarget - Return the target machine this machine code is compiled with
107 const TargetMachine &getTarget() const { return Target; }
109 /// getRegInfo - Return information about the registers currently in use.
111 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
112 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
114 /// getFrameInfo - Return the frame info object for the current function.
115 /// This object contains information about objects allocated on the stack
116 /// frame of the current function in an abstract way.
118 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
119 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
121 /// getJumpTableInfo - Return the jump table info object for the current
122 /// function. This object contains information about jump tables for switch
123 /// instructions in the current function.
125 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
126 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
128 /// getConstantPool - Return the constant pool object for the current
131 MachineConstantPool *getConstantPool() { return ConstantPool; }
132 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
134 /// MachineFunctionInfo - Keep track of various per-function pieces of
135 /// information for backends that would like to do so.
137 template<typename Ty>
140 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
141 // that apparently breaks GCC 3.3.
142 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
143 AlignOf<Ty>::Alignment));
144 MFInfo = new (Loc) Ty(*this);
147 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
148 "Invalid concrete type or multiple inheritence for getInfo");
149 return static_cast<Ty*>(MFInfo);
152 template<typename Ty>
153 const Ty *getInfo() const {
154 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
157 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
158 /// are inserted into the machine function. The block number for a machine
159 /// basic block can be found by using the MBB::getBlockNumber method, this
160 /// method provides the inverse mapping.
162 MachineBasicBlock *getBlockNumbered(unsigned N) {
163 assert(N < MBBNumbering.size() && "Illegal block number");
164 assert(MBBNumbering[N] && "Block was removed from the machine function!");
165 return MBBNumbering[N];
168 /// getNumBlockIDs - Return the number of MBB ID's allocated.
170 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
172 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
173 /// recomputes them. This guarantees that the MBB numbers are sequential,
174 /// dense, and match the ordering of the blocks within the function. If a
175 /// specific MachineBasicBlock is specified, only that block and those after
176 /// it are renumbered.
177 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
179 /// print - Print out the MachineFunction in a format suitable for debugging
180 /// to the specified stream.
182 void print(std::ostream &OS) const;
183 void print(std::ostream *OS) const { if (OS) print(*OS); }
185 /// viewCFG - This function is meant for use from the debugger. You can just
186 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
187 /// program, displaying the CFG of the current function with the code for each
188 /// basic block inside. This depends on there being a 'dot' and 'gv' program
191 void viewCFG() const;
193 /// viewCFGOnly - This function is meant for use from the debugger. It works
194 /// just like viewCFG, but it does not include the contents of basic blocks
195 /// into the nodes, just the label. If you are only interested in the CFG
196 /// this can make the graph smaller.
198 void viewCFGOnly() const;
200 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
204 /// construct - Allocate and initialize a MachineFunction for a given Function
207 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
209 /// destruct - Destroy the MachineFunction corresponding to a given Function
211 static void destruct(const Function *F);
213 /// get - Return a handle to a MachineFunction corresponding to the given
214 /// Function. This should not be called before "construct()" for a given
217 static MachineFunction& get(const Function *F);
219 // Provide accessors for the MachineBasicBlock list...
220 typedef BasicBlockListType::iterator iterator;
221 typedef BasicBlockListType::const_iterator const_iterator;
222 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
223 typedef std::reverse_iterator<iterator> reverse_iterator;
225 //===--------------------------------------------------------------------===//
226 // BasicBlock accessor functions.
228 iterator begin() { return BasicBlocks.begin(); }
229 const_iterator begin() const { return BasicBlocks.begin(); }
230 iterator end () { return BasicBlocks.end(); }
231 const_iterator end () const { return BasicBlocks.end(); }
233 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
234 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
235 reverse_iterator rend () { return BasicBlocks.rend(); }
236 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
238 unsigned size() const { return (unsigned)BasicBlocks.size();}
239 bool empty() const { return BasicBlocks.empty(); }
240 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
241 MachineBasicBlock &front() { return BasicBlocks.front(); }
242 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
243 MachineBasicBlock & back() { return BasicBlocks.back(); }
245 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
246 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
247 void insert(iterator MBBI, MachineBasicBlock *MBB) {
248 BasicBlocks.insert(MBBI, MBB);
250 void splice(iterator InsertPt, iterator MBBI) {
251 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
254 void remove(iterator MBBI) {
255 BasicBlocks.remove(MBBI);
257 void erase(iterator MBBI) {
258 BasicBlocks.erase(MBBI);
261 //===--------------------------------------------------------------------===//
262 // Internal functions used to automatically number MachineBasicBlocks
265 /// getNextMBBNumber - Returns the next unique number to be assigned
266 /// to a MachineBasicBlock in this MachineFunction.
268 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
269 MBBNumbering.push_back(MBB);
270 return (unsigned)MBBNumbering.size()-1;
273 /// removeFromMBBNumbering - Remove the specific machine basic block from our
274 /// tracker, this is only really to be used by the MachineBasicBlock
276 void removeFromMBBNumbering(unsigned N) {
277 assert(N < MBBNumbering.size() && "Illegal basic block #");
281 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
282 /// of `new MachineInstr'.
284 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
287 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
288 /// 'Orig' instruction, identical in all ways except the the instruction
289 /// has no parent, prev, or next.
291 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
293 /// DeleteMachineInstr - Delete the given MachineInstr.
295 void DeleteMachineInstr(MachineInstr *MI);
297 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
298 /// instead of `new MachineBasicBlock'.
300 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
302 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
304 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
306 /// CreateMachineMemOperand - Allocate a new MachineMemOperand. Use this
307 /// instead of `new MachineMemOperand'.
309 MachineMemOperand *CreateMachineMemOperand(const MachineMemOperand &MMO);
311 /// DeleteMachineMemOperand - Delete the given MachineMemOperand.
313 void DeleteMachineMemOperand(MachineMemOperand *MMO);
316 //===--------------------------------------------------------------------===//
317 // GraphTraits specializations for function basic block graphs (CFGs)
318 //===--------------------------------------------------------------------===//
320 // Provide specializations of GraphTraits to be able to treat a
321 // machine function as a graph of machine basic blocks... these are
322 // the same as the machine basic block iterators, except that the root
323 // node is implicitly the first node of the function.
325 template <> struct GraphTraits<MachineFunction*> :
326 public GraphTraits<MachineBasicBlock*> {
327 static NodeType *getEntryNode(MachineFunction *F) {
331 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
332 typedef MachineFunction::iterator nodes_iterator;
333 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
334 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
336 template <> struct GraphTraits<const MachineFunction*> :
337 public GraphTraits<const MachineBasicBlock*> {
338 static NodeType *getEntryNode(const MachineFunction *F) {
342 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
343 typedef MachineFunction::const_iterator nodes_iterator;
344 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
345 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
349 // Provide specializations of GraphTraits to be able to treat a function as a
350 // graph of basic blocks... and to walk it in inverse order. Inverse order for
351 // a function is considered to be when traversing the predecessor edges of a BB
352 // instead of the successor edges.
354 template <> struct GraphTraits<Inverse<MachineFunction*> > :
355 public GraphTraits<Inverse<MachineBasicBlock*> > {
356 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
357 return &G.Graph->front();
360 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
361 public GraphTraits<Inverse<const MachineBasicBlock*> > {
362 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
363 return &G.Graph->front();
367 } // End llvm namespace