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