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>
139 if (!MFInfo) MFInfo = new (Allocator.Allocate<Ty>()) Ty(*this);
141 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
142 "Invalid concrete type or multiple inheritence for getInfo");
143 return static_cast<Ty*>(MFInfo);
146 template<typename Ty>
147 const Ty *getInfo() const {
148 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
151 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
152 /// are inserted into the machine function. The block number for a machine
153 /// basic block can be found by using the MBB::getBlockNumber method, this
154 /// method provides the inverse mapping.
156 MachineBasicBlock *getBlockNumbered(unsigned N) {
157 assert(N < MBBNumbering.size() && "Illegal block number");
158 assert(MBBNumbering[N] && "Block was removed from the machine function!");
159 return MBBNumbering[N];
162 /// getNumBlockIDs - Return the number of MBB ID's allocated.
164 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
166 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
167 /// recomputes them. This guarantees that the MBB numbers are sequential,
168 /// dense, and match the ordering of the blocks within the function. If a
169 /// specific MachineBasicBlock is specified, only that block and those after
170 /// it are renumbered.
171 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
173 /// print - Print out the MachineFunction in a format suitable for debugging
174 /// to the specified stream.
176 void print(std::ostream &OS) const;
177 void print(std::ostream *OS) const { if (OS) print(*OS); }
179 /// viewCFG - This function is meant for use from the debugger. You can just
180 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
181 /// program, displaying the CFG of the current function with the code for each
182 /// basic block inside. This depends on there being a 'dot' and 'gv' program
185 void viewCFG() const;
187 /// viewCFGOnly - This function is meant for use from the debugger. It works
188 /// just like viewCFG, but it does not include the contents of basic blocks
189 /// into the nodes, just the label. If you are only interested in the CFG
190 /// this can make the graph smaller.
192 void viewCFGOnly() const;
194 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
198 /// construct - Allocate and initialize a MachineFunction for a given Function
201 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
203 /// destruct - Destroy the MachineFunction corresponding to a given Function
205 static void destruct(const Function *F);
207 /// get - Return a handle to a MachineFunction corresponding to the given
208 /// Function. This should not be called before "construct()" for a given
211 static MachineFunction& get(const Function *F);
213 // Provide accessors for the MachineBasicBlock list...
214 typedef BasicBlockListType::iterator iterator;
215 typedef BasicBlockListType::const_iterator const_iterator;
216 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
217 typedef std::reverse_iterator<iterator> reverse_iterator;
219 //===--------------------------------------------------------------------===//
220 // BasicBlock accessor functions.
222 iterator begin() { return BasicBlocks.begin(); }
223 const_iterator begin() const { return BasicBlocks.begin(); }
224 iterator end () { return BasicBlocks.end(); }
225 const_iterator end () const { return BasicBlocks.end(); }
227 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
228 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
229 reverse_iterator rend () { return BasicBlocks.rend(); }
230 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
232 unsigned size() const { return (unsigned)BasicBlocks.size();}
233 bool empty() const { return BasicBlocks.empty(); }
234 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
235 MachineBasicBlock &front() { return BasicBlocks.front(); }
236 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
237 MachineBasicBlock & back() { return BasicBlocks.back(); }
239 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
240 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
241 void insert(iterator MBBI, MachineBasicBlock *MBB) {
242 BasicBlocks.insert(MBBI, MBB);
244 void splice(iterator InsertPt, iterator MBBI) {
245 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
248 void remove(iterator MBBI) {
249 BasicBlocks.remove(MBBI);
251 void erase(iterator MBBI) {
252 BasicBlocks.erase(MBBI);
255 //===--------------------------------------------------------------------===//
256 // Internal functions used to automatically number MachineBasicBlocks
259 /// getNextMBBNumber - Returns the next unique number to be assigned
260 /// to a MachineBasicBlock in this MachineFunction.
262 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
263 MBBNumbering.push_back(MBB);
264 return (unsigned)MBBNumbering.size()-1;
267 /// removeFromMBBNumbering - Remove the specific machine basic block from our
268 /// tracker, this is only really to be used by the MachineBasicBlock
270 void removeFromMBBNumbering(unsigned N) {
271 assert(N < MBBNumbering.size() && "Illegal basic block #");
275 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
276 /// of `new MachineInstr'.
278 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
281 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
282 /// 'Orig' instruction, identical in all ways except the the instruction
283 /// has no parent, prev, or next.
285 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
287 /// DeleteMachineInstr - Delete the given MachineInstr.
289 void DeleteMachineInstr(MachineInstr *MI);
291 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
292 /// instead of `new MachineBasicBlock'.
294 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
296 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
298 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
300 /// CreateMachineMemOperand - Allocate a new MachineMemOperand. Use this
301 /// instead of `new MachineMemOperand'.
303 MachineMemOperand *CreateMachineMemOperand(const MachineMemOperand &MMO);
305 /// DeleteMachineMemOperand - Delete the given MachineMemOperand.
307 void DeleteMachineMemOperand(MachineMemOperand *MMO);
310 //===--------------------------------------------------------------------===//
311 // GraphTraits specializations for function basic block graphs (CFGs)
312 //===--------------------------------------------------------------------===//
314 // Provide specializations of GraphTraits to be able to treat a
315 // machine function as a graph of machine basic blocks... these are
316 // the same as the machine basic block iterators, except that the root
317 // node is implicitly the first node of the function.
319 template <> struct GraphTraits<MachineFunction*> :
320 public GraphTraits<MachineBasicBlock*> {
321 static NodeType *getEntryNode(MachineFunction *F) {
325 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
326 typedef MachineFunction::iterator nodes_iterator;
327 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
328 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
330 template <> struct GraphTraits<const MachineFunction*> :
331 public GraphTraits<const MachineBasicBlock*> {
332 static NodeType *getEntryNode(const MachineFunction *F) {
336 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
337 typedef MachineFunction::const_iterator nodes_iterator;
338 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
339 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
343 // Provide specializations of GraphTraits to be able to treat a function as a
344 // graph of basic blocks... and to walk it in inverse order. Inverse order for
345 // a function is considered to be when traversing the predecessor edges of a BB
346 // instead of the successor edges.
348 template <> struct GraphTraits<Inverse<MachineFunction*> > :
349 public GraphTraits<Inverse<MachineBasicBlock*> > {
350 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
351 return &G.Graph->front();
354 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
355 public GraphTraits<Inverse<const MachineBasicBlock*> > {
356 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
357 return &G.Graph->front();
361 } // End llvm namespace