Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_DATAFLOW_H_ |
| 6 | #define V8_DATAFLOW_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/v8.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/allocation.h" |
| 11 | #include "src/ast.h" |
| 12 | #include "src/compiler.h" |
| 13 | #include "src/zone-inl.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 18 | class BitVector : public ZoneObject { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 19 | public: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 20 | // Iterator for the elements of this BitVector. |
| 21 | class Iterator BASE_EMBEDDED { |
| 22 | public: |
| 23 | explicit Iterator(BitVector* target) |
| 24 | : target_(target), |
| 25 | current_index_(0), |
| 26 | current_value_(target->data_[0]), |
| 27 | current_(-1) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | DCHECK(target->data_length_ > 0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 29 | Advance(); |
| 30 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 31 | ~Iterator() {} |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 32 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 33 | bool Done() const { return current_index_ >= target_->data_length_; } |
| 34 | void Advance(); |
| 35 | |
| 36 | int Current() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | DCHECK(!Done()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 38 | return current_; |
| 39 | } |
| 40 | |
| 41 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 42 | uintptr_t SkipZeroBytes(uintptr_t val) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 43 | while ((val & 0xFF) == 0) { |
| 44 | val >>= 8; |
| 45 | current_ += 8; |
| 46 | } |
| 47 | return val; |
| 48 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 49 | uintptr_t SkipZeroBits(uintptr_t val) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 50 | while ((val & 0x1) == 0) { |
| 51 | val >>= 1; |
| 52 | current_++; |
| 53 | } |
| 54 | return val; |
| 55 | } |
| 56 | |
| 57 | BitVector* target_; |
| 58 | int current_index_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 59 | uintptr_t current_value_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 60 | int current_; |
| 61 | |
| 62 | friend class BitVector; |
| 63 | }; |
| 64 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 65 | static const int kDataBits = kPointerSize * 8; |
| 66 | static const int kDataBitShift = kPointerSize == 8 ? 6 : 5; |
| 67 | static const uintptr_t kOne = 1; // This saves some static_casts. |
| 68 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 69 | BitVector(int length, Zone* zone) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 70 | : length_(length), |
| 71 | data_length_(SizeFor(length)), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 72 | data_(zone->NewArray<uintptr_t>(data_length_)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | DCHECK(length > 0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 74 | Clear(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 77 | BitVector(const BitVector& other, Zone* zone) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 78 | : length_(other.length()), |
| 79 | data_length_(SizeFor(length_)), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 80 | data_(zone->NewArray<uintptr_t>(data_length_)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 81 | CopyFrom(other); |
| 82 | } |
| 83 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 84 | static int SizeFor(int length) { return 1 + ((length - 1) / kDataBits); } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 85 | |
| 86 | void CopyFrom(const BitVector& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | DCHECK(other.length() <= length()); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 88 | for (int i = 0; i < other.data_length_; i++) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 89 | data_[i] = other.data_[i]; |
| 90 | } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 91 | for (int i = other.data_length_; i < data_length_; i++) { |
| 92 | data_[i] = 0; |
| 93 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 96 | bool Contains(int i) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | DCHECK(i >= 0 && i < length()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 98 | uintptr_t block = data_[i / kDataBits]; |
| 99 | return (block & (kOne << (i % kDataBits))) != 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void Add(int i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | DCHECK(i >= 0 && i < length()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 104 | data_[i / kDataBits] |= (kOne << (i % kDataBits)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Remove(int i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | DCHECK(i >= 0 && i < length()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 109 | data_[i / kDataBits] &= ~(kOne << (i % kDataBits)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void Union(const BitVector& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | DCHECK(other.length() == length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 114 | for (int i = 0; i < data_length_; i++) { |
| 115 | data_[i] |= other.data_[i]; |
| 116 | } |
| 117 | } |
| 118 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 119 | bool UnionIsChanged(const BitVector& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 120 | DCHECK(other.length() == length()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 121 | bool changed = false; |
| 122 | for (int i = 0; i < data_length_; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 123 | uintptr_t old_data = data_[i]; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 124 | data_[i] |= other.data_[i]; |
| 125 | if (data_[i] != old_data) changed = true; |
| 126 | } |
| 127 | return changed; |
| 128 | } |
| 129 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 130 | void Intersect(const BitVector& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | DCHECK(other.length() == length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 132 | for (int i = 0; i < data_length_; i++) { |
| 133 | data_[i] &= other.data_[i]; |
| 134 | } |
| 135 | } |
| 136 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | bool IntersectIsChanged(const BitVector& other) { |
| 138 | DCHECK(other.length() == length()); |
| 139 | bool changed = false; |
| 140 | for (int i = 0; i < data_length_; i++) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 141 | uintptr_t old_data = data_[i]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | data_[i] &= other.data_[i]; |
| 143 | if (data_[i] != old_data) changed = true; |
| 144 | } |
| 145 | return changed; |
| 146 | } |
| 147 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 148 | void Subtract(const BitVector& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | DCHECK(other.length() == length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 150 | for (int i = 0; i < data_length_; i++) { |
| 151 | data_[i] &= ~other.data_[i]; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void Clear() { |
| 156 | for (int i = 0; i < data_length_; i++) { |
| 157 | data_[i] = 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | bool IsEmpty() const { |
| 162 | for (int i = 0; i < data_length_; i++) { |
| 163 | if (data_[i] != 0) return false; |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool Equals(const BitVector& other) { |
| 169 | for (int i = 0; i < data_length_; i++) { |
| 170 | if (data_[i] != other.data_[i]) return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | int Count() const; |
| 176 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 177 | int length() const { return length_; } |
| 178 | |
| 179 | #ifdef DEBUG |
| 180 | void Print(); |
| 181 | #endif |
| 182 | |
| 183 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 184 | const int length_; |
| 185 | const int data_length_; |
| 186 | uintptr_t* const data_; |
| 187 | |
| 188 | DISALLOW_COPY_AND_ASSIGN(BitVector); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 191 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 192 | class GrowableBitVector BASE_EMBEDDED { |
| 193 | public: |
| 194 | class Iterator BASE_EMBEDDED { |
| 195 | public: |
| 196 | Iterator(const GrowableBitVector* target, Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 197 | : it_(target->bits_ == NULL ? new (zone) BitVector(1, zone) |
| 198 | : target->bits_) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | bool Done() const { return it_.Done(); } |
| 200 | void Advance() { it_.Advance(); } |
| 201 | int Current() const { return it_.Current(); } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 202 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 203 | private: |
| 204 | BitVector::Iterator it_; |
| 205 | }; |
| 206 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 207 | GrowableBitVector() : bits_(NULL) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 208 | GrowableBitVector(int length, Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 209 | : bits_(new (zone) BitVector(length, zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | |
| 211 | bool Contains(int value) const { |
| 212 | if (!InBitsRange(value)) return false; |
| 213 | return bits_->Contains(value); |
| 214 | } |
| 215 | |
| 216 | void Add(int value, Zone* zone) { |
| 217 | EnsureCapacity(value, zone); |
| 218 | bits_->Add(value); |
| 219 | } |
| 220 | |
| 221 | void Union(const GrowableBitVector& other, Zone* zone) { |
| 222 | for (Iterator it(&other, zone); !it.Done(); it.Advance()) { |
| 223 | Add(it.Current(), zone); |
| 224 | } |
| 225 | } |
| 226 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 227 | void Clear() { |
| 228 | if (bits_ != NULL) bits_->Clear(); |
| 229 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 230 | |
| 231 | private: |
| 232 | static const int kInitialLength = 1024; |
| 233 | |
| 234 | bool InBitsRange(int value) const { |
| 235 | return bits_ != NULL && bits_->length() > value; |
| 236 | } |
| 237 | |
| 238 | void EnsureCapacity(int value, Zone* zone) { |
| 239 | if (InBitsRange(value)) return; |
| 240 | int new_length = bits_ == NULL ? kInitialLength : bits_->length(); |
| 241 | while (new_length <= value) new_length *= 2; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 242 | BitVector* new_bits = new (zone) BitVector(new_length, zone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | if (bits_ != NULL) new_bits->CopyFrom(*bits_); |
| 244 | bits_ = new_bits; |
| 245 | } |
| 246 | |
| 247 | BitVector* bits_; |
| 248 | }; |
| 249 | |
| 250 | } // namespace internal |
| 251 | } // namespace v8 |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 252 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 253 | #endif // V8_DATAFLOW_H_ |