diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index 0c3afc4e403..6bddbb2b1c5 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -2264,8 +2264,16 @@ bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI, if (DTU) { DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); - for (auto *BB : ToDeleteBBs) + bool Deleted = false; + for (auto *BB : ToDeleteBBs) { + if (DTU->isBBPendingDeletion(BB)) + --NumRemoved; + else + Deleted = true; DTU->deleteBB(BB); + } + if (!Deleted) + return false; } return true; } diff --git a/unittests/Transforms/Utils/Local.cpp b/unittests/Transforms/Utils/Local.cpp index 312c32e38ac..53cfc6b4a34 100644 --- a/unittests/Transforms/Utils/Local.cpp +++ b/unittests/Transforms/Utils/Local.cpp @@ -844,4 +844,23 @@ TEST(Local, RemoveUnreachableBlocks) { runWithDomTree(*M, "br_self_loop", runLazy); runWithDomTree(*M, "br_constant", runLazy); runWithDomTree(*M, "br_loop", runLazy); + + M = parseIR(C, + R"( + define void @f() { + entry: + ret void + bb0: + ret void + } + )"); + + auto checkRUBlocksRetVal = [&](Function &F, DominatorTree *DT) { + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); + EXPECT_TRUE(removeUnreachableBlocks(F, nullptr, &DTU)); + EXPECT_FALSE(removeUnreachableBlocks(F, nullptr, &DTU)); + EXPECT_TRUE(DTU.getDomTree().verify()); + }; + + runWithDomTree(*M, "f", checkRUBlocksRetVal); } \ No newline at end of file