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
|
/*(
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/>.
*/
/**
* Base class to perform data flow analysis during AST walks.
* Tracks assignments and is used as base class for both Rematerialiser and
* Common Subexpression Eliminator.
*/
#include <libyul/optimiser/DataFlowAnalyzer.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/Exceptions.h>
#include <libsolidity/inlineasm/AsmData.h>
#include <libdevcore/CommonData.h>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
using namespace dev;
using namespace dev::yul;
void DataFlowAnalyzer::operator()(Assignment& _assignment)
{
set<string> names;
for (auto const& var: _assignment.variableNames)
names.insert(var.name);
assertThrow(_assignment.value, OptimizerException, "");
visit(*_assignment.value);
handleAssignment(names, _assignment.value.get());
}
void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
{
set<string> names;
for (auto const& var: _varDecl.variables)
names.insert(var.name);
m_variableScopes.back().variables += names;
if (_varDecl.value)
visit(*_varDecl.value);
handleAssignment(names, _varDecl.value.get());
}
void DataFlowAnalyzer::operator()(If& _if)
{
ASTModifier::operator()(_if);
Assignments assignments;
assignments(_if.body);
clearValues(assignments.names());
}
void DataFlowAnalyzer::operator()(Switch& _switch)
{
visit(*_switch.expression);
set<string> assignedVariables;
for (auto& _case: _switch.cases)
{
(*this)(_case.body);
Assignments assignments;
assignments(_case.body);
assignedVariables += assignments.names();
// This is a little too destructive, we could retain the old values.
clearValues(assignments.names());
}
clearValues(assignedVariables);
}
void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
{
m_variableScopes.emplace_back(true);
for (auto const& parameter: _fun.parameters)
m_variableScopes.back().variables.insert(parameter.name);
for (auto const& var: _fun.returnVariables)
m_variableScopes.back().variables.insert(var.name);
ASTModifier::operator()(_fun);
m_variableScopes.pop_back();
}
void DataFlowAnalyzer::operator()(ForLoop& _for)
{
// Special scope handling of the pre block.
m_variableScopes.emplace_back(false);
for (auto& statement: _for.pre.statements)
visit(statement);
Assignments assignments;
assignments(_for.body);
assignments(_for.post);
clearValues(assignments.names());
visit(*_for.condition);
(*this)(_for.body);
(*this)(_for.post);
clearValues(assignments.names());
m_variableScopes.pop_back();
}
void DataFlowAnalyzer::operator()(Block& _block)
{
size_t numScopes = m_variableScopes.size();
m_variableScopes.emplace_back(false);
ASTModifier::operator()(_block);
m_variableScopes.pop_back();
assertThrow(numScopes == m_variableScopes.size(), OptimizerException, "");
}
void DataFlowAnalyzer::handleAssignment(set<string> const& _variables, Expression* _value)
{
clearValues(_variables);
MovableChecker movableChecker;
if (_value)
movableChecker.visit(*_value);
if (_variables.size() == 1)
{
string const& name = *_variables.begin();
// Expression has to be movable and cannot contain a reference
// to the variable that will be assigned to.
if (_value && movableChecker.movable() && !movableChecker.referencedVariables().count(name))
m_value[name] = _value;
}
auto const& referencedVariables = movableChecker.referencedVariables();
for (auto const& name: _variables)
{
m_references[name] = referencedVariables;
for (auto const& ref: referencedVariables)
m_referencedBy[ref].insert(name);
}
}
void DataFlowAnalyzer::clearValues(set<string> const& _variables)
{
// All variables that reference variables to be cleared also have to be
// cleared, but not recursively, since only the value of the original
// variables changes. Example:
// let a := 1
// let b := a
// let c := b
// let a := 2
// add(b, c)
// In the last line, we can replace c by b, but not b by a.
//
// This cannot be easily tested since the substitutions will be done
// one by one on the fly, and the last line will just be add(1, 1)
set<string> variables = _variables;
// Clear variables that reference variables to be cleared.
for (auto const& name: variables)
for (auto const& ref: m_referencedBy[name])
variables.insert(ref);
// Clear the value and update the reference relation.
for (auto const& name: variables)
m_value.erase(name);
for (auto const& name: variables)
{
for (auto const& ref: m_references[name])
m_referencedBy[ref].erase(name);
m_references[name].clear();
}
}
bool DataFlowAnalyzer::inScope(string const& _variableName) const
{
for (auto const& scope: m_variableScopes | boost::adaptors::reversed)
{
if (scope.variables.count(_variableName))
return true;
if (scope.isFunction)
return false;
}
return false;
}
|