mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-30 09:43:45 +01:00
riscv: Centralize reg mapping itself.
This commit is contained in:
1 parent
83946eeef8
commit
92f7374c89
6 files changed
+335
-288
No files matched your search
@@ -509,6 +509,200 @@ void IRNativeRegCacheBase::FlushNativeReg(IRNativeReg nreg) {
|
||||
nr[nreg].normalized32 = false;
|
||||
}
|
||||
|
||||
IRNativeReg IRNativeRegCacheBase::MapNativeReg(MIPSLoc type, IRReg first, int lanes, MIPSMap flags) {
|
||||
_assert_msg_(first != IRREG_INVALID, "Cannot map invalid register");
|
||||
_assert_msg_(lanes >= 1 && lanes <= 4, "Cannot map %d lanes", lanes);
|
||||
if (first == IRREG_INVALID || lanes < 0)
|
||||
return -1;
|
||||
|
||||
// Let's see if it's already mapped or we need a new reg.
|
||||
IRNativeReg nreg = mr[first].nReg;
|
||||
if (mr[first].isStatic) {
|
||||
_assert_msg_(nreg != -1, "MapIRReg on static without an nReg?");
|
||||
} else {
|
||||
switch (mr[first].loc) {
|
||||
case MIPSLoc::REG_IMM:
|
||||
case MIPSLoc::REG_AS_PTR:
|
||||
case MIPSLoc::REG:
|
||||
if (type != MIPSLoc::REG)
|
||||
nreg = AllocateReg(type);
|
||||
break;
|
||||
|
||||
case MIPSLoc::FREG:
|
||||
if (type != MIPSLoc::FREG)
|
||||
nreg = AllocateReg(type);
|
||||
break;
|
||||
|
||||
case MIPSLoc::VREG:
|
||||
if (type != MIPSLoc::VREG)
|
||||
nreg = AllocateReg(type);
|
||||
break;
|
||||
|
||||
case MIPSLoc::IMM:
|
||||
case MIPSLoc::MEM:
|
||||
nreg = AllocateReg(type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nreg != -1) {
|
||||
// This will handle already mapped and new mappings.
|
||||
MapNativeReg(type, nreg, first, lanes, flags);
|
||||
}
|
||||
|
||||
return nreg;
|
||||
}
|
||||
|
||||
void IRNativeRegCacheBase::MapNativeReg(MIPSLoc type, IRNativeReg nreg, IRReg first, int lanes, MIPSMap flags) {
|
||||
pendingFlush_ = true;
|
||||
|
||||
// First, try to clean up any lane mismatches.
|
||||
// It must either be in the same nreg and lane count, or not in an nreg.
|
||||
for (int i = 0; i < lanes; ++i) {
|
||||
auto &mreg = mr[first + i];
|
||||
if (mreg.nReg != -1) {
|
||||
// How many lanes is it currently in?
|
||||
int oldlanes = 0;
|
||||
for (IRReg m = nr[mreg.nReg].mipsReg; mr[m].nReg == mreg.nReg && m < IRREG_INVALID; ++m)
|
||||
oldlanes++;
|
||||
|
||||
// We may need to flush if it goes outside or we're initing.
|
||||
bool mismatch = oldlanes != lanes || mreg.lane != (lanes == 1 ? -1 : i);
|
||||
if (mismatch) {
|
||||
_assert_msg_(!mreg.isStatic, "Cannot MapNativeReg a static reg mismatch");
|
||||
if ((flags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
// If we need init, we have to flush mismatches.
|
||||
// TODO: Do a shuffle if interior only?
|
||||
// TODO: We may also be motivated to have multiple read-only "views" or an IRReg.
|
||||
// For example Vec4Scale v0..v3, v0..v3, v3
|
||||
FlushNativeReg(mreg.nReg);
|
||||
} else if (oldlanes != 1) {
|
||||
// Even if we don't care about the current contents, we can't discard outside.
|
||||
bool extendsBefore = mreg.lane > i;
|
||||
bool extendsAfter = i + oldlanes - mreg.lane > lanes;
|
||||
if (extendsBefore || extendsAfter)
|
||||
FlushNativeReg(mreg.nReg);
|
||||
}
|
||||
}
|
||||
|
||||
// If it's still in a different reg, either discard or possibly transfer.
|
||||
if (mreg.nReg != -1 && mreg.nReg != nreg) {
|
||||
_assert_msg_(!mreg.isStatic, "Cannot MapNativeReg a static reg to a new reg");
|
||||
if ((flags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
// We better not be trying to map to a different nreg if it's in one now.
|
||||
// This might happen on some sort of transfer...
|
||||
// TODO: Make a direct transfer, i.e. FREG -> VREG?
|
||||
FlushNativeReg(mreg.nReg);
|
||||
} else {
|
||||
DiscardNativeReg(mreg.nReg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If somehow this is an imm and mapping to a multilane native reg (HI/LO?), we store it.
|
||||
// TODO: Could check the others are imm and be smarter, but seems an unlikely case.
|
||||
if (mreg.loc == MIPSLoc::IMM && lanes > 1) {
|
||||
if ((flags & MIPSMap::NOINIT) != MIPSMap::NOINIT)
|
||||
StoreRegValue(first + i, mreg.imm);
|
||||
mreg.loc = MIPSLoc::MEM;
|
||||
if (!mreg.isStatic)
|
||||
mreg.nReg = -1;
|
||||
mreg.imm = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Double check: everything should be in the same loc for multilane now.
|
||||
for (int i = 1; i < lanes; ++i) {
|
||||
_assert_(mr[first + i].loc == mr[first].loc);
|
||||
}
|
||||
|
||||
bool markDirty = (flags & MIPSMap::DIRTY) == MIPSMap::DIRTY;
|
||||
if (mr[first].nReg != nreg) {
|
||||
nr[nreg].isDirty = markDirty;
|
||||
nr[nreg].pointerified = false;
|
||||
nr[nreg].normalized32 = (flags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32;
|
||||
}
|
||||
|
||||
// Alright, now to actually map.
|
||||
if ((flags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
if (first == MIPS_REG_ZERO) {
|
||||
_assert_msg_(lanes == 1, "Cannot use MIPS_REG_ZERO in multilane");
|
||||
SetNativeRegValue(nreg, 0);
|
||||
mr[first].loc = MIPSLoc::REG_IMM;
|
||||
mr[first].imm = 0;
|
||||
} else {
|
||||
// Note: we checked above, everything is in the same loc if multilane.
|
||||
switch (mr[first].loc) {
|
||||
case MIPSLoc::IMM:
|
||||
_assert_msg_(lanes == 1, "Not handling multilane imm here");
|
||||
SetNativeRegValue(nreg, mr[first].imm);
|
||||
// IMM is always dirty unless static.
|
||||
if (!mr[first].isStatic)
|
||||
nr[nreg].isDirty = true;
|
||||
|
||||
// If we are mapping dirty, it means we're gonna overwrite.
|
||||
// So the imm value is no longer valid.
|
||||
if ((flags & MIPSMap::DIRTY) == MIPSMap::DIRTY)
|
||||
mr[first].loc = MIPSLoc::REG;
|
||||
else
|
||||
mr[first].loc = MIPSLoc::REG_IMM;
|
||||
break;
|
||||
|
||||
case MIPSLoc::REG_IMM:
|
||||
// If it's not dirty, we can keep it.
|
||||
_assert_msg_(type == MIPSLoc::REG, "Should have flushed this reg already");
|
||||
if ((flags & MIPSMap::DIRTY) == MIPSMap::DIRTY || lanes != 1)
|
||||
mr[first].loc = MIPSLoc::REG;
|
||||
for (int i = 1; i < lanes; ++i)
|
||||
mr[first + i].loc = type;
|
||||
break;
|
||||
|
||||
case MIPSLoc::REG_AS_PTR:
|
||||
_assert_msg_(lanes == 1, "Should have flushed before getting here");
|
||||
_assert_msg_(type == MIPSLoc::REG, "Should have flushed this reg already");
|
||||
AdjustNativeRegAsPtr(nreg, false);
|
||||
for (int i = 0; i < lanes; ++i)
|
||||
mr[first + i].loc = type;
|
||||
break;
|
||||
|
||||
case MIPSLoc::REG:
|
||||
case MIPSLoc::FREG:
|
||||
case MIPSLoc::VREG:
|
||||
// Might be flipping from FREG -> VREG or something.
|
||||
_assert_msg_(type == mr[first].loc, "Should have flushed this reg already");
|
||||
for (int i = 0; i < lanes; ++i)
|
||||
mr[first + i].loc = type;
|
||||
break;
|
||||
|
||||
case MIPSLoc::MEM:
|
||||
for (int i = 0; i < lanes; ++i)
|
||||
mr[first + i].loc = type;
|
||||
LoadNativeReg(nreg, first, lanes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < lanes; ++i)
|
||||
mr[first + i].loc = type;
|
||||
}
|
||||
|
||||
for (int i = 0; i < lanes; ++i) {
|
||||
mr[first + i].nReg = nreg;
|
||||
mr[first + i].lane = lanes == 1 ? -1 : i;
|
||||
}
|
||||
|
||||
nr[nreg].mipsReg = first;
|
||||
|
||||
if (markDirty) {
|
||||
nr[nreg].isDirty = true;
|
||||
nr[nreg].pointerified = false;
|
||||
nr[nreg].normalized32 = (flags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32;
|
||||
_assert_(first != MIPS_REG_ZERO);
|
||||
} else if ((flags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32) {
|
||||
nr[nreg].normalized32 = true;
|
||||
}
|
||||
}
|
||||
|
||||
void IRNativeRegCacheBase::AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) {
|
||||
// This isn't necessary to implement if REG_AS_PTR is unsupported entirely.
|
||||
_assert_msg_(false, "AdjustNativeRegAsPtr unimplemented");
|
||||
|
||||
@@ -77,6 +77,20 @@ private:
|
||||
IRWriter *ir_;
|
||||
};
|
||||
|
||||
// Initing is the default so the flag is reversed.
|
||||
enum class MIPSMap {
|
||||
INIT = 0,
|
||||
DIRTY = 1,
|
||||
NOINIT = 2 | DIRTY,
|
||||
MARK_NORM32 = 4,
|
||||
};
|
||||
static inline MIPSMap operator |(const MIPSMap &lhs, const MIPSMap &rhs) {
|
||||
return MIPSMap((int)lhs | (int)rhs);
|
||||
}
|
||||
static inline MIPSMap operator &(const MIPSMap &lhs, const MIPSMap &rhs) {
|
||||
return MIPSMap((int)lhs & (int)rhs);
|
||||
}
|
||||
|
||||
class IRNativeRegCacheBase {
|
||||
protected:
|
||||
enum class MIPSLoc {
|
||||
@@ -90,7 +104,7 @@ protected:
|
||||
REG_IMM,
|
||||
// In a native floating-point reg.
|
||||
FREG,
|
||||
// In a native vector reg.
|
||||
// In a native vector reg. Note: if FREGs and VREGS overlap, just use FREG.
|
||||
VREG,
|
||||
// Away in memory (in the mips context struct.)
|
||||
MEM,
|
||||
@@ -183,7 +197,17 @@ protected:
|
||||
virtual void DiscardNativeReg(IRNativeReg nreg);
|
||||
virtual void FlushNativeReg(IRNativeReg nreg);
|
||||
virtual void AdjustNativeRegAsPtr(IRNativeReg nreg, bool state);
|
||||
virtual void MapNativeReg(MIPSLoc type, IRNativeReg nreg, IRReg first, int lanes, MIPSMap flags);
|
||||
virtual IRNativeReg MapNativeReg(MIPSLoc type, IRReg first, int lanes, MIPSMap flags);
|
||||
|
||||
// Load data from memory (possibly multiple lanes) into a native reg.
|
||||
virtual void LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) = 0;
|
||||
// Store data in a native reg back into memory.
|
||||
virtual void StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) = 0;
|
||||
// Set a native reg to a specific integer value.
|
||||
virtual void SetNativeRegValue(IRNativeReg nreg, uint32_t imm) = 0;
|
||||
// Store the imm value for a reg to memory (not currently in a native reg.)
|
||||
virtual void StoreRegValue(IRReg mreg, uint32_t imm) = 0;
|
||||
|
||||
void SetSpillLockIRIndex(IRReg reg, IRReg reg2, IRReg reg3, IRReg reg4, int offset, int index);
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ RiscVGen::RiscVReg RiscVRegCache::Normalize32(IRReg mipsReg, RiscVGen::RiscVReg
|
||||
if (destReg == INVALID_REG) {
|
||||
// If we can pointerify, SEXT_W will be enough.
|
||||
if (!jo_->enablePointerify)
|
||||
emit_->SUB((RiscVReg)mr[mipsReg].nReg, (RiscVReg)mr[mipsReg].nReg, MEMBASEREG);
|
||||
AdjustNativeRegAsPtr(mr[mipsReg].nReg, false);
|
||||
emit_->SEXT_W((RiscVReg)mr[mipsReg].nReg, (RiscVReg)mr[mipsReg].nReg);
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
nr[mr[mipsReg].nReg].normalized32 = true;
|
||||
@@ -188,76 +188,6 @@ RiscVGen::RiscVReg RiscVRegCache::Normalize32(IRReg mipsReg, RiscVGen::RiscVReg
|
||||
return destReg == INVALID_REG ? reg : destReg;
|
||||
}
|
||||
|
||||
void RiscVRegCache::SetRegImm(RiscVReg reg, u64 imm) {
|
||||
_dbg_assert_(reg != R_ZERO || imm == 0);
|
||||
_dbg_assert_(reg >= X0 && reg <= X31);
|
||||
// TODO: Could optimize this more for > 32 bit constants.
|
||||
emit_->LI(reg, imm);
|
||||
_dbg_assert_(!nr[reg].pointerified);
|
||||
nr[reg].normalized32 = imm == (u64)(s64)(s32)imm;
|
||||
}
|
||||
|
||||
void RiscVRegCache::MapRegTo(RiscVReg reg, IRReg mipsReg, MIPSMap mapFlags) {
|
||||
_dbg_assert_(reg > X0 && reg <= X31);
|
||||
_dbg_assert_(IsValidGPR(mipsReg));
|
||||
_dbg_assert_(!mr[mipsReg].isStatic);
|
||||
if (mr[mipsReg].isStatic) {
|
||||
ERROR_LOG(JIT, "Cannot MapRegTo static register %d", mipsReg);
|
||||
return;
|
||||
}
|
||||
nr[reg].isDirty = (mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY;
|
||||
if ((mapFlags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
if (mipsReg == MIPS_REG_ZERO) {
|
||||
// If we get a request to load the zero register, at least we won't spend
|
||||
// time on a memory access...
|
||||
emit_->LI(reg, 0);
|
||||
|
||||
// This way, if we SetImm() it, we'll keep it.
|
||||
mr[mipsReg].loc = MIPSLoc::REG_IMM;
|
||||
mr[mipsReg].imm = 0;
|
||||
nr[reg].normalized32 = true;
|
||||
} else {
|
||||
switch (mr[mipsReg].loc) {
|
||||
case MIPSLoc::MEM:
|
||||
emit_->LW(reg, CTXREG, GetMipsRegOffset(mipsReg));
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
nr[reg].normalized32 = true;
|
||||
break;
|
||||
case MIPSLoc::IMM:
|
||||
SetRegImm(reg, mr[mipsReg].imm);
|
||||
// IMM is always dirty.
|
||||
nr[reg].isDirty = true;
|
||||
|
||||
// If we are mapping dirty, it means we're gonna overwrite.
|
||||
// So the imm value is no longer valid.
|
||||
if ((mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY)
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
else
|
||||
mr[mipsReg].loc = MIPSLoc::REG_IMM;
|
||||
break;
|
||||
case MIPSLoc::REG_IMM:
|
||||
// If it's not dirty, we can keep it.
|
||||
if (nr[reg].isDirty)
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
break;
|
||||
default:
|
||||
_assert_msg_(mr[mipsReg].loc != MIPSLoc::REG_AS_PTR, "MapRegTo with a pointer?");
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_dbg_assert_(mipsReg != MIPS_REG_ZERO);
|
||||
_dbg_assert_(nr[reg].isDirty);
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
}
|
||||
nr[reg].mipsReg = mipsReg;
|
||||
nr[reg].pointerified = false;
|
||||
if (nr[reg].isDirty)
|
||||
nr[reg].normalized32 = (mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32;
|
||||
mr[mipsReg].nReg = reg;
|
||||
}
|
||||
|
||||
RiscVReg RiscVRegCache::TryMapTempImm(IRReg r) {
|
||||
_dbg_assert_(IsValidGPR(r));
|
||||
// If already mapped, no need for a temporary.
|
||||
@@ -293,103 +223,14 @@ RiscVReg RiscVRegCache::GetAndLockTempR() {
|
||||
RiscVReg RiscVRegCache::MapReg(IRReg mipsReg, MIPSMap mapFlags) {
|
||||
_dbg_assert_(IsValidGPR(mipsReg));
|
||||
|
||||
// TODO: Optimization to force HI/LO to be combined?
|
||||
|
||||
if (mipsReg == IRREG_INVALID) {
|
||||
ERROR_LOG(JIT, "Cannot map invalid register");
|
||||
return INVALID_REG;
|
||||
}
|
||||
|
||||
RiscVReg riscvReg = (RiscVReg)mr[mipsReg].nReg;
|
||||
|
||||
if (mr[mipsReg].isStatic) {
|
||||
_dbg_assert_(riscvReg != INVALID_REG);
|
||||
if (riscvReg == INVALID_REG) {
|
||||
ERROR_LOG(JIT, "MapReg on statically mapped reg %d failed - riscvReg got lost", mipsReg);
|
||||
}
|
||||
if (mr[mipsReg].loc == MIPSLoc::IMM) {
|
||||
// Back into the register, with or without the imm value.
|
||||
// If noinit, the MAP_DIRTY check below will take care of the rest.
|
||||
if ((mapFlags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
// This may set normalized32 to true.
|
||||
SetRegImm(riscvReg, mr[mipsReg].imm);
|
||||
mr[mipsReg].loc = MIPSLoc::REG_IMM;
|
||||
nr[riscvReg].pointerified = false;
|
||||
}
|
||||
if ((mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32)
|
||||
nr[riscvReg].normalized32 = true;
|
||||
} else if (mr[mipsReg].loc == MIPSLoc::REG_AS_PTR) {
|
||||
// Was mapped as pointer, now we want it mapped as a value, presumably to
|
||||
// add or subtract stuff to it.
|
||||
if ((mapFlags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
_dbg_assert_(!nr[riscvReg].isDirty && (mapFlags & MIPSMap::DIRTY) != MIPSMap::DIRTY);
|
||||
#endif
|
||||
emit_->SUB(riscvReg, riscvReg, MEMBASEREG);
|
||||
}
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
nr[riscvReg].normalized32 = false;
|
||||
}
|
||||
// Erasing the imm on dirty (necessary since otherwise we will still think it's ML_RVREG_IMM and return
|
||||
// true for IsImm and calculate crazily wrong things). /unknown
|
||||
if ((mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY) {
|
||||
// As we are dirty, can't keep RVREG_IMM, we will quickly drift out of sync
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
nr[riscvReg].pointerified = false;
|
||||
nr[riscvReg].isDirty = true;
|
||||
nr[riscvReg].normalized32 = (mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32;
|
||||
} else if ((mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32) {
|
||||
nr[riscvReg].normalized32 = true;
|
||||
}
|
||||
return (RiscVReg)mr[mipsReg].nReg;
|
||||
}
|
||||
|
||||
// Let's see if it's already mapped. If so we just need to update the dirty flag.
|
||||
// We don't need to check for ML_NOINIT because we assume that anyone who maps
|
||||
// with that flag immediately writes a "known" value to the register.
|
||||
if (mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM) {
|
||||
_dbg_assert_(riscvReg != INVALID_REG && nr[riscvReg].mipsReg == mipsReg);
|
||||
if (nr[riscvReg].mipsReg != mipsReg) {
|
||||
ERROR_LOG_REPORT(JIT, "Register mapping out of sync! %i", mipsReg);
|
||||
}
|
||||
if ((mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY) {
|
||||
// Mapping dirty means the old imm value is invalid.
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
nr[riscvReg].isDirty = true;
|
||||
// If reg is written to, pointerification is lost.
|
||||
nr[riscvReg].pointerified = false;
|
||||
nr[riscvReg].normalized32 = (mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32;
|
||||
} else if ((mapFlags & MIPSMap::MARK_NORM32) == MIPSMap::MARK_NORM32) {
|
||||
nr[riscvReg].normalized32 = true;
|
||||
}
|
||||
|
||||
return (RiscVReg)mr[mipsReg].nReg;
|
||||
} else if (mr[mipsReg].loc == MIPSLoc::REG_AS_PTR) {
|
||||
// Was mapped as pointer, now we want it mapped as a value, presumably to
|
||||
// add or subtract stuff to it.
|
||||
if ((mapFlags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
_dbg_assert_(!nr[riscvReg].isDirty && (mapFlags & MAP_DIRTY) == 0);
|
||||
#endif
|
||||
emit_->SUB(riscvReg, riscvReg, MEMBASEREG);
|
||||
}
|
||||
mr[mipsReg].loc = MIPSLoc::REG;
|
||||
if ((mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY) {
|
||||
nr[riscvReg].isDirty = true;
|
||||
}
|
||||
// Let's always set this false, the SUB won't normalize.
|
||||
nr[riscvReg].normalized32 = false;
|
||||
return (RiscVReg)mr[mipsReg].nReg;
|
||||
}
|
||||
|
||||
// Okay, not mapped, so we need to allocate an RV register.
|
||||
RiscVReg reg = (RiscVReg)AllocateReg(MIPSLoc::REG);
|
||||
if (reg != INVALID_REG) {
|
||||
// Grab it, and load the value into it (if requested).
|
||||
MapRegTo(reg, mipsReg, mapFlags);
|
||||
}
|
||||
|
||||
return reg;
|
||||
IRNativeReg nreg = MapNativeReg(MIPSLoc::REG, mipsReg, 1, mapFlags);
|
||||
return (RiscVReg)nreg;
|
||||
}
|
||||
|
||||
RiscVReg RiscVRegCache::MapRegAsPointer(IRReg reg) {
|
||||
@@ -413,10 +254,10 @@ RiscVReg RiscVRegCache::MapRegAsPointer(IRReg reg) {
|
||||
if (!jo_->enablePointerify) {
|
||||
// Convert to a pointer by adding the base and clearing off the top bits.
|
||||
// If SP, we can probably avoid the top bit clear, let's play with that later.
|
||||
AddMemBase(riscvReg);
|
||||
AdjustNativeRegAsPtr(mr[reg].nReg, true);
|
||||
mr[reg].loc = MIPSLoc::REG_AS_PTR;
|
||||
} else if (!nr[riscvReg].pointerified) {
|
||||
AddMemBase(riscvReg);
|
||||
AdjustNativeRegAsPtr(mr[reg].nReg, true);
|
||||
nr[riscvReg].pointerified = true;
|
||||
}
|
||||
nr[riscvReg].normalized32 = false;
|
||||
@@ -426,28 +267,6 @@ RiscVReg RiscVRegCache::MapRegAsPointer(IRReg reg) {
|
||||
return riscvReg;
|
||||
}
|
||||
|
||||
void RiscVRegCache::AddMemBase(RiscVGen::RiscVReg reg) {
|
||||
_assert_(reg >= X0 && reg <= X31);
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
// This destroys the value...
|
||||
_dbg_assert_(!nr[reg].isDirty);
|
||||
emit_->SLLIW(reg, reg, 2);
|
||||
emit_->SRLIW(reg, reg, 2);
|
||||
emit_->ADD(reg, reg, MEMBASEREG);
|
||||
#else
|
||||
// Clear the top bits to be safe.
|
||||
if (cpu_info.RiscV_Zba) {
|
||||
emit_->ADD_UW(reg, reg, MEMBASEREG);
|
||||
} else {
|
||||
_assert_(XLEN == 64);
|
||||
emit_->SLLI(reg, reg, 32);
|
||||
emit_->SRLI(reg, reg, 32);
|
||||
emit_->ADD(reg, reg, MEMBASEREG);
|
||||
}
|
||||
#endif
|
||||
nr[reg].normalized32 = false;
|
||||
}
|
||||
|
||||
void RiscVRegCache::MapIn(IRReg rs) {
|
||||
MapReg(rs);
|
||||
}
|
||||
@@ -503,8 +322,26 @@ void RiscVRegCache::MapDirtyDirtyInIn(IRReg rd1, IRReg rd2, IRReg rs, IRReg rt,
|
||||
|
||||
void RiscVRegCache::AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) {
|
||||
RiscVReg r = (RiscVReg)(X0 + nreg);
|
||||
_assert_(r >= X0 && r <= X31);
|
||||
if (state) {
|
||||
AddMemBase(r);
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
// This destroys the value...
|
||||
_dbg_assert_(!nr[nreg].isDirty);
|
||||
emit_->SLLIW(r, r, 2);
|
||||
emit_->SRLIW(r, r, 2);
|
||||
emit_->ADD(r, r, MEMBASEREG);
|
||||
#else
|
||||
// Clear the top bits to be safe.
|
||||
if (cpu_info.RiscV_Zba) {
|
||||
emit_->ADD_UW(r, r, MEMBASEREG);
|
||||
} else {
|
||||
_assert_(XLEN == 64);
|
||||
emit_->SLLI(r, r, 32);
|
||||
emit_->SRLI(r, r, 32);
|
||||
emit_->ADD(r, r, MEMBASEREG);
|
||||
}
|
||||
#endif
|
||||
nr[nreg].normalized32 = false;
|
||||
} else {
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
_dbg_assert_(!nr[nreg].isDirty);
|
||||
@@ -514,6 +351,21 @@ void RiscVRegCache::AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) {
|
||||
}
|
||||
}
|
||||
|
||||
void RiscVRegCache::LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
|
||||
RiscVReg r = (RiscVReg)(X0 + nreg);
|
||||
_dbg_assert_(r > X0 && r <= X31);
|
||||
_dbg_assert_(first != MIPS_REG_ZERO);
|
||||
// Multilane not yet supported.
|
||||
_assert_(lanes == 1 || (lanes == 2 && first == IRREG_LO));
|
||||
if (lanes == 1)
|
||||
emit_->LW(r, CTXREG, GetMipsRegOffset(first));
|
||||
else if (lanes == 2)
|
||||
emit_->LD(r, CTXREG, GetMipsRegOffset(first));
|
||||
else
|
||||
_assert_(false);
|
||||
nr[nreg].normalized32 = true;
|
||||
}
|
||||
|
||||
void RiscVRegCache::StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
|
||||
RiscVReg r = (RiscVReg)(X0 + nreg);
|
||||
_dbg_assert_(r > X0 && r <= X31);
|
||||
@@ -529,6 +381,44 @@ void RiscVRegCache::StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
|
||||
_assert_(false);
|
||||
}
|
||||
|
||||
void RiscVRegCache::SetNativeRegValue(IRNativeReg nreg, uint32_t imm) {
|
||||
RiscVReg r = (RiscVReg)(X0 + nreg);
|
||||
if (r == R_ZERO && imm == 0)
|
||||
return;
|
||||
_dbg_assert_(r > X0 && r <= X31);
|
||||
emit_->LI(r, (int32_t)imm);
|
||||
|
||||
// We always use 32-bit immediates, so this is normalized now.
|
||||
nr[nreg].normalized32 = true;
|
||||
}
|
||||
|
||||
void RiscVRegCache::StoreRegValue(IRReg mreg, uint32_t imm) {
|
||||
_assert_(mreg != MIPS_REG_ZERO);
|
||||
// Try to optimize using a different reg.
|
||||
RiscVReg storeReg = INVALID_REG;
|
||||
|
||||
// Zero is super easy.
|
||||
if (imm == 0) {
|
||||
storeReg = R_ZERO;
|
||||
} else {
|
||||
// Could we get lucky? Check for an exact match in another rvreg.
|
||||
for (int i = 0; i < TOTAL_MAPPABLE_IRREGS; ++i) {
|
||||
if (mr[i].loc == MIPSLoc::REG_IMM && mr[i].imm == imm) {
|
||||
// Awesome, let's just store this reg.
|
||||
storeReg = (RiscVReg)mr[i].nReg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (storeReg == INVALID_REG) {
|
||||
emit_->LI(SCRATCH1, imm);
|
||||
storeReg = SCRATCH1;
|
||||
}
|
||||
}
|
||||
|
||||
emit_->SW(storeReg, CTXREG, GetMipsRegOffset(mreg));
|
||||
}
|
||||
|
||||
void RiscVRegCache::DiscardR(IRReg mipsReg) {
|
||||
_dbg_assert_(IsValidGPRNoZero(mipsReg));
|
||||
if (mr[mipsReg].isStatic) {
|
||||
@@ -545,41 +435,6 @@ void RiscVRegCache::DiscardR(IRReg mipsReg) {
|
||||
}
|
||||
}
|
||||
|
||||
RiscVReg RiscVRegCache::RiscVRegForFlush(IRReg r) {
|
||||
_dbg_assert_(IsValidGPR(r));
|
||||
if (mr[r].isStatic)
|
||||
return INVALID_REG; // No flushing needed
|
||||
|
||||
switch (mr[r].loc) {
|
||||
case MIPSLoc::IMM:
|
||||
if (r == MIPS_REG_ZERO) {
|
||||
return INVALID_REG;
|
||||
}
|
||||
// Zero is super easy.
|
||||
if (mr[r].imm == 0) {
|
||||
return R_ZERO;
|
||||
}
|
||||
// Could we get lucky? Check for an exact match in another rvreg.
|
||||
for (int i = 0; i < TOTAL_MAPPABLE_IRREGS; ++i) {
|
||||
if (mr[i].loc == MIPSLoc::REG_IMM && mr[i].imm == mr[r].imm) {
|
||||
// Awesome, let's just store this reg.
|
||||
return (RiscVReg)mr[i].nReg;
|
||||
}
|
||||
}
|
||||
return INVALID_REG;
|
||||
|
||||
case MIPSLoc::REG:
|
||||
case MIPSLoc::REG_IMM:
|
||||
case MIPSLoc::REG_AS_PTR:
|
||||
case MIPSLoc::MEM:
|
||||
return INVALID_REG;
|
||||
|
||||
default:
|
||||
ERROR_LOG_REPORT(JIT, "RiscVRegForFlush: MipsReg %d with invalid location %d", r, (int)mr[r].loc);
|
||||
return INVALID_REG;
|
||||
}
|
||||
}
|
||||
|
||||
void RiscVRegCache::FlushR(IRReg r) {
|
||||
_dbg_assert_(IsValidGPRNoZero(r));
|
||||
if (mr[r].isStatic) {
|
||||
@@ -590,15 +445,8 @@ void RiscVRegCache::FlushR(IRReg r) {
|
||||
switch (mr[r].loc) {
|
||||
case MIPSLoc::IMM:
|
||||
// IMM is always "dirty".
|
||||
// TODO: HI/LO optimization?
|
||||
if (r != MIPS_REG_ZERO) {
|
||||
// Try to optimize using a different reg.
|
||||
RiscVReg storeReg = RiscVRegForFlush(r);
|
||||
if (storeReg == INVALID_REG) {
|
||||
SetRegImm(SCRATCH1, mr[r].imm);
|
||||
storeReg = SCRATCH1;
|
||||
}
|
||||
emit_->SW(storeReg, CTXREG, GetMipsRegOffset(r));
|
||||
StoreRegValue(r, mr[r].imm);
|
||||
mr[r].loc = MIPSLoc::MEM;
|
||||
mr[r].nReg = (int)INVALID_REG;
|
||||
mr[r].imm = -1;
|
||||
@@ -636,10 +484,10 @@ void RiscVRegCache::FlushAll() {
|
||||
IRReg mipsReg = IRReg(i);
|
||||
if (mr[i].isStatic) {
|
||||
RiscVReg riscvReg = (RiscVReg)mr[i].nReg;
|
||||
// Cannot leave any IMMs in registers, not even ML_ARMREG_IMM, can confuse the regalloc later if this flush is mid-block
|
||||
// Cannot leave any IMMs in registers, not even MIPSLoc::REG_IMM, can confuse the regalloc later if this flush is mid-block
|
||||
// due to an interpreter fallback that changes the register.
|
||||
if (mr[i].loc == MIPSLoc::IMM) {
|
||||
SetRegImm((RiscVReg)mr[i].nReg, mr[i].imm);
|
||||
SetNativeRegValue(mr[i].nReg, mr[i].imm);
|
||||
mr[i].loc = MIPSLoc::REG;
|
||||
nr[riscvReg].pointerified = false;
|
||||
} else if (mr[i].loc == MIPSLoc::REG_IMM) {
|
||||
@@ -650,10 +498,7 @@ void RiscVRegCache::FlushAll() {
|
||||
}
|
||||
mr[i].loc = MIPSLoc::REG;
|
||||
} else if (mr[i].loc == MIPSLoc::REG_AS_PTR) {
|
||||
#ifdef MASKED_PSP_MEMORY
|
||||
_dbg_assert_(!nr[riscvReg].isDirty);
|
||||
#endif
|
||||
emit_->SUB(riscvReg, riscvReg, MEMBASEREG);
|
||||
AdjustNativeRegAsPtr(mr[i].nReg, false);
|
||||
mr[i].loc = MIPSLoc::REG;
|
||||
}
|
||||
if (i != MIPS_REG_ZERO && mr[i].nReg == INVALID_REG) {
|
||||
@@ -671,7 +516,7 @@ void RiscVRegCache::FlushAll() {
|
||||
if (allocs[i].pointerified && !nr[allocs[i].nr].pointerified && jo_->enablePointerify) {
|
||||
// Re-pointerify
|
||||
_dbg_assert_(mr[allocs[i].mr].loc == MIPSLoc::REG);
|
||||
AddMemBase((RiscVReg)allocs[i].nr);
|
||||
AdjustNativeRegAsPtr(allocs[i].nr, true);
|
||||
nr[allocs[i].nr].pointerified = true;
|
||||
} else if (!allocs[i].pointerified) {
|
||||
// If this register got pointerified on the way, mark it as not.
|
||||
|
||||
@@ -35,20 +35,6 @@ const RiscVGen::RiscVReg MEMBASEREG = RiscVGen::X27;
|
||||
const RiscVGen::RiscVReg SCRATCH1 = RiscVGen::X10;
|
||||
const RiscVGen::RiscVReg SCRATCH2 = RiscVGen::X11;
|
||||
|
||||
// Initing is the default so the flag is reversed.
|
||||
enum class MIPSMap {
|
||||
INIT = 0,
|
||||
DIRTY = 1,
|
||||
NOINIT = 2 | DIRTY,
|
||||
MARK_NORM32 = 4,
|
||||
};
|
||||
static inline MIPSMap operator |(const MIPSMap &lhs, const MIPSMap &rhs) {
|
||||
return MIPSMap((int)lhs | (int)rhs);
|
||||
}
|
||||
static inline MIPSMap operator &(const MIPSMap &lhs, const MIPSMap &rhs) {
|
||||
return MIPSMap((int)lhs & (int)rhs);
|
||||
}
|
||||
|
||||
enum class MapType {
|
||||
AVOID_LOAD,
|
||||
AVOID_LOAD_MARK_NORM32,
|
||||
@@ -67,7 +53,7 @@ public:
|
||||
RiscVGen::RiscVReg TryMapTempImm(IRReg);
|
||||
|
||||
// Returns an ARM register containing the requested MIPS register.
|
||||
RiscVGen::RiscVReg MapReg(IRReg reg, RiscVJitConstants::MIPSMap mapFlags = RiscVJitConstants::MIPSMap::INIT);
|
||||
RiscVGen::RiscVReg MapReg(IRReg reg, MIPSMap mapFlags = MIPSMap::INIT);
|
||||
RiscVGen::RiscVReg MapRegAsPointer(IRReg reg);
|
||||
|
||||
bool IsNormalized32(IRReg reg);
|
||||
@@ -99,13 +85,13 @@ protected:
|
||||
const StaticAllocation *GetStaticAllocations(int &count) const override;
|
||||
const int *GetAllocationOrder(MIPSLoc type, int &count, int &base) const override;
|
||||
void AdjustNativeRegAsPtr(IRNativeReg nreg, bool state) override;
|
||||
|
||||
void LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) override;
|
||||
void StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) override;
|
||||
void SetNativeRegValue(IRNativeReg nreg, uint32_t imm) override;
|
||||
void StoreRegValue(IRReg mreg, uint32_t imm) override;
|
||||
|
||||
private:
|
||||
void MapRegTo(RiscVGen::RiscVReg reg, IRReg mipsReg, RiscVJitConstants::MIPSMap mapFlags);
|
||||
RiscVGen::RiscVReg RiscVRegForFlush(IRReg r);
|
||||
void SetRegImm(RiscVGen::RiscVReg reg, u64 imm);
|
||||
void AddMemBase(RiscVGen::RiscVReg reg);
|
||||
int GetMipsRegOffset(IRReg r);
|
||||
|
||||
RiscVGen::RiscVEmitter *emit_ = nullptr;
|
||||
|
||||
@@ -57,36 +57,10 @@ RiscVReg RiscVRegCacheFPU::MapReg(IRReg mipsReg, MIPSMap mapFlags) {
|
||||
_dbg_assert_(IsValidFPR(mipsReg));
|
||||
_dbg_assert_(mr[mipsReg + 32].loc == MIPSLoc::MEM || mr[mipsReg + 32].loc == MIPSLoc::FREG);
|
||||
|
||||
pendingFlush_ = true;
|
||||
|
||||
// Let's see if it's already mapped. If so we just need to update the dirty flag.
|
||||
// We don't need to check for NOINIT because we assume that anyone who maps
|
||||
// with that flag immediately writes a "known" value to the register.
|
||||
if (mr[mipsReg + 32].loc == MIPSLoc::FREG) {
|
||||
_assert_msg_(nr[mr[mipsReg + 32].nReg].mipsReg == mipsReg + 32, "FPR mapping out of sync, IR=%i", mipsReg);
|
||||
if ((mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY) {
|
||||
nr[mr[mipsReg + 32].nReg].isDirty = true;
|
||||
}
|
||||
return (RiscVReg)(mr[mipsReg + 32].nReg + F0);
|
||||
}
|
||||
|
||||
// Okay, not mapped, so we need to allocate an RV register.
|
||||
IRNativeReg nreg = AllocateReg(MIPSLoc::FREG);
|
||||
if (nreg != -1) {
|
||||
// That means it's free. Grab it, and load the value into it (if requested).
|
||||
nr[nreg].isDirty = (mapFlags & MIPSMap::DIRTY) == MIPSMap::DIRTY;
|
||||
if ((mapFlags & MIPSMap::NOINIT) != MIPSMap::NOINIT) {
|
||||
if (mr[mipsReg + 32].loc == MIPSLoc::MEM) {
|
||||
emit_->FL(32, (RiscVReg)(F0 + nreg), CTXREG, GetMipsRegOffset(mipsReg + 32));
|
||||
}
|
||||
}
|
||||
nr[nreg].mipsReg = mipsReg + 32;
|
||||
mr[mipsReg + 32].loc = MIPSLoc::FREG;
|
||||
mr[mipsReg + 32].nReg = nreg;
|
||||
IRNativeReg nreg = MapNativeReg(MIPSLoc::FREG, mipsReg + 32, 1, mapFlags);
|
||||
if (nreg != -1)
|
||||
return (RiscVReg)(F0 + nreg);
|
||||
}
|
||||
|
||||
return (RiscVReg)(F0 + nreg);
|
||||
return INVALID_REG;
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::MapInIn(IRReg rd, IRReg rs) {
|
||||
@@ -163,6 +137,18 @@ RiscVReg RiscVRegCacheFPU::Map4DirtyInTemp(IRReg rdbase, IRReg rsbase, bool avoi
|
||||
return temp;
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
|
||||
RiscVReg r = (RiscVReg)(F0 + nreg);
|
||||
_dbg_assert_(r >= F0 && r <= F31);
|
||||
// Multilane not yet supported.
|
||||
_assert_(lanes == 1);
|
||||
if (mr[first].loc == MIPSLoc::FREG) {
|
||||
emit_->FL(32, r, CTXREG, GetMipsRegOffset(first));
|
||||
} else {
|
||||
_assert_msg_(mr[first].loc == MIPSLoc::FREG, "Cannot store this type: %d", (int)mr[first].loc);
|
||||
}
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) {
|
||||
RiscVReg r = (RiscVReg)(F0 + nreg);
|
||||
_dbg_assert_(r >= F0 && r <= F31);
|
||||
@@ -175,6 +161,14 @@ void RiscVRegCacheFPU::StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes)
|
||||
}
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::SetNativeRegValue(IRNativeReg nreg, uint32_t imm) {
|
||||
_assert_msg_(false, "Set float to imm is unsupported");
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::StoreRegValue(IRReg mreg, uint32_t imm) {
|
||||
_assert_msg_(false, "Storing imms to floats is unsupported");
|
||||
}
|
||||
|
||||
void RiscVRegCacheFPU::FlushR(IRReg r) {
|
||||
_dbg_assert_(IsValidFPR(r));
|
||||
switch (mr[r + 32].loc) {
|
||||
@@ -226,7 +220,7 @@ void RiscVRegCacheFPU::FlushAll() {
|
||||
|
||||
if (nr[a].isDirty) {
|
||||
_assert_(m != MIPS_REG_INVALID);
|
||||
emit_->FS(32, (RiscVReg)(F0 + a), CTXREG, GetMipsRegOffset(m));
|
||||
StoreNativeReg(a, m, 1);
|
||||
|
||||
mr[m].loc = MIPSLoc::MEM;
|
||||
mr[m].nReg = (int)INVALID_REG;
|
||||
|
||||
@@ -32,7 +32,7 @@ public:
|
||||
void Init(RiscVGen::RiscVEmitter *emitter);
|
||||
|
||||
// Returns a RISC-V register containing the requested MIPS register.
|
||||
RiscVGen::RiscVReg MapReg(IRReg reg, RiscVJitConstants::MIPSMap mapFlags = RiscVJitConstants::MIPSMap::INIT);
|
||||
RiscVGen::RiscVReg MapReg(IRReg reg, MIPSMap mapFlags = MIPSMap::INIT);
|
||||
|
||||
void MapInIn(IRReg rd, IRReg rs);
|
||||
void MapDirtyIn(IRReg rd, IRReg rs, bool avoidLoad = true);
|
||||
@@ -51,7 +51,11 @@ public:
|
||||
protected:
|
||||
void SetupInitialRegs() override;
|
||||
const int *GetAllocationOrder(MIPSLoc type, int &count, int &base) const override;
|
||||
|
||||
void LoadNativeReg(IRNativeReg nreg, IRReg first, int lanes) override;
|
||||
void StoreNativeReg(IRNativeReg nreg, IRReg first, int lanes) override;
|
||||
void SetNativeRegValue(IRNativeReg nreg, uint32_t imm) override;
|
||||
void StoreRegValue(IRReg mreg, uint32_t imm) override;
|
||||
|
||||
private:
|
||||
int GetMipsRegOffset(IRReg r);
|
||||
|
||||
Reference in new issue
Block a user