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