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

--- Comment #6 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:57d1344c75e764d905c9e2c6501bc38d5a071552

commit r17-6633-g57d1344c75e764d905c9e2c6501bc38d5a071552
Author: Wesley Lewis <[email protected]>
Date:   Tue Nov 24 07:52:06 2026 -0700

    [PATCH v3] match: Fold ~X op -Y as `(Y-1) op X` [PR112628]

    Since ~(-Y) == Y - 1, the existing fold of ~X cmp ~Y into Y cmp X
    extends to ~X cmp -Y. Combined with existing patterns this turns
    unsigned ~a < -a into a != 0, signed ~a < -a into true and ~a == -a
    into false.

    For signed types the new Y + (-1) overflows only when -Y already does
    (Y == INT_MIN), so no new undefined behaviour is introduced.
    With -fwrapv both sides wrap identically.

    Bootstrapped tested on x86_64-pc-linux-gnu.

    ---
    Changes in v3:
    - Use :s on bit_not and negate instead of explicit single_use checks.

    Changes in v2:
    - Gate the fold on !TYPE_OVERFLOW_SANITIZED and !TYPE_OVERFLOW_TRAPS.
    - Add -ftrapv and ubsan testcases.

            PR tree-optimization/112628

    gcc/ChangeLog:

            * match.pd (~X cmp -Y): New pattern.

    gcc/testsuite/ChangeLog:

            * gcc.dg/torture/pr112628.c: New test.
            * gcc.dg/tree-ssa/pr112628-2.c: New test.
            * gcc.dg/tree-ssa/pr112628.c: New test.
            * gcc.dg/tree-ssa/pr112628-3.c: New test.
            * c-c++-common/ubsan/pr112628.c: New test.

    Signed-off-by: Wesley Lewis <[email protected]>

Reply via email to