2021-08-11 15:40:42 +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
|
|
|
|
/**
|
2021-11-23 09:37:12 +00:00
|
|
|
* Base class for both UnusedAssignEliminator and UnusedStoreEliminator.
|
2021-08-11 15:40:42 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
#include <libyul/optimiser/UnusedStoreBase.h>
|
2021-08-11 15:40:42 +00:00
|
|
|
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
#include <libyul/optimiser/OptimiserStep.h>
|
|
|
|
#include <libyul/AST.h>
|
|
|
|
|
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
|
|
|
#include <range/v3/action/remove_if.hpp>
|
|
|
|
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(If const& _if)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
|
|
|
visit(*_if.condition);
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
ActiveStores skipBranch{m_activeStores};
|
2021-08-11 15:40:42 +00:00
|
|
|
(*this)(_if.body);
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(skipBranch));
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(Switch const& _switch)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
|
|
|
visit(*_switch.expression);
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
ActiveStores const preState{m_activeStores};
|
2021-08-11 15:40:42 +00:00
|
|
|
|
|
|
|
bool hasDefault = false;
|
2023-08-23 02:03:44 +00:00
|
|
|
std::vector<ActiveStores> branches;
|
2021-08-11 15:40:42 +00:00
|
|
|
for (auto const& c: _switch.cases)
|
|
|
|
{
|
|
|
|
if (!c.value)
|
|
|
|
hasDefault = true;
|
|
|
|
(*this)(c.body);
|
2022-11-23 14:28:11 +00:00
|
|
|
branches.emplace_back(std::move(m_activeStores));
|
|
|
|
m_activeStores = preState;
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasDefault)
|
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
m_activeStores = std::move(branches.back());
|
2021-08-11 15:40:42 +00:00
|
|
|
branches.pop_back();
|
|
|
|
}
|
|
|
|
for (auto& branch: branches)
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(branch));
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(FunctionDefinition const& _functionDefinition)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
ScopedSaveAndRestore allStores(m_allStores, {});
|
|
|
|
ScopedSaveAndRestore usedStores(m_usedStores, {});
|
|
|
|
ScopedSaveAndRestore outerAssignments(m_activeStores, {});
|
2021-08-11 15:40:42 +00:00
|
|
|
ScopedSaveAndRestore forLoopInfo(m_forLoopInfo, {});
|
2022-03-02 12:49:40 +00:00
|
|
|
ScopedSaveAndRestore forLoopNestingDepth(m_forLoopNestingDepth, 0);
|
2021-08-11 15:40:42 +00:00
|
|
|
|
|
|
|
(*this)(_functionDefinition.body);
|
|
|
|
|
|
|
|
finalizeFunctionDefinition(_functionDefinition);
|
2022-11-23 14:28:11 +00:00
|
|
|
m_storesToRemove += m_allStores - m_usedStores;
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(ForLoop const& _forLoop)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
|
|
|
ScopedSaveAndRestore outerForLoopInfo(m_forLoopInfo, {});
|
|
|
|
ScopedSaveAndRestore forLoopNestingDepth(m_forLoopNestingDepth, m_forLoopNestingDepth + 1);
|
|
|
|
|
|
|
|
// If the pre block was not empty,
|
|
|
|
// we would have to deal with more complicated scoping rules.
|
|
|
|
assertThrow(_forLoop.pre.statements.empty(), OptimizerException, "");
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
ActiveStores zeroRuns{m_activeStores};
|
2021-08-11 15:40:42 +00:00
|
|
|
|
|
|
|
(*this)(_forLoop.body);
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(m_forLoopInfo.pendingContinueStmts));
|
2021-08-11 15:40:42 +00:00
|
|
|
m_forLoopInfo.pendingContinueStmts = {};
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
|
|
|
|
if (m_forLoopNestingDepth < 6)
|
|
|
|
{
|
|
|
|
// Do the second run only for small nesting depths to avoid horrible runtime.
|
2022-11-23 14:28:11 +00:00
|
|
|
ActiveStores oneRun{m_activeStores};
|
2021-08-11 15:40:42 +00:00
|
|
|
|
|
|
|
(*this)(_forLoop.body);
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(m_forLoopInfo.pendingContinueStmts));
|
2021-08-11 15:40:42 +00:00
|
|
|
m_forLoopInfo.pendingContinueStmts.clear();
|
|
|
|
(*this)(_forLoop.post);
|
|
|
|
|
|
|
|
visit(*_forLoop.condition);
|
|
|
|
// Order of merging does not matter because "max" is commutative and associative.
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(oneRun));
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
// Shortcut to avoid horrible runtime.
|
|
|
|
shortcutNestedLoop(zeroRuns);
|
|
|
|
|
|
|
|
// Order of merging does not matter because "max" is commutative and associative.
|
2022-11-23 14:28:11 +00:00
|
|
|
merge(m_activeStores, std::move(zeroRuns));
|
|
|
|
merge(m_activeStores, std::move(m_forLoopInfo.pendingBreakStmts));
|
2021-08-11 15:40:42 +00:00
|
|
|
m_forLoopInfo.pendingBreakStmts.clear();
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(Break const&)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
m_forLoopInfo.pendingBreakStmts.emplace_back(std::move(m_activeStores));
|
|
|
|
m_activeStores.clear();
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:37:12 +00:00
|
|
|
void UnusedStoreBase::operator()(Continue const&)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
m_forLoopInfo.pendingContinueStmts.emplace_back(std::move(m_activeStores));
|
|
|
|
m_activeStores.clear();
|
2021-08-11 15:40:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 14:28:11 +00:00
|
|
|
void UnusedStoreBase::merge(ActiveStores& _target, ActiveStores&& _other)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
util::joinMap(_target, std::move(_other), [](
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<Statement const*>& _storesHere,
|
|
|
|
std::set<Statement const*>&& _storesThere
|
2021-08-11 15:40:42 +00:00
|
|
|
)
|
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
_storesHere += _storesThere;
|
2021-08-11 15:40:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void UnusedStoreBase::merge(ActiveStores& _target, std::vector<ActiveStores>&& _source)
|
2021-08-11 15:40:42 +00:00
|
|
|
{
|
2022-11-23 14:28:11 +00:00
|
|
|
for (ActiveStores& ts: _source)
|
2022-08-23 17:28:45 +00:00
|
|
|
merge(_target, std::move(ts));
|
2021-08-11 15:40:42 +00:00
|
|
|
_source.clear();
|
|
|
|
}
|