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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-10-23 13:55:48 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2021-04-24 02:40:41 +00:00
|
|
|
#include <range/v3/action/remove_if.hpp>
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
void RedundantAssignEliminator::run(OptimiserStepContext& _context, Block& _ast)
|
|
|
|
{
|
|
|
|
RedundantAssignEliminator rae{_context.dialect};
|
|
|
|
rae(_ast);
|
|
|
|
|
|
|
|
AssignmentRemover remover{rae.m_pendingRemovals};
|
|
|
|
remover(_ast);
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
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);
|
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
TrackedAssignments skipBranch{m_assignments};
|
|
|
|
(*this)(_if.body);
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
merge(m_assignments, move(skipBranch));
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
visit(*_switch.expression);
|
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
TrackedAssignments const preState{m_assignments};
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
bool hasDefault = false;
|
2019-03-18 16:38:25 +00:00
|
|
|
vector<TrackedAssignments> branches;
|
2018-10-23 13:55:48 +00:00
|
|
|
for (auto const& c: _switch.cases)
|
|
|
|
{
|
|
|
|
if (!c.value)
|
|
|
|
hasDefault = true;
|
2019-03-18 16:38:25 +00:00
|
|
|
(*this)(c.body);
|
|
|
|
branches.emplace_back(move(m_assignments));
|
|
|
|
m_assignments = preState;
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasDefault)
|
|
|
|
{
|
2019-03-18 16:38:25 +00:00
|
|
|
m_assignments = move(branches.back());
|
2018-10-23 13:55:48 +00:00
|
|
|
branches.pop_back();
|
|
|
|
}
|
|
|
|
for (auto& branch: branches)
|
2019-03-18 16:38:25 +00:00
|
|
|
merge(m_assignments, move(branch));
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
2019-03-18 16:38:25 +00:00
|
|
|
std::set<YulString> outerDeclaredVariables;
|
2019-10-28 14:25:02 +00:00
|
|
|
std::set<YulString> outerReturnVariables;
|
2019-03-18 16:38:25 +00:00
|
|
|
TrackedAssignments outerAssignments;
|
2019-03-26 16:35:26 +00:00
|
|
|
ForLoopInfo forLoopInfo;
|
2019-03-18 16:38:25 +00:00
|
|
|
swap(m_declaredVariables, outerDeclaredVariables);
|
2019-10-28 14:25:02 +00:00
|
|
|
swap(m_returnVariables, outerReturnVariables);
|
2019-03-18 16:38:25 +00:00
|
|
|
swap(m_assignments, outerAssignments);
|
2019-03-26 16:35:26 +00:00
|
|
|
swap(m_forLoopInfo, forLoopInfo);
|
2019-02-21 19:04:20 +00:00
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
for (auto const& retParam: _functionDefinition.returnVariables)
|
|
|
|
m_returnVariables.insert(retParam.name);
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
(*this)(_functionDefinition.body);
|
|
|
|
|
|
|
|
for (auto const& param: _functionDefinition.parameters)
|
2019-03-26 16:35:26 +00:00
|
|
|
finalize(param.name, State::Unused);
|
2018-10-23 13:55:48 +00:00
|
|
|
for (auto const& retParam: _functionDefinition.returnVariables)
|
2019-03-26 16:35:26 +00:00
|
|
|
finalize(retParam.name, State::Used);
|
2019-02-21 19:04:20 +00:00
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
swap(m_declaredVariables, outerDeclaredVariables);
|
2019-10-28 14:25:02 +00:00
|
|
|
swap(m_returnVariables, outerReturnVariables);
|
2019-03-18 16:38:25 +00:00
|
|
|
swap(m_assignments, outerAssignments);
|
2019-03-26 16:35:26 +00:00
|
|
|
swap(m_forLoopInfo, forLoopInfo);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(ForLoop const& _forLoop)
|
|
|
|
{
|
2019-03-26 16:35:26 +00:00
|
|
|
ForLoopInfo outerForLoopInfo;
|
|
|
|
swap(outerForLoopInfo, m_forLoopInfo);
|
2019-04-23 07:49:16 +00:00
|
|
|
++m_forLoopNestingDepth;
|
2019-03-26 16:35:26 +00:00
|
|
|
|
2019-04-05 18:38:23 +00:00
|
|
|
// If the pre block was not empty,
|
|
|
|
// we would have to deal with more complicated scoping rules.
|
|
|
|
assertThrow(_forLoop.pre.statements.empty(), OptimizerException, "");
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
// We just run the loop twice to account for the back edge.
|
2018-10-23 13:55:48 +00:00
|
|
|
// There need not be more runs because we only have three different states.
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
TrackedAssignments zeroRuns{m_assignments};
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
(*this)(_forLoop.body);
|
2019-03-18 16:43:28 +00:00
|
|
|
merge(m_assignments, move(m_forLoopInfo.pendingContinueStmts));
|
|
|
|
m_forLoopInfo.pendingContinueStmts = {};
|
2018-10-23 13:55:48 +00:00
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
if (m_forLoopNestingDepth < 6)
|
|
|
|
{
|
|
|
|
// Do the second run only for small nesting depths to avoid horrible runtime.
|
|
|
|
TrackedAssignments oneRun{m_assignments};
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
(*this)(_forLoop.body);
|
2019-03-18 16:43:28 +00:00
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
merge(m_assignments, move(m_forLoopInfo.pendingContinueStmts));
|
|
|
|
m_forLoopInfo.pendingContinueStmts.clear();
|
|
|
|
(*this)(_forLoop.post);
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
visit(*_forLoop.condition);
|
|
|
|
// Order of merging does not matter because "max" is commutative and associative.
|
|
|
|
merge(m_assignments, move(oneRun));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Shortcut to avoid horrible runtime:
|
|
|
|
// Change all assignments that were newly introduced in the for loop to "used".
|
|
|
|
// We do not have to do that with the "break" or "continue" paths, because
|
|
|
|
// they will be joined later anyway.
|
|
|
|
// TODO parallel traversal might be more efficient here.
|
|
|
|
for (auto& var: m_assignments)
|
|
|
|
for (auto& assignment: var.second)
|
|
|
|
{
|
|
|
|
auto zeroIt = zeroRuns.find(var.first);
|
|
|
|
if (zeroIt != zeroRuns.end() && zeroIt->second.count(assignment.first))
|
|
|
|
continue;
|
|
|
|
assignment.second = State::Value::Used;
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2019-04-23 07:49:16 +00:00
|
|
|
// Order of merging does not matter because "max" is commutative and associative.
|
2019-03-18 16:38:25 +00:00
|
|
|
merge(m_assignments, move(zeroRuns));
|
2019-03-18 16:43:28 +00:00
|
|
|
merge(m_assignments, move(m_forLoopInfo.pendingBreakStmts));
|
2019-03-26 16:35:26 +00:00
|
|
|
m_forLoopInfo.pendingBreakStmts.clear();
|
2019-03-18 16:43:28 +00:00
|
|
|
|
2019-03-26 16:35:26 +00:00
|
|
|
// Restore potential outer for-loop states.
|
2019-03-18 16:43:28 +00:00
|
|
|
swap(m_forLoopInfo, outerForLoopInfo);
|
2019-04-23 07:49:16 +00:00
|
|
|
--m_forLoopNestingDepth;
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
void RedundantAssignEliminator::operator()(Break const&)
|
|
|
|
{
|
2019-03-26 16:35:26 +00:00
|
|
|
m_forLoopInfo.pendingBreakStmts.emplace_back(move(m_assignments));
|
|
|
|
m_assignments.clear();
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(Continue const&)
|
|
|
|
{
|
2019-03-26 16:35:26 +00:00
|
|
|
m_forLoopInfo.pendingContinueStmts.emplace_back(move(m_assignments));
|
|
|
|
m_assignments.clear();
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
void RedundantAssignEliminator::operator()(Leave const&)
|
|
|
|
{
|
|
|
|
for (YulString name: m_returnVariables)
|
|
|
|
changeUndecidedTo(name, State::Used);
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
void RedundantAssignEliminator::operator()(Block const& _block)
|
|
|
|
{
|
2019-03-26 16:35:26 +00:00
|
|
|
set<YulString> outerDeclaredVariables;
|
|
|
|
swap(m_declaredVariables, outerDeclaredVariables);
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
ASTWalker::operator()(_block);
|
2019-03-26 16:35:26 +00:00
|
|
|
|
|
|
|
for (auto const& var: m_declaredVariables)
|
|
|
|
finalize(var, State::Unused);
|
|
|
|
|
|
|
|
swap(m_declaredVariables, outerDeclaredVariables);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 16:38:25 +00:00
|
|
|
void RedundantAssignEliminator::merge(TrackedAssignments& _target, TrackedAssignments&& _other)
|
2018-10-29 14:12:31 +00:00
|
|
|
{
|
2019-03-18 16:38:25 +00:00
|
|
|
joinMap(_target, move(_other), [](
|
2018-10-29 14:12:31 +00:00
|
|
|
map<Assignment const*, State>& _assignmentHere,
|
|
|
|
map<Assignment const*, State>&& _assignmentThere
|
|
|
|
)
|
|
|
|
{
|
2019-03-18 16:38:25 +00:00
|
|
|
return joinMap(_assignmentHere, move(_assignmentThere), State::join);
|
2018-10-29 14:12:31 +00:00
|
|
|
});
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:43:28 +00:00
|
|
|
void RedundantAssignEliminator::merge(TrackedAssignments& _target, vector<TrackedAssignments>&& _source)
|
|
|
|
{
|
|
|
|
for (TrackedAssignments& ts: _source)
|
|
|
|
merge(_target, move(ts));
|
|
|
|
_source.clear();
|
|
|
|
}
|
|
|
|
|
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])
|
2019-03-26 16:35:26 +00:00
|
|
|
if (assignment.second == State::Undecided)
|
2018-10-23 13:55:48 +00:00
|
|
|
assignment.second = _newState;
|
|
|
|
}
|
|
|
|
|
2019-03-26 16:35:26 +00:00
|
|
|
void RedundantAssignEliminator::finalize(YulString _variable, RedundantAssignEliminator::State _finalState)
|
2018-11-03 15:04:42 +00:00
|
|
|
{
|
2019-12-29 13:21:23 +00:00
|
|
|
std::map<Assignment const*, State> assignments;
|
|
|
|
joinMap(assignments, std::move(m_assignments[_variable]), State::join);
|
|
|
|
m_assignments.erase(_variable);
|
2019-03-26 16:35:26 +00:00
|
|
|
|
2019-12-29 13:21:23 +00:00
|
|
|
for (auto& breakAssignments: m_forLoopInfo.pendingBreakStmts)
|
|
|
|
{
|
|
|
|
joinMap(assignments, std::move(breakAssignments[_variable]), State::join);
|
|
|
|
breakAssignments.erase(_variable);
|
|
|
|
}
|
|
|
|
for (auto& continueAssignments: m_forLoopInfo.pendingContinueStmts)
|
|
|
|
{
|
|
|
|
joinMap(assignments, std::move(continueAssignments[_variable]), State::join);
|
|
|
|
continueAssignments.erase(_variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& assignment: assignments)
|
2018-11-03 15:04:42 +00:00
|
|
|
{
|
2019-03-26 16:35:26 +00:00
|
|
|
State const state = assignment.second == State::Undecided ? _finalState : assignment.second;
|
|
|
|
|
2019-05-27 11:42:50 +00:00
|
|
|
if (state == State::Unused && SideEffectsCollector{*m_dialect, *assignment.first->value}.movable())
|
2019-03-18 16:38:25 +00:00
|
|
|
m_pendingRemovals.insert(assignment.first);
|
2018-11-03 15:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:55:48 +00:00
|
|
|
void AssignmentRemover::operator()(Block& _block)
|
|
|
|
{
|
2021-04-24 02:40:41 +00:00
|
|
|
ranges::actions::remove_if(_block.statements, [&](Statement const& _statement) -> bool {
|
2019-11-19 15:42:49 +00:00
|
|
|
return holds_alternative<Assignment>(_statement) && m_toRemove.count(&std::get<Assignment>(_statement));
|
2018-10-23 13:55:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ASTModifier::operator()(_block);
|
|
|
|
}
|