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