Coverage Report

Created: 2022-08-24 06:52

/src/solidity/libyul/optimiser/Suite.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 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/YulString.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
struct Dialect;
40
class GasMeter;
41
struct 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
0
  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
    Dialect const& _dialect,
67
    GasMeter const* _meter,
68
    Object& _object,
69
    bool _optimizeStackAllocation,
70
    std::string_view _optimisationSequence,
71
    std::optional<size_t> _expectedExecutionsPerDeployment,
72
    std::set<YulString> 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
79
  void runSequence(std::vector<std::string> const& _steps, Block& _ast);
80
  void runSequence(std::string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable = false);
81
82
  static std::map<std::string, std::unique_ptr<OptimiserStep>> const& allSteps();
83
  static std::map<std::string, char> const& stepNameToAbbreviationMap();
84
  static std::map<char, std::string> const& stepAbbreviationToNameMap();
85
86
private:
87
  OptimiserStepContext& m_context;
88
  Debug m_debug;
89
};
90
91
}