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

            Bug ID: 121590
           Summary: Code should be unreachable after exhausting all four
                    float compare relations
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Explorer09 at gmail dot com
  Target Milestone: ---

With IEEE 754 floating point, there are only four possible relations for
comparing two floating point numbers. After all four possible relations are
exhausted in conditionals, GCC should recognize the remaining code is
unreachable.

Example code:

```c
#include <math.h>
#include <stdlib.h>

int float_compare(double a, double b) {
    if (isless(a, b))
        return 1;
    if (isgreater(a, b))
        return 3;
    if (isunordered(a, b))
        return 4;
    if (a == b)   
        return 2;
    // Should never reach here
    abort();
}
```

As long as optimization is enabled (-O1 or above), the call to abort() function
should be optimized out.

I can reproduce this issue in at least x86-64, ARM64 and RV64 (in Compiler
Explorer (godbolt.org)).

For example and reference, ARM64 GCC 15.1 with '-O2 -Wno-float-equal' option
produces:

```assembly
float_compare:
        fcmp    d0, d1
        bpl     .L9
        mov     w0, 1
        ret
.L9:
        ble     .L10
        mov     w0, 3
        ret
.L10:
        bvs     .L7
        mov     w0, 2
        bne     .L17
        ret
.L7:
        mov     w0, 4
        ret
.L17:
        stp     x29, x30, [sp, -16]!
        mov     x29, sp
        bl      abort
```

Note the unneeded abort() function call.

Reply via email to