/src/solidity/libevmasm/BlockDeduplicator.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 | | * @file BlockDeduplicator.h |
20 | | * @author Christian <c@ethdev.com> |
21 | | * @date 2015 |
22 | | * Unifies basic blocks that share content. |
23 | | */ |
24 | | |
25 | | #pragma once |
26 | | |
27 | | #include <libsolutil/Common.h> |
28 | | #include <libsolutil/Numeric.h> |
29 | | |
30 | | |
31 | | #include <cstddef> |
32 | | #include <vector> |
33 | | #include <functional> |
34 | | #include <map> |
35 | | |
36 | | namespace solidity::evmasm |
37 | | { |
38 | | |
39 | | class AssemblyItem; |
40 | | using AssemblyItems = std::vector<AssemblyItem>; |
41 | | |
42 | | /** |
43 | | * Optimizer class to be used to unify blocks that share content. |
44 | | * Modifies the passed vector in place. |
45 | | */ |
46 | | class BlockDeduplicator |
47 | | { |
48 | | public: |
49 | 114k | explicit BlockDeduplicator(AssemblyItems& _items): m_items(_items) {} |
50 | | /// @returns true if something was changed |
51 | | bool deduplicate(); |
52 | | /// @returns the tags that were replaced. |
53 | 5.64k | std::map<u256, u256> const& replacedTags() const { return m_replacedTags; } |
54 | | |
55 | | /// Replaces all PushTag operations insied @a _items that match a key in |
56 | | /// @a _replacements by the respective value. If @a _subID is not -1, only |
57 | | /// apply the replacement for foreign tags from this sub id. |
58 | | /// @returns true iff a replacement was performed. |
59 | | static bool applyTagReplacement( |
60 | | AssemblyItems& _items, |
61 | | std::map<u256, u256> const& _replacements, |
62 | | size_t _subID = size_t(-1) |
63 | | ); |
64 | | |
65 | | private: |
66 | | /// Iterator that skips tags and skips to the end if (all branches of) the control |
67 | | /// flow does not continue to the next instruction. |
68 | | /// If the arguments are supplied to the constructor, replaces items on the fly. |
69 | | struct BlockIterator |
70 | | { |
71 | | public: |
72 | | using iterator_category = std::forward_iterator_tag; |
73 | | using value_type = AssemblyItem const; |
74 | | using difference_type = std::ptrdiff_t; |
75 | | using pointer = AssemblyItem const*; |
76 | | using reference = AssemblyItem const&; |
77 | | BlockIterator( |
78 | | AssemblyItems::const_iterator _it, |
79 | | AssemblyItems::const_iterator _end, |
80 | | AssemblyItem const* _replaceItem = nullptr, |
81 | | AssemblyItem const* _replaceWith = nullptr |
82 | | ): |
83 | 79.3M | it(_it), end(_end), replaceItem(_replaceItem), replaceWith(_replaceWith) {} |
84 | | BlockIterator& operator++(); |
85 | 55.9M | bool operator==(BlockIterator const& _other) const { return it == _other.it; } |
86 | 109M | bool operator!=(BlockIterator const& _other) const { return it != _other.it; } |
87 | | AssemblyItem const& operator*() const; |
88 | | AssemblyItems::const_iterator it; |
89 | | AssemblyItems::const_iterator end; |
90 | | AssemblyItem const* replaceItem; |
91 | | AssemblyItem const* replaceWith; |
92 | | }; |
93 | | |
94 | | std::map<u256, u256> m_replacedTags; |
95 | | AssemblyItems& m_items; |
96 | | }; |
97 | | |
98 | | } |