OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8_COMPILER_GENERIC_NODE_INL_H_ | |
6 #define V8_COMPILER_GENERIC_NODE_INL_H_ | |
7 | |
8 #include "src/v8.h" | |
9 | |
10 #include "src/compiler/generic-node.h" | |
11 #include "src/compiler/graph.h" | |
12 #include "src/zone.h" | |
13 | |
14 namespace v8 { | |
15 namespace internal { | |
16 namespace compiler { | |
17 | |
18 template <class B, class S> | |
19 GenericNode<B, S>::GenericNode(Graph* graph, int input_count, | |
20 int reserve_input_count) | |
21 : BaseClass(graph->zone()), | |
22 input_count_(input_count), | |
23 reserve_input_count_(reserve_input_count), | |
24 has_appendable_inputs_(false), | |
25 use_count_(0), | |
26 first_use_(NULL), | |
27 last_use_(NULL) { | |
28 DCHECK(reserve_input_count <= kMaxReservedInputs); | |
29 inputs_.static_ = reinterpret_cast<Input*>(this + 1); | |
30 AssignUniqueID(graph); | |
31 } | |
32 | |
33 template <class B, class S> | |
34 inline void GenericNode<B, S>::AssignUniqueID(Graph* graph) { | |
35 id_ = graph->NextNodeID(); | |
36 } | |
37 | |
38 template <class B, class S> | |
39 inline typename GenericNode<B, S>::Inputs::iterator | |
40 GenericNode<B, S>::Inputs::begin() { | |
41 return typename GenericNode<B, S>::Inputs::iterator(this->node_, 0); | |
42 } | |
43 | |
44 template <class B, class S> | |
45 inline typename GenericNode<B, S>::Inputs::iterator | |
46 GenericNode<B, S>::Inputs::end() { | |
47 return typename GenericNode<B, S>::Inputs::iterator( | |
48 this->node_, this->node_->InputCount()); | |
49 } | |
50 | |
51 template <class B, class S> | |
52 inline typename GenericNode<B, S>::Uses::iterator | |
53 GenericNode<B, S>::Uses::begin() { | |
54 return typename GenericNode<B, S>::Uses::iterator(this->node_); | |
55 } | |
56 | |
57 template <class B, class S> | |
58 inline typename GenericNode<B, S>::Uses::iterator | |
59 GenericNode<B, S>::Uses::end() { | |
60 return typename GenericNode<B, S>::Uses::iterator(); | |
61 } | |
62 | |
63 template <class B, class S> | |
64 void GenericNode<B, S>::ReplaceUses(GenericNode* replace_to) { | |
65 for (Use* use = first_use_; use != NULL; use = use->next) { | |
66 use->from->GetInputRecordPtr(use->input_index)->to = replace_to; | |
67 } | |
68 if (replace_to->last_use_ == NULL) { | |
69 DCHECK_EQ(NULL, replace_to->first_use_); | |
70 replace_to->first_use_ = first_use_; | |
71 replace_to->last_use_ = last_use_; | |
72 } else if (first_use_ != NULL) { | |
73 DCHECK_NE(NULL, replace_to->first_use_); | |
74 replace_to->last_use_->next = first_use_; | |
75 first_use_->prev = replace_to->last_use_; | |
76 replace_to->last_use_ = last_use_; | |
77 } | |
78 replace_to->use_count_ += use_count_; | |
79 use_count_ = 0; | |
80 first_use_ = NULL; | |
81 last_use_ = NULL; | |
82 } | |
83 | |
84 template <class B, class S> | |
85 template <class UnaryPredicate> | |
86 void GenericNode<B, S>::ReplaceUsesIf(UnaryPredicate pred, | |
87 GenericNode* replace_to) { | |
88 for (Use* use = first_use_; use != NULL;) { | |
89 Use* next = use->next; | |
90 if (pred(static_cast<S*>(use->from))) { | |
91 RemoveUse(use); | |
92 replace_to->AppendUse(use); | |
93 use->from->GetInputRecordPtr(use->input_index)->to = replace_to; | |
94 } | |
95 use = next; | |
96 } | |
97 } | |
98 | |
99 template <class B, class S> | |
100 void GenericNode<B, S>::RemoveAllInputs() { | |
101 for (typename Inputs::iterator iter(inputs().begin()); iter != inputs().end(); | |
102 ++iter) { | |
103 iter.GetInput()->Update(NULL); | |
104 } | |
105 } | |
106 | |
107 template <class B, class S> | |
108 void GenericNode<B, S>::TrimInputCount(int new_input_count) { | |
109 if (new_input_count == input_count_) return; // Nothing to do. | |
110 | |
111 DCHECK(new_input_count < input_count_); | |
112 | |
113 // Update inline inputs. | |
114 for (int i = new_input_count; i < input_count_; i++) { | |
115 typename GenericNode<B, S>::Input* input = GetInputRecordPtr(i); | |
116 input->Update(NULL); | |
117 } | |
118 input_count_ = new_input_count; | |
119 } | |
120 | |
121 template <class B, class S> | |
122 void GenericNode<B, S>::ReplaceInput(int index, GenericNode<B, S>* new_to) { | |
123 Input* input = GetInputRecordPtr(index); | |
124 input->Update(new_to); | |
125 } | |
126 | |
127 template <class B, class S> | |
128 void GenericNode<B, S>::Input::Update(GenericNode<B, S>* new_to) { | |
129 GenericNode* old_to = this->to; | |
130 if (new_to == old_to) return; // Nothing to do. | |
131 // Snip out the use from where it used to be | |
132 if (old_to != NULL) { | |
133 old_to->RemoveUse(use); | |
134 } | |
135 to = new_to; | |
136 // And put it into the new node's use list. | |
137 if (new_to != NULL) { | |
138 new_to->AppendUse(use); | |
139 } else { | |
140 use->next = NULL; | |
141 use->prev = NULL; | |
142 } | |
143 } | |
144 | |
145 template <class B, class S> | |
146 void GenericNode<B, S>::EnsureAppendableInputs(Zone* zone) { | |
147 if (!has_appendable_inputs_) { | |
148 void* deque_buffer = zone->New(sizeof(InputDeque)); | |
149 InputDeque* deque = new (deque_buffer) InputDeque(zone); | |
150 for (int i = 0; i < input_count_; ++i) { | |
151 deque->push_back(inputs_.static_[i]); | |
152 } | |
153 inputs_.appendable_ = deque; | |
154 has_appendable_inputs_ = true; | |
155 } | |
156 } | |
157 | |
158 template <class B, class S> | |
159 void GenericNode<B, S>::AppendInput(Zone* zone, GenericNode<B, S>* to_append) { | |
160 Use* new_use = new (zone) Use; | |
161 Input new_input; | |
162 new_input.to = to_append; | |
163 new_input.use = new_use; | |
164 if (reserve_input_count_ > 0) { | |
165 DCHECK(!has_appendable_inputs_); | |
166 reserve_input_count_--; | |
167 inputs_.static_[input_count_] = new_input; | |
168 } else { | |
169 EnsureAppendableInputs(zone); | |
170 inputs_.appendable_->push_back(new_input); | |
171 } | |
172 new_use->input_index = input_count_; | |
173 new_use->from = this; | |
174 to_append->AppendUse(new_use); | |
175 input_count_++; | |
176 } | |
177 | |
178 template <class B, class S> | |
179 void GenericNode<B, S>::InsertInput(Zone* zone, int index, | |
180 GenericNode<B, S>* to_insert) { | |
181 DCHECK(index >= 0 && index < InputCount()); | |
182 // TODO(turbofan): Optimize this implementation! | |
183 AppendInput(zone, InputAt(InputCount() - 1)); | |
184 for (int i = InputCount() - 1; i > index; --i) { | |
185 ReplaceInput(i, InputAt(i - 1)); | |
186 } | |
187 ReplaceInput(index, to_insert); | |
188 } | |
189 | |
190 template <class B, class S> | |
191 void GenericNode<B, S>::RemoveInput(int index) { | |
192 DCHECK(index >= 0 && index < InputCount()); | |
193 // TODO(turbofan): Optimize this implementation! | |
194 for (; index < InputCount() - 1; ++index) { | |
195 ReplaceInput(index, InputAt(index + 1)); | |
196 } | |
197 TrimInputCount(InputCount() - 1); | |
198 } | |
199 | |
200 template <class B, class S> | |
201 void GenericNode<B, S>::AppendUse(Use* use) { | |
202 use->next = NULL; | |
203 use->prev = last_use_; | |
204 if (last_use_ == NULL) { | |
205 first_use_ = use; | |
206 } else { | |
207 last_use_->next = use; | |
208 } | |
209 last_use_ = use; | |
210 ++use_count_; | |
211 } | |
212 | |
213 template <class B, class S> | |
214 void GenericNode<B, S>::RemoveUse(Use* use) { | |
215 if (last_use_ == use) { | |
216 last_use_ = use->prev; | |
217 } | |
218 if (use->prev != NULL) { | |
219 use->prev->next = use->next; | |
220 } else { | |
221 first_use_ = use->next; | |
222 } | |
223 if (use->next != NULL) { | |
224 use->next->prev = use->prev; | |
225 } | |
226 --use_count_; | |
227 } | |
228 | |
229 template <class B, class S> | |
230 inline bool GenericNode<B, S>::OwnedBy(GenericNode* owner) const { | |
231 return first_use_ != NULL && first_use_->from == owner && | |
232 first_use_->next == NULL; | |
233 } | |
234 | |
235 template <class B, class S> | |
236 S* GenericNode<B, S>::New(Graph* graph, int input_count, S** inputs, | |
237 bool has_extensible_inputs) { | |
238 size_t node_size = sizeof(GenericNode); | |
239 int reserve_input_count = has_extensible_inputs ? kDefaultReservedInputs : 0; | |
240 size_t inputs_size = (input_count + reserve_input_count) * sizeof(Input); | |
241 size_t uses_size = input_count * sizeof(Use); | |
242 int size = static_cast<int>(node_size + inputs_size + uses_size); | |
243 Zone* zone = graph->zone(); | |
244 void* buffer = zone->New(size); | |
245 S* result = new (buffer) S(graph, input_count, reserve_input_count); | |
246 Input* input = | |
247 reinterpret_cast<Input*>(reinterpret_cast<char*>(buffer) + node_size); | |
248 Use* use = | |
249 reinterpret_cast<Use*>(reinterpret_cast<char*>(input) + inputs_size); | |
250 | |
251 for (int current = 0; current < input_count; ++current) { | |
252 GenericNode* to = *inputs++; | |
253 input->to = to; | |
254 input->use = use; | |
255 use->input_index = current; | |
256 use->from = result; | |
257 to->AppendUse(use); | |
258 ++use; | |
259 ++input; | |
260 } | |
261 return result; | |
262 } | |
263 } | |
264 } | |
265 } // namespace v8::internal::compiler | |
266 | |
267 #endif // V8_COMPILER_GENERIC_NODE_INL_H_ | |
OLD | NEW |