mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implemented UnusedFunctionArgumentPruner
This commit is contained in:
@@ -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'},
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user