Coverage Report

Created: 2025-09-08 08:10

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