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 Handle<JSFunction> MathFunction(const char* name) { | 33 Node* 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 f; | 42 return HeapConstant(Unique<JSFunction>::CreateUninitialized(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 |
54 // TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering. | 63 // TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering. |
55 Type* const kNumberTypes[] = { | 64 Type* const kNumberTypes[] = { |
56 Type::UnsignedSmall(), Type::Negative32(), Type::Unsigned31(), | 65 Type::UnsignedSmall(), Type::Negative32(), Type::Unsigned31(), |
57 Type::SignedSmall(), Type::Signed32(), Type::Unsigned32(), | 66 Type::SignedSmall(), Type::Signed32(), Type::Unsigned32(), |
58 Type::Integral32(), Type::MinusZero(), Type::NaN(), | 67 Type::Integral32(), Type::MinusZero(), Type::NaN(), |
59 Type::OrderedNumber(), Type::PlainNumber(), Type::Number()}; | 68 Type::OrderedNumber(), Type::PlainNumber(), Type::Number()}; |
60 | 69 |
61 } // namespace | 70 } // namespace |
62 | 71 |
63 | 72 |
64 // ----------------------------------------------------------------------------- | 73 // ----------------------------------------------------------------------------- |
65 // Math.max | 74 // Math.max |
66 | 75 |
67 | 76 |
68 TEST_F(JSBuiltinReducerTest, MathMax0) { | 77 TEST_F(JSBuiltinReducerTest, MathMax0) { |
69 Handle<JSFunction> f = MathFunction("max"); | 78 Node* function = MathFunction("max"); |
70 | 79 |
71 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); | 80 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { |
72 Node* call = | 81 Node* call = graph()->NewNode( |
73 graph()->NewNode(javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS), | 82 javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS, language_mode), |
74 fun, UndefinedConstant()); | 83 function, UndefinedConstant()); |
75 Reduction r = Reduce(call); | 84 Reduction r = Reduce(call); |
76 | 85 |
77 ASSERT_TRUE(r.Changed()); | 86 ASSERT_TRUE(r.Changed()); |
78 EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY)); | 87 EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY)); |
| 88 } |
79 } | 89 } |
80 | 90 |
81 | 91 |
82 TEST_F(JSBuiltinReducerTest, MathMax1) { | 92 TEST_F(JSBuiltinReducerTest, MathMax1) { |
83 Handle<JSFunction> f = MathFunction("max"); | 93 Node* function = MathFunction("max"); |
84 | 94 |
85 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 95 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { |
86 Node* p0 = Parameter(t0, 0); | 96 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
87 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); | 97 Node* p0 = Parameter(t0, 0); |
88 Node* call = | 98 Node* call = graph()->NewNode( |
89 graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS), | 99 javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode), |
90 fun, UndefinedConstant(), p0); | 100 function, UndefinedConstant(), p0); |
91 Reduction r = Reduce(call); | 101 Reduction r = Reduce(call); |
92 | 102 |
93 ASSERT_TRUE(r.Changed()); | 103 ASSERT_TRUE(r.Changed()); |
94 EXPECT_THAT(r.replacement(), p0); | 104 EXPECT_THAT(r.replacement(), p0); |
| 105 } |
95 } | 106 } |
96 } | 107 } |
97 | 108 |
98 | 109 |
99 TEST_F(JSBuiltinReducerTest, MathMax2) { | 110 TEST_F(JSBuiltinReducerTest, MathMax2) { |
100 Handle<JSFunction> f = MathFunction("max"); | 111 Node* function = MathFunction("max"); |
101 | 112 |
102 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 113 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { |
103 TRACED_FOREACH(Type*, t1, kNumberTypes) { | 114 TRACED_FOREACH(Type*, t0, kIntegral32Types) { |
104 Node* p0 = Parameter(t0, 0); | 115 TRACED_FOREACH(Type*, t1, kIntegral32Types) { |
105 Node* p1 = Parameter(t1, 1); | 116 Node* p0 = Parameter(t0, 0); |
106 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); | 117 Node* p1 = Parameter(t1, 1); |
107 Node* call = graph()->NewNode( | 118 Node* call = |
108 javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun, | 119 graph()->NewNode(javascript()->CallFunction( |
109 UndefinedConstant(), p0, p1); | 120 4, NO_CALL_FUNCTION_FLAGS, language_mode), |
110 Reduction r = Reduce(call); | 121 function, UndefinedConstant(), p0, p1); |
| 122 Reduction r = Reduce(call); |
111 | 123 |
112 if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) { | |
113 ASSERT_TRUE(r.Changed()); | 124 ASSERT_TRUE(r.Changed()); |
114 EXPECT_THAT(r.replacement(), | 125 EXPECT_THAT(r.replacement(), |
115 IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1)); | 126 IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1)); |
116 } else { | |
117 ASSERT_FALSE(r.Changed()); | |
118 EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode()); | |
119 } | 127 } |
120 } | 128 } |
121 } | 129 } |
122 } | 130 } |
123 | 131 |
124 | 132 |
125 // ----------------------------------------------------------------------------- | 133 // ----------------------------------------------------------------------------- |
126 // Math.imul | 134 // Math.imul |
127 | 135 |
128 | 136 |
129 TEST_F(JSBuiltinReducerTest, MathImul) { | 137 TEST_F(JSBuiltinReducerTest, MathImul) { |
130 Handle<JSFunction> f = MathFunction("imul"); | 138 Node* function = MathFunction("imul"); |
131 | 139 |
132 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 140 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { |
133 TRACED_FOREACH(Type*, t1, kNumberTypes) { | 141 TRACED_FOREACH(Type*, t0, kIntegral32Types) { |
134 Node* p0 = Parameter(t0, 0); | 142 TRACED_FOREACH(Type*, t1, kIntegral32Types) { |
135 Node* p1 = Parameter(t1, 1); | 143 Node* p0 = Parameter(t0, 0); |
136 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); | 144 Node* p1 = Parameter(t1, 1); |
137 Node* call = graph()->NewNode( | 145 Node* call = |
138 javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun, | 146 graph()->NewNode(javascript()->CallFunction( |
139 UndefinedConstant(), p0, p1); | 147 4, NO_CALL_FUNCTION_FLAGS, language_mode), |
140 Reduction r = Reduce(call); | 148 function, UndefinedConstant(), p0, p1); |
| 149 Reduction r = Reduce(call); |
141 | 150 |
142 if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) { | |
143 ASSERT_TRUE(r.Changed()); | 151 ASSERT_TRUE(r.Changed()); |
144 EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1)); | 152 EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1)); |
145 } else { | |
146 ASSERT_FALSE(r.Changed()); | |
147 EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode()); | |
148 } | 153 } |
149 } | 154 } |
150 } | 155 } |
151 } | 156 } |
152 | 157 |
153 | 158 |
154 // ----------------------------------------------------------------------------- | 159 // ----------------------------------------------------------------------------- |
155 // Math.fround | 160 // Math.fround |
156 | 161 |
157 | 162 |
158 TEST_F(JSBuiltinReducerTest, MathFround) { | 163 TEST_F(JSBuiltinReducerTest, MathFround) { |
159 Handle<JSFunction> f = MathFunction("fround"); | 164 Node* function = MathFunction("fround"); |
160 | 165 |
161 TRACED_FOREACH(Type*, t0, kNumberTypes) { | 166 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { |
162 Node* p0 = Parameter(t0, 0); | 167 TRACED_FOREACH(Type*, t0, kNumberTypes) { |
163 Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f)); | 168 Node* p0 = Parameter(t0, 0); |
164 Node* call = | 169 Node* call = graph()->NewNode( |
165 graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS), | 170 javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode), |
166 fun, UndefinedConstant(), p0); | 171 function, UndefinedConstant(), p0); |
167 Reduction r = Reduce(call); | 172 Reduction r = Reduce(call); |
168 | 173 |
169 ASSERT_TRUE(r.Changed()); | 174 ASSERT_TRUE(r.Changed()); |
170 EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0)); | 175 EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0)); |
| 176 } |
171 } | 177 } |
172 } | 178 } |
173 | 179 |
174 } // namespace compiler | 180 } // namespace compiler |
175 } // namespace internal | 181 } // namespace internal |
176 } // namespace v8 | 182 } // namespace v8 |
OLD | NEW |