1 //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==// 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 family of functions perform manipulations on basic blocks, and 10 // instructions contained within basic blocks. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/Analysis/CFG.h" 20 #include "llvm/Analysis/DomTreeUpdater.h" 21 #include "llvm/Analysis/LoopInfo.h" 22 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 23 #include "llvm/Analysis/MemorySSAUpdater.h" 24 #include "llvm/Analysis/PostDominators.h" 25 #include "llvm/IR/BasicBlock.h" 26 #include "llvm/IR/CFG.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DebugInfoMetadata.h" 29 #include "llvm/IR/Dominators.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/InstrTypes.h" 32 #include "llvm/IR/Instruction.h" 33 #include "llvm/IR/Instructions.h" 34 #include "llvm/IR/IntrinsicInst.h" 35 #include "llvm/IR/LLVMContext.h" 36 #include "llvm/IR/Type.h" 37 #include "llvm/IR/User.h" 38 #include "llvm/IR/Value.h" 39 #include "llvm/IR/ValueHandle.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include "llvm/Transforms/Utils/Local.h" 44 #include <cassert> 45 #include <cstdint> 46 #include <string> 47 #include <utility> 48 #include <vector> 49 50 using namespace llvm; 51 52 #define DEBUG_TYPE "basicblock-utils" 53 54 void llvm::DetatchDeadBlocks( 55 ArrayRef<BasicBlock *> BBs, 56 SmallVectorImpl<DominatorTree::UpdateType> *Updates, 57 bool KeepOneInputPHIs) { 58 for (auto *BB : BBs) { 59 // Loop through all of our successors and make sure they know that one 60 // of their predecessors is going away. 61 SmallPtrSet<BasicBlock *, 4> UniqueSuccessors; 62 for (BasicBlock *Succ : successors(BB)) { 63 Succ->removePredecessor(BB, KeepOneInputPHIs); 64 if (Updates && UniqueSuccessors.insert(Succ).second) 65 Updates->push_back({DominatorTree::Delete, BB, Succ}); 66 } 67 68 // Zap all the instructions in the block. 69 while (!BB->empty()) { 70 Instruction &I = BB->back(); 71 // If this instruction is used, replace uses with an arbitrary value. 72 // Because control flow can't get here, we don't care what we replace the 73 // value with. Note that since this block is unreachable, and all values 74 // contained within it must dominate their uses, that all uses will 75 // eventually be removed (they are themselves dead). 76 if (!I.use_empty()) 77 I.replaceAllUsesWith(UndefValue::get(I.getType())); 78 BB->getInstList().pop_back(); 79 } 80 new UnreachableInst(BB->getContext(), BB); 81 assert(BB->getInstList().size() == 1 && 82 isa<UnreachableInst>(BB->getTerminator()) && 83 "The successor list of BB isn't empty before " 84 "applying corresponding DTU updates."); 85 } 86 } 87 88 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU, 89 bool KeepOneInputPHIs) { 90 DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs); 91 } 92 93 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU, 94 bool KeepOneInputPHIs) { 95 #ifndef NDEBUG 96 // Make sure that all predecessors of each dead block is also dead. 97 SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end()); 98 assert(Dead.size() == BBs.size() && "Duplicating blocks?"); 99 for (auto *BB : Dead) 100 for (BasicBlock *Pred : predecessors(BB)) 101 assert(Dead.count(Pred) && "All predecessors must be dead!"); 102 #endif 103 104 SmallVector<DominatorTree::UpdateType, 4> Updates; 105 DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs); 106 107 if (DTU) 108 DTU->applyUpdatesPermissive(Updates); 109 110 for (BasicBlock *BB : BBs) 111 if (DTU) 112 DTU->deleteBB(BB); 113 else 114 BB->eraseFromParent(); 115 } 116 117 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU, 118 bool KeepOneInputPHIs) { 119 df_iterator_default_set<BasicBlock*> Reachable; 120 121 // Mark all reachable blocks. 122 for (BasicBlock *BB : depth_first_ext(&F, Reachable)) 123 (void)BB/* Mark all reachable blocks */; 124 125 // Collect all dead blocks. 126 std::vector<BasicBlock*> DeadBlocks; 127 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 128 if (!Reachable.count(&*I)) { 129 BasicBlock *BB = &*I; 130 DeadBlocks.push_back(BB); 131 } 132 133 // Delete the dead blocks. 134 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs); 135 136 return !DeadBlocks.empty(); 137 } 138 139 void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, 140 MemoryDependenceResults *MemDep) { 141 if (!isa<PHINode>(BB->begin())) return; 142 143 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 144 if (PN->getIncomingValue(0) != PN) 145 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 146 else 147 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 148 149 if (MemDep) 150 MemDep->removeInstruction(PN); // Memdep updates AA itself. 151 152 PN->eraseFromParent(); 153 } 154 } 155 156 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI, 157 MemorySSAUpdater *MSSAU) { 158 // Recursively deleting a PHI may cause multiple PHIs to be deleted 159 // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete. 160 SmallVector<WeakTrackingVH, 8> PHIs; 161 for (PHINode &PN : BB->phis()) 162 PHIs.push_back(&PN); 163 164 bool Changed = false; 165 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) 166 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) 167 Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU); 168 169 return Changed; 170 } 171 172 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, 173 LoopInfo *LI, MemorySSAUpdater *MSSAU, 174 MemoryDependenceResults *MemDep, 175 bool PredecessorWithTwoSuccessors) { 176 if (BB->hasAddressTaken()) 177 return false; 178 179 // Can't merge if there are multiple predecessors, or no predecessors. 180 BasicBlock *PredBB = BB->getUniquePredecessor(); 181 if (!PredBB) return false; 182 183 // Don't break self-loops. 184 if (PredBB == BB) return false; 185 // Don't break unwinding instructions. 186 if (PredBB->getTerminator()->isExceptionalTerminator()) 187 return false; 188 189 // Can't merge if there are multiple distinct successors. 190 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB) 191 return false; 192 193 // Currently only allow PredBB to have two predecessors, one being BB. 194 // Update BI to branch to BB's only successor instead of BB. 195 BranchInst *PredBB_BI; 196 BasicBlock *NewSucc = nullptr; 197 unsigned FallThruPath; 198 if (PredecessorWithTwoSuccessors) { 199 if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator()))) 200 return false; 201 BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator()); 202 if (!BB_JmpI || !BB_JmpI->isUnconditional()) 203 return false; 204 NewSucc = BB_JmpI->getSuccessor(0); 205 FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1; 206 } 207 208 // Can't merge if there is PHI loop. 209 for (PHINode &PN : BB->phis()) 210 for (Value *IncValue : PN.incoming_values()) 211 if (IncValue == &PN) 212 return false; 213 214 LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into " 215 << PredBB->getName() << "\n"); 216 217 // Begin by getting rid of unneeded PHIs. 218 SmallVector<AssertingVH<Value>, 4> IncomingValues; 219 if (isa<PHINode>(BB->front())) { 220 for (PHINode &PN : BB->phis()) 221 if (!isa<PHINode>(PN.getIncomingValue(0)) || 222 cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB) 223 IncomingValues.push_back(PN.getIncomingValue(0)); 224 FoldSingleEntryPHINodes(BB, MemDep); 225 } 226 227 // DTU update: Collect all the edges that exit BB. 228 // These dominator edges will be redirected from Pred. 229 std::vector<DominatorTree::UpdateType> Updates; 230 if (DTU) { 231 Updates.reserve(1 + (2 * succ_size(BB))); 232 // Add insert edges first. Experimentally, for the particular case of two 233 // blocks that can be merged, with a single successor and single predecessor 234 // respectively, it is beneficial to have all insert updates first. Deleting 235 // edges first may lead to unreachable blocks, followed by inserting edges 236 // making the blocks reachable again. Such DT updates lead to high compile 237 // times. We add inserts before deletes here to reduce compile time. 238 for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 239 // This successor of BB may already have PredBB as a predecessor. 240 if (llvm::find(successors(PredBB), *I) == succ_end(PredBB)) 241 Updates.push_back({DominatorTree::Insert, PredBB, *I}); 242 for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) 243 Updates.push_back({DominatorTree::Delete, BB, *I}); 244 Updates.push_back({DominatorTree::Delete, PredBB, BB}); 245 } 246 247 Instruction *PTI = PredBB->getTerminator(); 248 Instruction *STI = BB->getTerminator(); 249 Instruction *Start = &*BB->begin(); 250 // If there's nothing to move, mark the starting instruction as the last 251 // instruction in the block. Terminator instruction is handled separately. 252 if (Start == STI) 253 Start = PTI; 254 255 // Move all definitions in the successor to the predecessor... 256 PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(), 257 BB->begin(), STI->getIterator()); 258 259 if (MSSAU) 260 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start); 261 262 // Make all PHI nodes that referred to BB now refer to Pred as their 263 // source... 264 BB->replaceAllUsesWith(PredBB); 265 266 if (PredecessorWithTwoSuccessors) { 267 // Delete the unconditional branch from BB. 268 BB->getInstList().pop_back(); 269 270 // Update branch in the predecessor. 271 PredBB_BI->setSuccessor(FallThruPath, NewSucc); 272 } else { 273 // Delete the unconditional branch from the predecessor. 274 PredBB->getInstList().pop_back(); 275 276 // Move terminator instruction. 277 PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); 278 279 // Terminator may be a memory accessing instruction too. 280 if (MSSAU) 281 if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>( 282 MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator()))) 283 MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End); 284 } 285 // Add unreachable to now empty BB. 286 new UnreachableInst(BB->getContext(), BB); 287 288 // Eliminate duplicate/redundant dbg.values. This seems to be a good place to 289 // do that since we might end up with redundant dbg.values describing the 290 // entry PHI node post-splice. 291 RemoveRedundantDbgInstrs(PredBB); 292 293 // Inherit predecessors name if it exists. 294 if (!PredBB->hasName()) 295 PredBB->takeName(BB); 296 297 if (LI) 298 LI->removeBlock(BB); 299 300 if (MemDep) 301 MemDep->invalidateCachedPredecessors(); 302 303 // Finally, erase the old block and update dominator info. 304 if (DTU) { 305 assert(BB->getInstList().size() == 1 && 306 isa<UnreachableInst>(BB->getTerminator()) && 307 "The successor list of BB isn't empty before " 308 "applying corresponding DTU updates."); 309 DTU->applyUpdatesPermissive(Updates); 310 DTU->deleteBB(BB); 311 } else { 312 BB->eraseFromParent(); // Nuke BB if DTU is nullptr. 313 } 314 315 return true; 316 } 317 318 /// Remove redundant instructions within sequences of consecutive dbg.value 319 /// instructions. This is done using a backward scan to keep the last dbg.value 320 /// describing a specific variable/fragment. 321 /// 322 /// BackwardScan strategy: 323 /// ---------------------- 324 /// Given a sequence of consecutive DbgValueInst like this 325 /// 326 /// dbg.value ..., "x", FragmentX1 (*) 327 /// dbg.value ..., "y", FragmentY1 328 /// dbg.value ..., "x", FragmentX2 329 /// dbg.value ..., "x", FragmentX1 (**) 330 /// 331 /// then the instruction marked with (*) can be removed (it is guaranteed to be 332 /// obsoleted by the instruction marked with (**) as the latter instruction is 333 /// describing the same variable using the same fragment info). 334 /// 335 /// Possible improvements: 336 /// - Check fully overlapping fragments and not only identical fragments. 337 /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta 338 /// instructions being part of the sequence of consecutive instructions. 339 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) { 340 SmallVector<DbgValueInst *, 8> ToBeRemoved; 341 SmallDenseSet<DebugVariable> VariableSet; 342 for (auto &I : reverse(*BB)) { 343 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 344 DebugVariable Key(DVI->getVariable(), 345 DVI->getExpression(), 346 DVI->getDebugLoc()->getInlinedAt()); 347 auto R = VariableSet.insert(Key); 348 // If the same variable fragment is described more than once it is enough 349 // to keep the last one (i.e. the first found since we for reverse 350 // iteration). 351 if (!R.second) 352 ToBeRemoved.push_back(DVI); 353 continue; 354 } 355 // Sequence with consecutive dbg.value instrs ended. Clear the map to 356 // restart identifying redundant instructions if case we find another 357 // dbg.value sequence. 358 VariableSet.clear(); 359 } 360 361 for (auto &Instr : ToBeRemoved) 362 Instr->eraseFromParent(); 363 364 return !ToBeRemoved.empty(); 365 } 366 367 /// Remove redundant dbg.value instructions using a forward scan. This can 368 /// remove a dbg.value instruction that is redundant due to indicating that a 369 /// variable has the same value as already being indicated by an earlier 370 /// dbg.value. 371 /// 372 /// ForwardScan strategy: 373 /// --------------------- 374 /// Given two identical dbg.value instructions, separated by a block of 375 /// instructions that isn't describing the same variable, like this 376 /// 377 /// dbg.value X1, "x", FragmentX1 (**) 378 /// <block of instructions, none being "dbg.value ..., "x", ..."> 379 /// dbg.value X1, "x", FragmentX1 (*) 380 /// 381 /// then the instruction marked with (*) can be removed. Variable "x" is already 382 /// described as being mapped to the SSA value X1. 383 /// 384 /// Possible improvements: 385 /// - Keep track of non-overlapping fragments. 386 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) { 387 SmallVector<DbgValueInst *, 8> ToBeRemoved; 388 DenseMap<DebugVariable, std::pair<Value *, DIExpression *> > VariableMap; 389 for (auto &I : *BB) { 390 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 391 DebugVariable Key(DVI->getVariable(), 392 NoneType(), 393 DVI->getDebugLoc()->getInlinedAt()); 394 auto VMI = VariableMap.find(Key); 395 // Update the map if we found a new value/expression describing the 396 // variable, or if the variable wasn't mapped already. 397 if (VMI == VariableMap.end() || 398 VMI->second.first != DVI->getValue() || 399 VMI->second.second != DVI->getExpression()) { 400 VariableMap[Key] = { DVI->getValue(), DVI->getExpression() }; 401 continue; 402 } 403 // Found an identical mapping. Remember the instruction for later removal. 404 ToBeRemoved.push_back(DVI); 405 } 406 } 407 408 for (auto &Instr : ToBeRemoved) 409 Instr->eraseFromParent(); 410 411 return !ToBeRemoved.empty(); 412 } 413 414 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) { 415 bool MadeChanges = false; 416 // By using the "backward scan" strategy before the "forward scan" strategy we 417 // can remove both dbg.value (2) and (3) in a situation like this: 418 // 419 // (1) dbg.value V1, "x", DIExpression() 420 // ... 421 // (2) dbg.value V2, "x", DIExpression() 422 // (3) dbg.value V1, "x", DIExpression() 423 // 424 // The backward scan will remove (2), it is made obsolete by (3). After 425 // getting (2) out of the way, the foward scan will remove (3) since "x" 426 // already is described as having the value V1 at (1). 427 MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB); 428 MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB); 429 430 if (MadeChanges) 431 LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: " 432 << BB->getName() << "\n"); 433 return MadeChanges; 434 } 435 436 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, 437 BasicBlock::iterator &BI, Value *V) { 438 Instruction &I = *BI; 439 // Replaces all of the uses of the instruction with uses of the value 440 I.replaceAllUsesWith(V); 441 442 // Make sure to propagate a name if there is one already. 443 if (I.hasName() && !V->hasName()) 444 V->takeName(&I); 445 446 // Delete the unnecessary instruction now... 447 BI = BIL.erase(BI); 448 } 449 450 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, 451 BasicBlock::iterator &BI, Instruction *I) { 452 assert(I->getParent() == nullptr && 453 "ReplaceInstWithInst: Instruction already inserted into basic block!"); 454 455 // Copy debug location to newly added instruction, if it wasn't already set 456 // by the caller. 457 if (!I->getDebugLoc()) 458 I->setDebugLoc(BI->getDebugLoc()); 459 460 // Insert the new instruction into the basic block... 461 BasicBlock::iterator New = BIL.insert(BI, I); 462 463 // Replace all uses of the old instruction, and delete it. 464 ReplaceInstWithValue(BIL, BI, I); 465 466 // Move BI back to point to the newly inserted instruction 467 BI = New; 468 } 469 470 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { 471 BasicBlock::iterator BI(From); 472 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); 473 } 474 475 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT, 476 LoopInfo *LI, MemorySSAUpdater *MSSAU) { 477 unsigned SuccNum = GetSuccessorNumber(BB, Succ); 478 479 // If this is a critical edge, let SplitCriticalEdge do it. 480 Instruction *LatchTerm = BB->getTerminator(); 481 if (SplitCriticalEdge( 482 LatchTerm, SuccNum, 483 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA())) 484 return LatchTerm->getSuccessor(SuccNum); 485 486 // If the edge isn't critical, then BB has a single successor or Succ has a 487 // single pred. Split the block. 488 if (BasicBlock *SP = Succ->getSinglePredecessor()) { 489 // If the successor only has a single pred, split the top of the successor 490 // block. 491 assert(SP == BB && "CFG broken"); 492 SP = nullptr; 493 return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU); 494 } 495 496 // Otherwise, if BB has a single successor, split it at the bottom of the 497 // block. 498 assert(BB->getTerminator()->getNumSuccessors() == 1 && 499 "Should have a single succ!"); 500 return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU); 501 } 502 503 unsigned 504 llvm::SplitAllCriticalEdges(Function &F, 505 const CriticalEdgeSplittingOptions &Options) { 506 unsigned NumBroken = 0; 507 for (BasicBlock &BB : F) { 508 Instruction *TI = BB.getTerminator(); 509 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI)) 510 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 511 if (SplitCriticalEdge(TI, i, Options)) 512 ++NumBroken; 513 } 514 return NumBroken; 515 } 516 517 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 518 DominatorTree *DT, LoopInfo *LI, 519 MemorySSAUpdater *MSSAU, const Twine &BBName) { 520 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 521 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 522 ++SplitIt; 523 std::string Name = BBName.str(); 524 BasicBlock *New = Old->splitBasicBlock( 525 SplitIt, Name.empty() ? Old->getName() + ".split" : Name); 526 527 // The new block lives in whichever loop the old one did. This preserves 528 // LCSSA as well, because we force the split point to be after any PHI nodes. 529 if (LI) 530 if (Loop *L = LI->getLoopFor(Old)) 531 L->addBasicBlockToLoop(New, *LI); 532 533 if (DT) 534 // Old dominates New. New node dominates all other nodes dominated by Old. 535 if (DomTreeNode *OldNode = DT->getNode(Old)) { 536 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 537 538 DomTreeNode *NewNode = DT->addNewBlock(New, Old); 539 for (DomTreeNode *I : Children) 540 DT->changeImmediateDominator(I, NewNode); 541 } 542 543 // Move MemoryAccesses still tracked in Old, but part of New now. 544 // Update accesses in successor blocks accordingly. 545 if (MSSAU) 546 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin())); 547 548 return New; 549 } 550 551 /// Update DominatorTree, LoopInfo, and LCCSA analysis information. 552 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, 553 ArrayRef<BasicBlock *> Preds, 554 DominatorTree *DT, LoopInfo *LI, 555 MemorySSAUpdater *MSSAU, 556 bool PreserveLCSSA, bool &HasLoopExit) { 557 // Update dominator tree if available. 558 if (DT) { 559 if (OldBB == DT->getRootNode()->getBlock()) { 560 assert(NewBB == &NewBB->getParent()->getEntryBlock()); 561 DT->setNewRoot(NewBB); 562 } else { 563 // Split block expects NewBB to have a non-empty set of predecessors. 564 DT->splitBlock(NewBB); 565 } 566 } 567 568 // Update MemoryPhis after split if MemorySSA is available 569 if (MSSAU) 570 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds); 571 572 // The rest of the logic is only relevant for updating the loop structures. 573 if (!LI) 574 return; 575 576 assert(DT && "DT should be available to update LoopInfo!"); 577 Loop *L = LI->getLoopFor(OldBB); 578 579 // If we need to preserve loop analyses, collect some information about how 580 // this split will affect loops. 581 bool IsLoopEntry = !!L; 582 bool SplitMakesNewLoopHeader = false; 583 for (BasicBlock *Pred : Preds) { 584 // Preds that are not reachable from entry should not be used to identify if 585 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks 586 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader 587 // as true and make the NewBB the header of some loop. This breaks LI. 588 if (!DT->isReachableFromEntry(Pred)) 589 continue; 590 // If we need to preserve LCSSA, determine if any of the preds is a loop 591 // exit. 592 if (PreserveLCSSA) 593 if (Loop *PL = LI->getLoopFor(Pred)) 594 if (!PL->contains(OldBB)) 595 HasLoopExit = true; 596 597 // If we need to preserve LoopInfo, note whether any of the preds crosses 598 // an interesting loop boundary. 599 if (!L) 600 continue; 601 if (L->contains(Pred)) 602 IsLoopEntry = false; 603 else 604 SplitMakesNewLoopHeader = true; 605 } 606 607 // Unless we have a loop for OldBB, nothing else to do here. 608 if (!L) 609 return; 610 611 if (IsLoopEntry) { 612 // Add the new block to the nearest enclosing loop (and not an adjacent 613 // loop). To find this, examine each of the predecessors and determine which 614 // loops enclose them, and select the most-nested loop which contains the 615 // loop containing the block being split. 616 Loop *InnermostPredLoop = nullptr; 617 for (BasicBlock *Pred : Preds) { 618 if (Loop *PredLoop = LI->getLoopFor(Pred)) { 619 // Seek a loop which actually contains the block being split (to avoid 620 // adjacent loops). 621 while (PredLoop && !PredLoop->contains(OldBB)) 622 PredLoop = PredLoop->getParentLoop(); 623 624 // Select the most-nested of these loops which contains the block. 625 if (PredLoop && PredLoop->contains(OldBB) && 626 (!InnermostPredLoop || 627 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) 628 InnermostPredLoop = PredLoop; 629 } 630 } 631 632 if (InnermostPredLoop) 633 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI); 634 } else { 635 L->addBasicBlockToLoop(NewBB, *LI); 636 if (SplitMakesNewLoopHeader) 637 L->moveToHeader(NewBB); 638 } 639 } 640 641 /// Update the PHI nodes in OrigBB to include the values coming from NewBB. 642 /// This also updates AliasAnalysis, if available. 643 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, 644 ArrayRef<BasicBlock *> Preds, BranchInst *BI, 645 bool HasLoopExit) { 646 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB. 647 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end()); 648 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) { 649 PHINode *PN = cast<PHINode>(I++); 650 651 // Check to see if all of the values coming in are the same. If so, we 652 // don't need to create a new PHI node, unless it's needed for LCSSA. 653 Value *InVal = nullptr; 654 if (!HasLoopExit) { 655 InVal = PN->getIncomingValueForBlock(Preds[0]); 656 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 657 if (!PredSet.count(PN->getIncomingBlock(i))) 658 continue; 659 if (!InVal) 660 InVal = PN->getIncomingValue(i); 661 else if (InVal != PN->getIncomingValue(i)) { 662 InVal = nullptr; 663 break; 664 } 665 } 666 } 667 668 if (InVal) { 669 // If all incoming values for the new PHI would be the same, just don't 670 // make a new PHI. Instead, just remove the incoming values from the old 671 // PHI. 672 673 // NOTE! This loop walks backwards for a reason! First off, this minimizes 674 // the cost of removal if we end up removing a large number of values, and 675 // second off, this ensures that the indices for the incoming values 676 // aren't invalidated when we remove one. 677 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) 678 if (PredSet.count(PN->getIncomingBlock(i))) 679 PN->removeIncomingValue(i, false); 680 681 // Add an incoming value to the PHI node in the loop for the preheader 682 // edge. 683 PN->addIncoming(InVal, NewBB); 684 continue; 685 } 686 687 // If the values coming into the block are not the same, we need a new 688 // PHI. 689 // Create the new PHI node, insert it into NewBB at the end of the block 690 PHINode *NewPHI = 691 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI); 692 693 // NOTE! This loop walks backwards for a reason! First off, this minimizes 694 // the cost of removal if we end up removing a large number of values, and 695 // second off, this ensures that the indices for the incoming values aren't 696 // invalidated when we remove one. 697 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) { 698 BasicBlock *IncomingBB = PN->getIncomingBlock(i); 699 if (PredSet.count(IncomingBB)) { 700 Value *V = PN->removeIncomingValue(i, false); 701 NewPHI->addIncoming(V, IncomingBB); 702 } 703 } 704 705 PN->addIncoming(NewPHI, NewBB); 706 } 707 } 708 709 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 710 ArrayRef<BasicBlock *> Preds, 711 const char *Suffix, DominatorTree *DT, 712 LoopInfo *LI, MemorySSAUpdater *MSSAU, 713 bool PreserveLCSSA) { 714 // Do not attempt to split that which cannot be split. 715 if (!BB->canSplitPredecessors()) 716 return nullptr; 717 718 // For the landingpads we need to act a bit differently. 719 // Delegate this work to the SplitLandingPadPredecessors. 720 if (BB->isLandingPad()) { 721 SmallVector<BasicBlock*, 2> NewBBs; 722 std::string NewName = std::string(Suffix) + ".split-lp"; 723 724 SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs, DT, 725 LI, MSSAU, PreserveLCSSA); 726 return NewBBs[0]; 727 } 728 729 // Create new basic block, insert right before the original block. 730 BasicBlock *NewBB = BasicBlock::Create( 731 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB); 732 733 // The new block unconditionally branches to the old block. 734 BranchInst *BI = BranchInst::Create(BB, NewBB); 735 // Splitting the predecessors of a loop header creates a preheader block. 736 if (LI && LI->isLoopHeader(BB)) 737 // Using the loop start line number prevents debuggers stepping into the 738 // loop body for this instruction. 739 BI->setDebugLoc(LI->getLoopFor(BB)->getStartLoc()); 740 else 741 BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc()); 742 743 // Move the edges from Preds to point to NewBB instead of BB. 744 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 745 // This is slightly more strict than necessary; the minimum requirement 746 // is that there be no more than one indirectbr branching to BB. And 747 // all BlockAddress uses would need to be updated. 748 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 749 "Cannot split an edge from an IndirectBrInst"); 750 assert(!isa<CallBrInst>(Preds[i]->getTerminator()) && 751 "Cannot split an edge from a CallBrInst"); 752 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); 753 } 754 755 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI 756 // node becomes an incoming value for BB's phi node. However, if the Preds 757 // list is empty, we need to insert dummy entries into the PHI nodes in BB to 758 // account for the newly created predecessor. 759 if (Preds.empty()) { 760 // Insert dummy values as the incoming value. 761 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) 762 cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); 763 } 764 765 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 766 bool HasLoopExit = false; 767 UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, MSSAU, PreserveLCSSA, 768 HasLoopExit); 769 770 if (!Preds.empty()) { 771 // Update the PHI nodes in BB with the values coming from NewBB. 772 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit); 773 } 774 775 return NewBB; 776 } 777 778 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 779 ArrayRef<BasicBlock *> Preds, 780 const char *Suffix1, const char *Suffix2, 781 SmallVectorImpl<BasicBlock *> &NewBBs, 782 DominatorTree *DT, LoopInfo *LI, 783 MemorySSAUpdater *MSSAU, 784 bool PreserveLCSSA) { 785 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!"); 786 787 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert 788 // it right before the original block. 789 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(), 790 OrigBB->getName() + Suffix1, 791 OrigBB->getParent(), OrigBB); 792 NewBBs.push_back(NewBB1); 793 794 // The new block unconditionally branches to the old block. 795 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); 796 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 797 798 // Move the edges from Preds to point to NewBB1 instead of OrigBB. 799 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 800 // This is slightly more strict than necessary; the minimum requirement 801 // is that there be no more than one indirectbr branching to BB. And 802 // all BlockAddress uses would need to be updated. 803 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 804 "Cannot split an edge from an IndirectBrInst"); 805 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1); 806 } 807 808 bool HasLoopExit = false; 809 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, MSSAU, PreserveLCSSA, 810 HasLoopExit); 811 812 // Update the PHI nodes in OrigBB with the values coming from NewBB1. 813 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit); 814 815 // Move the remaining edges from OrigBB to point to NewBB2. 816 SmallVector<BasicBlock*, 8> NewBB2Preds; 817 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB); 818 i != e; ) { 819 BasicBlock *Pred = *i++; 820 if (Pred == NewBB1) continue; 821 assert(!isa<IndirectBrInst>(Pred->getTerminator()) && 822 "Cannot split an edge from an IndirectBrInst"); 823 NewBB2Preds.push_back(Pred); 824 e = pred_end(OrigBB); 825 } 826 827 BasicBlock *NewBB2 = nullptr; 828 if (!NewBB2Preds.empty()) { 829 // Create another basic block for the rest of OrigBB's predecessors. 830 NewBB2 = BasicBlock::Create(OrigBB->getContext(), 831 OrigBB->getName() + Suffix2, 832 OrigBB->getParent(), OrigBB); 833 NewBBs.push_back(NewBB2); 834 835 // The new block unconditionally branches to the old block. 836 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); 837 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 838 839 // Move the remaining edges from OrigBB to point to NewBB2. 840 for (BasicBlock *NewBB2Pred : NewBB2Preds) 841 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); 842 843 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 844 HasLoopExit = false; 845 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI, MSSAU, 846 PreserveLCSSA, HasLoopExit); 847 848 // Update the PHI nodes in OrigBB with the values coming from NewBB2. 849 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit); 850 } 851 852 LandingPadInst *LPad = OrigBB->getLandingPadInst(); 853 Instruction *Clone1 = LPad->clone(); 854 Clone1->setName(Twine("lpad") + Suffix1); 855 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1); 856 857 if (NewBB2) { 858 Instruction *Clone2 = LPad->clone(); 859 Clone2->setName(Twine("lpad") + Suffix2); 860 NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2); 861 862 // Create a PHI node for the two cloned landingpad instructions only 863 // if the original landingpad instruction has some uses. 864 if (!LPad->use_empty()) { 865 assert(!LPad->getType()->isTokenTy() && 866 "Split cannot be applied if LPad is token type. Otherwise an " 867 "invalid PHINode of token type would be created."); 868 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad); 869 PN->addIncoming(Clone1, NewBB1); 870 PN->addIncoming(Clone2, NewBB2); 871 LPad->replaceAllUsesWith(PN); 872 } 873 LPad->eraseFromParent(); 874 } else { 875 // There is no second clone. Just replace the landing pad with the first 876 // clone. 877 LPad->replaceAllUsesWith(Clone1); 878 LPad->eraseFromParent(); 879 } 880 } 881 882 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 883 BasicBlock *Pred, 884 DomTreeUpdater *DTU) { 885 Instruction *UncondBranch = Pred->getTerminator(); 886 // Clone the return and add it to the end of the predecessor. 887 Instruction *NewRet = RI->clone(); 888 Pred->getInstList().push_back(NewRet); 889 890 // If the return instruction returns a value, and if the value was a 891 // PHI node in "BB", propagate the right value into the return. 892 for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end(); 893 i != e; ++i) { 894 Value *V = *i; 895 Instruction *NewBC = nullptr; 896 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) { 897 // Return value might be bitcasted. Clone and insert it before the 898 // return instruction. 899 V = BCI->getOperand(0); 900 NewBC = BCI->clone(); 901 Pred->getInstList().insert(NewRet->getIterator(), NewBC); 902 *i = NewBC; 903 } 904 if (PHINode *PN = dyn_cast<PHINode>(V)) { 905 if (PN->getParent() == BB) { 906 if (NewBC) 907 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred)); 908 else 909 *i = PN->getIncomingValueForBlock(Pred); 910 } 911 } 912 } 913 914 // Update any PHI nodes in the returning block to realize that we no 915 // longer branch to them. 916 BB->removePredecessor(Pred); 917 UncondBranch->eraseFromParent(); 918 919 if (DTU) 920 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}}); 921 922 return cast<ReturnInst>(NewRet); 923 } 924 925 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 926 Instruction *SplitBefore, 927 bool Unreachable, 928 MDNode *BranchWeights, 929 DominatorTree *DT, LoopInfo *LI, 930 BasicBlock *ThenBlock) { 931 BasicBlock *Head = SplitBefore->getParent(); 932 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 933 Instruction *HeadOldTerm = Head->getTerminator(); 934 LLVMContext &C = Head->getContext(); 935 Instruction *CheckTerm; 936 bool CreateThenBlock = (ThenBlock == nullptr); 937 if (CreateThenBlock) { 938 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 939 if (Unreachable) 940 CheckTerm = new UnreachableInst(C, ThenBlock); 941 else 942 CheckTerm = BranchInst::Create(Tail, ThenBlock); 943 CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); 944 } else 945 CheckTerm = ThenBlock->getTerminator(); 946 BranchInst *HeadNewTerm = 947 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond); 948 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 949 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 950 951 if (DT) { 952 if (DomTreeNode *OldNode = DT->getNode(Head)) { 953 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 954 955 DomTreeNode *NewNode = DT->addNewBlock(Tail, Head); 956 for (DomTreeNode *Child : Children) 957 DT->changeImmediateDominator(Child, NewNode); 958 959 // Head dominates ThenBlock. 960 if (CreateThenBlock) 961 DT->addNewBlock(ThenBlock, Head); 962 else 963 DT->changeImmediateDominator(ThenBlock, Head); 964 } 965 } 966 967 if (LI) { 968 if (Loop *L = LI->getLoopFor(Head)) { 969 L->addBasicBlockToLoop(ThenBlock, *LI); 970 L->addBasicBlockToLoop(Tail, *LI); 971 } 972 } 973 974 return CheckTerm; 975 } 976 977 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 978 Instruction **ThenTerm, 979 Instruction **ElseTerm, 980 MDNode *BranchWeights) { 981 BasicBlock *Head = SplitBefore->getParent(); 982 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 983 Instruction *HeadOldTerm = Head->getTerminator(); 984 LLVMContext &C = Head->getContext(); 985 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 986 BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 987 *ThenTerm = BranchInst::Create(Tail, ThenBlock); 988 (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 989 *ElseTerm = BranchInst::Create(Tail, ElseBlock); 990 (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 991 BranchInst *HeadNewTerm = 992 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond); 993 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 994 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 995 } 996 997 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 998 BasicBlock *&IfFalse) { 999 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); 1000 BasicBlock *Pred1 = nullptr; 1001 BasicBlock *Pred2 = nullptr; 1002 1003 if (SomePHI) { 1004 if (SomePHI->getNumIncomingValues() != 2) 1005 return nullptr; 1006 Pred1 = SomePHI->getIncomingBlock(0); 1007 Pred2 = SomePHI->getIncomingBlock(1); 1008 } else { 1009 pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 1010 if (PI == PE) // No predecessor 1011 return nullptr; 1012 Pred1 = *PI++; 1013 if (PI == PE) // Only one predecessor 1014 return nullptr; 1015 Pred2 = *PI++; 1016 if (PI != PE) // More than two predecessors 1017 return nullptr; 1018 } 1019 1020 // We can only handle branches. Other control flow will be lowered to 1021 // branches if possible anyway. 1022 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); 1023 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); 1024 if (!Pred1Br || !Pred2Br) 1025 return nullptr; 1026 1027 // Eliminate code duplication by ensuring that Pred1Br is conditional if 1028 // either are. 1029 if (Pred2Br->isConditional()) { 1030 // If both branches are conditional, we don't have an "if statement". In 1031 // reality, we could transform this case, but since the condition will be 1032 // required anyway, we stand no chance of eliminating it, so the xform is 1033 // probably not profitable. 1034 if (Pred1Br->isConditional()) 1035 return nullptr; 1036 1037 std::swap(Pred1, Pred2); 1038 std::swap(Pred1Br, Pred2Br); 1039 } 1040 1041 if (Pred1Br->isConditional()) { 1042 // The only thing we have to watch out for here is to make sure that Pred2 1043 // doesn't have incoming edges from other blocks. If it does, the condition 1044 // doesn't dominate BB. 1045 if (!Pred2->getSinglePredecessor()) 1046 return nullptr; 1047 1048 // If we found a conditional branch predecessor, make sure that it branches 1049 // to BB and Pred2Br. If it doesn't, this isn't an "if statement". 1050 if (Pred1Br->getSuccessor(0) == BB && 1051 Pred1Br->getSuccessor(1) == Pred2) { 1052 IfTrue = Pred1; 1053 IfFalse = Pred2; 1054 } else if (Pred1Br->getSuccessor(0) == Pred2 && 1055 Pred1Br->getSuccessor(1) == BB) { 1056 IfTrue = Pred2; 1057 IfFalse = Pred1; 1058 } else { 1059 // We know that one arm of the conditional goes to BB, so the other must 1060 // go somewhere unrelated, and this must not be an "if statement". 1061 return nullptr; 1062 } 1063 1064 return Pred1Br->getCondition(); 1065 } 1066 1067 // Ok, if we got here, both predecessors end with an unconditional branch to 1068 // BB. Don't panic! If both blocks only have a single (identical) 1069 // predecessor, and THAT is a conditional branch, then we're all ok! 1070 BasicBlock *CommonPred = Pred1->getSinglePredecessor(); 1071 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor()) 1072 return nullptr; 1073 1074 // Otherwise, if this is a conditional branch, then we can use it! 1075 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); 1076 if (!BI) return nullptr; 1077 1078 assert(BI->isConditional() && "Two successors but not conditional?"); 1079 if (BI->getSuccessor(0) == Pred1) { 1080 IfTrue = Pred1; 1081 IfFalse = Pred2; 1082 } else { 1083 IfTrue = Pred2; 1084 IfFalse = Pred1; 1085 } 1086 return BI->getCondition(); 1087 } 1088