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 const Module *BasicBlock::getModule() const { 117 return getParent()->getParent(); 118 } 119 120 const TerminatorInst *BasicBlock::getTerminator() const { 121 if (InstList.empty()) return nullptr; 122 return dyn_cast<TerminatorInst>(&InstList.back()); 123 } 124 125 const CallInst *BasicBlock::getTerminatingMustTailCall() const { 126 if (InstList.empty()) 127 return nullptr; 128 const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back()); 129 if (!RI || RI == &InstList.front()) 130 return nullptr; 131 132 const 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 const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const { 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 const Instruction* BasicBlock::getFirstNonPHI() const { 172 for (const Instruction &I : *this) 173 if (!isa<PHINode>(I)) 174 return &I; 175 return nullptr; 176 } 177 178 const Instruction* BasicBlock::getFirstNonPHIOrDbg() const { 179 for (const Instruction &I : *this) 180 if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I)) 181 return &I; 182 return nullptr; 183 } 184 185 const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const { 186 for (const 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::const_iterator BasicBlock::getFirstInsertionPt() const { 201 const Instruction *FirstNonPHI = getFirstNonPHI(); 202 if (!FirstNonPHI) 203 return end(); 204 205 const_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 const BasicBlock *BasicBlock::getSinglePredecessor() const { 218 const_pred_iterator PI = pred_begin(this), E = pred_end(this); 219 if (PI == E) return nullptr; // No preds. 220 const 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 const BasicBlock *BasicBlock::getUniquePredecessor() const { 231 const_pred_iterator PI = pred_begin(this), E = pred_end(this); 232 if (PI == E) return nullptr; // No preds. 233 const 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 const BasicBlock *BasicBlock::getSingleSuccessor() const { 245 succ_const_iterator SI = succ_begin(this), E = succ_end(this); 246 if (SI == E) return nullptr; // no successors 247 const BasicBlock *TheSucc = *SI; 248 ++SI; 249 return (SI == E) ? TheSucc : nullptr /* multiple successors */; 250 } 251 252 const BasicBlock *BasicBlock::getUniqueSuccessor() const { 253 succ_const_iterator SI = succ_begin(this), E = succ_end(this); 254 if (SI == E) return nullptr; // No successors 255 const 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 iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() { 267 PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin()); 268 return make_range<phi_iterator>(P, nullptr); 269 } 270 271 /// This method is used to notify a BasicBlock that the 272 /// specified Predecessor of the block is no longer able to reach it. This is 273 /// actually not used to update the Predecessor list, but is actually used to 274 /// update the PHI nodes that reside in the block. Note that this should be 275 /// called while the predecessor still refers to this block. 276 /// 277 void BasicBlock::removePredecessor(BasicBlock *Pred, 278 bool DontDeleteUselessPHIs) { 279 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs. 280 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) && 281 "removePredecessor: BB is not a predecessor!"); 282 283 if (InstList.empty()) return; 284 PHINode *APN = dyn_cast<PHINode>(&front()); 285 if (!APN) return; // Quick exit. 286 287 // If there are exactly two predecessors, then we want to nuke the PHI nodes 288 // altogether. However, we cannot do this, if this in this case: 289 // 290 // Loop: 291 // %x = phi [X, Loop] 292 // %x2 = add %x, 1 ;; This would become %x2 = add %x2, 1 293 // br Loop ;; %x2 does not dominate all uses 294 // 295 // This is because the PHI node input is actually taken from the predecessor 296 // basic block. The only case this can happen is with a self loop, so we 297 // check for this case explicitly now. 298 // 299 unsigned max_idx = APN->getNumIncomingValues(); 300 assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!"); 301 if (max_idx == 2) { 302 BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred); 303 304 // Disable PHI elimination! 305 if (this == Other) max_idx = 3; 306 } 307 308 // <= Two predecessors BEFORE I remove one? 309 if (max_idx <= 2 && !DontDeleteUselessPHIs) { 310 // Yup, loop through and nuke the PHI nodes 311 while (PHINode *PN = dyn_cast<PHINode>(&front())) { 312 // Remove the predecessor first. 313 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs); 314 315 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value 316 if (max_idx == 2) { 317 if (PN->getIncomingValue(0) != PN) 318 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 319 else 320 // We are left with an infinite loop with no entries: kill the PHI. 321 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 322 getInstList().pop_front(); // Remove the PHI node 323 } 324 325 // If the PHI node already only had one entry, it got deleted by 326 // removeIncomingValue. 327 } 328 } else { 329 // Okay, now we know that we need to remove predecessor #pred_idx from all 330 // PHI nodes. Iterate over each PHI node fixing them up 331 PHINode *PN; 332 for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) { 333 ++II; 334 PN->removeIncomingValue(Pred, false); 335 // If all incoming values to the Phi are the same, we can replace the Phi 336 // with that value. 337 Value* PNV = nullptr; 338 if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue())) 339 if (PNV != PN) { 340 PN->replaceAllUsesWith(PNV); 341 PN->eraseFromParent(); 342 } 343 } 344 } 345 } 346 347 bool BasicBlock::canSplitPredecessors() const { 348 const Instruction *FirstNonPHI = getFirstNonPHI(); 349 if (isa<LandingPadInst>(FirstNonPHI)) 350 return true; 351 // This is perhaps a little conservative because constructs like 352 // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors 353 // cannot handle such things just yet. 354 if (FirstNonPHI->isEHPad()) 355 return false; 356 return true; 357 } 358 359 bool BasicBlock::isLegalToHoistInto() const { 360 auto *Term = getTerminator(); 361 // No terminator means the block is under construction. 362 if (!Term) 363 return true; 364 365 // If the block has no successors, there can be no instructions to hoist. 366 assert(Term->getNumSuccessors() > 0); 367 368 // Instructions should not be hoisted across exception handling boundaries. 369 return !Term->isExceptional(); 370 } 371 372 /// This splits a basic block into two at the specified 373 /// instruction. Note that all instructions BEFORE the specified iterator stay 374 /// as part of the original basic block, an unconditional branch is added to 375 /// the new BB, and the rest of the instructions in the BB are moved to the new 376 /// BB, including the old terminator. This invalidates the iterator. 377 /// 378 /// Note that this only works on well formed basic blocks (must have a 379 /// terminator), and 'I' must not be the end of instruction list (which would 380 /// cause a degenerate basic block to be formed, having a terminator inside of 381 /// the basic block). 382 /// 383 BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) { 384 assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!"); 385 assert(I != InstList.end() && 386 "Trying to get me to create degenerate basic block!"); 387 388 BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), 389 this->getNextNode()); 390 391 // Save DebugLoc of split point before invalidating iterator. 392 DebugLoc Loc = I->getDebugLoc(); 393 // Move all of the specified instructions from the original basic block into 394 // the new basic block. 395 New->getInstList().splice(New->end(), this->getInstList(), I, end()); 396 397 // Add a branch instruction to the newly formed basic block. 398 BranchInst *BI = BranchInst::Create(New, this); 399 BI->setDebugLoc(Loc); 400 401 // Now we must loop through all of the successors of the New block (which 402 // _were_ the successors of the 'this' block), and update any PHI nodes in 403 // successors. If there were PHI nodes in the successors, then they need to 404 // know that incoming branches will be from New, not from Old. 405 // 406 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) { 407 // Loop over any phi nodes in the basic block, updating the BB field of 408 // incoming values... 409 BasicBlock *Successor = *I; 410 for (auto &PN : Successor->phis()) { 411 int Idx = PN.getBasicBlockIndex(this); 412 while (Idx != -1) { 413 PN.setIncomingBlock((unsigned)Idx, New); 414 Idx = PN.getBasicBlockIndex(this); 415 } 416 } 417 } 418 return New; 419 } 420 421 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { 422 TerminatorInst *TI = getTerminator(); 423 if (!TI) 424 // Cope with being called on a BasicBlock that doesn't have a terminator 425 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. 426 return; 427 for (BasicBlock *Succ : TI->successors()) { 428 // N.B. Succ might not be a complete BasicBlock, so don't assume 429 // that it ends with a non-phi instruction. 430 for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { 431 PHINode *PN = dyn_cast<PHINode>(II); 432 if (!PN) 433 break; 434 int i; 435 while ((i = PN->getBasicBlockIndex(this)) >= 0) 436 PN->setIncomingBlock(i, New); 437 } 438 } 439 } 440 441 /// Return true if this basic block is a landing pad. I.e., it's 442 /// the destination of the 'unwind' edge of an invoke instruction. 443 bool BasicBlock::isLandingPad() const { 444 return isa<LandingPadInst>(getFirstNonPHI()); 445 } 446 447 /// Return the landingpad instruction associated with the landing pad. 448 const LandingPadInst *BasicBlock::getLandingPadInst() const { 449 return dyn_cast<LandingPadInst>(getFirstNonPHI()); 450 } 451 452 Optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const { 453 const TerminatorInst *TI = getTerminator(); 454 if (MDNode *MDIrrLoopHeader = 455 TI->getMetadata(LLVMContext::MD_irr_loop)) { 456 MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0)); 457 if (MDName->getString().equals("loop_header_weight")) { 458 auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1)); 459 return Optional<uint64_t>(CI->getValue().getZExtValue()); 460 } 461 } 462 return Optional<uint64_t>(); 463 } 464