#include <libyul/optimiser/UnusedStoreBase.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/AST.h>
#include <libsolutil/CommonData.h>
#include <range/v3/action/remove_if.hpp>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
void UnusedStoreBase::operator()(If const& _if)
{
visit(*_if.condition);
TrackedStores skipBranch{m_stores};
(*this)(_if.body);
merge(m_stores, move(skipBranch));
}
void UnusedStoreBase::operator()(Switch const& _switch)
{
visit(*_switch.expression);
TrackedStores const preState{m_stores};
bool hasDefault = false;
vector<TrackedStores> branches;
for (auto const& c: _switch.cases)
{
if (!c.value)
hasDefault = true;
(*this)(c.body);
branches.emplace_back(move(m_stores));
m_stores = preState;
}
if (hasDefault)
{
m_stores = move(branches.back());
branches.pop_back();
}
for (auto& branch: branches)
merge(m_stores, move(branch));
}
void UnusedStoreBase::operator()(FunctionDefinition const& _functionDefinition)
{
ScopedSaveAndRestore outerAssignments(m_stores, {});
ScopedSaveAndRestore forLoopInfo(m_forLoopInfo, {});
ScopedSaveAndRestore forLoopNestingDepth(m_forLoopNestingDepth, 0);
(*this)(_functionDefinition.body);
finalizeFunctionDefinition(_functionDefinition);
}
void UnusedStoreBase::operator()(ForLoop const& _forLoop)
{
ScopedSaveAndRestore outerForLoopInfo(m_forLoopInfo, {});
ScopedSaveAndRestore forLoopNestingDepth(m_forLoopNestingDepth, m_forLoopNestingDepth + 1);
assertThrow(_forLoop.pre.statements.empty(), OptimizerException, "");
visit(*_forLoop.condition);
TrackedStores zeroRuns{m_stores};
(*this)(_forLoop.body);
merge(m_stores, move(m_forLoopInfo.pendingContinueStmts));
m_forLoopInfo.pendingContinueStmts = {};
(*this)(_forLoop.post);
visit(*_forLoop.condition);
if (m_forLoopNestingDepth < 6)
{
TrackedStores oneRun{m_stores};
(*this)(_forLoop.body);
merge(m_stores, move(m_forLoopInfo.pendingContinueStmts));
m_forLoopInfo.pendingContinueStmts.clear();
(*this)(_forLoop.post);
visit(*_forLoop.condition);
merge(m_stores, move(oneRun));
}
else
shortcutNestedLoop(zeroRuns);
merge(m_stores, move(zeroRuns));
merge(m_stores, move(m_forLoopInfo.pendingBreakStmts));
m_forLoopInfo.pendingBreakStmts.clear();
}
void UnusedStoreBase::operator()(Break const&)
{
m_forLoopInfo.pendingBreakStmts.emplace_back(move(m_stores));
m_stores.clear();
}
void UnusedStoreBase::operator()(Continue const&)
{
m_forLoopInfo.pendingContinueStmts.emplace_back(move(m_stores));
m_stores.clear();
}
void UnusedStoreBase::merge(TrackedStores& _target, TrackedStores&& _other)
{
util::joinMap(_target, move(_other), [](
map<Statement const*, State>& _assignmentHere,
map<Statement const*, State>&& _assignmentThere
)
{
return util::joinMap(_assignmentHere, move(_assignmentThere), State::join);
});
}
void UnusedStoreBase::merge(TrackedStores& _target, vector<TrackedStores>&& _source)
{
for (TrackedStores& ts: _source)
merge(_target, move(ts));
_source.clear();
}