From c3b46d6a780c4ac480d50b13a35351498606c439 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:31:31 +0200 Subject: [PATCH] Fix condition hoisting side path check --- oscar64/InterCode.cpp | 57 ++++++++++++++++++++++++++++++++++--------- oscar64/InterCode.h | 1 + oscar64/Parser.cpp | 8 +++--- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 1459caa..88d6dda 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -13291,6 +13291,26 @@ bool InterCodeBasicBlock::IsDirectDominatorBlock(InterCodeBasicBlock* block) return true; } +bool InterCodeBasicBlock::CollectBlocksToDominator(InterCodeBasicBlock* dblock, ExpandingArray& body) +{ + if (this == dblock) + return true; + + if (mLoopHead) + return false; + + if (mEntryBlocks.Size() == 0) + return false; + + body.Push(this); + + for (int i = 0; i < mEntryBlocks.Size(); i++) + if (!mEntryBlocks[i]->CollectBlocksToDominator(dblock, body)) + return false; + + return true; +} + bool InterCodeBasicBlock::HoistCommonConditionalPath(void) { bool changed = false; @@ -13316,6 +13336,11 @@ bool InterCodeBasicBlock::HoistCommonConditionalPath(void) if (cblock && cblock->mNumEntries == 1) { + ExpandingArray pblocks; + eblock->CollectBlocksToDominator(this, pblocks); + pblocks.RemoveAll(cblock); + pblocks.RemoveAll(eblock); + for (int i = 0; i < cblock->mInstructions.Size(); i++) { InterInstruction* ins = cblock->mInstructions[i]; @@ -13328,19 +13353,25 @@ bool InterCodeBasicBlock::HoistCommonConditionalPath(void) if (j < eblock->mInstructions.Size() && !eblock->IsTempModifiedInRange(0, j, ins->mDst.mTemp) && eblock->CanMoveInstructionBeforeBlock(j) && cblock->CanMoveInstructionBeforeBlock(cblock->mInstructions.Size(), eblock->mInstructions[j])) { - eblock->mInstructions[j]->mCode = IC_LOAD_TEMPORARY; - eblock->mInstructions[j]->mSrc[0] = ins->mDst; - eblock->mInstructions[j]->mNumOperands = 1; + int k = 0; + while (k < pblocks.Size() && !pblocks[k]->IsTempModified(ins->mDst.mTemp)) + k++; + if (k == pblocks.Size()) + { + eblock->mInstructions[j]->mCode = IC_LOAD_TEMPORARY; + eblock->mInstructions[j]->mSrc[0] = ins->mDst; + eblock->mInstructions[j]->mNumOperands = 1; - mInstructions.Insert(mInstructions.Size() - 1, ins); - cblock->mInstructions.Remove(i); + mInstructions.Insert(mInstructions.Size() - 1, ins); + cblock->mInstructions.Remove(i); - mExitRequiredTemps += ins->mDst.mTemp; - cblock->mEntryRequiredTemps += ins->mDst.mTemp; - cblock->mExitRequiredTemps += ins->mDst.mTemp; - eblock->mEntryRequiredTemps += ins->mDst.mTemp; - i--; - changed = true; + mExitRequiredTemps += ins->mDst.mTemp; + cblock->mEntryRequiredTemps += ins->mDst.mTemp; + cblock->mExitRequiredTemps += ins->mDst.mTemp; + eblock->mEntryRequiredTemps += ins->mDst.mTemp; + i--; + changed = true; + } } } } @@ -22568,7 +22599,7 @@ void InterCodeProcedure::Close(void) { GrowingTypeArray tstack(IT_NONE); - CheckFunc = !strcmp(mIdent->mString, "foo"); + CheckFunc = !strcmp(mIdent->mString, "Test::move"); CheckCase = false; mEntryBlock = mBlocks[0]; @@ -22963,6 +22994,7 @@ void InterCodeProcedure::Close(void) #endif + DisassembleDebug("PreHoistCommonConditionalPath"); HoistCommonConditionalPath(); DisassembleDebug("HoistCommonConditionalPath"); @@ -24232,6 +24264,7 @@ void InterCodeProcedure::HoistCommonConditionalPath(void) ResetVisited(); if (!mEntryBlock->HoistCommonConditionalPath()) return; + Disassemble("InnerHoist"); TempForwarding(); RemoveUnusedInstructions(); } diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index 455e0c0..c88b4fe 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -549,6 +549,7 @@ public: bool IsDominator(InterCodeBasicBlock* block); bool IsDirectDominatorBlock(InterCodeBasicBlock* block); bool IsDirectLoopPathBlock(InterCodeBasicBlock* block); + bool CollectBlocksToDominator(InterCodeBasicBlock* dblock, ExpandingArray& body); void MarkRelevantStatics(void); void RemoveNonRelevantStatics(void); diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index c201d50..cc3b69d 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -1735,7 +1735,7 @@ Expression* Parser::ParseVarInitExpression(Expression* vexp, bool inner) Declaration * dec = new Declaration(mScanner->mLocation, DT_CONST_DATA); dec->mBase = dtype; dec->mSize = dtype->mSize; - dec->mSection = mDataSection; + dec->mSection = mCodeSection; dec->mData = d; @@ -1816,7 +1816,7 @@ Expression* Parser::ParseVarInitExpression(Expression* vexp, bool inner) Declaration * csdec = new Declaration(mScanner->mLocation, DT_CONST_STRUCT); csdec->mBase = dtype; csdec->mSize = dtype->mSize; - csdec->mSection = mDataSection; + csdec->mSection = mCodeSection; Declaration* last = nullptr; @@ -5436,7 +5436,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex } else if (pthis) { - ndec->mFlags |= storageFlags & DTF_STATIC; + ndec->mFlags |= storageFlags & (DTF_STATIC | DTF_PREVENT_INLINE); } ndec->mOffset = 0; @@ -5746,7 +5746,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex ndec->mValue = ParseFunction(ndec->mBase); mFunction = nullptr; - if (pthis) + if (pthis && !(storageFlags & DTF_PREVENT_INLINE)) ndec->mFlags |= DTF_REQUEST_INLINE; ndec->mFlags |= DTF_DEFINED;