mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #9397 from ethereum/function-unused-parameter
[YUL] Remove unused function parameters
This commit is contained in:
commit
ff6415aa9e
@ -1,5 +1,12 @@
|
||||
### 0.7.2 (unreleased)
|
||||
|
||||
Language Features:
|
||||
|
||||
|
||||
Compiler Features:
|
||||
* Yul Optimizer: Prune unused parameters in functions.
|
||||
|
||||
Bugfixes:
|
||||
|
||||
|
||||
### 0.7.1 (2020-09-02)
|
||||
|
@ -41,7 +41,7 @@ struct OptimiserSettings
|
||||
|
||||
// should have good "compilability" property here.
|
||||
|
||||
"eul" // Run functional expression inliner
|
||||
"Tpeul" // Run functional expression inliner
|
||||
"xarulrul" // Prune a bit more in SSA
|
||||
"xarrcL" // Turn into SSA again and simplify
|
||||
"gvif" // Run full inliner
|
||||
|
@ -209,6 +209,13 @@ std::map<V, K> invertMap(std::map<K, V> const& originalMap)
|
||||
return inverseMap;
|
||||
}
|
||||
|
||||
/// Returns a set of keys of a map.
|
||||
template <typename K, typename V>
|
||||
std::set<K> keys(std::map<K, V> const& _map)
|
||||
{
|
||||
return applyMap(_map, [](auto const& _elem) { return _elem.first; }, std::set<K>{});
|
||||
}
|
||||
|
||||
// String conversion functions, mainly to/from hex/nibble/byte representations.
|
||||
|
||||
enum class WhenError
|
||||
|
@ -156,6 +156,9 @@ add_library(yul
|
||||
optimiser/SyntacticalEquality.h
|
||||
optimiser/TypeInfo.cpp
|
||||
optimiser/TypeInfo.h
|
||||
optimiser/UnusedFunctionParameterPruner.cpp
|
||||
optimiser/UnusedFunctionParameterPruner.h
|
||||
optimiser/UnusedFunctionsCommon.h
|
||||
optimiser/UnusedPruner.cpp
|
||||
optimiser/UnusedPruner.h
|
||||
optimiser/VarDeclInitializer.cpp
|
||||
|
@ -60,6 +60,8 @@ public:
|
||||
void operator()(FunctionCall& _funCall) override;
|
||||
void operator()(Block& _block) override;
|
||||
|
||||
std::map<YulString, YulString> const& translations() const { return m_translations; }
|
||||
|
||||
protected:
|
||||
/// Check if the newly introduced identifier @a _name has to be replaced.
|
||||
void checkAndReplaceNew(YulString& _name);
|
||||
|
@ -42,6 +42,7 @@
|
||||
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
||||
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
|
||||
#include <libyul/optimiser/UnusedPruner.h>
|
||||
#include <libyul/optimiser/ExpressionSimplifier.h>
|
||||
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
||||
@ -185,6 +186,7 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
||||
SSAReverser,
|
||||
SSATransform,
|
||||
StructuralSimplifier,
|
||||
UnusedFunctionParameterPruner,
|
||||
UnusedPruner,
|
||||
VarDeclInitializer
|
||||
>();
|
||||
@ -221,6 +223,7 @@ map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
|
||||
{SSAReverser::name, 'V'},
|
||||
{SSATransform::name, 'a'},
|
||||
{StructuralSimplifier::name, 't'},
|
||||
{UnusedFunctionParameterPruner::name, 'p'},
|
||||
{UnusedPruner::name, 'u'},
|
||||
{VarDeclInitializer::name, 'd'},
|
||||
};
|
||||
|
127
libyul/optimiser/UnusedFunctionParameterPruner.cpp
Normal file
127
libyul/optimiser/UnusedFunctionParameterPruner.cpp
Normal file
@ -0,0 +1,127 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
/**
|
||||
* UnusedFunctionParameterPruner: Optimiser step that removes unused parameters from function
|
||||
* definition.
|
||||
*/
|
||||
|
||||
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
|
||||
#include <libyul/optimiser/UnusedFunctionsCommon.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libyul/optimiser/NameDisplacer.h>
|
||||
#include <libyul/optimiser/NameDispenser.h>
|
||||
#include <libyul/YulString.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <boost/algorithm/cxx11/all_of.hpp>
|
||||
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::yul;
|
||||
using namespace solidity::yul::unusedFunctionsCommon;
|
||||
|
||||
void UnusedFunctionParameterPruner::run(OptimiserStepContext& _context, Block& _ast)
|
||||
{
|
||||
map<YulString, size_t> references = ReferencesCounter::countReferences(_ast);
|
||||
auto used = [&](auto v) -> bool { return references.count(v.name); };
|
||||
|
||||
// Function name and a pair of boolean masks, the first corresponds to parameters and the second
|
||||
// corresponds to returnVariables.
|
||||
//
|
||||
// For the first vector in the pair, a value `false` at index `i` indicates that the function
|
||||
// argument at index `i` in `FunctionDefinition::parameters` is unused inside the function body.
|
||||
//
|
||||
// Similarly for the second vector in the pair, a value `false` at index `i` indicates that the
|
||||
// return parameter at index `i` in `FunctionDefinition::returnVariables` is unused inside
|
||||
// function body.
|
||||
map<YulString, pair<vector<bool>, vector<bool>>> usedParametersAndReturnVariables;
|
||||
|
||||
// Step 1 of UnusedFunctionParameterPruner: Find functions whose parameters (both arguments and
|
||||
// return-parameters) are not used in its body.
|
||||
for (auto const& statement: _ast.statements)
|
||||
if (holds_alternative<FunctionDefinition>(statement))
|
||||
{
|
||||
FunctionDefinition const& f = std::get<FunctionDefinition>(statement);
|
||||
|
||||
if (tooSimpleToBePruned(f) || boost::algorithm::all_of(f.parameters + f.returnVariables, used))
|
||||
continue;
|
||||
|
||||
usedParametersAndReturnVariables[f.name] = {
|
||||
applyMap(f.parameters, used),
|
||||
applyMap(f.returnVariables, used)
|
||||
};
|
||||
}
|
||||
|
||||
set<YulString> functionNamesToFree = util::keys(usedParametersAndReturnVariables);
|
||||
|
||||
// Step 2 of UnusedFunctionParameterPruner: Renames the function and replaces all references to
|
||||
// the function, say `f`, by its new name, say `f_1`.
|
||||
NameDisplacer replace{_context.dispenser, functionNamesToFree};
|
||||
replace(_ast);
|
||||
|
||||
// Inverse-Map of the above translations. In the above example, this will store an element with
|
||||
// key `f_1` and value `f`.
|
||||
std::map<YulString, YulString> newToOriginalNames = invertMap(replace.translations());
|
||||
|
||||
// Step 3 of UnusedFunctionParameterPruner: introduce a new function in the block with body of
|
||||
// the old one. Replace the body of the old one with a function call to the new one with reduced
|
||||
// parameters.
|
||||
//
|
||||
// For example: introduce a new 'linking' function `f` with the same the body as `f_1`, but with
|
||||
// reduced parameters, i.e., `function f() -> y { y := 1 }`. Now replace the body of `f_1` with
|
||||
// a call to `f`, i.e., `f_1(x) -> y { y := f() }`.
|
||||
iterateReplacing(_ast.statements, [&](Statement& _s) -> optional<vector<Statement>> {
|
||||
if (holds_alternative<FunctionDefinition>(_s))
|
||||
{
|
||||
// The original function except that it has a new name (e.g., `f_1`)
|
||||
FunctionDefinition& originalFunction = get<FunctionDefinition>(_s);
|
||||
if (newToOriginalNames.count(originalFunction.name))
|
||||
{
|
||||
|
||||
YulString linkingFunctionName = originalFunction.name;
|
||||
YulString originalFunctionName = newToOriginalNames.at(linkingFunctionName);
|
||||
pair<vector<bool>, vector<bool>> used =
|
||||
usedParametersAndReturnVariables.at(originalFunctionName);
|
||||
|
||||
FunctionDefinition linkingFunction = createLinkingFunction(
|
||||
originalFunction,
|
||||
used,
|
||||
originalFunctionName,
|
||||
linkingFunctionName,
|
||||
_context.dispenser
|
||||
);
|
||||
|
||||
originalFunction.name = originalFunctionName;
|
||||
originalFunction.parameters =
|
||||
filter(originalFunction.parameters, used.first);
|
||||
originalFunction.returnVariables =
|
||||
filter(originalFunction.returnVariables, used.second);
|
||||
|
||||
return make_vector<Statement>(move(originalFunction), move(linkingFunction));
|
||||
}
|
||||
}
|
||||
|
||||
return nullopt;
|
||||
});
|
||||
}
|
52
libyul/optimiser/UnusedFunctionParameterPruner.h
Normal file
52
libyul/optimiser/UnusedFunctionParameterPruner.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
|
||||
namespace solidity::yul
|
||||
{
|
||||
|
||||
/**
|
||||
* UnusedFunctionParameterPruner: Optimiser step that removes unused parameters in a function.
|
||||
*
|
||||
* If a parameter is unused, like `c` and `y` in, `function f(a,b,c) -> x, y { x := div(a,b) }`
|
||||
*
|
||||
* We remove the parameter and create a new "linking" function as follows:
|
||||
*
|
||||
* `function f(a,b) -> x { x := div(a,b) }`
|
||||
* `function f2(a,b,c) -> x, y { x := f(a,b) }`
|
||||
*
|
||||
* and replace all references to `f` by `f2`.
|
||||
* The inliner should be run afterwards to make sure that all references to `f2` are replaced by
|
||||
* `f`.
|
||||
*
|
||||
* Prerequisites: Disambiguator, FunctionHoister, LiteralRematerialiser
|
||||
*
|
||||
* The step LiteralRematerialiser is not required for correctness. It helps deal with cases such as:
|
||||
* `function f(x) -> y { revert(y, y} }` where the literal `y` will be replaced by its value `0`,
|
||||
* allowing us to rewrite the function.
|
||||
*/
|
||||
struct UnusedFunctionParameterPruner
|
||||
{
|
||||
static constexpr char const* name{"UnusedFunctionParameterPruner"};
|
||||
static void run(OptimiserStepContext& _context, Block& _ast);
|
||||
};
|
||||
|
||||
}
|
103
libyul/optimiser/UnusedFunctionsCommon.h
Normal file
103
libyul/optimiser/UnusedFunctionsCommon.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
#include <libyul/optimiser/NameDispenser.h>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/Dialect.h>
|
||||
#include <libyul/Exceptions.h>
|
||||
|
||||
#include <liblangutil/SourceLocation.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <variant>
|
||||
|
||||
namespace solidity::yul::unusedFunctionsCommon
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> filter(std::vector<T> const& _vec, std::vector<bool> const& _mask)
|
||||
{
|
||||
yulAssert(_vec.size() == _mask.size(), "");
|
||||
|
||||
std::vector<T> ret;
|
||||
|
||||
for (size_t i = 0; i < _mask.size(); ++i)
|
||||
if (_mask[i])
|
||||
ret.push_back(_vec[i]);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Returns true if applying UnusedFunctionParameterPruner is not helpful or redundant because the
|
||||
/// inliner will be able to handle it anyway.
|
||||
bool tooSimpleToBePruned(FunctionDefinition const& _f)
|
||||
{
|
||||
return _f.body.statements.size() <= 1 && CodeSize::codeSize(_f.body) <= 1;
|
||||
}
|
||||
|
||||
FunctionDefinition createLinkingFunction(
|
||||
FunctionDefinition const& _original,
|
||||
std::pair<std::vector<bool>, std::vector<bool>> const& _usedParametersAndReturns,
|
||||
YulString const& _originalFunctionName,
|
||||
YulString const& _linkingFunctionName,
|
||||
NameDispenser& _nameDispenser
|
||||
)
|
||||
{
|
||||
auto generateTypedName = [&](TypedName t)
|
||||
{
|
||||
return TypedName{
|
||||
t.location,
|
||||
_nameDispenser.newName(t.name),
|
||||
t.type
|
||||
};
|
||||
};
|
||||
|
||||
langutil::SourceLocation loc = _original.location;
|
||||
|
||||
FunctionDefinition linkingFunction{
|
||||
loc,
|
||||
_linkingFunctionName,
|
||||
util::applyMap(_original.parameters, generateTypedName),
|
||||
util::applyMap(_original.returnVariables, generateTypedName),
|
||||
{loc, {}} // body
|
||||
};
|
||||
|
||||
FunctionCall call{loc, Identifier{loc, _originalFunctionName}, {}};
|
||||
for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first))
|
||||
call.arguments.emplace_back(Identifier{loc, p.name});
|
||||
|
||||
Assignment assignment{loc, {}, nullptr};
|
||||
|
||||
for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second))
|
||||
assignment.variableNames.emplace_back(Identifier{loc, r.name});
|
||||
|
||||
if (assignment.variableNames.empty())
|
||||
linkingFunction.body.statements.emplace_back(ExpressionStatement{loc, std::move(call)});
|
||||
else
|
||||
{
|
||||
assignment.value = std::make_unique<Expression>(std::move(call));
|
||||
linkingFunction.body.statements.emplace_back(std::move(assignment));
|
||||
}
|
||||
|
||||
return linkingFunction;
|
||||
}
|
||||
|
||||
}
|
@ -31,9 +31,10 @@ object "object" {
|
||||
let x_6 := x_2
|
||||
let x_7 := x_3
|
||||
let _2 := 1
|
||||
let _3:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(i64.or(_1, _1), i64.or(_1, _2)))))
|
||||
let _3 := i64.or(_1, _1)
|
||||
let _4:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, _2)))))
|
||||
for { }
|
||||
i32.eqz(_3)
|
||||
i32.eqz(_4)
|
||||
{
|
||||
let x_8, x_9, x_10, x_11 := add(x_4, x_5, x_6, x_7, _1, _1, _1, _2)
|
||||
x_4 := x_8
|
||||
@ -42,13 +43,10 @@ object "object" {
|
||||
x_7 := x_11
|
||||
}
|
||||
{
|
||||
let _4, _5, _6, _7 := lt(x_4, x_5, x_6, x_7, _1, _1, _1, 10)
|
||||
let _8, _9, _10, _11 := iszero(_4, _5, _6, _7)
|
||||
if i32.eqz(i64.eqz(i64.or(i64.or(_8, _9), i64.or(_10, _11)))) { break }
|
||||
let _12, _13, _14, _15 := eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2)
|
||||
if i32.eqz(i64.eqz(i64.or(i64.or(_12, _13), i64.or(_14, _15)))) { break }
|
||||
let _16, _17, _18, _19 := eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4)
|
||||
if i32.eqz(i64.eqz(i64.or(i64.or(_16, _17), i64.or(_18, _19)))) { continue }
|
||||
let _5, _6, _7, _8 := iszero_170_789(_1, _1, _1, lt_172(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
|
||||
if i32.eqz(i64.eqz(i64.or(i64.or(_5, _6), i64.or(_7, _8)))) { break }
|
||||
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2))))) { break }
|
||||
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4))))) { continue }
|
||||
}
|
||||
sstore(_1, _1, _1, _1, x_4, x_5, x_6, x_7)
|
||||
}
|
||||
@ -69,11 +67,11 @@ object "object" {
|
||||
let r1_1, carry_2 := add_carry(x1, y1, carry_1)
|
||||
r1 := r1_1
|
||||
}
|
||||
function iszero(x1, x2, x3, x4) -> r1, r2, r3, r4
|
||||
function iszero_170_789(x1, x2, x3, x4) -> r1, r2, r3, r4
|
||||
{
|
||||
r4 := i64.extend_i32_u(i64.eqz(i64.or(i64.or(x1, x2), i64.or(x3, x4))))
|
||||
}
|
||||
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4
|
||||
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r4
|
||||
{
|
||||
if i64.eq(x1, y1)
|
||||
{
|
||||
@ -89,7 +87,7 @@ object "object" {
|
||||
case 1:i32 { r := 0xffffffff:i32 }
|
||||
default { r := i64.ne(a, b) }
|
||||
}
|
||||
function lt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4
|
||||
function lt_172(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
|
||||
{
|
||||
let z:i32 := false
|
||||
switch cmp(x1, y1)
|
||||
@ -154,7 +152,7 @@ object "object" {
|
||||
|
||||
|
||||
Binary representation:
|
||||
0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020ac3080dde02030a7e017f147e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402005200620072008200020002000420a1008210b2300210c2301210d2302210e0b0240200b200c200d200e1005210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004202100621132300211423012115230221160b2013201484201520168484504504400c030b0240200520062007200820002000200042041006211723002118230121192302211a0b20172018842019201a8484504504400c010b0b0240200520062007200820002000200020091004211b2300211c2301211d2302211e0b201b2105201c2106201d2107201e21080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b3901047e0240200020045104402001200551044020022006510440200320075104404201210b0b0b0b0b0b20092400200a2401200b240220080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b960102047e047f02404100210c0240200020041007210d200d41004604400240200120051007210e200e41004604400240200220061007210f200f41004604402003200754210c05200f41014604404100210c054101210c0b0b0b05200e41014604404100210c054101210c0b0b0b05200d41014604404100210c054101210c0b0b0b200cad210b0b20092400200a2401200b240220080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b
|
||||
0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020af0070da302030b7e017f087e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a10081005210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210068484504504400c030b200a20002005200620072008200020002000420410068484504504400c010b0b024020052006200720082000200020002009100421102300211123012112230221130b201021052011210620122107201321080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041007210a200a41004604400240200120051007210b200b41004604400240200220061007210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b
|
||||
|
||||
Text representation:
|
||||
(module
|
||||
@ -177,23 +175,12 @@ Text representation:
|
||||
(local $x_6 i64)
|
||||
(local $x_7 i64)
|
||||
(local $_2 i64)
|
||||
(local $_3 i32)
|
||||
(local $_4 i64)
|
||||
(local $_3 i64)
|
||||
(local $_4 i32)
|
||||
(local $_5 i64)
|
||||
(local $_6 i64)
|
||||
(local $_7 i64)
|
||||
(local $_8 i64)
|
||||
(local $_9 i64)
|
||||
(local $_10 i64)
|
||||
(local $_11 i64)
|
||||
(local $_12 i64)
|
||||
(local $_13 i64)
|
||||
(local $_14 i64)
|
||||
(local $_15 i64)
|
||||
(local $_16 i64)
|
||||
(local $_17 i64)
|
||||
(local $_18 i64)
|
||||
(local $_19 i64)
|
||||
(local $x_8 i64)
|
||||
(local $x_9 i64)
|
||||
(local $x_10 i64)
|
||||
@ -212,46 +199,26 @@ Text representation:
|
||||
(local.set $x_6 (local.get $x_2))
|
||||
(local.set $x_7 (local.get $x_3))
|
||||
(local.set $_2 (i64.const 1))
|
||||
(local.set $_3 (i32.eqz (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_1) (local.get $_1)) (i64.or (local.get $_1) (local.get $_2)))))))
|
||||
(local.set $_3 (i64.or (local.get $_1) (local.get $_1)))
|
||||
(local.set $_4 (i32.eqz (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (local.get $_2)))))))
|
||||
(block $label__3
|
||||
(loop $label__5
|
||||
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_3))))
|
||||
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_4))))
|
||||
(block $label__4
|
||||
(block
|
||||
(local.set $_4 (call $lt (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10)))
|
||||
(local.set $_5 (global.get $global_))
|
||||
(local.set $_6 (global.get $global__1))
|
||||
(local.set $_7 (global.get $global__2))
|
||||
(local.set $_5 (call $iszero_170_789 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_172 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))))
|
||||
(local.set $_6 (global.get $global_))
|
||||
(local.set $_7 (global.get $global__1))
|
||||
(local.set $_8 (global.get $global__2))
|
||||
|
||||
)
|
||||
(block
|
||||
(local.set $_8 (call $iszero (local.get $_4) (local.get $_5) (local.get $_6) (local.get $_7)))
|
||||
(local.set $_9 (global.get $global_))
|
||||
(local.set $_10 (global.get $global__1))
|
||||
(local.set $_11 (global.get $global__2))
|
||||
|
||||
)
|
||||
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_8) (local.get $_9)) (i64.or (local.get $_10) (local.get $_11))))) (then
|
||||
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_5) (local.get $_6)) (i64.or (local.get $_7) (local.get $_8))))) (then
|
||||
(br $label__3)
|
||||
))
|
||||
(block
|
||||
(local.set $_12 (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2)))
|
||||
(local.set $_13 (global.get $global_))
|
||||
(local.set $_14 (global.get $global__1))
|
||||
(local.set $_15 (global.get $global__2))
|
||||
|
||||
)
|
||||
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_12) (local.get $_13)) (i64.or (local.get $_14) (local.get $_15))))) (then
|
||||
(if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2)))))) (then
|
||||
(br $label__3)
|
||||
))
|
||||
(block
|
||||
(local.set $_16 (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4)))
|
||||
(local.set $_17 (global.get $global_))
|
||||
(local.set $_18 (global.get $global__1))
|
||||
(local.set $_19 (global.get $global__2))
|
||||
|
||||
)
|
||||
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_16) (local.get $_17)) (i64.or (local.get $_18) (local.get $_19))))) (then
|
||||
(if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4)))))) (then
|
||||
(br $label__4)
|
||||
))
|
||||
|
||||
@ -343,7 +310,7 @@ Text representation:
|
||||
(local.get $r1)
|
||||
)
|
||||
|
||||
(func $iszero
|
||||
(func $iszero_170_789
|
||||
(param $x1 i64)
|
||||
(param $x2 i64)
|
||||
(param $x3 i64)
|
||||
@ -373,9 +340,6 @@ Text representation:
|
||||
(param $y3 i64)
|
||||
(param $y4 i64)
|
||||
(result i64)
|
||||
(local $r1 i64)
|
||||
(local $r2 i64)
|
||||
(local $r3 i64)
|
||||
(local $r4 i64)
|
||||
(block $label__9
|
||||
(if (i64.eq (local.get $x1) (local.get $y1)) (then
|
||||
@ -389,10 +353,7 @@ Text representation:
|
||||
))
|
||||
|
||||
)
|
||||
(global.set $global_ (local.get $r2))
|
||||
(global.set $global__1 (local.get $r3))
|
||||
(global.set $global__2 (local.get $r4))
|
||||
(local.get $r1)
|
||||
(local.get $r4)
|
||||
)
|
||||
|
||||
(func $cmp
|
||||
@ -416,7 +377,7 @@ Text representation:
|
||||
(local.get $r)
|
||||
)
|
||||
|
||||
(func $lt
|
||||
(func $lt_172
|
||||
(param $x1 i64)
|
||||
(param $x2 i64)
|
||||
(param $x3 i64)
|
||||
@ -426,9 +387,6 @@ Text representation:
|
||||
(param $y3 i64)
|
||||
(param $y4 i64)
|
||||
(result i64)
|
||||
(local $z1 i64)
|
||||
(local $z2 i64)
|
||||
(local $z3 i64)
|
||||
(local $z4 i64)
|
||||
(local $z i32)
|
||||
(local $condition_12 i32)
|
||||
@ -476,10 +434,7 @@ Text representation:
|
||||
(local.set $z4 (i64.extend_i32_u (local.get $z)))
|
||||
|
||||
)
|
||||
(global.set $global_ (local.get $z2))
|
||||
(global.set $global__1 (local.get $z3))
|
||||
(global.set $global__2 (local.get $z4))
|
||||
(local.get $z1)
|
||||
(local.get $z4)
|
||||
)
|
||||
|
||||
(func $calldataload
|
||||
|
@ -17,6 +17,21 @@
|
||||
sstore(add(a, 10), b)
|
||||
sstore(add(a, 11), b)
|
||||
sstore(add(a, 12), b)
|
||||
a3 := 1
|
||||
b3 := 1
|
||||
c3 := 1
|
||||
d3 := 1
|
||||
e3 := 1
|
||||
f3 := 1
|
||||
g3 := 1
|
||||
h3 := 1
|
||||
i3 := 1
|
||||
j3 := 1
|
||||
k3 := 1
|
||||
l3 := 1
|
||||
m3 := 1
|
||||
o3 := 1
|
||||
p3 := 1
|
||||
}
|
||||
let a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1 := fun()
|
||||
let a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2 := fun()
|
||||
|
@ -5,11 +5,11 @@ Pretty printed source:
|
||||
object "object" {
|
||||
code {
|
||||
{
|
||||
let a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1 := fun()
|
||||
let a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2 := fun()
|
||||
sstore(a1, a2)
|
||||
let a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, o3, p3 := fun()
|
||||
let a3_1, b3_1, c3_1, d3_1, e3_1, f3_1, g3_1, h3_1, i3_1, j3_1, k3_1, l3_1, m3_1, o3_1, p3_1 := fun()
|
||||
sstore(a3, a3_1)
|
||||
}
|
||||
function fun() -> a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, n3, o3, p3
|
||||
function fun() -> a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, o3, p3
|
||||
{
|
||||
let _1 := 1
|
||||
sstore(_1, _1)
|
||||
@ -25,21 +25,35 @@ object "object" {
|
||||
sstore(11, _1)
|
||||
sstore(12, _1)
|
||||
sstore(13, _1)
|
||||
a3 := _1
|
||||
b3 := _1
|
||||
c3 := _1
|
||||
d3 := _1
|
||||
e3 := _1
|
||||
f3 := _1
|
||||
g3 := _1
|
||||
h3 := _1
|
||||
i3 := _1
|
||||
j3 := _1
|
||||
k3 := _1
|
||||
l3 := _1
|
||||
m3 := _1
|
||||
o3 := _1
|
||||
p3 := _1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary representation:
|
||||
60056032565b505050505050505050505050505050601a6032565b5050505050505050505050505050508082555050609b565b60006000600060006000600060006000600060006000600060006000600060006001808155806002558060035580600455806005558060065580600755806008558060095580600a5580600b5580600c5580600d55505b909192939495969798999a9b9c9d9e9f565b
|
||||
60056030565b505050505050505050505050505060196030565b5050505050505050505050505050808255505060c3565b6000600060006000600060006000600060006000600060006000600060006001808155806002558060035580600455806005558060065580600755806008558060095580600a5580600b5580600c5580600d55809f50809e50809d50809c50809b50809a50809950809850809750809650809550809450809350809250809150505b909192939495969798999a9b9c9d9e565b
|
||||
|
||||
Text representation:
|
||||
/* "yul_stack_opt/input.sol":495:500 */
|
||||
/* "yul_stack_opt/input.sol":3:573 */
|
||||
tag_1
|
||||
tag_2
|
||||
jump // in
|
||||
tag_1:
|
||||
/* "yul_stack_opt/input.sol":425:500 */
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
@ -54,13 +68,10 @@ tag_1:
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":572:577 */
|
||||
tag_3
|
||||
tag_2
|
||||
jump // in
|
||||
tag_3:
|
||||
/* "yul_stack_opt/input.sol":502:577 */
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
@ -75,16 +86,15 @@ tag_3:
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":590:592 */
|
||||
/* "yul_stack_opt/input.sol":740:742 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":586:588 */
|
||||
/* "yul_stack_opt/input.sol":736:738 */
|
||||
dup3
|
||||
/* "yul_stack_opt/input.sol":579:593 */
|
||||
/* "yul_stack_opt/input.sol":729:743 */
|
||||
sstore
|
||||
pop
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":3:423 */
|
||||
/* "yul_stack_opt/input.sol":3:573 */
|
||||
jump(tag_4)
|
||||
tag_2:
|
||||
0x00
|
||||
@ -101,7 +111,6 @@ tag_2:
|
||||
0x00
|
||||
0x00
|
||||
0x00
|
||||
0x00
|
||||
0x00
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
0x01
|
||||
@ -181,8 +190,83 @@ tag_2:
|
||||
0x0d
|
||||
/* "yul_stack_opt/input.sol":399:420 */
|
||||
sstore
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":423:430 */
|
||||
swap16
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":85:423 */
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":433:440 */
|
||||
swap15
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":443:450 */
|
||||
swap14
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":453:460 */
|
||||
swap13
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":463:470 */
|
||||
swap12
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":473:480 */
|
||||
swap11
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":483:490 */
|
||||
swap10
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":493:500 */
|
||||
swap9
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":503:510 */
|
||||
swap8
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":513:520 */
|
||||
swap7
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":523:530 */
|
||||
swap6
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":533:540 */
|
||||
swap5
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":543:550 */
|
||||
swap4
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":553:560 */
|
||||
swap3
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":98:99 */
|
||||
dup1
|
||||
/* "yul_stack_opt/input.sol":563:570 */
|
||||
swap2
|
||||
pop
|
||||
pop
|
||||
/* "yul_stack_opt/input.sol":85:573 */
|
||||
tag_5:
|
||||
swap1
|
||||
swap2
|
||||
@ -199,6 +283,5 @@ tag_5:
|
||||
swap13
|
||||
swap14
|
||||
swap15
|
||||
swap16
|
||||
jump // out
|
||||
tag_4:
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <test/Metadata.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
using namespace std;
|
||||
@ -1235,9 +1236,16 @@ BOOST_AUTO_TEST_CASE(use_stack_optimization)
|
||||
BOOST_REQUIRE(contract["evm"]["bytecode"]["object"].isString());
|
||||
BOOST_CHECK(contract["evm"]["bytecode"]["object"].asString().length() > 20);
|
||||
|
||||
// Now disable stack optimizations
|
||||
// Now disable stack optimizations and UnusedFunctionParameterPruner (p)
|
||||
// results in "stack too deep"
|
||||
string optimiserSteps = OptimiserSettings::DefaultYulOptimiserSteps;
|
||||
optimiserSteps.erase(
|
||||
remove_if(optimiserSteps.begin(), optimiserSteps.end(), [](char ch) { return ch == 'p'; }),
|
||||
optimiserSteps.end()
|
||||
);
|
||||
parsedInput["settings"]["optimizer"]["details"]["yulDetails"]["stackAllocation"] = false;
|
||||
parsedInput["settings"]["optimizer"]["details"]["yulDetails"]["optimizerSteps"] = optimiserSteps;
|
||||
|
||||
result = compiler.compile(parsedInput);
|
||||
BOOST_REQUIRE(result["errors"].isArray());
|
||||
BOOST_CHECK(result["errors"][0]["severity"] == "error");
|
||||
|
@ -17,9 +17,9 @@ contract C {
|
||||
// optimize-yul: true
|
||||
// ----
|
||||
// creation:
|
||||
// codeDepositCost: 616600
|
||||
// codeDepositCost: 614600
|
||||
// executionCost: 651
|
||||
// totalCost: 617251
|
||||
// totalCost: 615251
|
||||
// external:
|
||||
// a(): 1029
|
||||
// b(uint256): 2084
|
||||
|
@ -49,6 +49,7 @@
|
||||
#include <libyul/optimiser/NameDisplacer.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
#include <libyul/optimiser/ExpressionSimplifier.h>
|
||||
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
|
||||
#include <libyul/optimiser/UnusedPruner.h>
|
||||
#include <libyul/optimiser/ExpressionJoiner.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
@ -241,6 +242,13 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
||||
ExpressionJoiner::run(*m_context, *m_object->code);
|
||||
ExpressionJoiner::run(*m_context, *m_object->code);
|
||||
}
|
||||
else if (m_optimizerStep == "unusedFunctionParameterPruner")
|
||||
{
|
||||
disambiguate();
|
||||
FunctionHoister::run(*m_context, *m_object->code);
|
||||
LiteralRematerialiser::run(*m_context, *m_object->code);
|
||||
UnusedFunctionParameterPruner::run(*m_context, *m_object->code);
|
||||
}
|
||||
else if (m_optimizerStep == "unusedPruner")
|
||||
{
|
||||
disambiguate();
|
||||
|
@ -49,7 +49,7 @@
|
||||
// sstore(not(gcd(10, 15)), 1)
|
||||
// sstore(0, 0)
|
||||
// sstore(2, 1)
|
||||
// pop(foo_singlereturn_1(calldataload(0), calldataload(3)))
|
||||
// extcodecopy(1, msize(), 1, 1)
|
||||
// sstore(0, 0)
|
||||
// sstore(3, 1)
|
||||
// }
|
||||
@ -59,6 +59,4 @@
|
||||
// case 0 { out := _a }
|
||||
// default { out := gcd(_b, mod(_a, _b)) }
|
||||
// }
|
||||
// function foo_singlereturn_1(in, in_1) -> out
|
||||
// { extcodecopy(1, msize(), 1, 1) }
|
||||
// }
|
||||
|
@ -0,0 +1,31 @@
|
||||
{
|
||||
let x, y := foo(sload(0),sload(32))
|
||||
sstore(0, x)
|
||||
sstore(0, y)
|
||||
x, y := foo(sload(32), sload(8))
|
||||
|
||||
function foo(a, b) -> out1, out2
|
||||
{
|
||||
out1 := mload(32)
|
||||
out1 := sload(out1)
|
||||
out2 := add(out1, 1)
|
||||
extcodecopy(out1, out2, 1, b)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: fullSuite
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// let out1, out2 := foo(sload(32))
|
||||
// sstore(0, out1)
|
||||
// sstore(0, out2)
|
||||
// let out1_1, out2_1 := foo(sload(8))
|
||||
// }
|
||||
// function foo(b) -> out1, out2
|
||||
// {
|
||||
// out1 := sload(mload(32))
|
||||
// out2 := add(out1, 1)
|
||||
// extcodecopy(out1, out2, 1, b)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,33 @@
|
||||
{
|
||||
sstore(f(1), 1)
|
||||
sstore(f(2), 1)
|
||||
sstore(f(3), 1)
|
||||
function f(a) -> x {
|
||||
for {let b := 10} iszero(b) { b := sub(b, 1) }
|
||||
{
|
||||
a := calldataload(0)
|
||||
mstore(a, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: fullSuite
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// }
|
||||
// function f()
|
||||
// {
|
||||
// let b := 10
|
||||
// let _1 := 0
|
||||
// let a := calldataload(_1)
|
||||
// for { } iszero(b) { b := add(b, not(0)) }
|
||||
// { mstore(a, _1) }
|
||||
// }
|
||||
// }
|
@ -0,0 +1,29 @@
|
||||
{
|
||||
let j, k, l := f(1, 2, 3)
|
||||
sstore(0, j)
|
||||
sstore(1, k)
|
||||
sstore(1, l)
|
||||
function f(a, b, c) -> x, y, z
|
||||
{
|
||||
x, y, z := f(1, 2, 3)
|
||||
x := add(x, 1)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: fullSuite
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// let x, y, z := f()
|
||||
// sstore(0, x)
|
||||
// sstore(1, y)
|
||||
// sstore(1, z)
|
||||
// }
|
||||
// function f() -> x, y, z
|
||||
// {
|
||||
// let x_1, y_1, z_1 := f()
|
||||
// y := y_1
|
||||
// z := z_1
|
||||
// x := add(x_1, 1)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,34 @@
|
||||
{
|
||||
let x, y, z := foo(sload(0),sload(32))
|
||||
sstore(0, x)
|
||||
sstore(0, y)
|
||||
sstore(0, z)
|
||||
x, y, z := foo(sload(32), sload(8))
|
||||
|
||||
// out3 is unassigned.
|
||||
function foo(a, b) -> out1, out2, out3
|
||||
{
|
||||
out1 := mload(32)
|
||||
out1 := sload(out1)
|
||||
out2 := add(out1, 1)
|
||||
extcodecopy(out1, out1, 1, b)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: fullSuite
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// let out1, out2 := foo(sload(32))
|
||||
// sstore(0, out1)
|
||||
// sstore(0, out2)
|
||||
// sstore(0, 0)
|
||||
// let out1_1, out2_1 := foo(sload(8))
|
||||
// }
|
||||
// function foo(b) -> out1, out2
|
||||
// {
|
||||
// out1 := sload(mload(32))
|
||||
// out2 := add(out1, 1)
|
||||
// extcodecopy(out1, out1, 1, b)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,36 @@
|
||||
{
|
||||
sstore(f(1), 1)
|
||||
sstore(f(2), 1)
|
||||
sstore(f(3), 1)
|
||||
function f(a) -> x {
|
||||
// The usage of a is redundant
|
||||
a := calldataload(0)
|
||||
mstore(a, x)
|
||||
// to prevent getting fully inlined
|
||||
sstore(1, 1)
|
||||
sstore(2, 2)
|
||||
sstore(3, 3)
|
||||
sstore(3, 3)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: fullSuite
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// }
|
||||
// function f()
|
||||
// {
|
||||
// mstore(calldataload(0), 0)
|
||||
// sstore(1, 1)
|
||||
// sstore(2, 2)
|
||||
// sstore(3, 3)
|
||||
// sstore(3, 3)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,27 @@
|
||||
{
|
||||
let a := f(mload(1))
|
||||
let b := f(a)
|
||||
sstore(a, b)
|
||||
function f(x) -> y
|
||||
{
|
||||
// Test if LiteralRematerializer can convert `y` to `0` and therefore allowing us to
|
||||
// rewrite the function f
|
||||
if iszero(x) { revert(y, y)}
|
||||
if iszero(add(x, 1)) { revert(y, y)}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// let a := f_1(mload(1))
|
||||
// let b := f_1(a)
|
||||
// sstore(a, b)
|
||||
// function f(x)
|
||||
// {
|
||||
// if iszero(x) { revert(0, 0) }
|
||||
// if iszero(add(x, 1)) { revert(0, 0) }
|
||||
// }
|
||||
// function f_1(x_2) -> y_3
|
||||
// { f(x_2) }
|
||||
// }
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
let d, e, i := f(1, 2, 3)
|
||||
sstore(d, 0)
|
||||
// b and x are unused
|
||||
function f(a, b, c) -> x, y, z
|
||||
{
|
||||
y := mload(a)
|
||||
z := mload(c)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// let d, e, i := f_1(1, 2, 3)
|
||||
// sstore(d, 0)
|
||||
// function f(a, c) -> y, z
|
||||
// {
|
||||
// y := mload(a)
|
||||
// z := mload(c)
|
||||
// }
|
||||
// function f_1(a_2, b_3, c_4) -> x_5, y_6, z_7
|
||||
// { y_6, z_7 := f(a_2, c_4) }
|
||||
// }
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
let a, b, c := f(sload(0))
|
||||
sstore(a, 0)
|
||||
// d is unused, x is unassigned
|
||||
function f(d) -> x, y, z
|
||||
{
|
||||
y := mload(1)
|
||||
z := mload(2)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// let a, b, c := f_1(sload(0))
|
||||
// sstore(a, 0)
|
||||
// function f() -> y, z
|
||||
// {
|
||||
// y := mload(1)
|
||||
// z := mload(2)
|
||||
// }
|
||||
// function f_1(d_2) -> x_3, y_4, z_5
|
||||
// { y_4, z_5 := f() }
|
||||
// }
|
@ -0,0 +1,56 @@
|
||||
// Test case to see if the step applies for nested functions. The function `j` has an unused argument.
|
||||
{
|
||||
sstore(f(1), 0)
|
||||
sstore(h(1), 0)
|
||||
|
||||
function f(a) -> x
|
||||
{
|
||||
x := g(1)
|
||||
x := add(x, 1)
|
||||
function g(b) -> y
|
||||
{
|
||||
b := add(b, 1)
|
||||
y := mload(b)
|
||||
}
|
||||
}
|
||||
|
||||
function h(c) -> u
|
||||
{
|
||||
u := j(c)
|
||||
// d is unused
|
||||
function j(d) -> w
|
||||
{
|
||||
w := 13
|
||||
w := add(w, 1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// sstore(f_1(1), 0)
|
||||
// sstore(h(1), 0)
|
||||
// function g(b) -> y
|
||||
// {
|
||||
// b := add(b, 1)
|
||||
// y := mload(b)
|
||||
// }
|
||||
// function f() -> x
|
||||
// {
|
||||
// x := g(1)
|
||||
// x := add(x, 1)
|
||||
// }
|
||||
// function f_1(a_3) -> x_4
|
||||
// { x_4 := f() }
|
||||
// function j() -> w
|
||||
// {
|
||||
// w := 13
|
||||
// w := add(13, 1)
|
||||
// }
|
||||
// function j_2(d_5) -> w_6
|
||||
// { w_6 := j() }
|
||||
// function h(c) -> u
|
||||
// { u := j_2(c) }
|
||||
// }
|
@ -0,0 +1,52 @@
|
||||
// Test case where the name `g` occurs at two different places. Test to see if name-collision can
|
||||
// cause issues.
|
||||
{
|
||||
sstore(h(1), 0)
|
||||
sstore(f(1), 0)
|
||||
|
||||
function f(c) -> u
|
||||
{
|
||||
u := g(c)
|
||||
function g(d) -> w
|
||||
{
|
||||
w := 13
|
||||
sstore(0, w)
|
||||
}
|
||||
}
|
||||
|
||||
function h(c) -> u
|
||||
{
|
||||
u := g(c)
|
||||
function g(d) -> w
|
||||
{
|
||||
w := 13
|
||||
sstore(0, w)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// sstore(h(1), 0)
|
||||
// sstore(f(1), 0)
|
||||
// function g() -> w
|
||||
// {
|
||||
// w := 13
|
||||
// sstore(0, 13)
|
||||
// }
|
||||
// function g_1(d_3) -> w_4
|
||||
// { w_4 := g() }
|
||||
// function f(c) -> u
|
||||
// { u := g_1(c) }
|
||||
// function g_3() -> w_5
|
||||
// {
|
||||
// w_5 := 13
|
||||
// sstore(0, 13)
|
||||
// }
|
||||
// function g_3_2(d_4_5) -> w_5_6
|
||||
// { w_5_6 := g_3() }
|
||||
// function h(c_1) -> u_2
|
||||
// { u_2 := g_3_2(c_1) }
|
||||
// }
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
f(1, 2)
|
||||
function f(a, b)
|
||||
{
|
||||
sstore(a, 0)
|
||||
a := add(a, 1)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// f_1(1, 2)
|
||||
// function f(a)
|
||||
// {
|
||||
// sstore(a, 0)
|
||||
// a := add(a, 1)
|
||||
// }
|
||||
// function f_1(a_2, b_3)
|
||||
// { f(a_2) }
|
||||
// }
|
@ -0,0 +1,16 @@
|
||||
// A test where all parameters are used
|
||||
{
|
||||
sstore(f(1), 0)
|
||||
function f(a) -> x { x := a }
|
||||
function g(b) -> y { y := g(b) }
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// sstore(f(1), 0)
|
||||
// function f(a) -> x
|
||||
// { x := a }
|
||||
// function g(b) -> y
|
||||
// { y := g(b) }
|
||||
// }
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
let a1, b1, c1 := f(1, 2, 3)
|
||||
function f(a, b, c) -> x, y, z
|
||||
{
|
||||
x, y, z := f(1, 2, 3)
|
||||
x := add(x, 1)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// let a1, b1, c1 := f_1(1, 2, 3)
|
||||
// function f() -> x, y, z
|
||||
// {
|
||||
// x, y, z := f_1(1, 2, 3)
|
||||
// x := add(x, 1)
|
||||
// }
|
||||
// function f_1(a_2, b_3, c_4) -> x_5, y_6, z_7
|
||||
// { x_5, y_6, z_7 := f() }
|
||||
// }
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
sstore(f(1), 0)
|
||||
function f(x) -> y
|
||||
{
|
||||
let w := mload(1)
|
||||
y := mload(w)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// sstore(f_1(1), 0)
|
||||
// function f() -> y
|
||||
// {
|
||||
// let w := mload(1)
|
||||
// y := mload(w)
|
||||
// }
|
||||
// function f_1(x_2) -> y_3
|
||||
// { y_3 := f() }
|
||||
// }
|
@ -0,0 +1,5 @@
|
||||
{}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// { }
|
@ -0,0 +1,23 @@
|
||||
{
|
||||
let a, b := f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
|
||||
sstore(a, b)
|
||||
function f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) -> y, z
|
||||
{
|
||||
y := mload(x3)
|
||||
z := mload(x7)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedFunctionParameterPruner
|
||||
//
|
||||
// {
|
||||
// let a, b := f_1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
|
||||
// sstore(a, b)
|
||||
// function f(x3, x7) -> y, z
|
||||
// {
|
||||
// y := mload(x3)
|
||||
// z := mload(x7)
|
||||
// }
|
||||
// function f_1(x1_2, x2_3, x3_4, x4_5, x5_6, x6_7, x7_8, x8_9, x9_10, x10_11, x11_12, x12_13, x13_14, x14_15, x15_16, x16_17, x17_18, x18_19) -> y_20, z_21
|
||||
// { y_20, z_21 := f(x3_4, x7_8) }
|
||||
// }
|
@ -130,7 +130,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) == "flcCUnDvejsxIOoighTLMrmVatud");
|
||||
BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighTLMrmVatpud");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(randomOptimisationStep_should_return_each_step_with_same_probability)
|
||||
|
@ -103,10 +103,10 @@ BOOST_AUTO_TEST_CASE(geneAddition_should_iterate_over_gene_positions_and_insert_
|
||||
SimulationRNG::reset(1);
|
||||
// f c C U n D v e j s
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace(" f c C UC n D v e jx s"))); // 20% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("j f cu C U ne D v eI j sf"))); // 50% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("s f cu C U nj D v eI j sf"))); // 50% more
|
||||
SimulationRNG::reset(2);
|
||||
BOOST_TEST(mutation01(chromosome) == Chromosome(stripWhitespace(" f cu C U n D v e j s"))); // 10% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("L f ce Cv U n D v e jO s"))); // 40% more
|
||||
BOOST_TEST(mutation05(chromosome) == Chromosome(stripWhitespace("M f ce Cv U n D v e jo s"))); // 40% more
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(geneAddition_should_be_able_to_insert_before_first_position)
|
||||
|
Loading…
Reference in New Issue
Block a user