On 8/12/2026 10:52 AM, Jeffrey Law wrote:


On 8/10/2026 10:32 AM, Daniel Barboza wrote:
This new forwprop  step is my attempt to implement Richi's suggestions from
v1 of this work [1] where he suggested to push things out of match.pd.

The idea is to simplify DIV/MOD into RSHIFT/BIT_AND ops in which the
divisor are pow2 integers in a PHI.  E.g.:

phi_var = PHI <16,4>
_x = _y % phi_var

Can be turned into:

phi_var = PHI <15,3>
_x = _y & phi_var

As long as we know that _y is a positive number or '_x' is used just in
zero equality comparisons.

Most of 101179 use cases are solved by this change.  PHI with 2+ args
are supported as long as every phi_arg meets the criteria.

Boostrapped and regression tested with x86_64, aarch64 and riscv64.

[1] https://gcc.gnu.org/pipermail/gcc-patches/2026-May/716303.html

    PR tree-optimization/101179

gcc/ChangeLog:

    * tree-ssa-forwprop.cc (simplify_phi_result_movdiv): New
    forwprop step where MOD/DIV ops with pow2 divisors can be
    simplified to BIT_AND/RSHIFT.
    (pass_forwprop::execute): Call simplify_phi_result_movdiv.

gcc/testsuite/ChangeLog:

    * gcc.dg/tree-ssa/pr101179.c: New test.
Can we really optimize the signed case when the result is used in a comparison 
against zero?  Consider something like:

y / (x ? 4 : 16) == 0

Where "y" is a small negative number, say -2.  In the original form that 
expression would be true.  In the right shifted form it would be false.

Did I miss something here?

Nope, you are correct.  For MOD we can do the bit_and transformation if the
dividend is positive or the MOD result is just used to compare with zero,
but for DIV this is only valid if the dividend is positive.  I remember having
code for this check in earlier versions but didn't find them in the ML ...

I'll send a new version with the DIV restriction in place.  Thanks,

Daniel


Jeff

Reply via email to