/src/solidity/libyul/optimiser/ExpressionSplitter.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 | | * Optimiser component that turns complex expressions into multiple variable |
20 | | * declarations. |
21 | | */ |
22 | | #pragma once |
23 | | |
24 | | #include <libyul/ASTForward.h> |
25 | | |
26 | | #include <libyul/optimiser/ASTWalker.h> |
27 | | #include <libyul/optimiser/NameDispenser.h> |
28 | | |
29 | | #include <vector> |
30 | | |
31 | | namespace solidity::yul |
32 | | { |
33 | | |
34 | | struct Dialect; |
35 | | struct OptimiserStepContext; |
36 | | class TypeInfo; |
37 | | |
38 | | /** |
39 | | * Optimiser component that modifies an AST in place, turning complex |
40 | | * expressions into simple expressions and multiple variable declarations. |
41 | | * |
42 | | * Code of the form |
43 | | * |
44 | | * sstore(mul(x, 4), mload(y)) |
45 | | * |
46 | | * is transformed into |
47 | | * |
48 | | * let a1 := mload(y) |
49 | | * let a2 := mul(x, 4) |
50 | | * sstore(a2, a1) |
51 | | * |
52 | | * The transformation is not applied to loop conditions, because the loop control flow |
53 | | * does not allow "outlining" the inner expressions in all cases. |
54 | | * |
55 | | * The final program should be in a form such that with the exception of a loop condition, |
56 | | * function calls can only appear in the right-hand side of a variable declaration, |
57 | | * assignments or expression statements and all arguments have to be constants or variables. |
58 | | */ |
59 | | class ExpressionSplitter: public ASTModifier |
60 | | { |
61 | | public: |
62 | | static constexpr char const* name{"ExpressionSplitter"}; |
63 | | static void run(OptimiserStepContext&, Block& _ast); |
64 | | |
65 | | void operator()(FunctionCall&) override; |
66 | | void operator()(If&) override; |
67 | | void operator()(Switch&) override; |
68 | | void operator()(ForLoop&) override; |
69 | | void operator()(Block& _block) override; |
70 | | |
71 | | private: |
72 | | explicit ExpressionSplitter( |
73 | | Dialect const& _dialect, |
74 | | NameDispenser& _nameDispenser, |
75 | | TypeInfo& _typeInfo |
76 | | ): |
77 | | m_dialect(_dialect), |
78 | | m_nameDispenser(_nameDispenser), |
79 | | m_typeInfo(_typeInfo) |
80 | 101k | { } |
81 | | |
82 | | /// Replaces the expression by a variable if it is a function call or functional |
83 | | /// instruction. The declaration of the variable is appended to m_statementsToPrefix. |
84 | | /// Recurses via visit(). |
85 | | void outlineExpression(Expression& _expr); |
86 | | |
87 | | /// List of statements that should go in front of the currently visited AST element, |
88 | | /// at the statement level. |
89 | | std::vector<Statement> m_statementsToPrefix; |
90 | | Dialect const& m_dialect; |
91 | | NameDispenser& m_nameDispenser; |
92 | | TypeInfo& m_typeInfo; |
93 | | }; |
94 | | |
95 | | } |