Add support for GCC compatible builtin setjmp and longjmp intrinsics. This is
[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   // HasBuiltinSetjmp - true if the function uses builtin_setjmp. Used to
74   // adjust callee-saved register tracking.
75   bool HasBuiltinSetjmp;
76
77   // RegInfo - Information about each register in use in the function.
78   MachineRegisterInfo *RegInfo;
79
80   // Used to keep track of target-specific per-machine function information for
81   // the target implementation.
82   MachineFunctionInfo *MFInfo;
83
84   // Keep track of objects allocated on the stack.
85   MachineFrameInfo *FrameInfo;
86
87   // Keep track of constants which are spilled to memory
88   MachineConstantPool *ConstantPool;
89   
90   // Keep track of jump tables for switch instructions
91   MachineJumpTableInfo *JumpTableInfo;
92
93   // Function-level unique numbering for MachineBasicBlocks.  When a
94   // MachineBasicBlock is inserted into a MachineFunction is it automatically
95   // numbered and this vector keeps track of the mapping from ID's to MBB's.
96   std::vector<MachineBasicBlock*> MBBNumbering;
97
98   // Pool-allocate MachineFunction-lifetime and IR objects.
99   BumpPtrAllocator Allocator;
100
101   // Allocation management for instructions in function.
102   Recycler<MachineInstr> InstructionRecycler;
103
104   // Allocation management for basic blocks in function.
105   Recycler<MachineBasicBlock> BasicBlockRecycler;
106
107   // List of machine basic blocks in function
108   typedef ilist<MachineBasicBlock> BasicBlockListType;
109   BasicBlockListType BasicBlocks;
110
111   // Default debug location. Used to print out the debug label at the beginning
112   // of a function.
113   DebugLoc DefaultDebugLoc;
114
115   // Tracks debug locations.
116   DebugLocTracker DebugLocInfo;
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   /// doesHaveBuiltinSetjmp - Return whether this function uses builtin_setjmp
131   ///
132   bool doesHaveBuiltinSetjmp() const { return HasBuiltinSetjmp; }
133
134   /// setHasBuiltinSetjmp - Mark whether this function uses builtin_setjmp
135   ///
136   void setHasBuiltinSetjmp (bool flag) { HasBuiltinSetjmp = flag; }
137
138   /// getRegInfo - Return information about the registers currently in use.
139   ///
140   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
141   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
142
143   /// getFrameInfo - Return the frame info object for the current function.
144   /// This object contains information about objects allocated on the stack
145   /// frame of the current function in an abstract way.
146   ///
147   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
148   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
149
150   /// getJumpTableInfo - Return the jump table info object for the current 
151   /// function.  This object contains information about jump tables for switch
152   /// instructions in the current function.
153   ///
154   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
155   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
156   
157   /// getConstantPool - Return the constant pool object for the current
158   /// function.
159   ///
160   MachineConstantPool *getConstantPool() { return ConstantPool; }
161   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
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) const;
212   void print(std::ostream *OS) const { if (OS) print(*OS); }
213
214   /// viewCFG - This function is meant for use from the debugger.  You can just
215   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
216   /// program, displaying the CFG of the current function with the code for each
217   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
218   /// in your path.
219   ///
220   void viewCFG() const;
221
222   /// viewCFGOnly - This function is meant for use from the debugger.  It works
223   /// just like viewCFG, but it does not include the contents of basic blocks
224   /// into the nodes, just the label.  If you are only interested in the CFG
225   /// this can make the graph smaller.
226   ///
227   void viewCFGOnly() const;
228
229   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
230   ///
231   void dump() const;
232
233   /// construct - Allocate and initialize a MachineFunction for a given Function
234   /// and Target
235   ///
236   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
237
238   /// destruct - Destroy the MachineFunction corresponding to a given Function
239   ///
240   static void destruct(const Function *F);
241
242   /// get - Return a handle to a MachineFunction corresponding to the given
243   /// Function.  This should not be called before "construct()" for a given
244   /// Function.
245   ///
246   static MachineFunction& get(const Function *F);
247
248   // Provide accessors for the MachineBasicBlock list...
249   typedef BasicBlockListType::iterator iterator;
250   typedef BasicBlockListType::const_iterator const_iterator;
251   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
252   typedef std::reverse_iterator<iterator>             reverse_iterator;
253
254   /// addLiveIn - Add the specified physical register as a live-in value and
255   /// create a corresponding virtual register for it.
256   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
257
258   //===--------------------------------------------------------------------===//
259   // BasicBlock accessor functions.
260   //
261   iterator                 begin()       { return BasicBlocks.begin(); }
262   const_iterator           begin() const { return BasicBlocks.begin(); }
263   iterator                 end  ()       { return BasicBlocks.end();   }
264   const_iterator           end  () const { return BasicBlocks.end();   }
265
266   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
267   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
268   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
269   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
270
271   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
272   bool                     empty() const { return BasicBlocks.empty(); }
273   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
274         MachineBasicBlock &front()       { return BasicBlocks.front(); }
275   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
276         MachineBasicBlock & back()       { return BasicBlocks.back(); }
277
278   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
279   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
280   void insert(iterator MBBI, MachineBasicBlock *MBB) {
281     BasicBlocks.insert(MBBI, MBB);
282   }
283   void splice(iterator InsertPt, iterator MBBI) {
284     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
285   }
286
287   void remove(iterator MBBI) {
288     BasicBlocks.remove(MBBI);
289   }
290   void erase(iterator MBBI) {
291     BasicBlocks.erase(MBBI);
292   }
293
294   //===--------------------------------------------------------------------===//
295   // Internal functions used to automatically number MachineBasicBlocks
296   //
297
298   /// getNextMBBNumber - Returns the next unique number to be assigned
299   /// to a MachineBasicBlock in this MachineFunction.
300   ///
301   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
302     MBBNumbering.push_back(MBB);
303     return (unsigned)MBBNumbering.size()-1;
304   }
305
306   /// removeFromMBBNumbering - Remove the specific machine basic block from our
307   /// tracker, this is only really to be used by the MachineBasicBlock
308   /// implementation.
309   void removeFromMBBNumbering(unsigned N) {
310     assert(N < MBBNumbering.size() && "Illegal basic block #");
311     MBBNumbering[N] = 0;
312   }
313
314   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
315   /// of `new MachineInstr'.
316   ///
317   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
318                                    DebugLoc DL,
319                                    bool NoImp = false);
320
321   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
322   /// 'Orig' instruction, identical in all ways except the the instruction
323   /// has no parent, prev, or next.
324   ///
325   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
326
327   /// DeleteMachineInstr - Delete the given MachineInstr.
328   ///
329   void DeleteMachineInstr(MachineInstr *MI);
330
331   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
332   /// instead of `new MachineBasicBlock'.
333   ///
334   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
335
336   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
337   ///
338   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
339
340   //===--------------------------------------------------------------------===//
341   // Debug location.
342   //
343
344   /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
345   /// source file, line, and column. If none currently exists, create a new
346   /// DebugLocTuple, and insert it into the DebugIdMap.
347   unsigned getOrCreateDebugLocID(GlobalVariable *CompileUnit,
348                                  unsigned Line, unsigned Col);
349
350   /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
351   DebugLocTuple getDebugLocTuple(DebugLoc DL) const;
352
353   /// getDefaultDebugLoc - Get the default debug location for the machine
354   /// function.
355   DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
356
357   /// setDefaultDebugLoc - Get the default debug location for the machine
358   /// function.
359   void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
360 };
361
362 //===--------------------------------------------------------------------===//
363 // GraphTraits specializations for function basic block graphs (CFGs)
364 //===--------------------------------------------------------------------===//
365
366 // Provide specializations of GraphTraits to be able to treat a
367 // machine function as a graph of machine basic blocks... these are
368 // the same as the machine basic block iterators, except that the root
369 // node is implicitly the first node of the function.
370 //
371 template <> struct GraphTraits<MachineFunction*> :
372   public GraphTraits<MachineBasicBlock*> {
373   static NodeType *getEntryNode(MachineFunction *F) {
374     return &F->front();
375   }
376
377   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
378   typedef MachineFunction::iterator nodes_iterator;
379   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
380   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
381 };
382 template <> struct GraphTraits<const MachineFunction*> :
383   public GraphTraits<const MachineBasicBlock*> {
384   static NodeType *getEntryNode(const MachineFunction *F) {
385     return &F->front();
386   }
387
388   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
389   typedef MachineFunction::const_iterator nodes_iterator;
390   static nodes_iterator nodes_begin(const MachineFunction *F) {
391     return F->begin();
392   }
393   static nodes_iterator nodes_end  (const MachineFunction *F) {
394     return F->end();
395   }
396 };
397
398
399 // Provide specializations of GraphTraits to be able to treat a function as a
400 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
401 // a function is considered to be when traversing the predecessor edges of a BB
402 // instead of the successor edges.
403 //
404 template <> struct GraphTraits<Inverse<MachineFunction*> > :
405   public GraphTraits<Inverse<MachineBasicBlock*> > {
406   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
407     return &G.Graph->front();
408   }
409 };
410 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
411   public GraphTraits<Inverse<const MachineBasicBlock*> > {
412   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
413     return &G.Graph->front();
414   }
415 };
416
417 } // End llvm namespace
418
419 #endif