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/CodeGen/MachineBasicBlock.h"
22 #include "llvm/ADT/ilist.h"
23 #include "llvm/Support/DebugLoc.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Recycler.h"
32 class MachineRegisterInfo;
33 class MachineFrameInfo;
34 class MachineConstantPool;
35 class MachineJumpTableInfo;
37 class TargetRegisterClass;
40 struct ilist_traits<MachineBasicBlock>
41 : public ilist_default_traits<MachineBasicBlock> {
42 mutable ilist_half_node<MachineBasicBlock> Sentinel;
44 MachineBasicBlock *createSentinel() const {
45 return static_cast<MachineBasicBlock*>(&Sentinel);
47 void destroySentinel(MachineBasicBlock *) const {}
49 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
50 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
51 return createSentinel();
53 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
55 void addNodeToList(MachineBasicBlock* MBB);
56 void removeNodeFromList(MachineBasicBlock* MBB);
57 void deleteNode(MachineBasicBlock *MBB);
59 void createNode(const MachineBasicBlock &);
62 /// MachineFunctionInfo - This class can be derived from and used by targets to
63 /// hold private target-specific information for each MachineFunction. Objects
64 /// of type are accessed/created with MF::getInfo and destroyed when the
65 /// MachineFunction is destroyed.
66 struct MachineFunctionInfo {
67 virtual ~MachineFunctionInfo();
70 class MachineFunction {
72 const TargetMachine &Target;
74 // RegInfo - Information about each register in use in the function.
75 MachineRegisterInfo *RegInfo;
77 // Used to keep track of target-specific per-machine function information for
78 // the target implementation.
79 MachineFunctionInfo *MFInfo;
81 // Keep track of objects allocated on the stack.
82 MachineFrameInfo *FrameInfo;
84 // Keep track of constants which are spilled to memory
85 MachineConstantPool *ConstantPool;
87 // Keep track of jump tables for switch instructions
88 MachineJumpTableInfo *JumpTableInfo;
90 // Function-level unique numbering for MachineBasicBlocks. When a
91 // MachineBasicBlock is inserted into a MachineFunction is it automatically
92 // numbered and this vector keeps track of the mapping from ID's to MBB's.
93 std::vector<MachineBasicBlock*> MBBNumbering;
95 // Pool-allocate MachineFunction-lifetime and IR objects.
96 BumpPtrAllocator Allocator;
98 // Allocation management for instructions in function.
99 Recycler<MachineInstr> InstructionRecycler;
101 // Allocation management for basic blocks in function.
102 Recycler<MachineBasicBlock> BasicBlockRecycler;
104 // List of machine basic blocks in function
105 typedef ilist<MachineBasicBlock> BasicBlockListType;
106 BasicBlockListType BasicBlocks;
108 // Default debug location. Used to print out the debug label at the beginning
110 DebugLoc DefaultDebugLoc;
112 // Tracks debug locations.
113 DebugLocTracker DebugLocInfo;
115 // The alignment of the function.
118 MachineFunction(const MachineFunction &); // intentionally unimplemented
119 void operator=(const MachineFunction&); // intentionally unimplemented
122 MachineFunction(Function *Fn, const TargetMachine &TM);
125 /// getFunction - Return the LLVM function that this machine code represents
127 Function *getFunction() const { return Fn; }
129 /// getTarget - Return the target machine this machine code is compiled with
131 const TargetMachine &getTarget() const { return Target; }
133 /// getRegInfo - Return information about the registers currently in use.
135 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
136 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
138 /// getFrameInfo - Return the frame info object for the current function.
139 /// This object contains information about objects allocated on the stack
140 /// frame of the current function in an abstract way.
142 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
143 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
145 /// getJumpTableInfo - Return the jump table info object for the current
146 /// function. This object contains information about jump tables for switch
147 /// instructions in the current function.
149 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
150 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
152 /// getConstantPool - Return the constant pool object for the current
155 MachineConstantPool *getConstantPool() { return ConstantPool; }
156 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
158 /// getAlignment - Return the alignment (log2, not bytes) of the function.
160 unsigned getAlignment() const { return Alignment; }
162 /// setAlignment - Set the alignment (log2, not bytes) of the function.
164 void setAlignment(unsigned A) { Alignment = A; }
166 /// getInfo - Keep track of various per-function pieces of information for
167 /// backends that would like to do so.
169 template<typename Ty>
172 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
173 // that apparently breaks GCC 3.3.
174 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
175 AlignOf<Ty>::Alignment));
176 MFInfo = new (Loc) Ty(*this);
179 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo &&
180 "Invalid concrete type or multiple inheritence for getInfo");
181 return static_cast<Ty*>(MFInfo);
184 template<typename Ty>
185 const Ty *getInfo() const {
186 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
189 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
190 /// are inserted into the machine function. The block number for a machine
191 /// basic block can be found by using the MBB::getBlockNumber method, this
192 /// method provides the inverse mapping.
194 MachineBasicBlock *getBlockNumbered(unsigned N) const {
195 assert(N < MBBNumbering.size() && "Illegal block number");
196 assert(MBBNumbering[N] && "Block was removed from the machine function!");
197 return MBBNumbering[N];
200 /// getNumBlockIDs - Return the number of MBB ID's allocated.
202 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
204 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
205 /// recomputes them. This guarantees that the MBB numbers are sequential,
206 /// dense, and match the ordering of the blocks within the function. If a
207 /// specific MachineBasicBlock is specified, only that block and those after
208 /// it are renumbered.
209 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
211 /// print - Print out the MachineFunction in a format suitable for debugging
212 /// to the specified stream.
214 void print(raw_ostream &OS) const;
216 /// viewCFG - This function is meant for use from the debugger. You can just
217 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
218 /// program, displaying the CFG of the current function with the code for each
219 /// basic block inside. This depends on there being a 'dot' and 'gv' program
222 void viewCFG() const;
224 /// viewCFGOnly - This function is meant for use from the debugger. It works
225 /// just like viewCFG, but it does not include the contents of basic blocks
226 /// into the nodes, just the label. If you are only interested in the CFG
227 /// this can make the graph smaller.
229 void viewCFGOnly() const;
231 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
235 // Provide accessors for the MachineBasicBlock list...
236 typedef BasicBlockListType::iterator iterator;
237 typedef BasicBlockListType::const_iterator const_iterator;
238 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
239 typedef std::reverse_iterator<iterator> reverse_iterator;
241 /// addLiveIn - Add the specified physical register as a live-in value and
242 /// create a corresponding virtual register for it.
243 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
245 //===--------------------------------------------------------------------===//
246 // BasicBlock accessor functions.
248 iterator begin() { return BasicBlocks.begin(); }
249 const_iterator begin() const { return BasicBlocks.begin(); }
250 iterator end () { return BasicBlocks.end(); }
251 const_iterator end () const { return BasicBlocks.end(); }
253 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
254 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
255 reverse_iterator rend () { return BasicBlocks.rend(); }
256 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
258 unsigned size() const { return (unsigned)BasicBlocks.size();}
259 bool empty() const { return BasicBlocks.empty(); }
260 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
261 MachineBasicBlock &front() { return BasicBlocks.front(); }
262 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
263 MachineBasicBlock & back() { return BasicBlocks.back(); }
265 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
266 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
267 void insert(iterator MBBI, MachineBasicBlock *MBB) {
268 BasicBlocks.insert(MBBI, MBB);
270 void splice(iterator InsertPt, iterator MBBI) {
271 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
273 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
274 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
277 void remove(iterator MBBI) {
278 BasicBlocks.remove(MBBI);
280 void erase(iterator MBBI) {
281 BasicBlocks.erase(MBBI);
284 //===--------------------------------------------------------------------===//
285 // Internal functions used to automatically number MachineBasicBlocks
288 /// getNextMBBNumber - Returns the next unique number to be assigned
289 /// to a MachineBasicBlock in this MachineFunction.
291 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
292 MBBNumbering.push_back(MBB);
293 return (unsigned)MBBNumbering.size()-1;
296 /// removeFromMBBNumbering - Remove the specific machine basic block from our
297 /// tracker, this is only really to be used by the MachineBasicBlock
299 void removeFromMBBNumbering(unsigned N) {
300 assert(N < MBBNumbering.size() && "Illegal basic block #");
304 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
305 /// of `new MachineInstr'.
307 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
311 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
312 /// 'Orig' instruction, identical in all ways except the the instruction
313 /// has no parent, prev, or next.
315 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
317 /// DeleteMachineInstr - Delete the given MachineInstr.
319 void DeleteMachineInstr(MachineInstr *MI);
321 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
322 /// instead of `new MachineBasicBlock'.
324 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
326 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
328 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
330 /// getMachineMemOperand - Allocate a new MachineMemOperand.
331 /// MachineMemOperands are owned by the MachineFunction and need not be
332 /// explicitly deallocated.
333 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
334 int64_t o, uint64_t s,
335 unsigned base_alignment);
337 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
338 /// an existing one, adjusting by an offset and using the given size.
339 /// MachineMemOperands are owned by the MachineFunction and need not be
340 /// explicitly deallocated.
341 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
342 int64_t Offset, uint64_t Size);
344 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
345 /// pointers. This array is owned by the MachineFunction.
346 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
348 /// extractLoadMemRefs - Allocate an array and populate it with just the
349 /// load information from the given MachineMemOperand sequence.
350 std::pair<MachineInstr::mmo_iterator,
351 MachineInstr::mmo_iterator>
352 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
353 MachineInstr::mmo_iterator End);
355 /// extractStoreMemRefs - Allocate an array and populate it with just the
356 /// store information from the given MachineMemOperand sequence.
357 std::pair<MachineInstr::mmo_iterator,
358 MachineInstr::mmo_iterator>
359 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
360 MachineInstr::mmo_iterator End);
362 //===--------------------------------------------------------------------===//
366 /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
367 DebugLocTuple getDebugLocTuple(DebugLoc DL) const;
369 /// getDefaultDebugLoc - Get the default debug location for the machine
371 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
373 /// setDefaultDebugLoc - Get the default debug location for the machine
375 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
377 /// getDebugLocInfo - Get the debug info location tracker.
378 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
381 //===--------------------------------------------------------------------===//
382 // GraphTraits specializations for function basic block graphs (CFGs)
383 //===--------------------------------------------------------------------===//
385 // Provide specializations of GraphTraits to be able to treat a
386 // machine function as a graph of machine basic blocks... these are
387 // the same as the machine basic block iterators, except that the root
388 // node is implicitly the first node of the function.
390 template <> struct GraphTraits<MachineFunction*> :
391 public GraphTraits<MachineBasicBlock*> {
392 static NodeType *getEntryNode(MachineFunction *F) {
396 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
397 typedef MachineFunction::iterator nodes_iterator;
398 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
399 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
401 template <> struct GraphTraits<const MachineFunction*> :
402 public GraphTraits<const MachineBasicBlock*> {
403 static NodeType *getEntryNode(const MachineFunction *F) {
407 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
408 typedef MachineFunction::const_iterator nodes_iterator;
409 static nodes_iterator nodes_begin(const MachineFunction *F) {
412 static nodes_iterator nodes_end (const MachineFunction *F) {
418 // Provide specializations of GraphTraits to be able to treat a function as a
419 // graph of basic blocks... and to walk it in inverse order. Inverse order for
420 // a function is considered to be when traversing the predecessor edges of a BB
421 // instead of the successor edges.
423 template <> struct GraphTraits<Inverse<MachineFunction*> > :
424 public GraphTraits<Inverse<MachineBasicBlock*> > {
425 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
426 return &G.Graph->front();
429 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
430 public GraphTraits<Inverse<const MachineBasicBlock*> > {
431 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
432 return &G.Graph->front();
436 } // End llvm namespace