https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122303
Bug ID: 122303
Summary: suboptimal code for conditional tail call
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: rockeet at gmail dot com
Target Milestone: ---
```
struct C { int x; };
void bar();
C C_get_impl();
C C_get_cond(bool cond) {
if (__builtin_expect(cond, 1)) {
return C_get_impl();
}
C r = C_get_impl();
bar();
return r;
}
```
gcc(trunk) generates:
```
"C_get_cond(bool)":
test dil, dil
je .L2
jmp "C_get_impl()"
.L2:
sub rsp, 24
call "C_get_impl()"
mov DWORD PTR [rsp+12], eax
call "bar()"
mov eax, DWORD PTR [rsp+12]
add rsp, 24
ret
```
The optimal should be:
```
"C_get_cond(bool)":
test dil, dil
jne "C_get_impl()" ; better than je+jmp
push rbx
call "C_get_impl()"
mov rbx, rax
call "bar()"
mov rax, rbx
pop rbx
ret
```