https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126880

            Bug ID: 126880
           Summary: umax(a,b)==0 folding into a|b==0 gets in the way
                    sometimes
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
unsigned f(unsigned a, unsigned b)
{
    unsigned c = a > b ? a : b;
    if (c != 0)
      return c;
    return 0;
}
```

This should simplify to just umax(a,b).

But in phiopt1 we have:
```
  c_4 = MAX_EXPR <b_2(D), a_3(D)>;
  _6 = b_2(D) | a_3(D);
  if (_6 != 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  // predicted unlikely by early return (on trees) predictor.

  <bb 4> :
  # _1 = PHI <c_4(3), 0(2)>
```

This is not recongized as just c_4.

So basically we need a pattern for (a|b)!=0 ? MAX<b,a> : 0 for unsigned types
(well rather nonnegative for a & b).

As in this testcase requires the nonnegative part (since
r17-1938-g61b702d88c8e55):
```
signed f(signed a, signed b)
{
    a = __builtin_abs(a);
    b = __builtin_abs(b);
    signed c = a > b ? a : b;
    if (c)
      return c;
    return 0;
}
```

Reply via email to