Skip to content

Commit 6580c91

Browse files
authored
[SCEV] Fold ((-1 * C1) * D / C1) -> -1 * D. (#157555)
Treat negative constants C as -1 * abs(C1) when folding multiplies and udivs. Alive2 Proof: https://alive2.llvm.org/ce/z/bdj9W2 PR: #157555
1 parent dcdbb39 commit 6580c91

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3217,15 +3217,19 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
32173217
}
32183218

32193219
// Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
3220-
// D is a multiple of C2, and C1 is a multiple of C1.
3220+
// D is a multiple of C2, and C1 is a multiple of C2.
32213221
const SCEV *D;
3222+
APInt C1V = LHSC->getAPInt();
3223+
// (C1 * D /u C2) == -1 * -C1 * D /u C2 when C1 != INT_MIN.
3224+
if (C1V.isNegative() && !C1V.isMinSignedValue())
3225+
C1V = C1V.abs();
32223226
const SCEVConstant *C2;
3223-
const APInt &LHSV = LHSC->getAPInt();
3224-
if (LHSV.isPowerOf2() &&
3227+
if (C1V.isPowerOf2() &&
32253228
match(Ops[1], m_scev_UDiv(m_SCEV(D), m_SCEVConstant(C2))) &&
3226-
C2->getAPInt().isPowerOf2() && LHSV.uge(C2->getAPInt()) &&
3227-
LHSV.logBase2() <= getMinTrailingZeros(D)) {
3228-
return getMulExpr(getUDivExpr(LHSC, C2), D);
3229+
C2->getAPInt().isPowerOf2() && C1V.uge(C2->getAPInt()) &&
3230+
C1V.logBase2() <= getMinTrailingZeros(D)) {
3231+
const SCEV *NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
3232+
return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(NewMul);
32293233
}
32303234
}
32313235
}

llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ define void @udiv4_and_udiv2(i1 %c, ptr %A) {
2323
; CHECK-NEXT: %gep.16 = getelementptr i16, ptr %A, i64 %iv
2424
; CHECK-NEXT: --> {((2 * ((zext i32 %start to i64) /u 4))<nuw><nsw> + %A),+,2}<%loop> U: full-set S: full-set Exits: ((zext i32 %start to i64) + %A) LoopDispositions: { %loop: Computable }
2525
; CHECK-NEXT: %gep.32 = getelementptr i32, ptr %A, i64 %iv
26-
; CHECK-NEXT: --> {((zext i32 %start to i64) + %A),+,4}<%loop> U: full-set S: full-set Exits: ((3 * (zext i32 %start to i64))<nuw><nsw> + (-4 * ((zext i32 %start to i64) /u 4))<nsw> + %A) LoopDispositions: { %loop: Computable }
26+
; CHECK-NEXT: --> {((zext i32 %start to i64) + %A),+,4}<%loop> U: full-set S: full-set Exits: ((2 * (zext i32 %start to i64))<nuw><nsw> + %A) LoopDispositions: { %loop: Computable }
2727
; CHECK-NEXT: %gep.40 = getelementptr <{ i32, i8 }>, ptr %A, i64 %iv
2828
; CHECK-NEXT: --> {((5 * ((zext i32 %start to i64) /u 4))<nuw><nsw> + %A),+,5}<%loop> U: full-set S: full-set Exits: ((5 * ((zext i32 %start to i64) /u 2))<nuw><nsw> + %A) LoopDispositions: { %loop: Computable }
2929
; CHECK-NEXT: %gep.48 = getelementptr <{ i32, i16 }>, ptr %A, i64 %iv

0 commit comments

Comments
 (0)