https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127375
Bug ID: 127375
Summary: Unnecessary RET instructions from multiple return-only
basic blocks
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: forkbombidable at gmail dot com
CC: jeffreyalaw at gmail dot com
Target Milestone: ---
Created attachment 65581
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65581&action=edit
Compiler configuration and invocation
The compiler sometimes generates unnecessary return instructions, like in the
following simple recursive factorial function compiled for RISC-V: the RET
labeled L12 can obviously be eliminated:
int fact(int n) {
if (n == 0 || n == 1) return 1;
return n * fact(n-1);
}
target riscv32-unknown-elf, -O3
fact:
mv a5,a0
li a0,1
mv a2,a0
bleu a5,a0,.L1
sub a4,a5,a0
and t0,a4,a0
mv a3,a0
beq t0,zero,.L2
mv a0,a5
addi a5,a5,-1
beq a5,a2,.L12
.L2:
mul a0,a0,a5
addi t1,a5,-1
addi a5,a5,-2
mul a0,a0,t1
bne a5,a3,.L2
.L1:
ret
.L12:
ret
This occurs in some of the test cases in gcc as well. For example, on x86-64
gcc/testsuite/gcc.c-torture/compile/20011114-4.c when compiled with -O3. The
attached file contains my invocation and output for this test case: the RET
labeled L13 can be eliminated by re-targeting to L14.