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

            Bug ID: 121887
           Summary: Floating point signaling compare should merge with
                    quiet compare if both operands are non-NaN
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Explorer09 at gmail dot com
  Target Milestone: ---

This is a missed optimization.

Test code:

```c
#include <math.h>
int test_isgreater1(double x, double y) {
    if (!isunordered(x, y) && x > y)
        return 1;
    return 0;
}
int test_isgreater2(double x, double y) {
    if (isgreaterequal(x, x) && isgreaterequal(y, y) && x > y) {
        return 1;
    }
    return 0;
}
int test_isgreater3(double x, double y) {
    // (x == x) may produce a -Wfloat-equal warning
    if (x == x && y == y && x > y) {
        return 1;
    }
    return 0;
}
int test_isgreater4(double x, double y) {
    return isgreater(x, y);
}
```

The compiler should be able to tell that neither `x` and `y` can be NaN after
the "quiet"  comparisons (I use the "quiet" word here which is IEEE 754
terminology. In x86 this is also known as "unordered" comparison such as
UCOMISD instruction). Therefore there's no need to perform "signaling"
comparison on the (x > y) part (COMISD instruction in x86). It can reuse the
results of the "quiet" comparison just done earlier.

The ideal outcome is that test_isgreater1, 2 and 3 all optimize to 4.

Reply via email to