/src/solidity/libyul/optimiser/SSAValueTracker.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * Component that collects variables that are never assigned to and their |
20 | | * initial values. |
21 | | */ |
22 | | |
23 | | #include <libyul/optimiser/SSAValueTracker.h> |
24 | | |
25 | | #include <libyul/AST.h> |
26 | | |
27 | | using namespace std; |
28 | | using namespace solidity; |
29 | | using namespace solidity::yul; |
30 | | |
31 | | void SSAValueTracker::operator()(Assignment const& _assignment) |
32 | 0 | { |
33 | 0 | for (auto const& var: _assignment.variableNames) |
34 | 0 | m_values.erase(var.name); |
35 | 0 | } |
36 | | |
37 | | void SSAValueTracker::operator()(FunctionDefinition const& _funDef) |
38 | 0 | { |
39 | 0 | for (auto const& var: _funDef.returnVariables) |
40 | 0 | setValue(var.name, nullptr); |
41 | 0 | ASTWalker::operator()(_funDef); |
42 | 0 | } |
43 | | |
44 | | void SSAValueTracker::operator()(VariableDeclaration const& _varDecl) |
45 | 0 | { |
46 | 0 | if (!_varDecl.value) |
47 | 0 | for (auto const& var: _varDecl.variables) |
48 | 0 | setValue(var.name, nullptr); |
49 | 0 | else if (_varDecl.variables.size() == 1) |
50 | 0 | setValue(_varDecl.variables.front().name, _varDecl.value.get()); |
51 | 0 | } |
52 | | |
53 | | set<YulString> SSAValueTracker::ssaVariables(Block const& _ast) |
54 | 0 | { |
55 | 0 | SSAValueTracker t; |
56 | 0 | t(_ast); |
57 | 0 | set<YulString> ssaVars; |
58 | 0 | for (auto const& value: t.values()) |
59 | 0 | ssaVars.insert(value.first); |
60 | 0 | return ssaVars; |
61 | 0 | } |
62 | | |
63 | | void SSAValueTracker::setValue(YulString _name, Expression const* _value) |
64 | 0 | { |
65 | 0 | assertThrow( |
66 | 0 | m_values.count(_name) == 0, |
67 | 0 | OptimizerException, |
68 | 0 | "Source needs to be disambiguated." |
69 | 0 | ); |
70 | 0 | if (!_value) |
71 | 0 | _value = &m_zero; |
72 | 0 | m_values[_name] = _value; |
73 | 0 | } |