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