/src/solidity/libyul/optimiser/CircularReferencesPruner.cpp
Line | Count | Source |
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 | | #include <libyul/optimiser/CircularReferencesPruner.h> |
19 | | |
20 | | #include <libyul/optimiser/CallGraphGenerator.h> |
21 | | #include <libyul/optimiser/FunctionGrouper.h> |
22 | | #include <libyul/optimiser/OptimizerUtilities.h> |
23 | | #include <libyul/AST.h> |
24 | | |
25 | | #include <libsolutil/Algorithms.h> |
26 | | |
27 | | using namespace solidity::yul; |
28 | | |
29 | | void CircularReferencesPruner::run(OptimiserStepContext& _context, Block& _ast) |
30 | 837k | { |
31 | 837k | CircularReferencesPruner{_context.reservedIdentifiers}(_ast); |
32 | 837k | FunctionGrouper::run(_context, _ast); |
33 | 837k | } |
34 | | |
35 | | void CircularReferencesPruner::operator()(Block& _block) |
36 | 837k | { |
37 | 837k | std::set<YulName> functionsToKeep = |
38 | 837k | functionsCalledFromOutermostContext(CallGraphGenerator::callGraph(_block)); |
39 | | |
40 | 837k | for (auto&& statement: _block.statements) |
41 | 2.01M | if (std::holds_alternative<FunctionDefinition>(statement)) |
42 | 1.17M | { |
43 | 1.17M | FunctionDefinition const& funDef = std::get<FunctionDefinition>(statement); |
44 | 1.17M | if (!functionsToKeep.count(funDef.name)) |
45 | 2.24k | statement = Block{}; |
46 | 1.17M | } |
47 | | |
48 | 837k | removeEmptyBlocks(_block); |
49 | 837k | } |
50 | | |
51 | | std::set<YulName> CircularReferencesPruner::functionsCalledFromOutermostContext(CallGraph const& _callGraph) |
52 | 837k | { |
53 | 837k | std::set<YulName> verticesToTraverse = m_reservedIdentifiers; |
54 | 837k | verticesToTraverse.insert(YulName("")); |
55 | | |
56 | 837k | return util::BreadthFirstSearch<YulName>{{verticesToTraverse.begin(), verticesToTraverse.end()}}.run( |
57 | 2.00M | [&_callGraph](YulName _function, auto&& _addChild) { |
58 | 2.00M | if (_callGraph.functionCalls.count(_function)) |
59 | 2.00M | for (auto const& callee: _callGraph.functionCalls.at(_function)) |
60 | 8.10M | if (std::holds_alternative<YulName>(callee) && _callGraph.functionCalls.count(std::get<YulName>(callee))) |
61 | 1.53M | _addChild(std::get<YulName>(callee)); |
62 | 2.00M | }).visited; |
63 | 837k | } |