rearrange MCContext ownership. Before LLVMTargetMachine created it
[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 Pass;
37 class TargetMachine;
38 class TargetRegisterClass;
39
40 template <>
41 struct ilist_traits<MachineBasicBlock>
42     : public ilist_default_traits<MachineBasicBlock> {
43   mutable ilist_half_node<MachineBasicBlock> Sentinel;
44 public:
45   MachineBasicBlock *createSentinel() const {
46     return static_cast<MachineBasicBlock*>(&Sentinel);
47   }
48   void destroySentinel(MachineBasicBlock *) const {}
49
50   MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
51   MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
52     return createSentinel();
53   }
54   static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
55
56   void addNodeToList(MachineBasicBlock* MBB);
57   void removeNodeFromList(MachineBasicBlock* MBB);
58   void deleteNode(MachineBasicBlock *MBB);
59 private:
60   void createNode(const MachineBasicBlock &);
61 };
62
63 /// MachineFunctionInfo - This class can be derived from and used by targets to
64 /// hold private target-specific information for each MachineFunction.  Objects
65 /// of type are accessed/created with MF::getInfo and destroyed when the
66 /// MachineFunction is destroyed.
67 struct MachineFunctionInfo {
68   virtual ~MachineFunctionInfo();
69 };
70
71 class MachineFunction {
72   Function *Fn;
73   const TargetMachine &Target;
74   MCContext &Ctx;
75
76   // RegInfo - Information about each register in use in the function.
77   MachineRegisterInfo *RegInfo;
78
79   // Used to keep track of target-specific per-machine function information for
80   // the target implementation.
81   MachineFunctionInfo *MFInfo;
82
83   // Keep track of objects allocated on the stack.
84   MachineFrameInfo *FrameInfo;
85
86   // Keep track of constants which are spilled to memory
87   MachineConstantPool *ConstantPool;
88   
89   // Keep track of jump tables for switch instructions
90   MachineJumpTableInfo *JumpTableInfo;
91
92   // Function-level unique numbering for MachineBasicBlocks.  When a
93   // MachineBasicBlock is inserted into a MachineFunction is it automatically
94   // numbered and this vector keeps track of the mapping from ID's to MBB's.
95   std::vector<MachineBasicBlock*> MBBNumbering;
96
97   // Pool-allocate MachineFunction-lifetime and IR objects.
98   BumpPtrAllocator Allocator;
99
100   // Allocation management for instructions in function.
101   Recycler<MachineInstr> InstructionRecycler;
102
103   // Allocation management for basic blocks in function.
104   Recycler<MachineBasicBlock> BasicBlockRecycler;
105
106   // List of machine basic blocks in function
107   typedef ilist<MachineBasicBlock> BasicBlockListType;
108   BasicBlockListType BasicBlocks;
109
110   // Default debug location. Used to print out the debug label at the beginning
111   // of a function.
112   DebugLoc DefaultDebugLoc;
113
114   // Tracks debug locations.
115   DebugLocTracker DebugLocInfo;
116
117   /// FunctionNumber - This provides a unique ID for each function emitted in
118   /// this translation unit.
119   ///
120   unsigned FunctionNumber;
121   
122   // The alignment of the function.
123   unsigned Alignment;
124
125   MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
126   void operator=(const MachineFunction&);   // DO NOT IMPLEMENT
127
128 public:
129   MachineFunction(Function *Fn, const TargetMachine &TM, unsigned FunctionNum,
130                   MCContext &Ctx);
131   ~MachineFunction();
132
133   MCContext &getContext() const { return Ctx; }
134   
135   /// getFunction - Return the LLVM function that this machine code represents
136   ///
137   Function *getFunction() const { return Fn; }
138
139   /// getFunctionNumber - Return a unique ID for the current function.
140   ///
141   unsigned getFunctionNumber() const { return FunctionNumber; }
142   
143   /// getTarget - Return the target machine this machine code is compiled with
144   ///
145   const TargetMachine &getTarget() const { return Target; }
146
147   /// getRegInfo - Return information about the registers currently in use.
148   ///
149   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
150   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
151
152   /// getFrameInfo - Return the frame info object for the current function.
153   /// This object contains information about objects allocated on the stack
154   /// frame of the current function in an abstract way.
155   ///
156   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
157   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
158
159   /// getJumpTableInfo - Return the jump table info object for the current 
160   /// function.  This object contains information about jump tables in the
161   /// current function.  If the current function has no jump tables, this will
162   /// return null.
163   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
164   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
165
166   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
167   /// does already exist, allocate one.
168   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
169
170   
171   /// getConstantPool - Return the constant pool object for the current
172   /// function.
173   ///
174   MachineConstantPool *getConstantPool() { return ConstantPool; }
175   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
176
177   /// getAlignment - Return the alignment (log2, not bytes) of the function.
178   ///
179   unsigned getAlignment() const { return Alignment; }
180
181   /// setAlignment - Set the alignment (log2, not bytes) of the function.
182   ///
183   void setAlignment(unsigned A) { Alignment = A; }
184
185   /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
186   void EnsureAlignment(unsigned A) {
187     if (Alignment < A) Alignment = A;
188   }
189   
190   /// getInfo - Keep track of various per-function pieces of information for
191   /// backends that would like to do so.
192   ///
193   template<typename Ty>
194   Ty *getInfo() {
195     if (!MFInfo) {
196         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
197         // that apparently breaks GCC 3.3.
198         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
199                                                       AlignOf<Ty>::Alignment));
200         MFInfo = new (Loc) Ty(*this);
201     }
202     return static_cast<Ty*>(MFInfo);
203   }
204
205   template<typename Ty>
206   const Ty *getInfo() const {
207      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
208   }
209
210   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
211   /// are inserted into the machine function.  The block number for a machine
212   /// basic block can be found by using the MBB::getBlockNumber method, this
213   /// method provides the inverse mapping.
214   ///
215   MachineBasicBlock *getBlockNumbered(unsigned N) const {
216     assert(N < MBBNumbering.size() && "Illegal block number");
217     assert(MBBNumbering[N] && "Block was removed from the machine function!");
218     return MBBNumbering[N];
219   }
220
221   /// getNumBlockIDs - Return the number of MBB ID's allocated.
222   ///
223   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
224   
225   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
226   /// recomputes them.  This guarantees that the MBB numbers are sequential,
227   /// dense, and match the ordering of the blocks within the function.  If a
228   /// specific MachineBasicBlock is specified, only that block and those after
229   /// it are renumbered.
230   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
231   
232   /// print - Print out the MachineFunction in a format suitable for debugging
233   /// to the specified stream.
234   ///
235   void print(raw_ostream &OS) const;
236
237   /// viewCFG - This function is meant for use from the debugger.  You can just
238   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
239   /// program, displaying the CFG of the current function with the code for each
240   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
241   /// in your path.
242   ///
243   void viewCFG() const;
244
245   /// viewCFGOnly - This function is meant for use from the debugger.  It works
246   /// just like viewCFG, but it does not include the contents of basic blocks
247   /// into the nodes, just the label.  If you are only interested in the CFG
248   /// this can make the graph smaller.
249   ///
250   void viewCFGOnly() const;
251
252   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
253   ///
254   void dump() const;
255
256   /// verify - Run the current MachineFunction through the machine code
257   /// verifier, useful for debugger use.
258   void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
259
260   // Provide accessors for the MachineBasicBlock list...
261   typedef BasicBlockListType::iterator iterator;
262   typedef BasicBlockListType::const_iterator const_iterator;
263   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
264   typedef std::reverse_iterator<iterator>             reverse_iterator;
265
266   /// addLiveIn - Add the specified physical register as a live-in value and
267   /// create a corresponding virtual register for it.
268   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
269
270   //===--------------------------------------------------------------------===//
271   // BasicBlock accessor functions.
272   //
273   iterator                 begin()       { return BasicBlocks.begin(); }
274   const_iterator           begin() const { return BasicBlocks.begin(); }
275   iterator                 end  ()       { return BasicBlocks.end();   }
276   const_iterator           end  () const { return BasicBlocks.end();   }
277
278   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
279   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
280   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
281   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
282
283   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
284   bool                     empty() const { return BasicBlocks.empty(); }
285   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
286         MachineBasicBlock &front()       { return BasicBlocks.front(); }
287   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
288         MachineBasicBlock & back()       { return BasicBlocks.back(); }
289
290   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
291   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
292   void insert(iterator MBBI, MachineBasicBlock *MBB) {
293     BasicBlocks.insert(MBBI, MBB);
294   }
295   void splice(iterator InsertPt, iterator MBBI) {
296     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
297   }
298   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
299     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
300   }
301
302   void remove(iterator MBBI) {
303     BasicBlocks.remove(MBBI);
304   }
305   void erase(iterator MBBI) {
306     BasicBlocks.erase(MBBI);
307   }
308
309   //===--------------------------------------------------------------------===//
310   // Internal functions used to automatically number MachineBasicBlocks
311   //
312
313   /// getNextMBBNumber - Returns the next unique number to be assigned
314   /// to a MachineBasicBlock in this MachineFunction.
315   ///
316   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
317     MBBNumbering.push_back(MBB);
318     return (unsigned)MBBNumbering.size()-1;
319   }
320
321   /// removeFromMBBNumbering - Remove the specific machine basic block from our
322   /// tracker, this is only really to be used by the MachineBasicBlock
323   /// implementation.
324   void removeFromMBBNumbering(unsigned N) {
325     assert(N < MBBNumbering.size() && "Illegal basic block #");
326     MBBNumbering[N] = 0;
327   }
328
329   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
330   /// of `new MachineInstr'.
331   ///
332   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
333                                    DebugLoc DL,
334                                    bool NoImp = false);
335
336   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
337   /// 'Orig' instruction, identical in all ways except the instruction
338   /// has no parent, prev, or next.
339   ///
340   /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
341   /// instructions.
342   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
343
344   /// DeleteMachineInstr - Delete the given MachineInstr.
345   ///
346   void DeleteMachineInstr(MachineInstr *MI);
347
348   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
349   /// instead of `new MachineBasicBlock'.
350   ///
351   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
352
353   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
354   ///
355   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
356
357   /// getMachineMemOperand - Allocate a new MachineMemOperand.
358   /// MachineMemOperands are owned by the MachineFunction and need not be
359   /// explicitly deallocated.
360   MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
361                                           int64_t o, uint64_t s,
362                                           unsigned base_alignment);
363
364   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
365   /// an existing one, adjusting by an offset and using the given size.
366   /// MachineMemOperands are owned by the MachineFunction and need not be
367   /// explicitly deallocated.
368   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
369                                           int64_t Offset, uint64_t Size);
370
371   /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
372   /// pointers.  This array is owned by the MachineFunction.
373   MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
374
375   /// extractLoadMemRefs - Allocate an array and populate it with just the
376   /// load information from the given MachineMemOperand sequence.
377   std::pair<MachineInstr::mmo_iterator,
378             MachineInstr::mmo_iterator>
379     extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
380                        MachineInstr::mmo_iterator End);
381
382   /// extractStoreMemRefs - Allocate an array and populate it with just the
383   /// store information from the given MachineMemOperand sequence.
384   std::pair<MachineInstr::mmo_iterator,
385             MachineInstr::mmo_iterator>
386     extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
387                         MachineInstr::mmo_iterator End);
388
389   //===--------------------------------------------------------------------===//
390   // Label Manipulation.
391   //
392   
393   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
394   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
395   /// normal 'L' label is returned.
396   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 
397                          bool isLinkerPrivate = false) const;
398   
399   
400   //===--------------------------------------------------------------------===//
401   // Debug location.
402   //
403
404   /// getDILocation - Get the DILocation for a given DebugLoc object.
405   DILocation getDILocation(DebugLoc DL) const;
406
407   /// getDefaultDebugLoc - Get the default debug location for the machine
408   /// function.
409   DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
410
411   /// setDefaultDebugLoc - Get the default debug location for the machine
412   /// function.
413   void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
414
415   /// getDebugLocInfo - Get the debug info location tracker.
416   DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
417 };
418
419 //===--------------------------------------------------------------------===//
420 // GraphTraits specializations for function basic block graphs (CFGs)
421 //===--------------------------------------------------------------------===//
422
423 // Provide specializations of GraphTraits to be able to treat a
424 // machine function as a graph of machine basic blocks... these are
425 // the same as the machine basic block iterators, except that the root
426 // node is implicitly the first node of the function.
427 //
428 template <> struct GraphTraits<MachineFunction*> :
429   public GraphTraits<MachineBasicBlock*> {
430   static NodeType *getEntryNode(MachineFunction *F) {
431     return &F->front();
432   }
433
434   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
435   typedef MachineFunction::iterator nodes_iterator;
436   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
437   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
438 };
439 template <> struct GraphTraits<const MachineFunction*> :
440   public GraphTraits<const MachineBasicBlock*> {
441   static NodeType *getEntryNode(const MachineFunction *F) {
442     return &F->front();
443   }
444
445   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
446   typedef MachineFunction::const_iterator nodes_iterator;
447   static nodes_iterator nodes_begin(const MachineFunction *F) {
448     return F->begin();
449   }
450   static nodes_iterator nodes_end  (const MachineFunction *F) {
451     return F->end();
452   }
453 };
454
455
456 // Provide specializations of GraphTraits to be able to treat a function as a
457 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
458 // a function is considered to be when traversing the predecessor edges of a BB
459 // instead of the successor edges.
460 //
461 template <> struct GraphTraits<Inverse<MachineFunction*> > :
462   public GraphTraits<Inverse<MachineBasicBlock*> > {
463   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
464     return &G.Graph->front();
465   }
466 };
467 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
468   public GraphTraits<Inverse<const MachineBasicBlock*> > {
469   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
470     return &G.Graph->front();
471   }
472 };
473
474 } // End llvm namespace
475
476 #endif