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;
36 class MachineModuleInfo;
40 class TargetRegisterClass;
41 struct MachinePointerInfo;
44 struct ilist_traits<MachineBasicBlock>
45 : public ilist_default_traits<MachineBasicBlock> {
46 mutable ilist_half_node<MachineBasicBlock> Sentinel;
48 MachineBasicBlock *createSentinel() const {
49 return static_cast<MachineBasicBlock*>(&Sentinel);
51 void destroySentinel(MachineBasicBlock *) const {}
53 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
54 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
55 return createSentinel();
57 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
59 void addNodeToList(MachineBasicBlock* MBB);
60 void removeNodeFromList(MachineBasicBlock* MBB);
61 void deleteNode(MachineBasicBlock *MBB);
63 void createNode(const MachineBasicBlock &);
66 /// MachineFunctionInfo - This class can be derived from and used by targets to
67 /// hold private target-specific information for each MachineFunction. Objects
68 /// of type are accessed/created with MF::getInfo and destroyed when the
69 /// MachineFunction is destroyed.
70 struct MachineFunctionInfo {
71 virtual ~MachineFunctionInfo();
74 class MachineFunction {
76 const TargetMachine &Target;
78 MachineModuleInfo &MMI;
81 // RegInfo - Information about each register in use in the function.
82 MachineRegisterInfo *RegInfo;
84 // Used to keep track of target-specific per-machine function information for
85 // the target implementation.
86 MachineFunctionInfo *MFInfo;
88 // Keep track of objects allocated on the stack.
89 MachineFrameInfo *FrameInfo;
91 // Keep track of constants which are spilled to memory
92 MachineConstantPool *ConstantPool;
94 // Keep track of jump tables for switch instructions
95 MachineJumpTableInfo *JumpTableInfo;
97 // Function-level unique numbering for MachineBasicBlocks. When a
98 // MachineBasicBlock is inserted into a MachineFunction is it automatically
99 // numbered and this vector keeps track of the mapping from ID's to MBB's.
100 std::vector<MachineBasicBlock*> MBBNumbering;
102 // Pool-allocate MachineFunction-lifetime and IR objects.
103 BumpPtrAllocator Allocator;
105 // Allocation management for instructions in function.
106 Recycler<MachineInstr> InstructionRecycler;
108 // Allocation management for basic blocks in function.
109 Recycler<MachineBasicBlock> BasicBlockRecycler;
111 // List of machine basic blocks in function
112 typedef ilist<MachineBasicBlock> BasicBlockListType;
113 BasicBlockListType BasicBlocks;
115 /// FunctionNumber - This provides a unique ID for each function emitted in
116 /// this translation unit.
118 unsigned FunctionNumber;
120 /// Alignment - The alignment of the function.
123 /// CallsSetJmp - True if the function calls setjmp or sigsetjmp. This is used
124 /// to limit optimizations which cannot reason about the control flow of
128 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
129 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
131 MachineFunction(const Function *Fn, const TargetMachine &TM,
132 unsigned FunctionNum, MachineModuleInfo &MMI,
136 MachineModuleInfo &getMMI() const { return MMI; }
137 GCModuleInfo *getGMI() const { return GMI; }
138 MCContext &getContext() const { return Ctx; }
140 /// getFunction - Return the LLVM function that this machine code represents
142 const Function *getFunction() const { return Fn; }
144 /// getFunctionNumber - Return a unique ID for the current function.
146 unsigned getFunctionNumber() const { return FunctionNumber; }
148 /// getTarget - Return the target machine this machine code is compiled with
150 const TargetMachine &getTarget() const { return Target; }
152 /// getRegInfo - Return information about the registers currently in use.
154 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
155 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
157 /// getFrameInfo - Return the frame info object for the current function.
158 /// This object contains information about objects allocated on the stack
159 /// frame of the current function in an abstract way.
161 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
162 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
164 /// getJumpTableInfo - Return the jump table info object for the current
165 /// function. This object contains information about jump tables in the
166 /// current function. If the current function has no jump tables, this will
168 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
169 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
171 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
172 /// does already exist, allocate one.
173 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
176 /// getConstantPool - Return the constant pool object for the current
179 MachineConstantPool *getConstantPool() { return ConstantPool; }
180 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
182 /// getAlignment - Return the alignment (log2, not bytes) of the function.
184 unsigned getAlignment() const { return Alignment; }
186 /// setAlignment - Set the alignment (log2, not bytes) of the function.
188 void setAlignment(unsigned A) { Alignment = A; }
190 /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
191 void EnsureAlignment(unsigned A) {
192 if (Alignment < A) Alignment = A;
195 /// callsSetJmp - Returns true if the function calls setjmp or sigsetjmp.
196 bool callsSetJmp() const {
200 /// setCallsSetJmp - Set a flag that indicates if there's a call to setjmp or
202 void setCallsSetJmp(bool B) {
206 /// getInfo - Keep track of various per-function pieces of information for
207 /// backends that would like to do so.
209 template<typename Ty>
212 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
213 // that apparently breaks GCC 3.3.
214 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
215 AlignOf<Ty>::Alignment));
216 MFInfo = new (Loc) Ty(*this);
218 return static_cast<Ty*>(MFInfo);
221 template<typename Ty>
222 const Ty *getInfo() const {
223 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
226 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
227 /// are inserted into the machine function. The block number for a machine
228 /// basic block can be found by using the MBB::getBlockNumber method, this
229 /// method provides the inverse mapping.
231 MachineBasicBlock *getBlockNumbered(unsigned N) const {
232 assert(N < MBBNumbering.size() && "Illegal block number");
233 assert(MBBNumbering[N] && "Block was removed from the machine function!");
234 return MBBNumbering[N];
237 /// getNumBlockIDs - Return the number of MBB ID's allocated.
239 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
241 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
242 /// recomputes them. This guarantees that the MBB numbers are sequential,
243 /// dense, and match the ordering of the blocks within the function. If a
244 /// specific MachineBasicBlock is specified, only that block and those after
245 /// it are renumbered.
246 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
248 /// print - Print out the MachineFunction in a format suitable for debugging
249 /// to the specified stream.
251 void print(raw_ostream &OS, SlotIndexes* = 0) const;
253 /// viewCFG - This function is meant for use from the debugger. You can just
254 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
255 /// program, displaying the CFG of the current function with the code for each
256 /// basic block inside. This depends on there being a 'dot' and 'gv' program
259 void viewCFG() const;
261 /// viewCFGOnly - This function is meant for use from the debugger. It works
262 /// just like viewCFG, but it does not include the contents of basic blocks
263 /// into the nodes, just the label. If you are only interested in the CFG
264 /// this can make the graph smaller.
266 void viewCFGOnly() const;
268 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
272 /// verify - Run the current MachineFunction through the machine code
273 /// verifier, useful for debugger use.
274 void verify(Pass *p=NULL) const;
276 // Provide accessors for the MachineBasicBlock list...
277 typedef BasicBlockListType::iterator iterator;
278 typedef BasicBlockListType::const_iterator const_iterator;
279 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
280 typedef std::reverse_iterator<iterator> reverse_iterator;
282 /// addLiveIn - Add the specified physical register as a live-in value and
283 /// create a corresponding virtual register for it.
284 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
286 //===--------------------------------------------------------------------===//
287 // BasicBlock accessor functions.
289 iterator begin() { return BasicBlocks.begin(); }
290 const_iterator begin() const { return BasicBlocks.begin(); }
291 iterator end () { return BasicBlocks.end(); }
292 const_iterator end () const { return BasicBlocks.end(); }
294 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
295 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
296 reverse_iterator rend () { return BasicBlocks.rend(); }
297 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
299 unsigned size() const { return (unsigned)BasicBlocks.size();}
300 bool empty() const { return BasicBlocks.empty(); }
301 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
302 MachineBasicBlock &front() { return BasicBlocks.front(); }
303 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
304 MachineBasicBlock & back() { return BasicBlocks.back(); }
306 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
307 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
308 void insert(iterator MBBI, MachineBasicBlock *MBB) {
309 BasicBlocks.insert(MBBI, MBB);
311 void splice(iterator InsertPt, iterator MBBI) {
312 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
314 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
315 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
318 void remove(iterator MBBI) {
319 BasicBlocks.remove(MBBI);
321 void erase(iterator MBBI) {
322 BasicBlocks.erase(MBBI);
325 //===--------------------------------------------------------------------===//
326 // Internal functions used to automatically number MachineBasicBlocks
329 /// getNextMBBNumber - Returns the next unique number to be assigned
330 /// to a MachineBasicBlock in this MachineFunction.
332 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
333 MBBNumbering.push_back(MBB);
334 return (unsigned)MBBNumbering.size()-1;
337 /// removeFromMBBNumbering - Remove the specific machine basic block from our
338 /// tracker, this is only really to be used by the MachineBasicBlock
340 void removeFromMBBNumbering(unsigned N) {
341 assert(N < MBBNumbering.size() && "Illegal basic block #");
345 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
346 /// of `new MachineInstr'.
348 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
352 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
353 /// 'Orig' instruction, identical in all ways except the instruction
354 /// has no parent, prev, or next.
356 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
358 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
360 /// DeleteMachineInstr - Delete the given MachineInstr.
362 void DeleteMachineInstr(MachineInstr *MI);
364 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
365 /// instead of `new MachineBasicBlock'.
367 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
369 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
371 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
373 /// getMachineMemOperand - Allocate a new MachineMemOperand.
374 /// MachineMemOperands are owned by the MachineFunction and need not be
375 /// explicitly deallocated.
376 MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
377 unsigned f, uint64_t s,
378 unsigned base_alignment,
379 const MDNode *TBAAInfo = 0);
381 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
382 /// an existing one, adjusting by an offset and using the given size.
383 /// MachineMemOperands are owned by the MachineFunction and need not be
384 /// explicitly deallocated.
385 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
386 int64_t Offset, uint64_t Size);
388 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
389 /// pointers. This array is owned by the MachineFunction.
390 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
392 /// extractLoadMemRefs - Allocate an array and populate it with just the
393 /// load information from the given MachineMemOperand sequence.
394 std::pair<MachineInstr::mmo_iterator,
395 MachineInstr::mmo_iterator>
396 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
397 MachineInstr::mmo_iterator End);
399 /// extractStoreMemRefs - Allocate an array and populate it with just the
400 /// store information from the given MachineMemOperand sequence.
401 std::pair<MachineInstr::mmo_iterator,
402 MachineInstr::mmo_iterator>
403 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
404 MachineInstr::mmo_iterator End);
406 //===--------------------------------------------------------------------===//
407 // Label Manipulation.
410 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
411 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
412 /// normal 'L' label is returned.
413 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
414 bool isLinkerPrivate = false) const;
416 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC
418 MCSymbol *getPICBaseSymbol() const;
421 //===--------------------------------------------------------------------===//
422 // GraphTraits specializations for function basic block graphs (CFGs)
423 //===--------------------------------------------------------------------===//
425 // Provide specializations of GraphTraits to be able to treat a
426 // machine function as a graph of machine basic blocks... these are
427 // the same as the machine basic block iterators, except that the root
428 // node is implicitly the first node of the function.
430 template <> struct GraphTraits<MachineFunction*> :
431 public GraphTraits<MachineBasicBlock*> {
432 static NodeType *getEntryNode(MachineFunction *F) {
436 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
437 typedef MachineFunction::iterator nodes_iterator;
438 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
439 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
441 template <> struct GraphTraits<const MachineFunction*> :
442 public GraphTraits<const MachineBasicBlock*> {
443 static NodeType *getEntryNode(const MachineFunction *F) {
447 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
448 typedef MachineFunction::const_iterator nodes_iterator;
449 static nodes_iterator nodes_begin(const MachineFunction *F) {
452 static nodes_iterator nodes_end (const MachineFunction *F) {
458 // Provide specializations of GraphTraits to be able to treat a function as a
459 // graph of basic blocks... and to walk it in inverse order. Inverse order for
460 // a function is considered to be when traversing the predecessor edges of a BB
461 // instead of the successor edges.
463 template <> struct GraphTraits<Inverse<MachineFunction*> > :
464 public GraphTraits<Inverse<MachineBasicBlock*> > {
465 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
466 return &G.Graph->front();
469 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
470 public GraphTraits<Inverse<const MachineBasicBlock*> > {
471 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
472 return &G.Graph->front();
476 } // End llvm namespace