94e9470b76792c175a73f6fcfaa8d22cf1509055
[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 "llvm/Support/Annotation.h"
23
24 namespace llvm {
25
26 class Function;
27 class TargetMachine;
28 class SSARegMap;
29 class MachineFrameInfo;
30 class MachineConstantPool;
31
32 // ilist_traits
33 template <>
34 struct ilist_traits<MachineBasicBlock> {
35   // this is only set by the MachineFunction owning the ilist
36   friend class MachineFunction;
37   MachineFunction* Parent;
38   
39 public:
40   ilist_traits<MachineBasicBlock>() : Parent(0) { }
41   
42   static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
43   static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
44   
45   static const MachineBasicBlock*
46   getPrev(const MachineBasicBlock* N) { return N->Prev; }
47   
48   static const MachineBasicBlock*
49   getNext(const MachineBasicBlock* N) { return N->Next; }
50   
51   static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
52     N->Prev = prev;
53   }
54   static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
55     N->Next = next;
56   }
57
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);
65 };
66
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() {};
73 };
74
75 class MachineFunction : private Annotation {
76   const Function *Fn;
77   const TargetMachine &Target;
78
79   // List of machine basic blocks in function
80   ilist<MachineBasicBlock> BasicBlocks;
81
82   // Keeping track of mapping from SSA values to registers
83   SSARegMap *SSARegMapping;
84
85   // Used to keep track of target-specific per-machine function information for
86   // the target implementation.
87   MachineFunctionInfo *MFInfo;
88
89   // Keep track of objects allocated on the stack.
90   MachineFrameInfo *FrameInfo;
91
92   // Keep track of constants which are spilled to memory
93   MachineConstantPool *ConstantPool;
94
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;
99
100   /// UsedPhysRegs - This is a new[]'d array of bools that is computed and set
101   /// by the register allocator, and must be kept up to date by passes that run
102   /// after register allocation (though most don't modify this).  This is used
103   /// so that the code generator knows which callee save registers to save and
104   /// for other target specific uses.
105   bool *UsedPhysRegs;
106
107 public:
108   MachineFunction(const Function *Fn, const TargetMachine &TM);
109   ~MachineFunction();
110
111   /// getFunction - Return the LLVM function that this machine code represents
112   ///
113   const Function *getFunction() const { return Fn; }
114
115   /// getTarget - Return the target machine this machine code is compiled with
116   ///
117   const TargetMachine &getTarget() const { return Target; }
118
119   /// SSARegMap Interface... Keep track of information about each SSA virtual
120   /// register, such as which register class it belongs to.
121   ///
122   SSARegMap *getSSARegMap() const { return SSARegMapping; }
123   void clearSSARegMap();
124
125   /// getFrameInfo - Return the frame info object for the current function.
126   /// This object contains information about objects allocated on the stack
127   /// frame of the current function in an abstract way.
128   ///
129   MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
130
131   /// getConstantPool - Return the constant pool object for the current
132   /// function.
133   ///
134   MachineConstantPool *getConstantPool() const { return ConstantPool; }
135
136   /// MachineFunctionInfo - Keep track of various per-function pieces of
137   /// information for the sparc backend.
138   ///
139   template<typename Ty>
140   Ty *getInfo() {
141     if (!MFInfo) MFInfo = new Ty(*this);
142
143     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
144            "Invalid concrete type or multiple inheritence for getInfo");
145     return static_cast<Ty*>(MFInfo);
146   }
147
148   /// setUsedPhysRegs - The register allocator should call this to initialized
149   /// the UsedPhysRegs set.  This should be passed a new[]'d array with entries
150   /// for all of the physical registers that the target supports.  Each array
151   /// entry should be set to true iff the physical register is used within the
152   /// function.
153   void setUsedPhysRegs(bool *UPR) { UsedPhysRegs = UPR; }
154
155   /// getUsedPhysregs - This returns the UsedPhysRegs array.  This returns null
156   /// before register allocation.
157   const bool *getUsedPhysregs() { return UsedPhysRegs; }
158
159   /// isPhysRegUsed - Return true if the specified register is used in this
160   /// function.  This only works after register allocation.
161   bool isPhysRegUsed(unsigned Reg) { return UsedPhysRegs[Reg]; }
162
163   /// changePhyRegUsed - This method allows code that runs after register
164   /// allocation to keep the PhysRegsUsed array up-to-date.
165   void changePhyRegUsed(unsigned Reg, bool State) { UsedPhysRegs[Reg] = State; }
166
167   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
168   /// are inserted into the machine function.  The block number for a machine
169   /// basic block can be found by using the MBB::getBlockNumber method, this
170   /// method provides the inverse mapping.
171   ///
172   MachineBasicBlock *getBlockNumbered(unsigned N) {
173     assert(N < MBBNumbering.size() && "Illegal block number");
174     assert(MBBNumbering[N] && "Block was removed from the machine function!");
175     return MBBNumbering[N];
176   }
177
178   /// getLastBlock - Returns the MachineBasicBlock with the greatest number
179   MachineBasicBlock *getLastBlock() {
180     return MBBNumbering.back();
181   }
182   const MachineBasicBlock *getLastBlock() const {
183     return MBBNumbering.back();
184   }
185
186   /// print - Print out the MachineFunction in a format suitable for debugging
187   /// to the specified stream.
188   ///
189   void print(std::ostream &OS) const;
190
191   /// viewCFG - This function is meant for use from the debugger.  You can just
192   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
193   /// program, displaying the CFG of the current function with the code for each
194   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
195   /// in your path.
196   ///
197   void viewCFG() const;
198   
199   /// viewCFGOnly - This function is meant for use from the debugger.  It works
200   /// just like viewCFG, but it does not include the contents of basic blocks
201   /// into the nodes, just the label.  If you are only interested in the CFG
202   /// this can make the graph smaller.
203   ///
204   void viewCFGOnly() const;
205
206   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
207   ///
208   void dump() const;
209
210   /// construct - Allocate and initialize a MachineFunction for a given Function
211   /// and Target
212   ///
213   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
214
215   /// destruct - Destroy the MachineFunction corresponding to a given Function
216   ///
217   static void destruct(const Function *F);
218
219   /// get - Return a handle to a MachineFunction corresponding to the given
220   /// Function.  This should not be called before "construct()" for a given
221   /// Function.
222   ///
223   static MachineFunction& get(const Function *F);
224
225   // Provide accessors for the MachineBasicBlock list...
226   typedef ilist<MachineBasicBlock> BasicBlockListType;
227   typedef BasicBlockListType::iterator iterator;
228   typedef BasicBlockListType::const_iterator const_iterator;
229   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
230   typedef std::reverse_iterator<iterator>             reverse_iterator;
231
232   // Provide accessors for basic blocks...
233   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
234         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
235  
236   //===--------------------------------------------------------------------===//
237   // BasicBlock iterator forwarding functions
238   //
239   iterator                 begin()       { return BasicBlocks.begin(); }
240   const_iterator           begin() const { return BasicBlocks.begin(); }
241   iterator                 end  ()       { return BasicBlocks.end();   }
242   const_iterator           end  () const { return BasicBlocks.end();   }
243
244   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
245   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
246   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
247   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
248
249   unsigned                  size() const { return BasicBlocks.size(); }
250   bool                     empty() const { return BasicBlocks.empty(); }
251   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
252         MachineBasicBlock &front()       { return BasicBlocks.front(); }
253   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
254         MachineBasicBlock & back()       { return BasicBlocks.back(); }
255
256   //===--------------------------------------------------------------------===//
257   // Internal functions used to automatically number MachineBasicBlocks
258   //
259
260   /// getNextMBBNumber - Returns the next unique number to be assigned
261   /// to a MachineBasicBlock in this MachineFunction.
262   ///
263   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
264     MBBNumbering.push_back(MBB);
265     return MBBNumbering.size()-1;
266   }
267
268   /// removeFromMBBNumbering - Remove the specific machine basic block from our
269   /// tracker, this is only really to be used by the MachineBasicBlock
270   /// implementation.
271   void removeFromMBBNumbering(unsigned N) {
272     assert(N < MBBNumbering.size() && "Illegal basic block #");
273     MBBNumbering[N] = 0;
274   }
275 };
276
277 //===--------------------------------------------------------------------===//
278 // GraphTraits specializations for function basic block graphs (CFGs)
279 //===--------------------------------------------------------------------===//
280
281 // Provide specializations of GraphTraits to be able to treat a
282 // machine function as a graph of machine basic blocks... these are
283 // the same as the machine basic block iterators, except that the root
284 // node is implicitly the first node of the function.
285 //
286 template <> struct GraphTraits<MachineFunction*> :
287   public GraphTraits<MachineBasicBlock*> {
288   static NodeType *getEntryNode(MachineFunction *F) {
289     return &F->front();
290   }
291
292   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
293   typedef MachineFunction::iterator nodes_iterator;
294   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
295   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
296 };
297 template <> struct GraphTraits<const MachineFunction*> :
298   public GraphTraits<const MachineBasicBlock*> {
299   static NodeType *getEntryNode(const MachineFunction *F) {
300     return &F->front();
301   }
302
303   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
304   typedef MachineFunction::const_iterator nodes_iterator;
305   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
306   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
307 };
308
309
310 // Provide specializations of GraphTraits to be able to treat a function as a 
311 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
312 // a function is considered to be when traversing the predecessor edges of a BB
313 // instead of the successor edges.
314 //
315 template <> struct GraphTraits<Inverse<MachineFunction*> > :
316   public GraphTraits<Inverse<MachineBasicBlock*> > {
317   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
318     return &G.Graph->front();
319   }
320 };
321 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
322   public GraphTraits<Inverse<const MachineBasicBlock*> > {
323   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
324     return &G.Graph->front();
325   }
326 };
327
328 } // End llvm namespace
329
330 #endif