https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95405
Bug ID: 95405
Summary: Unnecessary stores with std::optional
Product: gcc
Version: 10.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: steffen.hirschmann at ipvs dot uni-stuttgart.de
Target Milestone: ---
I posted this to the gcc-help mailing list a few days ago
(https://gcc.gnu.org/pipermail/gcc-help/2020-May/138978.html).
GCC produces stores that don't seem to be required for std::optional.
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.
Marc Glisse pointed out
(https://gcc.gnu.org/pipermail/gcc-help/2020-May/138982.html) that the first
pair of store/load seems to be a tuning choice and can be removed with the
correct tuning flags.
What I expected is:
mov QWORD PTR [rsp+8], rdx
cmp BYTE PTR [rsp+8], 0
should be a compare/test directly of dl.
And:
mov QWORD PTR [rsp], rax
mov rax, QWORD PTR [rsp]
is not present at all.
Can someone explain this behavior? Shouldn't the optimizer produce what I
pointed out?