1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code for a function. This class contains a list of
11 // MachineBasicBlock instances that make up the current compiled function.
13 // This class also contains pointers to various classes which hold
14 // target-specific information about the generated code.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
19 #define LLVM_CODEGEN_MACHINEFUNCTION_H
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/Support/Annotation.h"
29 class MachineFrameInfo;
30 class MachineConstantPool;
34 struct ilist_traits<MachineBasicBlock> {
35 // this is only set by the MachineFunction owning the ilist
36 friend class MachineFunction;
37 MachineFunction* Parent;
40 ilist_traits<MachineBasicBlock>() : Parent(0) { }
42 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
43 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
45 static const MachineBasicBlock*
46 getPrev(const MachineBasicBlock* N) { return N->Prev; }
48 static const MachineBasicBlock*
49 getNext(const MachineBasicBlock* N) { return N->Next; }
51 static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
54 static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
58 static MachineBasicBlock* createSentinel();
59 static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; }
60 void addNodeToList(MachineBasicBlock* N);
61 void removeNodeFromList(MachineBasicBlock* N);
62 void transferNodesFromList(iplist<MachineBasicBlock,
63 ilist_traits<MachineBasicBlock> > &toList,
64 ilist_iterator<MachineBasicBlock> first,
65 ilist_iterator<MachineBasicBlock> last);
68 /// MachineFunctionInfo - This class can be derived from and used by targets to
69 /// hold private target-specific information for each MachineFunction. Objects
70 /// of type are accessed/created with MF::getInfo and destroyed when the
71 /// MachineFunction is destroyed.
72 struct MachineFunctionInfo {
73 virtual ~MachineFunctionInfo() {};
76 class MachineFunction : private Annotation {
78 const TargetMachine &Target;
80 // List of machine basic blocks in function
81 ilist<MachineBasicBlock> BasicBlocks;
83 // Keeping track of mapping from SSA values to registers
84 SSARegMap *SSARegMapping;
86 // Used to keep track of target-specific per-machine function information for
87 // the target implementation.
88 MachineFunctionInfo *MFInfo;
90 // Keep track of objects allocated on the stack.
91 MachineFrameInfo *FrameInfo;
93 // Keep track of constants which are spilled to memory
94 MachineConstantPool *ConstantPool;
96 // Function-level unique numbering for MachineBasicBlocks. When a
97 // MachineBasicBlock is inserted into a MachineFunction is it automatically
98 // numbered and this vector keeps track of the mapping from ID's to MBB's.
99 std::vector<MachineBasicBlock*> MBBNumbering;
101 /// UsedPhysRegs - This is a new[]'d array of bools that is computed and set
102 /// by the register allocator, and must be kept up to date by passes that run
103 /// after register allocation (though most don't modify this). This is used
104 /// so that the code generator knows which callee save registers to save and
105 /// for other target specific uses.
108 /// LiveIns/LiveOuts - Keep track of the physical registers that are
109 /// livein/liveout of the function. Live in values are typically arguments in
110 /// registers, live out values are typically return values in registers.
111 std::vector<unsigned> LiveIns, LiveOuts;
114 MachineFunction(const Function *Fn, const TargetMachine &TM);
117 /// getFunction - Return the LLVM function that this machine code represents
119 const Function *getFunction() const { return Fn; }
121 /// getTarget - Return the target machine this machine code is compiled with
123 const TargetMachine &getTarget() const { return Target; }
125 /// SSARegMap Interface... Keep track of information about each SSA virtual
126 /// register, such as which register class it belongs to.
128 SSARegMap *getSSARegMap() const { return SSARegMapping; }
129 void clearSSARegMap();
131 /// getFrameInfo - Return the frame info object for the current function.
132 /// This object contains information about objects allocated on the stack
133 /// frame of the current function in an abstract way.
135 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
137 /// getConstantPool - Return the constant pool object for the current
140 MachineConstantPool *getConstantPool() const { return ConstantPool; }
142 /// MachineFunctionInfo - Keep track of various per-function pieces of
143 /// information for backends that would like to do so. (Currently,
146 template<typename Ty>
148 if (!MFInfo) MFInfo = new Ty(*this);
150 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
151 "Invalid concrete type or multiple inheritence for getInfo");
152 return static_cast<Ty*>(MFInfo);
155 /// setUsedPhysRegs - The register allocator should call this to initialized
156 /// the UsedPhysRegs set. This should be passed a new[]'d array with entries
157 /// for all of the physical registers that the target supports. Each array
158 /// entry should be set to true iff the physical register is used within the
160 void setUsedPhysRegs(bool *UPR) { UsedPhysRegs = UPR; }
162 /// getUsedPhysregs - This returns the UsedPhysRegs array. This returns null
163 /// before register allocation.
164 bool *getUsedPhysregs() { return UsedPhysRegs; }
165 const bool *getUsedPhysregs() const { return UsedPhysRegs; }
167 /// isPhysRegUsed - Return true if the specified register is used in this
168 /// function. This only works after register allocation.
169 bool isPhysRegUsed(unsigned Reg) { return UsedPhysRegs[Reg]; }
171 /// changePhyRegUsed - This method allows code that runs after register
172 /// allocation to keep the PhysRegsUsed array up-to-date.
173 void changePhyRegUsed(unsigned Reg, bool State) { UsedPhysRegs[Reg] = State; }
176 // LiveIn/LiveOut management methods.
178 /// addLiveIn/Out - Add the specified register as a live in/out. Note that it
179 /// is an error to add the same register to the same set more than once.
180 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
181 void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
183 // Iteration support for live in/out sets. These sets are kept in sorted
184 // order by their register number.
185 typedef std::vector<unsigned>::const_iterator liveinout_iterator;
186 liveinout_iterator livein_begin() const { return LiveIns.begin(); }
187 liveinout_iterator livein_end() const { return LiveIns.end(); }
188 liveinout_iterator liveout_begin() const { return LiveOuts.begin(); }
189 liveinout_iterator liveout_end() const { return LiveOuts.end(); }
191 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
192 /// are inserted into the machine function. The block number for a machine
193 /// basic block can be found by using the MBB::getBlockNumber method, this
194 /// method provides the inverse mapping.
196 MachineBasicBlock *getBlockNumbered(unsigned N) {
197 assert(N < MBBNumbering.size() && "Illegal block number");
198 assert(MBBNumbering[N] && "Block was removed from the machine function!");
199 return MBBNumbering[N];
202 /// getLastBlock - Returns the MachineBasicBlock with the greatest number
203 MachineBasicBlock *getLastBlock() {
204 return MBBNumbering.back();
206 const MachineBasicBlock *getLastBlock() const {
207 return MBBNumbering.back();
210 /// print - Print out the MachineFunction in a format suitable for debugging
211 /// to the specified stream.
213 void print(std::ostream &OS) const;
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
221 void viewCFG() const;
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.
228 void viewCFGOnly() const;
230 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
234 /// construct - Allocate and initialize a MachineFunction for a given Function
237 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
239 /// destruct - Destroy the MachineFunction corresponding to a given Function
241 static void destruct(const Function *F);
243 /// get - Return a handle to a MachineFunction corresponding to the given
244 /// Function. This should not be called before "construct()" for a given
247 static MachineFunction& get(const Function *F);
249 // Provide accessors for the MachineBasicBlock list...
250 typedef ilist<MachineBasicBlock> BasicBlockListType;
251 typedef BasicBlockListType::iterator iterator;
252 typedef BasicBlockListType::const_iterator const_iterator;
253 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
254 typedef std::reverse_iterator<iterator> reverse_iterator;
256 // Provide accessors for basic blocks...
257 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
258 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
260 //===--------------------------------------------------------------------===//
261 // BasicBlock iterator forwarding functions
263 iterator begin() { return BasicBlocks.begin(); }
264 const_iterator begin() const { return BasicBlocks.begin(); }
265 iterator end () { return BasicBlocks.end(); }
266 const_iterator end () const { return BasicBlocks.end(); }
268 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
269 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
270 reverse_iterator rend () { return BasicBlocks.rend(); }
271 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
273 unsigned size() const { return BasicBlocks.size(); }
274 bool empty() const { return BasicBlocks.empty(); }
275 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
276 MachineBasicBlock &front() { return BasicBlocks.front(); }
277 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
278 MachineBasicBlock & back() { return BasicBlocks.back(); }
280 //===--------------------------------------------------------------------===//
281 // Internal functions used to automatically number MachineBasicBlocks
284 /// getNextMBBNumber - Returns the next unique number to be assigned
285 /// to a MachineBasicBlock in this MachineFunction.
287 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
288 MBBNumbering.push_back(MBB);
289 return MBBNumbering.size()-1;
292 /// removeFromMBBNumbering - Remove the specific machine basic block from our
293 /// tracker, this is only really to be used by the MachineBasicBlock
295 void removeFromMBBNumbering(unsigned N) {
296 assert(N < MBBNumbering.size() && "Illegal basic block #");
301 //===--------------------------------------------------------------------===//
302 // GraphTraits specializations for function basic block graphs (CFGs)
303 //===--------------------------------------------------------------------===//
305 // Provide specializations of GraphTraits to be able to treat a
306 // machine function as a graph of machine basic blocks... these are
307 // the same as the machine basic block iterators, except that the root
308 // node is implicitly the first node of the function.
310 template <> struct GraphTraits<MachineFunction*> :
311 public GraphTraits<MachineBasicBlock*> {
312 static NodeType *getEntryNode(MachineFunction *F) {
316 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
317 typedef MachineFunction::iterator nodes_iterator;
318 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
319 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
321 template <> struct GraphTraits<const MachineFunction*> :
322 public GraphTraits<const MachineBasicBlock*> {
323 static NodeType *getEntryNode(const MachineFunction *F) {
327 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
328 typedef MachineFunction::const_iterator nodes_iterator;
329 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
330 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
334 // Provide specializations of GraphTraits to be able to treat a function as a
335 // graph of basic blocks... and to walk it in inverse order. Inverse order for
336 // a function is considered to be when traversing the predecessor edges of a BB
337 // instead of the successor edges.
339 template <> struct GraphTraits<Inverse<MachineFunction*> > :
340 public GraphTraits<Inverse<MachineBasicBlock*> > {
341 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
342 return &G.Graph->front();
345 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
346 public GraphTraits<Inverse<const MachineBasicBlock*> > {
347 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
348 return &G.Graph->front();
352 } // End llvm namespace