https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95783
Bug ID: 95783
Summary: Inefficient use of the stack when a function takes the
address of its argument
Product: gcc
Version: 10.1.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: josephcsible at gmail dot com
Target Milestone: ---
Target: x86_64-linux-gnu
Consider this C code:
void g(long *);
long f(long x) {
g(&x);
return x;
}
At either "-O3" or "-Os", it results in this assembly:
f:
subq $24, %rsp
movq %rdi, 8(%rsp)
leaq 8(%rsp), %rdi
call g
movq 8(%rsp), %rax
addq $24, %rsp
ret
There are two problems with this: it's unnecessarily complicated with extra
instructions, and it wastes 16 bytes of stack space. I'd rather see this
assembly instead:
f:
pushq %rdi
movq %rsp, %rdi
call g
popq %rax
ret
https://godbolt.org/z/PuNB6Y