Hi,
On 2026-09-13 23:51, Jeff Law wrote:
On 9/13/26 1:57 PM, Dominic P wrote:
The x & (x >> (PREC - 1)) -> MIN (x, 0) rules leave the branchless
min/max idiom
t = (a - b) & ((a - b) >> 31); a -= t; b += t;
half folded. Add
Y + MIN (X - Y, 0) -> MIN (X, Y) X - MIN (X - Y, 0) -> MAX (X, Y)
MIN (X + Y, Y) - Y -> MIN (X, 0)
and the MAX forms. Each needs the inner operation not to overflow, which
gimple_match_no_overflow_p takes from the type or, on GIMPLE, from ranges.
There is no :s because the idiom uses the masked value twice and both
uses fold.
x264's median-of-three predictor (common/base.h) goes from 20 to 11
instructions on x86_64 at -O2.
Bootstrapped and regression tested on x86_64-pc-linux-gnu.
Assisted-by: Claude Opus 5 (Anthropic)
gcc/ChangeLog:
* generic-match-head.cc (gimple_match_no_overflow_p): New.
* gimple-match-head.cc (gimple_match_no_overflow_p): New.
* match.pd (Y + MIN (X - Y, 0), X - MIN (X - Y, 0), Y + MAX (X - Y, 0),
X - MAX (X - Y, 0), MIN (X + Y, Y) - Y, MAX (X + Y, Y) - Y): New
patterns.
gcc/testsuite/ChangeLog:
* gcc.dg/torture/minmax-swar-1.c: New test.
* gcc.dg/tree-ssa/minmax-swar-1.c: New test.
* gcc.dg/tree-ssa/minmax-swar-2.c: New test.
* gcc.dg/tree-ssa/minmax-swar-3.c: New test.
* gcc.dg/tree-ssa/minmax-swar-4.c: New test.
* gcc.dg/tree-ssa/minmax-swar-5.c: New test.
This test fails for all targets that I test on arm-none-eabi except
-march=armv7ve+simd.
Is this test supposed to work for the others or is it missing a
dg-require-effective-target?
Testing tree-ssa/minmax-swar-5.c
doing compile
Executing on host: /build/r17-4210-g9d734bb468e714/bin/arm-none-eabi-gcc
/build/gcc_src/gcc/testsuite/gcc.dg/tree-ssa/minmax-swar-5.c -mthumb
-march=armv6s-m -mcpu=cortex-m0 -mfloat-abi=soft -mfpu=auto
-fdiagnostics-plain-output -O2 -Wno-psabi -fdump-tree-forwprop1-raw -S -o
minmax-swar-5.s (timeout = 800)
spawn -ignore SIGHUP /build/r17-4210-g9d734bb468e714/bin/arm-none-eabi-gcc
/build/gcc_src/gcc/testsuite/gcc.dg/tree-ssa/minmax-swar-5.c -mthumb
-march=armv6s-m -mcpu=cortex-m0 -mfloat-abi=soft -mfpu=auto
-fdiagnostics-plain-output -O2 -Wno-psabi -fdump-tree-forwprop1-raw -S -o
minmax-swar-5.s
pid is 176 -176
pid is -1
output is status 0
PASS: gcc.dg/tree-ssa/minmax-swar-5.c (test for excess errors)
FAIL: gcc.dg/tree-ssa/minmax-swar-5.c scan-tree-dump-not forwprop1 "<plus_expr,"
FAIL: gcc.dg/tree-ssa/minmax-swar-5.c scan-tree-dump-not forwprop1
"<minus_expr,"
gcc.dg/tree-ssa/minmax-swar-5.c: pattern found 0 times
FAIL: gcc.dg/tree-ssa/minmax-swar-5.c scan-tree-dump-times forwprop1
"<min_expr," 3
gcc.dg/tree-ssa/minmax-swar-5.c: pattern found 0 times
FAIL: gcc.dg/tree-ssa/minmax-swar-5.c scan-tree-dump-times forwprop1
"<max_expr," 3
Kind regards,
Torbjörn
* gcc.dg/tree-ssa/minmax-swar-6.c: New test.
* gcc.dg/tree-ssa/minmax-swar-7.c: New test.
Signed-off-by: Dominic P<[email protected]>
And I've pushed this to the trunk as well. Thanks again!
jeff