Skip to content

Commit

Permalink
Reverse lookup for DataFlowAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-matic committed May 11, 2023
1 parent f07c8b1 commit b6a2886
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Expand Up @@ -4,7 +4,7 @@ Language Features:


Compiler Features:

* Optimizer: Introduced an optimized variable assignment map to improve the performance of Yul optimizer steps that rely on data flow analysis.

Bugfixes:

Expand Down
2 changes: 2 additions & 0 deletions libyul/CMakeLists.txt
Expand Up @@ -219,6 +219,8 @@ add_library(yul
optimiser/UnusedPruner.h
optimiser/VarDeclInitializer.cpp
optimiser/VarDeclInitializer.h
optimiser/VariableAssignmentMap.cpp
optimiser/VariableAssignmentMap.h
optimiser/VarNameCleaner.cpp
optimiser/VarNameCleaner.h
)
Expand Down
7 changes: 3 additions & 4 deletions libyul/optimiser/DataFlowAnalyzer.cpp
Expand Up @@ -271,7 +271,7 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
auto const& referencedVariables = movableChecker.referencedVariables();
for (auto const& name: _variables)
{
m_state.references[name] = referencedVariables;
m_state.references.insert(name, referencedVariables);
if (!_isDeclaration)
{
// assignment to slot denoted by "name"
Expand Down Expand Up @@ -353,9 +353,8 @@ 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 (auto&& references = m_state.references.getReversedOrNullptr(variableToClear))
_variables += *references;

// Clear the value and update the reference relation.
for (auto const& name: _variables)
Expand Down
9 changes: 5 additions & 4 deletions libyul/optimiser/DataFlowAnalyzer.h
Expand Up @@ -25,6 +25,7 @@

#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/KnowledgeBase.h>
#include <libyul/optimiser/VariableAssignmentMap.h>
#include <libyul/YulString.h>
#include <libyul/AST.h> // Needed for m_zero below.
#include <libyul/SideEffects.h>
Expand Down Expand Up @@ -104,7 +105,7 @@ class DataFlowAnalyzer: public ASTModifier

/// @returns the current value of the given variable, if known - always movable.
AssignedValue const* variableValue(YulString _variable) const { return util::valueOrNullptr(m_state.value, _variable); }
std::set<YulString> const* references(YulString _variable) const { return util::valueOrNullptr(m_state.references, _variable); }
std::set<YulString> const* references(YulString _variable) const { return m_state.references.getOrderedOrNullptr(_variable); }
std::map<YulString, AssignedValue> const& allValues() const { return m_state.value; }
std::optional<YulString> storageValue(YulString _key) const;
std::optional<YulString> memoryValue(YulString _key) const;
Expand Down Expand Up @@ -179,9 +180,9 @@ class DataFlowAnalyzer: public ASTModifier
{
/// Current values of variables, always movable.
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;

/// references.m_ordered[a].contains(b) <=> the current expression assigned to a references b
/// references.m_reversed[b].contains(a) <=> b from current expression assigned to a is references by a
VariableAssignmentMap references;
Environment environment;
};

Expand Down
39 changes: 39 additions & 0 deletions libyul/optimiser/VariableAssignmentMap.cpp
@@ -0,0 +1,39 @@
#include <libyul/optimiser/VariableAssignmentMap.h>

using std::set;
using namespace solidity::yul;

void VariableAssignmentMap::insert(YulString const& _variable, std::set<YulString> const& _references)
{
erase(_variable);
m_ordered[_variable] = _references;
for (auto&& reference: _references)
m_reversed[reference].emplace(_variable);
}

void VariableAssignmentMap::erase(YulString const& _variable)
{
for (auto&& reference: m_ordered[_variable])
if (m_reversed.find(reference) != m_reversed.end())
{
if (m_reversed[reference].size() > 1)
m_reversed[reference].erase(_variable);
else
// Only fully remove an entry if no variables other than _variable
// are contained in the set pointed to by reference.
m_reversed.erase(reference);
}
m_ordered.erase(_variable);
}

set<YulString> const* VariableAssignmentMap::getOrderedOrNullptr(YulString const& _variable) const
{
auto&& it = m_ordered.find(_variable);
return (it != m_ordered.end()) ? &it->second : nullptr;
}

set<YulString> const* VariableAssignmentMap::getReversedOrNullptr(YulString const& _variable) const
{
auto&& it = m_reversed.find(_variable);
return (it != m_reversed.end()) ? &it->second : nullptr;
}
86 changes: 86 additions & 0 deletions libyul/optimiser/VariableAssignmentMap.h
@@ -0,0 +1,86 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <libyul/YulString.h>

#include <set>
#include <unordered_map>

namespace solidity::yul
{

/**
* Class that implements a reverse lookup for an ``unordered_map<YulString, set<YulString>>`` by wrapping the
* two such maps - one ordered, and one reversed, e.g.
*
* m_ordered m_reversed
* f -> (g,) g -> (f,)
* c -> (b,d,e,) d -> (c,)
* a -> (b,c,) c -> (a,)
* e -> (c,)
* b -> (a,c,)
*
* The above example will from here onwards be referenced as ```Ref 1```.
*
* This allows us to have simultaneously managed insertion and deletion via a single interface, instead of manually
* managing this at the point of usage (see ``DataFlowAnalyzer``).
*/
class VariableAssignmentMap
{
public:
VariableAssignmentMap() = default;
/**
* Insert a set of values for the provided key ``_variable`` into ``m_ordered`` and ``m_reversed``.
* This method will erase all references of ``_variable`` from both sets before performing the insertion,
* practically making this an insert with overwrite method, akin to container assignment with subscript operator,
* i.e. container[index] = value.
* For example, if ``_variable`` is ``x`` and ``_references`` is ``{"y", "z"}``, the following would be added to ``Ref 1``:
*
* m_ordered m_reversed
* x -> (y, z,) y -> (x,)
* y -> (z,)
*
* @param _variable current expression variable
* @param _references all referenced variables in the expression assigned to ``_variable``
*/
void insert(YulString const& _variable, std::set<YulString> const& _references);

/**
* Erase entries in both maps based on provided ``_variable``.
* For example, after deleting ``c`` for ``Ref 1``, ``Ref 1`` would contain the following:
*
* m_ordered m_reversed
* f -> (g,) g -> (f,)
* a -> (b,c,) c -> (a,)
* b -> (a,)
*
* @param _variable variable to erase
*/
void erase(YulString const& _variable);
std::set<YulString> const* getOrderedOrNullptr(YulString const& _variable) const;
std::set<YulString> const* getReversedOrNullptr(YulString const& _variable) const;

private:
/// m_ordered[a].contains[b] <=> the current expression assigned to ``a`` references ``b``
std::unordered_map<YulString, std::set<YulString>> m_ordered;
/// m_reversed[b].contains[a] <=> the current expression assigned to ``a`` references ``b``
std::unordered_map<YulString, std::set<YulString>> m_reversed;
};

} // solidity::yul

0 comments on commit b6a2886

Please sign in to comment.