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;
40 struct MachinePointerInfo;
43 struct ilist_traits<MachineBasicBlock>
44 : public ilist_default_traits<MachineBasicBlock> {
45 mutable ilist_half_node<MachineBasicBlock> Sentinel;
47 MachineBasicBlock *createSentinel() const {
48 return static_cast<MachineBasicBlock*>(&Sentinel);
50 void destroySentinel(MachineBasicBlock *) const {}
52 MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
53 MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
54 return createSentinel();
56 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
58 void addNodeToList(MachineBasicBlock* MBB);
59 void removeNodeFromList(MachineBasicBlock* MBB);
60 void deleteNode(MachineBasicBlock *MBB);
62 void createNode(const MachineBasicBlock &);
65 /// MachineFunctionInfo - This class can be derived from and used by targets to
66 /// hold private target-specific information for each MachineFunction. Objects
67 /// of type are accessed/created with MF::getInfo and destroyed when the
68 /// MachineFunction is destroyed.
69 struct MachineFunctionInfo {
70 virtual ~MachineFunctionInfo();
73 class MachineFunction {
75 const TargetMachine &Target;
77 MachineModuleInfo &MMI;
79 // RegInfo - Information about each register in use in the function.
80 MachineRegisterInfo *RegInfo;
82 // Used to keep track of target-specific per-machine function information for
83 // the target implementation.
84 MachineFunctionInfo *MFInfo;
86 // Keep track of objects allocated on the stack.
87 MachineFrameInfo *FrameInfo;
89 // Keep track of constants which are spilled to memory
90 MachineConstantPool *ConstantPool;
92 // Keep track of jump tables for switch instructions
93 MachineJumpTableInfo *JumpTableInfo;
95 // Function-level unique numbering for MachineBasicBlocks. When a
96 // MachineBasicBlock is inserted into a MachineFunction is it automatically
97 // numbered and this vector keeps track of the mapping from ID's to MBB's.
98 std::vector<MachineBasicBlock*> MBBNumbering;
100 // Pool-allocate MachineFunction-lifetime and IR objects.
101 BumpPtrAllocator Allocator;
103 // Allocation management for instructions in function.
104 Recycler<MachineInstr> InstructionRecycler;
106 // Allocation management for basic blocks in function.
107 Recycler<MachineBasicBlock> BasicBlockRecycler;
109 // List of machine basic blocks in function
110 typedef ilist<MachineBasicBlock> BasicBlockListType;
111 BasicBlockListType BasicBlocks;
113 /// FunctionNumber - This provides a unique ID for each function emitted in
114 /// this translation unit.
116 unsigned FunctionNumber;
118 /// Alignment - The alignment of the function.
121 /// CallsSetJmp - True if the function calls setjmp or sigsetjmp. This is used
122 /// to limit optimizations which cannot reason about the control flow of
126 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
127 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
129 MachineFunction(const Function *Fn, const TargetMachine &TM,
130 unsigned FunctionNum, MachineModuleInfo &MMI);
133 MachineModuleInfo &getMMI() const { return MMI; }
134 MCContext &getContext() const { return Ctx; }
136 /// getFunction - Return the LLVM function that this machine code represents
138 const 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 /// callsSetJmp - Returns true if the function calls setjmp or sigsetjmp.
192 bool callsSetJmp() const {
196 /// setCallsSetJmp - Set a flag that indicates if there's a call to setjmp or
198 void setCallsSetJmp(bool B) {
202 /// getInfo - Keep track of various per-function pieces of information for
203 /// backends that would like to do so.
205 template<typename Ty>
208 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
209 // that apparently breaks GCC 3.3.
210 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
211 AlignOf<Ty>::Alignment));
212 MFInfo = new (Loc) Ty(*this);
214 return static_cast<Ty*>(MFInfo);
217 template<typename Ty>
218 const Ty *getInfo() const {
219 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
222 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
223 /// are inserted into the machine function. The block number for a machine
224 /// basic block can be found by using the MBB::getBlockNumber method, this
225 /// method provides the inverse mapping.
227 MachineBasicBlock *getBlockNumbered(unsigned N) const {
228 assert(N < MBBNumbering.size() && "Illegal block number");
229 assert(MBBNumbering[N] && "Block was removed from the machine function!");
230 return MBBNumbering[N];
233 /// getNumBlockIDs - Return the number of MBB ID's allocated.
235 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
237 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
238 /// recomputes them. This guarantees that the MBB numbers are sequential,
239 /// dense, and match the ordering of the blocks within the function. If a
240 /// specific MachineBasicBlock is specified, only that block and those after
241 /// it are renumbered.
242 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
244 /// print - Print out the MachineFunction in a format suitable for debugging
245 /// to the specified stream.
247 void print(raw_ostream &OS) const;
249 /// viewCFG - This function is meant for use from the debugger. You can just
250 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
251 /// program, displaying the CFG of the current function with the code for each
252 /// basic block inside. This depends on there being a 'dot' and 'gv' program
255 void viewCFG() const;
257 /// viewCFGOnly - This function is meant for use from the debugger. It works
258 /// just like viewCFG, but it does not include the contents of basic blocks
259 /// into the nodes, just the label. If you are only interested in the CFG
260 /// this can make the graph smaller.
262 void viewCFGOnly() const;
264 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
268 /// verify - Run the current MachineFunction through the machine code
269 /// verifier, useful for debugger use.
270 void verify(Pass *p=NULL) const;
272 // Provide accessors for the MachineBasicBlock list...
273 typedef BasicBlockListType::iterator iterator;
274 typedef BasicBlockListType::const_iterator const_iterator;
275 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
276 typedef std::reverse_iterator<iterator> reverse_iterator;
278 /// addLiveIn - Add the specified physical register as a live-in value and
279 /// create a corresponding virtual register for it.
280 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
282 //===--------------------------------------------------------------------===//
283 // BasicBlock accessor functions.
285 iterator begin() { return BasicBlocks.begin(); }
286 const_iterator begin() const { return BasicBlocks.begin(); }
287 iterator end () { return BasicBlocks.end(); }
288 const_iterator end () const { return BasicBlocks.end(); }
290 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
291 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
292 reverse_iterator rend () { return BasicBlocks.rend(); }
293 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
295 unsigned size() const { return (unsigned)BasicBlocks.size();}
296 bool empty() const { return BasicBlocks.empty(); }
297 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
298 MachineBasicBlock &front() { return BasicBlocks.front(); }
299 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
300 MachineBasicBlock & back() { return BasicBlocks.back(); }
302 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
303 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
304 void insert(iterator MBBI, MachineBasicBlock *MBB) {
305 BasicBlocks.insert(MBBI, MBB);
307 void splice(iterator InsertPt, iterator MBBI) {
308 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
310 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
311 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
314 void remove(iterator MBBI) {
315 BasicBlocks.remove(MBBI);
317 void erase(iterator MBBI) {
318 BasicBlocks.erase(MBBI);
321 //===--------------------------------------------------------------------===//
322 // Internal functions used to automatically number MachineBasicBlocks
325 /// getNextMBBNumber - Returns the next unique number to be assigned
326 /// to a MachineBasicBlock in this MachineFunction.
328 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
329 MBBNumbering.push_back(MBB);
330 return (unsigned)MBBNumbering.size()-1;
333 /// removeFromMBBNumbering - Remove the specific machine basic block from our
334 /// tracker, this is only really to be used by the MachineBasicBlock
336 void removeFromMBBNumbering(unsigned N) {
337 assert(N < MBBNumbering.size() && "Illegal basic block #");
341 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
342 /// of `new MachineInstr'.
344 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
348 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
349 /// 'Orig' instruction, identical in all ways except the instruction
350 /// has no parent, prev, or next.
352 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
354 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
356 /// DeleteMachineInstr - Delete the given MachineInstr.
358 void DeleteMachineInstr(MachineInstr *MI);
360 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
361 /// instead of `new MachineBasicBlock'.
363 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
365 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
367 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
369 /// getMachineMemOperand - Allocate a new MachineMemOperand.
370 /// MachineMemOperands are owned by the MachineFunction and need not be
371 /// explicitly deallocated.
372 MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo,
373 unsigned f, uint64_t s,
374 unsigned base_alignment,
375 const MDNode *TBAAInfo = 0);
377 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
378 /// an existing one, adjusting by an offset and using the given size.
379 /// MachineMemOperands are owned by the MachineFunction and need not be
380 /// explicitly deallocated.
381 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
382 int64_t Offset, uint64_t Size);
384 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
385 /// pointers. This array is owned by the MachineFunction.
386 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
388 /// extractLoadMemRefs - Allocate an array and populate it with just the
389 /// load information from the given MachineMemOperand sequence.
390 std::pair<MachineInstr::mmo_iterator,
391 MachineInstr::mmo_iterator>
392 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
393 MachineInstr::mmo_iterator End);
395 /// extractStoreMemRefs - Allocate an array and populate it with just the
396 /// store information from the given MachineMemOperand sequence.
397 std::pair<MachineInstr::mmo_iterator,
398 MachineInstr::mmo_iterator>
399 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
400 MachineInstr::mmo_iterator End);
402 //===--------------------------------------------------------------------===//
403 // Label Manipulation.
406 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
407 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
408 /// normal 'L' label is returned.
409 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
410 bool isLinkerPrivate = false) const;
413 //===--------------------------------------------------------------------===//
414 // GraphTraits specializations for function basic block graphs (CFGs)
415 //===--------------------------------------------------------------------===//
417 // Provide specializations of GraphTraits to be able to treat a
418 // machine function as a graph of machine basic blocks... these are
419 // the same as the machine basic block iterators, except that the root
420 // node is implicitly the first node of the function.
422 template <> struct GraphTraits<MachineFunction*> :
423 public GraphTraits<MachineBasicBlock*> {
424 static NodeType *getEntryNode(MachineFunction *F) {
428 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
429 typedef MachineFunction::iterator nodes_iterator;
430 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
431 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
433 template <> struct GraphTraits<const MachineFunction*> :
434 public GraphTraits<const MachineBasicBlock*> {
435 static NodeType *getEntryNode(const MachineFunction *F) {
439 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
440 typedef MachineFunction::const_iterator nodes_iterator;
441 static nodes_iterator nodes_begin(const MachineFunction *F) {
444 static nodes_iterator nodes_end (const MachineFunction *F) {
450 // Provide specializations of GraphTraits to be able to treat a function as a
451 // graph of basic blocks... and to walk it in inverse order. Inverse order for
452 // a function is considered to be when traversing the predecessor edges of a BB
453 // instead of the successor edges.
455 template <> struct GraphTraits<Inverse<MachineFunction*> > :
456 public GraphTraits<Inverse<MachineBasicBlock*> > {
457 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
458 return &G.Graph->front();
461 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
462 public GraphTraits<Inverse<const MachineBasicBlock*> > {
463 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
464 return &G.Graph->front();
468 } // End llvm namespace