Skip to content

Commit 3512bcc

Browse files
committed
[SCEV] Fix incorrect extension in computeConstantDifference()
The Mul factor was zero-extended here, resulting in incorrect results for integers larger than 64-bit. As we currently only multiply by 1 or -1, just split this into two cases -- there's no need for a full multiplication here. Fixes #102597.
1 parent 513c372 commit 3512bcc

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11961,9 +11961,14 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1196111961
SmallDenseMap<const SCEV *, int, 8> Multiplicity;
1196211962
APInt Diff(BW, 0);
1196311963
auto Add = [&](const SCEV *S, int Mul) {
11964-
if (auto *C = dyn_cast<SCEVConstant>(S))
11965-
Diff += C->getAPInt() * Mul;
11966-
else
11964+
if (auto *C = dyn_cast<SCEVConstant>(S)) {
11965+
if (Mul == 1) {
11966+
Diff += C->getAPInt();
11967+
} else {
11968+
assert(Mul == -1);
11969+
Diff -= C->getAPInt();
11970+
}
11971+
} else
1196711972
Multiplicity[S] += Mul;
1196811973
};
1196911974
auto Decompose = [&](const SCEV *S, int Mul) {

llvm/test/Transforms/IndVarSimplify/pr102597.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=indvars < %s | FileCheck %s
33

4-
; FIXME: This is a miscompile.
4+
; The %tobool condition should not be optimized away.
55
define void @test() {
66
; CHECK-LABEL: define void @test() {
77
; CHECK-NEXT: [[ENTRY:.*]]:
88
; CHECK-NEXT: br label %[[LOOP:.*]]
99
; CHECK: [[LOOP]]:
1010
; CHECK-NEXT: [[IV:%.*]] = phi i128 [ 3, %[[ENTRY]] ], [ [[IV_DEC:%.*]], %[[LOOP_LATCH:.*]] ]
11-
; CHECK-NEXT: br i1 true, label %[[LOOP_LATCH]], label %[[IF:.*]]
11+
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i128 [[IV]], 0
12+
; CHECK-NEXT: br i1 [[TOBOOL]], label %[[LOOP_LATCH]], label %[[IF:.*]]
1213
; CHECK: [[IF]]:
1314
; CHECK-NEXT: call void @foo()
1415
; CHECK-NEXT: br label %[[LOOP_LATCH]]

0 commit comments

Comments
 (0)