Merge pull request #12272 from ethereum/equalStoreEliminator

Equal store eliminator.
This commit is contained in:
chriseth
2022-01-05 11:24:59 +01:00
committed by GitHub
27 changed files with 393 additions and 36 deletions
@@ -178,7 +178,7 @@ contract DepositContract is IDepositContract, ERC165 {
// compileViaYul: also
// ----
// constructor()
// gas irOptimized: 1558001
// gas irOptimized: 1557137
// gas legacy: 2436584
// gas legacyOptimized: 1776483
// supportsInterface(bytes4): 0x0 -> 0
@@ -20,7 +20,7 @@ contract test {
// compileViaYul: also
// ----
// set(uint8,uint8,uint8,uint8,uint8): 1, 21, 22, 42, 43 -> 0, 0, 0, 0
// gas irOptimized: 111965
// gas irOptimized: 111896
// gas legacy: 113806
// gas legacyOptimized: 111781
// get(uint8): 1 -> 21, 22, 42, 43
@@ -38,12 +38,12 @@ contract c {
// compileViaYul: also
// ----
// set(uint256): 7 -> true
// gas irOptimized: 110011
// gas irOptimized: 110119
// gas legacy: 110616
// gas legacyOptimized: 110006
// retrieve(uint256): 7 -> 1, 3, 4, 2
// copy(uint256,uint256): 7, 8 -> true
// gas irOptimized: 118707
// gas irOptimized: 118698
// gas legacy: 119166
// gas legacyOptimized: 118622
// retrieve(uint256): 7 -> 1, 3, 4, 2
+6
View File
@@ -28,6 +28,7 @@
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/EqualStoreEliminator.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
#include <libyul/optimiser/ExpressionSplitter.h>
#include <libyul/optimiser/FunctionGrouper.h>
@@ -236,6 +237,11 @@ YulOptimizerTestCommon::YulOptimizerTestCommon(
ForLoopInitRewriter::run(*m_context, *m_ast);
UnusedAssignEliminator::run(*m_context, *m_ast);
}},
{"equalStoreEliminator", [&]() {
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
EqualStoreEliminator::run(*m_context, *m_ast);
}},
{"ssaPlusCleanup", [&]() {
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
@@ -0,0 +1,25 @@
{
let a := calldataload(0)
let b := 20
sstore(a, b)
if calldataload(32) {
sstore(a, b)
pop(staticcall(0, 0, 0, 0, 0, 0))
sstore(a, b)
}
sstore(a, b)
}
// ====
// EVMVersion: >=byzantium
// ----
// step: equalStoreEliminator
//
// {
// let a := calldataload(0)
// let b := 20
// sstore(a, b)
// if calldataload(32)
// {
// pop(staticcall(0, 0, 0, 0, 0, 0))
// }
// }
@@ -0,0 +1,20 @@
{
let x := calldataload(0)
let y := calldataload(1)
sstore(x, y)
for {let a := 1} lt(a, 10) {a := add(a, 1) } {
sstore(x, y)
}
}
// ----
// step: equalStoreEliminator
//
// {
// let x := calldataload(0)
// let y := calldataload(1)
// sstore(x, y)
// let a := 1
// for { } lt(a, 10) { a := add(a, 1) }
// { sstore(x, y) }
// }
@@ -0,0 +1,56 @@
{
f(calldataload(0), calldataload(32))
h(calldataload(64), calldataload(96))
function f(a, b) {
// gets removed
sstore(a, b)
g()
sstore(a, b)
}
function g() {
pop(staticcall(0, 0, 0, 0, 0, 0))
}
function h(a_, b_) {
// cannot be removed
sstore(a_, b_)
i()
sstore(a_, b_)
}
function i() {
pop(delegatecall(0, 0, 0, 0, 0, 0))
}
}
// ====
// EVMVersion: >=byzantium
// ----
// step: equalStoreEliminator
//
// {
// f(calldataload(0), calldataload(32))
// h(calldataload(64), calldataload(96))
// function f(a, b)
// {
// sstore(a, b)
// g()
// }
// function g()
// {
// pop(staticcall(0, 0, 0, 0, 0, 0))
// }
// function h(a_, b_)
// {
// sstore(a_, b_)
// i()
// sstore(a_, b_)
// }
// function i()
// {
// pop(delegatecall(0, 0, 0, 0, 0, 0))
// }
// }
@@ -0,0 +1,24 @@
{
let x := calldataload(0)
let y := sload(x)
// both of these can be removed
sstore(x, y)
sstore(x, y)
let a := x
let b := mload(a)
// both of these can be removed
mstore(a, b)
mstore(a, b)
}
// ====
// EVMVersion: >=byzantium
// ----
// step: equalStoreEliminator
//
// {
// let x := calldataload(0)
// let y := sload(x)
// let a := x
// let b := mload(a)
// }
@@ -0,0 +1,31 @@
{
let var_k := calldataload(0)
let _1 := 0x00
let _2 := 0x20
mstore(_1, var_k)
mstore(_2, _1)
sstore(keccak256(_1, 0x40), 0x01)
mstore(_1, var_k)
mstore(_2, _1)
sstore(add(keccak256(_1, 0x40), 0x01), 0x03)
mstore(_1, var_k)
mstore(_2, _1)
sstore(add(keccak256(_1, 0x40), 2), 0x04)
mstore(_1, var_k)
mstore(_2, _1)
sstore(add(keccak256(_1, 0x40), 0x03), 2)
}
// ----
// step: equalStoreEliminator
//
// {
// let var_k := calldataload(0)
// let _1 := 0x00
// let _2 := 0x20
// mstore(_1, var_k)
// mstore(_2, _1)
// sstore(keccak256(_1, 0x40), 0x01)
// sstore(add(keccak256(_1, 0x40), 0x01), 0x03)
// sstore(add(keccak256(_1, 0x40), 2), 0x04)
// sstore(add(keccak256(_1, 0x40), 0x03), 2)
// }
@@ -0,0 +1,18 @@
{
let x := calldataload(0)
let y := calldataload(32)
sstore(x, y)
y := calldataload(64)
// cannot be removed
sstore(x, y)
}
// ----
// step: equalStoreEliminator
//
// {
// let x := calldataload(0)
// let y := calldataload(32)
// sstore(x, y)
// y := calldataload(64)
// sstore(x, y)
// }
@@ -0,0 +1,27 @@
{
let a := calldataload(0)
let b := 20
sstore(a, b)
if calldataload(32) {
sstore(a, b)
pop(staticcall(0, 0, 0, 0, 0, 0))
verbatim_0i_0o("xyz")
}
sstore(a, b)
}
// ====
// EVMVersion: >=byzantium
// ----
// step: equalStoreEliminator
//
// {
// let a := calldataload(0)
// let b := 20
// sstore(a, b)
// if calldataload(32)
// {
// pop(staticcall(0, 0, 0, 0, 0, 0))
// verbatim_0i_0o("xyz")
// }
// sstore(a, b)
// }
@@ -55,7 +55,6 @@
// sstore(0, 0)
// sstore(2, _1)
// extcodecopy(_1, msize(), _1, _1)
// sstore(0, 0)
// sstore(3, _1)
// }
// function gcd(_a, _b) -> out
@@ -20,9 +20,7 @@
// f()
// sstore(0, 1)
// f()
// sstore(0, 1)
// f()
// sstore(0, 1)
// }
// function f()
// {
@@ -18,9 +18,7 @@
// f()
// sstore(0, 1)
// f()
// sstore(0, 1)
// f()
// sstore(0, 1)
// }
// function f()
// {
+1 -1
View File
@@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_strin
BOOST_TEST(chromosome.length() == allSteps.size());
BOOST_TEST(chromosome.optimisationSteps() == allSteps);
BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighFTLMRmVatrpud");
BOOST_TEST(toString(chromosome) == "flcCUnDEvejsxIOoighFTLMRmVatrpud");
}
BOOST_AUTO_TEST_CASE(optimisationSteps_should_translate_chromosomes_genes_to_optimisation_step_names)