Reverse lookup optimization for DataFlowAnalyzer

This commit is contained in:
Nikola Matic 2023-03-22 13:19:29 +01:00
parent 79d975a77d
commit 83f6330ee9
2 changed files with 10 additions and 3 deletions

View File

@ -272,6 +272,8 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
for (auto const& name: _variables)
{
m_state.references[name] = referencedVariables;
for (auto&& referencedVariable: referencedVariables)
m_state.references_reverse_lookup[referencedVariable].insert(name);
if (!_isDeclaration)
{
// assignment to slot denoted by "name"
@ -316,6 +318,8 @@ void DataFlowAnalyzer::popScope()
for (auto const& name: m_variableScopes.back().variables)
{
m_state.value.erase(name);
for (auto&& ref: m_state.references[name])
m_state.references_reverse_lookup.erase(ref);
m_state.references.erase(name);
}
m_variableScopes.pop_back();
@ -353,14 +357,15 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
// Also clear variables that reference variables to be cleared.
for (auto const& variableToClear: _variables)
for (auto const& [ref, names]: m_state.references)
if (names.count(variableToClear))
_variables.emplace(ref);
if (m_state.references_reverse_lookup.count(variableToClear))
_variables += m_state.references_reverse_lookup[variableToClear];
// Clear the value and update the reference relation.
for (auto const& name: _variables)
{
m_state.value.erase(name);
for (auto&& ref: m_state.references[name])
m_state.references_reverse_lookup.erase(ref);
m_state.references.erase(name);
}
}

View File

@ -181,6 +181,8 @@ private:
std::map<YulString, AssignedValue> value;
/// m_references[a].contains(b) <=> the current expression assigned to a references b
std::unordered_map<YulString, std::set<YulString>> references;
/// Reverse lookup for above m_references map
std::unordered_map<YulString, std::set<YulString>> references_reverse_lookup;
Environment environment;
};