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