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