<div><div dir="auto">Oh, I hadn't realized that! Yes, I'll do so today. - Brian</div></div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 11, 2019 at 1:58 PM Philip Reames <<a href="mailto:listmail@philipreames.com">listmail@philipreames.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Unless I'm mistaken, this duplicates removeUnreachableBlocks in<br>
Transforms/Utils/Local.h. Can you merge them please?<br>
<br>
On 3/11/19 10:51 AM, Brian Gesiak via llvm-commits wrote:<br>
> Author: modocache<br>
> Date: Mon Mar 11 10:51:57 2019<br>
> New Revision: 355846<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=355846&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=355846&view=rev</a><br>
> Log:<br>
> [Utils] Extract EliminateUnreachableBlocks (NFC)<br>
><br>
> Summary:<br>
> Extract the functionality of eliminating unreachable basic blocks<br>
> within a function, previously encapsulated within the<br>
> -unreachableblockelim pass, and make it available as a function within<br>
> BlockUtils.h. No functional change intended other than making the logic<br>
> reusable.<br>
><br>
> Exposing this logic makes it easier to implement<br>
> <a href="https://reviews.llvm.org/D59068" rel="noreferrer" target="_blank">https://reviews.llvm.org/D59068</a>, which fixes coroutines bug<br>
> <a href="https://bugs.llvm.org/show_bug.cgi?id=40979" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=40979</a>.<br>
><br>
> Reviewers: mkazantsev, wmi, davidxl, silvas, davide<br>
><br>
> Reviewed By: davide<br>
><br>
> Subscribers: llvm-commits<br>
><br>
> Tags: #llvm<br>
><br>
> Differential Revision: <a href="https://reviews.llvm.org/D59069" rel="noreferrer" target="_blank">https://reviews.llvm.org/D59069</a><br>
><br>
> Modified:<br>
> llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h<br>
> llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp<br>
> llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp<br>
> llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp<br>
><br>
> Modified: llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h?rev=355846&r1=355845&r2=355846&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h?rev=355846&r1=355845&r2=355846&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h (original)<br>
> +++ llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h Mon Mar 11 10:51:57 2019<br>
> @@ -62,6 +62,12 @@ void DeleteDeadBlocks(ArrayRef <BasicBlo<br>
> DomTreeUpdater *DTU = nullptr,<br>
> bool KeepOneInputPHIs = false);<br>
> <br>
> +/// Delete all basic blocks from \p F that are not reachable from its entry<br>
> +/// node. If \p KeepOneInputPHIs is true, one-input Phis in successors of<br>
> +/// blocks being deleted will be preserved.<br>
> +bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr,<br>
> + bool KeepOneInputPHIs = false);<br>
> +<br>
> /// We know that BB has one predecessor. If there are any single-entry PHI nodes<br>
> /// in it, fold them away. This handles the case when all entries to the PHI<br>
> /// nodes in a block are guaranteed equal, such as when the block has exactly<br>
><br>
> Modified: llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp?rev=355846&r1=355845&r2=355846&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp?rev=355846&r1=355845&r2=355846&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp (original)<br>
> +++ llvm/trunk/lib/CodeGen/UnreachableBlockElim.cpp Mon Mar 11 10:51:57 2019<br>
> @@ -40,31 +40,10 @@<br>
> #include "llvm/Transforms/Utils/BasicBlockUtils.h"<br>
> using namespace llvm;<br>
> <br>
> -static bool eliminateUnreachableBlock(Function &F) {<br>
> - df_iterator_default_set<BasicBlock*> Reachable;<br>
> -<br>
> - // Mark all reachable blocks.<br>
> - for (BasicBlock *BB : depth_first_ext(&F, Reachable))<br>
> - (void)BB/* Mark all reachable blocks */;<br>
> -<br>
> - // Collect all dead blocks.<br>
> - std::vector<BasicBlock*> DeadBlocks;<br>
> - for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)<br>
> - if (!Reachable.count(&*I)) {<br>
> - BasicBlock *BB = &*I;<br>
> - DeadBlocks.push_back(BB);<br>
> - }<br>
> -<br>
> - // Delete the dead blocks.<br>
> - DeleteDeadBlocks(DeadBlocks);<br>
> -<br>
> - return !DeadBlocks.empty();<br>
> -}<br>
> -<br>
> namespace {<br>
> class UnreachableBlockElimLegacyPass : public FunctionPass {<br>
> bool runOnFunction(Function &F) override {<br>
> - return eliminateUnreachableBlock(F);<br>
> + return llvm::EliminateUnreachableBlocks(F);<br>
> }<br>
> <br>
> public:<br>
> @@ -89,7 +68,7 @@ FunctionPass *llvm::createUnreachableBlo<br>
> <br>
> PreservedAnalyses UnreachableBlockElimPass::run(Function &F,<br>
> FunctionAnalysisManager &AM) {<br>
> - bool Changed = eliminateUnreachableBlock(F);<br>
> + bool Changed = llvm::EliminateUnreachableBlocks(F);<br>
> if (!Changed)<br>
> return PreservedAnalyses::all();<br>
> PreservedAnalyses PA;<br>
><br>
> Modified: llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=355846&r1=355845&r2=355846&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=355846&r1=355845&r2=355846&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp (original)<br>
> +++ llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp Mon Mar 11 10:51:57 2019<br>
> @@ -110,6 +110,28 @@ void llvm::DeleteDeadBlocks(ArrayRef <Ba<br>
> BB->eraseFromParent();<br>
> }<br>
> <br>
> +bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,<br>
> + bool KeepOneInputPHIs) {<br>
> + df_iterator_default_set<BasicBlock*> Reachable;<br>
> +<br>
> + // Mark all reachable blocks.<br>
> + for (BasicBlock *BB : depth_first_ext(&F, Reachable))<br>
> + (void)BB/* Mark all reachable blocks */;<br>
> +<br>
> + // Collect all dead blocks.<br>
> + std::vector<BasicBlock*> DeadBlocks;<br>
> + for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)<br>
> + if (!Reachable.count(&*I)) {<br>
> + BasicBlock *BB = &*I;<br>
> + DeadBlocks.push_back(BB);<br>
> + }<br>
> +<br>
> + // Delete the dead blocks.<br>
> + DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);<br>
> +<br>
> + return !DeadBlocks.empty();<br>
> +}<br>
> +<br>
> void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,<br>
> MemoryDependenceResults *MemDep) {<br>
> if (!isa<PHINode>(BB->begin())) return;<br>
><br>
> Modified: llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp?rev=355846&r1=355845&r2=355846&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp?rev=355846&r1=355845&r2=355846&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp (original)<br>
> +++ llvm/trunk/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp Mon Mar 11 10:51:57 2019<br>
> @@ -25,6 +25,64 @@ static std::unique_ptr<Module> parseIR(L<br>
> return Mod;<br>
> }<br>
> <br>
> +TEST(BasicBlockUtils, EliminateUnreachableBlocks) {<br>
> + LLVMContext C;<br>
> +<br>
> + std::unique_ptr<Module> M = parseIR(<br>
> + C,<br>
> + "define i32 @has_unreachable(i1 %cond) {\n"<br>
> + "entry:\n"<br>
> + " br i1 %cond, label %bb0, label %bb1\n"<br>
> + "bb0:\n"<br>
> + " br label %bb1\n"<br>
> + "bb1:\n"<br>
> + " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"<br>
> + " ret i32 %phi\n"<br>
> + "bb2:\n"<br>
> + " ret i32 42\n"<br>
> + "}\n"<br>
> + "\n"<br>
> + );<br>
> +<br>
> + auto *F = M->getFunction("has_unreachable");<br>
> + DominatorTree DT(*F);<br>
> + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);<br>
> +<br>
> + EXPECT_EQ(F->size(), (size_t)4);<br>
> + bool Result = EliminateUnreachableBlocks(*F, &DTU);<br>
> + EXPECT_TRUE(Result);<br>
> + EXPECT_EQ(F->size(), (size_t)3);<br>
> + EXPECT_TRUE(DT.verify());<br>
> +}<br>
> +<br>
> +TEST(BasicBlockUtils, NoUnreachableBlocksToEliminate) {<br>
> + LLVMContext C;<br>
> +<br>
> + std::unique_ptr<Module> M = parseIR(<br>
> + C,<br>
> + "define i32 @no_unreachable(i1 %cond) {\n"<br>
> + "entry:\n"<br>
> + " br i1 %cond, label %bb0, label %bb1\n"<br>
> + "bb0:\n"<br>
> + " br label %bb1\n"<br>
> + "bb1:\n"<br>
> + " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]"<br>
> + " ret i32 %phi\n"<br>
> + "}\n"<br>
> + "\n"<br>
> + );<br>
> +<br>
> + auto *F = M->getFunction("no_unreachable");<br>
> + DominatorTree DT(*F);<br>
> + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);<br>
> +<br>
> + EXPECT_EQ(F->size(), (size_t)3);<br>
> + bool Result = EliminateUnreachableBlocks(*F, &DTU);<br>
> + EXPECT_FALSE(Result);<br>
> + EXPECT_EQ(F->size(), (size_t)3);<br>
> + EXPECT_TRUE(DT.verify());<br>
> +}<br>
> +<br>
> TEST(BasicBlockUtils, SplitBlockPredecessors) {<br>
> LLVMContext C;<br>
> <br>
><br>
><br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>