LLVM API Documentation
00001 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// 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 // This file implements the BasicBlock class for the VMCore library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/BasicBlock.h" 00015 #include "llvm/Constants.h" 00016 #include "llvm/Instructions.h" 00017 #include "llvm/Type.h" 00018 #include "llvm/Support/CFG.h" 00019 #include "llvm/Support/LeakDetector.h" 00020 #include "SymbolTableListTraitsImpl.h" 00021 #include <algorithm> 00022 using namespace llvm; 00023 00024 namespace { 00025 /// DummyInst - An instance of this class is used to mark the end of the 00026 /// instruction list. This is not a real instruction. 00027 struct DummyInst : public Instruction { 00028 DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd, 0, 0) { 00029 // This should not be garbage monitored. 00030 LeakDetector::removeGarbageObject(this); 00031 } 00032 00033 virtual Instruction *clone() const { 00034 assert(0 && "Cannot clone EOL");abort(); 00035 return 0; 00036 } 00037 virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; } 00038 00039 // Methods for support type inquiry through isa, cast, and dyn_cast... 00040 static inline bool classof(const DummyInst *) { return true; } 00041 static inline bool classof(const Instruction *I) { 00042 return I->getOpcode() == OtherOpsEnd; 00043 } 00044 static inline bool classof(const Value *V) { 00045 return isa<Instruction>(V) && classof(cast<Instruction>(V)); 00046 } 00047 }; 00048 } 00049 00050 Instruction *ilist_traits<Instruction>::createSentinel() { 00051 return new DummyInst(); 00052 } 00053 iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) { 00054 return BB->getInstList(); 00055 } 00056 00057 // Explicit instantiation of SymbolTableListTraits since some of the methods 00058 // are not in the public header file... 00059 template class SymbolTableListTraits<Instruction, BasicBlock, Function>; 00060 00061 00062 BasicBlock::BasicBlock(const std::string &Name, Function *Parent, 00063 BasicBlock *InsertBefore) 00064 : Value(Type::LabelTy, Value::BasicBlockVal, Name) { 00065 // Initialize the instlist... 00066 InstList.setItemParent(this); 00067 00068 // Make sure that we get added to a function 00069 LeakDetector::addGarbageObject(this); 00070 00071 if (InsertBefore) { 00072 assert(Parent && 00073 "Cannot insert block before another block with no function!"); 00074 Parent->getBasicBlockList().insert(InsertBefore, this); 00075 } else if (Parent) { 00076 Parent->getBasicBlockList().push_back(this); 00077 } 00078 } 00079 00080 00081 BasicBlock::~BasicBlock() { 00082 assert(getParent() == 0 && "BasicBlock still linked into the program!"); 00083 dropAllReferences(); 00084 InstList.clear(); 00085 } 00086 00087 void BasicBlock::setParent(Function *parent) { 00088 if (getParent()) 00089 LeakDetector::addGarbageObject(this); 00090 00091 InstList.setParent(parent); 00092 00093 if (getParent()) 00094 LeakDetector::removeGarbageObject(this); 00095 } 00096 00097 void BasicBlock::removeFromParent() { 00098 getParent()->getBasicBlockList().remove(this); 00099 } 00100 00101 void BasicBlock::eraseFromParent() { 00102 getParent()->getBasicBlockList().erase(this); 00103 } 00104 00105 /// moveBefore - Unlink this instruction from its current function and 00106 /// insert it into the function that MovePos lives in, right before 00107 /// MovePos. 00108 void BasicBlock::moveBefore(BasicBlock *MovePos) { 00109 MovePos->getParent()->getBasicBlockList().splice(MovePos, 00110 getParent()->getBasicBlockList(), this); 00111 } 00112 00113 00114 TerminatorInst *BasicBlock::getTerminator() { 00115 if (InstList.empty()) return 0; 00116 return dyn_cast<TerminatorInst>(&InstList.back()); 00117 } 00118 00119 const TerminatorInst *const BasicBlock::getTerminator() const { 00120 if (InstList.empty()) return 0; 00121 return dyn_cast<TerminatorInst>(&InstList.back()); 00122 } 00123 00124 void BasicBlock::dropAllReferences() { 00125 for(iterator I = begin(), E = end(); I != E; ++I) 00126 I->dropAllReferences(); 00127 } 00128 00129 /// getSinglePredecessor - If this basic block has a single predecessor block, 00130 /// return the block, otherwise return a null pointer. 00131 BasicBlock *BasicBlock::getSinglePredecessor() { 00132 pred_iterator PI = pred_begin(this), E = pred_end(this); 00133 if (PI == E) return 0; // No preds. 00134 BasicBlock *ThePred = *PI; 00135 ++PI; 00136 return (PI == E) ? ThePred : 0 /*multiple preds*/; 00137 } 00138 00139 /// removePredecessor - This method is used to notify a BasicBlock that the 00140 /// specified Predecessor of the block is no longer able to reach it. This is 00141 /// actually not used to update the Predecessor list, but is actually used to 00142 /// update the PHI nodes that reside in the block. Note that this should be 00143 /// called while the predecessor still refers to this block. 00144 /// 00145 void BasicBlock::removePredecessor(BasicBlock *Pred, 00146 bool DontDeleteUselessPHIs) { 00147 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. 00148 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && 00149 "removePredecessor: BB is not a predecessor!"); 00150 00151 if (InstList.empty()) return; 00152 PHINode *APN = dyn_cast<PHINode>(&front()); 00153 if (!APN) return; // Quick exit. 00154 00155 // If there are exactly two predecessors, then we want to nuke the PHI nodes 00156 // altogether. However, we cannot do this, if this in this case: 00157 // 00158 // Loop: 00159 // %x = phi [X, Loop] 00160 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 00161 // br Loop ;; %x2 does not dominate all uses 00162 // 00163 // This is because the PHI node input is actually taken from the predecessor 00164 // basic block. The only case this can happen is with a self loop, so we 00165 // check for this case explicitly now. 00166 // 00167 unsigned max_idx = APN->getNumIncomingValues(); 00168 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); 00169 if (max_idx == 2) { 00170 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); 00171 00172 // Disable PHI elimination! 00173 if (this == Other) max_idx = 3; 00174 } 00175 00176 // <= Two predecessors BEFORE I remove one? 00177 if (max_idx <= 2 && !DontDeleteUselessPHIs) { 00178 // Yup, loop through and nuke the PHI nodes 00179 while (PHINode *PN = dyn_cast<PHINode>(&front())) { 00180 // Remove the predecessor first. 00181 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); 00182 00183 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value 00184 if (max_idx == 2) { 00185 if (PN->getOperand(0) != PN) 00186 PN->replaceAllUsesWith(PN->getOperand(0)); 00187 else 00188 // We are left with an infinite loop with no entries: kill the PHI. 00189 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 00190 getInstList().pop_front(); // Remove the PHI node 00191 } 00192 00193 // If the PHI node already only had one entry, it got deleted by 00194 // removeIncomingValue. 00195 } 00196 } else { 00197 // Okay, now we know that we need to remove predecessor #pred_idx from all 00198 // PHI nodes. Iterate over each PHI node fixing them up 00199 PHINode *PN; 00200 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { 00201 ++II; 00202 PN->removeIncomingValue(Pred, false); 00203 // If all incoming values to the Phi are the same, we can replace the Phi 00204 // with that value. 00205 if (Value *PNV = PN->hasConstantValue()) { 00206 PN->replaceAllUsesWith(PNV); 00207 PN->eraseFromParent(); 00208 } 00209 } 00210 } 00211 } 00212 00213 00214 /// splitBasicBlock - This splits a basic block into two at the specified 00215 /// instruction. Note that all instructions BEFORE the specified iterator stay 00216 /// as part of the original basic block, an unconditional branch is added to 00217 /// the new BB, and the rest of the instructions in the BB are moved to the new 00218 /// BB, including the old terminator. This invalidates the iterator. 00219 /// 00220 /// Note that this only works on well formed basic blocks (must have a 00221 /// terminator), and 'I' must not be the end of instruction list (which would 00222 /// cause a degenerate basic block to be formed, having a terminator inside of 00223 /// the basic block). 00224 /// 00225 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const std::string &BBName) { 00226 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); 00227 assert(I != InstList.end() && 00228 "Trying to get me to create degenerate basic block!"); 00229 00230 BasicBlock *New = new BasicBlock(BBName, getParent(), getNext()); 00231 00232 // Move all of the specified instructions from the original basic block into 00233 // the new basic block. 00234 New->getInstList().splice(New->end(), this->getInstList(), I, end()); 00235 00236 // Add a branch instruction to the newly formed basic block. 00237 new BranchInst(New, this); 00238 00239 // Now we must loop through all of the successors of the New block (which 00240 // _were_ the successors of the 'this' block), and update any PHI nodes in 00241 // successors. If there were PHI nodes in the successors, then they need to 00242 // know that incoming branches will be from New, not from Old. 00243 // 00244 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) { 00245 // Loop over any phi nodes in the basic block, updating the BB field of 00246 // incoming values... 00247 BasicBlock *Successor = *I; 00248 PHINode *PN; 00249 for (BasicBlock::iterator II = Successor->begin(); 00250 (PN = dyn_cast<PHINode>(II)); ++II) { 00251 int IDX = PN->getBasicBlockIndex(this); 00252 while (IDX != -1) { 00253 PN->setIncomingBlock((unsigned)IDX, New); 00254 IDX = PN->getBasicBlockIndex(this); 00255 } 00256 } 00257 } 00258 return New; 00259 }