https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123652
Bug ID: 123652
Summary: Unnecessary x64 code generation when returning
zero-initialized 7-byte types
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: bernardo at bernardosulzbach dot com
Target Milestone: ---
I observed GCC emitting quite a bit of unnecessary code when returning a
zero-initialized 7-byte type in my code. Here's a minimal reproduction using
std::array:
#include <array>
template <std::size_t n>
std::array<unsigned char, n> f() {
return {};
}
template std::array<unsigned char, 1> f<1>();
template std::array<unsigned char, 2> f<2>();
template std::array<unsigned char, 3> f<3>();
template std::array<unsigned char, 4> f<4>();
template std::array<unsigned char, 5> f<5>();
template std::array<unsigned char, 6> f<6>();
template std::array<unsigned char, 7> f<7>();
template std::array<unsigned char, 8> f<8>();
emits
mov DWORD PTR [rsp-7], 0
mov DWORD PTR [rsp-4], 0
movzx ecx, WORD PTR [rsp-3]
mov eax, DWORD PTR [rsp-7]
movzx edx, BYTE PTR [rsp-1]
sal rcx, 32
sal rdx, 48
or rax, rcx
or rax, rdx
ret
for, and only for, f<7>.
You can see it in Compiler Explorer at https://godbolt.org/z/zMdeovK89.
I tried to guess the right component, and I might have guessed wrong.