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_
|
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
2011-05-06 06:50:20 +00:00
|
|
|
#include "allocation.h"
|
2010-01-29 09:42:13 +00:00
|
|
|
#include "ast.h"
|
2010-02-03 16:12:55 +00:00
|
|
|
#include "compiler.h"
|
2010-03-10 08:08:32 +00:00
|
|
|
#include "zone-inl.h"
|
2010-01-29 09:42:13 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2010-03-10 08:08:32 +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) {
|
|
|
|
ASSERT(target->data_length_ > 0);
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
~Iterator() { }
|
|
|
|
|
|
|
|
bool Done() const { return current_index_ >= target_->data_length_; }
|
|
|
|
void Advance();
|
|
|
|
|
|
|
|
int Current() const {
|
|
|
|
ASSERT(!Done());
|
|
|
|
return current_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t SkipZeroBytes(uint32_t val) {
|
|
|
|
while ((val & 0xFF) == 0) {
|
|
|
|
val >>= 8;
|
|
|
|
current_ += 8;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
uint32_t SkipZeroBits(uint32_t val) {
|
|
|
|
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_;
|
|
|
|
uint32_t current_value_;
|
|
|
|
int current_;
|
|
|
|
|
|
|
|
friend class BitVector;
|
|
|
|
};
|
|
|
|
|
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)),
|
2012-02-22 11:40:28 +00:00
|
|
|
data_(zone->NewArray<uint32_t>(data_length_)) {
|
2010-12-07 11:31:57 +00:00
|
|
|
ASSERT(length > 0);
|
|
|
|
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_)),
|
2012-02-22 11:40:28 +00:00
|
|
|
data_(zone->NewArray<uint32_t>(data_length_)) {
|
2010-03-09 09:51:37 +00:00
|
|
|
CopyFrom(other);
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
static int SizeFor(int length) {
|
|
|
|
return 1 + ((length - 1) / 32);
|
2010-03-10 08:08:32 +00:00
|
|
|
}
|
2010-03-02 10:43:46 +00:00
|
|
|
|
2010-03-09 09:51:37 +00:00
|
|
|
BitVector& operator=(const BitVector& rhs) {
|
|
|
|
if (this != &rhs) CopyFrom(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:43:46 +00:00
|
|
|
void CopyFrom(const BitVector& other) {
|
2011-01-03 17:02:15 +00:00
|
|
|
ASSERT(other.length() <= length());
|
|
|
|
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 {
|
2010-03-02 10:43:46 +00:00
|
|
|
ASSERT(i >= 0 && i < length());
|
2010-03-10 08:08:32 +00:00
|
|
|
uint32_t block = data_[i / 32];
|
2010-03-02 10:43:46 +00:00
|
|
|
return (block & (1U << (i % 32))) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Add(int i) {
|
|
|
|
ASSERT(i >= 0 && i < length());
|
2010-03-10 08:08:32 +00:00
|
|
|
data_[i / 32] |= (1U << (i % 32));
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(int i) {
|
|
|
|
ASSERT(i >= 0 && i < length());
|
2010-03-10 08:08:32 +00:00
|
|
|
data_[i / 32] &= ~(1U << (i % 32));
|
2010-03-02 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Union(const BitVector& other) {
|
|
|
|
ASSERT(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) {
|
|
|
|
ASSERT(other.length() == length());
|
|
|
|
bool changed = false;
|
|
|
|
for (int i = 0; i < data_length_; i++) {
|
|
|
|
uint32_t old_data = data_[i];
|
|
|
|
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) {
|
|
|
|
ASSERT(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-03-11 16:24:05 +00:00
|
|
|
void Subtract(const BitVector& other) {
|
|
|
|
ASSERT(other.length() == length());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
int length_;
|
2010-03-10 08:08:32 +00:00
|
|
|
int data_length_;
|
|
|
|
uint32_t* data_;
|
2010-03-02 10:43:46 +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)
|
|
|
|
: it_(target->bits_ == NULL
|
|
|
|
? new(zone) BitVector(1, zone)
|
|
|
|
: target->bits_) { }
|
|
|
|
bool Done() const { return it_.Done(); }
|
|
|
|
void Advance() { it_.Advance(); }
|
|
|
|
int Current() const { return it_.Current(); }
|
|
|
|
private:
|
|
|
|
BitVector::Iterator it_;
|
|
|
|
};
|
|
|
|
|
2013-01-04 12:48:18 +00:00
|
|
|
GrowableBitVector() : bits_(NULL) { }
|
2013-06-12 14:22:49 +00:00
|
|
|
GrowableBitVector(int length, Zone* zone)
|
|
|
|
: 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() { if (bits_ != NULL) bits_->Clear(); }
|
|
|
|
|
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;
|
|
|
|
BitVector* new_bits = new(zone) BitVector(new_length, zone);
|
|
|
|
if (bits_ != NULL) new_bits->CopyFrom(*bits_);
|
|
|
|
bits_ = new_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector* bits_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-29 09:42:13 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
2010-02-12 10:16:30 +00:00
|
|
|
|
2010-01-29 09:42:13 +00:00
|
|
|
#endif // V8_DATAFLOW_H_
|