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/PseudoProbe.h" 37 #include "llvm/IR/Type.h" 38 #include "llvm/IR/User.h" 39 #include "llvm/IR/Value.h" 40 #include "llvm/IR/ValueHandle.h" 41 #include "llvm/Support/Casting.h" 42 #include "llvm/Support/CommandLine.h" 43 #include "llvm/Support/Debug.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Transforms/Utils/Local.h" 46 #include <cassert> 47 #include <cstdint> 48 #include <string> 49 #include <utility> 50 #include <vector> 51 52 using namespace llvm; 53 54 #define DEBUG_TYPE "basicblock-utils" 55 56 static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth( 57 "max-deopt-or-unreachable-succ-check-depth", cl::init(8), cl::Hidden, 58 cl::desc("Set the maximum path length when checking whether a basic block " 59 "is followed by a block that either has a terminating " 60 "deoptimizing call or is terminated with an unreachable")); 61 62 void llvm::DetatchDeadBlocks( 63 ArrayRef<BasicBlock *> BBs, 64 SmallVectorImpl<DominatorTree::UpdateType> *Updates, 65 bool KeepOneInputPHIs) { 66 for (auto *BB : BBs) { 67 // Loop through all of our successors and make sure they know that one 68 // of their predecessors is going away. 69 SmallPtrSet<BasicBlock *, 4> UniqueSuccessors; 70 for (BasicBlock *Succ : successors(BB)) { 71 Succ->removePredecessor(BB, KeepOneInputPHIs); 72 if (Updates && UniqueSuccessors.insert(Succ).second) 73 Updates->push_back({DominatorTree::Delete, BB, Succ}); 74 } 75 76 // Zap all the instructions in the block. 77 while (!BB->empty()) { 78 Instruction &I = BB->back(); 79 // If this instruction is used, replace uses with an arbitrary value. 80 // Because control flow can't get here, we don't care what we replace the 81 // value with. Note that since this block is unreachable, and all values 82 // contained within it must dominate their uses, that all uses will 83 // eventually be removed (they are themselves dead). 84 if (!I.use_empty()) 85 I.replaceAllUsesWith(UndefValue::get(I.getType())); 86 BB->getInstList().pop_back(); 87 } 88 new UnreachableInst(BB->getContext(), BB); 89 assert(BB->getInstList().size() == 1 && 90 isa<UnreachableInst>(BB->getTerminator()) && 91 "The successor list of BB isn't empty before " 92 "applying corresponding DTU updates."); 93 } 94 } 95 96 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU, 97 bool KeepOneInputPHIs) { 98 DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs); 99 } 100 101 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU, 102 bool KeepOneInputPHIs) { 103 #ifndef NDEBUG 104 // Make sure that all predecessors of each dead block is also dead. 105 SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end()); 106 assert(Dead.size() == BBs.size() && "Duplicating blocks?"); 107 for (auto *BB : Dead) 108 for (BasicBlock *Pred : predecessors(BB)) 109 assert(Dead.count(Pred) && "All predecessors must be dead!"); 110 #endif 111 112 SmallVector<DominatorTree::UpdateType, 4> Updates; 113 DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs); 114 115 if (DTU) 116 DTU->applyUpdates(Updates); 117 118 for (BasicBlock *BB : BBs) 119 if (DTU) 120 DTU->deleteBB(BB); 121 else 122 BB->eraseFromParent(); 123 } 124 125 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU, 126 bool KeepOneInputPHIs) { 127 df_iterator_default_set<BasicBlock*> Reachable; 128 129 // Mark all reachable blocks. 130 for (BasicBlock *BB : depth_first_ext(&F, Reachable)) 131 (void)BB/* Mark all reachable blocks */; 132 133 // Collect all dead blocks. 134 std::vector<BasicBlock*> DeadBlocks; 135 for (BasicBlock &BB : F) 136 if (!Reachable.count(&BB)) 137 DeadBlocks.push_back(&BB); 138 139 // Delete the dead blocks. 140 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs); 141 142 return !DeadBlocks.empty(); 143 } 144 145 bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB, 146 MemoryDependenceResults *MemDep) { 147 if (!isa<PHINode>(BB->begin())) 148 return false; 149 150 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 151 if (PN->getIncomingValue(0) != PN) 152 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 153 else 154 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 155 156 if (MemDep) 157 MemDep->removeInstruction(PN); // Memdep updates AA itself. 158 159 PN->eraseFromParent(); 160 } 161 return true; 162 } 163 164 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI, 165 MemorySSAUpdater *MSSAU) { 166 // Recursively deleting a PHI may cause multiple PHIs to be deleted 167 // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete. 168 SmallVector<WeakTrackingVH, 8> PHIs; 169 for (PHINode &PN : BB->phis()) 170 PHIs.push_back(&PN); 171 172 bool Changed = false; 173 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) 174 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) 175 Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU); 176 177 return Changed; 178 } 179 180 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, 181 LoopInfo *LI, MemorySSAUpdater *MSSAU, 182 MemoryDependenceResults *MemDep, 183 bool PredecessorWithTwoSuccessors) { 184 if (BB->hasAddressTaken()) 185 return false; 186 187 // Can't merge if there are multiple predecessors, or no predecessors. 188 BasicBlock *PredBB = BB->getUniquePredecessor(); 189 if (!PredBB) return false; 190 191 // Don't break self-loops. 192 if (PredBB == BB) return false; 193 // Don't break unwinding instructions. 194 if (PredBB->getTerminator()->isExceptionalTerminator()) 195 return false; 196 197 // Can't merge if there are multiple distinct successors. 198 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB) 199 return false; 200 201 // Currently only allow PredBB to have two predecessors, one being BB. 202 // Update BI to branch to BB's only successor instead of BB. 203 BranchInst *PredBB_BI; 204 BasicBlock *NewSucc = nullptr; 205 unsigned FallThruPath; 206 if (PredecessorWithTwoSuccessors) { 207 if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator()))) 208 return false; 209 BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator()); 210 if (!BB_JmpI || !BB_JmpI->isUnconditional()) 211 return false; 212 NewSucc = BB_JmpI->getSuccessor(0); 213 FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1; 214 } 215 216 // Can't merge if there is PHI loop. 217 for (PHINode &PN : BB->phis()) 218 if (llvm::is_contained(PN.incoming_values(), &PN)) 219 return false; 220 221 LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into " 222 << PredBB->getName() << "\n"); 223 224 // Begin by getting rid of unneeded PHIs. 225 SmallVector<AssertingVH<Value>, 4> IncomingValues; 226 if (isa<PHINode>(BB->front())) { 227 for (PHINode &PN : BB->phis()) 228 if (!isa<PHINode>(PN.getIncomingValue(0)) || 229 cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB) 230 IncomingValues.push_back(PN.getIncomingValue(0)); 231 FoldSingleEntryPHINodes(BB, MemDep); 232 } 233 234 // DTU update: Collect all the edges that exit BB. 235 // These dominator edges will be redirected from Pred. 236 std::vector<DominatorTree::UpdateType> Updates; 237 if (DTU) { 238 SmallPtrSet<BasicBlock *, 2> SuccsOfBB(succ_begin(BB), succ_end(BB)); 239 SmallPtrSet<BasicBlock *, 2> SuccsOfPredBB(succ_begin(PredBB), 240 succ_end(PredBB)); 241 Updates.reserve(Updates.size() + 2 * SuccsOfBB.size() + 1); 242 // Add insert edges first. Experimentally, for the particular case of two 243 // blocks that can be merged, with a single successor and single predecessor 244 // respectively, it is beneficial to have all insert updates first. Deleting 245 // edges first may lead to unreachable blocks, followed by inserting edges 246 // making the blocks reachable again. Such DT updates lead to high compile 247 // times. We add inserts before deletes here to reduce compile time. 248 for (BasicBlock *SuccOfBB : SuccsOfBB) 249 // This successor of BB may already be a PredBB's successor. 250 if (!SuccsOfPredBB.contains(SuccOfBB)) 251 Updates.push_back({DominatorTree::Insert, PredBB, SuccOfBB}); 252 for (BasicBlock *SuccOfBB : SuccsOfBB) 253 Updates.push_back({DominatorTree::Delete, BB, SuccOfBB}); 254 Updates.push_back({DominatorTree::Delete, PredBB, BB}); 255 } 256 257 Instruction *PTI = PredBB->getTerminator(); 258 Instruction *STI = BB->getTerminator(); 259 Instruction *Start = &*BB->begin(); 260 // If there's nothing to move, mark the starting instruction as the last 261 // instruction in the block. Terminator instruction is handled separately. 262 if (Start == STI) 263 Start = PTI; 264 265 // Move all definitions in the successor to the predecessor... 266 PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(), 267 BB->begin(), STI->getIterator()); 268 269 if (MSSAU) 270 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start); 271 272 // Make all PHI nodes that referred to BB now refer to Pred as their 273 // source... 274 BB->replaceAllUsesWith(PredBB); 275 276 if (PredecessorWithTwoSuccessors) { 277 // Delete the unconditional branch from BB. 278 BB->getInstList().pop_back(); 279 280 // Update branch in the predecessor. 281 PredBB_BI->setSuccessor(FallThruPath, NewSucc); 282 } else { 283 // Delete the unconditional branch from the predecessor. 284 PredBB->getInstList().pop_back(); 285 286 // Move terminator instruction. 287 PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); 288 289 // Terminator may be a memory accessing instruction too. 290 if (MSSAU) 291 if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>( 292 MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator()))) 293 MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End); 294 } 295 // Add unreachable to now empty BB. 296 new UnreachableInst(BB->getContext(), BB); 297 298 // Inherit predecessors name if it exists. 299 if (!PredBB->hasName()) 300 PredBB->takeName(BB); 301 302 if (LI) 303 LI->removeBlock(BB); 304 305 if (MemDep) 306 MemDep->invalidateCachedPredecessors(); 307 308 if (DTU) 309 DTU->applyUpdates(Updates); 310 311 // Finally, erase the old block and update dominator info. 312 DeleteDeadBlock(BB, DTU); 313 314 return true; 315 } 316 317 bool llvm::MergeBlockSuccessorsIntoGivenBlocks( 318 SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU, 319 LoopInfo *LI) { 320 assert(!MergeBlocks.empty() && "MergeBlocks should not be empty"); 321 322 bool BlocksHaveBeenMerged = false; 323 while (!MergeBlocks.empty()) { 324 BasicBlock *BB = *MergeBlocks.begin(); 325 BasicBlock *Dest = BB->getSingleSuccessor(); 326 if (Dest && (!L || L->contains(Dest))) { 327 BasicBlock *Fold = Dest->getUniquePredecessor(); 328 (void)Fold; 329 if (MergeBlockIntoPredecessor(Dest, DTU, LI)) { 330 assert(Fold == BB && 331 "Expecting BB to be unique predecessor of the Dest block"); 332 MergeBlocks.erase(Dest); 333 BlocksHaveBeenMerged = true; 334 } else 335 MergeBlocks.erase(BB); 336 } else 337 MergeBlocks.erase(BB); 338 } 339 return BlocksHaveBeenMerged; 340 } 341 342 /// Remove redundant instructions within sequences of consecutive dbg.value 343 /// instructions. This is done using a backward scan to keep the last dbg.value 344 /// describing a specific variable/fragment. 345 /// 346 /// BackwardScan strategy: 347 /// ---------------------- 348 /// Given a sequence of consecutive DbgValueInst like this 349 /// 350 /// dbg.value ..., "x", FragmentX1 (*) 351 /// dbg.value ..., "y", FragmentY1 352 /// dbg.value ..., "x", FragmentX2 353 /// dbg.value ..., "x", FragmentX1 (**) 354 /// 355 /// then the instruction marked with (*) can be removed (it is guaranteed to be 356 /// obsoleted by the instruction marked with (**) as the latter instruction is 357 /// describing the same variable using the same fragment info). 358 /// 359 /// Possible improvements: 360 /// - Check fully overlapping fragments and not only identical fragments. 361 /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta 362 /// instructions being part of the sequence of consecutive instructions. 363 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) { 364 SmallVector<DbgValueInst *, 8> ToBeRemoved; 365 SmallDenseSet<DebugVariable> VariableSet; 366 for (auto &I : reverse(*BB)) { 367 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 368 DebugVariable Key(DVI->getVariable(), 369 DVI->getExpression(), 370 DVI->getDebugLoc()->getInlinedAt()); 371 auto R = VariableSet.insert(Key); 372 // If the same variable fragment is described more than once it is enough 373 // to keep the last one (i.e. the first found since we for reverse 374 // iteration). 375 if (!R.second) 376 ToBeRemoved.push_back(DVI); 377 continue; 378 } 379 // Sequence with consecutive dbg.value instrs ended. Clear the map to 380 // restart identifying redundant instructions if case we find another 381 // dbg.value sequence. 382 VariableSet.clear(); 383 } 384 385 for (auto &Instr : ToBeRemoved) 386 Instr->eraseFromParent(); 387 388 return !ToBeRemoved.empty(); 389 } 390 391 /// Remove redundant dbg.value instructions using a forward scan. This can 392 /// remove a dbg.value instruction that is redundant due to indicating that a 393 /// variable has the same value as already being indicated by an earlier 394 /// dbg.value. 395 /// 396 /// ForwardScan strategy: 397 /// --------------------- 398 /// Given two identical dbg.value instructions, separated by a block of 399 /// instructions that isn't describing the same variable, like this 400 /// 401 /// dbg.value X1, "x", FragmentX1 (**) 402 /// <block of instructions, none being "dbg.value ..., "x", ..."> 403 /// dbg.value X1, "x", FragmentX1 (*) 404 /// 405 /// then the instruction marked with (*) can be removed. Variable "x" is already 406 /// described as being mapped to the SSA value X1. 407 /// 408 /// Possible improvements: 409 /// - Keep track of non-overlapping fragments. 410 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) { 411 SmallVector<DbgValueInst *, 8> ToBeRemoved; 412 DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>> 413 VariableMap; 414 for (auto &I : *BB) { 415 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 416 DebugVariable Key(DVI->getVariable(), 417 NoneType(), 418 DVI->getDebugLoc()->getInlinedAt()); 419 auto VMI = VariableMap.find(Key); 420 // Update the map if we found a new value/expression describing the 421 // variable, or if the variable wasn't mapped already. 422 SmallVector<Value *, 4> Values(DVI->getValues()); 423 if (VMI == VariableMap.end() || VMI->second.first != Values || 424 VMI->second.second != DVI->getExpression()) { 425 VariableMap[Key] = {Values, DVI->getExpression()}; 426 continue; 427 } 428 // Found an identical mapping. Remember the instruction for later removal. 429 ToBeRemoved.push_back(DVI); 430 } 431 } 432 433 for (auto &Instr : ToBeRemoved) 434 Instr->eraseFromParent(); 435 436 return !ToBeRemoved.empty(); 437 } 438 439 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) { 440 bool MadeChanges = false; 441 // By using the "backward scan" strategy before the "forward scan" strategy we 442 // can remove both dbg.value (2) and (3) in a situation like this: 443 // 444 // (1) dbg.value V1, "x", DIExpression() 445 // ... 446 // (2) dbg.value V2, "x", DIExpression() 447 // (3) dbg.value V1, "x", DIExpression() 448 // 449 // The backward scan will remove (2), it is made obsolete by (3). After 450 // getting (2) out of the way, the foward scan will remove (3) since "x" 451 // already is described as having the value V1 at (1). 452 MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB); 453 MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB); 454 455 if (MadeChanges) 456 LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: " 457 << BB->getName() << "\n"); 458 return MadeChanges; 459 } 460 461 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, 462 BasicBlock::iterator &BI, Value *V) { 463 Instruction &I = *BI; 464 // Replaces all of the uses of the instruction with uses of the value 465 I.replaceAllUsesWith(V); 466 467 // Make sure to propagate a name if there is one already. 468 if (I.hasName() && !V->hasName()) 469 V->takeName(&I); 470 471 // Delete the unnecessary instruction now... 472 BI = BIL.erase(BI); 473 } 474 475 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, 476 BasicBlock::iterator &BI, Instruction *I) { 477 assert(I->getParent() == nullptr && 478 "ReplaceInstWithInst: Instruction already inserted into basic block!"); 479 480 // Copy debug location to newly added instruction, if it wasn't already set 481 // by the caller. 482 if (!I->getDebugLoc()) 483 I->setDebugLoc(BI->getDebugLoc()); 484 485 // Insert the new instruction into the basic block... 486 BasicBlock::iterator New = BIL.insert(BI, I); 487 488 // Replace all uses of the old instruction, and delete it. 489 ReplaceInstWithValue(BIL, BI, I); 490 491 // Move BI back to point to the newly inserted instruction 492 BI = New; 493 } 494 495 bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) { 496 // Remember visited blocks to avoid infinite loop 497 SmallPtrSet<const BasicBlock *, 8> VisitedBlocks; 498 unsigned Depth = 0; 499 while (BB && Depth++ < MaxDeoptOrUnreachableSuccessorCheckDepth && 500 VisitedBlocks.insert(BB).second) { 501 if (BB->getTerminatingDeoptimizeCall() || 502 isa<UnreachableInst>(BB->getTerminator())) 503 return true; 504 BB = BB->getUniqueSuccessor(); 505 } 506 return false; 507 } 508 509 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { 510 BasicBlock::iterator BI(From); 511 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); 512 } 513 514 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT, 515 LoopInfo *LI, MemorySSAUpdater *MSSAU, 516 const Twine &BBName) { 517 unsigned SuccNum = GetSuccessorNumber(BB, Succ); 518 519 Instruction *LatchTerm = BB->getTerminator(); 520 521 CriticalEdgeSplittingOptions Options = 522 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA(); 523 524 if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) { 525 // If it is a critical edge, and the succesor is an exception block, handle 526 // the split edge logic in this specific function 527 if (Succ->isEHPad()) 528 return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName); 529 530 // If this is a critical edge, let SplitKnownCriticalEdge do it. 531 return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName); 532 } 533 534 // If the edge isn't critical, then BB has a single successor or Succ has a 535 // single pred. Split the block. 536 if (BasicBlock *SP = Succ->getSinglePredecessor()) { 537 // If the successor only has a single pred, split the top of the successor 538 // block. 539 assert(SP == BB && "CFG broken"); 540 SP = nullptr; 541 return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName, 542 /*Before=*/true); 543 } 544 545 // Otherwise, if BB has a single successor, split it at the bottom of the 546 // block. 547 assert(BB->getTerminator()->getNumSuccessors() == 1 && 548 "Should have a single succ!"); 549 return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName); 550 } 551 552 void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) { 553 if (auto *II = dyn_cast<InvokeInst>(TI)) 554 II->setUnwindDest(Succ); 555 else if (auto *CS = dyn_cast<CatchSwitchInst>(TI)) 556 CS->setUnwindDest(Succ); 557 else if (auto *CR = dyn_cast<CleanupReturnInst>(TI)) 558 CR->setUnwindDest(Succ); 559 else 560 llvm_unreachable("unexpected terminator instruction"); 561 } 562 563 void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred, 564 BasicBlock *NewPred, PHINode *Until) { 565 int BBIdx = 0; 566 for (PHINode &PN : DestBB->phis()) { 567 // We manually update the LandingPadReplacement PHINode and it is the last 568 // PHI Node. So, if we find it, we are done. 569 if (Until == &PN) 570 break; 571 572 // Reuse the previous value of BBIdx if it lines up. In cases where we 573 // have multiple phi nodes with *lots* of predecessors, this is a speed 574 // win because we don't have to scan the PHI looking for TIBB. This 575 // happens because the BB list of PHI nodes are usually in the same 576 // order. 577 if (PN.getIncomingBlock(BBIdx) != OldPred) 578 BBIdx = PN.getBasicBlockIndex(OldPred); 579 580 assert(BBIdx != -1 && "Invalid PHI Index!"); 581 PN.setIncomingBlock(BBIdx, NewPred); 582 } 583 } 584 585 BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ, 586 LandingPadInst *OriginalPad, 587 PHINode *LandingPadReplacement, 588 const CriticalEdgeSplittingOptions &Options, 589 const Twine &BBName) { 590 591 auto *PadInst = Succ->getFirstNonPHI(); 592 if (!LandingPadReplacement && !PadInst->isEHPad()) 593 return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName); 594 595 auto *LI = Options.LI; 596 SmallVector<BasicBlock *, 4> LoopPreds; 597 // Check if extra modifications will be required to preserve loop-simplify 598 // form after splitting. If it would require splitting blocks with IndirectBr 599 // terminators, bail out if preserving loop-simplify form is requested. 600 if (Options.PreserveLoopSimplify && LI) { 601 if (Loop *BBLoop = LI->getLoopFor(BB)) { 602 603 // The only way that we can break LoopSimplify form by splitting a 604 // critical edge is when there exists some edge from BBLoop to Succ *and* 605 // the only edge into Succ from outside of BBLoop is that of NewBB after 606 // the split. If the first isn't true, then LoopSimplify still holds, 607 // NewBB is the new exit block and it has no non-loop predecessors. If the 608 // second isn't true, then Succ was not in LoopSimplify form prior to 609 // the split as it had a non-loop predecessor. In both of these cases, 610 // the predecessor must be directly in BBLoop, not in a subloop, or again 611 // LoopSimplify doesn't hold. 612 for (BasicBlock *P : predecessors(Succ)) { 613 if (P == BB) 614 continue; // The new block is known. 615 if (LI->getLoopFor(P) != BBLoop) { 616 // Loop is not in LoopSimplify form, no need to re simplify after 617 // splitting edge. 618 LoopPreds.clear(); 619 break; 620 } 621 LoopPreds.push_back(P); 622 } 623 // Loop-simplify form can be preserved, if we can split all in-loop 624 // predecessors. 625 if (any_of(LoopPreds, [](BasicBlock *Pred) { 626 return isa<IndirectBrInst>(Pred->getTerminator()); 627 })) { 628 return nullptr; 629 } 630 } 631 } 632 633 auto *NewBB = 634 BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ); 635 setUnwindEdgeTo(BB->getTerminator(), NewBB); 636 updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement); 637 638 if (LandingPadReplacement) { 639 auto *NewLP = OriginalPad->clone(); 640 auto *Terminator = BranchInst::Create(Succ, NewBB); 641 NewLP->insertBefore(Terminator); 642 LandingPadReplacement->addIncoming(NewLP, NewBB); 643 } else { 644 Value *ParentPad = nullptr; 645 if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst)) 646 ParentPad = FuncletPad->getParentPad(); 647 else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst)) 648 ParentPad = CatchSwitch->getParentPad(); 649 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst)) 650 ParentPad = CleanupPad->getParentPad(); 651 else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst)) 652 ParentPad = LandingPad->getParent(); 653 else 654 llvm_unreachable("handling for other EHPads not implemented yet"); 655 656 auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB); 657 CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB); 658 } 659 660 auto *DT = Options.DT; 661 auto *MSSAU = Options.MSSAU; 662 if (!DT && !LI) 663 return NewBB; 664 665 if (DT) { 666 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 667 SmallVector<DominatorTree::UpdateType, 3> Updates; 668 669 Updates.push_back({DominatorTree::Insert, BB, NewBB}); 670 Updates.push_back({DominatorTree::Insert, NewBB, Succ}); 671 Updates.push_back({DominatorTree::Delete, BB, Succ}); 672 673 DTU.applyUpdates(Updates); 674 DTU.flush(); 675 676 if (MSSAU) { 677 MSSAU->applyUpdates(Updates, *DT); 678 if (VerifyMemorySSA) 679 MSSAU->getMemorySSA()->verifyMemorySSA(); 680 } 681 } 682 683 if (LI) { 684 if (Loop *BBLoop = LI->getLoopFor(BB)) { 685 // If one or the other blocks were not in a loop, the new block is not 686 // either, and thus LI doesn't need to be updated. 687 if (Loop *SuccLoop = LI->getLoopFor(Succ)) { 688 if (BBLoop == SuccLoop) { 689 // Both in the same loop, the NewBB joins loop. 690 SuccLoop->addBasicBlockToLoop(NewBB, *LI); 691 } else if (BBLoop->contains(SuccLoop)) { 692 // Edge from an outer loop to an inner loop. Add to the outer loop. 693 BBLoop->addBasicBlockToLoop(NewBB, *LI); 694 } else if (SuccLoop->contains(BBLoop)) { 695 // Edge from an inner loop to an outer loop. Add to the outer loop. 696 SuccLoop->addBasicBlockToLoop(NewBB, *LI); 697 } else { 698 // Edge from two loops with no containment relation. Because these 699 // are natural loops, we know that the destination block must be the 700 // header of its loop (adding a branch into a loop elsewhere would 701 // create an irreducible loop). 702 assert(SuccLoop->getHeader() == Succ && 703 "Should not create irreducible loops!"); 704 if (Loop *P = SuccLoop->getParentLoop()) 705 P->addBasicBlockToLoop(NewBB, *LI); 706 } 707 } 708 709 // If BB is in a loop and Succ is outside of that loop, we may need to 710 // update LoopSimplify form and LCSSA form. 711 if (!BBLoop->contains(Succ)) { 712 assert(!BBLoop->contains(NewBB) && 713 "Split point for loop exit is contained in loop!"); 714 715 // Update LCSSA form in the newly created exit block. 716 if (Options.PreserveLCSSA) { 717 createPHIsForSplitLoopExit(BB, NewBB, Succ); 718 } 719 720 if (!LoopPreds.empty()) { 721 BasicBlock *NewExitBB = SplitBlockPredecessors( 722 Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA); 723 if (Options.PreserveLCSSA) 724 createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ); 725 } 726 } 727 } 728 } 729 730 return NewBB; 731 } 732 733 void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds, 734 BasicBlock *SplitBB, BasicBlock *DestBB) { 735 // SplitBB shouldn't have anything non-trivial in it yet. 736 assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() || 737 SplitBB->isLandingPad()) && 738 "SplitBB has non-PHI nodes!"); 739 740 // For each PHI in the destination block. 741 for (PHINode &PN : DestBB->phis()) { 742 int Idx = PN.getBasicBlockIndex(SplitBB); 743 assert(Idx >= 0 && "Invalid Block Index"); 744 Value *V = PN.getIncomingValue(Idx); 745 746 // If the input is a PHI which already satisfies LCSSA, don't create 747 // a new one. 748 if (const PHINode *VP = dyn_cast<PHINode>(V)) 749 if (VP->getParent() == SplitBB) 750 continue; 751 752 // Otherwise a new PHI is needed. Create one and populate it. 753 PHINode *NewPN = PHINode::Create( 754 PN.getType(), Preds.size(), "split", 755 SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator()); 756 for (BasicBlock *BB : Preds) 757 NewPN->addIncoming(V, BB); 758 759 // Update the original PHI. 760 PN.setIncomingValue(Idx, NewPN); 761 } 762 } 763 764 unsigned 765 llvm::SplitAllCriticalEdges(Function &F, 766 const CriticalEdgeSplittingOptions &Options) { 767 unsigned NumBroken = 0; 768 for (BasicBlock &BB : F) { 769 Instruction *TI = BB.getTerminator(); 770 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) && 771 !isa<CallBrInst>(TI)) 772 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 773 if (SplitCriticalEdge(TI, i, Options)) 774 ++NumBroken; 775 } 776 return NumBroken; 777 } 778 779 static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt, 780 DomTreeUpdater *DTU, DominatorTree *DT, 781 LoopInfo *LI, MemorySSAUpdater *MSSAU, 782 const Twine &BBName, bool Before) { 783 if (Before) { 784 DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 785 return splitBlockBefore(Old, SplitPt, 786 DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU, 787 BBName); 788 } 789 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 790 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) { 791 ++SplitIt; 792 assert(SplitIt != SplitPt->getParent()->end()); 793 } 794 std::string Name = BBName.str(); 795 BasicBlock *New = Old->splitBasicBlock( 796 SplitIt, Name.empty() ? Old->getName() + ".split" : Name); 797 798 // The new block lives in whichever loop the old one did. This preserves 799 // LCSSA as well, because we force the split point to be after any PHI nodes. 800 if (LI) 801 if (Loop *L = LI->getLoopFor(Old)) 802 L->addBasicBlockToLoop(New, *LI); 803 804 if (DTU) { 805 SmallVector<DominatorTree::UpdateType, 8> Updates; 806 // Old dominates New. New node dominates all other nodes dominated by Old. 807 SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld(succ_begin(New), 808 succ_end(New)); 809 Updates.push_back({DominatorTree::Insert, Old, New}); 810 Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfOld.size()); 811 for (BasicBlock *UniqueSuccessorOfOld : UniqueSuccessorsOfOld) { 812 Updates.push_back({DominatorTree::Insert, New, UniqueSuccessorOfOld}); 813 Updates.push_back({DominatorTree::Delete, Old, UniqueSuccessorOfOld}); 814 } 815 816 DTU->applyUpdates(Updates); 817 } else if (DT) 818 // Old dominates New. New node dominates all other nodes dominated by Old. 819 if (DomTreeNode *OldNode = DT->getNode(Old)) { 820 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 821 822 DomTreeNode *NewNode = DT->addNewBlock(New, Old); 823 for (DomTreeNode *I : Children) 824 DT->changeImmediateDominator(I, NewNode); 825 } 826 827 // Move MemoryAccesses still tracked in Old, but part of New now. 828 // Update accesses in successor blocks accordingly. 829 if (MSSAU) 830 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin())); 831 832 return New; 833 } 834 835 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 836 DominatorTree *DT, LoopInfo *LI, 837 MemorySSAUpdater *MSSAU, const Twine &BBName, 838 bool Before) { 839 return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName, 840 Before); 841 } 842 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 843 DomTreeUpdater *DTU, LoopInfo *LI, 844 MemorySSAUpdater *MSSAU, const Twine &BBName, 845 bool Before) { 846 return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName, 847 Before); 848 } 849 850 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, 851 DomTreeUpdater *DTU, LoopInfo *LI, 852 MemorySSAUpdater *MSSAU, 853 const Twine &BBName) { 854 855 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 856 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 857 ++SplitIt; 858 std::string Name = BBName.str(); 859 BasicBlock *New = Old->splitBasicBlock( 860 SplitIt, Name.empty() ? Old->getName() + ".split" : Name, 861 /* Before=*/true); 862 863 // The new block lives in whichever loop the old one did. This preserves 864 // LCSSA as well, because we force the split point to be after any PHI nodes. 865 if (LI) 866 if (Loop *L = LI->getLoopFor(Old)) 867 L->addBasicBlockToLoop(New, *LI); 868 869 if (DTU) { 870 SmallVector<DominatorTree::UpdateType, 8> DTUpdates; 871 // New dominates Old. The predecessor nodes of the Old node dominate 872 // New node. 873 SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld(pred_begin(New), 874 pred_end(New)); 875 DTUpdates.push_back({DominatorTree::Insert, New, Old}); 876 DTUpdates.reserve(DTUpdates.size() + 2 * UniquePredecessorsOfOld.size()); 877 for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) { 878 DTUpdates.push_back({DominatorTree::Insert, UniquePredecessorOfOld, New}); 879 DTUpdates.push_back({DominatorTree::Delete, UniquePredecessorOfOld, Old}); 880 } 881 882 DTU->applyUpdates(DTUpdates); 883 884 // Move MemoryAccesses still tracked in Old, but part of New now. 885 // Update accesses in successor blocks accordingly. 886 if (MSSAU) { 887 MSSAU->applyUpdates(DTUpdates, DTU->getDomTree()); 888 if (VerifyMemorySSA) 889 MSSAU->getMemorySSA()->verifyMemorySSA(); 890 } 891 } 892 return New; 893 } 894 895 /// Update DominatorTree, LoopInfo, and LCCSA analysis information. 896 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, 897 ArrayRef<BasicBlock *> Preds, 898 DomTreeUpdater *DTU, DominatorTree *DT, 899 LoopInfo *LI, MemorySSAUpdater *MSSAU, 900 bool PreserveLCSSA, bool &HasLoopExit) { 901 // Update dominator tree if available. 902 if (DTU) { 903 // Recalculation of DomTree is needed when updating a forward DomTree and 904 // the Entry BB is replaced. 905 if (NewBB->isEntryBlock() && DTU->hasDomTree()) { 906 // The entry block was removed and there is no external interface for 907 // the dominator tree to be notified of this change. In this corner-case 908 // we recalculate the entire tree. 909 DTU->recalculate(*NewBB->getParent()); 910 } else { 911 // Split block expects NewBB to have a non-empty set of predecessors. 912 SmallVector<DominatorTree::UpdateType, 8> Updates; 913 SmallPtrSet<BasicBlock *, 8> UniquePreds(Preds.begin(), Preds.end()); 914 Updates.push_back({DominatorTree::Insert, NewBB, OldBB}); 915 Updates.reserve(Updates.size() + 2 * UniquePreds.size()); 916 for (auto *UniquePred : UniquePreds) { 917 Updates.push_back({DominatorTree::Insert, UniquePred, NewBB}); 918 Updates.push_back({DominatorTree::Delete, UniquePred, OldBB}); 919 } 920 DTU->applyUpdates(Updates); 921 } 922 } else if (DT) { 923 if (OldBB == DT->getRootNode()->getBlock()) { 924 assert(NewBB->isEntryBlock()); 925 DT->setNewRoot(NewBB); 926 } else { 927 // Split block expects NewBB to have a non-empty set of predecessors. 928 DT->splitBlock(NewBB); 929 } 930 } 931 932 // Update MemoryPhis after split if MemorySSA is available 933 if (MSSAU) 934 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds); 935 936 // The rest of the logic is only relevant for updating the loop structures. 937 if (!LI) 938 return; 939 940 if (DTU && DTU->hasDomTree()) 941 DT = &DTU->getDomTree(); 942 assert(DT && "DT should be available to update LoopInfo!"); 943 Loop *L = LI->getLoopFor(OldBB); 944 945 // If we need to preserve loop analyses, collect some information about how 946 // this split will affect loops. 947 bool IsLoopEntry = !!L; 948 bool SplitMakesNewLoopHeader = false; 949 for (BasicBlock *Pred : Preds) { 950 // Preds that are not reachable from entry should not be used to identify if 951 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks 952 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader 953 // as true and make the NewBB the header of some loop. This breaks LI. 954 if (!DT->isReachableFromEntry(Pred)) 955 continue; 956 // If we need to preserve LCSSA, determine if any of the preds is a loop 957 // exit. 958 if (PreserveLCSSA) 959 if (Loop *PL = LI->getLoopFor(Pred)) 960 if (!PL->contains(OldBB)) 961 HasLoopExit = true; 962 963 // If we need to preserve LoopInfo, note whether any of the preds crosses 964 // an interesting loop boundary. 965 if (!L) 966 continue; 967 if (L->contains(Pred)) 968 IsLoopEntry = false; 969 else 970 SplitMakesNewLoopHeader = true; 971 } 972 973 // Unless we have a loop for OldBB, nothing else to do here. 974 if (!L) 975 return; 976 977 if (IsLoopEntry) { 978 // Add the new block to the nearest enclosing loop (and not an adjacent 979 // loop). To find this, examine each of the predecessors and determine which 980 // loops enclose them, and select the most-nested loop which contains the 981 // loop containing the block being split. 982 Loop *InnermostPredLoop = nullptr; 983 for (BasicBlock *Pred : Preds) { 984 if (Loop *PredLoop = LI->getLoopFor(Pred)) { 985 // Seek a loop which actually contains the block being split (to avoid 986 // adjacent loops). 987 while (PredLoop && !PredLoop->contains(OldBB)) 988 PredLoop = PredLoop->getParentLoop(); 989 990 // Select the most-nested of these loops which contains the block. 991 if (PredLoop && PredLoop->contains(OldBB) && 992 (!InnermostPredLoop || 993 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) 994 InnermostPredLoop = PredLoop; 995 } 996 } 997 998 if (InnermostPredLoop) 999 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI); 1000 } else { 1001 L->addBasicBlockToLoop(NewBB, *LI); 1002 if (SplitMakesNewLoopHeader) 1003 L->moveToHeader(NewBB); 1004 } 1005 } 1006 1007 /// Update the PHI nodes in OrigBB to include the values coming from NewBB. 1008 /// This also updates AliasAnalysis, if available. 1009 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, 1010 ArrayRef<BasicBlock *> Preds, BranchInst *BI, 1011 bool HasLoopExit) { 1012 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB. 1013 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end()); 1014 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) { 1015 PHINode *PN = cast<PHINode>(I++); 1016 1017 // Check to see if all of the values coming in are the same. If so, we 1018 // don't need to create a new PHI node, unless it's needed for LCSSA. 1019 Value *InVal = nullptr; 1020 if (!HasLoopExit) { 1021 InVal = PN->getIncomingValueForBlock(Preds[0]); 1022 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 1023 if (!PredSet.count(PN->getIncomingBlock(i))) 1024 continue; 1025 if (!InVal) 1026 InVal = PN->getIncomingValue(i); 1027 else if (InVal != PN->getIncomingValue(i)) { 1028 InVal = nullptr; 1029 break; 1030 } 1031 } 1032 } 1033 1034 if (InVal) { 1035 // If all incoming values for the new PHI would be the same, just don't 1036 // make a new PHI. Instead, just remove the incoming values from the old 1037 // PHI. 1038 1039 // NOTE! This loop walks backwards for a reason! First off, this minimizes 1040 // the cost of removal if we end up removing a large number of values, and 1041 // second off, this ensures that the indices for the incoming values 1042 // aren't invalidated when we remove one. 1043 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) 1044 if (PredSet.count(PN->getIncomingBlock(i))) 1045 PN->removeIncomingValue(i, false); 1046 1047 // Add an incoming value to the PHI node in the loop for the preheader 1048 // edge. 1049 PN->addIncoming(InVal, NewBB); 1050 continue; 1051 } 1052 1053 // If the values coming into the block are not the same, we need a new 1054 // PHI. 1055 // Create the new PHI node, insert it into NewBB at the end of the block 1056 PHINode *NewPHI = 1057 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI); 1058 1059 // NOTE! This loop walks backwards for a reason! First off, this minimizes 1060 // the cost of removal if we end up removing a large number of values, and 1061 // second off, this ensures that the indices for the incoming values aren't 1062 // invalidated when we remove one. 1063 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) { 1064 BasicBlock *IncomingBB = PN->getIncomingBlock(i); 1065 if (PredSet.count(IncomingBB)) { 1066 Value *V = PN->removeIncomingValue(i, false); 1067 NewPHI->addIncoming(V, IncomingBB); 1068 } 1069 } 1070 1071 PN->addIncoming(NewPHI, NewBB); 1072 } 1073 } 1074 1075 static void SplitLandingPadPredecessorsImpl( 1076 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1, 1077 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 1078 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, 1079 MemorySSAUpdater *MSSAU, bool PreserveLCSSA); 1080 1081 static BasicBlock * 1082 SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 1083 const char *Suffix, DomTreeUpdater *DTU, 1084 DominatorTree *DT, LoopInfo *LI, 1085 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) { 1086 // Do not attempt to split that which cannot be split. 1087 if (!BB->canSplitPredecessors()) 1088 return nullptr; 1089 1090 // For the landingpads we need to act a bit differently. 1091 // Delegate this work to the SplitLandingPadPredecessors. 1092 if (BB->isLandingPad()) { 1093 SmallVector<BasicBlock*, 2> NewBBs; 1094 std::string NewName = std::string(Suffix) + ".split-lp"; 1095 1096 SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs, 1097 DTU, DT, LI, MSSAU, PreserveLCSSA); 1098 return NewBBs[0]; 1099 } 1100 1101 // Create new basic block, insert right before the original block. 1102 BasicBlock *NewBB = BasicBlock::Create( 1103 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB); 1104 1105 // The new block unconditionally branches to the old block. 1106 BranchInst *BI = BranchInst::Create(BB, NewBB); 1107 1108 Loop *L = nullptr; 1109 BasicBlock *OldLatch = nullptr; 1110 // Splitting the predecessors of a loop header creates a preheader block. 1111 if (LI && LI->isLoopHeader(BB)) { 1112 L = LI->getLoopFor(BB); 1113 // Using the loop start line number prevents debuggers stepping into the 1114 // loop body for this instruction. 1115 BI->setDebugLoc(L->getStartLoc()); 1116 1117 // If BB is the header of the Loop, it is possible that the loop is 1118 // modified, such that the current latch does not remain the latch of the 1119 // loop. If that is the case, the loop metadata from the current latch needs 1120 // to be applied to the new latch. 1121 OldLatch = L->getLoopLatch(); 1122 } else 1123 BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc()); 1124 1125 // Move the edges from Preds to point to NewBB instead of BB. 1126 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 1127 // This is slightly more strict than necessary; the minimum requirement 1128 // is that there be no more than one indirectbr branching to BB. And 1129 // all BlockAddress uses would need to be updated. 1130 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 1131 "Cannot split an edge from an IndirectBrInst"); 1132 assert(!isa<CallBrInst>(Preds[i]->getTerminator()) && 1133 "Cannot split an edge from a CallBrInst"); 1134 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); 1135 } 1136 1137 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI 1138 // node becomes an incoming value for BB's phi node. However, if the Preds 1139 // list is empty, we need to insert dummy entries into the PHI nodes in BB to 1140 // account for the newly created predecessor. 1141 if (Preds.empty()) { 1142 // Insert dummy values as the incoming value. 1143 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) 1144 cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); 1145 } 1146 1147 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 1148 bool HasLoopExit = false; 1149 UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA, 1150 HasLoopExit); 1151 1152 if (!Preds.empty()) { 1153 // Update the PHI nodes in BB with the values coming from NewBB. 1154 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit); 1155 } 1156 1157 if (OldLatch) { 1158 BasicBlock *NewLatch = L->getLoopLatch(); 1159 if (NewLatch != OldLatch) { 1160 MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop"); 1161 NewLatch->getTerminator()->setMetadata("llvm.loop", MD); 1162 OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr); 1163 } 1164 } 1165 1166 return NewBB; 1167 } 1168 1169 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 1170 ArrayRef<BasicBlock *> Preds, 1171 const char *Suffix, DominatorTree *DT, 1172 LoopInfo *LI, MemorySSAUpdater *MSSAU, 1173 bool PreserveLCSSA) { 1174 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI, 1175 MSSAU, PreserveLCSSA); 1176 } 1177 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 1178 ArrayRef<BasicBlock *> Preds, 1179 const char *Suffix, 1180 DomTreeUpdater *DTU, LoopInfo *LI, 1181 MemorySSAUpdater *MSSAU, 1182 bool PreserveLCSSA) { 1183 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU, 1184 /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA); 1185 } 1186 1187 static void SplitLandingPadPredecessorsImpl( 1188 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1, 1189 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 1190 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, 1191 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) { 1192 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!"); 1193 1194 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert 1195 // it right before the original block. 1196 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(), 1197 OrigBB->getName() + Suffix1, 1198 OrigBB->getParent(), OrigBB); 1199 NewBBs.push_back(NewBB1); 1200 1201 // The new block unconditionally branches to the old block. 1202 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); 1203 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 1204 1205 // Move the edges from Preds to point to NewBB1 instead of OrigBB. 1206 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 1207 // This is slightly more strict than necessary; the minimum requirement 1208 // is that there be no more than one indirectbr branching to BB. And 1209 // all BlockAddress uses would need to be updated. 1210 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 1211 "Cannot split an edge from an IndirectBrInst"); 1212 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1); 1213 } 1214 1215 bool HasLoopExit = false; 1216 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU, 1217 PreserveLCSSA, HasLoopExit); 1218 1219 // Update the PHI nodes in OrigBB with the values coming from NewBB1. 1220 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit); 1221 1222 // Move the remaining edges from OrigBB to point to NewBB2. 1223 SmallVector<BasicBlock*, 8> NewBB2Preds; 1224 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB); 1225 i != e; ) { 1226 BasicBlock *Pred = *i++; 1227 if (Pred == NewBB1) continue; 1228 assert(!isa<IndirectBrInst>(Pred->getTerminator()) && 1229 "Cannot split an edge from an IndirectBrInst"); 1230 NewBB2Preds.push_back(Pred); 1231 e = pred_end(OrigBB); 1232 } 1233 1234 BasicBlock *NewBB2 = nullptr; 1235 if (!NewBB2Preds.empty()) { 1236 // Create another basic block for the rest of OrigBB's predecessors. 1237 NewBB2 = BasicBlock::Create(OrigBB->getContext(), 1238 OrigBB->getName() + Suffix2, 1239 OrigBB->getParent(), OrigBB); 1240 NewBBs.push_back(NewBB2); 1241 1242 // The new block unconditionally branches to the old block. 1243 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); 1244 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 1245 1246 // Move the remaining edges from OrigBB to point to NewBB2. 1247 for (BasicBlock *NewBB2Pred : NewBB2Preds) 1248 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); 1249 1250 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 1251 HasLoopExit = false; 1252 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU, 1253 PreserveLCSSA, HasLoopExit); 1254 1255 // Update the PHI nodes in OrigBB with the values coming from NewBB2. 1256 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit); 1257 } 1258 1259 LandingPadInst *LPad = OrigBB->getLandingPadInst(); 1260 Instruction *Clone1 = LPad->clone(); 1261 Clone1->setName(Twine("lpad") + Suffix1); 1262 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1); 1263 1264 if (NewBB2) { 1265 Instruction *Clone2 = LPad->clone(); 1266 Clone2->setName(Twine("lpad") + Suffix2); 1267 NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2); 1268 1269 // Create a PHI node for the two cloned landingpad instructions only 1270 // if the original landingpad instruction has some uses. 1271 if (!LPad->use_empty()) { 1272 assert(!LPad->getType()->isTokenTy() && 1273 "Split cannot be applied if LPad is token type. Otherwise an " 1274 "invalid PHINode of token type would be created."); 1275 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad); 1276 PN->addIncoming(Clone1, NewBB1); 1277 PN->addIncoming(Clone2, NewBB2); 1278 LPad->replaceAllUsesWith(PN); 1279 } 1280 LPad->eraseFromParent(); 1281 } else { 1282 // There is no second clone. Just replace the landing pad with the first 1283 // clone. 1284 LPad->replaceAllUsesWith(Clone1); 1285 LPad->eraseFromParent(); 1286 } 1287 } 1288 1289 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 1290 ArrayRef<BasicBlock *> Preds, 1291 const char *Suffix1, const char *Suffix2, 1292 SmallVectorImpl<BasicBlock *> &NewBBs, 1293 DominatorTree *DT, LoopInfo *LI, 1294 MemorySSAUpdater *MSSAU, 1295 bool PreserveLCSSA) { 1296 return SplitLandingPadPredecessorsImpl( 1297 OrigBB, Preds, Suffix1, Suffix2, NewBBs, 1298 /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA); 1299 } 1300 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 1301 ArrayRef<BasicBlock *> Preds, 1302 const char *Suffix1, const char *Suffix2, 1303 SmallVectorImpl<BasicBlock *> &NewBBs, 1304 DomTreeUpdater *DTU, LoopInfo *LI, 1305 MemorySSAUpdater *MSSAU, 1306 bool PreserveLCSSA) { 1307 return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2, 1308 NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU, 1309 PreserveLCSSA); 1310 } 1311 1312 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 1313 BasicBlock *Pred, 1314 DomTreeUpdater *DTU) { 1315 Instruction *UncondBranch = Pred->getTerminator(); 1316 // Clone the return and add it to the end of the predecessor. 1317 Instruction *NewRet = RI->clone(); 1318 Pred->getInstList().push_back(NewRet); 1319 1320 // If the return instruction returns a value, and if the value was a 1321 // PHI node in "BB", propagate the right value into the return. 1322 for (Use &Op : NewRet->operands()) { 1323 Value *V = Op; 1324 Instruction *NewBC = nullptr; 1325 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) { 1326 // Return value might be bitcasted. Clone and insert it before the 1327 // return instruction. 1328 V = BCI->getOperand(0); 1329 NewBC = BCI->clone(); 1330 Pred->getInstList().insert(NewRet->getIterator(), NewBC); 1331 Op = NewBC; 1332 } 1333 1334 Instruction *NewEV = nullptr; 1335 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) { 1336 V = EVI->getOperand(0); 1337 NewEV = EVI->clone(); 1338 if (NewBC) { 1339 NewBC->setOperand(0, NewEV); 1340 Pred->getInstList().insert(NewBC->getIterator(), NewEV); 1341 } else { 1342 Pred->getInstList().insert(NewRet->getIterator(), NewEV); 1343 Op = NewEV; 1344 } 1345 } 1346 1347 if (PHINode *PN = dyn_cast<PHINode>(V)) { 1348 if (PN->getParent() == BB) { 1349 if (NewEV) { 1350 NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1351 } else if (NewBC) 1352 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1353 else 1354 Op = PN->getIncomingValueForBlock(Pred); 1355 } 1356 } 1357 } 1358 1359 // Update any PHI nodes in the returning block to realize that we no 1360 // longer branch to them. 1361 BB->removePredecessor(Pred); 1362 UncondBranch->eraseFromParent(); 1363 1364 if (DTU) 1365 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}}); 1366 1367 return cast<ReturnInst>(NewRet); 1368 } 1369 1370 static Instruction * 1371 SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore, 1372 bool Unreachable, MDNode *BranchWeights, 1373 DomTreeUpdater *DTU, DominatorTree *DT, 1374 LoopInfo *LI, BasicBlock *ThenBlock) { 1375 SmallVector<DominatorTree::UpdateType, 8> Updates; 1376 BasicBlock *Head = SplitBefore->getParent(); 1377 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1378 if (DTU) { 1379 SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfHead(succ_begin(Tail), 1380 succ_end(Tail)); 1381 Updates.push_back({DominatorTree::Insert, Head, Tail}); 1382 Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfHead.size()); 1383 for (BasicBlock *UniqueSuccessorOfHead : UniqueSuccessorsOfHead) { 1384 Updates.push_back({DominatorTree::Insert, Tail, UniqueSuccessorOfHead}); 1385 Updates.push_back({DominatorTree::Delete, Head, UniqueSuccessorOfHead}); 1386 } 1387 } 1388 Instruction *HeadOldTerm = Head->getTerminator(); 1389 LLVMContext &C = Head->getContext(); 1390 Instruction *CheckTerm; 1391 bool CreateThenBlock = (ThenBlock == nullptr); 1392 if (CreateThenBlock) { 1393 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1394 if (Unreachable) 1395 CheckTerm = new UnreachableInst(C, ThenBlock); 1396 else { 1397 CheckTerm = BranchInst::Create(Tail, ThenBlock); 1398 if (DTU) 1399 Updates.push_back({DominatorTree::Insert, ThenBlock, Tail}); 1400 } 1401 CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); 1402 } else 1403 CheckTerm = ThenBlock->getTerminator(); 1404 BranchInst *HeadNewTerm = 1405 BranchInst::Create(/*ifTrue*/ ThenBlock, /*ifFalse*/ Tail, Cond); 1406 if (DTU) 1407 Updates.push_back({DominatorTree::Insert, Head, ThenBlock}); 1408 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1409 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1410 1411 if (DTU) 1412 DTU->applyUpdates(Updates); 1413 else if (DT) { 1414 if (DomTreeNode *OldNode = DT->getNode(Head)) { 1415 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 1416 1417 DomTreeNode *NewNode = DT->addNewBlock(Tail, Head); 1418 for (DomTreeNode *Child : Children) 1419 DT->changeImmediateDominator(Child, NewNode); 1420 1421 // Head dominates ThenBlock. 1422 if (CreateThenBlock) 1423 DT->addNewBlock(ThenBlock, Head); 1424 else 1425 DT->changeImmediateDominator(ThenBlock, Head); 1426 } 1427 } 1428 1429 if (LI) { 1430 if (Loop *L = LI->getLoopFor(Head)) { 1431 L->addBasicBlockToLoop(ThenBlock, *LI); 1432 L->addBasicBlockToLoop(Tail, *LI); 1433 } 1434 } 1435 1436 return CheckTerm; 1437 } 1438 1439 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 1440 Instruction *SplitBefore, 1441 bool Unreachable, 1442 MDNode *BranchWeights, 1443 DominatorTree *DT, LoopInfo *LI, 1444 BasicBlock *ThenBlock) { 1445 return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable, 1446 BranchWeights, 1447 /*DTU=*/nullptr, DT, LI, ThenBlock); 1448 } 1449 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 1450 Instruction *SplitBefore, 1451 bool Unreachable, 1452 MDNode *BranchWeights, 1453 DomTreeUpdater *DTU, LoopInfo *LI, 1454 BasicBlock *ThenBlock) { 1455 return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable, 1456 BranchWeights, DTU, /*DT=*/nullptr, LI, 1457 ThenBlock); 1458 } 1459 1460 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 1461 Instruction **ThenTerm, 1462 Instruction **ElseTerm, 1463 MDNode *BranchWeights) { 1464 BasicBlock *Head = SplitBefore->getParent(); 1465 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1466 Instruction *HeadOldTerm = Head->getTerminator(); 1467 LLVMContext &C = Head->getContext(); 1468 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1469 BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1470 *ThenTerm = BranchInst::Create(Tail, ThenBlock); 1471 (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1472 *ElseTerm = BranchInst::Create(Tail, ElseBlock); 1473 (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1474 BranchInst *HeadNewTerm = 1475 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond); 1476 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1477 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1478 } 1479 1480 BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 1481 BasicBlock *&IfFalse) { 1482 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); 1483 BasicBlock *Pred1 = nullptr; 1484 BasicBlock *Pred2 = nullptr; 1485 1486 if (SomePHI) { 1487 if (SomePHI->getNumIncomingValues() != 2) 1488 return nullptr; 1489 Pred1 = SomePHI->getIncomingBlock(0); 1490 Pred2 = SomePHI->getIncomingBlock(1); 1491 } else { 1492 pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 1493 if (PI == PE) // No predecessor 1494 return nullptr; 1495 Pred1 = *PI++; 1496 if (PI == PE) // Only one predecessor 1497 return nullptr; 1498 Pred2 = *PI++; 1499 if (PI != PE) // More than two predecessors 1500 return nullptr; 1501 } 1502 1503 // We can only handle branches. Other control flow will be lowered to 1504 // branches if possible anyway. 1505 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); 1506 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); 1507 if (!Pred1Br || !Pred2Br) 1508 return nullptr; 1509 1510 // Eliminate code duplication by ensuring that Pred1Br is conditional if 1511 // either are. 1512 if (Pred2Br->isConditional()) { 1513 // If both branches are conditional, we don't have an "if statement". In 1514 // reality, we could transform this case, but since the condition will be 1515 // required anyway, we stand no chance of eliminating it, so the xform is 1516 // probably not profitable. 1517 if (Pred1Br->isConditional()) 1518 return nullptr; 1519 1520 std::swap(Pred1, Pred2); 1521 std::swap(Pred1Br, Pred2Br); 1522 } 1523 1524 if (Pred1Br->isConditional()) { 1525 // The only thing we have to watch out for here is to make sure that Pred2 1526 // doesn't have incoming edges from other blocks. If it does, the condition 1527 // doesn't dominate BB. 1528 if (!Pred2->getSinglePredecessor()) 1529 return nullptr; 1530 1531 // If we found a conditional branch predecessor, make sure that it branches 1532 // to BB and Pred2Br. If it doesn't, this isn't an "if statement". 1533 if (Pred1Br->getSuccessor(0) == BB && 1534 Pred1Br->getSuccessor(1) == Pred2) { 1535 IfTrue = Pred1; 1536 IfFalse = Pred2; 1537 } else if (Pred1Br->getSuccessor(0) == Pred2 && 1538 Pred1Br->getSuccessor(1) == BB) { 1539 IfTrue = Pred2; 1540 IfFalse = Pred1; 1541 } else { 1542 // We know that one arm of the conditional goes to BB, so the other must 1543 // go somewhere unrelated, and this must not be an "if statement". 1544 return nullptr; 1545 } 1546 1547 return Pred1Br; 1548 } 1549 1550 // Ok, if we got here, both predecessors end with an unconditional branch to 1551 // BB. Don't panic! If both blocks only have a single (identical) 1552 // predecessor, and THAT is a conditional branch, then we're all ok! 1553 BasicBlock *CommonPred = Pred1->getSinglePredecessor(); 1554 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor()) 1555 return nullptr; 1556 1557 // Otherwise, if this is a conditional branch, then we can use it! 1558 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); 1559 if (!BI) return nullptr; 1560 1561 assert(BI->isConditional() && "Two successors but not conditional?"); 1562 if (BI->getSuccessor(0) == Pred1) { 1563 IfTrue = Pred1; 1564 IfFalse = Pred2; 1565 } else { 1566 IfTrue = Pred2; 1567 IfFalse = Pred1; 1568 } 1569 return BI; 1570 } 1571 1572 // After creating a control flow hub, the operands of PHINodes in an outgoing 1573 // block Out no longer match the predecessors of that block. Predecessors of Out 1574 // that are incoming blocks to the hub are now replaced by just one edge from 1575 // the hub. To match this new control flow, the corresponding values from each 1576 // PHINode must now be moved a new PHINode in the first guard block of the hub. 1577 // 1578 // This operation cannot be performed with SSAUpdater, because it involves one 1579 // new use: If the block Out is in the list of Incoming blocks, then the newly 1580 // created PHI in the Hub will use itself along that edge from Out to Hub. 1581 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock, 1582 const SetVector<BasicBlock *> &Incoming, 1583 BasicBlock *FirstGuardBlock) { 1584 auto I = Out->begin(); 1585 while (I != Out->end() && isa<PHINode>(I)) { 1586 auto Phi = cast<PHINode>(I); 1587 auto NewPhi = 1588 PHINode::Create(Phi->getType(), Incoming.size(), 1589 Phi->getName() + ".moved", &FirstGuardBlock->back()); 1590 for (auto In : Incoming) { 1591 Value *V = UndefValue::get(Phi->getType()); 1592 if (In == Out) { 1593 V = NewPhi; 1594 } else if (Phi->getBasicBlockIndex(In) != -1) { 1595 V = Phi->removeIncomingValue(In, false); 1596 } 1597 NewPhi->addIncoming(V, In); 1598 } 1599 assert(NewPhi->getNumIncomingValues() == Incoming.size()); 1600 if (Phi->getNumOperands() == 0) { 1601 Phi->replaceAllUsesWith(NewPhi); 1602 I = Phi->eraseFromParent(); 1603 continue; 1604 } 1605 Phi->addIncoming(NewPhi, GuardBlock); 1606 ++I; 1607 } 1608 } 1609 1610 using BBPredicates = DenseMap<BasicBlock *, PHINode *>; 1611 using BBSetVector = SetVector<BasicBlock *>; 1612 1613 // Redirects the terminator of the incoming block to the first guard 1614 // block in the hub. The condition of the original terminator (if it 1615 // was conditional) and its original successors are returned as a 1616 // tuple <condition, succ0, succ1>. The function additionally filters 1617 // out successors that are not in the set of outgoing blocks. 1618 // 1619 // - condition is non-null iff the branch is conditional. 1620 // - Succ1 is non-null iff the sole/taken target is an outgoing block. 1621 // - Succ2 is non-null iff condition is non-null and the fallthrough 1622 // target is an outgoing block. 1623 static std::tuple<Value *, BasicBlock *, BasicBlock *> 1624 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock, 1625 const BBSetVector &Outgoing) { 1626 auto Branch = cast<BranchInst>(BB->getTerminator()); 1627 auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr; 1628 1629 BasicBlock *Succ0 = Branch->getSuccessor(0); 1630 BasicBlock *Succ1 = nullptr; 1631 Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr; 1632 1633 if (Branch->isUnconditional()) { 1634 Branch->setSuccessor(0, FirstGuardBlock); 1635 assert(Succ0); 1636 } else { 1637 Succ1 = Branch->getSuccessor(1); 1638 Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr; 1639 assert(Succ0 || Succ1); 1640 if (Succ0 && !Succ1) { 1641 Branch->setSuccessor(0, FirstGuardBlock); 1642 } else if (Succ1 && !Succ0) { 1643 Branch->setSuccessor(1, FirstGuardBlock); 1644 } else { 1645 Branch->eraseFromParent(); 1646 BranchInst::Create(FirstGuardBlock, BB); 1647 } 1648 } 1649 1650 assert(Succ0 || Succ1); 1651 return std::make_tuple(Condition, Succ0, Succ1); 1652 } 1653 1654 // Capture the existing control flow as guard predicates, and redirect 1655 // control flow from every incoming block to the first guard block in 1656 // the hub. 1657 // 1658 // There is one guard predicate for each outgoing block OutBB. The 1659 // predicate is a PHINode with one input for each InBB which 1660 // represents whether the hub should transfer control flow to OutBB if 1661 // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub 1662 // evaluates them in the same order as the Outgoing set-vector, and 1663 // control branches to the first outgoing block whose predicate 1664 // evaluates to true. 1665 static void convertToGuardPredicates( 1666 BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates, 1667 SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming, 1668 const BBSetVector &Outgoing) { 1669 auto &Context = Incoming.front()->getContext(); 1670 auto BoolTrue = ConstantInt::getTrue(Context); 1671 auto BoolFalse = ConstantInt::getFalse(Context); 1672 1673 // The predicate for the last outgoing is trivially true, and so we 1674 // process only the first N-1 successors. 1675 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1676 auto Out = Outgoing[i]; 1677 LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n"); 1678 auto Phi = 1679 PHINode::Create(Type::getInt1Ty(Context), Incoming.size(), 1680 StringRef("Guard.") + Out->getName(), FirstGuardBlock); 1681 GuardPredicates[Out] = Phi; 1682 } 1683 1684 for (auto In : Incoming) { 1685 Value *Condition; 1686 BasicBlock *Succ0; 1687 BasicBlock *Succ1; 1688 std::tie(Condition, Succ0, Succ1) = 1689 redirectToHub(In, FirstGuardBlock, Outgoing); 1690 1691 // Optimization: Consider an incoming block A with both successors 1692 // Succ0 and Succ1 in the set of outgoing blocks. The predicates 1693 // for Succ0 and Succ1 complement each other. If Succ0 is visited 1694 // first in the loop below, control will branch to Succ0 using the 1695 // corresponding predicate. But if that branch is not taken, then 1696 // control must reach Succ1, which means that the predicate for 1697 // Succ1 is always true. 1698 bool OneSuccessorDone = false; 1699 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1700 auto Out = Outgoing[i]; 1701 auto Phi = GuardPredicates[Out]; 1702 if (Out != Succ0 && Out != Succ1) { 1703 Phi->addIncoming(BoolFalse, In); 1704 continue; 1705 } 1706 // Optimization: When only one successor is an outgoing block, 1707 // the predicate is always true. 1708 if (!Succ0 || !Succ1 || OneSuccessorDone) { 1709 Phi->addIncoming(BoolTrue, In); 1710 continue; 1711 } 1712 assert(Succ0 && Succ1); 1713 OneSuccessorDone = true; 1714 if (Out == Succ0) { 1715 Phi->addIncoming(Condition, In); 1716 continue; 1717 } 1718 auto Inverted = invertCondition(Condition); 1719 DeletionCandidates.push_back(Condition); 1720 Phi->addIncoming(Inverted, In); 1721 } 1722 } 1723 } 1724 1725 // For each outgoing block OutBB, create a guard block in the Hub. The 1726 // first guard block was already created outside, and available as the 1727 // first element in the vector of guard blocks. 1728 // 1729 // Each guard block terminates in a conditional branch that transfers 1730 // control to the corresponding outgoing block or the next guard 1731 // block. The last guard block has two outgoing blocks as successors 1732 // since the condition for the final outgoing block is trivially 1733 // true. So we create one less block (including the first guard block) 1734 // than the number of outgoing blocks. 1735 static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks, 1736 Function *F, const BBSetVector &Outgoing, 1737 BBPredicates &GuardPredicates, StringRef Prefix) { 1738 for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) { 1739 GuardBlocks.push_back( 1740 BasicBlock::Create(F->getContext(), Prefix + ".guard", F)); 1741 } 1742 assert(GuardBlocks.size() == GuardPredicates.size()); 1743 1744 // To help keep the loop simple, temporarily append the last 1745 // outgoing block to the list of guard blocks. 1746 GuardBlocks.push_back(Outgoing.back()); 1747 1748 for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) { 1749 auto Out = Outgoing[i]; 1750 assert(GuardPredicates.count(Out)); 1751 BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out], 1752 GuardBlocks[i]); 1753 } 1754 1755 // Remove the last block from the guard list. 1756 GuardBlocks.pop_back(); 1757 } 1758 1759 BasicBlock *llvm::CreateControlFlowHub( 1760 DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks, 1761 const BBSetVector &Incoming, const BBSetVector &Outgoing, 1762 const StringRef Prefix) { 1763 auto F = Incoming.front()->getParent(); 1764 auto FirstGuardBlock = 1765 BasicBlock::Create(F->getContext(), Prefix + ".guard", F); 1766 1767 SmallVector<DominatorTree::UpdateType, 16> Updates; 1768 if (DTU) { 1769 for (auto In : Incoming) { 1770 Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock}); 1771 for (auto Succ : successors(In)) { 1772 if (Outgoing.count(Succ)) 1773 Updates.push_back({DominatorTree::Delete, In, Succ}); 1774 } 1775 } 1776 } 1777 1778 BBPredicates GuardPredicates; 1779 SmallVector<WeakVH, 8> DeletionCandidates; 1780 convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates, 1781 Incoming, Outgoing); 1782 1783 GuardBlocks.push_back(FirstGuardBlock); 1784 createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix); 1785 1786 // Update the PHINodes in each outgoing block to match the new control flow. 1787 for (int i = 0, e = GuardBlocks.size(); i != e; ++i) { 1788 reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock); 1789 } 1790 reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock); 1791 1792 if (DTU) { 1793 int NumGuards = GuardBlocks.size(); 1794 assert((int)Outgoing.size() == NumGuards + 1); 1795 for (int i = 0; i != NumGuards - 1; ++i) { 1796 Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]}); 1797 Updates.push_back( 1798 {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]}); 1799 } 1800 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1801 Outgoing[NumGuards - 1]}); 1802 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1803 Outgoing[NumGuards]}); 1804 DTU->applyUpdates(Updates); 1805 } 1806 1807 for (auto I : DeletionCandidates) { 1808 if (I->use_empty()) 1809 if (auto Inst = dyn_cast_or_null<Instruction>(I)) 1810 Inst->eraseFromParent(); 1811 } 1812 1813 return FirstGuardBlock; 1814 } 1815