From 76e3246065fd8a95227fc79ee200520ba3923aa7 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 22 Jul 2023 00:30:29 -0700 Subject: [PATCH] riscv: Reduce jit codesize a bit. --- Common/RiscVEmitter.cpp | 15 +++++++++------ Common/RiscVEmitter.h | 8 +++++++- Core/MIPS/RiscV/RiscVCompBranch.cpp | 21 +++------------------ Core/MIPS/RiscV/RiscVJit.cpp | 7 +++---- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Common/RiscVEmitter.cpp b/Common/RiscVEmitter.cpp index f557354fff..0029da5287 100644 --- a/Common/RiscVEmitter.cpp +++ b/Common/RiscVEmitter.cpp @@ -1180,13 +1180,16 @@ bool RiscVEmitter::CJInRange(const void *src, const void *dst) const { return BJInRange(src, dst, 12); } -void RiscVEmitter::QuickCallFunction(RiscVReg scratchreg, const u8 *func) { - if (!JInRange(GetCodePointer(), func)) { - // TODO: Might be able to optimize a tiny bit by taking advantage of simm12. - LI(scratchreg, (uintptr_t)func); - JALR(R_RA, scratchreg, 0); +void RiscVEmitter::QuickJAL(RiscVReg scratchreg, RiscVReg rd, const u8 *dst) { + if (!JInRange(GetCodePointer(), dst)) { + static_assert(sizeof(intptr_t) <= sizeof(int64_t)); + int64_t pcdelta = (int64_t)dst - (int64_t)GetCodePointer(); + int32_t lower = (int32_t)SignReduce64(pcdelta, 12); + uintptr_t upper = ((pcdelta - lower) >> 12) << 12; + LI(scratchreg, (uintptr_t)GetCodePointer() + upper); + JALR(rd, scratchreg, lower); } else { - JAL(R_RA, func); + JAL(rd, dst); } } diff --git a/Common/RiscVEmitter.h b/Common/RiscVEmitter.h index fd093d2509..439a313fe9 100644 --- a/Common/RiscVEmitter.h +++ b/Common/RiscVEmitter.h @@ -213,7 +213,13 @@ public: bool BInRange(const void *func) const; bool JInRange(const void *func) const; - void QuickCallFunction(RiscVReg scratchreg, const u8 *func); + void QuickJAL(RiscVReg scratchreg, RiscVReg rd, const u8 *dst); + void QuickJ(RiscVReg scratchreg, const u8 *dst) { + QuickJAL(scratchreg, R_ZERO, dst); + } + void QuickCallFunction(RiscVReg scratchreg, const u8 *func) { + QuickJAL(scratchreg, R_RA, func); + } template void QuickCallFunction(RiscVReg scratchreg, T *func) { static_assert(std::is_function::value, "QuickCallFunction without function"); diff --git a/Core/MIPS/RiscV/RiscVCompBranch.cpp b/Core/MIPS/RiscV/RiscVCompBranch.cpp index 0532ae389b..f2d481426d 100644 --- a/Core/MIPS/RiscV/RiscVCompBranch.cpp +++ b/Core/MIPS/RiscV/RiscVCompBranch.cpp @@ -42,34 +42,19 @@ void RiscVJit::CompIR_Exit(IRInst inst) { case IROp::ExitToConst: FlushAll(); LI(SCRATCH1, inst.constant); - if (JInRange(dispatcherPCInSCRATCH1_)) { - J(dispatcherPCInSCRATCH1_); - } else { - LI(SCRATCH2, dispatcherPCInSCRATCH1_); - JALR(R_ZERO, SCRATCH2, 0); - } + QuickJ(SCRATCH2, dispatcherPCInSCRATCH1_); break; case IROp::ExitToReg: exitReg = gpr.MapReg(inst.src1); FlushAll(); MV(SCRATCH1, exitReg); - if (JInRange(dispatcherPCInSCRATCH1_)) { - J(dispatcherPCInSCRATCH1_); - } else { - LI(SCRATCH2, dispatcherPCInSCRATCH1_); - JALR(R_ZERO, SCRATCH2, 0); - } + QuickJ(SCRATCH2, dispatcherPCInSCRATCH1_); break; case IROp::ExitToPC: FlushAll(); - if (JInRange(dispatcher_)) { - J(dispatcher_); - } else { - LI(SCRATCH2, dispatcher_); - JALR(R_ZERO, SCRATCH2, 0); - } + QuickJ(SCRATCH2, dispatcher_); break; default: diff --git a/Core/MIPS/RiscV/RiscVJit.cpp b/Core/MIPS/RiscV/RiscVJit.cpp index 0be5b5d59d..57d53242d7 100644 --- a/Core/MIPS/RiscV/RiscVJit.cpp +++ b/Core/MIPS/RiscV/RiscVJit.cpp @@ -35,6 +35,7 @@ RiscVJit::RiscVJit(MIPSState *mipsState) : IRJit(mipsState), gpr(mipsState, &jo) } AllocCodeSpace(1024 * 1024 * 16); + SetAutoCompress(true); // TODO: Consider replacing block num method form IRJit - this is 2MB. blockStartAddrs_ = new const u8 *[MAX_ALLOWED_JIT_BLOCKS]; @@ -91,8 +92,7 @@ bool RiscVJit::CompileBlock(u32 em_address, std::vector &instructions, u // Note: a properly constructed block should never get here. // TODO: Need to do more than just this? Call a func to set an exception? - LI(SCRATCH2, crashHandler_); - JALR(R_ZERO, SCRATCH2, 0); + QuickJ(SCRATCH2, crashHandler_); FlushIcache(); @@ -401,8 +401,7 @@ void RiscVJit::CompIR_Generic(IRInst inst) { BNE(X10, R_ZERO, dispatcherPCInSCRATCH1_); } else { FixupBranch skip = BEQ(X10, R_ZERO); - LI(SCRATCH2, dispatcherPCInSCRATCH1_); - JALR(R_ZERO, SCRATCH2, 0); + QuickJ(SCRATCH2, dispatcherPCInSCRATCH1_); SetJumpTarget(skip); } }