Skip to content

Commit 6d01789

Browse files
nikicakiramenai
authored andcommitted
[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 llvm/llvm-project#102597.
1 parent f149d74 commit 6d01789

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
@@ -11976,9 +11976,14 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1197611976
SmallDenseMap<const SCEV *, int, 8> Multiplicity;
1197711977
APInt Diff(BW, 0);
1197811978
auto Add = [&](const SCEV *S, int Mul) {
11979-
if (auto *C = dyn_cast<SCEVConstant>(S))
11980-
Diff += C->getAPInt() * Mul;
11981-
else
11979+
if (auto *C = dyn_cast<SCEVConstant>(S)) {
11980+
if (Mul == 1) {
11981+
Diff += C->getAPInt();
11982+
} else {
11983+
assert(Mul == -1);
11984+
Diff -= C->getAPInt();
11985+
}
11986+
} else
1198211987
Multiplicity[S] += Mul;
1198311988
};
1198411989
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)