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.
|
|
|
|
*/
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
#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>
|
2022-11-23 14:28:11 +00:00
|
|
|
#include <libyul/ControlFlowSideEffectsCollector.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2022-11-23 14:28:11 +00:00
|
|
|
#include <libyul/AsmPrinter.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
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
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
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::run(OptimiserStepContext& _context, Block& _ast)
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +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
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::operator()(Identifier const& _identifier)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
markUsed(_identifier.name);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::operator()(Assignment const& _assignment)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
|
|
|
visit(*_assignment.value);
|
2022-11-23 14:28:11 +00:00
|
|
|
// Do not visit the variables because they are Identifiers
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
|
2021-11-23 09:37:12 +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-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-11-23 09:37:12 +00:00
|
|
|
UnusedStoreBase::operator()(_functionDefinition);
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::operator()(Leave const&)
|
2019-10-28 14:25:02 +00:00
|
|
|
{
|
|
|
|
for (YulString name: m_returnVariables)
|
2022-11-23 14:28:11 +00:00
|
|
|
markUsed(name);
|
|
|
|
m_activeStores.clear();
|
2019-10-28 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::operator()(Block const& _block)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2021-11-23 09:37:12 +00:00
|
|
|
UnusedStoreBase::operator()(_block);
|
2019-03-26 16:35:26 +00:00
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
for (auto const& statement: _block.statements)
|
|
|
|
if (auto const* varDecl = get_if<VariableDeclaration>(&statement))
|
|
|
|
for (auto const& var: varDecl->variables)
|
|
|
|
m_activeStores.erase(var.name);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::visit(Statement const& _statement)
|
2018-10-23 13:55:48 +00:00
|
|
|
{
|
2021-11-23 09:37:12 +00:00
|
|
|
UnusedStoreBase::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))
|
2022-11-23 14:28:11 +00:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
2018-10-29 14:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
void UnusedAssignEliminator::shortcutNestedLoop(ActiveStores 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.
|
2022-11-23 14:28:11 +00:00
|
|
|
|
|
|
|
for (auto& [variable, stores]: m_activeStores)
|
|
|
|
{
|
|
|
|
auto zeroIt = _zeroRuns.find(variable);
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto& assignment: stores)
|
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
if (zeroIt != _zeroRuns.end() && zeroIt->second.count(assignment))
|
2021-08-11 15:40:42 +00:00
|
|
|
continue;
|
2022-11-23 14:28:11 +00:00
|
|
|
m_usedStores.insert(assignment);
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
2022-11-23 14:28:11 +00:00
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedAssignEliminator::finalizeFunctionDefinition(FunctionDefinition const& _functionDefinition)
|
2019-03-18 16:43:28 +00:00
|
|
|
{
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto const& retParam: _functionDefinition.returnVariables)
|
2022-11-23 14:28:11 +00:00
|
|
|
markUsed(retParam.name);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
void UnusedAssignEliminator::markUsed(YulString _variable)
|
2018-11-03 15:04:42 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
for (auto& assignment: m_activeStores[_variable])
|
|
|
|
m_usedStores.insert(assignment);
|
|
|
|
m_activeStores.erase(_variable);
|
2018-10-23 13:55:48 +00:00
|
|
|
}
|