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;
39 class TargetRegisterClass;
42 struct ilist_traits<MachineBasicBlock>
43 : public ilist_default_traits<MachineBasicBlock> {
44 mutable ilist_half_node<MachineBasicBlock> Sentinel;
46 MachineBasicBlock *createSentinel() const {
47 return static_cast<MachineBasicBlock*>(&Sentinel);
49 void destroySentinel(MachineBasicBlock *) const {}
51 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
52 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
53 return createSentinel();
55 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
57 void addNodeToList(MachineBasicBlock* MBB);
58 void removeNodeFromList(MachineBasicBlock* MBB);
59 void deleteNode(MachineBasicBlock *MBB);
61 void createNode(const MachineBasicBlock &);
64 /// MachineFunctionInfo - This class can be derived from and used by targets to
65 /// hold private target-specific information for each MachineFunction. Objects
66 /// of type are accessed/created with MF::getInfo and destroyed when the
67 /// MachineFunction is destroyed.
68 struct MachineFunctionInfo {
69 virtual ~MachineFunctionInfo();
72 class MachineFunction {
74 const TargetMachine &Target;
77 // RegInfo - Information about each register in use in the function.
78 MachineRegisterInfo *RegInfo;
80 // Used to keep track of target-specific per-machine function information for
81 // the target implementation.
82 MachineFunctionInfo *MFInfo;
84 // Keep track of objects allocated on the stack.
85 MachineFrameInfo *FrameInfo;
87 // Keep track of constants which are spilled to memory
88 MachineConstantPool *ConstantPool;
90 // Keep track of jump tables for switch instructions
91 MachineJumpTableInfo *JumpTableInfo;
93 // Function-level unique numbering for MachineBasicBlocks. When a
94 // MachineBasicBlock is inserted into a MachineFunction is it automatically
95 // numbered and this vector keeps track of the mapping from ID's to MBB's.
96 std::vector<MachineBasicBlock*> MBBNumbering;
98 // Pool-allocate MachineFunction-lifetime and IR objects.
99 BumpPtrAllocator Allocator;
101 // Allocation management for instructions in function.
102 Recycler<MachineInstr> InstructionRecycler;
104 // Allocation management for basic blocks in function.
105 Recycler<MachineBasicBlock> BasicBlockRecycler;
107 // List of machine basic blocks in function
108 typedef ilist<MachineBasicBlock> BasicBlockListType;
109 BasicBlockListType BasicBlocks;
111 // Default debug location. Used to print out the debug label at the beginning
113 DebugLoc DefaultDebugLoc;
115 // Tracks debug locations.
116 DebugLocTracker DebugLocInfo;
118 /// FunctionNumber - This provides a unique ID for each function emitted in
119 /// this translation unit.
121 unsigned FunctionNumber;
123 // The alignment of the function.
126 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
127 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
130 MachineFunction(Function *Fn, const TargetMachine &TM, unsigned FunctionNum,
134 MCContext &getContext() const { return Ctx; }
136 /// getFunction - Return the LLVM function that this machine code represents
138 Function *getFunction() const { return Fn; }
140 /// getFunctionNumber - Return a unique ID for the current function.
142 unsigned getFunctionNumber() const { return FunctionNumber; }
144 /// getTarget - Return the target machine this machine code is compiled with
146 const TargetMachine &getTarget() const { return Target; }
148 /// getRegInfo - Return information about the registers currently in use.
150 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
151 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
153 /// getFrameInfo - Return the frame info object for the current function.
154 /// This object contains information about objects allocated on the stack
155 /// frame of the current function in an abstract way.
157 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
158 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
160 /// getJumpTableInfo - Return the jump table info object for the current
161 /// function. This object contains information about jump tables in the
162 /// current function. If the current function has no jump tables, this will
164 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
165 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
167 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
168 /// does already exist, allocate one.
169 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
172 /// getConstantPool - Return the constant pool object for the current
175 MachineConstantPool *getConstantPool() { return ConstantPool; }
176 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
178 /// getAlignment - Return the alignment (log2, not bytes) of the function.
180 unsigned getAlignment() const { return Alignment; }
182 /// setAlignment - Set the alignment (log2, not bytes) of the function.
184 void setAlignment(unsigned A) { Alignment = A; }
186 /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
187 void EnsureAlignment(unsigned A) {
188 if (Alignment < A) Alignment = A;
191 /// getInfo - Keep track of various per-function pieces of information for
192 /// backends that would like to do so.
194 template<typename Ty>
197 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
198 // that apparently breaks GCC 3.3.
199 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
200 AlignOf<Ty>::Alignment));
201 MFInfo = new (Loc) Ty(*this);
203 return static_cast<Ty*>(MFInfo);
206 template<typename Ty>
207 const Ty *getInfo() const {
208 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
211 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
212 /// are inserted into the machine function. The block number for a machine
213 /// basic block can be found by using the MBB::getBlockNumber method, this
214 /// method provides the inverse mapping.
216 MachineBasicBlock *getBlockNumbered(unsigned N) const {
217 assert(N < MBBNumbering.size() && "Illegal block number");
218 assert(MBBNumbering[N] && "Block was removed from the machine function!");
219 return MBBNumbering[N];
222 /// getNumBlockIDs - Return the number of MBB ID's allocated.
224 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
226 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
227 /// recomputes them. This guarantees that the MBB numbers are sequential,
228 /// dense, and match the ordering of the blocks within the function. If a
229 /// specific MachineBasicBlock is specified, only that block and those after
230 /// it are renumbered.
231 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
233 /// print - Print out the MachineFunction in a format suitable for debugging
234 /// to the specified stream.
236 void print(raw_ostream &OS) const;
238 /// viewCFG - This function is meant for use from the debugger. You can just
239 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
240 /// program, displaying the CFG of the current function with the code for each
241 /// basic block inside. This depends on there being a 'dot' and 'gv' program
244 void viewCFG() const;
246 /// viewCFGOnly - This function is meant for use from the debugger. It works
247 /// just like viewCFG, but it does not include the contents of basic blocks
248 /// into the nodes, just the label. If you are only interested in the CFG
249 /// this can make the graph smaller.
251 void viewCFGOnly() const;
253 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
257 /// verify - Run the current MachineFunction through the machine code
258 /// verifier, useful for debugger use.
259 void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
261 // Provide accessors for the MachineBasicBlock list...
262 typedef BasicBlockListType::iterator iterator;
263 typedef BasicBlockListType::const_iterator const_iterator;
264 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
265 typedef std::reverse_iterator<iterator> reverse_iterator;
267 /// addLiveIn - Add the specified physical register as a live-in value and
268 /// create a corresponding virtual register for it.
269 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
271 //===--------------------------------------------------------------------===//
272 // BasicBlock accessor functions.
274 iterator begin() { return BasicBlocks.begin(); }
275 const_iterator begin() const { return BasicBlocks.begin(); }
276 iterator end () { return BasicBlocks.end(); }
277 const_iterator end () const { return BasicBlocks.end(); }
279 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
280 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
281 reverse_iterator rend () { return BasicBlocks.rend(); }
282 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
284 unsigned size() const { return (unsigned)BasicBlocks.size();}
285 bool empty() const { return BasicBlocks.empty(); }
286 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
287 MachineBasicBlock &front() { return BasicBlocks.front(); }
288 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
289 MachineBasicBlock & back() { return BasicBlocks.back(); }
291 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
292 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
293 void insert(iterator MBBI, MachineBasicBlock *MBB) {
294 BasicBlocks.insert(MBBI, MBB);
296 void splice(iterator InsertPt, iterator MBBI) {
297 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
299 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
300 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
303 void remove(iterator MBBI) {
304 BasicBlocks.remove(MBBI);
306 void erase(iterator MBBI) {
307 BasicBlocks.erase(MBBI);
310 //===--------------------------------------------------------------------===//
311 // Internal functions used to automatically number MachineBasicBlocks
314 /// getNextMBBNumber - Returns the next unique number to be assigned
315 /// to a MachineBasicBlock in this MachineFunction.
317 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
318 MBBNumbering.push_back(MBB);
319 return (unsigned)MBBNumbering.size()-1;
322 /// removeFromMBBNumbering - Remove the specific machine basic block from our
323 /// tracker, this is only really to be used by the MachineBasicBlock
325 void removeFromMBBNumbering(unsigned N) {
326 assert(N < MBBNumbering.size() && "Illegal basic block #");
330 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
331 /// of `new MachineInstr'.
333 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
337 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
338 /// 'Orig' instruction, identical in all ways except the instruction
339 /// has no parent, prev, or next.
341 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
343 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
345 /// DeleteMachineInstr - Delete the given MachineInstr.
347 void DeleteMachineInstr(MachineInstr *MI);
349 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
350 /// instead of `new MachineBasicBlock'.
352 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
354 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
356 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
358 /// getMachineMemOperand - Allocate a new MachineMemOperand.
359 /// MachineMemOperands are owned by the MachineFunction and need not be
360 /// explicitly deallocated.
361 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
362 int64_t o, uint64_t s,
363 unsigned base_alignment);
365 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
366 /// an existing one, adjusting by an offset and using the given size.
367 /// MachineMemOperands are owned by the MachineFunction and need not be
368 /// explicitly deallocated.
369 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
370 int64_t Offset, uint64_t Size);
372 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
373 /// pointers. This array is owned by the MachineFunction.
374 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
376 /// extractLoadMemRefs - Allocate an array and populate it with just the
377 /// load information from the given MachineMemOperand sequence.
378 std::pair<MachineInstr::mmo_iterator,
379 MachineInstr::mmo_iterator>
380 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
381 MachineInstr::mmo_iterator End);
383 /// extractStoreMemRefs - Allocate an array and populate it with just the
384 /// store information from the given MachineMemOperand sequence.
385 std::pair<MachineInstr::mmo_iterator,
386 MachineInstr::mmo_iterator>
387 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
388 MachineInstr::mmo_iterator End);
390 //===--------------------------------------------------------------------===//
391 // Label Manipulation.
394 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
395 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
396 /// normal 'L' label is returned.
397 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
398 bool isLinkerPrivate = false) const;
401 //===--------------------------------------------------------------------===//
405 /// getDILocation - Get the DILocation for a given DebugLoc object.
406 DILocation getDILocation(DebugLoc DL) const;
408 /// getDefaultDebugLoc - Get the default debug location for the machine
410 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
412 /// setDefaultDebugLoc - Get the default debug location for the machine
414 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
416 /// getDebugLocInfo - Get the debug info location tracker.
417 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
420 //===--------------------------------------------------------------------===//
421 // GraphTraits specializations for function basic block graphs (CFGs)
422 //===--------------------------------------------------------------------===//
424 // Provide specializations of GraphTraits to be able to treat a
425 // machine function as a graph of machine basic blocks... these are
426 // the same as the machine basic block iterators, except that the root
427 // node is implicitly the first node of the function.
429 template <> struct GraphTraits<MachineFunction*> :
430 public GraphTraits<MachineBasicBlock*> {
431 static NodeType *getEntryNode(MachineFunction *F) {
435 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
436 typedef MachineFunction::iterator nodes_iterator;
437 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
438 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
440 template <> struct GraphTraits<const MachineFunction*> :
441 public GraphTraits<const MachineBasicBlock*> {
442 static NodeType *getEntryNode(const MachineFunction *F) {
446 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
447 typedef MachineFunction::const_iterator nodes_iterator;
448 static nodes_iterator nodes_begin(const MachineFunction *F) {
451 static nodes_iterator nodes_end (const MachineFunction *F) {
457 // Provide specializations of GraphTraits to be able to treat a function as a
458 // graph of basic blocks... and to walk it in inverse order. Inverse order for
459 // a function is considered to be when traversing the predecessor edges of a BB
460 // instead of the successor edges.
462 template <> struct GraphTraits<Inverse<MachineFunction*> > :
463 public GraphTraits<Inverse<MachineBasicBlock*> > {
464 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
465 return &G.Graph->front();
468 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
469 public GraphTraits<Inverse<const MachineBasicBlock*> > {
470 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
471 return &G.Graph->front();
475 } // End llvm namespace