[llvm] [SCEV] Fold (C1 * A /u C2) -> A /u (C2 /u C1), if C2 > C1. (PR #157656)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 10 11:05:27 PDT 2025


================
@@ -3226,10 +3227,15 @@ const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
       const SCEVConstant *C2;
       if (C1V.isPowerOf2() &&
           match(Ops[1], m_scev_UDiv(m_SCEV(D), m_SCEVConstant(C2))) &&
-          C2->getAPInt().isPowerOf2() && C1V.uge(C2->getAPInt()) &&
+          C2->getAPInt().isPowerOf2() &&
           C1V.logBase2() <= getMinTrailingZeros(D)) {
-        const SCEV *NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
-        return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(NewMul);
+        const SCEV *NewMul = nullptr;
+        if (C1V.uge(C2->getAPInt()))
+          NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
+        else if (C1V.ugt(1))
----------------
fhahn wrote:

Done. This required excluding C1 = -1 above, otherwise we would end up with `-1 * 1`, triggering the assert

https://github.com/llvm/llvm-project/pull/157656


More information about the llvm-commits mailing list