blob: 71f56e718b92861cfff2f3ad08814f2d39b7d39f [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Leon Clarke4515c472010-02-03 11:58:03 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_DATAFLOW_H_
29#define V8_DATAFLOW_H_
30
Steve Block6ded16b2010-05-10 14:33:55 +010031#include "v8.h"
32
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "allocation.h"
Leon Clarke4515c472010-02-03 11:58:03 +000034#include "ast.h"
Andrei Popescu31002712010-02-23 13:46:05 +000035#include "compiler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010036#include "zone-inl.h"
Leon Clarke4515c472010-02-03 11:58:03 +000037
38namespace v8 {
39namespace internal {
40
Steve Block6ded16b2010-05-10 14:33:55 +010041class BitVector: public ZoneObject {
42 public:
Ben Murdochb0fe1622011-05-05 13:52:32 +010043 // Iterator for the elements of this BitVector.
44 class Iterator BASE_EMBEDDED {
45 public:
46 explicit Iterator(BitVector* target)
47 : target_(target),
48 current_index_(0),
49 current_value_(target->data_[0]),
50 current_(-1) {
51 ASSERT(target->data_length_ > 0);
52 Advance();
53 }
54 ~Iterator() { }
Kristian Monsen80d68ea2010-09-08 11:05:35 +010055
Ben Murdochb0fe1622011-05-05 13:52:32 +010056 bool Done() const { return current_index_ >= target_->data_length_; }
57 void Advance();
58
59 int Current() const {
60 ASSERT(!Done());
61 return current_;
62 }
63
64 private:
65 uint32_t SkipZeroBytes(uint32_t val) {
66 while ((val & 0xFF) == 0) {
67 val >>= 8;
68 current_ += 8;
69 }
70 return val;
71 }
72 uint32_t SkipZeroBits(uint32_t val) {
73 while ((val & 0x1) == 0) {
74 val >>= 1;
75 current_++;
76 }
77 return val;
78 }
79
80 BitVector* target_;
81 int current_index_;
82 uint32_t current_value_;
83 int current_;
84
85 friend class BitVector;
86 };
87
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088 BitVector(int length, Zone* zone)
Ben Murdochb0fe1622011-05-05 13:52:32 +010089 : length_(length),
90 data_length_(SizeFor(length)),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010091 data_(zone->NewArray<uint32_t>(data_length_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +010092 ASSERT(length > 0);
93 Clear();
Steve Block6ded16b2010-05-10 14:33:55 +010094 }
95
Ben Murdoch3ef787d2012-04-12 10:51:47 +010096 BitVector(const BitVector& other, Zone* zone)
Steve Block6ded16b2010-05-10 14:33:55 +010097 : length_(other.length()),
98 data_length_(SizeFor(length_)),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010099 data_(zone->NewArray<uint32_t>(data_length_)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100100 CopyFrom(other);
101 }
102
Ben Murdochb0fe1622011-05-05 13:52:32 +0100103 static int SizeFor(int length) {
104 return 1 + ((length - 1) / 32);
Steve Block6ded16b2010-05-10 14:33:55 +0100105 }
106
107 BitVector& operator=(const BitVector& rhs) {
108 if (this != &rhs) CopyFrom(rhs);
109 return *this;
110 }
111
112 void CopyFrom(const BitVector& other) {
Steve Block9fac8402011-05-12 15:51:54 +0100113 ASSERT(other.length() <= length());
114 for (int i = 0; i < other.data_length_; i++) {
Steve Block6ded16b2010-05-10 14:33:55 +0100115 data_[i] = other.data_[i];
116 }
Steve Block9fac8402011-05-12 15:51:54 +0100117 for (int i = other.data_length_; i < data_length_; i++) {
118 data_[i] = 0;
119 }
Steve Block6ded16b2010-05-10 14:33:55 +0100120 }
121
Ben Murdochb0fe1622011-05-05 13:52:32 +0100122 bool Contains(int i) const {
Steve Block6ded16b2010-05-10 14:33:55 +0100123 ASSERT(i >= 0 && i < length());
124 uint32_t block = data_[i / 32];
125 return (block & (1U << (i % 32))) != 0;
126 }
127
128 void Add(int i) {
129 ASSERT(i >= 0 && i < length());
130 data_[i / 32] |= (1U << (i % 32));
131 }
132
133 void Remove(int i) {
134 ASSERT(i >= 0 && i < length());
135 data_[i / 32] &= ~(1U << (i % 32));
136 }
137
138 void Union(const BitVector& other) {
139 ASSERT(other.length() == length());
140 for (int i = 0; i < data_length_; i++) {
141 data_[i] |= other.data_[i];
142 }
143 }
144
Ben Murdochb0fe1622011-05-05 13:52:32 +0100145 bool UnionIsChanged(const BitVector& other) {
146 ASSERT(other.length() == length());
147 bool changed = false;
148 for (int i = 0; i < data_length_; i++) {
149 uint32_t old_data = data_[i];
150 data_[i] |= other.data_[i];
151 if (data_[i] != old_data) changed = true;
152 }
153 return changed;
154 }
155
Steve Block6ded16b2010-05-10 14:33:55 +0100156 void Intersect(const BitVector& other) {
157 ASSERT(other.length() == length());
158 for (int i = 0; i < data_length_; i++) {
159 data_[i] &= other.data_[i];
160 }
161 }
162
163 void Subtract(const BitVector& other) {
164 ASSERT(other.length() == length());
165 for (int i = 0; i < data_length_; i++) {
166 data_[i] &= ~other.data_[i];
167 }
168 }
169
170 void Clear() {
171 for (int i = 0; i < data_length_; i++) {
172 data_[i] = 0;
173 }
174 }
175
176 bool IsEmpty() const {
177 for (int i = 0; i < data_length_; i++) {
178 if (data_[i] != 0) return false;
179 }
180 return true;
181 }
182
183 bool Equals(const BitVector& other) {
184 for (int i = 0; i < data_length_; i++) {
185 if (data_[i] != other.data_[i]) return false;
186 }
187 return true;
188 }
189
190 int length() const { return length_; }
191
192#ifdef DEBUG
193 void Print();
194#endif
195
196 private:
197 int length_;
198 int data_length_;
199 uint32_t* data_;
200};
201
Leon Clarke4515c472010-02-03 11:58:03 +0000202} } // namespace v8::internal
203
Andrei Popescu402d9372010-02-26 13:31:12 +0000204
Leon Clarke4515c472010-02-03 11:58:03 +0000205#endif // V8_DATAFLOW_H_