From 78886f11f7a2e6c4af41ad6bc06623267da3b1c5 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 20 Sep 2021 13:02:38 +0200 Subject: [PATCH] Native code peephole optimisation for commutative instructions --- oscar64/NativeCodeGenerator.cpp | 17 ++++++++++++++++- oscar64/NativeCodeGenerator.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index f54faeb..a1b2dd3 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -71,7 +71,7 @@ bool NativeCodeInstruction::IsUsedResultInstructions(NumberSet& requiredTemps) mLive |= LIVE_CPU_REG_Z; if (requiredTemps[CPU_REG_C]) mLive |= LIVE_CPU_REG_C; - if (mMode == ASMIM_ZERO_PAGE) + if (mMode == ASMIM_ZERO_PAGE && requiredTemps[mAddress]) mLive |= LIVE_MEM; if (mType == ASMIT_JSR) @@ -446,6 +446,11 @@ bool NativeCodeInstruction::ChangesAddress(void) const return false; } +bool NativeCodeInstruction::IsCommutative(void) const +{ + return mType == ASMIT_ADC || mType == ASMIT_AND || mType == ASMIT_ORA || mType == ASMIT_EOR; +} + bool NativeCodeInstruction::SameEffectiveAddress(const NativeCodeInstruction& ins) const { @@ -4080,6 +4085,16 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(void) mIns[i + 2].mType = ASMIT_STA; progress = true; } + else if (mIns[i + 0].mType == ASMIT_STA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && + mIns[i + 1].mType == ASMIT_LDA && mIns[i + 1].mMode != ASMIM_ZERO_PAGE && + mIns[i + 2].mMode == ASMIM_ZERO_PAGE && mIns[i + 0].mAddress == mIns[i + 2].mAddress && + mIns[i + 2].IsCommutative() && HasAsmInstructionMode(mIns[i + 2].mType, mIns[i + 1].mMode) && + (mIns[i + 2].mLive & LIVE_MEM) == 0) + { + mIns[i + 1].mType = mIns[i + 2].mType; + mIns[i + 0].mType = ASMIT_NOP; + mIns[i + 2].mType = ASMIT_NOP; + } } if (i + 3 < mIns.Size()) diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h index e898a57..2681049 100644 --- a/oscar64/NativeCodeGenerator.h +++ b/oscar64/NativeCodeGenerator.h @@ -47,6 +47,7 @@ public: bool LoadsAccu(void) const; bool ChangesAddress(void) const; bool SameEffectiveAddress(const NativeCodeInstruction& ins) const; + bool IsCommutative(void) const; }; class NativeCodeBasicBlock