Issue |
98148
|
Summary |
Sub-optimal code generation for the spaceship (`<=>`) operator
|
Labels |
new issue
|
Assignees |
|
Reporter |
loskutov
|
Consider the following simple example ([compiler explorer](https://godbolt.org/z/hqYYdzcf7)):
```cpp
struct Triple {
uint64_t first;
uint64_t second;
uint64_t third;
// first option: hand-written member-wise comparison (for e.g. sorting, only operator< is needed)
bool operator<(const Triple& rhs) const {
if (first != rhs.first)
return first < rhs.first;
if (second != rhs.second)
return second < rhs.second;
return third < rhs.third;
}
// second option: compiler-generated spaceship operator
auto operator<=>(const WithSpaceship&) const = default;
};
```
Apparently, the generated code for the hand-written less-than comparison is better: the `setb` is only performed before the actual return, and the benchmarks show that it is indeed faster. I would expect the compiler-generated comparison to be at least as fast so that it doesn't discourage adopting `operator<=>`.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs