mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6848 from ethereum/invertibleRelation
Implement references using InvertibleRelation as data structure.
This commit is contained in:
commit
c9e2d388b5
@ -12,6 +12,7 @@ set(sources
|
|||||||
FixedHash.h
|
FixedHash.h
|
||||||
IndentedWriter.cpp
|
IndentedWriter.cpp
|
||||||
IndentedWriter.h
|
IndentedWriter.h
|
||||||
|
InvertibleMap.h
|
||||||
IpfsHash.cpp
|
IpfsHash.cpp
|
||||||
IpfsHash.h
|
IpfsHash.h
|
||||||
JSON.cpp
|
JSON.cpp
|
||||||
|
51
libdevcore/InvertibleMap.h
Normal file
51
libdevcore/InvertibleMap.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
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 <map>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct InvertibleRelation
|
||||||
|
{
|
||||||
|
/// forward[x] contains y <=> backward[y] contains x
|
||||||
|
std::map<T, std::set<T>> forward;
|
||||||
|
std::map<T, std::set<T>> backward;
|
||||||
|
|
||||||
|
void insert(T _key, T _value)
|
||||||
|
{
|
||||||
|
forward[_key].insert(_value);
|
||||||
|
backward[_value].insert(_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set(T _key, std::set<T> _values)
|
||||||
|
{
|
||||||
|
for (T v: forward[_key])
|
||||||
|
backward[v].erase(_key);
|
||||||
|
for (T v: _values)
|
||||||
|
backward[v].insert(_key);
|
||||||
|
forward[_key] = std::move(_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
void eraseKey(T _key)
|
||||||
|
{
|
||||||
|
for (auto const& v: forward[_key])
|
||||||
|
backward[v].erase(_key);
|
||||||
|
forward.erase(_key);
|
||||||
|
}
|
||||||
|
};
|
@ -88,11 +88,9 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
|||||||
// Save all information. We might rather reinstantiate this class,
|
// Save all information. We might rather reinstantiate this class,
|
||||||
// but this could be difficult if it is subclassed.
|
// but this could be difficult if it is subclassed.
|
||||||
map<YulString, Expression const*> value;
|
map<YulString, Expression const*> value;
|
||||||
map<YulString, set<YulString>> references;
|
InvertibleRelation<YulString> references;
|
||||||
map<YulString, set<YulString>> referencedBy;
|
|
||||||
m_value.swap(value);
|
m_value.swap(value);
|
||||||
m_references.swap(references);
|
swap(m_references, references);
|
||||||
m_referencedBy.swap(referencedBy);
|
|
||||||
pushScope(true);
|
pushScope(true);
|
||||||
|
|
||||||
for (auto const& parameter: _fun.parameters)
|
for (auto const& parameter: _fun.parameters)
|
||||||
@ -106,8 +104,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
|||||||
|
|
||||||
popScope();
|
popScope();
|
||||||
m_value.swap(value);
|
m_value.swap(value);
|
||||||
m_references.swap(references);
|
swap(m_references, references);
|
||||||
m_referencedBy.swap(referencedBy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||||
@ -162,11 +159,7 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
|
|||||||
|
|
||||||
auto const& referencedVariables = movableChecker.referencedVariables();
|
auto const& referencedVariables = movableChecker.referencedVariables();
|
||||||
for (auto const& name: _variables)
|
for (auto const& name: _variables)
|
||||||
{
|
m_references.set(name, referencedVariables);
|
||||||
m_references[name] = referencedVariables;
|
|
||||||
for (auto const& ref: referencedVariables)
|
|
||||||
m_referencedBy[ref].emplace(name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
||||||
@ -197,18 +190,14 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
|||||||
|
|
||||||
// Clear variables that reference variables to be cleared.
|
// Clear variables that reference variables to be cleared.
|
||||||
for (auto const& name: _variables)
|
for (auto const& name: _variables)
|
||||||
for (auto const& ref: m_referencedBy[name])
|
for (auto const& ref: m_references.backward[name])
|
||||||
_variables.emplace(ref);
|
_variables.emplace(ref);
|
||||||
|
|
||||||
// Clear the value and update the reference relation.
|
// Clear the value and update the reference relation.
|
||||||
for (auto const& name: _variables)
|
for (auto const& name: _variables)
|
||||||
m_value.erase(name);
|
m_value.erase(name);
|
||||||
for (auto const& name: _variables)
|
for (auto const& name: _variables)
|
||||||
{
|
m_references.eraseKey(name);
|
||||||
for (auto const& ref: m_references[name])
|
|
||||||
m_referencedBy[ref].erase(name);
|
|
||||||
m_references[name].clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
#include <libyul/YulString.h>
|
#include <libyul/YulString.h>
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
|
|
||||||
|
#include <libdevcore/InvertibleMap.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
@ -75,10 +77,9 @@ protected:
|
|||||||
|
|
||||||
/// Current values of variables, always movable.
|
/// Current values of variables, always movable.
|
||||||
std::map<YulString, Expression const*> m_value;
|
std::map<YulString, Expression const*> m_value;
|
||||||
/// m_references[a].contains(b) <=> the current expression assigned to a references b
|
/// m_references.forward[a].contains(b) <=> the current expression assigned to a references b
|
||||||
std::map<YulString, std::set<YulString>> m_references;
|
/// m_references.backward[b].contains(a) <=> the current expression assigned to a references b
|
||||||
/// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b
|
InvertibleRelation<YulString> m_references;
|
||||||
std::map<YulString, std::set<YulString>> m_referencedBy;
|
|
||||||
|
|
||||||
struct Scope
|
struct Scope
|
||||||
{
|
{
|
||||||
|
@ -81,7 +81,7 @@ void Rematerialiser::visit(Expression& _e)
|
|||||||
if (refs <= 1 || cost == 0 || (refs <= 5 && cost <= 1) || m_varsToAlwaysRematerialize.count(name))
|
if (refs <= 1 || cost == 0 || (refs <= 5 && cost <= 1) || m_varsToAlwaysRematerialize.count(name))
|
||||||
{
|
{
|
||||||
assertThrow(m_referenceCounts[name] > 0, OptimizerException, "");
|
assertThrow(m_referenceCounts[name] > 0, OptimizerException, "");
|
||||||
for (auto const& ref: m_references[name])
|
for (auto const& ref: m_references.forward[name])
|
||||||
assertThrow(inScope(ref), OptimizerException, "");
|
assertThrow(inScope(ref), OptimizerException, "");
|
||||||
// update reference counts
|
// update reference counts
|
||||||
m_referenceCounts[name]--;
|
m_referenceCounts[name]--;
|
||||||
|
@ -57,7 +57,7 @@ public:
|
|||||||
for (auto const& codeCost: m_expressionCodeCost)
|
for (auto const& codeCost: m_expressionCodeCost)
|
||||||
{
|
{
|
||||||
size_t numRef = m_numReferences[codeCost.first];
|
size_t numRef = m_numReferences[codeCost.first];
|
||||||
cand.emplace(make_tuple(codeCost.second * numRef, codeCost.first, m_references[codeCost.first]));
|
cand.emplace(make_tuple(codeCost.second * numRef, codeCost.first, m_references.forward[codeCost.first]));
|
||||||
}
|
}
|
||||||
return cand;
|
return cand;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
let a := calldataload(0)
|
||||||
|
let x := calldataload(0x20)
|
||||||
|
x := a
|
||||||
|
let z := 0
|
||||||
|
x := z
|
||||||
|
a := 9
|
||||||
|
sstore(x, 3)
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// step: commonSubexpressionEliminator
|
||||||
|
// ----
|
||||||
|
// {
|
||||||
|
// let a := calldataload(0)
|
||||||
|
// let x := calldataload(0x20)
|
||||||
|
// x := a
|
||||||
|
// let z := 0
|
||||||
|
// x := z
|
||||||
|
// a := 9
|
||||||
|
// sstore(z, 3)
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user