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