diff --git a/Core/MIPS/x86/CompALU.cpp b/Core/MIPS/x86/CompALU.cpp index 14c51e29f5..e2766be633 100644 --- a/Core/MIPS/x86/CompALU.cpp +++ b/Core/MIPS/x86/CompALU.cpp @@ -463,18 +463,37 @@ namespace MIPSComp gpr.SetImm(rd, 0); } else { gpr.Lock(rd, rs, rt); - gpr.MapReg(rs, true, false); gpr.MapReg(rd, rd == rt || rd == rs, true); + // Let's try to avoid loading rs or if it's an imm, flushing it. + MIPSGPReg lhs = rs; + MIPSGPReg rhs = rt; + CCFlags cc = CC_L; + if (gpr.IsImm(lhs)) { + // Hmm. This is a bit confusing. lhs may be ZERO, but its location may be memory. + // TODO: That's probably not a good thing, but changing it breaks more... + gpr.SetImm(lhs, gpr.GetImm(lhs)); + // rhs is guaranteed not to be an imm (handled above.) + std::swap(lhs, rhs); + cc = SwapCCFlag(cc); + } else if (!gpr.R(lhs).CanDoOpWith(gpr.R(rhs))) { + // Let's try to pick which makes more sense to load. + if (MIPSAnalyst::IsRegisterUsed(rhs, js.compilerPC + 4, 3)) { + std::swap(lhs, rhs); + cc = SwapCCFlag(cc); + } + gpr.MapReg(lhs, true, false); + } + bool needsTemp = NeedsTempForSETcc(gpr.RX(rd)) || rd == rt || rd == rs; if (needsTemp) { - CMP(32, gpr.R(rs), gpr.R(rt)); - SETcc(CC_L, R(EAX)); + CMP(32, gpr.R(lhs), gpr.R(rhs)); + SETcc(cc, R(EAX)); MOVZX(32, 8, gpr.RX(rd), R(EAX)); } else { XOR(32, gpr.R(rd), gpr.R(rd)); - CMP(32, gpr.R(rs), gpr.R(rt)); - SETcc(CC_L, gpr.R(rd)); + CMP(32, gpr.R(lhs), gpr.R(rhs)); + SETcc(cc, gpr.R(rd)); } gpr.UnlockAll(); } @@ -487,18 +506,37 @@ namespace MIPSComp gpr.SetImm(rd, 0); } else { gpr.Lock(rd, rs, rt); - gpr.MapReg(rs, true, false); gpr.MapReg(rd, rd == rt || rd == rs, true); + // Let's try to avoid loading rs or if it's an imm, flushing it. + MIPSGPReg lhs = rs; + MIPSGPReg rhs = rt; + CCFlags cc = CC_B; + if (gpr.IsImm(lhs)) { + // Hmm. This is a bit confusing. lhs may be ZERO, but its location may be memory. + // TODO: That's probably not a good thing, but changing it breaks more... + gpr.SetImm(lhs, gpr.GetImm(lhs)); + // rhs is guaranteed not to be an imm (handled above.) + std::swap(lhs, rhs); + cc = SwapCCFlag(cc); + } else if (!gpr.R(lhs).CanDoOpWith(gpr.R(rhs))) { + // Let's try to pick which makes more sense to load. + if (MIPSAnalyst::IsRegisterUsed(rhs, js.compilerPC + 4, 3)) { + std::swap(lhs, rhs); + cc = SwapCCFlag(cc); + } + gpr.MapReg(lhs, true, false); + } + bool needsTemp = NeedsTempForSETcc(gpr.RX(rd)) || rd == rt || rd == rs; if (needsTemp) { - CMP(32, gpr.R(rs), gpr.R(rt)); - SETcc(CC_B, R(EAX)); + CMP(32, gpr.R(lhs), gpr.R(rhs)); + SETcc(cc, R(EAX)); MOVZX(32, 8, gpr.RX(rd), R(EAX)); } else { XOR(32, gpr.R(rd), gpr.R(rd)); - CMP(32, gpr.R(rs), gpr.R(rt)); - SETcc(CC_B, gpr.R(rd)); + CMP(32, gpr.R(lhs), gpr.R(rhs)); + SETcc(cc, gpr.R(rd)); } gpr.UnlockAll(); } diff --git a/Core/MIPS/x86/CompBranch.cpp b/Core/MIPS/x86/CompBranch.cpp index 2d8d104b68..0fe5da98b2 100644 --- a/Core/MIPS/x86/CompBranch.cpp +++ b/Core/MIPS/x86/CompBranch.cpp @@ -130,7 +130,7 @@ void Jit::BranchLogExit(MIPSOpcode op, u32 dest, bool useEAX) SetJumpTarget(skip); } -static CCFlags FlipCCFlag(CCFlags flag) +CCFlags Jit::FlipCCFlag(CCFlags flag) { switch (flag) { @@ -155,6 +155,32 @@ static CCFlags FlipCCFlag(CCFlags flag) return CC_O; } +CCFlags Jit::SwapCCFlag(CCFlags flag) +{ + // This swaps the comparison for an lhs/rhs swap, but doesn't flip/invert the logic. + switch (flag) + { + case CC_O: return CC_O; + case CC_NO: return CC_NO; + case CC_B: return CC_A; + case CC_NB: return CC_NA; + case CC_Z: return CC_Z; + case CC_NZ: return CC_NZ; + case CC_BE: return CC_AE; + case CC_NBE: return CC_NAE; + case CC_S: return CC_S; + case CC_NS: return CC_NS; + case CC_P: return CC_P; + case CC_NP: return CC_NP; + case CC_L: return CC_G; + case CC_NL: return CC_NG; + case CC_LE: return CC_GE; + case CC_NLE: return CC_NGE; + } + ERROR_LOG_REPORT(JIT, "SwapCCFlag: Unexpected CC flag: %d", flag); + return CC_O; +} + bool Jit::PredictTakeBranch(u32 targetAddr, bool likely) { // If it's likely, it's... probably likely, right? if (likely) diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 8a5e75cf92..8ae228a8fa 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -230,6 +230,8 @@ private: void CompITypeMemUnpairedLRInner(MIPSOpcode op, X64Reg shiftReg); void CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink); void CompBranchExit(bool taken, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink); + static CCFlags FlipCCFlag(CCFlags flag); + static CCFlags SwapCCFlag(CCFlags flag); void CompFPTriArith(MIPSOpcode op, void (XEmitter::*arith)(X64Reg reg, OpArg), bool orderMatters); void CompFPComp(int lhs, int rhs, u8 compare, bool allowNaN = false);