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 was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // 00011 // This file contains the declaration of the BasicBlock class. 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" 00020 00021 namespace llvm { 00022 00023 class TerminatorInst; 00024 template <class Term, class BB> class SuccIterator; // Successor Iterator 00025 template <class Ptr, class USE_iterator> class PredIterator; 00026 00027 template<> struct ilist_traits<Instruction> 00028 : public SymbolTableListTraits<Instruction, BasicBlock, Function> { 00029 // createSentinel is used to create a node that marks the end of the list... 00030 static Instruction *createSentinel(); 00031 static void destroySentinel(Instruction *I) { delete I; } 00032 static iplist<Instruction> &getList(BasicBlock *BB); 00033 }; 00034 00035 /// This represents a single basic block in LLVM. A basic block is simply a 00036 /// container of instructions that execute sequentially. Basic blocks are Values 00037 /// because they are referenced by instructions such as branches and switch 00038 /// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 00039 /// represents a label to which a branch can jump. 00040 /// 00041 /// A well formed basic block is formed of a list of non-terminating 00042 /// instructions followed by a single TerminatorInst instruction. 00043 /// TerminatorInst's may not occur in the middle of basic blocks, and must 00044 /// terminate the blocks. The BasicBlock class allows malformed basic blocks to 00045 /// occur because it may be useful in the intermediate stage of constructing or 00046 /// modifying a program. However, the verifier will ensure that basic blocks 00047 /// are "well formed". 00048 /// @brief LLVM Basic Block Representation 00049 class BasicBlock : public Value { // Basic blocks are data objects also 00050 public: 00051 typedef iplist<Instruction> InstListType; 00052 private : 00053 InstListType InstList; 00054 BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list 00055 00056 void setParent(Function *parent); 00057 void setNext(BasicBlock *N) { Next = N; } 00058 void setPrev(BasicBlock *N) { Prev = N; } 00059 friend class SymbolTableListTraits<BasicBlock, Function, Function>; 00060 00061 BasicBlock(const BasicBlock &); // Do not implement 00062 void operator=(const BasicBlock &); // Do not implement 00063 00064 public: 00065 /// Instruction iterators... 00066 typedef InstListType::iterator iterator; 00067 typedef InstListType::const_iterator const_iterator; 00068 00069 /// BasicBlock ctor - If the function parameter is specified, the basic block 00070 /// is automatically inserted at either the end of the function (if 00071 /// InsertBefore is null), or before the specified basic block. 00072 /// 00073 BasicBlock(const std::string &Name = "", Function *Parent = 0, 00074 BasicBlock *InsertBefore = 0); 00075 ~BasicBlock(); 00076 00077 /// getParent - Return the enclosing method, or null if none 00078 /// 00079 const Function *getParent() const { return InstList.getParent(); } 00080 Function *getParent() { return InstList.getParent(); } 00081 00082 // getNext/Prev - Return the next or previous basic block in the list. 00083 BasicBlock *getNext() { return Next; } 00084 const BasicBlock *getNext() const { return Next; } 00085 BasicBlock *getPrev() { return Prev; } 00086 const BasicBlock *getPrev() const { return Prev; } 00087 00088 /// getTerminator() - If this is a well formed basic block, then this returns 00089 /// a pointer to the terminator instruction. If it is not, then you get a 00090 /// null pointer back. 00091 /// 00092 TerminatorInst *getTerminator(); 00093 const TerminatorInst *const getTerminator() const; 00094 00095 /// Returns a pointer to the first instructon in this block that is not a 00096 /// PHINode instruction. When adding instruction to the beginning of the 00097 /// basic block, they should be added before the returned value, not before 00098 /// the first instruction, which might be PHI. 00099 /// Returns 0 is there's no non-PHI instruction. 00100 Instruction* getFirstNonPHI(); 00101 00102 /// removeFromParent - This method unlinks 'this' from the containing 00103 /// function, but does not delete it. 00104 /// 00105 void removeFromParent(); 00106 00107 /// eraseFromParent - This method unlinks 'this' from the containing function 00108 /// and deletes it. 00109 /// 00110 void eraseFromParent(); 00111 00112 /// moveBefore - Unlink this instruction from its current function and 00113 /// insert it into the function that MovePos lives in, right before 00114 /// MovePos. 00115 void moveBefore(BasicBlock *MovePos); 00116 00117 /// getSinglePredecessor - If this basic block has a single predecessor block, 00118 /// return the block, otherwise return a null pointer. 00119 BasicBlock *getSinglePredecessor(); 00120 const BasicBlock *getSinglePredecessor() const { 00121 return const_cast<BasicBlock*>(this)->getSinglePredecessor(); 00122 } 00123 00124 //===--------------------------------------------------------------------===// 00125 /// Instruction iterator methods 00126 /// 00127 inline iterator begin() { return InstList.begin(); } 00128 inline const_iterator begin() const { return InstList.begin(); } 00129 inline iterator end () { return InstList.end(); } 00130 inline const_iterator end () const { return InstList.end(); } 00131 00132 inline size_t size() const { return InstList.size(); } 00133 inline bool empty() const { return InstList.empty(); } 00134 inline const Instruction &front() const { return InstList.front(); } 00135 inline Instruction &front() { return InstList.front(); } 00136 inline const Instruction &back() const { return InstList.back(); } 00137 inline Instruction &back() { return InstList.back(); } 00138 00139 /// getInstList() - Return the underlying instruction list container. You 00140 /// need to access it directly if you want to modify it currently. 00141 /// 00142 const InstListType &getInstList() const { return InstList; } 00143 InstListType &getInstList() { return InstList; } 00144 00145 virtual void print(std::ostream &OS) const { print(OS, 0); } 00146 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00147 00148 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00149 static inline bool classof(const BasicBlock *) { return true; } 00150 static inline bool classof(const Value *V) { 00151 return V->getValueType() == Value::BasicBlockVal; 00152 } 00153 00154 /// dropAllReferences() - This function causes all the subinstructions to "let 00155 /// go" of all references that they are maintaining. This allows one to 00156 /// 'delete' a whole class at a time, even though there may be circular 00157 /// references... first all references are dropped, and all use counts go to 00158 /// zero. Then everything is delete'd for real. Note that no operations are 00159 /// valid on an object that has "dropped all references", except operator 00160 /// delete. 00161 /// 00162 void dropAllReferences(); 00163 00164 /// removePredecessor - This method is used to notify a BasicBlock that the 00165 /// specified Predecessor of the block is no longer able to reach it. This is 00166 /// actually not used to update the Predecessor list, but is actually used to 00167 /// update the PHI nodes that reside in the block. Note that this should be 00168 /// called while the predecessor still refers to this block. 00169 /// 00170 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); 00171 00172 /// splitBasicBlock - This splits a basic block into two at the specified 00173 /// instruction. Note that all instructions BEFORE the specified iterator 00174 /// stay as part of the original basic block, an unconditional branch is added 00175 /// to the original BB, and the rest of the instructions in the BB are moved 00176 /// to the new BB, including the old terminator. The newly formed BasicBlock 00177 /// is returned. This function invalidates the specified iterator. 00178 /// 00179 /// Note that this only works on well formed basic blocks (must have a 00180 /// terminator), and 'I' must not be the end of instruction list (which would 00181 /// cause a degenerate basic block to be formed, having a terminator inside of 00182 /// the basic block). 00183 /// 00184 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = ""); 00185 }; 00186 00187 } // End llvm namespace 00188 00189 #endif