https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109370
Bug ID: 109370 Summary: Missed optimization for std::optional branchless unwrapping Product: gcc Version: 12.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: andre.schackier at gmail dot com Target Milestone: --- Given the following source code godbolt: https://godbolt.org/z/vW6ebqafK #include <optional> int f(std::optional<int>&& o) { if (!o) return -1; return *o; } gcc with '-O3' generates: f(std::optional<int>&&): cmp BYTE PTR [rdi+4], 0 je .L3 mov eax, DWORD PTR [rdi] ret .L3: mov eax, -1 ret while clang generates: f(std::optional<int>&&): # @f(std::optional<int>&&) xor eax, eax cmp byte ptr [rdi + 4], 1 sbb eax, eax or eax, dword ptr [rdi] ret