/src/solidity/libyul/optimiser/Suite.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 suite that combines all steps and also provides the settings for the heuristics. |
20 | | */ |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <libyul/ASTForward.h> |
25 | | #include <libyul/YulName.h> |
26 | | #include <libyul/optimiser/OptimiserStep.h> |
27 | | #include <libyul/optimiser/NameDispenser.h> |
28 | | #include <liblangutil/EVMVersion.h> |
29 | | |
30 | | #include <set> |
31 | | #include <string> |
32 | | #include <string_view> |
33 | | #include <memory> |
34 | | |
35 | | namespace solidity::yul |
36 | | { |
37 | | |
38 | | struct AsmAnalysisInfo; |
39 | | class Dialect; |
40 | | class GasMeter; |
41 | | class Object; |
42 | | |
43 | | /** |
44 | | * Optimiser suite that combines all steps and also provides the settings for the heuristics. |
45 | | * Only optimizes the code of the provided object, does not descend into the sub-objects. |
46 | | */ |
47 | | class OptimiserSuite |
48 | | { |
49 | | public: |
50 | | static constexpr size_t MaxRounds = 12; |
51 | | |
52 | | /// Special characters that do not represent optimiser steps but are allowed in abbreviation sequences. |
53 | | /// Some of them (like whitespace) are ignored, others (like brackets) are a part of the syntax. |
54 | | static constexpr char NonStepAbbreviations[] = " \n[]:"; |
55 | | |
56 | | enum class Debug |
57 | | { |
58 | | None, |
59 | | PrintStep, |
60 | | PrintChanges |
61 | | }; |
62 | 82.4k | OptimiserSuite(OptimiserStepContext& _context, Debug _debug = Debug::None): m_context(_context), m_debug(_debug) {} |
63 | | |
64 | | /// The value nullopt for `_expectedExecutionsPerDeployment` represents creation code. |
65 | | static void run( |
66 | | GasMeter const* _meter, |
67 | | Object& _object, |
68 | | bool _optimizeStackAllocation, |
69 | | std::string_view _optimisationSequence, |
70 | | std::string_view _optimisationCleanupSequence, |
71 | | std::optional<size_t> _expectedExecutionsPerDeployment, |
72 | | std::set<YulName> const& _externallyUsedIdentifiers = {} |
73 | | ); |
74 | | |
75 | | /// Ensures that specified sequence of step abbreviations is well-formed and can be executed. |
76 | | /// @throw OptimizerException if the sequence is invalid |
77 | | static void validateSequence(std::string_view _stepAbbreviations); |
78 | | /// Check whether the provided sequence is empty provided that the allowed characters are |
79 | | /// whitespace, newline and : |
80 | | static bool isEmptyOptimizerSequence(std::string const& _sequence); |
81 | | |
82 | | |
83 | | void runSequence(std::vector<std::string> const& _steps, Block& _ast); |
84 | | void runSequence(std::string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable = false); |
85 | | |
86 | | static std::map<std::string, std::unique_ptr<OptimiserStep>> const& allSteps(); |
87 | | static std::map<std::string, char> const& stepNameToAbbreviationMap(); |
88 | | static std::map<char, std::string> const& stepAbbreviationToNameMap(); |
89 | | |
90 | | private: |
91 | | OptimiserStepContext& m_context; |
92 | | Debug m_debug; |
93 | | }; |
94 | | |
95 | | } |