Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libyul/optimiser/LoopInvariantCodeMotion.h
Line
Count
Source
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
// SPDX-License-Identifier: GPL-3.0
18
#pragma once
19
20
#include <libyul/optimiser/ASTWalker.h>
21
#include <libyul/optimiser/Semantics.h>
22
#include <libyul/optimiser/OptimiserStep.h>
23
24
namespace solidity::yul
25
{
26
27
/**
28
 * Loop-invariant code motion.
29
 *
30
 * This optimization moves movable SSA variable declarations outside the loop.
31
 *
32
 * Only statements at the top level in a loop's body or post block are considered, i.e variable
33
 * declarations inside conditional branches will not be moved out of the loop.
34
 *
35
 * Requirements:
36
 * - The Disambiguator, ForLoopInitRewriter and FunctionHoister must be run upfront.
37
 * - Expression splitter and SSA transform should be run upfront to obtain better result.
38
 */
39
40
class LoopInvariantCodeMotion: public ASTModifier
41
{
42
public:
43
  static constexpr char const* name{"LoopInvariantCodeMotion"};
44
  static void run(OptimiserStepContext& _context, Block& _ast);
45
46
  void operator()(Block& _block) override;
47
48
private:
49
  explicit LoopInvariantCodeMotion(
50
    Dialect const& _dialect,
51
    std::set<YulString> const& _ssaVariables,
52
    std::map<YulString, SideEffects> const& _functionSideEffects,
53
    bool _containsMSize
54
  ):
55
    m_containsMSize(_containsMSize),
56
    m_dialect(_dialect),
57
    m_ssaVariables(_ssaVariables),
58
    m_functionSideEffects(_functionSideEffects)
59
137k
  { }
60
61
  /// @returns true if the given variable declaration can be moved to in front of the loop.
62
  bool canBePromoted(
63
    VariableDeclaration const& _varDecl,
64
    std::set<YulString> const& _varsDefinedInCurrentScope,
65
    SideEffects const& _forLoopSideEffects
66
  ) const;
67
  std::optional<std::vector<Statement>> rewriteLoop(ForLoop& _for);
68
69
  bool m_containsMSize = true;
70
  Dialect const& m_dialect;
71
  std::set<YulString> const& m_ssaVariables;
72
  std::map<YulString, SideEffects> const& m_functionSideEffects;
73
};
74
75
}