2017-12-20 12:48:23 +00:00
|
|
|
/*(
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimisation stage that removes unused variables and functions.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
2017-12-20 12:48:23 +00:00
|
|
|
|
2019-08-29 13:26:36 +00:00
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/optimiser/OptimizerUtilities.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2019-05-29 21:23:51 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2019-08-29 13:26:36 +00:00
|
|
|
#include <libyul/SideEffects.h>
|
2017-12-20 12:48:23 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/cxx11/none_of.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2017-12-20 12:48:23 +00:00
|
|
|
|
2019-05-27 11:42:50 +00:00
|
|
|
UnusedPruner::UnusedPruner(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Block& _ast,
|
|
|
|
bool _allowMSizeOptimization,
|
2019-08-29 13:26:36 +00:00
|
|
|
map<YulString, SideEffects> const* _functionSideEffects,
|
2019-05-27 11:42:50 +00:00
|
|
|
set<YulString> const& _externallyUsedFunctions
|
|
|
|
):
|
|
|
|
m_dialect(_dialect),
|
2019-08-29 13:26:36 +00:00
|
|
|
m_allowMSizeOptimization(_allowMSizeOptimization),
|
|
|
|
m_functionSideEffects(_functionSideEffects)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
2019-02-04 13:01:05 +00:00
|
|
|
m_references = ReferencesCounter::countReferences(_ast);
|
|
|
|
for (auto const& f: _externallyUsedFunctions)
|
|
|
|
++m_references[f];
|
|
|
|
}
|
2017-12-20 12:48:23 +00:00
|
|
|
|
2019-05-27 11:42:50 +00:00
|
|
|
UnusedPruner::UnusedPruner(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
FunctionDefinition& _function,
|
|
|
|
bool _allowMSizeOptimization,
|
|
|
|
set<YulString> const& _externallyUsedFunctions
|
|
|
|
):
|
|
|
|
m_dialect(_dialect),
|
|
|
|
m_allowMSizeOptimization(_allowMSizeOptimization)
|
2019-02-04 13:01:05 +00:00
|
|
|
{
|
|
|
|
m_references = ReferencesCounter::countReferences(_function);
|
2018-01-17 11:05:43 +00:00
|
|
|
for (auto const& f: _externallyUsedFunctions)
|
|
|
|
++m_references[f];
|
2017-12-20 12:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedPruner::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
for (auto&& statement: _block.statements)
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionDefinition>(statement))
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
FunctionDefinition& funDef = std::get<FunctionDefinition>(statement);
|
2017-12-20 12:48:23 +00:00
|
|
|
if (!used(funDef.name))
|
|
|
|
{
|
|
|
|
subtractReferences(ReferencesCounter::countReferences(funDef.body));
|
|
|
|
statement = Block{std::move(funDef.location), {}};
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<VariableDeclaration>(statement))
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
VariableDeclaration& varDecl = std::get<VariableDeclaration>(statement);
|
2018-02-05 17:16:08 +00:00
|
|
|
// Multi-variable declarations are special. We can only remove it
|
2019-03-06 11:21:56 +00:00
|
|
|
// if all variables are unused and the right-hand-side is either
|
|
|
|
// movable or it returns a single value. In the latter case, we
|
2018-06-18 21:26:31 +00:00
|
|
|
// replace `let a := f()` by `pop(f())` (in pure Yul, this will be
|
2018-02-05 17:16:08 +00:00
|
|
|
// `drop(f())`).
|
2017-12-20 12:48:23 +00:00
|
|
|
if (boost::algorithm::none_of(
|
|
|
|
varDecl.variables,
|
|
|
|
[=](TypedName const& _typedName) { return used(_typedName.name); }
|
|
|
|
))
|
|
|
|
{
|
2018-02-06 09:53:49 +00:00
|
|
|
if (!varDecl.value)
|
|
|
|
statement = Block{std::move(varDecl.location), {}};
|
2019-08-29 13:26:36 +00:00
|
|
|
else if (
|
|
|
|
SideEffectsCollector(m_dialect, *varDecl.value, m_functionSideEffects).
|
|
|
|
sideEffectFree(m_allowMSizeOptimization)
|
|
|
|
)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
|
|
|
statement = Block{std::move(varDecl.location), {}};
|
|
|
|
}
|
2019-05-29 21:23:51 +00:00
|
|
|
else if (varDecl.variables.size() == 1 && m_dialect.discardFunction())
|
|
|
|
statement = ExpressionStatement{varDecl.location, FunctionCall{
|
2017-12-20 12:48:23 +00:00
|
|
|
varDecl.location,
|
2019-05-29 21:23:51 +00:00
|
|
|
{varDecl.location, m_dialect.discardFunction()->name},
|
2017-12-20 12:48:23 +00:00
|
|
|
{*std::move(varDecl.value)}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<ExpressionStatement>(statement))
|
2018-10-28 11:58:21 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
ExpressionStatement& exprStmt = std::get<ExpressionStatement>(statement);
|
2019-08-29 13:26:36 +00:00
|
|
|
if (
|
|
|
|
SideEffectsCollector(m_dialect, exprStmt.expression, m_functionSideEffects).
|
|
|
|
sideEffectFree(m_allowMSizeOptimization)
|
|
|
|
)
|
2018-10-28 11:58:21 +00:00
|
|
|
{
|
|
|
|
subtractReferences(ReferencesCounter::countReferences(exprStmt.expression));
|
|
|
|
statement = Block{std::move(exprStmt.location), {}};
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 12:48:23 +00:00
|
|
|
|
|
|
|
removeEmptyBlocks(_block);
|
|
|
|
|
|
|
|
ASTModifier::operator()(_block);
|
|
|
|
}
|
|
|
|
|
2018-12-20 17:55:32 +00:00
|
|
|
void UnusedPruner::runUntilStabilised(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Block& _ast,
|
2019-07-15 12:29:29 +00:00
|
|
|
bool _allowMSizeOptimization,
|
2019-08-29 13:26:36 +00:00
|
|
|
map<YulString, SideEffects> const* _functionSideEffects,
|
2018-12-20 17:55:32 +00:00
|
|
|
set<YulString> const& _externallyUsedFunctions
|
|
|
|
)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2019-08-29 13:26:36 +00:00
|
|
|
UnusedPruner pruner(
|
|
|
|
_dialect, _ast, _allowMSizeOptimization, _functionSideEffects,
|
|
|
|
_externallyUsedFunctions);
|
2017-12-20 12:48:23 +00:00
|
|
|
pruner(_ast);
|
|
|
|
if (!pruner.shouldRunAgain())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:26:36 +00:00
|
|
|
void UnusedPruner::runUntilStabilisedOnFullAST(
|
2019-05-27 11:42:50 +00:00
|
|
|
Dialect const& _dialect,
|
|
|
|
Block& _ast,
|
|
|
|
set<YulString> const& _externallyUsedFunctions
|
|
|
|
)
|
|
|
|
{
|
2019-08-29 13:26:36 +00:00
|
|
|
map<YulString, SideEffects> functionSideEffects =
|
|
|
|
SideEffectsPropagator::sideEffects(_dialect, CallGraphGenerator::callGraph(_ast));
|
2019-08-13 11:34:33 +00:00
|
|
|
bool allowMSizeOptimization = !MSizeFinder::containsMSize(_dialect, _ast);
|
2019-08-29 13:26:36 +00:00
|
|
|
runUntilStabilised(_dialect, _ast, allowMSizeOptimization, &functionSideEffects, _externallyUsedFunctions);
|
2019-05-27 11:42:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:05 +00:00
|
|
|
void UnusedPruner::runUntilStabilised(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
FunctionDefinition& _function,
|
2019-05-27 11:42:50 +00:00
|
|
|
bool _allowMSizeOptimization,
|
2019-02-04 13:01:05 +00:00
|
|
|
set<YulString> const& _externallyUsedFunctions
|
|
|
|
)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2019-05-27 11:42:50 +00:00
|
|
|
UnusedPruner pruner(_dialect, _function, _allowMSizeOptimization, _externallyUsedFunctions);
|
2019-02-04 13:01:05 +00:00
|
|
|
pruner(_function);
|
|
|
|
if (!pruner.shouldRunAgain())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
bool UnusedPruner::used(YulString _name) const
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
return m_references.count(_name) && m_references.at(_name) > 0;
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
void UnusedPruner::subtractReferences(map<YulString, size_t> const& _subtrahend)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
for (auto const& ref: _subtrahend)
|
|
|
|
{
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(m_references.count(ref.first), OptimizerException, "");
|
|
|
|
assertThrow(m_references.at(ref.first) >= ref.second, OptimizerException, "");
|
2017-12-20 12:48:23 +00:00
|
|
|
m_references[ref.first] -= ref.second;
|
|
|
|
m_shouldRunAgain = true;
|
|
|
|
}
|
|
|
|
}
|