Coverage Report

Created: 2026-09-14 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/libevmasm/SimplificationRules.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
 * @file SimplificationRules
20
 * @author Christian <chris@ethereum.org>
21
 * @date 2017
22
 * Module for applying replacement rules against Expressions.
23
 */
24
25
#pragma once
26
27
#include <libevmasm/ExpressionClasses.h>
28
#include <libevmasm/SimplificationRule.h>
29
30
#include <libsolutil/CommonData.h>
31
32
#include <functional>
33
#include <vector>
34
35
namespace solidity::langutil
36
{
37
struct SourceLocation;
38
}
39
40
namespace solidity::evmasm
41
{
42
43
class Pattern;
44
45
/**
46
 * Container for all simplification rules.
47
 */
48
class Rules
49
{
50
public:
51
  /// Noncopyable.
52
  Rules(Rules const&) = delete;
53
  Rules& operator=(Rules const&) = delete;
54
55
  Rules();
56
57
  /// @returns a pointer to the first matching pattern and sets the match
58
  /// groups accordingly.
59
  SimplificationRule<Pattern> const* findFirstMatch(
60
    Expression const& _expr,
61
    ExpressionClasses const& _classes
62
  );
63
64
  /// Checks whether the rulelist is non-empty. This is usually enforced
65
  /// by the constructor, but we had some issues with static initialization.
66
  bool isInitialized() const;
67
68
private:
69
  void addRules(std::vector<SimplificationRule<Pattern>> const& _rules);
70
  void addRule(SimplificationRule<Pattern> const& _rule);
71
72
59.3M
  void resetMatchGroups() { m_matchGroups.clear(); }
73
74
  std::map<unsigned, Expression const*> m_matchGroups;
75
  /// Pattern to match, replacement to be applied and flag indicating whether
76
  /// the replacement might remove some elements (except constants).
77
  std::vector<SimplificationRule<Pattern>> m_rules[256];
78
};
79
80
/**
81
 * Pattern to match against an expression.
82
 * Also stores matched expressions to retrieve them later, for constructing new expressions using
83
 * ExpressionTemplate.
84
 */
85
class Pattern
86
{
87
public:
88
  using Id = ExpressionClasses::Id;
89
90
  using Builtins = evmasm::EVMBuiltins<Pattern>;
91
  static constexpr size_t WordSize = 256;
92
  using Word = u256;
93
94
  // Matches a specific constant value.
95
0
  Pattern(unsigned _value): Pattern(u256(_value)) {}
96
314
  Pattern(int _value): Pattern(u256(_value)) {}
97
0
  Pattern(long unsigned _value): Pattern(u256(_value)) {}
98
  // Matches a specific constant value.
99
8.09M
  Pattern(u256 const& _value): m_type(Push), m_requireDataMatch(true), m_data(std::make_shared<u256>(_value)) {}
100
  // Matches a specific assembly item type or anything if not given.
101
777k
  Pattern(AssemblyItemType _type = UndefinedItem): m_type(_type) {}
102
  // Matches a given instruction with given arguments
103
  Pattern(Instruction _instruction, std::initializer_list<Pattern> _arguments = {});
104
  /// Sets this pattern to be part of the match group with the identifier @a _group.
105
  /// Inside one rule, all patterns in the same match group have to match expressions from the
106
  /// same expression equivalence class.
107
  void setMatchGroup(unsigned _group, std::map<unsigned, Expression const*>& _matchGroups);
108
6.08M
  unsigned matchGroup() const { return m_matchGroup; }
109
  bool matches(Expression const& _expr, ExpressionClasses const& _classes) const;
110
111
  AssemblyItem toAssemblyItem(langutil::DebugData::ConstPtr _debugData) const;
112
6.08M
  std::vector<Pattern> arguments() const { return m_arguments; }
113
114
  /// @returns the id of the matched expression if this pattern is part of a match group.
115
931k
  Id id() const { return matchGroupValue().id; }
116
  /// @returns the data of the matched expression if this pattern is part of a match group.
117
8.04M
  u256 const& d() const { return matchGroupValue().item->data(); }
118
119
  std::string toString() const;
120
121
1.37k
  AssemblyItemType type() const { return m_type; }
122
  Instruction instruction() const
123
1.37k
  {
124
1.37k
    assertThrow(type() == Operation, OptimizerException, "");
125
1.37k
    return m_instruction;
126
1.37k
  }
127
128
private:
129
  bool matchesBaseItem(AssemblyItem const* _item) const;
130
  Expression const& matchGroupValue() const;
131
  u256 const& data() const;
132
133
  AssemblyItemType m_type;
134
  bool m_requireDataMatch = false;
135
  Instruction m_instruction; ///< Only valid if m_type is Operation
136
  std::shared_ptr<u256> m_data; ///< Only valid if m_type is not Operation
137
  std::vector<Pattern> m_arguments;
138
  unsigned m_matchGroup = 0;
139
  std::map<unsigned, Expression const*>* m_matchGroups = nullptr;
140
};
141
142
/**
143
 * Template for a new expression that can be built from matched patterns.
144
 */
145
struct ExpressionTemplate
146
{
147
  using Id = ExpressionClasses::Id;
148
  explicit ExpressionTemplate(Pattern const& _pattern, langutil::DebugData::ConstPtr const& _debugData);
149
  std::string toString() const;
150
  bool hasId = false;
151
  /// Id of the matched expression, if available.
152
  Id id = Id(-1);
153
  // Otherwise, assembly item.
154
  AssemblyItem item = UndefinedItem;
155
  std::vector<ExpressionTemplate> arguments;
156
};
157
158
}