Debian Codegraph @

js/src/jit/SparseBitSet.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef jit_SparseBitSet_h
8 #define jit_SparseBitSet_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/MathAlgorithms.h"
12
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include "ds/InlineTable.h"
17
18 namespace js::jit {
19
20 // jit::SparseBitSet is very similar to jit::BitSet, but optimized for sets that
21 // can be very large but are often very sparse.
22 //
23 // It uses an InlineMap mapping from word-index to 32-bit words storing bits.
24 //
25 // For example, consider the following bitmap of 192 bits:
26 //
27 // 0xff001100 0x00001000 0x00000000 0x00000000 0x00000000 0x11110000
28 // 0^ 1^ 2^ 3^ 4^ 5^
29 //
30 // Words 2-4 don't have any bits set, so a SparseBitSet only stores the other
31 // words:
32 //
33 // 0 => 0xff001100
34 // 1 => 0x00001000
35 // 5 => 0x11110000
36 //
37 // SparseBitSet ensures words in the map are never 0.
38 template <typename AllocPolicy>
39 class SparseBitSet {
40 // Note: use uint32_t (instead of uintptr_t or uint64_t) to not waste space in
41 // InlineMap's array of inline entries. It uses a struct for each key/value
42 // pair.
43 using WordType = uint32_t;
44 static constexpr size_t BitsPerWord = 8 * sizeof(WordType);
45
46 // Note: 8 inline entries is sufficient for the majority of bit sets.
47 // Compiling a large PhotoShop Wasm module with Ion, 94.5% of SparseBitSets
48 // had <= 8 map entries. For OpenOffice this was more than 98.5%.
49 static constexpr size_t NumEntries = 8;
50 using Map = InlineMap<uint32_t, WordType, NumEntries, DefaultHasher<uint32_t>,
51 AllocPolicy>;
52 using Range = typename Map::Range;
53 Map map_;
54
55 static_assert(mozilla::IsPowerOfTwo(BitsPerWord),
56 "Must be power-of-two for fast division/modulo");
57 static_assert((sizeof(uint32_t) + sizeof(WordType)) * NumEntries ==
58 Map::SizeOfInlineEntries,
59 "Array of inline entries must not have unused padding bytes");
60
61 static WordType bitMask(size_t bit) {
62 return WordType(1) << (bit % BitsPerWord);
63 }
64
65 public:
66 class Iterator;
67
68 bool contains(size_t bit) {
69 uint32_t word = bit / BitsPerWord;
70 if (auto p = map_.lookup(word)) {
71 return p->value() & bitMask(bit);
72 }
73 return false;
74 }
75 void remove(size_t bit) {
76 uint32_t word = bit / BitsPerWord;
77 if (auto p = map_.lookup(word)) {
78 WordType value = p->value() & ~bitMask(bit);
79 if (value != 0) {
80 p->value() = value;
81 } else {
82 // The iterator and empty() method rely on the map not containing
83 // entries without any bits set.
84 map_.remove(p);
85 }
86 }
87 }
88 [[nodiscard]] bool insert(size_t bit) {
89 uint32_t word = bit / BitsPerWord;
90 WordType mask = bitMask(bit);
91 auto p = map_.lookupForAdd(word);
92 if (p) {
93 p->value() |= mask;
94 return true;
95 }
96 return map_.add(p, word, mask);
97 }
98
99 bool empty() const { return map_.empty(); }
100
101 [[nodiscard]] bool insertAll(const SparseBitSet& other) {
102 for (Range r(other.map_.all()); !r.empty(); r.popFront()) {
103 auto index = r.front().key();
104 WordType bits = r.front().value();
105 MOZ_ASSERT(bits);
106 auto p = map_.lookupForAdd(index);
107 if (p) {
108 p->value() |= bits;
109 } else {
110 if (!map_.add(p, index, bits)) {
111 return false;
112 }
113 }
114 }
115 return true;
116 }
117 };
118
119 // Iterates over the set bits in a SparseBitSet. For example:
120 //
121 // using Set = SparseBitSet<AllocPolicy>;
122 // Set set;
123 // ...
124 // for (Set::Iterator iter(set); iter; ++iter) {
125 // MOZ_ASSERT(set.contains(*iter));
126 // }
127 template <typename AllocPolicy>
128 class SparseBitSet<AllocPolicy>::Iterator {
129 #ifdef DEBUG
130 SparseBitSet& bitSet_;
131 #endif
132 SparseBitSet::Range range_;
133 WordType currentWord_ = 0;
134 // Index of a 1-bit in the SparseBitSet. This is the value returned by
135 // |*iter|.
136 size_t index_ = 0;
137
138 bool done() const { return range_.empty(); }
139
140 void skipZeroBits() {
141 MOZ_ASSERT(!done());
142 MOZ_ASSERT(currentWord_ != 0);
143 auto numZeroes = mozilla::CountTrailingZeroes32(currentWord_);
144 index_ += numZeroes;
145 currentWord_ >>= numZeroes;
146 }
147
148 public:
149 explicit Iterator(SparseBitSet& bitSet)
150 :
151 #ifdef DEBUG
152 bitSet_(bitSet),
153 #endif
154 range_(bitSet.map_.all()) {
155 if (!range_.empty()) {
156 index_ = range_.front().key() * BitsPerWord;
157 currentWord_ = range_.front().value();
158 skipZeroBits();
159 }
160 }
161
162 size_t operator*() const {
163 MOZ_ASSERT(!done());
164 MOZ_ASSERT(bitSet_.contains(index_));
165 return index_;
166 }
167
168 explicit operator bool() const { return !done(); }
169
170 void operator++() {
171 MOZ_ASSERT(!done());
172 currentWord_ >>= 1;
173 if (currentWord_ == 0) {
174 range_.popFront();
175 if (range_.empty()) {
176 // Done iterating.
177 return;
178 }
179 index_ = range_.front().key() * BitsPerWord;
180 currentWord_ = range_.front().value();
181 } else {
182 index_++;
183 }
184 skipZeroBits();
185 }
186 };
187
188 } // namespace js::jit
189
190 #endif /* jit_SparseBitSet_h */