Remove unused member variable.
[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/ADT/ilist.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/IR/DebugLoc.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/ArrayRecycler.h"
27 #include "llvm/Support/Recycler.h"
28
29 namespace llvm {
30
31 class Value;
32 class Function;
33 class GCModuleInfo;
34 class MachineRegisterInfo;
35 class MachineFrameInfo;
36 class MachineConstantPool;
37 class MachineJumpTableInfo;
38 class MachineModuleInfo;
39 class MCContext;
40 class Pass;
41 class TargetMachine;
42 class TargetSubtargetInfo;
43 class TargetRegisterClass;
44 struct MachinePointerInfo;
45
46 template <>
47 struct ilist_traits<MachineBasicBlock>
48     : public ilist_default_traits<MachineBasicBlock> {
49   mutable ilist_half_node<MachineBasicBlock> Sentinel;
50 public:
51   MachineBasicBlock *createSentinel() const {
52     return static_cast<MachineBasicBlock*>(&Sentinel);
53   }
54   void destroySentinel(MachineBasicBlock *) const {}
55
56   MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
57   MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
58     return createSentinel();
59   }
60   static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
61
62   void addNodeToList(MachineBasicBlock* MBB);
63   void removeNodeFromList(MachineBasicBlock* MBB);
64   void deleteNode(MachineBasicBlock *MBB);
65 private:
66   void createNode(const MachineBasicBlock &);
67 };
68
69 /// MachineFunctionInfo - This class can be derived from and used by targets to
70 /// hold private target-specific information for each MachineFunction.  Objects
71 /// of type are accessed/created with MF::getInfo and destroyed when the
72 /// MachineFunction is destroyed.
73 struct MachineFunctionInfo {
74   virtual ~MachineFunctionInfo();
75 };
76
77 class MachineFunction {
78   const Function *Fn;
79   const TargetMachine &Target;
80   const TargetSubtargetInfo *STI;
81   MCContext &Ctx;
82   MachineModuleInfo &MMI;
83
84   // RegInfo - Information about each register in use in the function.
85   MachineRegisterInfo *RegInfo;
86
87   // Used to keep track of target-specific per-machine function information for
88   // the target implementation.
89   MachineFunctionInfo *MFInfo;
90
91   // Keep track of objects allocated on the stack.
92   MachineFrameInfo *FrameInfo;
93
94   // Keep track of constants which are spilled to memory
95   MachineConstantPool *ConstantPool;
96   
97   // Keep track of jump tables for switch instructions
98   MachineJumpTableInfo *JumpTableInfo;
99
100   // Function-level unique numbering for MachineBasicBlocks.  When a
101   // MachineBasicBlock is inserted into a MachineFunction is it automatically
102   // numbered and this vector keeps track of the mapping from ID's to MBB's.
103   std::vector<MachineBasicBlock*> MBBNumbering;
104
105   // Pool-allocate MachineFunction-lifetime and IR objects.
106   BumpPtrAllocator Allocator;
107
108   // Allocation management for instructions in function.
109   Recycler<MachineInstr> InstructionRecycler;
110
111   // Allocation management for operand arrays on instructions.
112   ArrayRecycler<MachineOperand> OperandRecycler;
113
114   // Allocation management for basic blocks in function.
115   Recycler<MachineBasicBlock> BasicBlockRecycler;
116
117   // List of machine basic blocks in function
118   typedef ilist<MachineBasicBlock> BasicBlockListType;
119   BasicBlockListType BasicBlocks;
120
121   /// FunctionNumber - This provides a unique ID for each function emitted in
122   /// this translation unit.
123   ///
124   unsigned FunctionNumber;
125   
126   /// Alignment - The alignment of the function.
127   unsigned Alignment;
128
129   /// ExposesReturnsTwice - True if the function calls setjmp or related
130   /// functions with attribute "returns twice", but doesn't have
131   /// the attribute itself.
132   /// This is used to limit optimizations which cannot reason
133   /// about the control flow of such functions.
134   bool ExposesReturnsTwice;
135
136   /// True if the function includes any inline assembly.
137   bool HasInlineAsm;
138
139   MachineFunction(const MachineFunction &) LLVM_DELETED_FUNCTION;
140   void operator=(const MachineFunction&) LLVM_DELETED_FUNCTION;
141 public:
142   MachineFunction(const Function *Fn, const TargetMachine &TM,
143                   unsigned FunctionNum, MachineModuleInfo &MMI);
144   ~MachineFunction();
145
146   MachineModuleInfo &getMMI() const { return MMI; }
147   MCContext &getContext() const { return Ctx; }
148
149   /// getFunction - Return the LLVM function that this machine code represents
150   ///
151   const Function *getFunction() const { return Fn; }
152
153   /// getName - Return the name of the corresponding LLVM function.
154   ///
155   StringRef getName() const;
156
157   /// getFunctionNumber - Return a unique ID for the current function.
158   ///
159   unsigned getFunctionNumber() const { return FunctionNumber; }
160
161   /// getTarget - Return the target machine this machine code is compiled with
162   ///
163   const TargetMachine &getTarget() const { return Target; }
164
165   /// getSubtarget - Return the subtarget for which this machine code is being
166   /// compiled.
167   const TargetSubtargetInfo &getSubtarget() const { return *STI; }
168   void setSubtarget(const TargetSubtargetInfo *ST) { STI = ST; }
169
170   /// getRegInfo - Return information about the registers currently in use.
171   ///
172   MachineRegisterInfo &getRegInfo() { return *RegInfo; }
173   const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
174
175   /// getFrameInfo - Return the frame info object for the current function.
176   /// This object contains information about objects allocated on the stack
177   /// frame of the current function in an abstract way.
178   ///
179   MachineFrameInfo *getFrameInfo() { return FrameInfo; }
180   const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
181
182   /// getJumpTableInfo - Return the jump table info object for the current 
183   /// function.  This object contains information about jump tables in the
184   /// current function.  If the current function has no jump tables, this will
185   /// return null.
186   const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
187   MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
188
189   /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
190   /// does already exist, allocate one.
191   MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
192
193   
194   /// getConstantPool - Return the constant pool object for the current
195   /// function.
196   ///
197   MachineConstantPool *getConstantPool() { return ConstantPool; }
198   const MachineConstantPool *getConstantPool() const { return ConstantPool; }
199
200   /// getAlignment - Return the alignment (log2, not bytes) of the function.
201   ///
202   unsigned getAlignment() const { return Alignment; }
203
204   /// setAlignment - Set the alignment (log2, not bytes) of the function.
205   ///
206   void setAlignment(unsigned A) { Alignment = A; }
207
208   /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
209   void ensureAlignment(unsigned A) {
210     if (Alignment < A) Alignment = A;
211   }
212
213   /// exposesReturnsTwice - Returns true if the function calls setjmp or
214   /// any other similar functions with attribute "returns twice" without
215   /// having the attribute itself.
216   bool exposesReturnsTwice() const {
217     return ExposesReturnsTwice;
218   }
219
220   /// setCallsSetJmp - Set a flag that indicates if there's a call to
221   /// a "returns twice" function.
222   void setExposesReturnsTwice(bool B) {
223     ExposesReturnsTwice = B;
224   }
225
226   /// Returns true if the function contains any inline assembly.
227   bool hasInlineAsm() const {
228     return HasInlineAsm;
229   }
230
231   /// Set a flag that indicates that the function contains inline assembly.
232   void setHasInlineAsm(bool B) {
233     HasInlineAsm = B;
234   }
235
236   /// getInfo - Keep track of various per-function pieces of information for
237   /// backends that would like to do so.
238   ///
239   template<typename Ty>
240   Ty *getInfo() {
241     if (!MFInfo)
242       MFInfo = new (Allocator.Allocate<Ty>()) Ty(*this);
243     return static_cast<Ty*>(MFInfo);
244   }
245
246   template<typename Ty>
247   const Ty *getInfo() const {
248      return const_cast<MachineFunction*>(this)->getInfo<Ty>();
249   }
250
251   /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
252   /// are inserted into the machine function.  The block number for a machine
253   /// basic block can be found by using the MBB::getBlockNumber method, this
254   /// method provides the inverse mapping.
255   ///
256   MachineBasicBlock *getBlockNumbered(unsigned N) const {
257     assert(N < MBBNumbering.size() && "Illegal block number");
258     assert(MBBNumbering[N] && "Block was removed from the machine function!");
259     return MBBNumbering[N];
260   }
261
262   /// Should we be emitting segmented stack stuff for the function
263   bool shouldSplitStack();
264
265   /// getNumBlockIDs - Return the number of MBB ID's allocated.
266   ///
267   unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
268   
269   /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
270   /// recomputes them.  This guarantees that the MBB numbers are sequential,
271   /// dense, and match the ordering of the blocks within the function.  If a
272   /// specific MachineBasicBlock is specified, only that block and those after
273   /// it are renumbered.
274   void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr);
275   
276   /// print - Print out the MachineFunction in a format suitable for debugging
277   /// to the specified stream.
278   ///
279   void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
280
281   /// viewCFG - This function is meant for use from the debugger.  You can just
282   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
283   /// program, displaying the CFG of the current function with the code for each
284   /// basic block inside.  This depends on there being a 'dot' and 'gv' program
285   /// in your path.
286   ///
287   void viewCFG() const;
288
289   /// viewCFGOnly - This function is meant for use from the debugger.  It works
290   /// just like viewCFG, but it does not include the contents of basic blocks
291   /// into the nodes, just the label.  If you are only interested in the CFG
292   /// this can make the graph smaller.
293   ///
294   void viewCFGOnly() const;
295
296   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
297   ///
298   void dump() const;
299
300   /// verify - Run the current MachineFunction through the machine code
301   /// verifier, useful for debugger use.
302   void verify(Pass *p = nullptr, const char *Banner = nullptr) const;
303
304   // Provide accessors for the MachineBasicBlock list...
305   typedef BasicBlockListType::iterator iterator;
306   typedef BasicBlockListType::const_iterator const_iterator;
307   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
308   typedef std::reverse_iterator<iterator>             reverse_iterator;
309
310   /// addLiveIn - Add the specified physical register as a live-in value and
311   /// create a corresponding virtual register for it.
312   unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
313
314   //===--------------------------------------------------------------------===//
315   // BasicBlock accessor functions.
316   //
317   iterator                 begin()       { return BasicBlocks.begin(); }
318   const_iterator           begin() const { return BasicBlocks.begin(); }
319   iterator                 end  ()       { return BasicBlocks.end();   }
320   const_iterator           end  () const { return BasicBlocks.end();   }
321
322   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
323   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
324   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
325   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
326
327   unsigned                  size() const { return (unsigned)BasicBlocks.size();}
328   bool                     empty() const { return BasicBlocks.empty(); }
329   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
330         MachineBasicBlock &front()       { return BasicBlocks.front(); }
331   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
332         MachineBasicBlock & back()       { return BasicBlocks.back(); }
333
334   void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
335   void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
336   void insert(iterator MBBI, MachineBasicBlock *MBB) {
337     BasicBlocks.insert(MBBI, MBB);
338   }
339   void splice(iterator InsertPt, iterator MBBI) {
340     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
341   }
342   void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
343     BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
344   }
345
346   void remove(iterator MBBI) {
347     BasicBlocks.remove(MBBI);
348   }
349   void erase(iterator MBBI) {
350     BasicBlocks.erase(MBBI);
351   }
352
353   //===--------------------------------------------------------------------===//
354   // Internal functions used to automatically number MachineBasicBlocks
355   //
356
357   /// \brief Adds the MBB to the internal numbering. Returns the unique number
358   /// assigned to the MBB.
359   ///
360   unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
361     MBBNumbering.push_back(MBB);
362     return (unsigned)MBBNumbering.size()-1;
363   }
364
365   /// removeFromMBBNumbering - Remove the specific machine basic block from our
366   /// tracker, this is only really to be used by the MachineBasicBlock
367   /// implementation.
368   void removeFromMBBNumbering(unsigned N) {
369     assert(N < MBBNumbering.size() && "Illegal basic block #");
370     MBBNumbering[N] = nullptr;
371   }
372
373   /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
374   /// of `new MachineInstr'.
375   ///
376   MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID,
377                                    DebugLoc DL,
378                                    bool NoImp = false);
379
380   /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
381   /// 'Orig' instruction, identical in all ways except the instruction
382   /// has no parent, prev, or next.
383   ///
384   /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
385   /// instructions.
386   MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
387
388   /// DeleteMachineInstr - Delete the given MachineInstr.
389   ///
390   void DeleteMachineInstr(MachineInstr *MI);
391
392   /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
393   /// instead of `new MachineBasicBlock'.
394   ///
395   MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = nullptr);
396
397   /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
398   ///
399   void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
400
401   /// getMachineMemOperand - Allocate a new MachineMemOperand.
402   /// MachineMemOperands are owned by the MachineFunction and need not be
403   /// explicitly deallocated.
404   MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
405                                           unsigned f, uint64_t s,
406                                           unsigned base_alignment,
407                                           const AAMDNodes &AAInfo = AAMDNodes(),
408                                           const MDNode *Ranges = nullptr);
409   
410   /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
411   /// an existing one, adjusting by an offset and using the given size.
412   /// MachineMemOperands are owned by the MachineFunction and need not be
413   /// explicitly deallocated.
414   MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
415                                           int64_t Offset, uint64_t Size);
416
417   typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
418
419   /// Allocate an array of MachineOperands. This is only intended for use by
420   /// internal MachineInstr functions.
421   MachineOperand *allocateOperandArray(OperandCapacity Cap) {
422     return OperandRecycler.allocate(Cap, Allocator);
423   }
424
425   /// Dellocate an array of MachineOperands and recycle the memory. This is
426   /// only intended for use by internal MachineInstr functions.
427   /// Cap must be the same capacity that was used to allocate the array.
428   void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) {
429     OperandRecycler.deallocate(Cap, Array);
430   }
431
432   /// \brief Allocate and initialize a register mask with @p NumRegister bits.
433   uint32_t *allocateRegisterMask(unsigned NumRegister) {
434     unsigned Size = (NumRegister + 31) / 32;
435     uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
436     for (unsigned i = 0; i != Size; ++i)
437       Mask[i] = 0;
438     return Mask;
439   }
440
441   /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
442   /// pointers.  This array is owned by the MachineFunction.
443   MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
444
445   /// extractLoadMemRefs - Allocate an array and populate it with just the
446   /// load information from the given MachineMemOperand sequence.
447   std::pair<MachineInstr::mmo_iterator,
448             MachineInstr::mmo_iterator>
449     extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
450                        MachineInstr::mmo_iterator End);
451
452   /// extractStoreMemRefs - Allocate an array and populate it with just the
453   /// store information from the given MachineMemOperand sequence.
454   std::pair<MachineInstr::mmo_iterator,
455             MachineInstr::mmo_iterator>
456     extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
457                         MachineInstr::mmo_iterator End);
458
459   //===--------------------------------------------------------------------===//
460   // Label Manipulation.
461   //
462   
463   /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
464   /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
465   /// normal 'L' label is returned.
466   MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 
467                          bool isLinkerPrivate = false) const;
468   
469   /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
470   /// base.
471   MCSymbol *getPICBaseSymbol() const;
472 };
473
474 //===--------------------------------------------------------------------===//
475 // GraphTraits specializations for function basic block graphs (CFGs)
476 //===--------------------------------------------------------------------===//
477
478 // Provide specializations of GraphTraits to be able to treat a
479 // machine function as a graph of machine basic blocks... these are
480 // the same as the machine basic block iterators, except that the root
481 // node is implicitly the first node of the function.
482 //
483 template <> struct GraphTraits<MachineFunction*> :
484   public GraphTraits<MachineBasicBlock*> {
485   static NodeType *getEntryNode(MachineFunction *F) {
486     return &F->front();
487   }
488
489   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
490   typedef MachineFunction::iterator nodes_iterator;
491   static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
492   static nodes_iterator nodes_end  (MachineFunction *F) { return F->end(); }
493   static unsigned       size       (MachineFunction *F) { return F->size(); }
494 };
495 template <> struct GraphTraits<const MachineFunction*> :
496   public GraphTraits<const MachineBasicBlock*> {
497   static NodeType *getEntryNode(const MachineFunction *F) {
498     return &F->front();
499   }
500
501   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
502   typedef MachineFunction::const_iterator nodes_iterator;
503   static nodes_iterator nodes_begin(const MachineFunction *F) {
504     return F->begin();
505   }
506   static nodes_iterator nodes_end  (const MachineFunction *F) {
507     return F->end();
508   }
509   static unsigned       size       (const MachineFunction *F)  {
510     return F->size();
511   }
512 };
513
514
515 // Provide specializations of GraphTraits to be able to treat a function as a
516 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
517 // a function is considered to be when traversing the predecessor edges of a BB
518 // instead of the successor edges.
519 //
520 template <> struct GraphTraits<Inverse<MachineFunction*> > :
521   public GraphTraits<Inverse<MachineBasicBlock*> > {
522   static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
523     return &G.Graph->front();
524   }
525 };
526 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
527   public GraphTraits<Inverse<const MachineBasicBlock*> > {
528   static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
529     return &G.Graph->front();
530   }
531 };
532
533 } // End llvm namespace
534
535 #endif