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

            Bug ID: 128867
           Summary: [17 Regression] wrong code at -O1 with
                    __builtin_islessgreater on a NaN operand (tree DOM)
           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 when compiled with -O1 (passes at -O0, -O2, -O3,
-Os, and with clang):

$cat repro.c

[[gnu::noipa]] double f (double a, float b, double c, long long ib)
{
  double L = __builtin_isless ((double)__builtin_islessgreater ((double)b /
(double)b, -c), (double)__builtin_isgreater (0.0, (double)__builtin_isinf_sign
((double)b)));
  double R = (a + (6.0e-5 + c));
  return L < R ? (double)(float)ib : 5.0;
}

int main (void)
{
  /* b / b is NaN, so islessgreater (NaN, -inf) == 0; isinf_sign (-inf) == -1,
so isgreater (0.0, -1.0) == 1;
     L = isless (0.0, 1.0) = 1.0;  R = 0.0 + (6e-5 + inf) = inf;  L < R, so the
result is (double)(float)ib.  */
  if (f (0.0, -__builtin_inff (), __builtin_inf (), 9ll << 40) != 0x1.2p+43)
    __builtin_abort ();
  return 0;
}



$ gcc -O1 repro.c && ./a.out
Aborted


At -O1, f returns 5.0 instead of 0x1.2p+43. Expected value (no UB; IEEE
semantics):
b / b is NaN, so islessgreater (NaN, -inf) == 0; isinf_sign (-inf) == -1, so
isgreater (0.0, -1.0) == 1;
L = isless (0.0, 1.0) = 1.0; R = 0.0 + (6e-5 + inf) = +inf; L < R holds, so f
returns (double)(float)ib.

-O1 -fno-tree-dominator-opts gives the correct result, so the problem seems to
be in DOM, which was reworked in
October 2026 (e.g. 58294ebfb40 "DOM: replace simplify_binary_operation with the
ranger",
8f91f1288ad "DOM: stop rewriting comparisons into equalities"). Replacing
islessgreater (x, y) by !(x == y) hides it.

Affected Versions
- gcc 12, 13, 15.2, 16: correct
- trunk 9571dd0b721 (2026-11-17): wrong at -O1

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

Thanks.

Reply via email to