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

            Bug ID: 106744
           Summary: phiopt miscompiles min/max
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

GCC miscompiles the following test at -O1 or higher optimization levels:

#include <stdint.h>

__attribute__((noinline)) uint8_t
three_minmax1 (uint8_t xc, uint8_t xm, uint8_t xy) {
  uint8_t  xk;
  if (xc > xm) {
    xk = (uint8_t) (xc < xy ? xc : xy);
  } else {
    xk = (uint8_t) (xm < xy ? xm : xy);
  }
  return xk;
}

int
main (void)
{
  volatile uint8_t xy = 255;
  volatile uint8_t xm = 0;
  volatile uint8_t xc = 255;
  if (three_minmax1 (xc, xm, xy) != 255)
    __builtin_abort ();
  return 0;
}

What is happening is that phiopt transforms three_minmax1 to

  _7 = MAX_EXPR <xc_2(D), xy_4(D)>;
  _9 = MIN_EXPR <_7, xm_3(D)>;
  return _9;

instead of the intended

  _7 = MAX_EXPR <xc_2(D), xm_3(D)>;
  _9 = MIN_EXPR <_7, xy_4(D)>;
  return _9;

Reply via email to