-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathFullInliner.cpp
375 lines (321 loc) · 12.2 KB
/
FullInliner.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
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
/**
* Optimiser component that performs function inlining for arbitrary functions.
*/
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ASTCopier.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/FunctionCallFinder.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/Metrics.h>
#include <libyul/optimiser/SSAValueTracker.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/Exceptions.h>
#include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/Visitor.h>
#include <range/v3/action/remove.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/zip.hpp>
using namespace solidity;
using namespace solidity::yul;
void FullInliner::run(OptimiserStepContext& _context, Block& _ast)
{
FullInliner inliner{_ast, _context.dispenser, _context.dialect};
inliner.run(Pass::InlineTiny);
inliner.run(Pass::InlineRest);
}
FullInliner::FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const& _dialect):
m_ast(_ast),
m_recursiveFunctions(CallGraphGenerator::callGraph(_ast).recursiveFunctions()),
m_nameDispenser(_dispenser),
m_dialect(_dialect)
{
// Determine constants
SSAValueTracker tracker;
tracker(m_ast);
for (auto const& ssaValue: tracker.values())
if (ssaValue.second && std::holds_alternative<Literal>(*ssaValue.second))
m_constants.emplace(ssaValue.first);
// Store size of global statements.
m_functionSizes[YulName{}] = CodeSize::codeSize(_ast);
std::map<FunctionHandle, size_t> references = ReferencesCounter::countReferences(m_ast);
for (auto& statement: m_ast.statements)
{
if (!std::holds_alternative<FunctionDefinition>(statement))
continue;
FunctionDefinition& fun = std::get<FunctionDefinition>(statement);
m_functions[fun.name] = &fun;
if (LeaveFinder::containsLeave(fun))
m_noInlineFunctions.insert(fun.name);
// Always inline functions that are only called once.
if (references[fun.name] == 1)
m_singleUse.emplace(fun.name);
updateCodeSize(fun);
}
// Check for memory guard.
if (auto const memoryGuard = m_dialect.findBuiltin("memoryguard"))
{
std::vector<FunctionCall*> memoryGuardCalls = findFunctionCalls(_ast, *memoryGuard);
// We will perform less aggressive inlining, if no ``memoryguard`` call is found.
if (!memoryGuardCalls.empty())
m_hasMemoryGuard = true;
}
}
void FullInliner::run(Pass _pass)
{
m_pass = _pass;
// Note that the order of inlining can result in very different code.
// Since AST IDs and thus function names depend on whether or not a contract
// is compiled together with other source files, a change in AST IDs
// should have as little an impact as possible. This is the case
// if we handle inlining in source (and thus, for the IR generator,
// function name) order.
// We use stable_sort below to keep the inlining order of two functions
// with the same depth.
std::map<FunctionHandle, size_t> depths = callDepths();
std::vector<FunctionDefinition*> functions;
for (auto& statement: m_ast.statements)
if (std::holds_alternative<FunctionDefinition>(statement))
functions.emplace_back(&std::get<FunctionDefinition>(statement));
std::stable_sort(functions.begin(), functions.end(), [&depths](
FunctionDefinition const* _a,
FunctionDefinition const* _b
) {
return depths.at(_a->name) < depths.at(_b->name);
});
for (FunctionDefinition* fun: functions)
{
handleBlock(fun->name, fun->body);
updateCodeSize(*fun);
}
for (auto& statement: m_ast.statements)
if (std::holds_alternative<Block>(statement))
handleBlock({}, std::get<Block>(statement));
}
std::map<FunctionHandle, size_t> FullInliner::callDepths() const
{
CallGraph cg = CallGraphGenerator::callGraph(m_ast);
cg.functionCalls.erase(""_yulname);
// Remove calls to builtin functions.
for (auto& call: cg.functionCalls)
for (auto it = call.second.begin(); it != call.second.end();)
if (std::holds_alternative<BuiltinHandle>(*it))
it = call.second.erase(it);
else
++it;
std::map<FunctionHandle, size_t> depths;
size_t currentDepth = 0;
while (true)
{
std::vector<FunctionHandle> removed;
for (auto it = cg.functionCalls.begin(); it != cg.functionCalls.end();)
{
auto const& [fun, callees] = *it;
if (callees.empty())
{
removed.emplace_back(fun);
depths[fun] = currentDepth;
it = cg.functionCalls.erase(it);
}
else
++it;
}
for (auto& call: cg.functionCalls)
for (FunctionHandle toBeRemoved: removed)
ranges::actions::remove(call.second, toBeRemoved);
currentDepth++;
if (removed.empty())
break;
}
// Only recursive functions left here.
for (auto const& fun: cg.functionCalls)
depths[fun.first] = currentDepth;
return depths;
}
bool FullInliner::shallInline(FunctionCall const& _funCall, YulName _callSite)
{
if (isBuiltinFunctionCall(_funCall))
return false;
yulAssert(std::holds_alternative<Identifier>(_funCall.functionName));
auto const& functionName = std::get<Identifier>(_funCall.functionName).name;
// No recursive inlining
if (functionName == _callSite)
return false;
FunctionDefinition* calledFunction = function(functionName);
if (!calledFunction)
return false;
if (m_noInlineFunctions.count(functionName) || recursive(*calledFunction))
return false;
// No inlining of calls where argument expressions may have side-effects.
// To avoid running into this, make sure that ExpressionSplitter runs before FullInliner.
for (auto const& argument: _funCall.arguments)
if (!std::holds_alternative<Literal>(argument) && !std::holds_alternative<Identifier>(argument))
return false;
// Inline really, really tiny functions
size_t size = m_functionSizes.at(calledFunction->name);
if (size <= 1)
return true;
// In the first pass, only inline tiny functions.
if (m_pass == Pass::InlineTiny)
return false;
bool aggressiveInlining = true;
if (
EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect);
!evmDialect || !evmDialect->providesObjectAccess() || evmDialect->evmVersion() <= langutil::EVMVersion::homestead()
)
// No aggressive inlining with the old code transform.
aggressiveInlining = false;
// No aggressive inlining, if we cannot perform stack-to-memory.
if (!m_hasMemoryGuard || m_recursiveFunctions.count(_callSite))
aggressiveInlining = false;
if (!aggressiveInlining && m_functionSizes.at(_callSite) > 45)
return false;
if (m_singleUse.count(calledFunction->name))
return true;
// Constant arguments might provide a means for further optimization, so they cause a bonus.
bool constantArg = false;
for (auto const& argument: _funCall.arguments)
if (std::holds_alternative<Literal>(argument) || (
std::holds_alternative<Identifier>(argument) &&
m_constants.count(std::get<Identifier>(argument).name)
))
{
constantArg = true;
break;
}
return (size < (aggressiveInlining ? 8u : 6u) || (constantArg && size < (aggressiveInlining ? 16u : 12u)));
}
void FullInliner::tentativelyUpdateCodeSize(YulName _function, YulName _callSite)
{
m_functionSizes.at(_callSite) += m_functionSizes.at(_function);
}
void FullInliner::updateCodeSize(FunctionDefinition const& _fun)
{
m_functionSizes[_fun.name] = CodeSize::codeSize(_fun.body);
}
void FullInliner::handleBlock(YulName _currentFunctionName, Block& _block)
{
InlineModifier{*this, m_nameDispenser, _currentFunctionName, m_dialect}(_block);
}
bool FullInliner::recursive(FunctionDefinition const& _fun) const
{
std::map<FunctionHandle, size_t> references = ReferencesCounter::countReferences(_fun);
return references[_fun.name] > 0;
}
void InlineModifier::operator()(Block& _block)
{
std::function<std::optional<std::vector<Statement>>(Statement&)> f = [&](Statement& _statement) -> std::optional<std::vector<Statement>> {
visit(_statement);
return tryInlineStatement(_statement);
};
util::iterateReplacing(_block.statements, f);
}
std::optional<std::vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
{
// Only inline for expression statements, assignments and variable declarations.
Expression* e = std::visit(util::GenericVisitor{
util::VisitorFallback<Expression*>{},
[](ExpressionStatement& _s) { return &_s.expression; },
[](Assignment& _s) { return _s.value.get(); },
[](VariableDeclaration& _s) { return _s.value.get(); }
}, _statement);
if (e)
{
// Only inline direct function calls.
FunctionCall* funCall = std::visit(util::GenericVisitor{
util::VisitorFallback<FunctionCall*>{},
[](FunctionCall& _e) { return &_e; }
}, *e);
if (funCall && m_driver.shallInline(*funCall, m_currentFunction))
return performInline(_statement, *funCall);
}
return {};
}
std::vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall)
{
std::vector<Statement> newStatements;
std::map<YulName, YulName> variableReplacements;
yulAssert(std::holds_alternative<Identifier>(_funCall.functionName));
FunctionDefinition* function = m_driver.function(std::get<Identifier>(_funCall.functionName).name);
assertThrow(!!function, OptimizerException, "Attempt to inline invalid function.");
m_driver.tentativelyUpdateCodeSize(function->name, m_currentFunction);
// helper function to create a new variable that is supposed to model
// an existing variable.
auto newVariable = [&](NameWithDebugData const& _existingVariable, Expression* _value) {
YulName newName = m_nameDispenser.newName(_existingVariable.name);
variableReplacements[_existingVariable.name] = newName;
VariableDeclaration varDecl{_funCall.debugData, {{_funCall.debugData, newName}}, {}};
if (_value)
varDecl.value = std::make_unique<Expression>(std::move(*_value));
else
varDecl.value = std::make_unique<Expression>(m_dialect.zeroLiteral());
newStatements.emplace_back(std::move(varDecl));
};
for (auto&& [parameter, argument]: ranges::views::zip(function->parameters, _funCall.arguments) | ranges::views::reverse)
newVariable(parameter, &argument);
for (auto const& var: function->returnVariables)
newVariable(var, nullptr);
Statement newBody = BodyCopier(m_nameDispenser, variableReplacements)(function->body);
newStatements += std::move(std::get<Block>(newBody).statements);
std::visit(util::GenericVisitor{
util::VisitorFallback<>{},
[&](Assignment& _assignment)
{
for (size_t i = 0; i < _assignment.variableNames.size(); ++i)
newStatements.emplace_back(Assignment{
_assignment.debugData,
{_assignment.variableNames[i]},
std::make_unique<Expression>(Identifier{
_assignment.debugData,
variableReplacements.at(function->returnVariables[i].name)
})
});
},
[&](VariableDeclaration& _varDecl)
{
for (size_t i = 0; i < _varDecl.variables.size(); ++i)
newStatements.emplace_back(VariableDeclaration{
_varDecl.debugData,
{std::move(_varDecl.variables[i])},
std::make_unique<Expression>(Identifier{
_varDecl.debugData,
variableReplacements.at(function->returnVariables[i].name)
})
});
}
// nothing to be done for expression statement
}, _statement);
return newStatements;
}
Statement BodyCopier::operator()(VariableDeclaration const& _varDecl)
{
for (auto const& var: _varDecl.variables)
m_variableReplacements[var.name] = m_nameDispenser.newName(var.name);
return ASTCopier::operator()(_varDecl);
}
Statement BodyCopier::operator()(FunctionDefinition const&)
{
assertThrow(false, OptimizerException, "Function hoisting has to be done before function inlining.");
return {};
}
YulName BodyCopier::translateIdentifier(YulName _name)
{
return util::valueOrDefault(m_variableReplacements, _name, _name);
}