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);
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
StatementRemover remover{rae.m_pendingRemovals};
|
2019-09-23 14:32:50 +00:00
|
|
|
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)
|
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
RedundantStoreBase::operator()(_variableDeclaration);
|
2018-10-23 13:55:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedundantAssignEliminator::operator()(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
ScopedSaveAndRestore outerDeclaredVariables(m_declaredVariables, {});
|
|
|
|
ScopedSaveAndRestore outerReturnVariables(m_returnVariables, {});
|
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);
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
RedundantStoreBase::operator()(_functionDefinition);
|
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)
|
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
ScopedSaveAndRestore outerDeclaredVariables(m_declaredVariables, {});
|
2018-10-23 13:55:48 +00:00
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
RedundantStoreBase::operator()(_block);
|
2019-03-26 16:35:26 +00:00
|
|
|
|
|
|
|
for (auto const& var: m_declaredVariables)
|
|
|
|
finalize(var, State::Unused);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
void RedundantAssignEliminator::visit(Statement const& _statement)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
RedundantStoreBase::visit(_statement);
|
2018-10-29 14:12:31 +00:00
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
if (auto const* assignment = get_if<Assignment>(&_statement))
|
|
|
|
if (assignment->variableNames.size() == 1)
|
|
|
|
// Default-construct it in "Undecided" state if it does not yet exist.
|
|
|
|
m_stores[assignment->variableNames.front().name][&_statement];
|
2018-10-29 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
void RedundantAssignEliminator::shortcutNestedLoop(TrackedStores const& _zeroRuns)
|
2018-10-29 14:12:31 +00:00
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
// 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& [variable, stores]: m_stores)
|
|
|
|
for (auto& assignment: stores)
|
|
|
|
{
|
|
|
|
auto zeroIt = _zeroRuns.find(variable);
|
|
|
|
if (zeroIt != _zeroRuns.end() && zeroIt->second.count(assignment.first))
|
|
|
|
continue;
|
|
|
|
assignment.second = State::Value::Used;
|
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
void RedundantAssignEliminator::finalizeFunctionDefinition(FunctionDefinition const& _functionDefinition)
|
2019-03-18 16:43:28 +00:00
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto const& param: _functionDefinition.parameters)
|
|
|
|
finalize(param.name, State::Unused);
|
|
|
|
for (auto const& retParam: _functionDefinition.returnVariables)
|
|
|
|
finalize(retParam.name, State::Used);
|
2019-03-18 16:43:28 +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
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto& assignment: m_stores[_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
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
std::map<Statement const*, State> stores = std::move(m_stores[_variable]);
|
|
|
|
m_stores.erase(_variable);
|
2019-03-26 16:35:26 +00:00
|
|
|
|
2019-12-29 13:21:23 +00:00
|
|
|
for (auto& breakAssignments: m_forLoopInfo.pendingBreakStmts)
|
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
util::joinMap(stores, std::move(breakAssignments[_variable]), State::join);
|
2019-12-29 13:21:23 +00:00
|
|
|
breakAssignments.erase(_variable);
|
|
|
|
}
|
|
|
|
for (auto& continueAssignments: m_forLoopInfo.pendingContinueStmts)
|
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
util::joinMap(stores, std::move(continueAssignments[_variable]), State::join);
|
2019-12-29 13:21:23 +00:00
|
|
|
continueAssignments.erase(_variable);
|
|
|
|
}
|
|
|
|
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto&& [statement, state]: stores)
|
|
|
|
if (
|
|
|
|
(state == State::Unused || (state == State::Undecided && _finalState == State::Unused)) &&
|
|
|
|
SideEffectsCollector{m_dialect, *std::get<Assignment>(*statement).value}.movable()
|
|
|
|
)
|
|
|
|
m_pendingRemovals.insert(statement);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|