From 65fc43c1238ebe4616ed5210a87113983ad48cd2 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 14 Aug 2022 17:52:58 +0200 Subject: [PATCH] More multi path constant folding --- oscar64/InterCode.cpp | 76 ++++++++++++++++++++------------ oscar64/InterCode.h | 1 + oscar64/NativeCodeGenerator.cpp | 40 ++++++++++++++++- oscar64/oscar64.cpp | 2 +- oscar64/oscar64.rc | 8 ++-- oscar64setup/oscar64setup.vdproj | 6 +-- 6 files changed, 95 insertions(+), 38 deletions(-) diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 1cd98f5..fe5ec6b 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -971,6 +971,16 @@ static bool CanBypass(const InterInstruction* lins, const InterInstruction* bins if (HasSideEffect(lins->mCode) && HasSideEffect(bins->mCode)) return false; + if (lins->mCode == IC_CALL || lins->mCode == IC_CALL_NATIVE) + { + if (bins->mCode == IC_CALL || bins->mCode == IC_CALL_NATIVE || + bins->mCode == IC_RETURN || bins->mCode == IC_RETURN_STRUCT || bins->mCode == IC_RETURN_VALUE || + bins->mCode == IC_PUSH_FRAME || bins->mCode == IC_POP_FRAME) + return false; + if (bins->mCode == IC_LOAD || bins->mCode == IC_STORE || bins->mCode == IC_COPY) + return false; + } + if (lins->mDst.mTemp >= 0) { if (lins->mDst.mTemp == bins->mDst.mTemp) @@ -8356,7 +8366,7 @@ bool InterCodeBasicBlock::CanMoveInstructionDown(int si, int ti) const { return false; } - else if (ins->mCode == IC_CALL || ins->mCode == IC_CALL_NATIVE || ins->mCode == IC_COPY || ins->mCode == IC_PUSH_FRAME || ins->mCode == IC_POP_FRAME || + else if (ins->mCode == IC_COPY || ins->mCode == IC_PUSH_FRAME || ins->mCode == IC_POP_FRAME || ins->mCode == IC_RETURN || ins->mCode == IC_RETURN_STRUCT || ins->mCode == IC_RETURN_VALUE) return false; else @@ -12392,6 +12402,39 @@ void InterCodeProcedure::LoadStoreForwarding(InterMemory paramMemory) } while (changed); } +void InterCodeProcedure::PropagateConstOperationsUp(void) +{ +#if 1 + ResetEntryBlocks(); + ResetVisited(); + mEntryBlock->CollectEntryBlocks(nullptr); + + bool changed; + do { + changed = false; + + ResetVisited(); + mEntryBlock->BuildConstTempSets(); + + ResetVisited(); + if (mEntryBlock->PropagateConstOperationsUp()) + { + BuildDataFlowSets(); + + GlobalConstantPropagation(); + + TempForwarding(); + + RemoveUnusedInstructions(); + + changed = true; + + DisassembleDebug("prop const op up"); + } + } while (changed); +#endif +} + void InterCodeProcedure::Close(void) { int i, j, k, start; @@ -12839,34 +12882,7 @@ void InterCodeProcedure::Close(void) DisassembleDebug("Rebuilt traces"); #endif -#if 1 - ResetEntryBlocks(); - ResetVisited(); - mEntryBlock->CollectEntryBlocks(nullptr); - - do { - changed = false; - - ResetVisited(); - mEntryBlock->BuildConstTempSets(); - - ResetVisited(); - if (mEntryBlock->PropagateConstOperationsUp()) - { - BuildDataFlowSets(); - - GlobalConstantPropagation(); - - TempForwarding(); - - RemoveUnusedInstructions(); - - changed = true; - - DisassembleDebug("prop const op up"); - } - } while (changed); -#endif + PropagateConstOperationsUp(); #if 1 BuildDataFlowSets(); @@ -12925,6 +12941,8 @@ void InterCodeProcedure::Close(void) #endif + PropagateConstOperationsUp(); + #if 1 for (int i = 0; i < 4; i++) { diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index 3966cca..7d663f0 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -577,6 +577,7 @@ protected: void EliminateAliasValues(); void LoadStoreForwarding(InterMemory paramMemory); void ExpandSelect(void); + void PropagateConstOperationsUp(void); void MergeBasicBlocks(void); void CheckUsedDefinedTemps(void); diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index f114f50..5139e9e 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -44,6 +44,7 @@ bool NativeRegisterData::SameData(const NativeRegisterData& d) const switch (mMode) { + case NRDM_UNKNOWN: case NRDM_IMMEDIATE: case NRDM_ZERO_PAGE: return mValue == d.mValue; @@ -189,7 +190,24 @@ void NativeRegisterDataSet::Intersect(const NativeRegisterDataSet& set) changed = true; } } - else if (mRegs[i].mMode == NRDM_ABSOLUTE_X || mRegs[i].mMode == NRDM_ABSOLUTE_Y || mRegs[i].mMode == NRDM_INDIRECT_Y) + else if (mRegs[i].mMode == NRDM_INDIRECT_Y) + { + if (set.mRegs[i].mMode != NRDM_INDIRECT_Y || mRegs[i].mValue != set.mRegs[i].mValue || !mRegs[CPU_REG_Y].SameData(set.mRegs[CPU_REG_Y])) + { + mRegs[i].Reset(); + changed = true; + } + else + { + int reg = mRegs[i].mValue; + if (!mRegs[reg].SameData(set.mRegs[reg]) || !mRegs[reg + 1].SameData(set.mRegs[reg + 1])) + { + mRegs[i].Reset(); + changed = true; + } + } + } + else if (mRegs[i].mMode == NRDM_ABSOLUTE_X || mRegs[i].mMode == NRDM_ABSOLUTE_Y) { mRegs[i].Reset(); changed = true; @@ -23147,6 +23165,26 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass mIns[i + 0].mType = ASMIT_LSR; progress = true; } + else if ( + mIns[i + 0].mType == ASMIT_ROL && mIns[i + 0].mMode == ASMIM_IMPLIED && + mIns[i + 1].mType == ASMIT_AND && mIns[i + 1].mMode == ASMIM_IMMEDIATE && !(mIns[i + 1].mLive & LIVE_CPU_REG_C)) + { + mIns[i + 0] = mIns[i + 1]; + mIns[i + 0].mLive |= LIVE_CPU_REG_C; + mIns[i + 0].mAddress &= 0x7f; + mIns[i + 1].mType = ASMIT_ROL; mIns[i + 1].mMode = ASMIM_IMPLIED; + progress = true; + } + else if ( + mIns[i + 0].mType == ASMIT_ROR && mIns[i + 0].mMode == ASMIM_IMPLIED && + mIns[i + 1].mType == ASMIT_AND && mIns[i + 1].mMode == ASMIM_IMMEDIATE && !(mIns[i + 1].mLive & LIVE_CPU_REG_C)) + { + mIns[i + 0] = mIns[i + 1]; + mIns[i + 0].mLive |= LIVE_CPU_REG_C; + mIns[i + 0].mAddress &= 0xfe; + mIns[i + 1].mType = ASMIT_ROR; mIns[i + 1].mMode = ASMIM_IMPLIED; + progress = true; + } else if ( mIns[i + 0].IsShift() && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mType == ASMIT_LDA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mAddress == mIns[i + 0].mAddress && !(mIns[i + 1].mLive & LIVE_MEM)) diff --git a/oscar64/oscar64.cpp b/oscar64/oscar64.cpp index 0d31da9..d48e0f3 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.152"); + strcpy(strProductVersion, "1.8.153"); #ifdef __APPLE__ uint32_t length = sizeof(basePath); diff --git a/oscar64/oscar64.rc b/oscar64/oscar64.rc index 4b90785..8542146 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,152,0 - PRODUCTVERSION 1,8,152,0 + FILEVERSION 1,8,153,0 + PRODUCTVERSION 1,8,153,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -43,12 +43,12 @@ BEGIN BEGIN VALUE "CompanyName", "oscar64" VALUE "FileDescription", "oscar64 compiler" - VALUE "FileVersion", "1.8.152.0" + VALUE "FileVersion", "1.8.153.0" VALUE "InternalName", "oscar64.exe" VALUE "LegalCopyright", "Copyright (C) 2021" VALUE "OriginalFilename", "oscar64.exe" VALUE "ProductName", "oscar64" - VALUE "ProductVersion", "1.8.152.0" + VALUE "ProductVersion", "1.8.153.0" END END BLOCK "VarFileInfo" diff --git a/oscar64setup/oscar64setup.vdproj b/oscar64setup/oscar64setup.vdproj index 051bbfc..c0348de 100644 --- a/oscar64setup/oscar64setup.vdproj +++ b/oscar64setup/oscar64setup.vdproj @@ -4231,15 +4231,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:oscar64" - "ProductCode" = "8:{5EE28B01-CA5F-4F23-8937-291BC1DD4853}" - "PackageCode" = "8:{F8E05C56-4982-400F-BBB1-D7C95C3C65C8}" + "ProductCode" = "8:{DD3A8713-73F3-416C-8D8A-905F9DF55406}" + "PackageCode" = "8:{077A6A53-3869-44C4-9BBE-C0F41C483A9B}" "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.152" + "ProductVersion" = "8:1.8.153" "Manufacturer" = "8:oscar64" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:"