1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/allocation.h"
12 #include "src/compiler.h"
13 #include "src/zone-inl.h"
18 class BitVector: public ZoneObject {
20 // Iterator for the elements of this BitVector.
21 class Iterator BASE_EMBEDDED {
23 explicit Iterator(BitVector* target)
26 current_value_(target->data_[0]),
28 DCHECK(target->data_length_ > 0);
33 bool Done() const { return current_index_ >= target_->data_length_; }
42 uint32_t SkipZeroBytes(uint32_t val) {
43 while ((val & 0xFF) == 0) {
49 uint32_t SkipZeroBits(uint32_t val) {
50 while ((val & 0x1) == 0) {
59 uint32_t current_value_;
62 friend class BitVector;
65 BitVector(int length, Zone* zone)
67 data_length_(SizeFor(length)),
68 data_(zone->NewArray<uint32_t>(data_length_)) {
73 BitVector(const BitVector& other, Zone* zone)
74 : length_(other.length()),
75 data_length_(SizeFor(length_)),
76 data_(zone->NewArray<uint32_t>(data_length_)) {
80 static int SizeFor(int length) {
81 return 1 + ((length - 1) / 32);
84 BitVector& operator=(const BitVector& rhs) {
85 if (this != &rhs) CopyFrom(rhs);
89 void CopyFrom(const BitVector& other) {
90 DCHECK(other.length() <= length());
91 for (int i = 0; i < other.data_length_; i++) {
92 data_[i] = other.data_[i];
94 for (int i = other.data_length_; i < data_length_; i++) {
99 bool Contains(int i) const {
100 DCHECK(i >= 0 && i < length());
101 uint32_t block = data_[i / 32];
102 return (block & (1U << (i % 32))) != 0;
106 DCHECK(i >= 0 && i < length());
107 data_[i / 32] |= (1U << (i % 32));
111 DCHECK(i >= 0 && i < length());
112 data_[i / 32] &= ~(1U << (i % 32));
115 void Union(const BitVector& other) {
116 DCHECK(other.length() == length());
117 for (int i = 0; i < data_length_; i++) {
118 data_[i] |= other.data_[i];
122 bool UnionIsChanged(const BitVector& other) {
123 DCHECK(other.length() == length());
124 bool changed = false;
125 for (int i = 0; i < data_length_; i++) {
126 uint32_t old_data = data_[i];
127 data_[i] |= other.data_[i];
128 if (data_[i] != old_data) changed = true;
133 void Intersect(const BitVector& other) {
134 DCHECK(other.length() == length());
135 for (int i = 0; i < data_length_; i++) {
136 data_[i] &= other.data_[i];
140 bool IntersectIsChanged(const BitVector& other) {
141 DCHECK(other.length() == length());
142 bool changed = false;
143 for (int i = 0; i < data_length_; i++) {
144 uint32_t old_data = data_[i];
145 data_[i] &= other.data_[i];
146 if (data_[i] != old_data) changed = true;
151 void Subtract(const BitVector& other) {
152 DCHECK(other.length() == length());
153 for (int i = 0; i < data_length_; i++) {
154 data_[i] &= ~other.data_[i];
159 for (int i = 0; i < data_length_; i++) {
164 bool IsEmpty() const {
165 for (int i = 0; i < data_length_; i++) {
166 if (data_[i] != 0) return false;
171 bool Equals(const BitVector& other) {
172 for (int i = 0; i < data_length_; i++) {
173 if (data_[i] != other.data_[i]) return false;
180 int length() const { return length_; }
193 class GrowableBitVector BASE_EMBEDDED {
195 class Iterator BASE_EMBEDDED {
197 Iterator(const GrowableBitVector* target, Zone* zone)
198 : it_(target->bits_ == NULL
199 ? new(zone) BitVector(1, zone)
201 bool Done() const { return it_.Done(); }
202 void Advance() { it_.Advance(); }
203 int Current() const { return it_.Current(); }
205 BitVector::Iterator it_;
208 GrowableBitVector() : bits_(NULL) { }
209 GrowableBitVector(int length, Zone* zone)
210 : bits_(new(zone) BitVector(length, zone)) { }
212 bool Contains(int value) const {
213 if (!InBitsRange(value)) return false;
214 return bits_->Contains(value);
217 void Add(int value, Zone* zone) {
218 EnsureCapacity(value, zone);
222 void Union(const GrowableBitVector& other, Zone* zone) {
223 for (Iterator it(&other, zone); !it.Done(); it.Advance()) {
224 Add(it.Current(), zone);
228 void Clear() { if (bits_ != NULL) bits_->Clear(); }
231 static const int kInitialLength = 1024;
233 bool InBitsRange(int value) const {
234 return bits_ != NULL && bits_->length() > value;
237 void EnsureCapacity(int value, Zone* zone) {
238 if (InBitsRange(value)) return;
239 int new_length = bits_ == NULL ? kInitialLength : bits_->length();
240 while (new_length <= value) new_length *= 2;
241 BitVector* new_bits = new(zone) BitVector(new_length, zone);
242 if (bits_ != NULL) new_bits->CopyFrom(*bits_);
249 } // namespace internal
252 #endif // V8_DATAFLOW_H_