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