| 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/js-builtin-reducer.h" | 5 #include "src/compiler/js-builtin-reducer.h" |
| 6 #include "src/compiler/js-graph.h" | 6 #include "src/compiler/js-graph.h" |
| 7 #include "src/compiler/node-properties.h" | 7 #include "src/compiler/node-properties.h" |
| 8 #include "src/compiler/typer.h" | 8 #include "src/compiler/typer.h" |
| 9 #include "test/unittests/compiler/graph-unittest.h" | 9 #include "test/unittests/compiler/graph-unittest.h" |
| 10 #include "test/unittests/compiler/node-test-utils.h" | 10 #include "test/unittests/compiler/node-test-utils.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 protected: | 24 protected: |
| 25 Reduction Reduce(Node* node, MachineOperatorBuilder::Flags flags = | 25 Reduction Reduce(Node* node, MachineOperatorBuilder::Flags flags = |
| 26 MachineOperatorBuilder::Flag::kNoFlags) { | 26 MachineOperatorBuilder::Flag::kNoFlags) { |
| 27 MachineOperatorBuilder machine(zone(), kMachPtr, flags); | 27 MachineOperatorBuilder machine(zone(), kMachPtr, flags); |
| 28 JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine); | 28 JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine); |
| 29 JSBuiltinReducer reducer(&jsgraph); | 29 JSBuiltinReducer reducer(&jsgraph); |
| 30 return reducer.Reduce(node); | 30 return reducer.Reduce(node); |
| 31 } | 31 } |
| 32 | 32 |
| 33 Node* MathFunction(const char* name) { | 33 Handle<JSFunction> MathFunction(const char* name) { |
| 34 Handle<Object> m = | 34 Handle<Object> m = |
| 35 JSObject::GetProperty(isolate()->global_object(), | 35 JSObject::GetProperty(isolate()->global_object(), |
| 36 isolate()->factory()->NewStringFromAsciiChecked( | 36 isolate()->factory()->NewStringFromAsciiChecked( |
| 37 "Math")).ToHandleChecked(); | 37 "Math")).ToHandleChecked(); |
| 38 Handle<JSFunction> f = Handle<JSFunction>::cast( | 38 Handle<JSFunction> f = Handle<JSFunction>::cast( |
| 39 JSObject::GetProperty( | 39 JSObject::GetProperty( |
| 40 m, isolate()->factory()->NewStringFromAsciiChecked(name)) | 40 m, isolate()->factory()->NewStringFromAsciiChecked(name)) |
| 41 .ToHandleChecked()); | 41 .ToHandleChecked()); |
| 42 return HeapConstant(Unique<JSFunction>::CreateUninitialized(f)); | 42 return f; |
| 43 } | 43 } |
| 44 | 44 |
| 45 JSOperatorBuilder* javascript() { return &javascript_; } | 45 JSOperatorBuilder* javascript() { return &javascript_; } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 JSOperatorBuilder javascript_; | 48 JSOperatorBuilder javascript_; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 | 51 |
| 52 namespace { | 52 namespace { |
| 53 | 53 |
| 54 Type* const kIntegral32Types[] = {Type::UnsignedSmall(), Type::Negative32(), | |
| 55 Type::Unsigned31(), Type::SignedSmall(), | |
| 56 Type::Signed32(), Type::Unsigned32(), | |
| 57 Type::Integral32()}; | |
| 58 | |
| 59 | |
| 60 const LanguageMode kLanguageModes[] = {SLOPPY, STRICT, STRONG}; | |
| 61 | |
| 62 | |
| 63 // TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering. | 54 // TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering. |
| 64 Type* const kNumberTypes[] = { | 55 Type* const kNumberTypes[] = { |
| 65 Type::UnsignedSmall(), Type::Negative32(), Type::Unsigned31(), | 56 Type::UnsignedSmall(), Type::Negative32(), Type::Unsigned31(), |
| 66 Type::SignedSmall(), Type::Signed32(), Type::Unsigned32(), | 57 Type::SignedSmall(), Type::Signed32(), Type::Unsigned32(), |
| 67 Type::Integral32(), Type::MinusZero(), Type::NaN(), | 58 Type::Integral32(), Type::MinusZero(), Type::NaN(), |
| 68 Type::OrderedNumber(), Type::PlainNumber(), Type::Number()}; | 59 Type::OrderedNumber(), Type::PlainNumber(), Type::Number()}; |
| 69 | 60 |
| 70 } // namespace | 61 } // namespace |
| 71 | 62 |
| 72 | 63 |
| 73 // ----------------------------------------------------------------------------- | 64 // ----------------------------------------------------------------------------- |
| 74 // Math.max | 65 // Math.max |
| 75 | 66 |
| 76 | 67 |
| 77 TEST_F(JSBuiltinReducerTest, MathMax0) { | 68 TEST_F(JSBuiltinReducerTest, MathMax0) { |
| 78 Node* function = MathFunction("max"); | 69 Handle<JSFunction> f = MathFunction("max"); |
| 79 | 70 |
| 80 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { | 71 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); |
| 81 Node* call = graph()->NewNode( | 72 Node* call = |
| 82 javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS, language_mode), | 73 graph()->NewNode(javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS), |
| 83 function, UndefinedConstant()); | 74 fun, UndefinedConstant()); |
| 84 Reduction r = Reduce(call); | 75 Reduction r = Reduce(call); |
| 85 | 76 |
| 86 ASSERT_TRUE(r.Changed()); | 77 ASSERT_TRUE(r.Changed()); |
| 87 EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY)); | 78 EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY)); |
| 88 } | |
| 89 } | 79 } |
| 90 | 80 |
| 91 | 81 |
| 92 TEST_F(JSBuiltinReducerTest, MathMax1) { | 82 TEST_F(JSBuiltinReducerTest, MathMax1) { |
| 93 Node* function = MathFunction("max"); | 83 Handle<JSFunction> f = MathFunction("max"); |
| 94 | 84 |
| 95 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { | 85 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
| 96 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 86 Node* p0 = Parameter(t0, 0); |
| 97 Node* p0 = Parameter(t0, 0); | 87 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); |
| 98 Node* call = graph()->NewNode( | 88 Node* call = |
| 99 javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode), | 89 graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS), |
| 100 function, UndefinedConstant(), p0); | 90 fun, UndefinedConstant(), p0); |
| 101 Reduction r = Reduce(call); | 91 Reduction r = Reduce(call); |
| 102 | 92 |
| 103 ASSERT_TRUE(r.Changed()); | 93 ASSERT_TRUE(r.Changed()); |
| 104 EXPECT_THAT(r.replacement(), p0); | 94 EXPECT_THAT(r.replacement(), p0); |
| 105 } | |
| 106 } | 95 } |
| 107 } | 96 } |
| 108 | 97 |
| 109 | 98 |
| 110 TEST_F(JSBuiltinReducerTest, MathMax2) { | 99 TEST_F(JSBuiltinReducerTest, MathMax2) { |
| 111 Node* function = MathFunction("max"); | 100 Handle<JSFunction> f = MathFunction("max"); |
| 112 | 101 |
| 113 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { | 102 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
| 114 TRACED_FOREACH(Type*, t0, kIntegral32Types) { | 103 TRACED_FOREACH(Type*, t1, kNumberTypes) { |
| 115 TRACED_FOREACH(Type*, t1, kIntegral32Types) { | 104 Node* p0 = Parameter(t0, 0); |
| 116 Node* p0 = Parameter(t0, 0); | 105 Node* p1 = Parameter(t1, 1); |
| 117 Node* p1 = Parameter(t1, 1); | 106 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); |
| 118 Node* call = | 107 Node* call = graph()->NewNode( |
| 119 graph()->NewNode(javascript()->CallFunction( | 108 javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun, |
| 120 4, NO_CALL_FUNCTION_FLAGS, language_mode), | 109 UndefinedConstant(), p0, p1); |
| 121 function, UndefinedConstant(), p0, p1); | 110 Reduction r = Reduce(call); |
| 122 Reduction r = Reduce(call); | |
| 123 | 111 |
| 112 if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) { |
| 124 ASSERT_TRUE(r.Changed()); | 113 ASSERT_TRUE(r.Changed()); |
| 125 EXPECT_THAT(r.replacement(), | 114 EXPECT_THAT(r.replacement(), |
| 126 IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1)); | 115 IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1)); |
| 116 } else { |
| 117 ASSERT_FALSE(r.Changed()); |
| 118 EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode()); |
| 127 } | 119 } |
| 128 } | 120 } |
| 129 } | 121 } |
| 130 } | 122 } |
| 131 | 123 |
| 132 | 124 |
| 133 // ----------------------------------------------------------------------------- | 125 // ----------------------------------------------------------------------------- |
| 134 // Math.imul | 126 // Math.imul |
| 135 | 127 |
| 136 | 128 |
| 137 TEST_F(JSBuiltinReducerTest, MathImul) { | 129 TEST_F(JSBuiltinReducerTest, MathImul) { |
| 138 Node* function = MathFunction("imul"); | 130 Handle<JSFunction> f = MathFunction("imul"); |
| 139 | 131 |
| 140 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { | 132 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
| 141 TRACED_FOREACH(Type*, t0, kIntegral32Types) { | 133 TRACED_FOREACH(Type*, t1, kNumberTypes) { |
| 142 TRACED_FOREACH(Type*, t1, kIntegral32Types) { | 134 Node* p0 = Parameter(t0, 0); |
| 143 Node* p0 = Parameter(t0, 0); | 135 Node* p1 = Parameter(t1, 1); |
| 144 Node* p1 = Parameter(t1, 1); | 136 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); |
| 145 Node* call = | 137 Node* call = graph()->NewNode( |
| 146 graph()->NewNode(javascript()->CallFunction( | 138 javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun, |
| 147 4, NO_CALL_FUNCTION_FLAGS, language_mode), | 139 UndefinedConstant(), p0, p1); |
| 148 function, UndefinedConstant(), p0, p1); | 140 Reduction r = Reduce(call); |
| 149 Reduction r = Reduce(call); | |
| 150 | 141 |
| 142 if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) { |
| 151 ASSERT_TRUE(r.Changed()); | 143 ASSERT_TRUE(r.Changed()); |
| 152 EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1)); | 144 EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1)); |
| 145 } else { |
| 146 ASSERT_FALSE(r.Changed()); |
| 147 EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode()); |
| 153 } | 148 } |
| 154 } | 149 } |
| 155 } | 150 } |
| 156 } | 151 } |
| 157 | 152 |
| 158 | 153 |
| 159 // ----------------------------------------------------------------------------- | 154 // ----------------------------------------------------------------------------- |
| 160 // Math.fround | 155 // Math.fround |
| 161 | 156 |
| 162 | 157 |
| 163 TEST_F(JSBuiltinReducerTest, MathFround) { | 158 TEST_F(JSBuiltinReducerTest, MathFround) { |
| 164 Node* function = MathFunction("fround"); | 159 Handle<JSFunction> f = MathFunction("fround"); |
| 165 | 160 |
| 166 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { | 161 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
| 167 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 162 Node* p0 = Parameter(t0, 0); |
| 168 Node* p0 = Parameter(t0, 0); | 163 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); |
| 169 Node* call = graph()->NewNode( | 164 Node* call = |
| 170 javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode), | 165 graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS), |
| 171 function, UndefinedConstant(), p0); | 166 fun, UndefinedConstant(), p0); |
| 172 Reduction r = Reduce(call); | 167 Reduction r = Reduce(call); |
| 173 | 168 |
| 174 ASSERT_TRUE(r.Changed()); | 169 ASSERT_TRUE(r.Changed()); |
| 175 EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0)); | 170 EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0)); |
| 176 } | |
| 177 } | 171 } |
| 178 } | 172 } |
| 179 | 173 |
| 180 } // namespace compiler | 174 } // namespace compiler |
| 181 } // namespace internal | 175 } // namespace internal |
| 182 } // namespace v8 | 176 } // namespace v8 |
| OLD | NEW |