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