-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathLoadResolver.cpp
160 lines (141 loc) · 5.06 KB
/
LoadResolver.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
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 expressions of type ``sload(x)`` by the value
* currently stored in storage, if known.
*/
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/SideEffects.h>
#include <libyul/AST.h>
#include <libyul/Utilities.h>
#include <libevmasm/GasMeter.h>
#include <libsolutil/Keccak256.h>
#include <limits>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::evmasm;
using namespace solidity::yul;
void LoadResolver::run(OptimiserStepContext& _context, Block& _ast)
{
bool containsMSize = MSizeFinder::containsMSize(_context.dialect, _ast);
LoadResolver{
_context.dialect,
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)),
containsMSize,
_context.expectedExecutionsPerDeployment
}(_ast);
}
void LoadResolver::visit(Expression& _e)
{
DataFlowAnalyzer::visit(_e);
if (
FunctionCall const* funCall = std::get_if<FunctionCall>(&_e);
funCall && std::holds_alternative<BuiltinName>(funCall->functionName)
)
{
auto const& builtinHandle = std::get<BuiltinName>(funCall->functionName).handle;
if (builtinHandle == m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Memory)])
tryResolve(_e, StoreLoadLocation::Memory, funCall->arguments);
else if (builtinHandle == m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Storage)])
tryResolve(_e, StoreLoadLocation::Storage, funCall->arguments);
else if (!m_containsMSize && builtinHandle == m_dialect.hashFunctionHandle())
{
Identifier const* start = std::get_if<Identifier>(&funCall->arguments.at(0));
Identifier const* length = std::get_if<Identifier>(&funCall->arguments.at(1));
if (start && length)
if (auto const& value = keccakValue(start->name, length->name))
if (inScope(*value))
{
_e = Identifier{debugDataOf(_e), *value};
return;
}
tryEvaluateKeccak(_e, funCall->arguments);
}
}
}
void LoadResolver::tryResolve(
Expression& _e,
StoreLoadLocation _location,
std::vector<Expression> const& _arguments
)
{
if (_arguments.empty() || !std::holds_alternative<Identifier>(_arguments.at(0)))
return;
YulName key = std::get<Identifier>(_arguments.at(0)).name;
if (_location == StoreLoadLocation::Storage)
{
if (auto value = storageValue(key))
if (inScope(*value))
_e = Identifier{debugDataOf(_e), *value};
}
else if (!m_containsMSize && _location == StoreLoadLocation::Memory)
if (auto value = memoryValue(key))
if (inScope(*value))
_e = Identifier{debugDataOf(_e), *value};
}
void LoadResolver::tryEvaluateKeccak(
Expression& _e,
std::vector<Expression> const& _arguments
)
{
yulAssert(_arguments.size() == 2, "");
Identifier const* memoryKey = std::get_if<Identifier>(&_arguments.at(0));
Identifier const* length = std::get_if<Identifier>(&_arguments.at(1));
if (!memoryKey || !length)
return;
// The costs are only correct for hashes of 32 bytes or 1 word (when rounded up).
GasMeter gasMeter{
dynamic_cast<EVMDialect const&>(m_dialect),
!m_expectedExecutionsPerDeployment,
m_expectedExecutionsPerDeployment ? *m_expectedExecutionsPerDeployment : 1
};
bigint costOfKeccak = gasMeter.costs(_e);
bigint costOfLiteral = gasMeter.costs(
Literal{
{},
LiteralKind::Number,
// a dummy 256-bit number to represent the Keccak256 hash.
LiteralValue{std::numeric_limits<u256>::max()}
}
);
// We skip if there are no net gas savings.
// Note that for default `m_runs = 200`, the values are
// `costOfLiteral = 7200` and `costOfKeccak = 9000` for runtime context.
// For creation context: `costOfLiteral = 531` and `costOfKeccak = 90`.
if (costOfLiteral > costOfKeccak)
return;
std::optional<YulName> value = memoryValue(memoryKey->name);
if (value && inScope(*value))
{
std::optional<u256> memoryContent = valueOfIdentifier(*value);
std::optional<u256> byteLength = valueOfIdentifier(length->name);
if (memoryContent && byteLength && *byteLength <= 32)
{
bytes contentAsBytes = toBigEndian(*memoryContent);
contentAsBytes.resize(static_cast<size_t>(*byteLength));
u256 const contentHash (keccak256(contentAsBytes));
_e = Literal{
debugDataOf(_e),
LiteralKind::Number,
LiteralValue{contentHash}
};
}
}
}