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

            Bug ID: 125757
           Summary: `smax <smin <a, nonneg>, 0>` can be simplified into
                    `umin<a, nonneg>`
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
                CC: unassigned 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 (riscv64 -O1 -march=rv64gcb_zicond):
Trying 8, 9 -> 26:
    8: r141:DI=0x64
    9: r139:DI=smin(r152:DI,r141:DI)
      REG_DEAD r152:DI
      REG_DEAD r141:DI
   26: a0:DI=smax(r139:DI,0)
      REG_DEAD r139:DI
Failed to match this instruction:
(set (reg/i:DI 10 a0)
    (smax:DI (smin:DI (reg:DI 152 [ aD.2473 ])
            (const_int 100 [0x64]))
        (const_int 0 [0])))

But smax<r152 , 0> is the same as umin<r152 , INT_MAX> so
the overall expression can be simplified into:
umin <r152, 100>

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

This is the RTL version of PR 125755.

Reply via email to