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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 21 06:20:13 PDT 2025


nikic wrote:

> In `SCEVDivision`, when the numerator is `SCEVAddRecExpr`, its no-wrap flags were propagated to the quotient and remainder. In general, it is incorrect. For example, consider dividing `{0,+,(%m + %n)}<nuw><nsw><%loop>` by `%m`. The quotient would be `{0,+,1}<%loop>` and the remainder would be `{0,+,%n}<%loop>`.

Was this example supposed to be `{%n,+,%m}<nuw><nsw><%loop>`?

----

I'm trying to understand why any of this code is correct at all. Is the invariant here that it gives you an expression of type `Q*Numerator + R`, but *without* the normal requirement that `R < Numerator`? Otherwise what this does for addrecs just doesn't make sense.

-----

For the nowrap flags, I think you can justify preserving `nuw` because division should make everything strictly smaller in an unsigned sense. So if it did not wrap with larger values, it should not wrap with smaller ones. The caveat here is that this is not correct for division by zero. We can't preserve flags for a possible division by zero.

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


More information about the llvm-commits mailing list