Skip to content

Commit 80d317e

Browse files
mtrofinDebadri Basak
authored andcommitted
[SimplifyCFG] Don't propagate weights to unconditional branches in turnSwitchRangeIntoICmp (llvm#165931)
PR llvm#161000 introduced a bug whereby the IR would become invalid by having an unconditional branch have `!prof`​attached to it. This only became evident in PR llvm#165744, because the IR of `test/Transforms/SimplifyCFG/pr165301.ll`​was simple enough to both (1) introduce the unconditional branch, and (2) survive in that fashion until the end of the pass (simplifycfg) and thus trip the verifier.
1 parent 2ed791f commit 80d317e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5956,7 +5956,7 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
59565956
}
59575957

59585958
// Update weight for the newly-created conditional branch.
5959-
if (hasBranchWeightMD(*SI)) {
5959+
if (hasBranchWeightMD(*SI) && NewBI->isConditional()) {
59605960
SmallVector<uint64_t, 8> Weights;
59615961
getBranchWeights(SI, Weights);
59625962
if (Weights.size() == 1 + SI->getNumCases()) {

llvm/test/Transforms/SimplifyCFG/pr165301.ll

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 6
22
; RUN: opt -S -passes="simplifycfg<switch-range-to-icmp>" < %s | FileCheck %s
33

44
; Make sure there's no use after free when removing incoming values from PHI nodes
55

6-
define i32 @pr165301(i1 %cond) {
6+
define i32 @pr165301(i1 %cond) !prof !0 {
77
; CHECK-LABEL: define i32 @pr165301(
8-
; CHECK-SAME: i1 [[COND:%.*]]) {
8+
; CHECK-SAME: i1 [[COND:%.*]]) !prof [[PROF0:![0-9]+]] {
99
; CHECK-NEXT: [[ENTRY:.*:]]
1010
; CHECK-NEXT: br label %[[SWITCHBB:.*]]
1111
; CHECK: [[SWITCHBB]]:
@@ -18,9 +18,14 @@ switchbb:
1818
switch i1 %cond, label %default [
1919
i1 false, label %switchbb
2020
i1 true, label %switchbb
21-
]
21+
], !prof !1
2222

2323
default:
2424
%phi.lcssa = phi i32 [ 0, %switchbb ]
2525
ret i32 %phi.lcssa
2626
}
27+
!0 = !{!"function_entry_count", i32 10}
28+
!1 = !{!"branch_weights", i32 2, i32 3, i32 5}
29+
;.
30+
; CHECK: [[PROF0]] = !{!"function_entry_count", i32 10}
31+
;.

0 commit comments

Comments
 (0)