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

--- Comment #1 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:ff133b4cd2202fc7a5be5cd9dc935c0a75fb6b68

commit r17-5264-gff133b4cd2202fc7a5be5cd9dc935c0a75fb6b68
Author: Uttam Bhadauriya <[email protected]>
Date:   Fri Oct 16 09:44:17 2026 -0600

    [PATCH v4] tree-optimization: Simplify (a & -a) < 0 to a == INT_MIN
[PR126627]

    Hello,
    apologies for the formatting and rebase issue, I am sending v4 of the
    patch to address the same.

    From 987b64d5230398390f926a91612ea5c0a5dcb781 Mon Sep 17 00:00:00 2001
    From: Uttam Singh Bhadauriya <[email protected]>
    Date: Fri, 16 Oct 2026 17:58:52 +0530
    Subject: [PATCH v4] tree-optimization: Simplify (a & -a) < 0 to a ==
INT_MIN
     [PR126627]

    The expression (a & -a) < 0 on a signed integer (or an unsigned integer
cast
    to a signed integer) is equivalent to checking if a is the minimum signed
    value for its type. The only value where the lowest set bit is the sign bit
    is INT_MIN. This patch introduces a simplification in match.pd to optimize
    this pattern into a direct equality check.

    It also handles the negate-in-unsigned form which avoids UB on negation,
    matching it using the new negate_maybe_without_ud predicate.

    It guards the transformation with !TYPE_OVERFLOW_TRAPS since -a could
    otherwise trap for INT_MIN, which would incorrectly be removed.

    ---
    v4:
    - Replaced scan counts and regexes in the testcases with exact counts.
    - Dropped precision > 1 requirement to also support 1-bit signed types.
    - Added '/* { dg-require-effective-target int32 } */' to both testcases.
    - Added INTEGRAL_TYPE_P guards to prevent matching pointer conversions.
    - Introduced negate_maybe_without_ud predicate to handle unsigned negation.
    - Guarded with !TYPE_OVERFLOW_TRAPS to prevent dropping traps under
-ftrapv.
    - Fixed (short) cast in pr126627-2.c and changed -fdump-tree-all to
    -fdump-tree-optimized.
    - Formatted comments according to GNU style guidelines.

    Bootstrapped and regression tested on x86_64-pc-linux-gnu, Windows 11
    Version 23H2 with WSL Ubuntu 22.04.05 LTS:
    - pr126627.c and pr126627-2.c: 12 passes in DejaGnu.
    - tree-ssa.exp: 9,194 passes, no regressions.
    - Verified behavior under -ftrapv (no folds) and -fwrapv (folded).

            PR tree-optimization/126627
    gcc/ChangeLog:

            * match.pd: Simplify (a & -a) < 0 to a == INT_MIN and
            (a & -a) >= 0 to a != INT_MIN, using the negate_maybe_without_ud
            predicate to support both signed and unsigned negation.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/pr126627.c: New test.
            * gcc.dg/tree-ssa/pr126627-2.c: New test.

    Signed-off-by: Uttam Singh Bhadauriya <[email protected]>

Reply via email to