From 2003f2f4fd40b0c95aae2ec56b1a530e5d3492f0 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sat, 27 Nov 2021 23:33:03 +0100 Subject: [PATCH] Fixed N flag thrashing due to cross block value forwarding --- oscar64/NativeCodeGenerator.cpp | 26 ++++++++++++++++++++------ oscar64/NativeCodeGenerator.h | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index deddb09..e359dda 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -8539,7 +8539,7 @@ bool NativeCodeBasicBlock::MoveLoadAddZPStoreUp(int at) return false; } -bool NativeCodeBasicBlock::ValueForwarding(const NativeRegisterDataSet& data) +bool NativeCodeBasicBlock::ValueForwarding(const NativeRegisterDataSet& data, bool global) { bool changed = false; @@ -8547,7 +8547,7 @@ bool NativeCodeBasicBlock::ValueForwarding(const NativeRegisterDataSet& data) { mNDataSet = data; - if (mLoopHead) + if (mLoopHead || !global) { mNDataSet.Reset(); } @@ -8677,9 +8677,9 @@ bool NativeCodeBasicBlock::ValueForwarding(const NativeRegisterDataSet& data) } } #endif - if (this->mTrueJump && this->mTrueJump->ValueForwarding(mNDataSet)) + if (this->mTrueJump && this->mTrueJump->ValueForwarding(mNDataSet, global)) changed = true; - if (this->mFalseJump && this->mFalseJump->ValueForwarding(mNDataSet)) + if (this->mFalseJump && this->mFalseJump->ValueForwarding(mNDataSet, global)) changed = true; } @@ -9727,6 +9727,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass) // Replace (a & 0x80) != 0 with bpl/bmi int sz = mIns.Size(); +#if 1 if (sz > 1 && mIns[sz - 2].ChangesAccuAndFlag() && mIns[sz - 1].mType == ASMIT_AND && mIns[sz - 1].mMode == ASMIM_IMMEDIATE && mIns[sz - 1].mAddress == 0x80 && !(mIns[sz - 1].mLive & LIVE_CPU_REG_A)) @@ -9746,7 +9747,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass) changed = true; } } - +#endif if (sz > 4 && mIns[sz - 4].mType == ASMIT_ASL && mIns[sz - 4].mMode == ASMIM_IMPLIED && mIns[sz - 3].mType == ASMIT_LDA && mIns[sz - 3].mMode == ASMIM_IMMEDIATE && mIns[sz - 3].mAddress == 0 && @@ -10154,12 +10155,21 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass) { mIns.Insert(i + 2, NativeCodeInstruction(ASMIT_LDY, ASMIM_IMMEDIATE, mIns[i + 0].mAddress)); mIns[i + 2].mLive |= LIVE_CPU_REG_Y; + if (mIns[i + 1].mLive & LIVE_CPU_REG_Z) + { + mIns.Insert(i + 3, NativeCodeInstruction(ASMIT_ORA, ASMIM_IMMEDIATE, 0)); + mIns[i + 3].mLive |= LIVE_CPU_REG_Y | LIVE_CPU_REG_Z; + } } if (iins || (flags & LIVE_CPU_REG_Y)) + { mIns[i + 1].mMode = ASMIM_ABSOLUTE_Y; + } else + { mIns[i + 1].mMode = ASMIM_ABSOLUTE_X; + } mIns[i + 1].mLinkerObject = ains->mLinkerObject; mIns[i + 1].mAddress = ains->mAddress + mIns[i + 0].mAddress; @@ -10170,11 +10180,15 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(int pass) { mIns.Insert(apos, NativeCodeInstruction(ASMIT_TAY, ASMIM_IMPLIED)); mIns[apos].mLive = LIVE_CPU_REG_Y | LIVE_CPU_REG_A; + for (int j = apos; j < i + 2; j++) + mIns[j].mLive |= LIVE_CPU_REG_Y; } else { mIns.Insert(apos, NativeCodeInstruction(ASMIT_TAX, ASMIM_IMPLIED)); mIns[apos].mLive = LIVE_CPU_REG_X | LIVE_CPU_REG_A; + for (int j = apos; j < i + 2; j++) + mIns[j].mLive |= LIVE_CPU_REG_X; } } else if (iins->mMode != ASMIM_ZERO_PAGE) @@ -11558,7 +11572,7 @@ void NativeCodeProcedure::Optimize(void) ResetVisited(); NativeRegisterDataSet data; - if (mEntryBlock->ValueForwarding(data)) + if (mEntryBlock->ValueForwarding(data, step > 0)) changed = true; } while (changed); diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h index 42c33fa..e06d0c0 100644 --- a/oscar64/NativeCodeGenerator.h +++ b/oscar64/NativeCodeGenerator.h @@ -188,7 +188,7 @@ public: bool MoveStoreHighByteDown(int at); bool MoveAddHighByteDown(int at); - bool ValueForwarding(const NativeRegisterDataSet& data); + bool ValueForwarding(const NativeRegisterDataSet& data, bool global); void CollectEntryBlocks(NativeCodeBasicBlock* block);