Skip to content

Commit e2fc11c

Browse files
[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
1 parent f5f1a5c commit e2fc11c

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
11111111
<< '\n');
11121112
++NumFolds;
11131113
ConstantFoldTerminator(BB, true, nullptr, DTU);
1114+
if (HasProfileData)
1115+
BPI->eraseBlock(BB);
11141116
return true;
11151117
}
11161118

@@ -1166,6 +1168,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
11661168
}
11671169
DTU->applyUpdatesPermissive(
11681170
{{DominatorTree::Delete, BB, ToRemoveSucc}});
1171+
if (HasProfileData)
1172+
BPI->eraseBlock(BB);
11691173
return true;
11701174
}
11711175

@@ -1263,6 +1267,8 @@ bool JumpThreadingPass::processImpliedCondition(BasicBlock *BB) {
12631267
UncondBI->setDebugLoc(BI->getDebugLoc());
12641268
BI->eraseFromParent();
12651269
DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, RemoveSucc}});
1270+
if (HasProfileData)
1271+
BPI->eraseBlock(BB);
12661272
return true;
12671273
}
12681274
CurrentBB = CurrentPred;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; Make sure that we clear edge probabilities for bb1 as we fold
5+
; the conditional branch in it.
6+
7+
; CHECK: eraseBlock bb1
8+
9+
define i32 @foo(i32 %arg) !prof !0 {
10+
; CHECK-LABEL: @foo
11+
%cond1 = icmp eq i32 %arg, 42
12+
br i1 %cond1, label %bb1, label %bb2
13+
14+
bb2:
15+
ret i32 2
16+
17+
bb1:
18+
%cond2 = icmp eq i32 %arg, 42
19+
br i1 %cond2, label %bb3, label %bb4
20+
21+
bb3:
22+
ret i32 3
23+
24+
bb4:
25+
; CHECK-NOT: bb4:
26+
ret i32 4
27+
}
28+
29+
!0 = !{!"function_entry_count", i64 0}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; Make sure that we clear edge probabilities for bb1 as we fold
5+
; the conditional branch in it.
6+
7+
; CHECK: eraseBlock bb1
8+
9+
define void @foo(i32 %i, i32 %len) !prof !0 {
10+
; CHECK-LABEL: @foo
11+
%i.inc = add nuw i32 %i, 1
12+
%c0 = icmp ult i32 %i.inc, %len
13+
br i1 %c0, label %bb1, label %bb2
14+
15+
bb1:
16+
; CHECK: bb1:
17+
%c1 = icmp ult i32 %i, %len
18+
br i1 %c1, label %bb2, label %bb3
19+
20+
bb2:
21+
ret void
22+
23+
bb3:
24+
; CHECK-NOT: bb3:
25+
ret void
26+
}
27+
28+
!0 = !{!"function_entry_count", i64 0}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: opt -debug-only=branch-prob -jump-threading -S %s 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; Make sure that we clear edge probabilities for bb1 as we fold
5+
; the conditional branch in it.
6+
7+
; CHECK: eraseBlock bb1
8+
9+
define i32 @foo() !prof !0 {
10+
; CHECK-LABEL: @foo
11+
bb1:
12+
br i1 true, label %bb2, label %bb3
13+
14+
bb2:
15+
ret i32 3
16+
17+
bb3:
18+
; CHECK-NOT: bb3:
19+
ret i32 7
20+
}
21+
22+
!0 = !{!"function_entry_count", i64 0}

0 commit comments

Comments
 (0)