https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90883
Bug ID: 90883
Summary: Generated code is worse if returned struct is unnamed
Product: gcc
Version: 9.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jzwinck at gmail dot com
Target Milestone: ---
Demo code:
#include <array>
class C
{
std::array<char, 7> a{};
int b{};
};
C slow()
{
return {};
}
C fast()
{
C c;
return c;
}
On x86-64 with -O2 or -O3, fast() generates good assembly:
xor eax, eax
xor edx, edx
But slow() does this:
xor eax, eax
mov DWORD PTR [rsp-25], 0
mov BYTE PTR [rsp-21], 0
mov edx, DWORD PTR [rsp-24]
mov DWORD PTR [rsp-32], 0
mov WORD PTR [rsp-28], ax
mov BYTE PTR [rsp-26], 0
mov rax, QWORD PTR [rsp-32]
GCC has had this problem since version 7. GCC 6 generated poor code for both
functions. GCC 4 and 5 generated different but not terrible code. Clang does
a good job in all versions.
StackOverflow discussion:
https://stackoverflow.com/questions/56578196/why-does-gcc-fail-to-optimize-unless-the-return-value-has-a-name/