Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -423,13 +423,21 @@ 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; - } + !LoopHeaders.count(&BB) && !LoopHeaders.count(BI->getSuccessor(0))) + // This if should be separated from condition above because in debug + // build the key for LoopHeader is AssertVH. As a result in + // LoopHeaders.count check new temporary AssertVH will be created. + // At the same time the function TryToSimplifyUncondBranchFromEmptyBlock + // can remove the block causing the check for alive AssertVH will be + // failed and error will be triggered. So we need the temporary + // AssertVH is destroyed before TryToSimplifyUncondBranchFromEmptyBlock + // is invoked. + if (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; + } } EverChanged |= Changed; } while (Changed);