/src/solidity/libyul/optimiser/SimplificationRules.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 | | * Module for applying replacement rules against Expressions. |
20 | | */ |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <libevmasm/SimplificationRule.h> |
25 | | |
26 | | #include <libyul/ASTForward.h> |
27 | | #include <libyul/YulString.h> |
28 | | |
29 | | #include <libsolutil/CommonData.h> |
30 | | #include <libsolutil/Numeric.h> |
31 | | |
32 | | #include <liblangutil/EVMVersion.h> |
33 | | #include <liblangutil/SourceLocation.h> |
34 | | |
35 | | #include <functional> |
36 | | #include <optional> |
37 | | #include <vector> |
38 | | |
39 | | namespace solidity::yul |
40 | | { |
41 | | struct Dialect; |
42 | | struct AssignedValue; |
43 | | class Pattern; |
44 | | |
45 | | /** |
46 | | * Container for all simplification rules. |
47 | | */ |
48 | | class SimplificationRules |
49 | | { |
50 | | public: |
51 | | /// Noncopiable. |
52 | | SimplificationRules(SimplificationRules const&) = delete; |
53 | | SimplificationRules& operator=(SimplificationRules const&) = delete; |
54 | | |
55 | | using Rule = evmasm::SimplificationRule<Pattern>; |
56 | | |
57 | | explicit SimplificationRules(std::optional<langutil::EVMVersion> _evmVersion = std::nullopt); |
58 | | |
59 | | /// @returns a pointer to the first matching pattern and sets the match |
60 | | /// groups accordingly. |
61 | | /// @param _ssaValues values of variables that are assigned exactly once. |
62 | | static Rule const* findFirstMatch( |
63 | | Expression const& _expr, |
64 | | Dialect const& _dialect, |
65 | | std::function<AssignedValue const*(YulString)> const& _ssaValues |
66 | | ); |
67 | | |
68 | | /// Checks whether the rulelist is non-empty. This is usually enforced |
69 | | /// by the constructor, but we had some issues with static initialization. |
70 | | bool isInitialized() const; |
71 | | |
72 | | static std::optional<std::pair<evmasm::Instruction, std::vector<Expression> const*>> |
73 | | instructionAndArguments(Dialect const& _dialect, Expression const& _expr); |
74 | | |
75 | | private: |
76 | | void addRules(std::vector<Rule> const& _rules); |
77 | | void addRule(Rule const& _rule); |
78 | | |
79 | 0 | void resetMatchGroups() { m_matchGroups.clear(); } |
80 | | |
81 | | std::map<unsigned, Expression const*> m_matchGroups; |
82 | | std::vector<evmasm::SimplificationRule<Pattern>> m_rules[256]; |
83 | | }; |
84 | | |
85 | | enum class PatternKind |
86 | | { |
87 | | Operation, |
88 | | Constant, |
89 | | Any |
90 | | }; |
91 | | |
92 | | /** |
93 | | * Pattern to match against an expression. |
94 | | * Also stores matched expressions to retrieve them later, for constructing new expressions using |
95 | | * ExpressionTemplate. |
96 | | */ |
97 | | class Pattern |
98 | | { |
99 | | public: |
100 | | using Builtins = evmasm::EVMBuiltins<Pattern>; |
101 | | static constexpr size_t WordSize = 256; |
102 | | using Word = u256; |
103 | | |
104 | | /// Matches any expression. |
105 | 0 | Pattern(PatternKind _kind = PatternKind::Any): m_kind(_kind) {} |
106 | | // Matches a specific constant value. |
107 | 0 | Pattern(unsigned _value): Pattern(u256(_value)) {} |
108 | 0 | Pattern(int _value): Pattern(u256(_value)) {} |
109 | 0 | Pattern(long unsigned _value): Pattern(u256(_value)) {} |
110 | | // Matches a specific constant value. |
111 | 0 | Pattern(u256 const& _value): m_kind(PatternKind::Constant), m_data(std::make_shared<u256>(_value)) {} |
112 | | // Matches a given instruction with given arguments |
113 | | Pattern(evmasm::Instruction _instruction, std::initializer_list<Pattern> _arguments = {}); |
114 | | /// Sets this pattern to be part of the match group with the identifier @a _group. |
115 | | /// Inside one rule, all patterns in the same match group have to match expressions from the |
116 | | /// same expression equivalence class. |
117 | | void setMatchGroup(unsigned _group, std::map<unsigned, Expression const*>& _matchGroups); |
118 | 0 | unsigned matchGroup() const { return m_matchGroup; } |
119 | | bool matches( |
120 | | Expression const& _expr, |
121 | | Dialect const& _dialect, |
122 | | std::function<AssignedValue const*(YulString)> const& _ssaValues |
123 | | ) const; |
124 | | |
125 | 0 | std::vector<Pattern> arguments() const { return m_arguments; } |
126 | | |
127 | | /// @returns the data of the matched expression if this pattern is part of a match group. |
128 | | u256 d() const; |
129 | | |
130 | | evmasm::Instruction instruction() const; |
131 | | |
132 | | /// Turns this pattern into an actual expression. Should only be called |
133 | | /// for patterns resulting from an action, i.e. with match groups assigned. |
134 | | Expression toExpression(std::shared_ptr<DebugData const> const& _debugData) const; |
135 | | |
136 | | private: |
137 | | Expression const& matchGroupValue() const; |
138 | | |
139 | | PatternKind m_kind = PatternKind::Any; |
140 | | evmasm::Instruction m_instruction; ///< Only valid if m_kind is Operation |
141 | | std::shared_ptr<u256> m_data; ///< Only valid if m_kind is Constant |
142 | | std::vector<Pattern> m_arguments; |
143 | | unsigned m_matchGroup = 0; |
144 | | std::map<unsigned, Expression const*>* m_matchGroups = nullptr; |
145 | | }; |
146 | | |
147 | | } |