Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 31 | #include "v8.h" |
| 32 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 33 | #include "ast.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 34 | #include "compiler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 35 | #include "zone-inl.h" |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 40 | // Forward declarations. |
| 41 | class Node; |
| 42 | |
| 43 | class BitVector: public ZoneObject { |
| 44 | public: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 45 | BitVector() : length_(0), data_length_(0), data_(NULL) { } |
| 46 | |
| 47 | explicit BitVector(int length) { |
| 48 | ExpandTo(length); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | BitVector(const BitVector& other) |
| 52 | : length_(other.length()), |
| 53 | data_length_(SizeFor(length_)), |
| 54 | data_(Zone::NewArray<uint32_t>(data_length_)) { |
| 55 | CopyFrom(other); |
| 56 | } |
| 57 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 58 | void ExpandTo(int length) { |
| 59 | ASSERT(length > 0); |
| 60 | length_ = length; |
| 61 | data_length_ = SizeFor(length); |
| 62 | data_ = Zone::NewArray<uint32_t>(data_length_); |
| 63 | Clear(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | BitVector& operator=(const BitVector& rhs) { |
| 67 | if (this != &rhs) CopyFrom(rhs); |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | void CopyFrom(const BitVector& other) { |
| 72 | ASSERT(other.length() == length()); |
| 73 | for (int i = 0; i < data_length_; i++) { |
| 74 | data_[i] = other.data_[i]; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | bool Contains(int i) { |
| 79 | ASSERT(i >= 0 && i < length()); |
| 80 | uint32_t block = data_[i / 32]; |
| 81 | return (block & (1U << (i % 32))) != 0; |
| 82 | } |
| 83 | |
| 84 | void Add(int i) { |
| 85 | ASSERT(i >= 0 && i < length()); |
| 86 | data_[i / 32] |= (1U << (i % 32)); |
| 87 | } |
| 88 | |
| 89 | void Remove(int i) { |
| 90 | ASSERT(i >= 0 && i < length()); |
| 91 | data_[i / 32] &= ~(1U << (i % 32)); |
| 92 | } |
| 93 | |
| 94 | void Union(const BitVector& other) { |
| 95 | ASSERT(other.length() == length()); |
| 96 | for (int i = 0; i < data_length_; i++) { |
| 97 | data_[i] |= other.data_[i]; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void Intersect(const BitVector& other) { |
| 102 | ASSERT(other.length() == length()); |
| 103 | for (int i = 0; i < data_length_; i++) { |
| 104 | data_[i] &= other.data_[i]; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void Subtract(const BitVector& other) { |
| 109 | ASSERT(other.length() == length()); |
| 110 | for (int i = 0; i < data_length_; i++) { |
| 111 | data_[i] &= ~other.data_[i]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void Clear() { |
| 116 | for (int i = 0; i < data_length_; i++) { |
| 117 | data_[i] = 0; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool IsEmpty() const { |
| 122 | for (int i = 0; i < data_length_; i++) { |
| 123 | if (data_[i] != 0) return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool Equals(const BitVector& other) { |
| 129 | for (int i = 0; i < data_length_; i++) { |
| 130 | if (data_[i] != other.data_[i]) return false; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | int length() const { return length_; } |
| 136 | |
| 137 | #ifdef DEBUG |
| 138 | void Print(); |
| 139 | #endif |
| 140 | |
| 141 | private: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 142 | static int SizeFor(int length) { |
| 143 | return 1 + ((length - 1) / 32); |
| 144 | } |
| 145 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 146 | int length_; |
| 147 | int data_length_; |
| 148 | uint32_t* data_; |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | // Simple fixed-capacity list-based worklist (managed as a queue) of |
| 153 | // pointers to T. |
| 154 | template<typename T> |
| 155 | class WorkList BASE_EMBEDDED { |
| 156 | public: |
| 157 | // The worklist cannot grow bigger than size. We keep one item empty to |
| 158 | // distinguish between empty and full. |
| 159 | explicit WorkList(int size) |
| 160 | : capacity_(size + 1), head_(0), tail_(0), queue_(capacity_) { |
| 161 | for (int i = 0; i < capacity_; i++) queue_.Add(NULL); |
| 162 | } |
| 163 | |
| 164 | bool is_empty() { return head_ == tail_; } |
| 165 | |
| 166 | bool is_full() { |
| 167 | // The worklist is full if head is at 0 and tail is at capacity - 1: |
| 168 | // head == 0 && tail == capacity-1 ==> tail - head == capacity - 1 |
| 169 | // or if tail is immediately to the left of head: |
| 170 | // tail+1 == head ==> tail - head == -1 |
| 171 | int diff = tail_ - head_; |
| 172 | return (diff == -1 || diff == capacity_ - 1); |
| 173 | } |
| 174 | |
| 175 | void Insert(T* item) { |
| 176 | ASSERT(!is_full()); |
| 177 | queue_[tail_++] = item; |
| 178 | if (tail_ == capacity_) tail_ = 0; |
| 179 | } |
| 180 | |
| 181 | T* Remove() { |
| 182 | ASSERT(!is_empty()); |
| 183 | T* item = queue_[head_++]; |
| 184 | if (head_ == capacity_) head_ = 0; |
| 185 | return item; |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | int capacity_; // Including one empty slot. |
| 190 | int head_; // Where the first item is. |
| 191 | int tail_; // Where the next inserted item will go. |
| 192 | List<T*> queue_; |
| 193 | }; |
| 194 | |
| 195 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 196 | // Computes the set of assigned variables and annotates variables proxies |
| 197 | // that are trivial sub-expressions and for-loops where the loop variable |
| 198 | // is guaranteed to be a smi. |
| 199 | class AssignedVariablesAnalyzer : public AstVisitor { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 200 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 201 | explicit AssignedVariablesAnalyzer() : info_(NULL) { } |
| 202 | bool Analyze(CompilationInfo* info); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 203 | |
| 204 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 205 | Variable* FindSmiLoopVariable(ForStatement* stmt); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 206 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 207 | int BitIndex(Variable* var); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 208 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 209 | void RecordAssignedVar(Variable* var); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 210 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 211 | void MarkIfTrivial(Expression* expr); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 212 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 213 | // Visits an expression saving the accumulator before, clearing |
| 214 | // it before visting and restoring it after visiting. |
| 215 | void ProcessExpression(Expression* expr); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 216 | |
| 217 | // AST node visit functions. |
| 218 | #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 219 | AST_NODE_LIST(DECLARE_VISIT) |
| 220 | #undef DECLARE_VISIT |
| 221 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 222 | CompilationInfo* info_; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 223 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 224 | // Accumulator for assigned variables set. |
| 225 | BitVector av_; |
| 226 | |
| 227 | DISALLOW_COPY_AND_ASSIGN(AssignedVariablesAnalyzer); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 231 | } } // namespace v8::internal |
| 232 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 233 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 234 | #endif // V8_DATAFLOW_H_ |