https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126311

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jeff Law <[email protected]>:

https://gcc.gnu.org/g:85f255021606d2bf147c7083e8b49e8987d5c393

commit r17-6643-g85f255021606d2bf147c7083e8b49e8987d5c393
Author: Shivam Gupta <[email protected]>
Date:   Tue Nov 24 10:48:59 2026 -0700

    [PATCH v3] tree-optimization: Reduce (t * u) / v when gcd(u, v) > 1

    For an integer division (t * u) / v where the constants u and v share a
    common factor g = gcd (u, v) > 1, rewrite it as (t * (u / g)) / (v / g).

    This is done when t * u is known not to overflow: always for types with
    undefined overflow, otherwise only if the value range of t shows that the
    multiplication cannot wrap.

    The smaller constants avoid overflow and give cheaper
    division-by-constant sequences.

    Changes since v2:
    * Compute the gcd in widest_int; wi::gcd on the type-precision
      wide_int mishandles INT_MIN and unsigned constants with the top
      bit set, which could miscompile (new runtime test).
    * Require a single-use multiply (mult:s).

    Changes since v1:
    * Fix the scan counts in pr126311.c.
    * Add a bounded unsigned case in the same test file.

    Regression tested on x86_64-linux-gnu.

            PR tree-optimization/126311

    gcc/ChangeLog:

            * match.pd ((t * u) / v): Reduce integer constants u and v by
            their greatest common divisor if gcd(u, v) > 1.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/pr126311.c: New test.
            * gcc.dg/torture/pr126311.c: New test.

    Signed-off-by: Shivam Gupta <[email protected]>

Reply via email to