/src/solidity/libyul/optimiser/ExpressionInliner.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 | | * Optimiser component that performs function inlining. |
20 | | */ |
21 | | #pragma once |
22 | | |
23 | | #include <libyul/optimiser/ASTWalker.h> |
24 | | #include <libyul/ASTForward.h> |
25 | | |
26 | | #include <optional> |
27 | | #include <set> |
28 | | |
29 | | namespace solidity::yul |
30 | | { |
31 | | struct Dialect; |
32 | | struct OptimiserStepContext; |
33 | | |
34 | | /** |
35 | | * Optimiser component that modifies an AST in place, inlining functions that can be |
36 | | * inlined inside functional expressions, i.e. functions that |
37 | | * - return a single value |
38 | | * - have a body like r := <functional expression> |
39 | | * - neither reference themselves nor r in the right hand side |
40 | | * |
41 | | * Furthermore, for all parameters, all of the following need to be true |
42 | | * - the argument is movable |
43 | | * - the parameter is either referenced less than twice in the function body, or the argument is rather cheap |
44 | | * ("cost" of at most 1 like a constant up to 0xff) |
45 | | * |
46 | | * This component can only be used on sources with unique names. |
47 | | */ |
48 | | class ExpressionInliner: public ASTModifier |
49 | | { |
50 | | public: |
51 | | static constexpr char const* name{"ExpressionInliner"}; |
52 | | static void run(OptimiserStepContext&, Block& _ast); |
53 | | |
54 | | using ASTModifier::operator(); |
55 | | void operator()(FunctionDefinition& _fun) override; |
56 | | |
57 | | void visit(Expression& _expression) override; |
58 | | |
59 | | private: |
60 | | ExpressionInliner( |
61 | | Dialect const& _dialect, |
62 | | std::map<YulString, FunctionDefinition const*> const& _inlinableFunctions |
63 | | ): m_dialect(_dialect), m_inlinableFunctions(_inlinableFunctions) |
64 | 0 | {} |
65 | | |
66 | | Dialect const& m_dialect; |
67 | | std::map<YulString, FunctionDefinition const*> const& m_inlinableFunctions; |
68 | | |
69 | | std::map<YulString, YulString> m_varReplacements; |
70 | | /// Set of functions we are currently visiting inside. |
71 | | std::set<YulString> m_currentFunctions; |
72 | | }; |
73 | | |
74 | | } |