1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/ilist.h"
22 #include "llvm/Support/DebugLoc.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/Support/Annotation.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/Recycler.h"
31 class MachineRegisterInfo;
32 class MachineFrameInfo;
33 class MachineConstantPool;
34 class MachineJumpTableInfo;
36 class TargetRegisterClass;
39 struct ilist_traits<MachineBasicBlock>
40 : public ilist_default_traits<MachineBasicBlock> {
41 mutable ilist_node<MachineBasicBlock> Sentinel;
43 MachineBasicBlock *createSentinel() const {
44 return static_cast<MachineBasicBlock*>(&Sentinel);
46 void destroySentinel(MachineBasicBlock *) const {}
48 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
49 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
50 return createSentinel();
52 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
54 void addNodeToList(MachineBasicBlock* MBB);
55 void removeNodeFromList(MachineBasicBlock* MBB);
56 void deleteNode(MachineBasicBlock *MBB);
58 void createNode(const MachineBasicBlock &);
61 /// MachineFunctionInfo - This class can be derived from and used by targets to
62 /// hold private target-specific information for each MachineFunction. Objects
63 /// of type are accessed/created with MF::getInfo and destroyed when the
64 /// MachineFunction is destroyed.
65 struct MachineFunctionInfo {
66 virtual ~MachineFunctionInfo() {}
69 class MachineFunction : private Annotation {
71 const TargetMachine &Target;
73 // RegInfo - Information about each register in use in the function.
74 MachineRegisterInfo *RegInfo;
76 // Used to keep track of target-specific per-machine function information for
77 // the target implementation.
78 MachineFunctionInfo *MFInfo;
80 // Keep track of objects allocated on the stack.
81 MachineFrameInfo *FrameInfo;
83 // Keep track of constants which are spilled to memory
84 MachineConstantPool *ConstantPool;
86 // Keep track of jump tables for switch instructions
87 MachineJumpTableInfo *JumpTableInfo;
89 // Function-level unique numbering for MachineBasicBlocks. When a
90 // MachineBasicBlock is inserted into a MachineFunction is it automatically
91 // numbered and this vector keeps track of the mapping from ID's to MBB's.
92 std::vector<MachineBasicBlock*> MBBNumbering;
94 // Pool-allocate MachineFunction-lifetime and IR objects.
95 BumpPtrAllocator Allocator;
97 // Allocation management for instructions in function.
98 Recycler<MachineInstr> InstructionRecycler;
100 // Allocation management for basic blocks in function.
101 Recycler<MachineBasicBlock> BasicBlockRecycler;
103 // List of machine basic blocks in function
104 typedef ilist<MachineBasicBlock> BasicBlockListType;
105 BasicBlockListType BasicBlocks;
107 // Default debug location. Used to print out the debug label at the beginning
109 DebugLoc DefaultDebugLoc;
111 // Tracks debug locations.
112 DebugLocTracker DebugLocInfo;
114 // The alignment of the function.
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 /// getRegInfo - Return information about the registers currently in use.
131 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
132 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
134 /// getFrameInfo - Return the frame info object for the current function.
135 /// This object contains information about objects allocated on the stack
136 /// frame of the current function in an abstract way.
138 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
139 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
141 /// getJumpTableInfo - Return the jump table info object for the current
142 /// function. This object contains information about jump tables for switch
143 /// instructions in the current function.
145 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
146 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
148 /// getConstantPool - Return the constant pool object for the current
151 MachineConstantPool *getConstantPool() { return ConstantPool; }
152 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
154 /// getAlignment - Return the alignment (log2, not bytes) of the function.
156 unsigned getAlignment() const { return Alignment; }
158 /// setAlignment - Set the alignment (log2, not bytes) of the function.
160 void setAlignment(unsigned A) { Alignment = A; }
162 /// MachineFunctionInfo - Keep track of various per-function pieces of
163 /// information for backends that would like to do so.
165 template<typename Ty>
168 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
169 // that apparently breaks GCC 3.3.
170 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
171 AlignOf<Ty>::Alignment));
172 MFInfo = new (Loc) Ty(*this);
175 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
176 "Invalid concrete type or multiple inheritence for getInfo");
177 return static_cast<Ty*>(MFInfo);
180 template<typename Ty>
181 const Ty *getInfo() const {
182 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
185 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
186 /// are inserted into the machine function. The block number for a machine
187 /// basic block can be found by using the MBB::getBlockNumber method, this
188 /// method provides the inverse mapping.
190 MachineBasicBlock *getBlockNumbered(unsigned N) const {
191 assert(N < MBBNumbering.size() && "Illegal block number");
192 assert(MBBNumbering[N] && "Block was removed from the machine function!");
193 return MBBNumbering[N];
196 /// getNumBlockIDs - Return the number of MBB ID's allocated.
198 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
200 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
201 /// recomputes them. This guarantees that the MBB numbers are sequential,
202 /// dense, and match the ordering of the blocks within the function. If a
203 /// specific MachineBasicBlock is specified, only that block and those after
204 /// it are renumbered.
205 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
207 /// print - Print out the MachineFunction in a format suitable for debugging
208 /// to the specified stream.
210 void print(std::ostream &OS) const;
211 void print(std::ostream *OS) const { if (OS) print(*OS); }
213 /// viewCFG - This function is meant for use from the debugger. You can just
214 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
215 /// program, displaying the CFG of the current function with the code for each
216 /// basic block inside. This depends on there being a 'dot' and 'gv' program
219 void viewCFG() const;
221 /// viewCFGOnly - This function is meant for use from the debugger. It works
222 /// just like viewCFG, but it does not include the contents of basic blocks
223 /// into the nodes, just the label. If you are only interested in the CFG
224 /// this can make the graph smaller.
226 void viewCFGOnly() const;
228 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
232 /// construct - Allocate and initialize a MachineFunction for a given Function
235 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
237 /// destruct - Destroy the MachineFunction corresponding to a given Function
239 static void destruct(const Function *F);
241 /// get - Return a handle to a MachineFunction corresponding to the given
242 /// Function. This should not be called before "construct()" for a given
245 static MachineFunction& get(const Function *F);
247 // Provide accessors for the MachineBasicBlock list...
248 typedef BasicBlockListType::iterator iterator;
249 typedef BasicBlockListType::const_iterator const_iterator;
250 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
251 typedef std::reverse_iterator<iterator> reverse_iterator;
253 /// addLiveIn - Add the specified physical register as a live-in value and
254 /// create a corresponding virtual register for it.
255 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
257 //===--------------------------------------------------------------------===//
258 // BasicBlock accessor functions.
260 iterator begin() { return BasicBlocks.begin(); }
261 const_iterator begin() const { return BasicBlocks.begin(); }
262 iterator end () { return BasicBlocks.end(); }
263 const_iterator end () const { return BasicBlocks.end(); }
265 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
266 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
267 reverse_iterator rend () { return BasicBlocks.rend(); }
268 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
270 unsigned size() const { return (unsigned)BasicBlocks.size();}
271 bool empty() const { return BasicBlocks.empty(); }
272 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
273 MachineBasicBlock &front() { return BasicBlocks.front(); }
274 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
275 MachineBasicBlock & back() { return BasicBlocks.back(); }
277 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
278 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
279 void insert(iterator MBBI, MachineBasicBlock *MBB) {
280 BasicBlocks.insert(MBBI, MBB);
282 void splice(iterator InsertPt, iterator MBBI) {
283 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
286 void remove(iterator MBBI) {
287 BasicBlocks.remove(MBBI);
289 void erase(iterator MBBI) {
290 BasicBlocks.erase(MBBI);
293 //===--------------------------------------------------------------------===//
294 // Internal functions used to automatically number MachineBasicBlocks
297 /// getNextMBBNumber - Returns the next unique number to be assigned
298 /// to a MachineBasicBlock in this MachineFunction.
300 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
301 MBBNumbering.push_back(MBB);
302 return (unsigned)MBBNumbering.size()-1;
305 /// removeFromMBBNumbering - Remove the specific machine basic block from our
306 /// tracker, this is only really to be used by the MachineBasicBlock
308 void removeFromMBBNumbering(unsigned N) {
309 assert(N < MBBNumbering.size() && "Illegal basic block #");
313 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
314 /// of `new MachineInstr'.
316 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
320 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
321 /// 'Orig' instruction, identical in all ways except the the instruction
322 /// has no parent, prev, or next.
324 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
326 /// DeleteMachineInstr - Delete the given MachineInstr.
328 void DeleteMachineInstr(MachineInstr *MI);
330 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
331 /// instead of `new MachineBasicBlock'.
333 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
335 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
337 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
339 //===--------------------------------------------------------------------===//
343 /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
344 /// source file, line, and column. If none currently exists, create a new
345 /// DebugLocTuple, and insert it into the DebugIdMap.
346 unsigned getOrCreateDebugLocID(GlobalVariable *CompileUnit,
347 unsigned Line, unsigned Col);
349 /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
350 DebugLocTuple getDebugLocTuple(DebugLoc DL) const;
352 /// getDefaultDebugLoc - Get the default debug location for the machine
354 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
356 /// setDefaultDebugLoc - Get the default debug location for the machine
358 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
360 /// getDebugLocInfo - Get the debug info location tracker.
361 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
364 //===--------------------------------------------------------------------===//
365 // GraphTraits specializations for function basic block graphs (CFGs)
366 //===--------------------------------------------------------------------===//
368 // Provide specializations of GraphTraits to be able to treat a
369 // machine function as a graph of machine basic blocks... these are
370 // the same as the machine basic block iterators, except that the root
371 // node is implicitly the first node of the function.
373 template <> struct GraphTraits<MachineFunction*> :
374 public GraphTraits<MachineBasicBlock*> {
375 static NodeType *getEntryNode(MachineFunction *F) {
379 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
380 typedef MachineFunction::iterator nodes_iterator;
381 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
382 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
384 template <> struct GraphTraits<const MachineFunction*> :
385 public GraphTraits<const MachineBasicBlock*> {
386 static NodeType *getEntryNode(const MachineFunction *F) {
390 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
391 typedef MachineFunction::const_iterator nodes_iterator;
392 static nodes_iterator nodes_begin(const MachineFunction *F) {
395 static nodes_iterator nodes_end (const MachineFunction *F) {
401 // Provide specializations of GraphTraits to be able to treat a function as a
402 // graph of basic blocks... and to walk it in inverse order. Inverse order for
403 // a function is considered to be when traversing the predecessor edges of a BB
404 // instead of the successor edges.
406 template <> struct GraphTraits<Inverse<MachineFunction*> > :
407 public GraphTraits<Inverse<MachineBasicBlock*> > {
408 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
409 return &G.Graph->front();
412 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
413 public GraphTraits<Inverse<const MachineBasicBlock*> > {
414 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
415 return &G.Graph->front();
419 } // End llvm namespace