[llvm] [VPlan] Generalize vputils::getStrideExpr for known-sign (NFC) (PR #219152)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 03:20:46 PDT 2026


================
@@ -362,16 +362,23 @@ vputils::getStrideExpr(const VPValue *Ptr, PredicatedScalarEvolution &PSE,
   TypeSize AllocSz = DL.getTypeAllocSize(AccessTy);
   if (AllocSz.isScalable())
     return std::nullopt;
-  // TODO: ScalarEvolution doesn't have SRem/SDiv expressions yet, so we
-  // resort to matching APInt.
-  const APInt *StepC;
-  if (!match(Step, m_scev_APInt(StepC)) ||
-      StepC->sext(StrideTy->getIntegerBitWidth()).srem(AllocSz) != 0)
+  const SCEV *StepExpr = SE.getNoopOrSignExtend(Step, StrideTy);
+  const SCEV *AllocSzExpr = SE.getConstant(StrideTy, AllocSz);
+  // TODO: Could extend to add a known-multiple-of predicate, once we have SRem
+  // expressions.
+  if (!SE.getURemExpr(StepExpr, AllocSzExpr)->isZero())
     return std::nullopt;
-  return std::make_tuple(
-      Base,
-      SE.getConstant(StepC->sext(StrideTy->getIntegerBitWidth()).sdiv(AllocSz)),
-      NWFlags);
+  // TODO: ScalarEvolution doesn't have SDiv expressions yet, so we resort to
+  // isKnownNonNegative/isKnownNegative, knowing that AllocSz is an unsigned
+  // quantity.
+  const SCEV *UDivExpr = SE.getUDivExactExpr(StepExpr, AllocSzExpr);
+  if (SE.isKnownNonNegative(StepExpr))
+    return std::make_tuple(Base, UDivExpr, NWFlags);
+  if (SE.isKnownNegative(StepExpr))
+    // SDiv(Negative(StepExpr), NonNegative(AllocSzExpr)) =
+    // -UDiv(NonNegative(StepExpr), NonNegative(AllocSzExpr)).
+    return std::make_tuple(Base, SE.getNegativeSCEV(UDivExpr), NWFlags);
----------------
artagnon wrote:

Oops, this patch was supposed to be non-functional -- fixed now, thanks.

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


More information about the llvm-commits mailing list