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/instruction-selector-impl.h" | 5 #include "src/compiler/instruction-selector-impl.h" |
6 #include "src/compiler/node-matchers.h" | 6 #include "src/compiler/node-matchers.h" |
7 #include "src/compiler/node-properties-inl.h" | 7 #include "src/compiler/node-properties-inl.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 InstructionOperand** first_output = | 641 InstructionOperand** first_output = |
642 buffer.outputs.size() > 0 ? &buffer.outputs.front() : NULL; | 642 buffer.outputs.size() > 0 ? &buffer.outputs.front() : NULL; |
643 Instruction* call_instr = | 643 Instruction* call_instr = |
644 Emit(opcode, buffer.outputs.size(), first_output, | 644 Emit(opcode, buffer.outputs.size(), first_output, |
645 buffer.instruction_args.size(), &buffer.instruction_args.front()); | 645 buffer.instruction_args.size(), &buffer.instruction_args.front()); |
646 call_instr->MarkAsCall(); | 646 call_instr->MarkAsCall(); |
647 } | 647 } |
648 | 648 |
649 | 649 |
650 // Shared routine for multiple compare operations. | 650 // Shared routine for multiple compare operations. |
651 static inline void VisitCompare(InstructionSelector* selector, | 651 static void VisitCompare(InstructionSelector* selector, InstructionCode opcode, |
652 InstructionCode opcode, | 652 InstructionOperand* left, InstructionOperand* right, |
653 InstructionOperand* left, | 653 FlagsContinuation* cont) { |
654 InstructionOperand* right, | |
655 FlagsContinuation* cont) { | |
656 IA32OperandGenerator g(selector); | 654 IA32OperandGenerator g(selector); |
657 if (cont->IsBranch()) { | 655 if (cont->IsBranch()) { |
658 selector->Emit(cont->Encode(opcode), NULL, left, right, | 656 selector->Emit(cont->Encode(opcode), NULL, left, right, |
659 g.Label(cont->true_block()), | 657 g.Label(cont->true_block()), |
660 g.Label(cont->false_block()))->MarkAsControl(); | 658 g.Label(cont->false_block()))->MarkAsControl(); |
661 } else { | 659 } else { |
662 DCHECK(cont->IsSet()); | 660 DCHECK(cont->IsSet()); |
663 // TODO(titzer): Needs byte register. | 661 // TODO(titzer): Needs byte register. |
664 selector->Emit(cont->Encode(opcode), g.DefineAsRegister(cont->result()), | 662 selector->Emit(cont->Encode(opcode), g.DefineAsRegister(cont->result()), |
665 left, right); | 663 left, right); |
666 } | 664 } |
667 } | 665 } |
668 | 666 |
669 | 667 |
| 668 // Shared routine for multiple compare operations. |
| 669 static void VisitCompare(InstructionSelector* selector, InstructionCode opcode, |
| 670 Node* left, Node* right, FlagsContinuation* cont, |
| 671 bool commutative) { |
| 672 IA32OperandGenerator g(selector); |
| 673 if (commutative && g.CanBeBetterLeftOperand(right)) { |
| 674 std::swap(left, right); |
| 675 } |
| 676 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); |
| 677 } |
| 678 |
| 679 |
670 // Shared routine for multiple word compare operations. | 680 // Shared routine for multiple word compare operations. |
671 static inline void VisitWordCompare(InstructionSelector* selector, Node* node, | 681 static void VisitWordCompare(InstructionSelector* selector, Node* node, |
672 InstructionCode opcode, | 682 InstructionCode opcode, FlagsContinuation* cont) { |
673 FlagsContinuation* cont, bool commutative) { | |
674 IA32OperandGenerator g(selector); | 683 IA32OperandGenerator g(selector); |
675 Node* left = node->InputAt(0); | 684 Node* const left = node->InputAt(0); |
676 Node* right = node->InputAt(1); | 685 Node* const right = node->InputAt(1); |
677 | 686 |
678 // Match immediates on left or right side of comparison. | 687 // Match immediates on left or right side of comparison. |
679 if (g.CanBeImmediate(right)) { | 688 if (g.CanBeImmediate(right)) { |
680 VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont); | 689 VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), cont); |
681 } else if (g.CanBeImmediate(left)) { | 690 } else if (g.CanBeImmediate(left)) { |
682 if (!commutative) cont->Commute(); | 691 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); |
683 VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont); | 692 VisitCompare(selector, opcode, g.Use(right), g.UseImmediate(left), cont); |
684 } else { | 693 } else { |
685 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); | 694 VisitCompare(selector, opcode, left, right, cont, |
| 695 node->op()->HasProperty(Operator::kCommutative)); |
686 } | 696 } |
687 } | 697 } |
688 | 698 |
689 | 699 |
690 // Shared routine for multiple float compare operations. | 700 // Shared routine for multiple float compare operations. |
691 static void VisitFloat64Compare(InstructionSelector* selector, Node* node, | 701 static void VisitFloat64Compare(InstructionSelector* selector, Node* node, |
692 FlagsContinuation* cont) { | 702 FlagsContinuation* cont) { |
693 IA32OperandGenerator g(selector); | 703 VisitCompare(selector, kSSEFloat64Cmp, node->InputAt(0), node->InputAt(1), |
694 Node* left = node->InputAt(0); | 704 cont, node->op()->HasProperty(Operator::kCommutative)); |
695 Node* right = node->InputAt(1); | |
696 VisitCompare(selector, kSSEFloat64Cmp, g.UseRegister(left), g.Use(right), | |
697 cont); | |
698 } | 705 } |
699 | 706 |
700 | 707 |
701 void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, | 708 void InstructionSelector::VisitBranch(Node* branch, BasicBlock* tbranch, |
702 BasicBlock* fbranch) { | 709 BasicBlock* fbranch) { |
703 IA32OperandGenerator g(this); | 710 IA32OperandGenerator g(this); |
704 Node* user = branch; | 711 Node* user = branch; |
705 Node* value = branch->InputAt(0); | 712 Node* value = branch->InputAt(0); |
706 | 713 |
707 FlagsContinuation cont(kNotEqual, tbranch, fbranch); | 714 FlagsContinuation cont(kNotEqual, tbranch, fbranch); |
(...skipping 14 matching lines...) Expand all Loading... |
722 } else { | 729 } else { |
723 break; | 730 break; |
724 } | 731 } |
725 } | 732 } |
726 | 733 |
727 // Try to combine the branch with a comparison. | 734 // Try to combine the branch with a comparison. |
728 if (CanCover(user, value)) { | 735 if (CanCover(user, value)) { |
729 switch (value->opcode()) { | 736 switch (value->opcode()) { |
730 case IrOpcode::kWord32Equal: | 737 case IrOpcode::kWord32Equal: |
731 cont.OverwriteAndNegateIfEqual(kEqual); | 738 cont.OverwriteAndNegateIfEqual(kEqual); |
732 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 739 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
733 case IrOpcode::kInt32LessThan: | 740 case IrOpcode::kInt32LessThan: |
734 cont.OverwriteAndNegateIfEqual(kSignedLessThan); | 741 cont.OverwriteAndNegateIfEqual(kSignedLessThan); |
735 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 742 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
736 case IrOpcode::kInt32LessThanOrEqual: | 743 case IrOpcode::kInt32LessThanOrEqual: |
737 cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); | 744 cont.OverwriteAndNegateIfEqual(kSignedLessThanOrEqual); |
738 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 745 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
739 case IrOpcode::kUint32LessThan: | 746 case IrOpcode::kUint32LessThan: |
740 cont.OverwriteAndNegateIfEqual(kUnsignedLessThan); | 747 cont.OverwriteAndNegateIfEqual(kUnsignedLessThan); |
741 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 748 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
742 case IrOpcode::kUint32LessThanOrEqual: | 749 case IrOpcode::kUint32LessThanOrEqual: |
743 cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); | 750 cont.OverwriteAndNegateIfEqual(kUnsignedLessThanOrEqual); |
744 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 751 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
745 case IrOpcode::kFloat64Equal: | 752 case IrOpcode::kFloat64Equal: |
746 cont.OverwriteAndNegateIfEqual(kUnorderedEqual); | 753 cont.OverwriteAndNegateIfEqual(kUnorderedEqual); |
747 return VisitFloat64Compare(this, value, &cont); | 754 return VisitFloat64Compare(this, value, &cont); |
748 case IrOpcode::kFloat64LessThan: | 755 case IrOpcode::kFloat64LessThan: |
749 cont.OverwriteAndNegateIfEqual(kUnorderedLessThan); | 756 cont.OverwriteAndNegateIfEqual(kUnorderedLessThan); |
750 return VisitFloat64Compare(this, value, &cont); | 757 return VisitFloat64Compare(this, value, &cont); |
751 case IrOpcode::kFloat64LessThanOrEqual: | 758 case IrOpcode::kFloat64LessThanOrEqual: |
752 cont.OverwriteAndNegateIfEqual(kUnorderedLessThanOrEqual); | 759 cont.OverwriteAndNegateIfEqual(kUnorderedLessThanOrEqual); |
753 return VisitFloat64Compare(this, value, &cont); | 760 return VisitFloat64Compare(this, value, &cont); |
754 case IrOpcode::kProjection: | 761 case IrOpcode::kProjection: |
(...skipping 15 matching lines...) Expand all Loading... |
770 case IrOpcode::kInt32SubWithOverflow: | 777 case IrOpcode::kInt32SubWithOverflow: |
771 cont.OverwriteAndNegateIfEqual(kOverflow); | 778 cont.OverwriteAndNegateIfEqual(kOverflow); |
772 return VisitBinop(this, node, kIA32Sub, &cont); | 779 return VisitBinop(this, node, kIA32Sub, &cont); |
773 default: | 780 default: |
774 break; | 781 break; |
775 } | 782 } |
776 } | 783 } |
777 } | 784 } |
778 break; | 785 break; |
779 case IrOpcode::kInt32Sub: | 786 case IrOpcode::kInt32Sub: |
780 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 787 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
781 case IrOpcode::kWord32And: | 788 case IrOpcode::kWord32And: |
782 return VisitWordCompare(this, value, kIA32Test, &cont, true); | 789 return VisitWordCompare(this, value, kIA32Test, &cont); |
783 default: | 790 default: |
784 break; | 791 break; |
785 } | 792 } |
786 } | 793 } |
787 | 794 |
788 // Branch could not be combined with a compare, emit compare against 0. | 795 // Branch could not be combined with a compare, emit compare against 0. |
789 VisitCompare(this, kIA32Cmp, g.Use(value), g.TempImmediate(0), &cont); | 796 VisitCompare(this, kIA32Cmp, g.Use(value), g.TempImmediate(0), &cont); |
790 } | 797 } |
791 | 798 |
792 | 799 |
793 void InstructionSelector::VisitWord32Equal(Node* const node) { | 800 void InstructionSelector::VisitWord32Equal(Node* const node) { |
794 Node* const user = node; | 801 Node* const user = node; |
795 FlagsContinuation cont(kEqual, node); | 802 FlagsContinuation cont(kEqual, node); |
796 Int32BinopMatcher m(user); | 803 Int32BinopMatcher m(user); |
797 if (m.right().Is(0)) { | 804 if (m.right().Is(0)) { |
798 Node* const value = m.left().node(); | 805 Node* const value = m.left().node(); |
799 if (CanCover(user, value)) { | 806 if (CanCover(user, value)) { |
800 switch (value->opcode()) { | 807 switch (value->opcode()) { |
801 case IrOpcode::kInt32Sub: | 808 case IrOpcode::kInt32Sub: |
802 return VisitWordCompare(this, value, kIA32Cmp, &cont, false); | 809 return VisitWordCompare(this, value, kIA32Cmp, &cont); |
803 case IrOpcode::kWord32And: | 810 case IrOpcode::kWord32And: |
804 return VisitWordCompare(this, value, kIA32Test, &cont, true); | 811 return VisitWordCompare(this, value, kIA32Test, &cont); |
805 default: | 812 default: |
806 break; | 813 break; |
807 } | 814 } |
808 } | 815 } |
809 } | 816 } |
810 return VisitWordCompare(this, node, kIA32Cmp, &cont, false); | 817 VisitWordCompare(this, node, kIA32Cmp, &cont); |
811 } | 818 } |
812 | 819 |
813 | 820 |
814 void InstructionSelector::VisitInt32LessThan(Node* node) { | 821 void InstructionSelector::VisitInt32LessThan(Node* node) { |
815 FlagsContinuation cont(kSignedLessThan, node); | 822 FlagsContinuation cont(kSignedLessThan, node); |
816 return VisitWordCompare(this, node, kIA32Cmp, &cont, false); | 823 VisitWordCompare(this, node, kIA32Cmp, &cont); |
817 } | 824 } |
818 | 825 |
819 | 826 |
820 void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) { | 827 void InstructionSelector::VisitInt32LessThanOrEqual(Node* node) { |
821 FlagsContinuation cont(kSignedLessThanOrEqual, node); | 828 FlagsContinuation cont(kSignedLessThanOrEqual, node); |
822 return VisitWordCompare(this, node, kIA32Cmp, &cont, false); | 829 VisitWordCompare(this, node, kIA32Cmp, &cont); |
823 } | 830 } |
824 | 831 |
825 | 832 |
826 void InstructionSelector::VisitUint32LessThan(Node* node) { | 833 void InstructionSelector::VisitUint32LessThan(Node* node) { |
827 FlagsContinuation cont(kUnsignedLessThan, node); | 834 FlagsContinuation cont(kUnsignedLessThan, node); |
828 return VisitWordCompare(this, node, kIA32Cmp, &cont, false); | 835 VisitWordCompare(this, node, kIA32Cmp, &cont); |
829 } | 836 } |
830 | 837 |
831 | 838 |
832 void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) { | 839 void InstructionSelector::VisitUint32LessThanOrEqual(Node* node) { |
833 FlagsContinuation cont(kUnsignedLessThanOrEqual, node); | 840 FlagsContinuation cont(kUnsignedLessThanOrEqual, node); |
834 return VisitWordCompare(this, node, kIA32Cmp, &cont, false); | 841 VisitWordCompare(this, node, kIA32Cmp, &cont); |
835 } | 842 } |
836 | 843 |
837 | 844 |
838 void InstructionSelector::VisitInt32AddWithOverflow(Node* node) { | 845 void InstructionSelector::VisitInt32AddWithOverflow(Node* node) { |
839 if (Node* ovf = node->FindProjection(1)) { | 846 if (Node* ovf = node->FindProjection(1)) { |
840 FlagsContinuation cont(kOverflow, ovf); | 847 FlagsContinuation cont(kOverflow, ovf); |
841 return VisitBinop(this, node, kIA32Add, &cont); | 848 return VisitBinop(this, node, kIA32Add, &cont); |
842 } | 849 } |
843 FlagsContinuation cont; | 850 FlagsContinuation cont; |
844 VisitBinop(this, node, kIA32Add, &cont); | 851 VisitBinop(this, node, kIA32Add, &cont); |
(...skipping 23 matching lines...) Expand all Loading... |
868 | 875 |
869 | 876 |
870 void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) { | 877 void InstructionSelector::VisitFloat64LessThanOrEqual(Node* node) { |
871 FlagsContinuation cont(kUnorderedLessThanOrEqual, node); | 878 FlagsContinuation cont(kUnorderedLessThanOrEqual, node); |
872 VisitFloat64Compare(this, node, &cont); | 879 VisitFloat64Compare(this, node, &cont); |
873 } | 880 } |
874 | 881 |
875 } // namespace compiler | 882 } // namespace compiler |
876 } // namespace internal | 883 } // namespace internal |
877 } // namespace v8 | 884 } // namespace v8 |
OLD | NEW |