forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantOptimiser.h
109 lines (89 loc) · 2.91 KB
/
ConstantOptimiser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Optimisation stage that replaces constants by expressions that compute them.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/YulName.h>
#include <libyul/Dialect.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/ASTForward.h>
#include <liblangutil/DebugData.h>
#include <libsolutil/Common.h>
#include <tuple>
#include <map>
#include <memory>
namespace solidity::yul
{
class Dialect;
class GasMeter;
/**
* Optimisation stage that replaces constants by expressions that compute them.
*
* Prerequisite: None
*/
class ConstantOptimiser: public ASTModifier
{
public:
ConstantOptimiser(EVMDialect const& _dialect, GasMeter const& _meter):
m_dialect(_dialect),
m_meter(_meter)
{}
void visit(Expression& _e) override;
struct Representation
{
std::unique_ptr<Expression> expression;
bigint cost;
};
private:
EVMDialect const& m_dialect;
GasMeter const& m_meter;
std::map<u256, Representation> m_cache;
};
class RepresentationFinder
{
public:
using Representation = ConstantOptimiser::Representation;
RepresentationFinder(
EVMDialect const& _dialect,
GasMeter const& _meter,
langutil::DebugData::ConstPtr _debugData,
std::map<u256, Representation>& _cache
):
m_dialect(_dialect),
m_meter(_meter),
m_debugData(std::move(_debugData)),
m_cache(_cache)
{}
/// @returns a cheaper representation for the number than its representation
/// as a literal or nullptr otherwise.
Expression const* tryFindRepresentation(u256 const& _value);
private:
/// Recursively try to find the cheapest representation of the given number,
/// literal if necessary.
Representation const& findRepresentation(u256 const& _value);
Representation represent(u256 const& _value) const;
Representation represent(BuiltinHandle const& _instruction, Representation const& _arg) const;
Representation represent(BuiltinHandle const& _instruction, Representation const& _arg1, Representation const& _arg2) const;
Representation min(Representation _a, Representation _b);
EVMDialect const& m_dialect;
GasMeter const& m_meter;
langutil::DebugData::ConstPtr m_debugData;
/// Counter for the complexity of optimization, will stop when it reaches zero.
size_t m_maxSteps = 10000;
std::map<u256, Representation>& m_cache;
};
}