SjLj based exception handling unwinding support. This patch is nasty, brutish
[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 <map>
22 #include "llvm/ADT/ilist.h"
23 #include "llvm/Support/DebugLoc.h"
24 #include "llvm/CodeGen/Dump.h"
25 #include "llvm/CodeGen/MachineBasicBlock.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 {
71   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   // The currently active call_site value
119   unsigned CallSiteIndex;
120
121   // The largest call_site value encountered
122   unsigned MaxCallSiteIndex;
123
124   // Call sites mapped to corresponding landing pads
125   std::map<MachineBasicBlock*, unsigned> LandingPadCallSiteIndexMap;
126
127 public:
128   MachineFunction(Function *Fn, const TargetMachine &TM);
129   ~MachineFunction();
130
131   /// getFunction - Return the LLVM function that this machine code represents
132   ///
133   Function *getFunction() const { return Fn; }
134
135   /// getTarget - Return the target machine this machine code is compiled with
136   ///
137   const TargetMachine &getTarget() const { return Target; }
138
139   /// getRegInfo - Return information about the registers currently in use.
140   ///
141   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
142   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
143
144   /// getFrameInfo - Return the frame info object for the current function.
145   /// This object contains information about objects allocated on the stack
146   /// frame of the current function in an abstract way.
147   ///
148   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
149   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
150
151   /// getJumpTableInfo - Return the jump table info object for the current 
152   /// function.  This object contains information about jump tables for switch
153   /// instructions in the current function.
154   ///
155   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
156   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
157   
158   /// getConstantPool - Return the constant pool object for the current
159   /// function.
160   ///
161   MachineConstantPool *getConstantPool() { return ConstantPool; }
162   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
163
164   /// getAlignment - Return the alignment (log2, not bytes) of the function.
165   ///
166   unsigned getAlignment() const { return Alignment; }
167
168   /// setAlignment - Set the alignment (log2, not bytes) of the function.
169   ///
170   void setAlignment(unsigned A) { Alignment = A; }
171
172   /// getCallSiteIndex() - Get the current call site index
173   ///
174   unsigned getCallSiteIndex() { return CallSiteIndex; }
175
176   /// setCallSiteIndex() - Set the current call site index
177   ///
178   void setCallSiteIndex(unsigned Idx) {
179     CallSiteIndex = Idx;
180     if (CallSiteIndex > MaxCallSiteIndex)
181       MaxCallSiteIndex = CallSiteIndex;
182   }
183
184   /// getMaxCallSiteIndex() - Get the largest call site index issued
185   ///
186   unsigned getMaxCallSiteIndex() { return MaxCallSiteIndex; }
187
188   /// setCallSiteIndexLandingPad() - Map the call site to a landing pad
189   ///
190   void setLandingPadCallSiteIndex(MachineBasicBlock *LandingPad,
191                                   unsigned CallSite) {
192     LandingPadCallSiteIndexMap[LandingPad] = CallSite;
193   }
194
195   /// getCallSiteIndexLandingPad() - Get landing pad for the call site index
196   ///
197   unsigned getLandingPadCallSiteIndex(MachineBasicBlock *LandingPad) {
198     return LandingPadCallSiteIndexMap[LandingPad];
199   }
200
201   /// getCallSiteCount() - Get the count of call site entries
202   ///
203   unsigned getCallSiteCount() {
204     return LandingPadCallSiteIndexMap.size();
205   }
206
207   /// MachineFunctionInfo - Keep track of various per-function pieces of
208   /// information for backends that would like to do so.
209   ///
210   template<typename Ty>
211   Ty *getInfo() {
212     if (!MFInfo) {
213         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
214         // that apparently breaks GCC 3.3.
215         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
216                                                       AlignOf<Ty>::Alignment));
217         MFInfo = new (Loc) Ty(*this);
218     }
219
220     assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
221            "Invalid concrete type or multiple inheritence for getInfo");
222     return static_cast<Ty*>(MFInfo);
223   }
224
225   template<typename Ty>
226   const Ty *getInfo() const {
227      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
228   }
229
230   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
231   /// are inserted into the machine function.  The block number for a machine
232   /// basic block can be found by using the MBB::getBlockNumber method, this
233   /// method provides the inverse mapping.
234   ///
235   MachineBasicBlock *getBlockNumbered(unsigned N) const {
236     assert(N < MBBNumbering.size() && "Illegal block number");
237     assert(MBBNumbering[N] && "Block was removed from the machine function!");
238     return MBBNumbering[N];
239   }
240
241   /// getNumBlockIDs - Return the number of MBB ID's allocated.
242   ///
243   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
244   
245   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
246   /// recomputes them.  This guarantees that the MBB numbers are sequential,
247   /// dense, and match the ordering of the blocks within the function.  If a
248   /// specific MachineBasicBlock is specified, only that block and those after
249   /// it are renumbered.
250   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
251   
252   /// print - Print out the MachineFunction in a format suitable for debugging
253   /// to the specified stream.
254   ///
255   void print(std::ostream &OS, 
256              const PrefixPrinter &prefix = PrefixPrinter()) const;
257   void print(std::ostream *OS,
258              const PrefixPrinter &prefix = PrefixPrinter()) const {
259     if (OS) print(*OS, prefix); 
260   }
261
262   /// viewCFG - This function is meant for use from the debugger.  You can just
263   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
264   /// program, displaying the CFG of the current function with the code for each
265   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
266   /// in your path.
267   ///
268   void viewCFG() const;
269
270   /// viewCFGOnly - This function is meant for use from the debugger.  It works
271   /// just like viewCFG, but it does not include the contents of basic blocks
272   /// into the nodes, just the label.  If you are only interested in the CFG
273   /// this can make the graph smaller.
274   ///
275   void viewCFGOnly() const;
276
277   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
278   ///
279   void dump() const;
280
281   // Provide accessors for the MachineBasicBlock list...
282   typedef BasicBlockListType::iterator iterator;
283   typedef BasicBlockListType::const_iterator const_iterator;
284   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
285   typedef std::reverse_iterator<iterator>             reverse_iterator;
286
287   /// addLiveIn - Add the specified physical register as a live-in value and
288   /// create a corresponding virtual register for it.
289   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
290
291   //===--------------------------------------------------------------------===//
292   // BasicBlock accessor functions.
293   //
294   iterator                 begin()       { return BasicBlocks.begin(); }
295   const_iterator           begin() const { return BasicBlocks.begin(); }
296   iterator                 end  ()       { return BasicBlocks.end();   }
297   const_iterator           end  () const { return BasicBlocks.end();   }
298
299   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
300   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
301   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
302   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
303
304   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
305   bool                     empty() const { return BasicBlocks.empty(); }
306   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
307         MachineBasicBlock &front()       { return BasicBlocks.front(); }
308   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
309         MachineBasicBlock & back()       { return BasicBlocks.back(); }
310
311   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
312   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
313   void insert(iterator MBBI, MachineBasicBlock *MBB) {
314     BasicBlocks.insert(MBBI, MBB);
315   }
316   void splice(iterator InsertPt, iterator MBBI) {
317     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
318   }
319
320   void remove(iterator MBBI) {
321     BasicBlocks.remove(MBBI);
322   }
323   void erase(iterator MBBI) {
324     BasicBlocks.erase(MBBI);
325   }
326
327   //===--------------------------------------------------------------------===//
328   // Internal functions used to automatically number MachineBasicBlocks
329   //
330
331   /// getNextMBBNumber - Returns the next unique number to be assigned
332   /// to a MachineBasicBlock in this MachineFunction.
333   ///
334   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
335     MBBNumbering.push_back(MBB);
336     return (unsigned)MBBNumbering.size()-1;
337   }
338
339   /// removeFromMBBNumbering - Remove the specific machine basic block from our
340   /// tracker, this is only really to be used by the MachineBasicBlock
341   /// implementation.
342   void removeFromMBBNumbering(unsigned N) {
343     assert(N < MBBNumbering.size() && "Illegal basic block #");
344     MBBNumbering[N] = 0;
345   }
346
347   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
348   /// of `new MachineInstr'.
349   ///
350   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
351                                    DebugLoc DL,
352                                    bool NoImp = false);
353
354   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
355   /// 'Orig' instruction, identical in all ways except the the instruction
356   /// has no parent, prev, or next.
357   ///
358   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
359
360   /// DeleteMachineInstr - Delete the given MachineInstr.
361   ///
362   void DeleteMachineInstr(MachineInstr *MI);
363
364   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
365   /// instead of `new MachineBasicBlock'.
366   ///
367   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
368
369   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
370   ///
371   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
372
373   //===--------------------------------------------------------------------===//
374   // Debug location.
375   //
376
377   /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
378   /// source file, line, and column. If none currently exists, create a new
379   /// DebugLocTuple, and insert it into the DebugIdMap.
380   unsigned getOrCreateDebugLocID(GlobalVariable *CompileUnit,
381                                  unsigned Line, unsigned Col);
382
383   /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
384   DebugLocTuple getDebugLocTuple(DebugLoc DL) const;
385
386   /// getDefaultDebugLoc - Get the default debug location for the machine
387   /// function.
388   DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
389
390   /// setDefaultDebugLoc - Get the default debug location for the machine
391   /// function.
392   void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
393
394   /// getDebugLocInfo - Get the debug info location tracker.
395   DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
396 };
397
398 //===--------------------------------------------------------------------===//
399 // GraphTraits specializations for function basic block graphs (CFGs)
400 //===--------------------------------------------------------------------===//
401
402 // Provide specializations of GraphTraits to be able to treat a
403 // machine function as a graph of machine basic blocks... these are
404 // the same as the machine basic block iterators, except that the root
405 // node is implicitly the first node of the function.
406 //
407 template <> struct GraphTraits<MachineFunction*> :
408   public GraphTraits<MachineBasicBlock*> {
409   static NodeType *getEntryNode(MachineFunction *F) {
410     return &F->front();
411   }
412
413   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
414   typedef MachineFunction::iterator nodes_iterator;
415   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
416   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
417 };
418 template <> struct GraphTraits<const MachineFunction*> :
419   public GraphTraits<const MachineBasicBlock*> {
420   static NodeType *getEntryNode(const MachineFunction *F) {
421     return &F->front();
422   }
423
424   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
425   typedef MachineFunction::const_iterator nodes_iterator;
426   static nodes_iterator nodes_begin(const MachineFunction *F) {
427     return F->begin();
428   }
429   static nodes_iterator nodes_end  (const MachineFunction *F) {
430     return F->end();
431   }
432 };
433
434
435 // Provide specializations of GraphTraits to be able to treat a function as a
436 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
437 // a function is considered to be when traversing the predecessor edges of a BB
438 // instead of the successor edges.
439 //
440 template <> struct GraphTraits<Inverse<MachineFunction*> > :
441   public GraphTraits<Inverse<MachineBasicBlock*> > {
442   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
443     return &G.Graph->front();
444   }
445 };
446 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
447   public GraphTraits<Inverse<const MachineBasicBlock*> > {
448   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
449     return &G.Graph->front();
450   }
451 };
452
453 } // End llvm namespace
454
455 #endif