[llvm] 147ccc8 - [JumpThreading] Call eraseBlock when folding a conditional branch
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 15 22:29:51 PST 2020
Author: Kazu Hirata
Date: 2020-11-15T22:29:30-08:00
New Revision: 147ccc848a5581765a758dc752842053aa0024d0
URL: https://github.com/llvm/llvm-project/commit/147ccc848a5581765a758dc752842053aa0024d0
DIFF: https://github.com/llvm/llvm-project/commit/147ccc848a5581765a758dc752842053aa0024d0.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.
This patch is one of the steps before we can safely re-apply D91017.
Differential Revision: https://reviews.llvm.org/D91511
Added:
llvm/test/Transforms/JumpThreading/thread-prob-2.ll
Modified:
llvm/lib/Analysis/BranchProbabilityInfo.cpp
llvm/lib/Transforms/Scalar/JumpThreading.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
index a75001ff5f98..ca4b58044d67 100644
--- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -1182,6 +1182,8 @@ BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
}
void BranchProbabilityInfo::eraseBlock(const BasicBlock *BB) {
+ LLVM_DEBUG(dbgs() << "eraseBlock " << BB->getName() << "\n");
+
// Note that we cannot use successors of BB because the terminator of BB may
// have changed when eraseBlock is called as a BasicBlockCallbackVH callback.
// Instead we remove prob data for the block by iterating successors by their
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 936c888620a2..df44b3893589 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1697,6 +1697,8 @@ bool JumpThreadingPass::processThreadableEdges(Value *Cond, BasicBlock *BB,
BranchInst::Create(OnlyDest, Term);
Term->eraseFromParent();
DTU->applyUpdatesPermissive(Updates);
+ if (HasProfileData)
+ BPI->eraseBlock(BB);
// If the condition is now dead due to the removal of the old terminator,
// erase it.
diff --git a/llvm/test/Transforms/JumpThreading/thread-prob-2.ll b/llvm/test/Transforms/JumpThreading/thread-prob-2.ll
new file mode 100644
index 000000000000..0cd721639498
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-2.ll
@@ -0,0 +1,27 @@
+; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure that we clear edge probabilities for bb.cond as we fold
+; the conditional branch in it.
+
+; CHECK: eraseBlock bb.cond
+
+define i32 @foo(i1 %cond) !prof !0 {
+; CHECK-LABEL: @foo
+; CHECK: bb.entry:
+; CHECK-NEXT: br i1 %cond, label %bb.31, label %bb.12
+; CHECK-NOT: bb.cond:
+bb.entry:
+ br i1 %cond, label %bb.31, label %bb.cond
+
+bb.cond:
+ br i1 %cond, label %bb.31, label %bb.12
+
+bb.31:
+ ret i32 31
+
+bb.12:
+ ret i32 12
+}
+
+!0 = !{!"function_entry_count", i64 0}
More information about the llvm-commits
mailing list