blob: 2cf899b6cbf54cc9554d58f2dbb41c54f5c6ff73 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 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/common-operator.h"
6#include "src/compiler/graph.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +01007#include "src/compiler/js-operator.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/compiler/linkage.h"
9#include "src/compiler/node-properties.h"
10#include "src/compiler/operator-properties.h"
11#include "src/compiler/verifier.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010012#include "src/handles-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013
14namespace v8 {
15namespace internal {
16namespace compiler {
17
18// static
19int NodeProperties::PastValueIndex(Node* node) {
20 return FirstValueIndex(node) + node->op()->ValueInputCount();
21}
22
23
24// static
25int NodeProperties::PastContextIndex(Node* node) {
26 return FirstContextIndex(node) +
27 OperatorProperties::GetContextInputCount(node->op());
28}
29
30
31// static
32int NodeProperties::PastFrameStateIndex(Node* node) {
33 return FirstFrameStateIndex(node) +
34 OperatorProperties::GetFrameStateInputCount(node->op());
35}
36
37
38// static
39int NodeProperties::PastEffectIndex(Node* node) {
40 return FirstEffectIndex(node) + node->op()->EffectInputCount();
41}
42
43
44// static
45int NodeProperties::PastControlIndex(Node* node) {
46 return FirstControlIndex(node) + node->op()->ControlInputCount();
47}
48
49
50// static
51Node* NodeProperties::GetValueInput(Node* node, int index) {
52 DCHECK(0 <= index && index < node->op()->ValueInputCount());
53 return node->InputAt(FirstValueIndex(node) + index);
54}
55
56
57// static
58Node* NodeProperties::GetContextInput(Node* node) {
59 DCHECK(OperatorProperties::HasContextInput(node->op()));
60 return node->InputAt(FirstContextIndex(node));
61}
62
63
64// static
65Node* NodeProperties::GetFrameStateInput(Node* node, int index) {
66 DCHECK_LT(index, OperatorProperties::GetFrameStateInputCount(node->op()));
67 return node->InputAt(FirstFrameStateIndex(node) + index);
68}
69
70
71// static
72Node* NodeProperties::GetEffectInput(Node* node, int index) {
73 DCHECK(0 <= index && index < node->op()->EffectInputCount());
74 return node->InputAt(FirstEffectIndex(node) + index);
75}
76
77
78// static
79Node* NodeProperties::GetControlInput(Node* node, int index) {
80 DCHECK(0 <= index && index < node->op()->ControlInputCount());
81 return node->InputAt(FirstControlIndex(node) + index);
82}
83
84
85// static
86bool NodeProperties::IsValueEdge(Edge edge) {
87 Node* const node = edge.from();
88 return IsInputRange(edge, FirstValueIndex(node),
89 node->op()->ValueInputCount());
90}
91
92
93// static
94bool NodeProperties::IsContextEdge(Edge edge) {
95 Node* const node = edge.from();
96 return IsInputRange(edge, FirstContextIndex(node),
97 OperatorProperties::GetContextInputCount(node->op()));
98}
99
100
101// static
102bool NodeProperties::IsFrameStateEdge(Edge edge) {
103 Node* const node = edge.from();
104 return IsInputRange(edge, FirstFrameStateIndex(node),
105 OperatorProperties::GetFrameStateInputCount(node->op()));
106}
107
108
109// static
110bool NodeProperties::IsEffectEdge(Edge edge) {
111 Node* const node = edge.from();
112 return IsInputRange(edge, FirstEffectIndex(node),
113 node->op()->EffectInputCount());
114}
115
116
117// static
118bool NodeProperties::IsControlEdge(Edge edge) {
119 Node* const node = edge.from();
120 return IsInputRange(edge, FirstControlIndex(node),
121 node->op()->ControlInputCount());
122}
123
124
125// static
126bool NodeProperties::IsExceptionalCall(Node* node) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100127 if (node->op()->HasProperty(Operator::kNoThrow)) return false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 for (Edge const edge : node->use_edges()) {
129 if (!NodeProperties::IsControlEdge(edge)) continue;
130 if (edge.from()->opcode() == IrOpcode::kIfException) return true;
131 }
132 return false;
133}
134
135
136// static
137void NodeProperties::ReplaceValueInput(Node* node, Node* value, int index) {
138 DCHECK(index < node->op()->ValueInputCount());
139 node->ReplaceInput(FirstValueIndex(node) + index, value);
140}
141
142
143// static
144void NodeProperties::ReplaceValueInputs(Node* node, Node* value) {
145 int value_input_count = node->op()->ValueInputCount();
146 DCHECK_LE(1, value_input_count);
147 node->ReplaceInput(0, value);
148 while (--value_input_count > 0) {
149 node->RemoveInput(value_input_count);
150 }
151}
152
153
154// static
155void NodeProperties::ReplaceContextInput(Node* node, Node* context) {
156 node->ReplaceInput(FirstContextIndex(node), context);
157}
158
159
160// static
Ben Murdochc5610432016-08-08 18:44:38 +0100161void NodeProperties::ReplaceControlInput(Node* node, Node* control, int index) {
162 DCHECK(index < node->op()->ControlInputCount());
163 node->ReplaceInput(FirstControlIndex(node) + index, control);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164}
165
166
167// static
168void NodeProperties::ReplaceEffectInput(Node* node, Node* effect, int index) {
169 DCHECK(index < node->op()->EffectInputCount());
170 return node->ReplaceInput(FirstEffectIndex(node) + index, effect);
171}
172
173
174// static
175void NodeProperties::ReplaceFrameStateInput(Node* node, int index,
176 Node* frame_state) {
177 DCHECK_LT(index, OperatorProperties::GetFrameStateInputCount(node->op()));
178 node->ReplaceInput(FirstFrameStateIndex(node) + index, frame_state);
179}
180
181
182// static
183void NodeProperties::RemoveFrameStateInput(Node* node, int index) {
184 DCHECK_LT(index, OperatorProperties::GetFrameStateInputCount(node->op()));
185 node->RemoveInput(FirstFrameStateIndex(node) + index);
186}
187
188
189// static
190void NodeProperties::RemoveNonValueInputs(Node* node) {
191 node->TrimInputCount(node->op()->ValueInputCount());
192}
193
194
195// static
196void NodeProperties::RemoveValueInputs(Node* node) {
197 int value_input_count = node->op()->ValueInputCount();
198 while (--value_input_count >= 0) {
199 node->RemoveInput(value_input_count);
200 }
201}
202
203
204void NodeProperties::MergeControlToEnd(Graph* graph,
205 CommonOperatorBuilder* common,
206 Node* node) {
207 graph->end()->AppendInput(graph->zone(), node);
208 graph->end()->set_op(common->End(graph->end()->InputCount()));
209}
210
211
212// static
213void NodeProperties::ReplaceUses(Node* node, Node* value, Node* effect,
214 Node* success, Node* exception) {
215 // Requires distinguishing between value, effect and control edges.
216 for (Edge edge : node->use_edges()) {
217 if (IsControlEdge(edge)) {
218 if (edge.from()->opcode() == IrOpcode::kIfSuccess) {
219 DCHECK_NOT_NULL(success);
220 edge.UpdateTo(success);
221 } else if (edge.from()->opcode() == IrOpcode::kIfException) {
222 DCHECK_NOT_NULL(exception);
223 edge.UpdateTo(exception);
224 } else {
225 UNREACHABLE();
226 }
227 } else if (IsEffectEdge(edge)) {
228 DCHECK_NOT_NULL(effect);
229 edge.UpdateTo(effect);
230 } else {
231 DCHECK_NOT_NULL(value);
232 edge.UpdateTo(value);
233 }
234 }
235}
236
237
238// static
239void NodeProperties::ChangeOp(Node* node, const Operator* new_op) {
240 node->set_op(new_op);
241 Verifier::VerifyNode(node);
242}
243
244
245// static
246Node* NodeProperties::FindProjection(Node* node, size_t projection_index) {
247 for (auto use : node->uses()) {
248 if (use->opcode() == IrOpcode::kProjection &&
249 ProjectionIndexOf(use->op()) == projection_index) {
250 return use;
251 }
252 }
253 return nullptr;
254}
255
256
257// static
258void NodeProperties::CollectControlProjections(Node* node, Node** projections,
259 size_t projection_count) {
260#ifdef DEBUG
261 DCHECK_LE(static_cast<int>(projection_count), node->UseCount());
262 std::memset(projections, 0, sizeof(*projections) * projection_count);
263#endif
264 size_t if_value_index = 0;
265 for (Edge const edge : node->use_edges()) {
266 if (!IsControlEdge(edge)) continue;
267 Node* use = edge.from();
268 size_t index;
269 switch (use->opcode()) {
270 case IrOpcode::kIfTrue:
271 DCHECK_EQ(IrOpcode::kBranch, node->opcode());
272 index = 0;
273 break;
274 case IrOpcode::kIfFalse:
275 DCHECK_EQ(IrOpcode::kBranch, node->opcode());
276 index = 1;
277 break;
278 case IrOpcode::kIfSuccess:
279 DCHECK(!node->op()->HasProperty(Operator::kNoThrow));
280 index = 0;
281 break;
282 case IrOpcode::kIfException:
283 DCHECK(!node->op()->HasProperty(Operator::kNoThrow));
284 index = 1;
285 break;
286 case IrOpcode::kIfValue:
287 DCHECK_EQ(IrOpcode::kSwitch, node->opcode());
288 index = if_value_index++;
289 break;
290 case IrOpcode::kIfDefault:
291 DCHECK_EQ(IrOpcode::kSwitch, node->opcode());
292 index = projection_count - 1;
293 break;
294 default:
295 continue;
296 }
297 DCHECK_LT(if_value_index, projection_count);
298 DCHECK_LT(index, projection_count);
299 DCHECK_NULL(projections[index]);
300 projections[index] = use;
301 }
302#ifdef DEBUG
303 for (size_t index = 0; index < projection_count; ++index) {
304 DCHECK_NOT_NULL(projections[index]);
305 }
306#endif
307}
308
309
310// static
311MaybeHandle<Context> NodeProperties::GetSpecializationContext(
312 Node* node, MaybeHandle<Context> context) {
313 switch (node->opcode()) {
314 case IrOpcode::kHeapConstant:
315 return Handle<Context>::cast(OpParameter<Handle<HeapObject>>(node));
316 case IrOpcode::kParameter: {
317 Node* const start = NodeProperties::GetValueInput(node, 0);
318 DCHECK_EQ(IrOpcode::kStart, start->opcode());
319 int const index = ParameterIndexOf(node->op());
320 // The context is always the last parameter to a JavaScript function, and
321 // {Parameter} indices start at -1, so value outputs of {Start} look like
322 // this: closure, receiver, param0, ..., paramN, context.
323 if (index == start->op()->ValueOutputCount() - 2) {
324 return context;
325 }
326 break;
327 }
328 default:
329 break;
330 }
331 return MaybeHandle<Context>();
332}
333
334
335// static
336MaybeHandle<Context> NodeProperties::GetSpecializationNativeContext(
337 Node* node, MaybeHandle<Context> native_context) {
338 while (true) {
339 switch (node->opcode()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100340 case IrOpcode::kJSLoadContext: {
341 ContextAccess const& access = ContextAccessOf(node->op());
342 if (access.index() != Context::NATIVE_CONTEXT_INDEX) {
343 return MaybeHandle<Context>();
344 }
345 // Skip over the intermediate contexts, we're only interested in the
346 // very last context in the context chain anyway.
347 node = NodeProperties::GetContextInput(node);
348 break;
349 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000350 case IrOpcode::kJSCreateBlockContext:
351 case IrOpcode::kJSCreateCatchContext:
352 case IrOpcode::kJSCreateFunctionContext:
353 case IrOpcode::kJSCreateModuleContext:
354 case IrOpcode::kJSCreateScriptContext:
355 case IrOpcode::kJSCreateWithContext: {
356 // Skip over the intermediate contexts, we're only interested in the
357 // very last context in the context chain anyway.
358 node = NodeProperties::GetContextInput(node);
359 break;
360 }
361 case IrOpcode::kHeapConstant: {
362 // Extract the native context from the actual {context}.
363 Handle<Context> context =
364 Handle<Context>::cast(OpParameter<Handle<HeapObject>>(node));
365 return handle(context->native_context());
366 }
367 case IrOpcode::kOsrValue: {
368 int const index = OpParameter<int>(node);
369 if (index == Linkage::kOsrContextSpillSlotIndex) {
370 return native_context;
371 }
372 return MaybeHandle<Context>();
373 }
374 case IrOpcode::kParameter: {
375 Node* const start = NodeProperties::GetValueInput(node, 0);
376 DCHECK_EQ(IrOpcode::kStart, start->opcode());
377 int const index = ParameterIndexOf(node->op());
378 // The context is always the last parameter to a JavaScript function,
379 // and {Parameter} indices start at -1, so value outputs of {Start}
380 // look like this: closure, receiver, param0, ..., paramN, context.
381 if (index == start->op()->ValueOutputCount() - 2) {
382 return native_context;
383 }
384 return MaybeHandle<Context>();
385 }
386 default:
387 return MaybeHandle<Context>();
388 }
389 }
390}
391
392
393// static
394MaybeHandle<JSGlobalObject> NodeProperties::GetSpecializationGlobalObject(
395 Node* node, MaybeHandle<Context> native_context) {
396 Handle<Context> context;
397 if (GetSpecializationNativeContext(node, native_context).ToHandle(&context)) {
398 return handle(context->global_object());
399 }
400 return MaybeHandle<JSGlobalObject>();
401}
402
403
404// static
405Type* NodeProperties::GetTypeOrAny(Node* node) {
406 return IsTyped(node) ? node->type() : Type::Any();
407}
408
409
410// static
411bool NodeProperties::AllValueInputsAreTyped(Node* node) {
412 int input_count = node->op()->ValueInputCount();
413 for (int index = 0; index < input_count; ++index) {
414 if (!IsTyped(GetValueInput(node, index))) return false;
415 }
416 return true;
417}
418
419
420// static
421bool NodeProperties::IsInputRange(Edge edge, int first, int num) {
422 if (num == 0) return false;
423 int const index = edge.index();
424 return first <= index && index < first + num;
425}
426
427} // namespace compiler
428} // namespace internal
429} // namespace v8