From a1d4bc837589e37ec68c20e44373bc6704cbe0f5 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Tue, 27 May 2025 14:03:04 +0200 Subject: [PATCH] Improve const initialized simple type usage for local variables --- oscar64/Declaration.cpp | 4 ++ oscar64/NativeCodeGenerator.cpp | 101 ++++++++++++++++++++++++++++++++ oscar64/NativeCodeGenerator.h | 4 ++ 3 files changed, 109 insertions(+) diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index 5d2486c..1575f96 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -1250,6 +1250,10 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio ex->mDecType = mDecType; return ex; } + else if (mType == EX_VARIABLE && mDecType->IsSimpleType() && (mDecValue->mFlags & DTF_CONST) && mDecValue->mValue && mDecValue->mValue->mType == EX_INITIALIZATION && mDecValue->mValue->mRight->mType == EX_CONSTANT) + { + return mDecValue->mValue->mRight; + } else if (mType == EX_CALL && mLeft->mType == EX_CONSTANT && (mLeft->mDecValue->mFlags & DTF_INTRINSIC) && mRight && mRight->mType == EX_CONSTANT) { Declaration* decf = mLeft->mDecValue, * decp = mRight->mDecValue; diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index a8279e0..cfccc11 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -19767,6 +19767,7 @@ bool NativeCodeBasicBlock::MoveStoresBehindCondition(void) NativeCodeBasicBlock* eb = mEntryBlocks[0]; int addr, index, taddr, tindex; + NativeCodeInstruction ains; if (eb->HasTailSTAInto(addr, index, this)) { @@ -19841,6 +19842,60 @@ bool NativeCodeBasicBlock::MoveStoresBehindCondition(void) } } } + + if (mFalseJump && mTrueJump) + { + NativeCodeBasicBlock* eblock = nullptr, * cblock = nullptr; + if (mTrueJump->mTrueJump == mFalseJump && !mTrueJump->mFalseJump && mFalseJump->mNumEntries == 2 && mTrueJump->mNumEntries == 1) + { + cblock = mTrueJump; + eblock = mFalseJump; + } + if (mFalseJump->mTrueJump == mTrueJump && !mFalseJump->mFalseJump && mTrueJump->mNumEntries == 2 && mFalseJump->mNumEntries == 1) + { + cblock = mFalseJump; + eblock = mTrueJump; + } + + if (eblock && eblock != this) + { + NativeCodeInstruction ins, cins; + int index, cindex; + + if (HasTailSTAGlobal(ins, index) && cblock->HasTailSTAGlobal(cins, cindex) && ins.SameEffectiveAddress(cins)) + { + if (!cblock->ReferencesMemory(ins, 0, cindex)) + { + mExitRequiredRegs += CPU_REG_A; + cblock->mExitRequiredRegs += CPU_REG_A; + eblock->mEntryRequiredRegs += CPU_REG_A; + + uint32 live = LIVE_CPU_REG_A; + if (ins.ReferencesXReg()) + { + live += LIVE_CPU_REG_X; + mExitRequiredRegs += CPU_REG_X; + cblock->mExitRequiredRegs += CPU_REG_X; + } + if (ins.ReferencesYReg()) + { + live += LIVE_CPU_REG_Y; + mExitRequiredRegs += CPU_REG_Y; + cblock->mExitRequiredRegs += CPU_REG_Y; + } + + eblock->mIns.Insert(0, ins); + for (int i = index; i < mIns.Size(); i++) + mIns[i].mLive |= live; + for (int i = cindex; i < cblock->mIns.Size(); i++) + cblock->mIns[i].mLive |= live; + mIns.Remove(index); + cblock->mIns.Remove(cindex); + changed = true; + } + } + } + } if (mTrueJump && mTrueJump->MoveStoresBehindCondition()) changed = true; if (mFalseJump && mFalseJump->MoveStoresBehindCondition()) changed = true; @@ -24085,6 +24140,35 @@ bool NativeCodeBasicBlock::HasTailSTYInto(int& addr, int& index, NativeCodeBasic } +bool NativeCodeBasicBlock::HasTailSTAGlobal(NativeCodeInstruction & ins, int& index) const +{ + int i = mIns.Size(); + while (i > 0) + { + i--; + if (mIns[i].ChangesAccu()) + return false; + if (mIns[i].mType == ASMIT_STA && mIns[i].mMode != ASMIM_ZERO_PAGE) + { + index = i; + ins = mIns[i]; + + i++; + while (i < mIns.Size()) + { + if (mIns[i].MayReference(ins, true)) + return false; + else if (ins.ReferencesXReg() && mIns[i].ChangesXReg()) + return false; + else if (ins.ReferencesYReg() && mIns[i].ChangesYReg()) + return false; + i++; + } + return true; + } + } + return false; +} void NativeCodeBasicBlock::AddEntryBlock(NativeCodeBasicBlock* block) { @@ -45206,6 +45290,23 @@ bool NativeCodeBasicBlock::ReferencesZeroPage(int address, int from, int to) con return false; } +bool NativeCodeBasicBlock::ChangesMemory(const NativeCodeInstruction& ins, int from, int to) const +{ + if (to > mIns.Size()) to = mIns.Size(); + for (int i = from; i < to; i++) + if (ins.MayBeChangedOnAddress(mIns[i])) + return true; + return false; +} + +bool NativeCodeBasicBlock::ReferencesMemory(const NativeCodeInstruction& ins, int from, int to) const +{ + if (to > mIns.Size()) to = mIns.Size(); + for (int i = from; i < to; i++) + if (mIns[i].MayReference(ins)) + return true; + return false; +} diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h index 4b3362a..8fbc0e9 100644 --- a/oscar64/NativeCodeGenerator.h +++ b/oscar64/NativeCodeGenerator.h @@ -327,6 +327,8 @@ public: bool UsesZeroPage(int address, int from = 0, int to = 65536) const; bool ReferencesZeroPage(int address, int from = 0, int to = 65536) const; + bool ChangesMemory(const NativeCodeInstruction& ins, int from = 0, int to = 65536) const; + bool ReferencesMemory(const NativeCodeInstruction& ins, int from = 0, int to = 65536) const; bool RemoveNops(void); bool PeepHoleOptimizer(int pass); @@ -629,6 +631,8 @@ public: bool HasTailSTXInto(int& addr, int& index, NativeCodeBasicBlock* tblock) const; bool HasTailSTYInto(int& addr, int& index, NativeCodeBasicBlock* tblock) const; + bool HasTailSTAGlobal(NativeCodeInstruction & ins, int& index) const; + bool MayBeMovedBeforeBlock(int at); bool MayBeMovedBeforeBlock(int at, const NativeCodeInstruction & ins); bool MayBeMovedBeforeBlock(int start, int end);