diff options
author | chriseth <chris@ethereum.org> | 2018-11-03 23:04:42 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2018-11-13 06:00:37 +0800 |
commit | b1454433b27124badece507b3f3aae822de8d83d (patch) | |
tree | ae6e54ba7734db5fea4e7c1c2cdf0ed277a0efec /libyul | |
parent | faa7b61d763824f482b513b20bf6a2c38807480e (diff) | |
download | dexon-solidity-b1454433b27124badece507b3f3aae822de8d83d.tar.gz dexon-solidity-b1454433b27124badece507b3f3aae822de8d83d.tar.zst dexon-solidity-b1454433b27124badece507b3f3aae822de8d83d.zip |
Remove variables that go out of scope from data structure.
Diffstat (limited to 'libyul')
-rw-r--r-- | libyul/optimiser/RedundantAssignEliminator.cpp | 32 | ||||
-rw-r--r-- | libyul/optimiser/RedundantAssignEliminator.h | 7 |
2 files changed, 29 insertions, 10 deletions
diff --git a/libyul/optimiser/RedundantAssignEliminator.cpp b/libyul/optimiser/RedundantAssignEliminator.cpp index df6c4e4e..b7217074 100644 --- a/libyul/optimiser/RedundantAssignEliminator.cpp +++ b/libyul/optimiser/RedundantAssignEliminator.cpp @@ -96,9 +96,15 @@ void RedundantAssignEliminator::operator()(FunctionDefinition const& _functionDe (*this)(_functionDefinition.body); for (auto const& param: _functionDefinition.parameters) + { changeUndecidedTo(param.name, State::Unused); + finalize(param.name); + } for (auto const& retParam: _functionDefinition.returnVariables) + { changeUndecidedTo(retParam.name, State::Used); + finalize(retParam.name); + } } void RedundantAssignEliminator::operator()(ForLoop const& _forLoop) @@ -150,16 +156,7 @@ void RedundantAssignEliminator::run(Block& _ast) RedundantAssignEliminator rae; rae(_ast); - std::set<Assignment const*> assignmentsToRemove; - for (auto const& variables: rae.m_assignments) - for (auto const& assignment: variables.second) - { - assertThrow(assignment.second != State::Undecided, OptimizerException, ""); - if (assignment.second == State::Unused && MovableChecker{*assignment.first->value}.movable()) - assignmentsToRemove.emplace(assignment.first); - } - - AssignmentRemover remover{assignmentsToRemove}; + AssignmentRemover remover{rae.m_assignmentsToRemove}; remover(_ast); } @@ -192,6 +189,8 @@ void joinMap(std::map<K, V>& _a, std::map<K, V>&& _b, F _conflictSolver) void RedundantAssignEliminator::join(RedundantAssignEliminator& _other) { + m_assignmentsToRemove.insert(begin(_other.m_assignmentsToRemove), end(_other.m_assignmentsToRemove)); + joinMap(m_assignments, std::move(_other.m_assignments), []( map<Assignment const*, State>& _assignmentHere, map<Assignment const*, State>&& _assignmentThere @@ -208,6 +207,19 @@ void RedundantAssignEliminator::changeUndecidedTo(YulString _variable, Redundant assignment.second = _newState; } +void RedundantAssignEliminator::finalize(YulString _variable) +{ + for (auto& assignment: m_assignments[_variable]) + { + assertThrow(assignment.second != State::Undecided, OptimizerException, ""); + if (assignment.second == State{State::Unused} && MovableChecker{*assignment.first->value}.movable()) + // TODO the only point where we actually need this + // to be a set is for the for loop + m_assignmentsToRemove.insert(assignment.first); + } + m_assignments.erase(_variable); +} + void AssignmentRemover::operator()(Block& _block) { boost::range::remove_erase_if(_block.statements, [=](Statement const& _statement) -> bool { diff --git a/libyul/optimiser/RedundantAssignEliminator.h b/libyul/optimiser/RedundantAssignEliminator.h index a9c6a8f7..76106aae 100644 --- a/libyul/optimiser/RedundantAssignEliminator.h +++ b/libyul/optimiser/RedundantAssignEliminator.h @@ -149,8 +149,12 @@ private: } ~BlockScope() { + // This should actually store all declared variables + // into a different mapping for (auto const& var: m_rae.m_declaredVariables) m_rae.changeUndecidedTo(var, State::Unused); + for (auto const& var: m_rae.m_declaredVariables) + m_rae.finalize(var); swap(m_rae.m_declaredVariables, m_outerDeclaredVariables); } @@ -164,10 +168,13 @@ private: /// Will destroy @a _other. void join(RedundantAssignEliminator& _other); void changeUndecidedTo(YulString _variable, State _newState); + void finalize(YulString _variable); std::set<YulString> m_declaredVariables; // TODO check that this does not cause nondeterminism! + // This could also be a pseudo-map from state to assignment. std::map<YulString, std::map<Assignment const*, State>> m_assignments; + std::set<Assignment const*> m_assignmentsToRemove; }; class AssignmentRemover: public ASTModifier |