1 //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This family of functions perform manipulations on basic blocks, and 10 // instructions contained within basic blocks. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/Analysis/CFG.h" 20 #include "llvm/Analysis/DomTreeUpdater.h" 21 #include "llvm/Analysis/LoopInfo.h" 22 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 23 #include "llvm/Analysis/MemorySSAUpdater.h" 24 #include "llvm/Analysis/PostDominators.h" 25 #include "llvm/IR/BasicBlock.h" 26 #include "llvm/IR/CFG.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DebugInfoMetadata.h" 29 #include "llvm/IR/Dominators.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/InstrTypes.h" 32 #include "llvm/IR/Instruction.h" 33 #include "llvm/IR/Instructions.h" 34 #include "llvm/IR/IntrinsicInst.h" 35 #include "llvm/IR/LLVMContext.h" 36 #include "llvm/IR/Type.h" 37 #include "llvm/IR/User.h" 38 #include "llvm/IR/Value.h" 39 #include "llvm/IR/ValueHandle.h" 40 #include "llvm/Support/Casting.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include "llvm/Transforms/Utils/Local.h" 44 #include <cassert> 45 #include <cstdint> 46 #include <string> 47 #include <utility> 48 #include <vector> 49 50 using namespace llvm; 51 52 #define DEBUG_TYPE "basicblock-utils" 53 54 void llvm::DetatchDeadBlocks( 55 ArrayRef<BasicBlock *> BBs, 56 SmallVectorImpl<DominatorTree::UpdateType> *Updates, 57 bool KeepOneInputPHIs) { 58 for (auto *BB : BBs) { 59 // Loop through all of our successors and make sure they know that one 60 // of their predecessors is going away. 61 SmallPtrSet<BasicBlock *, 4> UniqueSuccessors; 62 for (BasicBlock *Succ : successors(BB)) { 63 Succ->removePredecessor(BB, KeepOneInputPHIs); 64 if (Updates && UniqueSuccessors.insert(Succ).second) 65 Updates->push_back({DominatorTree::Delete, BB, Succ}); 66 } 67 68 // Zap all the instructions in the block. 69 while (!BB->empty()) { 70 Instruction &I = BB->back(); 71 // If this instruction is used, replace uses with an arbitrary value. 72 // Because control flow can't get here, we don't care what we replace the 73 // value with. Note that since this block is unreachable, and all values 74 // contained within it must dominate their uses, that all uses will 75 // eventually be removed (they are themselves dead). 76 if (!I.use_empty()) 77 I.replaceAllUsesWith(UndefValue::get(I.getType())); 78 BB->getInstList().pop_back(); 79 } 80 new UnreachableInst(BB->getContext(), BB); 81 assert(BB->getInstList().size() == 1 && 82 isa<UnreachableInst>(BB->getTerminator()) && 83 "The successor list of BB isn't empty before " 84 "applying corresponding DTU updates."); 85 } 86 } 87 88 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU, 89 bool KeepOneInputPHIs) { 90 DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs); 91 } 92 93 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU, 94 bool KeepOneInputPHIs) { 95 #ifndef NDEBUG 96 // Make sure that all predecessors of each dead block is also dead. 97 SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end()); 98 assert(Dead.size() == BBs.size() && "Duplicating blocks?"); 99 for (auto *BB : Dead) 100 for (BasicBlock *Pred : predecessors(BB)) 101 assert(Dead.count(Pred) && "All predecessors must be dead!"); 102 #endif 103 104 SmallVector<DominatorTree::UpdateType, 4> Updates; 105 DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs); 106 107 if (DTU) 108 DTU->applyUpdates(Updates); 109 110 for (BasicBlock *BB : BBs) 111 if (DTU) 112 DTU->deleteBB(BB); 113 else 114 BB->eraseFromParent(); 115 } 116 117 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU, 118 bool KeepOneInputPHIs) { 119 df_iterator_default_set<BasicBlock*> Reachable; 120 121 // Mark all reachable blocks. 122 for (BasicBlock *BB : depth_first_ext(&F, Reachable)) 123 (void)BB/* Mark all reachable blocks */; 124 125 // Collect all dead blocks. 126 std::vector<BasicBlock*> DeadBlocks; 127 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 128 if (!Reachable.count(&*I)) { 129 BasicBlock *BB = &*I; 130 DeadBlocks.push_back(BB); 131 } 132 133 // Delete the dead blocks. 134 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs); 135 136 return !DeadBlocks.empty(); 137 } 138 139 bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB, 140 MemoryDependenceResults *MemDep) { 141 if (!isa<PHINode>(BB->begin())) 142 return false; 143 144 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 145 if (PN->getIncomingValue(0) != PN) 146 PN->replaceAllUsesWith(PN->getIncomingValue(0)); 147 else 148 PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 149 150 if (MemDep) 151 MemDep->removeInstruction(PN); // Memdep updates AA itself. 152 153 PN->eraseFromParent(); 154 } 155 return true; 156 } 157 158 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI, 159 MemorySSAUpdater *MSSAU) { 160 // Recursively deleting a PHI may cause multiple PHIs to be deleted 161 // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete. 162 SmallVector<WeakTrackingVH, 8> PHIs; 163 for (PHINode &PN : BB->phis()) 164 PHIs.push_back(&PN); 165 166 bool Changed = false; 167 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) 168 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) 169 Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU); 170 171 return Changed; 172 } 173 174 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, 175 LoopInfo *LI, MemorySSAUpdater *MSSAU, 176 MemoryDependenceResults *MemDep, 177 bool PredecessorWithTwoSuccessors) { 178 if (BB->hasAddressTaken()) 179 return false; 180 181 // Can't merge if there are multiple predecessors, or no predecessors. 182 BasicBlock *PredBB = BB->getUniquePredecessor(); 183 if (!PredBB) return false; 184 185 // Don't break self-loops. 186 if (PredBB == BB) return false; 187 // Don't break unwinding instructions. 188 if (PredBB->getTerminator()->isExceptionalTerminator()) 189 return false; 190 191 // Can't merge if there are multiple distinct successors. 192 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB) 193 return false; 194 195 // Currently only allow PredBB to have two predecessors, one being BB. 196 // Update BI to branch to BB's only successor instead of BB. 197 BranchInst *PredBB_BI; 198 BasicBlock *NewSucc = nullptr; 199 unsigned FallThruPath; 200 if (PredecessorWithTwoSuccessors) { 201 if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator()))) 202 return false; 203 BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator()); 204 if (!BB_JmpI || !BB_JmpI->isUnconditional()) 205 return false; 206 NewSucc = BB_JmpI->getSuccessor(0); 207 FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1; 208 } 209 210 // Can't merge if there is PHI loop. 211 for (PHINode &PN : BB->phis()) 212 for (Value *IncValue : PN.incoming_values()) 213 if (IncValue == &PN) 214 return false; 215 216 LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into " 217 << PredBB->getName() << "\n"); 218 219 // Begin by getting rid of unneeded PHIs. 220 SmallVector<AssertingVH<Value>, 4> IncomingValues; 221 if (isa<PHINode>(BB->front())) { 222 for (PHINode &PN : BB->phis()) 223 if (!isa<PHINode>(PN.getIncomingValue(0)) || 224 cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB) 225 IncomingValues.push_back(PN.getIncomingValue(0)); 226 FoldSingleEntryPHINodes(BB, MemDep); 227 } 228 229 // DTU update: Collect all the edges that exit BB. 230 // These dominator edges will be redirected from Pred. 231 std::vector<DominatorTree::UpdateType> Updates; 232 if (DTU) { 233 SmallSetVector<BasicBlock *, 2> UniqueSuccessors(succ_begin(BB), 234 succ_end(BB)); 235 Updates.reserve(1 + (2 * UniqueSuccessors.size())); 236 // Add insert edges first. Experimentally, for the particular case of two 237 // blocks that can be merged, with a single successor and single predecessor 238 // respectively, it is beneficial to have all insert updates first. Deleting 239 // edges first may lead to unreachable blocks, followed by inserting edges 240 // making the blocks reachable again. Such DT updates lead to high compile 241 // times. We add inserts before deletes here to reduce compile time. 242 for (BasicBlock *UniqueSuccessor : UniqueSuccessors) 243 // This successor of BB may already have PredBB as a predecessor. 244 if (!llvm::is_contained(successors(PredBB), UniqueSuccessor)) 245 Updates.push_back({DominatorTree::Insert, PredBB, UniqueSuccessor}); 246 for (BasicBlock *UniqueSuccessor : UniqueSuccessors) 247 Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor}); 248 Updates.push_back({DominatorTree::Delete, PredBB, BB}); 249 } 250 251 Instruction *PTI = PredBB->getTerminator(); 252 Instruction *STI = BB->getTerminator(); 253 Instruction *Start = &*BB->begin(); 254 // If there's nothing to move, mark the starting instruction as the last 255 // instruction in the block. Terminator instruction is handled separately. 256 if (Start == STI) 257 Start = PTI; 258 259 // Move all definitions in the successor to the predecessor... 260 PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(), 261 BB->begin(), STI->getIterator()); 262 263 if (MSSAU) 264 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start); 265 266 // Make all PHI nodes that referred to BB now refer to Pred as their 267 // source... 268 BB->replaceAllUsesWith(PredBB); 269 270 if (PredecessorWithTwoSuccessors) { 271 // Delete the unconditional branch from BB. 272 BB->getInstList().pop_back(); 273 274 // Update branch in the predecessor. 275 PredBB_BI->setSuccessor(FallThruPath, NewSucc); 276 } else { 277 // Delete the unconditional branch from the predecessor. 278 PredBB->getInstList().pop_back(); 279 280 // Move terminator instruction. 281 PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); 282 283 // Terminator may be a memory accessing instruction too. 284 if (MSSAU) 285 if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>( 286 MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator()))) 287 MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End); 288 } 289 // Add unreachable to now empty BB. 290 new UnreachableInst(BB->getContext(), BB); 291 292 // Inherit predecessors name if it exists. 293 if (!PredBB->hasName()) 294 PredBB->takeName(BB); 295 296 if (LI) 297 LI->removeBlock(BB); 298 299 if (MemDep) 300 MemDep->invalidateCachedPredecessors(); 301 302 // Finally, erase the old block and update dominator info. 303 if (DTU) { 304 assert(BB->getInstList().size() == 1 && 305 isa<UnreachableInst>(BB->getTerminator()) && 306 "The successor list of BB isn't empty before " 307 "applying corresponding DTU updates."); 308 DTU->applyUpdates(Updates); 309 DTU->deleteBB(BB); 310 } else { 311 BB->eraseFromParent(); // Nuke BB if DTU is nullptr. 312 } 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<Value *, DIExpression *> > VariableMap; 413 for (auto &I : *BB) { 414 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) { 415 DebugVariable Key(DVI->getVariable(), 416 NoneType(), 417 DVI->getDebugLoc()->getInlinedAt()); 418 auto VMI = VariableMap.find(Key); 419 // Update the map if we found a new value/expression describing the 420 // variable, or if the variable wasn't mapped already. 421 if (VMI == VariableMap.end() || 422 VMI->second.first != DVI->getValue() || 423 VMI->second.second != DVI->getExpression()) { 424 VariableMap[Key] = { DVI->getValue(), DVI->getExpression() }; 425 continue; 426 } 427 // Found an identical mapping. Remember the instruction for later removal. 428 ToBeRemoved.push_back(DVI); 429 } 430 } 431 432 for (auto &Instr : ToBeRemoved) 433 Instr->eraseFromParent(); 434 435 return !ToBeRemoved.empty(); 436 } 437 438 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) { 439 bool MadeChanges = false; 440 // By using the "backward scan" strategy before the "forward scan" strategy we 441 // can remove both dbg.value (2) and (3) in a situation like this: 442 // 443 // (1) dbg.value V1, "x", DIExpression() 444 // ... 445 // (2) dbg.value V2, "x", DIExpression() 446 // (3) dbg.value V1, "x", DIExpression() 447 // 448 // The backward scan will remove (2), it is made obsolete by (3). After 449 // getting (2) out of the way, the foward scan will remove (3) since "x" 450 // already is described as having the value V1 at (1). 451 MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB); 452 MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(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 // If this is a critical edge, let SplitCriticalEdge do it. 505 Instruction *LatchTerm = BB->getTerminator(); 506 if (SplitCriticalEdge( 507 LatchTerm, SuccNum, 508 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA(), 509 BBName)) 510 return LatchTerm->getSuccessor(SuccNum); 511 512 // If the edge isn't critical, then BB has a single successor or Succ has a 513 // single pred. Split the block. 514 if (BasicBlock *SP = Succ->getSinglePredecessor()) { 515 // If the successor only has a single pred, split the top of the successor 516 // block. 517 assert(SP == BB && "CFG broken"); 518 SP = nullptr; 519 return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName, 520 /*Before=*/true); 521 } 522 523 // Otherwise, if BB has a single successor, split it at the bottom of the 524 // block. 525 assert(BB->getTerminator()->getNumSuccessors() == 1 && 526 "Should have a single succ!"); 527 return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName); 528 } 529 530 unsigned 531 llvm::SplitAllCriticalEdges(Function &F, 532 const CriticalEdgeSplittingOptions &Options) { 533 unsigned NumBroken = 0; 534 for (BasicBlock &BB : F) { 535 Instruction *TI = BB.getTerminator(); 536 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) && 537 !isa<CallBrInst>(TI)) 538 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 539 if (SplitCriticalEdge(TI, i, Options)) 540 ++NumBroken; 541 } 542 return NumBroken; 543 } 544 545 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, 546 DominatorTree *DT, LoopInfo *LI, 547 MemorySSAUpdater *MSSAU, const Twine &BBName, 548 bool Before) { 549 if (Before) 550 return splitBlockBefore(Old, SplitPt, DT, LI, MSSAU, BBName); 551 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 552 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 553 ++SplitIt; 554 std::string Name = BBName.str(); 555 BasicBlock *New = Old->splitBasicBlock( 556 SplitIt, Name.empty() ? Old->getName() + ".split" : Name); 557 558 // The new block lives in whichever loop the old one did. This preserves 559 // LCSSA as well, because we force the split point to be after any PHI nodes. 560 if (LI) 561 if (Loop *L = LI->getLoopFor(Old)) 562 L->addBasicBlockToLoop(New, *LI); 563 564 if (DT) 565 // Old dominates New. New node dominates all other nodes dominated by Old. 566 if (DomTreeNode *OldNode = DT->getNode(Old)) { 567 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 568 569 DomTreeNode *NewNode = DT->addNewBlock(New, Old); 570 for (DomTreeNode *I : Children) 571 DT->changeImmediateDominator(I, NewNode); 572 } 573 574 // Move MemoryAccesses still tracked in Old, but part of New now. 575 // Update accesses in successor blocks accordingly. 576 if (MSSAU) 577 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin())); 578 579 return New; 580 } 581 582 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, 583 DominatorTree *DT, LoopInfo *LI, 584 MemorySSAUpdater *MSSAU, 585 const Twine &BBName) { 586 587 BasicBlock::iterator SplitIt = SplitPt->getIterator(); 588 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) 589 ++SplitIt; 590 std::string Name = BBName.str(); 591 BasicBlock *New = Old->splitBasicBlock( 592 SplitIt, Name.empty() ? Old->getName() + ".split" : Name, 593 /* Before=*/true); 594 595 // The new block lives in whichever loop the old one did. This preserves 596 // LCSSA as well, because we force the split point to be after any PHI nodes. 597 if (LI) 598 if (Loop *L = LI->getLoopFor(Old)) 599 L->addBasicBlockToLoop(New, *LI); 600 601 if (DT) { 602 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 603 SmallVector<DominatorTree::UpdateType, 8> DTUpdates; 604 // New dominates Old. The predecessor nodes of the Old node dominate 605 // New node. 606 DTUpdates.push_back({DominatorTree::Insert, New, Old}); 607 for (BasicBlock *Pred : predecessors(New)) 608 if (DT->getNode(Pred)) { 609 DTUpdates.push_back({DominatorTree::Insert, Pred, New}); 610 DTUpdates.push_back({DominatorTree::Delete, Pred, Old}); 611 } 612 613 DTU.applyUpdates(DTUpdates); 614 DTU.flush(); 615 616 // Move MemoryAccesses still tracked in Old, but part of New now. 617 // Update accesses in successor blocks accordingly. 618 if (MSSAU) { 619 MSSAU->applyUpdates(DTUpdates, *DT); 620 if (VerifyMemorySSA) 621 MSSAU->getMemorySSA()->verifyMemorySSA(); 622 } 623 } 624 return New; 625 } 626 627 /// Update DominatorTree, LoopInfo, and LCCSA analysis information. 628 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, 629 ArrayRef<BasicBlock *> Preds, 630 DominatorTree *DT, LoopInfo *LI, 631 MemorySSAUpdater *MSSAU, 632 bool PreserveLCSSA, bool &HasLoopExit) { 633 // Update dominator tree if available. 634 if (DT) { 635 if (OldBB == DT->getRootNode()->getBlock()) { 636 assert(NewBB == &NewBB->getParent()->getEntryBlock()); 637 DT->setNewRoot(NewBB); 638 } else { 639 // Split block expects NewBB to have a non-empty set of predecessors. 640 DT->splitBlock(NewBB); 641 } 642 } 643 644 // Update MemoryPhis after split if MemorySSA is available 645 if (MSSAU) 646 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds); 647 648 // The rest of the logic is only relevant for updating the loop structures. 649 if (!LI) 650 return; 651 652 assert(DT && "DT should be available to update LoopInfo!"); 653 Loop *L = LI->getLoopFor(OldBB); 654 655 // If we need to preserve loop analyses, collect some information about how 656 // this split will affect loops. 657 bool IsLoopEntry = !!L; 658 bool SplitMakesNewLoopHeader = false; 659 for (BasicBlock *Pred : Preds) { 660 // Preds that are not reachable from entry should not be used to identify if 661 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks 662 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader 663 // as true and make the NewBB the header of some loop. This breaks LI. 664 if (!DT->isReachableFromEntry(Pred)) 665 continue; 666 // If we need to preserve LCSSA, determine if any of the preds is a loop 667 // exit. 668 if (PreserveLCSSA) 669 if (Loop *PL = LI->getLoopFor(Pred)) 670 if (!PL->contains(OldBB)) 671 HasLoopExit = true; 672 673 // If we need to preserve LoopInfo, note whether any of the preds crosses 674 // an interesting loop boundary. 675 if (!L) 676 continue; 677 if (L->contains(Pred)) 678 IsLoopEntry = false; 679 else 680 SplitMakesNewLoopHeader = true; 681 } 682 683 // Unless we have a loop for OldBB, nothing else to do here. 684 if (!L) 685 return; 686 687 if (IsLoopEntry) { 688 // Add the new block to the nearest enclosing loop (and not an adjacent 689 // loop). To find this, examine each of the predecessors and determine which 690 // loops enclose them, and select the most-nested loop which contains the 691 // loop containing the block being split. 692 Loop *InnermostPredLoop = nullptr; 693 for (BasicBlock *Pred : Preds) { 694 if (Loop *PredLoop = LI->getLoopFor(Pred)) { 695 // Seek a loop which actually contains the block being split (to avoid 696 // adjacent loops). 697 while (PredLoop && !PredLoop->contains(OldBB)) 698 PredLoop = PredLoop->getParentLoop(); 699 700 // Select the most-nested of these loops which contains the block. 701 if (PredLoop && PredLoop->contains(OldBB) && 702 (!InnermostPredLoop || 703 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) 704 InnermostPredLoop = PredLoop; 705 } 706 } 707 708 if (InnermostPredLoop) 709 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI); 710 } else { 711 L->addBasicBlockToLoop(NewBB, *LI); 712 if (SplitMakesNewLoopHeader) 713 L->moveToHeader(NewBB); 714 } 715 } 716 717 /// Update the PHI nodes in OrigBB to include the values coming from NewBB. 718 /// This also updates AliasAnalysis, if available. 719 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, 720 ArrayRef<BasicBlock *> Preds, BranchInst *BI, 721 bool HasLoopExit) { 722 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB. 723 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end()); 724 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) { 725 PHINode *PN = cast<PHINode>(I++); 726 727 // Check to see if all of the values coming in are the same. If so, we 728 // don't need to create a new PHI node, unless it's needed for LCSSA. 729 Value *InVal = nullptr; 730 if (!HasLoopExit) { 731 InVal = PN->getIncomingValueForBlock(Preds[0]); 732 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 733 if (!PredSet.count(PN->getIncomingBlock(i))) 734 continue; 735 if (!InVal) 736 InVal = PN->getIncomingValue(i); 737 else if (InVal != PN->getIncomingValue(i)) { 738 InVal = nullptr; 739 break; 740 } 741 } 742 } 743 744 if (InVal) { 745 // If all incoming values for the new PHI would be the same, just don't 746 // make a new PHI. Instead, just remove the incoming values from the old 747 // PHI. 748 749 // NOTE! This loop walks backwards for a reason! First off, this minimizes 750 // the cost of removal if we end up removing a large number of values, and 751 // second off, this ensures that the indices for the incoming values 752 // aren't invalidated when we remove one. 753 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) 754 if (PredSet.count(PN->getIncomingBlock(i))) 755 PN->removeIncomingValue(i, false); 756 757 // Add an incoming value to the PHI node in the loop for the preheader 758 // edge. 759 PN->addIncoming(InVal, NewBB); 760 continue; 761 } 762 763 // If the values coming into the block are not the same, we need a new 764 // PHI. 765 // Create the new PHI node, insert it into NewBB at the end of the block 766 PHINode *NewPHI = 767 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI); 768 769 // NOTE! This loop walks backwards for a reason! First off, this minimizes 770 // the cost of removal if we end up removing a large number of values, and 771 // second off, this ensures that the indices for the incoming values aren't 772 // invalidated when we remove one. 773 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) { 774 BasicBlock *IncomingBB = PN->getIncomingBlock(i); 775 if (PredSet.count(IncomingBB)) { 776 Value *V = PN->removeIncomingValue(i, false); 777 NewPHI->addIncoming(V, IncomingBB); 778 } 779 } 780 781 PN->addIncoming(NewPHI, NewBB); 782 } 783 } 784 785 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, 786 ArrayRef<BasicBlock *> Preds, 787 const char *Suffix, DominatorTree *DT, 788 LoopInfo *LI, MemorySSAUpdater *MSSAU, 789 bool PreserveLCSSA) { 790 // Do not attempt to split that which cannot be split. 791 if (!BB->canSplitPredecessors()) 792 return nullptr; 793 794 // For the landingpads we need to act a bit differently. 795 // Delegate this work to the SplitLandingPadPredecessors. 796 if (BB->isLandingPad()) { 797 SmallVector<BasicBlock*, 2> NewBBs; 798 std::string NewName = std::string(Suffix) + ".split-lp"; 799 800 SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs, DT, 801 LI, MSSAU, PreserveLCSSA); 802 return NewBBs[0]; 803 } 804 805 // Create new basic block, insert right before the original block. 806 BasicBlock *NewBB = BasicBlock::Create( 807 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB); 808 809 // The new block unconditionally branches to the old block. 810 BranchInst *BI = BranchInst::Create(BB, NewBB); 811 812 Loop *L = nullptr; 813 BasicBlock *OldLatch = nullptr; 814 // Splitting the predecessors of a loop header creates a preheader block. 815 if (LI && LI->isLoopHeader(BB)) { 816 L = LI->getLoopFor(BB); 817 // Using the loop start line number prevents debuggers stepping into the 818 // loop body for this instruction. 819 BI->setDebugLoc(L->getStartLoc()); 820 821 // If BB is the header of the Loop, it is possible that the loop is 822 // modified, such that the current latch does not remain the latch of the 823 // loop. If that is the case, the loop metadata from the current latch needs 824 // to be applied to the new latch. 825 OldLatch = L->getLoopLatch(); 826 } else 827 BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc()); 828 829 // Move the edges from Preds to point to NewBB instead of BB. 830 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 831 // This is slightly more strict than necessary; the minimum requirement 832 // is that there be no more than one indirectbr branching to BB. And 833 // all BlockAddress uses would need to be updated. 834 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 835 "Cannot split an edge from an IndirectBrInst"); 836 assert(!isa<CallBrInst>(Preds[i]->getTerminator()) && 837 "Cannot split an edge from a CallBrInst"); 838 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); 839 } 840 841 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI 842 // node becomes an incoming value for BB's phi node. However, if the Preds 843 // list is empty, we need to insert dummy entries into the PHI nodes in BB to 844 // account for the newly created predecessor. 845 if (Preds.empty()) { 846 // Insert dummy values as the incoming value. 847 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) 848 cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); 849 } 850 851 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 852 bool HasLoopExit = false; 853 UpdateAnalysisInformation(BB, NewBB, Preds, DT, LI, MSSAU, PreserveLCSSA, 854 HasLoopExit); 855 856 if (!Preds.empty()) { 857 // Update the PHI nodes in BB with the values coming from NewBB. 858 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit); 859 } 860 861 if (OldLatch) { 862 BasicBlock *NewLatch = L->getLoopLatch(); 863 if (NewLatch != OldLatch) { 864 MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop"); 865 NewLatch->getTerminator()->setMetadata("llvm.loop", MD); 866 OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr); 867 } 868 } 869 870 return NewBB; 871 } 872 873 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, 874 ArrayRef<BasicBlock *> Preds, 875 const char *Suffix1, const char *Suffix2, 876 SmallVectorImpl<BasicBlock *> &NewBBs, 877 DominatorTree *DT, LoopInfo *LI, 878 MemorySSAUpdater *MSSAU, 879 bool PreserveLCSSA) { 880 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!"); 881 882 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert 883 // it right before the original block. 884 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(), 885 OrigBB->getName() + Suffix1, 886 OrigBB->getParent(), OrigBB); 887 NewBBs.push_back(NewBB1); 888 889 // The new block unconditionally branches to the old block. 890 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); 891 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 892 893 // Move the edges from Preds to point to NewBB1 instead of OrigBB. 894 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 895 // This is slightly more strict than necessary; the minimum requirement 896 // is that there be no more than one indirectbr branching to BB. And 897 // all BlockAddress uses would need to be updated. 898 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && 899 "Cannot split an edge from an IndirectBrInst"); 900 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1); 901 } 902 903 bool HasLoopExit = false; 904 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DT, LI, MSSAU, PreserveLCSSA, 905 HasLoopExit); 906 907 // Update the PHI nodes in OrigBB with the values coming from NewBB1. 908 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit); 909 910 // Move the remaining edges from OrigBB to point to NewBB2. 911 SmallVector<BasicBlock*, 8> NewBB2Preds; 912 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB); 913 i != e; ) { 914 BasicBlock *Pred = *i++; 915 if (Pred == NewBB1) continue; 916 assert(!isa<IndirectBrInst>(Pred->getTerminator()) && 917 "Cannot split an edge from an IndirectBrInst"); 918 NewBB2Preds.push_back(Pred); 919 e = pred_end(OrigBB); 920 } 921 922 BasicBlock *NewBB2 = nullptr; 923 if (!NewBB2Preds.empty()) { 924 // Create another basic block for the rest of OrigBB's predecessors. 925 NewBB2 = BasicBlock::Create(OrigBB->getContext(), 926 OrigBB->getName() + Suffix2, 927 OrigBB->getParent(), OrigBB); 928 NewBBs.push_back(NewBB2); 929 930 // The new block unconditionally branches to the old block. 931 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); 932 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc()); 933 934 // Move the remaining edges from OrigBB to point to NewBB2. 935 for (BasicBlock *NewBB2Pred : NewBB2Preds) 936 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); 937 938 // Update DominatorTree, LoopInfo, and LCCSA analysis information. 939 HasLoopExit = false; 940 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DT, LI, MSSAU, 941 PreserveLCSSA, HasLoopExit); 942 943 // Update the PHI nodes in OrigBB with the values coming from NewBB2. 944 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit); 945 } 946 947 LandingPadInst *LPad = OrigBB->getLandingPadInst(); 948 Instruction *Clone1 = LPad->clone(); 949 Clone1->setName(Twine("lpad") + Suffix1); 950 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1); 951 952 if (NewBB2) { 953 Instruction *Clone2 = LPad->clone(); 954 Clone2->setName(Twine("lpad") + Suffix2); 955 NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2); 956 957 // Create a PHI node for the two cloned landingpad instructions only 958 // if the original landingpad instruction has some uses. 959 if (!LPad->use_empty()) { 960 assert(!LPad->getType()->isTokenTy() && 961 "Split cannot be applied if LPad is token type. Otherwise an " 962 "invalid PHINode of token type would be created."); 963 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad); 964 PN->addIncoming(Clone1, NewBB1); 965 PN->addIncoming(Clone2, NewBB2); 966 LPad->replaceAllUsesWith(PN); 967 } 968 LPad->eraseFromParent(); 969 } else { 970 // There is no second clone. Just replace the landing pad with the first 971 // clone. 972 LPad->replaceAllUsesWith(Clone1); 973 LPad->eraseFromParent(); 974 } 975 } 976 977 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 978 BasicBlock *Pred, 979 DomTreeUpdater *DTU) { 980 Instruction *UncondBranch = Pred->getTerminator(); 981 // Clone the return and add it to the end of the predecessor. 982 Instruction *NewRet = RI->clone(); 983 Pred->getInstList().push_back(NewRet); 984 985 // If the return instruction returns a value, and if the value was a 986 // PHI node in "BB", propagate the right value into the return. 987 for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end(); 988 i != e; ++i) { 989 Value *V = *i; 990 Instruction *NewBC = nullptr; 991 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) { 992 // Return value might be bitcasted. Clone and insert it before the 993 // return instruction. 994 V = BCI->getOperand(0); 995 NewBC = BCI->clone(); 996 Pred->getInstList().insert(NewRet->getIterator(), NewBC); 997 *i = NewBC; 998 } 999 1000 Instruction *NewEV = nullptr; 1001 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) { 1002 V = EVI->getOperand(0); 1003 NewEV = EVI->clone(); 1004 if (NewBC) { 1005 NewBC->setOperand(0, NewEV); 1006 Pred->getInstList().insert(NewBC->getIterator(), NewEV); 1007 } else { 1008 Pred->getInstList().insert(NewRet->getIterator(), NewEV); 1009 *i = NewEV; 1010 } 1011 } 1012 1013 if (PHINode *PN = dyn_cast<PHINode>(V)) { 1014 if (PN->getParent() == BB) { 1015 if (NewEV) { 1016 NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1017 } else if (NewBC) 1018 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred)); 1019 else 1020 *i = PN->getIncomingValueForBlock(Pred); 1021 } 1022 } 1023 } 1024 1025 // Update any PHI nodes in the returning block to realize that we no 1026 // longer branch to them. 1027 BB->removePredecessor(Pred); 1028 UncondBranch->eraseFromParent(); 1029 1030 if (DTU) 1031 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}}); 1032 1033 return cast<ReturnInst>(NewRet); 1034 } 1035 1036 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, 1037 Instruction *SplitBefore, 1038 bool Unreachable, 1039 MDNode *BranchWeights, 1040 DominatorTree *DT, LoopInfo *LI, 1041 BasicBlock *ThenBlock) { 1042 BasicBlock *Head = SplitBefore->getParent(); 1043 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1044 Instruction *HeadOldTerm = Head->getTerminator(); 1045 LLVMContext &C = Head->getContext(); 1046 Instruction *CheckTerm; 1047 bool CreateThenBlock = (ThenBlock == nullptr); 1048 if (CreateThenBlock) { 1049 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1050 if (Unreachable) 1051 CheckTerm = new UnreachableInst(C, ThenBlock); 1052 else 1053 CheckTerm = BranchInst::Create(Tail, ThenBlock); 1054 CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); 1055 } else 1056 CheckTerm = ThenBlock->getTerminator(); 1057 BranchInst *HeadNewTerm = 1058 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond); 1059 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1060 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1061 1062 if (DT) { 1063 if (DomTreeNode *OldNode = DT->getNode(Head)) { 1064 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); 1065 1066 DomTreeNode *NewNode = DT->addNewBlock(Tail, Head); 1067 for (DomTreeNode *Child : Children) 1068 DT->changeImmediateDominator(Child, NewNode); 1069 1070 // Head dominates ThenBlock. 1071 if (CreateThenBlock) 1072 DT->addNewBlock(ThenBlock, Head); 1073 else 1074 DT->changeImmediateDominator(ThenBlock, Head); 1075 } 1076 } 1077 1078 if (LI) { 1079 if (Loop *L = LI->getLoopFor(Head)) { 1080 L->addBasicBlockToLoop(ThenBlock, *LI); 1081 L->addBasicBlockToLoop(Tail, *LI); 1082 } 1083 } 1084 1085 return CheckTerm; 1086 } 1087 1088 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 1089 Instruction **ThenTerm, 1090 Instruction **ElseTerm, 1091 MDNode *BranchWeights) { 1092 BasicBlock *Head = SplitBefore->getParent(); 1093 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); 1094 Instruction *HeadOldTerm = Head->getTerminator(); 1095 LLVMContext &C = Head->getContext(); 1096 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1097 BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); 1098 *ThenTerm = BranchInst::Create(Tail, ThenBlock); 1099 (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1100 *ElseTerm = BranchInst::Create(Tail, ElseBlock); 1101 (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc()); 1102 BranchInst *HeadNewTerm = 1103 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond); 1104 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); 1105 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); 1106 } 1107 1108 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 1109 BasicBlock *&IfFalse) { 1110 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); 1111 BasicBlock *Pred1 = nullptr; 1112 BasicBlock *Pred2 = nullptr; 1113 1114 if (SomePHI) { 1115 if (SomePHI->getNumIncomingValues() != 2) 1116 return nullptr; 1117 Pred1 = SomePHI->getIncomingBlock(0); 1118 Pred2 = SomePHI->getIncomingBlock(1); 1119 } else { 1120 pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 1121 if (PI == PE) // No predecessor 1122 return nullptr; 1123 Pred1 = *PI++; 1124 if (PI == PE) // Only one predecessor 1125 return nullptr; 1126 Pred2 = *PI++; 1127 if (PI != PE) // More than two predecessors 1128 return nullptr; 1129 } 1130 1131 // We can only handle branches. Other control flow will be lowered to 1132 // branches if possible anyway. 1133 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); 1134 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); 1135 if (!Pred1Br || !Pred2Br) 1136 return nullptr; 1137 1138 // Eliminate code duplication by ensuring that Pred1Br is conditional if 1139 // either are. 1140 if (Pred2Br->isConditional()) { 1141 // If both branches are conditional, we don't have an "if statement". In 1142 // reality, we could transform this case, but since the condition will be 1143 // required anyway, we stand no chance of eliminating it, so the xform is 1144 // probably not profitable. 1145 if (Pred1Br->isConditional()) 1146 return nullptr; 1147 1148 std::swap(Pred1, Pred2); 1149 std::swap(Pred1Br, Pred2Br); 1150 } 1151 1152 if (Pred1Br->isConditional()) { 1153 // The only thing we have to watch out for here is to make sure that Pred2 1154 // doesn't have incoming edges from other blocks. If it does, the condition 1155 // doesn't dominate BB. 1156 if (!Pred2->getSinglePredecessor()) 1157 return nullptr; 1158 1159 // If we found a conditional branch predecessor, make sure that it branches 1160 // to BB and Pred2Br. If it doesn't, this isn't an "if statement". 1161 if (Pred1Br->getSuccessor(0) == BB && 1162 Pred1Br->getSuccessor(1) == Pred2) { 1163 IfTrue = Pred1; 1164 IfFalse = Pred2; 1165 } else if (Pred1Br->getSuccessor(0) == Pred2 && 1166 Pred1Br->getSuccessor(1) == BB) { 1167 IfTrue = Pred2; 1168 IfFalse = Pred1; 1169 } else { 1170 // We know that one arm of the conditional goes to BB, so the other must 1171 // go somewhere unrelated, and this must not be an "if statement". 1172 return nullptr; 1173 } 1174 1175 return Pred1Br->getCondition(); 1176 } 1177 1178 // Ok, if we got here, both predecessors end with an unconditional branch to 1179 // BB. Don't panic! If both blocks only have a single (identical) 1180 // predecessor, and THAT is a conditional branch, then we're all ok! 1181 BasicBlock *CommonPred = Pred1->getSinglePredecessor(); 1182 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor()) 1183 return nullptr; 1184 1185 // Otherwise, if this is a conditional branch, then we can use it! 1186 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); 1187 if (!BI) return nullptr; 1188 1189 assert(BI->isConditional() && "Two successors but not conditional?"); 1190 if (BI->getSuccessor(0) == Pred1) { 1191 IfTrue = Pred1; 1192 IfFalse = Pred2; 1193 } else { 1194 IfTrue = Pred2; 1195 IfFalse = Pred1; 1196 } 1197 return BI->getCondition(); 1198 } 1199 1200 // After creating a control flow hub, the operands of PHINodes in an outgoing 1201 // block Out no longer match the predecessors of that block. Predecessors of Out 1202 // that are incoming blocks to the hub are now replaced by just one edge from 1203 // the hub. To match this new control flow, the corresponding values from each 1204 // PHINode must now be moved a new PHINode in the first guard block of the hub. 1205 // 1206 // This operation cannot be performed with SSAUpdater, because it involves one 1207 // new use: If the block Out is in the list of Incoming blocks, then the newly 1208 // created PHI in the Hub will use itself along that edge from Out to Hub. 1209 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock, 1210 const SetVector<BasicBlock *> &Incoming, 1211 BasicBlock *FirstGuardBlock) { 1212 auto I = Out->begin(); 1213 while (I != Out->end() && isa<PHINode>(I)) { 1214 auto Phi = cast<PHINode>(I); 1215 auto NewPhi = 1216 PHINode::Create(Phi->getType(), Incoming.size(), 1217 Phi->getName() + ".moved", &FirstGuardBlock->back()); 1218 for (auto In : Incoming) { 1219 Value *V = UndefValue::get(Phi->getType()); 1220 if (In == Out) { 1221 V = NewPhi; 1222 } else if (Phi->getBasicBlockIndex(In) != -1) { 1223 V = Phi->removeIncomingValue(In, false); 1224 } 1225 NewPhi->addIncoming(V, In); 1226 } 1227 assert(NewPhi->getNumIncomingValues() == Incoming.size()); 1228 if (Phi->getNumOperands() == 0) { 1229 Phi->replaceAllUsesWith(NewPhi); 1230 I = Phi->eraseFromParent(); 1231 continue; 1232 } 1233 Phi->addIncoming(NewPhi, GuardBlock); 1234 ++I; 1235 } 1236 } 1237 1238 using BBPredicates = DenseMap<BasicBlock *, PHINode *>; 1239 using BBSetVector = SetVector<BasicBlock *>; 1240 1241 // Redirects the terminator of the incoming block to the first guard 1242 // block in the hub. The condition of the original terminator (if it 1243 // was conditional) and its original successors are returned as a 1244 // tuple <condition, succ0, succ1>. The function additionally filters 1245 // out successors that are not in the set of outgoing blocks. 1246 // 1247 // - condition is non-null iff the branch is conditional. 1248 // - Succ1 is non-null iff the sole/taken target is an outgoing block. 1249 // - Succ2 is non-null iff condition is non-null and the fallthrough 1250 // target is an outgoing block. 1251 static std::tuple<Value *, BasicBlock *, BasicBlock *> 1252 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock, 1253 const BBSetVector &Outgoing) { 1254 auto Branch = cast<BranchInst>(BB->getTerminator()); 1255 auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr; 1256 1257 BasicBlock *Succ0 = Branch->getSuccessor(0); 1258 BasicBlock *Succ1 = nullptr; 1259 Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr; 1260 1261 if (Branch->isUnconditional()) { 1262 Branch->setSuccessor(0, FirstGuardBlock); 1263 assert(Succ0); 1264 } else { 1265 Succ1 = Branch->getSuccessor(1); 1266 Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr; 1267 assert(Succ0 || Succ1); 1268 if (Succ0 && !Succ1) { 1269 Branch->setSuccessor(0, FirstGuardBlock); 1270 } else if (Succ1 && !Succ0) { 1271 Branch->setSuccessor(1, FirstGuardBlock); 1272 } else { 1273 Branch->eraseFromParent(); 1274 BranchInst::Create(FirstGuardBlock, BB); 1275 } 1276 } 1277 1278 assert(Succ0 || Succ1); 1279 return std::make_tuple(Condition, Succ0, Succ1); 1280 } 1281 1282 // Capture the existing control flow as guard predicates, and redirect 1283 // control flow from every incoming block to the first guard block in 1284 // the hub. 1285 // 1286 // There is one guard predicate for each outgoing block OutBB. The 1287 // predicate is a PHINode with one input for each InBB which 1288 // represents whether the hub should transfer control flow to OutBB if 1289 // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub 1290 // evaluates them in the same order as the Outgoing set-vector, and 1291 // control branches to the first outgoing block whose predicate 1292 // evaluates to true. 1293 static void convertToGuardPredicates( 1294 BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates, 1295 SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming, 1296 const BBSetVector &Outgoing) { 1297 auto &Context = Incoming.front()->getContext(); 1298 auto BoolTrue = ConstantInt::getTrue(Context); 1299 auto BoolFalse = ConstantInt::getFalse(Context); 1300 1301 // The predicate for the last outgoing is trivially true, and so we 1302 // process only the first N-1 successors. 1303 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1304 auto Out = Outgoing[i]; 1305 LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n"); 1306 auto Phi = 1307 PHINode::Create(Type::getInt1Ty(Context), Incoming.size(), 1308 StringRef("Guard.") + Out->getName(), FirstGuardBlock); 1309 GuardPredicates[Out] = Phi; 1310 } 1311 1312 for (auto In : Incoming) { 1313 Value *Condition; 1314 BasicBlock *Succ0; 1315 BasicBlock *Succ1; 1316 std::tie(Condition, Succ0, Succ1) = 1317 redirectToHub(In, FirstGuardBlock, Outgoing); 1318 1319 // Optimization: Consider an incoming block A with both successors 1320 // Succ0 and Succ1 in the set of outgoing blocks. The predicates 1321 // for Succ0 and Succ1 complement each other. If Succ0 is visited 1322 // first in the loop below, control will branch to Succ0 using the 1323 // corresponding predicate. But if that branch is not taken, then 1324 // control must reach Succ1, which means that the predicate for 1325 // Succ1 is always true. 1326 bool OneSuccessorDone = false; 1327 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) { 1328 auto Out = Outgoing[i]; 1329 auto Phi = GuardPredicates[Out]; 1330 if (Out != Succ0 && Out != Succ1) { 1331 Phi->addIncoming(BoolFalse, In); 1332 continue; 1333 } 1334 // Optimization: When only one successor is an outgoing block, 1335 // the predicate is always true. 1336 if (!Succ0 || !Succ1 || OneSuccessorDone) { 1337 Phi->addIncoming(BoolTrue, In); 1338 continue; 1339 } 1340 assert(Succ0 && Succ1); 1341 OneSuccessorDone = true; 1342 if (Out == Succ0) { 1343 Phi->addIncoming(Condition, In); 1344 continue; 1345 } 1346 auto Inverted = invertCondition(Condition); 1347 DeletionCandidates.push_back(Condition); 1348 Phi->addIncoming(Inverted, In); 1349 } 1350 } 1351 } 1352 1353 // For each outgoing block OutBB, create a guard block in the Hub. The 1354 // first guard block was already created outside, and available as the 1355 // first element in the vector of guard blocks. 1356 // 1357 // Each guard block terminates in a conditional branch that transfers 1358 // control to the corresponding outgoing block or the next guard 1359 // block. The last guard block has two outgoing blocks as successors 1360 // since the condition for the final outgoing block is trivially 1361 // true. So we create one less block (including the first guard block) 1362 // than the number of outgoing blocks. 1363 static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks, 1364 Function *F, const BBSetVector &Outgoing, 1365 BBPredicates &GuardPredicates, StringRef Prefix) { 1366 for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) { 1367 GuardBlocks.push_back( 1368 BasicBlock::Create(F->getContext(), Prefix + ".guard", F)); 1369 } 1370 assert(GuardBlocks.size() == GuardPredicates.size()); 1371 1372 // To help keep the loop simple, temporarily append the last 1373 // outgoing block to the list of guard blocks. 1374 GuardBlocks.push_back(Outgoing.back()); 1375 1376 for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) { 1377 auto Out = Outgoing[i]; 1378 assert(GuardPredicates.count(Out)); 1379 BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out], 1380 GuardBlocks[i]); 1381 } 1382 1383 // Remove the last block from the guard list. 1384 GuardBlocks.pop_back(); 1385 } 1386 1387 BasicBlock *llvm::CreateControlFlowHub( 1388 DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks, 1389 const BBSetVector &Incoming, const BBSetVector &Outgoing, 1390 const StringRef Prefix) { 1391 auto F = Incoming.front()->getParent(); 1392 auto FirstGuardBlock = 1393 BasicBlock::Create(F->getContext(), Prefix + ".guard", F); 1394 1395 SmallVector<DominatorTree::UpdateType, 16> Updates; 1396 if (DTU) { 1397 for (auto In : Incoming) { 1398 Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock}); 1399 for (auto Succ : successors(In)) { 1400 if (Outgoing.count(Succ)) 1401 Updates.push_back({DominatorTree::Delete, In, Succ}); 1402 } 1403 } 1404 } 1405 1406 BBPredicates GuardPredicates; 1407 SmallVector<WeakVH, 8> DeletionCandidates; 1408 convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates, 1409 Incoming, Outgoing); 1410 1411 GuardBlocks.push_back(FirstGuardBlock); 1412 createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix); 1413 1414 // Update the PHINodes in each outgoing block to match the new control flow. 1415 for (int i = 0, e = GuardBlocks.size(); i != e; ++i) { 1416 reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock); 1417 } 1418 reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock); 1419 1420 if (DTU) { 1421 int NumGuards = GuardBlocks.size(); 1422 assert((int)Outgoing.size() == NumGuards + 1); 1423 for (int i = 0; i != NumGuards - 1; ++i) { 1424 Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]}); 1425 Updates.push_back( 1426 {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]}); 1427 } 1428 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1429 Outgoing[NumGuards - 1]}); 1430 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1], 1431 Outgoing[NumGuards]}); 1432 DTU->applyUpdates(Updates); 1433 } 1434 1435 for (auto I : DeletionCandidates) { 1436 if (I->use_empty()) 1437 if (auto Inst = dyn_cast_or_null<Instruction>(I)) 1438 Inst->eraseFromParent(); 1439 } 1440 1441 return FirstGuardBlock; 1442 } 1443