mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Extended LoopInvariantCodeMotion for storage and state
This commit is contained in:
parent
3cbe65e4f3
commit
5c6e7f03b4
@ -35,9 +35,9 @@ void LoopInvariantCodeMotion::run(OptimiserStepContext& _context, Block& _ast)
|
|||||||
{
|
{
|
||||||
map<YulString, SideEffects> functionSideEffects =
|
map<YulString, SideEffects> functionSideEffects =
|
||||||
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast));
|
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast));
|
||||||
|
bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _ast);
|
||||||
set<YulString> ssaVars = SSAValueTracker::ssaVariables(_ast);
|
set<YulString> ssaVars = SSAValueTracker::ssaVariables(_ast);
|
||||||
LoopInvariantCodeMotion{_context.dialect, ssaVars, functionSideEffects}(_ast);
|
LoopInvariantCodeMotion{_context.dialect, ssaVars, functionSideEffects, containsMSize}(_ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoopInvariantCodeMotion::operator()(Block& _block)
|
void LoopInvariantCodeMotion::operator()(Block& _block)
|
||||||
@ -57,7 +57,8 @@ void LoopInvariantCodeMotion::operator()(Block& _block)
|
|||||||
|
|
||||||
bool LoopInvariantCodeMotion::canBePromoted(
|
bool LoopInvariantCodeMotion::canBePromoted(
|
||||||
VariableDeclaration const& _varDecl,
|
VariableDeclaration const& _varDecl,
|
||||||
set<YulString> const& _varsDefinedInCurrentScope
|
set<YulString> const& _varsDefinedInCurrentScope,
|
||||||
|
SideEffects const& _forLoopSideEffects
|
||||||
) const
|
) const
|
||||||
{
|
{
|
||||||
// A declaration can be promoted iff
|
// A declaration can be promoted iff
|
||||||
@ -73,7 +74,8 @@ bool LoopInvariantCodeMotion::canBePromoted(
|
|||||||
for (auto const& ref: ReferencesCounter::countReferences(*_varDecl.value, ReferencesCounter::OnlyVariables))
|
for (auto const& ref: ReferencesCounter::countReferences(*_varDecl.value, ReferencesCounter::OnlyVariables))
|
||||||
if (_varsDefinedInCurrentScope.count(ref.first) || !m_ssaVariables.count(ref.first))
|
if (_varsDefinedInCurrentScope.count(ref.first) || !m_ssaVariables.count(ref.first))
|
||||||
return false;
|
return false;
|
||||||
if (!SideEffectsCollector{m_dialect, *_varDecl.value, &m_functionSideEffects}.movable())
|
SideEffectsCollector sideEffects{m_dialect, *_varDecl.value, &m_functionSideEffects};
|
||||||
|
if (!sideEffects.movableRelativeTo(_forLoopSideEffects, m_containsMSize))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -82,6 +84,10 @@ bool LoopInvariantCodeMotion::canBePromoted(
|
|||||||
optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
|
optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
|
||||||
{
|
{
|
||||||
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
|
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
|
||||||
|
|
||||||
|
auto forLoopSideEffects =
|
||||||
|
SideEffectsCollector{m_dialect, _for, &m_functionSideEffects}.sideEffects();
|
||||||
|
|
||||||
vector<Statement> replacement;
|
vector<Statement> replacement;
|
||||||
for (Block* block: {&_for.post, &_for.body})
|
for (Block* block: {&_for.post, &_for.body})
|
||||||
{
|
{
|
||||||
@ -93,7 +99,7 @@ optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
|
|||||||
if (holds_alternative<VariableDeclaration>(_s))
|
if (holds_alternative<VariableDeclaration>(_s))
|
||||||
{
|
{
|
||||||
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s);
|
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s);
|
||||||
if (canBePromoted(varDecl, varsDefinedInScope))
|
if (canBePromoted(varDecl, varsDefinedInScope, forLoopSideEffects))
|
||||||
{
|
{
|
||||||
replacement.emplace_back(std::move(_s));
|
replacement.emplace_back(std::move(_s));
|
||||||
// Do not add the variables declared here to varsDefinedInScope because we are moving them.
|
// Do not add the variables declared here to varsDefinedInScope because we are moving them.
|
||||||
|
@ -49,17 +49,24 @@ private:
|
|||||||
explicit LoopInvariantCodeMotion(
|
explicit LoopInvariantCodeMotion(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
std::set<YulString> const& _ssaVariables,
|
std::set<YulString> const& _ssaVariables,
|
||||||
std::map<YulString, SideEffects> const& _functionSideEffects
|
std::map<YulString, SideEffects> const& _functionSideEffects,
|
||||||
|
bool _containsMSize
|
||||||
):
|
):
|
||||||
|
m_containsMSize(_containsMSize),
|
||||||
m_dialect(_dialect),
|
m_dialect(_dialect),
|
||||||
m_ssaVariables(_ssaVariables),
|
m_ssaVariables(_ssaVariables),
|
||||||
m_functionSideEffects(_functionSideEffects)
|
m_functionSideEffects(_functionSideEffects)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
/// @returns true if the given variable declaration can be moved to in front of the loop.
|
/// @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;
|
bool canBePromoted(
|
||||||
|
VariableDeclaration const& _varDecl,
|
||||||
|
std::set<YulString> const& _varsDefinedInCurrentScope,
|
||||||
|
SideEffects const& _forLoopSideEffects
|
||||||
|
) const;
|
||||||
std::optional<std::vector<Statement>> rewriteLoop(ForLoop& _for);
|
std::optional<std::vector<Statement>> rewriteLoop(ForLoop& _for);
|
||||||
|
|
||||||
|
bool m_containsMSize = true;
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
std::set<YulString> const& m_ssaVariables;
|
std::set<YulString> const& m_ssaVariables;
|
||||||
std::map<YulString, SideEffects> const& m_functionSideEffects;
|
std::map<YulString, SideEffects> const& m_functionSideEffects;
|
||||||
|
@ -62,6 +62,16 @@ SideEffectsCollector::SideEffectsCollector(
|
|||||||
operator()(_ast);
|
operator()(_ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SideEffectsCollector::SideEffectsCollector(
|
||||||
|
Dialect const& _dialect,
|
||||||
|
ForLoop const& _ast,
|
||||||
|
map<YulString, SideEffects> const* _functionSideEffects
|
||||||
|
):
|
||||||
|
SideEffectsCollector(_dialect, _functionSideEffects)
|
||||||
|
{
|
||||||
|
operator()(_ast);
|
||||||
|
}
|
||||||
|
|
||||||
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||||
{
|
{
|
||||||
ASTWalker::operator()(_functionCall);
|
ASTWalker::operator()(_functionCall);
|
||||||
|
@ -54,11 +54,48 @@ public:
|
|||||||
Block const& _ast,
|
Block const& _ast,
|
||||||
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
||||||
);
|
);
|
||||||
|
SideEffectsCollector(
|
||||||
|
Dialect const& _dialect,
|
||||||
|
ForLoop const& _ast,
|
||||||
|
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
||||||
|
);
|
||||||
|
|
||||||
using ASTWalker::operator();
|
using ASTWalker::operator();
|
||||||
void operator()(FunctionCall const& _functionCall) override;
|
void operator()(FunctionCall const& _functionCall) override;
|
||||||
|
|
||||||
bool movable() const { return m_sideEffects.movable; }
|
bool movable() const { return m_sideEffects.movable; }
|
||||||
|
|
||||||
|
bool movableRelativeTo(SideEffects const& _other, bool _codeContainsMSize)
|
||||||
|
{
|
||||||
|
if (!m_sideEffects.cannotLoop)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (m_sideEffects.movable)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!m_sideEffects.movableApartFromEffects ||
|
||||||
|
m_sideEffects.storage == SideEffects::Write ||
|
||||||
|
m_sideEffects.otherState == SideEffects::Write ||
|
||||||
|
m_sideEffects.memory == SideEffects::Write
|
||||||
|
)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (m_sideEffects.otherState == SideEffects::Read)
|
||||||
|
if (_other.otherState == SideEffects::Write)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (m_sideEffects.storage == SideEffects::Read)
|
||||||
|
if (_other.storage == SideEffects::Write)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (m_sideEffects.memory == SideEffects::Read)
|
||||||
|
if (_codeContainsMSize || _other.memory == SideEffects::Write)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool canBeRemoved(bool _allowMSizeModification = false) const
|
bool canBeRemoved(bool _allowMSizeModification = false) const
|
||||||
{
|
{
|
||||||
if (_allowMSizeModification)
|
if (_allowMSizeModification)
|
||||||
@ -70,6 +107,7 @@ public:
|
|||||||
bool invalidatesStorage() const { return m_sideEffects.storage == SideEffects::Write; }
|
bool invalidatesStorage() const { return m_sideEffects.storage == SideEffects::Write; }
|
||||||
bool invalidatesMemory() const { return m_sideEffects.memory == SideEffects::Write; }
|
bool invalidatesMemory() const { return m_sideEffects.memory == SideEffects::Write; }
|
||||||
|
|
||||||
|
SideEffects sideEffects() { return m_sideEffects; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
|
Loading…
Reference in New Issue
Block a user