2012-02-22 11:40:28 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-01-29 09:42:13 +00:00
|
|
|
|
|
|
|
#ifndef V8_DATAFLOW_H_
|
|
|
|
#define V8_DATAFLOW_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation.h"
|
2015-02-11 15:49:02 +00:00
|
|
|
#include "src/zone.h"
|
2010-01-29 09:42:13 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2014-10-31 10:44:04 +00:00
|
|
|
class BitVector : public ZoneObject {
|
2010-03-02 10:43:46 +00:00
|
|
|
public:
|
2010-12-07 11:31:57 +00:00
|
|
|
// 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) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(target->data_length_ > 0);
|
2010-12-07 11:31:57 +00:00
|
|
|
Advance();
|
|
|
|
}
|
2014-10-31 10:44:04 +00:00
|
|
|
~Iterator() {}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
bool Done() const { return current_index_ >= target_->data_length_; }
|
|
|
|
void Advance();
|
|
|
|
|
|
|
|
int Current() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!Done());
|
2010-12-07 11:31:57 +00:00
|
|
|
return current_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t SkipZeroBytes(uintptr_t val) {
|
2010-12-07 11:31:57 +00:00
|
|
|
while ((val & 0xFF) == 0) {
|
|
|
|
val >>= 8;
|
|
|
|
current_ += 8;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t SkipZeroBits(uintptr_t val) {
|
2010-12-07 11:31:57 +00:00
|
|
|
while ((val & 0x1) == 0) {
|
|
|
|
val >>= 1;
|
|
|
|
current_++;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2010-08-24 07:26:49 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
BitVector* target_;
|
|
|
|
int current_index_;
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t current_value_;
|
2010-12-07 11:31:57 +00:00
|
|
|
int current_;
|
|
|
|
|
|
|
|
friend class BitVector;
|
|
|
|
};
|
|
|
|
|
2014-10-31 10:44:04 +00:00
|
|
|
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.
|
|
|
|
|
2012-02-22 11:40:28 +00:00
|
|
|
BitVector(int length, Zone* zone)
|
2010-12-07 11:31:57 +00:00
|
|
|
: length_(length),
|
|
|
|
data_length_(SizeFor(length)),
|
2014-10-31 10:44:04 +00:00
|
|
|
data_(zone->NewArray<uintptr_t>(data_length_)) {
|
2015-05-08 12:01:46 +00:00
|
|
|
DCHECK_LE(0, length);
|
2010-12-07 11:31:57 +00:00
|
|
|
Clear();
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 11:40:28 +00:00
|
|
|
BitVector(const BitVector& other, Zone* zone)
|
2010-03-09 09:51:37 +00:00
|
|
|
: length_(other.length()),
|
2010-03-10 08:08:32 +00:00
|
|
|
data_length_(SizeFor(length_)),
|
2014-10-31 10:44:04 +00:00
|
|
|
data_(zone->NewArray<uintptr_t>(data_length_)) {
|
2010-03-09 09:51:37 +00:00
|
|
|
CopyFrom(other);
|
|
|
|
}
|
|
|
|
|
2015-05-08 12:01:46 +00:00
|
|
|
static int SizeFor(int length) {
|
|
|
|
if (length == 0) return 1;
|
|
|
|
return 1 + ((length - 1) / kDataBits);
|
|
|
|
}
|
2010-03-09 09:51:37 +00:00
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
void CopyFrom(const BitVector& other) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(other.length() <= length());
|
2011-01-03 17:02:15 +00:00
|
|
|
for (int i = 0; i < other.data_length_; i++) {
|
2010-03-10 08:08:32 +00:00
|
|
|
data_[i] = other.data_[i];
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
2011-01-03 17:02:15 +00:00
|
|
|
for (int i = other.data_length_; i < data_length_; i++) {
|
|
|
|
data_[i] = 0;
|
|
|
|
}
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
bool Contains(int i) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(i >= 0 && i < length());
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t block = data_[i / kDataBits];
|
|
|
|
return (block & (kOne << (i % kDataBits))) != 0;
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Add(int i) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(i >= 0 && i < length());
|
2014-10-31 10:44:04 +00:00
|
|
|
data_[i / kDataBits] |= (kOne << (i % kDataBits));
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 15:56:58 +00:00
|
|
|
void AddAll() { memset(data_, -1, sizeof(uintptr_t) * data_length_); }
|
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
void Remove(int i) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(i >= 0 && i < length());
|
2014-10-31 10:44:04 +00:00
|
|
|
data_[i / kDataBits] &= ~(kOne << (i % kDataBits));
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Union(const BitVector& other) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(other.length() == length());
|
2010-03-10 08:08:32 +00:00
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
data_[i] |= other.data_[i];
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
bool UnionIsChanged(const BitVector& other) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(other.length() == length());
|
2010-12-07 11:31:57 +00:00
|
|
|
bool changed = false;
|
|
|
|
for (int i = 0; i < data_length_; i++) {
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t old_data = data_[i];
|
2010-12-07 11:31:57 +00:00
|
|
|
data_[i] |= other.data_[i];
|
|
|
|
if (data_[i] != old_data) changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
void Intersect(const BitVector& other) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(other.length() == length());
|
2010-03-10 08:08:32 +00:00
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
data_[i] &= other.data_[i];
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 12:37:51 +00:00
|
|
|
bool IntersectIsChanged(const BitVector& other) {
|
|
|
|
DCHECK(other.length() == length());
|
|
|
|
bool changed = false;
|
|
|
|
for (int i = 0; i < data_length_; i++) {
|
2014-10-31 10:44:04 +00:00
|
|
|
uintptr_t old_data = data_[i];
|
2014-08-22 12:37:51 +00:00
|
|
|
data_[i] &= other.data_[i];
|
|
|
|
if (data_[i] != old_data) changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2010-03-11 16:24:05 +00:00
|
|
|
void Subtract(const BitVector& other) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(other.length() == length());
|
2010-03-11 16:24:05 +00:00
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
data_[i] &= ~other.data_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 09:51:37 +00:00
|
|
|
void Clear() {
|
2010-03-10 08:08:32 +00:00
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
data_[i] = 0;
|
2010-03-09 09:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-10 08:08:32 +00:00
|
|
|
bool IsEmpty() const {
|
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
if (data_[i] != 0) return false;
|
2010-03-09 09:51:37 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-12 10:35:45 +00:00
|
|
|
bool Equals(const BitVector& other) {
|
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
if (data_[i] != other.data_[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 12:10:41 +00:00
|
|
|
int Count() const;
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
int length() const { return length_; }
|
|
|
|
|
2010-03-16 10:54:02 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
void Print();
|
|
|
|
#endif
|
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
private:
|
2014-10-31 10:44:04 +00:00
|
|
|
const int length_;
|
|
|
|
const int data_length_;
|
|
|
|
uintptr_t* const data_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BitVector);
|
2010-03-02 10:43:46 +00:00
|
|
|
};
|
|
|
|
|
2014-08-20 12:10:41 +00:00
|
|
|
|
2013-01-04 12:48:18 +00:00
|
|
|
class GrowableBitVector BASE_EMBEDDED {
|
|
|
|
public:
|
2013-01-07 15:28:20 +00:00
|
|
|
class Iterator BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
Iterator(const GrowableBitVector* target, Zone* zone)
|
2014-10-31 10:44:04 +00:00
|
|
|
: it_(target->bits_ == NULL ? new (zone) BitVector(1, zone)
|
|
|
|
: target->bits_) {}
|
2013-01-07 15:28:20 +00:00
|
|
|
bool Done() const { return it_.Done(); }
|
|
|
|
void Advance() { it_.Advance(); }
|
|
|
|
int Current() const { return it_.Current(); }
|
2014-10-31 10:44:04 +00:00
|
|
|
|
2013-01-07 15:28:20 +00:00
|
|
|
private:
|
|
|
|
BitVector::Iterator it_;
|
|
|
|
};
|
|
|
|
|
2014-10-31 10:44:04 +00:00
|
|
|
GrowableBitVector() : bits_(NULL) {}
|
2013-06-12 14:22:49 +00:00
|
|
|
GrowableBitVector(int length, Zone* zone)
|
2014-10-31 10:44:04 +00:00
|
|
|
: bits_(new (zone) BitVector(length, zone)) {}
|
2013-01-04 12:48:18 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-01-07 15:28:20 +00:00
|
|
|
void Union(const GrowableBitVector& other, Zone* zone) {
|
|
|
|
for (Iterator it(&other, zone); !it.Done(); it.Advance()) {
|
|
|
|
Add(it.Current(), zone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:44:04 +00:00
|
|
|
void Clear() {
|
|
|
|
if (bits_ != NULL) bits_->Clear();
|
|
|
|
}
|
2013-01-07 15:28:20 +00:00
|
|
|
|
2013-01-04 12:48:18 +00:00
|
|
|
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;
|
2014-10-31 10:44:04 +00:00
|
|
|
BitVector* new_bits = new (zone) BitVector(new_length, zone);
|
2013-01-04 12:48:18 +00:00
|
|
|
if (bits_ != NULL) new_bits->CopyFrom(*bits_);
|
|
|
|
bits_ = new_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector* bits_;
|
|
|
|
};
|
|
|
|
|
2014-08-20 12:10:41 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2010-02-12 10:16:30 +00:00
|
|
|
|
2010-01-29 09:42:13 +00:00
|
|
|
#endif // V8_DATAFLOW_H_
|