https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102355
Bug ID: 102355 Summary: excessive stack usage Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: vanyacpp at gmail dot com Target Milestone: --- void escape(unsigned long long& a); void foobar() { unsigned long long local; escape(local); } For the function "foobar" GCC allocates excessive stack space: foobar(): sub rsp, 24 lea rdi, [rsp+8] call escape(unsigned long long&) add rsp, 24 ret The function "foobar" only needs 8 bytes of stack space, but GCC allocates 24. Please note, that this excessive allocation isn't needed for stack alignment: 8 bytes of local variables are enough to keep the stack aligned. I also tested Clang and it allocates 8 bytes. GCC makes this stack layout: 8 bytes padding 8 bytes variable "local" 8 bytes padding 8 bytes return address I believe the problem is related to the fact that GCC aligns the stack twice: the first time after the return address placement and the second time after the local variables are placed. Playing with -mpreferred-stack-boundary confirms this: -mpreferred-stack-boundary | stack usage 3 8 4 (default) 24 5 56 6 120 https://godbolt.org/z/h56aoKvvh In all cases the stack usage is twice as much (minus 8 bytes for return address) as the required alignment. I believe stack space can be conserved by doing alignment only once.