Skip to content

Commit 430ecb6

Browse files
authored
Merge pull request #12795 from ethereum/morePopRemovalInPeepholeOptimizer
More pop removal in peephole optimizer.
2 parents cd19023 + 4b9c017 commit 430ecb6

37 files changed

+66
-37
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
);

test/cmdlineTests/function_debug_info_via_yul/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"calldata_array_index_access_uint256_dyn_calldata":
1515
{
16-
"entryPoint": 145,
16+
"entryPoint": 144,
1717
"parameterSlots": 2,
1818
"returnSlots": 1
1919
}

test/cmdlineTests/viair_subobject_optimization/output

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ sub_0: assembly {
125125
eq
126126
tag_4
127127
jumpi
128-
pop
129128
0x00
130129
dup1
131130
revert

test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ contract C is B {
3232
// compileViaYul: also
3333
// ----
3434
// test() -> 77
35-
// gas irOptimized: 119911
35+
// gas irOptimized: 119711
3636
// gas legacy: 155093
3737
// gas legacyOptimized: 111550

test/libsolidity/semanticTests/array/constant_var_as_array_length.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract C {
1111
// compileViaYul: also
1212
// ----
1313
// constructor(): 1, 2, 3 ->
14-
// gas irOptimized: 142856
14+
// gas irOptimized: 142640
1515
// gas legacy: 183490
1616
// gas legacyOptimized: 151938
1717
// a(uint256): 0 -> 1

test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ contract B {
2121
// compileViaYul: also
2222
// ----
2323
// f() -> 2, 3, 4, 5, 6, 1000, 1001, 1002, 1003, 1004
24-
// gas irOptimized: 128110
24+
// gas irOptimized: 127910
2525
// gas legacy: 234719
2626
// gas legacyOptimized: 132639

test/libsolidity/semanticTests/array/function_array_cross_calls.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ contract C {
4545
// compileViaYul: also
4646
// ----
4747
// test() -> 5, 6, 7
48-
// gas irOptimized: 292702
48+
// gas irOptimized: 292502
4949
// gas legacy: 452136
5050
// gas legacyOptimized: 284945

test/libsolidity/semanticTests/array/reusing_memory.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ contract Main {
2626
// compileViaYul: also
2727
// ----
2828
// f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1
29-
// gas irOptimized: 113398
29+
// gas irOptimized: 113198
3030
// gas legacy: 126596
3131
// gas legacyOptimized: 113823

test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ contract Creator {
2626
// compileViaYul: also
2727
// ----
2828
// f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h"
29-
// gas irOptimized: 295403
29+
// gas irOptimized: 293203
3030
// gas legacy: 428711
3131
// gas legacyOptimized: 297922

0 commit comments

Comments
 (0)