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