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->applyUpdates(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 (BasicBlock &BB : F) 128 if (!Reachable.count(&BB)) 129 DeadBlocks.push_back(&BB); 130 131 // Delete the dead blocks. 132 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs); 133 134 return !DeadBlocks.empty(); 135 } 136 137 bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB, 138 MemoryDependenceResults *MemDep) { 139 if (!isa<PHINode>(BB->begin())) 140 return false; 141 142 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 143 if (PN->getIncomingValue(0) != PN) 144 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 145 else 146 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 147 148 if (MemDep) 149 MemDep->removeInstruction(PN); // Memdep updates AA itself. 150 151 PN->eraseFromParent(); 152 } 153 return true; 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 SmallSetVector<BasicBlock *, 2> UniqueSuccessors(succ_begin(BB), 232 succ_end(BB)); 233 Updates.reserve(1 + (2 * UniqueSuccessors.size())); 234 // Add insert edges first. Experimentally, for the particular case of two 235 // blocks that can be merged, with a single successor and single predecessor 236 // respectively, it is beneficial to have all insert updates first. Deleting 237 // edges first may lead to unreachable blocks, followed by inserting edges 238 // making the blocks reachable again. Such DT updates lead to high compile 239 // times. We add inserts before deletes here to reduce compile time. 240 for (BasicBlock *UniqueSuccessor : UniqueSuccessors) 241 // This successor of BB may already have PredBB as a predecessor. 242 if (!llvm::is_contained(successors(PredBB), UniqueSuccessor)) 243 Updates.push_back({DominatorTree::Insert, PredBB, UniqueSuccessor}); 244 for (BasicBlock *UniqueSuccessor : UniqueSuccessors) 245 Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor}); 246 Updates.push_back({DominatorTree::Delete, PredBB, BB}); 247 } 248 249 Instruction *PTI = PredBB->getTerminator(); 250 Instruction *STI = BB->getTerminator(); 251 Instruction *Start = &*BB->begin(); 252 // If there's nothing to move, mark the starting instruction as the last 253 // instruction in the block. Terminator instruction is handled separately. 254 if (Start == STI) 255 Start = PTI; 256 257 // Move all definitions in the successor to the predecessor... 258 PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(), 259 BB->begin(), STI->getIterator()); 260 261 if (MSSAU) 262 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start); 263 264 // Make all PHI nodes that referred to BB now refer to Pred as their 265 // source... 266 BB->replaceAllUsesWith(PredBB); 267 268 if (PredecessorWithTwoSuccessors) { 269 // Delete the unconditional branch from BB. 270 BB->getInstList().pop_back(); 271 272 // Update branch in the predecessor. 273 PredBB_BI->setSuccessor(FallThruPath, NewSucc); 274 } else { 275 // Delete the unconditional branch from the predecessor. 276 PredBB->getInstList().pop_back(); 277 278 // Move terminator instruction. 279 PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); 280 281 // Terminator may be a memory accessing instruction too. 282 if (MSSAU) 283 if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>( 284 MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator()))) 285 MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End); 286 } 287 // Add unreachable to now empty BB. 288 new UnreachableInst(BB->getContext(), BB); 289 290 // Inherit predecessors name if it exists. 291 if (!PredBB->hasName()) 292 PredBB->takeName(BB); 293 294 if (LI) 295 LI->removeBlock(BB); 296 297 if (MemDep) 298 MemDep->invalidateCachedPredecessors(); 299 300 // Finally, erase the old block and update dominator info. 301 if (DTU) { 302 assert(BB->getInstList().size() == 1 && 303 isa<UnreachableInst>(BB->getTerminator()) && 304 "The successor list of BB isn't empty before " 305 "applying corresponding DTU updates."); 306 DTU->applyUpdates(Updates); 307 DTU->deleteBB(BB); 308 } else { 309 BB->eraseFromParent(); // Nuke BB if DTU is nullptr. 310 } 311 312 return true; 313 } 314 315 bool llvm::MergeBlockSuccessorsIntoGivenBlocks( 316 SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU, 317 LoopInfo *LI) { 318 assert(!MergeBlocks.empty() && "MergeBlocks should not be empty"); 319 320 bool BlocksHaveBeenMerged = false; 321 while (!MergeBlocks.empty()) { 322 BasicBlock *BB = *MergeBlocks.begin(); 323 BasicBlock *Dest = BB->getSingleSuccessor(); 324 if (Dest && (!L || L->contains(Dest))) { 325 BasicBlock *Fold = Dest->getUniquePredecessor(); 326 (void)Fold; 327 if (MergeBlockIntoPredecessor(Dest, DTU, LI)) { 328 assert(Fold == BB && 329 "Expecting BB to be unique predecessor of the Dest block"); 330 MergeBlocks.erase(Dest); 331 BlocksHaveBeenMerged = true; 332 } else 333 MergeBlocks.erase(BB); 334 } else 335 MergeBlocks.erase(BB); 336 } 337 return BlocksHaveBeenMerged; 338 } 339 340 /// Remove redundant instructions within sequences of consecutive dbg.value 341 /// instructions. This is done using a backward scan to keep the last dbg.value 342 /// describing a specific variable/fragment. 343 /// 344 /// BackwardScan strategy: 345 /// ---------------------- 346 /// Given a sequence of consecutive DbgValueInst like this 347 /// 348 /// dbg.value ..., "x", FragmentX1 (*) 349 /// dbg.value ..., "y", FragmentY1 350 /// dbg.value ..., "x", FragmentX2 351 /// dbg.value ..., "x", FragmentX1 (**) 352 /// 353 /// then the instruction marked with (*) can be removed (it is guaranteed to be 354 /// obsoleted by the instruction marked with (**) as the latter instruction is 355 /// describing the same variable using the same fragment info). 356 /// 357 /// Possible improvements: 358 /// - Check fully overlapping fragments and not only identical fragments. 359 /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta 360 /// instructions being part of the sequence of consecutive instructions. 361 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) { 362 SmallVector<DbgValueInst *, 8> ToBeRemoved; 363 SmallDenseSet<DebugVariable> VariableSet; 364 for (auto &I : reverse(*BB)) { 365 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 366 DebugVariable Key(DVI->getVariable(), 367 DVI->getExpression(), 368 DVI->getDebugLoc()->getInlinedAt()); 369 auto R = VariableSet.insert(Key); 370 // If the same variable fragment is described more than once it is enough 371 // to keep the last one (i.e. the first found since we for reverse 372 // iteration). 373 if (!R.second) 374 ToBeRemoved.push_back(DVI); 375 continue; 376 } 377 // Sequence with consecutive dbg.value instrs ended. Clear the map to 378 // restart identifying redundant instructions if case we find another 379 // dbg.value sequence. 380 VariableSet.clear(); 381 } 382 383 for (auto &Instr : ToBeRemoved) 384 Instr->eraseFromParent(); 385 386 return !ToBeRemoved.empty(); 387 } 388 389 /// Remove redundant dbg.value instructions using a forward scan. This can 390 /// remove a dbg.value instruction that is redundant due to indicating that a 391 /// variable has the same value as already being indicated by an earlier 392 /// dbg.value. 393 /// 394 /// ForwardScan strategy: 395 /// --------------------- 396 /// Given two identical dbg.value instructions, separated by a block of 397 /// instructions that isn't describing the same variable, like this 398 /// 399 /// dbg.value X1, "x", FragmentX1 (**) 400 /// <block of instructions, none being "dbg.value ..., "x", ..."> 401 /// dbg.value X1, "x", FragmentX1 (*) 402 /// 403 /// then the instruction marked with (*) can be removed. Variable "x" is already 404 /// described as being mapped to the SSA value X1. 405 /// 406 /// Possible improvements: 407 /// - Keep track of non-overlapping fragments. 408 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) { 409 SmallVector<DbgValueInst *, 8> ToBeRemoved; 410 DenseMap<DebugVariable, std::pair<Value *, DIExpression *> > VariableMap; 411 for (auto &I : *BB) { 412 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 413 DebugVariable Key(DVI->getVariable(), 414 NoneType(), 415 DVI->getDebugLoc()->getInlinedAt()); 416 auto VMI = VariableMap.find(Key); 417 // Update the map if we found a new value/expression describing the 418 // variable, or if the variable wasn't mapped already. 419 if (VMI == VariableMap.end() || 420 VMI->second.first != DVI->getValue() || 421 VMI->second.second != DVI->getExpression()) { 422 VariableMap[Key] = { DVI->getValue(), DVI->getExpression() }; 423 continue; 424 } 425 // Found an identical mapping. Remember the instruction for later removal. 426 ToBeRemoved.push_back(DVI); 427 } 428 } 429 430 for (auto &Instr : ToBeRemoved) 431 Instr->eraseFromParent(); 432 433 return !ToBeRemoved.empty(); 434 } 435 436 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) { 437 bool MadeChanges = false; 438 // By using the "backward scan" strategy before the "forward scan" strategy we 439 // can remove both dbg.value (2) and (3) in a situation like this: 440 // 441 // (1) dbg.value V1, "x", DIExpression() 442 // ... 443 // (2) dbg.value V2, "x", DIExpression() 444 // (3) dbg.value V1, "x", DIExpression() 445 // 446 // The backward scan will remove (2), it is made obsolete by (3). After 447 // getting (2) out of the way, the foward scan will remove (3) since "x" 448 // already is described as having the value V1 at (1). 449 MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB); 450 MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB); 451 452 if (MadeChanges) 453 LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: " 454 << BB->getName() << "\n"); 455 return MadeChanges; 456 } 457 458 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, 459 BasicBlock::iterator &BI, Value *V) { 460 Instruction &I = *BI; 461 // Replaces all of the uses of the instruction with uses of the value 462 I.replaceAllUsesWith(V); 463 464 // Make sure to propagate a name if there is one already. 465 if (I.hasName() && !V->hasName()) 466 V->takeName(&I); 467 468 // Delete the unnecessary instruction now... 469 BI = BIL.erase(BI); 470 } 471 472 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, 473 BasicBlock::iterator &BI, Instruction *I) { 474 assert(I->getParent() == nullptr && 475 "ReplaceInstWithInst: Instruction already inserted into basic block!"); 476 477 // Copy debug location to newly added instruction, if it wasn't already set 478 // by the caller. 479 if (!I->getDebugLoc()) 480 I->setDebugLoc(BI->getDebugLoc()); 481 482 // Insert the new instruction into the basic block... 483 BasicBlock::iterator New = BIL.insert(BI, I); 484 485 // Replace all uses of the old instruction, and delete it. 486 ReplaceInstWithValue(BIL, BI, I); 487 488 // Move BI back to point to the newly inserted instruction 489 BI = New; 490 } 491 492 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { 493 BasicBlock::iterator BI(From); 494 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); 495 } 496 497 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT, 498 LoopInfo *LI, MemorySSAUpdater *MSSAU, 499 const Twine &BBName) { 500 unsigned SuccNum = GetSuccessorNumber(BB, Succ); 501 502 // If this is a critical edge, let SplitCriticalEdge do it. 503 Instruction *LatchTerm = BB->getTerminator(); 504 if (SplitCriticalEdge( 505 LatchTerm, SuccNum, 506 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA(), 507 BBName)) 508 return LatchTerm->getSuccessor(SuccNum); 509 510 // If the edge isn't critical, then BB has a single successor or Succ has a 511 // single pred. Split the block. 512 if (BasicBlock *SP = Succ->getSinglePredecessor()) { 513 // If the successor only has a single pred, split the top of the successor 514 // block. 515 assert(SP == BB && "CFG broken"); 516 SP = nullptr; 517 return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName, 518 /*Before=*/true); 519 } 520 521 // Otherwise, if BB has a single successor, split it at the bottom of the 522 // block. 523 assert(BB->getTerminator()->getNumSuccessors() == 1 && 524 "Should have a single succ!"); 525 return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName); 526 } 527 528 unsigned 529 llvm::SplitAllCriticalEdges(Function &F, 530 const CriticalEdgeSplittingOptions &Options) { 531 unsigned NumBroken = 0; 532 for (BasicBlock &BB : F) { 533 Instruction *TI = BB.getTerminator(); 534 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) && 535 !isa<CallBrInst>(TI)) 536 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 537 if (SplitCriticalEdge(TI, i, Options)) 538 ++NumBroken; 539 } 540 return NumBroken; 541 } 542 543 static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt, 544 DomTreeUpdater *DTU, DominatorTree *DT, 545 LoopInfo *LI, MemorySSAUpdater *MSSAU, 546 const Twine &BBName, bool Before) { 547 if (Before) { 548 DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 549 return splitBlockBefore(Old, SplitPt, 550 DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU, 551 BBName); 552 } 553 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 554 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 555 ++SplitIt; 556 std::string Name = BBName.str(); 557 BasicBlock *New = Old->splitBasicBlock( 558 SplitIt, Name.empty() ? Old->getName() + ".split" : Name); 559 560 // The new block lives in whichever loop the old one did. This preserves 561 // LCSSA as well, because we force the split point to be after any PHI nodes. 562 if (LI) 563 if (Loop *L = LI->getLoopFor(Old)) 564 L->addBasicBlockToLoop(New, *LI); 565 566 if (DTU) { 567 SmallVector<DominatorTree::UpdateType, 8> Updates; 568 // Old dominates New. New node dominates all other nodes dominated by Old. 569 SmallSetVector<BasicBlock *, 8> UniqueSuccessorsOfOld(succ_begin(New), 570 succ_end(New)); 571 Updates.push_back({DominatorTree::Insert, Old, New}); 572 Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfOld.size()); 573 for (BasicBlock *UniqueSuccessorOfOld : UniqueSuccessorsOfOld) { 574 Updates.push_back({DominatorTree::Insert, New, UniqueSuccessorOfOld}); 575 Updates.push_back({DominatorTree::Delete, Old, UniqueSuccessorOfOld}); 576 } 577 578 DTU->applyUpdates(Updates); 579 } else if (DT) 580 // Old dominates New. New node dominates all other nodes dominated by Old. 581 if (DomTreeNode *OldNode = DT->getNode(Old)) { 582 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 583 584 DomTreeNode *NewNode = DT->addNewBlock(New, Old); 585 for (DomTreeNode *I : Children) 586 DT->changeImmediateDominator(I, NewNode); 587 } 588 589 // Move MemoryAccesses still tracked in Old, but part of New now. 590 // Update accesses in successor blocks accordingly. 591 if (MSSAU) 592 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin())); 593 594 return New; 595 } 596 597 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 598 DominatorTree *DT, LoopInfo *LI, 599 MemorySSAUpdater *MSSAU, const Twine &BBName, 600 bool Before) { 601 return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName, 602 Before); 603 } 604 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 605 DomTreeUpdater *DTU, LoopInfo *LI, 606 MemorySSAUpdater *MSSAU, const Twine &BBName, 607 bool Before) { 608 return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName, 609 Before); 610 } 611 612 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, 613 DomTreeUpdater *DTU, LoopInfo *LI, 614 MemorySSAUpdater *MSSAU, 615 const Twine &BBName) { 616 617 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 618 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 619 ++SplitIt; 620 std::string Name = BBName.str(); 621 BasicBlock *New = Old->splitBasicBlock( 622 SplitIt, Name.empty() ? Old->getName() + ".split" : Name, 623 /* Before=*/true); 624 625 // The new block lives in whichever loop the old one did. This preserves 626 // LCSSA as well, because we force the split point to be after any PHI nodes. 627 if (LI) 628 if (Loop *L = LI->getLoopFor(Old)) 629 L->addBasicBlockToLoop(New, *LI); 630 631 if (DTU) { 632 SmallVector<DominatorTree::UpdateType, 8> DTUpdates; 633 // New dominates Old. The predecessor nodes of the Old node dominate 634 // New node. 635 SmallSetVector<BasicBlock *, 8> UniquePredecessorsOfOld(pred_begin(New), 636 pred_end(New)); 637 DTUpdates.push_back({DominatorTree::Insert, New, Old}); 638 DTUpdates.reserve(DTUpdates.size() + 2 * UniquePredecessorsOfOld.size()); 639 for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) { 640 DTUpdates.push_back({DominatorTree::Insert, UniquePredecessorOfOld, New}); 641 DTUpdates.push_back({DominatorTree::Delete, UniquePredecessorOfOld, Old}); 642 } 643 644 DTU->applyUpdates(DTUpdates); 645 646 // Move MemoryAccesses still tracked in Old, but part of New now. 647 // Update accesses in successor blocks accordingly. 648 if (MSSAU) { 649 MSSAU->applyUpdates(DTUpdates, DTU->getDomTree()); 650 if (VerifyMemorySSA) 651 MSSAU->getMemorySSA()->verifyMemorySSA(); 652 } 653 } 654 return New; 655 } 656 657 /// Update DominatorTree, LoopInfo, and LCCSA analysis information. 658 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, 659 ArrayRef<BasicBlock *> Preds, 660 DomTreeUpdater *DTU, DominatorTree *DT, 661 LoopInfo *LI, MemorySSAUpdater *MSSAU, 662 bool PreserveLCSSA, bool &HasLoopExit) { 663 // Update dominator tree if available. 664 if (DTU) { 665 // Recalculation of DomTree is needed when updating a forward DomTree and 666 // the Entry BB is replaced. 667 if (NewBB == &NewBB->getParent()->getEntryBlock() && DTU->hasDomTree()) { 668 // The entry block was removed and there is no external interface for 669 // the dominator tree to be notified of this change. In this corner-case 670 // we recalculate the entire tree. 671 DTU->recalculate(*NewBB->getParent()); 672 } else { 673 // Split block expects NewBB to have a non-empty set of predecessors. 674 SmallVector<DominatorTree::UpdateType, 8> Updates; 675 SmallSetVector<BasicBlock *, 8> UniquePreds(Preds.begin(), Preds.end()); 676 Updates.push_back({DominatorTree::Insert, NewBB, OldBB}); 677 Updates.reserve(Updates.size() + 2 * UniquePreds.size()); 678 for (auto *UniquePred : UniquePreds) { 679 Updates.push_back({DominatorTree::Insert, UniquePred, NewBB}); 680 Updates.push_back({DominatorTree::Delete, UniquePred, OldBB}); 681 } 682 DTU->applyUpdates(Updates); 683 } 684 } else if (DT) { 685 if (OldBB == DT->getRootNode()->getBlock()) { 686 assert(NewBB == &NewBB->getParent()->getEntryBlock()); 687 DT->setNewRoot(NewBB); 688 } else { 689 // Split block expects NewBB to have a non-empty set of predecessors. 690 DT->splitBlock(NewBB); 691 } 692 } 693 694 // Update MemoryPhis after split if MemorySSA is available 695 if (MSSAU) 696 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds); 697 698 // The rest of the logic is only relevant for updating the loop structures. 699 if (!LI) 700 return; 701 702 if (DTU && DTU->hasDomTree()) 703 DT = &DTU->getDomTree(); 704 assert(DT && "DT should be available to update LoopInfo!"); 705 Loop *L = LI->getLoopFor(OldBB); 706 707 // If we need to preserve loop analyses, collect some information about how 708 // this split will affect loops. 709 bool IsLoopEntry = !!L; 710 bool SplitMakesNewLoopHeader = false; 711 for (BasicBlock *Pred : Preds) { 712 // Preds that are not reachable from entry should not be used to identify if 713 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks 714 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader 715 // as true and make the NewBB the header of some loop. This breaks LI. 716 if (!DT->isReachableFromEntry(Pred)) 717 continue; 718 // If we need to preserve LCSSA, determine if any of the preds is a loop 719 // exit. 720 if (PreserveLCSSA) 721 if (Loop *PL = LI->getLoopFor(Pred)) 722 if (!PL->contains(OldBB)) 723 HasLoopExit = true; 724 725 // If we need to preserve LoopInfo, note whether any of the preds crosses 726 // an interesting loop boundary. 727 if (!L) 728 continue; 729 if (L->contains(Pred)) 730 IsLoopEntry = false; 731 else 732 SplitMakesNewLoopHeader = true; 733 } 734 735 // Unless we have a loop for OldBB, nothing else to do here. 736 if (!L) 737 return; 738 739 if (IsLoopEntry) { 740 // Add the new block to the nearest enclosing loop (and not an adjacent 741 // loop). To find this, examine each of the predecessors and determine which 742 // loops enclose them, and select the most-nested loop which contains the 743 // loop containing the block being split. 744 Loop *InnermostPredLoop = nullptr; 745 for (BasicBlock *Pred : Preds) { 746 if (Loop *PredLoop = LI->getLoopFor(Pred)) { 747 // Seek a loop which actually contains the block being split (to avoid 748 // adjacent loops). 749 while (PredLoop && !PredLoop->contains(OldBB)) 750 PredLoop = PredLoop->getParentLoop(); 751 752 // Select the most-nested of these loops which contains the block. 753 if (PredLoop && PredLoop->contains(OldBB) && 754 (!InnermostPredLoop || 755 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) 756 InnermostPredLoop = PredLoop; 757 } 758 } 759 760 if (InnermostPredLoop) 761 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI); 762 } else { 763 L->addBasicBlockToLoop(NewBB, *LI); 764 if (SplitMakesNewLoopHeader) 765 L->moveToHeader(NewBB); 766 } 767 } 768 769 /// Update the PHI nodes in OrigBB to include the values coming from NewBB. 770 /// This also updates AliasAnalysis, if available. 771 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, 772 ArrayRef<BasicBlock *> Preds, BranchInst *BI, 773 bool HasLoopExit) { 774 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB. 775 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end()); 776 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) { 777 PHINode *PN = cast<PHINode>(I++); 778 779 // Check to see if all of the values coming in are the same. If so, we 780 // don't need to create a new PHI node, unless it's needed for LCSSA. 781 Value *InVal = nullptr; 782 if (!HasLoopExit) { 783 InVal = PN->getIncomingValueForBlock(Preds[0]); 784 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 785 if (!PredSet.count(PN->getIncomingBlock(i))) 786 continue; 787 if (!InVal) 788 InVal = PN->getIncomingValue(i); 789 else if (InVal != PN->getIncomingValue(i)) { 790 InVal = nullptr; 791 break; 792 } 793 } 794 } 795 796 if (InVal) { 797 // If all incoming values for the new PHI would be the same, just don't 798 // make a new PHI. Instead, just remove the incoming values from the old 799 // PHI. 800 801 // NOTE! This loop walks backwards for a reason! First off, this minimizes 802 // the cost of removal if we end up removing a large number of values, and 803 // second off, this ensures that the indices for the incoming values 804 // aren't invalidated when we remove one. 805 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) 806 if (PredSet.count(PN->getIncomingBlock(i))) 807 PN->removeIncomingValue(i, false); 808 809 // Add an incoming value to the PHI node in the loop for the preheader 810 // edge. 811 PN->addIncoming(InVal, NewBB); 812 continue; 813 } 814 815 // If the values coming into the block are not the same, we need a new 816 // PHI. 817 // Create the new PHI node, insert it into NewBB at the end of the block 818 PHINode *NewPHI = 819 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI); 820 821 // NOTE! This loop walks backwards for a reason! First off, this minimizes 822 // the cost of removal if we end up removing a large number of values, and 823 // second off, this ensures that the indices for the incoming values aren't 824 // invalidated when we remove one. 825 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) { 826 BasicBlock *IncomingBB = PN->getIncomingBlock(i); 827 if (PredSet.count(IncomingBB)) { 828 Value *V = PN->removeIncomingValue(i, false); 829 NewPHI->addIncoming(V, IncomingBB); 830 } 831 } 832 833 PN->addIncoming(NewPHI, NewBB); 834 } 835 } 836 837 static void SplitLandingPadPredecessorsImpl( 838 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1, 839 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 840 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, 841 MemorySSAUpdater *MSSAU, bool PreserveLCSSA); 842 843 static BasicBlock * 844 SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 845 const char *Suffix, DomTreeUpdater *DTU, 846 DominatorTree *DT, LoopInfo *LI, 847 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) { 848 // Do not attempt to split that which cannot be split. 849 if (!BB->canSplitPredecessors()) 850 return nullptr; 851 852 // For the landingpads we need to act a bit differently. 853 // Delegate this work to the SplitLandingPadPredecessors. 854 if (BB->isLandingPad()) { 855 SmallVector<BasicBlock*, 2> NewBBs; 856 std::string NewName = std::string(Suffix) + ".split-lp"; 857 858 SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs, 859 DTU, DT, LI, MSSAU, PreserveLCSSA); 860 return NewBBs[0]; 861 } 862 863 // Create new basic block, insert right before the original block. 864 BasicBlock *NewBB = BasicBlock::Create( 865 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB); 866 867 // The new block unconditionally branches to the old block. 868 BranchInst *BI = BranchInst::Create(BB, NewBB); 869 870 Loop *L = nullptr; 871 BasicBlock *OldLatch = nullptr; 872 // Splitting the predecessors of a loop header creates a preheader block. 873 if (LI && LI->isLoopHeader(BB)) { 874 L = LI->getLoopFor(BB); 875 // Using the loop start line number prevents debuggers stepping into the 876 // loop body for this instruction. 877 BI->setDebugLoc(L->getStartLoc()); 878 879 // If BB is the header of the Loop, it is possible that the loop is 880 // modified, such that the current latch does not remain the latch of the 881 // loop. If that is the case, the loop metadata from the current latch needs 882 // to be applied to the new latch. 883 OldLatch = L->getLoopLatch(); 884 } else 885 BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc()); 886 887 // Move the edges from Preds to point to NewBB instead of BB. 888 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 889 // This is slightly more strict than necessary; the minimum requirement 890 // is that there be no more than one indirectbr branching to BB. And 891 // all BlockAddress uses would need to be updated. 892 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 893 "Cannot split an edge from an IndirectBrInst"); 894 assert(!isa<CallBrInst>(Preds[i]->getTerminator()) && 895 "Cannot split an edge from a CallBrInst"); 896 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); 897 } 898 899 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI 900 // node becomes an incoming value for BB's phi node. However, if the Preds 901 // list is empty, we need to insert dummy entries into the PHI nodes in BB to 902 // account for the newly created predecessor. 903 if (Preds.empty()) { 904 // Insert dummy values as the incoming value. 905 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) 906 cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); 907 } 908 909 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 910 bool HasLoopExit = false; 911 UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA, 912 HasLoopExit); 913 914 if (!Preds.empty()) { 915 // Update the PHI nodes in BB with the values coming from NewBB. 916 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit); 917 } 918 919 if (OldLatch) { 920 BasicBlock *NewLatch = L->getLoopLatch(); 921 if (NewLatch != OldLatch) { 922 MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop"); 923 NewLatch->getTerminator()->setMetadata("llvm.loop", MD); 924 OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr); 925 } 926 } 927 928 return NewBB; 929 } 930 931 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 932 ArrayRef<BasicBlock *> Preds, 933 const char *Suffix, DominatorTree *DT, 934 LoopInfo *LI, MemorySSAUpdater *MSSAU, 935 bool PreserveLCSSA) { 936 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI, 937 MSSAU, PreserveLCSSA); 938 } 939 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 940 ArrayRef<BasicBlock *> Preds, 941 const char *Suffix, 942 DomTreeUpdater *DTU, LoopInfo *LI, 943 MemorySSAUpdater *MSSAU, 944 bool PreserveLCSSA) { 945 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU, 946 /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA); 947 } 948 949 static void SplitLandingPadPredecessorsImpl( 950 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1, 951 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 952 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, 953 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) { 954 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!"); 955 956 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert 957 // it right before the original block. 958 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(), 959 OrigBB->getName() + Suffix1, 960 OrigBB->getParent(), OrigBB); 961 NewBBs.push_back(NewBB1); 962 963 // The new block unconditionally branches to the old block. 964 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); 965 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 966 967 // Move the edges from Preds to point to NewBB1 instead of OrigBB. 968 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 969 // This is slightly more strict than necessary; the minimum requirement 970 // is that there be no more than one indirectbr branching to BB. And 971 // all BlockAddress uses would need to be updated. 972 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 973 "Cannot split an edge from an IndirectBrInst"); 974 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1); 975 } 976 977 bool HasLoopExit = false; 978 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU, 979 PreserveLCSSA, HasLoopExit); 980 981 // Update the PHI nodes in OrigBB with the values coming from NewBB1. 982 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit); 983 984 // Move the remaining edges from OrigBB to point to NewBB2. 985 SmallVector<BasicBlock*, 8> NewBB2Preds; 986 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB); 987 i != e; ) { 988 BasicBlock *Pred = *i++; 989 if (Pred == NewBB1) continue; 990 assert(!isa<IndirectBrInst>(Pred->getTerminator()) && 991 "Cannot split an edge from an IndirectBrInst"); 992 NewBB2Preds.push_back(Pred); 993 e = pred_end(OrigBB); 994 } 995 996 BasicBlock *NewBB2 = nullptr; 997 if (!NewBB2Preds.empty()) { 998 // Create another basic block for the rest of OrigBB's predecessors. 999 NewBB2 = BasicBlock::Create(OrigBB->getContext(), 1000 OrigBB->getName() + Suffix2, 1001 OrigBB->getParent(), OrigBB); 1002 NewBBs.push_back(NewBB2); 1003 1004 // The new block unconditionally branches to the old block. 1005 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); 1006 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 1007 1008 // Move the remaining edges from OrigBB to point to NewBB2. 1009 for (BasicBlock *NewBB2Pred : NewBB2Preds) 1010 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); 1011 1012 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 1013 HasLoopExit = false; 1014 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU, 1015 PreserveLCSSA, HasLoopExit); 1016 1017 // Update the PHI nodes in OrigBB with the values coming from NewBB2. 1018 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit); 1019 } 1020 1021 LandingPadInst *LPad = OrigBB->getLandingPadInst(); 1022 Instruction *Clone1 = LPad->clone(); 1023 Clone1->setName(Twine("lpad") + Suffix1); 1024 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1); 1025 1026 if (NewBB2) { 1027 Instruction *Clone2 = LPad->clone(); 1028 Clone2->setName(Twine("lpad") + Suffix2); 1029 NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2); 1030 1031 // Create a PHI node for the two cloned landingpad instructions only 1032 // if the original landingpad instruction has some uses. 1033 if (!LPad->use_empty()) { 1034 assert(!LPad->getType()->isTokenTy() && 1035 "Split cannot be applied if LPad is token type. Otherwise an " 1036 "invalid PHINode of token type would be created."); 1037 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad); 1038 PN->addIncoming(Clone1, NewBB1); 1039 PN->addIncoming(Clone2, NewBB2); 1040 LPad->replaceAllUsesWith(PN); 1041 } 1042 LPad->eraseFromParent(); 1043 } else { 1044 // There is no second clone. Just replace the landing pad with the first 1045 // clone. 1046 LPad->replaceAllUsesWith(Clone1); 1047 LPad->eraseFromParent(); 1048 } 1049 } 1050 1051 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 1052 ArrayRef<BasicBlock *> Preds, 1053 const char *Suffix1, const char *Suffix2, 1054 SmallVectorImpl<BasicBlock *> &NewBBs, 1055 DominatorTree *DT, LoopInfo *LI, 1056 MemorySSAUpdater *MSSAU, 1057 bool PreserveLCSSA) { 1058 return SplitLandingPadPredecessorsImpl( 1059 OrigBB, Preds, Suffix1, Suffix2, NewBBs, 1060 /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA); 1061 } 1062 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 1063 ArrayRef<BasicBlock *> Preds, 1064 const char *Suffix1, const char *Suffix2, 1065 SmallVectorImpl<BasicBlock *> &NewBBs, 1066 DomTreeUpdater *DTU, LoopInfo *LI, 1067 MemorySSAUpdater *MSSAU, 1068 bool PreserveLCSSA) { 1069 return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2, 1070 NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU, 1071 PreserveLCSSA); 1072 } 1073 1074 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 1075 BasicBlock *Pred, 1076 DomTreeUpdater *DTU) { 1077 Instruction *UncondBranch = Pred->getTerminator(); 1078 // Clone the return and add it to the end of the predecessor. 1079 Instruction *NewRet = RI->clone(); 1080 Pred->getInstList().push_back(NewRet); 1081 1082 // If the return instruction returns a value, and if the value was a 1083 // PHI node in "BB", propagate the right value into the return. 1084 for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end(); 1085 i != e; ++i) { 1086 Value *V = *i; 1087 Instruction *NewBC = nullptr; 1088 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) { 1089 // Return value might be bitcasted. Clone and insert it before the 1090 // return instruction. 1091 V = BCI->getOperand(0); 1092 NewBC = BCI->clone(); 1093 Pred->getInstList().insert(NewRet->getIterator(), NewBC); 1094 *i = NewBC; 1095 } 1096 1097 Instruction *NewEV = nullptr; 1098 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) { 1099 V = EVI->getOperand(0); 1100 NewEV = EVI->clone(); 1101 if (NewBC) { 1102 NewBC->setOperand(0, NewEV); 1103 Pred->getInstList().insert(NewBC->getIterator(), NewEV); 1104 } else { 1105 Pred->getInstList().insert(NewRet->getIterator(), NewEV); 1106 *i = NewEV; 1107 } 1108 } 1109 1110 if (PHINode *PN = dyn_cast<PHINode>(V)) { 1111 if (PN->getParent() == BB) { 1112 if (NewEV) { 1113 NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1114 } else if (NewBC) 1115 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1116 else 1117 *i = PN->getIncomingValueForBlock(Pred); 1118 } 1119 } 1120 } 1121 1122 // Update any PHI nodes in the returning block to realize that we no 1123 // longer branch to them. 1124 BB->removePredecessor(Pred); 1125 UncondBranch->eraseFromParent(); 1126 1127 if (DTU) 1128 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}}); 1129 1130 return cast<ReturnInst>(NewRet); 1131 } 1132 1133 static Instruction * 1134 SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore, 1135 bool Unreachable, MDNode *BranchWeights, 1136 DomTreeUpdater *DTU, DominatorTree *DT, 1137 LoopInfo *LI, BasicBlock *ThenBlock) { 1138 SmallVector<DominatorTree::UpdateType, 8> Updates; 1139 BasicBlock *Head = SplitBefore->getParent(); 1140 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1141 if (DTU) { 1142 SmallSetVector<BasicBlock *, 8> UniqueSuccessorsOfHead(succ_begin(Tail), 1143 succ_end(Tail)); 1144 Updates.push_back({DominatorTree::Insert, Head, Tail}); 1145 Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfHead.size()); 1146 for (BasicBlock *UniqueSuccessorOfHead : UniqueSuccessorsOfHead) { 1147 Updates.push_back({DominatorTree::Insert, Tail, UniqueSuccessorOfHead}); 1148 Updates.push_back({DominatorTree::Delete, Head, UniqueSuccessorOfHead}); 1149 } 1150 } 1151 Instruction *HeadOldTerm = Head->getTerminator(); 1152 LLVMContext &C = Head->getContext(); 1153 Instruction *CheckTerm; 1154 bool CreateThenBlock = (ThenBlock == nullptr); 1155 if (CreateThenBlock) { 1156 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1157 if (Unreachable) 1158 CheckTerm = new UnreachableInst(C, ThenBlock); 1159 else { 1160 CheckTerm = BranchInst::Create(Tail, ThenBlock); 1161 if (DTU) 1162 Updates.push_back({DominatorTree::Insert, ThenBlock, Tail}); 1163 } 1164 CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); 1165 } else 1166 CheckTerm = ThenBlock->getTerminator(); 1167 BranchInst *HeadNewTerm = 1168 BranchInst::Create(/*ifTrue*/ ThenBlock, /*ifFalse*/ Tail, Cond); 1169 if (DTU) 1170 Updates.push_back({DominatorTree::Insert, Head, ThenBlock}); 1171 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1172 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1173 1174 if (DTU) 1175 DTU->applyUpdates(Updates); 1176 else if (DT) { 1177 if (DomTreeNode *OldNode = DT->getNode(Head)) { 1178 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 1179 1180 DomTreeNode *NewNode = DT->addNewBlock(Tail, Head); 1181 for (DomTreeNode *Child : Children) 1182 DT->changeImmediateDominator(Child, NewNode); 1183 1184 // Head dominates ThenBlock. 1185 if (CreateThenBlock) 1186 DT->addNewBlock(ThenBlock, Head); 1187 else 1188 DT->changeImmediateDominator(ThenBlock, Head); 1189 } 1190 } 1191 1192 if (LI) { 1193 if (Loop *L = LI->getLoopFor(Head)) { 1194 L->addBasicBlockToLoop(ThenBlock, *LI); 1195 L->addBasicBlockToLoop(Tail, *LI); 1196 } 1197 } 1198 1199 return CheckTerm; 1200 } 1201 1202 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 1203 Instruction *SplitBefore, 1204 bool Unreachable, 1205 MDNode *BranchWeights, 1206 DominatorTree *DT, LoopInfo *LI, 1207 BasicBlock *ThenBlock) { 1208 return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable, 1209 BranchWeights, 1210 /*DTU=*/nullptr, DT, LI, ThenBlock); 1211 } 1212 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 1213 Instruction *SplitBefore, 1214 bool Unreachable, 1215 MDNode *BranchWeights, 1216 DomTreeUpdater *DTU, LoopInfo *LI, 1217 BasicBlock *ThenBlock) { 1218 return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable, 1219 BranchWeights, DTU, /*DT=*/nullptr, LI, 1220 ThenBlock); 1221 } 1222 1223 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 1224 Instruction **ThenTerm, 1225 Instruction **ElseTerm, 1226 MDNode *BranchWeights) { 1227 BasicBlock *Head = SplitBefore->getParent(); 1228 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1229 Instruction *HeadOldTerm = Head->getTerminator(); 1230 LLVMContext &C = Head->getContext(); 1231 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1232 BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1233 *ThenTerm = BranchInst::Create(Tail, ThenBlock); 1234 (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1235 *ElseTerm = BranchInst::Create(Tail, ElseBlock); 1236 (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1237 BranchInst *HeadNewTerm = 1238 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond); 1239 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1240 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1241 } 1242 1243 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 1244 BasicBlock *&IfFalse) { 1245 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); 1246 BasicBlock *Pred1 = nullptr; 1247 BasicBlock *Pred2 = nullptr; 1248 1249 if (SomePHI) { 1250 if (SomePHI->getNumIncomingValues() != 2) 1251 return nullptr; 1252 Pred1 = SomePHI->getIncomingBlock(0); 1253 Pred2 = SomePHI->getIncomingBlock(1); 1254 } else { 1255 pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 1256 if (PI == PE) // No predecessor 1257 return nullptr; 1258 Pred1 = *PI++; 1259 if (PI == PE) // Only one predecessor 1260 return nullptr; 1261 Pred2 = *PI++; 1262 if (PI != PE) // More than two predecessors 1263 return nullptr; 1264 } 1265 1266 // We can only handle branches. Other control flow will be lowered to 1267 // branches if possible anyway. 1268 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); 1269 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); 1270 if (!Pred1Br || !Pred2Br) 1271 return nullptr; 1272 1273 // Eliminate code duplication by ensuring that Pred1Br is conditional if 1274 // either are. 1275 if (Pred2Br->isConditional()) { 1276 // If both branches are conditional, we don't have an "if statement". In 1277 // reality, we could transform this case, but since the condition will be 1278 // required anyway, we stand no chance of eliminating it, so the xform is 1279 // probably not profitable. 1280 if (Pred1Br->isConditional()) 1281 return nullptr; 1282 1283 std::swap(Pred1, Pred2); 1284 std::swap(Pred1Br, Pred2Br); 1285 } 1286 1287 if (Pred1Br->isConditional()) { 1288 // The only thing we have to watch out for here is to make sure that Pred2 1289 // doesn't have incoming edges from other blocks. If it does, the condition 1290 // doesn't dominate BB. 1291 if (!Pred2->getSinglePredecessor()) 1292 return nullptr; 1293 1294 // If we found a conditional branch predecessor, make sure that it branches 1295 // to BB and Pred2Br. If it doesn't, this isn't an "if statement". 1296 if (Pred1Br->getSuccessor(0) == BB && 1297 Pred1Br->getSuccessor(1) == Pred2) { 1298 IfTrue = Pred1; 1299 IfFalse = Pred2; 1300 } else if (Pred1Br->getSuccessor(0) == Pred2 && 1301 Pred1Br->getSuccessor(1) == BB) { 1302 IfTrue = Pred2; 1303 IfFalse = Pred1; 1304 } else { 1305 // We know that one arm of the conditional goes to BB, so the other must 1306 // go somewhere unrelated, and this must not be an "if statement". 1307 return nullptr; 1308 } 1309 1310 return Pred1Br->getCondition(); 1311 } 1312 1313 // Ok, if we got here, both predecessors end with an unconditional branch to 1314 // BB. Don't panic! If both blocks only have a single (identical) 1315 // predecessor, and THAT is a conditional branch, then we're all ok! 1316 BasicBlock *CommonPred = Pred1->getSinglePredecessor(); 1317 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor()) 1318 return nullptr; 1319 1320 // Otherwise, if this is a conditional branch, then we can use it! 1321 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); 1322 if (!BI) return nullptr; 1323 1324 assert(BI->isConditional() && "Two successors but not conditional?"); 1325 if (BI->getSuccessor(0) == Pred1) { 1326 IfTrue = Pred1; 1327 IfFalse = Pred2; 1328 } else { 1329 IfTrue = Pred2; 1330 IfFalse = Pred1; 1331 } 1332 return BI->getCondition(); 1333 } 1334 1335 // After creating a control flow hub, the operands of PHINodes in an outgoing 1336 // block Out no longer match the predecessors of that block. Predecessors of Out 1337 // that are incoming blocks to the hub are now replaced by just one edge from 1338 // the hub. To match this new control flow, the corresponding values from each 1339 // PHINode must now be moved a new PHINode in the first guard block of the hub. 1340 // 1341 // This operation cannot be performed with SSAUpdater, because it involves one 1342 // new use: If the block Out is in the list of Incoming blocks, then the newly 1343 // created PHI in the Hub will use itself along that edge from Out to Hub. 1344 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock, 1345 const SetVector<BasicBlock *> &Incoming, 1346 BasicBlock *FirstGuardBlock) { 1347 auto I = Out->begin(); 1348 while (I != Out->end() && isa<PHINode>(I)) { 1349 auto Phi = cast<PHINode>(I); 1350 auto NewPhi = 1351 PHINode::Create(Phi->getType(), Incoming.size(), 1352 Phi->getName() + ".moved", &FirstGuardBlock->back()); 1353 for (auto In : Incoming) { 1354 Value *V = UndefValue::get(Phi->getType()); 1355 if (In == Out) { 1356 V = NewPhi; 1357 } else if (Phi->getBasicBlockIndex(In) != -1) { 1358 V = Phi->removeIncomingValue(In, false); 1359 } 1360 NewPhi->addIncoming(V, In); 1361 } 1362 assert(NewPhi->getNumIncomingValues() == Incoming.size()); 1363 if (Phi->getNumOperands() == 0) { 1364 Phi->replaceAllUsesWith(NewPhi); 1365 I = Phi->eraseFromParent(); 1366 continue; 1367 } 1368 Phi->addIncoming(NewPhi, GuardBlock); 1369 ++I; 1370 } 1371 } 1372 1373 using BBPredicates = DenseMap<BasicBlock *, PHINode *>; 1374 using BBSetVector = SetVector<BasicBlock *>; 1375 1376 // Redirects the terminator of the incoming block to the first guard 1377 // block in the hub. The condition of the original terminator (if it 1378 // was conditional) and its original successors are returned as a 1379 // tuple <condition, succ0, succ1>. The function additionally filters 1380 // out successors that are not in the set of outgoing blocks. 1381 // 1382 // - condition is non-null iff the branch is conditional. 1383 // - Succ1 is non-null iff the sole/taken target is an outgoing block. 1384 // - Succ2 is non-null iff condition is non-null and the fallthrough 1385 // target is an outgoing block. 1386 static std::tuple<Value *, BasicBlock *, BasicBlock *> 1387 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock, 1388 const BBSetVector &Outgoing) { 1389 auto Branch = cast<BranchInst>(BB->getTerminator()); 1390 auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr; 1391 1392 BasicBlock *Succ0 = Branch->getSuccessor(0); 1393 BasicBlock *Succ1 = nullptr; 1394 Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr; 1395 1396 if (Branch->isUnconditional()) { 1397 Branch->setSuccessor(0, FirstGuardBlock); 1398 assert(Succ0); 1399 } else { 1400 Succ1 = Branch->getSuccessor(1); 1401 Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr; 1402 assert(Succ0 || Succ1); 1403 if (Succ0 && !Succ1) { 1404 Branch->setSuccessor(0, FirstGuardBlock); 1405 } else if (Succ1 && !Succ0) { 1406 Branch->setSuccessor(1, FirstGuardBlock); 1407 } else { 1408 Branch->eraseFromParent(); 1409 BranchInst::Create(FirstGuardBlock, BB); 1410 } 1411 } 1412 1413 assert(Succ0 || Succ1); 1414 return std::make_tuple(Condition, Succ0, Succ1); 1415 } 1416 1417 // Capture the existing control flow as guard predicates, and redirect 1418 // control flow from every incoming block to the first guard block in 1419 // the hub. 1420 // 1421 // There is one guard predicate for each outgoing block OutBB. The 1422 // predicate is a PHINode with one input for each InBB which 1423 // represents whether the hub should transfer control flow to OutBB if 1424 // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub 1425 // evaluates them in the same order as the Outgoing set-vector, and 1426 // control branches to the first outgoing block whose predicate 1427 // evaluates to true. 1428 static void convertToGuardPredicates( 1429 BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates, 1430 SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming, 1431 const BBSetVector &Outgoing) { 1432 auto &Context = Incoming.front()->getContext(); 1433 auto BoolTrue = ConstantInt::getTrue(Context); 1434 auto BoolFalse = ConstantInt::getFalse(Context); 1435 1436 // The predicate for the last outgoing is trivially true, and so we 1437 // process only the first N-1 successors. 1438 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1439 auto Out = Outgoing[i]; 1440 LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n"); 1441 auto Phi = 1442 PHINode::Create(Type::getInt1Ty(Context), Incoming.size(), 1443 StringRef("Guard.") + Out->getName(), FirstGuardBlock); 1444 GuardPredicates[Out] = Phi; 1445 } 1446 1447 for (auto In : Incoming) { 1448 Value *Condition; 1449 BasicBlock *Succ0; 1450 BasicBlock *Succ1; 1451 std::tie(Condition, Succ0, Succ1) = 1452 redirectToHub(In, FirstGuardBlock, Outgoing); 1453 1454 // Optimization: Consider an incoming block A with both successors 1455 // Succ0 and Succ1 in the set of outgoing blocks. The predicates 1456 // for Succ0 and Succ1 complement each other. If Succ0 is visited 1457 // first in the loop below, control will branch to Succ0 using the 1458 // corresponding predicate. But if that branch is not taken, then 1459 // control must reach Succ1, which means that the predicate for 1460 // Succ1 is always true. 1461 bool OneSuccessorDone = false; 1462 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1463 auto Out = Outgoing[i]; 1464 auto Phi = GuardPredicates[Out]; 1465 if (Out != Succ0 && Out != Succ1) { 1466 Phi->addIncoming(BoolFalse, In); 1467 continue; 1468 } 1469 // Optimization: When only one successor is an outgoing block, 1470 // the predicate is always true. 1471 if (!Succ0 || !Succ1 || OneSuccessorDone) { 1472 Phi->addIncoming(BoolTrue, In); 1473 continue; 1474 } 1475 assert(Succ0 && Succ1); 1476 OneSuccessorDone = true; 1477 if (Out == Succ0) { 1478 Phi->addIncoming(Condition, In); 1479 continue; 1480 } 1481 auto Inverted = invertCondition(Condition); 1482 DeletionCandidates.push_back(Condition); 1483 Phi->addIncoming(Inverted, In); 1484 } 1485 } 1486 } 1487 1488 // For each outgoing block OutBB, create a guard block in the Hub. The 1489 // first guard block was already created outside, and available as the 1490 // first element in the vector of guard blocks. 1491 // 1492 // Each guard block terminates in a conditional branch that transfers 1493 // control to the corresponding outgoing block or the next guard 1494 // block. The last guard block has two outgoing blocks as successors 1495 // since the condition for the final outgoing block is trivially 1496 // true. So we create one less block (including the first guard block) 1497 // than the number of outgoing blocks. 1498 static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks, 1499 Function *F, const BBSetVector &Outgoing, 1500 BBPredicates &GuardPredicates, StringRef Prefix) { 1501 for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) { 1502 GuardBlocks.push_back( 1503 BasicBlock::Create(F->getContext(), Prefix + ".guard", F)); 1504 } 1505 assert(GuardBlocks.size() == GuardPredicates.size()); 1506 1507 // To help keep the loop simple, temporarily append the last 1508 // outgoing block to the list of guard blocks. 1509 GuardBlocks.push_back(Outgoing.back()); 1510 1511 for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) { 1512 auto Out = Outgoing[i]; 1513 assert(GuardPredicates.count(Out)); 1514 BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out], 1515 GuardBlocks[i]); 1516 } 1517 1518 // Remove the last block from the guard list. 1519 GuardBlocks.pop_back(); 1520 } 1521 1522 BasicBlock *llvm::CreateControlFlowHub( 1523 DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks, 1524 const BBSetVector &Incoming, const BBSetVector &Outgoing, 1525 const StringRef Prefix) { 1526 auto F = Incoming.front()->getParent(); 1527 auto FirstGuardBlock = 1528 BasicBlock::Create(F->getContext(), Prefix + ".guard", F); 1529 1530 SmallVector<DominatorTree::UpdateType, 16> Updates; 1531 if (DTU) { 1532 for (auto In : Incoming) { 1533 Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock}); 1534 for (auto Succ : successors(In)) { 1535 if (Outgoing.count(Succ)) 1536 Updates.push_back({DominatorTree::Delete, In, Succ}); 1537 } 1538 } 1539 } 1540 1541 BBPredicates GuardPredicates; 1542 SmallVector<WeakVH, 8> DeletionCandidates; 1543 convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates, 1544 Incoming, Outgoing); 1545 1546 GuardBlocks.push_back(FirstGuardBlock); 1547 createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix); 1548 1549 // Update the PHINodes in each outgoing block to match the new control flow. 1550 for (int i = 0, e = GuardBlocks.size(); i != e; ++i) { 1551 reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock); 1552 } 1553 reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock); 1554 1555 if (DTU) { 1556 int NumGuards = GuardBlocks.size(); 1557 assert((int)Outgoing.size() == NumGuards + 1); 1558 for (int i = 0; i != NumGuards - 1; ++i) { 1559 Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]}); 1560 Updates.push_back( 1561 {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]}); 1562 } 1563 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1564 Outgoing[NumGuards - 1]}); 1565 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1566 Outgoing[NumGuards]}); 1567 DTU->applyUpdates(Updates); 1568 } 1569 1570 for (auto I : DeletionCandidates) { 1571 if (I->use_empty()) 1572 if (auto Inst = dyn_cast_or_null<Instruction>(I)) 1573 Inst->eraseFromParent(); 1574 } 1575 1576 return FirstGuardBlock; 1577 } 1578