Apply a patch from Mahadevan R, with minor formatting changes, to
[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/ADT/alist.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/Support/Annotation.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Recycler.h"
26
27 namespace llvm {
28
29 class Function;
30 class TargetMachine;
31 class MachineRegisterInfo;
32 class MachineFrameInfo;
33 class MachineConstantPool;
34 class MachineJumpTableInfo;
35
36 template <>
37 class alist_traits<MachineBasicBlock, MachineBasicBlock> {
38   typedef alist_iterator<MachineBasicBlock> iterator;
39 public:
40   void addNodeToList(MachineBasicBlock* MBB);
41   void removeNodeFromList(MachineBasicBlock* MBB);
42   void transferNodesFromList(alist_traits<MachineBasicBlock> &,
43                              iterator,
44                              iterator) {}
45   void deleteNode(MachineBasicBlock *MBB);
46 };
47
48 /// MachineFunctionInfo - This class can be derived from and used by targets to
49 /// hold private target-specific information for each MachineFunction.  Objects
50 /// of type are accessed/created with MF::getInfo and destroyed when the
51 /// MachineFunction is destroyed.
52 struct MachineFunctionInfo {
53   virtual ~MachineFunctionInfo() {}
54 };
55
56 class MachineFunction : private Annotation {
57   const Function *Fn;
58   const TargetMachine &Target;
59
60   // RegInfo - Information about each register in use in the function.
61   MachineRegisterInfo *RegInfo;
62
63   // Used to keep track of target-specific per-machine function information for
64   // the target implementation.
65   MachineFunctionInfo *MFInfo;
66
67   // Keep track of objects allocated on the stack.
68   MachineFrameInfo *FrameInfo;
69
70   // Keep track of constants which are spilled to memory
71   MachineConstantPool *ConstantPool;
72   
73   // Keep track of jump tables for switch instructions
74   MachineJumpTableInfo *JumpTableInfo;
75
76   // Function-level unique numbering for MachineBasicBlocks.  When a
77   // MachineBasicBlock is inserted into a MachineFunction is it automatically
78   // numbered and this vector keeps track of the mapping from ID's to MBB's.
79   std::vector<MachineBasicBlock*> MBBNumbering;
80
81   // Pool-allocate MachineFunction-lifetime and IR objects.
82   BumpPtrAllocator Allocator;
83
84   // Allocation management for instructions in function.
85   Recycler<MachineInstr> InstructionRecycler;
86
87   // Allocation management for basic blocks in function.
88   Recycler<MachineBasicBlock> BasicBlockRecycler;
89
90   // Allocation management for memoperands in function.
91   Recycler<MachineMemOperand> MemOperandRecycler;
92
93   // List of machine basic blocks in function
94   typedef alist<MachineBasicBlock> BasicBlockListType;
95   BasicBlockListType BasicBlocks;
96
97 public:
98   MachineFunction(const Function *Fn, const TargetMachine &TM);
99   ~MachineFunction();
100
101   /// getFunction - Return the LLVM function that this machine code represents
102   ///
103   const Function *getFunction() const { return Fn; }
104
105   /// getTarget - Return the target machine this machine code is compiled with
106   ///
107   const TargetMachine &getTarget() const { return Target; }
108
109   /// getRegInfo - Return information about the registers currently in use.
110   ///
111   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
112   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
113
114   /// getFrameInfo - Return the frame info object for the current function.
115   /// This object contains information about objects allocated on the stack
116   /// frame of the current function in an abstract way.
117   ///
118   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
119   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
120
121   /// getJumpTableInfo - Return the jump table info object for the current 
122   /// function.  This object contains information about jump tables for switch
123   /// instructions in the current function.
124   ///
125   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
126   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
127   
128   /// getConstantPool - Return the constant pool object for the current
129   /// function.
130   ///
131   MachineConstantPool *getConstantPool() { return ConstantPool; }
132   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
133
134   /// MachineFunctionInfo - Keep track of various per-function pieces of
135   /// information for backends that would like to do so.
136   ///
137   template<typename Ty>
138   Ty *getInfo() {
139     if (!MFInfo) {
140         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
141         // that apparently breaks GCC 3.3.
142         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
143                                                       AlignOf<Ty>::Alignment));
144         MFInfo = new (Loc) Ty(*this);
145     }
146
147     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
148            "Invalid concrete type or multiple inheritence for getInfo");
149     return static_cast<Ty*>(MFInfo);
150   }
151
152   template<typename Ty>
153   const Ty *getInfo() const {
154      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
155   }
156
157   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
158   /// are inserted into the machine function.  The block number for a machine
159   /// basic block can be found by using the MBB::getBlockNumber method, this
160   /// method provides the inverse mapping.
161   ///
162   MachineBasicBlock *getBlockNumbered(unsigned N) {
163     assert(N < MBBNumbering.size() && "Illegal block number");
164     assert(MBBNumbering[N] && "Block was removed from the machine function!");
165     return MBBNumbering[N];
166   }
167
168   /// getNumBlockIDs - Return the number of MBB ID's allocated.
169   ///
170   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
171   
172   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
173   /// recomputes them.  This guarantees that the MBB numbers are sequential,
174   /// dense, and match the ordering of the blocks within the function.  If a
175   /// specific MachineBasicBlock is specified, only that block and those after
176   /// it are renumbered.
177   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
178   
179   /// print - Print out the MachineFunction in a format suitable for debugging
180   /// to the specified stream.
181   ///
182   void print(std::ostream &OS) const;
183   void print(std::ostream *OS) const { if (OS) print(*OS); }
184
185   /// viewCFG - This function is meant for use from the debugger.  You can just
186   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
187   /// program, displaying the CFG of the current function with the code for each
188   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
189   /// in your path.
190   ///
191   void viewCFG() const;
192
193   /// viewCFGOnly - This function is meant for use from the debugger.  It works
194   /// just like viewCFG, but it does not include the contents of basic blocks
195   /// into the nodes, just the label.  If you are only interested in the CFG
196   /// this can make the graph smaller.
197   ///
198   void viewCFGOnly() const;
199
200   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
201   ///
202   void dump() const;
203
204   /// construct - Allocate and initialize a MachineFunction for a given Function
205   /// and Target
206   ///
207   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
208
209   /// destruct - Destroy the MachineFunction corresponding to a given Function
210   ///
211   static void destruct(const Function *F);
212
213   /// get - Return a handle to a MachineFunction corresponding to the given
214   /// Function.  This should not be called before "construct()" for a given
215   /// Function.
216   ///
217   static MachineFunction& get(const Function *F);
218
219   // Provide accessors for the MachineBasicBlock list...
220   typedef BasicBlockListType::iterator iterator;
221   typedef BasicBlockListType::const_iterator const_iterator;
222   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
223   typedef std::reverse_iterator<iterator>             reverse_iterator;
224
225   //===--------------------------------------------------------------------===//
226   // BasicBlock accessor functions.
227   //
228   iterator                 begin()       { return BasicBlocks.begin(); }
229   const_iterator           begin() const { return BasicBlocks.begin(); }
230   iterator                 end  ()       { return BasicBlocks.end();   }
231   const_iterator           end  () const { return BasicBlocks.end();   }
232
233   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
234   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
235   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
236   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
237
238   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
239   bool                     empty() const { return BasicBlocks.empty(); }
240   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
241         MachineBasicBlock &front()       { return BasicBlocks.front(); }
242   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
243         MachineBasicBlock & back()       { return BasicBlocks.back(); }
244
245   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
246   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
247   void insert(iterator MBBI, MachineBasicBlock *MBB) {
248     BasicBlocks.insert(MBBI, MBB);
249   }
250   void splice(iterator InsertPt, iterator MBBI) {
251     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
252   }
253
254   void remove(iterator MBBI) {
255     BasicBlocks.remove(MBBI);
256   }
257   void erase(iterator MBBI) {
258     BasicBlocks.erase(MBBI);
259   }
260
261   //===--------------------------------------------------------------------===//
262   // Internal functions used to automatically number MachineBasicBlocks
263   //
264
265   /// getNextMBBNumber - Returns the next unique number to be assigned
266   /// to a MachineBasicBlock in this MachineFunction.
267   ///
268   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
269     MBBNumbering.push_back(MBB);
270     return (unsigned)MBBNumbering.size()-1;
271   }
272
273   /// removeFromMBBNumbering - Remove the specific machine basic block from our
274   /// tracker, this is only really to be used by the MachineBasicBlock
275   /// implementation.
276   void removeFromMBBNumbering(unsigned N) {
277     assert(N < MBBNumbering.size() && "Illegal basic block #");
278     MBBNumbering[N] = 0;
279   }
280
281   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
282   /// of `new MachineInstr'.
283   ///
284   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
285                                    bool NoImp = false);
286
287   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
288   /// 'Orig' instruction, identical in all ways except the the instruction
289   /// has no parent, prev, or next.
290   ///
291   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
292
293   /// DeleteMachineInstr - Delete the given MachineInstr.
294   ///
295   void DeleteMachineInstr(MachineInstr *MI);
296
297   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
298   /// instead of `new MachineBasicBlock'.
299   ///
300   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
301
302   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
303   ///
304   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
305
306   /// CreateMachineMemOperand - Allocate a new MachineMemOperand. Use this
307   /// instead of `new MachineMemOperand'.
308   ///
309   MachineMemOperand *CreateMachineMemOperand(const MachineMemOperand &MMO);
310
311   /// DeleteMachineMemOperand - Delete the given MachineMemOperand.
312   ///
313   void DeleteMachineMemOperand(MachineMemOperand *MMO);
314 };
315
316 //===--------------------------------------------------------------------===//
317 // GraphTraits specializations for function basic block graphs (CFGs)
318 //===--------------------------------------------------------------------===//
319
320 // Provide specializations of GraphTraits to be able to treat a
321 // machine function as a graph of machine basic blocks... these are
322 // the same as the machine basic block iterators, except that the root
323 // node is implicitly the first node of the function.
324 //
325 template <> struct GraphTraits<MachineFunction*> :
326   public GraphTraits<MachineBasicBlock*> {
327   static NodeType *getEntryNode(MachineFunction *F) {
328     return &F->front();
329   }
330
331   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
332   typedef MachineFunction::iterator nodes_iterator;
333   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
334   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
335 };
336 template <> struct GraphTraits<const MachineFunction*> :
337   public GraphTraits<const MachineBasicBlock*> {
338   static NodeType *getEntryNode(const MachineFunction *F) {
339     return &F->front();
340   }
341
342   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
343   typedef MachineFunction::const_iterator nodes_iterator;
344   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
345   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
346 };
347
348
349 // Provide specializations of GraphTraits to be able to treat a function as a
350 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
351 // a function is considered to be when traversing the predecessor edges of a BB
352 // instead of the successor edges.
353 //
354 template <> struct GraphTraits<Inverse<MachineFunction*> > :
355   public GraphTraits<Inverse<MachineBasicBlock*> > {
356   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
357     return &G.Graph->front();
358   }
359 };
360 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
361   public GraphTraits<Inverse<const MachineBasicBlock*> > {
362   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
363     return &G.Graph->front();
364   }
365 };
366
367 } // End llvm namespace
368
369 #endif