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"
31 class MachineRegisterInfo;
32 class MachineFrameInfo;
33 class MachineConstantPool;
34 class MachineJumpTableInfo;
35 class MachineModuleInfo;
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;
76 MachineModuleInfo &MMI;
78 // RegInfo - Information about each register in use in the function.
79 MachineRegisterInfo *RegInfo;
81 // Used to keep track of target-specific per-machine function information for
82 // the target implementation.
83 MachineFunctionInfo *MFInfo;
85 // Keep track of objects allocated on the stack.
86 MachineFrameInfo *FrameInfo;
88 // Keep track of constants which are spilled to memory
89 MachineConstantPool *ConstantPool;
91 // Keep track of jump tables for switch instructions
92 MachineJumpTableInfo *JumpTableInfo;
94 // Function-level unique numbering for MachineBasicBlocks. When a
95 // MachineBasicBlock is inserted into a MachineFunction is it automatically
96 // numbered and this vector keeps track of the mapping from ID's to MBB's.
97 std::vector<MachineBasicBlock*> MBBNumbering;
99 // Pool-allocate MachineFunction-lifetime and IR objects.
100 BumpPtrAllocator Allocator;
102 // Allocation management for instructions in function.
103 Recycler<MachineInstr> InstructionRecycler;
105 // Allocation management for basic blocks in function.
106 Recycler<MachineBasicBlock> BasicBlockRecycler;
108 // List of machine basic blocks in function
109 typedef ilist<MachineBasicBlock> BasicBlockListType;
110 BasicBlockListType BasicBlocks;
112 /// FunctionNumber - This provides a unique ID for each function emitted in
113 /// this translation unit.
115 unsigned FunctionNumber;
117 /// Alignment - The alignment of the function.
120 /// HasReturnsTwiceCall - Returns true if there's a call with a
121 /// "returns_twice" attribute, like setjmp.
122 bool HasReturnsTwiceCall;
124 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
125 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
127 MachineFunction(const Function *Fn, const TargetMachine &TM,
128 unsigned FunctionNum, MachineModuleInfo &MMI);
131 MachineModuleInfo &getMMI() const { return MMI; }
132 MCContext &getContext() const { return Ctx; }
134 /// getFunction - Return the LLVM function that this machine code represents
136 const Function *getFunction() const { return Fn; }
138 /// getFunctionNumber - Return a unique ID for the current function.
140 unsigned getFunctionNumber() const { return FunctionNumber; }
142 /// getTarget - Return the target machine this machine code is compiled with
144 const TargetMachine &getTarget() const { return Target; }
146 /// getRegInfo - Return information about the registers currently in use.
148 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
149 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
151 /// getFrameInfo - Return the frame info object for the current function.
152 /// This object contains information about objects allocated on the stack
153 /// frame of the current function in an abstract way.
155 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
156 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
158 /// getJumpTableInfo - Return the jump table info object for the current
159 /// function. This object contains information about jump tables in the
160 /// current function. If the current function has no jump tables, this will
162 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
163 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
165 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
166 /// does already exist, allocate one.
167 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
170 /// getConstantPool - Return the constant pool object for the current
173 MachineConstantPool *getConstantPool() { return ConstantPool; }
174 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
176 /// getAlignment - Return the alignment (log2, not bytes) of the function.
178 unsigned getAlignment() const { return Alignment; }
180 /// setAlignment - Set the alignment (log2, not bytes) of the function.
182 void setAlignment(unsigned A) { Alignment = A; }
184 /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
185 void EnsureAlignment(unsigned A) {
186 if (Alignment < A) Alignment = A;
189 /// hasReturnsTwiceCall - Returns true if there's a call with a
190 /// "returns_twice" attribute, like setjmp.
191 bool hasReturnsTwiceCall() const {
192 return HasReturnsTwiceCall;
194 void setReturnsTwiceCall(bool B) {
195 HasReturnsTwiceCall = B;
198 /// getInfo - Keep track of various per-function pieces of information for
199 /// backends that would like to do so.
201 template<typename Ty>
204 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
205 // that apparently breaks GCC 3.3.
206 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
207 AlignOf<Ty>::Alignment));
208 MFInfo = new (Loc) Ty(*this);
210 return static_cast<Ty*>(MFInfo);
213 template<typename Ty>
214 const Ty *getInfo() const {
215 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
218 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
219 /// are inserted into the machine function. The block number for a machine
220 /// basic block can be found by using the MBB::getBlockNumber method, this
221 /// method provides the inverse mapping.
223 MachineBasicBlock *getBlockNumbered(unsigned N) const {
224 assert(N < MBBNumbering.size() && "Illegal block number");
225 assert(MBBNumbering[N] && "Block was removed from the machine function!");
226 return MBBNumbering[N];
229 /// getNumBlockIDs - Return the number of MBB ID's allocated.
231 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
233 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
234 /// recomputes them. This guarantees that the MBB numbers are sequential,
235 /// dense, and match the ordering of the blocks within the function. If a
236 /// specific MachineBasicBlock is specified, only that block and those after
237 /// it are renumbered.
238 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
240 /// print - Print out the MachineFunction in a format suitable for debugging
241 /// to the specified stream.
243 void print(raw_ostream &OS) const;
245 /// viewCFG - This function is meant for use from the debugger. You can just
246 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
247 /// program, displaying the CFG of the current function with the code for each
248 /// basic block inside. This depends on there being a 'dot' and 'gv' program
251 void viewCFG() const;
253 /// viewCFGOnly - This function is meant for use from the debugger. It works
254 /// just like viewCFG, but it does not include the contents of basic blocks
255 /// into the nodes, just the label. If you are only interested in the CFG
256 /// this can make the graph smaller.
258 void viewCFGOnly() const;
260 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
264 /// verify - Run the current MachineFunction through the machine code
265 /// verifier, useful for debugger use.
266 void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
268 // Provide accessors for the MachineBasicBlock list...
269 typedef BasicBlockListType::iterator iterator;
270 typedef BasicBlockListType::const_iterator const_iterator;
271 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
272 typedef std::reverse_iterator<iterator> reverse_iterator;
274 /// addLiveIn - Add the specified physical register as a live-in value and
275 /// create a corresponding virtual register for it.
276 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
278 //===--------------------------------------------------------------------===//
279 // BasicBlock accessor functions.
281 iterator begin() { return BasicBlocks.begin(); }
282 const_iterator begin() const { return BasicBlocks.begin(); }
283 iterator end () { return BasicBlocks.end(); }
284 const_iterator end () const { return BasicBlocks.end(); }
286 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
287 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
288 reverse_iterator rend () { return BasicBlocks.rend(); }
289 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
291 unsigned size() const { return (unsigned)BasicBlocks.size();}
292 bool empty() const { return BasicBlocks.empty(); }
293 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
294 MachineBasicBlock &front() { return BasicBlocks.front(); }
295 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
296 MachineBasicBlock & back() { return BasicBlocks.back(); }
298 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
299 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
300 void insert(iterator MBBI, MachineBasicBlock *MBB) {
301 BasicBlocks.insert(MBBI, MBB);
303 void splice(iterator InsertPt, iterator MBBI) {
304 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
306 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
307 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
310 void remove(iterator MBBI) {
311 BasicBlocks.remove(MBBI);
313 void erase(iterator MBBI) {
314 BasicBlocks.erase(MBBI);
317 //===--------------------------------------------------------------------===//
318 // Internal functions used to automatically number MachineBasicBlocks
321 /// getNextMBBNumber - Returns the next unique number to be assigned
322 /// to a MachineBasicBlock in this MachineFunction.
324 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
325 MBBNumbering.push_back(MBB);
326 return (unsigned)MBBNumbering.size()-1;
329 /// removeFromMBBNumbering - Remove the specific machine basic block from our
330 /// tracker, this is only really to be used by the MachineBasicBlock
332 void removeFromMBBNumbering(unsigned N) {
333 assert(N < MBBNumbering.size() && "Illegal basic block #");
337 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
338 /// of `new MachineInstr'.
340 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
344 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
345 /// 'Orig' instruction, identical in all ways except the instruction
346 /// has no parent, prev, or next.
348 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
350 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
352 /// DeleteMachineInstr - Delete the given MachineInstr.
354 void DeleteMachineInstr(MachineInstr *MI);
356 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
357 /// instead of `new MachineBasicBlock'.
359 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
361 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
363 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
365 /// getMachineMemOperand - Allocate a new MachineMemOperand.
366 /// MachineMemOperands are owned by the MachineFunction and need not be
367 /// explicitly deallocated.
368 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
369 int64_t o, uint64_t s,
370 unsigned base_alignment);
372 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
373 /// an existing one, adjusting by an offset and using the given size.
374 /// MachineMemOperands are owned by the MachineFunction and need not be
375 /// explicitly deallocated.
376 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
377 int64_t Offset, uint64_t Size);
379 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
380 /// pointers. This array is owned by the MachineFunction.
381 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
383 /// extractLoadMemRefs - Allocate an array and populate it with just the
384 /// load information from the given MachineMemOperand sequence.
385 std::pair<MachineInstr::mmo_iterator,
386 MachineInstr::mmo_iterator>
387 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
388 MachineInstr::mmo_iterator End);
390 /// extractStoreMemRefs - Allocate an array and populate it with just the
391 /// store information from the given MachineMemOperand sequence.
392 std::pair<MachineInstr::mmo_iterator,
393 MachineInstr::mmo_iterator>
394 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
395 MachineInstr::mmo_iterator End);
397 //===--------------------------------------------------------------------===//
398 // Label Manipulation.
401 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
402 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
403 /// normal 'L' label is returned.
404 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
405 bool isLinkerPrivate = false) const;
408 //===--------------------------------------------------------------------===//
409 // GraphTraits specializations for function basic block graphs (CFGs)
410 //===--------------------------------------------------------------------===//
412 // Provide specializations of GraphTraits to be able to treat a
413 // machine function as a graph of machine basic blocks... these are
414 // the same as the machine basic block iterators, except that the root
415 // node is implicitly the first node of the function.
417 template <> struct GraphTraits<MachineFunction*> :
418 public GraphTraits<MachineBasicBlock*> {
419 static NodeType *getEntryNode(MachineFunction *F) {
423 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
424 typedef MachineFunction::iterator nodes_iterator;
425 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
426 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
428 template <> struct GraphTraits<const MachineFunction*> :
429 public GraphTraits<const MachineBasicBlock*> {
430 static NodeType *getEntryNode(const MachineFunction *F) {
434 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
435 typedef MachineFunction::const_iterator nodes_iterator;
436 static nodes_iterator nodes_begin(const MachineFunction *F) {
439 static nodes_iterator nodes_end (const MachineFunction *F) {
445 // Provide specializations of GraphTraits to be able to treat a function as a
446 // graph of basic blocks... and to walk it in inverse order. Inverse order for
447 // a function is considered to be when traversing the predecessor edges of a BB
448 // instead of the successor edges.
450 template <> struct GraphTraits<Inverse<MachineFunction*> > :
451 public GraphTraits<Inverse<MachineBasicBlock*> > {
452 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
453 return &G.Graph->front();
456 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
457 public GraphTraits<Inverse<const MachineBasicBlock*> > {
458 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
459 return &G.Graph->front();
463 } // End llvm namespace