/src/solidity/libyul/optimiser/InlinableExpressionFunctionFinder.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 | | * Optimiser component that identifies functions to be inlined. |
20 | | */ |
21 | | |
22 | | #include <libyul/optimiser/InlinableExpressionFunctionFinder.h> |
23 | | |
24 | | #include <libyul/optimiser/OptimizerUtilities.h> |
25 | | #include <libyul/AST.h> |
26 | | |
27 | | using namespace std; |
28 | | using namespace solidity; |
29 | | using namespace solidity::yul; |
30 | | |
31 | | void InlinableExpressionFunctionFinder::operator()(Identifier const& _identifier) |
32 | 0 | { |
33 | 0 | checkAllowed(_identifier.name); |
34 | 0 | ASTWalker::operator()(_identifier); |
35 | 0 | } |
36 | | |
37 | | void InlinableExpressionFunctionFinder::operator()(FunctionCall const& _funCall) |
38 | 0 | { |
39 | 0 | checkAllowed(_funCall.functionName.name); |
40 | 0 | ASTWalker::operator()(_funCall); |
41 | 0 | } |
42 | | |
43 | | void InlinableExpressionFunctionFinder::operator()(FunctionDefinition const& _function) |
44 | 0 | { |
45 | 0 | if (_function.returnVariables.size() == 1 && _function.body.statements.size() == 1) |
46 | 0 | { |
47 | 0 | YulString retVariable = _function.returnVariables.front().name; |
48 | 0 | Statement const& bodyStatement = _function.body.statements.front(); |
49 | 0 | if (holds_alternative<Assignment>(bodyStatement)) |
50 | 0 | { |
51 | 0 | Assignment const& assignment = std::get<Assignment>(bodyStatement); |
52 | 0 | if (assignment.variableNames.size() == 1 && assignment.variableNames.front().name == retVariable) |
53 | 0 | { |
54 | | // TODO: use code size metric here |
55 | | |
56 | | // We cannot overwrite previous settings, because this function definition |
57 | | // would not be valid here if we were searching inside a functionally inlinable |
58 | | // function body. |
59 | 0 | assertThrow(m_disallowedIdentifiers.empty() && !m_foundDisallowedIdentifier, OptimizerException, ""); |
60 | 0 | m_disallowedIdentifiers = set<YulString>{retVariable, _function.name}; |
61 | 0 | std::visit(*this, *assignment.value); |
62 | 0 | if (!m_foundDisallowedIdentifier) |
63 | 0 | m_inlinableFunctions[_function.name] = &_function; |
64 | 0 | m_disallowedIdentifiers.clear(); |
65 | 0 | m_foundDisallowedIdentifier = false; |
66 | 0 | } |
67 | 0 | } |
68 | 0 | } |
69 | 0 | ASTWalker::operator()(_function.body); |
70 | 0 | } |