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