https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45115
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- This affects C++20 three-way comparisons, which return trivial structs wrapping an integer. >From PR 108635: #include <compare> struct S { std::weak_ordering operator<=>(const S&) const __attribute__((const)); }; int compare3way(S& a, S& b) { return (a < b) ? -1 : (a > b) ? 1 : 0; } I expect operator<=> to be called once, but it is called twice. This can be a major missed optimization if operator<=> is expensive. It happens regardless of: 1. Using attribute((const)) or attribute((pure)). 2. Making operator<=> a free function or a member. 3. Comparing (a > b) or (a < b) in the second ternary expression. This is especially strange, because it's really calling the same pure function twice, and that's optimized correctly when the function being called is operator< instead of operator<=>. Clang optimizes it as expected. Demo: https://godbolt.org/z/jP51E6xaz