diff --git a/libyul/optimiser/DataFlowAnalyzer.cpp b/libyul/optimiser/DataFlowAnalyzer.cpp index 6755500b3..97cdd2062 100644 --- a/libyul/optimiser/DataFlowAnalyzer.cpp +++ b/libyul/optimiser/DataFlowAnalyzer.cpp @@ -82,7 +82,7 @@ void DataFlowAnalyzer::operator()(Assignment& _assignment) assertThrow(_assignment.value, OptimizerException, ""); clearKnowledgeIfInvalidated(*_assignment.value); visit(*_assignment.value); - handleAssignment(names, _assignment.value.get()); + handleAssignment(names, _assignment.value.get(), false); } void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl) @@ -98,7 +98,7 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl) visit(*_varDecl.value); } - handleAssignment(names, _varDecl.value.get()); + handleAssignment(names, _varDecl.value.get(), true); } void DataFlowAnalyzer::operator()(If& _if) @@ -161,7 +161,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun) for (auto const& var: _fun.returnVariables) { m_variableScopes.back().variables.emplace(var.name); - handleAssignment({var.name}, nullptr); + handleAssignment({var.name}, nullptr, true); } ASTModifier::operator()(_fun); @@ -220,9 +220,10 @@ void DataFlowAnalyzer::operator()(Block& _block) assertThrow(numScopes == m_variableScopes.size(), OptimizerException, ""); } -void DataFlowAnalyzer::handleAssignment(set const& _variables, Expression* _value) +void DataFlowAnalyzer::handleAssignment(set const& _variables, Expression* _value, bool _isDeclaration) { - clearValues(_variables); + if (!_isDeclaration) + clearValues(_variables); MovableChecker movableChecker{m_dialect, &m_functionSideEffects}; if (_value) @@ -244,14 +245,17 @@ void DataFlowAnalyzer::handleAssignment(set const& _variables, Expres for (auto const& name: _variables) { m_references.set(name, referencedVariables); - // assignment to slot denoted by "name" - m_storage.eraseKey(name); - // assignment to slot contents denoted by "name" - m_storage.eraseValue(name); - // assignment to slot denoted by "name" - m_memory.eraseKey(name); - // assignment to slot contents denoted by "name" - m_memory.eraseValue(name); + if (!_isDeclaration) + { + // assignment to slot denoted by "name" + m_storage.eraseKey(name); + // assignment to slot contents denoted by "name" + m_storage.eraseValue(name); + // assignment to slot denoted by "name" + m_memory.eraseKey(name); + // assignment to slot contents denoted by "name" + m_memory.eraseValue(name); + } } if (_value && _variables.size() == 1) diff --git a/libyul/optimiser/DataFlowAnalyzer.h b/libyul/optimiser/DataFlowAnalyzer.h index 237b48597..e5d868246 100644 --- a/libyul/optimiser/DataFlowAnalyzer.h +++ b/libyul/optimiser/DataFlowAnalyzer.h @@ -107,7 +107,7 @@ public: protected: /// Registers the assignment. - void handleAssignment(std::set const& _names, Expression* _value); + void handleAssignment(std::set const& _names, Expression* _value, bool _isDeclaration); /// Creates a new inner scope. void pushScope(bool _functionScope);