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