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 /// CallsSetJmp - True if the function calls setjmp or sigsetjmp. This is used
121 /// to limit optimizations which cannot reason about the control flow of
125 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
126 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
128 MachineFunction(const Function *Fn, const TargetMachine &TM,
129 unsigned FunctionNum, MachineModuleInfo &MMI);
132 MachineModuleInfo &getMMI() const { return MMI; }
133 MCContext &getContext() const { return Ctx; }
135 /// getFunction - Return the LLVM function that this machine code represents
137 const 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 /// callsSetJmp - Returns true if the function calls setjmp or sigsetjmp.
191 bool callsSetJmp() const {
195 /// setCallsSetJmp - Set a flag that indicates if there's a call to setjmp or
197 void setCallsSetJmp(bool B) {
201 /// getInfo - Keep track of various per-function pieces of information for
202 /// backends that would like to do so.
204 template<typename Ty>
207 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
208 // that apparently breaks GCC 3.3.
209 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
210 AlignOf<Ty>::Alignment));
211 MFInfo = new (Loc) Ty(*this);
213 return static_cast<Ty*>(MFInfo);
216 template<typename Ty>
217 const Ty *getInfo() const {
218 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
221 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
222 /// are inserted into the machine function. The block number for a machine
223 /// basic block can be found by using the MBB::getBlockNumber method, this
224 /// method provides the inverse mapping.
226 MachineBasicBlock *getBlockNumbered(unsigned N) const {
227 assert(N < MBBNumbering.size() && "Illegal block number");
228 assert(MBBNumbering[N] && "Block was removed from the machine function!");
229 return MBBNumbering[N];
232 /// getNumBlockIDs - Return the number of MBB ID's allocated.
234 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
236 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
237 /// recomputes them. This guarantees that the MBB numbers are sequential,
238 /// dense, and match the ordering of the blocks within the function. If a
239 /// specific MachineBasicBlock is specified, only that block and those after
240 /// it are renumbered.
241 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
243 /// print - Print out the MachineFunction in a format suitable for debugging
244 /// to the specified stream.
246 void print(raw_ostream &OS) const;
248 /// viewCFG - This function is meant for use from the debugger. You can just
249 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
250 /// program, displaying the CFG of the current function with the code for each
251 /// basic block inside. This depends on there being a 'dot' and 'gv' program
254 void viewCFG() const;
256 /// viewCFGOnly - This function is meant for use from the debugger. It works
257 /// just like viewCFG, but it does not include the contents of basic blocks
258 /// into the nodes, just the label. If you are only interested in the CFG
259 /// this can make the graph smaller.
261 void viewCFGOnly() const;
263 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
267 /// verify - Run the current MachineFunction through the machine code
268 /// verifier, useful for debugger use.
269 void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
271 // Provide accessors for the MachineBasicBlock list...
272 typedef BasicBlockListType::iterator iterator;
273 typedef BasicBlockListType::const_iterator const_iterator;
274 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
275 typedef std::reverse_iterator<iterator> reverse_iterator;
277 /// addLiveIn - Add the specified physical register as a live-in value and
278 /// create a corresponding virtual register for it.
279 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
281 //===--------------------------------------------------------------------===//
282 // BasicBlock accessor functions.
284 iterator begin() { return BasicBlocks.begin(); }
285 const_iterator begin() const { return BasicBlocks.begin(); }
286 iterator end () { return BasicBlocks.end(); }
287 const_iterator end () const { return BasicBlocks.end(); }
289 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
290 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
291 reverse_iterator rend () { return BasicBlocks.rend(); }
292 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
294 unsigned size() const { return (unsigned)BasicBlocks.size();}
295 bool empty() const { return BasicBlocks.empty(); }
296 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
297 MachineBasicBlock &front() { return BasicBlocks.front(); }
298 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
299 MachineBasicBlock & back() { return BasicBlocks.back(); }
301 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
302 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
303 void insert(iterator MBBI, MachineBasicBlock *MBB) {
304 BasicBlocks.insert(MBBI, MBB);
306 void splice(iterator InsertPt, iterator MBBI) {
307 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
309 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
310 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
313 void remove(iterator MBBI) {
314 BasicBlocks.remove(MBBI);
316 void erase(iterator MBBI) {
317 BasicBlocks.erase(MBBI);
320 //===--------------------------------------------------------------------===//
321 // Internal functions used to automatically number MachineBasicBlocks
324 /// getNextMBBNumber - Returns the next unique number to be assigned
325 /// to a MachineBasicBlock in this MachineFunction.
327 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
328 MBBNumbering.push_back(MBB);
329 return (unsigned)MBBNumbering.size()-1;
332 /// removeFromMBBNumbering - Remove the specific machine basic block from our
333 /// tracker, this is only really to be used by the MachineBasicBlock
335 void removeFromMBBNumbering(unsigned N) {
336 assert(N < MBBNumbering.size() && "Illegal basic block #");
340 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
341 /// of `new MachineInstr'.
343 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
347 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
348 /// 'Orig' instruction, identical in all ways except the instruction
349 /// has no parent, prev, or next.
351 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
353 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
355 /// DeleteMachineInstr - Delete the given MachineInstr.
357 void DeleteMachineInstr(MachineInstr *MI);
359 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
360 /// instead of `new MachineBasicBlock'.
362 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
364 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
366 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
368 /// getMachineMemOperand - Allocate a new MachineMemOperand.
369 /// MachineMemOperands are owned by the MachineFunction and need not be
370 /// explicitly deallocated.
371 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
372 int64_t o, uint64_t s,
373 unsigned base_alignment);
375 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
376 /// an existing one, adjusting by an offset and using the given size.
377 /// MachineMemOperands are owned by the MachineFunction and need not be
378 /// explicitly deallocated.
379 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
380 int64_t Offset, uint64_t Size);
382 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
383 /// pointers. This array is owned by the MachineFunction.
384 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
386 /// extractLoadMemRefs - Allocate an array and populate it with just the
387 /// load information from the given MachineMemOperand sequence.
388 std::pair<MachineInstr::mmo_iterator,
389 MachineInstr::mmo_iterator>
390 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
391 MachineInstr::mmo_iterator End);
393 /// extractStoreMemRefs - Allocate an array and populate it with just the
394 /// store information from the given MachineMemOperand sequence.
395 std::pair<MachineInstr::mmo_iterator,
396 MachineInstr::mmo_iterator>
397 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
398 MachineInstr::mmo_iterator End);
400 //===--------------------------------------------------------------------===//
401 // Label Manipulation.
404 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
405 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
406 /// normal 'L' label is returned.
407 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
408 bool isLinkerPrivate = false) const;
411 //===--------------------------------------------------------------------===//
412 // GraphTraits specializations for function basic block graphs (CFGs)
413 //===--------------------------------------------------------------------===//
415 // Provide specializations of GraphTraits to be able to treat a
416 // machine function as a graph of machine basic blocks... these are
417 // the same as the machine basic block iterators, except that the root
418 // node is implicitly the first node of the function.
420 template <> struct GraphTraits<MachineFunction*> :
421 public GraphTraits<MachineBasicBlock*> {
422 static NodeType *getEntryNode(MachineFunction *F) {
426 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
427 typedef MachineFunction::iterator nodes_iterator;
428 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
429 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
431 template <> struct GraphTraits<const MachineFunction*> :
432 public GraphTraits<const MachineBasicBlock*> {
433 static NodeType *getEntryNode(const MachineFunction *F) {
437 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
438 typedef MachineFunction::const_iterator nodes_iterator;
439 static nodes_iterator nodes_begin(const MachineFunction *F) {
442 static nodes_iterator nodes_end (const MachineFunction *F) {
448 // Provide specializations of GraphTraits to be able to treat a function as a
449 // graph of basic blocks... and to walk it in inverse order. Inverse order for
450 // a function is considered to be when traversing the predecessor edges of a BB
451 // instead of the successor edges.
453 template <> struct GraphTraits<Inverse<MachineFunction*> > :
454 public GraphTraits<Inverse<MachineBasicBlock*> > {
455 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
456 return &G.Graph->front();
459 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
460 public GraphTraits<Inverse<const MachineBasicBlock*> > {
461 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
462 return &G.Graph->front();
466 } // End llvm namespace