[llvm] [SCEVDivision] Prevent propagation of incorrect no-wrap flags (PR #154745)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 22 12:17:27 PDT 2025


================
@@ -141,10 +141,26 @@ void SCEVDivision::visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
   if (Ty != StartQ->getType() || Ty != StartR->getType() ||
       Ty != StepQ->getType() || Ty != StepR->getType())
     return cannotDivide(Numerator);
+
+  // Infer no-wrap flags for Remainder.
+  // TODO: Catch more cases.
+  SCEV::NoWrapFlags NumFlags = Numerator->getNoWrapFlags();
+  SCEV::NoWrapFlags RemFlags = SCEV::NoWrapFlags::FlagAnyWrap;
+  const SCEV *StepNumAbs =
+      SE.getAbsExpr(Numerator->getStepRecurrence(SE), /*IsNSW=*/false);
+  const SCEV *StepRAbs = SE.getAbsExpr(StepR, /*IsNSW=*/false);
+  const Loop *L = Numerator->getLoop();
+
+  // If abs(StepR) <=u abs(StepNumAbs) and both are loop invariant, propagate
----------------
kasuga-fj wrote:

This is currently used in Delinearization and does not correspond to actual div instructions in LLVM IR. That is, given a multiplication like `%m * %n`, an invocation like `divide(%m * %n, %m)` can happen without a non-zero check. Anyway, in such a case the division seems ill-defined.

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


More information about the llvm-commits mailing list