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