Coverage Report

Created: 2022-08-24 06:52

/src/solidity/libyul/optimiser/UnusedPruner.h
Line
Count
Source (jump to first uncovered line)
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
/**
19
 * Optimisation stage that removes unused variables and functions.
20
 */
21
22
#pragma once
23
24
#include <libyul/optimiser/ASTWalker.h>
25
#include <libyul/optimiser/OptimiserStep.h>
26
#include <libyul/YulString.h>
27
28
#include <map>
29
#include <set>
30
31
namespace solidity::yul
32
{
33
struct Dialect;
34
struct SideEffects;
35
36
/**
37
 * Optimisation stage that removes unused variables and functions and also
38
 * removes side-effect-free expression statements.
39
 *
40
 * If msize is used, we cannot remove any statements that access memory.
41
 * Because of that, the Unused Pruner should only be invoked on full ASTs,
42
 * such that it can check for the presence of msize itself, or
43
 * the `_allowMSizeOptimization` needs to be passed.
44
 *
45
 * Note that this does not remove circular references.
46
 *
47
 * Prerequisite: Disambiguator
48
 */
49
class UnusedPruner: public ASTModifier
50
{
51
public:
52
  static constexpr char const* name{"UnusedPruner"};
53
  static void run(OptimiserStepContext& _context, Block& _ast);
54
55
56
  using ASTModifier::operator();
57
  void operator()(Block& _block) override;
58
59
  // @returns true iff the code changed in the previous run.
60
0
  bool shouldRunAgain() const { return m_shouldRunAgain; }
61
62
  // Run the pruner until the code does not change anymore.
63
  static void runUntilStabilised(
64
    Dialect const& _dialect,
65
    Block& _ast,
66
    bool _allowMSizeOptimization,
67
    std::map<YulString, SideEffects> const* _functionSideEffects = nullptr,
68
    std::set<YulString> const& _externallyUsedFunctions = {}
69
  );
70
71
  static void run(
72
    Dialect const& _dialect,
73
    Block& _ast,
74
    std::set<YulString> const& _externallyUsedFunctions = {}
75
  )
76
0
  {
77
0
    runUntilStabilisedOnFullAST(_dialect, _ast, _externallyUsedFunctions);
78
0
  }
79
80
  /// Run the pruner until the code does not change anymore.
81
  /// The provided block has to be a full AST.
82
  /// The pruner itself determines if msize is used and which user-defined functions
83
  /// are side-effect free.
84
  static void runUntilStabilisedOnFullAST(
85
    Dialect const& _dialect,
86
    Block& _ast,
87
    std::set<YulString> const& _externallyUsedFunctions = {}
88
  );
89
90
  // Run the pruner until the code does not change anymore.
91
  // Only run on the given function.
92
  // @param _allowMSizeOptimization if true, allows to remove instructions
93
  //        whose only side-effect is a potential change of the return value of
94
  //        the msize instruction.
95
  static void runUntilStabilised(
96
    Dialect const& _dialect,
97
    FunctionDefinition& _functionDefinition,
98
    bool _allowMSizeOptimization,
99
    std::set<YulString> const& _externallyUsedFunctions = {}
100
  );
101
102
private:
103
  UnusedPruner(
104
    Dialect const& _dialect,
105
    Block& _ast,
106
    bool _allowMSizeOptimization,
107
    std::map<YulString, SideEffects> const* _functionSideEffects = nullptr,
108
    std::set<YulString> const& _externallyUsedFunctions = {}
109
  );
110
  UnusedPruner(
111
    Dialect const& _dialect,
112
    FunctionDefinition& _function,
113
    bool _allowMSizeOptimization,
114
    std::set<YulString> const& _externallyUsedFunctions = {}
115
  );
116
117
  bool used(YulString _name) const;
118
  void subtractReferences(std::map<YulString, size_t> const& _subtrahend);
119
120
  Dialect const& m_dialect;
121
  bool m_allowMSizeOptimization = false;
122
  std::map<YulString, SideEffects> const* m_functionSideEffects = nullptr;
123
  bool m_shouldRunAgain = false;
124
  std::map<YulString, size_t> m_references;
125
};
126
127
}