LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Collect native machine code for a function. This class contains a list of 00011 // MachineBasicBlock instances that make up the current compiled function. 00012 // 00013 // This class also contains pointers to various classes which hold 00014 // target-specific information about the generated code. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H 00019 #define LLVM_CODEGEN_MACHINEFUNCTION_H 00020 00021 #include <map> 00022 #include "llvm/ADT/ilist.h" 00023 #include "llvm/Support/DebugLoc.h" 00024 #include "llvm/CodeGen/Dump.h" 00025 #include "llvm/CodeGen/MachineBasicBlock.h" 00026 #include "llvm/Support/Allocator.h" 00027 #include "llvm/Support/Recycler.h" 00028 00029 namespace llvm { 00030 00031 class Function; 00032 class MachineRegisterInfo; 00033 class MachineFrameInfo; 00034 class MachineConstantPool; 00035 class MachineJumpTableInfo; 00036 class TargetMachine; 00037 class TargetRegisterClass; 00038 00039 template <> 00040 struct ilist_traits<MachineBasicBlock> 00041 : public ilist_default_traits<MachineBasicBlock> { 00042 mutable ilist_node<MachineBasicBlock> Sentinel; 00043 public: 00044 MachineBasicBlock *createSentinel() const { 00045 return static_cast<MachineBasicBlock*>(&Sentinel); 00046 } 00047 void destroySentinel(MachineBasicBlock *) const {} 00048 00049 MachineBasicBlock *provideInitialHead() const { return createSentinel(); } 00050 MachineBasicBlock *ensureHead(MachineBasicBlock*) const { 00051 return createSentinel(); 00052 } 00053 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {} 00054 00055 void addNodeToList(MachineBasicBlock* MBB); 00056 void removeNodeFromList(MachineBasicBlock* MBB); 00057 void deleteNode(MachineBasicBlock *MBB); 00058 private: 00059 void createNode(const MachineBasicBlock &); 00060 }; 00061 00062 /// MachineFunctionInfo - This class can be derived from and used by targets to 00063 /// hold private target-specific information for each MachineFunction. Objects 00064 /// of type are accessed/created with MF::getInfo and destroyed when the 00065 /// MachineFunction is destroyed. 00066 struct MachineFunctionInfo { 00067 virtual ~MachineFunctionInfo() {} 00068 }; 00069 00070 class MachineFunction { 00071 Function *Fn; 00072 const TargetMachine &Target; 00073 00074 // RegInfo - Information about each register in use in the function. 00075 MachineRegisterInfo *RegInfo; 00076 00077 // Used to keep track of target-specific per-machine function information for 00078 // the target implementation. 00079 MachineFunctionInfo *MFInfo; 00080 00081 // Keep track of objects allocated on the stack. 00082 MachineFrameInfo *FrameInfo; 00083 00084 // Keep track of constants which are spilled to memory 00085 MachineConstantPool *ConstantPool; 00086 00087 // Keep track of jump tables for switch instructions 00088 MachineJumpTableInfo *JumpTableInfo; 00089 00090 // Function-level unique numbering for MachineBasicBlocks. When a 00091 // MachineBasicBlock is inserted into a MachineFunction is it automatically 00092 // numbered and this vector keeps track of the mapping from ID's to MBB's. 00093 std::vector<MachineBasicBlock*> MBBNumbering; 00094 00095 // Pool-allocate MachineFunction-lifetime and IR objects. 00096 BumpPtrAllocator Allocator; 00097 00098 // Allocation management for instructions in function. 00099 Recycler<MachineInstr> InstructionRecycler; 00100 00101 // Allocation management for basic blocks in function. 00102 Recycler<MachineBasicBlock> BasicBlockRecycler; 00103 00104 // List of machine basic blocks in function 00105 typedef ilist<MachineBasicBlock> BasicBlockListType; 00106 BasicBlockListType BasicBlocks; 00107 00108 // Default debug location. Used to print out the debug label at the beginning 00109 // of a function. 00110 DebugLoc DefaultDebugLoc; 00111 00112 // Tracks debug locations. 00113 DebugLocTracker DebugLocInfo; 00114 00115 // The alignment of the function. 00116 unsigned Alignment; 00117 00118 public: 00119 MachineFunction(Function *Fn, const TargetMachine &TM); 00120 ~MachineFunction(); 00121 00122 /// getFunction - Return the LLVM function that this machine code represents 00123 /// 00124 Function *getFunction() const { return Fn; } 00125 00126 /// getTarget - Return the target machine this machine code is compiled with 00127 /// 00128 const TargetMachine &getTarget() const { return Target; } 00129 00130 /// getRegInfo - Return information about the registers currently in use. 00131 /// 00132 MachineRegisterInfo &getRegInfo() { return *RegInfo; } 00133 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; } 00134 00135 /// getFrameInfo - Return the frame info object for the current function. 00136 /// This object contains information about objects allocated on the stack 00137 /// frame of the current function in an abstract way. 00138 /// 00139 MachineFrameInfo *getFrameInfo() { return FrameInfo; } 00140 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; } 00141 00142 /// getJumpTableInfo - Return the jump table info object for the current 00143 /// function. This object contains information about jump tables for switch 00144 /// instructions in the current function. 00145 /// 00146 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; } 00147 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; } 00148 00149 /// getConstantPool - Return the constant pool object for the current 00150 /// function. 00151 /// 00152 MachineConstantPool *getConstantPool() { return ConstantPool; } 00153 const MachineConstantPool *getConstantPool() const { return ConstantPool; } 00154 00155 /// getAlignment - Return the alignment (log2, not bytes) of the function. 00156 /// 00157 unsigned getAlignment() const { return Alignment; } 00158 00159 /// setAlignment - Set the alignment (log2, not bytes) of the function. 00160 /// 00161 void setAlignment(unsigned A) { Alignment = A; } 00162 00163 /// MachineFunctionInfo - Keep track of various per-function pieces of 00164 /// information for backends that would like to do so. 00165 /// 00166 template<typename Ty> 00167 Ty *getInfo() { 00168 if (!MFInfo) { 00169 // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but 00170 // that apparently breaks GCC 3.3. 00171 Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty), 00172 AlignOf<Ty>::Alignment)); 00173 MFInfo = new (Loc) Ty(*this); 00174 } 00175 00176 assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo && 00177 "Invalid concrete type or multiple inheritence for getInfo"); 00178 return static_cast<Ty*>(MFInfo); 00179 } 00180 00181 template<typename Ty> 00182 const Ty *getInfo() const { 00183 return const_cast<MachineFunction*>(this)->getInfo<Ty>(); 00184 } 00185 00186 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they 00187 /// are inserted into the machine function. The block number for a machine 00188 /// basic block can be found by using the MBB::getBlockNumber method, this 00189 /// method provides the inverse mapping. 00190 /// 00191 MachineBasicBlock *getBlockNumbered(unsigned N) const { 00192 assert(N < MBBNumbering.size() && "Illegal block number"); 00193 assert(MBBNumbering[N] && "Block was removed from the machine function!"); 00194 return MBBNumbering[N]; 00195 } 00196 00197 /// getNumBlockIDs - Return the number of MBB ID's allocated. 00198 /// 00199 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); } 00200 00201 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 00202 /// recomputes them. This guarantees that the MBB numbers are sequential, 00203 /// dense, and match the ordering of the blocks within the function. If a 00204 /// specific MachineBasicBlock is specified, only that block and those after 00205 /// it are renumbered. 00206 void RenumberBlocks(MachineBasicBlock *MBBFrom = 0); 00207 00208 /// print - Print out the MachineFunction in a format suitable for debugging 00209 /// to the specified stream. 00210 /// 00211 void print(std::ostream &OS, 00212 const PrefixPrinter &prefix = PrefixPrinter()) const; 00213 void print(std::ostream *OS, 00214 const PrefixPrinter &prefix = PrefixPrinter()) const { 00215 if (OS) print(*OS, prefix); 00216 } 00217 00218 /// viewCFG - This function is meant for use from the debugger. You can just 00219 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 00220 /// program, displaying the CFG of the current function with the code for each 00221 /// basic block inside. This depends on there being a 'dot' and 'gv' program 00222 /// in your path. 00223 /// 00224 void viewCFG() const; 00225 00226 /// viewCFGOnly - This function is meant for use from the debugger. It works 00227 /// just like viewCFG, but it does not include the contents of basic blocks 00228 /// into the nodes, just the label. If you are only interested in the CFG 00229 /// this can make the graph smaller. 00230 /// 00231 void viewCFGOnly() const; 00232 00233 /// dump - Print the current MachineFunction to cerr, useful for debugger use. 00234 /// 00235 void dump() const; 00236 00237 // Provide accessors for the MachineBasicBlock list... 00238 typedef BasicBlockListType::iterator iterator; 00239 typedef BasicBlockListType::const_iterator const_iterator; 00240 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00241 typedef std::reverse_iterator<iterator> reverse_iterator; 00242 00243 /// addLiveIn - Add the specified physical register as a live-in value and 00244 /// create a corresponding virtual register for it. 00245 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC); 00246 00247 //===--------------------------------------------------------------------===// 00248 // BasicBlock accessor functions. 00249 // 00250 iterator begin() { return BasicBlocks.begin(); } 00251 const_iterator begin() const { return BasicBlocks.begin(); } 00252 iterator end () { return BasicBlocks.end(); } 00253 const_iterator end () const { return BasicBlocks.end(); } 00254 00255 reverse_iterator rbegin() { return BasicBlocks.rbegin(); } 00256 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } 00257 reverse_iterator rend () { return BasicBlocks.rend(); } 00258 const_reverse_iterator rend () const { return BasicBlocks.rend(); } 00259 00260 unsigned size() const { return (unsigned)BasicBlocks.size();} 00261 bool empty() const { return BasicBlocks.empty(); } 00262 const MachineBasicBlock &front() const { return BasicBlocks.front(); } 00263 MachineBasicBlock &front() { return BasicBlocks.front(); } 00264 const MachineBasicBlock & back() const { return BasicBlocks.back(); } 00265 MachineBasicBlock & back() { return BasicBlocks.back(); } 00266 00267 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); } 00268 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); } 00269 void insert(iterator MBBI, MachineBasicBlock *MBB) { 00270 BasicBlocks.insert(MBBI, MBB); 00271 } 00272 void splice(iterator InsertPt, iterator MBBI) { 00273 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI); 00274 } 00275 00276 void remove(iterator MBBI) { 00277 BasicBlocks.remove(MBBI); 00278 } 00279 void erase(iterator MBBI) { 00280 BasicBlocks.erase(MBBI); 00281 } 00282 00283 //===--------------------------------------------------------------------===// 00284 // Internal functions used to automatically number MachineBasicBlocks 00285 // 00286 00287 /// getNextMBBNumber - Returns the next unique number to be assigned 00288 /// to a MachineBasicBlock in this MachineFunction. 00289 /// 00290 unsigned addToMBBNumbering(MachineBasicBlock *MBB) { 00291 MBBNumbering.push_back(MBB); 00292 return (unsigned)MBBNumbering.size()-1; 00293 } 00294 00295 /// removeFromMBBNumbering - Remove the specific machine basic block from our 00296 /// tracker, this is only really to be used by the MachineBasicBlock 00297 /// implementation. 00298 void removeFromMBBNumbering(unsigned N) { 00299 assert(N < MBBNumbering.size() && "Illegal basic block #"); 00300 MBBNumbering[N] = 0; 00301 } 00302 00303 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 00304 /// of `new MachineInstr'. 00305 /// 00306 MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID, 00307 DebugLoc DL, 00308 bool NoImp = false); 00309 00310 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the 00311 /// 'Orig' instruction, identical in all ways except the the instruction 00312 /// has no parent, prev, or next. 00313 /// 00314 MachineInstr *CloneMachineInstr(const MachineInstr *Orig); 00315 00316 /// DeleteMachineInstr - Delete the given MachineInstr. 00317 /// 00318 void DeleteMachineInstr(MachineInstr *MI); 00319 00320 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 00321 /// instead of `new MachineBasicBlock'. 00322 /// 00323 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0); 00324 00325 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 00326 /// 00327 void DeleteMachineBasicBlock(MachineBasicBlock *MBB); 00328 00329 //===--------------------------------------------------------------------===// 00330 // Debug location. 00331 // 00332 00333 /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given 00334 /// source file, line, and column. If none currently exists, create a new 00335 /// DebugLocTuple, and insert it into the DebugIdMap. 00336 unsigned getOrCreateDebugLocID(GlobalVariable *CompileUnit, 00337 unsigned Line, unsigned Col); 00338 00339 /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object. 00340 DebugLocTuple getDebugLocTuple(DebugLoc DL) const; 00341 00342 /// getDefaultDebugLoc - Get the default debug location for the machine 00343 /// function. 00344 DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; } 00345 00346 /// setDefaultDebugLoc - Get the default debug location for the machine 00347 /// function. 00348 void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; } 00349 00350 /// getDebugLocInfo - Get the debug info location tracker. 00351 DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; } 00352 }; 00353 00354 //===--------------------------------------------------------------------===// 00355 // GraphTraits specializations for function basic block graphs (CFGs) 00356 //===--------------------------------------------------------------------===// 00357 00358 // Provide specializations of GraphTraits to be able to treat a 00359 // machine function as a graph of machine basic blocks... these are 00360 // the same as the machine basic block iterators, except that the root 00361 // node is implicitly the first node of the function. 00362 // 00363 template <> struct GraphTraits<MachineFunction*> : 00364 public GraphTraits<MachineBasicBlock*> { 00365 static NodeType *getEntryNode(MachineFunction *F) { 00366 return &F->front(); 00367 } 00368 00369 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 00370 typedef MachineFunction::iterator nodes_iterator; 00371 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } 00372 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } 00373 }; 00374 template <> struct GraphTraits<const MachineFunction*> : 00375 public GraphTraits<const MachineBasicBlock*> { 00376 static NodeType *getEntryNode(const MachineFunction *F) { 00377 return &F->front(); 00378 } 00379 00380 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 00381 typedef MachineFunction::const_iterator nodes_iterator; 00382 static nodes_iterator nodes_begin(const MachineFunction *F) { 00383 return F->begin(); 00384 } 00385 static nodes_iterator nodes_end (const MachineFunction *F) { 00386 return F->end(); 00387 } 00388 }; 00389 00390 00391 // Provide specializations of GraphTraits to be able to treat a function as a 00392 // graph of basic blocks... and to walk it in inverse order. Inverse order for 00393 // a function is considered to be when traversing the predecessor edges of a BB 00394 // instead of the successor edges. 00395 // 00396 template <> struct GraphTraits<Inverse<MachineFunction*> > : 00397 public GraphTraits<Inverse<MachineBasicBlock*> > { 00398 static NodeType *getEntryNode(Inverse<MachineFunction*> G) { 00399 return &G.Graph->front(); 00400 } 00401 }; 00402 template <> struct GraphTraits<Inverse<const MachineFunction*> > : 00403 public GraphTraits<Inverse<const MachineBasicBlock*> > { 00404 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { 00405 return &G.Graph->front(); 00406 } 00407 }; 00408 00409 } // End llvm namespace 00410 00411 #endif