https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119679
Bug ID: 119679
Summary: [RISC-V] Compiler adds and removes stack to functions
even when not needed
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: vincenzo.romano at gmail dot com
Target Milestone: ---
This C code:
typedef struct {
unsigned long one;
unsigned long two;
} twin;
twin function( twin t ) {
return (twin){ 0,0 };
}
results in this assembly file:
.file "p.c"
.option nopic
.attribute arch,
"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.align 1
.globl function
.type function, @function
function:
addi sp,sp,-32
li a0,0
li a1,0
addi sp,sp,32
jr ra
.size function, .-function
.ident "GCC: (g04696df09) 14.2.0"
.section .note.GNU-stack,"",@progbits
when passed through "riscv64-unknown-elf-gcc (g04696df09) 14.2.0" with "-O",
"-O2", "-O3" and "-Os" optimization options.
The generated code just uses "a0" and "a1" registers and needs no local stack
("sp") inside the function body.
So those two instructions on "sp" register are pretty useless nd get generated
also with "-fomit-frame-pointer" option.
As a comparison, clang v20.1.0 works in an expected way.
You can check this also on GodBolt:
https://godbolt.org/z/11Gedzn4Y