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