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
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
#include <libyul/optimiser/Utilities.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
2017-12-20 12:48:23 +00:00
|
|
|
|
|
|
|
#include <libsolidity/inlineasm/AsmData.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/cxx11/none_of.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
2018-10-15 09:58:51 +00:00
|
|
|
using namespace dev::yul;
|
2017-12-20 12:48:23 +00:00
|
|
|
|
2018-01-17 11:05:43 +00:00
|
|
|
UnusedPruner::UnusedPruner(Block& _ast, set<string> const& _externallyUsedFunctions)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
ReferencesCounter counter;
|
|
|
|
counter(_ast);
|
|
|
|
|
|
|
|
m_references = counter.references();
|
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)
|
|
|
|
if (statement.type() == typeid(FunctionDefinition))
|
|
|
|
{
|
|
|
|
FunctionDefinition& funDef = boost::get<FunctionDefinition>(statement);
|
|
|
|
if (!used(funDef.name))
|
|
|
|
{
|
|
|
|
subtractReferences(ReferencesCounter::countReferences(funDef.body));
|
|
|
|
statement = Block{std::move(funDef.location), {}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (statement.type() == typeid(VariableDeclaration))
|
|
|
|
{
|
|
|
|
VariableDeclaration& varDecl = boost::get<VariableDeclaration>(statement);
|
2018-02-05 17:16:08 +00:00
|
|
|
// Multi-variable declarations are special. We can only remove it
|
|
|
|
// if all vairables are unused and the right-hand-side is either
|
|
|
|
// movable or it return 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), {}};
|
|
|
|
else if (MovableChecker(*varDecl.value).movable())
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
|
|
|
statement = Block{std::move(varDecl.location), {}};
|
|
|
|
}
|
2018-02-06 09:53:49 +00:00
|
|
|
else if (varDecl.variables.size() == 1)
|
2018-06-18 21:26:31 +00:00
|
|
|
// In pure Yul, this should be replaced by a function call to `drop`
|
2018-02-05 17:16:08 +00:00
|
|
|
// instead of `pop`.
|
2017-12-20 12:48:23 +00:00
|
|
|
statement = ExpressionStatement{varDecl.location, FunctionalInstruction{
|
|
|
|
varDecl.location,
|
|
|
|
solidity::Instruction::POP,
|
|
|
|
{*std::move(varDecl.value)}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEmptyBlocks(_block);
|
|
|
|
|
|
|
|
ASTModifier::operator()(_block);
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:05:43 +00:00
|
|
|
void UnusedPruner::runUntilStabilised(Block& _ast, set<string> const& _externallyUsedFunctions)
|
2017-12-20 12:48:23 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2018-01-17 11:05:43 +00:00
|
|
|
UnusedPruner pruner(_ast, _externallyUsedFunctions);
|
2017-12-20 12:48:23 +00:00
|
|
|
pruner(_ast);
|
|
|
|
if (!pruner.shouldRunAgain())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnusedPruner::used(string const& _name) const
|
|
|
|
{
|
|
|
|
return m_references.count(_name) && m_references.at(_name) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedPruner::subtractReferences(map<string, size_t> const& _subtrahend)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|