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