Merge pull request #5203 from ethereum/moveMoreYulTests

Move more yul optimizer tests
This commit is contained in:
chriseth 2018-10-12 15:15:41 +02:00 committed by GitHub
commit 1d312c8e40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
93 changed files with 1395 additions and 1271 deletions

View File

@ -1,102 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Unit tests for the common subexpression eliminator optimizer stage.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/CommonSubexpressionEliminator.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>
using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p;\
Block b = disambiguate(_original, false);\
(CommonSubexpressionEliminator{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation, false));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulCSE)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(trivial)
{
CHECK(
"{ let a := mul(1, codesize()) let b := mul(1, codesize()) }",
"{ let a := mul(1, codesize()) let b := a }"
);
}
BOOST_AUTO_TEST_CASE(non_movable_instr)
{
CHECK(
"{ let a := mload(1) let b := mload(1) }",
"{ let a := mload(1) let b := mload(1) }"
);
}
BOOST_AUTO_TEST_CASE(non_movable_instr2)
{
CHECK(
"{ let a := gas() let b := gas() }",
"{ let a := gas() let b := gas() }"
);
}
BOOST_AUTO_TEST_CASE(branches_if)
{
CHECK(
"{ let b := 1 if b { b := 1 } let c := 1 }",
"{ let b := 1 if b { b := b } let c := 1 }"
);
}
BOOST_AUTO_TEST_CASE(branches_for)
{
CHECK(
"{ let a := 1 let b := codesize()"
"for { } lt(1, codesize()) { mstore(1, codesize()) a := add(a, codesize()) }"
"{ mstore(1, codesize()) } mstore(1, codesize()) }",
"{ let a := 1 let b := codesize()"
"for { } lt(1, b) { mstore(1, b) a := add(a, b) }"
"{ mstore(1, b) } mstore(1, b) }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,156 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Unit tests for the expression breaker.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/ExpressionSplitter.h>
#include <libjulia/optimiser/NameCollector.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>
using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
auto result = parse(_original, false);\
NameDispenser nameDispenser;\
nameDispenser.m_usedNames = NameCollector(*result.first).names();\
ExpressionSplitter{nameDispenser}(*result.first);\
BOOST_CHECK_EQUAL(assembly::AsmPrinter{}(*result.first), format(_expectation, false));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulExpressionSplitter)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(trivial)
{
CHECK(
"{ mstore(add(calldataload(2), mload(3)), 8) }",
"{ let _1 := mload(3) let _2 := calldataload(2) let _3 := add(_2, _1) mstore(_3, 8) }"
);
}
BOOST_AUTO_TEST_CASE(control_flow)
{
string input = R"({
let x := calldataload(0)
if mul(add(x, 2), 3) {
for { let a := 2 } lt(a, mload(a)) { a := add(a, mul(a, 2)) } {
let b := mul(add(a, 2), 4)
sstore(b, mul(b, 2))
}
}
})";
string expectation = R"({
let x := calldataload(0)
let _1 := add(x, 2)
let _2 := mul(_1, 3)
if _2
{
for { let a := 2 } lt(a, mload(a))
{
let _3 := mul(a, 2)
a := add(a, _3)
}
{
let _4 := add(a, 2)
let b := mul(_4, 4)
let _5 := mul(b, 2)
sstore(b, _5)
}
}
})";
CHECK(input, expectation);
}
BOOST_AUTO_TEST_CASE(switch_)
{
string input = R"({
let x := 8
switch add(2, calldataload(0))
case 0 { sstore(0, mload(2)) }
default { mstore(0, mload(3)) }
x := add(mload(3), 4)
})";
string expectation = R"({
let x := 8
let _1 := calldataload(0)
let _2 := add(2, _1)
switch _2
case 0 {
let _3 := mload(2)
sstore(0, _3)
}
default {
let _4 := mload(3)
mstore(0, _4)
}
let _5 := mload(3)
x := add(_5, 4)
})";
CHECK(input, expectation);
}
BOOST_AUTO_TEST_CASE(inside_function)
{
string input = R"({
let x := mul(f(0, mload(7)), 3)
function f(a, b) -> c {
c := mul(a, mload(add(b, c)))
}
sstore(x, f(mload(2), mload(2)))
})";
string expectation = R"({
let _1 := mload(7)
let _2 := f(0, _1)
let x := mul(_2, 3)
function f(a, b) -> c
{
let _3 := add(b, c)
let _4 := mload(_3)
c := mul(a, _4)
}
let _5 := mload(2)
let _6 := mload(2)
let _7 := f(_6, _5)
sstore(x, _7)
})";
CHECK(input, expectation);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,85 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2017
* Unit tests for the Yul function grouper.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/FunctionGrouper.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p(true);\
Block b = disambiguate(_original);\
(FunctionGrouper{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulFunctionGrouper)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ { } }");
}
BOOST_AUTO_TEST_CASE(single_fun)
{
CHECK(
"{ let a:u256 function f() {} }",
"{ { let a:u256 } function f() {} }"
);
}
BOOST_AUTO_TEST_CASE(multi_fun_mixed)
{
CHECK(
"{ let a:u256 function f() { let b:u256 } let c:u256 function g() { let d:u256 } let e:u256 }",
"{ { let a:u256 let c:u256 let e:u256 } function f() { let b:u256 } function g() { let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(nested_fun)
{
CHECK(
"{ let a:u256 function f() { let b:u256 function g() { let c:u256} let d:u256 } }",
"{ { let a:u256 } function f() { let b:u256 function g() { let c:u256} let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(empty_block)
{
CHECK(
"{ let a:u256 { } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }",
"{ { let a:u256 { } } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,85 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2017
* Unit tests for the Yul function hoister.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/FunctionHoister.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p(true);\
Block b = disambiguate(_original);\
(FunctionHoister{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulFunctionHoister)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(single_fun)
{
CHECK(
"{ let a:u256 function f() {} }",
"{ let a:u256 function f() {} }"
);
}
BOOST_AUTO_TEST_CASE(multi_fun_mixed)
{
CHECK(
"{ let a:u256 function f() { let b:u256 } let c:u256 function g() { let d:u256 } let e:u256 }",
"{ let a:u256 let c:u256 let e:u256 function f() { let b:u256 } function g() { let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(nested_fun)
{
CHECK(
"{ let a:u256 function f() { let b:u256 function g() { let c:u256} let d:u256 } }",
"{ let a:u256 function g() { let c:u256 } function f() { let b:u256 let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(empty_block)
{
CHECK(
"{ let a:u256 { } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }",
"{ let a:u256 function f() -> x:bool { let b:u256 := 4:u256 for {} f() {} {} } }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -55,20 +55,6 @@ string inlinableFunctions(string const& _source)
);
}
string inlineFunctions(string const& _source, bool _yul = true)
{
auto ast = disambiguate(_source, _yul);
ExpressionInliner(ast).run();
return assembly::AsmPrinter(_yul)(ast);
}
string fullInline(string const& _source, bool _yul = true)
{
Block ast = disambiguate(_source, _yul);
(FunctionHoister{})(ast);
(FunctionGrouper{})(ast);\
FullInliner(ast).run();
return assembly::AsmPrinter(_yul)(ast);
}
}
@ -122,260 +108,4 @@ BOOST_AUTO_TEST_CASE(negative)
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(YulFunctionInliner)
BOOST_AUTO_TEST_CASE(simple)
{
BOOST_CHECK_EQUAL(
inlineFunctions("{ function f() -> x:u256 { x := 2:u256 } let y:u256 := f() }"),
format("{ function f() -> x:u256 { x := 2:u256 } let y:u256 := 2:u256 }")
);
}
BOOST_AUTO_TEST_CASE(with_args)
{
BOOST_CHECK_EQUAL(
inlineFunctions("{ function f(a:u256) -> x:u256 { x := a } let y:u256 := f(7:u256) }"),
format("{ function f(a:u256) -> x:u256 { x := a } let y:u256 := 7:u256 }")
);
}
BOOST_AUTO_TEST_CASE(no_inline_with_mload)
{
// Does not inline because mload could be moved out of sequence
BOOST_CHECK_EQUAL(
inlineFunctions("{ function f(a) -> x { x := a } let y := f(mload(2)) }", false),
format("{ function f(a) -> x { x := a } let y := f(mload(2)) }", false)
);
}
BOOST_AUTO_TEST_CASE(no_move_with_side_effects)
{
// The calls to g and h cannot be moved because g and h are not movable. Therefore, the call
// to f is not inlined.
BOOST_CHECK_EQUAL(
inlineFunctions("{"
"function f(a, b) -> x { x := add(b, a) }"
"function g() -> y { y := mload(0) mstore(0, 4) }"
"function h() -> z { mstore(0, 4) z := mload(0) }"
"let r := f(g(), h())"
"}", false),
format("{"
"function f(a, b) -> x { x := add(b, a) }"
"function g() -> y { y := mload(0) mstore(0, 4) }"
"function h() -> z { mstore(0, 4) z := mload(0) }"
"let r := f(g(), h())"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(complex_with_evm)
{
BOOST_CHECK_EQUAL(
inlineFunctions("{ function f(a) -> x { x := add(a, a) } let y := f(calldatasize()) }", false),
format("{ function f(a) -> x { x := add(a, a) } let y := add(calldatasize(), calldatasize()) }", false)
);
}
BOOST_AUTO_TEST_CASE(double_calls)
{
BOOST_CHECK_EQUAL(
inlineFunctions("{"
"function f(a) -> x { x := add(a, a) }"
"function g(b, c) -> y { y := mul(mload(c), f(b)) }"
"let y := g(calldatasize(), 7)"
"}", false),
format("{"
"function f(a) -> x { x := add(a, a) }"
"function g(b, c) -> y { y := mul(mload(c), add(b, b)) }"
"let y_1 := mul(mload(7), add(calldatasize(), calldatasize()))"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(double_recursive_calls)
{
BOOST_CHECK_EQUAL(
inlineFunctions("{"
"function f(a, r) -> x { x := g(a, g(r, r)) }"
"function g(b, s) -> y { y := f(b, f(s, s)) }"
"let y := g(calldatasize(), 7)"
"}", false),
format("{"
"function f(a, r) -> x { x := g(a, f(r, f(r, r))) }"
"function g(b, s) -> y { y := f(b, g(s, f(s, f(s, s))))}"
"let y_1 := f(calldatasize(), g(7, f(7, f(7, 7))))"
"}", false)
);
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(YulFullInliner)
BOOST_AUTO_TEST_CASE(simple)
{
BOOST_CHECK_EQUAL(
fullInline("{"
"function f(a) -> x { let r := mul(a, a) x := add(r, r) }"
"let y := add(f(sload(mload(2))), mload(7))"
"}", false),
format("{"
"{"
"let _1 := mload(7)"
"let f_a := sload(mload(2))"
"let f_x"
"{"
"let f_r := mul(f_a, f_a)"
"f_x := add(f_r, f_r)"
"}"
"let y := add(f_x, _1)"
"}"
"function f(a) -> x"
"{"
"let r := mul(a, a)"
"x := add(r, r)"
"}"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(multi_fun)
{
BOOST_CHECK_EQUAL(
fullInline("{"
"function f(a) -> x { x := add(a, a) }"
"function g(b, c) -> y { y := mul(mload(c), f(b)) }"
"let y := g(f(3), 7)"
"}", false),
format("{"
"{"
"let g_c := 7 "
"let f_a_1 := 3 "
"let f_x_1 "
"{ f_x_1 := add(f_a_1, f_a_1) } "
"let g_y "
"{"
"let g_f_a := f_x_1 "
"let g_f_x "
"{"
"g_f_x := add(g_f_a, g_f_a)"
"}"
"g_y := mul(mload(g_c), g_f_x)"
"}"
"let y_1 := g_y"
"}"
"function f(a) -> x"
"{"
"x := add(a, a)"
"}"
"function g(b, c) -> y"
"{"
"let f_a := b "
"let f_x "
"{"
"f_x := add(f_a, f_a)"
"}"
"y := mul(mload(c), f_x)"
"}"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(move_up_rightwards_arguments)
{
BOOST_CHECK_EQUAL(
fullInline("{"
"function f(a, b, c) -> x { x := add(a, b) x := mul(x, c) }"
"let y := add(mload(1), add(f(mload(2), mload(3), mload(4)), mload(5)))"
"}", false),
format("{"
"{"
"let _1 := mload(5)"
"let f_c := mload(4)"
"let f_b := mload(3)"
"let f_a := mload(2)"
"let f_x"
"{"
"f_x := add(f_a, f_b)"
"f_x := mul(f_x, f_c)"
"}"
"let y := add(mload(1), add(f_x, _1))"
"}"
"function f(a, b, c) -> x"
"{"
"x := add(a, b)"
"x := mul(x, c)"
"}"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(pop_result)
{
// This tests that `pop(r)` is removed.
BOOST_CHECK_EQUAL(
fullInline("{"
"function f(a) -> x { let r := mul(a, a) x := add(r, r) }"
"pop(add(f(7), 2))"
"}", false),
format("{"
"{"
"let _1 := 2 "
"let f_a := 7 "
"let f_x "
"{"
"let f_r := mul(f_a, f_a) "
"f_x := add(f_r, f_r)"
"}"
"{"
"}"
"}"
"function f(a) -> x"
"{"
"let r := mul(a, a) "
"x := add(r, r)"
"}"
"}", false)
);
}
BOOST_AUTO_TEST_CASE(inside_condition)
{
// This tests that breaking the expression inside the condition works properly.
BOOST_CHECK_EQUAL(
fullInline("{"
"if gt(f(mload(1)), mload(0)) {"
"sstore(0, 2)"
"}"
"function f(a) -> r {"
"a := mload(a)"
"r := add(a, calldatasize())"
"}"
"}", false),
format("{"
"{"
"let _1 := mload(0)"
"let f_a := mload(1)"
"let f_r"
"{"
"f_a := mload(f_a)"
"f_r := add(f_a, calldatasize())"
"}"
"if gt(f_r, _1)"
"{"
"sstore(0, 2)"
"}"
"}"
"function f(a) -> r"
"{"
"a := mload(a)"
"r := add(a, calldatasize())"
"}"
"}", false)
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,87 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2018
* Unit tests for the Yul MainFunction transformation.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/FunctionGrouper.h>
#include <libjulia/optimiser/MainFunction.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p(true);\
Block b = disambiguate(_original);\
(FunctionGrouper{})(b);\
(MainFunction{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulMainFunction)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ function main() { } }");
}
BOOST_AUTO_TEST_CASE(single_fun)
{
CHECK(
"{ let a:u256 function f() {} }",
"{ function main() { let a:u256 } function f() {} }"
);
}
BOOST_AUTO_TEST_CASE(multi_fun_mixed)
{
CHECK(
"{ let a:u256 function f() { let b:u256 } let c:u256 function g() { let d:u256 } let e:u256 }",
"{ function main() { let a:u256 let c:u256 let e:u256 } function f() { let b:u256 } function g() { let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(nested_fun)
{
CHECK(
"{ let a:u256 function f() { let b:u256 function g() { let c:u256} let d:u256 } }",
"{ function main() { let a:u256 } function f() { let b:u256 function g() { let c:u256} let d:u256 } }"
);
}
BOOST_AUTO_TEST_CASE(empty_block)
{
CHECK(
"{ let a:u256 { } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }",
"{ function main() { let a:u256 { } } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,179 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2017
* Unit tests for the rematerialiser optimizer stage.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/Rematerialiser.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>
using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p;\
Block b = disambiguate(_original, false);\
(Rematerialiser{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation, false));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulRematerialiser)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(trivial)
{
CHECK(
"{ let a := 1 let b := a mstore(0, b) }",
"{ let a := 1 let b := 1 mstore(0, 1) }"
);
}
BOOST_AUTO_TEST_CASE(expression)
{
CHECK(
"{ let a := add(mul(calldatasize(), 2), number()) let b := add(a, a) }",
"{ let a := add(mul(calldatasize(), 2), number()) let b := add("
"add(mul(calldatasize(), 2), number()),"
"add(mul(calldatasize(), 2), number())"
") }"
);
}
BOOST_AUTO_TEST_CASE(reassign)
{
CHECK(
"{ let a := extcodesize(0) let b := a let c := b a := 2 let d := add(b, c) pop(a) pop(b) pop(c) pop(d) }",
"{ let a := extcodesize(0) let b := a let c := a a := 2 let d := add(b, c) pop(2) pop(b) pop(c) pop(add(b, c)) }"
);
}
BOOST_AUTO_TEST_CASE(non_movable_instr)
{
CHECK(
"{ let a := 1 let b := mload(a) let c := a mstore(add(a, b), c) }",
"{ let a := 1 let b := mload(1) let c := 1 mstore(add(1, b), 1) }"
);
}
BOOST_AUTO_TEST_CASE(non_movable_fun)
{
CHECK(
"{ function f(x) -> y {} let a := 1 let b := f(a) let c := a mstore(add(a, b), c) }",
"{ function f(x) -> y {} let a := 1 let b := f(1) let c := 1 mstore(add(1, b), 1) }"
);
}
BOOST_AUTO_TEST_CASE(branches_if)
{
CHECK(
"{ let a := 1 let b := 2 if b { pop(b) b := a } let c := b }",
"{ let a := 1 let b := 2 if 2 { pop(2) b := 1 } let c := b }"
);
}
BOOST_AUTO_TEST_CASE(branches_switch)
{
CHECK(
"{ let a := 1 let b := 2 switch number() case 1 { b := a } default { let x := a let y := b b := a } pop(add(a, b)) }",
"{ let a := 1 let b := 2 switch number() case 1 { b := 1 } default { let x := 1 let y := b b := 1 } pop(add(1, b)) }"
);
}
BOOST_AUTO_TEST_CASE(branches_for)
{
CHECK(
"{ let a := 1 for { pop(a) } a { pop(a) } { pop(a) } }",
"{ let a := 1 for { pop(1) } 1 { pop(1) } { pop(1) } }"
);
CHECK(
"{ let a := 1 for { pop(a) } a { pop(a) } { a := 7 let c := a } let x := a }",
"{ let a := 1 for { pop(1) } a { pop(7) } { a := 7 let c := 7 } let x := a }"
);
}
BOOST_AUTO_TEST_CASE(branches_for_declared_in_init)
{
CHECK(
"{ let b := 0 for { let a := 1 pop(a) } a { pop(a) } { b := 1 pop(a) } }",
"{ let b := 0 for { let a := 1 pop(1) } 1 { pop(1) } { b := 1 pop(1) } }"
);
CHECK(
"{ let b := 0 for { let a := 1 pop(a) } lt(a, 0) { pop(a) a := add(a, 3) } { b := 1 pop(a) } }",
"{ let b := 0 for { let a := 1 pop(1) } lt(a, 0) { pop(a) a := add(a, 3) } { b := 1 pop(a) } }"
);
}
BOOST_AUTO_TEST_CASE(reassignment)
{
CHECK(
"{ let a := 1 pop(a) if a { a := 2 } let b := mload(a) pop(b) }",
"{ let a := 1 pop(1) if 1 { a := 2 } let b := mload(a) pop(b) }"
);
}
BOOST_AUTO_TEST_CASE(update_assignment_remat)
{
// We cannot substitute `a` in `let b := a`
CHECK(
"{ let a := extcodesize(0) a := mul(a, 2) let b := a }",
"{ let a := extcodesize(0) a := mul(a, 2) let b := a }"
);
}
BOOST_AUTO_TEST_CASE(do_not_move_out_of_scope)
{
// Cannot replace by `let b := x` by `let b := a` since a is out of scope.
CHECK(
"{ let x { let a := sload(0) x := a } let b := x }",
"{ let x { let a := sload(0) x := a } let b := x }"
);
}
BOOST_AUTO_TEST_CASE(do_not_remat_large_amounts_of_code)
{
CHECK(
"{ let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize())) let b := x }",
"{ let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize())) let b := x }"
);
CHECK(
"{ let x := add(mul(calldataload(2), calldataload(4)), calldatasize()) let b := x }",
"{ let x := add(mul(calldataload(2), calldataload(4)), calldatasize()) let b := add(mul(calldataload(2), calldataload(4)), calldatasize()) }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,178 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2017
* Unit tests for the expression simplifier optimizer stage.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/ExpressionSimplifier.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>
using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p;\
Block b = *(parse(_original, false).first);\
(ExpressionSimplifier{})(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation, false));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulSimplifier)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(constants)
{
CHECK(
"{ let a := add(1, mul(3, 4)) }",
"{ let a := 13 }"
);
}
BOOST_AUTO_TEST_CASE(invariant)
{
CHECK(
"{ let a := mload(sub(7, 7)) let b := sub(a, 0) }",
"{ let a := mload(0) let b := a }"
);
}
BOOST_AUTO_TEST_CASE(reversed)
{
CHECK(
"{ let a := add(0, mload(0)) }",
"{ let a := mload(0) }"
);
}
BOOST_AUTO_TEST_CASE(constant_propagation)
{
CHECK(
"{ let a := add(7, sub(mload(0), 7)) }",
"{ let a := mload(0) }"
);
}
BOOST_AUTO_TEST_CASE(identity_rules_simple)
{
CHECK(
"{ let a := mload(0) let b := sub(a, a) }",
"{ let a := mload(0) let b := 0 }"
);
}
BOOST_AUTO_TEST_CASE(identity_rules_complex)
{
CHECK(
"{ let a := sub(calldataload(0), calldataload(0)) }",
"{ let a := 0 }"
);
}
BOOST_AUTO_TEST_CASE(identity_rules_negative)
{
CHECK(
"{ let a := sub(calldataload(1), calldataload(0)) }",
"{ let a := sub(calldataload(1), calldataload(0)) }"
);
}
BOOST_AUTO_TEST_CASE(including_function_calls)
{
CHECK(
"{ function f() -> a {} let b := add(7, sub(f(), 7)) }",
"{ function f() -> a {} let b := f() }"
);
}
BOOST_AUTO_TEST_CASE(inside_for)
{
CHECK(
"{ for { let a := 10 } iszero(eq(a, 0)) { a := add(a, 1) } {} }",
"{ for { let a := 10 } iszero(iszero(a)) { a := add(a, 1) } {} }"
);
}
BOOST_AUTO_TEST_CASE(mod_and)
{
CHECK(
"{ mstore(0, mod(calldataload(0), exp(2, 8))) }",
"{ mstore(0, and(calldataload(0), 255)) }"
);
CHECK(
"{ mstore(0, mod(calldataload(0), exp(2, 255))) }",
"{ mstore(0, and(calldataload(0), 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)) }"
);
}
BOOST_AUTO_TEST_CASE(not_applied_removes_non_constant_and_not_movable)
{
CHECK(
// The first argument of div is not constant.
// keccak256 is not movable.
"{ let a := div(keccak256(0, 0), 0) }",
"{ let a := div(keccak256(0, 0), 0) }"
);
}
BOOST_AUTO_TEST_CASE(not_applied_function_call_different_names)
{
CHECK(
"{ function f1() -> a { } function f2() -> b {} let c := sub(f1(), f2()) }",
"{ function f1() -> a { } function f2() -> b {} let c := sub(f1(), f2()) }"
);
}
BOOST_AUTO_TEST_CASE(not_applied_function_call_different_arguments)
{
CHECK(
"{ function f(a) -> b { } let c := sub(f(0), f(1)) }",
"{ function f(a) -> b { } let c := sub(f(0), f(1)) }"
);
}
BOOST_AUTO_TEST_CASE(not_applied_function_call_equality_not_movable)
{
CHECK(
// Even if the functions pass the equality check, they are not movable.
"{ function f() -> a { } let b := sub(f(), f()) }",
"{ function f() -> a { } let b := sub(f(), f()) }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1,129 +0,0 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @date 2017
* Unit tests for the pruning of unused variables and functions.
*/
#include <test/libjulia/Common.h>
#include <libjulia/optimiser/UnusedPruner.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <boost/test/unit_test.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>
using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;
#define CHECK(_original, _expectation)\
do\
{\
assembly::AsmPrinter p;\
Block b = disambiguate(_original, false);\
UnusedPruner::runUntilStabilised(b);\
string result = p(b);\
BOOST_CHECK_EQUAL(result, format(_expectation, false));\
}\
while(false)
BOOST_AUTO_TEST_SUITE(YulUnusedPruner)
BOOST_AUTO_TEST_CASE(smoke_test)
{
CHECK("{ }", "{ }");
}
BOOST_AUTO_TEST_CASE(trivial)
{
CHECK(
"{ let a := 1 let b := 1 mstore(0, 1) }",
"{ mstore(0, 1) }"
);
}
BOOST_AUTO_TEST_CASE(multi_declarations)
{
CHECK(
"{ let x, y }",
"{ }"
);
}
BOOST_AUTO_TEST_CASE(multi_assignments)
{
CHECK(
"{ let x, y x := 1 y := 2 }",
"{ let x, y x := 1 y := 2 }"
);
}
BOOST_AUTO_TEST_CASE(multi_partial_assignments)
{
CHECK(
"{ let x, y x := 1 }",
"{ let x, y x := 1 }"
);
}
BOOST_AUTO_TEST_CASE(functions)
{
CHECK(
"{ function f() { let a := 1 } function g() { f() } }",
"{ }"
);
}
BOOST_AUTO_TEST_CASE(intermediate_assignment)
{
CHECK(
"{ let a := 1 a := 4 let b := 1 }",
"{ let a := 1 a := 4 }"
);
}
BOOST_AUTO_TEST_CASE(intermediate_multi_assignment){
CHECK(
"{ let a, b function f() -> x { } a := f() b := 1 }",
"{ let a, b function f() -> x { } a := f() b := 1 }"
);
}
BOOST_AUTO_TEST_CASE(multi_declare)
{
CHECK(
"{ function f() -> x, y { } let a, b := f() }",
"{ function f() -> x, y { } let a, b := f() }"
);
}
BOOST_AUTO_TEST_CASE(multi_assign)
{
CHECK(
"{ let a let b function f() -> x, y { } a, b := f() }",
"{ let a let b function f() -> x, y { } a, b := f() }"
);
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -22,6 +22,18 @@
#include <test/Options.h>
#include <libjulia/optimiser/Disambiguator.h>
#include <libjulia/optimiser/CommonSubexpressionEliminator.h>
#include <libjulia/optimiser/NameCollector.h>
#include <libjulia/optimiser/ExpressionSplitter.h>
#include <libjulia/optimiser/FunctionGrouper.h>
#include <libjulia/optimiser/FunctionHoister.h>
#include <libjulia/optimiser/ExpressionInliner.h>
#include <libjulia/optimiser/FullInliner.h>
#include <libjulia/optimiser/MainFunction.h>
#include <libjulia/optimiser/Rematerialiser.h>
#include <libjulia/optimiser/ExpressionSimplifier.h>
#include <libjulia/optimiser/UnusedPruner.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/inlineasm/AsmPrinter.h>
#include <libsolidity/inlineasm/AsmParser.h>
@ -80,6 +92,60 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
if (m_optimizerStep == "disambiguator")
disambiguate();
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
(CommonSubexpressionEliminator{})(*m_ast);
}
else if (m_optimizerStep == "expressionSplitter")
{
NameDispenser nameDispenser;
nameDispenser.m_usedNames = NameCollector(*m_ast).names();
ExpressionSplitter{nameDispenser}(*m_ast);
}
else if (m_optimizerStep == "functionGrouper")
{
disambiguate();
(FunctionGrouper{})(*m_ast);
}
else if (m_optimizerStep == "functionHoister")
{
disambiguate();
(FunctionHoister{})(*m_ast);
}
else if (m_optimizerStep == "expressionInliner")
{
disambiguate();
ExpressionInliner(*m_ast).run();
}
else if (m_optimizerStep == "fullInliner")
{
disambiguate();
(FunctionHoister{})(*m_ast);
(FunctionGrouper{})(*m_ast);
FullInliner(*m_ast).run();
}
else if (m_optimizerStep == "mainFunction")
{
disambiguate();
(FunctionGrouper{})(*m_ast);
(MainFunction{})(*m_ast);
}
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
(Rematerialiser{})(*m_ast);
}
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
(ExpressionSimplifier{})(*m_ast);
}
else if (m_optimizerStep == "unusedPruner")
{
disambiguate();
UnusedPruner::runUntilStabilised(*m_ast);
}
else
{
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;

View File

@ -0,0 +1,24 @@
{
let a := 1 let b := codesize()
for { } lt(1, codesize()) { mstore(1, codesize()) a := add(a, codesize()) } {
mstore(1, codesize())
}
mstore(1, codesize())
}
// ----
// commonSubexpressionEliminator
// {
// let a := 1
// let b := codesize()
// for {
// }
// lt(1, b)
// {
// mstore(1, b)
// a := add(a, b)
// }
// {
// mstore(1, b)
// }
// mstore(1, b)
// }

View File

@ -0,0 +1,15 @@
{
let b := 1
if b { b := 1 }
let c := 1
}
// ----
// commonSubexpressionEliminator
// {
// let b := 1
// if b
// {
// b := b
// }
// let c := 1
// }

View File

@ -0,0 +1,10 @@
{
let a := mload(1)
let b := mload(1)
}
// ----
// commonSubexpressionEliminator
// {
// let a := mload(1)
// let b := mload(1)
// }

View File

@ -0,0 +1,10 @@
{
let a := gas()
let b := gas()
}
// ----
// commonSubexpressionEliminator
// {
// let a := gas()
// let b := gas()
// }

View File

@ -0,0 +1,5 @@
{ }
// ----
// commonSubexpressionEliminator
// {
// }

View File

@ -0,0 +1,10 @@
{
let a := mul(1, codesize())
let b := mul(1, codesize())
}
// ----
// commonSubexpressionEliminator
// {
// let a := mul(1, codesize())
// let b := a
// }

View File

@ -0,0 +1,13 @@
{
function f(a) -> x { x := add(a, a) }
let y := f(calldatasize())
}
// ----
// expressionInliner
// {
// function f(a) -> x
// {
// x := add(a, a)
// }
// let y := add(calldatasize(), calldatasize())
// }

View File

@ -0,0 +1,18 @@
{
function f(a) -> x { x := add(a, a) }
function g(b, c) -> y { y := mul(mload(c), f(b)) }
let y := g(calldatasize(), 7)
}
// ----
// expressionInliner
// {
// function f(a) -> x
// {
// x := add(a, a)
// }
// function g(b, c) -> y
// {
// y := mul(mload(c), add(b, b))
// }
// let y_1 := mul(mload(7), add(calldatasize(), calldatasize()))
// }

View File

@ -0,0 +1,18 @@
{
function f(a, r) -> x { x := g(a, g(r, r)) }
function g(b, s) -> y { y := f(b, f(s, s)) }
let y := g(calldatasize(), 7)
}
// ----
// expressionInliner
// {
// function f(a, r) -> x
// {
// x := g(a, f(r, f(r, r)))
// }
// function g(b, s) -> y
// {
// y := f(b, g(s, f(s, f(s, s))))
// }
// let y_1 := f(calldatasize(), g(7, f(7, f(7, 7))))
// }

View File

@ -0,0 +1,14 @@
// Does not inline because mload could be moved out of sequence
{
function f(a) -> x { x := a }
let y := f(mload(2))
}
// ----
// expressionInliner
// {
// function f(a) -> x
// {
// x := a
// }
// let y := f(mload(2))
// }

View File

@ -0,0 +1,27 @@
// The calls to g and h cannot be moved because g and h are not movable. Therefore, the call
// to f is not inlined.
{
function f(a, b) -> x { x := add(b, a) }
function g() -> y { y := mload(0) mstore(0, 4) }
function h() -> z { mstore(0, 4) z := mload(0) }
let r := f(g(), h())
}
// ----
// expressionInliner
// {
// function f(a, b) -> x
// {
// x := add(b, a)
// }
// function g() -> y
// {
// y := mload(0)
// mstore(0, 4)
// }
// function h() -> z
// {
// mstore(0, 4)
// z := mload(0)
// }
// let r := f(g(), h())
// }

View File

@ -0,0 +1,14 @@
// yul
{
function f() -> x:u256 { x := 2:u256 }
let y:u256 := f()
}
// ----
// expressionInliner
// {
// function f() -> x:u256
// {
// x := 2:u256
// }
// let y:u256 := 2:u256
// }

View File

@ -0,0 +1,14 @@
// yul
{
function f(a:u256) -> x:u256 { x := a }
let y:u256 := f(7:u256)
}
// ----
// expressionInliner
// {
// function f(a:u256) -> x:u256
// {
// x := a
// }
// let y:u256 := 7:u256
// }

View File

@ -0,0 +1,6 @@
{ let a := add(7, sub(mload(0), 7)) }
// ----
// expressionSimplifier
// {
// let a := mload(0)
// }

View File

@ -0,0 +1,6 @@
{ let a := add(1, mul(3, 4)) }
// ----
// expressionSimplifier
// {
// let a := 13
// }

View File

@ -0,0 +1,6 @@
{ let a := sub(calldataload(0), calldataload(0)) }
// ----
// expressionSimplifier
// {
// let a := 0
// }

View File

@ -0,0 +1,6 @@
{ let a := sub(calldataload(1), calldataload(0)) }
// ----
// expressionSimplifier
// {
// let a := sub(calldataload(1), calldataload(0))
// }

View File

@ -0,0 +1,10 @@
{
let a := mload(0)
let b := sub(a, a)
}
// ----
// expressionSimplifier
// {
// let a := mload(0)
// let b := 0
// }

View File

@ -0,0 +1,12 @@
{
function f() -> a {}
let b := add(7, sub(f(), 7))
}
// ----
// expressionSimplifier
// {
// function f() -> a
// {
// }
// let b := f()
// }

View File

@ -0,0 +1,16 @@
{
for { let a := 10 } iszero(eq(a, 0)) { a := add(a, 1) } {}
}
// ----
// expressionSimplifier
// {
// for {
// let a := 10
// }
// iszero(iszero(a))
// {
// a := add(a, 1)
// }
// {
// }
// }

View File

@ -0,0 +1,10 @@
{
let a := mload(sub(7, 7))
let b := sub(a, 0)
}
// ----
// expressionSimplifier
// {
// let a := mload(0)
// let b := a
// }

View File

@ -0,0 +1,8 @@
{
mstore(0, mod(calldataload(0), exp(2, 8)))
}
// ----
// expressionSimplifier
// {
// mstore(0, and(calldataload(0), 255))
// }

View File

@ -0,0 +1,8 @@
{
mstore(0, mod(calldataload(0), exp(2, 255)))
}
// ----
// expressionSimplifier
// {
// mstore(0, and(calldataload(0), 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff))
// }

View File

@ -0,0 +1,12 @@
{
function f(a) -> b { }
let c := sub(f(0), f(1))
}
// ----
// expressionSimplifier
// {
// function f(a) -> b
// {
// }
// let c := sub(f(0), f(1))
// }

View File

@ -0,0 +1,16 @@
{
function f1() -> a { }
function f2() -> b { }
let c := sub(f1(), f2())
}
// ----
// expressionSimplifier
// {
// function f1() -> a
// {
// }
// function f2() -> b
// {
// }
// let c := sub(f1(), f2())
// }

View File

@ -0,0 +1,13 @@
// Even if the functions pass the equality check, they are not movable.
{
function f() -> a { }
let b := sub(f(), f())
}
// ----
// expressionSimplifier
// {
// function f() -> a
// {
// }
// let b := sub(f(), f())
// }

View File

@ -0,0 +1,10 @@
// The first argument of div is not constant.
// keccak256 is not movable.
{
let a := div(keccak256(0, 0), 0)
}
// ----
// expressionSimplifier
// {
// let a := div(keccak256(0, 0), 0)
// }

View File

@ -0,0 +1,8 @@
{
let a := add(0, mload(0))
}
// ----
// expressionSimplifier
// {
// let a := mload(0)
// }

View File

@ -0,0 +1,5 @@
{ }
// ----
// expressionSimplifier
// {
// }

View File

@ -0,0 +1,33 @@
{
let x := calldataload(0)
if mul(add(x, 2), 3) {
for { let a := 2 } lt(a, mload(a)) { a := add(a, mul(a, 2)) } {
let b := mul(add(a, 2), 4)
sstore(b, mul(b, 2))
}
}
}
// ----
// expressionSplitter
// {
// let x := calldataload(0)
// let _1 := add(x, 2)
// let _2 := mul(_1, 3)
// if _2
// {
// for {
// let a := 2
// }
// lt(a, mload(a))
// {
// let _3 := mul(a, 2)
// a := add(a, _3)
// }
// {
// let _4 := add(a, 2)
// let b := mul(_4, 4)
// let _5 := mul(b, 2)
// sstore(b, _5)
// }
// }
// }

View File

@ -0,0 +1,24 @@
{
let x := mul(f(0, mload(7)), 3)
function f(a, b) -> c {
c := mul(a, mload(add(b, c)))
}
sstore(x, f(mload(2), mload(2)))
}
// ----
// expressionSplitter
// {
// let _1 := mload(7)
// let _2 := f(0, _1)
// let x := mul(_2, 3)
// function f(a, b) -> c
// {
// let _3 := add(b, c)
// let _4 := mload(_3)
// c := mul(a, _4)
// }
// let _5 := mload(2)
// let _6 := mload(2)
// let _7 := f(_6, _5)
// sstore(x, _7)
// }

View File

@ -0,0 +1,5 @@
{ }
// ----
// expressionSplitter
// {
// }

View File

@ -0,0 +1,25 @@
{
let x := 8
switch add(2, calldataload(0))
case 0 { sstore(0, mload(2)) }
default { mstore(0, mload(3)) }
x := add(mload(3), 4)
}
// ----
// expressionSplitter
// {
// let x := 8
// let _1 := calldataload(0)
// let _2 := add(2, _1)
// switch _2
// case 0 {
// let _3 := mload(2)
// sstore(0, _3)
// }
// default {
// let _4 := mload(3)
// mstore(0, _4)
// }
// let _5 := mload(3)
// x := add(_5, 4)
// }

View File

@ -0,0 +1,11 @@
{
mstore(add(calldataload(2), mload(3)), 8)
}
// ----
// expressionSplitter
// {
// let _1 := mload(3)
// let _2 := calldataload(2)
// let _3 := add(_2, _1)
// mstore(_3, 8)
// }

View File

@ -0,0 +1,32 @@
// This tests that breaking the expression inside the condition works properly.
{
if gt(f(mload(1)), mload(0)) {
sstore(0, 2)
}
function f(a) -> r {
a := mload(a)
r := add(a, calldatasize())
}
}
// ----
// fullInliner
// {
// {
// let _1 := mload(0)
// let f_a := mload(1)
// let f_r
// {
// f_a := mload(f_a)
// f_r := add(f_a, calldatasize())
// }
// if gt(f_r, _1)
// {
// sstore(0, 2)
// }
// }
// function f(a) -> r
// {
// a := mload(a)
// r := add(a, calldatasize())
// }
// }

View File

@ -0,0 +1,28 @@
{
function f(a, b, c) -> x {
x := add(a, b)
x := mul(x, c)
}
let y := add(mload(1), add(f(mload(2), mload(3), mload(4)), mload(5)))
}
// ----
// fullInliner
// {
// {
// let _1 := mload(5)
// let f_c := mload(4)
// let f_b := mload(3)
// let f_a := mload(2)
// let f_x
// {
// f_x := add(f_a, f_b)
// f_x := mul(f_x, f_c)
// }
// let y := add(mload(1), add(f_x, _1))
// }
// function f(a, b, c) -> x
// {
// x := add(a, b)
// x := mul(x, c)
// }
// }

View File

@ -0,0 +1,40 @@
{
function f(a) -> x { x := add(a, a) }
function g(b, c) -> y { y := mul(mload(c), f(b)) }
let y := g(f(3), 7)
}
// ----
// fullInliner
// {
// {
// let g_c := 7
// let f_a_1 := 3
// let f_x_1
// {
// f_x_1 := add(f_a_1, f_a_1)
// }
// let g_y
// {
// let g_f_a := f_x_1
// let g_f_x
// {
// g_f_x := add(g_f_a, g_f_a)
// }
// g_y := mul(mload(g_c), g_f_x)
// }
// let y_1 := g_y
// }
// function f(a) -> x
// {
// x := add(a, a)
// }
// function g(b, c) -> y
// {
// let f_a := b
// let f_x
// {
// f_x := add(f_a, f_a)
// }
// y := mul(mload(c), f_x)
// }
// }

View File

@ -0,0 +1,21 @@
// The full inliner currently does not work with
// functions returning multiple values.
{
function f(a) -> x, y {
x := mul(a, a)
y := add(a, x)
}
let a, b := f(mload(0))
}
// ----
// fullInliner
// {
// {
// let a_1, b := f(mload(0))
// }
// function f(a) -> x, y
// {
// x := mul(a, a)
// y := add(a, x)
// }
// }

View File

@ -0,0 +1,22 @@
{
function f(a) {
sstore(a, a)
}
f(mload(0))
}
// ----
// fullInliner
// {
// {
// let f_a := mload(0)
// {
// sstore(f_a, f_a)
// }
// {
// }
// }
// function f(a)
// {
// sstore(a, a)
// }
// }

View File

@ -0,0 +1,28 @@
// This tests that `pop(r)` is removed.
{
function f(a) -> x {
let r := mul(a, a)
x := add(r, r)
}
pop(add(f(7), 2))
}
// ----
// fullInliner
// {
// {
// let _1 := 2
// let f_a := 7
// let f_x
// {
// let f_r := mul(f_a, f_a)
// f_x := add(f_r, f_r)
// }
// {
// }
// }
// function f(a) -> x
// {
// let r := mul(a, a)
// x := add(r, r)
// }
// }

View File

@ -0,0 +1,26 @@
{
function f(a) -> x {
let r := mul(a, a)
x := add(r, r)
}
let y := add(f(sload(mload(2))), mload(7))
}
// ----
// fullInliner
// {
// {
// let _1 := mload(7)
// let f_a := sload(mload(2))
// let f_x
// {
// let f_r := mul(f_a, f_a)
// f_x := add(f_r, f_r)
// }
// let y := add(f_x, _1)
// }
// function f(a) -> x
// {
// let r := mul(a, a)
// x := add(r, r)
// }
// }

View File

@ -0,0 +1,24 @@
// yul
{ let a:u256 { } function f() -> x:bool { let b:u256 := 4:u256 {} for {} f() {} {} } }
// ----
// functionGrouper
// {
// {
// let a:u256
// {
// }
// }
// function f() -> x:bool
// {
// let b:u256 := 4:u256
// {
// }
// for {
// }
// f()
// {
// }
// {
// }
// }
// }

View File

@ -0,0 +1,24 @@
// yul
{
let a:u256
function f() { let b:u256 }
let c:u256 function g() { let d:u256 }
let e:u256
}
// ----
// functionGrouper
// {
// {
// let a:u256
// let c:u256
// let e:u256
// }
// function f()
// {
// let b:u256
// }
// function g()
// {
// let d:u256
// }
// }

View File

@ -0,0 +1,27 @@
// yul
{
let a:u256
function f() {
let b:u256
function g() {
let c:u256
}
let d:u256
}
}
// ----
// functionGrouper
// {
// {
// let a:u256
// }
// function f()
// {
// let b:u256
// function g()
// {
// let c:u256
// }
// let d:u256
// }
// }

View File

@ -0,0 +1,14 @@
// yul
{
let a:u256 function f() {}
}
// ----
// functionGrouper
// {
// {
// let a:u256
// }
// function f()
// {
// }
// }

View File

@ -0,0 +1,7 @@
{ }
// ----
// functionGrouper
// {
// {
// }
// }

View File

@ -0,0 +1,26 @@
// yul
{
let a:u256
{ }
function f() -> x:bool {
let b:u256 := 4:u256
{ }
for {} f() {} {}
}
}
// ----
// functionHoister
// {
// let a:u256
// function f() -> x:bool
// {
// let b:u256 := 4:u256
// for {
// }
// f()
// {
// }
// {
// }
// }
// }

View File

@ -0,0 +1,23 @@
// yul
{
let a:u256
function f() { let b:u256 }
let c:u256
function g() { let d:u256 }
let e:u256
}
// ----
// functionHoister
// {
// let a:u256
// let c:u256
// let e:u256
// function f()
// {
// let b:u256
// }
// function g()
// {
// let d:u256
// }
// }

View File

@ -0,0 +1,23 @@
// yul
{
let a:u256
function f() {
let b:u256
function g() { let c:u256 }
let d:u256
}
}
// ----
// functionHoister
// {
// let a:u256
// function g()
// {
// let c:u256
// }
// function f()
// {
// let b:u256
// let d:u256
// }
// }

View File

@ -0,0 +1,13 @@
// yul
{
let a:u256
function f() {}
}
// ----
// functionHoister
// {
// let a:u256
// function f()
// {
// }
// }

View File

@ -0,0 +1,6 @@
{
}
// ----
// functionHoister
// {
// }

View File

@ -0,0 +1,33 @@
// yul
{
let a:u256
{ }
function f() -> x:bool {
let b:u256 := 4:u256
{}
for {} f() {} {}
}
}
// ----
// mainFunction
// {
// function main()
// {
// let a:u256
// {
// }
// }
// function f() -> x:bool
// {
// let b:u256 := 4:u256
// {
// }
// for {
// }
// f()
// {
// }
// {
// }
// }
// }

View File

@ -0,0 +1,26 @@
// yul
{
let a:u256
function f() { let b:u256 }
let c:u256
function g() { let d:u256 }
let e:u256
}
// ----
// mainFunction
// {
// function main()
// {
// let a:u256
// let c:u256
// let e:u256
// }
// function f()
// {
// let b:u256
// }
// function g()
// {
// let d:u256
// }
// }

View File

@ -0,0 +1,26 @@
// yul
{
let a:u256
function f() {
let b:u256
function g() { let c:u256}
let d:u256
}
}
// ----
// mainFunction
// {
// function main()
// {
// let a:u256
// }
// function f()
// {
// let b:u256
// function g()
// {
// let c:u256
// }
// let d:u256
// }
// }

View File

@ -0,0 +1,16 @@
// yul
{
let a:u256
function f() {}
}
// ----
// mainFunction
// {
// function main()
// {
// let a:u256
// }
// function f()
// {
// }
// }

View File

@ -0,0 +1,9 @@
// yul
{}
// ----
// mainFunction
// {
// function main()
// {
// }
// }

View File

@ -0,0 +1,21 @@
{
let a := 1
for { pop(a) } a { pop(a) } {
pop(a)
}
}
// ----
// rematerialiser
// {
// let a := 1
// for {
// pop(1)
// }
// 1
// {
// pop(1)
// }
// {
// pop(1)
// }
// }

View File

@ -0,0 +1,25 @@
{
let a := 1
for { pop(a) } a { pop(a) } {
a := 7
let c := a
}
let x := a
}
// ----
// rematerialiser
// {
// let a := 1
// for {
// pop(1)
// }
// a
// {
// pop(7)
// }
// {
// a := 7
// let c := 7
// }
// let x := a
// }

View File

@ -0,0 +1,23 @@
{
let b := 0
for { let a := 1 pop(a) } a { pop(a) } {
b := 1 pop(a)
}
}
// ----
// rematerialiser
// {
// let b := 0
// for {
// let a := 1
// pop(1)
// }
// 1
// {
// pop(1)
// }
// {
// b := 1
// pop(1)
// }
// }

View File

@ -0,0 +1,24 @@
{
let b := 0
for { let a := 1 pop(a) } lt(a, 0) { pop(a) a := add(a, 3) } {
b := 1 pop(a)
}
}
// ----
// rematerialiser
// {
// let b := 0
// for {
// let a := 1
// pop(1)
// }
// lt(a, 0)
// {
// pop(a)
// a := add(a, 3)
// }
// {
// b := 1
// pop(a)
// }
// }

View File

@ -0,0 +1,18 @@
{
let a := 1
let b := 2
if b { pop(b) b := a }
let c := b
}
// ----
// rematerialiser
// {
// let a := 1
// let b := 2
// if 2
// {
// pop(2)
// b := 1
// }
// let c := b
// }

View File

@ -0,0 +1,24 @@
{
let a := 1
let b := 2
switch number()
case 1 { b := a }
default { let x := a let y := b b := a }
pop(add(a, b))
}
// ----
// rematerialiser
// {
// let a := 1
// let b := 2
// switch number()
// case 1 {
// b := 1
// }
// default {
// let x := 1
// let y := b
// b := 1
// }
// pop(add(1, b))
// }

View File

@ -0,0 +1,19 @@
// Cannot replace `let b := x` by `let b := a` since a is out of scope.
{
let x
{
let a := sload(0)
x := a
}
let b := x
}
// ----
// rematerialiser
// {
// let x
// {
// let a := sload(0)
// x := a
// }
// let b := x
// }

View File

@ -0,0 +1,10 @@
{
let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize()))
let b := x
}
// ----
// rematerialiser
// {
// let x := add(mul(calldataload(2), calldataload(4)), mul(2, calldatasize()))
// let b := x
// }

View File

@ -0,0 +1,10 @@
{
let x := add(mul(calldataload(2), calldataload(4)), calldatasize())
let b := x
}
// ----
// rematerialiser
// {
// let x := add(mul(calldataload(2), calldataload(4)), calldatasize())
// let b := add(mul(calldataload(2), calldataload(4)), calldatasize())
// }

View File

@ -0,0 +1,10 @@
{
let a := add(mul(calldatasize(), 2), number())
let b := add(a, a)
}
// ----
// rematerialiser
// {
// let a := add(mul(calldatasize(), 2), number())
// let b := add(add(mul(calldatasize(), 2), number()), add(mul(calldatasize(), 2), number()))
// }

View File

@ -0,0 +1,18 @@
{
function f(x) -> y {}
let a := 1
let b := f(a)
let c := a
mstore(add(a, b), c)
}
// ----
// rematerialiser
// {
// function f(x) -> y
// {
// }
// let a := 1
// let b := f(1)
// let c := 1
// mstore(add(1, b), 1)
// }

View File

@ -0,0 +1,14 @@
{
let a := 1
let b := mload(a)
let c := a
mstore(add(a, b), c)
}
// ----
// rematerialiser
// {
// let a := 1
// let b := mload(1)
// let c := 1
// mstore(add(1, b), 1)
// }

View File

@ -0,0 +1,21 @@
{
let a := extcodesize(0)
let b := a
let c := b
a := 2
let d := add(b, c)
pop(a) pop(b) pop(c) pop(d)
}
// ----
// rematerialiser
// {
// let a := extcodesize(0)
// let b := a
// let c := a
// a := 2
// let d := add(b, c)
// pop(2)
// pop(b)
// pop(c)
// pop(add(b, c))
// }

View File

@ -0,0 +1,19 @@
{
let a := 1
pop(a)
if a { a := 2 }
let b := mload(a)
pop(b)
}
// ----
// rematerialiser
// {
// let a := 1
// pop(1)
// if 1
// {
// a := 2
// }
// let b := mload(a)
// pop(b)
// }

View File

@ -0,0 +1,5 @@
{}
// ----
// rematerialiser
// {
// }

View File

@ -0,0 +1,12 @@
{
let a := 1
let b := a
mstore(0, b)
}
// ----
// rematerialiser
// {
// let a := 1
// let b := 1
// mstore(0, 1)
// }

View File

@ -0,0 +1,13 @@
// We cannot substitute `a` in `let b := a`
{
let a := extcodesize(0)
a := mul(a, 2)
let b := a
}
// ----
// rematerialiser
// {
// let a := extcodesize(0)
// a := mul(a, 2)
// let b := a
// }

View File

@ -0,0 +1,8 @@
{
function f() { let a := 1 }
function g() { f() }
}
// ----
// unusedPruner
// {
// }

View File

@ -0,0 +1,11 @@
{
let a := 1
a := 4
let b := 1
}
// ----
// unusedPruner
// {
// let a := 1
// a := 4
// }

View File

@ -0,0 +1,16 @@
{
let a, b
function f() -> x { }
a := f()
b := 1
}
// ----
// unusedPruner
// {
// let a, b
// function f() -> x
// {
// }
// a := f()
// b := 1
// }

View File

@ -0,0 +1,16 @@
{
let a
let b
function f() -> x, y { }
a, b := f()
}
// ----
// unusedPruner
// {
// let a
// let b
// function f() -> x, y
// {
// }
// a, b := f()
// }

View File

@ -0,0 +1,12 @@
{
let x, y
x := 1
y := 2
}
// ----
// unusedPruner
// {
// let x, y
// x := 1
// y := 2
// }

View File

@ -0,0 +1,7 @@
{
let x, y
}
// ----
// unusedPruner
// {
// }

View File

@ -0,0 +1,12 @@
{
function f() -> x, y { }
let a, b := f()
}
// ----
// unusedPruner
// {
// function f() -> x, y
// {
// }
// let a, b := f()
// }

View File

@ -0,0 +1,10 @@
{
let x, y
x := 1
}
// ----
// unusedPruner
// {
// let x, y
// x := 1
// }

View File

@ -0,0 +1,5 @@
{ }
// ----
// unusedPruner
// {
// }

View File

@ -0,0 +1,10 @@
{
let a := 1
let b := 1
mstore(0, 1)
}
// ----
// unusedPruner
// {
// mstore(0, 1)
// }