On 8/18/26 1:54 PM, Dominic P wrote:
The absolute-difference idiom is already folded to abs (A - B) when the
comparison is written against the subtraction itself, e.g.
(A - B) > 0 ? (A - B) : (B - A).  The very common alternative spelling that
compares the operands directly, A > B ? A - B : B - A, was not recognised, so
phiopt kept two independent subtractions which the backend if-converted into a
compare plus two predicated subtracts (three instructions on Arm) instead of
the two-instruction "subs; rsb<cc>" abs sequence.

For a signed, non-wrapping integer type A > B is equivalent to A - B > 0, so the
idiom (and its >=, < and <= variants) folds to [-]abs (A - B).  Add the two
simplifications next to the existing (A - B) cmp 0 family, guarded on
!TYPE_OVERFLOW_WRAPS so that -fwrapv does not trigger the fold.

As a two-patch series with the following widened-operand extension,
bootstrapped on x86_64-pc-linux-gnu with the stage2/stage3 comparison
successful, and regtested there with gcc.dg/dg.exp and
gcc.dg/tree-ssa/tree-ssa.exp: no unexpected results.

Assisted-by: Claude Opus 5 (Anthropic)

        PR tree-optimization/50856

gcc/ChangeLog:

        * match.pd: Fold A > B ? A - B : B - A (and the >=, < and <=
        spellings) into [-]abs (A - B) for signed non-wrapping integer
        types.

gcc/testsuite/ChangeLog:

        * gcc.target/arm/pr50856.c: New test.
        * gcc.dg/tree-ssa/absdiff-1.c: New test.
Are these two patches still relevant?  I vaguely recall someone else poking around at the same problem, but with my travel over the last couple months I haven't been able to keep as close tabs on everything and may have missed something important.

jeff

Reply via email to