| Message ID | CAB92yRzWYDpDweGFMGheq3OP1iZZ4r0VgK9p9aKJW1w6AtHz-Q@mail.gmail.com |
|---|---|
| State | New |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9293A4BA2E1E for <patchwork@sourceware.org>; Sat, 10 Oct 2026 14:00:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9293A4BA2E1E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=WmlhMJv0 X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 73BBB4BA2E10 for <gcc-patches@gcc.gnu.org>; Sat, 10 Oct 2026 13:59:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73BBB4BA2E10 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 73BBB4BA2E10 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2a00:1450:4864:20::436 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1791640776; cv=pass; b=M/PTZFZ067/TgF5iKEo3Su1ahBROnVfiU2HD0YwbJIBNw4wC9DrCbkH2ur0uwjlGvf05a6INxxjNPRtFJ2IMcS+0WO0VU4M4yExkYBts1HZ2ixxLPZ1KgQ8BCGWImz+/W8//EYIdMTXIgub3PfXNcQ5TUmcdE5CrH16nYqdq8WI= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1791640776; c=relaxed/simple; bh=stEZg68G0TujrMbSRmjRx6IlFW8LBisqBueZra938Do=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=nVBYK2gB0qqZux7nQ2lwiDwi+a8nNtlwtz0KGUxuaydd0AAZu9z66RxZgZrCKeQ31irA0RxnvctOQ+NchFKPIdp3DtLVc7mNPckdY7psCTPhQjLbuUpD0/PIwv9Aq1D8lwj1SXg25jOALRQtcvcCn69zZq/ALU5YSM1QD0MRE/g= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=WmlhMJv0 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73BBB4BA2E10 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-48b060ec084so333802f8f.0 for <gcc-patches@gcc.gnu.org>; Sat, 10 Oct 2026 06:59:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1791640775; cv=none; d=google.com; s=arc-20260327; b=bJ/tccVOejrATOC2ufTcG/1RKI6Vw2S9SVplO0LltL9igBoM7PqbzlmkbEjl3qdCu5 4+03qOVzGN0CtRPmsLNT+uDN85iXVQkyT8+N4CFM1mVS1HXt2ISnSOZkx8pPigFptbxF GFejr0jgGVCqZQYzEx7I4xUN41WRErK1n9h9ZE4q7JK0TDXM5SBSKGnnxrn3FCzIfBFt hbJ9mzVTUi876HAGLI3VO2is58vViwtEBmBc26JzPu2F+I570bDOPUBPVBWktdbKCg4h JdK8uh3FS49zmgX6w3YqkJv9vqol/D/erJT1euQFKEJ3eYQtJ0n1RP9zjvY+cSNN99oT ubiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=to:subject:message-id:date:from:mime-version:dkim-signature; bh=L5doX9JV0gWHRKzOr2nIFb61875zTAFaY7moEdlntis=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=EyCsVYeidrDFiX0ISwOUyeV2yba3pSEKbdccMyc6MDyUMfkOKFQh3fLQsay5MfYTGP F2v0F/ISjw2zg2rS1+8hGReJJJtXuiasnD56EgDKc31BwA3WTB/D0xg8onpYn0mpStxL ic9KYhTwPcXuOfp7kSRKeCoecYWkDXBHAJ+H8L37495nCmlnzs3DNOEyCuEiLUBJEpfG k0U9c2kCRBSXX0K0CHzdhC7ysEP1E7Mp3j+PXEPuhSGsr9mj90CWQHru0xU7vpy9lfEP mnWmrCBqFQhxy1FWndyasPG6cnbhBem/UIC12cbO+7YaBJUtZRXIQllrAYDL9PnDSFva 89BA==; darn=gcc.gnu.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791640775; x=1792245575; darn=gcc.gnu.org; h=content-type:to:subject:message-id:date:from:mime-version:from:to :cc:subject:date:message-id:reply-to:content-type; bh=L5doX9JV0gWHRKzOr2nIFb61875zTAFaY7moEdlntis=; b=WmlhMJv051Tc7EY0UrRw2xZSR1vi6NYQ0uRaJH1xA1cUqZUYiCNPAJtzYzQ8c4ZMo4 g2MqbUrED6XQpq/45xXJA2j5qF0mFMagGm/XlCfBmZs+cI+B4Y6G8r0cP5rZokTGO/Fe 4wXGOqllLFiCMFnFunUtsSVGaTl+m6U558c3ljzN5mncpYR1oW9x0mb9W5hNWtZgpt5x DcSQEXlshqZgl/M5pusmogv9m1culb7PwMlnk8t/En46LR9dlPtsbr+3D1RzBPchBPgq g9PLqxbLEDMRcwPy2jpheQFsm9qxpcEzc/QvRxLZFJ+j0H1j9mZpGmrWHci9EIcqgalp ukoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791640775; x=1792245575; h=content-type:to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=L5doX9JV0gWHRKzOr2nIFb61875zTAFaY7moEdlntis=; b=q3Da6ZZVtgmKsvIsa0N9SMrUM2hwgR3J1Dm1pbqd5m3b9c53aUvYL8tLnD3kz5/Rfa kLuGx00ZcFPnnhdwT4sbH9le8zIw8M1fECYrVe14osXagKePTUNFame1OilDRaXzr5sh VH8MUQc/DQhhIblfgAglvhAsWXmuGlh//AHYev8I24vRQQ+xsFfL/gPChk4EQyn0nR19 jS5be/1RKprhReBV/44ZMAVxt/1v3jwpjpHQnLRdArN+YR7OG36BQlRFaRjollCt74AO s8PDOWcEQM48uEnsR93UT4Mwbk6a2k7k5fBYKLVX916MJEaoE2hQhu7BH6Ld03K5bYb1 u4gw== X-Gm-Message-State: AFq9FYIXJPcpAZo6SRH+vqYVlh9cMTcvbblsqK/TGPwEhXhO67lpxvOb p/v72HZp/jLgZLMLaQHVAw89mpUDCFQmP3YKiLgDy31WzUmHY7t4PiVjzv2hhIo7Q5XPF4jQnvJ 2TmmiJsv5AlpJRhalOmrErLCmSaGZzvE7jnid X-Gm-Gg: AYBFou3lIP5UGP7+RiNOjXOA+nFJ6s9MahnD+q/kPQmQcQfWPQrlI9KWSQ6QN3V5Y3d yFC3K6W3JQJmqI7vUJfRaa0bBDCqpYxeRJQsszYP0bycWEUoEkHhCQ7QuC7VAW2l1QiPc3VU/8k zyIEfcFwR+fO0cn+5vLWKJujPbn+/i9Jz9+GQXnSJvcW/GrTYTqQ1i9hgRNatfTDW2jCMh0iAt/ vIbN2em/JMPUZjKOa7Jbx78M3LeAMsiBopo2YBjRdBtDQEVx7ziLM4YCtz/oQ5xRSDTAFGPeatl Lcz/9hNVzV3q6DHO+bG2Ux5a6zKpQ52PH5mm/t7fC0QDn1VGW7A5FKNRWOfUKdexphXXVUDWDJZ i6ewSF0LMH5jkRGjV5tvO5A6vcW3KylpeNsKpbpyneQRO3Q== X-Received: by 2002:a5d:5f92:0:b0:48c:4c06:448a with SMTP id ffacd0b85a97d-48dba7b3326mr9178043f8f.3.1791640775051; Sat, 10 Oct 2026 06:59:35 -0700 (PDT) MIME-Version: 1.0 From: Uttam Bhadauriya <uttamsinghbhadoriya23@gmail.com> Date: Sat, 10 Oct 2026 19:29:20 +0530 X-Gm-Features: AclHuK92Gls2oEeGxF0Sy3nezP9_i7Z5HX9SWtsTZ4hcirQUpSWt-TMCkZitRQo Message-ID: <CAB92yRzWYDpDweGFMGheq3OP1iZZ4r0VgK9p9aKJW1w6AtHz-Q@mail.gmail.com> Subject: [PATCH] tree-optimization: Simplify (a & -a) < 0 to a == INT_MIN [PR126627]. To: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FORGED_GMAIL_RCVD, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
tree-optimization: Simplify (a & -a) < 0 to a == INT_MIN [PR126627].
|
|
Commit Message
Uttam Bhadauriya
Oct. 10, 2026, 1:59 p.m. UTC
Hello !
I am suggesting a patch for PR126627 / tree - optimization via this email.
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 ('a == INT_MIN' and 'a !=
INT_MIN').
It also explicitly guards the transformation with
'!TYPE_OVERFLOW_TRAPS' since '-a' could otherwise trap for 'INT_MIN',
which would incorrectly be removed if folded.
This patch has been locally built and regression tested on
'x86_64-pc-linux-gnu' ( As per the target shown in make file ).
OS : Windows 11, Version : 23H2, with WSL Ubuntu 22.04.05 LTS
Hopefully this is as per the expectations of the maintainers, this
would be my first contribution in the GCC toolchain so apologies for
any mistakes.
Kind regards
Uttam Bhadauriya
P.S.: Please check the patch details below.
From 31ff0cbfe3481c58956c6609bb09c10444978737 Mon Sep 17 00:00:00 2001
From: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com>
Date: Sat, 10 Oct 2026 18:57:38 +0530
Subject: [PATCH] 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 guards the transformation with !TYPE_OVERFLOW_TRAPS since -a could
otherwise trap for INT_MIN, which would incorrectly be removed.
gcc/ChangeLog:
PR tree-optimization/126627
* match.pd: Simplify (a & -a) < 0 to a == INT_MIN and
(a & -a) >= 0 to a != INT_MIN.
gcc/testsuite/ChangeLog:
PR tree-optimization/126627
* gcc.dg/tree-ssa/pr126627.c: New test.
Signed-off-by: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com>
---
gcc/match.pd | 18 +++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr126627.c | 25 ++++++++++++++++++++++++
2 files changed, 43 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126627.c
+/* { dg-final { scan-tree-dump-times " == 2147483648" 2 "optimized" {
target { ilp32 || lp64 } } } } */
Comments
On 10/10/26 7:59 AM, Uttam Bhadauriya wrote: > Hello ! > > I am suggesting a patch for PR126627 / tree - optimization via this email. > > 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 ('a == INT_MIN' and 'a != > INT_MIN'). > > It also explicitly guards the transformation with > '!TYPE_OVERFLOW_TRAPS' since '-a' could otherwise trap for 'INT_MIN', > which would incorrectly be removed if folded. > > This patch has been locally built and regression tested on > 'x86_64-pc-linux-gnu' ( As per the target shown in make file ). > > OS : Windows 11, Version : 23H2, with WSL Ubuntu 22.04.05 LTS > > Hopefully this is as per the expectations of the maintainers, this > would be my first contribution in the GCC toolchain so apologies for > any mistakes. > > Kind regards > Uttam Bhadauriya > P.S.: Please check the patch details below. > > From 31ff0cbfe3481c58956c6609bb09c10444978737 Mon Sep 17 00:00:00 2001 > From: Uttam Singh Bhadauriya<uttamsinghbhadoriya23@gmail.com> > Date: Sat, 10 Oct 2026 18:57:38 +0530 > Subject: [PATCH] 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 guards the transformation with !TYPE_OVERFLOW_TRAPS since -a could > otherwise trap for INT_MIN, which would incorrectly be removed. > > gcc/ChangeLog: > > PR tree-optimization/126627 > * match.pd: Simplify (a & -a) < 0 to a == INT_MIN and > (a & -a) >= 0 to a != INT_MIN. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/126627 > * gcc.dg/tree-ssa/pr126627.c: New test. > > Signed-off-by: Uttam Singh Bhadauriya<uttamsinghbhadoriya23@gmail.com> So have you verified this test produces the expected output? LLM review flagged the test as potentially incorrect and my own experimentation confirms that. I get this in the .optimized dump on riscv64-elf using -O2: jlaw@x11-dpi:~/test/obj/rv/gcc/gcc$ grep 214 j*.optimized _1 = a_2(D) == -2147483648; _1 = a_2(D) != -2147483648; _1 = a_2(D) == 2147483648; _1 = a_2(D) != 2147483648; That won't match your test scans: +/* { dg-final { scan-tree-dump-times " == -2147483648" 2 "optimized" { target { ilp32 || lp64 } } } } */ +/* { dg-final { scan-tree-dump-times " == 2147483648" 2 "optimized" { target { ilp32 || lp64 } } } } */ I'm also a bit unsure on your target selectors. Is { target int32 } sufficient? Your test doesn't do anything with other types such as longs or pointers. > --- > gcc/match.pd | 18 +++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr126627.c | 25 ++++++++++++++++++++++++ > 2 files changed, 43 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126627.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 0a4f7b718b4..5cc08f36e9b 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -9269,6 +9269,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) > (cmp @0 (convert @1))))) > > +/* (a & -a) < 0 -> a == INT_MIN > + (a & -a) >= 0 -> a != INT_MIN */ > +(for cmp (lt ge) > + eqcmp (eq ne) > + (simplify > + (cmp (convert?@0 (bit_and:c (negate @1) @1)) integer_zerop) > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && INTEGRAL_TYPE_P (TREE_TYPE (@1)) > + && TYPE_PRECISION (TREE_TYPE (@1)) > 1 What's the purpose of the precision test > 1? I *think* the transformation works for single bit boolean types. I think you're basically on the right track, but we definitely need a V2 and I'd like Andrea to have a chance to chime in as she's better with match.pd than I am. Jeff
On Sat, Oct 10, 2026 at 7:00 AM Uttam Bhadauriya <uttamsinghbhadoriya23@gmail.com> wrote: > > Hello ! > > I am suggesting a patch for PR126627 / tree - optimization via this email. > > 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 ('a == INT_MIN' and 'a != > INT_MIN'). > > It also explicitly guards the transformation with > '!TYPE_OVERFLOW_TRAPS' since '-a' could otherwise trap for 'INT_MIN', > which would incorrectly be removed if folded. > > This patch has been locally built and regression tested on > 'x86_64-pc-linux-gnu' ( As per the target shown in make file ). > > OS : Windows 11, Version : 23H2, with WSL Ubuntu 22.04.05 LTS > > Hopefully this is as per the expectations of the maintainers, this > would be my first contribution in the GCC toolchain so apologies for > any mistakes. > > Kind regards > Uttam Bhadauriya > P.S.: Please check the patch details below. > > From 31ff0cbfe3481c58956c6609bb09c10444978737 Mon Sep 17 00:00:00 2001 > From: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com> > Date: Sat, 10 Oct 2026 18:57:38 +0530 > Subject: [PATCH] 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 guards the transformation with !TYPE_OVERFLOW_TRAPS since -a could > otherwise trap for INT_MIN, which would incorrectly be removed. > > gcc/ChangeLog: > > PR tree-optimization/126627 > * match.pd: Simplify (a & -a) < 0 to a == INT_MIN and > (a & -a) >= 0 to a != INT_MIN. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/126627 > * gcc.dg/tree-ssa/pr126627.c: New test. > > Signed-off-by: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com> > --- > gcc/match.pd | 18 +++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr126627.c | 25 ++++++++++++++++++++++++ > 2 files changed, 43 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126627.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 0a4f7b718b4..5cc08f36e9b 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -9269,6 +9269,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) > (cmp @0 (convert @1))))) > > +/* (a & -a) < 0 -> a == INT_MIN > + (a & -a) >= 0 -> a != INT_MIN */ > +(for cmp (lt ge) > + eqcmp (eq ne) > + (simplify > + (cmp (convert?@0 (bit_and:c (negate @1) @1)) integer_zerop) > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && INTEGRAL_TYPE_P (TREE_TYPE (@1)) > + && TYPE_PRECISION (TREE_TYPE (@1)) > 1 > + && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) Instead of convert, use nop_convert and then you can remove the TYPE_PRECISION==TYPE_PRECISION check since that will be true there. > + && !TYPE_UNSIGNED (TREE_TYPE (@0)) > + && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@1))) > + (with { > + unsigned int prec = TYPE_PRECISION (TREE_TYPE (@1)); > + wide_int min = wi::set_bit_in_zero (prec - 1, prec); Instead of set_bit_in_zero use `wi::min_value (prec, SIGNED)`. > + } > + (eqcmp @1 { wide_int_to_tree (TREE_TYPE (@1), min); }))))) > + > /* Non-equality compare simplifications from fold_binary */ > (for cmp (lt gt le ge) > /* Comparisons with the highest or lowest possible integer of > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c > b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c > new file mode 100644 > index 00000000000..07c91bd7862 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c > @@ -0,0 +1,25 @@ > +/* PR tree-optimization/126627 */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +int test1(int a) { > + return (a & -a) < 0; > +} > + > +int test2(int a) { > + return (a & -a) >= 0; > +} > + > +int test3(unsigned int a) { > + return (int)(a & -a) < 0; > +} > + > +int test4(unsigned int a) { > + return (int)(a & -a) >= 0; > +} > + > +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " < 0" "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " >= 0" "optimized" } } */ > +/* { dg-final { scan-tree-dump-times " == -2147483648" 2 "optimized" > { target { ilp32 || lp64 } } } } */ > +/* { dg-final { scan-tree-dump-times " == 2147483648" 2 "optimized" { > target { ilp32 || lp64 } } } } */ Just use int32 as suggested by Jeff since that will also support LLP64 targets (aka windows). But you should also add a testcase for short and char. e.g.: ``` int test_us(unsigned short a) { return ((short)(a & -a)) >= 0; } int test_ss(signed short a) { return ((short)(a & -a)) >= 0; } ``` I am less worried about long though. You will notice with short, your match pattern does not match as there are few extra converts so that `-a` would not introduce undefined behavior (for short, it will promote to int) so you need to support that case too. Likewise this for int too: ``` int test_si(signed a) { unsigned b = a; b = -b; signed t = b; return (a & t) >= 0; } ``` The simplified way of doing this is something like: (match (negate_maybe_without_ud @0) (negate @0)) (match (negate_maybe_without_ud @0) (convert (negate (nop_convert @0))) (if (types_match (TREE_TYPE (@0, type))) And then use negate_maybe_without_ud instead of negate. This should simplify the pattern without adding too many optional converts. Thanks, Andrea > -- > 2.34.1
Thank you Andrea and Jeff for providing the feedback, I learned a lot while working on this issue, I'm sharing the updated patch with the following changes: - Replaced scan counts and regexes in the testcases with exact counts matching the simplified comparison constants. - Dropped precision > 1 requirement to, this was definitely not needed as Jeff pointed out. - Used `dg-require-effective-target int32` on both testcases. - Added `INTEGRAL_TYPE_P` guards to prevent matching pointer conversions of equal precision with `nop_convert`. - Introduced `negate_maybe_without_ud` to handle both direct signed negation and unsigned negation casts `(convert (negate (nop_convert @0)))`. - Guarded with `!TYPE_OVERFLOW_TRAPS` to avoid dropping traps under -ftrapv. - Fixed `(short)` cast in `pr126627-2.c` and changed `-fdump-tree-all` to `-fdump-tree-optimized`. Test results generated via DejaGnu show the following ( briefly ) : Running /home/utam1/gcc/gcc/testsuite/gcc.dg/tree-ssa/tree-ssa.exp ... PASS: gcc.dg/tree-ssa/pr126627-2.c (test for excess errors) PASS: gcc.dg/tree-ssa/pr126627-2.c scan-tree-dump-not optimized " & " PASS: gcc.dg/tree-ssa/pr126627-2.c scan-tree-dump-times optimized " == -32768" 1 PASS: gcc.dg/tree-ssa/pr126627-2.c scan-tree-dump-times optimized " != -32768" 1 PASS: gcc.dg/tree-ssa/pr126627-2.c scan-tree-dump-times optimized " == 32768" 1 PASS: gcc.dg/tree-ssa/pr126627-2.c scan-tree-dump-times optimized " != 32768" 1 PASS: gcc.dg/tree-ssa/pr126627.c (test for excess errors) PASS: gcc.dg/tree-ssa/pr126627.c scan-tree-dump-not optimized " & " PASS: gcc.dg/tree-ssa/pr126627.c scan-tree-dump-times optimized " == -2147483648" 1 PASS: gcc.dg/tree-ssa/pr126627.c scan-tree-dump-times optimized " != -2147483648" 2 PASS: gcc.dg/tree-ssa/pr126627.c scan-tree-dump-times optimized " == 2147483648" 1 PASS: gcc.dg/tree-ssa/pr126627.c scan-tree-dump-times optimized " != 2147483648" 1 === gcc Summary === # of expected passes 12 Regression Tests on tree-ssa testsuite : make -C build/gcc check-gcc RUNTESTFLAGS="tree-ssa.exp": Result: # of expected passes: 9,194 # of expected failures: 76 Please review the updated patch below, and let me know if further improvements are needed. Kind regards Uttam Bhadauriya ------------- Patch V2 ------------- From e75cd42ceff72c5cd247e2eb81a6f7901c728644 Mon Sep 17 00:00:00 2001 From: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com> Date: Sun, 11 Oct 2026 10:00:10 +0530 Subject: [PATCH] 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. gcc/ChangeLog: PR tree-optimization/126627 * 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: PR tree-optimization/126627 * gcc.dg/tree-ssa/pr126627.c: New test. * gcc.dg/tree-ssa/pr126627-2.c: New test. Signed-off-by: Uttam Singh Bhadauriya <uttamsinghbhadoriya23@gmail.com> --- gcc/match.pd | 20 ++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr126627-2.c | 15 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr126627.c | 24 ++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126627-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126627.c diff --git a/gcc/match.pd b/gcc/match.pd index 0a4f7b718b4..4bbda205fbf 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -9269,6 +9269,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) (cmp @0 (convert @1))))) +/* (a & -a) < 0 -> a == INT_MIN + (a & -a) >= 0 -> a != INT_MIN. */ +(match (negate_maybe_without_ud @0) + (negate @0)) +(match (negate_maybe_without_ud @0) + (convert (negate (nop_convert @0))) + (if (types_match (type, @0)))) + +(for cmp (lt ge) + eqcmp (eq ne) + (simplify + (cmp (nop_convert?@0 (bit_and:c (negate_maybe_without_ud @1) @1)) + integer_zerop) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@1))) + (eqcmp @1 { wide_int_to_tree (TREE_TYPE (@1), + wi::min_value (TYPE_PRECISION (TREE_TYPE (@1)), SIGNED)); })))) + /* Non-equality compare simplifications from fold_binary */ (for cmp (lt gt le ge) /* Comparisons with the highest or lowest possible integer of diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126627-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126627-2.c new file mode 100644 index 00000000000..ea68b8897be --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126627-2.c @@ -0,0 +1,15 @@ +/* PR tree-optimization/126627 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-require-effective-target int32 } */ + +int test_ss (short a) { return (short)(a & -a) < 0; } +int test_ss2 (short a) { return (short)(a & -a) >= 0; } +int test_us (unsigned short a) { return (short)(a & -a) < 0; } +int test_us2 (unsigned short a) { return (short)(a & -a) >= 0; } + +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == -32768" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " != -32768" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == 32768" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " != 32768" 1 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c new file mode 100644 index 00000000000..d11f5312baf --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c @@ -0,0 +1,24 @@ +/* PR tree-optimization/126627 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-require-effective-target int32 } */ + +int test1 (int a) { return (a & -a) < 0; } +int test2 (int a) { return (a & -a) >= 0; } +int test3 (unsigned a) { return (int)(a & -a) < 0; } +int test4 (unsigned a) { return (int)(a & -a) >= 0; } + +/* Negation done in unsigned to avoid UB. */ +int test_si (signed a) +{ + unsigned b = a; + b = -b; + signed t = b; + return (a & t) >= 0; +} + +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == -2147483648" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " != -2147483648" 2 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == 2147483648" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " != 2147483648" 1 "optimized" } } */
diff --git a/gcc/match.pd b/gcc/match.pd index 0a4f7b718b4..5cc08f36e9b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -9269,6 +9269,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) (cmp @0 (convert @1))))) +/* (a & -a) < 0 -> a == INT_MIN + (a & -a) >= 0 -> a != INT_MIN */ +(for cmp (lt ge) + eqcmp (eq ne) + (simplify + (cmp (convert?@0 (bit_and:c (negate @1) @1)) integer_zerop) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && TYPE_PRECISION (TREE_TYPE (@1)) > 1 + && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) + && !TYPE_UNSIGNED (TREE_TYPE (@0)) + && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@1))) + (with { + unsigned int prec = TYPE_PRECISION (TREE_TYPE (@1)); + wide_int min = wi::set_bit_in_zero (prec - 1, prec); + } + (eqcmp @1 { wide_int_to_tree (TREE_TYPE (@1), min); }))))) + /* Non-equality compare simplifications from fold_binary */ (for cmp (lt gt le ge) /* Comparisons with the highest or lowest possible integer of diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c new file mode 100644 index 00000000000..07c91bd7862 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126627.c @@ -0,0 +1,25 @@ +/* PR tree-optimization/126627 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int test1(int a) { + return (a & -a) < 0; +} + +int test2(int a) { + return (a & -a) >= 0; +} + +int test3(unsigned int a) { + return (int)(a & -a) < 0; +} + +int test4(unsigned int a) { + return (int)(a & -a) >= 0; +} + +/* { dg-final { scan-tree-dump-not " & " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " < 0" "optimized" } } */ +/* { dg-final { scan-tree-dump-not " >= 0" "optimized" } } */ +/* { dg-final { scan-tree-dump-times " == -2147483648" 2 "optimized" { target { ilp32 || lp64 } } } } */