2018-10-23 13:55:48 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimiser component that removes assignments to variables that are not used
|
|
|
|
* until they go out of scope or are re-assigned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
|
|
|
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
|
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
2018-11-21 11:42:34 +00:00
|
|
|
using namespace yul;
|
2018-10-23 13:55:48 +00:00
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
changeUndecidedTo(_identifier.name, State::Used);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(VariableDeclaration const& _variableDeclaration)
|
|
|
|
{
|
|
|
|
ASTWalker::operator()(_variableDeclaration);
|
|
|
|
|
|
|
|
for (auto const& var: _variableDeclaration.variables)
|
2018-10-29 14:12:02 +00:00
|
|
|
m_declaredVariables.emplace(var.name);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Assignment const& _assignment)
|
|
|
|
{
|
|
|
|
visit(*_assignment.value);
|
|
|
|
for (auto const& var: _assignment.variableNames)
|
|
|
|
changeUndecidedTo(var.name, State::Unused);
|
|
|
|
|
|
|
|
if (_assignment.variableNames.size() == 1)
|
|
|
|
// Default-construct it in "Undecided" state if it does not yet exist.
|
|
|
|
m_assignments[_assignment.variableNames.front().name][&_assignment];
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(If const& _if)
|
|
|
|
{
|
|
|
|
visit(*_if.condition);
|
|
|
|
|
|
|
|
RedundantAssignEliminator branch{*this};
|
|
|
|
branch(_if.body);
|
|
|
|
|
|
|
|
join(branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
visit(*_switch.expression);
|
|
|
|
|
|
|
|
bool hasDefault = false;
|
|
|
|
vector<RedundantAssignEliminator> branches;
|
|
|
|
for (auto const& c: _switch.cases)
|
|
|
|
{
|
|
|
|
if (!c.value)
|
|
|
|
hasDefault = true;
|
|
|
|
branches.emplace_back(*this);
|
|
|
|
branches.back()(c.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasDefault)
|
|
|
|
{
|
|
|
|
*this = std::move(branches.back());
|
|
|
|
branches.pop_back();
|
|
|
|
}
|
|
|
|
for (auto& branch: branches)
|
|
|
|
join(branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
2019-02-21 19:04:20 +00:00
|
|
|
std::set<YulString> declaredVariables;
|
|
|
|
std::map<YulString, std::map<Assignment const*, State>> assignments;
|
|
|
|
swap(m_declaredVariables, declaredVariables);
|
|
|
|
swap(m_assignments, assignments);
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
(*this)(_functionDefinition.body);
|
|
|
|
|
|
|
|
for (auto const& param: _functionDefinition.parameters)
|
2018-11-03 15:04:42 +00:00
|
|
|
{
|
2018-10-23 13:55:48 +00:00
|
|
|
changeUndecidedTo(param.name, State::Unused);
|
2018-11-03 15:04:42 +00:00
|
|
|
finalize(param.name);
|
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
for (auto const& retParam: _functionDefinition.returnVariables)
|
2018-11-03 15:04:42 +00:00
|
|
|
{
|
2018-10-23 13:55:48 +00:00
|
|
|
changeUndecidedTo(retParam.name, State::Used);
|
2018-11-03 15:04:42 +00:00
|
|
|
finalize(retParam.name);
|
|
|
|
}
|
2019-02-21 19:04:20 +00:00
|
|
|
|
|
|
|
swap(m_declaredVariables, declaredVariables);
|
|
|
|
swap(m_assignments, assignments);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(ForLoop const& _forLoop)
|
|
|
|
{
|
|
|
|
// This will set all variables that are declared in this
|
|
|
|
// block to "unused" when it is destroyed.
|
|
|
|
BlockScope scope(*this);
|
|
|
|
|
|
|
|
// We need to visit the statements directly because of the
|
|
|
|
// scoping rules.
|
|
|
|
walkVector(_forLoop.pre.statements);
|
|
|
|
|
|
|
|
// We just run the loop twice to account for the
|
|
|
|
// back edge.
|
|
|
|
// There need not be more runs because we only have three different states.
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
|
|
|
RedundantAssignEliminator zeroRuns{*this};
|
|
|
|
|
|
|
|
(*this)(_forLoop.body);
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
|
|
|
RedundantAssignEliminator oneRun{*this};
|
|
|
|
|
|
|
|
(*this)(_forLoop.body);
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
|
|
|
// Order does not matter because "max" is commutative and associative.
|
|
|
|
join(oneRun);
|
|
|
|
join(zeroRuns);
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
void RedundantAssignEliminator::operator()(Break const&)
|
|
|
|
{
|
|
|
|
yulAssert(false, "Not implemented yet.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Continue const&)
|
|
|
|
{
|
|
|
|
yulAssert(false, "Not implemented yet.");
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
void RedundantAssignEliminator::operator()(Block const& _block)
|
|
|
|
{
|
|
|
|
// This will set all variables that are declared in this
|
|
|
|
// block to "unused" when it is destroyed.
|
|
|
|
BlockScope scope(*this);
|
|
|
|
|
|
|
|
ASTWalker::operator()(_block);
|
|
|
|
}
|
|
|
|
|
2018-12-20 17:55:32 +00:00
|
|
|
void RedundantAssignEliminator::run(Dialect const& _dialect, Block& _ast)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2018-12-20 17:55:32 +00:00
|
|
|
RedundantAssignEliminator rae{_dialect};
|
2018-10-23 13:55:48 +00:00
|
|
|
rae(_ast);
|
|
|
|
|
2018-11-03 15:04:42 +00:00
|
|
|
AssignmentRemover remover{rae.m_assignmentsToRemove};
|
2018-10-23 13:55:48 +00:00
|
|
|
remover(_ast);
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:31 +00:00
|
|
|
template <class K, class V, class F>
|
|
|
|
void joinMap(std::map<K, V>& _a, std::map<K, V>&& _b, F _conflictSolver)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2018-10-29 14:12:31 +00:00
|
|
|
// TODO Perhaps it is better to just create a sorted list
|
|
|
|
// and then use insert(begin, end)
|
|
|
|
|
|
|
|
auto ita = _a.begin();
|
|
|
|
auto aend = _a.end();
|
|
|
|
auto itb = _b.begin();
|
|
|
|
auto bend = _b.end();
|
|
|
|
|
|
|
|
for (; itb != bend; ++ita)
|
|
|
|
{
|
|
|
|
if (ita == aend)
|
|
|
|
ita = _a.insert(ita, std::move(*itb++));
|
|
|
|
else if (ita->first < itb->first)
|
|
|
|
continue;
|
|
|
|
else if (itb->first < ita->first)
|
|
|
|
ita = _a.insert(ita, std::move(*itb++));
|
|
|
|
else
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2018-10-29 14:12:31 +00:00
|
|
|
_conflictSolver(ita->second, std::move(itb->second));
|
|
|
|
++itb;
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
2018-10-29 14:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::join(RedundantAssignEliminator& _other)
|
|
|
|
{
|
2018-11-03 15:04:42 +00:00
|
|
|
m_assignmentsToRemove.insert(begin(_other.m_assignmentsToRemove), end(_other.m_assignmentsToRemove));
|
|
|
|
|
2018-10-29 14:12:31 +00:00
|
|
|
joinMap(m_assignments, std::move(_other.m_assignments), [](
|
|
|
|
map<Assignment const*, State>& _assignmentHere,
|
|
|
|
map<Assignment const*, State>&& _assignmentThere
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return joinMap(_assignmentHere, std::move(_assignmentThere), State::join);
|
|
|
|
});
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
void RedundantAssignEliminator::changeUndecidedTo(YulString _variable, RedundantAssignEliminator::State _newState)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
|
|
|
for (auto& assignment: m_assignments[_variable])
|
|
|
|
if (assignment.second == State{State::Undecided})
|
|
|
|
assignment.second = _newState;
|
|
|
|
}
|
|
|
|
|
2018-11-03 15:04:42 +00:00
|
|
|
void RedundantAssignEliminator::finalize(YulString _variable)
|
|
|
|
{
|
|
|
|
for (auto& assignment: m_assignments[_variable])
|
|
|
|
{
|
|
|
|
assertThrow(assignment.second != State::Undecided, OptimizerException, "");
|
2018-12-20 17:55:32 +00:00
|
|
|
if (assignment.second == State{State::Unused} && MovableChecker{*m_dialect, *assignment.first->value}.movable())
|
2018-11-03 15:04:42 +00:00
|
|
|
// TODO the only point where we actually need this
|
|
|
|
// to be a set is for the for loop
|
|
|
|
m_assignmentsToRemove.insert(assignment.first);
|
|
|
|
}
|
|
|
|
m_assignments.erase(_variable);
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
void AssignmentRemover::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
boost::range::remove_erase_if(_block.statements, [=](Statement const& _statement) -> bool {
|
|
|
|
return _statement.type() == typeid(Assignment) && m_toRemove.count(&boost::get<Assignment>(_statement));
|
|
|
|
});
|
|
|
|
|
|
|
|
ASTModifier::operator()(_block);
|
|
|
|
}
|