Merge pull request #6848 from ethereum/invertibleRelation

Implement references using InvertibleRelation as data structure.
This commit is contained in:
chriseth
2019-05-29 13:34:10 +02:00
committed by GitHub
7 changed files with 86 additions and 23 deletions
+6 -17
View File
@@ -88,11 +88,9 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
// Save all information. We might rather reinstantiate this class,
// but this could be difficult if it is subclassed.
map<YulString, Expression const*> value;
map<YulString, set<YulString>> references;
map<YulString, set<YulString>> referencedBy;
InvertibleRelation<YulString> references;
m_value.swap(value);
m_references.swap(references);
m_referencedBy.swap(referencedBy);
swap(m_references, references);
pushScope(true);
for (auto const& parameter: _fun.parameters)
@@ -106,8 +104,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
popScope();
m_value.swap(value);
m_references.swap(references);
m_referencedBy.swap(referencedBy);
swap(m_references, references);
}
void DataFlowAnalyzer::operator()(ForLoop& _for)
@@ -162,11 +159,7 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
auto const& referencedVariables = movableChecker.referencedVariables();
for (auto const& name: _variables)
{
m_references[name] = referencedVariables;
for (auto const& ref: referencedVariables)
m_referencedBy[ref].emplace(name);
}
m_references.set(name, referencedVariables);
}
void DataFlowAnalyzer::pushScope(bool _functionScope)
@@ -197,18 +190,14 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
// Clear variables that reference variables to be cleared.
for (auto const& name: _variables)
for (auto const& ref: m_referencedBy[name])
for (auto const& ref: m_references.backward[name])
_variables.emplace(ref);
// Clear the value and update the reference relation.
for (auto const& name: _variables)
m_value.erase(name);
for (auto const& name: _variables)
{
for (auto const& ref: m_references[name])
m_referencedBy[ref].erase(name);
m_references[name].clear();
}
m_references.eraseKey(name);
}
bool DataFlowAnalyzer::inScope(YulString _variableName) const
+5 -4
View File
@@ -26,6 +26,8 @@
#include <libyul/YulString.h>
#include <libyul/AsmData.h>
#include <libdevcore/InvertibleMap.h>
#include <map>
#include <set>
@@ -75,10 +77,9 @@ protected:
/// Current values of variables, always movable.
std::map<YulString, Expression const*> m_value;
/// m_references[a].contains(b) <=> the current expression assigned to a references b
std::map<YulString, std::set<YulString>> m_references;
/// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b
std::map<YulString, std::set<YulString>> m_referencedBy;
/// m_references.forward[a].contains(b) <=> the current expression assigned to a references b
/// m_references.backward[b].contains(a) <=> the current expression assigned to a references b
InvertibleRelation<YulString> m_references;
struct Scope
{
+1 -1
View File
@@ -81,7 +81,7 @@ void Rematerialiser::visit(Expression& _e)
if (refs <= 1 || cost == 0 || (refs <= 5 && cost <= 1) || m_varsToAlwaysRematerialize.count(name))
{
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, "");
// update reference counts
m_referenceCounts[name]--;
+1 -1
View File
@@ -57,7 +57,7 @@ public:
for (auto const& codeCost: m_expressionCodeCost)
{
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;
}