Coverage Report

Created: 2025-09-04 07:34

/src/solidity/libyul/optimiser/Rematerialiser.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 replaces variables by their most recently assigned expressions.
20
 */
21
22
#pragma once
23
24
#include <libyul/optimiser/DataFlowAnalyzer.h>
25
#include <libyul/optimiser/OptimiserStep.h>
26
27
namespace solidity::yul
28
{
29
30
/**
31
 * Optimisation stage that replaces variable references by those expressions
32
 * that are most recently assigned to the referenced variables,
33
 * but only if the expression is movable and one of the following holds:
34
 *  - the variable is referenced exactly once (and definition-to-reference does not cross a loop boundary)
35
 *  - the value is extremely cheap ("cost" of zero like ``caller()``)
36
 *  - the variable is referenced at most 5 times and the value is rather cheap
37
 *    ("cost" of at most 1 like a constant up to 0xff) and we are not in a loop
38
 *
39
 * Prerequisite: Disambiguator, ForLoopInitRewriter.
40
 */
41
class Rematerialiser: public DataFlowAnalyzer
42
{
43
public:
44
  static constexpr char const* name{"Rematerialiser"};
45
  static void run(
46
    OptimiserStepContext& _context,
47
    Block& _ast
48
187k
  ) { run(_context.dialect, _ast); }
49
50
  static void run(
51
    Dialect const& _dialect,
52
    Block& _ast,
53
    std::set<YulName> _varsToAlwaysRematerialize = {},
54
    bool _onlySelectedVariables = false
55
  );
56
57
protected:
58
  Rematerialiser(
59
    Dialect const& _dialect,
60
    Block& _ast,
61
    std::set<YulName> _varsToAlwaysRematerialize = {},
62
    bool _onlySelectedVariables = false
63
  );
64
65
  using DataFlowAnalyzer::operator();
66
67
  using ASTModifier::visit;
68
  void visit(Expression& _e) override;
69
70
  std::map<YulName, size_t> m_referenceCounts;
71
  std::set<YulName> m_varsToAlwaysRematerialize;
72
  bool m_onlySelectedVariables = false;
73
};
74
75
/**
76
 * If a variable is referenced that is known to have a literal
77
 * value at that point, replace it by a literal.
78
 *
79
 * This is mostly used so that other components do not have to rely
80
 * on the data flow analyzer.
81
 *
82
 * Prerequisite: Disambiguator, ForLoopInitRewriter.
83
 */
84
class LiteralRematerialiser: public DataFlowAnalyzer
85
{
86
public:
87
  static constexpr char const* name{"LiteralRematerialiser"};
88
  static void run(
89
    OptimiserStepContext& _context,
90
    Block& _ast
91
608k
  ) { LiteralRematerialiser{_context.dialect}(_ast); }
92
93
  using ASTModifier::visit;
94
  void visit(Expression& _e) override;
95
96
private:
97
  LiteralRematerialiser(Dialect const& _dialect):
98
    DataFlowAnalyzer(_dialect, MemoryAndStorage::Ignore)
99
608k
  {}
100
};
101
102
103
}