diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp index 9eea3be026..1cfe9fe1f3 100644 --- a/Core/MIPS/IR/IRFrontend.cpp +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -32,7 +32,7 @@ namespace MIPSComp { IRFrontend::IRFrontend(bool startDefaultPrefix) { - js.startDefaultPrefix = true; + js.startDefaultPrefix = startDefaultPrefix; js.hasSetRounding = false; // js.currentRoundingFunc = convertS0ToSCRATCH1[0]; diff --git a/Core/MIPS/IR/IRPassSimplify.cpp b/Core/MIPS/IR/IRPassSimplify.cpp index 46a182c8a1..2687a83ade 100644 --- a/Core/MIPS/IR/IRPassSimplify.cpp +++ b/Core/MIPS/IR/IRPassSimplify.cpp @@ -440,9 +440,25 @@ bool PropagateConstants(const IRWriter &in, IRWriter &out, const IROptions &opts goto doDefault; case IROp::Sub: + if (gpr.IsImm(inst.src1) && gpr.GetImm(inst.src1) == 0 && !gpr.IsImm(inst.src2)) { + // Morph into a Neg. + gpr.MapDirtyIn(inst.dest, inst.src2); + out.Write(IROp::Neg, inst.dest, inst.src2); + break; + } else if (inst.src1 == inst.src2) { + // Seen sometimes, yet another way of producing zero. + gpr.SetImm(inst.dest, 0); + break; + } +#if __cplusplus >= 201703 || _MSC_VER > 1910 + [[fallthrough]]; +#endif case IROp::Slt: case IROp::SltU: - symmetric = false; // fallthrough + symmetric = false; +#if __cplusplus >= 201703 || _MSC_VER > 1910 + [[fallthrough]]; +#endif case IROp::Add: case IROp::And: case IROp::Or: diff --git a/Core/MIPS/IR/IRRegCache.cpp b/Core/MIPS/IR/IRRegCache.cpp index 3d6f6f0891..30b4131aea 100644 --- a/Core/MIPS/IR/IRRegCache.cpp +++ b/Core/MIPS/IR/IRRegCache.cpp @@ -160,16 +160,17 @@ bool IRNativeRegCacheBase::IsFPRMapped(IRReg fpr) { } int IRNativeRegCacheBase::GetFPRLaneCount(IRReg fpr) { - if (!IsFPRMapped(fpr) || mr[fpr].lane > 0) + if (!IsFPRMapped(fpr) || mr[fpr + 32].lane > 0) return 0; - if (mr[fpr].lane == -1) + if (mr[fpr + 32].lane == -1) return 1; + IRReg base = fpr + 32 - mr[fpr + 32].lane; int c = 1; for (int i = 1; i < 4; ++i) { - if (mr[fpr + i].nReg != mr[fpr].nReg || mr[fpr + i].loc != mr[fpr].loc) + if (mr[base + i].nReg != mr[base].nReg || mr[base + i].loc != mr[base].loc) return c; - if (mr[fpr + i].lane != i) + if (mr[base + i].lane != i) return c; c++; diff --git a/Core/MIPS/x86/X64IRAsm.cpp b/Core/MIPS/x86/X64IRAsm.cpp index cbbfff09dc..2876be3f77 100644 --- a/Core/MIPS/x86/X64IRAsm.cpp +++ b/Core/MIPS/x86/X64IRAsm.cpp @@ -52,16 +52,31 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { BeginWrite(GetMemoryProtectPageSize()); const u8 *start = AlignCodePage(); + jo.downcountInRegister = false; +#if PPSSPP_ARCH(AMD64) + bool jitbaseInR15 = false; + uintptr_t jitbase = (uintptr_t)GetBasePtr(); + if (jitbase > 0x7FFFFFFFULL) { + jo.reserveR15ForAsm = true; + jitbaseInR15 = true; + } else { + jo.downcountInRegister = true; + jo.reserveR15ForAsm = true; + } +#endif + if (jo.useStaticAlloc && false) { saveStaticRegisters_ = AlignCode16(); - //MOV(32, MDisp(CTXREG, downcountOffset), R(DOWNCOUNTREG)); + if (jo.downcountInRegister) + MOV(32, MDisp(CTXREG, downcountOffset), R(DOWNCOUNTREG)); //regs_.EmitSaveStaticRegisters(); RET(); // Note: needs to not modify EAX, or to save it if it does. loadStaticRegisters_ = AlignCode16(); //regs_.EmitLoadStaticRegisters(); - //MOV(32, R(DOWNCOUNTREG), MDisp(CTXREG, downcountOffset)); + if (jo.downcountInRegister) + MOV(32, R(DOWNCOUNTREG), MDisp(CTXREG, downcountOffset)); RET(); start = saveStaticRegisters_; @@ -119,11 +134,8 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { #if PPSSPP_ARCH(AMD64) // Two x64-specific statically allocated registers. MOV(64, R(MEMBASEREG), ImmPtr(Memory::base)); - uintptr_t jitbase = (uintptr_t)GetBasePtr(); - if (jitbase > 0x7FFFFFFFULL) { + if (jitbaseInR15) MOV(64, R(JITBASEREG), ImmPtr(GetBasePtr())); - jo.reserveR15ForAsm = true; - } #endif // From the start of the FP reg, a single byte offset can reach all GPR + all FPR (but not VFPR.) MOV(PTRBITS, R(CTXREG), ImmPtr(&mipsState->f[0])); @@ -144,15 +156,18 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { // TODO: See if we can get the slice decrement to line up with IR. if (RipAccessible((const void *)&coreState)) { - CMP(32, M(&coreState), Imm32(0)); // rip accessible + CMP(32, M(&coreState), Imm8(0)); // rip accessible } else { MOV(PTRBITS, R(RAX), ImmPtr((const void *)&coreState)); - CMP(32, MatR(RAX), Imm32(0)); + CMP(32, MatR(RAX), Imm8(0)); } FixupBranch badCoreState = J_CC(CC_NZ, true); - //CMP(32, R(DOWNCOUNTREG), Imm32(0)); - CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + if (jo.downcountInRegister) { + TEST(32, R(DOWNCOUNTREG), R(DOWNCOUNTREG)); + } else { + CMP(32, MDisp(CTXREG, downcountOffset), Imm8(0)); + } J_CC(CC_S, outerLoop_); FixupBranch skipToRealDispatch = J(); @@ -162,8 +177,11 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { hooks_.dispatcher = GetCodePtr(); // TODO: See if we can get the slice decrement to line up with IR. - //CMP(32, R(DOWNCOUNTREG), Imm32(0)); - CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + if (jo.downcountInRegister) { + TEST(32, R(DOWNCOUNTREG), R(DOWNCOUNTREG)); + } else { + CMP(32, MDisp(CTXREG, downcountOffset), Imm8(0)); + } FixupBranch bail = J_CC(CC_S, true); SetJumpTarget(skipToRealDispatch); @@ -172,7 +190,7 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { // Debug if (enableDebug) { #if PPSSPP_ARCH(AMD64) - if (jo.reserveR15ForAsm) + if (jitbaseInR15) ABI_CallFunctionAA(reinterpret_cast(&ShowPC), R(MEMBASEREG), R(JITBASEREG)); else ABI_CallFunctionAC(reinterpret_cast(&ShowPC), R(MEMBASEREG), (u32)jitbase); @@ -192,21 +210,26 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { #elif PPSSPP_ARCH(AMD64) MOV(32, R(SCRATCH1), MComplex(MEMBASEREG, SCRATCH1, SCALE_1, 0)); #endif - MOV(32, R(EDX), R(SCRATCH1)); _assert_msg_(MIPS_JITBLOCK_MASK == 0xFF000000, "Hardcoded assumption of emuhack mask"); - SHR(32, R(EDX), Imm8(24)); - CMP(32, R(EDX), Imm8(MIPS_EMUHACK_OPCODE >> 24)); + if (cpu_info.bBMI2) { + RORX(32, EDX, R(SCRATCH1), 24); + CMP(8, R(EDX), Imm8(MIPS_EMUHACK_OPCODE >> 24)); + } else { + MOV(32, R(EDX), R(SCRATCH1)); + SHR(32, R(EDX), Imm8(24)); + CMP(32, R(EDX), Imm8(MIPS_EMUHACK_OPCODE >> 24)); + } FixupBranch needsCompile = J_CC(CC_NE); // Mask by 0x00FFFFFF and extract the block jit offset. AND(32, R(SCRATCH1), Imm32(MIPS_EMUHACK_VALUE_MASK)); #if PPSSPP_ARCH(X86) - ADD(32, R(SCRATCH1), ImmPtr(GetBasePtr())); + LEA(32, SCRATCH1, MDisp(SCRATCH1, (u32)GetBasePtr())); #elif PPSSPP_ARCH(AMD64) - if (jo.reserveR15ForAsm) { + if (jitbaseInR15) { ADD(64, R(SCRATCH1), R(JITBASEREG)); } else { // See above, reserveR15ForAsm is used when above 0x7FFFFFFF. - ADD(64, R(SCRATCH1), Imm32((u32)jitbase)); + LEA(64, SCRATCH1, MDisp(SCRATCH1, (u32)jitbase)); } #endif JMPptr(R(SCRATCH1)); @@ -222,10 +245,10 @@ void X64JitBackend::GenerateFixedCode(MIPSState *mipsState) { SetJumpTarget(bail); if (RipAccessible((const void *)&coreState)) { - CMP(32, M(&coreState), Imm32(0)); // rip accessible + CMP(32, M(&coreState), Imm8(0)); // rip accessible } else { MOV(PTRBITS, R(RAX), ImmPtr((const void *)&coreState)); - CMP(32, MatR(RAX), Imm32(0)); + CMP(32, MatR(RAX), Imm8(0)); } J_CC(CC_Z, outerLoop_, true); diff --git a/Core/MIPS/x86/X64IRCompALU.cpp b/Core/MIPS/x86/X64IRCompALU.cpp index c592f84ffe..9132ce5b05 100644 --- a/Core/MIPS/x86/X64IRCompALU.cpp +++ b/Core/MIPS/x86/X64IRCompALU.cpp @@ -50,7 +50,9 @@ void X64JitBackend::CompIR_Arith(IRInst inst) { switch (inst.op) { case IROp::Add: regs_.Map(inst); - if (inst.dest == inst.src2) { + if (inst.src1 == inst.src2) { + LEA(32, regs_.RX(inst.dest), MScaled(regs_.RX(inst.src1), 2, 0)); + } else if (inst.dest == inst.src2) { ADD(32, regs_.R(inst.dest), regs_.R(inst.src1)); } else if (inst.dest == inst.src1) { ADD(32, regs_.R(inst.dest), regs_.R(inst.src2)); @@ -61,25 +63,15 @@ void X64JitBackend::CompIR_Arith(IRInst inst) { break; case IROp::Sub: - if (regs_.IsGPRImm(inst.src1) && regs_.GetGPRImm(inst.src1) == 0) { - // TODO: Might be nice to have a pass to turn this into Neg. - // Special cased to avoid wasting a reg on zero. - regs_.SpillLockGPR(inst.dest, inst.src2); - regs_.MapGPR(inst.src2); - regs_.MapGPR(inst.dest, MIPSMap::NOINIT); - if (inst.dest != inst.src2) - MOV(32, regs_.R(inst.dest), regs_.R(inst.src2)); - NEG(32, regs_.R(inst.dest)); + regs_.Map(inst); + if (inst.dest == inst.src2 && inst.dest == inst.src1) { + regs_.SetGPRImm(inst.dest, 0); } else if (inst.dest == inst.src2) { - regs_.Map(inst); - MOV(32, R(SCRATCH1), regs_.R(inst.src2)); - MOV(32, regs_.R(inst.dest), regs_.R(inst.src1)); - SUB(32, regs_.R(inst.dest), R(SCRATCH1)); + NEG(32, regs_.R(inst.src2)); + ADD(32, regs_.R(inst.dest), regs_.R(inst.src1)); } else if (inst.dest == inst.src1) { - regs_.Map(inst); SUB(32, regs_.R(inst.dest), regs_.R(inst.src2)); } else { - regs_.Map(inst); MOV(32, regs_.R(inst.dest), regs_.R(inst.src1)); SUB(32, regs_.R(inst.dest), regs_.R(inst.src2)); } @@ -168,11 +160,11 @@ void X64JitBackend::CompIR_Bits(IRInst inst) { if (cpu_info.bLZCNT) { LZCNT(32, regs_.RX(inst.dest), regs_.R(inst.src1)); } else { - BSR(32, SCRATCH1, regs_.R(inst.src1)); + BSR(32, regs_.RX(inst.dest), regs_.R(inst.src1)); FixupBranch notFound = J_CC(CC_Z); - MOV(32, regs_.R(inst.dest), Imm32(31)); - SUB(32, regs_.R(inst.dest), R(SCRATCH1)); + // Since one of these bits must be set, and none outside, this subtracts from 31. + XOR(32, regs_.R(inst.dest), Imm8(31)); FixupBranch skip = J(); SetJumpTarget(notFound); @@ -192,7 +184,12 @@ void X64JitBackend::CompIR_Compare(IRInst inst) { CONDITIONAL_DISABLE; auto setCC = [&](const OpArg &arg, CCFlags cc) { - if (regs_.HasLowSubregister(regs_.RX(inst.dest)) && inst.dest != inst.src1 && inst.dest != inst.src2) { + // If it's carry, we can take advantage of ADC to avoid subregisters. + if (cc == CC_C && inst.dest != inst.src1 && inst.dest != inst.src2) { + XOR(32, regs_.R(inst.dest), regs_.R(inst.dest)); + CMP(32, regs_.R(inst.src1), arg); + ADC(32, regs_.R(inst.dest), Imm8(0)); + } else if (regs_.HasLowSubregister(regs_.RX(inst.dest)) && inst.dest != inst.src1 && inst.dest != inst.src2) { XOR(32, regs_.R(inst.dest), regs_.R(inst.dest)); CMP(32, regs_.R(inst.src1), arg); SETcc(cc, regs_.R(inst.dest)); diff --git a/Core/MIPS/x86/X64IRCompSystem.cpp b/Core/MIPS/x86/X64IRCompSystem.cpp index 80c5187e74..91d4730773 100644 --- a/Core/MIPS/x86/X64IRCompSystem.cpp +++ b/Core/MIPS/x86/X64IRCompSystem.cpp @@ -46,8 +46,11 @@ void X64JitBackend::CompIR_Basic(IRInst inst) { switch (inst.op) { case IROp::Downcount: - //SUB(32, R(DOWNCOUNTREG), R(DOWNCOUNTREG), Imm32((s32)inst.constant)); - SUB(32, MDisp(CTXREG, downcountOffset), Imm32((s32)inst.constant)); + // As long as we don't care about flags, just use LEA. + if (jo.downcountInRegister) + LEA(32, DOWNCOUNTREG, MDisp(DOWNCOUNTREG, -(s32)inst.constant)); + else + SUB(32, MDisp(CTXREG, downcountOffset), SImmAuto((s32)inst.constant)); break; case IROp::SetConst: diff --git a/Core/MIPS/x86/X64IRJit.cpp b/Core/MIPS/x86/X64IRJit.cpp index b2e6b40056..d905dadb92 100644 --- a/Core/MIPS/x86/X64IRJit.cpp +++ b/Core/MIPS/x86/X64IRJit.cpp @@ -63,8 +63,11 @@ bool X64JitBackend::CompileBlock(IRBlock *block, int block_num, bool preload) { wroteCheckedOffset = true; // TODO: See if we can get flags to always have the downcount compare. - //CMP(32, R(DOWNCOUNTREG), Imm32(0)); - CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + if (jo.downcountInRegister) { + TEST(32, R(DOWNCOUNTREG), R(DOWNCOUNTREG)); + } else { + CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + } FixupBranch normalEntry = J_CC(CC_NS); MOV(32, R(SCRATCH1), Imm32(startPC)); JMP(outerLoopPCInSCRATCH1_, true); @@ -116,8 +119,11 @@ bool X64JitBackend::CompileBlock(IRBlock *block, int block_num, bool preload) { } if (jo.enableBlocklink && jo.useBackJump) { - //CMP(32, R(DOWNCOUNTREG), Imm32(0)); - CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + if (jo.downcountInRegister) { + TEST(32, R(DOWNCOUNTREG), R(DOWNCOUNTREG)); + } else { + CMP(32, MDisp(CTXREG, downcountOffset), Imm32(0)); + } J_CC(CC_NS, blockStart, true); MOV(32, R(SCRATCH1), Imm32(startPC)); @@ -317,17 +323,17 @@ void X64JitBackend::MovToPC(X64Reg r) { void X64JitBackend::SaveStaticRegisters() { if (jo.useStaticAlloc) { //CALL(saveStaticRegisters_); - } else { + } else if (jo.downcountInRegister) { // Inline the single operation - //MOV(32, MDisp(CTXREG, downcountOffset), R(DOWNCOUNTREG)); + MOV(32, MDisp(CTXREG, downcountOffset), R(DOWNCOUNTREG)); } } void X64JitBackend::LoadStaticRegisters() { if (jo.useStaticAlloc) { //CALL(loadStaticRegisters_); - } else { - //MOV(32, R(DOWNCOUNTREG), MDisp(CTXREG, downcountOffset)); + } else if (jo.downcountInRegister) { + MOV(32, R(DOWNCOUNTREG), MDisp(CTXREG, downcountOffset)); } } diff --git a/Core/MIPS/x86/X64IRRegCache.h b/Core/MIPS/x86/X64IRRegCache.h index 023d31fe70..d36463d876 100644 --- a/Core/MIPS/x86/X64IRRegCache.h +++ b/Core/MIPS/x86/X64IRRegCache.h @@ -31,8 +31,10 @@ namespace X64IRJitConstants { const Gen::X64Reg MEMBASEREG = Gen::RBX; const Gen::X64Reg CTXREG = Gen::R14; const Gen::X64Reg JITBASEREG = Gen::R15; +const Gen::X64Reg DOWNCOUNTREG = Gen::R15; #else const Gen::X64Reg CTXREG = Gen::EBP; +const Gen::X64Reg DOWNCOUNTREG = Gen::INVALID_REG; #endif const Gen::X64Reg SCRATCH1 = Gen::EAX;