https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116211
Bug ID: 116211
Summary: C Functions returning a struct always manipulate the
stack pointer on RISC-V targets
Product: gcc
Version: 14.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: gr.audio at gmail dot com
Target Milestone: ---
Here's what I consider a "bug" for RISC-V targets (32 & 64) with GCC 14.1
(tested down to v8, so that's a long-standing one), even if it doesn't produce
incorrect code, it generates unnecessary code.
Any C function returning a struct, even when the stack is not used within said
function, generates instructions for reserving space on the stack, and
restoring sp afterwards. I don't see an ABI reason for doing so, and FYI, LLVM
doesn't generate such code.
Here is a minimal code that triggers this oddity;
[code]
#include <stdint.h>
typedef struct
{
double x;
uint64_t n;
} Test_t;
Test_t Foo(double x1, uint64_t n1)
{
return (Test_t){ .x = x1, .n = n1 };
}
[/code]
The generated assembly at -O1 to -O3 is:
[code]
Foo:
addi sp,sp,-32
addi sp,sp,32
jr ra
[/code]
Note that the struct needs to have at least 2 fields for this unncessary
manipulation of sp to happen.