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