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/base/bits.h" | 5 #include "src/base/bits.h" |
6 #include "src/base/division-by-constant.h" | 6 #include "src/base/division-by-constant.h" |
7 #include "src/compiler/js-graph.h" | 7 #include "src/compiler/js-graph.h" |
8 #include "src/compiler/machine-operator-reducer.h" | 8 #include "src/compiler/machine-operator-reducer.h" |
9 #include "src/compiler/typer.h" | 9 #include "src/compiler/typer.h" |
10 #include "test/unittests/compiler/graph-unittest.h" | 10 #include "test/unittests/compiler/graph-unittest.h" |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 if (base::bits::IsPowerOfTwo32(divisor)) continue; | 724 if (base::bits::IsPowerOfTwo32(divisor)) continue; |
725 Reduction const r = Reduce( | 725 Reduction const r = Reduce( |
726 graph()->NewNode(machine()->Int32Div(), p0, Int32Constant(divisor))); | 726 graph()->NewNode(machine()->Int32Div(), p0, Int32Constant(divisor))); |
727 ASSERT_TRUE(r.Changed()); | 727 ASSERT_TRUE(r.Changed()); |
728 EXPECT_THAT(r.replacement(), IsTruncatingDiv(p0, divisor)); | 728 EXPECT_THAT(r.replacement(), IsTruncatingDiv(p0, divisor)); |
729 } | 729 } |
730 } | 730 } |
731 } | 731 } |
732 | 732 |
733 | 733 |
| 734 TEST_F(MachineOperatorReducerTest, Int32DivWithParameters) { |
| 735 Node* const p0 = Parameter(0); |
| 736 Reduction const r = Reduce(graph()->NewNode(machine()->Int32Div(), p0, p0)); |
| 737 ASSERT_TRUE(r.Changed()); |
| 738 EXPECT_THAT( |
| 739 r.replacement(), |
| 740 IsWord32Equal(IsWord32Equal(p0, IsInt32Constant(0)), IsInt32Constant(0))); |
| 741 } |
| 742 |
| 743 |
| 744 // ----------------------------------------------------------------------------- |
| 745 // Uint32Div |
| 746 |
| 747 |
| 748 TEST_F(MachineOperatorReducerTest, Uint32DivWithConstant) { |
| 749 Node* const p0 = Parameter(0); |
| 750 { |
| 751 Reduction const r = |
| 752 Reduce(graph()->NewNode(machine()->Uint32Div(), Int32Constant(0), p0)); |
| 753 ASSERT_TRUE(r.Changed()); |
| 754 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 755 } |
| 756 { |
| 757 Reduction const r = |
| 758 Reduce(graph()->NewNode(machine()->Uint32Div(), p0, Int32Constant(0))); |
| 759 ASSERT_TRUE(r.Changed()); |
| 760 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 761 } |
| 762 { |
| 763 Reduction const r = |
| 764 Reduce(graph()->NewNode(machine()->Uint32Div(), p0, Int32Constant(1))); |
| 765 ASSERT_TRUE(r.Changed()); |
| 766 EXPECT_EQ(r.replacement(), p0); |
| 767 } |
| 768 TRACED_FOREACH(uint32_t, dividend, kUint32Values) { |
| 769 TRACED_FOREACH(uint32_t, divisor, kUint32Values) { |
| 770 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Div(), |
| 771 Uint32Constant(dividend), |
| 772 Uint32Constant(divisor))); |
| 773 ASSERT_TRUE(r.Changed()); |
| 774 EXPECT_THAT(r.replacement(), |
| 775 IsInt32Constant(bit_cast<int32_t>( |
| 776 base::bits::UnsignedDiv32(dividend, divisor)))); |
| 777 } |
| 778 } |
| 779 TRACED_FORRANGE(uint32_t, shift, 1, 31) { |
| 780 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Div(), p0, |
| 781 Uint32Constant(1u << shift))); |
| 782 ASSERT_TRUE(r.Changed()); |
| 783 EXPECT_THAT(r.replacement(), |
| 784 IsWord32Shr(p0, IsInt32Constant(bit_cast<int32_t>(shift)))); |
| 785 } |
| 786 } |
| 787 |
| 788 |
| 789 TEST_F(MachineOperatorReducerTest, Uint32DivWithParameters) { |
| 790 Node* const p0 = Parameter(0); |
| 791 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Div(), p0, p0)); |
| 792 ASSERT_TRUE(r.Changed()); |
| 793 EXPECT_THAT( |
| 794 r.replacement(), |
| 795 IsWord32Equal(IsWord32Equal(p0, IsInt32Constant(0)), IsInt32Constant(0))); |
| 796 } |
| 797 |
| 798 |
734 // ----------------------------------------------------------------------------- | 799 // ----------------------------------------------------------------------------- |
735 // Int32Mod | 800 // Int32Mod |
736 | 801 |
737 | 802 |
738 TEST_F(MachineOperatorReducerTest, Int32ModWithConstant) { | 803 TEST_F(MachineOperatorReducerTest, Int32ModWithConstant) { |
739 Node* const p0 = Parameter(0); | 804 Node* const p0 = Parameter(0); |
740 static const int32_t kOnes[] = {-1, 1}; | 805 { |
741 TRACED_FOREACH(int32_t, one, kOnes) { | |
742 Reduction const r = | 806 Reduction const r = |
743 Reduce(graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(one))); | 807 Reduce(graph()->NewNode(machine()->Int32Mod(), Int32Constant(0), p0)); |
744 ASSERT_TRUE(r.Changed()); | 808 ASSERT_TRUE(r.Changed()); |
745 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); | 809 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
746 } | 810 } |
| 811 { |
| 812 Reduction const r = |
| 813 Reduce(graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(0))); |
| 814 ASSERT_TRUE(r.Changed()); |
| 815 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 816 } |
| 817 { |
| 818 Reduction const r = |
| 819 Reduce(graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(1))); |
| 820 ASSERT_TRUE(r.Changed()); |
| 821 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 822 } |
| 823 { |
| 824 Reduction const r = |
| 825 Reduce(graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(-1))); |
| 826 ASSERT_TRUE(r.Changed()); |
| 827 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 828 } |
| 829 TRACED_FOREACH(int32_t, dividend, kInt32Values) { |
| 830 TRACED_FOREACH(int32_t, divisor, kInt32Values) { |
| 831 Reduction const r = Reduce(graph()->NewNode(machine()->Int32Mod(), |
| 832 Int32Constant(dividend), |
| 833 Int32Constant(divisor))); |
| 834 ASSERT_TRUE(r.Changed()); |
| 835 EXPECT_THAT(r.replacement(), |
| 836 IsInt32Constant(base::bits::SignedMod32(dividend, divisor))); |
| 837 } |
| 838 } |
747 TRACED_FORRANGE(int32_t, shift, 1, 30) { | 839 TRACED_FORRANGE(int32_t, shift, 1, 30) { |
748 Reduction const r = Reduce( | 840 Reduction const r = Reduce( |
749 graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(1 << shift))); | 841 graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(1 << shift))); |
750 ASSERT_TRUE(r.Changed()); | 842 ASSERT_TRUE(r.Changed()); |
751 | 843 |
752 Capture<Node*> branch; | 844 Capture<Node*> branch; |
753 Node* const phi = r.replacement(); | 845 Node* const phi = r.replacement(); |
754 int32_t const mask = (1 << shift) - 1; | 846 int32_t const mask = (1 << shift) - 1; |
755 EXPECT_THAT( | 847 EXPECT_THAT( |
756 phi, IsPhi(kMachInt32, | 848 phi, IsPhi(kMachInt32, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 Reduction const r = Reduce( | 882 Reduction const r = Reduce( |
791 graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(divisor))); | 883 graph()->NewNode(machine()->Int32Mod(), p0, Int32Constant(divisor))); |
792 ASSERT_TRUE(r.Changed()); | 884 ASSERT_TRUE(r.Changed()); |
793 EXPECT_THAT(r.replacement(), | 885 EXPECT_THAT(r.replacement(), |
794 IsInt32Sub(p0, IsInt32Mul(IsTruncatingDiv(p0, Abs(divisor)), | 886 IsInt32Sub(p0, IsInt32Mul(IsTruncatingDiv(p0, Abs(divisor)), |
795 IsInt32Constant(Abs(divisor))))); | 887 IsInt32Constant(Abs(divisor))))); |
796 } | 888 } |
797 } | 889 } |
798 | 890 |
799 | 891 |
| 892 TEST_F(MachineOperatorReducerTest, Int32ModWithParameters) { |
| 893 Node* const p0 = Parameter(0); |
| 894 Reduction const r = Reduce(graph()->NewNode(machine()->Int32Mod(), p0, p0)); |
| 895 ASSERT_TRUE(r.Changed()); |
| 896 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 897 } |
| 898 |
| 899 |
| 900 // ----------------------------------------------------------------------------- |
| 901 // Uint32Mod |
| 902 |
| 903 |
| 904 TEST_F(MachineOperatorReducerTest, Uint32ModWithConstant) { |
| 905 Node* const p0 = Parameter(0); |
| 906 { |
| 907 Reduction const r = |
| 908 Reduce(graph()->NewNode(machine()->Uint32Mod(), p0, Int32Constant(0))); |
| 909 ASSERT_TRUE(r.Changed()); |
| 910 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 911 } |
| 912 { |
| 913 Reduction const r = |
| 914 Reduce(graph()->NewNode(machine()->Uint32Mod(), Int32Constant(0), p0)); |
| 915 ASSERT_TRUE(r.Changed()); |
| 916 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 917 } |
| 918 { |
| 919 Reduction const r = |
| 920 Reduce(graph()->NewNode(machine()->Uint32Mod(), p0, Int32Constant(1))); |
| 921 ASSERT_TRUE(r.Changed()); |
| 922 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 923 } |
| 924 TRACED_FOREACH(uint32_t, dividend, kUint32Values) { |
| 925 TRACED_FOREACH(uint32_t, divisor, kUint32Values) { |
| 926 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Mod(), |
| 927 Uint32Constant(dividend), |
| 928 Uint32Constant(divisor))); |
| 929 ASSERT_TRUE(r.Changed()); |
| 930 EXPECT_THAT(r.replacement(), |
| 931 IsInt32Constant(bit_cast<int32_t>( |
| 932 base::bits::UnsignedMod32(dividend, divisor)))); |
| 933 } |
| 934 } |
| 935 TRACED_FORRANGE(uint32_t, shift, 1, 31) { |
| 936 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Mod(), p0, |
| 937 Uint32Constant(1u << shift))); |
| 938 ASSERT_TRUE(r.Changed()); |
| 939 EXPECT_THAT(r.replacement(), |
| 940 IsWord32And(p0, IsInt32Constant( |
| 941 bit_cast<int32_t>((1u << shift) - 1u)))); |
| 942 } |
| 943 } |
| 944 |
| 945 |
| 946 TEST_F(MachineOperatorReducerTest, Uint32ModWithParameters) { |
| 947 Node* const p0 = Parameter(0); |
| 948 Reduction const r = Reduce(graph()->NewNode(machine()->Uint32Mod(), p0, p0)); |
| 949 ASSERT_TRUE(r.Changed()); |
| 950 EXPECT_THAT(r.replacement(), IsInt32Constant(0)); |
| 951 } |
| 952 |
| 953 |
800 // ----------------------------------------------------------------------------- | 954 // ----------------------------------------------------------------------------- |
801 // Int32AddWithOverflow | 955 // Int32AddWithOverflow |
802 | 956 |
803 | 957 |
804 TEST_F(MachineOperatorReducerTest, Int32AddWithOverflowWithZero) { | 958 TEST_F(MachineOperatorReducerTest, Int32AddWithOverflowWithZero) { |
805 Node* p0 = Parameter(0); | 959 Node* p0 = Parameter(0); |
806 { | 960 { |
807 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), | 961 Node* add = graph()->NewNode(machine()->Int32AddWithOverflow(), |
808 Int32Constant(0), p0); | 962 Int32Constant(0), p0); |
809 | 963 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 Reduction r = Reduce(node); | 1109 Reduction r = Reduce(node); |
956 ASSERT_TRUE(r.Changed()); | 1110 ASSERT_TRUE(r.Changed()); |
957 EXPECT_THAT(r.replacement(), | 1111 EXPECT_THAT(r.replacement(), |
958 IsStore(rep, base, index, value, effect, control)); | 1112 IsStore(rep, base, index, value, effect, control)); |
959 } | 1113 } |
960 } | 1114 } |
961 | 1115 |
962 } // namespace compiler | 1116 } // namespace compiler |
963 } // namespace internal | 1117 } // namespace internal |
964 } // namespace v8 | 1118 } // namespace v8 |
OLD | NEW |