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