From 8fe6ef1fa800157f19bfdf87045d906266bc618b Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 21 Dec 2023 01:53:14 +1000 Subject: [PATCH] CPU/NewRec: Allocate callee-saved registers before caller-saved --- src/core/cpu_newrec_compiler.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/core/cpu_newrec_compiler.cpp b/src/core/cpu_newrec_compiler.cpp index 69f4fe4d2..360beeca2 100644 --- a/src/core/cpu_newrec_compiler.cpp +++ b/src/core/cpu_newrec_compiler.cpp @@ -525,11 +525,20 @@ u32 CPU::NewRec::Compiler::GetFreeHostReg(u32 flags) { const u32 req_flags = HR_USABLE | (flags & HR_CALLEE_SAVED); + u32 fallback = NUM_HOST_REGS; for (u32 i = 0; i < NUM_HOST_REGS; i++) { if ((m_host_regs[i].flags & (req_flags | HR_NEEDED | HR_ALLOCATED)) == req_flags) - return i; + { + // Prefer callee-saved registers. + if (m_host_regs[i].flags & HR_CALLEE_SAVED) + return i; + else if (fallback == NUM_HOST_REGS) + fallback = i; + } } + if (fallback != NUM_HOST_REGS) + return fallback; // find register with lowest counter u32 lowest = NUM_HOST_REGS;