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

            Bug ID: 125755
           Summary: `max <min <a, nonneg>, 0>` can be simplified into
                    (signed)min<(unsigned)a, (unsigned)nonneg>
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int f(int a)
{
    int b = 0;
    int c = 100;
    int t = a >= b ? a : b;
    int t2 = t >= c ? c : t;
    return t2;
}
```
This produces right now:
  _2 = MIN_EXPR <a_3(D), 100>;
  _1 = MAX_EXPR <_2, 0>;

But MAX_EXPR <_2, 0> is the same as (signed)MIN<(unsigned)_2, INT_MAX> so
the overall expression can be simplified into:
(signed)MIN_EXPR <(unsigned)a_3, 100>

This is true for all non-negative c and not just constants. 0 works because
umin with 0 argument is always 0.

This is similar to PR 122617 but not exactly.

Reply via email to