/src/solidity/libevmasm/Inliner.h
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 | | * @file Inliner.h |
20 | | * Inlines small code snippets by replacing JUMP with a copy of the code jumped to. |
21 | | */ |
22 | | #pragma once |
23 | | |
24 | | #include <libsolutil/Common.h> |
25 | | #include <libevmasm/Assembly.h> |
26 | | #include <libevmasm/AssemblyItem.h> |
27 | | #include <liblangutil/EVMVersion.h> |
28 | | |
29 | | #include <range/v3/view/span.hpp> |
30 | | #include <map> |
31 | | #include <set> |
32 | | #include <vector> |
33 | | |
34 | | namespace solidity::evmasm |
35 | | { |
36 | | |
37 | | class Inliner |
38 | | { |
39 | | public: |
40 | | explicit Inliner( |
41 | | AssemblyItems& _items, |
42 | | std::set<size_t> const& _tagsReferencedFromOutside, |
43 | | size_t _runs, |
44 | | bool _isCreation, |
45 | | langutil::EVMVersion _evmVersion |
46 | | ): |
47 | | m_items(_items), |
48 | | m_tagsReferencedFromOutside(_tagsReferencedFromOutside), |
49 | | m_runs(_runs), |
50 | | m_isCreation(_isCreation), |
51 | | m_evmVersion(_evmVersion) |
52 | 0 | { |
53 | 0 | } |
54 | 0 | virtual ~Inliner() = default; |
55 | | |
56 | | void optimise(); |
57 | | |
58 | | private: |
59 | | struct InlinableBlock |
60 | | { |
61 | | ranges::span<AssemblyItem const> items; |
62 | | uint64_t pushTagCount = 0; |
63 | | }; |
64 | | |
65 | | /// @returns the exit item for the block to be inlined, if a particular jump to it should be inlined, otherwise nullopt. |
66 | | std::optional<AssemblyItem> shouldInline(size_t _tag, AssemblyItem const& _jump, InlinableBlock const& _block) const; |
67 | | /// @returns true, if the full function at tag @a _tag with body @a _block that is referenced @a _pushTagCount times |
68 | | /// should be inlined, false otherwise. @a _block should start at the first instruction after the function entry tag |
69 | | /// up to and including the return jump. |
70 | | bool shouldInlineFullFunctionBody(size_t _tag, ranges::span<AssemblyItem const> _block, uint64_t _pushTagCount) const; |
71 | | /// @returns true, if the @a _items at @a _tag are a potential candidate for inlining. |
72 | | bool isInlineCandidate(size_t _tag, ranges::span<AssemblyItem const> _items) const; |
73 | | /// @returns a map from tags that can potentially be inlined to the inlinable item range behind that tag and the |
74 | | /// number of times the tag in question was referenced. |
75 | | std::map<size_t, InlinableBlock> determineInlinableBlocks(AssemblyItems const& _items) const; |
76 | | |
77 | | AssemblyItems& m_items; |
78 | | std::set<size_t> const& m_tagsReferencedFromOutside; |
79 | | size_t const m_runs = Assembly::OptimiserSettings{}.expectedExecutionsPerDeployment; |
80 | | bool const m_isCreation = false; |
81 | | langutil::EVMVersion const m_evmVersion; |
82 | | }; |
83 | | |
84 | | } |