e1c19865fa48f6e1886389d8ed11711f718d68ca
[oota-llvm.git] / include / llvm / CodeGen / MachineFunction.h
1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Support/Annotation.h"
23
24 namespace llvm {
25
26 // ilist_traits
27 template <>
28 class ilist_traits<MachineBasicBlock> {
29   // this is only set by the MachineFunction owning the ilist
30   friend class MachineFunction;
31   MachineFunction* Parent;
32   
33 public:
34   ilist_traits<MachineBasicBlock>() : Parent(0) { }
35   
36   static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
37   static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
38   
39   static const MachineBasicBlock*
40   getPrev(const MachineBasicBlock* N) { return N->Prev; }
41   
42   static const MachineBasicBlock*
43   getNext(const MachineBasicBlock* N) { return N->Next; }
44   
45   static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { N->Prev = prev; }
46   static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { N->Next = next; }
47   
48   static MachineBasicBlock* createNode();
49   void addNodeToList(MachineBasicBlock* N);
50   void removeNodeFromList(MachineBasicBlock* N);
51   void transferNodesFromList(
52                              iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
53                              ilist_iterator<MachineBasicBlock> first,
54                              ilist_iterator<MachineBasicBlock> last);
55 };
56   
57
58
59 class Function;
60 class TargetMachine;
61 class SSARegMap;
62 class MachineFunctionInfo;
63 class MachineFrameInfo;
64 class MachineConstantPool;
65
66 class MachineFunction : private Annotation {
67   const Function *Fn;
68   const TargetMachine &Target;
69
70   // List of machine basic blocks in function
71   ilist<MachineBasicBlock> BasicBlocks;
72
73   // Keeping track of mapping from SSA values to registers
74   SSARegMap *SSARegMapping;
75
76   // Used to keep track of frame and constant area information for sparc be
77   MachineFunctionInfo *MFInfo;
78
79   // Keep track of objects allocated on the stack.
80   MachineFrameInfo *FrameInfo;
81
82   // Keep track of constants which are spilled to memory
83   MachineConstantPool *ConstantPool;
84
85   // Function-level unique numbering for MachineBasicBlocks
86   int NextMBBNumber;
87
88 public:
89   MachineFunction(const Function *Fn, const TargetMachine &TM);
90   ~MachineFunction();
91
92   /// getFunction - Return the LLVM function that this machine code represents
93   ///
94   const Function *getFunction() const { return Fn; }
95
96   /// getTarget - Return the target machine this machine code is compiled with
97   ///
98   const TargetMachine &getTarget() const { return Target; }
99
100   /// SSARegMap Interface... Keep track of information about each SSA virtual
101   /// register, such as which register class it belongs to.
102   ///
103   SSARegMap *getSSARegMap() const { return SSARegMapping; }
104   void clearSSARegMap();
105
106   /// getFrameInfo - Return the frame info object for the current function.
107   /// This object contains information about objects allocated on the stack
108   /// frame of the current function in an abstract way.
109   ///
110   MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
111
112   /// getConstantPool - Return the constant pool object for the current
113   /// function.
114   MachineConstantPool *getConstantPool() const { return ConstantPool; }
115
116   /// MachineFunctionInfo - Keep track of various per-function pieces of
117   /// information for the sparc backend.
118   ///
119   MachineFunctionInfo *getInfo() const { return MFInfo; }
120
121   /// getNextMBBNumber - Returns the next unique number to be assigned
122   /// to a MachineBasicBlock in this MachineFunction.
123   ///
124   int getNextMBBNumber() { return NextMBBNumber++; }
125
126   /// print - Print out the MachineFunction in a format suitable for debugging
127   /// to the specified stream.
128   ///
129   void print(std::ostream &OS) const;
130
131   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
132   ///
133   void dump() const;
134
135   // The next three methods are used to construct, destruct, and retrieve the
136   // MachineFunction object for the given method.
137   //
138   // construct() -- Allocates and initializes for a given method and target
139   // get()       -- Returns a handle to the object.
140   //                This should not be called before "construct()"
141   //                for a given Method.
142   // 
143   static MachineFunction& construct(const Function *F, const TargetMachine &TM);
144   static void destruct(const Function *F);
145   static MachineFunction& get(const Function *F);
146
147   // Provide accessors for the MachineBasicBlock list...
148   typedef ilist<MachineBasicBlock> BasicBlockListType;
149   typedef BasicBlockListType::iterator iterator;
150   typedef BasicBlockListType::const_iterator const_iterator;
151   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
152   typedef std::reverse_iterator<iterator>             reverse_iterator;
153
154   // Provide accessors for basic blocks...
155   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
156         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
157  
158   //===--------------------------------------------------------------------===//
159   // BasicBlock iterator forwarding functions
160   //
161   iterator                 begin()       { return BasicBlocks.begin(); }
162   const_iterator           begin() const { return BasicBlocks.begin(); }
163   iterator                 end  ()       { return BasicBlocks.end();   }
164   const_iterator           end  () const { return BasicBlocks.end();   }
165
166   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
167   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
168   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
169   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
170
171   unsigned                  size() const { return BasicBlocks.size(); }
172   bool                     empty() const { return BasicBlocks.empty(); }
173   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
174         MachineBasicBlock &front()       { return BasicBlocks.front(); }
175   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
176         MachineBasicBlock & back()       { return BasicBlocks.back(); }
177 };
178
179 } // End llvm namespace
180
181 #endif