BasicBlock.h revision 881765af0acd2e05f8b01fd3b3b05a3cea03038b
1//===-- llvm/BasicBlock.h - Represent a basic block in the VM ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// 11// This file contains the declaration of the BasicBlock class. 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_BASICBLOCK_H 15#define LLVM_BASICBLOCK_H 16 17#include "llvm/Instruction.h" 18#include "llvm/SymbolTableListTraits.h" 19#include "llvm/ADT/ilist" 20 21namespace llvm { 22 23class TerminatorInst; 24template <class Term, class BB> class SuccIterator; // Successor Iterator 25template <class Ptr, class USE_iterator> class PredIterator; 26 27template<> struct ilist_traits<Instruction> 28 : public SymbolTableListTraits<Instruction, BasicBlock> { 29 // createSentinel is used to create a node that marks the end of the list... 30 static Instruction *createSentinel(); 31 static void destroySentinel(Instruction *I) { delete I; } 32 static iplist<Instruction> &getList(BasicBlock *BB); 33 static ValueSymbolTable *getSymTab(BasicBlock *ItemParent); 34 static int getListOffset(); 35}; 36 37/// This represents a single basic block in LLVM. A basic block is simply a 38/// container of instructions that execute sequentially. Basic blocks are Values 39/// because they are referenced by instructions such as branches and switch 40/// tables. The type of a BasicBlock is "Type::LabelTy" because the basic block 41/// represents a label to which a branch can jump. 42/// 43/// A well formed basic block is formed of a list of non-terminating 44/// instructions followed by a single TerminatorInst instruction. 45/// TerminatorInst's may not occur in the middle of basic blocks, and must 46/// terminate the blocks. The BasicBlock class allows malformed basic blocks to 47/// occur because it may be useful in the intermediate stage of constructing or 48/// modifying a program. However, the verifier will ensure that basic blocks 49/// are "well formed". 50/// @brief LLVM Basic Block Representation 51class BasicBlock : public Value { // Basic blocks are data objects also 52public: 53 typedef iplist<Instruction> InstListType; 54private : 55 InstListType InstList; 56 BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list 57 Function *Parent; 58 59 void setParent(Function *parent); 60 void setNext(BasicBlock *N) { Next = N; } 61 void setPrev(BasicBlock *N) { Prev = N; } 62 friend class SymbolTableListTraits<BasicBlock, Function>; 63 64 BasicBlock(const BasicBlock &); // Do not implement 65 void operator=(const BasicBlock &); // Do not implement 66 67public: 68 /// Instruction iterators... 69 typedef InstListType::iterator iterator; 70 typedef InstListType::const_iterator const_iterator; 71 72 /// BasicBlock ctor - If the function parameter is specified, the basic block 73 /// is automatically inserted at either the end of the function (if 74 /// InsertBefore is null), or before the specified basic block. 75 /// 76 explicit BasicBlock(const std::string &Name = "", Function *Parent = 0, 77 BasicBlock *InsertBefore = 0); 78 ~BasicBlock(); 79 80 /// getParent - Return the enclosing method, or null if none 81 /// 82 const Function *getParent() const { return Parent; } 83 Function *getParent() { return Parent; } 84 85 /// use_back - Specialize the methods defined in Value, as we know that an 86 /// BasicBlock can only be used by Instructions (specifically PHI and terms). 87 Instruction *use_back() { return cast<Instruction>(*use_begin());} 88 const Instruction *use_back() const { return cast<Instruction>(*use_begin());} 89 90 /// getTerminator() - If this is a well formed basic block, then this returns 91 /// a pointer to the terminator instruction. If it is not, then you get a 92 /// null pointer back. 93 /// 94 TerminatorInst *getTerminator(); 95 const TerminatorInst *const getTerminator() const; 96 97 /// Returns a pointer to the first instructon in this block that is not a 98 /// PHINode instruction. When adding instruction to the beginning of the 99 /// basic block, they should be added before the returned value, not before 100 /// the first instruction, which might be PHI. 101 /// Returns 0 is there's no non-PHI instruction. 102 Instruction* getFirstNonPHI(); 103 104 /// removeFromParent - This method unlinks 'this' from the containing 105 /// function, but does not delete it. 106 /// 107 void removeFromParent(); 108 109 /// eraseFromParent - This method unlinks 'this' from the containing function 110 /// and deletes it. 111 /// 112 void eraseFromParent(); 113 114 /// moveBefore - Unlink this basic block from its current function and 115 /// insert it into the function that MovePos lives in, right before MovePos. 116 void moveBefore(BasicBlock *MovePos); 117 118 /// moveAfter - Unlink this basic block from its current function and 119 /// insert it into the function that MovePos lives in, right after MovePos. 120 void moveAfter(BasicBlock *MovePos); 121 122 123 /// getSinglePredecessor - If this basic block has a single predecessor block, 124 /// return the block, otherwise return a null pointer. 125 BasicBlock *getSinglePredecessor(); 126 const BasicBlock *getSinglePredecessor() const { 127 return const_cast<BasicBlock*>(this)->getSinglePredecessor(); 128 } 129 130 //===--------------------------------------------------------------------===// 131 /// Instruction iterator methods 132 /// 133 inline iterator begin() { return InstList.begin(); } 134 inline const_iterator begin() const { return InstList.begin(); } 135 inline iterator end () { return InstList.end(); } 136 inline const_iterator end () const { return InstList.end(); } 137 138 inline size_t size() const { return InstList.size(); } 139 inline bool empty() const { return InstList.empty(); } 140 inline const Instruction &front() const { return InstList.front(); } 141 inline Instruction &front() { return InstList.front(); } 142 inline const Instruction &back() const { return InstList.back(); } 143 inline Instruction &back() { return InstList.back(); } 144 145 /// getInstList() - Return the underlying instruction list container. You 146 /// need to access it directly if you want to modify it currently. 147 /// 148 const InstListType &getInstList() const { return InstList; } 149 InstListType &getInstList() { return InstList; } 150 151 virtual void print(std::ostream &OS) const { print(OS, 0); } 152 void print(std::ostream *OS) const { if (OS) print(*OS); } 153 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 154 155 /// Methods for support type inquiry through isa, cast, and dyn_cast: 156 static inline bool classof(const BasicBlock *) { return true; } 157 static inline bool classof(const Value *V) { 158 return V->getValueID() == Value::BasicBlockVal; 159 } 160 161 /// dropAllReferences() - This function causes all the subinstructions to "let 162 /// go" of all references that they are maintaining. This allows one to 163 /// 'delete' a whole class at a time, even though there may be circular 164 /// references... first all references are dropped, and all use counts go to 165 /// zero. Then everything is delete'd for real. Note that no operations are 166 /// valid on an object that has "dropped all references", except operator 167 /// delete. 168 /// 169 void dropAllReferences(); 170 171 /// removePredecessor - This method is used to notify a BasicBlock that the 172 /// specified Predecessor of the block is no longer able to reach it. This is 173 /// actually not used to update the Predecessor list, but is actually used to 174 /// update the PHI nodes that reside in the block. Note that this should be 175 /// called while the predecessor still refers to this block. 176 /// 177 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false); 178 179 /// splitBasicBlock - This splits a basic block into two at the specified 180 /// instruction. Note that all instructions BEFORE the specified iterator 181 /// stay as part of the original basic block, an unconditional branch is added 182 /// to the original BB, and the rest of the instructions in the BB are moved 183 /// to the new BB, including the old terminator. The newly formed BasicBlock 184 /// is returned. This function invalidates the specified iterator. 185 /// 186 /// Note that this only works on well formed basic blocks (must have a 187 /// terminator), and 'I' must not be the end of instruction list (which would 188 /// cause a degenerate basic block to be formed, having a terminator inside of 189 /// the basic block). 190 /// 191 BasicBlock *splitBasicBlock(iterator I, const std::string &BBName = ""); 192 193 194 static unsigned getInstListOffset() { 195 BasicBlock *Obj = 0; 196 return unsigned(reinterpret_cast<uintptr_t>(&Obj->InstList)); 197 } 198 199private: 200 // getNext/Prev - Return the next or previous basic block in the list. Access 201 // these with Function::iterator. 202 BasicBlock *getNext() { return Next; } 203 const BasicBlock *getNext() const { return Next; } 204 BasicBlock *getPrev() { return Prev; } 205 const BasicBlock *getPrev() const { return Prev; } 206}; 207 208inline int 209ilist_traits<Instruction>::getListOffset() { 210 return BasicBlock::getInstListOffset(); 211} 212 213} // End llvm namespace 214 215#endif 216