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/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/Support/Annotation.h"
24 #include "llvm/ADT/BitVector.h"
31 class MachineFrameInfo;
32 class MachineConstantPool;
33 class MachineJumpTableInfo;
37 struct ilist_traits<MachineBasicBlock> {
38 // this is only set by the MachineFunction owning the ilist
39 friend class MachineFunction;
40 MachineFunction* Parent;
43 ilist_traits<MachineBasicBlock>() : Parent(0) { }
45 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
46 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
48 static const MachineBasicBlock*
49 getPrev(const MachineBasicBlock* N) { return N->Prev; }
51 static const MachineBasicBlock*
52 getNext(const MachineBasicBlock* N) { return N->Next; }
54 static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
57 static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
61 static MachineBasicBlock* createSentinel();
62 static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; }
63 void addNodeToList(MachineBasicBlock* N);
64 void removeNodeFromList(MachineBasicBlock* N);
65 void transferNodesFromList(iplist<MachineBasicBlock,
66 ilist_traits<MachineBasicBlock> > &toList,
67 ilist_iterator<MachineBasicBlock> first,
68 ilist_iterator<MachineBasicBlock> last);
71 /// MachineFunctionInfo - This class can be derived from and used by targets to
72 /// hold private target-specific information for each MachineFunction. Objects
73 /// of type are accessed/created with MF::getInfo and destroyed when the
74 /// MachineFunction is destroyed.
75 struct MachineFunctionInfo {
76 virtual ~MachineFunctionInfo() {};
79 class MachineFunction : private Annotation {
81 const TargetMachine &Target;
83 // List of machine basic blocks in function
84 ilist<MachineBasicBlock> BasicBlocks;
86 // Keeping track of mapping from SSA values to registers
87 SSARegMap *SSARegMapping;
89 // Used to keep track of target-specific per-machine function information for
90 // the target implementation.
91 MachineFunctionInfo *MFInfo;
93 // Keep track of objects allocated on the stack.
94 MachineFrameInfo *FrameInfo;
96 // Keep track of constants which are spilled to memory
97 MachineConstantPool *ConstantPool;
99 // Keep track of jump tables for switch instructions
100 MachineJumpTableInfo *JumpTableInfo;
102 // Function-level unique numbering for MachineBasicBlocks. When a
103 // MachineBasicBlock is inserted into a MachineFunction is it automatically
104 // numbered and this vector keeps track of the mapping from ID's to MBB's.
105 std::vector<MachineBasicBlock*> MBBNumbering;
107 /// UsedPhysRegs - This is a bit vector that is computed and set by the
108 /// register allocator, and must be kept up to date by passes that run after
109 /// register allocation (though most don't modify this). This is used
110 /// so that the code generator knows which callee save registers to save and
111 /// for other target specific uses.
112 BitVector UsedPhysRegs;
114 /// LiveIns/LiveOuts - Keep track of the physical registers that are
115 /// livein/liveout of the function. Live in values are typically arguments in
116 /// registers, live out values are typically return values in registers.
117 /// LiveIn values are allowed to have virtual registers associated with them,
118 /// stored in the second element.
119 std::vector<std::pair<unsigned, unsigned> > LiveIns;
120 std::vector<unsigned> LiveOuts;
123 MachineFunction(const Function *Fn, const TargetMachine &TM);
126 /// getFunction - Return the LLVM function that this machine code represents
128 const Function *getFunction() const { return Fn; }
130 /// getTarget - Return the target machine this machine code is compiled with
132 const TargetMachine &getTarget() const { return Target; }
134 /// SSARegMap Interface... Keep track of information about each SSA virtual
135 /// register, such as which register class it belongs to.
137 SSARegMap *getSSARegMap() const { return SSARegMapping; }
138 void clearSSARegMap();
140 /// getFrameInfo - Return the frame info object for the current function.
141 /// This object contains information about objects allocated on the stack
142 /// frame of the current function in an abstract way.
144 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
146 /// getJumpTableInfo - Return the jump table info object for the current
147 /// function. This object contains information about jump tables for switch
148 /// instructions in the current function.
150 MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
152 /// getConstantPool - Return the constant pool object for the current
155 MachineConstantPool *getConstantPool() const { return ConstantPool; }
157 /// MachineFunctionInfo - Keep track of various per-function pieces of
158 /// information for backends that would like to do so.
160 template<typename Ty>
162 if (!MFInfo) MFInfo = new Ty(*this);
164 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
165 "Invalid concrete type or multiple inheritence for getInfo");
166 return static_cast<Ty*>(MFInfo);
169 template<typename Ty>
170 const Ty *getInfo() const {
171 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
174 /// isPhysRegUsed - Return true if the specified register is used in this
175 /// function. This only works after register allocation.
176 bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
178 /// setPhysRegUsed - Mark the specified register used in this function.
179 /// This should only be called during and after register allocation.
180 void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; }
182 /// setPhysRegUnused - Mark the specified register unused in this function.
183 /// This should only be called during and after register allocation.
184 void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; }
186 // LiveIn/LiveOut management methods.
188 /// addLiveIn/Out - Add the specified register as a live in/out. Note that it
189 /// is an error to add the same register to the same set more than once.
190 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
191 LiveIns.push_back(std::make_pair(Reg, vreg));
193 void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
195 // Iteration support for live in/out sets. These sets are kept in sorted
196 // order by their register number.
197 typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
199 typedef std::vector<unsigned>::const_iterator liveout_iterator;
200 livein_iterator livein_begin() const { return LiveIns.begin(); }
201 livein_iterator livein_end() const { return LiveIns.end(); }
202 bool livein_empty() const { return LiveIns.empty(); }
203 liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
204 liveout_iterator liveout_end() const { return LiveOuts.end(); }
205 bool liveout_empty() const { return LiveOuts.empty(); }
207 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
208 /// are inserted into the machine function. The block number for a machine
209 /// basic block can be found by using the MBB::getBlockNumber method, this
210 /// method provides the inverse mapping.
212 MachineBasicBlock *getBlockNumbered(unsigned N) {
213 assert(N < MBBNumbering.size() && "Illegal block number");
214 assert(MBBNumbering[N] && "Block was removed from the machine function!");
215 return MBBNumbering[N];
218 /// getNumBlockIDs - Return the number of MBB ID's allocated.
220 unsigned getNumBlockIDs() const { return MBBNumbering.size(); }
222 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
223 /// recomputes them. This guarantees that the MBB numbers are sequential,
224 /// dense, and match the ordering of the blocks within the function. If a
225 /// specific MachineBasicBlock is specified, only that block and those after
226 /// it are renumbered.
227 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
229 /// print - Print out the MachineFunction in a format suitable for debugging
230 /// to the specified stream.
232 void print(std::ostream &OS) const;
233 void print(std::ostream *OS) const { if (OS) print(*OS); }
235 /// viewCFG - This function is meant for use from the debugger. You can just
236 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
237 /// program, displaying the CFG of the current function with the code for each
238 /// basic block inside. This depends on there being a 'dot' and 'gv' program
241 void viewCFG() const;
243 /// viewCFGOnly - This function is meant for use from the debugger. It works
244 /// just like viewCFG, but it does not include the contents of basic blocks
245 /// into the nodes, just the label. If you are only interested in the CFG
246 /// this can make the graph smaller.
248 void viewCFGOnly() const;
250 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
254 /// construct - Allocate and initialize a MachineFunction for a given Function
257 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
259 /// destruct - Destroy the MachineFunction corresponding to a given Function
261 static void destruct(const Function *F);
263 /// get - Return a handle to a MachineFunction corresponding to the given
264 /// Function. This should not be called before "construct()" for a given
267 static MachineFunction& get(const Function *F);
269 // Provide accessors for the MachineBasicBlock list...
270 typedef ilist<MachineBasicBlock> BasicBlockListType;
271 typedef BasicBlockListType::iterator iterator;
272 typedef BasicBlockListType::const_iterator const_iterator;
273 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
274 typedef std::reverse_iterator<iterator> reverse_iterator;
276 // Provide accessors for basic blocks...
277 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
278 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
280 //===--------------------------------------------------------------------===//
281 // BasicBlock iterator forwarding functions
283 iterator begin() { return BasicBlocks.begin(); }
284 const_iterator begin() const { return BasicBlocks.begin(); }
285 iterator end () { return BasicBlocks.end(); }
286 const_iterator end () const { return BasicBlocks.end(); }
288 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
289 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
290 reverse_iterator rend () { return BasicBlocks.rend(); }
291 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
293 unsigned size() const { return BasicBlocks.size(); }
294 bool empty() const { return BasicBlocks.empty(); }
295 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
296 MachineBasicBlock &front() { return BasicBlocks.front(); }
297 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
298 MachineBasicBlock & back() { return BasicBlocks.back(); }
300 //===--------------------------------------------------------------------===//
301 // Internal functions used to automatically number MachineBasicBlocks
304 /// getNextMBBNumber - Returns the next unique number to be assigned
305 /// to a MachineBasicBlock in this MachineFunction.
307 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
308 MBBNumbering.push_back(MBB);
309 return MBBNumbering.size()-1;
312 /// removeFromMBBNumbering - Remove the specific machine basic block from our
313 /// tracker, this is only really to be used by the MachineBasicBlock
315 void removeFromMBBNumbering(unsigned N) {
316 assert(N < MBBNumbering.size() && "Illegal basic block #");
321 //===--------------------------------------------------------------------===//
322 // GraphTraits specializations for function basic block graphs (CFGs)
323 //===--------------------------------------------------------------------===//
325 // Provide specializations of GraphTraits to be able to treat a
326 // machine function as a graph of machine basic blocks... these are
327 // the same as the machine basic block iterators, except that the root
328 // node is implicitly the first node of the function.
330 template <> struct GraphTraits<MachineFunction*> :
331 public GraphTraits<MachineBasicBlock*> {
332 static NodeType *getEntryNode(MachineFunction *F) {
336 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
337 typedef MachineFunction::iterator nodes_iterator;
338 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
339 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
341 template <> struct GraphTraits<const MachineFunction*> :
342 public GraphTraits<const MachineBasicBlock*> {
343 static NodeType *getEntryNode(const MachineFunction *F) {
347 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
348 typedef MachineFunction::const_iterator nodes_iterator;
349 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
350 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
354 // Provide specializations of GraphTraits to be able to treat a function as a
355 // graph of basic blocks... and to walk it in inverse order. Inverse order for
356 // a function is considered to be when traversing the predecessor edges of a BB
357 // instead of the successor edges.
359 template <> struct GraphTraits<Inverse<MachineFunction*> > :
360 public GraphTraits<Inverse<MachineBasicBlock*> > {
361 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
362 return &G.Graph->front();
365 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
366 public GraphTraits<Inverse<const MachineBasicBlock*> > {
367 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
368 return &G.Graph->front();
372 } // End llvm namespace