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/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 struct ilist_traits<MachineBasicBlock>
38 : public ilist_default_traits<MachineBasicBlock> {
39 mutable MachineBasicBlock Sentinel;
41 MachineBasicBlock *createSentinel() const { return &Sentinel; }
42 void destroySentinel(MachineBasicBlock *) const {}
44 void addNodeToList(MachineBasicBlock* MBB);
45 void removeNodeFromList(MachineBasicBlock* MBB);
46 void deleteNode(MachineBasicBlock *MBB);
48 void createNode(const MachineBasicBlock &);
51 /// MachineFunctionInfo - This class can be derived from and used by targets to
52 /// hold private target-specific information for each MachineFunction. Objects
53 /// of type are accessed/created with MF::getInfo and destroyed when the
54 /// MachineFunction is destroyed.
55 struct MachineFunctionInfo {
56 virtual ~MachineFunctionInfo() {}
59 class MachineFunction : private Annotation {
61 const TargetMachine &Target;
63 // RegInfo - Information about each register in use in the function.
64 MachineRegisterInfo *RegInfo;
66 // Used to keep track of target-specific per-machine function information for
67 // the target implementation.
68 MachineFunctionInfo *MFInfo;
70 // Keep track of objects allocated on the stack.
71 MachineFrameInfo *FrameInfo;
73 // Keep track of constants which are spilled to memory
74 MachineConstantPool *ConstantPool;
76 // Keep track of jump tables for switch instructions
77 MachineJumpTableInfo *JumpTableInfo;
79 // Function-level unique numbering for MachineBasicBlocks. When a
80 // MachineBasicBlock is inserted into a MachineFunction is it automatically
81 // numbered and this vector keeps track of the mapping from ID's to MBB's.
82 std::vector<MachineBasicBlock*> MBBNumbering;
84 // Pool-allocate MachineFunction-lifetime and IR objects.
85 BumpPtrAllocator Allocator;
87 // Allocation management for instructions in function.
88 Recycler<MachineInstr> InstructionRecycler;
90 // Allocation management for basic blocks in function.
91 Recycler<MachineBasicBlock> BasicBlockRecycler;
93 // List of machine basic blocks in function
94 typedef ilist<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);
307 //===--------------------------------------------------------------------===//
308 // GraphTraits specializations for function basic block graphs (CFGs)
309 //===--------------------------------------------------------------------===//
311 // Provide specializations of GraphTraits to be able to treat a
312 // machine function as a graph of machine basic blocks... these are
313 // the same as the machine basic block iterators, except that the root
314 // node is implicitly the first node of the function.
316 template <> struct GraphTraits<MachineFunction*> :
317 public GraphTraits<MachineBasicBlock*> {
318 static NodeType *getEntryNode(MachineFunction *F) {
322 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
323 typedef MachineFunction::iterator nodes_iterator;
324 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
325 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
327 template <> struct GraphTraits<const MachineFunction*> :
328 public GraphTraits<const MachineBasicBlock*> {
329 static NodeType *getEntryNode(const MachineFunction *F) {
333 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
334 typedef MachineFunction::const_iterator nodes_iterator;
335 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
336 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
340 // Provide specializations of GraphTraits to be able to treat a function as a
341 // graph of basic blocks... and to walk it in inverse order. Inverse order for
342 // a function is considered to be when traversing the predecessor edges of a BB
343 // instead of the successor edges.
345 template <> struct GraphTraits<Inverse<MachineFunction*> > :
346 public GraphTraits<Inverse<MachineBasicBlock*> > {
347 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
348 return &G.Graph->front();
351 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
352 public GraphTraits<Inverse<const MachineBasicBlock*> > {
353 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
354 return &G.Graph->front();
358 } // End llvm namespace