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/MachineDebugInfo.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/Support/Annotation.h"
30 class MachineFrameInfo;
31 class MachineConstantPool;
35 struct ilist_traits<MachineBasicBlock> {
36 // this is only set by the MachineFunction owning the ilist
37 friend class MachineFunction;
38 MachineFunction* Parent;
41 ilist_traits<MachineBasicBlock>() : Parent(0) { }
43 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
44 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
46 static const MachineBasicBlock*
47 getPrev(const MachineBasicBlock* N) { return N->Prev; }
49 static const MachineBasicBlock*
50 getNext(const MachineBasicBlock* N) { return N->Next; }
52 static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) {
55 static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) {
59 static MachineBasicBlock* createSentinel();
60 static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; }
61 void addNodeToList(MachineBasicBlock* N);
62 void removeNodeFromList(MachineBasicBlock* N);
63 void transferNodesFromList(iplist<MachineBasicBlock,
64 ilist_traits<MachineBasicBlock> > &toList,
65 ilist_iterator<MachineBasicBlock> first,
66 ilist_iterator<MachineBasicBlock> last);
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() {};
77 class MachineFunction : private Annotation {
79 const TargetMachine &Target;
81 // List of machine basic blocks in function
82 ilist<MachineBasicBlock> BasicBlocks;
84 // Keeping track of mapping from SSA values to registers
85 SSARegMap *SSARegMapping;
87 // Used to keep track of target-specific per-machine function information for
88 // the target implementation.
89 MachineFunctionInfo *MFInfo;
91 // Keep track of objects allocated on the stack.
92 MachineFrameInfo *FrameInfo;
94 // Keep track of constants which are spilled to memory
95 MachineConstantPool *ConstantPool;
97 // Function-level unique numbering for MachineBasicBlocks. When a
98 // MachineBasicBlock is inserted into a MachineFunction is it automatically
99 // numbered and this vector keeps track of the mapping from ID's to MBB's.
100 std::vector<MachineBasicBlock*> MBBNumbering;
102 /// UsedPhysRegs - This is a new[]'d array of bools that is computed and set
103 /// by the register allocator, and must be kept up to date by passes that run
104 /// after register allocation (though most don't modify this). This is used
105 /// so that the code generator knows which callee save registers to save and
106 /// for other target specific uses.
109 /// LiveIns/LiveOuts - Keep track of the physical registers that are
110 /// livein/liveout of the function. Live in values are typically arguments in
111 /// registers, live out values are typically return values in registers.
112 /// LiveIn values are allowed to have virtual registers associated with them,
113 /// stored in the second element.
114 std::vector<std::pair<unsigned, unsigned> > LiveIns;
115 std::vector<unsigned> LiveOuts;
118 MachineFunction(const Function *Fn, const TargetMachine &TM);
121 /// getFunction - Return the LLVM function that this machine code represents
123 const Function *getFunction() const { return Fn; }
125 /// getTarget - Return the target machine this machine code is compiled with
127 const TargetMachine &getTarget() const { return Target; }
129 /// SSARegMap Interface... Keep track of information about each SSA virtual
130 /// register, such as which register class it belongs to.
132 SSARegMap *getSSARegMap() const { return SSARegMapping; }
133 void clearSSARegMap();
135 /// getFrameInfo - Return the frame info object for the current function.
136 /// This object contains information about objects allocated on the stack
137 /// frame of the current function in an abstract way.
139 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
141 /// getConstantPool - Return the constant pool object for the current
144 MachineConstantPool *getConstantPool() const { return ConstantPool; }
146 /// MachineFunctionInfo - Keep track of various per-function pieces of
147 /// information for backends that would like to do so.
149 template<typename Ty>
151 if (!MFInfo) MFInfo = new Ty(*this);
153 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
154 "Invalid concrete type or multiple inheritence for getInfo");
155 return static_cast<Ty*>(MFInfo);
158 /// setUsedPhysRegs - The register allocator should call this to initialized
159 /// the UsedPhysRegs set. This should be passed a new[]'d array with entries
160 /// for all of the physical registers that the target supports. Each array
161 /// entry should be set to true iff the physical register is used within the
163 void setUsedPhysRegs(bool *UPR) { UsedPhysRegs = UPR; }
165 /// getUsedPhysregs - This returns the UsedPhysRegs array. This returns null
166 /// before register allocation.
167 bool *getUsedPhysregs() { return UsedPhysRegs; }
168 const bool *getUsedPhysregs() const { return UsedPhysRegs; }
170 /// isPhysRegUsed - Return true if the specified register is used in this
171 /// function. This only works after register allocation.
172 bool isPhysRegUsed(unsigned Reg) { return UsedPhysRegs[Reg]; }
174 /// changePhyRegUsed - This method allows code that runs after register
175 /// allocation to keep the PhysRegsUsed array up-to-date.
176 void changePhyRegUsed(unsigned Reg, bool State) { UsedPhysRegs[Reg] = State; }
179 // LiveIn/LiveOut management methods.
181 /// addLiveIn/Out - Add the specified register as a live in/out. Note that it
182 /// is an error to add the same register to the same set more than once.
183 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
184 LiveIns.push_back(std::make_pair(Reg, vreg));
186 void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
188 // Iteration support for live in/out sets. These sets are kept in sorted
189 // order by their register number.
190 typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
192 typedef std::vector<unsigned>::const_iterator liveout_iterator;
193 livein_iterator livein_begin() const { return LiveIns.begin(); }
194 livein_iterator livein_end() const { return LiveIns.end(); }
195 liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
196 liveout_iterator liveout_end() const { return LiveOuts.end(); }
198 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
199 /// are inserted into the machine function. The block number for a machine
200 /// basic block can be found by using the MBB::getBlockNumber method, this
201 /// method provides the inverse mapping.
203 MachineBasicBlock *getBlockNumbered(unsigned N) {
204 assert(N < MBBNumbering.size() && "Illegal block number");
205 assert(MBBNumbering[N] && "Block was removed from the machine function!");
206 return MBBNumbering[N];
209 /// getLastBlock - Returns the MachineBasicBlock with the greatest number
210 MachineBasicBlock *getLastBlock() {
211 return MBBNumbering.back();
213 const MachineBasicBlock *getLastBlock() const {
214 return MBBNumbering.back();
217 /// print - Print out the MachineFunction in a format suitable for debugging
218 /// to the specified stream.
220 void print(std::ostream &OS) const;
222 /// viewCFG - This function is meant for use from the debugger. You can just
223 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
224 /// program, displaying the CFG of the current function with the code for each
225 /// basic block inside. This depends on there being a 'dot' and 'gv' program
228 void viewCFG() const;
230 /// viewCFGOnly - This function is meant for use from the debugger. It works
231 /// just like viewCFG, but it does not include the contents of basic blocks
232 /// into the nodes, just the label. If you are only interested in the CFG
233 /// this can make the graph smaller.
235 void viewCFGOnly() const;
237 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
241 /// construct - Allocate and initialize a MachineFunction for a given Function
244 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
246 /// destruct - Destroy the MachineFunction corresponding to a given Function
248 static void destruct(const Function *F);
250 /// get - Return a handle to a MachineFunction corresponding to the given
251 /// Function. This should not be called before "construct()" for a given
254 static MachineFunction& get(const Function *F);
256 // Provide accessors for the MachineBasicBlock list...
257 typedef ilist<MachineBasicBlock> BasicBlockListType;
258 typedef BasicBlockListType::iterator iterator;
259 typedef BasicBlockListType::const_iterator const_iterator;
260 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
261 typedef std::reverse_iterator<iterator> reverse_iterator;
263 // Provide accessors for basic blocks...
264 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
265 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
267 //===--------------------------------------------------------------------===//
268 // BasicBlock iterator forwarding functions
270 iterator begin() { return BasicBlocks.begin(); }
271 const_iterator begin() const { return BasicBlocks.begin(); }
272 iterator end () { return BasicBlocks.end(); }
273 const_iterator end () const { return BasicBlocks.end(); }
275 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
276 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
277 reverse_iterator rend () { return BasicBlocks.rend(); }
278 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
280 unsigned size() const { return BasicBlocks.size(); }
281 bool empty() const { return BasicBlocks.empty(); }
282 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
283 MachineBasicBlock &front() { return BasicBlocks.front(); }
284 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
285 MachineBasicBlock & back() { return BasicBlocks.back(); }
287 //===--------------------------------------------------------------------===//
288 // Internal functions used to automatically number MachineBasicBlocks
291 /// getNextMBBNumber - Returns the next unique number to be assigned
292 /// to a MachineBasicBlock in this MachineFunction.
294 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
295 MBBNumbering.push_back(MBB);
296 return MBBNumbering.size()-1;
299 /// removeFromMBBNumbering - Remove the specific machine basic block from our
300 /// tracker, this is only really to be used by the MachineBasicBlock
302 void removeFromMBBNumbering(unsigned N) {
303 assert(N < MBBNumbering.size() && "Illegal basic block #");
308 //===--------------------------------------------------------------------===//
309 // GraphTraits specializations for function basic block graphs (CFGs)
310 //===--------------------------------------------------------------------===//
312 // Provide specializations of GraphTraits to be able to treat a
313 // machine function as a graph of machine basic blocks... these are
314 // the same as the machine basic block iterators, except that the root
315 // node is implicitly the first node of the function.
317 template <> struct GraphTraits<MachineFunction*> :
318 public GraphTraits<MachineBasicBlock*> {
319 static NodeType *getEntryNode(MachineFunction *F) {
323 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
324 typedef MachineFunction::iterator nodes_iterator;
325 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
326 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
328 template <> struct GraphTraits<const MachineFunction*> :
329 public GraphTraits<const MachineBasicBlock*> {
330 static NodeType *getEntryNode(const MachineFunction *F) {
334 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
335 typedef MachineFunction::const_iterator nodes_iterator;
336 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
337 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
341 // Provide specializations of GraphTraits to be able to treat a function as a
342 // graph of basic blocks... and to walk it in inverse order. Inverse order for
343 // a function is considered to be when traversing the predecessor edges of a BB
344 // instead of the successor edges.
346 template <> struct GraphTraits<Inverse<MachineFunction*> > :
347 public GraphTraits<Inverse<MachineBasicBlock*> > {
348 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
349 return &G.Graph->front();
352 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
353 public GraphTraits<Inverse<const MachineBasicBlock*> > {
354 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
355 return &G.Graph->front();
359 } // End llvm namespace