Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3217,15 +3217,19 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
}

// Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
// D is a multiple of C2, and C1 is a multiple of C1.
// D is a multiple of C2, and C1 is a multiple of C2.
const SCEV *D;
APInt C1V = LHSC->getAPInt();
// (C1 * D /u C2) == -1 * -C1 * D /u C2 when C1 != INT_MIN.
if (C1V.isNegative() && !C1V.isMinSignedValue())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the INT_MIN check here needed? Isn't abs() just a no-op for that?

Copy link
Contributor

@nikic nikic Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I guess this avoid doing an unnecessary getNegativeSCEV() lateron. No, that's based on the equality comparison, so it shouldn't matter if we do an extra abs() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a no-op, we would end up with (-INT_MIN * D /u C2) == (-1 * -INT_MIN * D /u C2). Can change to use abs() if you prefer and remove the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it as-is for now, when this gets generalized to non-constants we can adjust it

C1V = C1V.abs();
const SCEVConstant *C2;
const APInt &LHSV = LHSC->getAPInt();
if (LHSV.isPowerOf2() &&
if (C1V.isPowerOf2() &&
match(Ops[1], m_scev_UDiv(m_SCEV(D), m_SCEVConstant(C2))) &&
C2->getAPInt().isPowerOf2() && LHSV.uge(C2->getAPInt()) &&
LHSV.logBase2() <= getMinTrailingZeros(D)) {
return getMulExpr(getUDivExpr(LHSC, C2), D);
C2->getAPInt().isPowerOf2() && C1V.uge(C2->getAPInt()) &&
C1V.logBase2() <= getMinTrailingZeros(D)) {
const SCEV *NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(NewMul);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Analysis/ScalarEvolution/mul-udiv-folds.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ define void @udiv4_and_udiv2(i1 %c, ptr %A) {
; CHECK-NEXT: %gep.16 = getelementptr i16, ptr %A, i64 %iv
; 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 }
; CHECK-NEXT: %gep.32 = getelementptr i32, ptr %A, i64 %iv
; 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 }
; 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 }
; CHECK-NEXT: %gep.40 = getelementptr <{ i32, i8 }>, ptr %A, i64 %iv
; 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 }
; CHECK-NEXT: %gep.48 = getelementptr <{ i32, i16 }>, ptr %A, i64 %iv
Expand Down