Simplify X * C1 == C2 with undefined overflow

Joern Wolfgang Rennecke gnu@amylaar.uk
Fri Aug 7 13:05:41 GMT 2020


> this transformation is quite straightforward, without overflow, 3*X==15 is
> the same as X==5 and 3*X==5 cannot happen.

Actually, with binary and decimal computers, this transformation (with these specific numbers)
is also valid for wrapping overflow.  More generally, it is valid for wrapping overflow
if the right hand side of the comparison is divisible without rest by the constant factor,
and the constant factor has no sub-factor that is a zero divisor for the ring defined by the wrapping operation.
For binary computers, the latter condition can be more simply be restated as: The constant factor has to be odd.
(For decimal computers, it's: must not be divisible by two and/or five.)

(Even if the variable factor is wider than equality comparison, that is not a problem
  as long as the comparison is not widened by the transformation.)

On the other hand, the following generalizations would work only without overflow:
- handling of inequality-comparisons - merely have to account for the sign of the factor reversing the sense of
   the inequality, e.g. -3*X >= 15 ---> X <= 5
- If the right-hand-side constant is not a multiple of the constant factor, the product is always unequal, i.e.
   an EQ test would be always false, NE would be always true.



More information about the Gcc-patches mailing list