Unnecessary stores with std::optional?

Marc Glisse marc.glisse@inria.fr
Thu May 28 15:00:18 GMT 2020


On Tue, 26 May 2020, Steffen Hirschmann wrote:

> 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?

One of them seems to be a tuning choice (it disappears with some 
-march/-mtune flags), but not the other. Could you file an issue in gcc's 
bugzilla please?

-- 
Marc Glisse


More information about the Gcc-help mailing list