Skip to content

Commit b93ff3e

Browse files
authored
[SimplifyCFG] Fix uint32_t overflow in cbranch to cbranch merge prevention check. (#72329)
This fixes #72323. Resulted from f054947
1 parent 35b10ac commit b93ff3e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4351,10 +4351,11 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
43514351
SmallVector<uint32_t, 2> PredWeights;
43524352
if (!PBI->getMetadata(LLVMContext::MD_unpredictable) &&
43534353
extractBranchWeights(*PBI, PredWeights) &&
4354-
(PredWeights[0] + PredWeights[1]) != 0) {
4354+
(static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
43554355

43564356
BranchProbability CommonDestProb = BranchProbability::getBranchProbability(
4357-
PredWeights[PBIOp], PredWeights[0] + PredWeights[1]);
4357+
PredWeights[PBIOp],
4358+
static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
43584359

43594360
BranchProbability Likely = TTI.getPredictableBranchThreshold();
43604361
if (CommonDestProb >= Likely)

llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ exit:
7979
ret void
8080
}
8181

82+
define void @uint32_overflow_test(i1 %arg, i1 %arg1) {
83+
; CHECK-LABEL: @uint32_overflow_test(
84+
; CHECK-NEXT: bb:
85+
; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[ARG:%.*]], i1 true, i1 [[ARG1:%.*]]
86+
; CHECK-NEXT: ret void
87+
;
88+
bb:
89+
br i1 %arg, label %bb4, label %bb2, !prof !3
90+
91+
bb2:
92+
br i1 %arg1, label %bb4, label %bb3
93+
94+
bb3:
95+
br label %bb4
96+
97+
bb4:
98+
ret void
99+
}
100+
82101
!0 = !{!"branch_weights", i32 1, i32 1000}
83102
!1 = !{!"branch_weights", i32 1000, i32 1}
84103
!2 = !{!"branch_weights", i32 3, i32 2}
104+
!3 = !{!"branch_weights", i32 -258677585, i32 -1212131848}

0 commit comments

Comments
 (0)