https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101326
Bug ID: 101326 Summary: std::optional returns forced through stack Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: tnfchris at gcc dot gnu.org Target Milestone: --- The simple example #include <optional> std::optional<long> foo() { return 0; } shows suboptimal codegen at -O3 -std=c++17. The values seem to be forced through the stack. On AArch64 we get foo(): sub sp, sp, #16 mov w0, 1 strb w0, [sp, 8] mov x0, 0 ldr x1, [sp, 8] add sp, sp, 16 ret instead of (what clang gives) foo(): // @foo() mov w1, #1 mov x0, xzr ret On x86 it's the same: foo(): mov QWORD PTR [rsp-24], 0 mov rax, QWORD PTR [rsp-24] mov BYTE PTR [rsp-16], 1 mov rdx, QWORD PTR [rsp-16] ret vs foo(): // @foo() mov w1, #1 mov x0, xzr ret