OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #include "src/compiler/simd-scalar-lowering.h" | |
6 #include "src/compiler/diamond.h" | |
7 #include "src/compiler/linkage.h" | |
8 #include "src/compiler/node-matchers.h" | |
9 #include "src/compiler/node-properties.h" | |
10 | |
11 #include "src/compiler/node.h" | |
12 #include "src/wasm/wasm-module.h" | |
13 | |
14 namespace v8 { | |
15 namespace internal { | |
16 namespace compiler { | |
17 | |
18 SimdScalarLowering::SimdScalarLowering( | |
19 Graph* graph, MachineOperatorBuilder* machine, | |
20 CommonOperatorBuilder* common, Zone* zone, | |
21 Signature<MachineRepresentation>* signature) | |
22 : zone_(zone), | |
23 graph_(graph), | |
24 machine_(machine), | |
25 common_(common), | |
26 state_(graph, 3), | |
27 stack_(zone), | |
28 replacements_(nullptr), | |
29 signature_(signature), | |
30 placeholder_(graph->NewNode(common->Parameter(-2, "placeholder"), | |
31 graph->start())) { | |
32 DCHECK_NOT_NULL(graph); | |
33 DCHECK_NOT_NULL(graph->end()); | |
34 replacements_ = zone->NewArray<Replacement>(graph->NodeCount()); | |
35 memset(replacements_, 0, sizeof(Replacement) * graph->NodeCount()); | |
36 } | |
37 | |
38 void SimdScalarLowering::LowerGraph() { | |
39 stack_.push_back({graph()->end(), 0}); | |
40 state_.Set(graph()->end(), State::kOnStack); | |
41 replacements_[graph()->end()->id()].type = SimdType::kInt32; | |
42 | |
43 while (!stack_.empty()) { | |
44 NodeState& top = stack_.back(); | |
45 if (top.input_index == top.node->InputCount()) { | |
46 // All inputs of top have already been lowered, now lower top. | |
47 stack_.pop_back(); | |
48 state_.Set(top.node, State::kVisited); | |
49 LowerNode(top.node); | |
50 } else { | |
51 // Push the next input onto the stack. | |
52 Node* input = top.node->InputAt(top.input_index++); | |
53 if (state_.Get(input) == State::kUnvisited) { | |
54 SetLoweredType(input, top.node); | |
55 if (input->opcode() == IrOpcode::kPhi) { | |
56 // To break cycles with phi nodes we push phis on a separate stack so | |
57 // that they are processed after all other nodes. | |
58 PreparePhiReplacement(input); | |
59 stack_.push_front({input, 0}); | |
60 } else { | |
61 stack_.push_back({input, 0}); | |
62 } | |
63 state_.Set(input, State::kOnStack); | |
64 } | |
65 } | |
66 } | |
67 } | |
68 | |
69 #define FOREACH_INT32X4_OPCODE(V) \ | |
70 V(Int32x4Add) \ | |
71 V(Int32x4ExtractLane) \ | |
72 V(CreateInt32x4) | |
73 | |
74 #define FOREACH_FLOAT32X4_OPCODE(V) \ | |
75 V(Float32x4Add) \ | |
76 V(Float32x4ExtractLane) \ | |
77 V(CreateFloat32x4) | |
78 | |
79 void SimdScalarLowering::SetLoweredType(Node* node, Node* output) { | |
80 switch (node->opcode()) { | |
81 #define CASE_STMT(name) case IrOpcode::k##name: | |
82 FOREACH_INT32X4_OPCODE(CASE_STMT) | |
83 case IrOpcode::kReturn: | |
84 case IrOpcode::kParameter: | |
85 case IrOpcode::kCall: { | |
86 replacements_[node->id()].type = SimdType::kInt32; | |
87 break; | |
88 } | |
89 FOREACH_FLOAT32X4_OPCODE(CASE_STMT) { | |
90 replacements_[node->id()].type = SimdType::kFloat32; | |
91 break; | |
92 } | |
93 #undef CASE_STMT | |
94 default: | |
95 replacements_[node->id()].type = replacements_[output->id()].type; | |
96 } | |
97 } | |
98 | |
99 static int GetParameterIndexAfterLowering( | |
100 Signature<MachineRepresentation>* signature, int old_index) { | |
101 // In function calls, the simd128 types are passed as 4 Int32 types. The | |
102 // parameters are typecast to the types as needed for various operations. | |
103 int result = old_index; | |
104 for (int i = 0; i < old_index; i++) { | |
105 if (signature->GetParam(i) == MachineRepresentation::kSimd128) { | |
106 result += 3; | |
107 } | |
108 } | |
109 return result; | |
110 } | |
111 | |
112 int SimdScalarLowering::GetParameterCountAfterLowering( | |
113 Signature<MachineRepresentation>* signature) { | |
114 // GetParameterIndexAfterLowering(parameter_count) returns the parameter count | |
115 // after lowering. | |
116 return GetParameterIndexAfterLowering( | |
117 signature, static_cast<int>(signature->parameter_count())); | |
118 } | |
119 | |
120 static int GetReturnCountAfterLowering( | |
121 Signature<MachineRepresentation>* signature) { | |
122 int result = static_cast<int>(signature->return_count()); | |
123 for (int i = 0; i < static_cast<int>(signature->return_count()); i++) { | |
124 if (signature->GetReturn(i) == MachineRepresentation::kSimd128) { | |
125 result += 3; | |
126 } | |
127 } | |
128 return result; | |
129 } | |
130 | |
131 void SimdScalarLowering::LowerNode(Node* node) { | |
132 SimdType rep_type = ReplacementType(node); | |
133 switch (node->opcode()) { | |
134 case IrOpcode::kStart: { | |
135 int parameter_count = GetParameterCountAfterLowering(signature()); | |
titzer
2016/10/12 19:06:55
We should probably cache the GetParameterCountAfte
aseemgarg
2016/10/17 19:01:44
Done.
| |
136 // Only exchange the node if the parameter count actually changed. | |
137 if (parameter_count != signature()->parameter_count()) { | |
138 int delta = | |
139 parameter_count - static_cast<int>(signature()->parameter_count()); | |
140 int new_output_count = node->op()->ValueOutputCount() + delta; | |
141 NodeProperties::ChangeOp(node, common()->Start(new_output_count)); | |
142 } | |
143 break; | |
144 } | |
145 case IrOpcode::kParameter: { | |
146 DCHECK(node->InputCount() == 1); | |
147 // Only exchange the node if the parameter count actually changed. We do | |
148 // not even have to do the default lowering because the the start node, | |
149 // the only input of a parameter node, only changes if the parameter count | |
150 // changes. | |
151 if (GetParameterCountAfterLowering(signature()) != | |
152 signature()->parameter_count()) { | |
153 int old_index = ParameterIndexOf(node->op()); | |
titzer
2016/10/12 19:06:55
Probably want to skip all of this if old_index ==
aseemgarg
2016/10/17 19:01:44
Done.
| |
154 int new_index = GetParameterIndexAfterLowering(signature(), old_index); | |
155 NodeProperties::ChangeOp(node, common()->Parameter(new_index)); | |
156 | |
157 Node* new_node[kMaxLanes]; | |
158 for (int i = 0; i < kMaxLanes; i++) { | |
159 new_node[i] = nullptr; | |
160 } | |
161 new_node[0] = node; | |
162 if (signature()->GetParam(old_index) == | |
163 MachineRepresentation::kSimd128) { | |
164 for (int i = 1; i < kMaxLanes; i++) { | |
165 new_node[i] = graph()->NewNode(common()->Parameter(new_index + i), | |
166 graph()->start()); | |
167 } | |
168 } | |
169 ReplaceNode(node, new_node); | |
170 } | |
171 break; | |
172 } | |
173 case IrOpcode::kReturn: { | |
174 DefaultLowering(node); | |
175 int new_return_count = GetReturnCountAfterLowering(signature()); | |
176 if (signature()->return_count() != new_return_count) { | |
177 NodeProperties::ChangeOp(node, common()->Return(new_return_count)); | |
178 } | |
179 break; | |
180 } | |
181 case IrOpcode::kCall: { | |
182 // TODO(turbofan): Make WASM code const-correct wrt. CallDescriptor. | |
183 CallDescriptor* descriptor = | |
184 const_cast<CallDescriptor*>(CallDescriptorOf(node->op())); | |
185 if (DefaultLowering(node) || | |
186 (descriptor->ReturnCount() == 1 && | |
187 descriptor->GetReturnType(0) == MachineType::Simd128())) { | |
188 // We have to adjust the call descriptor. | |
189 const Operator* op = | |
190 common()->Call(wasm::ModuleEnv::GetI32WasmCallDescriptorForSimd( | |
191 zone(), descriptor)); | |
192 NodeProperties::ChangeOp(node, op); | |
193 } | |
194 if (descriptor->ReturnCount() == 1 && | |
195 descriptor->GetReturnType(0) == MachineType::Simd128()) { | |
196 // We access the additional return values through projections. | |
197 Node* rep_node[kMaxLanes]; | |
198 for (int i = 0; i < kMaxLanes; i++) { | |
199 rep_node[i] = | |
200 graph()->NewNode(common()->Projection(i), node, graph()->start()); | |
201 } | |
202 ReplaceNode(node, rep_node); | |
203 } | |
204 break; | |
205 } | |
206 case IrOpcode::kPhi: { | |
207 MachineRepresentation rep = PhiRepresentationOf(node->op()); | |
208 if (rep == MachineRepresentation::kSimd128) { | |
209 // The replacement nodes have already been created, we only have to | |
210 // replace placeholder nodes. | |
211 Node** rep_node = GetReplacements(node); | |
212 for (int i = 0; i < node->op()->ValueInputCount(); i++) { | |
213 Node** rep_input = | |
214 GetReplacementsWithType(node->InputAt(i), rep_type); | |
215 for (int j = 0; j < kMaxLanes; j++) { | |
216 rep_node[j]->ReplaceInput(i, rep_input[j]); | |
217 } | |
218 } | |
219 } else { | |
220 DefaultLowering(node); | |
221 } | |
222 break; | |
223 } | |
224 | |
225 case IrOpcode::kInt32x4Add: { | |
226 DCHECK(node->InputCount() == 2); | |
227 Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type); | |
228 Node** rep_right = GetReplacementsWithType(node->InputAt(1), rep_type); | |
229 Node* rep_node[kMaxLanes]; | |
230 for (int i = 0; i < kMaxLanes; i++) { | |
231 rep_node[i] = | |
232 graph()->NewNode(machine()->Int32Add(), rep_left[i], rep_right[i]); | |
233 } | |
234 ReplaceNode(node, rep_node); | |
235 break; | |
236 } | |
237 | |
238 case IrOpcode::kCreateInt32x4: { | |
239 Node* rep_node[kMaxLanes]; | |
240 for (int i = 0; i < kMaxLanes; i++) { | |
241 DCHECK(!HasReplacement(1, node->InputAt(i))); | |
242 rep_node[i] = node->InputAt(i); | |
243 } | |
244 ReplaceNode(node, rep_node); | |
245 break; | |
246 } | |
247 | |
248 case IrOpcode::kInt32x4ExtractLane: { | |
249 Node* laneNode = node->InputAt(1); | |
250 DCHECK_EQ(laneNode->opcode(), IrOpcode::kInt32Constant); | |
251 int32_t lane = OpParameter<int32_t>(laneNode); | |
252 Node* rep_node[kMaxLanes] = { | |
253 GetReplacementsWithType(node->InputAt(0), rep_type)[lane], nullptr, | |
254 nullptr, nullptr}; | |
255 ReplaceNode(node, rep_node); | |
256 break; | |
257 } | |
258 | |
259 case IrOpcode::kFloat32x4Add: { | |
260 DCHECK(node->InputCount() == 2); | |
261 Node** rep_left = GetReplacementsWithType(node->InputAt(0), rep_type); | |
262 Node** rep_right = GetReplacementsWithType(node->InputAt(1), rep_type); | |
263 Node* rep_node[kMaxLanes]; | |
264 for (int i = 0; i < kMaxLanes; i++) { | |
265 rep_node[i] = graph()->NewNode(machine()->Float32Add(), rep_left[i], | |
266 rep_right[i]); | |
267 } | |
268 ReplaceNode(node, rep_node); | |
269 break; | |
270 } | |
271 | |
272 case IrOpcode::kCreateFloat32x4: { | |
273 Node* rep_node[kMaxLanes]; | |
274 for (int i = 0; i < kMaxLanes; i++) { | |
275 DCHECK(!HasReplacement(1, node->InputAt(i))); | |
276 rep_node[i] = node->InputAt(i); | |
277 } | |
278 ReplaceNode(node, rep_node); | |
279 break; | |
280 } | |
281 | |
282 case IrOpcode::kFloat32x4ExtractLane: { | |
283 Node* laneNode = node->InputAt(1); | |
284 DCHECK_EQ(laneNode->opcode(), IrOpcode::kInt32Constant); | |
285 int32_t lane = OpParameter<int32_t>(laneNode); | |
286 Node* rep_node[kMaxLanes] = { | |
287 GetReplacementsWithType(node->InputAt(0), rep_type)[lane], nullptr, | |
288 nullptr, nullptr}; | |
289 ReplaceNode(node, rep_node); | |
290 break; | |
291 } | |
292 | |
293 default: { DefaultLowering(node); } | |
294 } | |
295 } | |
296 | |
297 bool SimdScalarLowering::DefaultLowering(Node* node) { | |
298 bool something_changed = false; | |
299 for (int i = NodeProperties::PastValueIndex(node) - 1; i >= 0; i--) { | |
300 Node* input = node->InputAt(i); | |
301 if (HasReplacement(0, input)) { | |
302 something_changed = true; | |
303 node->ReplaceInput(i, GetReplacements(input)[0]); | |
304 } | |
305 if (HasReplacement(1, input)) { | |
306 something_changed = true; | |
307 for (int j = 1; j < kMaxLanes; j++) { | |
308 node->InsertInput(zone(), i + j, GetReplacements(input)[j]); | |
309 } | |
310 } | |
311 } | |
312 return something_changed; | |
313 } | |
314 | |
315 void SimdScalarLowering::ReplaceNode(Node* old, Node** new_node) { | |
316 // if new_low == nullptr, then also new_high == nullptr. | |
317 DCHECK(new_node[0] != nullptr || | |
318 (new_node[1] == nullptr && new_node[2] == nullptr && | |
319 new_node[3] == nullptr)); | |
320 for (int i = 0; i < kMaxLanes; i++) { | |
321 replacements_[old->id()].node[i] = new_node[i]; | |
322 } | |
323 } | |
324 | |
325 bool SimdScalarLowering::HasReplacement(size_t index, Node* node) { | |
326 return replacements_[node->id()].node[index] != nullptr; | |
327 } | |
328 | |
329 SimdScalarLowering::SimdType SimdScalarLowering::ReplacementType(Node* node) { | |
330 return replacements_[node->id()].type; | |
331 } | |
332 | |
333 Node** SimdScalarLowering::GetReplacements(Node* node) { | |
334 Node** result = replacements_[node->id()].node; | |
335 DCHECK(result); | |
336 return result; | |
337 } | |
338 | |
339 Node** SimdScalarLowering::GetReplacementsWithType(Node* node, SimdType type) { | |
340 Node** replacements = GetReplacements(node); | |
341 if (ReplacementType(node) == type) { | |
342 return GetReplacements(node); | |
343 } | |
344 Node** result = zone()->NewArray<Node*>(kMaxLanes); | |
345 if (ReplacementType(node) == SimdType::kInt32 && type == SimdType::kFloat32) { | |
346 for (int i = 0; i < kMaxLanes; i++) { | |
347 if (replacements[i] != nullptr) { | |
348 result[i] = graph()->NewNode(machine()->BitcastInt32ToFloat32(), | |
349 replacements[i]); | |
350 } else { | |
351 result[i] = nullptr; | |
352 } | |
353 } | |
354 } else { | |
355 for (int i = 0; i < kMaxLanes; i++) { | |
356 if (replacements[i] != nullptr) { | |
357 result[i] = graph()->NewNode(machine()->BitcastFloat32ToInt32(), | |
358 replacements[i]); | |
359 } else { | |
360 result[i] = nullptr; | |
361 } | |
362 } | |
363 } | |
364 return result; | |
365 } | |
366 | |
367 void SimdScalarLowering::PreparePhiReplacement(Node* phi) { | |
368 MachineRepresentation rep = PhiRepresentationOf(phi->op()); | |
369 if (rep == MachineRepresentation::kSimd128) { | |
370 // We have to create the replacements for a phi node before we actually | |
371 // lower the phi to break potential cycles in the graph. The replacements of | |
372 // input nodes do not exist yet, so we use a placeholder node to pass the | |
373 // graph verifier. | |
374 int value_count = phi->op()->ValueInputCount(); | |
375 SimdType type = ReplacementType(phi); | |
376 Node** inputs_rep[kMaxLanes]; | |
377 for (int i = 0; i < kMaxLanes; i++) { | |
378 inputs_rep[i] = zone()->NewArray<Node*>(value_count + 1); | |
379 inputs_rep[i][value_count] = NodeProperties::GetControlInput(phi, 0); | |
380 } | |
381 for (int i = 0; i < value_count; i++) { | |
382 for (int j = 0; j < kMaxLanes; j++) { | |
383 inputs_rep[j][i] = placeholder_; | |
384 } | |
385 } | |
386 Node* rep_nodes[kMaxLanes]; | |
387 for (int i = 0; i < kMaxLanes; i++) { | |
388 if (type == SimdType::kInt32) { | |
389 rep_nodes[i] = graph()->NewNode( | |
390 common()->Phi(MachineRepresentation::kWord32, value_count), | |
391 value_count + 1, inputs_rep[i], false); | |
392 } else if (type == SimdType::kFloat32) { | |
393 rep_nodes[i] = graph()->NewNode( | |
394 common()->Phi(MachineRepresentation::kFloat32, value_count), | |
395 value_count + 1, inputs_rep[i], false); | |
396 } else { | |
397 UNREACHABLE(); | |
398 } | |
399 } | |
400 ReplaceNode(phi, rep_nodes); | |
401 } | |
402 } | |
403 } // namespace compiler | |
404 } // namespace internal | |
405 } // namespace v8 | |
OLD | NEW |