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 "Support/Annotation.h"
28 class ilist_traits<MachineBasicBlock> {
29 // this is only set by the MachineFunction owning the ilist
30 friend class MachineFunction;
31 MachineFunction* Parent;
34 ilist_traits<MachineBasicBlock>() : Parent(0) { }
36 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
37 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
39 static const MachineBasicBlock*
40 getPrev(const MachineBasicBlock* N) { return N->Prev; }
42 static const MachineBasicBlock*
43 getNext(const MachineBasicBlock* N) { return N->Next; }
45 static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { N->Prev = prev; }
46 static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { N->Next = next; }
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);
62 class MachineFunctionInfo;
63 class MachineFrameInfo;
64 class MachineConstantPool;
66 class MachineFunction : private Annotation {
68 const TargetMachine &Target;
70 // List of machine basic blocks in function
71 ilist<MachineBasicBlock> BasicBlocks;
73 // Keeping track of mapping from SSA values to registers
74 SSARegMap *SSARegMapping;
76 // Used to keep track of frame and constant area information for sparc be
77 MachineFunctionInfo *MFInfo;
79 // Keep track of objects allocated on the stack.
80 MachineFrameInfo *FrameInfo;
82 // Keep track of constants which are spilled to memory
83 MachineConstantPool *ConstantPool;
85 // Function-level unique numbering for MachineBasicBlocks. When a
86 // MachineBasicBlock is inserted into a MachineFunction is it automatically
87 // numbered and this vector keeps track of the mapping from ID's to MBB's.
88 std::vector<MachineBasicBlock*> MBBNumbering;
91 MachineFunction(const Function *Fn, const TargetMachine &TM);
94 /// getFunction - Return the LLVM function that this machine code represents
96 const Function *getFunction() const { return Fn; }
98 /// getTarget - Return the target machine this machine code is compiled with
100 const TargetMachine &getTarget() const { return Target; }
102 /// SSARegMap Interface... Keep track of information about each SSA virtual
103 /// register, such as which register class it belongs to.
105 SSARegMap *getSSARegMap() const { return SSARegMapping; }
106 void clearSSARegMap();
108 /// getFrameInfo - Return the frame info object for the current function.
109 /// This object contains information about objects allocated on the stack
110 /// frame of the current function in an abstract way.
112 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
114 /// getConstantPool - Return the constant pool object for the current
116 MachineConstantPool *getConstantPool() const { return ConstantPool; }
118 /// MachineFunctionInfo - Keep track of various per-function pieces of
119 /// information for the sparc backend.
121 MachineFunctionInfo *getInfo() const { return MFInfo; }
123 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
124 /// are inserted into the machine function. The block number for a machine
125 /// basic block can be found by using the MBB::getBlockNumber method, this
126 /// method provides the inverse mapping.
127 MachineBasicBlock *getBlockNumbered(unsigned N) {
128 assert(N < MBBNumbering.size() && "Illegal block number");
129 assert(MBBNumbering[N] && "Block was removed from the machine function!");
130 return MBBNumbering[N];
133 /// print - Print out the MachineFunction in a format suitable for debugging
134 /// to the specified stream.
136 void print(std::ostream &OS) const;
138 /// viewCFG - This function is meant for use from the debugger. You can just
139 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
140 /// program, displaying the CFG of the current function with the code for each
141 /// basic block inside. This depends on there being a 'dot' and 'gv' program
144 void viewCFG() const;
146 /// viewCFGOnly - This function is meant for use from the debugger. It works
147 /// just like viewCFG, but it does not include the contents of basic blocks
148 /// into the nodes, just the label. If you are only interested in the CFG
149 /// this can make the graph smaller.
151 void viewCFGOnly() const;
153 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
157 /// construct - Allocate and initialize a MachineFunction for a given Function
160 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
162 /// destruct - Destroy the MachineFunction corresponding to a given Function
164 static void destruct(const Function *F);
166 /// get - Return a handle to a MachineFunction corresponding to the given
167 /// Function. This should not be called before "construct()" for a given
170 static MachineFunction& get(const Function *F);
172 // Provide accessors for the MachineBasicBlock list...
173 typedef ilist<MachineBasicBlock> BasicBlockListType;
174 typedef BasicBlockListType::iterator iterator;
175 typedef BasicBlockListType::const_iterator const_iterator;
176 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
177 typedef std::reverse_iterator<iterator> reverse_iterator;
179 // Provide accessors for basic blocks...
180 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
181 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
183 //===--------------------------------------------------------------------===//
184 // BasicBlock iterator forwarding functions
186 iterator begin() { return BasicBlocks.begin(); }
187 const_iterator begin() const { return BasicBlocks.begin(); }
188 iterator end () { return BasicBlocks.end(); }
189 const_iterator end () const { return BasicBlocks.end(); }
191 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
192 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
193 reverse_iterator rend () { return BasicBlocks.rend(); }
194 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
196 unsigned size() const { return BasicBlocks.size(); }
197 bool empty() const { return BasicBlocks.empty(); }
198 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
199 MachineBasicBlock &front() { return BasicBlocks.front(); }
200 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
201 MachineBasicBlock & back() { return BasicBlocks.back(); }
203 //===--------------------------------------------------------------------===//
204 // Internal functions used to automatically number MachineBasicBlocks
207 /// getNextMBBNumber - Returns the next unique number to be assigned
208 /// to a MachineBasicBlock in this MachineFunction.
210 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
211 MBBNumbering.push_back(MBB);
212 return MBBNumbering.size()-1;
215 /// removeFromMBBNumbering - Remove the specific machine basic block from our
216 /// tracker, this is only really to be used by the MachineBasicBlock
218 void removeFromMBBNumbering(unsigned N) {
219 assert(N < MBBNumbering.size() && "Illegal basic block #");
224 //===--------------------------------------------------------------------===//
225 // GraphTraits specializations for function basic block graphs (CFGs)
226 //===--------------------------------------------------------------------===//
228 // Provide specializations of GraphTraits to be able to treat a
229 // machine function as a graph of machine basic blocks... these are
230 // the same as the machine basic block iterators, except that the root
231 // node is implicitly the first node of the function.
233 template <> struct GraphTraits<MachineFunction*> :
234 public GraphTraits<MachineBasicBlock*> {
235 static NodeType *getEntryNode(MachineFunction *F) {
239 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
240 typedef MachineFunction::iterator nodes_iterator;
241 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
242 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
244 template <> struct GraphTraits<const MachineFunction*> :
245 public GraphTraits<const MachineBasicBlock*> {
246 static NodeType *getEntryNode(const MachineFunction *F) {
250 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
251 typedef MachineFunction::const_iterator nodes_iterator;
252 static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); }
253 static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); }
257 // Provide specializations of GraphTraits to be able to treat a function as a
258 // graph of basic blocks... and to walk it in inverse order. Inverse order for
259 // a function is considered to be when traversing the predecessor edges of a BB
260 // instead of the successor edges.
262 template <> struct GraphTraits<Inverse<MachineFunction*> > :
263 public GraphTraits<Inverse<MachineBasicBlock*> > {
264 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
265 return &G.Graph->front();
268 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
269 public GraphTraits<Inverse<const MachineBasicBlock*> > {
270 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
271 return &G.Graph->front();
275 } // End llvm namespace