Add viewCFG() and viewCFGOnly() APIs.
[oota-llvm.git] / include / llvm / CodeGen / MachineFunction.h
1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // Collect native machine code for a function.  This class contains a list of
11 // MachineBasicBlock instances that make up the current compiled function.
12 //
13 // This class also contains pointers to various classes which hold
14 // target-specific information about the generated code.
15 //   
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
19 #define LLVM_CODEGEN_MACHINEFUNCTION_H
20
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "Support/Annotation.h"
23
24 namespace llvm {
25
26 // ilist_traits
27 template <>
28 class ilist_traits<MachineBasicBlock> {
29   // this is only set by the MachineFunction owning the ilist
30   friend class MachineFunction;
31   MachineFunction* Parent;
32   
33 public:
34   ilist_traits<MachineBasicBlock>() : Parent(0) { }
35   
36   static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
37   static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
38   
39   static const MachineBasicBlock*
40   getPrev(const MachineBasicBlock* N) { return N->Prev; }
41   
42   static const MachineBasicBlock*
43   getNext(const MachineBasicBlock* N) { return N->Next; }
44   
45   static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { N->Prev = prev; }
46   static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { N->Next = next; }
47   
48   static MachineBasicBlock* createNode();
49   void addNodeToList(MachineBasicBlock* N);
50   void removeNodeFromList(MachineBasicBlock* N);
51   void transferNodesFromList(
52                              iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
53                              ilist_iterator<MachineBasicBlock> first,
54                              ilist_iterator<MachineBasicBlock> last);
55 };
56   
57
58
59 class Function;
60 class TargetMachine;
61 class SSARegMap;
62 class MachineFunctionInfo;
63 class MachineFrameInfo;
64 class MachineConstantPool;
65
66 class MachineFunction : private Annotation {
67   const Function *Fn;
68   const TargetMachine &Target;
69
70   // List of machine basic blocks in function
71   ilist<MachineBasicBlock> BasicBlocks;
72
73   // Keeping track of mapping from SSA values to registers
74   SSARegMap *SSARegMapping;
75
76   // Used to keep track of frame and constant area information for sparc be
77   MachineFunctionInfo *MFInfo;
78
79   // Keep track of objects allocated on the stack.
80   MachineFrameInfo *FrameInfo;
81
82   // Keep track of constants which are spilled to memory
83   MachineConstantPool *ConstantPool;
84
85   // Function-level unique numbering for MachineBasicBlocks.  When a
86   // MachineBasicBlock is inserted into a MachineFunction is it automatically
87   // numbered and this vector keeps track of the mapping from ID's to MBB's.
88   std::vector<MachineBasicBlock*> MBBNumbering;
89
90 public:
91   MachineFunction(const Function *Fn, const TargetMachine &TM);
92   ~MachineFunction();
93
94   /// getFunction - Return the LLVM function that this machine code represents
95   ///
96   const Function *getFunction() const { return Fn; }
97
98   /// getTarget - Return the target machine this machine code is compiled with
99   ///
100   const TargetMachine &getTarget() const { return Target; }
101
102   /// SSARegMap Interface... Keep track of information about each SSA virtual
103   /// register, such as which register class it belongs to.
104   ///
105   SSARegMap *getSSARegMap() const { return SSARegMapping; }
106   void clearSSARegMap();
107
108   /// getFrameInfo - Return the frame info object for the current function.
109   /// This object contains information about objects allocated on the stack
110   /// frame of the current function in an abstract way.
111   ///
112   MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
113
114   /// getConstantPool - Return the constant pool object for the current
115   /// function.
116   MachineConstantPool *getConstantPool() const { return ConstantPool; }
117
118   /// MachineFunctionInfo - Keep track of various per-function pieces of
119   /// information for the sparc backend.
120   ///
121   MachineFunctionInfo *getInfo() const { return MFInfo; }
122
123   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
124   /// are inserted into the machine function.  The block number for a machine
125   /// basic block can be found by using the MBB::getBlockNumber method, this
126   /// method provides the inverse mapping.
127   MachineBasicBlock *getBlockNumbered(unsigned N) {
128     assert(N < MBBNumbering.size() && "Illegal block number");
129     assert(MBBNumbering[N] && "Block was removed from the machine function!");
130     return MBBNumbering[N];
131   }
132
133   /// print - Print out the MachineFunction in a format suitable for debugging
134   /// to the specified stream.
135   ///
136   void print(std::ostream &OS) const;
137
138   /// viewCFG - This function is meant for use from the debugger.  You can just
139   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
140   /// program, displaying the CFG of the current function with the code for each
141   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
142   /// in your path.
143   ///
144   void viewCFG() const;
145   
146   /// viewCFGOnly - This function is meant for use from the debugger.  It works
147   /// just like viewCFG, but it does not include the contents of basic blocks
148   /// into the nodes, just the label.  If you are only interested in the CFG
149   /// this can make the graph smaller.
150   ///
151   void viewCFGOnly() const;
152
153   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
154   ///
155   void dump() const;
156
157   /// construct - Allocate and initialize a MachineFunction for a given Function
158   /// and Target
159   ///
160   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
161
162   /// destruct - Destroy the MachineFunction corresponding to a given Function
163   ///
164   static void destruct(const Function *F);
165
166   /// get - Return a handle to a MachineFunction corresponding to the given
167   /// Function.  This should not be called before "construct()" for a given
168   /// Function.
169   ///
170   static MachineFunction& get(const Function *F);
171
172   // Provide accessors for the MachineBasicBlock list...
173   typedef ilist<MachineBasicBlock> BasicBlockListType;
174   typedef BasicBlockListType::iterator iterator;
175   typedef BasicBlockListType::const_iterator const_iterator;
176   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
177   typedef std::reverse_iterator<iterator>             reverse_iterator;
178
179   // Provide accessors for basic blocks...
180   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
181         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
182  
183   //===--------------------------------------------------------------------===//
184   // BasicBlock iterator forwarding functions
185   //
186   iterator                 begin()       { return BasicBlocks.begin(); }
187   const_iterator           begin() const { return BasicBlocks.begin(); }
188   iterator                 end  ()       { return BasicBlocks.end();   }
189   const_iterator           end  () const { return BasicBlocks.end();   }
190
191   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
192   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
193   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
194   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
195
196   unsigned                  size() const { return BasicBlocks.size(); }
197   bool                     empty() const { return BasicBlocks.empty(); }
198   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
199         MachineBasicBlock &front()       { return BasicBlocks.front(); }
200   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
201         MachineBasicBlock & back()       { return BasicBlocks.back(); }
202
203   //===--------------------------------------------------------------------===//
204   // Internal functions used to automatically number MachineBasicBlocks
205   //
206
207   /// getNextMBBNumber - Returns the next unique number to be assigned
208   /// to a MachineBasicBlock in this MachineFunction.
209   ///
210   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
211     MBBNumbering.push_back(MBB);
212     return MBBNumbering.size()-1;
213   }
214
215   /// removeFromMBBNumbering - Remove the specific machine basic block from our
216   /// tracker, this is only really to be used by the MachineBasicBlock
217   /// implementation.
218   void removeFromMBBNumbering(unsigned N) {
219     assert(N < MBBNumbering.size() && "Illegal basic block #");
220     MBBNumbering[N] = 0;
221   }
222 };
223
224 //===--------------------------------------------------------------------===//
225 // GraphTraits specializations for function basic block graphs (CFGs)
226 //===--------------------------------------------------------------------===//
227
228 // Provide specializations of GraphTraits to be able to treat a
229 // machine function as a graph of machine basic blocks... these are
230 // the same as the machine basic block iterators, except that the root
231 // node is implicitly the first node of the function.
232 //
233 template <> struct GraphTraits<MachineFunction*> :
234   public GraphTraits<MachineBasicBlock*> {
235   static NodeType *getEntryNode(MachineFunction *F) {
236     return &F->front();
237   }
238
239   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
240   typedef MachineFunction::iterator nodes_iterator;
241   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
242   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
243 };
244 template <> struct GraphTraits<const MachineFunction*> :
245   public GraphTraits<const MachineBasicBlock*> {
246   static NodeType *getEntryNode(const MachineFunction *F) {
247     return &F->front();
248   }
249
250   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
251   typedef MachineFunction::const_iterator nodes_iterator;
252   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
253   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
254 };
255
256
257 // Provide specializations of GraphTraits to be able to treat a function as a 
258 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
259 // a function is considered to be when traversing the predecessor edges of a BB
260 // instead of the successor edges.
261 //
262 template <> struct GraphTraits<Inverse<MachineFunction*> > :
263   public GraphTraits<Inverse<MachineBasicBlock*> > {
264   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
265     return &G.Graph->front();
266   }
267 };
268 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
269   public GraphTraits<Inverse<const MachineBasicBlock*> > {
270   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
271     return &G.Graph->front();
272   }
273 };
274
275 } // End llvm namespace
276
277 #endif