Consider this code:
#include <array>
class C
{
std::array<char, 7> a{};
int b{};
};
C slow()
{
return {};
}
C fast()
{
C c;
return c;
}
GCC 6 through 9 produce very bloated code for slow():
slow():
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]
ret
fast():
xor eax, eax
xor edx, edx
ret
Is there a difference in meaning between the two functions? Clang emits code like fast() for both, while GCC 4-5 do a better job than 6-9, but not quite optimal either.
Build flags: -std=c++11 -O3
Demo: https://godbolt.org/z/rPNG9o
Submitted as a GCC bug based on the feedback here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90883