gnu/gcc/ce6a2e9349065db52a7e25808aefc97ddaef9cba match.pd: fold (t * u) % u to zero when the product cannot overflow
A product t * u is an exact multiple of u, so t * u % u is zero under
every rounding convention whenever the multiplication does not wrap.
For signed types the overflow is undefined so this always holds; for
others use value ranges to prove the multiply is overflow-free, exactly
as the neighbouring (t * u) / u -> t already does. This is the
remainder counterpart of that fold, for all four modulo codes: the
C family only produces TRUNC_MOD_EXPR, but Fortran's MODULO and Ada's
mod produce the floor and ceiling forms, which fold equally.
Assisted-by: Claude Opus 4.8 (Anthropic)
gcc/ChangeLog:
* match.pd ((t * u) % u -> 0): New simplification.
gcc/testsuite/ChangeLog:
* gcc.dg/fold-mod-mult-1.c: New test.
Signed-off-by: Dominic P <gcc@gcc.dp11.uk>
2 files changed