Optimize compare to boolean variable

This commit is contained in:
drmortalwombat 2022-03-31 18:16:31 +02:00
parent cb4f60b2ff
commit 48f97b6e60
6 changed files with 163 additions and 15 deletions

View File

@ -4684,6 +4684,14 @@ void InterCodeBasicBlock::SimplifyIntegerRangeRelops(void)
if (cins->mSrc[0].IsUnsigned() && cins->mSrc[1].IsUnsigned()) if (cins->mSrc[0].IsUnsigned() && cins->mSrc[1].IsUnsigned())
cins->mOperator = IA_CMPLEU; cins->mOperator = IA_CMPLEU;
break; break;
case IA_CMPGS:
if (cins->mSrc[0].IsUnsigned() && cins->mSrc[1].IsUnsigned())
cins->mOperator = IA_CMPGU;
break;
case IA_CMPGES:
if (cins->mSrc[0].IsUnsigned() && cins->mSrc[1].IsUnsigned())
cins->mOperator = IA_CMPGEU;
break;
} }
} }
} }

View File

@ -974,7 +974,7 @@ bool NativeCodeInstruction::IsSame(const NativeCodeInstruction& ins) const
return false; return false;
} }
bool NativeCodeInstruction::MayBeSameAddress(const NativeCodeInstruction& ins) const bool NativeCodeInstruction::MayBeSameAddress(const NativeCodeInstruction& ins, bool sameXY) const
{ {
if (ins.mMode == ASMIM_ZERO_PAGE) if (ins.mMode == ASMIM_ZERO_PAGE)
{ {
@ -999,7 +999,12 @@ bool NativeCodeInstruction::MayBeSameAddress(const NativeCodeInstruction& ins) c
else if (ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y) else if (ins.mMode == ASMIM_ABSOLUTE_X || ins.mMode == ASMIM_ABSOLUTE_Y)
{ {
if (mMode == ASMIM_ABSOLUTE || mMode == ASMIM_ABSOLUTE_X || mMode == ASMIM_ABSOLUTE_Y) if (mMode == ASMIM_ABSOLUTE || mMode == ASMIM_ABSOLUTE_X || mMode == ASMIM_ABSOLUTE_Y)
return mLinkerObject == ins.mLinkerObject; {
if (mLinkerObject != ins.mLinkerObject)
return false;
else
return mMode != ins.mMode || !sameXY || mAddress == ins.mAddress;
}
else else
return mMode == ASMIM_INDIRECT_Y || mMode == ASMIM_INDIRECT_X; return mMode == ASMIM_INDIRECT_Y || mMode == ASMIM_INDIRECT_X;
} }
@ -7190,6 +7195,23 @@ NativeCodeBasicBlock* NativeCodeBasicBlock::BinaryOperator(InterCodeProcedure* p
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_ZERO_PAGE, treg + 1)); mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_ZERO_PAGE, treg + 1));
} }
} }
#if 1
else if (shift == 6)
{
int sreg = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp];
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, sreg + 1));
mIns.Push(NativeCodeInstruction(ASMIT_LDX, ASMIM_ZERO_PAGE, sreg + 0));
mIns.Push(NativeCodeInstruction(ASMIT_STX, ASMIM_ZERO_PAGE, treg + 1));
mIns.Push(NativeCodeInstruction(ASMIT_LSR, ASMIM_IMPLIED));
mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_ZERO_PAGE, treg + 1));
mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_IMPLIED));
mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_ZERO_PAGE, treg + 1));
mIns.Push(NativeCodeInstruction(ASMIT_ROR, ASMIM_IMPLIED));
mIns.Push(NativeCodeInstruction(ASMIT_AND, ASMIM_IMMEDIATE, 0xc0));
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, treg + 0));
}
#endif
else if (shift == 7) else if (shift == 7)
{ {
int sreg = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp]; int sreg = BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp];
@ -12377,7 +12399,7 @@ bool NativeCodeBasicBlock::MoveAbsoluteLoadStoreUp(int at)
} }
if (mIns[j].ChangesZeroPage(mIns[at].mAddress)) if (mIns[j].ChangesZeroPage(mIns[at].mAddress))
return false; return false;
if (mIns[j].MayBeSameAddress(mIns[at + 1])) if (mIns[j].MayBeSameAddress(mIns[at + 1], true))
return false; return false;
if (mIns[at + 1].mMode == ASMIM_ABSOLUTE_X && mIns[j].ChangesXReg()) if (mIns[at + 1].mMode == ASMIM_ABSOLUTE_X && mIns[j].ChangesXReg())
return false; return false;
@ -12782,9 +12804,9 @@ bool NativeCodeBasicBlock::MoveLoadAddImmStoreAbsXUp(int at)
{ {
if (mIns[j].ChangesXReg()) if (mIns[j].ChangesXReg())
break; break;
if (mIns[j].MayBeSameAddress(mIns[at + 3])) if (mIns[j].MayBeSameAddress(mIns[at + 3], true))
break; break;
if (mIns[j].MayBeSameAddress(mIns[at + 0]) && mIns[j].ChangesAddress()) if (mIns[j].MayBeSameAddress(mIns[at + 0], true) && mIns[j].ChangesAddress())
break; break;
if (!(mIns[j - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C | LIVE_CPU_REG_Z))) if (!(mIns[j - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C | LIVE_CPU_REG_Z)))
@ -15635,6 +15657,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
mVisited = true; mVisited = true;
#if 1
#if 1 #if 1
// move load store pairs up to initial store // move load store pairs up to initial store
@ -16185,6 +16208,8 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
} }
#endif #endif
#endif
#if 1 #if 1
bool progress = false; bool progress = false;
do { do {
@ -17206,10 +17231,22 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
#endif #endif
else if ( else if (
mIns[i + 0].mType == ASMIT_TAX && mIns[i + 0].mType == ASMIT_TAX &&
!mIns[i + 1].ChangesXReg() && mIns[i + 1].mType == ASMIT_ASL && mIns[i + 1].mMode == ASMIM_IMPLIED &&
mIns[i + 2].mType == ASMIT_TXA && !(mIns[i + 2].mLive & (LIVE_CPU_REG_Z | LIVE_CPU_REG_X)))
{
mIns[i + 0].mType = ASMIT_CMP; mIns[i + 0].mMode = ASMIM_IMMEDIATE; mIns[i + 0].mAddress = 0x80;
mIns[i + 0].mLive |= LIVE_CPU_REG_A | LIVE_CPU_REG_C;
mIns[i + 1].mType = ASMIT_NOP; mIns[i + 1].mMode = ASMIM_IMPLIED;
mIns[i + 2].mType = ASMIT_NOP; mIns[i + 2].mMode = ASMIM_IMPLIED;
progress = true;
}
else if (
mIns[i + 0].mType == ASMIT_TAX &&
!mIns[i + 1].ChangesXReg() && !mIns[i + 1].ChangesAccu() &&
mIns[i + 2].mType == ASMIT_TXA && !(mIns[i + 2].mLive & LIVE_CPU_REG_Z)) mIns[i + 2].mType == ASMIT_TXA && !(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;
mIns[i + 1].mLive |= LIVE_CPU_REG_A;
progress = true; progress = true;
} }
else if ( else if (
@ -17232,6 +17269,7 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
mIns[i + 1].mLive |= LIVE_CPU_REG_A; mIns[i + 1].mLive |= LIVE_CPU_REG_A;
progress = true; progress = true;
} }
else if ( else if (
mIns[i + 0].mType == ASMIT_SEC && mIns[i + 0].mType == ASMIT_SEC &&
mIns[i + 1].mType == ASMIT_TXA && mIns[i + 1].mType == ASMIT_TXA &&
@ -19003,6 +19041,106 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
} while (progress); } while (progress);
int sz = mIns.Size();
#if 1
if (sz >= 2 &&
mIns[sz - 2].mType == ASMIT_LDA && mIns[sz - 2].mMode == ASMIM_IMMEDIATE && mIns[sz - 2].mAddress == 0 &&
mIns[sz - 1].mType == ASMIT_CMP)
{
if (mBranch == ASMIT_BNE)
{
if (mTrueJump->mIns.Size() == 0 && mTrueJump->mBranch == ASMIT_BCC)
{
mTrueJump = mTrueJump->mTrueJump;
changed = true;
}
else if (mTrueJump->mIns.Size() == 0 && mTrueJump->mBranch == ASMIT_BCS)
{
mTrueJump = mTrueJump->mFalseJump;
changed = true;
}
}
else if (mBranch == ASMIT_BEQ)
{
if (mFalseJump->mIns.Size() == 0 && mFalseJump->mBranch == ASMIT_BCS)
{
mFalseJump = mFalseJump->mTrueJump;
changed = true;
}
else if (mFalseJump->mIns.Size() == 0 && mFalseJump->mBranch == ASMIT_BCC)
{
mFalseJump = mFalseJump->mFalseJump;
changed = true;
}
}
}
#endif
#if 1
else if (sz >= 2 &&
mIns[sz - 2].mType == ASMIT_LDA && mIns[sz - 2].mMode == ASMIM_IMMEDIATE && mIns[sz - 2].mAddress == 0 &&
mIns[sz - 1].mType == ASMIT_ROL && mIns[sz - 1].mMode == ASMIM_IMPLIED && !(mIns[sz - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C)))
{
if (mBranch == ASMIT_BNE)
{
mBranch = ASMIT_BCS;
mIns.SetSize(sz - 2);
changed = true;
}
else if (mBranch == ASMIT_BEQ)
{
mBranch = ASMIT_BCC;
mIns.SetSize(sz - 2);
changed = true;
}
}
#endif
#if 1
if (mTrueJump && mFalseJump && !mTrueJump->mFalseJump && !mFalseJump->mFalseJump && mTrueJump->mTrueJump == mFalseJump->mTrueJump &&
mTrueJump->mIns.Size() == 1 && mFalseJump->mIns.Size() == 1 &&
mTrueJump->mIns[0].mType == ASMIT_LDA && mTrueJump->mIns[0].mMode == ASMIM_IMMEDIATE &&
mFalseJump->mIns[0].mType == ASMIT_LDA && mFalseJump->mIns[0].mMode == ASMIM_IMMEDIATE)
{
if (mBranch == ASMIT_BCS && mTrueJump->mIns[0].mAddress == 1 && mFalseJump->mIns[0].mAddress == 0)
{
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_IMPLIED));
mBranch = ASMIT_JMP;
mTrueJump = mTrueJump->mTrueJump;
mFalseJump = nullptr;
changed = true;
}
else if (mBranch == ASMIT_BCC && mTrueJump->mIns[0].mAddress == 0 && mFalseJump->mIns[0].mAddress == 1)
{
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_IMPLIED));
mBranch = ASMIT_JMP;
mTrueJump = mTrueJump->mTrueJump;
mFalseJump = nullptr;
changed = true;
}
else if (mBranch == ASMIT_BCS && mTrueJump->mIns[0].mAddress == 0 && mFalseJump->mIns[0].mAddress == 1)
{
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_IMPLIED));
mIns.Push(NativeCodeInstruction(ASMIT_EOR, ASMIM_IMMEDIATE, 1));
mBranch = ASMIT_JMP;
mTrueJump = mTrueJump->mTrueJump;
mFalseJump = nullptr;
changed = true;
}
else if (mBranch == ASMIT_BCC && mTrueJump->mIns[0].mAddress == 1 && mFalseJump->mIns[0].mAddress == 0)
{
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_ROL, ASMIM_IMPLIED));
mIns.Push(NativeCodeInstruction(ASMIT_EOR, ASMIM_IMMEDIATE, 1));
mBranch = ASMIT_JMP;
mTrueJump = mTrueJump->mTrueJump;
mFalseJump = nullptr;
changed = true;
}
}
#endif
#endif #endif
if (this->mTrueJump && this->mTrueJump->PeepHoleOptimizer(proc, pass)) if (this->mTrueJump && this->mTrueJump->PeepHoleOptimizer(proc, pass))
@ -20186,12 +20324,14 @@ void NativeCodeProcedure::Optimize(void)
#endif #endif
#endif #endif
#if 1
if (step == 5) if (step == 5)
{ {
ResetVisited(); ResetVisited();
if (mEntryBlock->AlternateXYUsage()) if (mEntryBlock->AlternateXYUsage())
changed = true; changed = true;
} }
#endif
#if 1 #if 1
ResetVisited(); ResetVisited();

View File

@ -109,7 +109,7 @@ public:
bool ChangesGlobalMemory(void) const; bool ChangesGlobalMemory(void) const;
bool SameEffectiveAddress(const NativeCodeInstruction& ins) const; bool SameEffectiveAddress(const NativeCodeInstruction& ins) const;
bool MayBeChangedOnAddress(const NativeCodeInstruction& ins) const; bool MayBeChangedOnAddress(const NativeCodeInstruction& ins) const;
bool MayBeSameAddress(const NativeCodeInstruction& ins) const; bool MayBeSameAddress(const NativeCodeInstruction& ins, bool sameXY = false) const;
bool IsSame(const NativeCodeInstruction& ins) const; bool IsSame(const NativeCodeInstruction& ins) const;
bool IsCommutative(void) const; bool IsCommutative(void) const;
bool IsShift(void) const; bool IsShift(void) const;

View File

@ -73,7 +73,7 @@ int main2(int argc, const char** argv)
#else #else
strcpy(strProductName, "oscar64"); strcpy(strProductName, "oscar64");
strcpy(strProductVersion, "1.5.108"); strcpy(strProductVersion, "1.5.109");
#ifdef __APPLE__ #ifdef __APPLE__
uint32_t length = sizeof(basePath); uint32_t length = sizeof(basePath);

View File

@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,5,108,0 FILEVERSION 1,5,109,0
PRODUCTVERSION 1,5,108,0 PRODUCTVERSION 1,5,109,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -43,12 +43,12 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "oscar64" VALUE "CompanyName", "oscar64"
VALUE "FileDescription", "oscar64 compiler" VALUE "FileDescription", "oscar64 compiler"
VALUE "FileVersion", "1.5.108.0" VALUE "FileVersion", "1.5.109.0"
VALUE "InternalName", "oscar64.exe" VALUE "InternalName", "oscar64.exe"
VALUE "LegalCopyright", "Copyright (C) 2021" VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "oscar64.exe" VALUE "OriginalFilename", "oscar64.exe"
VALUE "ProductName", "oscar64" VALUE "ProductName", "oscar64"
VALUE "ProductVersion", "1.5.108.0" VALUE "ProductVersion", "1.5.109.0"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"

View File

@ -4023,15 +4023,15 @@
{ {
"Name" = "8:Microsoft Visual Studio" "Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:oscar64" "ProductName" = "8:oscar64"
"ProductCode" = "8:{EE66F8BE-2269-4D71-A6A6-CC21A650858D}" "ProductCode" = "8:{5BB56FF9-6768-4927-AF3F-EE7248AAF646}"
"PackageCode" = "8:{AAC14556-801D-484D-8285-9DE33FBBA6FE}" "PackageCode" = "8:{BBAC5D24-DB07-4725-AEA9-031CB1236496}"
"UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}" "UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}"
"AspNetVersion" = "8:2.0.50727.0" "AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE" "RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE" "RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE" "InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.5.108" "ProductVersion" = "8:1.5.109"
"Manufacturer" = "8:oscar64" "Manufacturer" = "8:oscar64"
"ARPHELPTELEPHONE" = "8:" "ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:" "ARPHELPLINK" = "8:"