summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/sanitizer_bitvector.h
blob: 0f65814c398fdbca5cba15b6ee4c2edc4d737cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//===-- sanitizer_bitvector.h -----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Specializer BitVector implementation.
//
//===----------------------------------------------------------------------===//

#ifndef SANITIZER_BITVECTOR_H
#define SANITIZER_BITVECTOR_H

#include "sanitizer_common.h"

namespace __sanitizer {

// Fixed size bit vector based on a single basic integer.
template <class basic_int_t = uptr>
class BasicBitVector {
 public:
  enum SizeEnum : uptr { kSize = sizeof(basic_int_t) * 8 };

  uptr size() const { return kSize; }
  // No CTOR.
  void clear() { bits_ = 0; }
  void setAll() { bits_ = ~(basic_int_t)0; }
  bool empty() const { return bits_ == 0; }

  // Returns true if the bit has changed from 0 to 1.
  bool setBit(uptr idx) {
    basic_int_t old = bits_;
    bits_ |= mask(idx);
    return bits_ != old;
  }

  // Returns true if the bit has changed from 1 to 0.
  bool clearBit(uptr idx) {
    basic_int_t old = bits_;
    bits_ &= ~mask(idx);
    return bits_ != old;
  }

  bool getBit(uptr idx) const { return (bits_ & mask(idx)) != 0; }

  uptr getAndClearFirstOne() {
    CHECK(!empty());
    uptr idx = LeastSignificantSetBitIndex(bits_);
    clearBit(idx);
    return idx;
  }

  // Do "this |= v" and return whether new bits have been added.
  bool setUnion(const BasicBitVector &v) {
    basic_int_t old = bits_;
    bits_ |= v.bits_;
    return bits_ != old;
  }

  // Do "this &= v" and return whether any bits have been removed.
  bool setIntersection(const BasicBitVector &v) {
    basic_int_t old = bits_;
    bits_ &= v.bits_;
    return bits_ != old;
  }

  // Do "this &= ~v" and return whether any bits have been removed.
  bool setDifference(const BasicBitVector &v) {
    basic_int_t old = bits_;
    bits_ &= ~v.bits_;
    return bits_ != old;
  }

  void copyFrom(const BasicBitVector &v) { bits_ = v.bits_; }

  // Returns true if 'this' intersects with 'v'.
  bool intersectsWith(const BasicBitVector &v) const {
    return (bits_ & v.bits_) != 0;
  }

  // for (BasicBitVector<>::Iterator it(bv); it.hasNext();) {
  //   uptr idx = it.next();
  //   use(idx);
  // }
  class Iterator {
   public:
    Iterator() { }
    explicit Iterator(const BasicBitVector &bv) : bv_(bv) {}
    bool hasNext() const { return !bv_.empty(); }
    uptr next() { return bv_.getAndClearFirstOne(); }
    void clear() { bv_.clear(); }
   private:
    BasicBitVector bv_;
  };

 private:
  basic_int_t mask(uptr idx) const {
    CHECK_LT(idx, size());
    return (basic_int_t)1UL << idx;
  }
  basic_int_t bits_;
};

// Fixed size bit vector of (kLevel1Size*BV::kSize**2) bits.
// The implementation is optimized for better performance on
// sparse bit vectors, i.e. the those with few set bits.
template <uptr kLevel1Size = 1, class BV = BasicBitVector<> >
class TwoLevelBitVector {
  // This is essentially a 2-level bit vector.
  // Set bit in the first level BV indicates that there are set bits
  // in the corresponding BV of the second level.
  // This structure allows O(kLevel1Size) time for clear() and empty(),
  // as well fast handling of sparse BVs.
 public:
  enum SizeEnum : uptr { kSize = BV::kSize * BV::kSize * kLevel1Size };
  // No CTOR.

  uptr size() const { return kSize; }

  void clear() {
    for (uptr i = 0; i < kLevel1Size; i++)
      l1_[i].clear();
  }

  void setAll() {
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      l1_[i0].setAll();
      for (uptr i1 = 0; i1 < BV::kSize; i1++)
        l2_[i0][i1].setAll();
    }
  }

  bool empty() const {
    for (uptr i = 0; i < kLevel1Size; i++)
      if (!l1_[i].empty())
        return false;
    return true;
  }

  // Returns true if the bit has changed from 0 to 1.
  bool setBit(uptr idx) {
    check(idx);
    uptr i0 = idx0(idx);
    uptr i1 = idx1(idx);
    uptr i2 = idx2(idx);
    if (!l1_[i0].getBit(i1)) {
      l1_[i0].setBit(i1);
      l2_[i0][i1].clear();
    }
    bool res = l2_[i0][i1].setBit(i2);
    // Printf("%s: %zd => %zd %zd %zd; %d\n", __func__,
    // idx, i0, i1, i2, res);
    return res;
  }

  bool clearBit(uptr idx) {
    check(idx);
    uptr i0 = idx0(idx);
    uptr i1 = idx1(idx);
    uptr i2 = idx2(idx);
    bool res = false;
    if (l1_[i0].getBit(i1)) {
      res = l2_[i0][i1].clearBit(i2);
      if (l2_[i0][i1].empty())
        l1_[i0].clearBit(i1);
    }
    return res;
  }

  bool getBit(uptr idx) const {
    check(idx);
    uptr i0 = idx0(idx);
    uptr i1 = idx1(idx);
    uptr i2 = idx2(idx);
    // Printf("%s: %zd => %zd %zd %zd\n", __func__, idx, i0, i1, i2);
    return l1_[i0].getBit(i1) && l2_[i0][i1].getBit(i2);
  }

  uptr getAndClearFirstOne() {
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      if (l1_[i0].empty()) continue;
      uptr i1 = l1_[i0].getAndClearFirstOne();
      uptr i2 = l2_[i0][i1].getAndClearFirstOne();
      if (!l2_[i0][i1].empty())
        l1_[i0].setBit(i1);
      uptr res = i0 * BV::kSize * BV::kSize + i1 * BV::kSize + i2;
      // Printf("getAndClearFirstOne: %zd %zd %zd => %zd\n", i0, i1, i2, res);
      return res;
    }
    CHECK(0);
    return 0;
  }

  // Do "this |= v" and return whether new bits have been added.
  bool setUnion(const TwoLevelBitVector &v) {
    bool res = false;
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      BV t = v.l1_[i0];
      while (!t.empty()) {
        uptr i1 = t.getAndClearFirstOne();
        if (l1_[i0].setBit(i1))
          l2_[i0][i1].clear();
        if (l2_[i0][i1].setUnion(v.l2_[i0][i1]))
          res = true;
      }
    }
    return res;
  }

  // Do "this &= v" and return whether any bits have been removed.
  bool setIntersection(const TwoLevelBitVector &v) {
    bool res = false;
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      if (l1_[i0].setIntersection(v.l1_[i0]))
        res = true;
      if (!l1_[i0].empty()) {
        BV t = l1_[i0];
        while (!t.empty()) {
          uptr i1 = t.getAndClearFirstOne();
          if (l2_[i0][i1].setIntersection(v.l2_[i0][i1]))
            res = true;
          if (l2_[i0][i1].empty())
            l1_[i0].clearBit(i1);
        }
      }
    }
    return res;
  }

  // Do "this &= ~v" and return whether any bits have been removed.
  bool setDifference(const TwoLevelBitVector &v) {
    bool res = false;
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      BV t = l1_[i0];
      t.setIntersection(v.l1_[i0]);
      while (!t.empty()) {
        uptr i1 = t.getAndClearFirstOne();
        if (l2_[i0][i1].setDifference(v.l2_[i0][i1]))
          res = true;
        if (l2_[i0][i1].empty())
          l1_[i0].clearBit(i1);
      }
    }
    return res;
  }

  void copyFrom(const TwoLevelBitVector &v) {
    clear();
    setUnion(v);
  }

  // Returns true if 'this' intersects with 'v'.
  bool intersectsWith(const TwoLevelBitVector &v) const {
    for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
      BV t = l1_[i0];
      t.setIntersection(v.l1_[i0]);
      while (!t.empty()) {
        uptr i1 = t.getAndClearFirstOne();
        if (!v.l1_[i0].getBit(i1)) continue;
        if (l2_[i0][i1].intersectsWith(v.l2_[i0][i1]))
          return true;
      }
    }
    return false;
  }

  // for (TwoLevelBitVector<>::Iterator it(bv); it.hasNext();) {
  //   uptr idx = it.next();
  //   use(idx);
  // }
  class Iterator {
   public:
    Iterator() { }
    explicit Iterator(const TwoLevelBitVector &bv) : bv_(bv), i0_(0), i1_(0) {
      it1_.clear();
      it2_.clear();
    }

    bool hasNext() const {
      if (it1_.hasNext()) return true;
      for (uptr i = i0_; i < kLevel1Size; i++)
        if (!bv_.l1_[i].empty()) return true;
      return false;
    }

    uptr next() {
      // Printf("++++: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
      //       it2_.hasNext(), kSize);
      if (!it1_.hasNext() && !it2_.hasNext()) {
        for (; i0_ < kLevel1Size; i0_++) {
          if (bv_.l1_[i0_].empty()) continue;
          it1_ = typename BV::Iterator(bv_.l1_[i0_]);
          // Printf("+i0: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
          //   it2_.hasNext(), kSize);
          break;
        }
      }
      if (!it2_.hasNext()) {
        CHECK(it1_.hasNext());
        i1_ = it1_.next();
        it2_ = typename BV::Iterator(bv_.l2_[i0_][i1_]);
        // Printf("++i1: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
        //       it2_.hasNext(), kSize);
      }
      CHECK(it2_.hasNext());
      uptr i2 = it2_.next();
      uptr res = i0_ * BV::kSize * BV::kSize + i1_ * BV::kSize + i2;
      // Printf("+ret: %zd %zd; %d %d; size %zd; res: %zd\n", i0_, i1_,
      //       it1_.hasNext(), it2_.hasNext(), kSize, res);
      if (!it1_.hasNext() && !it2_.hasNext())
        i0_++;
      return res;
    }

   private:
    const TwoLevelBitVector &bv_;
    uptr i0_, i1_;
    typename BV::Iterator it1_, it2_;
  };

 private:
  void check(uptr idx) const { CHECK_LE(idx, size()); }

  uptr idx0(uptr idx) const {
    uptr res = idx / (BV::kSize * BV::kSize);
    CHECK_LE(res, kLevel1Size);
    return res;
  }

  uptr idx1(uptr idx) const {
    uptr res = (idx / BV::kSize) % BV::kSize;
    CHECK_LE(res, BV::kSize);
    return res;
  }

  uptr idx2(uptr idx) const {
    uptr res = idx % BV::kSize;
    CHECK_LE(res, BV::kSize);
    return res;
  }

  BV l1_[kLevel1Size];
  BV l2_[kLevel1Size][BV::kSize];
};

} // namespace __sanitizer

#endif // SANITIZER_BITVECTOR_H