Shortcut zero page register moves

This commit is contained in:
drmortalwombat 2022-02-20 14:12:01 +01:00
parent 46e1caaa4a
commit 8b63d5bb34
5 changed files with 153 additions and 1 deletions

View File

@ -40,6 +40,8 @@ void vic_setmode(VicMode mode, char * text, char * font)
vic.ctrl1 = VIC_CTRL1_BMM | VIC_CTRL1_DEN | VIC_CTRL1_RSEL | 3; vic.ctrl1 = VIC_CTRL1_BMM | VIC_CTRL1_DEN | VIC_CTRL1_RSEL | 3;
vic.ctrl2 = VIC_CTRL2_CSEL | VIC_CTRL2_MCM; vic.ctrl2 = VIC_CTRL2_CSEL | VIC_CTRL2_MCM;
break; break;
default:
__assume(false)
} }
cia2.pra = (cia2.pra & 0xfc) | (((unsigned)text >> 14) ^ 0x03); cia2.pra = (cia2.pra & 0xfc) | (((unsigned)text >> 14) ^ 0x03);

View File

@ -10704,6 +10704,73 @@ bool NativeCodeBasicBlock::MoveAbsoluteLoadStoreUp(int at)
return false; return false;
} }
bool NativeCodeBasicBlock::ReplaceZeroPageUp(int at)
{
int i = at - 1;
while (i >= 0)
{
if ((mIns[i].mType == ASMIT_STA || mIns[i].mType == ASMIT_STX || mIns[i].mType == ASMIT_STY) && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == mIns[at].mAddress)
{
while (i < at)
{
if (mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == mIns[at].mAddress)
mIns[i].mAddress = mIns[at + 1].mAddress;
i++;
}
mIns[at + 0].mType = ASMIT_NOP; mIns[at + 0].mMode = ASMIM_IMPLIED;
mIns[at + 1].mType = ASMIT_NOP; mIns[at + 1].mMode = ASMIM_IMPLIED;
return true;
}
if (mIns[i].mType == ASMIT_JSR)
return false;
if (mIns[i].ChangesZeroPage(mIns[at + 1].mAddress))
return false;
if (mIns[i].UsesZeroPage(mIns[at + 1].mAddress))
return false;
if (mIns[i].mMode == ASMIM_INDIRECT_Y && (mIns[i].mAddress == mIns[at].mAddress || mIns[i].mAddress + 1 == mIns[at].mAddress))
return false;
i--;
}
return false;
}
bool NativeCodeBasicBlock::MoveLoadXUp(int at)
{
int i = at - 1;
while (i >= 0)
{
if (mIns[i].mType == ASMIT_STA && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == mIns[at].mAddress)
{
mIns[i].mType = ASMIT_TAX;
mIns[i].mMode = ASMIM_IMPLIED;
mIns[at].mType = ASMIT_NOP;
mIns[at].mMode = ASMIM_IMPLIED;
while (i < at)
{
mIns[i].mLive |= LIVE_CPU_REG_X;
i++;
}
return true;
}
if (mIns[i].ChangesXReg() || (mIns[i].mLive & LIVE_CPU_REG_X) || mIns[i].UsesZeroPage(mIns[at].mAddress))
return false;
i--;
}
return false;
}
bool NativeCodeBasicBlock::MoveStoreXUp(int at) bool NativeCodeBasicBlock::MoveStoreXUp(int at)
{ {
bool done = false; bool done = false;
@ -11283,6 +11350,68 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc
return true; return true;
} }
if (sz >= 3 && mIns[0].mType == ASMIT_LDX && mIns[sz - 2].mType == ASMIT_LDA && mIns[0].SameEffectiveAddress(mIns[sz - 2]) &&
mIns[sz - 1].mType == ASMIT_CMP && HasAsmInstructionMode(ASMIT_CPX, mIns[sz - 1].mMode) && !(mIns[sz - 1].mLive & LIVE_CPU_REG_A))
{
if (!prevBlock)
return OptimizeSimpleLoopInvariant(proc);
mIns[sz - 2].mType = ASMIT_LDX;
mIns[sz - 1].mType = ASMIT_CPX;
prevBlock->mIns.Push(mIns[0]);
mIns.Remove(0);
return true;
}
if (sz >= 2 && mIns[0].mType == ASMIT_LDY && mIns[0].mMode == ASMIM_ZERO_PAGE)
{
int i = mIns.Size() - 1;
while (i > 0 && !mIns[i].ChangesYReg() && !mIns[i].ChangesZeroPage(mIns[0].mAddress))
i--;
if (i > 0 &&
(mIns[i].mType == ASMIT_LDY && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == mIns[0].mAddress) ||
(mIns[i].mType == ASMIT_TAY && (mIns[i - 1].mType == ASMIT_LDA || mIns[i - 1].mType == ASMIT_STA) && mIns[i - 1].mMode == ASMIM_ZERO_PAGE && mIns[i - 1].mAddress == mIns[0].mAddress))
{
if (!prevBlock)
return OptimizeSimpleLoopInvariant(proc);
while (i < mIns.Size())
{
mIns[i].mLive |= LIVE_CPU_REG_Y;
i++;
}
prevBlock->mIns.Push(mIns[0]);
mIns.Remove(0);
return true;
}
}
if (sz >= 2 && mIns[0].mType == ASMIT_LDX && mIns[0].mMode == ASMIM_ZERO_PAGE)
{
int i = mIns.Size() - 1;
while (i > 0 && !mIns[i].ChangesXReg() && !mIns[i].ChangesZeroPage(mIns[0].mAddress))
i--;
if (i > 0 &&
(mIns[i].mType == ASMIT_LDX && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i].mAddress == mIns[0].mAddress) ||
(mIns[i].mType == ASMIT_TAX && (mIns[i - 1].mType == ASMIT_LDA || mIns[i - 1].mType == ASMIT_STA) && mIns[i - 1].mMode == ASMIM_ZERO_PAGE && mIns[i - 1].mAddress == mIns[0].mAddress))
{
if (!prevBlock)
return OptimizeSimpleLoopInvariant(proc);
while (i < mIns.Size())
{
mIns[i].mLive |= LIVE_CPU_REG_X;
i++;
}
prevBlock->mIns.Push(mIns[0]);
mIns.Remove(0);
return true;
}
}
int ai = 0; int ai = 0;
while (ai < mIns.Size() && !mIns[ai].ChangesAccu()) while (ai < mIns.Size() && !mIns[ai].ChangesAccu())
@ -13033,6 +13162,20 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass)
} }
#endif #endif
#if 1
// replace zero page up
for (int i = 1; i + 1 < mIns.Size(); i++)
{
if (mIns[i].mType == ASMIT_LDA && mIns[i].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && !(mIns[i + 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)) && !(mIns[i + 0].mLive & LIVE_MEM))
{
if (ReplaceZeroPageUp(i))
changed = true;
}
}
#endif
#if 1 #if 1
// move load store pairs up to initial store // move load store pairs up to initial store
@ -13214,6 +13357,11 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass)
if (MoveStoreXUp(i)) if (MoveStoreXUp(i))
changed = true; changed = true;
} }
else if (mIns[i].mType == ASMIT_LDX && mIns[i].mMode == ASMIM_ZERO_PAGE && !(mIns[i].mLive & LIVE_MEM))
{
if (MoveLoadXUp(i))
changed = true;
}
} }
#endif #endif

View File

@ -227,9 +227,11 @@ public:
bool FindExternAddressSumY(int at, int reg, int& breg, int& ireg); bool FindExternAddressSumY(int at, int reg, int& breg, int& ireg);
bool FindPageStartAddress(int at, int reg, int& addr); bool FindPageStartAddress(int at, int reg, int& addr);
bool MoveStoreXUp(int at); bool MoveStoreXUp(int at);
bool MoveLoadXUp(int at);
bool MoveStoreHighByteDown(int at); bool MoveStoreHighByteDown(int at);
bool MoveAddHighByteDown(int at); bool MoveAddHighByteDown(int at);
bool ReverseLoadCommutativeOpUp(int aload, int aop); bool ReverseLoadCommutativeOpUp(int aload, int aop);
bool ReplaceZeroPageUp(int at);
bool FindImmediateStore(int at, int reg, const NativeCodeInstruction*& ains); bool FindImmediateStore(int at, int reg, const NativeCodeInstruction*& ains);

View File

@ -833,7 +833,7 @@ int main(void)
// Build to multicolor highres at top of screen // Build to multicolor highres at top of screen
rirq_build(&top, 3); rirq_build(&top, 3);
rirq_delay(&top, 9); rirq_delay(&top, 10);
rirq_write(&top, 1, &vic.ctrl1, VIC_CTRL1_BMM | VIC_CTRL1_DEN | VIC_CTRL1_RSEL | 3); rirq_write(&top, 1, &vic.ctrl1, VIC_CTRL1_BMM | VIC_CTRL1_DEN | VIC_CTRL1_RSEL | 3);
rirq_write(&top, 2, &vic.memptr, 0x28); rirq_write(&top, 2, &vic.memptr, 0x28);
rirq_set(0, 57, &top); rirq_set(0, 57, &top);

Binary file not shown.