solidity/libyul/optimiser/UnusedAssignEliminator.cpp

161 lines
4.7 KiB
C++
Raw Permalink Normal View History

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/>.
*/
// 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/UnusedAssignEliminator.h>
2018-10-23 13:55:48 +00:00
#include <libyul/optimiser/Semantics.h>
2021-11-11 14:54:38 +00:00
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/ControlFlowSideEffectsCollector.h>
#include <libyul/AST.h>
#include <libyul/AsmPrinter.h>
2018-10-23 13:55:48 +00:00
#include <libsolutil/CommonData.h>
2018-10-23 13:55:48 +00:00
#include <range/v3/action/remove_if.hpp>
2018-10-23 13:55:48 +00:00
#include <iostream>
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::yul;
2018-10-23 13:55:48 +00:00
void UnusedAssignEliminator::run(OptimiserStepContext& _context, Block& _ast)
2019-09-23 14:32:50 +00:00
{
UnusedAssignEliminator uae{
_context.dialect,
ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed()
};
uae(_ast);
uae.m_storesToRemove += uae.m_allStores - uae.m_usedStores;
2019-09-23 14:32:50 +00:00
std::set<Statement const*> toRemove{uae.m_storesToRemove.begin(), uae.m_storesToRemove.end()};
StatementRemover remover{toRemove};
2019-09-23 14:32:50 +00:00
remover(_ast);
}
void UnusedAssignEliminator::operator()(Identifier const& _identifier)
2018-10-23 13:55:48 +00:00
{
markUsed(_identifier.name);
2018-10-23 13:55:48 +00:00
}
void UnusedAssignEliminator::operator()(Assignment const& _assignment)
2018-10-23 13:55:48 +00:00
{
visit(*_assignment.value);
// Do not visit the variables because they are Identifiers
2018-10-23 13:55:48 +00:00
}
void UnusedAssignEliminator::operator()(FunctionDefinition const& _functionDefinition)
2018-10-23 13:55:48 +00:00
{
2021-08-11 15:40:42 +00:00
ScopedSaveAndRestore outerReturnVariables(m_returnVariables, {});
2019-10-28 14:25:02 +00:00
for (auto const& retParam: _functionDefinition.returnVariables)
m_returnVariables.insert(retParam.name);
UnusedStoreBase::operator()(_functionDefinition);
}
void UnusedAssignEliminator::operator()(FunctionCall const& _functionCall)
{
UnusedStoreBase::operator()(_functionCall);
ControlFlowSideEffects sideEffects;
if (auto builtin = m_dialect.builtin(_functionCall.functionName.name))
sideEffects = builtin->controlFlowSideEffects;
else
sideEffects = m_controlFlowSideEffects.at(_functionCall.functionName.name);
if (!sideEffects.canContinue)
// We do not return from the current function, so it is OK to also
// clear the return variables.
m_activeStores.clear();
}
void UnusedAssignEliminator::operator()(Leave const&)
2019-10-28 14:25:02 +00:00
{
for (YulString name: m_returnVariables)
markUsed(name);
m_activeStores.clear();
2019-10-28 14:25:02 +00:00
}
void UnusedAssignEliminator::operator()(Block const& _block)
2018-10-23 13:55:48 +00:00
{
UnusedStoreBase::operator()(_block);
for (auto const& statement: _block.statements)
if (auto const* varDecl = std::get_if<VariableDeclaration>(&statement))
for (auto const& var: varDecl->variables)
m_activeStores.erase(var.name);
2018-10-23 13:55:48 +00:00
}
void UnusedAssignEliminator::visit(Statement const& _statement)
2018-10-23 13:55:48 +00:00
{
UnusedStoreBase::visit(_statement);
if (auto const* assignment = std::get_if<Assignment>(&_statement))
{
// We do not remove assignments whose values might have side-effects,
// but clear the active stores to the assigned variables in any case.
if (SideEffectsCollector{m_dialect, *assignment->value}.movable())
{
m_allStores.insert(&_statement);
for (auto const& var: assignment->variableNames)
m_activeStores[var.name] = {&_statement};
}
else
for (auto const& var: assignment->variableNames)
m_activeStores[var.name].clear();
}
}
void UnusedAssignEliminator::shortcutNestedLoop(ActiveStores const& _zeroRuns)
{
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.
for (auto& [variable, stores]: m_activeStores)
{
auto zeroIt = _zeroRuns.find(variable);
2021-08-11 15:40:42 +00:00
for (auto& assignment: stores)
{
if (zeroIt != _zeroRuns.end() && zeroIt->second.count(assignment))
2021-08-11 15:40:42 +00:00
continue;
m_usedStores.insert(assignment);
2021-08-11 15:40:42 +00:00
}
}
2018-10-23 13:55:48 +00:00
}
void UnusedAssignEliminator::finalizeFunctionDefinition(FunctionDefinition const& _functionDefinition)
{
2021-08-11 15:40:42 +00:00
for (auto const& retParam: _functionDefinition.returnVariables)
markUsed(retParam.name);
2018-10-23 13:55:48 +00:00
}
void UnusedAssignEliminator::markUsed(YulString _variable)
{
for (auto& assignment: m_activeStores[_variable])
m_usedStores.insert(assignment);
m_activeStores.erase(_variable);
2018-10-23 13:55:48 +00:00
}