]> git.ipfire.org Git - thirdparty/gcc.git/commit
match: Handle X != INT_MIN ? -X : INT_MIN [PR125050]
authorAvinal Kumar <avinal.xlvii@gmail.com>
Thu, 21 May 2026 10:24:12 +0000 (15:54 +0530)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 22 May 2026 04:48:00 +0000 (21:48 -0700)
commit33cd7bbb32c5eee4aff7aa06f351aa983be419bc
treec08f28e82685d6e6ef337d2239855ddcbb5b287c
parent8b45b008f07892bdb9765d7bbbf718c7bb78b8b1
match: Handle X != INT_MIN ? -X : INT_MIN [PR125050]

The pattern X != C1 ? -X : C2 currently bails out when C1 is
INT_MIN and the type doesn't wrap, because a signed negation
of INT_MIN is undefined behavior.  But the whole expression is
well-defined: it is equivalent to (signed)(-(unsigned)X).

Handle the wi::only_sign_bit_p case by emitting an unsigned
negate instead of giving up, mirroring what the abs pattern
already does for the same edge case.

PR tree-optimization/125050

gcc/ChangeLog:

* match.pd: (X != C1 ? -X : C2): Handle C1 being INT_MIN
by emitting (signed)(-(unsigned)X) instead of bailing out.

gcc/testsuite/ChangeLog:

* gcc.dg/fold-condneg-2.c: Update expected optimization.
* gcc.dg/pr125050.c: New test.
* gcc.dg/tree-ssa/phi-opt-50.c: New test.
* gcc.dg/tree-ssa/phi-opt-51.c: New test.

Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
gcc/match.pd
gcc/testsuite/gcc.dg/fold-condneg-2.c
gcc/testsuite/gcc.dg/pr125050.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-50.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/phi-opt-51.c [new file with mode: 0644]