Fix sign flag propagationa after immediate ora

This commit is contained in:
drmortalwombat 2024-06-30 21:28:35 +02:00
parent 7eb149a71b
commit ad310f8484
2 changed files with 62 additions and 10 deletions

View File

@ -3444,7 +3444,7 @@ bool NativeCodeInstruction::ValueForwarding(NativeRegisterDataSet& data, AsmInsT
else if (mMode == ASMIM_IMMEDIATE && mType == ASMIT_ORA && mAddress != 0x00) else if (mMode == ASMIM_IMMEDIATE && mType == ASMIT_ORA && mAddress != 0x00)
{ {
data.mRegs[CPU_REG_Z].mMode = NRDM_IMMEDIATE; data.mRegs[CPU_REG_Z].mMode = NRDM_IMMEDIATE;
data.mRegs[CPU_REG_Z].mValue = 1; data.mRegs[CPU_REG_Z].mValue = mAddress;
data.mRegs[CPU_REG_A].Reset(); data.mRegs[CPU_REG_A].Reset();
} }
else if (mMode == ASMIM_IMMEDIATE && ((mAddress == 0 && (mType == ASMIT_ORA || mType == ASMIT_EOR)) || (mAddress == 0xff && mType == ASMIT_AND))) else if (mMode == ASMIM_IMMEDIATE && ((mAddress == 0 && (mType == ASMIT_ORA || mType == ASMIT_EOR)) || (mAddress == 0xff && mType == ASMIT_AND)))
@ -18830,6 +18830,46 @@ bool NativeCodeBasicBlock::SimpleInlineCalls(void)
return changed; return changed;
} }
bool NativeCodeBasicBlock::Expand16BitLoopBranch(void)
{
bool changed = false;
if (!mVisited)
{
mVisited = true;
if (mTrueJump && mFalseJump && mBranch == ASMIT_BNE && mTrueJump->mIns.Size() == 0 && mTrueJump->mBranch == ASMIT_BCC)
{
int sz = mIns.Size();
if (sz >= 1 && mIns[sz - 1].mType == ASMIT_CMP)
{
if (mTrueJump->mTrueJump->mLoopHead && IsDominatedBy(mTrueJump->mTrueJump) ||
mLoopHead && !mTrueJump->mTrueJump->mFalseJump && mTrueJump->mTrueJump->mTrueJump == this)
{
NativeCodeBasicBlock* tblock = mTrueJump->mTrueJump;
NativeCodeBasicBlock* nblock = mProc->AllocateBlock();
nblock->Close(mIns[sz - 1].mIns, mFalseJump, mTrueJump->mFalseJump, ASMIT_BEQ);
mBranch = ASMIT_BCC;
mFalseJump->RemEntryBlock(this);
mFalseJump->AddEntryBlock(nblock);
mTrueJump->RemEntryBlock(this);
tblock->AddEntryBlock(this);
mFalseJump = nblock;
mTrueJump = tblock;
changed = true;
}
}
}
if (mTrueJump && mTrueJump->Expand16BitLoopBranch())
changed = true;
if (mFalseJump && mFalseJump->Expand16BitLoopBranch())
changed = true;
}
return changed;
}
bool NativeCodeBasicBlock::ExpandADCToBranch(NativeCodeProcedure* proc) bool NativeCodeBasicBlock::ExpandADCToBranch(NativeCodeProcedure* proc)
{ {
bool changed = false; bool changed = false;
@ -35597,7 +35637,7 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc
if (mIns[i + 0].mType == ASMIT_LDA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && mIns[i + 0].mAddress == mIns[1].mAddress && if (mIns[i + 0].mType == ASMIT_LDA && mIns[i + 0].mMode == ASMIM_ZERO_PAGE && mIns[i + 0].mAddress == mIns[1].mAddress &&
mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mAddress == mIns[0].mAddress && !(mIns[1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z))) mIns[i + 1].mType == ASMIT_STA && mIns[i + 1].mMode == ASMIM_ZERO_PAGE && mIns[i + 1].mAddress == mIns[0].mAddress && !(mIns[1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)))
{ {
if ((!mEntryRequiredRegs[CPU_REG_A] || !mEntryRequiredRegs[CPU_REG_X]) && (!mExitRequiredRegs[CPU_REG_A] || !mExitRequiredRegs[CPU_REG_X])) if ((!mEntryRequiredRegs[CPU_REG_A] || !mEntryRequiredRegs[CPU_REG_X]) && (!mExitRequiredRegs[CPU_REG_A] || !mExitRequiredRegs[CPU_REG_X]) && !mExitRequiredRegs[CPU_REG_Z])
{ {
if (!prevBlock) if (!prevBlock)
return OptimizeSimpleLoopInvariant(proc, full); return OptimizeSimpleLoopInvariant(proc, full);
@ -35848,7 +35888,7 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoopInvariant(NativeCodeProcedure* proc
} }
#if 1 #if 1
if (mEntryRequiredRegs.Size() && (!mEntryRequiredRegs[CPU_REG_A] || !mEntryRequiredRegs[CPU_REG_X]) && (!mExitRequiredRegs[CPU_REG_A] || !mExitRequiredRegs[CPU_REG_X])) if (mEntryRequiredRegs.Size() && (!mEntryRequiredRegs[CPU_REG_A] || !mEntryRequiredRegs[CPU_REG_X]) && (!mExitRequiredRegs[CPU_REG_A] || !mExitRequiredRegs[CPU_REG_X]) && !mExitRequiredRegs[CPU_REG_Z])
{ {
for (int i = 0; i + 1 < mIns.Size(); i++) for (int i = 0; i + 1 < mIns.Size(); i++)
{ {
@ -36796,7 +36836,7 @@ bool NativeCodeBasicBlock::OptimizeSimpleLoop(NativeCodeProcedure * proc, bool f
bool changed = false; bool changed = false;
int sz = mIns.Size(); int sz = mIns.Size();
assert(mBranch != ASMIT_JMP || mFalseJump == nullptr); assert(mBranch != ASMIT_JMP || mFalseJump == nullptr);
CheckLive(); CheckLive();
@ -43312,8 +43352,8 @@ bool NativeCodeBasicBlock::PeepHoleOptimizerIterate3(int i, int pass)
mIns[i + 1].mType == ASMIT_STX && mIns[i + 1].mType == ASMIT_STX &&
mIns[i + 2].mType == ASMIT_TXA && !(mIns[i + 2].mLive & LIVE_CPU_REG_X)) mIns[i + 2].mType == ASMIT_TXA && !(mIns[i + 2].mLive & LIVE_CPU_REG_X))
{ {
mIns[i + 0].mType = ASMIT_LDA; mIns[i + 0].mLive |= LIVE_CPU_REG_A; mIns[i + 0].mType = ASMIT_LDA; mIns[i + 0].mLive |= LIVE_CPU_REG_A | (mIns[i + 2].mLive & LIVE_CPU_REG_Z);
mIns[i + 1].mType = ASMIT_STA; mIns[i + 1].mLive |= LIVE_CPU_REG_A; mIns[i + 1].mType = ASMIT_STA; mIns[i + 1].mLive |= LIVE_CPU_REG_A | (mIns[i + 2].mLive & LIVE_CPU_REG_Z);
mIns[i + 2].mType = ASMIT_NOP; mIns[i + 2].mMode = ASMIM_IMPLIED; mIns[i + 2].mType = ASMIT_NOP; mIns[i + 2].mMode = ASMIM_IMPLIED;
return true; return true;
} }
@ -43322,8 +43362,8 @@ bool NativeCodeBasicBlock::PeepHoleOptimizerIterate3(int i, int pass)
mIns[i + 1].mType == ASMIT_STY && mIns[i + 1].mType == ASMIT_STY &&
mIns[i + 2].mType == ASMIT_TYA && !(mIns[i + 2].mLive & LIVE_CPU_REG_Y)) mIns[i + 2].mType == ASMIT_TYA && !(mIns[i + 2].mLive & LIVE_CPU_REG_Y))
{ {
mIns[i + 0].mType = ASMIT_LDA; mIns[i + 0].mLive |= LIVE_CPU_REG_A; mIns[i + 0].mType = ASMIT_LDA; mIns[i + 0].mLive |= LIVE_CPU_REG_A | (mIns[i + 2].mLive & LIVE_CPU_REG_Z);
mIns[i + 1].mType = ASMIT_STA; mIns[i + 1].mLive |= LIVE_CPU_REG_A; mIns[i + 1].mType = ASMIT_STA; mIns[i + 1].mLive |= LIVE_CPU_REG_A | (mIns[i + 2].mLive & LIVE_CPU_REG_Z);
mIns[i + 2].mType = ASMIT_NOP; mIns[i + 2].mMode = ASMIM_IMPLIED; mIns[i + 2].mType = ASMIT_NOP; mIns[i + 2].mMode = ASMIM_IMPLIED;
return true; return true;
} }
@ -50393,7 +50433,6 @@ void NativeCodeProcedure::Optimize(void)
} }
#endif #endif
if (step == 16) if (step == 16)
{ {
if (mInterProc->mCompilerOptions & COPT_OPTIMIZE_INLINE) if (mInterProc->mCompilerOptions & COPT_OPTIMIZE_INLINE)
@ -50404,6 +50443,17 @@ void NativeCodeProcedure::Optimize(void)
} }
} }
if (step == 17)
{
if (!(mInterProc->mCompilerOptions & COPT_OPTIMIZE_CODE_SIZE))
{
ResetVisited();
if (mEntryBlock->Expand16BitLoopBranch())
changed = true;
}
}
#if _DEBUG #if _DEBUG
ResetVisited(); ResetVisited();
mEntryBlock->CheckAsmCode(); mEntryBlock->CheckAsmCode();
@ -50423,7 +50473,7 @@ void NativeCodeProcedure::Optimize(void)
} }
#if 1 #if 1
if (!changed && step < 16) if (!changed && step < 17)
{ {
ResetIndexFlipped(); ResetIndexFlipped();
@ -50436,6 +50486,7 @@ void NativeCodeProcedure::Optimize(void)
else else
cnt++; cnt++;
} while (changed); } while (changed);
#if 1 #if 1

View File

@ -651,6 +651,7 @@ public:
bool RemoveDoubleZPStore(void); bool RemoveDoubleZPStore(void);
bool ExpandADCToBranch(NativeCodeProcedure* proc); bool ExpandADCToBranch(NativeCodeProcedure* proc);
bool Expand16BitLoopBranch(void);
bool SimpleInlineCalls(void); bool SimpleInlineCalls(void);
bool Split16BitLoopCount(NativeCodeProcedure* proc); bool Split16BitLoopCount(NativeCodeProcedure* proc);