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 // Drop the lazy bailout location and use the eager bailout point for | |
| 346 // the runtime function (actually as lazy bailout point). It doesn't | |
| 347 // really matter which bailout location we use since we never really | |
| 348 // go back after throwing the exception. | |
|
Jarin
2015/11/23 13:06:28
FYI, the frame state will matter if we implement l
| |
| 349 NodeProperties::RemoveFrameStateInput(node, 0); | |
| 341 NodeProperties::ReplaceValueInputs(node, target); | 350 NodeProperties::ReplaceValueInputs(node, target); |
| 342 NodeProperties::ChangeOp( | 351 NodeProperties::ChangeOp( |
| 343 node, | 352 node, |
| 344 javascript()->CallRuntime(Runtime::kThrowCalledNonCallable, 1)); | 353 javascript()->CallRuntime(Runtime::kThrowCalledNonCallable, 1)); |
| 345 return Changed(node); | 354 return Changed(node); |
| 346 } | 355 } |
| 347 | 356 |
| 348 // Check for the ArrayConstructor. | 357 // Check for the ArrayConstructor. |
| 349 if (*function == function->native_context()->array_function()) { | 358 if (*function == function->native_context()->array_function()) { |
| 350 // Check if we have an allocation site. | 359 // Check if we have an allocation site. |
| 351 Handle<AllocationSite> site; | 360 Handle<AllocationSite> site; |
| 352 if (p.feedback().IsValid()) { | 361 if (p.feedback().IsValid()) { |
| 353 Handle<Object> feedback( | 362 Handle<Object> feedback( |
| 354 p.feedback().vector()->Get(p.feedback().slot()), isolate()); | 363 p.feedback().vector()->Get(p.feedback().slot()), isolate()); |
| 355 if (feedback->IsAllocationSite()) { | 364 if (feedback->IsAllocationSite()) { |
| 356 site = Handle<AllocationSite>::cast(feedback); | 365 site = Handle<AllocationSite>::cast(feedback); |
| 357 } | 366 } |
| 358 } | 367 } |
| 359 | 368 |
| 360 // Turn the {node} into a {JSCreateArray} call. | 369 // Turn the {node} into a {JSCreateArray} call. |
| 370 NodeProperties::RemoveFrameStateInput(node, 1); | |
| 361 for (int i = arity; i > 0; --i) { | 371 for (int i = arity; i > 0; --i) { |
| 362 NodeProperties::ReplaceValueInput( | 372 NodeProperties::ReplaceValueInput( |
| 363 node, NodeProperties::GetValueInput(node, i), i + 1); | 373 node, NodeProperties::GetValueInput(node, i), i + 1); |
| 364 } | 374 } |
| 365 NodeProperties::ReplaceValueInput(node, new_target, 1); | 375 NodeProperties::ReplaceValueInput(node, new_target, 1); |
| 366 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); | 376 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); |
| 367 return Changed(node); | 377 return Changed(node); |
| 368 } | 378 } |
| 369 } | 379 } |
| 370 | 380 |
| 371 // Don't mess with other {node}s that have a constant {target}. | 381 // Don't mess with other {node}s that have a constant {target}. |
| 372 // TODO(bmeurer): Also support optimizing bound functions and proxies here. | 382 // TODO(bmeurer): Also support optimizing bound functions and proxies here. |
| 373 return NoChange(); | 383 return NoChange(); |
| 374 } | 384 } |
| 375 | 385 |
| 386 // Not much we can do if deoptimization support is disabled. | |
| 387 if (!(flags() & kDeoptimizationEnabled)) return NoChange(); | |
| 388 | |
| 389 // TODO(mvstanton): Use ConstructICNexus here, once available. | |
| 390 Handle<Object> feedback; | |
| 391 if (!p.feedback().IsValid()) return NoChange(); | |
| 392 feedback = handle(p.feedback().vector()->Get(p.feedback().slot()), isolate()); | |
| 393 if (feedback->IsAllocationSite()) { | |
| 394 // The feedback is an AllocationSite, which means we have called the | |
| 395 // Array function and collected transition (and pretenuring) feedback | |
| 396 // for the resulting arrays. This has to be kept in sync with the | |
| 397 // implementation of the CallConstructStub. | |
| 398 Handle<AllocationSite> site = Handle<AllocationSite>::cast(feedback); | |
| 399 | |
| 400 // Retrieve the Array function from the {node}. | |
| 401 Node* array_function; | |
| 402 Handle<Context> native_context; | |
| 403 if (GetNativeContext(node).ToHandle(&native_context)) { | |
| 404 array_function = jsgraph()->HeapConstant( | |
| 405 handle(native_context->array_function(), isolate())); | |
| 406 } else { | |
| 407 Node* global_object = effect = graph()->NewNode( | |
| 408 javascript()->LoadContext(0, Context::GLOBAL_OBJECT_INDEX, true), | |
| 409 context, context, effect); | |
| 410 Node* native_context = effect = graph()->NewNode( | |
| 411 javascript()->LoadNativeContext(), global_object, context, effect); | |
| 412 array_function = effect = graph()->NewNode( | |
| 413 javascript()->LoadContext(0, Context::ARRAY_FUNCTION_INDEX, true), | |
| 414 native_context, native_context, effect); | |
| 415 } | |
| 416 | |
| 417 // Check that the {target} is still the {array_function}. | |
| 418 Node* check = effect = | |
| 419 graph()->NewNode(javascript()->StrictEqual(), target, array_function, | |
| 420 context, effect, control); | |
| 421 Node* branch = | |
| 422 graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control); | |
| 423 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | |
| 424 Node* deoptimize = | |
| 425 graph()->NewNode(common()->Deoptimize(), frame_state, effect, if_false); | |
| 426 // TODO(bmeurer): This should be on the AdvancedReducer somehow. | |
| 427 NodeProperties::MergeControlToEnd(graph(), common(), deoptimize); | |
| 428 control = graph()->NewNode(common()->IfTrue(), branch); | |
| 429 | |
| 430 // Turn the {node} into a {JSCreateArray} call. | |
| 431 NodeProperties::ReplaceEffectInput(node, effect); | |
| 432 NodeProperties::ReplaceControlInput(node, control); | |
| 433 NodeProperties::RemoveFrameStateInput(node, 1); | |
| 434 for (int i = arity; i > 0; --i) { | |
| 435 NodeProperties::ReplaceValueInput( | |
| 436 node, NodeProperties::GetValueInput(node, i), i + 1); | |
| 437 } | |
| 438 NodeProperties::ReplaceValueInput(node, new_target, 1); | |
| 439 NodeProperties::ChangeOp(node, javascript()->CreateArray(arity, site)); | |
| 440 return Changed(node); | |
| 441 } else if (feedback->IsWeakCell()) { | |
| 442 Handle<WeakCell> cell = Handle<WeakCell>::cast(feedback); | |
| 443 if (cell->value()->IsJSFunction()) { | |
| 444 Node* target_function = | |
| 445 jsgraph()->Constant(handle(cell->value(), isolate())); | |
| 446 | |
| 447 // Check that the {target} is still the {target_function}. | |
| 448 Node* check = effect = | |
| 449 graph()->NewNode(javascript()->StrictEqual(), target, target_function, | |
| 450 context, effect, control); | |
| 451 Node* branch = | |
| 452 graph()->NewNode(common()->Branch(BranchHint::kTrue), check, control); | |
| 453 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); | |
| 454 Node* deoptimize = graph()->NewNode(common()->Deoptimize(), frame_state, | |
| 455 effect, if_false); | |
| 456 // TODO(bmeurer): This should be on the AdvancedReducer somehow. | |
| 457 NodeProperties::MergeControlToEnd(graph(), common(), deoptimize); | |
| 458 control = graph()->NewNode(common()->IfTrue(), branch); | |
| 459 | |
| 460 // Specialize the JSCallConstruct node to the {target_function}. | |
| 461 NodeProperties::ReplaceValueInput(node, target_function, 0); | |
| 462 NodeProperties::ReplaceEffectInput(node, effect); | |
| 463 NodeProperties::ReplaceControlInput(node, control); | |
| 464 if (target == new_target) { | |
| 465 NodeProperties::ReplaceValueInput(node, target_function, arity + 1); | |
| 466 } | |
| 467 | |
| 468 // Try to further reduce the JSCallConstruct {node}. | |
| 469 Reduction const reduction = ReduceJSCallConstruct(node); | |
| 470 return reduction.Changed() ? reduction : Changed(node); | |
| 471 } | |
| 472 } | |
| 473 | |
| 376 return NoChange(); | 474 return NoChange(); |
| 377 } | 475 } |
| 378 | 476 |
| 379 | 477 |
| 380 MaybeHandle<Context> JSCallReducer::GetNativeContext(Node* node) { | 478 MaybeHandle<Context> JSCallReducer::GetNativeContext(Node* node) { |
| 381 Node* const context = NodeProperties::GetContextInput(node); | 479 Node* const context = NodeProperties::GetContextInput(node); |
| 382 return NodeProperties::GetSpecializationNativeContext(context, | 480 return NodeProperties::GetSpecializationNativeContext(context, |
| 383 native_context()); | 481 native_context()); |
| 384 } | 482 } |
| 385 | 483 |
| 386 | 484 |
| 387 Graph* JSCallReducer::graph() const { return jsgraph()->graph(); } | 485 Graph* JSCallReducer::graph() const { return jsgraph()->graph(); } |
| 388 | 486 |
| 389 | 487 |
| 390 Isolate* JSCallReducer::isolate() const { return jsgraph()->isolate(); } | 488 Isolate* JSCallReducer::isolate() const { return jsgraph()->isolate(); } |
| 391 | 489 |
| 392 | 490 |
| 393 CommonOperatorBuilder* JSCallReducer::common() const { | 491 CommonOperatorBuilder* JSCallReducer::common() const { |
| 394 return jsgraph()->common(); | 492 return jsgraph()->common(); |
| 395 } | 493 } |
| 396 | 494 |
| 397 | 495 |
| 398 JSOperatorBuilder* JSCallReducer::javascript() const { | 496 JSOperatorBuilder* JSCallReducer::javascript() const { |
| 399 return jsgraph()->javascript(); | 497 return jsgraph()->javascript(); |
| 400 } | 498 } |
| 401 | 499 |
| 402 } // namespace compiler | 500 } // namespace compiler |
| 403 } // namespace internal | 501 } // namespace internal |
| 404 } // namespace v8 | 502 } // namespace v8 |
| OLD | NEW |