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