/src/solidity/libyul/optimiser/DeadCodeEliminator.h
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 | | /** |
19 | | * Optimisation stage that removes unused variables and functions. |
20 | | */ |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <libyul/optimiser/ASTWalker.h> |
25 | | #include <libyul/YulName.h> |
26 | | #include <libyul/ControlFlowSideEffects.h> |
27 | | |
28 | | #include <map> |
29 | | #include <set> |
30 | | |
31 | | namespace solidity::yul |
32 | | { |
33 | | class Dialect; |
34 | | struct OptimiserStepContext; |
35 | | |
36 | | /** |
37 | | * Optimisation stage that removes unreachable code |
38 | | * |
39 | | * Unreachable code is any code within a block which is preceded by a |
40 | | * leave, return, invalid, break, continue, selfdestruct or revert or |
41 | | * a call to a user-defined function that never returns (either due to |
42 | | * recursion or a call to return / revert / stop). |
43 | | * |
44 | | * Function definitions are retained as they might be called by earlier |
45 | | * code and thus are considered reachable. |
46 | | * |
47 | | * Because variables declared in a for loop's init block have their scope extended to the loop body, |
48 | | * we require ForLoopInitRewriter to run before this step. |
49 | | * |
50 | | * Prerequisite: ForLoopInitRewriter, Function Hoister, Function Grouper |
51 | | */ |
52 | | class DeadCodeEliminator: public ASTModifier |
53 | | { |
54 | | public: |
55 | | static constexpr char const* name{"DeadCodeEliminator"}; |
56 | | static void run(OptimiserStepContext&, Block& _ast); |
57 | | |
58 | | using ASTModifier::operator(); |
59 | | void operator()(ForLoop& _for) override; |
60 | | void operator()(Block& _block) override; |
61 | | |
62 | | private: |
63 | | DeadCodeEliminator( |
64 | | Dialect const& _dialect, |
65 | | std::map<YulName, ControlFlowSideEffects> _sideEffects |
66 | 416k | ): m_dialect(_dialect), m_functionSideEffects(std::move(_sideEffects)) {} |
67 | | |
68 | | Dialect const& m_dialect; |
69 | | std::map<YulName, ControlFlowSideEffects> m_functionSideEffects; |
70 | | }; |
71 | | |
72 | | } |