diff options
author | Brian Gesiak <modocache@gmail.com> | 2019-03-11 17:51:57 +0000 |
---|---|---|
committer | Brian Gesiak <modocache@gmail.com> | 2019-03-11 17:51:57 +0000 |
commit | 4349dc76fa08d0d6dde47286f0b83a6f76729fe4 (patch) | |
tree | d3597aef9dd9c04725251e93444dee2a2b636844 /llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp | |
parent | 1bb5b564857eed24e13003af8392cb48d62ee14e (diff) | |
download | bcm5719-llvm-4349dc76fa08d0d6dde47286f0b83a6f76729fe4.tar.gz bcm5719-llvm-4349dc76fa08d0d6dde47286f0b83a6f76729fe4.zip |
[Utils] Extract EliminateUnreachableBlocks (NFC)
Summary:
Extract the functionality of eliminating unreachable basic blocks
within a function, previously encapsulated within the
-unreachableblockelim pass, and make it available as a function within
BlockUtils.h. No functional change intended other than making the logic
reusable.
Exposing this logic makes it easier to implement
https://reviews.llvm.org/D59068, which fixes coroutines bug
https://bugs.llvm.org/show_bug.cgi?id=40979.
Reviewers: mkazantsev, wmi, davidxl, silvas, davide
Reviewed By: davide
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59069
llvm-svn: 355846
Diffstat (limited to 'llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp')
-rw-r--r-- | llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp b/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp index 2d3731c03e0..bf8228e99d8 100644 --- a/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp +++ b/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp @@ -25,6 +25,64 @@ static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { return Mod; } +TEST(BasicBlockUtils, EliminateUnreachableBlocks) { + LLVMContext C; + + std::unique_ptr<Module> M = parseIR( + C, + "define i32 @has_unreachable(i1 %cond) {\n" + "entry:\n" + " br i1 %cond, label %bb0, label %bb1\n" + "bb0:\n" + " br label %bb1\n" + "bb1:\n" + " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]" + " ret i32 %phi\n" + "bb2:\n" + " ret i32 42\n" + "}\n" + "\n" + ); + + auto *F = M->getFunction("has_unreachable"); + DominatorTree DT(*F); + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); + + EXPECT_EQ(F->size(), (size_t)4); + bool Result = EliminateUnreachableBlocks(*F, &DTU); + EXPECT_TRUE(Result); + EXPECT_EQ(F->size(), (size_t)3); + EXPECT_TRUE(DT.verify()); +} + +TEST(BasicBlockUtils, NoUnreachableBlocksToEliminate) { + LLVMContext C; + + std::unique_ptr<Module> M = parseIR( + C, + "define i32 @no_unreachable(i1 %cond) {\n" + "entry:\n" + " br i1 %cond, label %bb0, label %bb1\n" + "bb0:\n" + " br label %bb1\n" + "bb1:\n" + " %phi = phi i32 [ 0, %entry ], [ 1, %bb0 ]" + " ret i32 %phi\n" + "}\n" + "\n" + ); + + auto *F = M->getFunction("no_unreachable"); + DominatorTree DT(*F); + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); + + EXPECT_EQ(F->size(), (size_t)3); + bool Result = EliminateUnreachableBlocks(*F, &DTU); + EXPECT_FALSE(Result); + EXPECT_EQ(F->size(), (size_t)3); + EXPECT_TRUE(DT.verify()); +} + TEST(BasicBlockUtils, SplitBlockPredecessors) { LLVMContext C; |