[PATCH] D92608: [JumpThreading] Call eraseBlock when folding a conditional branch

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 3 14:13:39 PST 2020


kazu created this revision.
kazu added a reviewer: yrouban.
Herald added a subscriber: hiraditya.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92608

Files:
  llvm/lib/Transforms/Scalar/JumpThreading.cpp
  llvm/test/Transforms/JumpThreading/thread-prob-4.ll
  llvm/test/Transforms/JumpThreading/thread-prob-5.ll


Index: llvm/test/Transforms/JumpThreading/thread-prob-5.ll
===================================================================
--- /dev/null
+++ 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}
Index: llvm/test/Transforms/JumpThreading/thread-prob-4.ll
===================================================================
--- /dev/null
+++ 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}
Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1166,6 +1166,8 @@
         }
         DTU->applyUpdatesPermissive(
             {{DominatorTree::Delete, BB, ToRemoveSucc}});
+        if (HasProfileData)
+          BPI->eraseBlock(BB);
         return true;
       }
 
@@ -1263,6 +1265,8 @@
       UncondBI->setDebugLoc(BI->getDebugLoc());
       BI->eraseFromParent();
       DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
+      if (HasProfileData)
+        BPI->eraseBlock(BB);
       return true;
     }
     CurrentBB = CurrentPred;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92608.309363.patch
Type: text/x-patch
Size: 2325 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201203/508cf50e/attachment.bin>


More information about the llvm-commits mailing list