diff --git a/Core/MIPS/IR/IRPassSimplify.cpp b/Core/MIPS/IR/IRPassSimplify.cpp index 09dcc97f5f..d93a29632a 100644 --- a/Core/MIPS/IR/IRPassSimplify.cpp +++ b/Core/MIPS/IR/IRPassSimplify.cpp @@ -459,8 +459,9 @@ bool PropagateConstants(const IRWriter &in, IRWriter &out, const IROptions &opts IRImmRegCache gpr(&out); bool logBlocks = false; - bool skipNextExitToConst = false; - for (int i = 0; i < (int)in.GetInstructions().size(); i++) { + // Set once a conditional exit is known to be taken, making the rest of the block dead. + bool unreachable = false; + for (int i = 0; i < (int)in.GetInstructions().size() && !unreachable; i++) { IRInst inst = in.GetInstructions()[i]; bool symmetric = true; switch (inst.op) { @@ -901,7 +902,7 @@ bool PropagateConstants(const IRWriter &in, IRWriter &out, const IROptions &opts if (passed) { gpr.FlushAll(); out.Write(IROp::ExitToConst, 0, 0, 0, inst.constant); - skipNextExitToConst = true; + unreachable = true; } break; } @@ -926,7 +927,7 @@ bool PropagateConstants(const IRWriter &in, IRWriter &out, const IROptions &opts if (passed) { gpr.FlushAll(); out.Write(IROp::ExitToConst, 0, 0, 0, inst.constant); - skipNextExitToConst = true; + unreachable = true; } break; } @@ -935,10 +936,6 @@ bool PropagateConstants(const IRWriter &in, IRWriter &out, const IROptions &opts goto doDefault; case IROp::ExitToConst: - if (skipNextExitToConst) { - skipNextExitToConst = false; - break; - } gpr.FlushAll(); goto doDefault; diff --git a/unittest/TestIRPassSimplify.cpp b/unittest/TestIRPassSimplify.cpp index 4645d80c22..e6ccf1b906 100644 --- a/unittest/TestIRPassSimplify.cpp +++ b/unittest/TestIRPassSimplify.cpp @@ -404,6 +404,21 @@ static const IRVerification tests[] = { }, { &PropagateConstants }, }, + { + // The exit is always taken, so nothing after it runs. + "PropagateConstantsTakenExit", + { + { IROp::SetConst, { MIPS_REG_A0 }, 0, 0, 0 }, + { IROp::ExitToConstIfEq, { 0 }, MIPS_REG_A0, MIPS_REG_ZERO, 0x08804000 }, + { IROp::Downcount, { 0 }, 0, 0, 4 }, + { IROp::ExitToConst, { 0 }, 0, 0, 0x08804100 }, + }, + { + { IROp::SetConst, { MIPS_REG_A0 }, 0, 0, 0 }, + { IROp::ExitToConst, { 0 }, 0, 0, 0x08804000 }, + }, + { &PropagateConstants }, + }, }; bool TestIRPassSimplify() {