Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f07c8b1
commit b6a2886
Showing
6 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |