diff --git a/Core/MIPS/IR/IRCompBranch.cpp b/Core/MIPS/IR/IRCompBranch.cpp index a8bbdc8f9c..c21e2d6b31 100644 --- a/Core/MIPS/IR/IRCompBranch.cpp +++ b/Core/MIPS/IR/IRCompBranch.cpp @@ -54,27 +54,6 @@ using namespace MIPSAnalyst; namespace MIPSComp { -// This seems to be the same for all branch types. -static u32 ResolveNotTakenTarget(u32 pc, const MIPSOpcode &op, bool likely, const MIPSOpcode &delaySlotOp, const MIPSInfo &delaySlotInfo) { - u32 notTakenTarget = pc + 8; - if ((delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0) { - // If a branch has a j/jr/jal/jalr as a delay slot, that is run if the branch is not taken. - // TODO: Technically, in the likely case, we should somehow suppress andLink on this exit. - bool isJump = (delaySlotInfo & IS_JUMP) != 0; - // If the delay slot is a branch, likely skips it. - if (isJump || !likely) - notTakenTarget -= 4; - - // For a branch (not a jump), it actually should try the delay slot and take its target potentially. - // This is similar to the VFPU case and has not been seen, so just report it. - if (!isJump && SignExtend16ToU32(delaySlotOp) != SignExtend16ToU32(op) - 1) - ERROR_LOG_REPORT(JIT, "Branch in branch delay slot at %08x with different target", pc); - if (isJump && likely && (delaySlotInfo & (OUT_RA | OUT_RD)) != 0) - ERROR_LOG_REPORT(JIT, "Jump in likely branch delay slot with link at %08x", pc); - } - return notTakenTarget; -} - void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { if (js.inDelaySlot) { ERROR_LOG_REPORT(JIT, "Branch in RSRTComp delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart); @@ -85,17 +64,16 @@ void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { MIPSGPReg rs = _RS; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - MIPSInfo delaySlotInfo = MIPSGetInfo(delaySlotOp); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - bool delaySlotIsBranch = (delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0; - bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rt, rs); + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); + branchInfo.delaySlotIsNice = IsDelaySlotNiceReg(op, branchInfo.delaySlotOp, rt, rs); + + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); // Often, div/divu are followed by a likely "break" if the divisor was zero. // Stalling is not really useful for us, so we optimize this out. - if (likely && offset == 4 && MIPS_IS_BREAK(delaySlotOp)) { + if (likely && offset == 4 && MIPS_IS_BREAK(branchInfo.delaySlotOp)) { // Okay, let's not actually branch at all. We're done here. - EatInstruction(delaySlotOp); + EatInstruction(branchInfo.delaySlotOp); // Let's not double-count the downcount, though. js.downcountAmount--; return; @@ -103,7 +81,7 @@ void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { MIPSGPReg lhs = rs; MIPSGPReg rhs = rt; - if (!delaySlotIsNice && !likely) { // if likely, we don't need this + if (!branchInfo.delaySlotIsNice && !likely) { // if likely, we don't need this if (rs != 0) { ir.Write(IROp::Mov, IRTEMP_LHS, rs); lhs = (MIPSGPReg)IRTEMP_LHS; @@ -114,9 +92,7 @@ void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { } } - u32 notTakenTarget = ResolveNotTakenTarget(GetCompilerPC(), op, likely, delaySlotOp, delaySlotInfo); - - if (!likely && !delaySlotIsBranch) + if (!likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); int dcAmount = js.downcountAmount; @@ -124,17 +100,17 @@ void IRFrontend::BranchRSRTComp(MIPSOpcode op, IRComparison cc, bool likely) { js.downcountAmount = 0; FlushAll(); - ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), lhs, rhs); + ir.Write(ComparisonToExit(cc), ir.AddConstant(ResolveNotTakenTarget(branchInfo)), lhs, rhs); // This makes the block "impure" :( - if (likely && !delaySlotIsBranch) + if (likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); - if (delaySlotIsBranch) { + if (branchInfo.delaySlotIsBranch) { // We still link when the branch is taken (targetAddr case.) // Remember, it's from the perspective of the delay slot, so +12. - if ((delaySlotInfo & OUT_RA) != 0) + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 12); - if ((delaySlotInfo & OUT_RD) != 0) - ir.WriteSetConstant(MIPS_GET_RD(delaySlotOp), GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + ir.WriteSetConstant(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); } FlushAll(); @@ -154,23 +130,20 @@ void IRFrontend::BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, MIPSGPReg rs = _RS; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - MIPSInfo delaySlotInfo = MIPSGetInfo(delaySlotOp); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - bool delaySlotIsBranch = (delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0; - bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), andLink, likely); + branchInfo.delaySlotIsNice = IsDelaySlotNiceReg(op, branchInfo.delaySlotOp, rs); + + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); MIPSGPReg lhs = rs; - if (!delaySlotIsNice) { // if likely, we don't need this + if (!branchInfo.delaySlotIsNice) { // if likely, we don't need this ir.Write(IROp::Mov, IRTEMP_LHS, rs); lhs = (MIPSGPReg)IRTEMP_LHS; } if (andLink) ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 8); - u32 notTakenTarget = ResolveNotTakenTarget(GetCompilerPC(), op, likely, delaySlotOp, delaySlotInfo); - - if (!likely && !delaySlotIsBranch) + if (!likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); int dcAmount = js.downcountAmount; @@ -178,16 +151,16 @@ void IRFrontend::BranchRSZeroComp(MIPSOpcode op, IRComparison cc, bool andLink, js.downcountAmount = 0; FlushAll(); - ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), lhs); - if (likely && !delaySlotIsBranch) + ir.Write(ComparisonToExit(cc), ir.AddConstant(ResolveNotTakenTarget(branchInfo)), lhs); + if (likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); - if (delaySlotIsBranch) { + if (branchInfo.delaySlotIsBranch) { // We still link when the branch is taken (targetAddr case.) // Remember, it's from the perspective of the delay slot, so +12. - if ((delaySlotInfo & OUT_RA) != 0) + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 12); - if ((delaySlotInfo & OUT_RD) != 0) - ir.WriteSetConstant(MIPS_GET_RD(delaySlotOp), GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + ir.WriteSetConstant(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); } // Taken @@ -245,33 +218,29 @@ void IRFrontend::BranchFPFlag(MIPSOpcode op, IRComparison cc, bool likely) { int offset = TARGET16; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - MIPSInfo delaySlotInfo = MIPSGetInfo(delaySlotOp); - bool delaySlotIsBranch = (delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0; + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); ir.Write(IROp::FpCondToReg, IRTEMP_LHS); - if (!likely && !delaySlotIsBranch) + if (!likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); int dcAmount = js.downcountAmount; ir.Write(IROp::Downcount, 0, ir.AddConstant(dcAmount)); js.downcountAmount = 0; - u32 notTakenTarget = ResolveNotTakenTarget(GetCompilerPC(), op, likely, delaySlotOp, delaySlotInfo); - FlushAll(); // Not taken - ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), IRTEMP_LHS, 0); + ir.Write(ComparisonToExit(cc), ir.AddConstant(ResolveNotTakenTarget(branchInfo)), IRTEMP_LHS, 0); // Taken - if (likely && !delaySlotIsBranch) + if (likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); - if (delaySlotIsBranch) { + if (branchInfo.delaySlotIsBranch) { // We still link when the branch is taken (targetAddr case.) // Remember, it's from the perspective of the delay slot, so +12. - if ((delaySlotInfo & OUT_RA) != 0) + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 12); - if ((delaySlotInfo & OUT_RD) != 0) - ir.WriteSetConstant(MIPS_GET_RD(delaySlotOp), GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + ir.WriteSetConstant(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); } FlushAll(); @@ -303,39 +272,36 @@ void IRFrontend::BranchVFPUFlag(MIPSOpcode op, IRComparison cc, bool likely) { int offset = TARGET16; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - MIPSInfo delaySlotInfo = MIPSGetInfo(delaySlotOp); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); + + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); ir.Write(IROp::VfpuCtrlToReg, IRTEMP_LHS, VFPU_CTRL_CC); // Sometimes there's a VFPU branch in a delay slot (Disgaea 2: Dark Hero Days, Zettai Hero Project, La Pucelle) // The behavior is undefined - the CPU may take the second branch even if the first one passes. // However, it does consistently try each branch, which these games seem to expect. - bool delaySlotIsBranch = (delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0; - if (!likely && !delaySlotIsBranch) + if (!likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); int dcAmount = js.downcountAmount; ir.Write(IROp::Downcount, 0, ir.AddConstant(dcAmount)); js.downcountAmount = 0; - u32 notTakenTarget = ResolveNotTakenTarget(GetCompilerPC(), op, likely, delaySlotOp, delaySlotInfo); - int imm3 = (op >> 18) & 7; ir.Write(IROp::AndConst, IRTEMP_LHS, IRTEMP_LHS, ir.AddConstant(1 << imm3)); FlushAll(); - ir.Write(ComparisonToExit(cc), ir.AddConstant(notTakenTarget), IRTEMP_LHS, 0); + ir.Write(ComparisonToExit(cc), ir.AddConstant(ResolveNotTakenTarget(branchInfo)), IRTEMP_LHS, 0); - if (likely && !delaySlotIsBranch) + if (likely && !branchInfo.delaySlotIsBranch) CompileDelaySlot(); - if (delaySlotIsBranch) { + if (branchInfo.delaySlotIsBranch) { // We still link when the branch is taken (targetAddr case.) // Remember, it's from the perspective of the delay slot, so +12. - if ((delaySlotInfo & OUT_RA) != 0) + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) ir.WriteSetConstant(MIPS_REG_RA, GetCompilerPC() + 12); - if ((delaySlotInfo & OUT_RD) != 0) - ir.WriteSetConstant(MIPS_GET_RD(delaySlotOp), GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + ir.WriteSetConstant(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); } // Taken diff --git a/Core/MIPS/JitCommon/JitCommon.cpp b/Core/MIPS/JitCommon/JitCommon.cpp index 6e5e1e4ee4..f85e7df741 100644 --- a/Core/MIPS/JitCommon/JitCommon.cpp +++ b/Core/MIPS/JitCommon/JitCommon.cpp @@ -22,6 +22,7 @@ #include "ext/disarm.h" #include "ext/udis86/udis86.h" +#include "Common/LogReporting.h" #include "Common/StringUtils.h" #include "Common/Serialize/Serializer.h" #include "Common/Serialize/SerializeFuncs.h" @@ -29,9 +30,11 @@ #include "Core/Util/DisArm64.h" #include "Core/Config.h" +#include "Core/MIPS/IR/IRJit.h" #include "Core/MIPS/JitCommon/JitCommon.h" #include "Core/MIPS/JitCommon/JitState.h" -#include "Core/MIPS/IR/IRJit.h" +#include "Core/MIPS/MIPSCodeUtils.h" +#include "Core/MIPS/MIPSTables.h" #if PPSSPP_ARCH(ARM) #include "../ARM/ArmJit.h" @@ -67,6 +70,32 @@ namespace MIPSComp { } } + BranchInfo::BranchInfo(u32 pc, MIPSOpcode o, MIPSOpcode delayO, bool al, bool l) + : compilerPC(pc), op(o), delaySlotOp(delayO), likely(l), andLink(al) { + delaySlotInfo = MIPSGetInfo(delaySlotOp).value; + delaySlotIsBranch = (delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0; + } + + u32 ResolveNotTakenTarget(const BranchInfo &branchInfo) { + u32 notTakenTarget = branchInfo.compilerPC + 8; + if ((branchInfo.delaySlotInfo & (IS_JUMP | IS_CONDBRANCH)) != 0) { + // If a branch has a j/jr/jal/jalr as a delay slot, that is run if the branch is not taken. + // TODO: Technically, in the likely case, we should somehow suppress andLink on this exit. + bool isJump = (branchInfo.delaySlotInfo & IS_JUMP) != 0; + // If the delay slot is a branch, likely skips it. + if (isJump || !branchInfo.likely) + notTakenTarget -= 4; + + // For a branch (not a jump), it actually should try the delay slot and take its target potentially. + // This is similar to the VFPU case and has not been seen, so just report it. + if (!isJump && SignExtend16ToU32(branchInfo.delaySlotOp) != SignExtend16ToU32(branchInfo.op) - 1) + ERROR_LOG_REPORT(JIT, "Branch in branch delay slot at %08x with different target", branchInfo.compilerPC); + if (isJump && branchInfo.likely && (branchInfo.delaySlotInfo & (OUT_RA | OUT_RD)) != 0) + ERROR_LOG_REPORT(JIT, "Jump in likely branch delay slot with link at %08x", branchInfo.compilerPC); + } + return notTakenTarget; +} + JitInterface *CreateNativeJit(MIPSState *mipsState) { #if PPSSPP_ARCH(ARM) return new MIPSComp::ArmJit(mipsState); diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index 87eee83a9f..f101591154 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -153,6 +153,23 @@ namespace MIPSComp { typedef void (MIPSFrontendInterface::*MIPSCompileFunc)(MIPSOpcode opcode); typedef int (MIPSFrontendInterface::*MIPSReplaceFunc)(); + struct BranchInfo { + BranchInfo(u32 pc, MIPSOpcode op, MIPSOpcode delaySlotOp, bool andLink, bool likely); + + u32 compilerPC; + MIPSOpcode op; + MIPSOpcode delaySlotOp; + u64 delaySlotInfo; + bool likely; + bool andLink; + // Update manually if it's not always nice (rs/rt, rs/zero, etc.) + bool delaySlotIsNice = true; + bool delaySlotIsBranch; + }; + + // This seems to be the same for all branch types. + u32 ResolveNotTakenTarget(const BranchInfo &branchInfo); + extern JitInterface *jit; extern std::recursive_mutex jitLock; diff --git a/Core/MIPS/x86/CompBranch.cpp b/Core/MIPS/x86/CompBranch.cpp index 9bd167d2e2..276c049955 100644 --- a/Core/MIPS/x86/CompBranch.cpp +++ b/Core/MIPS/x86/CompBranch.cpp @@ -63,7 +63,7 @@ using namespace MIPSAnalyst; #define DO_CONDITIONAL_LOG 0 // We can also disable nice delay slots. -// #define CONDITIONAL_NICE_DELAYSLOT delaySlotIsNice = false; +// #define CONDITIONAL_NICE_DELAYSLOT branchInfo.delaySlotIsNice = false; #define CONDITIONAL_NICE_DELAYSLOT ; #if DO_CONDITIONAL_LOG @@ -197,22 +197,22 @@ bool Jit::PredictTakeBranch(u32 targetAddr, bool likely) { return targetAddr > GetCompilerPC(); } -void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink) { - if (andLink) +void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, const BranchInfo &branchInfo) { + if (branchInfo.andLink) gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 8); // We may want to try to continue along this branch a little while, to reduce reg flushing. - bool predictTakeBranch = PredictTakeBranch(targetAddr, likely); - if (CanContinueBranch(predictTakeBranch ? targetAddr : notTakenAddr)) + bool predictTakeBranch = PredictTakeBranch(targetAddr, branchInfo.likely); + if (!branchInfo.delaySlotIsBranch && CanContinueBranch(predictTakeBranch ? targetAddr : notTakenAddr)) { if (predictTakeBranch) cc = FlipCCFlag(cc); Gen::FixupBranch ptr; RegCacheState state; - if (!likely) + if (!branchInfo.likely) { - if (!delaySlotIsNice) + if (!branchInfo.delaySlotIsNice) CompileDelaySlot(DELAYSLOT_SAFE); ptr = J_CC(cc, true); GetStateAndFlushAll(state); @@ -243,7 +243,7 @@ void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool del CONDITIONAL_LOG_EXIT(targetAddr); // Don't forget to run the delay slot if likely. - if (likely) + if (branchInfo.likely) CompileDelaySlot(DELAYSLOT_NICE); AddContinuedBlock(targetAddr); @@ -272,9 +272,9 @@ void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool del else { Gen::FixupBranch ptr; - if (!likely) + if (!branchInfo.likely) { - if (!delaySlotIsNice) + if (!branchInfo.delaySlotIsNice && !branchInfo.delaySlotIsBranch) CompileDelaySlot(DELAYSLOT_SAFE_FLUSH); else FlushAll(); @@ -284,7 +284,19 @@ void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool del { FlushAll(); ptr = J_CC(cc, true); - CompileDelaySlot(DELAYSLOT_FLUSH); + if (!branchInfo.delaySlotIsBranch) + CompileDelaySlot(DELAYSLOT_FLUSH); + } + + // Handle the linkage of a delay slot, even when we're taking the branch. + if (branchInfo.delaySlotIsBranch) { + // We still link when the branch is taken (targetAddr case.) + // Remember, it's from the perspective of the delay slot, so +12. + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) + gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + gpr.SetImm(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); + FlushAll(); } // Take the branch @@ -299,14 +311,25 @@ void Jit::CompBranchExits(CCFlags cc, u32 targetAddr, u32 notTakenAddr, bool del } } -void Jit::CompBranchExit(bool taken, u32 targetAddr, u32 notTakenAddr, bool delaySlotIsNice, bool likely, bool andLink) { +void Jit::CompBranchExit(bool taken, u32 targetAddr, u32 notTakenAddr, const BranchInfo &branchInfo) { // Continuing is handled in the imm branch case... TODO: move it here? - if (andLink) + if (branchInfo.andLink) gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 8); - if (taken || !likely) - CompileDelaySlot(DELAYSLOT_FLUSH); - else + if (branchInfo.delaySlotIsBranch) { + if (taken) { + // We still link when the branch is taken (targetAddr case.) + // Remember, it's from the perspective of the delay slot, so +12. + if ((branchInfo.delaySlotInfo & OUT_RA) != 0) + gpr.SetImm(MIPS_REG_RA, GetCompilerPC() + 12); + if ((branchInfo.delaySlotInfo & OUT_RD) != 0) + gpr.SetImm(MIPS_GET_RD(branchInfo.delaySlotOp), GetCompilerPC() + 12); + } FlushAll(); + } else if (taken || !branchInfo.likely) { + CompileDelaySlot(DELAYSLOT_FLUSH); + } else { + FlushAll(); + } const u32 destAddr = taken ? targetAddr : notTakenAddr; CONDITIONAL_LOG_EXIT(destAddr); @@ -326,9 +349,13 @@ void Jit::BranchRSRTComp(MIPSOpcode op, Gen::CCFlags cc, bool likely) MIPSGPReg rs = _RS; u32 targetAddr = GetCompilerPC() + offset + 4; + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); + branchInfo.delaySlotIsNice = IsDelaySlotNiceReg(op, branchInfo.delaySlotOp, rt, rs); + CONDITIONAL_NICE_DELAYSLOT; + bool immBranch = false; bool immBranchTaken = false; - if (gpr.IsImm(rs) && gpr.IsImm(rt)) { + if (gpr.IsImm(rs) && gpr.IsImm(rt) && !branchInfo.delaySlotIsBranch) { // The cc flags are opposites: when NOT to take the branch. bool immBranchNotTaken; s32 rsImm = (s32)gpr.GetImm(rs); @@ -364,16 +391,14 @@ void Jit::BranchRSRTComp(MIPSOpcode op, Gen::CCFlags cc, bool likely) return; } - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rt, rs); - CONDITIONAL_NICE_DELAYSLOT; + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); + u32 notTakenTarget = ResolveNotTakenTarget(branchInfo); if (immBranch) - CompBranchExit(immBranchTaken, targetAddr, GetCompilerPC() + 8, delaySlotIsNice, likely, false); + CompBranchExit(immBranchTaken, targetAddr, notTakenTarget, branchInfo); else { - if (!likely && delaySlotIsNice) + if (!likely && branchInfo.delaySlotIsNice && !branchInfo.delaySlotIsBranch) CompileDelaySlot(DELAYSLOT_NICE); if (gpr.IsImm(rt) && gpr.GetImm(rt) == 0) @@ -387,7 +412,7 @@ void Jit::BranchRSRTComp(MIPSOpcode op, Gen::CCFlags cc, bool likely) CMP(32, gpr.R(rs), gpr.R(rt)); } - CompBranchExits(cc, targetAddr, GetCompilerPC() + 8, delaySlotIsNice, likely, false); + CompBranchExits(cc, targetAddr, notTakenTarget, branchInfo); } } @@ -402,9 +427,14 @@ void Jit::BranchRSZeroComp(MIPSOpcode op, Gen::CCFlags cc, bool andLink, bool li MIPSGPReg rs = _RS; u32 targetAddr = GetCompilerPC() + offset + 4; + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), andLink, likely); + branchInfo.delaySlotIsNice = IsDelaySlotNiceReg(op, branchInfo.delaySlotOp, rs); + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); + CONDITIONAL_NICE_DELAYSLOT; + bool immBranch = false; bool immBranchTaken = false; - if (gpr.IsImm(rs)) { + if (gpr.IsImm(rs) && !branchInfo.delaySlotIsBranch) { // The cc flags are opposites: when NOT to take the branch. bool immBranchNotTaken; s32 imm = (s32)gpr.GetImm(rs); @@ -446,22 +476,20 @@ void Jit::BranchRSZeroComp(MIPSOpcode op, Gen::CCFlags cc, bool andLink, bool li return; } - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - bool delaySlotIsNice = IsDelaySlotNiceReg(op, delaySlotOp, rs); - CONDITIONAL_NICE_DELAYSLOT; + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); + u32 notTakenTarget = ResolveNotTakenTarget(branchInfo); if (immBranch) - CompBranchExit(immBranchTaken, targetAddr, GetCompilerPC() + 8, delaySlotIsNice, likely, andLink); + CompBranchExit(immBranchTaken, targetAddr, notTakenTarget, branchInfo); else { - if (!likely && delaySlotIsNice) + if (!likely && branchInfo.delaySlotIsNice && !branchInfo.delaySlotIsBranch) CompileDelaySlot(DELAYSLOT_NICE); gpr.MapReg(rs, true, false); CMP(32, gpr.R(rs), Imm32(0)); - CompBranchExits(cc, targetAddr, GetCompilerPC() + 8, delaySlotIsNice, likely, andLink); + CompBranchExits(cc, targetAddr, notTakenTarget, branchInfo); } } @@ -518,17 +546,18 @@ void Jit::BranchFPFlag(MIPSOpcode op, Gen::CCFlags cc, bool likely) int offset = TARGET16; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - bool delaySlotIsNice = IsDelaySlotNiceFPU(op, delaySlotOp); + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); + branchInfo.delaySlotIsNice = IsDelaySlotNiceFPU(op, branchInfo.delaySlotOp); + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); CONDITIONAL_NICE_DELAYSLOT; - if (!likely && delaySlotIsNice) + if (!likely && branchInfo.delaySlotIsNice && !branchInfo.delaySlotIsBranch) CompileDelaySlot(DELAYSLOT_NICE); gpr.KillImmediate(MIPS_REG_FPCOND, true, false); TEST(32, gpr.R(MIPS_REG_FPCOND), Imm32(1)); - CompBranchExits(cc, targetAddr, GetCompilerPC() + 8, delaySlotIsNice, likely, false); + u32 notTakenTarget = ResolveNotTakenTarget(branchInfo); + CompBranchExits(cc, targetAddr, notTakenTarget, branchInfo); } @@ -559,19 +588,16 @@ void Jit::BranchVFPUFlag(MIPSOpcode op, Gen::CCFlags cc, bool likely) int offset = TARGET16; u32 targetAddr = GetCompilerPC() + offset + 4; - MIPSOpcode delaySlotOp = GetOffsetInstruction(1); - js.downcountAmount += MIPSGetInstructionCycleEstimate(delaySlotOp); - + BranchInfo branchInfo(GetCompilerPC(), op, GetOffsetInstruction(1), false, likely); // Sometimes there's a VFPU branch in a delay slot (Disgaea 2: Dark Hero Days, Zettai Hero Project, La Pucelle) // The behavior is undefined - the CPU may take the second branch even if the first one passes. // However, it does consistently try each branch, which these games seem to expect. - bool delaySlotIsBranch = MIPSCodeUtils::IsVFPUBranch(delaySlotOp); - bool delaySlotIsNice = !delaySlotIsBranch && IsDelaySlotNiceVFPU(op, delaySlotOp); + branchInfo.delaySlotIsNice = !branchInfo.delaySlotIsBranch && IsDelaySlotNiceVFPU(op, branchInfo.delaySlotOp); + js.downcountAmount += MIPSGetInstructionCycleEstimate(branchInfo.delaySlotOp); + CONDITIONAL_NICE_DELAYSLOT; - if (!likely && delaySlotIsNice) + if (!likely && branchInfo.delaySlotIsNice && !branchInfo.delaySlotIsBranch) CompileDelaySlot(DELAYSLOT_NICE); - if (delaySlotIsBranch && (signed short)(delaySlotOp & 0xFFFF) != (signed short)(op & 0xFFFF) - 1) - ERROR_LOG_REPORT(JIT, "VFPU branch in VFPU delay slot at %08x with different target %d / %d", GetCompilerPC(), (signed short)(delaySlotOp & 0xFFFF), (signed short)(op & 0xFFFF) - 1); // THE CONDITION int imm3 = (op >> 18) & 7; @@ -579,8 +605,8 @@ void Jit::BranchVFPUFlag(MIPSOpcode op, Gen::CCFlags cc, bool likely) gpr.KillImmediate(MIPS_REG_VFPUCC, true, false); TEST(32, gpr.R(MIPS_REG_VFPUCC), Imm32(1 << imm3)); - u32 notTakenTarget = GetCompilerPC() + (delaySlotIsBranch ? 4 : 8); - CompBranchExits(cc, targetAddr, notTakenTarget, delaySlotIsNice, likely, false); + u32 notTakenTarget = ResolveNotTakenTarget(branchInfo); + CompBranchExits(cc, targetAddr, notTakenTarget, branchInfo); } diff --git a/Core/MIPS/x86/Jit.h b/Core/MIPS/x86/Jit.h index 4cfd12bdf2..0c96188ee4 100644 --- a/Core/MIPS/x86/Jit.h +++ b/Core/MIPS/x86/Jit.h @@ -230,8 +230,8 @@ private: } void CompITypeMemUnpairedLR(MIPSOpcode op, bool isStore); void CompITypeMemUnpairedLRInner(MIPSOpcode op, Gen::X64Reg shiftReg); - void CompBranchExits(Gen::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); + void CompBranchExits(Gen::CCFlags cc, u32 targetAddr, u32 notTakenAddr, const BranchInfo &branchInfo); + void CompBranchExit(bool taken, u32 targetAddr, u32 notTakenAddr, const BranchInfo &branchInfo); static Gen::CCFlags FlipCCFlag(Gen::CCFlags flag); static Gen::CCFlags SwapCCFlag(Gen::CCFlags flag);