Unnecessary spills when passing a struct argument by value
| Bugzilla Link | 46798 |
| Version | trunk |
| OS | All |
| Attachments | C test case to reproduce the problem |
| CC | @topperc,@dwblaikie,@efriedma-quic,@RKSimon,@rotateright |
Extended Description
Clang generates unnecessary spills of %xmm0 and %rax for the attached test case. See below (and attached test) for details.
The issue goes away if an attribute((align(8)) is added to the variable blah.
// Test case: // compile: clang -O3 -S -emit-llvm -o test.s test.c
typedef struct { int a; int b; int c; int d; int e; int f; } T; extern int foo(T a);
extern T blah;
int boo() { return foo(blah); }
Code generated by gcc:
subq $40, %rsp
movq blah+16(%rip), %rax
movdqu blah(%rip), %xmm0
movq %rax, 16(%rsp)
movups %xmm0, (%rsp)
call foo
addq $40, %rsp
ret
Code generated by "clang -O3"
subq $56, %rsp
movq blah+16(%rip), %rax
movq %rax, 48(%rsp) ; unnecessary
movups blah(%rip), %xmm0
movaps %xmm0, 32(%rsp) ; unnecessary
movq 48(%rsp), %rax ; unnecessary
movq %rax, 16(%rsp)
movaps 32(%rsp), %xmm0 ; unnecessary
movups %xmm0, (%rsp)
callq foo
addq $56, %rsp
retq
This is a longstanding issue with the LLVM IR representation of the "byval" attribute, although I'm not finding a duplicate bug with a quick search. Essentially, the "align" refers to both the alignment of the argument in the callee and the alignment of the pointer in the caller. If the alignment in the caller is insufficient, clang makes an extra temporary, and the optimizer can't eliminate it.