From e7be7b192d212751da488ae4fa6b334f6d649b69 Mon Sep 17 00:00:00 2001 From: Aleksi Hannula Date: Fri, 21 Nov 2025 09:32:14 +0200 Subject: [PATCH] Simplify RV64 function epilogue Previous sp can be computed based from only frame pointer and short offset. ra and s0 can be restored from stack with constant fp offset. --- riscv64-gen.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/riscv64-gen.c b/riscv64-gen.c index 61593fdf..715180ec 100644 --- a/riscv64-gen.c +++ b/riscv64-gen.c @@ -911,18 +911,14 @@ ST_FUNC void gfunc_epilog(void) loc = (loc - num_va_regs * 8); d = v = (-loc + 15) & -16; - if (v >= (1 << 11)) { - d = 16; - o(0x37 | (5 << 7) | UPPER(v-16)); //lui t0, upper(v) - EI(0x13, 0, 5, 5, SIGN11(v-16)); // addi t0, t0, lo(v) - ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0 - } - EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp) - EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp) - EI(0x13, 0, 2, 2, d); // addi sp, sp, v - EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret + EI(0x13, 0, 2, 8, num_va_regs * 8); // addi sp, s0, num_va_regs*8 + EI(0x03, 3, 1, 8, -8); // ld ra, -8(s0) + EI(0x03, 3, 8, 8, -16); // ld s0, -16(s0) + EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret + large_ofs_ind = ind; if (v >= (1 << 11)) { + d = 16; EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d o(0x37 | (5 << 7) | UPPER(v-16)); //lui t0, upper(v) EI(0x13, 0, 5, 5, SIGN11(v-16)); // addi t0, t0, lo(v)