From 4b4c8866dd6a0052a7a7eb425273d8df7168721b Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 22 Aug 2022 22:00:16 +0200 Subject: [PATCH] Optimize multiply of ranges 0..1 --- oscar64/NativeCodeGenerator.cpp | 67 ++++++++++++++++++++++++++++++++ oscar64/oscar64.cpp | 2 +- oscar64/oscar64.rc | 8 ++-- oscar64setup/oscar64setup.vdproj | 6 +-- 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index aed8590..7110e73 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -6571,6 +6571,39 @@ int NativeCodeBasicBlock::ShortMultiply(InterCodeProcedure* proc, NativeCodeProc { mul &= 0xffff; + if (ins->mSrc[index].IsUByte() && ins->mSrc[index].mRange.mMaxValue == 1 && mul > 4 && mul < 256) + { + int dreg = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp]; + int sreg = BC_REG_TMP + proc->mTempOffset[ins->mSrc[index].mTemp]; + + if (sins) + { + LoadValueToReg(proc, sins, dreg, nullptr, nullptr); + sreg = dreg; + } + + if (mul == 128) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, sreg)); + mIns.Push(NativeCodeInstruction(ASMIT_LSR)); + mIns.Push(NativeCodeInstruction(ASMIT_ROR)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg)); + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg + 1)); + } + else + { + mIns.Push(NativeCodeInstruction(ASMIT_SEC)); + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg + 1)); + mIns.Push(NativeCodeInstruction(ASMIT_SBC, ASMIM_ZERO_PAGE, sreg)); + mIns.Push(NativeCodeInstruction(ASMIT_AND, ASMIM_IMMEDIATE, mul)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg)); + } + + return dreg; + } + int lshift = 0, lmul = mul; while (!(lmul & 1)) { @@ -14212,6 +14245,27 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool } } + if (eb->HasTailSTX(addr, index)) + { + i = 1; + while (i < mEntryBlocks.Size() && mEntryBlocks[i]->HasTailSTX(taddr, tindex) && taddr == addr) + i++; + if (i == mEntryBlocks.Size()) + { + mIns.Insert(0, eb->mIns[index]); + mIns[0].mLive |= LIVE_CPU_REG_A | LIVE_CPU_REG_X | LIVE_CPU_REG_Y; + for (int i = 0; i < mEntryBlocks.Size(); i++) + { + NativeCodeBasicBlock* b = mEntryBlocks[i]; + b->HasTailSTX(taddr, tindex); + for (int j = tindex + 1; j < b->mIns.Size(); j++) + b->mIns[j].mLive |= LIVE_CPU_REG_X; + b->mIns.Remove(tindex); + } + changed = true; + } + } + break; } } @@ -26518,6 +26572,19 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass } } + else if (sz >= 3 && + mIns[sz - 3].mType == ASMIT_LDA && mIns[sz - 3].mMode == ASMIM_IMMEDIATE && mIns[sz - 3].mAddress == 0x00 && + mIns[sz - 2].mType == ASMIT_ROL && mIns[sz - 2].mMode == ASMIM_IMPLIED && + mIns[sz - 1].mType == ASMIT_CMP && mIns[sz - 1].mMode == ASMIM_IMMEDIATE && mIns[sz - 1].mAddress == 0x01 && !(mIns[sz - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)) && !mExitRequiredRegs[CPU_REG_C]) + { + if (mBranch == ASMIT_BCC || mBranch == ASMIT_BCS) + { + mIns.SetSize(sz - 3); + sz -= 3; + changed = true; + } + } + else if (sz >= 2 && mIns[sz - 2].mType == ASMIT_LDA && mIns[sz - 2].mMode == ASMIM_IMMEDIATE && mIns[sz - 2].mAddress == 0 && mIns[sz - 1].mType == ASMIT_SBC && mIns[sz - 1].mMode == ASMIM_IMMEDIATE && mIns[sz - 1].mAddress == 0 && !(mIns[sz - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C)) && !mExitRequiredRegs[CPU_REG_Z]) diff --git a/oscar64/oscar64.cpp b/oscar64/oscar64.cpp index d48e0f3..a3c0b99 100644 --- a/oscar64/oscar64.cpp +++ b/oscar64/oscar64.cpp @@ -74,7 +74,7 @@ int main2(int argc, const char** argv) #else strcpy(strProductName, "oscar64"); - strcpy(strProductVersion, "1.8.153"); + strcpy(strProductVersion, "1.8.154"); #ifdef __APPLE__ uint32_t length = sizeof(basePath); diff --git a/oscar64/oscar64.rc b/oscar64/oscar64.rc index 8542146..0d5dd8e 100644 --- a/oscar64/oscar64.rc +++ b/oscar64/oscar64.rc @@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL // VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,8,153,0 - PRODUCTVERSION 1,8,153,0 + FILEVERSION 1,8,154,0 + PRODUCTVERSION 1,8,154,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -43,12 +43,12 @@ BEGIN BEGIN VALUE "CompanyName", "oscar64" VALUE "FileDescription", "oscar64 compiler" - VALUE "FileVersion", "1.8.153.0" + VALUE "FileVersion", "1.8.154.0" VALUE "InternalName", "oscar64.exe" VALUE "LegalCopyright", "Copyright (C) 2021" VALUE "OriginalFilename", "oscar64.exe" VALUE "ProductName", "oscar64" - VALUE "ProductVersion", "1.8.153.0" + VALUE "ProductVersion", "1.8.154.0" END END BLOCK "VarFileInfo" diff --git a/oscar64setup/oscar64setup.vdproj b/oscar64setup/oscar64setup.vdproj index c0348de..c1aaf1d 100644 --- a/oscar64setup/oscar64setup.vdproj +++ b/oscar64setup/oscar64setup.vdproj @@ -4231,15 +4231,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:oscar64" - "ProductCode" = "8:{DD3A8713-73F3-416C-8D8A-905F9DF55406}" - "PackageCode" = "8:{077A6A53-3869-44C4-9BBE-C0F41C483A9B}" + "ProductCode" = "8:{2B60A66D-2355-44A1-A18F-6D4FFB905B87}" + "PackageCode" = "8:{83F5E3A7-10BF-4D0E-AE7A-D51EBDB9A37E}" "UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}" "AspNetVersion" = "8:2.0.50727.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:1.8.153" + "ProductVersion" = "8:1.8.154" "Manufacturer" = "8:oscar64" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:"