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