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/Allocator.h"
25 #include "llvm/Support/Recycler.h"
30 class MachineRegisterInfo;
31 class MachineFrameInfo;
32 class MachineConstantPool;
33 class MachineJumpTableInfo;
35 class TargetRegisterClass;
38 struct ilist_traits<MachineBasicBlock>
39 : public ilist_default_traits<MachineBasicBlock> {
40 mutable ilist_node<MachineBasicBlock> Sentinel;
42 MachineBasicBlock *createSentinel() const {
43 return static_cast<MachineBasicBlock*>(&Sentinel);
45 void destroySentinel(MachineBasicBlock *) const {}
47 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
48 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
49 return createSentinel();
51 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
53 void addNodeToList(MachineBasicBlock* MBB);
54 void removeNodeFromList(MachineBasicBlock* MBB);
55 void deleteNode(MachineBasicBlock *MBB);
57 void createNode(const MachineBasicBlock &);
60 /// MachineFunctionInfo - This class can be derived from and used by targets to
61 /// hold private target-specific information for each MachineFunction. Objects
62 /// of type are accessed/created with MF::getInfo and destroyed when the
63 /// MachineFunction is destroyed.
64 struct MachineFunctionInfo {
65 virtual ~MachineFunctionInfo() {}
68 class MachineFunction {
70 const TargetMachine &Target;
72 // RegInfo - Information about each register in use in the function.
73 MachineRegisterInfo *RegInfo;
75 // Used to keep track of target-specific per-machine function information for
76 // the target implementation.
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 // Keep track of jump tables for switch instructions
86 MachineJumpTableInfo *JumpTableInfo;
88 // Function-level unique numbering for MachineBasicBlocks. When a
89 // MachineBasicBlock is inserted into a MachineFunction is it automatically
90 // numbered and this vector keeps track of the mapping from ID's to MBB's.
91 std::vector<MachineBasicBlock*> MBBNumbering;
93 // Pool-allocate MachineFunction-lifetime and IR objects.
94 BumpPtrAllocator Allocator;
96 // Allocation management for instructions in function.
97 Recycler<MachineInstr> InstructionRecycler;
99 // Allocation management for basic blocks in function.
100 Recycler<MachineBasicBlock> BasicBlockRecycler;
102 // List of machine basic blocks in function
103 typedef ilist<MachineBasicBlock> BasicBlockListType;
104 BasicBlockListType BasicBlocks;
106 // Default debug location. Used to print out the debug label at the beginning
108 DebugLoc DefaultDebugLoc;
110 // Tracks debug locations.
111 DebugLocTracker DebugLocInfo;
113 // The alignment of the function.
117 MachineFunction(Function *Fn, const TargetMachine &TM);
120 /// getFunction - Return the LLVM function that this machine code represents
122 Function *getFunction() const { return Fn; }
124 /// getTarget - Return the target machine this machine code is compiled with
126 const TargetMachine &getTarget() const { return Target; }
128 /// getRegInfo - Return information about the registers currently in use.
130 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
131 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
133 /// getFrameInfo - Return the frame info object for the current function.
134 /// This object contains information about objects allocated on the stack
135 /// frame of the current function in an abstract way.
137 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
138 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
140 /// getJumpTableInfo - Return the jump table info object for the current
141 /// function. This object contains information about jump tables for switch
142 /// instructions in the current function.
144 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
145 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
147 /// getConstantPool - Return the constant pool object for the current
150 MachineConstantPool *getConstantPool() { return ConstantPool; }
151 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
153 /// getAlignment - Return the alignment (log2, not bytes) of the function.
155 unsigned getAlignment() const { return Alignment; }
157 /// setAlignment - Set the alignment (log2, not bytes) of the function.
159 void setAlignment(unsigned A) { Alignment = A; }
161 /// MachineFunctionInfo - Keep track of various per-function pieces of
162 /// information for backends that would like to do so.
164 template<typename Ty>
167 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
168 // that apparently breaks GCC 3.3.
169 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
170 AlignOf<Ty>::Alignment));
171 MFInfo = new (Loc) Ty(*this);
174 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
175 "Invalid concrete type or multiple inheritence for getInfo");
176 return static_cast<Ty*>(MFInfo);
179 template<typename Ty>
180 const Ty *getInfo() const {
181 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
184 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
185 /// are inserted into the machine function. The block number for a machine
186 /// basic block can be found by using the MBB::getBlockNumber method, this
187 /// method provides the inverse mapping.
189 MachineBasicBlock *getBlockNumbered(unsigned N) const {
190 assert(N < MBBNumbering.size() && "Illegal block number");
191 assert(MBBNumbering[N] && "Block was removed from the machine function!");
192 return MBBNumbering[N];
195 /// getNumBlockIDs - Return the number of MBB ID's allocated.
197 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
199 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
200 /// recomputes them. This guarantees that the MBB numbers are sequential,
201 /// dense, and match the ordering of the blocks within the function. If a
202 /// specific MachineBasicBlock is specified, only that block and those after
203 /// it are renumbered.
204 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
206 /// print - Print out the MachineFunction in a format suitable for debugging
207 /// to the specified stream.
209 void print(std::ostream &OS) const;
210 void print(std::ostream *OS) const { if (OS) print(*OS); }
212 /// viewCFG - This function is meant for use from the debugger. You can just
213 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
214 /// program, displaying the CFG of the current function with the code for each
215 /// basic block inside. This depends on there being a 'dot' and 'gv' program
218 void viewCFG() const;
220 /// viewCFGOnly - This function is meant for use from the debugger. It works
221 /// just like viewCFG, but it does not include the contents of basic blocks
222 /// into the nodes, just the label. If you are only interested in the CFG
223 /// this can make the graph smaller.
225 void viewCFGOnly() const;
227 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
231 // Provide accessors for the MachineBasicBlock list...
232 typedef BasicBlockListType::iterator iterator;
233 typedef BasicBlockListType::const_iterator const_iterator;
234 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
235 typedef std::reverse_iterator<iterator> reverse_iterator;
237 /// addLiveIn - Add the specified physical register as a live-in value and
238 /// create a corresponding virtual register for it.
239 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
241 //===--------------------------------------------------------------------===//
242 // BasicBlock accessor functions.
244 iterator begin() { return BasicBlocks.begin(); }
245 const_iterator begin() const { return BasicBlocks.begin(); }
246 iterator end () { return BasicBlocks.end(); }
247 const_iterator end () const { return BasicBlocks.end(); }
249 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
250 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
251 reverse_iterator rend () { return BasicBlocks.rend(); }
252 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
254 unsigned size() const { return (unsigned)BasicBlocks.size();}
255 bool empty() const { return BasicBlocks.empty(); }
256 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
257 MachineBasicBlock &front() { return BasicBlocks.front(); }
258 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
259 MachineBasicBlock & back() { return BasicBlocks.back(); }
261 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
262 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
263 void insert(iterator MBBI, MachineBasicBlock *MBB) {
264 BasicBlocks.insert(MBBI, MBB);
266 void splice(iterator InsertPt, iterator MBBI) {
267 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
270 void remove(iterator MBBI) {
271 BasicBlocks.remove(MBBI);
273 void erase(iterator MBBI) {
274 BasicBlocks.erase(MBBI);
277 //===--------------------------------------------------------------------===//
278 // Internal functions used to automatically number MachineBasicBlocks
281 /// getNextMBBNumber - Returns the next unique number to be assigned
282 /// to a MachineBasicBlock in this MachineFunction.
284 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
285 MBBNumbering.push_back(MBB);
286 return (unsigned)MBBNumbering.size()-1;
289 /// removeFromMBBNumbering - Remove the specific machine basic block from our
290 /// tracker, this is only really to be used by the MachineBasicBlock
292 void removeFromMBBNumbering(unsigned N) {
293 assert(N < MBBNumbering.size() && "Illegal basic block #");
297 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
298 /// of `new MachineInstr'.
300 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
304 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
305 /// 'Orig' instruction, identical in all ways except the the instruction
306 /// has no parent, prev, or next.
308 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
310 /// DeleteMachineInstr - Delete the given MachineInstr.
312 void DeleteMachineInstr(MachineInstr *MI);
314 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
315 /// instead of `new MachineBasicBlock'.
317 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
319 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
321 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
323 //===--------------------------------------------------------------------===//
327 /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
328 /// source file, line, and column. If none currently exists, create a new
329 /// DebugLocTuple, and insert it into the DebugIdMap.
330 unsigned getOrCreateDebugLocID(GlobalVariable *CompileUnit,
331 unsigned Line, unsigned Col);
333 /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
334 DebugLocTuple getDebugLocTuple(DebugLoc DL) const;
336 /// getDefaultDebugLoc - Get the default debug location for the machine
338 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
340 /// setDefaultDebugLoc - Get the default debug location for the machine
342 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
344 /// getDebugLocInfo - Get the debug info location tracker.
345 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
348 //===--------------------------------------------------------------------===//
349 // GraphTraits specializations for function basic block graphs (CFGs)
350 //===--------------------------------------------------------------------===//
352 // Provide specializations of GraphTraits to be able to treat a
353 // machine function as a graph of machine basic blocks... these are
354 // the same as the machine basic block iterators, except that the root
355 // node is implicitly the first node of the function.
357 template <> struct GraphTraits<MachineFunction*> :
358 public GraphTraits<MachineBasicBlock*> {
359 static NodeType *getEntryNode(MachineFunction *F) {
363 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
364 typedef MachineFunction::iterator nodes_iterator;
365 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
366 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
368 template <> struct GraphTraits<const MachineFunction*> :
369 public GraphTraits<const MachineBasicBlock*> {
370 static NodeType *getEntryNode(const MachineFunction *F) {
374 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
375 typedef MachineFunction::const_iterator nodes_iterator;
376 static nodes_iterator nodes_begin(const MachineFunction *F) {
379 static nodes_iterator nodes_end (const MachineFunction *F) {
385 // Provide specializations of GraphTraits to be able to treat a function as a
386 // graph of basic blocks... and to walk it in inverse order. Inverse order for
387 // a function is considered to be when traversing the predecessor edges of a BB
388 // instead of the successor edges.
390 template <> struct GraphTraits<Inverse<MachineFunction*> > :
391 public GraphTraits<Inverse<MachineBasicBlock*> > {
392 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
393 return &G.Graph->front();
396 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
397 public GraphTraits<Inverse<const MachineBasicBlock*> > {
398 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
399 return &G.Graph->front();
403 } // End llvm namespace