|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <libyul/optimiser/LoopInvariantCodeMotion.h> |
| 19 | + |
| 20 | +#include <libyul/optimiser/CallGraphGenerator.h> |
| 21 | +#include <libyul/optimiser/NameCollector.h> |
| 22 | +#include <libyul/optimiser/Semantics.h> |
| 23 | +#include <libyul/optimiser/SSAValueTracker.h> |
| 24 | +#include <libyul/AsmData.h> |
| 25 | +#include <libdevcore/CommonData.h> |
| 26 | + |
| 27 | +#include <utility> |
| 28 | + |
| 29 | +using namespace std; |
| 30 | +using namespace dev; |
| 31 | +using namespace yul; |
| 32 | + |
| 33 | +void LoopInvariantCodeMotion::run(OptimiserStepContext& _context, Block& _ast) |
| 34 | +{ |
| 35 | + map<YulString, SideEffects> functionSideEffects = |
| 36 | + SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)); |
| 37 | + |
| 38 | + set<YulString> ssaVars = SSAValueTracker::ssaVariables(_ast); |
| 39 | + LoopInvariantCodeMotion{_context.dialect, ssaVars, functionSideEffects}(_ast); |
| 40 | +} |
| 41 | + |
| 42 | +void LoopInvariantCodeMotion::operator()(Block& _block) |
| 43 | +{ |
| 44 | + iterateReplacing( |
| 45 | + _block.statements, |
| 46 | + [&](Statement& _s) -> optional<vector<Statement>> |
| 47 | + { |
| 48 | + visit(_s); |
| 49 | + if (holds_alternative<ForLoop>(_s)) |
| 50 | + return rewriteLoop(get<ForLoop>(_s)); |
| 51 | + else |
| 52 | + return {}; |
| 53 | + } |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +bool LoopInvariantCodeMotion::canBePromoted( |
| 58 | + VariableDeclaration const& _varDecl, |
| 59 | + set<YulString> const& _varsDefinedInCurrentScope |
| 60 | +) const |
| 61 | +{ |
| 62 | + // A declaration can be promoted iff |
| 63 | + // 1. Its LHS is a SSA variable |
| 64 | + // 2. Its RHS only references SSA variables declared outside of the current scope |
| 65 | + // 3. Its RHS is movable |
| 66 | + |
| 67 | + for (auto const& var: _varDecl.variables) |
| 68 | + if (!m_ssaVariables.count(var.name)) |
| 69 | + return false; |
| 70 | + if (_varDecl.value) |
| 71 | + { |
| 72 | + for (auto const& ref: ReferencesCounter::countReferences(*_varDecl.value, ReferencesCounter::OnlyVariables)) |
| 73 | + if (_varsDefinedInCurrentScope.count(ref.first) || !m_ssaVariables.count(ref.first)) |
| 74 | + return false; |
| 75 | + if (!SideEffectsCollector{m_dialect, *_varDecl.value, &m_functionSideEffects}.movable()) |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +optional<vector<Statement>> LoopInvariantCodeMotion::rewriteLoop(ForLoop& _for) |
| 82 | +{ |
| 83 | + assertThrow(_for.pre.statements.empty(), OptimizerException, ""); |
| 84 | + vector<Statement> replacement; |
| 85 | + for (Block* block: {&_for.post, &_for.body}) |
| 86 | + { |
| 87 | + set<YulString> varsDefinedInScope; |
| 88 | + iterateReplacing( |
| 89 | + block->statements, |
| 90 | + [&](Statement& _s) -> optional<vector<Statement>> |
| 91 | + { |
| 92 | + if (holds_alternative<VariableDeclaration>(_s)) |
| 93 | + { |
| 94 | + VariableDeclaration const& varDecl = std::get<VariableDeclaration>(_s); |
| 95 | + if (canBePromoted(varDecl, varsDefinedInScope)) |
| 96 | + { |
| 97 | + replacement.emplace_back(std::move(_s)); |
| 98 | + // Do not add the variables declared here to varsDefinedInScope because we are moving them. |
| 99 | + return vector<Statement>{}; |
| 100 | + } |
| 101 | + for (auto const& var: varDecl.variables) |
| 102 | + varsDefinedInScope.insert(var.name); |
| 103 | + } |
| 104 | + return {}; |
| 105 | + } |
| 106 | + ); |
| 107 | + } |
| 108 | + if (replacement.empty()) |
| 109 | + return {}; |
| 110 | + else |
| 111 | + { |
| 112 | + replacement.emplace_back(std::move(_for)); |
| 113 | + return { std::move(replacement) }; |
| 114 | + } |
| 115 | +} |
0 commit comments