LLVM API Documentation
00001 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- 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 // This file contains the declaration of the BasicBlock class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_BASICBLOCK_H 00015 #define LLVM_BASICBLOCK_H 00016 00017 #include "llvm/Instruction.h" 00018 #include "llvm/SymbolTableListTraits.h" 00019 #include "llvm/ADT/ilist.h" 00020 #include "llvm/Support/DataTypes.h" 00021 00022 namespace llvm { 00023 00024 class TerminatorInst; 00025 class LLVMContext; 00026 00027 template<> struct ilist_traits<Instruction> 00028 : public SymbolTableListTraits<Instruction, BasicBlock> { 00029 // createSentinel is used to get hold of a node that marks the end of 00030 // the list... 00031 // The sentinel is relative to this instance, so we use a non-static 00032 // method. 00033 Instruction *createSentinel() const { 00034 // since i(p)lists always publicly derive from the corresponding 00035 // traits, placing a data member in this class will augment i(p)list. 00036 // But since the NodeTy is expected to publicly derive from 00037 // ilist_node<NodeTy>, there is a legal viable downcast from it 00038 // to NodeTy. We use this trick to superpose i(p)list with a "ghostly" 00039 // NodeTy, which becomes the sentinel. Dereferencing the sentinel is 00040 // forbidden (save the ilist_node<NodeTy>) so no one will ever notice 00041 // the superposition. 00042 return static_cast<Instruction*>(&Sentinel); 00043 } 00044 static void destroySentinel(Instruction*) {} 00045 00046 Instruction *provideInitialHead() const { return createSentinel(); } 00047 Instruction *ensureHead(Instruction*) const { return createSentinel(); } 00048 static void noteHead(Instruction*, Instruction*) {} 00049 private: 00050 mutable ilist_node<Instruction> Sentinel; 00051 }; 00052 00053 /// This represents a single basic block in LLVM. A basic block is simply a 00054 /// container of instructions that execute sequentially. Basic blocks are Values 00055 /// because they are referenced by instructions such as branches and switch 00056 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 00057 /// represents a label to which a branch can jump. 00058 /// 00059 /// A well formed basic block is formed of a list of non-terminating 00060 /// instructions followed by a single TerminatorInst instruction. 00061 /// TerminatorInst's may not occur in the middle of basic blocks, and must 00062 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to 00063 /// occur because it may be useful in the intermediate stage of constructing or 00064 /// modifying a program. However, the verifier will ensure that basic blocks 00065 /// are "well formed". 00066 /// @brief LLVM Basic Block Representation 00067 class BasicBlock : public Value, // Basic blocks are data objects also 00068 public ilist_node<BasicBlock> { 00069 00070 public: 00071 typedef iplist<Instruction> InstListType; 00072 private: 00073 InstListType InstList; 00074 Function *Parent; 00075 00076 void setParent(Function *parent); 00077 friend class SymbolTableListTraits<BasicBlock, Function>; 00078 00079 BasicBlock(const BasicBlock &); // Do not implement 00080 void operator=(const BasicBlock &); // Do not implement 00081 00082 /// BasicBlock ctor - If the function parameter is specified, the basic block 00083 /// is automatically inserted at either the end of the function (if 00084 /// InsertBefore is null), or before the specified basic block. 00085 /// 00086 explicit BasicBlock(LLVMContext &C, const Twine &Name = "", 00087 Function *Parent = 0, BasicBlock *InsertBefore = 0); 00088 public: 00089 /// getContext - Get the context in which this basic block lives. 00090 LLVMContext &getContext() const; 00091 00092 /// Instruction iterators... 00093 typedef InstListType::iterator iterator; 00094 typedef InstListType::const_iterator const_iterator; 00095 00096 /// Create - Creates a new BasicBlock. If the Parent parameter is specified, 00097 /// the basic block is automatically inserted at either the end of the 00098 /// function (if InsertBefore is 0), or before the specified basic block. 00099 static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", 00100 Function *Parent = 0,BasicBlock *InsertBefore = 0) { 00101 return new BasicBlock(Context, Name, Parent, InsertBefore); 00102 } 00103 ~BasicBlock(); 00104 00105 /// getParent - Return the enclosing method, or null if none 00106 /// 00107 const Function *getParent() const { return Parent; } 00108 Function *getParent() { return Parent; } 00109 00110 /// use_back - Specialize the methods defined in Value, as we know that an 00111 /// BasicBlock can only be used by Instructions (specifically PHI nodes and 00112 /// terminators). 00113 Instruction *use_back() { return cast<Instruction>(*use_begin());} 00114 const Instruction *use_back() const { return cast<Instruction>(*use_begin());} 00115 00116 /// getTerminator() - If this is a well formed basic block, then this returns 00117 /// a pointer to the terminator instruction. If it is not, then you get a 00118 /// null pointer back. 00119 /// 00120 TerminatorInst *getTerminator(); 00121 const TerminatorInst *getTerminator() const; 00122 00123 /// Returns a pointer to the first instructon in this block that is not a 00124 /// PHINode instruction. When adding instruction to the beginning of the 00125 /// basic block, they should be added before the returned value, not before 00126 /// the first instruction, which might be PHI. 00127 /// Returns 0 is there's no non-PHI instruction. 00128 Instruction* getFirstNonPHI(); 00129 const Instruction* getFirstNonPHI() const { 00130 return const_cast<BasicBlock*>(this)->getFirstNonPHI(); 00131 } 00132 00133 /// removeFromParent - This method unlinks 'this' from the containing 00134 /// function, but does not delete it. 00135 /// 00136 void removeFromParent(); 00137 00138 /// eraseFromParent - This method unlinks 'this' from the containing function 00139 /// and deletes it. 00140 /// 00141 void eraseFromParent(); 00142 00143 /// moveBefore - Unlink this basic block from its current function and 00144 /// insert it into the function that MovePos lives in, right before MovePos. 00145 void moveBefore(BasicBlock *MovePos); 00146 00147 /// moveAfter - Unlink this basic block from its current function and 00148 /// insert it into the function that MovePos lives in, right after MovePos. 00149 void moveAfter(BasicBlock *MovePos); 00150 00151 00152 /// getSinglePredecessor - If this basic block has a single predecessor block, 00153 /// return the block, otherwise return a null pointer. 00154 BasicBlock *getSinglePredecessor(); 00155 const BasicBlock *getSinglePredecessor() const { 00156 return const_cast<BasicBlock*>(this)->getSinglePredecessor(); 00157 } 00158 00159 /// getUniquePredecessor - If this basic block has a unique predecessor block, 00160 /// return the block, otherwise return a null pointer. 00161 /// Note that unique predecessor doesn't mean single edge, there can be 00162 /// multiple edges from the unique predecessor to this block (for example 00163 /// a switch statement with multiple cases having the same destination). 00164 BasicBlock *getUniquePredecessor(); 00165 const BasicBlock *getUniquePredecessor() const { 00166 return const_cast<BasicBlock*>(this)->getUniquePredecessor(); 00167 } 00168 00169 //===--------------------------------------------------------------------===// 00170 /// Instruction iterator methods 00171 /// 00172 inline iterator begin() { return InstList.begin(); } 00173 inline const_iterator begin() const { return InstList.begin(); } 00174 inline iterator end () { return InstList.end(); } 00175 inline const_iterator end () const { return InstList.end(); } 00176 00177 inline size_t size() const { return InstList.size(); } 00178 inline bool empty() const { return InstList.empty(); } 00179 inline const Instruction &front() const { return InstList.front(); } 00180 inline Instruction &front() { return InstList.front(); } 00181 inline const Instruction &back() const { return InstList.back(); } 00182 inline Instruction &back() { return InstList.back(); } 00183 00184 /// getInstList() - Return the underlying instruction list container. You 00185 /// need to access it directly if you want to modify it currently. 00186 /// 00187 const InstListType &getInstList() const { return InstList; } 00188 InstListType &getInstList() { return InstList; } 00189 00190 /// getSublistAccess() - returns pointer to member of instruction list 00191 static iplist<Instruction> BasicBlock::*getSublistAccess(Instruction*) { 00192 return &BasicBlock::InstList; 00193 } 00194 00195 /// getValueSymbolTable() - returns pointer to symbol table (if any) 00196 ValueSymbolTable *getValueSymbolTable(); 00197 00198 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00199 static inline bool classof(const BasicBlock *) { return true; } 00200 static inline bool classof(const Value *V) { 00201 return V->getValueID() == Value::BasicBlockVal; 00202 } 00203 00204 /// dropAllReferences() - This function causes all the subinstructions to "let 00205 /// go" of all references that they are maintaining. This allows one to 00206 /// 'delete' a whole class at a time, even though there may be circular 00207 /// references... first all references are dropped, and all use counts go to 00208 /// zero. Then everything is delete'd for real. Note that no operations are 00209 /// valid on an object that has "dropped all references", except operator 00210 /// delete. 00211 /// 00212 void dropAllReferences(); 00213 00214 /// removePredecessor - This method is used to notify a BasicBlock that the 00215 /// specified Predecessor of the block is no longer able to reach it. This is 00216 /// actually not used to update the Predecessor list, but is actually used to 00217 /// update the PHI nodes that reside in the block. Note that this should be 00218 /// called while the predecessor still refers to this block. 00219 /// 00220 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); 00221 00222 /// splitBasicBlock - This splits a basic block into two at the specified 00223 /// instruction. Note that all instructions BEFORE the specified iterator 00224 /// stay as part of the original basic block, an unconditional branch is added 00225 /// to the original BB, and the rest of the instructions in the BB are moved 00226 /// to the new BB, including the old terminator. The newly formed BasicBlock 00227 /// is returned. This function invalidates the specified iterator. 00228 /// 00229 /// Note that this only works on well formed basic blocks (must have a 00230 /// terminator), and 'I' must not be the end of instruction list (which would 00231 /// cause a degenerate basic block to be formed, having a terminator inside of 00232 /// the basic block). 00233 /// 00234 BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = ""); 00235 }; 00236 00237 } // End llvm namespace 00238 00239 #endif