https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116896
Bug ID: 116896
Summary: codegen for <=> compared to hand-written equivalent
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: barry.revzin at gmail dot com
Target Milestone: ---
Consider this example (https://godbolt.org/z/66sjx78ej):
#include <compare>
std::strong_ordering wat(int const& lhs, int const& rhs) {
#ifdef MANUAL
if (lhs < rhs) {
return std::strong_ordering::less;
} else if (lhs > rhs) {
return std::strong_ordering::greater;
} else {
return std::strong_ordering::equal;
}
#else
return lhs <=> rhs;
#endif
}
With -DMANUAL -O3 (i.e. manually writing out the three-way comparison), we get:
wat(int const&, int const&):
mov eax, DWORD PTR [rsi]
cmp DWORD PTR [rdi], eax
mov edx, -1
setg al
cmovl eax, edx
ret
But lhs <=> rhs, which means the same thing, gives us:
wat(int const&, int const&):
mov edx, DWORD PTR [rsi]
xor eax, eax
cmp DWORD PTR [rdi], edx
je .L2
setge al
lea eax, [rax-1+rax]
.L2:
ret
The explicit three-way comparison should generate at least as good code as the
implicit one.