Unnecessary stores with std::optional?

Steffen Hirschmann steffen.hirschmann@ipvs.uni-stuttgart.de
Tue May 26 13:07:10 GMT 2020


Dear GCC community,

I was testing std::optional when I noticed that gcc produces stores that
don't seem to be required.

Code:
--------
#include <optional>
std::optional<long> foo();
long bar()
{
    auto r = foo();
    if (r)
        return *r;
    else
        return 0L;
}
--------

What gcc 10.1 with -std=c++17 -O3 produces is:
bar():
        sub     rsp, 24
        call    foo()
        mov     QWORD PTR [rsp+8], rdx
        cmp     BYTE PTR [rsp+8], 0
        mov     QWORD PTR [rsp], rax
        mov     rax, QWORD PTR [rsp]
        jne     .L1
        xor     eax, eax
.L1:
        add     rsp, 24
        ret

(see: https://godbolt.org/z/uHE6QB)

I don't understand the stores (and loads) after the call to foo. They
don't seem necessary to me. Can anyone explain them to me?

Greetings,
Steffen



More information about the Gcc-help mailing list