From ac7e1d586703eb94baad6b2198d4d484b1dce2e7 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 16 Apr 2023 22:56:58 +0200 Subject: [PATCH] Fix over eager function parameter optimization --- oscar64/NativeCodeGenerator.cpp | 68 +++++++++++++++++++++++++++++--- samples/memmap/charsetload.d64 | Bin 174848 -> 174848 bytes 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index e24bc69..ae8ad29 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -27252,7 +27252,14 @@ bool NativeCodeBasicBlock::RemoveSimpleLoopUnusedIndex(void) if (lo->mZeroPageSet[i]) required += i; } + + for (int i = 0; i < ins.mLinkerObject->mNumTemporaries; i++) + { + for (int j = 0; j < ins.mLinkerObject->mTempSizes[i]; j++) + required += ins.mLinkerObject->mTemporaries[i] + j; + } } + #else for (int i = BC_REG_TMP; i < BC_REG_TMP_SAVED; i++) required += i; @@ -27951,7 +27958,6 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc return true; } } - } @@ -28150,7 +28156,7 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc } } - if (mEntryRequiredRegs.Size() && !mEntryRequiredRegs[CPU_REG_A]) + if (mEntryRequiredRegs.Size() && (!mEntryRequiredRegs[CPU_REG_A] || !mEntryRequiredRegs[CPU_REG_X])) { for (int i = 0; i + 1 < mIns.Size(); i++) { @@ -28166,10 +28172,52 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc { if (!prevBlock) return OptimizeSimpleLoopInvariant(proc, full); - prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); - prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + if (!mEntryRequiredRegs[CPU_REG_A]) + { + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } + else + { + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDX, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STX, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED; } + else if (!(mIns[i + 0].mLive & LIVE_MEM) && !(mIns[i + 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)) && !mExitRequiredRegs[mIns[i + 1].mAddress]) + { + int j = mIns.Size() - 1; + + while (j >= i + 2 && !mIns[j].ReferencesZeroPage(mIns[i + 0].mAddress) && !mIns[j].ReferencesZeroPage(mIns[i + 1].mAddress)) + j--; + + if (j >= i + 2 && mIns[j].mType == ASMIT_STA && mIns[j].mMode == ASMIM_ZERO_PAGE && mIns[j].mAddress == mIns[i + 0].mAddress) + { + if (!prevBlock) + return OptimizeSimpleLoopInvariant(proc, full); + + mIns[j].mAddress = mIns[i + 1].mAddress; + + if (!mEntryRequiredRegs[CPU_REG_A]) + { + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } + else + { + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDX, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); + prevBlock->mIns.Push(NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STX, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } + + prevBlock->mExitRequiredRegs += mIns[i + 1].mAddress; + mEntryRequiredRegs += mIns[i + 1].mAddress; + mExitRequiredRegs += mIns[i + 1].mAddress; + exitBlock->mIns.Insert(0, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + exitBlock->mIns.Insert(1, NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 0].mAddress)); + mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED; + changed = true; + } + } } else if (mIns[i + 0].mType == ASMIT_LDA && mIns[i + 0].mMode == ASMIM_IMMEDIATE && mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE) @@ -28187,8 +28235,16 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc while (k > 0 && (prevBlock->mIns[k - 1].mLive && LIVE_CPU_REG_A)) k--; - prevBlock->mIns.Insert(k, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_IMMEDIATE, mIns[i + 0].mAddress)); - prevBlock->mIns.Insert(k + 1, NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + if (!mEntryRequiredRegs[CPU_REG_A]) + { + prevBlock->mIns.Insert(k, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDA, ASMIM_IMMEDIATE, mIns[i + 0].mAddress)); + prevBlock->mIns.Insert(k + 1, NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STA, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } + else + { + prevBlock->mIns.Insert(k, NativeCodeInstruction(mIns[i + 0].mIns, ASMIT_LDX, ASMIM_IMMEDIATE, mIns[i + 0].mAddress)); + prevBlock->mIns.Insert(k + 1, NativeCodeInstruction(mIns[i + 1].mIns, ASMIT_STX, ASMIM_ZERO_PAGE, mIns[i + 1].mAddress)); + } mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED; } } diff --git a/samples/memmap/charsetload.d64 b/samples/memmap/charsetload.d64 index 2b32b48533774ee2746d1aac1595035f54aeab4b..6ef744c8ce65b6fd75de291122717ce8c34649b5 100644 GIT binary patch delta 405 zcmZp;#?^3*t6>WxQ?M|@R)&f4KJgqIC0Cu;DA~fa!spv`?qJ5(VuBou9NZjS4D*Z( z%nTVA7yLK9P`STGq#(DFrH@Ol030$qol=CnG5TuGles*VBf)aVxwg5 z&gmP&8O1ox@GX_fWavFR{d_p1x~U)!0|P@2Pc272PZfyCz`)SSGo5E4&w412fnhI{ z^%P8V@=EefWK?nc3zuhL(BpOBh3G{FLA(qM=}_@%Fx>~`9fi`0d=nX!_yYKH_)7We z_&|E6^G#%w5?%ym@8!G8C&tgf5Wru;-@5%z1S2aG_ll*e6O~UaRhy_hV>(kb}rsu^lS~AXx%l?9 zTvlrjG@&hSIE)-9rR3s5i5o{n)+F!c_j{hVCwV6)@8skWl9B47>+lpQMBhZ?2)+im zTXcBjNxM1lNhsAwGUWPt!o2`+Vr{B>mS~D$YsL19RqBu76%jO!g^qpZI@~;9*@(f@ zJVT!S0f6Y3lx;D%dB*boSuoGVeH^<6^Bs23&3D?ttd9b~6kU@Hy7Z#}uue@XXx>Wz zhn_u@`9)R5#*qI0_Xh|mVdZ!G|ArNSaj#!4Gv%dIkIl@+cBZo_HqTbr&(>r%`s=f0 zKV-LzssOq=r>>ShN3n`vPS7#@uxsV51TJ7sEGBROEm25dCt9WJ1V$^+DruA0UD;r& zkC@Rel^e1rKaxz{&lEXI)+%<^)4YXKk A_y7O^