Skip to content

Commit

Permalink
Fix over eager loop load/store optimizations for values needed after …
Browse files Browse the repository at this point in the history
…loop
  • Loading branch information
drmortalwombat committed Mar 12, 2023
1 parent 7d6f637 commit ecd0fbd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion oscar64/InterCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10984,7 +10984,7 @@ bool InterCodeBasicBlock::SingleTailLoopOptimization(const NumberSet& aliasedPar
{
InterInstruction* lins = mInstructions[i];

if (lins->mCode == IC_LOAD && lins->mSrc[0].mTemp < 0)
if (lins->mCode == IC_LOAD && lins->mSrc[0].mTemp < 0 && !tail->mExitRequiredTemps[lins->mDst.mTemp])
{
if (CanMoveInstructionBeforeBlock(i))
{
Expand Down
81 changes: 80 additions & 1 deletion oscar64/NativeCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23619,6 +23619,78 @@ bool NativeCodeBasicBlock::ReplaceFinalZeroPageUse(NativeCodeProcedure* nproc)
return changed;
}

bool NativeCodeBasicBlock::Propagate16BitHighSum(void)
{
bool changed = false;

if (!mVisited)
{
mVisited = true;

for (int i = 0; i + 5 < mIns.Size(); i++)
{
if (mIns[i + 0].mType == ASMIT_LDA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE &&
mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE &&
mIns[i + 2].mType == ASMIT_CLC &&
mIns[i + 3].mType == ASMIT_LDA && mIns[i + 3].mMode == ASMIM_ZERO_PAGE && mIns[i + 3].mAddress == mIns[i + 0].mAddress + 1 && !(mIns[i + 3].mLive & LIVE_MEM) &&
mIns[i + 4].mType == ASMIT_ADC && mIns[i + 4].mMode == ASMIM_IMMEDIATE &&
mIns[i + 5].mType == ASMIT_STA && mIns[i + 5].mMode == ASMIM_ZERO_PAGE && mIns[i + 5].mAddress == mIns[i + 1].mAddress + 1)
{
bool success = false;

int taddr = mIns[i + 1].mAddress, saddr = mIns[i + 0].mAddress;

int j = i + 6;
while (j < mIns.Size())
{
if (mIns[j].mType == ASMIT_JSR)
break;
else if (mIns[j].ChangesZeroPage(saddr) || mIns[j].ChangesZeroPage(saddr + 1))
break;
else if (mIns[j].ChangesZeroPage(taddr) || mIns[j].ChangesZeroPage(taddr + 1))
break;
else if (mIns[j].UsesZeroPage(taddr + 1) && !(mIns[j].mLive & LIVE_MEM))
{
success = true;
break;
}
j++;
}

if (success)
{
mIns[i + 5].mAddress = saddr + 1;
for (int k = i + 6; k <= j; k++)
{
if (mIns[k].mMode == ASMIM_ZERO_PAGE)
{
if (mIns[k].mAddress == taddr)
mIns[k].mAddress = saddr;
else if (mIns[k].mAddress == taddr + 1)
mIns[k].mAddress = saddr + 1;
}
else if (mIns[k].mMode == ASMIM_INDIRECT_Y)
{
if (mIns[k].mAddress == taddr)
mIns[k].mAddress = saddr;
}
}
changed = true;
}
}
}

if (mTrueJump && mTrueJump->Propagate16BitHighSum())
changed = true;
if (mFalseJump && mFalseJump->Propagate16BitHighSum())
changed = true;
}

return changed;
}



bool NativeCodeBasicBlock::ForwardReplaceZeroPage(int at, int from, int to)
{
bool changed = false;
Expand Down Expand Up @@ -37338,7 +37410,7 @@ void NativeCodeProcedure::RebuildEntry(void)

void NativeCodeProcedure::Optimize(void)
{
CheckFunc = !strcmp(mInterProc->mIdent->mString, "diggers_list");
CheckFunc = !strcmp(mInterProc->mIdent->mString, "tile_draw_p");

#if 1
int step = 0;
Expand Down Expand Up @@ -37847,6 +37919,13 @@ void NativeCodeProcedure::Optimize(void)
mEntryBlock->CheckBlocks();
#endif

if (step == 6 || step == 7)
{
ResetVisited();
if (mEntryBlock->Propagate16BitHighSum())
changed = true;
}


if (step == 8)
{
Expand Down
2 changes: 2 additions & 0 deletions oscar64/NativeCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ class NativeCodeBasicBlock

bool Check16BitSum(int at, NativeRegisterSum16Info& info);
bool Propagate16BitSum(const ExpandingArray<NativeRegisterSum16Info>& cinfo);

bool Propagate16BitHighSum(void);

bool IsFinalZeroPageUse(const NativeCodeBasicBlock* block, int at, int from, int to, bool pair);
bool ReplaceFinalZeroPageUse(NativeCodeProcedure* nproc);
Expand Down

0 comments on commit ecd0fbd

Please sign in to comment.