Intermediate code generator cleanup
This commit is contained in:
parent
fc7bb2c377
commit
5000d521a1
|
@ -205,9 +205,11 @@ public:
|
||||||
|
|
||||||
GrowingArray & operator=(const GrowingArray& a)
|
GrowingArray & operator=(const GrowingArray& a)
|
||||||
{
|
{
|
||||||
|
if (a.size != size)
|
||||||
Grow(a.size, true);
|
Grow(a.size, true);
|
||||||
int i;
|
|
||||||
for (i = 0; i < size; i++) array[i] = a.array[i];
|
for (int i = 0; i < size; i++) array[i] = a.array[i];
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11191,6 +11191,182 @@ void InterCodeProcedure::RemoveUnusedStoreInstructions(InterMemory paramMemory)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::MergeCommonPathInstructions(void)
|
||||||
|
{
|
||||||
|
bool changed;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->CompactInstructions();
|
||||||
|
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
changed = mEntryBlock->MergeCommonPathInstructions();
|
||||||
|
|
||||||
|
DisassembleDebug("Merged common path part");
|
||||||
|
|
||||||
|
if (changed)
|
||||||
|
{
|
||||||
|
TempForwarding();
|
||||||
|
RemoveUnusedInstructions();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (changed);
|
||||||
|
|
||||||
|
DisassembleDebug("Merged common path instructions");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::PushSinglePathResultInstructions(void)
|
||||||
|
{
|
||||||
|
bool changed;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
changed = mEntryBlock->PushSinglePathResultInstructions();
|
||||||
|
|
||||||
|
DisassembleDebug("Pushed single path result");
|
||||||
|
|
||||||
|
} while (changed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::PromoteSimpleLocalsToTemp(InterMemory paramMemory, int nlocals, int nparams)
|
||||||
|
{
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->CollectVariables(mModule->mGlobalVars, mLocalVars, mParamVars, paramMemory);
|
||||||
|
|
||||||
|
RemoveUnusedStoreInstructions(paramMemory);
|
||||||
|
|
||||||
|
for (int j = 0; j < 2; j++)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Promote local variables to temporaries
|
||||||
|
//
|
||||||
|
|
||||||
|
FastNumberSet simpleLocals(nlocals), complexLocals(nlocals);
|
||||||
|
GrowingTypeArray localTypes(IT_NONE);
|
||||||
|
|
||||||
|
FastNumberSet simpleParams(nparams), complexParams(nparams);
|
||||||
|
GrowingTypeArray paramTypes(IT_NONE);
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->CollectSimpleLocals(complexLocals, simpleLocals, localTypes, complexParams, simpleParams, paramTypes);
|
||||||
|
|
||||||
|
for (int i = 0; i < simpleLocals.Num(); i++)
|
||||||
|
{
|
||||||
|
int vi = simpleLocals.Element(i);
|
||||||
|
if (!complexLocals[vi])
|
||||||
|
{
|
||||||
|
mLocalVars[vi]->mTemp = true;
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->SimpleLocalToTemp(vi, AddTemporary(localTypes[vi]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DisassembleDebug("local variables to temps");
|
||||||
|
|
||||||
|
BuildTraces(false);
|
||||||
|
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
RenameTemporaries();
|
||||||
|
|
||||||
|
do {
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
TempForwarding();
|
||||||
|
} while (GlobalConstantPropagation());
|
||||||
|
|
||||||
|
//
|
||||||
|
// Now remove unused instructions
|
||||||
|
//
|
||||||
|
|
||||||
|
RemoveUnusedInstructions();
|
||||||
|
|
||||||
|
DisassembleDebug("removed unused instructions 2");
|
||||||
|
|
||||||
|
TempForwarding();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->CompactInstructions();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::SimplifyIntegerNumeric(FastNumberSet& activeSet)
|
||||||
|
{
|
||||||
|
GrowingInstructionPtrArray silvalues(nullptr);
|
||||||
|
int silvused;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
TempForwarding();
|
||||||
|
RemoveUnusedInstructions();
|
||||||
|
|
||||||
|
activeSet.Clear();
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->CollectActiveTemporaries(activeSet);
|
||||||
|
|
||||||
|
silvused = activeSet.Num();
|
||||||
|
silvalues.SetSize(silvused + 16, true);
|
||||||
|
|
||||||
|
mTemporaries.SetSize(activeSet.Num(), true);
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->ShrinkActiveTemporaries(activeSet, mTemporaries);
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
mEntryBlock->RemapActiveTemporaries(activeSet);
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
} while (mEntryBlock->SimplifyIntegerNumeric(silvalues, silvused));
|
||||||
|
|
||||||
|
assert(silvused == mTemporaries.Size());
|
||||||
|
|
||||||
|
DisassembleDebug("SimplifyIntegerNumeric");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::EliminateAliasValues()
|
||||||
|
{
|
||||||
|
GrowingInstructionPtrArray eivalues(nullptr);
|
||||||
|
do {
|
||||||
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
assert(mTemporaries.Size() == mEntryBlock->mLocalValueRange.Size());
|
||||||
|
|
||||||
|
eivalues.SetSize(mTemporaries.Size(), true);
|
||||||
|
|
||||||
|
ResetVisited();
|
||||||
|
} while (mEntryBlock->EliminateAliasValues(eivalues, eivalues));
|
||||||
|
|
||||||
|
DisassembleDebug("EliminateAliasValues");
|
||||||
|
}
|
||||||
|
|
||||||
|
void InterCodeProcedure::LoadStoreForwarding(InterMemory paramMemory)
|
||||||
|
{
|
||||||
|
bool changed;
|
||||||
|
do {
|
||||||
|
GrowingInstructionPtrArray gipa(nullptr);
|
||||||
|
ResetVisited();
|
||||||
|
changed = mEntryBlock->LoadStoreForwarding(gipa, mModule->mGlobalVars);
|
||||||
|
|
||||||
|
DisassembleDebug("Load/Store forwardingX");
|
||||||
|
|
||||||
|
RemoveUnusedStoreInstructions(paramMemory);
|
||||||
|
|
||||||
|
TempForwarding();
|
||||||
|
RemoveUnusedInstructions();
|
||||||
|
|
||||||
|
DisassembleDebug("Load/Store forwarding");
|
||||||
|
} while (changed);
|
||||||
|
}
|
||||||
|
|
||||||
void InterCodeProcedure::Close(void)
|
void InterCodeProcedure::Close(void)
|
||||||
{
|
{
|
||||||
int i, j, k, start;
|
int i, j, k, start;
|
||||||
|
@ -11321,86 +11497,13 @@ void InterCodeProcedure::Close(void)
|
||||||
// Now remove unused instructions
|
// Now remove unused instructions
|
||||||
//
|
//
|
||||||
|
|
||||||
do {
|
RemoveUnusedInstructions();
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->BuildLocalTempSets(numTemps);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->BuildGlobalProvidedTempSet(NumberSet(numTemps));
|
|
||||||
|
|
||||||
NumberSet totalRequired2(numTemps);
|
|
||||||
|
|
||||||
do {
|
|
||||||
ResetVisited();
|
|
||||||
} while (mEntryBlock->BuildGlobalRequiredTempSet(totalRequired2));
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
} while (mEntryBlock->RemoveUnusedResultInstructions());
|
|
||||||
|
|
||||||
DisassembleDebug("removed unused instructions");
|
DisassembleDebug("removed unused instructions");
|
||||||
|
|
||||||
InterMemory paramMemory = mFastCallProcedure ? IM_FPARAM : IM_PARAM;
|
InterMemory paramMemory = mFastCallProcedure ? IM_FPARAM : IM_PARAM;
|
||||||
|
|
||||||
ResetVisited();
|
PromoteSimpleLocalsToTemp(paramMemory, nlocals, nparams);
|
||||||
mEntryBlock->CollectVariables(mModule->mGlobalVars, mLocalVars, mParamVars, paramMemory);
|
|
||||||
|
|
||||||
RemoveUnusedStoreInstructions(paramMemory);
|
|
||||||
|
|
||||||
for (int j = 0; j < 2; j++)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Promote local variables to temporaries
|
|
||||||
//
|
|
||||||
|
|
||||||
FastNumberSet simpleLocals(nlocals), complexLocals(nlocals);
|
|
||||||
GrowingTypeArray localTypes(IT_NONE);
|
|
||||||
|
|
||||||
FastNumberSet simpleParams(nparams), complexParams(nparams);
|
|
||||||
GrowingTypeArray paramTypes(IT_NONE);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CollectSimpleLocals(complexLocals, simpleLocals, localTypes, complexParams, simpleParams, paramTypes);
|
|
||||||
|
|
||||||
for (int i = 0; i < simpleLocals.Num(); i++)
|
|
||||||
{
|
|
||||||
int vi = simpleLocals.Element(i);
|
|
||||||
if (!complexLocals[vi])
|
|
||||||
{
|
|
||||||
mLocalVars[vi]->mTemp = true;
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->SimpleLocalToTemp(vi, AddTemporary(localTypes[vi]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DisassembleDebug("local variables to temps");
|
|
||||||
|
|
||||||
BuildTraces(false);
|
|
||||||
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
RenameTemporaries();
|
|
||||||
|
|
||||||
do {
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
} while (GlobalConstantPropagation());
|
|
||||||
|
|
||||||
//
|
|
||||||
// Now remove unused instructions
|
|
||||||
//
|
|
||||||
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("removed unused instructions 2");
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CompactInstructions();
|
|
||||||
|
|
||||||
|
|
||||||
BuildDataFlowSets();
|
BuildDataFlowSets();
|
||||||
|
|
||||||
|
@ -11460,18 +11563,8 @@ void InterCodeProcedure::Close(void)
|
||||||
DisassembleDebug("Peephole optimized");
|
DisassembleDebug("Peephole optimized");
|
||||||
|
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
#if 1
|
|
||||||
do
|
|
||||||
{
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
ResetVisited();
|
PushSinglePathResultInstructions();
|
||||||
changed = mEntryBlock->PushSinglePathResultInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Pushed single path result");
|
|
||||||
|
|
||||||
} while (changed);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
BuildDataFlowSets();
|
BuildDataFlowSets();
|
||||||
TempForwarding();
|
TempForwarding();
|
||||||
|
@ -11487,33 +11580,7 @@ void InterCodeProcedure::Close(void)
|
||||||
|
|
||||||
DisassembleDebug("propagate non local used temps");
|
DisassembleDebug("propagate non local used temps");
|
||||||
|
|
||||||
#if 1
|
MergeCommonPathInstructions();
|
||||||
do
|
|
||||||
{
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CompactInstructions();
|
|
||||||
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->MergeCommonPathInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Merged common path part");
|
|
||||||
|
|
||||||
if (changed)
|
|
||||||
{
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (changed);
|
|
||||||
|
|
||||||
DisassembleDebug("Merged common path instructions");
|
|
||||||
#else
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CompactInstructions();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
@ -11526,20 +11593,7 @@ void InterCodeProcedure::Close(void)
|
||||||
|
|
||||||
DisassembleDebug("Copy forwarding");
|
DisassembleDebug("Copy forwarding");
|
||||||
|
|
||||||
do {
|
LoadStoreForwarding(paramMemory);
|
||||||
GrowingInstructionPtrArray gipa(nullptr);
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->LoadStoreForwarding(gipa, mModule->mGlobalVars);
|
|
||||||
|
|
||||||
DisassembleDebug("Load/Store forwardingX");
|
|
||||||
|
|
||||||
RemoveUnusedStoreInstructions(paramMemory);
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Load/Store forwarding");
|
|
||||||
} while (changed);
|
|
||||||
|
|
||||||
FastNumberSet activeSet(numTemps);
|
FastNumberSet activeSet(numTemps);
|
||||||
|
|
||||||
|
@ -11610,53 +11664,11 @@ void InterCodeProcedure::Close(void)
|
||||||
mEntryBlock->CollectEntryBlocks(nullptr);
|
mEntryBlock->CollectEntryBlocks(nullptr);
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
GrowingInstructionPtrArray silvalues(nullptr);
|
SimplifyIntegerNumeric(activeSet);
|
||||||
int silvused;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
activeSet.Clear();
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CollectActiveTemporaries(activeSet);
|
|
||||||
|
|
||||||
silvused = activeSet.Num();
|
|
||||||
silvalues.SetSize(silvused + 16, true);
|
|
||||||
|
|
||||||
mTemporaries.SetSize(activeSet.Num(), true);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->ShrinkActiveTemporaries(activeSet, mTemporaries);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->RemapActiveTemporaries(activeSet);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
} while (mEntryBlock->SimplifyIntegerNumeric(silvalues, silvused));
|
|
||||||
|
|
||||||
assert(silvused == mTemporaries.Size());
|
|
||||||
|
|
||||||
DisassembleDebug("SimplifyIntegerNumeric");
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GrowingInstructionPtrArray eivalues(nullptr);
|
EliminateAliasValues();
|
||||||
do {
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
assert(mTemporaries.Size() == mEntryBlock->mLocalValueRange.Size());
|
|
||||||
|
|
||||||
eivalues.SetSize(mTemporaries.Size(), true);
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
} while (mEntryBlock->EliminateAliasValues(eivalues, eivalues));
|
|
||||||
|
|
||||||
DisassembleDebug("EliminateAliasValues");
|
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
ResetVisited();
|
ResetVisited();
|
||||||
|
@ -11724,20 +11736,7 @@ void InterCodeProcedure::Close(void)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if 1
|
LoadStoreForwarding(paramMemory);
|
||||||
do {
|
|
||||||
GrowingInstructionPtrArray gipa(nullptr);
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->LoadStoreForwarding(gipa, mModule->mGlobalVars);
|
|
||||||
|
|
||||||
RemoveUnusedStoreInstructions(paramMemory);
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Load/Store forwarding2");
|
|
||||||
} while (changed);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
BuildLoopPrefix();
|
BuildLoopPrefix();
|
||||||
|
@ -11906,55 +11905,11 @@ void InterCodeProcedure::Close(void)
|
||||||
TempForwarding();
|
TempForwarding();
|
||||||
} while (GlobalConstantPropagation());
|
} while (GlobalConstantPropagation());
|
||||||
|
|
||||||
do {
|
LoadStoreForwarding(paramMemory);
|
||||||
GrowingInstructionPtrArray gipa(nullptr);
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->LoadStoreForwarding(gipa, mModule->mGlobalVars);
|
|
||||||
|
|
||||||
DisassembleDebug("Load/Store forwardingX");
|
|
||||||
|
|
||||||
RemoveUnusedStoreInstructions(paramMemory);
|
|
||||||
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Load/Store forwarding");
|
|
||||||
} while (changed);
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
ResetVisited();
|
|
||||||
mEntryBlock->CompactInstructions();
|
|
||||||
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->MergeCommonPathInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Merged common path part");
|
|
||||||
|
|
||||||
if (changed)
|
|
||||||
{
|
|
||||||
TempForwarding();
|
|
||||||
RemoveUnusedInstructions();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (changed);
|
|
||||||
|
|
||||||
DisassembleDebug("Merged common path instructions");
|
|
||||||
|
|
||||||
|
MergeCommonPathInstructions();
|
||||||
#if 1
|
#if 1
|
||||||
do
|
PushSinglePathResultInstructions();
|
||||||
{
|
|
||||||
BuildDataFlowSets();
|
|
||||||
|
|
||||||
ResetVisited();
|
|
||||||
changed = mEntryBlock->PushSinglePathResultInstructions();
|
|
||||||
|
|
||||||
DisassembleDebug("Pushed single path result");
|
|
||||||
|
|
||||||
} while (changed);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TempForwarding();
|
TempForwarding();
|
||||||
|
|
|
@ -555,6 +555,12 @@ protected:
|
||||||
void BuildLoopPrefix(void);
|
void BuildLoopPrefix(void);
|
||||||
void SingleAssignmentForwarding(void);
|
void SingleAssignmentForwarding(void);
|
||||||
void RemoveUnusedStoreInstructions(InterMemory paramMemory);
|
void RemoveUnusedStoreInstructions(InterMemory paramMemory);
|
||||||
|
void MergeCommonPathInstructions(void);
|
||||||
|
void PushSinglePathResultInstructions(void);
|
||||||
|
void PromoteSimpleLocalsToTemp(InterMemory paramMemory, int nlocals, int nparams);
|
||||||
|
void SimplifyIntegerNumeric(FastNumberSet& activeSet);
|
||||||
|
void EliminateAliasValues();
|
||||||
|
void LoadStoreForwarding(InterMemory paramMemory);
|
||||||
|
|
||||||
void MergeBasicBlocks(void);
|
void MergeBasicBlocks(void);
|
||||||
|
|
||||||
|
|
|
@ -6098,7 +6098,7 @@ NativeCodeBasicBlock * NativeCodeBasicBlock::CopyValue(InterCodeProcedure* proc,
|
||||||
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp] + 1));
|
mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + proc->mTempOffset[ins->mSrc[1].mTemp] + 1));
|
||||||
mIns.Push(NativeCodeInstruction(ASMIT_ADC, ASMIM_IMMEDIATE, (index >> 8) & 0xff));
|
mIns.Push(NativeCodeInstruction(ASMIT_ADC, ASMIM_IMMEDIATE, (index >> 8) & 0xff));
|
||||||
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, BC_REG_ADDR + 1));
|
mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, BC_REG_ADDR + 1));
|
||||||
sreg = BC_REG_ADDR;
|
dreg = BC_REG_ADDR;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue