Skip to content

Commit

Permalink
Optimize multiply of ranges 0..1
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Aug 22, 2022
1 parent 49330ce commit 4b4c886
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
67 changes: 67 additions & 0 deletions oscar64/NativeCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6571,6 +6571,39 @@ int NativeCodeBasicBlock::ShortMultiply(InterCodeProcedure* proc, NativeCodeProc
{
mul &= 0xffff;

if (ins->mSrc[index].IsUByte() && ins->mSrc[index].mRange.mMaxValue == 1 && mul > 4 && mul < 256)
{
int dreg = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp];
int sreg = BC_REG_TMP + proc->mTempOffset[ins->mSrc[index].mTemp];

if (sins)
{
LoadValueToReg(proc, sins, dreg, nullptr, nullptr);
sreg = dreg;
}

if (mul == 128)
{
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, sreg));
mIns.Push(NativeCodeInstruction(ASMIT_LSR));
mIns.Push(NativeCodeInstruction(ASMIT_ROR));
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg));
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg + 1));
}
else
{
mIns.Push(NativeCodeInstruction(ASMIT_SEC));
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0));
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg + 1));
mIns.Push(NativeCodeInstruction(ASMIT_SBC, ASMIM_ZERO_PAGE, sreg));
mIns.Push(NativeCodeInstruction(ASMIT_AND, ASMIM_IMMEDIATE, mul));
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, dreg));
}

return dreg;
}

int lshift = 0, lmul = mul;
while (!(lmul & 1))
{
Expand Down Expand Up @@ -14212,6 +14245,27 @@ bool NativeCodeBasicBlock::JoinTailCodeSequences(NativeCodeProcedure* proc, bool
}
}

if (eb->HasTailSTX(addr, index))
{
i = 1;
while (i < mEntryBlocks.Size() && mEntryBlocks[i]->HasTailSTX(taddr, tindex) && taddr == addr)
i++;
if (i == mEntryBlocks.Size())
{
mIns.Insert(0, eb->mIns[index]);
mIns[0].mLive |= LIVE_CPU_REG_A | LIVE_CPU_REG_X | LIVE_CPU_REG_Y;
for (int i = 0; i < mEntryBlocks.Size(); i++)
{
NativeCodeBasicBlock* b = mEntryBlocks[i];
b->HasTailSTX(taddr, tindex);
for (int j = tindex + 1; j < b->mIns.Size(); j++)
b->mIns[j].mLive |= LIVE_CPU_REG_X;
b->mIns.Remove(tindex);
}
changed = true;
}
}

break;
}
}
Expand Down Expand Up @@ -26518,6 +26572,19 @@ bool NativeCodeBasicBlock::PeepHoleOptimizer(NativeCodeProcedure* proc, int pass
}
}

else if (sz >= 3 &&
mIns[sz - 3].mType == ASMIT_LDA && mIns[sz - 3].mMode == ASMIM_IMMEDIATE && mIns[sz - 3].mAddress == 0x00 &&
mIns[sz - 2].mType == ASMIT_ROL && mIns[sz - 2].mMode == ASMIM_IMPLIED &&
mIns[sz - 1].mType == ASMIT_CMP && mIns[sz - 1].mMode == ASMIM_IMMEDIATE && mIns[sz - 1].mAddress == 0x01 && !(mIns[sz - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_Z)) && !mExitRequiredRegs[CPU_REG_C])
{
if (mBranch == ASMIT_BCC || mBranch == ASMIT_BCS)
{
mIns.SetSize(sz - 3);
sz -= 3;
changed = true;
}
}

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_SBC && mIns[sz - 1].mMode == ASMIM_IMMEDIATE && mIns[sz - 1].mAddress == 0 && !(mIns[sz - 1].mLive & (LIVE_CPU_REG_A | LIVE_CPU_REG_C)) && !mExitRequiredRegs[CPU_REG_Z])
Expand Down
2 changes: 1 addition & 1 deletion oscar64/oscar64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main2(int argc, const char** argv)

#else
strcpy(strProductName, "oscar64");
strcpy(strProductVersion, "1.8.153");
strcpy(strProductVersion, "1.8.154");

#ifdef __APPLE__
uint32_t length = sizeof(basePath);
Expand Down
8 changes: 4 additions & 4 deletions oscar64/oscar64.rc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,8,153,0
PRODUCTVERSION 1,8,153,0
FILEVERSION 1,8,154,0
PRODUCTVERSION 1,8,154,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -43,12 +43,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "oscar64"
VALUE "FileDescription", "oscar64 compiler"
VALUE "FileVersion", "1.8.153.0"
VALUE "FileVersion", "1.8.154.0"
VALUE "InternalName", "oscar64.exe"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "oscar64.exe"
VALUE "ProductName", "oscar64"
VALUE "ProductVersion", "1.8.153.0"
VALUE "ProductVersion", "1.8.154.0"
END
END
BLOCK "VarFileInfo"
Expand Down
6 changes: 3 additions & 3 deletions oscar64setup/oscar64setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -4231,15 +4231,15 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:oscar64"
"ProductCode" = "8:{DD3A8713-73F3-416C-8D8A-905F9DF55406}"
"PackageCode" = "8:{077A6A53-3869-44C4-9BBE-C0F41C483A9B}"
"ProductCode" = "8:{2B60A66D-2355-44A1-A18F-6D4FFB905B87}"
"PackageCode" = "8:{83F5E3A7-10BF-4D0E-AE7A-D51EBDB9A37E}"
"UpgradeCode" = "8:{9AB61EFF-ACAC-4079-9950-8D96615CD4EF}"
"AspNetVersion" = "8:2.0.50727.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:1.8.153"
"ProductVersion" = "8:1.8.154"
"Manufacturer" = "8:oscar64"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
Expand Down

0 comments on commit 4b4c886

Please sign in to comment.