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