From 83f6330ee9a6799549c554cb90c22554895e5dbf Mon Sep 17 00:00:00 2001 From: Nikola Matic Date: Wed, 22 Mar 2023 13:19:29 +0100 Subject: [PATCH] Reverse lookup optimization for DataFlowAnalyzer --- libyul/optimiser/DataFlowAnalyzer.cpp | 11 ++++++++--- libyul/optimiser/DataFlowAnalyzer.h | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/libyul/optimiser/DataFlowAnalyzer.cpp b/libyul/optimiser/DataFlowAnalyzer.cpp index 81028e300..9b15d20a9 100644 --- a/libyul/optimiser/DataFlowAnalyzer.cpp +++ b/libyul/optimiser/DataFlowAnalyzer.cpp @@ -272,6 +272,8 @@ void DataFlowAnalyzer::handleAssignment(set 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 _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); } } diff --git a/libyul/optimiser/DataFlowAnalyzer.h b/libyul/optimiser/DataFlowAnalyzer.h index b449357e6..77862eb45 100644 --- a/libyul/optimiser/DataFlowAnalyzer.h +++ b/libyul/optimiser/DataFlowAnalyzer.h @@ -181,6 +181,8 @@ private: std::map value; /// m_references[a].contains(b) <=> the current expression assigned to a references b std::unordered_map> references; + /// Reverse lookup for above m_references map + std::unordered_map> references_reverse_lookup; Environment environment; };