1 //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the BasicBlock class for the IR library. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/IR/BasicBlock.h" 15 #include "SymbolTableListTraitsImpl.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/IR/CFG.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/Instructions.h" 20 #include "llvm/IR/IntrinsicInst.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/IR/Type.h" 23 #include <algorithm> 24 25 using namespace llvm; 26 27 ValueSymbolTable *BasicBlock::getValueSymbolTable() { 28 if (Function *F = getParent()) 29 return F->getValueSymbolTable(); 30 return nullptr; 31 } 32 33 LLVMContext &BasicBlock::getContext() const { 34 return getType()->getContext(); 35 } 36 37 // Explicit instantiation of SymbolTableListTraits since some of the methods 38 // are not in the public header file... 39 template class llvm::SymbolTableListTraits<Instruction>; 40 41 BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, 42 BasicBlock *InsertBefore) 43 : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) { 44 45 if (NewParent) 46 insertInto(NewParent, InsertBefore); 47 else 48 assert(!InsertBefore && 49 "Cannot insert block before another block with no function!"); 50 51 setName(Name); 52 } 53 54 void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) { 55 assert(NewParent && "Expected a parent"); 56 assert(!Parent && "Already has a parent"); 57 58 if (InsertBefore) 59 NewParent->getBasicBlockList().insert(InsertBefore->getIterator(), this); 60 else 61 NewParent->getBasicBlockList().push_back(this); 62 } 63 64 BasicBlock::~BasicBlock() { 65 // If the address of the block is taken and it is being deleted (e.g. because 66 // it is dead), this means that there is either a dangling constant expr 67 // hanging off the block, or an undefined use of the block (source code 68 // expecting the address of a label to keep the block alive even though there 69 // is no indirect branch). Handle these cases by zapping the BlockAddress 70 // nodes. There are no other possible uses at this point. 71 if (hasAddressTaken()) { 72 assert(!use_empty() && "There should be at least one blockaddress!"); 73 Constant *Replacement = 74 ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1); 75 while (!use_empty()) { 76 BlockAddress *BA = cast<BlockAddress>(user_back()); 77 BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, 78 BA->getType())); 79 BA->destroyConstant(); 80 } 81 } 82 83 assert(getParent() == nullptr && "BasicBlock still linked into the program!"); 84 dropAllReferences(); 85 InstList.clear(); 86 } 87 88 void BasicBlock::setParent(Function *parent) { 89 // Set Parent=parent, updating instruction symtab entries as appropriate. 90 InstList.setSymTabObject(&Parent, parent); 91 } 92 93 void BasicBlock::removeFromParent() { 94 getParent()->getBasicBlockList().remove(getIterator()); 95 } 96 97 iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() { 98 return getParent()->getBasicBlockList().erase(getIterator()); 99 } 100 101 /// Unlink this basic block from its current function and 102 /// insert it into the function that MovePos lives in, right before MovePos. 103 void BasicBlock::moveBefore(BasicBlock *MovePos) { 104 MovePos->getParent()->getBasicBlockList().splice( 105 MovePos->getIterator(), getParent()->getBasicBlockList(), getIterator()); 106 } 107 108 /// Unlink this basic block from its current function and 109 /// insert it into the function that MovePos lives in, right after MovePos. 110 void BasicBlock::moveAfter(BasicBlock *MovePos) { 111 MovePos->getParent()->getBasicBlockList().splice( 112 ++MovePos->getIterator(), getParent()->getBasicBlockList(), 113 getIterator()); 114 } 115 116 Module *BasicBlock::getModule() { 117 return getParent()->getParent(); 118 } 119 120 TerminatorInst *BasicBlock::getTerminator() { 121 if (InstList.empty()) return nullptr; 122 return dyn_cast<TerminatorInst>(&InstList.back()); 123 } 124 125 CallInst *BasicBlock::getTerminatingMustTailCall() { 126 if (InstList.empty()) 127 return nullptr; 128 ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back()); 129 if (!RI || RI == &InstList.front()) 130 return nullptr; 131 132 Instruction *Prev = RI->getPrevNode(); 133 if (!Prev) 134 return nullptr; 135 136 if (Value *RV = RI->getReturnValue()) { 137 if (RV != Prev) 138 return nullptr; 139 140 // Look through the optional bitcast. 141 if (auto *BI = dyn_cast<BitCastInst>(Prev)) { 142 RV = BI->getOperand(0); 143 Prev = BI->getPrevNode(); 144 if (!Prev || RV != Prev) 145 return nullptr; 146 } 147 } 148 149 if (auto *CI = dyn_cast<CallInst>(Prev)) { 150 if (CI->isMustTailCall()) 151 return CI; 152 } 153 return nullptr; 154 } 155 156 CallInst *BasicBlock::getTerminatingDeoptimizeCall() { 157 if (InstList.empty()) 158 return nullptr; 159 auto *RI = dyn_cast<ReturnInst>(&InstList.back()); 160 if (!RI || RI == &InstList.front()) 161 return nullptr; 162 163 if (auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode())) 164 if (Function *F = CI->getCalledFunction()) 165 if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize) 166 return CI; 167 168 return nullptr; 169 } 170 171 Instruction* BasicBlock::getFirstNonPHI() { 172 for (Instruction &I : *this) 173 if (!isa<PHINode>(I)) 174 return &I; 175 return nullptr; 176 } 177 178 Instruction* BasicBlock::getFirstNonPHIOrDbg() { 179 for (Instruction &I : *this) 180 if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I)) 181 return &I; 182 return nullptr; 183 } 184 185 Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() { 186 for (Instruction &I : *this) { 187 if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I)) 188 continue; 189 190 if (auto *II = dyn_cast<IntrinsicInst>(&I)) 191 if (II->getIntrinsicID() == Intrinsic::lifetime_start || 192 II->getIntrinsicID() == Intrinsic::lifetime_end) 193 continue; 194 195 return &I; 196 } 197 return nullptr; 198 } 199 200 BasicBlock::iterator BasicBlock::getFirstInsertionPt() { 201 Instruction *FirstNonPHI = getFirstNonPHI(); 202 if (!FirstNonPHI) 203 return end(); 204 205 iterator InsertPt = FirstNonPHI->getIterator(); 206 if (InsertPt->isEHPad()) ++InsertPt; 207 return InsertPt; 208 } 209 210 void BasicBlock::dropAllReferences() { 211 for (Instruction &I : *this) 212 I.dropAllReferences(); 213 } 214 215 /// If this basic block has a single predecessor block, 216 /// return the block, otherwise return a null pointer. 217 BasicBlock *BasicBlock::getSinglePredecessor() { 218 pred_iterator PI = pred_begin(this), E = pred_end(this); 219 if (PI == E) return nullptr; // No preds. 220 BasicBlock *ThePred = *PI; 221 ++PI; 222 return (PI == E) ? ThePred : nullptr /*multiple preds*/; 223 } 224 225 /// If this basic block has a unique predecessor block, 226 /// return the block, otherwise return a null pointer. 227 /// Note that unique predecessor doesn't mean single edge, there can be 228 /// multiple edges from the unique predecessor to this block (for example 229 /// a switch statement with multiple cases having the same destination). 230 BasicBlock *BasicBlock::getUniquePredecessor() { 231 pred_iterator PI = pred_begin(this), E = pred_end(this); 232 if (PI == E) return nullptr; // No preds. 233 BasicBlock *PredBB = *PI; 234 ++PI; 235 for (;PI != E; ++PI) { 236 if (*PI != PredBB) 237 return nullptr; 238 // The same predecessor appears multiple times in the predecessor list. 239 // This is OK. 240 } 241 return PredBB; 242 } 243 244 BasicBlock *BasicBlock::getSingleSuccessor() { 245 succ_iterator SI = succ_begin(this), E = succ_end(this); 246 if (SI == E) return nullptr; // no successors 247 BasicBlock *TheSucc = *SI; 248 ++SI; 249 return (SI == E) ? TheSucc : nullptr /* multiple successors */; 250 } 251 252 BasicBlock *BasicBlock::getUniqueSuccessor() { 253 succ_iterator SI = succ_begin(this), E = succ_end(this); 254 if (SI == E) return nullptr; // No successors 255 BasicBlock *SuccBB = *SI; 256 ++SI; 257 for (;SI != E; ++SI) { 258 if (*SI != SuccBB) 259 return nullptr; 260 // The same successor appears multiple times in the successor list. 261 // This is OK. 262 } 263 return SuccBB; 264 } 265 266 /// This method is used to notify a BasicBlock that the 267 /// specified Predecessor of the block is no longer able to reach it. This is 268 /// actually not used to update the Predecessor list, but is actually used to 269 /// update the PHI nodes that reside in the block. Note that this should be 270 /// called while the predecessor still refers to this block. 271 /// 272 void BasicBlock::removePredecessor(BasicBlock *Pred, 273 bool DontDeleteUselessPHIs) { 274 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. 275 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && 276 "removePredecessor: BB is not a predecessor!"); 277 278 if (InstList.empty()) return; 279 PHINode *APN = dyn_cast<PHINode>(&front()); 280 if (!APN) return; // Quick exit. 281 282 // If there are exactly two predecessors, then we want to nuke the PHI nodes 283 // altogether. However, we cannot do this, if this in this case: 284 // 285 // Loop: 286 // %x = phi [X, Loop] 287 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 288 // br Loop ;; %x2 does not dominate all uses 289 // 290 // This is because the PHI node input is actually taken from the predecessor 291 // basic block. The only case this can happen is with a self loop, so we 292 // check for this case explicitly now. 293 // 294 unsigned max_idx = APN->getNumIncomingValues(); 295 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); 296 if (max_idx == 2) { 297 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); 298 299 // Disable PHI elimination! 300 if (this == Other) max_idx = 3; 301 } 302 303 // <= Two predecessors BEFORE I remove one? 304 if (max_idx <= 2 && !DontDeleteUselessPHIs) { 305 // Yup, loop through and nuke the PHI nodes 306 while (PHINode *PN = dyn_cast<PHINode>(&front())) { 307 // Remove the predecessor first. 308 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); 309 310 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value 311 if (max_idx == 2) { 312 if (PN->getIncomingValue(0) != PN) 313 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 314 else 315 // We are left with an infinite loop with no entries: kill the PHI. 316 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 317 getInstList().pop_front(); // Remove the PHI node 318 } 319 320 // If the PHI node already only had one entry, it got deleted by 321 // removeIncomingValue. 322 } 323 } else { 324 // Okay, now we know that we need to remove predecessor #pred_idx from all 325 // PHI nodes. Iterate over each PHI node fixing them up 326 PHINode *PN; 327 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { 328 ++II; 329 PN->removeIncomingValue(Pred, false); 330 // If all incoming values to the Phi are the same, we can replace the Phi 331 // with that value. 332 Value* PNV = nullptr; 333 if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue())) 334 if (PNV != PN) { 335 PN->replaceAllUsesWith(PNV); 336 PN->eraseFromParent(); 337 } 338 } 339 } 340 } 341 342 bool BasicBlock::canSplitPredecessors() const { 343 const Instruction *FirstNonPHI = getFirstNonPHI(); 344 if (isa<LandingPadInst>(FirstNonPHI)) 345 return true; 346 // This is perhaps a little conservative because constructs like 347 // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors 348 // cannot handle such things just yet. 349 if (FirstNonPHI->isEHPad()) 350 return false; 351 return true; 352 } 353 354 /// This splits a basic block into two at the specified 355 /// instruction. Note that all instructions BEFORE the specified iterator stay 356 /// as part of the original basic block, an unconditional branch is added to 357 /// the new BB, and the rest of the instructions in the BB are moved to the new 358 /// BB, including the old terminator. This invalidates the iterator. 359 /// 360 /// Note that this only works on well formed basic blocks (must have a 361 /// terminator), and 'I' must not be the end of instruction list (which would 362 /// cause a degenerate basic block to be formed, having a terminator inside of 363 /// the basic block). 364 /// 365 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) { 366 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); 367 assert(I != InstList.end() && 368 "Trying to get me to create degenerate basic block!"); 369 370 BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), 371 this->getNextNode()); 372 373 // Save DebugLoc of split point before invalidating iterator. 374 DebugLoc Loc = I->getDebugLoc(); 375 // Move all of the specified instructions from the original basic block into 376 // the new basic block. 377 New->getInstList().splice(New->end(), this->getInstList(), I, end()); 378 379 // Add a branch instruction to the newly formed basic block. 380 BranchInst *BI = BranchInst::Create(New, this); 381 BI->setDebugLoc(Loc); 382 383 // Now we must loop through all of the successors of the New block (which 384 // _were_ the successors of the 'this' block), and update any PHI nodes in 385 // successors. If there were PHI nodes in the successors, then they need to 386 // know that incoming branches will be from New, not from Old. 387 // 388 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) { 389 // Loop over any phi nodes in the basic block, updating the BB field of 390 // incoming values... 391 BasicBlock *Successor = *I; 392 PHINode *PN; 393 for (BasicBlock::iterator II = Successor->begin(); 394 (PN = dyn_cast<PHINode>(II)); ++II) { 395 int IDX = PN->getBasicBlockIndex(this); 396 while (IDX != -1) { 397 PN->setIncomingBlock((unsigned)IDX, New); 398 IDX = PN->getBasicBlockIndex(this); 399 } 400 } 401 } 402 return New; 403 } 404 405 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { 406 TerminatorInst *TI = getTerminator(); 407 if (!TI) 408 // Cope with being called on a BasicBlock that doesn't have a terminator 409 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. 410 return; 411 for (BasicBlock *Succ : TI->successors()) { 412 // N.B. Succ might not be a complete BasicBlock, so don't assume 413 // that it ends with a non-phi instruction. 414 for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { 415 PHINode *PN = dyn_cast<PHINode>(II); 416 if (!PN) 417 break; 418 int i; 419 while ((i = PN->getBasicBlockIndex(this)) >= 0) 420 PN->setIncomingBlock(i, New); 421 } 422 } 423 } 424 425 /// Return true if this basic block is a landing pad. I.e., it's 426 /// the destination of the 'unwind' edge of an invoke instruction. 427 bool BasicBlock::isLandingPad() const { 428 return isa<LandingPadInst>(getFirstNonPHI()); 429 } 430 431 /// Return the landingpad instruction associated with the landing pad. 432 LandingPadInst *BasicBlock::getLandingPadInst() { 433 return dyn_cast<LandingPadInst>(getFirstNonPHI()); 434 } 435 const LandingPadInst *BasicBlock::getLandingPadInst() const { 436 return dyn_cast<LandingPadInst>(getFirstNonPHI()); 437 } 438