Fix infinite loop in compiler trying to optimize infinite loops

This commit is contained in:
drmortalwombat 2023-04-04 08:37:26 +02:00
parent 62c3c4ab90
commit 7803e2ecc4
2 changed files with 49 additions and 11 deletions

View File

@ -4261,7 +4261,8 @@ void InterCodeBasicBlock::GenerateTraces(bool expand, bool compact)
mVisited = true; mVisited = true;
mInPath = true; mInPath = true;
for (;;) // Limit number of contractions
for (int i=0; i<100; i++)
{ {
if (mTrueJump && mTrueJump->mInstructions.Size() == 1 && mTrueJump->mInstructions[0]->mCode == IC_JUMP && !mTrueJump->mLoopHead && mTrueJump->mTraceIndex != mIndex) if (mTrueJump && mTrueJump->mInstructions.Size() == 1 && mTrueJump->mInstructions[0]->mCode == IC_JUMP && !mTrueJump->mLoopHead && mTrueJump->mTraceIndex != mIndex)
{ {

View File

@ -26897,11 +26897,15 @@ bool NativeCodeBasicBlock::ValueForwarding(NativeCodeProcedure* proc, const Nati
target = mTrueJump->mFalseJump; target = mTrueJump->mFalseJump;
} }
mTrueJump->mEntryBlocks.RemoveAll(this); if (target != mTrueJump)
mTrueJump->mNumEntries--; {
target->mEntryBlocks.Push(this); mTrueJump->mEntryBlocks.RemoveAll(this);
target->mNumEntries++; mTrueJump->mNumEntries--;
mTrueJump = target; target->mEntryBlocks.Push(this);
target->mNumEntries++;
mTrueJump = target;
changed = true;
}
} }
} }
else if (mTrueJump->mIns[0].mType == ASMIT_ORA && mTrueJump->mIns[0].mAddress == 0) else if (mTrueJump->mIns[0].mType == ASMIT_ORA && mTrueJump->mIns[0].mAddress == 0)
@ -26925,11 +26929,15 @@ bool NativeCodeBasicBlock::ValueForwarding(NativeCodeProcedure* proc, const Nati
target = mTrueJump->mFalseJump; target = mTrueJump->mFalseJump;
} }
mTrueJump->mEntryBlocks.RemoveAll(this); if (target != mTrueJump)
mTrueJump->mNumEntries--; {
target->mEntryBlocks.Push(this); mTrueJump->mEntryBlocks.RemoveAll(this);
target->mNumEntries++; mTrueJump->mNumEntries--;
mTrueJump = target; target->mEntryBlocks.Push(this);
target->mNumEntries++;
mTrueJump = target;
changed = true;
}
} }
} }
} }
@ -27278,6 +27286,33 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc
} }
} }
#endif #endif
for (int i = 0; i + 1 < mIns.Size(); i++)
{
if (mIns[i + 0].mType == ASMIT_LDA && (mIns[i + 0].mMode == ASMIM_IMMEDIATE || mIns[i + 0].mMode == ASMIM_IMMEDIATE_ADDRESS) &&
mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE)
{
int reg = mIns[i + 1].mAddress;
if (!ReferencesZeroPage(reg, 0, i) && !ChangesZeroPage(reg, i + 2))
{
if (!prevBlock)
return OptimizeSimpleLoopInvariant(proc, full);
if (!mEntryRequiredRegs[CPU_REG_A])
{
prevBlock->mIns.Push(mIns[i + 0]);
prevBlock->mIns.Push(mIns[i + 1]);
mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED;
prevBlock->mExitRequiredRegs += reg;
mEntryRequiredRegs += reg;
mExitRequiredRegs += reg;
changed = true;
}
}
}
}
if (sz >= 3 && mIns[0].mType == ASMIT_LDA && mIns[sz - 2].mType == ASMIT_LDA && mIns[0].SameEffectiveAddress(mIns[sz - 2]) && mIns[sz - 1].mType == ASMIT_CMP) if (sz >= 3 && mIns[0].mType == ASMIT_LDA && mIns[sz - 2].mType == ASMIT_LDA && mIns[0].SameEffectiveAddress(mIns[sz - 2]) && mIns[sz - 1].mType == ASMIT_CMP)
{ {
if (!prevBlock) if (!prevBlock)
@ -38786,7 +38821,9 @@ void NativeCodeProcedure::Optimize(void)
{ {
ResetVisited(); ResetVisited();
if (mEntryBlock->GlobalValueForwarding(this, step == 8)) if (mEntryBlock->GlobalValueForwarding(this, step == 8))
{
changed = true; changed = true;
}
} }
#endif #endif
} }