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

            Bug ID: 129217
           Summary: [17 Regression] wrong code at -O3: three-way double
                    comparison against a constant table containing a NaN,
                    after ifcombine forms `!(x <> y)`, since r17-2708
           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,

$cat repro.c

const double a[8] = { 3.5, 0.0, __builtin_nan (""), 1e10, 1e10, 0.0, 0.0, 0.0
};

__attribute__((noipa)) long
f (double b)
{
  long d = 0;
  for (int e = 0; e < 8; e++)
    {
      int s = b < a[e] ? -1 : b > a[e];
      if (s == 0)
        d = d * 3 + e;
    }
  return d;
}

int
main (void)
{
  if (f (0.0) != 205)
    __builtin_abort ();
  return 0;
}

$ gcc -O2 repro.c && ./a.out                          # ok
$ gcc -O3 repro.c && ./a.out                          # Aborted
$ gcc -O2 -funroll-loops repro.c && ./a.out           # Aborted
$ gcc -O3 -fno-tree-fre repro.c && ./a.out            # ok
$ gcc -O3 -fdisable-tree-ifcombine repro.c && ./a.out # ok
$ gcc -O3 -fno-trapping-math repro.c && ./a.out       # ok

For `b == 0.0`, `s == 0` (neither `b < a[e]` nor `b > a[e]`) holds for `a[e]`
equal to 0.0 and for the NaN, i.e. for `e = 1, 2, 5, 6, 7`, so `d = 205`. Trunk
at -O3 returns 556, i.e. it also counts `e = 3` (`a[3] = 1e10`): `0.0 < 1e10`,
so `s` must be -1 there. With the NaN replaced by an ordinary value, -O3 is
correct.

ifcombine now turns `!(b < a[e]) && !(b > a[e])` into `!(b <> a[e])`
("optimizing trapping cond to !(_1 <> b_9(D))"), which is the correct UNEQ
test. After complete unrolling the loop has `b <> NaN` followed by `b <>
1.0e+10` twice; FRE records
`b ltgt_expr NaN == false` on the edge after the NaN test (true for every `b`)
and the following `b <> 1.0e+10` test is then decided wrongly. `-fno-tree-fre`
or `-fdisable-tree-ifcombine` avoid it.

## Cause

**Bisected to r17-2708** (ea33efbbdcf, "ifcombine: Handle trapping ifcombining
better. [PR126138]", Andrea Pinski, 2026-07-11):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ea33efbbdcf69794b9962bf4926b07e73618d016

C-only builds of the two commits (`--enable-checking=release`), same testcase:

| Commit | repro.c -O2 | repro.c -O3 |
|---|---|---|
| 7d781db9c3d (parent of r17-2708) | ok | ok |
| **ea33efbbdcf (r17-2708) "ifcombine: Handle trapping ifcombining better.
[PR126138]"** | ok | **aborts** |

## Checked versions

- gcc trunk c10b819941b (2026-11-30): -O3 and -O2 -funroll-loops abort; -O0,
-O1, -O2, -Os ok.
- gcc 16.2, 15.2, 14.3 and 16.0.1, 15.2, 13.4: ok at -O3.
- UB: clean under ASan+UBSan (clang 21 and gcc 16, -O0); clang agrees with gcc
-O0.

Godbolt (gcc trunk, -O3 aborts, -O0 runs): https://godbolt.org/z/br97v1zMc

Thanks.

Reply via email to