[llvm] r334992 - Simplify blockaddress usage before giving up in MergeBlockIntoPredecessor
Xin Tong via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 18 15:59:13 PDT 2018
Author: trentxintong
Date: Mon Jun 18 15:59:13 2018
New Revision: 334992
URL: http://llvm.org/viewvc/llvm-project?rev=334992&view=rev
Log:
Simplify blockaddress usage before giving up in MergeBlockIntoPredecessor
Summary:
Simplify blockaddress usage before giving up in MergeBlockIntoPredecessor
This is a missing small optimization in MergeBlockIntoPredecessor.
This helps with one simplifycfg test which expects this case to be handled.
Reviewers: davide, spatel, brzycki, asbirlea
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D48284
Modified:
llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/trunk/test/Transforms/SimplifyCFG/dce-cond-after-folding-terminator.ll
llvm/trunk/unittests/Transforms/Utils/BasicBlockUtils.cpp
Modified: llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=334992&r1=334991&r2=334992&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp Mon Jun 18 15:59:13 2018
@@ -119,7 +119,14 @@ bool llvm::MergeBlockIntoPredecessor(Bas
LoopInfo *LI,
MemoryDependenceResults *MemDep) {
// Don't merge away blocks who have their address taken.
- if (BB->hasAddressTaken()) return false;
+ if (BB->hasAddressTaken()) {
+ // If the block has its address taken, it may be a tree of dead constants
+ // hanging off of it. These shouldn't keep the block alive.
+ BlockAddress *BA = BlockAddress::get(BB);
+ BA->removeDeadConstantUsers();
+ if (!BA->use_empty())
+ return false;
+ }
// Can't merge if there are multiple predecessors, or no predecessors.
BasicBlock *PredBB = BB->getUniquePredecessor();
Modified: llvm/trunk/test/Transforms/SimplifyCFG/dce-cond-after-folding-terminator.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/dce-cond-after-folding-terminator.ll?rev=334992&r1=334991&r2=334992&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/dce-cond-after-folding-terminator.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/dce-cond-after-folding-terminator.ll Mon Jun 18 15:59:13 2018
@@ -37,10 +37,7 @@ define void @test_indirectbr(i32 %x) {
entry:
; CHECK-LABEL: @test_indirectbr(
; CHECK-NEXT: entry:
-; Ideally this should now check:
-; CHK-NEXT: ret void
-; But that doesn't happen yet. Instead:
-; CHECK-NEXT: br label %L1
+; CHECK-NEXT: ret void
%label = bitcast i8* blockaddress(@test_indirectbr, %L1) to i8*
indirectbr i8* %label, [label %L1, label %L2]
Modified: llvm/trunk/unittests/Transforms/Utils/BasicBlockUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Utils/BasicBlockUtils.cpp?rev=334992&r1=334991&r2=334992&view=diff
==============================================================================
--- llvm/trunk/unittests/Transforms/Utils/BasicBlockUtils.cpp (original)
+++ llvm/trunk/unittests/Transforms/Utils/BasicBlockUtils.cpp Mon Jun 18 15:59:13 2018
@@ -50,3 +50,31 @@ TEST(BasicBlockUtils, SplitBlockPredeces
SplitBlockPredecessors(&F->getEntryBlock(), {}, "split.entry", &DT);
EXPECT_TRUE(DT.verify());
}
+
+TEST(BasicBlockUtils, MergeBlockIntoPredecessor) {
+ LLVMContext C;
+ std::unique_ptr<Module> M = parseIR(C,
+ R"(
+
+ define i32 @f(i8* %str) {
+ entry:
+ %dead = extractvalue [1 x i8*] [ i8* blockaddress(@f, %L0) ], 0
+ br label %L0
+ L0:
+ ret i32 0
+ }
+ )");
+
+ // First remove the dead instruction to empty the usage of the constant
+ // containing blockaddress(@f, %L0)
+ Function *F = M->getFunction("f");
+ auto BBI = F->begin();
+ Instruction *DI = &*((*BBI).begin());
+ EXPECT_TRUE(DI->use_empty());
+ DI->eraseFromParent();
+
+ // Get L0 and make sure that it can be merged into entry block.
+ ++BBI;
+ BasicBlock *BB = &(*BBI);
+ EXPECT_TRUE(MergeBlockIntoPredecessor(BB));
+}
More information about the llvm-commits
mailing list