/src/solidity/libyul/optimiser/ForLoopConditionIntoBody.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 | | #pragma once |
19 | | |
20 | | #include <libyul/optimiser/ASTWalker.h> |
21 | | #include <libyul/Dialect.h> |
22 | | |
23 | | namespace solidity::yul |
24 | | { |
25 | | |
26 | | struct OptimiserStepContext; |
27 | | |
28 | | /** |
29 | | * Rewrites ForLoop by moving iteration condition into the ForLoop body. |
30 | | * For example, `for {} lt(a, b) {} { mstore(1, 2) }` will become |
31 | | * `for {} 1 {} { if iszero(lt(a, b)) { break } mstore(1, 2) }` |
32 | | * |
33 | | * By moving the iteration check part into the ForLoop body, we can apply expression splitter |
34 | | * to the condition expression. |
35 | | * |
36 | | * This rewriter will skip loops that already have literal constant as iteration condition. |
37 | | * |
38 | | * Requirements: |
39 | | * - The Disambiguator must be run upfront. |
40 | | * - To avoid unnecessary rewrite, it is recommended to run this rewriter after StructuralSimplifier. |
41 | | * - Only works for dialects with a builtin boolean negation function. |
42 | | */ |
43 | | class ForLoopConditionIntoBody: public ASTModifier |
44 | | { |
45 | | public: |
46 | | static constexpr char const* name{"ForLoopConditionIntoBody"}; |
47 | | static void run(OptimiserStepContext&, Block& _ast); |
48 | | |
49 | | using ASTModifier::operator(); |
50 | | void operator()(ForLoop& _forLoop) override; |
51 | | |
52 | | private: |
53 | 194k | ForLoopConditionIntoBody(Dialect const& _dialect): m_dialect(_dialect) {} |
54 | | |
55 | | Dialect const& m_dialect; |
56 | | }; |
57 | | |
58 | | } |