Skip to content

Commit 54ab09f

Browse files
committed
Additional peephole optimizer rules for removing side-effect free instructions before simple terminations.
1 parent 936b07a commit 54ab09f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Language Features:
44

55

66
Compiler Features:
7+
* Peephole Optimizer: Remove operations without side effects before simple terminations.
78

89

910
Bugfixes:

libevmasm/PeepholeOptimiser.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ struct OpStop: SimplePeepholeOptimizerMethod<OpStop>
146146
}
147147
};
148148

149+
struct OpReturnRevert: SimplePeepholeOptimizerMethod<OpReturnRevert>
150+
{
151+
static bool applySimple(
152+
AssemblyItem const& _op,
153+
AssemblyItem const& _push,
154+
AssemblyItem const& _pushOrDup,
155+
AssemblyItem const& _returnRevert,
156+
std::back_insert_iterator<AssemblyItems> _out
157+
)
158+
{
159+
if (
160+
(_returnRevert == Instruction::RETURN || _returnRevert == Instruction::REVERT) &&
161+
_push.type() == Push &&
162+
(_pushOrDup.type() == Push || _pushOrDup == dupInstruction(1))
163+
)
164+
if (
165+
(_op.type() == Operation && !instructionInfo(_op.instruction()).sideEffects) ||
166+
_op.type() == Push
167+
)
168+
{
169+
*_out = _push;
170+
*_out = _pushOrDup;
171+
*_out = _returnRevert;
172+
return true;
173+
}
174+
return false;
175+
}
176+
};
177+
149178
struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap>
150179
{
151180
static size_t applySimple(AssemblyItem const& _s1, AssemblyItem const& _s2, std::back_insert_iterator<AssemblyItems>)
@@ -459,7 +488,7 @@ bool PeepholeOptimiser::optimise()
459488
while (state.i < m_items.size())
460489
applyMethods(
461490
state,
462-
PushPop(), OpPop(), OpStop(), DoublePush(), DoubleSwap(), CommutativeSwap(), SwapComparison(),
491+
PushPop(), OpPop(), OpStop(), OpReturnRevert(), DoublePush(), DoubleSwap(), CommutativeSwap(), SwapComparison(),
463492
DupSwap(), IsZeroIsZeroJumpI(), EqIsZeroJumpI(), DoubleJump(), JumpToNext(), UnreachableCode(),
464493
TagConjunctions(), TruthyAnd(), Identity()
465494
);

0 commit comments

Comments
 (0)