2fbe9b803eec13b837d180bc3f4ff842f0cb87be
[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) MFInfo = new (Allocator.Allocate<Ty>()) Ty(*this);
140
141     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
142            "Invalid concrete type or multiple inheritence for getInfo");
143     return static_cast<Ty*>(MFInfo);
144   }
145
146   template<typename Ty>
147   const Ty *getInfo() const {
148      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
149   }
150
151   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
152   /// are inserted into the machine function.  The block number for a machine
153   /// basic block can be found by using the MBB::getBlockNumber method, this
154   /// method provides the inverse mapping.
155   ///
156   MachineBasicBlock *getBlockNumbered(unsigned N) {
157     assert(N < MBBNumbering.size() && "Illegal block number");
158     assert(MBBNumbering[N] && "Block was removed from the machine function!");
159     return MBBNumbering[N];
160   }
161
162   /// getNumBlockIDs - Return the number of MBB ID's allocated.
163   ///
164   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
165   
166   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
167   /// recomputes them.  This guarantees that the MBB numbers are sequential,
168   /// dense, and match the ordering of the blocks within the function.  If a
169   /// specific MachineBasicBlock is specified, only that block and those after
170   /// it are renumbered.
171   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
172   
173   /// print - Print out the MachineFunction in a format suitable for debugging
174   /// to the specified stream.
175   ///
176   void print(std::ostream &OS) const;
177   void print(std::ostream *OS) const { if (OS) print(*OS); }
178
179   /// viewCFG - This function is meant for use from the debugger.  You can just
180   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
181   /// program, displaying the CFG of the current function with the code for each
182   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
183   /// in your path.
184   ///
185   void viewCFG() const;
186
187   /// viewCFGOnly - This function is meant for use from the debugger.  It works
188   /// just like viewCFG, but it does not include the contents of basic blocks
189   /// into the nodes, just the label.  If you are only interested in the CFG
190   /// this can make the graph smaller.
191   ///
192   void viewCFGOnly() const;
193
194   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
195   ///
196   void dump() const;
197
198   /// construct - Allocate and initialize a MachineFunction for a given Function
199   /// and Target
200   ///
201   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
202
203   /// destruct - Destroy the MachineFunction corresponding to a given Function
204   ///
205   static void destruct(const Function *F);
206
207   /// get - Return a handle to a MachineFunction corresponding to the given
208   /// Function.  This should not be called before "construct()" for a given
209   /// Function.
210   ///
211   static MachineFunction& get(const Function *F);
212
213   // Provide accessors for the MachineBasicBlock list...
214   typedef BasicBlockListType::iterator iterator;
215   typedef BasicBlockListType::const_iterator const_iterator;
216   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
217   typedef std::reverse_iterator<iterator>             reverse_iterator;
218
219   //===--------------------------------------------------------------------===//
220   // BasicBlock accessor functions.
221   //
222   iterator                 begin()       { return BasicBlocks.begin(); }
223   const_iterator           begin() const { return BasicBlocks.begin(); }
224   iterator                 end  ()       { return BasicBlocks.end();   }
225   const_iterator           end  () const { return BasicBlocks.end();   }
226
227   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
228   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
229   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
230   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
231
232   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
233   bool                     empty() const { return BasicBlocks.empty(); }
234   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
235         MachineBasicBlock &front()       { return BasicBlocks.front(); }
236   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
237         MachineBasicBlock & back()       { return BasicBlocks.back(); }
238
239   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
240   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
241   void insert(iterator MBBI, MachineBasicBlock *MBB) {
242     BasicBlocks.insert(MBBI, MBB);
243   }
244   void splice(iterator InsertPt, iterator MBBI) {
245     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
246   }
247
248   void remove(iterator MBBI) {
249     BasicBlocks.remove(MBBI);
250   }
251   void erase(iterator MBBI) {
252     BasicBlocks.erase(MBBI);
253   }
254
255   //===--------------------------------------------------------------------===//
256   // Internal functions used to automatically number MachineBasicBlocks
257   //
258
259   /// getNextMBBNumber - Returns the next unique number to be assigned
260   /// to a MachineBasicBlock in this MachineFunction.
261   ///
262   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
263     MBBNumbering.push_back(MBB);
264     return (unsigned)MBBNumbering.size()-1;
265   }
266
267   /// removeFromMBBNumbering - Remove the specific machine basic block from our
268   /// tracker, this is only really to be used by the MachineBasicBlock
269   /// implementation.
270   void removeFromMBBNumbering(unsigned N) {
271     assert(N < MBBNumbering.size() && "Illegal basic block #");
272     MBBNumbering[N] = 0;
273   }
274
275   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
276   /// of `new MachineInstr'.
277   ///
278   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
279                                    bool NoImp = false);
280
281   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
282   /// 'Orig' instruction, identical in all ways except the the instruction
283   /// has no parent, prev, or next.
284   ///
285   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
286
287   /// DeleteMachineInstr - Delete the given MachineInstr.
288   ///
289   void DeleteMachineInstr(MachineInstr *MI);
290
291   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
292   /// instead of `new MachineBasicBlock'.
293   ///
294   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
295
296   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
297   ///
298   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
299
300   /// CreateMachineMemOperand - Allocate a new MachineMemOperand. Use this
301   /// instead of `new MachineMemOperand'.
302   ///
303   MachineMemOperand *CreateMachineMemOperand(const MachineMemOperand &MMO);
304
305   /// DeleteMachineMemOperand - Delete the given MachineMemOperand.
306   ///
307   void DeleteMachineMemOperand(MachineMemOperand *MMO);
308 };
309
310 //===--------------------------------------------------------------------===//
311 // GraphTraits specializations for function basic block graphs (CFGs)
312 //===--------------------------------------------------------------------===//
313
314 // Provide specializations of GraphTraits to be able to treat a
315 // machine function as a graph of machine basic blocks... these are
316 // the same as the machine basic block iterators, except that the root
317 // node is implicitly the first node of the function.
318 //
319 template <> struct GraphTraits<MachineFunction*> :
320   public GraphTraits<MachineBasicBlock*> {
321   static NodeType *getEntryNode(MachineFunction *F) {
322     return &F->front();
323   }
324
325   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
326   typedef MachineFunction::iterator nodes_iterator;
327   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
328   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
329 };
330 template <> struct GraphTraits<const MachineFunction*> :
331   public GraphTraits<const MachineBasicBlock*> {
332   static NodeType *getEntryNode(const MachineFunction *F) {
333     return &F->front();
334   }
335
336   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
337   typedef MachineFunction::const_iterator nodes_iterator;
338   static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
339   static nodes_iterator nodes_end  (const MachineFunction *F) { return F->end(); }
340 };
341
342
343 // Provide specializations of GraphTraits to be able to treat a function as a
344 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
345 // a function is considered to be when traversing the predecessor edges of a BB
346 // instead of the successor edges.
347 //
348 template <> struct GraphTraits<Inverse<MachineFunction*> > :
349   public GraphTraits<Inverse<MachineBasicBlock*> > {
350   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
351     return &G.Graph->front();
352   }
353 };
354 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
355   public GraphTraits<Inverse<const MachineBasicBlock*> > {
356   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
357     return &G.Graph->front();
358   }
359 };
360
361 } // End llvm namespace
362
363 #endif