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