On Sun, Sep 13, 2026 at 1:52 PM Jeff Law <[email protected]> wrote: > > > > On 9/13/26 1:57 PM, Dominic P wrote: > > A - MIN (B, C) -> A + MAX (-B, -C) > > A - MAX (B, C) -> A + MIN (-B, -C) > > > > when B and C are negatable. TYPE_OVERFLOW_UNDEFINED is required as well: > > for a wrapping type negate_expr_p accepts X - Y, which can be the type > > minimum. This catches A - MIN (-x, 5), which min (-A, -B) -> -max (A, B) > > does not. > > > > Bootstrapped and regression tested on x86_64-pc-linux-gnu. > > > > Assisted-by: Claude Opus 5 (Anthropic) > > > > gcc/ChangeLog: > > > > * match.pd (A - MIN (B, C), A - MAX (B, C)): New patterns. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/torture/minmax-negate-1.c: New test. > > * gcc.dg/tree-ssa/minmax-negate-1.c: New test. > > * gcc.dg/tree-ssa/minmax-negate-2.c: New test. > > * gcc.dg/tree-ssa/minmax-negate-3.c: New test. > > > > Signed-off-by: Dominic P <[email protected]> > So my worry here is this violates the cost model we have for gimple. > The original form has two expression evaluations, the new form has 4. > So before going forward I'd really like to hear from Andrea & Richi.
In this case, it does not because the negate and the negate_expr_p cancel out; leaving no negate there. Examples of where this is used already: ``` (simplify (minus @0 negate_expr_p@1) (if (!FIXED_POINT_TYPE_P (type)) (plus @0 (negate @1)))) ``` You can see there we go from 1 to 2. But negate_expr_p matches only the following things: Constants (with some restrictions) (negate @0) (with some restrictions) (minus @0 @1) (with some restrictions) So the additional negate will always simplify (there is a pattern for `(negate (minus @0 @1))`) when used in connection of negate_expr_p . The one pattern missing here is: `(negate (min/max negate_expr_p@0 negate_expr_p@1))` For this canonicalization to be complete. That is we always push the negate into the min/max as far as possible which should reduce the number of negate that is needed. And reduce the number of patterns needed otherwise. This is the same reason why we canonicalization `-(a-b)` into `(b - a)`. > > jeff
