llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Unnecessary spills when passing a struct argument by value

Open riyazvp opened this issue 6 years ago • 1 comments

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

riyazvp avatar Jul 21 '20 23:07 riyazvp

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.

efriedma-quic avatar Jul 21 '20 23:07 efriedma-quic