From 9daf4fa62172b7c77372e02f0aa7c8653975f688 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 18 Dec 2022 20:33:52 +0100 Subject: [PATCH] Some more cross block pointer forwarding --- oscar64/InterCode.cpp | 48 +++++++++++++++++++++++++++++++++ oscar64/InterCode.h | 1 + oscar64/NativeCodeGenerator.cpp | 29 ++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index ece36fb..f13b166 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -9265,6 +9265,50 @@ bool InterCodeBasicBlock::MoveTrainCrossBlock(void) return changed; } +bool InterCodeBasicBlock::ForwardLoopMovedTemp(void) +{ + bool changed = false; + + if (!mVisited) + { + mVisited = true; + + if (mTrueJump && !mFalseJump && mTrueJump->mLoopHead && mTrueJump->mNumEntries == 2) + { + InterCodeBasicBlock* eblock = nullptr; + if (mTrueJump->mTrueJump == mTrueJump) + eblock = mTrueJump->mFalseJump; + else if (mTrueJump->mFalseJump == mTrueJump) + eblock = mTrueJump->mTrueJump; + + if (eblock) + { + int i = mInstructions.Size() - 1; + while (i >= 0) + { + if (mInstructions[i]->mCode == IC_LOAD_TEMPORARY && CanMoveInstructionBehindBlock(i) && + !mTrueJump->mLocalUsedTemps[mInstructions[i]->mDst.mTemp] && + !mTrueJump->mLocalModifiedTemps[mInstructions[i]->mSrc[0].mTemp]) + { + eblock->mInstructions.Insert(0, mInstructions[i]); + mInstructions.Remove(i); + changed = true; + } + else + i--; + } + } + } + + if (mTrueJump && mTrueJump->ForwardLoopMovedTemp()) + changed = true; + if (mFalseJump && mFalseJump->ForwardLoopMovedTemp()) + changed = true; + } + + return changed; +} + bool InterCodeBasicBlock::ForwardDiamondMovedTemp(void) { bool changed = false; @@ -14187,6 +14231,10 @@ void InterCodeProcedure::Close(void) } #endif + BuildDataFlowSets(); + ResetVisited(); + mEntryBlock->ForwardLoopMovedTemp(); + #if 1 do { TempForwarding(); diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index 5aea73e..9aa49f6 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -464,6 +464,7 @@ public: bool IsLeafProcedure(void); bool ForwardDiamondMovedTemp(void); + bool ForwardLoopMovedTemp(void); bool MoveTrainCrossBlock(void); diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index 022756a..f8fba15 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -28004,6 +28004,22 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass mIns[i + 2].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED; progress = true; } + else if ( + mIns[i + 0].ChangesAccuAndFlag() && + (mIns[i + 1].mType == ASMIT_INC || mIns[i + 1].mType == ASMIT_DEC) && + mIns[i + 2].mType == ASMIT_ORA && mIns[i + 2].mMode == ASMIM_IMMEDIATE && mIns[i + 2].mAddress == 0 && + mIns[i + 1].MayBeMovedBefore(mIns[i + 0])) + { + mIns.Insert(i, mIns[i + 1]); + mIns[i].mLive |= LIVE_CPU_REG_A; + if (mIns[i + 1].RequiresYReg()) + mIns[i].mLive |= LIVE_CPU_REG_Y; + if (mIns[i + 1].RequiresXReg()) + mIns[i].mLive |= LIVE_CPU_REG_X; + mIns[i + 1].mLive |= LIVE_CPU_REG_Z; + mIns.Remove(i + 2); + progress = true; + } if ( mIns[i + 0].mType == ASMIT_LDY && mIns[i + 0].mMode == ASMIM_IMMEDIATE && mIns[i + 0].mAddress <= 1 && @@ -28882,6 +28898,19 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass } #endif #if 1 + else if ( + mIns[i + 0].mType == ASMIT_LDA && mIns[i + 0].mMode == ASMIM_IMMEDIATE && + mIns[i + 1].mType == ASMIT_STA && + mIns[i + 2].mType == ASMIT_LDA && + mIns[i + 3].mType == ASMIT_CMP && mIns[i + 3].mMode == ASMIM_IMMEDIATE && mIns[i + 3].mAddress == mIns[i + 0].mAddress && !(mIns[i + 3].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C))) + { + mIns[i + 1].mLive |= LIVE_CPU_REG_A; + mIns[i + 2].mType = ASMIT_CMP; mIns[i + 2].mLive |= LIVE_CPU_REG_Z; + mIns[i + 3].mType = ASMIT_NOP; mIns[i + 3].mMode = ASMIM_IMPLIED; + + progress = true; + } + else if ( mIns[i + 0].mType == ASMIT_STA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && !(mIns[i + 0].mLive & LIVE_CPU_REG_A) && !mIns[i + 1].ReferencesZeroPage(mIns[i + 0].mAddress) && !mIns[i + 1].ChangesCarry() &&