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;
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 /// FunctionNumber - This provides a unique ID for each function emitted in
115 /// this translation unit.
117 unsigned FunctionNumber;
119 // The alignment of the function.
122 MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
123 void operator=(const MachineFunction&); // DO NOT IMPLEMENT
126 MachineFunction(Function *Fn, const TargetMachine &TM, unsigned FunctionNum,
130 MCContext &getContext() const { return Ctx; }
132 /// getFunction - Return the LLVM function that this machine code represents
134 Function *getFunction() const { return Fn; }
136 /// getFunctionNumber - Return a unique ID for the current function.
138 unsigned getFunctionNumber() const { return FunctionNumber; }
140 /// getTarget - Return the target machine this machine code is compiled with
142 const TargetMachine &getTarget() const { return Target; }
144 /// getRegInfo - Return information about the registers currently in use.
146 MachineRegisterInfo &getRegInfo() { return *RegInfo; }
147 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
149 /// getFrameInfo - Return the frame info object for the current function.
150 /// This object contains information about objects allocated on the stack
151 /// frame of the current function in an abstract way.
153 MachineFrameInfo *getFrameInfo() { return FrameInfo; }
154 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
156 /// getJumpTableInfo - Return the jump table info object for the current
157 /// function. This object contains information about jump tables in the
158 /// current function. If the current function has no jump tables, this will
160 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
161 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
163 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
164 /// does already exist, allocate one.
165 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
168 /// getConstantPool - Return the constant pool object for the current
171 MachineConstantPool *getConstantPool() { return ConstantPool; }
172 const MachineConstantPool *getConstantPool() const { return ConstantPool; }
174 /// getAlignment - Return the alignment (log2, not bytes) of the function.
176 unsigned getAlignment() const { return Alignment; }
178 /// setAlignment - Set the alignment (log2, not bytes) of the function.
180 void setAlignment(unsigned A) { Alignment = A; }
182 /// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
183 void EnsureAlignment(unsigned A) {
184 if (Alignment < A) Alignment = A;
187 /// getInfo - Keep track of various per-function pieces of information for
188 /// backends that would like to do so.
190 template<typename Ty>
193 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
194 // that apparently breaks GCC 3.3.
195 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
196 AlignOf<Ty>::Alignment));
197 MFInfo = new (Loc) Ty(*this);
199 return static_cast<Ty*>(MFInfo);
202 template<typename Ty>
203 const Ty *getInfo() const {
204 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
207 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
208 /// are inserted into the machine function. The block number for a machine
209 /// basic block can be found by using the MBB::getBlockNumber method, this
210 /// method provides the inverse mapping.
212 MachineBasicBlock *getBlockNumbered(unsigned N) const {
213 assert(N < MBBNumbering.size() && "Illegal block number");
214 assert(MBBNumbering[N] && "Block was removed from the machine function!");
215 return MBBNumbering[N];
218 /// getNumBlockIDs - Return the number of MBB ID's allocated.
220 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
222 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
223 /// recomputes them. This guarantees that the MBB numbers are sequential,
224 /// dense, and match the ordering of the blocks within the function. If a
225 /// specific MachineBasicBlock is specified, only that block and those after
226 /// it are renumbered.
227 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
229 /// print - Print out the MachineFunction in a format suitable for debugging
230 /// to the specified stream.
232 void print(raw_ostream &OS) const;
234 /// viewCFG - This function is meant for use from the debugger. You can just
235 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
236 /// program, displaying the CFG of the current function with the code for each
237 /// basic block inside. This depends on there being a 'dot' and 'gv' program
240 void viewCFG() const;
242 /// viewCFGOnly - This function is meant for use from the debugger. It works
243 /// just like viewCFG, but it does not include the contents of basic blocks
244 /// into the nodes, just the label. If you are only interested in the CFG
245 /// this can make the graph smaller.
247 void viewCFGOnly() const;
249 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
253 /// verify - Run the current MachineFunction through the machine code
254 /// verifier, useful for debugger use.
255 void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
257 // Provide accessors for the MachineBasicBlock list...
258 typedef BasicBlockListType::iterator iterator;
259 typedef BasicBlockListType::const_iterator const_iterator;
260 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
261 typedef std::reverse_iterator<iterator> reverse_iterator;
263 /// addLiveIn - Add the specified physical register as a live-in value and
264 /// create a corresponding virtual register for it.
265 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
267 //===--------------------------------------------------------------------===//
268 // BasicBlock accessor functions.
270 iterator begin() { return BasicBlocks.begin(); }
271 const_iterator begin() const { return BasicBlocks.begin(); }
272 iterator end () { return BasicBlocks.end(); }
273 const_iterator end () const { return BasicBlocks.end(); }
275 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
276 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
277 reverse_iterator rend () { return BasicBlocks.rend(); }
278 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
280 unsigned size() const { return (unsigned)BasicBlocks.size();}
281 bool empty() const { return BasicBlocks.empty(); }
282 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
283 MachineBasicBlock &front() { return BasicBlocks.front(); }
284 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
285 MachineBasicBlock & back() { return BasicBlocks.back(); }
287 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
288 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
289 void insert(iterator MBBI, MachineBasicBlock *MBB) {
290 BasicBlocks.insert(MBBI, MBB);
292 void splice(iterator InsertPt, iterator MBBI) {
293 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
295 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
296 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
299 void remove(iterator MBBI) {
300 BasicBlocks.remove(MBBI);
302 void erase(iterator MBBI) {
303 BasicBlocks.erase(MBBI);
306 //===--------------------------------------------------------------------===//
307 // Internal functions used to automatically number MachineBasicBlocks
310 /// getNextMBBNumber - Returns the next unique number to be assigned
311 /// to a MachineBasicBlock in this MachineFunction.
313 unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
314 MBBNumbering.push_back(MBB);
315 return (unsigned)MBBNumbering.size()-1;
318 /// removeFromMBBNumbering - Remove the specific machine basic block from our
319 /// tracker, this is only really to be used by the MachineBasicBlock
321 void removeFromMBBNumbering(unsigned N) {
322 assert(N < MBBNumbering.size() && "Illegal basic block #");
326 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
327 /// of `new MachineInstr'.
329 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
333 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
334 /// 'Orig' instruction, identical in all ways except the instruction
335 /// has no parent, prev, or next.
337 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
339 MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
341 /// DeleteMachineInstr - Delete the given MachineInstr.
343 void DeleteMachineInstr(MachineInstr *MI);
345 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
346 /// instead of `new MachineBasicBlock'.
348 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
350 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
352 void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
354 /// getMachineMemOperand - Allocate a new MachineMemOperand.
355 /// MachineMemOperands are owned by the MachineFunction and need not be
356 /// explicitly deallocated.
357 MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
358 int64_t o, uint64_t s,
359 unsigned base_alignment);
361 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying
362 /// an existing one, adjusting by an offset and using the given size.
363 /// MachineMemOperands are owned by the MachineFunction and need not be
364 /// explicitly deallocated.
365 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
366 int64_t Offset, uint64_t Size);
368 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
369 /// pointers. This array is owned by the MachineFunction.
370 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
372 /// extractLoadMemRefs - Allocate an array and populate it with just the
373 /// load information from the given MachineMemOperand sequence.
374 std::pair<MachineInstr::mmo_iterator,
375 MachineInstr::mmo_iterator>
376 extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
377 MachineInstr::mmo_iterator End);
379 /// extractStoreMemRefs - Allocate an array and populate it with just the
380 /// store information from the given MachineMemOperand sequence.
381 std::pair<MachineInstr::mmo_iterator,
382 MachineInstr::mmo_iterator>
383 extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
384 MachineInstr::mmo_iterator End);
386 //===--------------------------------------------------------------------===//
387 // Label Manipulation.
390 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
391 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
392 /// normal 'L' label is returned.
393 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
394 bool isLinkerPrivate = false) const;
397 //===--------------------------------------------------------------------===//
401 /// getDefaultDebugLoc - Get the default debug location for the machine
403 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
405 /// setDefaultDebugLoc - Get the default debug location for the machine
407 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
410 //===--------------------------------------------------------------------===//
411 // GraphTraits specializations for function basic block graphs (CFGs)
412 //===--------------------------------------------------------------------===//
414 // Provide specializations of GraphTraits to be able to treat a
415 // machine function as a graph of machine basic blocks... these are
416 // the same as the machine basic block iterators, except that the root
417 // node is implicitly the first node of the function.
419 template <> struct GraphTraits<MachineFunction*> :
420 public GraphTraits<MachineBasicBlock*> {
421 static NodeType *getEntryNode(MachineFunction *F) {
425 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
426 typedef MachineFunction::iterator nodes_iterator;
427 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
428 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
430 template <> struct GraphTraits<const MachineFunction*> :
431 public GraphTraits<const MachineBasicBlock*> {
432 static NodeType *getEntryNode(const MachineFunction *F) {
436 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
437 typedef MachineFunction::const_iterator nodes_iterator;
438 static nodes_iterator nodes_begin(const MachineFunction *F) {
441 static nodes_iterator nodes_end (const MachineFunction *F) {
447 // Provide specializations of GraphTraits to be able to treat a function as a
448 // graph of basic blocks... and to walk it in inverse order. Inverse order for
449 // a function is considered to be when traversing the predecessor edges of a BB
450 // instead of the successor edges.
452 template <> struct GraphTraits<Inverse<MachineFunction*> > :
453 public GraphTraits<Inverse<MachineBasicBlock*> > {
454 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
455 return &G.Graph->front();
458 template <> struct GraphTraits<Inverse<const MachineFunction*> > :
459 public GraphTraits<Inverse<const MachineBasicBlock*> > {
460 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
461 return &G.Graph->front();
465 } // End llvm namespace