From db34b8510767ff11d03dbfb2572045093d01b522 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 21 Aug 2023 20:46:05 -0700 Subject: [PATCH] irjit: Allow flag-based allocation order. Sometimes backends have needs, like XMM0/v0-only, or similar. --- Core/MIPS/IR/IRRegCache.cpp | 28 ++++++++++++++-------------- Core/MIPS/IR/IRRegCache.h | 9 +++++---- Core/MIPS/RiscV/RiscVRegCache.cpp | 4 ++-- Core/MIPS/RiscV/RiscVRegCache.h | 2 +- Core/MIPS/x86/X64IRRegCache.cpp | 4 ++-- Core/MIPS/x86/X64IRRegCache.h | 2 +- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Core/MIPS/IR/IRRegCache.cpp b/Core/MIPS/IR/IRRegCache.cpp index 985adad7a2..0141041500 100644 --- a/Core/MIPS/IR/IRRegCache.cpp +++ b/Core/MIPS/IR/IRRegCache.cpp @@ -330,18 +330,18 @@ void IRNativeRegCacheBase::MarkGPRAsPointerDirty(IRReg gpr) { // Stays pointerified or REG_AS_PTR. } -IRNativeReg IRNativeRegCacheBase::AllocateReg(MIPSLoc type) { +IRNativeReg IRNativeRegCacheBase::AllocateReg(MIPSLoc type, MIPSMap flags) { _dbg_assert_(type == MIPSLoc::REG || type == MIPSLoc::FREG || type == MIPSLoc::VREG); - IRNativeReg nreg = FindFreeReg(type); + IRNativeReg nreg = FindFreeReg(type, flags); if (nreg != -1) return nreg; // Still nothing. Let's spill a reg and goto 10. bool clobbered; - IRNativeReg bestToSpill = FindBestToSpill(type, true, &clobbered); + IRNativeReg bestToSpill = FindBestToSpill(type, flags, true, &clobbered); if (bestToSpill == -1) { - bestToSpill = FindBestToSpill(type, false, &clobbered); + bestToSpill = FindBestToSpill(type, flags, false, &clobbered); } if (bestToSpill != -1) { @@ -351,7 +351,7 @@ IRNativeReg IRNativeRegCacheBase::AllocateReg(MIPSLoc type) { FlushNativeReg(bestToSpill); } // Now one must be free. - return FindFreeReg(type); + return FindFreeReg(type, flags); } // Uh oh, we have all of them spilllocked.... @@ -360,9 +360,9 @@ IRNativeReg IRNativeRegCacheBase::AllocateReg(MIPSLoc type) { return -1; } -IRNativeReg IRNativeRegCacheBase::FindFreeReg(MIPSLoc type) const { +IRNativeReg IRNativeRegCacheBase::FindFreeReg(MIPSLoc type, MIPSMap flags) const { int allocCount = 0, base = 0; - const int *allocOrder = GetAllocationOrder(type, allocCount, base); + const int *allocOrder = GetAllocationOrder(type, flags, allocCount, base); for (int i = 0; i < allocCount; i++) { IRNativeReg nreg = IRNativeReg(allocOrder[i] - base); @@ -375,9 +375,9 @@ IRNativeReg IRNativeRegCacheBase::FindFreeReg(MIPSLoc type) const { return -1; } -IRNativeReg IRNativeRegCacheBase::FindBestToSpill(MIPSLoc type, bool unusedOnly, bool *clobbered) const { +IRNativeReg IRNativeRegCacheBase::FindBestToSpill(MIPSLoc type, MIPSMap flags, bool unusedOnly, bool *clobbered) const { int allocCount = 0, base = 0; - const int *allocOrder = GetAllocationOrder(type, allocCount, base); + const int *allocOrder = GetAllocationOrder(type, flags, allocCount, base); static const int UNUSED_LOOKAHEAD_OPS = 30; @@ -670,7 +670,7 @@ IRNativeReg IRNativeRegCacheBase::MapWithTemp(const IRInst &inst, MIPSLoc type) ApplyMapping(mapping, 3); // Grab a temp while things are spill locked. - IRNativeReg temp = AllocateReg(type); + IRNativeReg temp = AllocateReg(type, MIPSMap::INIT); CleanupMapping(mapping, 3); return temp; } @@ -829,22 +829,22 @@ IRNativeReg IRNativeRegCacheBase::MapNativeReg(MIPSLoc type, IRReg first, int la case MIPSLoc::REG_AS_PTR: case MIPSLoc::REG: if (type != MIPSLoc::REG) - nreg = AllocateReg(type); + nreg = AllocateReg(type, flags); break; case MIPSLoc::FREG: if (type != MIPSLoc::FREG) - nreg = AllocateReg(type); + nreg = AllocateReg(type, flags); break; case MIPSLoc::VREG: if (type != MIPSLoc::VREG) - nreg = AllocateReg(type); + nreg = AllocateReg(type, flags); break; case MIPSLoc::IMM: case MIPSLoc::MEM: - nreg = AllocateReg(type); + nreg = AllocateReg(type, flags); break; } } diff --git a/Core/MIPS/IR/IRRegCache.h b/Core/MIPS/IR/IRRegCache.h index 23cfa32b51..2d162a835b 100644 --- a/Core/MIPS/IR/IRRegCache.h +++ b/Core/MIPS/IR/IRRegCache.h @@ -74,6 +74,7 @@ private: }; // Initing is the default so the flag is reversed. +// 8 bits - upper 4 are reserved for backend purposes. enum class MIPSMap : uint8_t { INIT = 0, DIRTY = 1, @@ -191,15 +192,15 @@ public: protected: virtual void SetupInitialRegs(); - virtual const int *GetAllocationOrder(MIPSLoc type, int &count, int &base) const = 0; + virtual const int *GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const = 0; virtual const StaticAllocation *GetStaticAllocations(int &count) const { count = 0; return nullptr; } - IRNativeReg AllocateReg(MIPSLoc type); - IRNativeReg FindFreeReg(MIPSLoc type) const; - IRNativeReg FindBestToSpill(MIPSLoc type, bool unusedOnly, bool *clobbered) const; + IRNativeReg AllocateReg(MIPSLoc type, MIPSMap flags); + IRNativeReg FindFreeReg(MIPSLoc type, MIPSMap flags) const; + IRNativeReg FindBestToSpill(MIPSLoc type, MIPSMap flags, bool unusedOnly, bool *clobbered) const; virtual void DiscardNativeReg(IRNativeReg nreg); virtual void FlushNativeReg(IRNativeReg nreg); virtual void DiscardReg(IRReg mreg); diff --git a/Core/MIPS/RiscV/RiscVRegCache.cpp b/Core/MIPS/RiscV/RiscVRegCache.cpp index e7017ebd7b..a39dd6b344 100644 --- a/Core/MIPS/RiscV/RiscVRegCache.cpp +++ b/Core/MIPS/RiscV/RiscVRegCache.cpp @@ -55,7 +55,7 @@ void RiscVRegCache::SetupInitialRegs() { mrInitial_[MIPS_REG_ZERO].isStatic = true; } -const int *RiscVRegCache::GetAllocationOrder(MIPSLoc type, int &count, int &base) const { +const int *RiscVRegCache::GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const { base = X0; if (type == MIPSLoc::REG) { @@ -236,7 +236,7 @@ RiscVReg RiscVRegCache::TryMapTempImm(IRReg r) { } RiscVReg RiscVRegCache::GetAndLockTempR() { - RiscVReg reg = (RiscVReg)AllocateReg(MIPSLoc::REG); + RiscVReg reg = (RiscVReg)AllocateReg(MIPSLoc::REG, MIPSMap::INIT); if (reg != INVALID_REG) { nr[reg].tempLockIRIndex = irIndex_; } diff --git a/Core/MIPS/RiscV/RiscVRegCache.h b/Core/MIPS/RiscV/RiscVRegCache.h index 4da57cc9b0..c1b140896f 100644 --- a/Core/MIPS/RiscV/RiscVRegCache.h +++ b/Core/MIPS/RiscV/RiscVRegCache.h @@ -73,7 +73,7 @@ public: protected: void SetupInitialRegs() override; const StaticAllocation *GetStaticAllocations(int &count) const override; - const int *GetAllocationOrder(MIPSLoc type, int &count, int &base) const override; + const int *GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const override; void AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) override; void LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) override; diff --git a/Core/MIPS/x86/X64IRRegCache.cpp b/Core/MIPS/x86/X64IRRegCache.cpp index cb47994397..5b39b28de1 100644 --- a/Core/MIPS/x86/X64IRRegCache.cpp +++ b/Core/MIPS/x86/X64IRRegCache.cpp @@ -46,7 +46,7 @@ void X64IRRegCache::Init(XEmitter *emitter) { emit_ = emitter; } -const int *X64IRRegCache::GetAllocationOrder(MIPSLoc type, int &count, int &base) const { +const int *X64IRRegCache::GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const { if (type == MIPSLoc::REG) { base = RAX; @@ -143,7 +143,7 @@ X64Reg X64IRRegCache::TryMapTempImm(IRReg r) { } X64Reg X64IRRegCache::GetAndLockTempR() { - X64Reg reg = FromNativeReg(AllocateReg(MIPSLoc::REG)); + X64Reg reg = FromNativeReg(AllocateReg(MIPSLoc::REG, MIPSMap::INIT)); if (reg != INVALID_REG) { nr[reg].tempLockIRIndex = irIndex_; } diff --git a/Core/MIPS/x86/X64IRRegCache.h b/Core/MIPS/x86/X64IRRegCache.h index a56dffb3a7..3e5909591b 100644 --- a/Core/MIPS/x86/X64IRRegCache.h +++ b/Core/MIPS/x86/X64IRRegCache.h @@ -71,7 +71,7 @@ public: Gen::X64Reg FX(IRReg preg); protected: - const int *GetAllocationOrder(MIPSLoc type, int &count, int &base) const override; + const int *GetAllocationOrder(MIPSLoc type, MIPSMap flags, int &count, int &base) const override; void AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) override; void LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) override;