mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-12-06 12:43:35 +01:00
JitRegCache: Refactor using a data oriented design
Replace the "array of structure" design with a "structure of array". Boolean members are represented by a bitset. The main benefit is that filtering the registers in regcache operations can be done in one simple bitwise instruction. This is especially useful on `Flush`, as callers don't need anymore to limit the iterated registers for performance reasons. The change only applies to the x86 regcache.
This commit is contained in:
1 parent
a76221bc31
commit
b23f8d91b0
10 files changed
+419
-466
No files matched your search
@@ -631,7 +631,6 @@ if(_M_X86_64)
|
||||
PowerPC/Jit64/Jit_SystemRegisters.cpp
|
||||
PowerPC/Jit64/JitAsm.cpp
|
||||
PowerPC/Jit64/JitAsm.h
|
||||
PowerPC/Jit64/RegCache/CachedReg.h
|
||||
PowerPC/Jit64/RegCache/FPURegCache.cpp
|
||||
PowerPC/Jit64/RegCache/FPURegCache.h
|
||||
PowerPC/Jit64/RegCache/GPRRegCache.cpp
|
||||
|
||||
@@ -1256,12 +1256,10 @@ bool Jit64::DoJit(u32 em_address, JitBlock* b, u32 nextPC)
|
||||
gpr.Discard(op.gprDiscardable);
|
||||
fpr.Discard(op.fprDiscardable);
|
||||
}
|
||||
gpr.Flush(~(op.gprWillBeRead | op.gprWillBeWritten) & (op.regsIn | op.regsOut),
|
||||
RegCache::FlushMode::Full);
|
||||
fpr.Flush(~(op.fprWillBeRead | op.fprWillBeWritten) & (op.fregsIn | op.GetFregsOut()),
|
||||
RegCache::FlushMode::Full);
|
||||
gpr.Flush(~op.gprWillBeWritten & op.regsOut, RegCache::FlushMode::Undirty);
|
||||
fpr.Flush(~op.fprWillBeWritten & op.GetFregsOut(), RegCache::FlushMode::Undirty);
|
||||
gpr.Flush(~(op.gprWillBeRead | op.gprWillBeWritten), RegCache::FlushMode::Full);
|
||||
fpr.Flush(~(op.fprWillBeRead | op.fprWillBeWritten), RegCache::FlushMode::Full);
|
||||
gpr.Flush(~op.gprWillBeWritten, RegCache::FlushMode::Undirty);
|
||||
fpr.Flush(~op.fprWillBeWritten, RegCache::FlushMode::Undirty);
|
||||
|
||||
if (opinfo->flags & FL_LOADSTORE)
|
||||
++js.numLoadStoreInst;
|
||||
@@ -1335,8 +1333,8 @@ BitSet8 Jit64::ComputeStaticGQRs(const PPCAnalyst::CodeBlock& cb) const
|
||||
|
||||
BitSet32 Jit64::CallerSavedRegistersInUse(BitSet32 additional_registers) const
|
||||
{
|
||||
BitSet32 in_use = gpr.RegistersInUse().Cast<u32>() | (fpr.RegistersInUse().Cast<u32>() << 16) |
|
||||
additional_registers;
|
||||
BitSet32 in_use = gpr.HostRegistersInUse().Cast<u32>() |
|
||||
(fpr.HostRegistersInUse().Cast<u32>() << 16) | additional_registers;
|
||||
return in_use & ABI_ALL_CALLER_SAVED;
|
||||
}
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ void Jit64::ComputeRC(preg_t preg, bool needs_test, bool needs_sext)
|
||||
// If the output operand to the cmp/rc op we're merging with the branch isn't used anymore, it'd
|
||||
// be better to flush it here so that we don't have to flush it on both sides of the branch.
|
||||
// The flush is before the TEST so that it can macro-op fusion with the conditional branch.
|
||||
gpr.Flush(~js.op->gprWillBeWritten & js.op->regsOut, RegCache::FlushMode::Undirty);
|
||||
gpr.Flush(~js.op->gprWillBeWritten, RegCache::FlushMode::Undirty);
|
||||
if (needs_test)
|
||||
TEST(32, arg, arg);
|
||||
|
||||
@@ -1913,7 +1913,7 @@ void Jit64::rlwinmx(UGeckoInstruction inst)
|
||||
bool needs_sext = true;
|
||||
int mask_size = inst.ME - inst.MB + 1;
|
||||
|
||||
if (simple_mask && !(inst.SH & (mask_size - 1)) && !gpr.IsBound(s) && !gpr.IsImm(s))
|
||||
if (simple_mask && !(inst.SH & (mask_size - 1)) && !gpr.GetHostRegister(s) && !gpr.IsImm(s))
|
||||
{
|
||||
// optimized case: byte/word extract from m_ppc_state
|
||||
|
||||
|
||||
@@ -1,248 +0,0 @@
|
||||
// Copyright 2008 Dolphin Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/x64Reg.h"
|
||||
#include "Core/PowerPC/Jit64/RegCache/RCMode.h"
|
||||
|
||||
using preg_t = size_t;
|
||||
|
||||
class PPCCachedReg
|
||||
{
|
||||
public:
|
||||
PPCCachedReg() = default;
|
||||
|
||||
Gen::X64Reg GetHostRegister() const
|
||||
{
|
||||
ASSERT(m_in_host_register);
|
||||
return m_host_register;
|
||||
}
|
||||
|
||||
bool IsInPPCState() const { return m_in_ppc_state; }
|
||||
bool IsInHostRegister() const { return m_in_host_register; }
|
||||
|
||||
void SetFlushed(bool maintain_host_register)
|
||||
{
|
||||
ASSERT(!m_revertable);
|
||||
if (!maintain_host_register)
|
||||
m_in_host_register = false;
|
||||
m_in_ppc_state = true;
|
||||
}
|
||||
|
||||
void SetInHostRegister(Gen::X64Reg xreg, bool dirty)
|
||||
{
|
||||
if (dirty)
|
||||
m_in_ppc_state = false;
|
||||
m_in_host_register = true;
|
||||
m_host_register = xreg;
|
||||
}
|
||||
|
||||
void SetDirty() { m_in_ppc_state = false; }
|
||||
|
||||
void SetDiscarded()
|
||||
{
|
||||
ASSERT(!m_revertable);
|
||||
m_in_ppc_state = false;
|
||||
m_in_host_register = false;
|
||||
}
|
||||
|
||||
bool IsRevertable() const { return m_revertable; }
|
||||
void SetRevertable()
|
||||
{
|
||||
ASSERT(m_in_host_register);
|
||||
m_revertable = true;
|
||||
}
|
||||
void SetCommit()
|
||||
{
|
||||
ASSERT(m_revertable);
|
||||
m_revertable = false;
|
||||
}
|
||||
|
||||
bool IsLocked() const { return m_locked > 0; }
|
||||
void Lock() { m_locked++; }
|
||||
void Unlock()
|
||||
{
|
||||
ASSERT(IsLocked());
|
||||
m_locked--;
|
||||
}
|
||||
|
||||
private:
|
||||
Gen::X64Reg m_host_register{};
|
||||
bool m_in_ppc_state = true;
|
||||
bool m_in_host_register = false;
|
||||
bool m_revertable = false;
|
||||
size_t m_locked = 0;
|
||||
};
|
||||
|
||||
class X64CachedReg
|
||||
{
|
||||
public:
|
||||
X64CachedReg() = default;
|
||||
|
||||
preg_t Contents() const { return ppcReg; }
|
||||
|
||||
void SetBoundTo(preg_t ppcReg_)
|
||||
{
|
||||
free = false;
|
||||
ppcReg = ppcReg_;
|
||||
}
|
||||
|
||||
void Unbind()
|
||||
{
|
||||
ppcReg = static_cast<preg_t>(Gen::INVALID_REG);
|
||||
free = true;
|
||||
}
|
||||
|
||||
bool IsFree() const { return free && !locked; }
|
||||
|
||||
bool IsLocked() const { return locked > 0; }
|
||||
void Lock() { locked++; }
|
||||
void Unlock()
|
||||
{
|
||||
ASSERT(IsLocked());
|
||||
locked--;
|
||||
}
|
||||
|
||||
private:
|
||||
preg_t ppcReg = static_cast<preg_t>(Gen::INVALID_REG);
|
||||
bool free = true;
|
||||
size_t locked = 0;
|
||||
};
|
||||
|
||||
class RCConstraint
|
||||
{
|
||||
public:
|
||||
bool IsRealized() const { return realized != RealizedLoc::Invalid; }
|
||||
bool IsActive() const
|
||||
{
|
||||
return IsRealized() || write || read || kill_imm || kill_mem || revertable;
|
||||
}
|
||||
|
||||
bool ShouldLoad() const { return read; }
|
||||
bool ShouldDirty() const { return write; }
|
||||
bool ShouldBeRevertable() const { return revertable; }
|
||||
bool ShouldKillImmediate() const { return kill_imm; }
|
||||
bool ShouldKillMemory() const { return kill_mem; }
|
||||
|
||||
enum class RealizedLoc
|
||||
{
|
||||
Invalid,
|
||||
Bound,
|
||||
Imm,
|
||||
Mem,
|
||||
};
|
||||
|
||||
void Realized(RealizedLoc loc)
|
||||
{
|
||||
realized = loc;
|
||||
ASSERT(IsRealized());
|
||||
}
|
||||
|
||||
enum class ConstraintLoc
|
||||
{
|
||||
Bound,
|
||||
BoundOrImm,
|
||||
BoundOrMem,
|
||||
Any,
|
||||
};
|
||||
|
||||
void AddUse(RCMode mode) { AddConstraint(mode, ConstraintLoc::Any, false); }
|
||||
void AddUseNoImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrMem, false); }
|
||||
void AddBindOrImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrImm, false); }
|
||||
void AddBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, false); }
|
||||
void AddRevertableBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, true); }
|
||||
|
||||
private:
|
||||
void AddConstraint(RCMode mode, ConstraintLoc loc, bool should_revertable)
|
||||
{
|
||||
if (IsRealized())
|
||||
{
|
||||
ASSERT(IsCompatible(mode, loc, should_revertable));
|
||||
return;
|
||||
}
|
||||
|
||||
if (should_revertable)
|
||||
revertable = true;
|
||||
|
||||
switch (loc)
|
||||
{
|
||||
case ConstraintLoc::Bound:
|
||||
kill_imm = true;
|
||||
kill_mem = true;
|
||||
break;
|
||||
case ConstraintLoc::BoundOrImm:
|
||||
kill_mem = true;
|
||||
break;
|
||||
case ConstraintLoc::BoundOrMem:
|
||||
kill_imm = true;
|
||||
break;
|
||||
case ConstraintLoc::Any:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case RCMode::Read:
|
||||
read = true;
|
||||
break;
|
||||
case RCMode::Write:
|
||||
write = true;
|
||||
break;
|
||||
case RCMode::ReadWrite:
|
||||
read = true;
|
||||
write = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCompatible(RCMode mode, ConstraintLoc loc, bool should_revertable) const
|
||||
{
|
||||
if (should_revertable && !revertable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool is_loc_compatible = [&] {
|
||||
switch (loc)
|
||||
{
|
||||
case ConstraintLoc::Bound:
|
||||
return realized == RealizedLoc::Bound;
|
||||
case ConstraintLoc::BoundOrImm:
|
||||
return realized == RealizedLoc::Bound || realized == RealizedLoc::Imm;
|
||||
case ConstraintLoc::BoundOrMem:
|
||||
return realized == RealizedLoc::Bound || realized == RealizedLoc::Mem;
|
||||
case ConstraintLoc::Any:
|
||||
return true;
|
||||
}
|
||||
ASSERT(false);
|
||||
return false;
|
||||
}();
|
||||
|
||||
const bool is_mode_compatible = [&] {
|
||||
switch (mode)
|
||||
{
|
||||
case RCMode::Read:
|
||||
return read;
|
||||
case RCMode::Write:
|
||||
return write;
|
||||
case RCMode::ReadWrite:
|
||||
return read && write;
|
||||
}
|
||||
ASSERT(false);
|
||||
return false;
|
||||
}();
|
||||
|
||||
return is_loc_compatible && is_mode_compatible;
|
||||
}
|
||||
|
||||
RealizedLoc realized = RealizedLoc::Invalid;
|
||||
bool write = false;
|
||||
bool read = false;
|
||||
bool kill_imm = false;
|
||||
bool kill_mem = false;
|
||||
bool revertable = false;
|
||||
};
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "Core/PowerPC/Jit64/RegCache/FPURegCache.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "Common/x64Reg.h"
|
||||
#include "Core/PowerPC/Jit64/Jit.h"
|
||||
#include "Core/PowerPC/Jit64Common/Jit64PowerPCState.h"
|
||||
@@ -32,13 +34,13 @@ s32 FPURegCache::SImm32(preg_t preg) const
|
||||
|
||||
OpArg FPURegCache::R(preg_t preg) const
|
||||
{
|
||||
if (m_regs[preg].IsInHostRegister())
|
||||
if (auto xreg = GetHostRegister(preg))
|
||||
{
|
||||
return ::Gen::R(m_regs[preg].GetHostRegister());
|
||||
return ::Gen::R(xreg.value());
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, m_regs[preg].IsInPPCState(), "FPR {} missing!", preg);
|
||||
ASSERT_MSG(DYNA_REC, m_state.m_guests_in_ppc_state[preg], "FPR {} missing!", preg);
|
||||
return GetPPCStateLocation(preg);
|
||||
}
|
||||
}
|
||||
@@ -46,9 +48,9 @@ OpArg FPURegCache::R(preg_t preg) const
|
||||
void FPURegCache::StoreRegister(preg_t preg, const OpArg& new_loc,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers)
|
||||
{
|
||||
if (m_regs[preg].IsInHostRegister())
|
||||
if (auto xreg = GetHostRegister(preg))
|
||||
{
|
||||
m_emitter.MOVAPD(new_loc, m_regs[preg].GetHostRegister());
|
||||
m_emitter.MOVAPD(new_loc, xreg.value());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -59,7 +61,7 @@ void FPURegCache::StoreRegister(preg_t preg, const OpArg& new_loc,
|
||||
|
||||
void FPURegCache::LoadRegister(preg_t preg, X64Reg new_loc)
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, m_regs[preg].IsInPPCState(), "FPR {} not in PPCState", preg);
|
||||
ASSERT_MSG(DYNA_REC, m_state.m_guests_in_ppc_state[preg], "FPR {} not in PPCState", preg);
|
||||
m_emitter.MOVAPD(new_loc, GetPPCStateLocation(preg));
|
||||
}
|
||||
|
||||
@@ -68,11 +70,25 @@ void FPURegCache::DiscardImm(preg_t preg)
|
||||
// FPURegCache doesn't support immediates, so no need to do anything
|
||||
}
|
||||
|
||||
static constexpr std::array<X64Reg, 14> ALLOCATION_ORDER = {
|
||||
XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, XMM2, XMM3, XMM4, XMM5};
|
||||
|
||||
std::span<const X64Reg> FPURegCache::GetAllocationOrder() const
|
||||
{
|
||||
static constexpr X64Reg allocation_order[] = {XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12,
|
||||
XMM13, XMM14, XMM15, XMM2, XMM3, XMM4, XMM5};
|
||||
return allocation_order;
|
||||
return ALLOCATION_ORDER;
|
||||
}
|
||||
|
||||
BitSetHost FPURegCache::GetAllocatableRegisters() const
|
||||
{
|
||||
constexpr auto result = [&]() {
|
||||
BitSetHost hregs;
|
||||
for (auto r : ALLOCATION_ORDER)
|
||||
{
|
||||
hregs[r] = true;
|
||||
}
|
||||
return hregs;
|
||||
}();
|
||||
return result;
|
||||
}
|
||||
|
||||
OpArg FPURegCache::GetPPCStateLocation(preg_t preg) const
|
||||
@@ -80,18 +96,18 @@ OpArg FPURegCache::GetPPCStateLocation(preg_t preg) const
|
||||
return PPCSTATE_PS0(preg);
|
||||
}
|
||||
|
||||
BitSet32 FPURegCache::GetRegUtilization() const
|
||||
BitSetGuest FPURegCache::GetRegUtilization() const
|
||||
{
|
||||
return m_jit.js.op->fprInXmm;
|
||||
}
|
||||
|
||||
BitSet32 FPURegCache::CountRegsIn(preg_t preg, u32 lookahead) const
|
||||
BitSetGuest FPURegCache::CountRegsIn(preg_t preg, u32 lookahead) const
|
||||
{
|
||||
BitSet32 regs_used;
|
||||
BitSetGuest regs_used;
|
||||
|
||||
for (u32 i = 1; i < lookahead; i++)
|
||||
{
|
||||
BitSet32 regs_in = m_jit.js.op[i].fregsIn;
|
||||
BitSetGuest regs_in = m_jit.js.op[i].fregsIn;
|
||||
regs_used |= regs_in;
|
||||
if (regs_in[preg])
|
||||
return regs_used;
|
||||
|
||||
@@ -24,6 +24,7 @@ protected:
|
||||
void LoadRegister(preg_t preg, Gen::X64Reg newLoc) override;
|
||||
void DiscardImm(preg_t preg) override;
|
||||
std::span<const Gen::X64Reg> GetAllocationOrder() const override;
|
||||
BitSet32 GetRegUtilization() const override;
|
||||
BitSet32 CountRegsIn(preg_t preg, u32 lookahead) const override;
|
||||
BitSetHost GetAllocatableRegisters() const override;
|
||||
BitSetGuest GetRegUtilization() const override;
|
||||
BitSetGuest CountRegsIn(preg_t preg, u32 lookahead) const override;
|
||||
};
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "Core/PowerPC/Jit64/RegCache/GPRRegCache.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "Common/x64Reg.h"
|
||||
#include "Core/PowerPC/Jit64/Jit.h"
|
||||
#include "Core/PowerPC/Jit64Common/Jit64PowerPCState.h"
|
||||
@@ -32,9 +34,9 @@ s32 GPRRegCache::SImm32(preg_t preg) const
|
||||
|
||||
OpArg GPRRegCache::R(preg_t preg) const
|
||||
{
|
||||
if (m_regs[preg].IsInHostRegister())
|
||||
if (auto xreg = GetHostRegister(preg))
|
||||
{
|
||||
return ::Gen::R(m_regs[preg].GetHostRegister());
|
||||
return ::Gen::R(xreg.value());
|
||||
}
|
||||
else if (m_jit.GetConstantPropagation().HasGPR(preg))
|
||||
{
|
||||
@@ -42,7 +44,7 @@ OpArg GPRRegCache::R(preg_t preg) const
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, m_regs[preg].IsInPPCState(), "GPR {} missing!", preg);
|
||||
ASSERT_MSG(DYNA_REC, m_state.m_guests_in_ppc_state[preg], "GPR {} missing!", preg);
|
||||
return GetPPCStateLocation(preg);
|
||||
}
|
||||
}
|
||||
@@ -50,9 +52,9 @@ OpArg GPRRegCache::R(preg_t preg) const
|
||||
void GPRRegCache::StoreRegister(preg_t preg, const OpArg& new_loc,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers)
|
||||
{
|
||||
if (m_regs[preg].IsInHostRegister())
|
||||
if (auto xreg = GetHostRegister(preg))
|
||||
{
|
||||
m_emitter.MOV(32, new_loc, ::Gen::R(m_regs[preg].GetHostRegister()));
|
||||
m_emitter.MOV(32, new_loc, ::Gen::R(xreg.value()));
|
||||
}
|
||||
else if (m_jit.GetConstantPropagation().HasGPR(preg))
|
||||
{
|
||||
@@ -74,7 +76,7 @@ void GPRRegCache::LoadRegister(preg_t preg, X64Reg new_loc)
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, m_regs[preg].IsInPPCState(), "GPR {} not in PPCState", preg);
|
||||
ASSERT_MSG(DYNA_REC, m_state.m_guests_in_ppc_state[preg], "GPR {} not in PPCState", preg);
|
||||
m_emitter.MOV(32, ::Gen::R(new_loc), GetPPCStateLocation(preg));
|
||||
}
|
||||
}
|
||||
@@ -89,18 +91,32 @@ OpArg GPRRegCache::GetPPCStateLocation(preg_t preg) const
|
||||
return PPCSTATE_GPR(preg);
|
||||
}
|
||||
|
||||
static constexpr std::array<X64Reg, 11> ALLOCATION_ORDER = {
|
||||
#ifdef _WIN32
|
||||
RSI, RDI, R13, R14, R15, R8,
|
||||
R9, R10, R11, R12, RCX
|
||||
#else
|
||||
R12, R13, R14, R15, RSI, RDI,
|
||||
R8, R9, R10, R11, RCX
|
||||
#endif
|
||||
};
|
||||
|
||||
std::span<const X64Reg> GPRRegCache::GetAllocationOrder() const
|
||||
{
|
||||
static constexpr X64Reg allocation_order[] = {
|
||||
#ifdef _WIN32
|
||||
RSI, RDI, R13, R14, R15, R8,
|
||||
R9, R10, R11, R12, RCX
|
||||
#else
|
||||
R12, R13, R14, R15, RSI, RDI,
|
||||
R8, R9, R10, R11, RCX
|
||||
#endif
|
||||
};
|
||||
return allocation_order;
|
||||
return ALLOCATION_ORDER;
|
||||
}
|
||||
|
||||
BitSetHost GPRRegCache::GetAllocatableRegisters() const
|
||||
{
|
||||
constexpr auto result = [&]() {
|
||||
BitSetHost hregs;
|
||||
for (auto r : ALLOCATION_ORDER)
|
||||
{
|
||||
hregs[r] = true;
|
||||
}
|
||||
return hregs;
|
||||
}();
|
||||
return result;
|
||||
}
|
||||
|
||||
void GPRRegCache::SetImmediate32(preg_t preg, u32 imm_value, bool dirty)
|
||||
@@ -112,18 +128,18 @@ void GPRRegCache::SetImmediate32(preg_t preg, u32 imm_value, bool dirty)
|
||||
m_jit.GetConstantPropagation().SetGPR(preg, imm_value);
|
||||
}
|
||||
|
||||
BitSet32 GPRRegCache::GetRegUtilization() const
|
||||
BitSetGuest GPRRegCache::GetRegUtilization() const
|
||||
{
|
||||
return m_jit.js.op->gprWillBeRead | m_jit.js.op->gprWillBeWritten;
|
||||
}
|
||||
|
||||
BitSet32 GPRRegCache::CountRegsIn(preg_t preg, u32 lookahead) const
|
||||
BitSetGuest GPRRegCache::CountRegsIn(preg_t preg, u32 lookahead) const
|
||||
{
|
||||
BitSet32 regs_used;
|
||||
BitSetGuest regs_used;
|
||||
|
||||
for (u32 i = 1; i < lookahead; i++)
|
||||
{
|
||||
BitSet32 regs_in = m_jit.js.op[i].regsIn;
|
||||
BitSetGuest regs_in = m_jit.js.op[i].regsIn;
|
||||
regs_used |= regs_in;
|
||||
if (regs_in[preg])
|
||||
return regs_used;
|
||||
|
||||
@@ -26,6 +26,7 @@ protected:
|
||||
void LoadRegister(preg_t preg, Gen::X64Reg new_loc) override;
|
||||
void DiscardImm(preg_t preg) override;
|
||||
std::span<const Gen::X64Reg> GetAllocationOrder() const override;
|
||||
BitSetHost GetAllocatableRegisters() const override;
|
||||
BitSet32 GetRegUtilization() const override;
|
||||
BitSet32 CountRegsIn(preg_t preg, u32 lookahead) const override;
|
||||
};
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "Core/PowerPC/Jit64/RegCache/JitRegCache.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
@@ -14,8 +13,8 @@
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/VariantUtil.h"
|
||||
#include "Common/x64Emitter.h"
|
||||
#include "Common/x64Reg.h"
|
||||
#include "Core/PowerPC/Jit64/Jit.h"
|
||||
#include "Core/PowerPC/Jit64/RegCache/CachedReg.h"
|
||||
#include "Core/PowerPC/Jit64/RegCache/RCMode.h"
|
||||
|
||||
using namespace Gen;
|
||||
@@ -209,13 +208,12 @@ void RCX64Reg::Unlock()
|
||||
contents = std::monostate{};
|
||||
}
|
||||
|
||||
RCForkGuard::RCForkGuard(RegCache& rc_) : rc(&rc_), m_regs(rc_.m_regs), m_xregs(rc_.m_xregs)
|
||||
RCForkGuard::RCForkGuard(RegCache& rc_) : rc(&rc_), m_state(rc_.m_state)
|
||||
{
|
||||
ASSERT(!rc->IsAnyConstraintActive());
|
||||
}
|
||||
|
||||
RCForkGuard::RCForkGuard(RCForkGuard&& other) noexcept
|
||||
: rc(other.rc), m_regs(other.m_regs), m_xregs(other.m_xregs)
|
||||
RCForkGuard::RCForkGuard(RCForkGuard&& other) noexcept : rc(other.rc), m_state(other.m_state)
|
||||
{
|
||||
other.rc = nullptr;
|
||||
}
|
||||
@@ -226,8 +224,8 @@ void RCForkGuard::EndFork()
|
||||
return;
|
||||
|
||||
ASSERT(!rc->IsAnyConstraintActive());
|
||||
rc->m_regs = m_regs;
|
||||
rc->m_xregs = m_xregs;
|
||||
|
||||
rc->m_state = m_state;
|
||||
rc = nullptr;
|
||||
}
|
||||
|
||||
@@ -237,57 +235,55 @@ RegCache::RegCache(Jit64& jit, XEmitter& emitter) : m_jit{jit}, m_emitter(emitte
|
||||
|
||||
void RegCache::Start()
|
||||
{
|
||||
m_xregs.fill({});
|
||||
m_regs.fill({});
|
||||
m_constraints.fill({});
|
||||
m_state = {};
|
||||
}
|
||||
|
||||
bool RegCache::SanityCheck() const
|
||||
{
|
||||
for (size_t i = 0; i < m_regs.size(); i++)
|
||||
if (m_state.m_guests_in_host_register &
|
||||
(m_state.m_guests_is_locked | m_state.m_guests_revertable))
|
||||
{
|
||||
if (m_regs[i].IsInHostRegister())
|
||||
{
|
||||
if (m_regs[i].IsLocked() || m_regs[i].IsRevertable())
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
Gen::X64Reg xr = m_regs[i].GetHostRegister();
|
||||
if (m_xregs[xr].IsLocked())
|
||||
return false;
|
||||
if (m_xregs[xr].Contents() != i)
|
||||
return false;
|
||||
}
|
||||
for (const preg_t i : m_state.m_guests_in_host_register)
|
||||
{
|
||||
auto xr = m_state.m_guests_host_register[i];
|
||||
if (m_state.m_hosts_is_locked[xr])
|
||||
return false;
|
||||
if (m_state.m_hosts_guest_register[xr] != i)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RCOpArg RegCache::Use(preg_t preg, RCMode mode)
|
||||
{
|
||||
m_constraints[preg].AddUse(mode);
|
||||
m_guests_constraints[preg].AddUse(mode);
|
||||
return RCOpArg{this, preg};
|
||||
}
|
||||
|
||||
RCOpArg RegCache::UseNoImm(preg_t preg, RCMode mode)
|
||||
{
|
||||
m_constraints[preg].AddUseNoImm(mode);
|
||||
m_guests_constraints[preg].AddUseNoImm(mode);
|
||||
return RCOpArg{this, preg};
|
||||
}
|
||||
|
||||
RCOpArg RegCache::BindOrImm(preg_t preg, RCMode mode)
|
||||
{
|
||||
m_constraints[preg].AddBindOrImm(mode);
|
||||
m_guests_constraints[preg].AddBindOrImm(mode);
|
||||
return RCOpArg{this, preg};
|
||||
}
|
||||
|
||||
RCX64Reg RegCache::Bind(preg_t preg, RCMode mode)
|
||||
{
|
||||
m_constraints[preg].AddBind(mode);
|
||||
m_guests_constraints[preg].AddBind(mode);
|
||||
return RCX64Reg{this, preg};
|
||||
}
|
||||
|
||||
RCX64Reg RegCache::RevertableBind(preg_t preg, RCMode mode)
|
||||
{
|
||||
m_constraints[preg].AddRevertableBind(mode);
|
||||
m_guests_constraints[preg].AddRevertableBind(mode);
|
||||
return RCX64Reg{this, preg};
|
||||
}
|
||||
|
||||
@@ -307,156 +303,155 @@ RCForkGuard RegCache::Fork()
|
||||
return RCForkGuard{*this};
|
||||
}
|
||||
|
||||
void RegCache::Discard(BitSet32 pregs)
|
||||
void RegCache::Discard(BitSetGuest pregs)
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, std::ranges::none_of(m_xregs, &X64CachedReg::IsLocked),
|
||||
"Someone forgot to unlock a X64 reg");
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_hosts_is_locked, "Someone forgot to unlock a X64 reg");
|
||||
const BitSetGuest locked_pregs = pregs & m_state.m_guests_is_locked;
|
||||
ASSERT_MSG(DYNA_REC, !locked_pregs, "Someone forgot to unlock the following PPC regs {:b}.",
|
||||
locked_pregs.m_val);
|
||||
const BitSetGuest revertable_pregs = pregs & m_state.m_guests_revertable;
|
||||
ASSERT_MSG(DYNA_REC, !revertable_pregs,
|
||||
"Register transaction is in progress for the following PPC regs {:b}.",
|
||||
revertable_pregs.m_val);
|
||||
|
||||
for (preg_t i : pregs)
|
||||
for (const preg_t i : (pregs & m_state.m_guests_in_host_register))
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsLocked(), "Someone forgot to unlock PPC reg {} (X64 reg {}).",
|
||||
i, std::to_underlying(RX(i)));
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsRevertable(), "Register transaction is in progress for {}!",
|
||||
i);
|
||||
|
||||
DiscardRegister(i);
|
||||
const auto xr = m_state.m_guests_host_register[i];
|
||||
m_state.m_hosts_has_guest_register[xr] = false;
|
||||
}
|
||||
|
||||
m_state.m_guests_in_ppc_state &= ~pregs;
|
||||
m_state.m_guests_in_host_register &= ~pregs;
|
||||
}
|
||||
|
||||
void RegCache::Flush(BitSet32 pregs, FlushMode mode,
|
||||
void RegCache::Flush(BitSetGuest pregs, FlushMode mode,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers)
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, std::ranges::none_of(m_xregs, &X64CachedReg::IsLocked),
|
||||
"Someone forgot to unlock a X64 reg");
|
||||
const BitSetGuest revertable_pregs = pregs & m_state.m_guests_revertable;
|
||||
ASSERT_MSG(DYNA_REC, !revertable_pregs,
|
||||
"Register transaction is in progress for the following PPC regs {:b}.",
|
||||
revertable_pregs.m_val);
|
||||
|
||||
for (preg_t i : pregs)
|
||||
for (const preg_t i : (pregs & ~m_state.m_guests_in_ppc_state))
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, mode != FlushMode::Full || !m_regs[i].IsLocked(),
|
||||
"Someone forgot to unlock PPC reg {} (X64 reg {}).", i, std::to_underlying(RX(i)));
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsRevertable(), "Register transaction is in progress for {}!",
|
||||
i);
|
||||
|
||||
StoreFromRegister(i, mode, ignore_discarded_registers);
|
||||
StoreRegister(i, GetPPCStateLocation(i), ignore_discarded_registers);
|
||||
}
|
||||
|
||||
if (mode == FlushMode::Full)
|
||||
{
|
||||
const BitSetGuest locked_pregs = pregs & m_state.m_guests_is_locked;
|
||||
ASSERT_MSG(DYNA_REC, !locked_pregs, "Someone forgot to unlock the following PPC regs {:b}.",
|
||||
locked_pregs.m_val);
|
||||
|
||||
for (const preg_t i : (pregs & m_state.m_guests_in_host_register))
|
||||
{
|
||||
const auto xr = m_state.m_guests_host_register[i];
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_hosts_is_locked[xr],
|
||||
"Someone forgot to unlock X64 reg {} (PPC reg {}).", xr, i);
|
||||
m_state.m_hosts_has_guest_register[xr] = false;
|
||||
}
|
||||
|
||||
m_state.m_guests_in_host_register &= ~pregs;
|
||||
}
|
||||
|
||||
m_state.m_guests_in_ppc_state |= pregs;
|
||||
}
|
||||
|
||||
void RegCache::Reset(BitSet32 pregs)
|
||||
void RegCache::Reset(BitSetGuest pregs)
|
||||
{
|
||||
for (preg_t i : pregs)
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsInHostRegister(),
|
||||
"Attempted to reset a loaded register (did you mean to flush it?)");
|
||||
m_regs[i].SetFlushed(false);
|
||||
}
|
||||
const BitSetGuest in_host_register_pregs = pregs & m_state.m_guests_in_host_register;
|
||||
ASSERT_MSG(DYNA_REC, !in_host_register_pregs,
|
||||
"Attempted to reset the loaded registers {:b} (did you mean to flush them?)",
|
||||
in_host_register_pregs.m_val);
|
||||
|
||||
m_state.m_guests_in_ppc_state |= pregs;
|
||||
}
|
||||
|
||||
BitSet32 RegCache::RegistersRevertable() const
|
||||
BitSetGuest RegCache::RegistersRevertable() const
|
||||
{
|
||||
ASSERT(IsAllUnlocked());
|
||||
BitSet32 result;
|
||||
for (size_t i = 0; i < m_regs.size(); i++)
|
||||
{
|
||||
if (m_regs[i].IsRevertable())
|
||||
result[i] = true;
|
||||
}
|
||||
return result;
|
||||
return m_state.m_guests_revertable;
|
||||
}
|
||||
|
||||
void RegCache::Commit()
|
||||
{
|
||||
ASSERT(IsAllUnlocked());
|
||||
for (auto& reg : m_regs)
|
||||
{
|
||||
if (reg.IsRevertable())
|
||||
reg.SetCommit();
|
||||
}
|
||||
m_state.m_guests_revertable = {};
|
||||
}
|
||||
|
||||
bool RegCache::IsAllUnlocked() const
|
||||
{
|
||||
return std::ranges::none_of(m_regs, &PPCCachedReg::IsLocked) &&
|
||||
std::ranges::none_of(m_xregs, &X64CachedReg::IsLocked) && !IsAnyConstraintActive();
|
||||
return !m_state.m_hosts_is_locked && !m_state.m_guests_is_locked && !IsAnyConstraintActive();
|
||||
}
|
||||
|
||||
void RegCache::PreloadRegisters(BitSet32 to_preload)
|
||||
void RegCache::PreloadRegisters(BitSetGuest to_preload)
|
||||
{
|
||||
for (preg_t preg : to_preload)
|
||||
for (const preg_t preg : to_preload & ~m_state.m_guests_in_host_register)
|
||||
{
|
||||
if (NumFreeRegisters() < 2)
|
||||
if (GetFreeRegisters().Count() < 2)
|
||||
return;
|
||||
if (!IsImm(preg))
|
||||
BindToRegister(preg, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
BitSet16 RegCache::RegistersInUse() const
|
||||
BitSetHost RegCache::HostRegistersInUse() const
|
||||
{
|
||||
BitSet16 result;
|
||||
for (size_t i = 0; i < m_xregs.size(); i++)
|
||||
{
|
||||
if (!m_xregs[i].IsFree())
|
||||
result[i] = true;
|
||||
}
|
||||
return result;
|
||||
return m_state.m_hosts_has_guest_register | m_state.m_hosts_is_locked;
|
||||
}
|
||||
|
||||
void RegCache::FlushX(X64Reg reg)
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, reg < m_xregs.size(), "Flushing non-existent reg {}",
|
||||
std::to_underlying(reg));
|
||||
ASSERT(!m_xregs[reg].IsLocked());
|
||||
if (!m_xregs[reg].IsFree())
|
||||
ASSERT(!m_state.m_hosts_is_locked[reg]);
|
||||
if (m_state.m_hosts_has_guest_register[reg])
|
||||
{
|
||||
StoreFromRegister(m_xregs[reg].Contents());
|
||||
StoreFromRegister(m_state.m_hosts_guest_register[reg]);
|
||||
}
|
||||
}
|
||||
|
||||
void RegCache::DiscardRegister(preg_t preg)
|
||||
{
|
||||
if (m_regs[preg].IsInHostRegister())
|
||||
if (m_state.m_guests_in_host_register[preg])
|
||||
{
|
||||
X64Reg xr = m_regs[preg].GetHostRegister();
|
||||
m_xregs[xr].Unbind();
|
||||
const auto xr = m_state.m_guests_host_register[preg];
|
||||
m_state.m_hosts_has_guest_register[xr] = false;
|
||||
}
|
||||
|
||||
m_regs[preg].SetDiscarded();
|
||||
m_state.m_guests_in_ppc_state[preg] = false;
|
||||
m_state.m_guests_in_host_register[preg] = false;
|
||||
}
|
||||
|
||||
void RegCache::BindToRegister(preg_t i, bool doLoad, bool makeDirty)
|
||||
{
|
||||
if (!m_regs[i].IsInHostRegister())
|
||||
if (!m_state.m_guests_in_host_register[i])
|
||||
{
|
||||
X64Reg xr = GetFreeXReg();
|
||||
const X64Reg xr = GetFreeXReg();
|
||||
|
||||
ASSERT_MSG(DYNA_REC, !m_xregs[xr].IsLocked(), "GetFreeXReg returned locked register");
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsRevertable(), "Invalid transaction state");
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_hosts_is_locked[xr], "GetFreeXReg returned locked register");
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_guests_revertable[i], "Invalid transaction state");
|
||||
|
||||
m_xregs[xr].SetBoundTo(i);
|
||||
m_state.m_hosts_has_guest_register[xr] = true;
|
||||
m_state.m_hosts_guest_register[xr] = i;
|
||||
|
||||
if (doLoad)
|
||||
LoadRegister(i, xr);
|
||||
|
||||
ASSERT_MSG(DYNA_REC,
|
||||
std::ranges::none_of(m_regs,
|
||||
[xr](const auto& r) {
|
||||
return r.IsInHostRegister() && r.GetHostRegister() == xr;
|
||||
}),
|
||||
std::ranges::none_of(
|
||||
m_state.m_guests_in_host_register,
|
||||
[&](const auto& r) { return m_state.m_guests_host_register[r] == xr; }),
|
||||
"Xreg {} already bound", std::to_underlying(xr));
|
||||
|
||||
m_regs[i].SetInHostRegister(xr, makeDirty);
|
||||
m_state.m_guests_in_host_register[i] = true;
|
||||
m_state.m_guests_host_register[i] = xr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// reg location must be simplereg; memory locations
|
||||
// and immediates are taken care of above.
|
||||
if (makeDirty)
|
||||
m_regs[i].SetDirty();
|
||||
}
|
||||
|
||||
if (makeDirty)
|
||||
{
|
||||
m_state.m_guests_in_ppc_state[i] = false;
|
||||
DiscardImm(i);
|
||||
}
|
||||
|
||||
ASSERT_MSG(DYNA_REC, !m_xregs[RX(i)].IsLocked(),
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_hosts_is_locked[RX(i)],
|
||||
"WTF, this reg ({} -> {}) should have been flushed", i, std::to_underlying(RX(i)));
|
||||
}
|
||||
|
||||
@@ -464,37 +459,45 @@ void RegCache::StoreFromRegister(preg_t i, FlushMode mode,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers)
|
||||
{
|
||||
// When a transaction is in progress, allowing the store would overwrite the old value.
|
||||
ASSERT_MSG(DYNA_REC, !m_regs[i].IsRevertable(), "Register transaction on {} is in progress!", i);
|
||||
ASSERT_MSG(DYNA_REC, !m_state.m_guests_revertable[i],
|
||||
"Register transaction on {} is in progress!", i);
|
||||
|
||||
if (!m_regs[i].IsInPPCState())
|
||||
if (!m_state.m_guests_in_ppc_state[i])
|
||||
StoreRegister(i, GetPPCStateLocation(i), ignore_discarded_registers);
|
||||
|
||||
if (mode == FlushMode::Full && m_regs[i].IsInHostRegister())
|
||||
m_xregs[m_regs[i].GetHostRegister()].Unbind();
|
||||
if (mode == FlushMode::Full && m_state.m_guests_in_host_register[i])
|
||||
{
|
||||
m_state.m_guests_in_host_register[i] = false;
|
||||
m_state.m_hosts_has_guest_register[m_state.m_guests_host_register[i]] = false;
|
||||
}
|
||||
|
||||
m_regs[i].SetFlushed(mode != FlushMode::Full);
|
||||
m_state.m_guests_in_ppc_state[i] = true;
|
||||
}
|
||||
|
||||
X64Reg RegCache::GetFreeXReg()
|
||||
{
|
||||
const auto order = GetAllocationOrder();
|
||||
for (const X64Reg xr : order)
|
||||
const BitSetHost free_registers = GetFreeRegisters();
|
||||
if (free_registers)
|
||||
{
|
||||
if (m_xregs[xr].IsFree())
|
||||
return xr;
|
||||
for (const X64Reg xr : GetAllocationOrder())
|
||||
{
|
||||
if (free_registers[xr])
|
||||
return xr;
|
||||
}
|
||||
}
|
||||
|
||||
// Okay, not found; run the register allocator heuristic and
|
||||
// figure out which register we should clobber.
|
||||
float min_score = std::numeric_limits<float>::max();
|
||||
X64Reg best_xreg = INVALID_REG;
|
||||
size_t best_preg = 0;
|
||||
for (const X64Reg xreg : order)
|
||||
preg_t best_preg = 0;
|
||||
for (const auto i : GetAllocatableRegisters() & ~m_state.m_hosts_is_locked)
|
||||
{
|
||||
const preg_t preg = m_xregs[xreg].Contents();
|
||||
if (m_xregs[xreg].IsLocked() || m_regs[preg].IsLocked())
|
||||
const preg_t preg = m_state.m_hosts_guest_register[i];
|
||||
if (m_state.m_guests_is_locked[preg])
|
||||
continue;
|
||||
|
||||
const auto xreg = static_cast<X64Reg>(i);
|
||||
const float score = ScoreRegister(xreg);
|
||||
if (score < min_score)
|
||||
{
|
||||
@@ -515,29 +518,24 @@ X64Reg RegCache::GetFreeXReg()
|
||||
return INVALID_REG;
|
||||
}
|
||||
|
||||
int RegCache::NumFreeRegisters() const
|
||||
BitSetHost RegCache::GetFreeRegisters() const
|
||||
{
|
||||
int count = 0;
|
||||
for (const X64Reg reg : GetAllocationOrder())
|
||||
{
|
||||
if (m_xregs[reg].IsFree())
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
return (~m_state.m_hosts_has_guest_register & ~m_state.m_hosts_is_locked &
|
||||
GetAllocatableRegisters());
|
||||
}
|
||||
|
||||
// Estimate roughly how bad it would be to de-allocate this register. Higher score
|
||||
// means more bad.
|
||||
float RegCache::ScoreRegister(X64Reg xreg) const
|
||||
{
|
||||
preg_t preg = m_xregs[xreg].Contents();
|
||||
const preg_t preg = m_state.m_hosts_guest_register[xreg];
|
||||
float score = 0;
|
||||
|
||||
// If it's not dirty, we don't need a store to write it back to the register file, so
|
||||
// bias a bit against dirty registers. Testing shows that a bias of 2 seems roughly
|
||||
// right: 3 causes too many extra clobbers, while 1 saves very few clobbers relative
|
||||
// to the number of extra stores it causes.
|
||||
if (!m_regs[preg].IsInPPCState())
|
||||
if (!m_state.m_guests_in_ppc_state[preg])
|
||||
score += 2;
|
||||
|
||||
// If the register isn't actually needed in a physical register for a later instruction,
|
||||
@@ -547,9 +545,9 @@ float RegCache::ScoreRegister(X64Reg xreg) const
|
||||
// Don't look too far ahead; we don't want to have quadratic compilation times for
|
||||
// enormous block sizes!
|
||||
// This actually improves register allocation a tiny bit; I'm not sure why.
|
||||
u32 lookahead = std::min(m_jit.js.instructionsLeft, 64);
|
||||
const u32 lookahead = std::min(m_jit.js.instructionsLeft, 64);
|
||||
// Count how many other registers are going to be used before we need this one again.
|
||||
u32 regs_in_count = CountRegsIn(preg, lookahead).Count();
|
||||
const u32 regs_in_count = CountRegsIn(preg, lookahead).Count();
|
||||
// Totally ad-hoc heuristic to bias based on how many other registers we'll need
|
||||
// before this one gets used again.
|
||||
score += 1 + 2 * (5 - log2f(1 + (float)regs_in_count));
|
||||
@@ -560,60 +558,65 @@ float RegCache::ScoreRegister(X64Reg xreg) const
|
||||
|
||||
X64Reg RegCache::RX(preg_t preg) const
|
||||
{
|
||||
ASSERT_MSG(DYNA_REC, m_regs[preg].IsInHostRegister(), "Not in host register - {}", preg);
|
||||
return m_regs[preg].GetHostRegister();
|
||||
auto xreg = GetHostRegister(preg);
|
||||
ASSERT_MSG(DYNA_REC, xreg, "Not in host register - {}", preg);
|
||||
return xreg.value();
|
||||
}
|
||||
|
||||
void RegCache::Lock(preg_t preg)
|
||||
{
|
||||
m_regs[preg].Lock();
|
||||
++m_state.m_guests_n_locked[preg];
|
||||
m_state.m_guests_is_locked[preg] = true;
|
||||
}
|
||||
|
||||
void RegCache::Unlock(preg_t preg)
|
||||
{
|
||||
m_regs[preg].Unlock();
|
||||
if (!m_regs[preg].IsLocked())
|
||||
--m_state.m_guests_n_locked[preg];
|
||||
if (m_state.m_guests_n_locked[preg] == 0)
|
||||
{
|
||||
m_state.m_guests_is_locked[preg] = false;
|
||||
// Fully unlocked, reset realization state.
|
||||
m_constraints[preg] = {};
|
||||
m_guests_constraints[preg] = {};
|
||||
}
|
||||
}
|
||||
|
||||
void RegCache::LockX(X64Reg xr)
|
||||
{
|
||||
m_xregs[xr].Lock();
|
||||
++m_state.m_hosts_n_locked[xr];
|
||||
m_state.m_hosts_is_locked[xr] = true;
|
||||
}
|
||||
|
||||
void RegCache::UnlockX(X64Reg xr)
|
||||
{
|
||||
m_xregs[xr].Unlock();
|
||||
--m_state.m_hosts_n_locked[xr];
|
||||
m_state.m_hosts_is_locked[xr] = m_state.m_hosts_n_locked[xr] > 0;
|
||||
}
|
||||
|
||||
bool RegCache::IsRealized(preg_t preg) const
|
||||
{
|
||||
return m_constraints[preg].IsRealized();
|
||||
return m_guests_constraints[preg].IsRealized();
|
||||
}
|
||||
|
||||
void RegCache::Realize(preg_t preg)
|
||||
{
|
||||
if (m_constraints[preg].IsRealized())
|
||||
if (m_guests_constraints[preg].IsRealized())
|
||||
return;
|
||||
|
||||
const bool load = m_constraints[preg].ShouldLoad();
|
||||
const bool dirty = m_constraints[preg].ShouldDirty();
|
||||
const bool kill_imm = m_constraints[preg].ShouldKillImmediate();
|
||||
const bool kill_mem = m_constraints[preg].ShouldKillMemory();
|
||||
const bool load = m_guests_constraints[preg].ShouldLoad();
|
||||
const bool dirty = m_guests_constraints[preg].ShouldDirty();
|
||||
const bool kill_imm = m_guests_constraints[preg].ShouldKillImmediate();
|
||||
const bool kill_mem = m_guests_constraints[preg].ShouldKillMemory();
|
||||
|
||||
const auto do_bind = [&] {
|
||||
BindToRegister(preg, load, dirty);
|
||||
m_constraints[preg].Realized(RCConstraint::RealizedLoc::Bound);
|
||||
m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Bound);
|
||||
};
|
||||
|
||||
if (m_constraints[preg].ShouldBeRevertable())
|
||||
if (m_guests_constraints[preg].ShouldBeRevertable())
|
||||
{
|
||||
StoreFromRegister(preg, FlushMode::Undirty);
|
||||
do_bind();
|
||||
m_regs[preg].SetRevertable();
|
||||
m_state.m_guests_revertable[preg] = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -622,14 +625,14 @@ void RegCache::Realize(preg_t preg)
|
||||
if (dirty || kill_imm)
|
||||
do_bind();
|
||||
else
|
||||
m_constraints[preg].Realized(RCConstraint::RealizedLoc::Imm);
|
||||
m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Imm);
|
||||
}
|
||||
else if (!m_regs[preg].IsInHostRegister())
|
||||
else if (!m_state.m_guests_in_host_register[preg])
|
||||
{
|
||||
if (kill_mem)
|
||||
do_bind();
|
||||
else
|
||||
m_constraints[preg].Realized(RCConstraint::RealizedLoc::Mem);
|
||||
m_guests_constraints[preg].Realized(RCConstraint::RealizedLoc::Mem);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -639,5 +642,5 @@ void RegCache::Realize(preg_t preg)
|
||||
|
||||
bool RegCache::IsAnyConstraintActive() const
|
||||
{
|
||||
return std::ranges::any_of(m_constraints, &RCConstraint::IsActive);
|
||||
return std::ranges::any_of(m_guests_constraints, &RCConstraint::IsActive);
|
||||
}
|
||||
@@ -5,12 +5,13 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
|
||||
#include "Common/x64Emitter.h"
|
||||
#include "Core/PowerPC/Jit64/RegCache/CachedReg.h"
|
||||
#include "Core/PowerPC/Jit64/RegCache/RCMode.h"
|
||||
|
||||
class Jit64;
|
||||
enum class RCMode;
|
||||
@@ -19,8 +20,11 @@ class RCOpArg;
|
||||
class RCX64Reg;
|
||||
class RegCache;
|
||||
|
||||
using preg_t = size_t;
|
||||
static constexpr size_t NUM_XREGS = 16;
|
||||
using preg_t = u8;
|
||||
static constexpr size_t NUM_HOST_REGS = 16;
|
||||
using BitSetHost = BitSet16;
|
||||
static constexpr size_t NUM_GUEST_REGS = 32;
|
||||
using BitSetGuest = BitSet32;
|
||||
|
||||
class RCOpArg
|
||||
{
|
||||
@@ -94,6 +98,30 @@ private:
|
||||
std::variant<std::monostate, Gen::X64Reg, preg_t> contents;
|
||||
};
|
||||
|
||||
struct RegsState
|
||||
{
|
||||
// If m_hosts_has_guest_register[xr], then xr is bound to m_hosts_guest_register[xr]
|
||||
std::array<preg_t, NUM_HOST_REGS> m_hosts_guest_register{};
|
||||
BitSetHost m_hosts_has_guest_register;
|
||||
// TODO: If all the unlocks for a register always happened at the same time, then the array
|
||||
// wouldn't be necessary. However, that's fragile, so it's better to take the safe option.
|
||||
BitSetHost m_hosts_is_locked;
|
||||
std::array<u8, NUM_GUEST_REGS> m_hosts_n_locked{};
|
||||
|
||||
// If m_guests_in_host_register[preg], then preg is bound to m_guests_host_register[preg]
|
||||
std::array<u8, NUM_GUEST_REGS> m_guests_host_register{};
|
||||
BitSetGuest m_guests_in_host_register;
|
||||
// Is the value in PPCState correct right now?
|
||||
BitSetGuest m_guests_in_ppc_state = BitSetGuest::AllTrue();
|
||||
// If certain memory load operations fail, the destination register must be reverted.
|
||||
// This is achieved by not flushing it, and using the old value from PPCState.
|
||||
BitSetGuest m_guests_revertable;
|
||||
// TODO: If all the unlocks for a register always happened at the same time, then the array
|
||||
// wouldn't be necessary. However, that's fragile, so it's better to take the safe option.
|
||||
BitSetGuest m_guests_is_locked;
|
||||
std::array<u8, NUM_GUEST_REGS> m_guests_n_locked{};
|
||||
};
|
||||
|
||||
class RCForkGuard
|
||||
{
|
||||
public:
|
||||
@@ -112,8 +140,141 @@ private:
|
||||
explicit RCForkGuard(RegCache& rc_);
|
||||
|
||||
RegCache* rc;
|
||||
std::array<PPCCachedReg, 32> m_regs;
|
||||
std::array<X64CachedReg, NUM_XREGS> m_xregs;
|
||||
RegsState m_state;
|
||||
};
|
||||
|
||||
class RCConstraint
|
||||
{
|
||||
public:
|
||||
bool IsRealized() const { return realized != RealizedLoc::Invalid; }
|
||||
bool IsActive() const
|
||||
{
|
||||
return IsRealized() || write || read || kill_imm || kill_mem || revertable;
|
||||
}
|
||||
|
||||
bool ShouldLoad() const { return read; }
|
||||
bool ShouldDirty() const { return write; }
|
||||
bool ShouldBeRevertable() const { return revertable; }
|
||||
bool ShouldKillImmediate() const { return kill_imm; }
|
||||
bool ShouldKillMemory() const { return kill_mem; }
|
||||
|
||||
enum class RealizedLoc
|
||||
{
|
||||
Invalid,
|
||||
Bound,
|
||||
Imm,
|
||||
Mem,
|
||||
};
|
||||
|
||||
void Realized(RealizedLoc loc)
|
||||
{
|
||||
realized = loc;
|
||||
ASSERT(IsRealized());
|
||||
}
|
||||
|
||||
enum class ConstraintLoc
|
||||
{
|
||||
Bound,
|
||||
BoundOrImm,
|
||||
BoundOrMem,
|
||||
Any,
|
||||
};
|
||||
|
||||
void AddUse(RCMode mode) { AddConstraint(mode, ConstraintLoc::Any, false); }
|
||||
void AddUseNoImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrMem, false); }
|
||||
void AddBindOrImm(RCMode mode) { AddConstraint(mode, ConstraintLoc::BoundOrImm, false); }
|
||||
void AddBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, false); }
|
||||
void AddRevertableBind(RCMode mode) { AddConstraint(mode, ConstraintLoc::Bound, true); }
|
||||
|
||||
private:
|
||||
void AddConstraint(RCMode mode, ConstraintLoc loc, bool should_revertable)
|
||||
{
|
||||
if (IsRealized())
|
||||
{
|
||||
ASSERT(IsCompatible(mode, loc, should_revertable));
|
||||
return;
|
||||
}
|
||||
|
||||
if (should_revertable)
|
||||
revertable = true;
|
||||
|
||||
switch (loc)
|
||||
{
|
||||
case ConstraintLoc::Bound:
|
||||
kill_imm = true;
|
||||
kill_mem = true;
|
||||
break;
|
||||
case ConstraintLoc::BoundOrImm:
|
||||
kill_mem = true;
|
||||
break;
|
||||
case ConstraintLoc::BoundOrMem:
|
||||
kill_imm = true;
|
||||
break;
|
||||
case ConstraintLoc::Any:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case RCMode::Read:
|
||||
read = true;
|
||||
break;
|
||||
case RCMode::Write:
|
||||
write = true;
|
||||
break;
|
||||
case RCMode::ReadWrite:
|
||||
read = true;
|
||||
write = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCompatible(RCMode mode, ConstraintLoc loc, bool should_revertable) const
|
||||
{
|
||||
if (should_revertable && !revertable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool is_loc_compatible = [&] {
|
||||
switch (loc)
|
||||
{
|
||||
case ConstraintLoc::Bound:
|
||||
return realized == RealizedLoc::Bound;
|
||||
case ConstraintLoc::BoundOrImm:
|
||||
return realized == RealizedLoc::Bound || realized == RealizedLoc::Imm;
|
||||
case ConstraintLoc::BoundOrMem:
|
||||
return realized == RealizedLoc::Bound || realized == RealizedLoc::Mem;
|
||||
case ConstraintLoc::Any:
|
||||
return true;
|
||||
}
|
||||
ASSERT(false);
|
||||
return false;
|
||||
}();
|
||||
|
||||
const bool is_mode_compatible = [&] {
|
||||
switch (mode)
|
||||
{
|
||||
case RCMode::Read:
|
||||
return read;
|
||||
case RCMode::Write:
|
||||
return write;
|
||||
case RCMode::ReadWrite:
|
||||
return read && write;
|
||||
}
|
||||
ASSERT(false);
|
||||
return false;
|
||||
}();
|
||||
|
||||
return is_loc_compatible && is_mode_compatible;
|
||||
}
|
||||
|
||||
RealizedLoc realized = RealizedLoc::Invalid;
|
||||
bool write = false;
|
||||
bool read = false;
|
||||
bool kill_imm = false;
|
||||
bool kill_mem = false;
|
||||
bool revertable = false;
|
||||
};
|
||||
|
||||
class RegCache
|
||||
@@ -165,7 +326,14 @@ public:
|
||||
virtual u32 Imm32(preg_t preg) const = 0;
|
||||
virtual s32 SImm32(preg_t preg) const = 0;
|
||||
|
||||
bool IsBound(preg_t preg) const { return m_regs[preg].IsInHostRegister(); }
|
||||
std::optional<Gen::X64Reg> GetHostRegister(preg_t preg) const
|
||||
{
|
||||
if (!m_state.m_guests_in_host_register[preg])
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
return static_cast<Gen::X64Reg>(m_state.m_guests_host_register[preg]);
|
||||
}
|
||||
|
||||
RCOpArg Use(preg_t preg, RCMode mode);
|
||||
RCOpArg UseNoImm(preg_t preg, RCMode mode);
|
||||
@@ -176,17 +344,17 @@ public:
|
||||
RCX64Reg Scratch(Gen::X64Reg xr);
|
||||
|
||||
RCForkGuard Fork();
|
||||
void Discard(BitSet32 pregs);
|
||||
void Flush(BitSet32 pregs = BitSet32::AllTrue(), FlushMode mode = FlushMode::Full,
|
||||
void Discard(BitSetGuest pregs);
|
||||
void Flush(BitSetGuest pregs = BitSetGuest::AllTrue(), FlushMode mode = FlushMode::Full,
|
||||
IgnoreDiscardedRegisters ignore_discarded_registers = IgnoreDiscardedRegisters::No);
|
||||
void Reset(BitSet32 pregs);
|
||||
BitSet32 RegistersRevertable() const;
|
||||
void Reset(BitSetGuest pregs);
|
||||
BitSetGuest RegistersRevertable() const;
|
||||
void Commit();
|
||||
|
||||
bool IsAllUnlocked() const;
|
||||
|
||||
void PreloadRegisters(BitSet32 pregs);
|
||||
BitSet16 RegistersInUse() const;
|
||||
void PreloadRegisters(BitSetGuest to_preload);
|
||||
BitSetHost HostRegistersInUse() const;
|
||||
|
||||
protected:
|
||||
friend class RCOpArg;
|
||||
@@ -200,9 +368,9 @@ protected:
|
||||
virtual void DiscardImm(preg_t preg) = 0;
|
||||
|
||||
virtual std::span<const Gen::X64Reg> GetAllocationOrder() const = 0;
|
||||
|
||||
virtual BitSet32 GetRegUtilization() const = 0;
|
||||
virtual BitSet32 CountRegsIn(preg_t preg, u32 lookahead) const = 0;
|
||||
virtual BitSetHost GetAllocatableRegisters() const = 0;
|
||||
virtual BitSetGuest GetRegUtilization() const = 0;
|
||||
virtual BitSetGuest CountRegsIn(preg_t preg, u32 lookahead) const = 0;
|
||||
|
||||
void FlushX(Gen::X64Reg reg);
|
||||
void DiscardRegister(preg_t preg);
|
||||
@@ -213,7 +381,7 @@ protected:
|
||||
|
||||
Gen::X64Reg GetFreeXReg();
|
||||
|
||||
int NumFreeRegisters() const;
|
||||
BitSetHost GetFreeRegisters() const;
|
||||
float ScoreRegister(Gen::X64Reg xreg) const;
|
||||
|
||||
virtual Gen::OpArg R(preg_t preg) const = 0;
|
||||
@@ -231,7 +399,6 @@ protected:
|
||||
Jit64& m_jit;
|
||||
Gen::XEmitter& m_emitter;
|
||||
|
||||
std::array<PPCCachedReg, 32> m_regs;
|
||||
std::array<X64CachedReg, NUM_XREGS> m_xregs;
|
||||
std::array<RCConstraint, 32> m_constraints;
|
||||
RegsState m_state{};
|
||||
std::array<RCConstraint, NUM_GUEST_REGS> m_guests_constraints;
|
||||
};
|
||||
Reference in new issue
Block a user