1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/CodeGen/MachineBasicBlock.h"
22 #include "llvm/Support/Annotation.h"
29 class MachineFrameInfo;
30 class MachineConstantPool;
34 class ilist_traits<MachineBasicBlock> {
35 // this is only set by the MachineFunction owning the ilist
36 friend class MachineFunction;
37 MachineFunction* Parent;
40 ilist_traits<MachineBasicBlock>() : Parent(0) { }
42 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
43 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
45 static const MachineBasicBlock*
46 getPrev(const MachineBasicBlock* N) { return N->Prev; }
48 static const MachineBasicBlock*
49 getNext(const MachineBasicBlock* N) { return N->Next; }
51 static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
54 static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
58 static MachineBasicBlock* createNode();
59 void addNodeToList(MachineBasicBlock* N);
60 void removeNodeFromList(MachineBasicBlock* N);
61 void transferNodesFromList(iplist<MachineBasicBlock,
62 ilist_traits<MachineBasicBlock> > &toList,
63 ilist_iterator<MachineBasicBlock> first,
64 ilist_iterator<MachineBasicBlock> last);
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 : private Annotation {
77 const TargetMachine &Target;
79 // List of machine basic blocks in function
80 ilist<MachineBasicBlock> BasicBlocks;
82 // Keeping track of mapping from SSA values to registers
83 SSARegMap *SSARegMapping;
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 // Function-level unique numbering for MachineBasicBlocks. When a
96 // MachineBasicBlock is inserted into a MachineFunction is it automatically
97 // numbered and this vector keeps track of the mapping from ID's to MBB's.
98 std::vector<MachineBasicBlock*> MBBNumbering;
101 MachineFunction(const Function *Fn, const TargetMachine &TM);
104 /// getFunction - Return the LLVM function that this machine code represents
106 const Function *getFunction() const { return Fn; }
108 /// getTarget - Return the target machine this machine code is compiled with
110 const TargetMachine &getTarget() const { return Target; }
112 /// SSARegMap Interface... Keep track of information about each SSA virtual
113 /// register, such as which register class it belongs to.
115 SSARegMap *getSSARegMap() const { return SSARegMapping; }
116 void clearSSARegMap();
118 /// getFrameInfo - Return the frame info object for the current function.
119 /// This object contains information about objects allocated on the stack
120 /// frame of the current function in an abstract way.
122 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
124 /// getConstantPool - Return the constant pool object for the current
127 MachineConstantPool *getConstantPool() const { return ConstantPool; }
129 /// MachineFunctionInfo - Keep track of various per-function pieces of
130 /// information for the sparc backend.
132 template<typename Ty>
134 if (!MFInfo) MFInfo = new Ty(*this);
136 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
137 "Invalid concrete type or multiple inheritence for getInfo");
138 return static_cast<Ty*>(MFInfo);
141 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
142 /// are inserted into the machine function. The block number for a machine
143 /// basic block can be found by using the MBB::getBlockNumber method, this
144 /// method provides the inverse mapping.
146 MachineBasicBlock *getBlockNumbered(unsigned N) {
147 assert(N < MBBNumbering.size() && "Illegal block number");
148 assert(MBBNumbering[N] && "Block was removed from the machine function!");
149 return MBBNumbering[N];
152 /// getLastBlock - Returns the MachineBasicBlock with the greatest number
153 MachineBasicBlock *getLastBlock() {
154 return MBBNumbering.back();
157 /// print - Print out the MachineFunction in a format suitable for debugging
158 /// to the specified stream.
160 void print(std::ostream &OS) const;
162 /// viewCFG - This function is meant for use from the debugger. You can just
163 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
164 /// program, displaying the CFG of the current function with the code for each
165 /// basic block inside. This depends on there being a 'dot' and 'gv' program
168 void viewCFG() const;
170 /// viewCFGOnly - This function is meant for use from the debugger. It works
171 /// just like viewCFG, but it does not include the contents of basic blocks
172 /// into the nodes, just the label. If you are only interested in the CFG
173 /// this can make the graph smaller.
175 void viewCFGOnly() const;
177 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
181 /// construct - Allocate and initialize a MachineFunction for a given Function
184 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
186 /// destruct - Destroy the MachineFunction corresponding to a given Function
188 static void destruct(const Function *F);
190 /// get - Return a handle to a MachineFunction corresponding to the given
191 /// Function. This should not be called before "construct()" for a given
194 static MachineFunction& get(const Function *F);
196 // Provide accessors for the MachineBasicBlock list...
197 typedef ilist<MachineBasicBlock> BasicBlockListType;
198 typedef BasicBlockListType::iterator iterator;
199 typedef BasicBlockListType::const_iterator const_iterator;
200 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
201 typedef std::reverse_iterator<iterator> reverse_iterator;
203 // Provide accessors for basic blocks...
204 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
205 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
207 //===--------------------------------------------------------------------===//
208 // BasicBlock iterator forwarding functions
210 iterator begin() { return BasicBlocks.begin(); }
211 const_iterator begin() const { return BasicBlocks.begin(); }
212 iterator end () { return BasicBlocks.end(); }
213 const_iterator end () const { return BasicBlocks.end(); }
215 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
216 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
217 reverse_iterator rend () { return BasicBlocks.rend(); }
218 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
220 unsigned size() const { return BasicBlocks.size(); }
221 bool empty() const { return BasicBlocks.empty(); }
222 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
223 MachineBasicBlock &front() { return BasicBlocks.front(); }
224 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
225 MachineBasicBlock & back() { return BasicBlocks.back(); }
227 //===--------------------------------------------------------------------===//
228 // Internal functions used to automatically number MachineBasicBlocks
231 /// getNextMBBNumber - Returns the next unique number to be assigned
232 /// to a MachineBasicBlock in this MachineFunction.
234 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
235 MBBNumbering.push_back(MBB);
236 return MBBNumbering.size()-1;
239 /// removeFromMBBNumbering - Remove the specific machine basic block from our
240 /// tracker, this is only really to be used by the MachineBasicBlock
242 void removeFromMBBNumbering(unsigned N) {
243 assert(N < MBBNumbering.size() && "Illegal basic block #");
248 //===--------------------------------------------------------------------===//
249 // GraphTraits specializations for function basic block graphs (CFGs)
250 //===--------------------------------------------------------------------===//
252 // Provide specializations of GraphTraits to be able to treat a
253 // machine function as a graph of machine basic blocks... these are
254 // the same as the machine basic block iterators, except that the root
255 // node is implicitly the first node of the function.
257 template <> struct GraphTraits<MachineFunction*> :
258 public GraphTraits<MachineBasicBlock*> {
259 static NodeType *getEntryNode(MachineFunction *F) {
263 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
264 typedef MachineFunction::iterator nodes_iterator;
265 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
266 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
268 template <> struct GraphTraits<const MachineFunction*> :
269 public GraphTraits<const MachineBasicBlock*> {
270 static NodeType *getEntryNode(const MachineFunction *F) {
274 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
275 typedef MachineFunction::const_iterator nodes_iterator;
276 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
277 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
281 // Provide specializations of GraphTraits to be able to treat a function as a
282 // graph of basic blocks... and to walk it in inverse order. Inverse order for
283 // a function is considered to be when traversing the predecessor edges of a BB
284 // instead of the successor edges.
286 template <> struct GraphTraits<Inverse<MachineFunction*> > :
287 public GraphTraits<Inverse<MachineBasicBlock*> > {
288 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
289 return &G.Graph->front();
292 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
293 public GraphTraits<Inverse<const MachineBasicBlock*> > {
294 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
295 return &G.Graph->front();
299 } // End llvm namespace