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