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