| 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/js-graph.h" | 6 #include "src/compiler/js-graph.h" |
| 7 #include "src/compiler/js-operator.h" | 7 #include "src/compiler/js-operator.h" |
| 8 #include "src/compiler/js-typed-lowering.h" | 8 #include "src/compiler/js-typed-lowering.h" |
| 9 #include "src/compiler/machine-operator.h" | 9 #include "src/compiler/machine-operator.h" |
| 10 #include "src/compiler/node-properties-inl.h" | 10 #include "src/compiler/node-properties-inl.h" |
| 11 #include "src/compiler/typer.h" | |
| 12 #include "test/unittests/compiler/compiler-test-utils.h" | 11 #include "test/unittests/compiler/compiler-test-utils.h" |
| 13 #include "test/unittests/compiler/graph-unittest.h" | 12 #include "test/unittests/compiler/graph-unittest.h" |
| 14 #include "test/unittests/compiler/node-test-utils.h" | 13 #include "test/unittests/compiler/node-test-utils.h" |
| 15 #include "testing/gmock-support.h" | 14 #include "testing/gmock-support.h" |
| 16 | 15 |
| 17 using testing::BitEq; | 16 using testing::BitEq; |
| 18 | 17 |
| 19 | 18 |
| 20 namespace v8 { | 19 namespace v8 { |
| 21 namespace internal { | 20 namespace internal { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 } | 65 } |
| 67 | 66 |
| 68 JSOperatorBuilder* javascript() { return &javascript_; } | 67 JSOperatorBuilder* javascript() { return &javascript_; } |
| 69 | 68 |
| 70 private: | 69 private: |
| 71 JSOperatorBuilder javascript_; | 70 JSOperatorBuilder javascript_; |
| 72 }; | 71 }; |
| 73 | 72 |
| 74 | 73 |
| 75 // ----------------------------------------------------------------------------- | 74 // ----------------------------------------------------------------------------- |
| 75 // JSUnaryNot |
| 76 |
| 77 |
| 78 TEST_F(JSTypedLoweringTest, JSUnaryNotWithBoolean) { |
| 79 Node* input = Parameter(Type::Boolean(), 0); |
| 80 Node* context = Parameter(Type::Any(), 1); |
| 81 Reduction r = |
| 82 Reduce(graph()->NewNode(javascript()->UnaryNot(), input, context)); |
| 83 ASSERT_TRUE(r.Changed()); |
| 84 EXPECT_THAT(r.replacement(), IsBooleanNot(input)); |
| 85 } |
| 86 |
| 87 |
| 88 TEST_F(JSTypedLoweringTest, JSUnaryNotWithFalsish) { |
| 89 Handle<Object> zero = factory()->NewNumber(0); |
| 90 Node* input = Parameter( |
| 91 Type::Union( |
| 92 Type::MinusZero(), |
| 93 Type::Union( |
| 94 Type::NaN(), |
| 95 Type::Union( |
| 96 Type::Null(), |
| 97 Type::Union( |
| 98 Type::Undefined(), |
| 99 Type::Union( |
| 100 Type::Undetectable(), |
| 101 Type::Union( |
| 102 Type::Constant(factory()->false_value(), zone()), |
| 103 Type::Range(zero, zero, zone()), zone()), |
| 104 zone()), |
| 105 zone()), |
| 106 zone()), |
| 107 zone()), |
| 108 zone()), |
| 109 0); |
| 110 Node* context = Parameter(Type::Any(), 1); |
| 111 Reduction r = |
| 112 Reduce(graph()->NewNode(javascript()->UnaryNot(), input, context)); |
| 113 ASSERT_TRUE(r.Changed()); |
| 114 EXPECT_THAT(r.replacement(), IsTrueConstant()); |
| 115 } |
| 116 |
| 117 |
| 118 TEST_F(JSTypedLoweringTest, JSUnaryNotWithTruish) { |
| 119 Node* input = Parameter( |
| 120 Type::Union( |
| 121 Type::Constant(factory()->true_value(), zone()), |
| 122 Type::Union(Type::DetectableReceiver(), Type::Symbol(), zone()), |
| 123 zone()), |
| 124 0); |
| 125 Node* context = Parameter(Type::Any(), 1); |
| 126 Reduction r = |
| 127 Reduce(graph()->NewNode(javascript()->UnaryNot(), input, context)); |
| 128 ASSERT_TRUE(r.Changed()); |
| 129 EXPECT_THAT(r.replacement(), IsFalseConstant()); |
| 130 } |
| 131 |
| 132 |
| 133 TEST_F(JSTypedLoweringTest, JSUnaryNotWithNonZeroPlainNumber) { |
| 134 Node* input = Parameter( |
| 135 Type::Range(factory()->NewNumber(1), factory()->NewNumber(42), zone()), |
| 136 0); |
| 137 Node* context = Parameter(Type::Any(), 1); |
| 138 Reduction r = |
| 139 Reduce(graph()->NewNode(javascript()->UnaryNot(), input, context)); |
| 140 ASSERT_TRUE(r.Changed()); |
| 141 EXPECT_THAT(r.replacement(), IsFalseConstant()); |
| 142 } |
| 143 |
| 144 |
| 145 TEST_F(JSTypedLoweringTest, JSUnaryNotWithAny) { |
| 146 Node* input = Parameter(Type::Any(), 0); |
| 147 Node* context = Parameter(Type::Any(), 1); |
| 148 Reduction r = |
| 149 Reduce(graph()->NewNode(javascript()->UnaryNot(), input, context)); |
| 150 ASSERT_TRUE(r.Changed()); |
| 151 EXPECT_THAT(r.replacement(), IsBooleanNot(IsAnyToBoolean(input))); |
| 152 } |
| 153 |
| 154 |
| 155 // ----------------------------------------------------------------------------- |
| 76 // JSToBoolean | 156 // JSToBoolean |
| 77 | 157 |
| 78 | 158 |
| 79 TEST_F(JSTypedLoweringTest, JSToBooleanWithBoolean) { | 159 TEST_F(JSTypedLoweringTest, JSToBooleanWithBoolean) { |
| 80 Node* input = Parameter(Type::Boolean()); | 160 Node* input = Parameter(Type::Boolean(), 0); |
| 81 Node* context = UndefinedConstant(); | 161 Node* context = Parameter(Type::Any(), 1); |
| 82 | |
| 83 Reduction r = | 162 Reduction r = |
| 84 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 163 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); |
| 85 ASSERT_TRUE(r.Changed()); | 164 ASSERT_TRUE(r.Changed()); |
| 86 EXPECT_EQ(input, r.replacement()); | 165 EXPECT_EQ(input, r.replacement()); |
| 87 } | 166 } |
| 88 | 167 |
| 89 | 168 |
| 90 TEST_F(JSTypedLoweringTest, JSToBooleanWithUndefined) { | 169 TEST_F(JSTypedLoweringTest, JSToBooleanWithFalsish) { |
| 91 Node* input = Parameter(Type::Undefined()); | 170 Handle<Object> zero = factory()->NewNumber(0); |
| 92 Node* context = UndefinedConstant(); | 171 Node* input = Parameter( |
| 93 | 172 Type::Union( |
| 173 Type::MinusZero(), |
| 174 Type::Union( |
| 175 Type::NaN(), |
| 176 Type::Union( |
| 177 Type::Null(), |
| 178 Type::Union( |
| 179 Type::Undefined(), |
| 180 Type::Union( |
| 181 Type::Undetectable(), |
| 182 Type::Union( |
| 183 Type::Constant(factory()->false_value(), zone()), |
| 184 Type::Range(zero, zero, zone()), zone()), |
| 185 zone()), |
| 186 zone()), |
| 187 zone()), |
| 188 zone()), |
| 189 zone()), |
| 190 0); |
| 191 Node* context = Parameter(Type::Any(), 1); |
| 94 Reduction r = | 192 Reduction r = |
| 95 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 193 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); |
| 96 ASSERT_TRUE(r.Changed()); | 194 ASSERT_TRUE(r.Changed()); |
| 97 EXPECT_THAT(r.replacement(), IsFalseConstant()); | 195 EXPECT_THAT(r.replacement(), IsFalseConstant()); |
| 98 } | 196 } |
| 99 | 197 |
| 100 | 198 |
| 101 TEST_F(JSTypedLoweringTest, JSToBooleanWithNull) { | 199 TEST_F(JSTypedLoweringTest, JSToBooleanWithTruish) { |
| 102 Node* input = Parameter(Type::Null()); | 200 Node* input = Parameter( |
| 103 Node* context = UndefinedConstant(); | 201 Type::Union( |
| 104 | 202 Type::Constant(factory()->true_value(), zone()), |
| 105 Reduction r = | 203 Type::Union(Type::DetectableReceiver(), Type::Symbol(), zone()), |
| 106 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 204 zone()), |
| 107 ASSERT_TRUE(r.Changed()); | 205 0); |
| 108 EXPECT_THAT(r.replacement(), IsFalseConstant()); | 206 Node* context = Parameter(Type::Any(), 1); |
| 109 } | |
| 110 | |
| 111 | |
| 112 TEST_F(JSTypedLoweringTest, JSToBooleanWithDetectableReceiver) { | |
| 113 Node* input = Parameter(Type::DetectableReceiver()); | |
| 114 Node* context = UndefinedConstant(); | |
| 115 | |
| 116 Reduction r = | 207 Reduction r = |
| 117 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 208 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); |
| 118 ASSERT_TRUE(r.Changed()); | 209 ASSERT_TRUE(r.Changed()); |
| 119 EXPECT_THAT(r.replacement(), IsTrueConstant()); | 210 EXPECT_THAT(r.replacement(), IsTrueConstant()); |
| 120 } | 211 } |
| 121 | 212 |
| 122 | 213 |
| 123 TEST_F(JSTypedLoweringTest, JSToBooleanWithUndetectable) { | 214 TEST_F(JSTypedLoweringTest, JSToBooleanWithNonZeroPlainNumber) { |
| 124 Node* input = Parameter(Type::Undetectable()); | 215 Node* input = |
| 125 Node* context = UndefinedConstant(); | 216 Parameter(Type::Range(factory()->NewNumber(1), |
| 126 | 217 factory()->NewNumber(V8_INFINITY), zone()), |
| 218 0); |
| 219 Node* context = Parameter(Type::Any(), 1); |
| 127 Reduction r = | 220 Reduction r = |
| 128 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 221 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); |
| 129 ASSERT_TRUE(r.Changed()); | 222 ASSERT_TRUE(r.Changed()); |
| 130 EXPECT_THAT(r.replacement(), IsFalseConstant()); | 223 EXPECT_THAT(r.replacement(), IsTrueConstant()); |
| 131 } | 224 } |
| 132 | 225 |
| 133 | 226 |
| 134 TEST_F(JSTypedLoweringTest, JSToBooleanWithOrderedNumber) { | 227 TEST_F(JSTypedLoweringTest, JSToBooleanWithAny) { |
| 135 Node* input = Parameter(Type::OrderedNumber()); | 228 Node* input = Parameter(Type::Any(), 0); |
| 136 Node* context = UndefinedConstant(); | 229 Node* context = Parameter(Type::Any(), 1); |
| 137 | |
| 138 Reduction r = | 230 Reduction r = |
| 139 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | 231 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); |
| 140 ASSERT_TRUE(r.Changed()); | 232 ASSERT_TRUE(r.Changed()); |
| 141 EXPECT_THAT(r.replacement(), | 233 EXPECT_THAT(r.replacement(), IsAnyToBoolean(input)); |
| 142 IsBooleanNot(IsNumberEqual(input, IsNumberConstant(BitEq(0.0))))); | |
| 143 } | 234 } |
| 144 | 235 |
| 145 | 236 |
| 146 TEST_F(JSTypedLoweringTest, JSToBooleanWithString) { | |
| 147 Node* input = Parameter(Type::String()); | |
| 148 Node* context = UndefinedConstant(); | |
| 149 | |
| 150 Reduction r = | |
| 151 Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); | |
| 152 ASSERT_TRUE(r.Changed()); | |
| 153 EXPECT_THAT(r.replacement(), | |
| 154 IsBooleanNot(IsNumberEqual( | |
| 155 IsLoadField(AccessBuilder::ForStringLength(), input, | |
| 156 graph()->start(), graph()->start()), | |
| 157 IsNumberConstant(BitEq(0.0))))); | |
| 158 } | |
| 159 | |
| 160 | |
| 161 TEST_F(JSTypedLoweringTest, JSToBooleanWithPhi) { | |
| 162 Node* p0 = Parameter(Type::OrderedNumber(), 0); | |
| 163 Node* p1 = Parameter(Type::Boolean(), 1); | |
| 164 Node* context = UndefinedConstant(); | |
| 165 Node* control = graph()->start(); | |
| 166 | |
| 167 Reduction r = Reduce(graph()->NewNode( | |
| 168 javascript()->ToBoolean(), | |
| 169 graph()->NewNode(common()->Phi(kMachAnyTagged, 2), p0, p1, control), | |
| 170 context)); | |
| 171 ASSERT_TRUE(r.Changed()); | |
| 172 EXPECT_THAT(r.replacement(), | |
| 173 IsPhi(kMachAnyTagged, IsBooleanNot(IsNumberEqual( | |
| 174 p0, IsNumberConstant(BitEq(0.0)))), | |
| 175 p1, control)); | |
| 176 } | |
| 177 | |
| 178 | |
| 179 TEST_F(JSTypedLoweringTest, JSToBooleanWithSelect) { | |
| 180 Node* p0 = Parameter(Type::Boolean(), 0); | |
| 181 Node* p1 = Parameter(Type::DetectableReceiver(), 1); | |
| 182 Node* p2 = Parameter(Type::OrderedNumber(), 2); | |
| 183 Node* context = UndefinedConstant(); | |
| 184 | |
| 185 Reduction r = Reduce(graph()->NewNode( | |
| 186 javascript()->ToBoolean(), | |
| 187 graph()->NewNode(common()->Select(kMachAnyTagged, BranchHint::kTrue), p0, | |
| 188 p1, p2), | |
| 189 context)); | |
| 190 ASSERT_TRUE(r.Changed()); | |
| 191 EXPECT_THAT( | |
| 192 r.replacement(), | |
| 193 IsSelect(kMachAnyTagged, p0, IsTrueConstant(), | |
| 194 IsBooleanNot(IsNumberEqual(p2, IsNumberConstant(BitEq(0.0)))))); | |
| 195 } | |
| 196 | |
| 197 | |
| 198 // ----------------------------------------------------------------------------- | 237 // ----------------------------------------------------------------------------- |
| 199 // JSToNumber | 238 // JSToNumber |
| 200 | 239 |
| 201 | 240 |
| 202 TEST_F(JSTypedLoweringTest, JSToNumberWithPlainPrimitive) { | 241 TEST_F(JSTypedLoweringTest, JSToNumberWithPlainPrimitive) { |
| 203 Node* const input = Parameter(Type::PlainPrimitive(), 0); | 242 Node* const input = Parameter(Type::PlainPrimitive(), 0); |
| 204 Node* const context = Parameter(Type::Any(), 1); | 243 Node* const context = Parameter(Type::Any(), 1); |
| 205 Node* const effect = graph()->start(); | 244 Node* const effect = graph()->start(); |
| 206 Node* const control = graph()->start(); | 245 Node* const control = graph()->start(); |
| 207 Reduction r = Reduce(graph()->NewNode(javascript()->ToNumber(), input, | 246 Reduction r = Reduce(graph()->NewNode(javascript()->ToNumber(), input, |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 IsStoreElement( | 673 IsStoreElement( |
| 635 access, IsIntPtrConstant(bit_cast<intptr_t>(&backing_store[0])), | 674 access, IsIntPtrConstant(bit_cast<intptr_t>(&backing_store[0])), |
| 636 key, value, effect, control)); | 675 key, value, effect, control)); |
| 637 } | 676 } |
| 638 } | 677 } |
| 639 } | 678 } |
| 640 | 679 |
| 641 } // namespace compiler | 680 } // namespace compiler |
| 642 } // namespace internal | 681 } // namespace internal |
| 643 } // namespace v8 | 682 } // namespace v8 |
| OLD | NEW |