Coverage Report

Created: 2025-09-04 07:34

/src/solidity/libyul/optimiser/UnusedPruner.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
/**
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/YulName.h>
27
28
#include <map>
29
#include <set>
30
31
namespace solidity::yul
32
{
33
class 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
  using ASTModifier::operator();
56
  void operator()(Block& _block) override;
57
58
  // @returns true iff the code changed in the previous run.
59
846k
  bool shouldRunAgain() const { return m_shouldRunAgain; }
60
61
  // Run the pruner until the code does not change anymore.
62
  static void runUntilStabilised(
63
    Dialect const& _dialect,
64
    Block& _ast,
65
    bool _allowMSizeOptimization,
66
    std::map<FunctionHandle, SideEffects> const* _functionSideEffects = nullptr,
67
    std::set<YulName> const& _externallyUsedFunctions = {}
68
  );
69
70
  /// Run the pruner until the code does not change anymore.
71
  /// The provided block has to be a full AST.
72
  /// The pruner itself determines if msize is used and which user-defined functions
73
  /// are side-effect free.
74
  static void runUntilStabilisedOnFullAST(
75
    Dialect const& _dialect,
76
    Block& _ast,
77
    std::set<YulName> const& _externallyUsedFunctions = {}
78
  );
79
80
private:
81
  UnusedPruner(
82
    Dialect const& _dialect,
83
    Block& _ast,
84
    bool _allowMSizeOptimization,
85
    std::map<FunctionHandle, SideEffects> const* _functionSideEffects = nullptr,
86
    std::set<YulName> const& _externallyUsedFunctions = {}
87
  );
88
89
  bool used(YulName _name) const;
90
  void subtractReferences(std::map<FunctionHandle, size_t> const& _subtrahend);
91
92
  Dialect const& m_dialect;
93
  bool m_allowMSizeOptimization = false;
94
  std::map<FunctionHandle, SideEffects> const* m_functionSideEffects = nullptr;
95
  bool m_shouldRunAgain = false;
96
  std::map<FunctionHandle, size_t> m_references;
97
};
98
99
}