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