Additional peephole optimizer rules for removing side-effect free instructions before simple terminations.

This commit is contained in:
Daniel Kirchner 2022-03-15 17:25:22 +01:00
parent 936b07a979
commit 54ab09fee8
2 changed files with 31 additions and 1 deletions

View File

@ -4,6 +4,7 @@ Language Features:
Compiler Features:
* Peephole Optimizer: Remove operations without side effects before simple terminations.
Bugfixes:

View File

@ -146,6 +146,35 @@ struct OpStop: SimplePeepholeOptimizerMethod<OpStop>
}
};
struct OpReturnRevert: SimplePeepholeOptimizerMethod<OpReturnRevert>
{
static bool applySimple(
AssemblyItem const& _op,
AssemblyItem const& _push,
AssemblyItem const& _pushOrDup,
AssemblyItem const& _returnRevert,
std::back_insert_iterator<AssemblyItems> _out
)
{
if (
(_returnRevert == Instruction::RETURN || _returnRevert == Instruction::REVERT) &&
_push.type() == Push &&
(_pushOrDup.type() == Push || _pushOrDup == dupInstruction(1))
)
if (
(_op.type() == Operation && !instructionInfo(_op.instruction()).sideEffects) ||
_op.type() == Push
)
{
*_out = _push;
*_out = _pushOrDup;
*_out = _returnRevert;
return true;
}
return false;
}
};
struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap>
{
static size_t applySimple(AssemblyItem const& _s1, AssemblyItem const& _s2, std::back_insert_iterator<AssemblyItems>)
@ -459,7 +488,7 @@ bool PeepholeOptimiser::optimise()
while (state.i < m_items.size())
applyMethods(
state,
PushPop(), OpPop(), OpStop(), DoublePush(), DoubleSwap(), CommutativeSwap(), SwapComparison(),
PushPop(), OpPop(), OpStop(), OpReturnRevert(), DoublePush(), DoubleSwap(), CommutativeSwap(), SwapComparison(),
DupSwap(), IsZeroIsZeroJumpI(), EqIsZeroJumpI(), DoubleJump(), JumpToNext(), UnreachableCode(),
TagConjunctions(), TruthyAnd(), Identity()
);