mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #7461 from sifmelcara/licm
[YulOpt] Implement loop-invariant code motion
This commit is contained in:
@@ -110,6 +110,8 @@ add_library(yul
|
||||
optimiser/KnowledgeBase.h
|
||||
optimiser/LoadResolver.cpp
|
||||
optimiser/LoadResolver.h
|
||||
optimiser/LoopInvariantCodeMotion.cpp
|
||||
optimiser/LoopInvariantCodeMotion.h
|
||||
optimiser/MainFunction.cpp
|
||||
optimiser/MainFunction.h
|
||||
optimiser/Metrics.cpp
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#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>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libdevcore/CommonData.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void LoopInvariantCodeMotion::run(OptimiserStepContext& _context, Block& _ast)
|
||||
{
|
||||
map<YulString, SideEffects> functionSideEffects =
|
||||
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast));
|
||||
|
||||
set<YulString> ssaVars = SSAValueTracker::ssaVariables(_ast);
|
||||
LoopInvariantCodeMotion{_context.dialect, ssaVars, functionSideEffects}(_ast);
|
||||
}
|
||||
|
||||
void LoopInvariantCodeMotion::operator()(Block& _block)
|
||||
{
|
||||
iterateReplacing(
|
||||
_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,
|
||||
set<YulString> const& _varsDefinedInCurrentScope
|
||||
) 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;
|
||||
if (!SideEffectsCollector{m_dialect, *_varDecl.value, &m_functionSideEffects}.movable())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
|
||||
{
|
||||
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
|
||||
vector<Statement> replacement;
|
||||
for (Block* block: {&_for.post, &_for.body})
|
||||
{
|
||||
set<YulString> varsDefinedInScope;
|
||||
iterateReplacing(
|
||||
block->statements,
|
||||
[&](Statement& _s) -> optional<vector<Statement>>
|
||||
{
|
||||
if (holds_alternative<VariableDeclaration>(_s))
|
||||
{
|
||||
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s);
|
||||
if (canBePromoted(varDecl, varsDefinedInScope))
|
||||
{
|
||||
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) };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
/**
|
||||
* Loop-invariant code motion.
|
||||
*
|
||||
* This optimization moves movable SSA variable declarations outside the loop.
|
||||
*
|
||||
* Only statements at the top level in a loop's body or post block are considered, i.e variable
|
||||
* declarations inside conditional branches will not be moved out of the loop.
|
||||
*
|
||||
* Requirements:
|
||||
* - The Disambiguator, ForLoopInitRewriter and FunctionHoister must be run upfront.
|
||||
* - Expression splitter and SSA transform should be run upfront to obtain better result.
|
||||
*/
|
||||
|
||||
class LoopInvariantCodeMotion: public ASTModifier
|
||||
{
|
||||
public:
|
||||
static constexpr char const* name{"LoopInvariantCodeMotion"};
|
||||
static void run(OptimiserStepContext& _context, Block& _ast);
|
||||
|
||||
void operator()(Block& _block) override;
|
||||
|
||||
private:
|
||||
explicit LoopInvariantCodeMotion(
|
||||
Dialect const& _dialect,
|
||||
std::set<YulString> const& _ssaVariables,
|
||||
std::map<YulString, SideEffects> const& _functionSideEffects
|
||||
):
|
||||
m_dialect(_dialect),
|
||||
m_ssaVariables(_ssaVariables),
|
||||
m_functionSideEffects(_functionSideEffects)
|
||||
{ }
|
||||
|
||||
/// @returns true if the given variable declaration can be moved to in front of the loop.
|
||||
bool canBePromoted(VariableDeclaration const& _varDecl, std::set<YulString> const& _varsDefinedInCurrentScope) const;
|
||||
std::optional<std::vector<Statement>> rewriteLoop(ForLoop& _for);
|
||||
|
||||
Dialect const& m_dialect;
|
||||
std::set<YulString> const& m_ssaVariables;
|
||||
std::map<YulString, SideEffects> const& m_functionSideEffects;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -49,27 +49,28 @@ void ReferencesCounter::operator()(Identifier const& _identifier)
|
||||
|
||||
void ReferencesCounter::operator()(FunctionCall const& _funCall)
|
||||
{
|
||||
++m_references[_funCall.functionName.name];
|
||||
if (m_countWhat == VariablesAndFunctions)
|
||||
++m_references[_funCall.functionName.name];
|
||||
ASTWalker::operator()(_funCall);
|
||||
}
|
||||
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(Block const& _block)
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(Block const& _block, CountWhat _countWhat)
|
||||
{
|
||||
ReferencesCounter counter;
|
||||
ReferencesCounter counter(_countWhat);
|
||||
counter(_block);
|
||||
return counter.references();
|
||||
}
|
||||
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(FunctionDefinition const& _function)
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(FunctionDefinition const& _function, CountWhat _countWhat)
|
||||
{
|
||||
ReferencesCounter counter;
|
||||
ReferencesCounter counter(_countWhat);
|
||||
counter(_function);
|
||||
return counter.references();
|
||||
}
|
||||
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(Expression const& _expression)
|
||||
map<YulString, size_t> ReferencesCounter::countReferences(Expression const& _expression, CountWhat _countWhat)
|
||||
{
|
||||
ReferencesCounter counter;
|
||||
ReferencesCounter counter(_countWhat);
|
||||
counter.visit(_expression);
|
||||
return counter.references();
|
||||
}
|
||||
|
||||
@@ -54,16 +54,23 @@ private:
|
||||
class ReferencesCounter: public ASTWalker
|
||||
{
|
||||
public:
|
||||
enum CountWhat { VariablesAndFunctions, OnlyVariables };
|
||||
|
||||
explicit ReferencesCounter(CountWhat _countWhat = VariablesAndFunctions):
|
||||
m_countWhat(_countWhat)
|
||||
{}
|
||||
|
||||
using ASTWalker::operator ();
|
||||
virtual void operator()(Identifier const& _identifier);
|
||||
virtual void operator()(FunctionCall const& _funCall);
|
||||
|
||||
static std::map<YulString, size_t> countReferences(Block const& _block);
|
||||
static std::map<YulString, size_t> countReferences(FunctionDefinition const& _function);
|
||||
static std::map<YulString, size_t> countReferences(Expression const& _expression);
|
||||
static std::map<YulString, size_t> countReferences(Block const& _block, CountWhat _countWhat = VariablesAndFunctions);
|
||||
static std::map<YulString, size_t> countReferences(FunctionDefinition const& _function, CountWhat _countWhat = VariablesAndFunctions);
|
||||
static std::map<YulString, size_t> countReferences(Expression const& _expression, CountWhat _countWhat = VariablesAndFunctions);
|
||||
|
||||
std::map<YulString, size_t> const& references() const { return m_references; }
|
||||
private:
|
||||
CountWhat m_countWhat = CountWhat::VariablesAndFunctions;
|
||||
std::map<YulString, size_t> m_references;
|
||||
};
|
||||
|
||||
|
||||
@@ -49,6 +49,16 @@ void SSAValueTracker::operator()(VariableDeclaration const& _varDecl)
|
||||
setValue(_varDecl.variables.front().name, _varDecl.value.get());
|
||||
}
|
||||
|
||||
set<YulString> SSAValueTracker::ssaVariables(Block const& _ast)
|
||||
{
|
||||
SSAValueTracker t;
|
||||
t(_ast);
|
||||
set<YulString> ssaVars;
|
||||
for (auto const& value: t.values())
|
||||
ssaVars.insert(value.first);
|
||||
return ssaVars;
|
||||
}
|
||||
|
||||
void SSAValueTracker::setValue(YulString _name, Expression const* _value)
|
||||
{
|
||||
assertThrow(
|
||||
|
||||
@@ -49,6 +49,8 @@ public:
|
||||
std::map<YulString, Expression const*> const& values() const { return m_values; }
|
||||
Expression const* value(YulString _name) const { return m_values.at(_name); }
|
||||
|
||||
static std::set<YulString> ssaVariables(Block const& _ast);
|
||||
|
||||
private:
|
||||
void setValue(YulString _name, Expression const* _value);
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
||||
#include <libyul/optimiser/VarNameCleaner.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
#include <libyul/backends/evm/ConstantOptimiser.h>
|
||||
#include <libyul/AsmAnalysis.h>
|
||||
@@ -129,7 +130,8 @@ void OptimiserSuite::run(
|
||||
RedundantAssignEliminator::name,
|
||||
ExpressionSimplifier::name,
|
||||
CommonSubexpressionEliminator::name,
|
||||
LoadResolver::name
|
||||
LoadResolver::name,
|
||||
LoopInvariantCodeMotion::name
|
||||
}, ast);
|
||||
}
|
||||
|
||||
@@ -357,6 +359,7 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
||||
FunctionHoister,
|
||||
LiteralRematerialiser,
|
||||
LoadResolver,
|
||||
LoopInvariantCodeMotion,
|
||||
RedundantAssignEliminator,
|
||||
Rematerialiser,
|
||||
SSAReverser,
|
||||
|
||||
Reference in New Issue
Block a user