Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/js-call-reducer.h" | 5 #include "src/compiler/js-call-reducer.h" |
| 6 | 6 |
| 7 #include "src/compiler/js-graph.h" | 7 #include "src/compiler/js-graph.h" |
| 8 #include "src/compiler/node-matchers.h" | 8 #include "src/compiler/node-matchers.h" |
| 9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
| 10 #include "src/type-feedback-vector-inl.h" | 10 #include "src/type-feedback-vector-inl.h" |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 } | 322 } |
| 323 | 323 |
| 324 | 324 |
| 325 Reduction JSCallReducer::ReduceJSCallConstruct(Node* node) { | 325 Reduction JSCallReducer::ReduceJSCallConstruct(Node* node) { |
| 326 DCHECK_EQ(IrOpcode::kJSCallConstruct, node->opcode()); | 326 DCHECK_EQ(IrOpcode::kJSCallConstruct, node->opcode()); |
| 327 CallConstructParameters const& p = CallConstructParametersOf(node->op()); | 327 CallConstructParameters const& p = CallConstructParametersOf(node->op()); |
| 328 DCHECK_LE(2u, p.arity()); | 328 DCHECK_LE(2u, p.arity()); |
| 329 int const arity = static_cast<int>(p.arity() - 2); | 329 int const arity = static_cast<int>(p.arity() - 2); |
| 330 Node* target = NodeProperties::GetValueInput(node, 0); | 330 Node* target = NodeProperties::GetValueInput(node, 0); |
| 331 Node* new_target = NodeProperties::GetValueInput(node, arity + 1); | 331 Node* new_target = NodeProperties::GetValueInput(node, arity + 1); |
| 332 Node* context = NodeProperties::GetContextInput(node); | |
| 333 Node* frame_state = NodeProperties::GetFrameStateInput(node, 1); | |
| 334 Node* effect = NodeProperties::GetEffectInput(node); | |
| 335 Node* control = NodeProperties::GetControlInput(node); | |
| 332 | 336 |
| 333 // Try to specialize JSCallConstruct {node}s with constant {target}s. | 337 // Try to specialize JSCallConstruct {node}s with constant {target}s. |
| 334 HeapObjectMatcher m(target); | 338 HeapObjectMatcher m(target); |
| 335 if (m.HasValue()) { | 339 if (m.HasValue()) { |
| 336 if (m.Value()->IsJSFunction()) { | 340 if (m.Value()->IsJSFunction()) { |
| 337 Handle<JSFunction> function = Handle<JSFunction>::cast(m.Value()); | 341 Handle<JSFunction> function = Handle<JSFunction>::cast(m.Value()); |
| 338 | 342 |
| 339 // Raise a TypeError if the {target} is not a constructor. | 343 // Raise a TypeError if the {target} is not a constructor. |
| 340 if (!function->IsConstructor()) { | 344 if (!function->IsConstructor()) { |
| 345 NodeProperties::RemoveFrameStateInput(node, 0); | |
|
Jarin
2015/11/23 12:52:07
Could you add a comment that explains that you rem
Benedikt Meurer
2015/11/23 12:59:03
Done. It's the eager one BTW.
Jarin
2015/11/23 13:06:28
Right, it looks like we both meant 'lazy' and some
| |
| 341 NodeProperties::ReplaceValueInputs(node, target); | 346 NodeProperties::ReplaceValueInputs(node, target); |
| 342 NodeProperties::ChangeOp( | 347 NodeProperties::ChangeOp( |
| 343 node, | 348 node, |
| 344 javascript()->CallRuntime(Runtime::kThrowCalledNonCallable, 1)); | 349 javascript()->CallRuntime(Runtime::kThrowCalledNonCallable, 1)); |
| 345 return Changed(node); | 350 return Changed(node); |
| 346 } | 351 } |
| 347 | 352 |
| 348 // Check for the ArrayConstructor. | 353 // Check for the ArrayConstructor. |
| 349 if (*function == function->native_context()->array_function()) { | 354 if (*function == function->native_context()->array_function()) { |
| 350 // Check if we have an allocation site. | 355 // Check if we have an allocation site. |
| 351 Handle<AllocationSite> site; | 356 Handle<AllocationSite> site; |
| 352 if (p.feedback().IsValid()) { | 357 if (p.feedback().IsValid()) { |
| 353 Handle<Object> feedback( | 358 Handle<Object> feedback( |
| 354 p.feedback().vector()->Get(p.feedback().slot()), isolate()); | 359 p.feedback().vector()->Get(p.feedback().slot()), isolate()); |
| 355 if (feedback->IsAllocationSite()) { | 360 if (feedback->IsAllocationSite()) { |
| 356 site = Handle<AllocationSite>::cast(feedback); | 361 site = Handle<AllocationSite>::cast(feedback); |
| 357 } | 362 } |
| 358 } | 363 } |
| 359 | 364 |
| 360 // Turn the {node} into a {JSCreateArray} call. | 365 // Turn the {node} into a {JSCreateArray} call. |
| 366 NodeProperties::RemoveFrameStateInput(node, 1); | |
| 361 for (int i = arity; i > 0; --i) { | 367 for (int i = arity; i > 0; --i) { |
| 362 NodeProperties::ReplaceValueInput( | 368 NodeProperties::ReplaceValueInput( |
| 363 node, NodeProperties::GetValueInput(node, i), i + 1); | 369 node, NodeProperties::GetValueInput(node, i), i + 1); |
| 364 } | 370 } |
| 365 NodeProperties::ReplaceValueInput(node, new_target, 1); | 371 NodeProperties::ReplaceValueInput(node, new_target, 1); |
| 366 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); | 372 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); |
| 367 return Changed(node); | 373 return Changed(node); |
| 368 } | 374 } |
| 369 } | 375 } |
| 370 | 376 |
| 371 // Don't mess with other {node}s that have a constant {target}. | 377 // Don't mess with other {node}s that have a constant {target}. |
| 372 // TODO(bmeurer): Also support optimizing bound functions and proxies here. | 378 // TODO(bmeurer): Also support optimizing bound functions and proxies here. |
| 373 return NoChange(); | 379 return NoChange(); |
| 374 } | 380 } |
| 375 | 381 |
| 382 // Not much we can do if deoptimization support is disabled. | |
| 383 if (!(flags() & kDeoptimizationEnabled)) return NoChange(); | |
| 384 | |
| 385 // TODO(mvstanton): Use ConstructICNexus here, once available. | |
| 386 Handle<Object> feedback; | |
| 387 if (!p.feedback().IsValid()) return NoChange(); | |
| 388 feedback = handle(p.feedback().vector()->Get(p.feedback().slot()), isolate()); | |
| 389 if (feedback->IsAllocationSite()) { | |
|
Jarin
2015/11/23 12:52:07
Perhaps explanation why allocation site implies ar
Benedikt Meurer
2015/11/23 12:59:03
Done.
| |
| 390 Handle<AllocationSite> site = Handle<AllocationSite>::cast(feedback); | |
| 391 | |
| 392 // Retrieve the Array function from the {node}. | |
| 393 Node* array_function; | |
| 394 Handle<Context> native_context; | |
| 395 if (GetNativeContext(node).ToHandle(&native_context)) { | |
| 396 array_function = jsgraph()->HeapConstant( | |
| 397 handle(native_context->array_function(), isolate())); | |
| 398 } else { | |
| 399 Node* global_object = effect = graph()->NewNode( | |
| 400 javascript()->LoadContext(0, Context::GLOBAL_OBJECT_INDEX, true), | |
| 401 context, context, effect); | |
| 402 Node* native_context = effect = graph()->NewNode( | |
| 403 javascript()->LoadNativeContext(), global_object, context, effect); | |
| 404 array_function = effect = graph()->NewNode( | |
| 405 javascript()->LoadContext(0, Context::ARRAY_FUNCTION_INDEX, true), | |
| 406 native_context, native_context, effect); | |
| 407 } | |
| 408 | |
| 409 // Check that the {target} is still the {array_function}. | |
| 410 Node* check = effect = | |
| 411 graph()->NewNode(javascript()->StrictEqual(), target, array_function, | |
| 412 context, effect, control); | |
| 413 Node* branch = | |
| 414 graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control); | |
| 415 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | |
| 416 Node* deoptimize = | |
| 417 graph()->NewNode(common()->Deoptimize(), frame_state, effect, if_false); | |
| 418 // TODO(bmeurer): This should be on the AdvancedReducer somehow. | |
| 419 NodeProperties::MergeControlToEnd(graph(), common(), deoptimize); | |
| 420 control = graph()->NewNode(common()->IfTrue(), branch); | |
| 421 | |
| 422 // Turn the {node} into a {JSCreateArray} call. | |
| 423 NodeProperties::ReplaceEffectInput(node, effect); | |
| 424 NodeProperties::ReplaceControlInput(node, control); | |
| 425 NodeProperties::RemoveFrameStateInput(node, 1); | |
| 426 for (int i = arity; i > 0; --i) { | |
| 427 NodeProperties::ReplaceValueInput( | |
| 428 node, NodeProperties::GetValueInput(node, i), i + 1); | |
| 429 } | |
| 430 NodeProperties::ReplaceValueInput(node, new_target, 1); | |
| 431 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); | |
| 432 return Changed(node); | |
| 433 } else if (feedback->IsWeakCell()) { | |
| 434 Handle<WeakCell> cell = Handle<WeakCell>::cast(feedback); | |
| 435 if (cell->value()->IsJSFunction()) { | |
| 436 Node* target_function = | |
| 437 jsgraph()->Constant(handle(cell->value(), isolate())); | |
| 438 | |
| 439 // Check that the {target} is still the {target_function}. | |
| 440 Node* check = effect = | |
| 441 graph()->NewNode(javascript()->StrictEqual(), target, target_function, | |
| 442 context, effect, control); | |
| 443 Node* branch = | |
| 444 graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control); | |
| 445 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | |
| 446 Node* deoptimize = graph()->NewNode(common()->Deoptimize(), frame_state, | |
| 447 effect, if_false); | |
| 448 // TODO(bmeurer): This should be on the AdvancedReducer somehow. | |
| 449 NodeProperties::MergeControlToEnd(graph(), common(), deoptimize); | |
| 450 control = graph()->NewNode(common()->IfTrue(), branch); | |
| 451 | |
| 452 // Specialize the JSCallConstruct node to the {target_function}. | |
| 453 NodeProperties::ReplaceValueInput(node, target_function, 0); | |
| 454 NodeProperties::ReplaceEffectInput(node, effect); | |
| 455 NodeProperties::ReplaceControlInput(node, control); | |
| 456 if (target == new_target) { | |
| 457 NodeProperties::ReplaceValueInput(node, target_function, arity + 1); | |
| 458 } | |
| 459 | |
| 460 // Try to further reduce the JSCallConstruct {node}. | |
| 461 Reduction const reduction = ReduceJSCallConstruct(node); | |
| 462 return reduction.Changed() ? reduction : Changed(node); | |
| 463 } | |
| 464 } | |
| 465 | |
| 376 return NoChange(); | 466 return NoChange(); |
| 377 } | 467 } |
| 378 | 468 |
| 379 | 469 |
| 380 MaybeHandle<Context> JSCallReducer::GetNativeContext(Node* node) { | 470 MaybeHandle<Context> JSCallReducer::GetNativeContext(Node* node) { |
| 381 Node* const context = NodeProperties::GetContextInput(node); | 471 Node* const context = NodeProperties::GetContextInput(node); |
| 382 return NodeProperties::GetSpecializationNativeContext(context, | 472 return NodeProperties::GetSpecializationNativeContext(context, |
| 383 native_context()); | 473 native_context()); |
| 384 } | 474 } |
| 385 | 475 |
| 386 | 476 |
| 387 Graph* JSCallReducer::graph() const { return jsgraph()->graph(); } | 477 Graph* JSCallReducer::graph() const { return jsgraph()->graph(); } |
| 388 | 478 |
| 389 | 479 |
| 390 Isolate* JSCallReducer::isolate() const { return jsgraph()->isolate(); } | 480 Isolate* JSCallReducer::isolate() const { return jsgraph()->isolate(); } |
| 391 | 481 |
| 392 | 482 |
| 393 CommonOperatorBuilder* JSCallReducer::common() const { | 483 CommonOperatorBuilder* JSCallReducer::common() const { |
| 394 return jsgraph()->common(); | 484 return jsgraph()->common(); |
| 395 } | 485 } |
| 396 | 486 |
| 397 | 487 |
| 398 JSOperatorBuilder* JSCallReducer::javascript() const { | 488 JSOperatorBuilder* JSCallReducer::javascript() const { |
| 399 return jsgraph()->javascript(); | 489 return jsgraph()->javascript(); |
| 400 } | 490 } |
| 401 | 491 |
| 402 } // namespace compiler | 492 } // namespace compiler |
| 403 } // namespace internal | 493 } // namespace internal |
| 404 } // namespace v8 | 494 } // namespace v8 |
| OLD | NEW |