Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack-returned struct prevents tail-calls and moving operations to registers #61195

Open
dzaima opened this issue Mar 5, 2023 · 0 comments
Open

Comments

@dzaima
Copy link

dzaima commented Mar 5, 2023

typedef struct {
  long long a, b, c;
} S;

S ext1(void);
int ext2(int);

int f() {
  S v = ext1();
  v.a+= v.b;
  return ext2(v.a);
}
f:                                      # @f
        sub     rsp, 24
        mov     rdi, rsp
        call    ext1
        mov     rdi, qword ptr [rsp]
        add     rdi, qword ptr [rsp + 8]
        mov     qword ptr [rsp], rdi
        call    ext2
        add     rsp, 24
        ret

https://godbolt.org/z/zdr339Ke6 - gcc does a tail call, and doesn't store the result of the addition.

A related scenario is a stack-allocated value whose lifetime ends before the return, e.g.

typedef struct {
  long long a, b, c;
} S;

void ext1(S*);
int ext2(int);

int f(int a) {
  {
    S v;
    ext1(&v);
  }
  return ext2(a);
}

https://godbolt.org/z/fz58qWcos - gcc again tail-calls when clang doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants