Temporarily revert r104655 as it's breaking the bots.
[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   /// FunctionNumber - This provides a unique ID for each function emitted in
113   /// this translation unit.
114   ///
115   unsigned FunctionNumber;
116   
117   /// The alignment of the function.
118   unsigned Alignment;
119
120   MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
121   void operator=(const MachineFunction&);   // DO NOT IMPLEMENT
122 public:
123   MachineFunction(const Function *Fn, const TargetMachine &TM,
124                   unsigned FunctionNum, MachineModuleInfo &MMI);
125   ~MachineFunction();
126
127   MachineModuleInfo &getMMI() const { return MMI; }
128   MCContext &getContext() const { return Ctx; }
129   
130   /// getFunction - Return the LLVM function that this machine code represents
131   ///
132   const 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   /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
181   void EnsureAlignment(unsigned A) {
182     if (Alignment < A) Alignment = A;
183   }
184   
185   /// getInfo - Keep track of various per-function pieces of information for
186   /// backends that would like to do so.
187   ///
188   template<typename Ty>
189   Ty *getInfo() {
190     if (!MFInfo) {
191         // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
192         // that apparently breaks GCC 3.3.
193         Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
194                                                       AlignOf<Ty>::Alignment));
195         MFInfo = new (Loc) Ty(*this);
196     }
197     return static_cast<Ty*>(MFInfo);
198   }
199
200   template<typename Ty>
201   const Ty *getInfo() const {
202      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
203   }
204
205   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
206   /// are inserted into the machine function.  The block number for a machine
207   /// basic block can be found by using the MBB::getBlockNumber method, this
208   /// method provides the inverse mapping.
209   ///
210   MachineBasicBlock *getBlockNumbered(unsigned N) const {
211     assert(N < MBBNumbering.size() && "Illegal block number");
212     assert(MBBNumbering[N] && "Block was removed from the machine function!");
213     return MBBNumbering[N];
214   }
215
216   /// getNumBlockIDs - Return the number of MBB ID's allocated.
217   ///
218   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
219   
220   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
221   /// recomputes them.  This guarantees that the MBB numbers are sequential,
222   /// dense, and match the ordering of the blocks within the function.  If a
223   /// specific MachineBasicBlock is specified, only that block and those after
224   /// it are renumbered.
225   void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
226   
227   /// print - Print out the MachineFunction in a format suitable for debugging
228   /// to the specified stream.
229   ///
230   void print(raw_ostream &OS) const;
231
232   /// viewCFG - This function is meant for use from the debugger.  You can just
233   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
234   /// program, displaying the CFG of the current function with the code for each
235   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
236   /// in your path.
237   ///
238   void viewCFG() const;
239
240   /// viewCFGOnly - This function is meant for use from the debugger.  It works
241   /// just like viewCFG, but it does not include the contents of basic blocks
242   /// into the nodes, just the label.  If you are only interested in the CFG
243   /// this can make the graph smaller.
244   ///
245   void viewCFGOnly() const;
246
247   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
248   ///
249   void dump() const;
250
251   /// verify - Run the current MachineFunction through the machine code
252   /// verifier, useful for debugger use.
253   void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
254
255   // Provide accessors for the MachineBasicBlock list...
256   typedef BasicBlockListType::iterator iterator;
257   typedef BasicBlockListType::const_iterator const_iterator;
258   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
259   typedef std::reverse_iterator<iterator>             reverse_iterator;
260
261   /// addLiveIn - Add the specified physical register as a live-in value and
262   /// create a corresponding virtual register for it.
263   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
264
265   //===--------------------------------------------------------------------===//
266   // BasicBlock accessor functions.
267   //
268   iterator                 begin()       { return BasicBlocks.begin(); }
269   const_iterator           begin() const { return BasicBlocks.begin(); }
270   iterator                 end  ()       { return BasicBlocks.end();   }
271   const_iterator           end  () const { return BasicBlocks.end();   }
272
273   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
274   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
275   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
276   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
277
278   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
279   bool                     empty() const { return BasicBlocks.empty(); }
280   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
281         MachineBasicBlock &front()       { return BasicBlocks.front(); }
282   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
283         MachineBasicBlock & back()       { return BasicBlocks.back(); }
284
285   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
286   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
287   void insert(iterator MBBI, MachineBasicBlock *MBB) {
288     BasicBlocks.insert(MBBI, MBB);
289   }
290   void splice(iterator InsertPt, iterator MBBI) {
291     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
292   }
293   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
294     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
295   }
296
297   void remove(iterator MBBI) {
298     BasicBlocks.remove(MBBI);
299   }
300   void erase(iterator MBBI) {
301     BasicBlocks.erase(MBBI);
302   }
303
304   //===--------------------------------------------------------------------===//
305   // Internal functions used to automatically number MachineBasicBlocks
306   //
307
308   /// getNextMBBNumber - Returns the next unique number to be assigned
309   /// to a MachineBasicBlock in this MachineFunction.
310   ///
311   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
312     MBBNumbering.push_back(MBB);
313     return (unsigned)MBBNumbering.size()-1;
314   }
315
316   /// removeFromMBBNumbering - Remove the specific machine basic block from our
317   /// tracker, this is only really to be used by the MachineBasicBlock
318   /// implementation.
319   void removeFromMBBNumbering(unsigned N) {
320     assert(N < MBBNumbering.size() && "Illegal basic block #");
321     MBBNumbering[N] = 0;
322   }
323
324   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
325   /// of `new MachineInstr'.
326   ///
327   MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
328                                    DebugLoc DL,
329                                    bool NoImp = false);
330
331   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
332   /// 'Orig' instruction, identical in all ways except the instruction
333   /// has no parent, prev, or next.
334   ///
335   /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
336   /// instructions.
337   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
338
339   /// DeleteMachineInstr - Delete the given MachineInstr.
340   ///
341   void DeleteMachineInstr(MachineInstr *MI);
342
343   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
344   /// instead of `new MachineBasicBlock'.
345   ///
346   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
347
348   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
349   ///
350   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
351
352   /// getMachineMemOperand - Allocate a new MachineMemOperand.
353   /// MachineMemOperands are owned by the MachineFunction and need not be
354   /// explicitly deallocated.
355   MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
356                                           int64_t o, uint64_t s,
357                                           unsigned base_alignment);
358
359   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
360   /// an existing one, adjusting by an offset and using the given size.
361   /// MachineMemOperands are owned by the MachineFunction and need not be
362   /// explicitly deallocated.
363   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
364                                           int64_t Offset, uint64_t Size);
365
366   /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
367   /// pointers.  This array is owned by the MachineFunction.
368   MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
369
370   /// extractLoadMemRefs - Allocate an array and populate it with just the
371   /// load information from the given MachineMemOperand sequence.
372   std::pair<MachineInstr::mmo_iterator,
373             MachineInstr::mmo_iterator>
374     extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
375                        MachineInstr::mmo_iterator End);
376
377   /// extractStoreMemRefs - Allocate an array and populate it with just the
378   /// store information from the given MachineMemOperand sequence.
379   std::pair<MachineInstr::mmo_iterator,
380             MachineInstr::mmo_iterator>
381     extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
382                         MachineInstr::mmo_iterator End);
383
384   //===--------------------------------------------------------------------===//
385   // Label Manipulation.
386   //
387   
388   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
389   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
390   /// normal 'L' label is returned.
391   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 
392                          bool isLinkerPrivate = false) const;
393 };
394
395 //===--------------------------------------------------------------------===//
396 // GraphTraits specializations for function basic block graphs (CFGs)
397 //===--------------------------------------------------------------------===//
398
399 // Provide specializations of GraphTraits to be able to treat a
400 // machine function as a graph of machine basic blocks... these are
401 // the same as the machine basic block iterators, except that the root
402 // node is implicitly the first node of the function.
403 //
404 template <> struct GraphTraits<MachineFunction*> :
405   public GraphTraits<MachineBasicBlock*> {
406   static NodeType *getEntryNode(MachineFunction *F) {
407     return &F->front();
408   }
409
410   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
411   typedef MachineFunction::iterator nodes_iterator;
412   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
413   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
414 };
415 template <> struct GraphTraits<const MachineFunction*> :
416   public GraphTraits<const MachineBasicBlock*> {
417   static NodeType *getEntryNode(const MachineFunction *F) {
418     return &F->front();
419   }
420
421   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
422   typedef MachineFunction::const_iterator nodes_iterator;
423   static nodes_iterator nodes_begin(const MachineFunction *F) {
424     return F->begin();
425   }
426   static nodes_iterator nodes_end  (const MachineFunction *F) {
427     return F->end();
428   }
429 };
430
431
432 // Provide specializations of GraphTraits to be able to treat a function as a
433 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
434 // a function is considered to be when traversing the predecessor edges of a BB
435 // instead of the successor edges.
436 //
437 template <> struct GraphTraits<Inverse<MachineFunction*> > :
438   public GraphTraits<Inverse<MachineBasicBlock*> > {
439   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
440     return &G.Graph->front();
441   }
442 };
443 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
444   public GraphTraits<Inverse<const MachineBasicBlock*> > {
445   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
446     return &G.Graph->front();
447   }
448 };
449
450 } // End llvm namespace
451
452 #endif