On 10/21/26 6:15 AM, Uttam Bhadauriya wrote:
 From fb6b4d83d08d8d30b31a450d6aa410d95d1b2e89 Mon Sep 17 00:00:00 2001
From: Uttam Singh Bhadauriya <[email protected]>
Date: Wed, 21 Oct 2026 17:45:13 +0530
Subject: [PATCH v3] match.pd: Recognize X == MIN ? MIN : X - 1 as signed sat add
  [PR127985]

Commit ff133b4cd22 (PR126627) simplified (X & -X) < 0 into X == INT_MIN
and (X & -X) >= 0 into X != INT_MIN.  However, the signed saturating
addition pattern for IMM = -1 in match-sat-alu.pd looked for the
unsimplified (X & -X) < 0 condition.  As a result, a saturating decrement
is no longer recognized as .SAT_ADD, which causes regressions in
gcc.target/riscv/sat/sat_s_add_imm-*.c.

Because the old (X & -X) < 0 pattern can no longer match outside
-ftrapv, this patch replaces it with match patterns for the canonical
X == MIN ? MIN : X - 1 and X != MIN ? X - 1 : MIN forms.  This restores
.SAT_ADD discovery on RISC-V and also captures saturating decrements
written directly with MIN comparisons.

PR tree-optimization/127985

gcc/ChangeLog:

* match-sat-alu.pd (signed_integer_sat_add): Replace obsolete
(X & -X) < 0 pattern with patterns for X == MIN ? MIN : X - 1
and X != MIN ? X - 1 : MIN.

gcc/testsuite/ChangeLog:

* gcc.dg/torture/pr127985.c: New test.
* gcc.target/riscv/sat/pr127985.c: New test.

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

I am presenting the v3 patch, I was able to build the cross-compiler
and test it locally( had some issues with my hardware apparently so
had to do some tweaking), the changes made are as follows:

1. Replaced the old obsolete pattern:
    Verified that (X & -X) < 0 cannot fire after commit ff133b4cd22 (PR126627)
    outside -ftrapv. The obsolete pattern has been completely replaced with
    canonical matchers for both:
      - X == MIN ? MIN : X - 1
      - X != MIN ? X - 1 : MIN
Essentially your pattern for 126627 will not fire with -fwrapv is active, so we're still rely on the pattern you removed in this patch to optimize that case, or am I misunderstanding something?  It seems the simple solution is to just keep the old pattern and add your new one.

5. Test Verification Summary:
    - x86_64: gcc.dg/torture/pr127985.c passes runtime execution across
      all optimization levels (-O0, -O1, -O2, -O3, -Os).
    - RISC-V (rv64gc-lp64d): Verified on cc1 that sat_s_add_imm-1-i16.c
      restores the regression (2 .SAT_ADD instances found).
    - RISC-V (rv64gc-lp64d): Verified pr127985.c finds all 8 .SAT_ADD
      instances and produces branchless assembly (0 branch labels).
Since we're changing a generic file a full bootstrap and regression test is required on one of the major targets (x86_64 and aarch64 are the most common).

Your patch fails to apply to the trunk.  I would hazard a guess there's a context difference because of other changes in this space.  Given this patch is meant to fix a regression, let's focus on getting this patch under control first.

If you can repost a version that works with the trunk it'd be hugely appreciated.  Sashiko ought to pick it up and I can do some regression testing on it.

Thanks!

jeff


Reply via email to