From 4b9011e475dbd5e55b9d05f86cbf95b37d45e0ac Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 6 Aug 2023 23:42:58 -0700 Subject: [PATCH] riscv: Reduce call bloat using temps. --- Common/RiscVEmitter.cpp | 5 ++++- Common/RiscVEmitter.h | 8 ++++---- Core/MIPS/RiscV/RiscVAsm.cpp | 6 +++--- Core/MIPS/RiscV/RiscVCompFPU.cpp | 2 +- Core/MIPS/RiscV/RiscVCompSystem.cpp | 10 +++++----- Core/MIPS/RiscV/RiscVJit.cpp | 8 ++++---- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Common/RiscVEmitter.cpp b/Common/RiscVEmitter.cpp index 5c986bdf81..71484f4656 100644 --- a/Common/RiscVEmitter.cpp +++ b/Common/RiscVEmitter.cpp @@ -1186,7 +1186,10 @@ void RiscVEmitter::QuickJAL(RiscVReg scratchreg, RiscVReg rd, const u8 *dst) { 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); + if (scratchreg != rd) + LI(scratchreg, (uintptr_t)GetCodePointer() + upper, rd); + else + LI(scratchreg, (uintptr_t)GetCodePointer() + upper); JALR(rd, scratchreg, lower); } else { JAL(rd, dst); diff --git a/Common/RiscVEmitter.h b/Common/RiscVEmitter.h index fedb0b0c7f..9fe5d87164 100644 --- a/Common/RiscVEmitter.h +++ b/Common/RiscVEmitter.h @@ -217,13 +217,13 @@ public: void QuickJ(RiscVReg scratchreg, const u8 *dst) { QuickJAL(scratchreg, R_ZERO, dst); } - void QuickCallFunction(const u8 *func) { - QuickJAL(R_RA, R_RA, func); + void QuickCallFunction(const u8 *func, RiscVReg scratchreg = R_RA) { + QuickJAL(scratchreg, R_RA, func); } template - void QuickCallFunction(T *func) { + void QuickCallFunction(T *func, RiscVReg scratchreg = R_RA) { static_assert(std::is_function::value, "QuickCallFunction without function"); - QuickCallFunction((const u8 *)func); + QuickCallFunction((const u8 *)func, scratchreg); } void LUI(RiscVReg rd, s32 simm32); diff --git a/Core/MIPS/RiscV/RiscVAsm.cpp b/Core/MIPS/RiscV/RiscVAsm.cpp index 0a057d1213..a5205d7b9b 100644 --- a/Core/MIPS/RiscV/RiscVAsm.cpp +++ b/Core/MIPS/RiscV/RiscVAsm.cpp @@ -131,7 +131,7 @@ void RiscVJitBackend::GenerateFixedCode(MIPSState *mipsState) { // Advance can change the downcount (or thread), so must save/restore around it. SaveStaticRegisters(); RestoreRoundingMode(true); - QuickCallFunction(&CoreTiming::Advance); + QuickCallFunction(&CoreTiming::Advance, X7); ApplyRoundingMode(true); LoadStaticRegisters(); @@ -158,7 +158,7 @@ void RiscVJitBackend::GenerateFixedCode(MIPSState *mipsState) { MV(X10, DOWNCOUNTREG); MV(X11, MEMBASEREG); MV(X12, JITBASEREG); - QuickCallFunction(&ShowPC); + QuickCallFunction(&ShowPC, X7); } LWU(SCRATCH1, CTXREG, offsetof(MIPSState, pc)); @@ -182,7 +182,7 @@ void RiscVJitBackend::GenerateFixedCode(MIPSState *mipsState) { // No block found, let's jit. We don't need to save static regs, they're all callee saved. RestoreRoundingMode(true); - QuickCallFunction(&MIPSComp::JitAt); + QuickCallFunction(&MIPSComp::JitAt, X7); ApplyRoundingMode(true); // Try again, the block index should be set now. diff --git a/Core/MIPS/RiscV/RiscVCompFPU.cpp b/Core/MIPS/RiscV/RiscVCompFPU.cpp index c1491266da..e1581f3688 100644 --- a/Core/MIPS/RiscV/RiscVCompFPU.cpp +++ b/Core/MIPS/RiscV/RiscVCompFPU.cpp @@ -624,7 +624,7 @@ void RiscVJitBackend::CompIR_FSpecial(IRInst inst) { int offset = offsetof(MIPSState, f) + inst.src1 * 4; FL(32, F10, CTXREG, offset); } - QuickCallFunction(func); + QuickCallFunction(func, SCRATCH1); fpr.MapReg(inst.dest, MIPSMap::NOINIT); // If it's already F10, we're done - MapReg doesn't actually overwrite the reg in that case. diff --git a/Core/MIPS/RiscV/RiscVCompSystem.cpp b/Core/MIPS/RiscV/RiscVCompSystem.cpp index ec519e66fd..c6c0070751 100644 --- a/Core/MIPS/RiscV/RiscVCompSystem.cpp +++ b/Core/MIPS/RiscV/RiscVCompSystem.cpp @@ -188,7 +188,7 @@ void RiscVJitBackend::CompIR_System(IRInst inst) { #ifdef USE_PROFILER // When profiling, we can't skip CallSyscall, since it times syscalls. LI(X10, (int32_t)inst.constant); - QuickCallFunction(&CallSyscall); + QuickCallFunction(&CallSyscall, SCRATCH2); #else // Skip the CallSyscall where possible. { @@ -196,10 +196,10 @@ void RiscVJitBackend::CompIR_System(IRInst inst) { void *quickFunc = GetQuickSyscallFunc(op); if (quickFunc) { LI(X10, (uintptr_t)GetSyscallFuncPointer(op)); - QuickCallFunction((const u8 *)quickFunc); + QuickCallFunction((const u8 *)quickFunc, SCRATCH2); } else { LI(X10, (int32_t)inst.constant); - QuickCallFunction(&CallSyscall); + QuickCallFunction(&CallSyscall, SCRATCH2); } } #endif @@ -211,7 +211,7 @@ void RiscVJitBackend::CompIR_System(IRInst inst) { case IROp::CallReplacement: FlushAll(); SaveStaticRegisters(); - QuickCallFunction(GetReplacementFunc(inst.constant)->replaceFunc); + QuickCallFunction(GetReplacementFunc(inst.constant)->replaceFunc, SCRATCH2); LoadStaticRegisters(); SUB(DOWNCOUNTREG, DOWNCOUNTREG, X10); break; @@ -222,7 +222,7 @@ void RiscVJitBackend::CompIR_System(IRInst inst) { RestoreRoundingMode(true); SaveStaticRegisters(); MovFromPC(X10); - QuickCallFunction(&Core_Break); + QuickCallFunction(&Core_Break, SCRATCH2); LoadStaticRegisters(); ApplyRoundingMode(true); MovFromPC(SCRATCH1); diff --git a/Core/MIPS/RiscV/RiscVJit.cpp b/Core/MIPS/RiscV/RiscVJit.cpp index 42d1c0783c..98fb87a0df 100644 --- a/Core/MIPS/RiscV/RiscVJit.cpp +++ b/Core/MIPS/RiscV/RiscVJit.cpp @@ -81,7 +81,7 @@ bool RiscVJitBackend::CompileBlock(IRBlock *block, int block_num, bool preload) // We should've written an exit above. If we didn't, bad things will happen. // Only check if debug stats are enabled - needlessly wastes jit space. if (DebugStatsEnabled()) { - QuickCallFunction(&NoBlockExits); + QuickCallFunction(&NoBlockExits, SCRATCH2); QuickJ(R_RA, hooks_.crashHandler); } @@ -98,7 +98,7 @@ void RiscVJitBackend::CompIR_Generic(IRInst inst) { FlushAll(); LI(X10, value, SCRATCH2); SaveStaticRegisters(); - QuickCallFunction(&DoIRInst); + QuickCallFunction(&DoIRInst, SCRATCH2); LoadStaticRegisters(); // We only need to check the return value if it's a potential exit. @@ -123,10 +123,10 @@ void RiscVJitBackend::CompIR_Interpret(IRInst inst) { SaveStaticRegisters(); if (DebugStatsEnabled()) { LI(X10, MIPSGetName(op)); - QuickCallFunction(&NotifyMIPSInterpret); + QuickCallFunction(&NotifyMIPSInterpret, SCRATCH2); } LI(X10, (int32_t)inst.constant); - QuickCallFunction((const u8 *)MIPSGetInterpretFunc(op)); + QuickCallFunction((const u8 *)MIPSGetInterpretFunc(op), SCRATCH2); LoadStaticRegisters(); }