Improve const initialized simple type usage for local variables

This commit is contained in:
drmortalwombat 2025-05-27 14:03:04 +02:00
parent 197e2a91be
commit a1d4bc8375
3 changed files with 109 additions and 0 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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);