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