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 while (BB->getUniqueSuccessor()) 199 BB = BB->getUniqueSuccessor(); 200 return BB->getTerminatingDeoptimizeCall(); 201 } 202 203 const Instruction* BasicBlock::getFirstNonPHI() const { 204 for (const Instruction &I : *this) 205 if (!isa<PHINode>(I)) 206 return &I; 207 return nullptr; 208 } 209 210 const Instruction* BasicBlock::getFirstNonPHIOrDbg() const { 211 for (const Instruction &I : *this) 212 if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I)) 213 return &I; 214 return nullptr; 215 } 216 217 const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const { 218 for (const Instruction &I : *this) { 219 if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I)) 220 continue; 221 222 if (I.isLifetimeStartOrEnd()) 223 continue; 224 225 return &I; 226 } 227 return nullptr; 228 } 229 230 BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const { 231 const Instruction *FirstNonPHI = getFirstNonPHI(); 232 if (!FirstNonPHI) 233 return end(); 234 235 const_iterator InsertPt = FirstNonPHI->getIterator(); 236 if (InsertPt->isEHPad()) ++InsertPt; 237 return InsertPt; 238 } 239 240 void BasicBlock::dropAllReferences() { 241 for (Instruction &I : *this) 242 I.dropAllReferences(); 243 } 244 245 /// If this basic block has a single predecessor block, 246 /// return the block, otherwise return a null pointer. 247 const BasicBlock *BasicBlock::getSinglePredecessor() const { 248 const_pred_iterator PI = pred_begin(this), E = pred_end(this); 249 if (PI == E) return nullptr; // No preds. 250 const BasicBlock *ThePred = *PI; 251 ++PI; 252 return (PI == E) ? ThePred : nullptr /*multiple preds*/; 253 } 254 255 /// If this basic block has a unique predecessor block, 256 /// return the block, otherwise return a null pointer. 257 /// Note that unique predecessor doesn't mean single edge, there can be 258 /// multiple edges from the unique predecessor to this block (for example 259 /// a switch statement with multiple cases having the same destination). 260 const BasicBlock *BasicBlock::getUniquePredecessor() const { 261 const_pred_iterator PI = pred_begin(this), E = pred_end(this); 262 if (PI == E) return nullptr; // No preds. 263 const BasicBlock *PredBB = *PI; 264 ++PI; 265 for (;PI != E; ++PI) { 266 if (*PI != PredBB) 267 return nullptr; 268 // The same predecessor appears multiple times in the predecessor list. 269 // This is OK. 270 } 271 return PredBB; 272 } 273 274 bool BasicBlock::hasNPredecessors(unsigned N) const { 275 return hasNItems(pred_begin(this), pred_end(this), N); 276 } 277 278 bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const { 279 return hasNItemsOrMore(pred_begin(this), pred_end(this), N); 280 } 281 282 const BasicBlock *BasicBlock::getSingleSuccessor() const { 283 succ_const_iterator SI = succ_begin(this), E = succ_end(this); 284 if (SI == E) return nullptr; // no successors 285 const BasicBlock *TheSucc = *SI; 286 ++SI; 287 return (SI == E) ? TheSucc : nullptr /* multiple successors */; 288 } 289 290 const BasicBlock *BasicBlock::getUniqueSuccessor() const { 291 succ_const_iterator SI = succ_begin(this), E = succ_end(this); 292 if (SI == E) return nullptr; // No successors 293 const BasicBlock *SuccBB = *SI; 294 ++SI; 295 for (;SI != E; ++SI) { 296 if (*SI != SuccBB) 297 return nullptr; 298 // The same successor appears multiple times in the successor list. 299 // This is OK. 300 } 301 return SuccBB; 302 } 303 304 iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() { 305 PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin()); 306 return make_range<phi_iterator>(P, nullptr); 307 } 308 309 /// This method is used to notify a BasicBlock that the 310 /// specified Predecessor of the block is no longer able to reach it. This is 311 /// actually not used to update the Predecessor list, but is actually used to 312 /// update the PHI nodes that reside in the block. Note that this should be 313 /// called while the predecessor still refers to this block. 314 /// 315 void BasicBlock::removePredecessor(BasicBlock *Pred, 316 bool KeepOneInputPHIs) { 317 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. 318 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && 319 "removePredecessor: BB is not a predecessor!"); 320 321 if (InstList.empty()) return; 322 PHINode *APN = dyn_cast<PHINode>(&front()); 323 if (!APN) return; // Quick exit. 324 325 // If there are exactly two predecessors, then we want to nuke the PHI nodes 326 // altogether. However, we cannot do this, if this in this case: 327 // 328 // Loop: 329 // %x = phi [X, Loop] 330 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 331 // br Loop ;; %x2 does not dominate all uses 332 // 333 // This is because the PHI node input is actually taken from the predecessor 334 // basic block. The only case this can happen is with a self loop, so we 335 // check for this case explicitly now. 336 // 337 unsigned max_idx = APN->getNumIncomingValues(); 338 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); 339 if (max_idx == 2) { 340 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); 341 342 // Disable PHI elimination! 343 if (this == Other) max_idx = 3; 344 } 345 346 // <= Two predecessors BEFORE I remove one? 347 if (max_idx <= 2 && !KeepOneInputPHIs) { 348 // Yup, loop through and nuke the PHI nodes 349 while (PHINode *PN = dyn_cast<PHINode>(&front())) { 350 // Remove the predecessor first. 351 PN->removeIncomingValue(Pred, !KeepOneInputPHIs); 352 353 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value 354 if (max_idx == 2) { 355 if (PN->getIncomingValue(0) != PN) 356 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 357 else 358 // We are left with an infinite loop with no entries: kill the PHI. 359 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 360 getInstList().pop_front(); // Remove the PHI node 361 } 362 363 // If the PHI node already only had one entry, it got deleted by 364 // removeIncomingValue. 365 } 366 } else { 367 // Okay, now we know that we need to remove predecessor #pred_idx from all 368 // PHI nodes. Iterate over each PHI node fixing them up 369 PHINode *PN; 370 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { 371 ++II; 372 PN->removeIncomingValue(Pred, false); 373 // If all incoming values to the Phi are the same, we can replace the Phi 374 // with that value. 375 Value* PNV = nullptr; 376 if (!KeepOneInputPHIs && (PNV = PN->hasConstantValue())) 377 if (PNV != PN) { 378 PN->replaceAllUsesWith(PNV); 379 PN->eraseFromParent(); 380 } 381 } 382 } 383 } 384 385 bool BasicBlock::canSplitPredecessors() const { 386 const Instruction *FirstNonPHI = getFirstNonPHI(); 387 if (isa<LandingPadInst>(FirstNonPHI)) 388 return true; 389 // This is perhaps a little conservative because constructs like 390 // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors 391 // cannot handle such things just yet. 392 if (FirstNonPHI->isEHPad()) 393 return false; 394 return true; 395 } 396 397 bool BasicBlock::isLegalToHoistInto() const { 398 auto *Term = getTerminator(); 399 // No terminator means the block is under construction. 400 if (!Term) 401 return true; 402 403 // If the block has no successors, there can be no instructions to hoist. 404 assert(Term->getNumSuccessors() > 0); 405 406 // Instructions should not be hoisted across exception handling boundaries. 407 return !Term->isExceptionalTerminator(); 408 } 409 410 /// This splits a basic block into two at the specified 411 /// instruction. Note that all instructions BEFORE the specified iterator stay 412 /// as part of the original basic block, an unconditional branch is added to 413 /// the new BB, and the rest of the instructions in the BB are moved to the new 414 /// BB, including the old terminator. This invalidates the iterator. 415 /// 416 /// Note that this only works on well formed basic blocks (must have a 417 /// terminator), and 'I' must not be the end of instruction list (which would 418 /// cause a degenerate basic block to be formed, having a terminator inside of 419 /// the basic block). 420 /// 421 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) { 422 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); 423 assert(I != InstList.end() && 424 "Trying to get me to create degenerate basic block!"); 425 426 BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), 427 this->getNextNode()); 428 429 // Save DebugLoc of split point before invalidating iterator. 430 DebugLoc Loc = I->getDebugLoc(); 431 // Move all of the specified instructions from the original basic block into 432 // the new basic block. 433 New->getInstList().splice(New->end(), this->getInstList(), I, end()); 434 435 // Add a branch instruction to the newly formed basic block. 436 BranchInst *BI = BranchInst::Create(New, this); 437 BI->setDebugLoc(Loc); 438 439 // Now we must loop through all of the successors of the New block (which 440 // _were_ the successors of the 'this' block), and update any PHI nodes in 441 // successors. If there were PHI nodes in the successors, then they need to 442 // know that incoming branches will be from New, not from Old (this). 443 // 444 New->replaceSuccessorsPhiUsesWith(this, New); 445 return New; 446 } 447 448 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) { 449 // N.B. This might not be a complete BasicBlock, so don't assume 450 // that it ends with a non-phi instruction. 451 for (iterator II = begin(), IE = end(); II != IE; ++II) { 452 PHINode *PN = dyn_cast<PHINode>(II); 453 if (!PN) 454 break; 455 PN->replaceIncomingBlockWith(Old, New); 456 } 457 } 458 459 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old, 460 BasicBlock *New) { 461 Instruction *TI = getTerminator(); 462 if (!TI) 463 // Cope with being called on a BasicBlock that doesn't have a terminator 464 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. 465 return; 466 llvm::for_each(successors(TI), [Old, New](BasicBlock *Succ) { 467 Succ->replacePhiUsesWith(Old, New); 468 }); 469 } 470 471 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { 472 this->replaceSuccessorsPhiUsesWith(this, New); 473 } 474 475 /// Return true if this basic block is a landing pad. I.e., it's 476 /// the destination of the 'unwind' edge of an invoke instruction. 477 bool BasicBlock::isLandingPad() const { 478 return isa<LandingPadInst>(getFirstNonPHI()); 479 } 480 481 /// Return the landingpad instruction associated with the landing pad. 482 const LandingPadInst *BasicBlock::getLandingPadInst() const { 483 return dyn_cast<LandingPadInst>(getFirstNonPHI()); 484 } 485 486 Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const { 487 const Instruction *TI = getTerminator(); 488 if (MDNode *MDIrrLoopHeader = 489 TI->getMetadata(LLVMContext::MD_irr_loop)) { 490 MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0)); 491 if (MDName->getString().equals("loop_header_weight")) { 492 auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1)); 493 return Optional<uint64_t>(CI->getValue().getZExtValue()); 494 } 495 } 496 return Optional<uint64_t>(); 497 } 498 499 BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) { 500 while (isa<DbgInfoIntrinsic>(It)) 501 ++It; 502 return It; 503 } 504