Skip to content

Commit

Permalink
Merge pull request #12517 from lioncash/alloc
Browse files Browse the repository at this point in the history
Jit64/JitRegCache: Simplify GetAllocationOrder()
  • Loading branch information
JosJuice committed Jan 23, 2024
2 parents e12933f + 9f82efa commit 96fda3d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
7 changes: 3 additions & 4 deletions Source/Core/Core/PowerPC/Jit64/RegCache/FPURegCache.cpp
Expand Up @@ -25,11 +25,10 @@ void FPURegCache::LoadRegister(preg_t preg, X64Reg new_loc)
m_emitter->MOVAPD(new_loc, m_regs[preg].Location().value());
}

const X64Reg* FPURegCache::GetAllocationOrder(size_t* count) const
std::span<const X64Reg> FPURegCache::GetAllocationOrder() const
{
static const X64Reg allocation_order[] = {XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12,
XMM13, XMM14, XMM15, XMM2, XMM3, XMM4, XMM5};
*count = sizeof(allocation_order) / sizeof(X64Reg);
static constexpr X64Reg allocation_order[] = {XMM6, XMM7, XMM8, XMM9, XMM10, XMM11, XMM12,
XMM13, XMM14, XMM15, XMM2, XMM3, XMM4, XMM5};
return allocation_order;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/PowerPC/Jit64/RegCache/FPURegCache.h
Expand Up @@ -16,7 +16,7 @@ class FPURegCache final : public RegCache
Gen::OpArg GetDefaultLocation(preg_t preg) const override;
void StoreRegister(preg_t preg, const Gen::OpArg& newLoc) override;
void LoadRegister(preg_t preg, Gen::X64Reg newLoc) override;
const Gen::X64Reg* GetAllocationOrder(size_t* count) const override;
std::span<const Gen::X64Reg> GetAllocationOrder() const override;
BitSet32 GetRegUtilization() const override;
BitSet32 CountRegsIn(preg_t preg, u32 lookahead) const override;
};
5 changes: 2 additions & 3 deletions Source/Core/Core/PowerPC/Jit64/RegCache/GPRRegCache.cpp
Expand Up @@ -30,9 +30,9 @@ OpArg GPRRegCache::GetDefaultLocation(preg_t preg) const
return PPCSTATE_GPR(preg);
}

const X64Reg* GPRRegCache::GetAllocationOrder(size_t* count) const
std::span<const X64Reg> GPRRegCache::GetAllocationOrder() const
{
static const X64Reg allocation_order[] = {
static constexpr X64Reg allocation_order[] = {
// R12, when used as base register, for example in a LEA, can generate bad code! Need to look into
// this.
#ifdef _WIN32
Expand All @@ -43,7 +43,6 @@ const X64Reg* GPRRegCache::GetAllocationOrder(size_t* count) const
R8, R9, R10, R11, RCX
#endif
};
*count = sizeof(allocation_order) / sizeof(X64Reg);
return allocation_order;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/PowerPC/Jit64/RegCache/GPRRegCache.h
Expand Up @@ -17,7 +17,7 @@ class GPRRegCache final : public RegCache
Gen::OpArg GetDefaultLocation(preg_t preg) const override;
void StoreRegister(preg_t preg, const Gen::OpArg& new_loc) override;
void LoadRegister(preg_t preg, Gen::X64Reg new_loc) override;
const Gen::X64Reg* GetAllocationOrder(size_t* count) const override;
std::span<const Gen::X64Reg> GetAllocationOrder() const override;
BitSet32 GetRegUtilization() const override;
BitSet32 CountRegsIn(preg_t preg, u32 lookahead) const override;
};
28 changes: 12 additions & 16 deletions Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.cpp
Expand Up @@ -592,29 +592,25 @@ void RegCache::StoreFromRegister(preg_t i, FlushMode mode)

X64Reg RegCache::GetFreeXReg()
{
size_t aCount;
const X64Reg* aOrder = GetAllocationOrder(&aCount);
for (size_t i = 0; i < aCount; i++)
const auto order = GetAllocationOrder();
for (const X64Reg xr : order)
{
X64Reg xr = aOrder[i];
if (m_xregs[xr].IsFree())
{
return xr;
}
}

// Okay, not found; run the register allocator heuristic and figure out which register we should
// clobber.
// 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 (size_t i = 0; i < aCount; i++)
for (const X64Reg xreg : order)
{
X64Reg xreg = (X64Reg)aOrder[i];
preg_t preg = m_xregs[xreg].Contents();
const preg_t preg = m_xregs[xreg].Contents();
if (m_xregs[xreg].IsLocked() || m_regs[preg].IsLocked())
continue;
float score = ScoreRegister(xreg);

const float score = ScoreRegister(xreg);
if (score < min_score)
{
min_score = score;
Expand All @@ -637,11 +633,11 @@ X64Reg RegCache::GetFreeXReg()
int RegCache::NumFreeRegisters() const
{
int count = 0;
size_t aCount;
const X64Reg* aOrder = GetAllocationOrder(&aCount);
for (size_t i = 0; i < aCount; i++)
if (m_xregs[aOrder[i]].IsFree())
for (const X64Reg reg : GetAllocationOrder())
{
if (m_xregs[reg].IsFree())
count++;
}
return count;
}

Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/PowerPC/Jit64/RegCache/JitRegCache.h
Expand Up @@ -5,6 +5,7 @@

#include <array>
#include <cstddef>
#include <span>
#include <type_traits>
#include <variant>

Expand Down Expand Up @@ -187,7 +188,7 @@ class RegCache
virtual void StoreRegister(preg_t preg, const Gen::OpArg& new_loc) = 0;
virtual void LoadRegister(preg_t preg, Gen::X64Reg new_loc) = 0;

virtual const Gen::X64Reg* GetAllocationOrder(size_t* count) const = 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;
Expand Down

0 comments on commit 96fda3d

Please sign in to comment.