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

            Bug ID: 128887
           Summary: [16/17 Regression] wrong code at -O1 and above: DOM
                    treats the false edge of "x - y < 0" as "x - y >= 0"
                    when x - y may be NaN
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

The following program aborts at -O1, -O2, -O3, and -Os with GCC 16 and trunk;
GCC 13, 15, and clang are correct.

$cat repro.c

[[gnu::noipa]] double f (float a, long double b)
{
  return -b + 0 / a < 0 ? 1 : ({ long double d = b + b; d > 0 ? (unsigned) d :
0; });
}

int main (void)
{
  /* a = 0, b = 65504: 0 / 0.0f is NaN, so "-b + NaN < 0" is false; d = 131008
> 0, so f returns 131008.  */
  if (f (0.0f, 65504.0L) != 131008.0)
    __builtin_abort ();
  return 0;
}

$ gcc-trunk -O1 repro2.c && ./a.out
Aborted

$ gcc-trunk -O1 -fno-tree-dominator-opts t.c && ./a.out    # OK

Expected: with a = 0, 0 / a is 0.0 / 0.0 = NaN, so -b + NaN is NaN and NaN < 0
is false. The second arm is then evaluated with b = 65504: d = 131008 > 0, so f
must return 131008.

Actual: f returns 0. The optimized GIMPLE at -O1 has replaced the whole second
arm with the constant 0.


## Checked Versions
  - gcc 13, 15.2, clang: ok; 
  - gcc 16, trunk 9571dd0b721 (2026-11-17): wrong.


Reproduced in Godbolt: https://godbolt.org/z/s5xo9oasq

Thanks.

Reply via email to