https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95383
Bug ID: 95383 Summary: Poor codegen when constructing a trivial Optional Product: gcc Version: 10.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: --- Here is a complete example: struct nullopt_t {} inline constexpr nullopt{}; template <typename T> struct Optional { struct Empty { }; union { Empty _; T value; }; bool engaged; Optional(nullopt_t) : _(), engaged(false) { } Optional(T v) : value(v), engaged(true) { } }; Optional<int> foo(bool b) { if (b) { return 42; } return nullopt; } Optional here is a valid implementation strategy for trivial types, like int. You can see some codegen examples here: https://godbolt.org/z/KwuWzB gcc 10.1 -O2 emits: foo(bool): test dil, dil mov eax, 0 movabs rdx, 4294967338 cmovne rax, rdx ret gcc 10.1 -O3 is worse: foo(bool): xor eax, eax mov ecx, 42 test dil, dil cmovne rdx, rcx mov ecx, 1 cmovne rax, rcx movabs rcx, -1095216660481 and rdx, rcx sal rax, 32 or rax, rdx ret gcc trunk (as of today) -O2 is the same as this bad -O3 version. clang 10, on the other hand, on -O2 or -O3, emits: foo(bool): # @foo(bool) shl rdi, 32 lea rax, [rdi + 42] ret which is much better. Using std::optional instead of this Optional (https://godbolt.org/z/By-fYx) for comparison, clang emits the same code as above. gcc 10 -O3 emits a branch: foo(bool): xor eax, eax test dil, dil je .L2 mov DWORD PTR [rsp-8], 42 mov eax, 1 .L2: mov BYTE PTR [rsp-4], al mov rax, QWORD PTR [rsp-8] ret