2019-09-18 08:47:49 +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
|
2019-09-18 08:47:49 +00:00
|
|
|
|
|
|
|
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
|
|
|
|
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
#include <libyul/optimiser/SSAValueTracker.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2019-09-18 08:47:49 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2019-09-18 08:47:49 +00:00
|
|
|
|
|
|
|
void LoopInvariantCodeMotion::run(OptimiserStepContext& _context, Block& _ast)
|
|
|
|
{
|
|
|
|
map<YulString, SideEffects> functionSideEffects =
|
|
|
|
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast));
|
2020-07-03 13:10:29 +00:00
|
|
|
bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _ast);
|
2019-09-18 08:47:49 +00:00
|
|
|
set<YulString> ssaVars = SSAValueTracker::ssaVariables(_ast);
|
2020-07-03 13:10:29 +00:00
|
|
|
LoopInvariantCodeMotion{_context.dialect, ssaVars, functionSideEffects, containsMSize}(_ast);
|
2019-09-18 08:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInvariantCodeMotion::operator()(Block& _block)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
util::iterateReplacing(
|
2019-09-18 08:47:49 +00:00
|
|
|
_block.statements,
|
|
|
|
[&](Statement& _s) -> optional<vector<Statement>>
|
|
|
|
{
|
|
|
|
visit(_s);
|
|
|
|
if (holds_alternative<ForLoop>(_s))
|
|
|
|
return rewriteLoop(get<ForLoop>(_s));
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopInvariantCodeMotion::canBePromoted(
|
|
|
|
VariableDeclaration const& _varDecl,
|
2020-07-03 13:10:29 +00:00
|
|
|
set<YulString> const& _varsDefinedInCurrentScope,
|
|
|
|
SideEffects const& _forLoopSideEffects
|
2019-09-18 08:47:49 +00:00
|
|
|
) const
|
|
|
|
{
|
|
|
|
// A declaration can be promoted iff
|
|
|
|
// 1. Its LHS is a SSA variable
|
|
|
|
// 2. Its RHS only references SSA variables declared outside of the current scope
|
|
|
|
// 3. Its RHS is movable
|
|
|
|
|
|
|
|
for (auto const& var: _varDecl.variables)
|
|
|
|
if (!m_ssaVariables.count(var.name))
|
|
|
|
return false;
|
|
|
|
if (_varDecl.value)
|
|
|
|
{
|
|
|
|
for (auto const& ref: ReferencesCounter::countReferences(*_varDecl.value, ReferencesCounter::OnlyVariables))
|
|
|
|
if (_varsDefinedInCurrentScope.count(ref.first) || !m_ssaVariables.count(ref.first))
|
|
|
|
return false;
|
2020-07-03 13:10:29 +00:00
|
|
|
SideEffectsCollector sideEffects{m_dialect, *_varDecl.value, &m_functionSideEffects};
|
|
|
|
if (!sideEffects.movableRelativeTo(_forLoopSideEffects, m_containsMSize))
|
2019-09-18 08:47:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
|
|
|
|
{
|
|
|
|
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
|
2020-07-03 13:10:29 +00:00
|
|
|
|
|
|
|
auto forLoopSideEffects =
|
|
|
|
SideEffectsCollector{m_dialect, _for, &m_functionSideEffects}.sideEffects();
|
|
|
|
|
2019-09-18 08:47:49 +00:00
|
|
|
vector<Statement> replacement;
|
|
|
|
for (Block* block: {&_for.post, &_for.body})
|
|
|
|
{
|
|
|
|
set<YulString> varsDefinedInScope;
|
2019-12-11 16:31:36 +00:00
|
|
|
util::iterateReplacing(
|
2019-09-18 08:47:49 +00:00
|
|
|
block->statements,
|
|
|
|
[&](Statement& _s) -> optional<vector<Statement>>
|
|
|
|
{
|
|
|
|
if (holds_alternative<VariableDeclaration>(_s))
|
|
|
|
{
|
|
|
|
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s);
|
2020-07-03 13:10:29 +00:00
|
|
|
if (canBePromoted(varDecl, varsDefinedInScope, forLoopSideEffects))
|
2019-09-18 08:47:49 +00:00
|
|
|
{
|
|
|
|
replacement.emplace_back(std::move(_s));
|
|
|
|
// Do not add the variables declared here to varsDefinedInScope because we are moving them.
|
|
|
|
return vector<Statement>{};
|
|
|
|
}
|
|
|
|
for (auto const& var: varDecl.variables)
|
|
|
|
varsDefinedInScope.insert(var.name);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (replacement.empty())
|
|
|
|
return {};
|
|
|
|
else
|
|
|
|
{
|
|
|
|
replacement.emplace_back(std::move(_for));
|
|
|
|
return { std::move(replacement) };
|
|
|
|
}
|
|
|
|
}
|