diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -418,17 +418,21 @@ // ProcessBlock doesn't thread BBs with unconditional TIs. However, if BB // is "almost empty", we attempt to merge BB with its sole successor. auto *BI = dyn_cast(BB.getTerminator()); - if (BI && BI->isUnconditional() && - // The terminator must be the only non-phi instruction in BB. - BB.getFirstNonPHIOrDbg()->isTerminator() && - // Don't alter Loop headers and latches to ensure another pass can - // detect and transform nested loops later. - !LoopHeaders.count(&BB) && !LoopHeaders.count(BI->getSuccessor(0)) && - TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU)) { - // BB is valid for cleanup here because we passed in DTU. F remains - // BB's parent until a DTU->getDomTree() event. - LVI->eraseBlock(&BB); - Changed = true; + if (BI && BI->isUnconditional()) { + BasicBlock *Succ = BI->getSuccessor(0); + if ( + // The terminator must be the only non-phi instruction in BB. + BB.getFirstNonPHIOrDbg()->isTerminator() && + // Don't alter Loop headers and latches to ensure another pass can + // detect and transform nested loops later. + !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) && + TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU)) { + // BB is valid for cleanup here because we passed in DTU. F remains + // BB's parent until a DTU->getDomTree() event. + RemoveRedundantDbgInstrs(Succ); + LVI->eraseBlock(&BB); + Changed = true; + } } } EverChanged |= Changed;