bit-vector.h 5.96 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_DATAFLOW_H_
#define V8_DATAFLOW_H_

8
#include "src/v8.h"
9

10 11 12 13
#include "src/allocation.h"
#include "src/ast.h"
#include "src/compiler.h"
#include "src/zone-inl.h"
14 15 16 17

namespace v8 {
namespace internal {

18
class BitVector : public ZoneObject {
19
 public:
20 21 22 23 24 25 26 27
  // Iterator for the elements of this BitVector.
  class Iterator BASE_EMBEDDED {
   public:
    explicit Iterator(BitVector* target)
        : target_(target),
          current_index_(0),
          current_value_(target->data_[0]),
          current_(-1) {
28
      DCHECK(target->data_length_ > 0);
29 30
      Advance();
    }
31
    ~Iterator() {}
32 33 34 35 36

    bool Done() const { return current_index_ >= target_->data_length_; }
    void Advance();

    int Current() const {
37
      DCHECK(!Done());
38 39 40 41
      return current_;
    }

   private:
42
    uintptr_t SkipZeroBytes(uintptr_t val) {
43 44 45 46 47 48
      while ((val & 0xFF) == 0) {
        val >>= 8;
        current_ += 8;
      }
      return val;
    }
49
    uintptr_t SkipZeroBits(uintptr_t val) {
50 51 52 53 54 55
      while ((val & 0x1) == 0) {
        val >>= 1;
        current_++;
      }
      return val;
    }
56

57 58
    BitVector* target_;
    int current_index_;
59
    uintptr_t current_value_;
60 61 62 63 64
    int current_;

    friend class BitVector;
  };

65 66 67 68
  static const int kDataBits = kPointerSize * 8;
  static const int kDataBitShift = kPointerSize == 8 ? 6 : 5;
  static const uintptr_t kOne = 1;  // This saves some static_casts.

69
  BitVector(int length, Zone* zone)
70 71
      : length_(length),
        data_length_(SizeFor(length)),
72
        data_(zone->NewArray<uintptr_t>(data_length_)) {
73
    DCHECK(length > 0);
74
    Clear();
75 76
  }

77
  BitVector(const BitVector& other, Zone* zone)
78
      : length_(other.length()),
79
        data_length_(SizeFor(length_)),
80
        data_(zone->NewArray<uintptr_t>(data_length_)) {
81 82 83
    CopyFrom(other);
  }

84
  static int SizeFor(int length) { return 1 + ((length - 1) / kDataBits); }
85

86
  void CopyFrom(const BitVector& other) {
87
    DCHECK(other.length() <= length());
88
    for (int i = 0; i < other.data_length_; i++) {
89
      data_[i] = other.data_[i];
90
    }
91 92 93
    for (int i = other.data_length_; i < data_length_; i++) {
      data_[i] = 0;
    }
94 95
  }

96
  bool Contains(int i) const {
97
    DCHECK(i >= 0 && i < length());
98 99
    uintptr_t block = data_[i / kDataBits];
    return (block & (kOne << (i % kDataBits))) != 0;
100 101 102
  }

  void Add(int i) {
103
    DCHECK(i >= 0 && i < length());
104
    data_[i / kDataBits] |= (kOne << (i % kDataBits));
105 106 107
  }

  void Remove(int i) {
108
    DCHECK(i >= 0 && i < length());
109
    data_[i / kDataBits] &= ~(kOne << (i % kDataBits));
110 111 112
  }

  void Union(const BitVector& other) {
113
    DCHECK(other.length() == length());
114 115
    for (int i = 0; i < data_length_; i++) {
      data_[i] |= other.data_[i];
116 117 118
    }
  }

119
  bool UnionIsChanged(const BitVector& other) {
120
    DCHECK(other.length() == length());
121 122
    bool changed = false;
    for (int i = 0; i < data_length_; i++) {
123
      uintptr_t old_data = data_[i];
124 125 126 127 128 129
      data_[i] |= other.data_[i];
      if (data_[i] != old_data) changed = true;
    }
    return changed;
  }

130
  void Intersect(const BitVector& other) {
131
    DCHECK(other.length() == length());
132 133
    for (int i = 0; i < data_length_; i++) {
      data_[i] &= other.data_[i];
134 135 136
    }
  }

137 138 139 140
  bool IntersectIsChanged(const BitVector& other) {
    DCHECK(other.length() == length());
    bool changed = false;
    for (int i = 0; i < data_length_; i++) {
141
      uintptr_t old_data = data_[i];
142 143 144 145 146 147
      data_[i] &= other.data_[i];
      if (data_[i] != old_data) changed = true;
    }
    return changed;
  }

148
  void Subtract(const BitVector& other) {
149
    DCHECK(other.length() == length());
150 151 152 153 154
    for (int i = 0; i < data_length_; i++) {
      data_[i] &= ~other.data_[i];
    }
  }

155
  void Clear() {
156 157
    for (int i = 0; i < data_length_; i++) {
      data_[i] = 0;
158 159 160
    }
  }

161 162 163
  bool IsEmpty() const {
    for (int i = 0; i < data_length_; i++) {
      if (data_[i] != 0) return false;
164 165 166 167
    }
    return true;
  }

168 169 170 171 172 173 174
  bool Equals(const BitVector& other) {
    for (int i = 0; i < data_length_; i++) {
      if (data_[i] != other.data_[i]) return false;
    }
    return true;
  }

175
  int Count() const;
176

177 178
  int length() const { return length_; }

179 180 181 182
#ifdef DEBUG
  void Print();
#endif

183
 private:
184 185 186 187 188
  const int length_;
  const int data_length_;
  uintptr_t* const data_;

  DISALLOW_COPY_AND_ASSIGN(BitVector);
189 190
};

191

192 193
class GrowableBitVector BASE_EMBEDDED {
 public:
194 195 196
  class Iterator BASE_EMBEDDED {
   public:
    Iterator(const GrowableBitVector* target, Zone* zone)
197 198
        : it_(target->bits_ == NULL ? new (zone) BitVector(1, zone)
                                    : target->bits_) {}
199 200 201
    bool Done() const { return it_.Done(); }
    void Advance() { it_.Advance(); }
    int Current() const { return it_.Current(); }
202

203 204 205 206
   private:
    BitVector::Iterator it_;
  };

207
  GrowableBitVector() : bits_(NULL) {}
208
  GrowableBitVector(int length, Zone* zone)
209
      : bits_(new (zone) BitVector(length, zone)) {}
210 211 212 213 214 215 216 217 218 219 220

  bool Contains(int value) const {
    if (!InBitsRange(value)) return false;
    return bits_->Contains(value);
  }

  void Add(int value, Zone* zone) {
    EnsureCapacity(value, zone);
    bits_->Add(value);
  }

221 222 223 224 225 226
  void Union(const GrowableBitVector& other, Zone* zone) {
    for (Iterator it(&other, zone); !it.Done(); it.Advance()) {
      Add(it.Current(), zone);
    }
  }

227 228 229
  void Clear() {
    if (bits_ != NULL) bits_->Clear();
  }
230

231 232 233 234 235 236 237 238 239 240 241
 private:
  static const int kInitialLength = 1024;

  bool InBitsRange(int value) const {
    return bits_ != NULL && bits_->length() > value;
  }

  void EnsureCapacity(int value, Zone* zone) {
    if (InBitsRange(value)) return;
    int new_length = bits_ == NULL ? kInitialLength : bits_->length();
    while (new_length <= value) new_length *= 2;
242
    BitVector* new_bits = new (zone) BitVector(new_length, zone);
243 244 245 246 247 248 249
    if (bits_ != NULL) new_bits->CopyFrom(*bits_);
    bits_ = new_bits;
  }

  BitVector* bits_;
};

250 251
}  // namespace internal
}  // namespace v8
252

253
#endif  // V8_DATAFLOW_H_