https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112392
Bug ID: 112392
Summary: `a == nonnegative ? a : abs<a>` -> abs<a>
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: enhancement
Priority: P3
Component: tree-optimization
Assignee: pinskia at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Take:
```
int f(int a, unsigned char b)
{
int absb = b;
if (a == absb) return absb;
return a > 0 ? a : -a;
}
```
This should just be turned into
`return ABS_EXPR<a>;`
as we know that if a was a nonnegative value, then taking the ABS of that is
the same as a.
So something like:
```
(for cmp (eq gt ge)
(simplify
(cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
(if (INTEGRAL_TYPE_P (type))
@3))
```
`a > nonnegative ? a : abs<a>` is also just `abs<a>` too.
There are some more improvements dealing with type changing between signed and
unsigned but this is the first step.