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.
        * 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

Reply via email to