https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101353
Bug ID: 101353
Summary: [x86-64] missed optimization: missed tail call in
placement-new
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: arthur.j.odwyer at gmail dot com
Target Milestone: ---
// https://godbolt.org/z/9zP5dP3sE
#include <new>
struct T {
int x;
T(int) noexcept;
~T();
};
T factory(int) noexcept;
alignas(T) char buffer[sizeof(T)];
void placement_new() {
::new ((void*)buffer) T(42);
}
void placement_call() {
::new ((void*)buffer) T(factory(42));
}
g++ -O2 -std=c++20 test.cpp
Somehow the compiler is failing to generate an actual `jmp` tail call for
`placement_call` the way it's able to for `placement_new`.
_Z13placement_newv:
movl $42, %esi
movl $buffer, %edi
jmp _ZN1TC1Ei
_Z14placement_callv:
subq $8, %rsp
movl $42, %esi
movl $buffer, %edi
call _Z7factoryi
addq $8, %rsp
ret
Note that right now Clang has the same symptom; but ICC and MSVC both get this
right and generate tail-calls appropriately in both cases. So this isn't any
obscure C++ corner case AFAICT; seems it's truly just a missed optimization.
Clang's missed-optimization bug is filed as
https://bugs.llvm.org/show_bug.cgi?id=51000