Extended LoopInvariantCodeMotion for storage and state

This commit is contained in:
Harikrishnan Mulackal 2020-07-03 18:40:29 +05:30
parent 3cbe65e4f3
commit 5c6e7f03b4
4 changed files with 68 additions and 7 deletions

View File

@ -35,9 +35,9 @@ void LoopInvariantCodeMotion::run(OptimiserStepContext& _context, Block& _ast)
{
map<YulString, SideEffects> functionSideEffects =
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast));
bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _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)
@ -57,7 +57,8 @@ void LoopInvariantCodeMotion::operator()(Block& _block)
bool LoopInvariantCodeMotion::canBePromoted(
VariableDeclaration const& _varDecl,
set<YulString> const& _varsDefinedInCurrentScope
set<YulString> const& _varsDefinedInCurrentScope,
SideEffects const& _forLoopSideEffects
) const
{
// A declaration can be promoted iff
@ -73,7 +74,8 @@ bool LoopInvariantCodeMotion::canBePromoted(
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())
SideEffectsCollector sideEffects{m_dialect, *_varDecl.value, &m_functionSideEffects};
if (!sideEffects.movableRelativeTo(_forLoopSideEffects, m_containsMSize))
return false;
}
return true;
@ -82,6 +84,10 @@ bool LoopInvariantCodeMotion::canBePromoted(
optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
{
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
auto forLoopSideEffects =
SideEffectsCollector{m_dialect, _for, &m_functionSideEffects}.sideEffects();
vector<Statement> replacement;
for (Block* block: {&_for.post, &_for.body})
{
@ -93,7 +99,7 @@ optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for)
if (holds_alternative<VariableDeclaration>(_s))
{
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s);
if (canBePromoted(varDecl, varsDefinedInScope))
if (canBePromoted(varDecl, varsDefinedInScope, forLoopSideEffects))
{
replacement.emplace_back(std::move(_s));
// Do not add the variables declared here to varsDefinedInScope because we are moving them.

View File

@ -49,17 +49,24 @@ private:
explicit LoopInvariantCodeMotion(
Dialect const& _dialect,
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_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;
bool canBePromoted(
VariableDeclaration const& _varDecl,
std::set<YulString> const& _varsDefinedInCurrentScope,
SideEffects const& _forLoopSideEffects
) const;
std::optional<std::vector<Statement>> rewriteLoop(ForLoop& _for);
bool m_containsMSize = true;
Dialect const& m_dialect;
std::set<YulString> const& m_ssaVariables;
std::map<YulString, SideEffects> const& m_functionSideEffects;

View File

@ -62,6 +62,16 @@ SideEffectsCollector::SideEffectsCollector(
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)
{
ASTWalker::operator()(_functionCall);

View File

@ -54,11 +54,48 @@ public:
Block const& _ast,
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
);
SideEffectsCollector(
Dialect const& _dialect,
ForLoop const& _ast,
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
);
using ASTWalker::operator();
void operator()(FunctionCall const& _functionCall) override;
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
{
if (_allowMSizeModification)
@ -70,6 +107,7 @@ public:
bool invalidatesStorage() const { return m_sideEffects.storage == SideEffects::Write; }
bool invalidatesMemory() const { return m_sideEffects.memory == SideEffects::Write; }
SideEffects sideEffects() { return m_sideEffects; }
private:
Dialect const& m_dialect;