Merge pull request #10536 from ethereum/fix-createlinkingfunction-to-cpp

Move the implementation of createLinkingFunction into a cpp file
This commit is contained in:
chriseth 2020-12-09 17:49:16 +01:00 committed by GitHub
commit d75821e068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 63 deletions

View File

@ -163,6 +163,22 @@ auto applyMap(Container const& _c, Callable&& _op, OutputContainer _oc = OutputC
return _oc;
}
/// Filter a vector.
/// Returns a copy of the vector after only taking indices `i` such that `_mask[i]` is true.
template<typename T>
std::vector<T> filter(std::vector<T> const& _vec, std::vector<bool> const& _mask)
{
assert(_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;
}
/// Functional fold.
/// Given a container @param _c, an initial value @param _acc,
/// and a binary operator @param _binaryOp(T, U), accumulate

View File

@ -189,6 +189,7 @@ add_library(yul
optimiser/UnusedFunctionParameterPruner.cpp
optimiser/UnusedFunctionParameterPruner.h
optimiser/UnusedFunctionsCommon.h
optimiser/UnusedFunctionsCommon.cpp
optimiser/UnusedPruner.cpp
optimiser/UnusedPruner.h
optimiser/VarDeclInitializer.cpp

View File

@ -0,0 +1,77 @@
/*
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
#include <libyul/optimiser/UnusedFunctionsCommon.h>
#include <libyul/Dialect.h>
#include <liblangutil/SourceLocation.h>
#include <libsolutil/CommonData.h>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::yul;
using namespace solidity::yul::unusedFunctionsCommon;
FunctionDefinition unusedFunctionsCommon::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;
}

View File

@ -19,85 +19,42 @@
#include <libyul/optimiser/Metrics.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/AST.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)
inline bool tooSimpleToBePruned(FunctionDefinition const& _f)
{
return _f.body.statements.size() <= 1 && CodeSize::codeSize(_f.body) <= 1;
}
/// Given a function definition `_original`, this function returns a 'linking' function that calls
/// `_originalFunctionName` (with reduced parameters and return values).
///
/// The parameter `_usedParametersAndReturnVariables` is a pair of boolean-vectors. Its `.first`
/// corresponds to function parameters and its `.second` corresponds to function return-variables. A
/// false value at index `i` means that the corresponding function parameter / return-variable at
/// index `i` is unused.
///
/// Example:
///
/// Let `_original` be the function `function f_1() -> y { }`. (In practice, this function usually cannot
/// be inlined and has parameters / return-variables that are unused.)
/// Let `_usedParametersAndReturnVariables` be `({}, {false})`
/// Let `_originalFunctionName` be `f`.
/// Let `_linkingFunctionName` be `f_1`.
///
/// Then the returned linking function would be `function f_1() -> y_1 { f() }`
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;
}
);
}