https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121973
Bug ID: 121973
Summary: [trunk, RV64] Poor quality (useless, harmless) code is
added to naked functions
Product: gcc
Version: 16.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 code:
__attribute__ ((naked)) void
test1 ()
{
asm volatile ("ecall");
asm volatile ("ret");
}
__attribute__ ((naked)) long
test2 ()
{
asm volatile ("ecall"
:
:
: "memory", "a0", "a1", "a2", "a3", "a4", "a5", "a6",
"a7");
asm volatile ("ret");
}
typedef struct
{
long l0;
long l1;
} ret;
__attribute__ ((naked)) ret
test3 ()
{
asm volatile ("ecall"
:
:
: "memory", "a0", "a1", "a2", "a3", "a4", "a5", "a6",
"a7");
asm volatile ("ret");
}
generates this RV64 assembly:
test1:
ecall
ret
nop
test2:
ecall
ret
nop
mv a0,a5
test3:
ecall
ret
nop
mv a0,a4
mv a1,a5
Please note:
1. Useless nop's after the ret's
2. Useless mv's after the useless nop
Basically, naked functions should never add anything to whatever assembly code
there is in the body.
Functionally speaking, that extra code is harmless and never executed.
Machine code-wise speaking, the binary is larger than needed.
Clang-trunk does it right.
You can check here:
https://godbolt.org/z/PeTrjPWK8