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