[llvm-branch-commits] [llvm] e2fc11c - [JumpThreading] Call eraseBlock when folding a conditional branch

Kazu Hirata via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 3 23:55:23 PST 2020


Author: Kazu Hirata
Date: 2020-12-03T23:50:17-08:00
New Revision: e2fc11cf9f6cd801b5f3bf94541561829ff02f77

URL: https://github.com/llvm/llvm-project/commit/e2fc11cf9f6cd801b5f3bf94541561829ff02f77
DIFF: https://github.com/llvm/llvm-project/commit/e2fc11cf9f6cd801b5f3bf94541561829ff02f77.diff

LOG: [JumpThreading] Call eraseBlock when folding a conditional branch

This patch teaches the jump threading pass to call BPI->eraseBlock
when it folds a conditional branch.

Without this patch, BranchProbabilityInfo could end up with stale edge
probabilities for the basic block containing the conditional branch --
one edge probability with less than 1.0 and the other for a removed
edge.

Differential Revision: https://reviews.llvm.org/D92608

Added: 
    llvm/test/Transforms/JumpThreading/thread-prob-4.ll
    llvm/test/Transforms/JumpThreading/thread-prob-5.ll
    llvm/test/Transforms/JumpThreading/thread-prob-6.ll

Modified: 
    llvm/lib/Transforms/Scalar/JumpThreading.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index b287a804600a..a24dd8029f68 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1111,6 +1111,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
                       << '\n');
     ++NumFolds;
     ConstantFoldTerminator(BB, true, nullptr, DTU);
+    if (HasProfileData)
+      BPI->eraseBlock(BB);
     return true;
   }
 
@@ -1166,6 +1168,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
         }
         DTU->applyUpdatesPermissive(
             {{DominatorTree::Delete, BB, ToRemoveSucc}});
+        if (HasProfileData)
+          BPI->eraseBlock(BB);
         return true;
       }
 
@@ -1263,6 +1267,8 @@ bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) {
       UncondBI->setDebugLoc(BI->getDebugLoc());
       BI->eraseFromParent();
       DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
+      if (HasProfileData)
+        BPI->eraseBlock(BB);
       return true;
     }
     CurrentBB = CurrentPred;

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-4.ll b/llvm/test/Transforms/JumpThreading/thread-prob-4.ll
new file mode 100644
index 000000000000..2e3f1f241bd8
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-4.ll
@@ -0,0 +1,29 @@
+; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure that we clear edge probabilities for bb1 as we fold
+; the conditional branch in it.
+
+; CHECK: eraseBlock bb1
+
+define i32 @foo(i32 %arg) !prof !0 {
+; CHECK-LABEL: @foo
+  %cond1 = icmp eq i32 %arg, 42
+  br i1 %cond1, label %bb1, label %bb2
+
+bb2:
+  ret i32 2
+
+bb1:
+  %cond2 = icmp eq i32 %arg, 42
+  br i1 %cond2, label %bb3, label %bb4
+
+bb3:
+  ret i32 3
+
+bb4:
+; CHECK-NOT: bb4:
+  ret i32 4
+}
+
+!0 = !{!"function_entry_count", i64 0}

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-5.ll b/llvm/test/Transforms/JumpThreading/thread-prob-5.ll
new file mode 100644
index 000000000000..89bf2ad4efcf
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-5.ll
@@ -0,0 +1,28 @@
+; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure that we clear edge probabilities for bb1 as we fold
+; the conditional branch in it.
+
+; CHECK: eraseBlock bb1
+
+define void @foo(i32 %i, i32 %len) !prof !0 {
+; CHECK-LABEL: @foo
+  %i.inc = add nuw i32 %i, 1
+  %c0 = icmp ult i32 %i.inc, %len
+  br i1 %c0, label %bb1, label %bb2
+
+bb1:
+; CHECK: bb1:
+  %c1 = icmp ult i32 %i, %len
+  br i1 %c1, label %bb2, label %bb3
+
+bb2:
+  ret void
+
+bb3:
+; CHECK-NOT: bb3:
+  ret void
+}
+
+!0 = !{!"function_entry_count", i64 0}

diff  --git a/llvm/test/Transforms/JumpThreading/thread-prob-6.ll b/llvm/test/Transforms/JumpThreading/thread-prob-6.ll
new file mode 100644
index 000000000000..1e0949ff6618
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-6.ll
@@ -0,0 +1,22 @@
+; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure that we clear edge probabilities for bb1 as we fold
+; the conditional branch in it.
+
+; CHECK: eraseBlock bb1
+
+define i32 @foo() !prof !0 {
+; CHECK-LABEL: @foo
+bb1:
+  br i1 true, label %bb2, label %bb3
+
+bb2:
+  ret i32 3
+
+bb3:
+; CHECK-NOT: bb3:
+  ret i32 7
+}
+
+!0 = !{!"function_entry_count", i64 0}


        


More information about the llvm-branch-commits mailing list