-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathSSATransform.cpp
377 lines (321 loc) · 11.2 KB
/
SSATransform.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 turns subsequent assignments to variable declarations
* and assignments.
*/
#include <libyul/optimiser/SSATransform.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/AST.h>
#include <libsolutil/CommonData.h>
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::langutil;
namespace
{
/**
* First step of SSA transform: Introduces new SSA variables for each assignment or
* declaration of a variable to be replaced.
*/
class IntroduceSSA: public ASTModifier
{
public:
explicit IntroduceSSA(
NameDispenser& _nameDispenser,
std::set<YulName> const& _variablesToReplace
):
m_nameDispenser(_nameDispenser),
m_variablesToReplace(_variablesToReplace)
{ }
void operator()(Block& _block) override;
private:
NameDispenser& m_nameDispenser;
std::set<YulName> const& m_variablesToReplace;
};
void IntroduceSSA::operator()(Block& _block)
{
util::iterateReplacing(
_block.statements,
[&](Statement& _s) -> std::optional<std::vector<Statement>>
{
if (std::holds_alternative<VariableDeclaration>(_s))
{
VariableDeclaration& varDecl = std::get<VariableDeclaration>(_s);
if (varDecl.value)
visit(*varDecl.value);
bool needToReplaceSome = false;
for (auto const& var: varDecl.variables)
if (m_variablesToReplace.count(var.name))
needToReplaceSome = true;
if (!needToReplaceSome)
return {};
// Replace "let a := v" by "let a_1 := v let a := a_1"
// Replace "let a, b := v" by "let a_1, b_1 := v let a := a_1 let b := b_2"
langutil::DebugData::ConstPtr debugData = varDecl.debugData;
std::vector<Statement> statements;
statements.emplace_back(VariableDeclaration{debugData, {}, std::move(varDecl.value)});
NameWithDebugDataList newVariables;
for (auto const& var: varDecl.variables)
{
YulName oldName = var.name;
YulName newName = m_nameDispenser.newName(oldName);
newVariables.emplace_back(NameWithDebugData{debugData, newName});
statements.emplace_back(VariableDeclaration{
debugData,
{NameWithDebugData{debugData, oldName}},
std::make_unique<Expression>(Identifier{debugData, newName})
});
}
std::get<VariableDeclaration>(statements.front()).variables = std::move(newVariables);
return { std::move(statements) };
}
else if (std::holds_alternative<Assignment>(_s))
{
Assignment& assignment = std::get<Assignment>(_s);
visit(*assignment.value);
for (auto const& var: assignment.variableNames)
assertThrow(m_variablesToReplace.count(var.name), OptimizerException, "");
// Replace "a := v" by "let a_1 := v a := v"
// Replace "a, b := v" by "let a_1, b_1 := v a := a_1 b := b_2"
langutil::DebugData::ConstPtr debugData = assignment.debugData;
std::vector<Statement> statements;
statements.emplace_back(VariableDeclaration{debugData, {}, std::move(assignment.value)});
NameWithDebugDataList newVariables;
for (auto const& var: assignment.variableNames)
{
YulName oldName = var.name;
YulName newName = m_nameDispenser.newName(oldName);
newVariables.emplace_back(NameWithDebugData{debugData, newName});
statements.emplace_back(Assignment{
debugData,
{Identifier{debugData, oldName}},
std::make_unique<Expression>(Identifier{debugData, newName})
});
}
std::get<VariableDeclaration>(statements.front()).variables = std::move(newVariables);
return { std::move(statements) };
}
else
visit(_s);
return {};
}
);
}
/**
* Second step of SSA transform: Introduces new SSA variables at each control-flow join
* and at the beginning of functions.
*/
class IntroduceControlFlowSSA: public ASTModifier
{
public:
explicit IntroduceControlFlowSSA(
NameDispenser& _nameDispenser,
std::set<YulName> const& _variablesToReplace
):
m_nameDispenser(_nameDispenser),
m_variablesToReplace(_variablesToReplace)
{ }
void operator()(FunctionDefinition& _function) override;
void operator()(ForLoop& _forLoop) override;
void operator()(Switch& _switch) override;
void operator()(Block& _block) override;
private:
NameDispenser& m_nameDispenser;
std::set<YulName> const& m_variablesToReplace;
/// Variables (that are to be replaced) currently in scope.
std::set<YulName> m_variablesInScope;
/// Variables that do not have a specific value.
util::UniqueVector<YulName> m_variablesToReassign;
};
void IntroduceControlFlowSSA::operator()(FunctionDefinition& _function)
{
std::set<YulName> varsInScope;
std::swap(varsInScope, m_variablesInScope);
util::UniqueVector<YulName> toReassign;
std::swap(toReassign, m_variablesToReassign);
for (auto const& param: _function.parameters)
if (m_variablesToReplace.count(param.name))
{
m_variablesInScope.insert(param.name);
m_variablesToReassign.pushBack(param.name);
}
ASTModifier::operator()(_function);
m_variablesInScope = std::move(varsInScope);
m_variablesToReassign = std::move(toReassign);
}
void IntroduceControlFlowSSA::operator()(ForLoop& _for)
{
yulAssert(_for.pre.statements.empty(), "For loop init rewriter not run.");
for (auto const& var: assignedVariableNames(_for.body) + assignedVariableNames(_for.post))
if (util::contains(m_variablesInScope,var))
m_variablesToReassign.pushBack(var);
(*this)(_for.body);
(*this)(_for.post);
}
void IntroduceControlFlowSSA::operator()(Switch& _switch)
{
yulAssert(m_variablesToReassign.empty(), "");
util::UniqueVector<YulName> toReassign;
for (auto& c: _switch.cases)
{
(*this)(c.body);
toReassign.pushBack(m_variablesToReassign);
}
m_variablesToReassign.pushBack(toReassign);
}
void IntroduceControlFlowSSA::operator()(Block& _block)
{
util::UniqueVector<YulName> variablesDeclaredHere;
util::UniqueVector<YulName> assignedVariables;
util::iterateReplacing(
_block.statements,
[&](Statement& _s) -> std::optional<std::vector<Statement>>
{
std::vector<Statement> toPrepend;
for (YulName toReassign: m_variablesToReassign)
{
YulName newName = m_nameDispenser.newName(toReassign);
toPrepend.emplace_back(VariableDeclaration{
debugDataOf(_s),
{NameWithDebugData{debugDataOf(_s), newName}},
std::make_unique<Expression>(Identifier{debugDataOf(_s), toReassign})
});
assignedVariables.pushBack(toReassign);
}
m_variablesToReassign.clear();
if (std::holds_alternative<VariableDeclaration>(_s))
{
VariableDeclaration& varDecl = std::get<VariableDeclaration>(_s);
for (auto const& var: varDecl.variables)
if (m_variablesToReplace.count(var.name))
{
variablesDeclaredHere.pushBack(var.name);
m_variablesInScope.insert(var.name);
}
}
else if (std::holds_alternative<Assignment>(_s))
{
Assignment& assignment = std::get<Assignment>(_s);
for (auto const& var: assignment.variableNames)
if (m_variablesToReplace.count(var.name))
assignedVariables.pushBack(var.name);
}
else
visit(_s);
if (toPrepend.empty())
return {};
else
{
toPrepend.emplace_back(std::move(_s));
return {std::move(toPrepend)};
}
}
);
m_variablesToReassign.pushBack(assignedVariables);
m_variablesInScope -= variablesDeclaredHere.contents();
m_variablesToReassign.removeAll(variablesDeclaredHere.contents());
}
/**
* Third step of SSA transform: Replace the references to variables-to-be-replaced
* by their current values.
*/
class PropagateValues: public ASTModifier
{
public:
explicit PropagateValues(std::set<YulName> const& _variablesToReplace):
m_variablesToReplace(_variablesToReplace)
{ }
void operator()(Identifier& _identifier) override;
void operator()(VariableDeclaration& _varDecl) override;
void operator()(Assignment& _assignment) override;
void operator()(ForLoop& _for) override;
void operator()(Block& _block) override;
private:
/// This is a set of all variables that are assigned to anywhere in the code.
/// Variables that are only declared but never re-assigned are not touched.
std::set<YulName> const& m_variablesToReplace;
std::map<YulName, YulName> m_currentVariableValues;
std::set<YulName> m_clearAtEndOfBlock;
};
void PropagateValues::operator()(Identifier& _identifier)
{
if (m_currentVariableValues.count(_identifier.name))
_identifier.name = m_currentVariableValues[_identifier.name];
}
void PropagateValues::operator()(VariableDeclaration& _varDecl)
{
ASTModifier::operator()(_varDecl);
if (_varDecl.variables.size() != 1)
return;
YulName variable = _varDecl.variables.front().name;
if (m_variablesToReplace.count(variable))
{
// `let a := a_1` - regular declaration of non-SSA variable
yulAssert(std::holds_alternative<Identifier>(*_varDecl.value), "");
m_currentVariableValues[variable] = std::get<Identifier>(*_varDecl.value).name;
m_clearAtEndOfBlock.insert(variable);
}
else if (_varDecl.value && std::holds_alternative<Identifier>(*_varDecl.value))
{
// `let a_1 := a` - assignment to SSA variable after a branch.
YulName value = std::get<Identifier>(*_varDecl.value).name;
if (m_variablesToReplace.count(value))
{
// This is safe because `a_1` is not a "variable to replace" and thus
// will not be re-assigned.
m_currentVariableValues[value] = variable;
m_clearAtEndOfBlock.insert(value);
}
}
}
void PropagateValues::operator()(Assignment& _assignment)
{
visit(*_assignment.value);
if (_assignment.variableNames.size() != 1)
return;
YulName name = _assignment.variableNames.front().name;
if (!m_variablesToReplace.count(name))
return;
yulAssert(_assignment.value && std::holds_alternative<Identifier>(*_assignment.value), "");
m_currentVariableValues[name] = std::get<Identifier>(*_assignment.value).name;
m_clearAtEndOfBlock.insert(name);
}
void PropagateValues::operator()(ForLoop& _for)
{
yulAssert(_for.pre.statements.empty(), "For loop init rewriter not run.");
for (auto const& var: assignedVariableNames(_for.body) + assignedVariableNames(_for.post))
m_currentVariableValues.erase(var);
visit(*_for.condition);
(*this)(_for.body);
(*this)(_for.post);
}
void PropagateValues::operator()(Block& _block)
{
std::set<YulName> clearAtParentBlock = std::move(m_clearAtEndOfBlock);
m_clearAtEndOfBlock.clear();
ASTModifier::operator()(_block);
for (auto const& var: m_clearAtEndOfBlock)
m_currentVariableValues.erase(var);
m_clearAtEndOfBlock = std::move(clearAtParentBlock);
}
}
void SSATransform::run(OptimiserStepContext& _context, Block& _ast)
{
std::set<YulName> assignedVariables = assignedVariableNames(_ast);
IntroduceSSA{_context.dispenser, assignedVariables}(_ast);
IntroduceControlFlowSSA{_context.dispenser, assignedVariables}(_ast);
PropagateValues{assignedVariables}(_ast);
}