OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/access-builder.h" | 5 #include "src/compiler/access-builder.h" |
6 #include "src/compiler/graph-inl.h" | 6 #include "src/compiler/graph-inl.h" |
7 #include "src/compiler/js-graph.h" | 7 #include "src/compiler/js-graph.h" |
8 #include "src/compiler/js-typed-lowering.h" | 8 #include "src/compiler/js-typed-lowering.h" |
9 #include "src/compiler/node-aux-data-inl.h" | 9 #include "src/compiler/node-aux-data-inl.h" |
10 #include "src/compiler/node-matchers.h" | 10 #include "src/compiler/node-matchers.h" |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 | 530 |
531 | 531 |
532 Reduction JSTypedLowering::ReduceJSToNumberInput(Node* input) { | 532 Reduction JSTypedLowering::ReduceJSToNumberInput(Node* input) { |
533 if (input->opcode() == IrOpcode::kJSToNumber) { | 533 if (input->opcode() == IrOpcode::kJSToNumber) { |
534 // Recursively try to reduce the input first. | 534 // Recursively try to reduce the input first. |
535 Reduction result = ReduceJSToNumber(input); | 535 Reduction result = ReduceJSToNumber(input); |
536 if (result.Changed()) return result; | 536 if (result.Changed()) return result; |
537 return Changed(input); // JSToNumber(JSToNumber(x)) => JSToNumber(x) | 537 return Changed(input); // JSToNumber(JSToNumber(x)) => JSToNumber(x) |
538 } | 538 } |
539 // Check if we have a cached conversion. | 539 // Check if we have a cached conversion. |
540 Node* conversion = FindConversion<IrOpcode::kJSToNumber>(input); | 540 Node* conversion = FindConversion<IrOpcode::kPlainPrimitiveToNumber>(input); |
541 if (conversion) return Replace(conversion); | 541 if (conversion) return Replace(conversion); |
542 Type* input_type = NodeProperties::GetBounds(input).upper; | 542 Type* input_type = NodeProperties::GetBounds(input).upper; |
543 if (input_type->Is(Type::Number())) { | 543 if (input_type->Is(Type::Number())) { |
544 // JSToNumber(x:number) => x | 544 // JSToNumber(x:number) => x |
545 return Changed(input); | 545 return Changed(input); |
546 } | 546 } |
547 if (input_type->Is(Type::Undefined())) { | 547 if (input_type->Is(Type::Undefined())) { |
548 // JSToNumber(undefined) => #NaN | 548 // JSToNumber(undefined) => #NaN |
549 return Replace(jsgraph()->NaNConstant()); | 549 return Replace(jsgraph()->NaNConstant()); |
550 } | 550 } |
(...skipping 13 matching lines...) Expand all Loading... |
564 Reduction JSTypedLowering::ReduceJSToNumber(Node* node) { | 564 Reduction JSTypedLowering::ReduceJSToNumber(Node* node) { |
565 // Try to reduce the input first. | 565 // Try to reduce the input first. |
566 Node* const input = node->InputAt(0); | 566 Node* const input = node->InputAt(0); |
567 Reduction reduction = ReduceJSToNumberInput(input); | 567 Reduction reduction = ReduceJSToNumberInput(input); |
568 if (reduction.Changed()) { | 568 if (reduction.Changed()) { |
569 NodeProperties::ReplaceWithValue(node, reduction.replacement()); | 569 NodeProperties::ReplaceWithValue(node, reduction.replacement()); |
570 return reduction; | 570 return reduction; |
571 } | 571 } |
572 Type* const input_type = NodeProperties::GetBounds(input).upper; | 572 Type* const input_type = NodeProperties::GetBounds(input).upper; |
573 if (input_type->Is(Type::PlainPrimitive())) { | 573 if (input_type->Is(Type::PlainPrimitive())) { |
| 574 RelaxEffects(node); |
574 if (input->opcode() == IrOpcode::kPhi) { | 575 if (input->opcode() == IrOpcode::kPhi) { |
575 // JSToNumber(phi(x1,...,xn,control):plain-primitive,context) | 576 // JSToNumber(phi(x1,...,xn,control):plain-primitive,context) |
576 // => phi(JSToNumber(x1,no-context), | 577 // => phi(PlainPrimitiveToNumber(x1), |
577 // ..., | 578 // ..., |
578 // JSToNumber(xn,no-context),control) | 579 // PlainPrimitiveToNumber(xn), |
| 580 // control) |
579 int const input_count = input->InputCount() - 1; | 581 int const input_count = input->InputCount() - 1; |
580 Node* const control = input->InputAt(input_count); | 582 Node* const control = input->InputAt(input_count); |
581 DCHECK_LE(0, input_count); | 583 DCHECK_LE(0, input_count); |
582 DCHECK(NodeProperties::IsControl(control)); | 584 DCHECK(NodeProperties::IsControl(control)); |
583 DCHECK(NodeProperties::GetBounds(node).upper->Is(Type::Number())); | 585 DCHECK(NodeProperties::GetBounds(node).upper->Is(Type::Number())); |
584 DCHECK(!NodeProperties::GetBounds(input).upper->Is(Type::Number())); | 586 DCHECK(!NodeProperties::GetBounds(input).upper->Is(Type::Number())); |
585 RelaxEffects(node); | |
586 node->set_op(common()->Phi(kMachAnyTagged, input_count)); | 587 node->set_op(common()->Phi(kMachAnyTagged, input_count)); |
587 for (int i = 0; i < input_count; ++i) { | 588 for (int i = 0; i < input_count; ++i) { |
588 // We must be very careful not to introduce cycles when pushing | 589 // We must be very careful not to introduce cycles when pushing |
589 // operations into phis. It is safe for {value}, since it appears | 590 // operations into phis. It is safe for {value}, since it appears |
590 // as input to the phi that we are replacing, but it's not safe | 591 // as input to the phi that we are replacing, but it's not safe |
591 // to simply reuse the context of the {node}. However, ToNumber() | 592 // to simply reuse the context of the {node}. However, ToNumber() |
592 // does not require a context anyways, so it's safe to discard it | 593 // does not require a context anyways, so it's safe to discard it |
593 // here and pass the dummy context. | 594 // here and pass the dummy context. |
594 Node* const value = ConvertToNumber(input->InputAt(i)); | 595 Node* const value = ConvertToNumber(input->InputAt(i)); |
595 if (i < node->InputCount()) { | 596 if (i < node->InputCount()) { |
596 node->ReplaceInput(i, value); | 597 node->ReplaceInput(i, value); |
597 } else { | 598 } else { |
598 node->AppendInput(graph()->zone(), value); | 599 node->AppendInput(graph()->zone(), value); |
599 } | 600 } |
600 } | 601 } |
601 if (input_count < node->InputCount()) { | 602 if (input_count < node->InputCount()) { |
602 node->ReplaceInput(input_count, control); | 603 node->ReplaceInput(input_count, control); |
603 } else { | 604 } else { |
604 node->AppendInput(graph()->zone(), control); | 605 node->AppendInput(graph()->zone(), control); |
605 } | 606 } |
606 node->TrimInputCount(input_count + 1); | 607 node->TrimInputCount(input_count + 1); |
607 return Changed(node); | 608 return Changed(node); |
608 } | 609 } |
609 if (input->opcode() == IrOpcode::kSelect) { | 610 if (input->opcode() == IrOpcode::kSelect) { |
610 // JSToNumber(select(c,x1,x2):plain-primitive,context) | 611 // JSToNumber(select(c,x1,x2):plain-primitive,context) |
611 // => select(c,JSToNumber(x1,no-context),JSToNumber(x2,no-context)) | 612 // => select(c,PlainPrimitiveToNumber(x1),PlainPrimitiveToNumber(x2)) |
612 int const input_count = input->InputCount(); | 613 int const input_count = input->InputCount(); |
613 BranchHint const input_hint = SelectParametersOf(input->op()).hint(); | 614 BranchHint const input_hint = SelectParametersOf(input->op()).hint(); |
614 DCHECK_EQ(3, input_count); | 615 DCHECK_EQ(3, input_count); |
615 DCHECK(NodeProperties::GetBounds(node).upper->Is(Type::Number())); | 616 DCHECK(NodeProperties::GetBounds(node).upper->Is(Type::Number())); |
616 DCHECK(!NodeProperties::GetBounds(input).upper->Is(Type::Number())); | 617 DCHECK(!NodeProperties::GetBounds(input).upper->Is(Type::Number())); |
617 RelaxEffects(node); | |
618 node->set_op(common()->Select(kMachAnyTagged, input_hint)); | 618 node->set_op(common()->Select(kMachAnyTagged, input_hint)); |
619 node->ReplaceInput(0, input->InputAt(0)); | 619 node->ReplaceInput(0, input->InputAt(0)); |
620 for (int i = 1; i < input_count; ++i) { | 620 for (int i = 1; i < input_count; ++i) { |
621 // We must be very careful not to introduce cycles when pushing | 621 // We must be very careful not to introduce cycles when pushing |
622 // operations into selects. It is safe for {value}, since it appears | 622 // operations into selects. It is safe for {value}, since it appears |
623 // as input to the select that we are replacing, but it's not safe | 623 // as input to the select that we are replacing, but it's not safe |
624 // to simply reuse the context of the {node}. However, ToNumber() | 624 // to simply reuse the context of the {node}. However, ToNumber() |
625 // does not require a context anyways, so it's safe to discard it | 625 // does not require a context anyways, so it's safe to discard it |
626 // here and pass the dummy context. | 626 // here and pass the dummy context. |
627 Node* const value = ConvertToNumber(input->InputAt(i)); | 627 Node* const value = ConvertToNumber(input->InputAt(i)); |
628 node->ReplaceInput(i, value); | 628 node->ReplaceInput(i, value); |
629 } | 629 } |
630 node->TrimInputCount(input_count); | 630 node->TrimInputCount(input_count); |
631 return Changed(node); | 631 return Changed(node); |
632 } | 632 } |
| 633 // JSToNumber(x:plain-primitive,context) => PlainPrimitiveToNumber(x) |
| 634 node->set_op(simplified()->PlainPrimitiveToNumber()); |
| 635 node->TrimInputCount(1); |
633 // Remember this conversion. | 636 // Remember this conversion. |
634 InsertConversion(node); | 637 InsertConversion(node); |
635 if (NodeProperties::GetContextInput(node) != | 638 return Changed(node); |
636 jsgraph()->NoContextConstant() || | |
637 NodeProperties::GetEffectInput(node) != graph()->start() || | |
638 NodeProperties::GetControlInput(node) != graph()->start()) { | |
639 // JSToNumber(x:plain-primitive,context,effect,control) | |
640 // => JSToNumber(x,no-context,start,start) | |
641 RelaxEffects(node); | |
642 NodeProperties::ReplaceContextInput(node, jsgraph()->NoContextConstant()); | |
643 NodeProperties::ReplaceControlInput(node, graph()->start()); | |
644 NodeProperties::ReplaceEffectInput(node, graph()->start()); | |
645 if (OperatorProperties::HasFrameStateInput(node->op())) { | |
646 NodeProperties::ReplaceFrameStateInput(node, | |
647 jsgraph()->EmptyFrameState()); | |
648 } | |
649 return Changed(node); | |
650 } | |
651 } | 639 } |
652 return NoChange(); | 640 return NoChange(); |
653 } | 641 } |
654 | 642 |
655 | 643 |
656 Reduction JSTypedLowering::ReduceJSToStringInput(Node* input) { | 644 Reduction JSTypedLowering::ReduceJSToStringInput(Node* input) { |
657 if (input->opcode() == IrOpcode::kJSToString) { | 645 if (input->opcode() == IrOpcode::kJSToString) { |
658 // Recursively try to reduce the input first. | 646 // Recursively try to reduce the input first. |
659 Reduction result = ReduceJSToString(input); | 647 Reduction result = ReduceJSToString(input); |
660 if (result.Changed()) return result; | 648 if (result.Changed()) return result; |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 } | 933 } |
946 return NoChange(); | 934 return NoChange(); |
947 } | 935 } |
948 | 936 |
949 | 937 |
950 Node* JSTypedLowering::ConvertToNumber(Node* input) { | 938 Node* JSTypedLowering::ConvertToNumber(Node* input) { |
951 DCHECK(NodeProperties::GetBounds(input).upper->Is(Type::PlainPrimitive())); | 939 DCHECK(NodeProperties::GetBounds(input).upper->Is(Type::PlainPrimitive())); |
952 // Avoid inserting too many eager ToNumber() operations. | 940 // Avoid inserting too many eager ToNumber() operations. |
953 Reduction const reduction = ReduceJSToNumberInput(input); | 941 Reduction const reduction = ReduceJSToNumberInput(input); |
954 if (reduction.Changed()) return reduction.replacement(); | 942 if (reduction.Changed()) return reduction.replacement(); |
955 // TODO(jarin) Use PlainPrimitiveToNumber once we have it. | |
956 Node* const conversion = | 943 Node* const conversion = |
957 FLAG_turbo_deoptimization | 944 graph()->NewNode(simplified()->PlainPrimitiveToNumber(), input); |
958 ? graph()->NewNode(javascript()->ToNumber(), input, | |
959 jsgraph()->NoContextConstant(), | |
960 jsgraph()->EmptyFrameState(), graph()->start(), | |
961 graph()->start()) | |
962 : graph()->NewNode(javascript()->ToNumber(), input, | |
963 jsgraph()->NoContextConstant(), graph()->start(), | |
964 graph()->start()); | |
965 InsertConversion(conversion); | 945 InsertConversion(conversion); |
966 return conversion; | 946 return conversion; |
967 } | 947 } |
968 | 948 |
969 | 949 |
970 template <IrOpcode::Value kOpcode> | 950 template <IrOpcode::Value kOpcode> |
971 Node* JSTypedLowering::FindConversion(Node* input) { | 951 Node* JSTypedLowering::FindConversion(Node* input) { |
972 size_t const input_id = input->id(); | 952 size_t const input_id = input->id(); |
973 if (input_id < conversions_.size()) { | 953 if (input_id < conversions_.size()) { |
974 Node* const conversion = conversions_[input_id]; | 954 Node* const conversion = conversions_[input_id]; |
975 if (conversion && conversion->opcode() == kOpcode) { | 955 if (conversion && conversion->opcode() == kOpcode) { |
976 return conversion; | 956 return conversion; |
977 } | 957 } |
978 } | 958 } |
979 return nullptr; | 959 return nullptr; |
980 } | 960 } |
981 | 961 |
982 | 962 |
983 void JSTypedLowering::InsertConversion(Node* conversion) { | 963 void JSTypedLowering::InsertConversion(Node* conversion) { |
984 DCHECK(conversion->opcode() == IrOpcode::kJSToNumber); | 964 DCHECK(conversion->opcode() == IrOpcode::kPlainPrimitiveToNumber); |
985 size_t const input_id = conversion->InputAt(0)->id(); | 965 size_t const input_id = conversion->InputAt(0)->id(); |
986 if (input_id >= conversions_.size()) { | 966 if (input_id >= conversions_.size()) { |
987 conversions_.resize(2 * input_id + 1); | 967 conversions_.resize(2 * input_id + 1); |
988 } | 968 } |
989 conversions_[input_id] = conversion; | 969 conversions_[input_id] = conversion; |
990 } | 970 } |
991 | 971 |
992 | 972 |
993 Node* JSTypedLowering::Word32Shl(Node* const lhs, int32_t const rhs) { | 973 Node* JSTypedLowering::Word32Shl(Node* const lhs, int32_t const rhs) { |
994 if (rhs == 0) return lhs; | 974 if (rhs == 0) return lhs; |
(...skipping 18 matching lines...) Expand all Loading... |
1013 } | 993 } |
1014 | 994 |
1015 | 995 |
1016 MachineOperatorBuilder* JSTypedLowering::machine() const { | 996 MachineOperatorBuilder* JSTypedLowering::machine() const { |
1017 return jsgraph()->machine(); | 997 return jsgraph()->machine(); |
1018 } | 998 } |
1019 | 999 |
1020 } // namespace compiler | 1000 } // namespace compiler |
1021 } // namespace internal | 1001 } // namespace internal |
1022 } // namespace v8 | 1002 } // namespace v8 |
OLD | NEW |