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;
38 class TargetRegisterClass;
41 struct ilist_traits<MachineBasicBlock>
42 : public ilist_default_traits<MachineBasicBlock> {
43 mutable ilist_half_node<MachineBasicBlock> Sentinel;
45 MachineBasicBlock *createSentinel() const {
46 return static_cast<MachineBasicBlock*>(&Sentinel);
48 void destroySentinel(MachineBasicBlock *) const {}
50 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
51 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
52 return createSentinel();
54 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
56 void addNodeToList(MachineBasicBlock* MBB);
57 void removeNodeFromList(MachineBasicBlock* MBB);
58 void deleteNode(MachineBasicBlock *MBB);
60 void createNode(const MachineBasicBlock &);
63 /// MachineFunctionInfo - This class can be derived from and used by targets to
64 /// hold private target-specific information for each MachineFunction. Objects
65 /// of type are accessed/created with MF::getInfo and destroyed when the
66 /// MachineFunction is destroyed.
67 struct MachineFunctionInfo {
68 virtual ~MachineFunctionInfo();
71 class MachineFunction {
73 const TargetMachine &Target;
76 // RegInfo - Information about each register in use in the function.
77 MachineRegisterInfo *RegInfo;
79 // Used to keep track of target-specific per-machine function information for
80 // the target implementation.
81 MachineFunctionInfo *MFInfo;
83 // Keep track of objects allocated on the stack.
84 MachineFrameInfo *FrameInfo;
86 // Keep track of constants which are spilled to memory
87 MachineConstantPool *ConstantPool;
89 // Keep track of jump tables for switch instructions
90 MachineJumpTableInfo *JumpTableInfo;
92 // Function-level unique numbering for MachineBasicBlocks. When a
93 // MachineBasicBlock is inserted into a MachineFunction is it automatically
94 // numbered and this vector keeps track of the mapping from ID's to MBB's.
95 std::vector<MachineBasicBlock*> MBBNumbering;
97 // Pool-allocate MachineFunction-lifetime and IR objects.
98 BumpPtrAllocator Allocator;
100 // Allocation management for instructions in function.
101 Recycler<MachineInstr> InstructionRecycler;
103 // Allocation management for basic blocks in function.
104 Recycler<MachineBasicBlock> BasicBlockRecycler;
106 // List of machine basic blocks in function
107 typedef ilist<MachineBasicBlock> BasicBlockListType;
108 BasicBlockListType BasicBlocks;
110 // Default debug location. Used to print out the debug label at the beginning
112 DebugLoc DefaultDebugLoc;
114 // Tracks debug locations.
115 DebugLocTracker DebugLocInfo;
117 /// FunctionNumber - This provides a unique ID for each function emitted in
118 /// this translation unit.
120 unsigned FunctionNumber;
122 // The alignment of the function.
125 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
126 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
129 MachineFunction(Function *Fn, const TargetMachine &TM, unsigned FunctionNum,
133 MCContext &getContext() const { return Ctx; }
135 /// getFunction - Return the LLVM function that this machine code represents
137 Function *getFunction() const { return Fn; }
139 /// getFunctionNumber - Return a unique ID for the current function.
141 unsigned getFunctionNumber() const { return FunctionNumber; }
143 /// getTarget - Return the target machine this machine code is compiled with
145 const TargetMachine &getTarget() const { return Target; }
147 /// getRegInfo - Return information about the registers currently in use.
149 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
150 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
152 /// getFrameInfo - Return the frame info object for the current function.
153 /// This object contains information about objects allocated on the stack
154 /// frame of the current function in an abstract way.
156 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
157 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
159 /// getJumpTableInfo - Return the jump table info object for the current
160 /// function. This object contains information about jump tables in the
161 /// current function. If the current function has no jump tables, this will
163 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
164 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
166 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
167 /// does already exist, allocate one.
168 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
171 /// getConstantPool - Return the constant pool object for the current
174 MachineConstantPool *getConstantPool() { return ConstantPool; }
175 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
177 /// getAlignment - Return the alignment (log2, not bytes) of the function.
179 unsigned getAlignment() const { return Alignment; }
181 /// setAlignment - Set the alignment (log2, not bytes) of the function.
183 void setAlignment(unsigned A) { Alignment = A; }
185 /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
186 void EnsureAlignment(unsigned A) {
187 if (Alignment < A) Alignment = A;
190 /// getInfo - Keep track of various per-function pieces of information for
191 /// backends that would like to do so.
193 template<typename Ty>
196 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
197 // that apparently breaks GCC 3.3.
198 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
199 AlignOf<Ty>::Alignment));
200 MFInfo = new (Loc) Ty(*this);
202 return static_cast<Ty*>(MFInfo);
205 template<typename Ty>
206 const Ty *getInfo() const {
207 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
210 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
211 /// are inserted into the machine function. The block number for a machine
212 /// basic block can be found by using the MBB::getBlockNumber method, this
213 /// method provides the inverse mapping.
215 MachineBasicBlock *getBlockNumbered(unsigned N) const {
216 assert(N < MBBNumbering.size() && "Illegal block number");
217 assert(MBBNumbering[N] && "Block was removed from the machine function!");
218 return MBBNumbering[N];
221 /// getNumBlockIDs - Return the number of MBB ID's allocated.
223 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
225 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
226 /// recomputes them. This guarantees that the MBB numbers are sequential,
227 /// dense, and match the ordering of the blocks within the function. If a
228 /// specific MachineBasicBlock is specified, only that block and those after
229 /// it are renumbered.
230 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
232 /// print - Print out the MachineFunction in a format suitable for debugging
233 /// to the specified stream.
235 void print(raw_ostream &OS) const;
237 /// viewCFG - This function is meant for use from the debugger. You can just
238 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
239 /// program, displaying the CFG of the current function with the code for each
240 /// basic block inside. This depends on there being a 'dot' and 'gv' program
243 void viewCFG() const;
245 /// viewCFGOnly - This function is meant for use from the debugger. It works
246 /// just like viewCFG, but it does not include the contents of basic blocks
247 /// into the nodes, just the label. If you are only interested in the CFG
248 /// this can make the graph smaller.
250 void viewCFGOnly() const;
252 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
256 /// verify - Run the current MachineFunction through the machine code
257 /// verifier, useful for debugger use.
258 void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
260 // Provide accessors for the MachineBasicBlock list...
261 typedef BasicBlockListType::iterator iterator;
262 typedef BasicBlockListType::const_iterator const_iterator;
263 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
264 typedef std::reverse_iterator<iterator> reverse_iterator;
266 /// addLiveIn - Add the specified physical register as a live-in value and
267 /// create a corresponding virtual register for it.
268 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
270 //===--------------------------------------------------------------------===//
271 // BasicBlock accessor functions.
273 iterator begin() { return BasicBlocks.begin(); }
274 const_iterator begin() const { return BasicBlocks.begin(); }
275 iterator end () { return BasicBlocks.end(); }
276 const_iterator end () const { return BasicBlocks.end(); }
278 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
279 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
280 reverse_iterator rend () { return BasicBlocks.rend(); }
281 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
283 unsigned size() const { return (unsigned)BasicBlocks.size();}
284 bool empty() const { return BasicBlocks.empty(); }
285 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
286 MachineBasicBlock &front() { return BasicBlocks.front(); }
287 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
288 MachineBasicBlock & back() { return BasicBlocks.back(); }
290 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
291 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
292 void insert(iterator MBBI, MachineBasicBlock *MBB) {
293 BasicBlocks.insert(MBBI, MBB);
295 void splice(iterator InsertPt, iterator MBBI) {
296 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
298 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
299 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
302 void remove(iterator MBBI) {
303 BasicBlocks.remove(MBBI);
305 void erase(iterator MBBI) {
306 BasicBlocks.erase(MBBI);
309 //===--------------------------------------------------------------------===//
310 // Internal functions used to automatically number MachineBasicBlocks
313 /// getNextMBBNumber - Returns the next unique number to be assigned
314 /// to a MachineBasicBlock in this MachineFunction.
316 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
317 MBBNumbering.push_back(MBB);
318 return (unsigned)MBBNumbering.size()-1;
321 /// removeFromMBBNumbering - Remove the specific machine basic block from our
322 /// tracker, this is only really to be used by the MachineBasicBlock
324 void removeFromMBBNumbering(unsigned N) {
325 assert(N < MBBNumbering.size() && "Illegal basic block #");
329 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
330 /// of `new MachineInstr'.
332 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
336 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
337 /// 'Orig' instruction, identical in all ways except the instruction
338 /// has no parent, prev, or next.
340 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
342 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
344 /// DeleteMachineInstr - Delete the given MachineInstr.
346 void DeleteMachineInstr(MachineInstr *MI);
348 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
349 /// instead of `new MachineBasicBlock'.
351 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
353 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
355 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
357 /// getMachineMemOperand - Allocate a new MachineMemOperand.
358 /// MachineMemOperands are owned by the MachineFunction and need not be
359 /// explicitly deallocated.
360 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
361 int64_t o, uint64_t s,
362 unsigned base_alignment);
364 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
365 /// an existing one, adjusting by an offset and using the given size.
366 /// MachineMemOperands are owned by the MachineFunction and need not be
367 /// explicitly deallocated.
368 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
369 int64_t Offset, uint64_t Size);
371 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
372 /// pointers. This array is owned by the MachineFunction.
373 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
375 /// extractLoadMemRefs - Allocate an array and populate it with just the
376 /// load information from the given MachineMemOperand sequence.
377 std::pair<MachineInstr::mmo_iterator,
378 MachineInstr::mmo_iterator>
379 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
380 MachineInstr::mmo_iterator End);
382 /// extractStoreMemRefs - Allocate an array and populate it with just the
383 /// store information from the given MachineMemOperand sequence.
384 std::pair<MachineInstr::mmo_iterator,
385 MachineInstr::mmo_iterator>
386 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
387 MachineInstr::mmo_iterator End);
389 //===--------------------------------------------------------------------===//
390 // Label Manipulation.
393 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
394 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
395 /// normal 'L' label is returned.
396 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
397 bool isLinkerPrivate = false) const;
400 //===--------------------------------------------------------------------===//
404 /// getDILocation - Get the DILocation for a given DebugLoc object.
405 DILocation getDILocation(DebugLoc DL) const;
407 /// getDefaultDebugLoc - Get the default debug location for the machine
409 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
411 /// setDefaultDebugLoc - Get the default debug location for the machine
413 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
415 /// getDebugLocInfo - Get the debug info location tracker.
416 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
419 //===--------------------------------------------------------------------===//
420 // GraphTraits specializations for function basic block graphs (CFGs)
421 //===--------------------------------------------------------------------===//
423 // Provide specializations of GraphTraits to be able to treat a
424 // machine function as a graph of machine basic blocks... these are
425 // the same as the machine basic block iterators, except that the root
426 // node is implicitly the first node of the function.
428 template <> struct GraphTraits<MachineFunction*> :
429 public GraphTraits<MachineBasicBlock*> {
430 static NodeType *getEntryNode(MachineFunction *F) {
434 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
435 typedef MachineFunction::iterator nodes_iterator;
436 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
437 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
439 template <> struct GraphTraits<const MachineFunction*> :
440 public GraphTraits<const MachineBasicBlock*> {
441 static NodeType *getEntryNode(const MachineFunction *F) {
445 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
446 typedef MachineFunction::const_iterator nodes_iterator;
447 static nodes_iterator nodes_begin(const MachineFunction *F) {
450 static nodes_iterator nodes_end (const MachineFunction *F) {
456 // Provide specializations of GraphTraits to be able to treat a function as a
457 // graph of basic blocks... and to walk it in inverse order. Inverse order for
458 // a function is considered to be when traversing the predecessor edges of a BB
459 // instead of the successor edges.
461 template <> struct GraphTraits<Inverse<MachineFunction*> > :
462 public GraphTraits<Inverse<MachineBasicBlock*> > {
463 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
464 return &G.Graph->front();
467 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
468 public GraphTraits<Inverse<const MachineBasicBlock*> > {
469 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
470 return &G.Graph->front();
474 } // End llvm namespace