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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user