/src/solidity/libyul/optimiser/Suite.cpp
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 suite that combines all steps and also provides the settings for the heuristics.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include <libyul/optimiser/Suite.h>  | 
23  |  |  | 
24  |  | #include <libyul/optimiser/Disambiguator.h>  | 
25  |  | #include <libyul/optimiser/VarDeclInitializer.h>  | 
26  |  | #include <libyul/optimiser/BlockFlattener.h>  | 
27  |  | #include <libyul/optimiser/CallGraphGenerator.h>  | 
28  |  | #include <libyul/optimiser/CircularReferencesPruner.h>  | 
29  |  | #include <libyul/optimiser/ControlFlowSimplifier.h>  | 
30  |  | #include <libyul/optimiser/ConditionalSimplifier.h>  | 
31  |  | #include <libyul/optimiser/ConditionalUnsimplifier.h>  | 
32  |  | #include <libyul/optimiser/DeadCodeEliminator.h>  | 
33  |  | #include <libyul/optimiser/FunctionGrouper.h>  | 
34  |  | #include <libyul/optimiser/FunctionHoister.h>  | 
35  |  | #include <libyul/optimiser/EqualStoreEliminator.h>  | 
36  |  | #include <libyul/optimiser/EquivalentFunctionCombiner.h>  | 
37  |  | #include <libyul/optimiser/ExpressionSplitter.h>  | 
38  |  | #include <libyul/optimiser/ExpressionJoiner.h>  | 
39  |  | #include <libyul/optimiser/ExpressionInliner.h>  | 
40  |  | #include <libyul/optimiser/FullInliner.h>  | 
41  |  | #include <libyul/optimiser/ForLoopConditionIntoBody.h>  | 
42  |  | #include <libyul/optimiser/ForLoopConditionOutOfBody.h>  | 
43  |  | #include <libyul/optimiser/ForLoopInitRewriter.h>  | 
44  |  | #include <libyul/optimiser/ForLoopConditionIntoBody.h>  | 
45  |  | #include <libyul/optimiser/FunctionSpecializer.h>  | 
46  |  | #include <libyul/optimiser/ReasoningBasedSimplifier.h>  | 
47  |  | #include <libyul/optimiser/Rematerialiser.h>  | 
48  |  | #include <libyul/optimiser/UnusedFunctionParameterPruner.h>  | 
49  |  | #include <libyul/optimiser/UnusedPruner.h>  | 
50  |  | #include <libyul/optimiser/ExpressionSimplifier.h>  | 
51  |  | #include <libyul/optimiser/CommonSubexpressionEliminator.h>  | 
52  |  | #include <libyul/optimiser/Semantics.h>  | 
53  |  | #include <libyul/optimiser/SSAReverser.h>  | 
54  |  | #include <libyul/optimiser/SSATransform.h>  | 
55  |  | #include <libyul/optimiser/StackCompressor.h>  | 
56  |  | #include <libyul/optimiser/StackLimitEvader.h>  | 
57  |  | #include <libyul/optimiser/StructuralSimplifier.h>  | 
58  |  | #include <libyul/optimiser/SyntacticalEquality.h>  | 
59  |  | #include <libyul/optimiser/UnusedAssignEliminator.h>  | 
60  |  | #include <libyul/optimiser/UnusedStoreEliminator.h>  | 
61  |  | #include <libyul/optimiser/VarNameCleaner.h>  | 
62  |  | #include <libyul/optimiser/LoadResolver.h>  | 
63  |  | #include <libyul/optimiser/LoopInvariantCodeMotion.h>  | 
64  |  | #include <libyul/optimiser/Metrics.h>  | 
65  |  | #include <libyul/optimiser/NameSimplifier.h>  | 
66  |  | #include <libyul/backends/evm/ConstantOptimiser.h>  | 
67  |  | #include <libyul/AsmAnalysis.h>  | 
68  |  | #include <libyul/AsmAnalysisInfo.h>  | 
69  |  | #include <libyul/AsmPrinter.h>  | 
70  |  | #include <libyul/AST.h>  | 
71  |  | #include <libyul/Object.h>  | 
72  |  |  | 
73  |  | #include <libyul/backends/wasm/WasmDialect.h>  | 
74  |  | #include <libyul/backends/evm/NoOutputAssembly.h>  | 
75  |  |  | 
76  |  | #include <libsolutil/CommonData.h>  | 
77  |  |  | 
78  |  | #include <libyul/CompilabilityChecker.h>  | 
79  |  |  | 
80  |  | #include <range/v3/view/map.hpp>  | 
81  |  | #include <range/v3/action/remove.hpp>  | 
82  |  |  | 
83  |  | #include <limits>  | 
84  |  | #include <tuple>  | 
85  |  |  | 
86  |  | using namespace std;  | 
87  |  | using namespace solidity;  | 
88  |  | using namespace solidity::yul;  | 
89  |  |  | 
90  |  | void OptimiserSuite::run(  | 
91  |  |   Dialect const& _dialect,  | 
92  |  |   GasMeter const* _meter,  | 
93  |  |   Object& _object,  | 
94  |  |   bool _optimizeStackAllocation,  | 
95  |  |   string_view _optimisationSequence,  | 
96  |  |   optional<size_t> _expectedExecutionsPerDeployment,  | 
97  |  |   set<YulString> const& _externallyUsedIdentifiers  | 
98  |  | )  | 
99  | 5.08k  | { | 
100  | 5.08k  |   EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect);  | 
101  | 5.08k  |   bool usesOptimizedCodeGenerator =  | 
102  | 5.08k  |     _optimizeStackAllocation &&  | 
103  | 5.08k  |     evmDialect &&  | 
104  | 5.08k  |     evmDialect->evmVersion().canOverchargeGasForCall() &&  | 
105  | 5.08k  |     evmDialect->providesObjectAccess();  | 
106  | 5.08k  |   set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;  | 
107  | 5.08k  |   reservedIdentifiers += _dialect.fixedFunctionNames();  | 
108  |  |  | 
109  | 5.08k  |   *_object.code = std::get<Block>(Disambiguator(  | 
110  | 5.08k  |     _dialect,  | 
111  | 5.08k  |     *_object.analysisInfo,  | 
112  | 5.08k  |     reservedIdentifiers  | 
113  | 5.08k  |   )(*_object.code));  | 
114  | 5.08k  |   Block& ast = *_object.code;  | 
115  |  |  | 
116  | 5.08k  |   NameDispenser dispenser{_dialect, ast, reservedIdentifiers}; | 
117  | 5.08k  |   OptimiserStepContext context{_dialect, dispenser, reservedIdentifiers, _expectedExecutionsPerDeployment}; | 
118  |  |  | 
119  | 5.08k  |   OptimiserSuite suite(context, Debug::None);  | 
120  |  |  | 
121  |  |   // Some steps depend on properties ensured by FunctionHoister, BlockFlattener, FunctionGrouper and  | 
122  |  |   // ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.  | 
123  | 5.08k  |   suite.runSequence("hgfo", ast); | 
124  |  |  | 
125  | 5.08k  |   NameSimplifier::run(suite.m_context, ast);  | 
126  |  |   // Now the user-supplied part  | 
127  | 5.08k  |   suite.runSequence(_optimisationSequence, ast);  | 
128  |  |  | 
129  |  |   // This is a tuning parameter, but actually just prevents infinite loops.  | 
130  | 5.08k  |   size_t stackCompressorMaxIterations = 16;  | 
131  | 5.08k  |   suite.runSequence("g", ast); | 
132  |  |  | 
133  |  |   // We ignore the return value because we will get a much better error  | 
134  |  |   // message once we perform code generation.  | 
135  | 5.08k  |   if (!usesOptimizedCodeGenerator)  | 
136  | 2.78k  |     StackCompressor::run(  | 
137  | 2.78k  |       _dialect,  | 
138  | 2.78k  |       _object,  | 
139  | 2.78k  |       _optimizeStackAllocation,  | 
140  | 2.78k  |       stackCompressorMaxIterations  | 
141  | 2.78k  |     );  | 
142  | 5.08k  |   suite.runSequence("fDnTOc g", ast); | 
143  |  |  | 
144  | 5.08k  |   if (evmDialect)  | 
145  | 5.08k  |   { | 
146  | 5.08k  |     yulAssert(_meter, "");  | 
147  | 5.08k  |     ConstantOptimiser{*evmDialect, *_meter}(ast); | 
148  | 5.08k  |     if (usesOptimizedCodeGenerator)  | 
149  | 2.30k  |     { | 
150  | 2.30k  |       StackCompressor::run(  | 
151  | 2.30k  |         _dialect,  | 
152  | 2.30k  |         _object,  | 
153  | 2.30k  |         _optimizeStackAllocation,  | 
154  | 2.30k  |         stackCompressorMaxIterations  | 
155  | 2.30k  |       );  | 
156  | 2.30k  |       if (evmDialect->providesObjectAccess())  | 
157  | 2.30k  |         StackLimitEvader::run(suite.m_context, _object);  | 
158  | 2.30k  |     }  | 
159  | 2.78k  |     else if (evmDialect->providesObjectAccess() && _optimizeStackAllocation)  | 
160  | 592  |       StackLimitEvader::run(suite.m_context, _object);  | 
161  | 5.08k  |   }  | 
162  | 0  |   else if (dynamic_cast<WasmDialect const*>(&_dialect))  | 
163  | 0  |   { | 
164  |  |     // If the first statement is an empty block, remove it.  | 
165  |  |     // We should only have function definitions after that.  | 
166  | 0  |     if (ast.statements.size() > 1 && std::get<Block>(ast.statements.front()).statements.empty())  | 
167  | 0  |       ast.statements.erase(ast.statements.begin());  | 
168  | 0  |   }  | 
169  |  |  | 
170  | 5.08k  |   dispenser.reset(ast);  | 
171  | 5.08k  |   NameSimplifier::run(suite.m_context, ast);  | 
172  | 5.08k  |   VarNameCleaner::run(suite.m_context, ast);  | 
173  |  |  | 
174  | 5.08k  |   *_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);  | 
175  | 5.08k  | }  | 
176  |  |  | 
177  |  | namespace  | 
178  |  | { | 
179  |  |  | 
180  |  |  | 
181  |  | template <class... Step>  | 
182  |  | map<string, unique_ptr<OptimiserStep>> optimiserStepCollection()  | 
183  | 1  | { | 
184  | 1  |   map<string, unique_ptr<OptimiserStep>> ret;  | 
185  | 1  |   for (unique_ptr<OptimiserStep>& s: util::make_vector<unique_ptr<OptimiserStep>>(  | 
186  | 1  |     (make_unique<OptimiserStepInstance<Step>>())...  | 
187  | 1  |   ))  | 
188  | 33  |   { | 
189  | 33  |     yulAssert(!ret.count(s->name), "");  | 
190  | 33  |     ret[s->name] = std::move(s);  | 
191  | 33  |   }  | 
192  | 1  |   return ret;  | 
193  | 1  | }  | 
194  |  |  | 
195  |  | }  | 
196  |  |  | 
197  |  | map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()  | 
198  | 2.42M  | { | 
199  | 2.42M  |   static map<string, unique_ptr<OptimiserStep>> instance;  | 
200  | 2.42M  |   if (instance.empty())  | 
201  | 1  |     instance = optimiserStepCollection<  | 
202  | 1  |       BlockFlattener,  | 
203  | 1  |       CircularReferencesPruner,  | 
204  | 1  |       CommonSubexpressionEliminator,  | 
205  | 1  |       ConditionalSimplifier,  | 
206  | 1  |       ConditionalUnsimplifier,  | 
207  | 1  |       ControlFlowSimplifier,  | 
208  | 1  |       DeadCodeEliminator,  | 
209  | 1  |       EqualStoreEliminator,  | 
210  | 1  |       EquivalentFunctionCombiner,  | 
211  | 1  |       ExpressionInliner,  | 
212  | 1  |       ExpressionJoiner,  | 
213  | 1  |       ExpressionSimplifier,  | 
214  | 1  |       ExpressionSplitter,  | 
215  | 1  |       ForLoopConditionIntoBody,  | 
216  | 1  |       ForLoopConditionOutOfBody,  | 
217  | 1  |       ForLoopInitRewriter,  | 
218  | 1  |       FullInliner,  | 
219  | 1  |       FunctionGrouper,  | 
220  | 1  |       FunctionHoister,  | 
221  | 1  |       FunctionSpecializer,  | 
222  | 1  |       LiteralRematerialiser,  | 
223  | 1  |       LoadResolver,  | 
224  | 1  |       LoopInvariantCodeMotion,  | 
225  | 1  |       UnusedAssignEliminator,  | 
226  | 1  |       UnusedStoreEliminator,  | 
227  | 1  |       ReasoningBasedSimplifier,  | 
228  | 1  |       Rematerialiser,  | 
229  | 1  |       SSAReverser,  | 
230  | 1  |       SSATransform,  | 
231  | 1  |       StructuralSimplifier,  | 
232  | 1  |       UnusedFunctionParameterPruner,  | 
233  | 1  |       UnusedPruner,  | 
234  | 1  |       VarDeclInitializer  | 
235  | 1  |     >();  | 
236  |  |   // Does not include VarNameCleaner because it destroys the property of unique names.  | 
237  |  |   // Does not include NameSimplifier.  | 
238  | 2.42M  |   return instance;  | 
239  | 2.42M  | }  | 
240  |  |  | 
241  |  | map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()  | 
242  | 1  | { | 
243  | 1  |   static map<string, char> lookupTable{ | 
244  | 1  |     {BlockFlattener::name,                'f'}, | 
245  | 1  |     {CircularReferencesPruner::name,      'l'}, | 
246  | 1  |     {CommonSubexpressionEliminator::name, 'c'}, | 
247  | 1  |     {ConditionalSimplifier::name,         'C'}, | 
248  | 1  |     {ConditionalUnsimplifier::name,       'U'}, | 
249  | 1  |     {ControlFlowSimplifier::name,         'n'}, | 
250  | 1  |     {DeadCodeEliminator::name,            'D'}, | 
251  | 1  |     {EqualStoreEliminator::name,          'E'}, | 
252  | 1  |     {EquivalentFunctionCombiner::name,    'v'}, | 
253  | 1  |     {ExpressionInliner::name,             'e'}, | 
254  | 1  |     {ExpressionJoiner::name,              'j'}, | 
255  | 1  |     {ExpressionSimplifier::name,          's'}, | 
256  | 1  |     {ExpressionSplitter::name,            'x'}, | 
257  | 1  |     {ForLoopConditionIntoBody::name,      'I'}, | 
258  | 1  |     {ForLoopConditionOutOfBody::name,     'O'}, | 
259  | 1  |     {ForLoopInitRewriter::name,           'o'}, | 
260  | 1  |     {FullInliner::name,                   'i'}, | 
261  | 1  |     {FunctionGrouper::name,               'g'}, | 
262  | 1  |     {FunctionHoister::name,               'h'}, | 
263  | 1  |     {FunctionSpecializer::name,           'F'}, | 
264  | 1  |     {LiteralRematerialiser::name,         'T'}, | 
265  | 1  |     {LoadResolver::name,                  'L'}, | 
266  | 1  |     {LoopInvariantCodeMotion::name,       'M'}, | 
267  | 1  |     {ReasoningBasedSimplifier::name,      'R'}, | 
268  | 1  |     {UnusedAssignEliminator::name,        'r'}, | 
269  | 1  |     {UnusedStoreEliminator::name,         'S'}, | 
270  | 1  |     {Rematerialiser::name,                'm'}, | 
271  | 1  |     {SSAReverser::name,                   'V'}, | 
272  | 1  |     {SSATransform::name,                  'a'}, | 
273  | 1  |     {StructuralSimplifier::name,          't'}, | 
274  | 1  |     {UnusedFunctionParameterPruner::name, 'p'}, | 
275  | 1  |     {UnusedPruner::name,                  'u'}, | 
276  | 1  |     {VarDeclInitializer::name,            'd'}, | 
277  | 1  |   };  | 
278  | 1  |   yulAssert(lookupTable.size() == allSteps().size(), "");  | 
279  | 1  |   yulAssert((  | 
280  | 1  |       util::convertContainer<set<char>>(string(NonStepAbbreviations)) -  | 
281  | 1  |       util::convertContainer<set<char>>(lookupTable | ranges::views::values)  | 
282  | 1  |     ).size() == string(NonStepAbbreviations).size(),  | 
283  | 1  |     "Step abbreviation conflicts with a character reserved for another syntactic element"  | 
284  | 1  |   );  | 
285  |  |  | 
286  | 1  |   return lookupTable;  | 
287  | 1  | }  | 
288  |  |  | 
289  |  | map<char, string> const& OptimiserSuite::stepAbbreviationToNameMap()  | 
290  | 4.64M  | { | 
291  | 4.64M  |   static map<char, string> lookupTable = util::invertMap(stepNameToAbbreviationMap());  | 
292  |  |  | 
293  | 4.64M  |   return lookupTable;  | 
294  | 4.64M  | }  | 
295  |  |  | 
296  |  | void OptimiserSuite::validateSequence(string_view _stepAbbreviations)  | 
297  | 108k  | { | 
298  | 108k  |   int8_t nestingLevel = 0;  | 
299  | 108k  |   for (char abbreviation: _stepAbbreviations)  | 
300  | 1.27M  |     switch (abbreviation)  | 
301  | 1.27M  |     { | 
302  | 25.4k  |     case ' ':  | 
303  | 25.4k  |     case '\n':  | 
304  | 25.4k  |       break;  | 
305  | 71.2k  |     case '[':  | 
306  | 71.2k  |       assertThrow(nestingLevel < numeric_limits<int8_t>::max(), OptimizerException, "Brackets nested too deep");  | 
307  | 71.2k  |       nestingLevel++;  | 
308  | 71.2k  |       break;  | 
309  | 71.2k  |     case ']':  | 
310  | 71.2k  |       nestingLevel--;  | 
311  | 71.2k  |       assertThrow(nestingLevel >= 0, OptimizerException, "Unbalanced brackets");  | 
312  | 71.2k  |       break;  | 
313  | 1.11M  |     default:  | 
314  | 1.11M  |     { | 
315  | 1.11M  |       yulAssert(  | 
316  | 1.11M  |         string(NonStepAbbreviations).find(abbreviation) == string::npos,  | 
317  | 1.11M  |         "Unhandled syntactic element in the abbreviation sequence"  | 
318  | 1.11M  |       );  | 
319  | 1.11M  |       assertThrow(  | 
320  | 1.11M  |         stepAbbreviationToNameMap().find(abbreviation) != stepAbbreviationToNameMap().end(),  | 
321  | 1.11M  |         OptimizerException,  | 
322  | 1.11M  |         "'"s + abbreviation + "' is not a valid step abbreviation"  | 
323  | 1.11M  |       );  | 
324  | 1.11M  |       optional<string> invalid = allSteps().at(stepAbbreviationToNameMap().at(abbreviation))->invalidInCurrentEnvironment();  | 
325  | 1.11M  |       assertThrow(  | 
326  | 1.11M  |         !invalid.has_value(),  | 
327  | 1.11M  |         OptimizerException,  | 
328  | 1.11M  |         "'"s + abbreviation + "' is invalid in the current environment: " + *invalid  | 
329  | 1.11M  |       );  | 
330  | 1.11M  |     }  | 
331  | 1.27M  |     }  | 
332  | 108k  |   assertThrow(nestingLevel == 0, OptimizerException, "Unbalanced brackets");  | 
333  | 108k  | }  | 
334  |  |  | 
335  |  | void OptimiserSuite::runSequence(string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable)  | 
336  | 108k  | { | 
337  | 108k  |   validateSequence(_stepAbbreviations);  | 
338  |  |  | 
339  |  |   // This splits 'aaa[bbb]ccc...' into 'aaa' and '[bbb]ccc...'.  | 
340  | 108k  |   auto extractNonNestedPrefix = [](string_view _tail) -> tuple<string_view, string_view>  | 
341  | 149k  |   { | 
342  | 848k  |     for (size_t i = 0; i < _tail.size(); ++i)  | 
343  | 739k  |     { | 
344  | 739k  |       yulAssert(_tail[i] != ']');  | 
345  | 739k  |       if (_tail[i] == '[')  | 
346  | 40.7k  |         return {_tail.substr(0, i), _tail.substr(i)}; | 
347  | 739k  |     }  | 
348  | 108k  |     return {_tail, {}}; | 
349  | 149k  |   };  | 
350  |  |  | 
351  |  |   // This splits '[bbb]ccc...' into 'bbb' and 'ccc...'.  | 
352  | 108k  |   auto extractBracketContent = [](string_view _tail) -> tuple<string_view, string_view>  | 
353  | 108k  |   { | 
354  | 40.7k  |     yulAssert(!_tail.empty() && _tail[0] == '[');  | 
355  |  |  | 
356  | 40.7k  |     size_t contentLength = 0;  | 
357  | 40.7k  |     int8_t nestingLevel = 1;  | 
358  | 40.7k  |     for (char abbreviation: _tail.substr(1))  | 
359  | 539k  |     { | 
360  | 539k  |       if (abbreviation == '[')  | 
361  | 30.5k  |       { | 
362  | 30.5k  |         yulAssert(nestingLevel < numeric_limits<int8_t>::max());  | 
363  | 30.5k  |         ++nestingLevel;  | 
364  | 30.5k  |       }  | 
365  | 508k  |       else if (abbreviation == ']')  | 
366  | 71.2k  |       { | 
367  | 71.2k  |         --nestingLevel;  | 
368  | 71.2k  |         if (nestingLevel == 0)  | 
369  | 40.7k  |           break;  | 
370  | 71.2k  |       }  | 
371  | 498k  |       ++contentLength;  | 
372  | 498k  |     }  | 
373  | 40.7k  |     yulAssert(nestingLevel == 0);  | 
374  | 40.7k  |     yulAssert(_tail[contentLength + 1] == ']');  | 
375  |  |  | 
376  | 40.7k  |     return {_tail.substr(1, contentLength), _tail.substr(contentLength + 2)}; | 
377  | 40.7k  |   };  | 
378  |  |  | 
379  | 108k  |   auto abbreviationsToSteps = [](string_view _sequence) -> vector<string>  | 
380  | 285k  |   { | 
381  | 285k  |     vector<string> steps;  | 
382  | 285k  |     for (char abbreviation: _sequence)  | 
383  | 1.34M  |       if (abbreviation != ' ' && abbreviation != '\n')  | 
384  | 1.31M  |         steps.emplace_back(stepAbbreviationToNameMap().at(abbreviation));  | 
385  | 285k  |     return steps;  | 
386  | 285k  |   };  | 
387  |  |  | 
388  | 108k  |   vector<tuple<string_view, bool>> subsequences;  | 
389  | 108k  |   string_view tail = _stepAbbreviations;  | 
390  | 149k  |   while (!tail.empty())  | 
391  | 149k  |   { | 
392  | 149k  |     string_view subsequence;  | 
393  | 149k  |     tie(subsequence, tail) = extractNonNestedPrefix(tail);  | 
394  | 149k  |     if (subsequence.size() > 0)  | 
395  | 149k  |       subsequences.push_back({subsequence, false}); | 
396  |  |  | 
397  | 149k  |     if (tail.empty())  | 
398  | 108k  |       break;  | 
399  |  |  | 
400  | 40.7k  |     tie(subsequence, tail) = extractBracketContent(tail);  | 
401  | 40.7k  |     if (subsequence.size() > 0)  | 
402  | 40.7k  |       subsequences.push_back({subsequence, true}); | 
403  | 40.7k  |   }  | 
404  |  |  | 
405  | 108k  |   size_t codeSize = 0;  | 
406  | 196k  |   for (size_t round = 0; round < MaxRounds; ++round)  | 
407  | 196k  |   { | 
408  | 196k  |     for (auto const& [subsequence, repeat]: subsequences)  | 
409  | 373k  |     { | 
410  | 373k  |       if (repeat)  | 
411  | 88.0k  |         runSequence(subsequence, _ast, true);  | 
412  | 285k  |       else  | 
413  | 285k  |         runSequence(abbreviationsToSteps(subsequence), _ast);  | 
414  | 373k  |     }  | 
415  |  |  | 
416  | 196k  |     if (!_repeatUntilStable)  | 
417  | 20.3k  |       break;  | 
418  |  |  | 
419  | 176k  |     size_t newSize = CodeSize::codeSizeIncludingFunctions(_ast);  | 
420  | 176k  |     if (newSize == codeSize)  | 
421  | 88.0k  |       break;  | 
422  | 88.5k  |     codeSize = newSize;  | 
423  | 88.5k  |   }  | 
424  | 108k  | }  | 
425  |  |  | 
426  |  | void OptimiserSuite::runSequence(std::vector<string> const& _steps, Block& _ast)  | 
427  | 285k  | { | 
428  | 285k  |   unique_ptr<Block> copy;  | 
429  | 285k  |   if (m_debug == Debug::PrintChanges)  | 
430  | 0  |     copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast))); | 
431  | 285k  |   for (string const& step: _steps)  | 
432  | 1.31M  |   { | 
433  | 1.31M  |     if (m_debug == Debug::PrintStep)  | 
434  | 0  |       cout << "Running " << step << endl;  | 
435  | 1.31M  |     allSteps().at(step)->run(m_context, _ast);  | 
436  | 1.31M  |     if (m_debug == Debug::PrintChanges)  | 
437  | 0  |     { | 
438  |  |       // TODO should add switch to also compare variable names!  | 
439  | 0  |       if (SyntacticallyEqual{}.statementEqual(_ast, *copy)) | 
440  | 0  |         cout << "== Running " << step << " did not cause changes." << endl;  | 
441  | 0  |       else  | 
442  | 0  |       { | 
443  | 0  |         cout << "== Running " << step << " changed the AST." << endl;  | 
444  | 0  |         cout << AsmPrinter{}(_ast) << endl; | 
445  | 0  |         copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast))); | 
446  | 0  |       }  | 
447  | 0  |     }  | 
448  | 1.31M  |   }  | 
449  | 285k  | }  |