On 10/15/26 4:10 AM, Alexey Lapshin wrote:
PR tree-optimization/101039 folded A CMP 0 ? A : -A after that form
was expanded into an if, by matching it in match.pd (r12-2041). The
true arm there is A and the false arm is -A.
The swapped form still does not fold for floating point. Integers
reach the existing pattern by inverting the comparison. A floating
comparison that may trap on NaNs cannot be inverted: the inverse of
< is UNGE, and UNGE does not raise invalid.
Fold A </<= 0 ? -A : A to ABS (A) and A >/>= 0 ? -A : A to -ABS (A)
when signed zeros need not be preserved. ABS does not raise invalid,
so the ordered compare is removed only when the operand cannot be a
NaN, or when the invalid exception is not observable. A conversion
from an integer cannot be a NaN. A NaN selects either arm, and both
arms are NaNs. Vectors are folded only when the target can do ABS
(and NEGATE for the -ABS form).
gcc:
* match.pd (A </<= 0 ? -A : A, A >/>= 0 ? -A : A): New patterns.
* testsuite/gcc.dg/tree-ssa/abs-5.c: New test.
* testsuite/gcc.dg/tree-ssa/abs-6.c: New test.
* testsuite/gcc.dg/tree-ssa/phi-opt-53.c: New test.
Thanks. This looks good to me.
Sashiko raised two pre-existing issues, neither of which I believe
should block this patch, but provide ideas for independent followups.
First there is a pattern just before where you insert yours that is
lacking trapping-math guards. A >=/> 0 ? A : -A and A <=/< 0 ? A : -A
Second, there's also a pattern for (A - B) >=/> 0 ? (A - B) : (B - A)
that appears to be missing similar guards. Closely related there's a
missing pattern for (A - B) </<= 0 ? (B - A) : (A - B) that you might
consider as a followup as well. I've pushed your patch to the trunk.
Thanks again! jeff