<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/98148>98148</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Sub-optimal code generation for the spaceship (`<=>`) operator
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          loskutov
      </td>
    </tr>
</table>

<pre>
    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<=>`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VE2P4zYM_TXKhUjgyIk_Dj5kJg3Q8xYo9lTIFm2rK4uuRE925tcXsvPZBmsYUSyST4-PFFUIpnOIldi_if1xpSbuyVeWwo-J6WNVk_6s3skFo9ED9wgtWUtn4zoIZhgtAv5U8ypkIfZvDQ2jsegBf46WPHqxPwpZ9MxjEOlByJOQp450TZY35DshT19Cnvp_vn_XX02bC1nGNz2I5CiSg8iS5W3GcdkJ7KeG4Q9v5kPzt2UbAGAyjrPdXwyt8YFF-soUsCGnX9u4N_5uWqguWEAjG3IiPUCvnF6fvWFGBwMONfr12QSEmLnyJpCLUrTkATfdBgJ5Nq4T8h3I2U-gEb1i8iJ9BxPAIWrUMeUbn5rIProJWTTkwjVnITPwfRCyhGX7S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