diff options
Diffstat (limited to 'libyul/optimiser/DataFlowAnalyzer.cpp')
-rw-r--r-- | libyul/optimiser/DataFlowAnalyzer.cpp | 70 |
1 files changed, 46 insertions, 24 deletions
diff --git a/libyul/optimiser/DataFlowAnalyzer.cpp b/libyul/optimiser/DataFlowAnalyzer.cpp index ca1e5153..134777d0 100644 --- a/libyul/optimiser/DataFlowAnalyzer.cpp +++ b/libyul/optimiser/DataFlowAnalyzer.cpp @@ -38,9 +38,9 @@ using namespace dev::yul; void DataFlowAnalyzer::operator()(Assignment& _assignment) { - set<string> names; + set<YulString> names; for (auto const& var: _assignment.variableNames) - names.insert(var.name); + names.emplace(var.name); assertThrow(_assignment.value, OptimizerException, ""); visit(*_assignment.value); handleAssignment(names, _assignment.value.get()); @@ -48,9 +48,9 @@ void DataFlowAnalyzer::operator()(Assignment& _assignment) void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl) { - set<string> names; + set<YulString> names; for (auto const& var: _varDecl.variables) - names.insert(var.name); + names.emplace(var.name); m_variableScopes.back().variables += names; if (_varDecl.value) visit(*_varDecl.value); @@ -69,7 +69,7 @@ void DataFlowAnalyzer::operator()(If& _if) void DataFlowAnalyzer::operator()(Switch& _switch) { visit(*_switch.expression); - set<string> assignedVariables; + set<YulString> assignedVariables; for (auto& _case: _switch.cases) { (*this)(_case.body); @@ -84,19 +84,32 @@ void DataFlowAnalyzer::operator()(Switch& _switch) void DataFlowAnalyzer::operator()(FunctionDefinition& _fun) { - m_variableScopes.emplace_back(true); + // 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; + m_value.swap(value); + m_references.swap(references); + m_referencedBy.swap(referencedBy); + pushScope(true); + for (auto const& parameter: _fun.parameters) - m_variableScopes.back().variables.insert(parameter.name); + m_variableScopes.back().variables.emplace(parameter.name); for (auto const& var: _fun.returnVariables) - m_variableScopes.back().variables.insert(var.name); + m_variableScopes.back().variables.emplace(var.name); ASTModifier::operator()(_fun); - m_variableScopes.pop_back(); + + popScope(); + m_value.swap(value); + m_references.swap(references); + m_referencedBy.swap(referencedBy); } void DataFlowAnalyzer::operator()(ForLoop& _for) { // Special scope handling of the pre block. - m_variableScopes.emplace_back(false); + pushScope(false); for (auto& statement: _for.pre.statements) visit(statement); @@ -110,20 +123,19 @@ void DataFlowAnalyzer::operator()(ForLoop& _for) (*this)(_for.post); clearValues(assignments.names()); - - m_variableScopes.pop_back(); + popScope(); } void DataFlowAnalyzer::operator()(Block& _block) { size_t numScopes = m_variableScopes.size(); - m_variableScopes.emplace_back(false); + pushScope(false); ASTModifier::operator()(_block); - m_variableScopes.pop_back(); + popScope(); assertThrow(numScopes == m_variableScopes.size(), OptimizerException, ""); } -void DataFlowAnalyzer::handleAssignment(set<string> const& _variables, Expression* _value) +void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expression* _value) { clearValues(_variables); @@ -132,7 +144,7 @@ void DataFlowAnalyzer::handleAssignment(set<string> const& _variables, Expressio movableChecker.visit(*_value); if (_variables.size() == 1) { - string const& name = *_variables.begin(); + YulString name = *_variables.begin(); // Expression has to be movable and cannot contain a reference // to the variable that will be assigned to. if (_value && movableChecker.movable() && !movableChecker.referencedVariables().count(name)) @@ -144,11 +156,22 @@ void DataFlowAnalyzer::handleAssignment(set<string> const& _variables, Expressio { m_references[name] = referencedVariables; for (auto const& ref: referencedVariables) - m_referencedBy[ref].insert(name); + m_referencedBy[ref].emplace(name); } } -void DataFlowAnalyzer::clearValues(set<string> const& _variables) +void DataFlowAnalyzer::pushScope(bool _functionScope) +{ + m_variableScopes.emplace_back(_functionScope); +} + +void DataFlowAnalyzer::popScope() +{ + clearValues(std::move(m_variableScopes.back().variables)); + m_variableScopes.pop_back(); +} + +void DataFlowAnalyzer::clearValues(set<YulString> _variables) { // All variables that reference variables to be cleared also have to be // cleared, but not recursively, since only the value of the original @@ -163,16 +186,15 @@ void DataFlowAnalyzer::clearValues(set<string> const& _variables) // This cannot be easily tested since the substitutions will be done // one by one on the fly, and the last line will just be add(1, 1) - set<string> variables = _variables; // 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]) - variables.insert(ref); + _variables.emplace(ref); // Clear the value and update the reference relation. - for (auto const& name: variables) + for (auto const& name: _variables) m_value.erase(name); - for (auto const& name: variables) + for (auto const& name: _variables) { for (auto const& ref: m_references[name]) m_referencedBy[ref].erase(name); @@ -180,7 +202,7 @@ void DataFlowAnalyzer::clearValues(set<string> const& _variables) } } -bool DataFlowAnalyzer::inScope(string const& _variableName) const +bool DataFlowAnalyzer::inScope(YulString _variableName) const { for (auto const& scope: m_variableScopes | boost::adaptors::reversed) { |