https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122904
Bug ID: 122904
Summary: x86 missed optimization with (x < 0x300000000) in -Os
mode
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: Explorer09 at gmail dot com
Target Milestone: ---
The case is about comparison code between a variable and a compile time
constant.
The constant is greater than 2^32 but has lower bits being all zeros.
```c
#include <stdint.h>
#include <stdbool.h>
bool func1(uint64_t x) {
return x <= 0x300000000; // GCC can optimize this to (x <= (3UL<<32))
}
bool func2(uint64_t x) {
return x > 0x300000000; // GCC can optimize this to (x > (3UL<<32))
}
bool func3(uint64_t x) {
return x < 0x300000000; // GCC misses this
}
bool func4(uint64_t x) {
return x >= 0x300000000; // GCC also misses this
}
```
Clang optimizes the func3 and func4 cases to these:
```c
bool func3_clang(uint64_t x) {
return (x >> 32) < 0x3;
}
bool func4_clang(uint64_t x) {
return (x >> 32) >= 0x3;
}
```
(I would say this is related to bug 121136 but is not the same issue.
Specifically the constants in this report is not a power of two, but only the
lower bits being zero.)
(The constant that applies to my use case is 0x7ff0000000000000, the bit mask
that can be used to check floating point infinities and NaNs.)