OLD | NEW |
| (Empty) |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/compiler.h" | |
6 | |
7 #include "src/compiler/access-builder.h" | |
8 #include "src/compiler/js-graph.h" | |
9 #include "src/compiler/js-operator.h" | |
10 #include "src/compiler/js-type-feedback.h" | |
11 #include "src/compiler/machine-operator.h" | |
12 #include "src/compiler/node-matchers.h" | |
13 #include "src/compiler/node-properties.h" | |
14 #include "src/compiler/operator-properties.h" | |
15 | |
16 #include "test/unittests/compiler/compiler-test-utils.h" | |
17 #include "test/unittests/compiler/graph-unittest.h" | |
18 #include "test/unittests/compiler/node-test-utils.h" | |
19 #include "testing/gmock-support.h" | |
20 | |
21 using testing::Capture; | |
22 | |
23 | |
24 namespace v8 { | |
25 namespace internal { | |
26 namespace compiler { | |
27 | |
28 class JSTypeFeedbackTest : public TypedGraphTest { | |
29 public: | |
30 JSTypeFeedbackTest() | |
31 : TypedGraphTest(3), | |
32 javascript_(zone()), | |
33 dependencies_(isolate(), zone()) {} | |
34 ~JSTypeFeedbackTest() override { dependencies_.Rollback(); } | |
35 | |
36 protected: | |
37 Reduction Reduce(Node* node) { | |
38 Handle<GlobalObject> global_object( | |
39 isolate()->native_context()->global_object(), isolate()); | |
40 | |
41 MachineOperatorBuilder machine(zone()); | |
42 JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine); | |
43 JSTypeFeedbackTable table(zone()); | |
44 JSTypeFeedbackSpecializer reducer(&jsgraph, &table, nullptr, global_object, | |
45 &dependencies_); | |
46 return reducer.Reduce(node); | |
47 } | |
48 | |
49 Node* EmptyFrameState() { | |
50 MachineOperatorBuilder machine(zone()); | |
51 JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine); | |
52 return jsgraph.EmptyFrameState(); | |
53 } | |
54 | |
55 JSOperatorBuilder* javascript() { return &javascript_; } | |
56 | |
57 void SetGlobalProperty(const char* string, int value) { | |
58 SetGlobalProperty(string, Handle<Smi>(Smi::FromInt(value), isolate())); | |
59 } | |
60 | |
61 void SetGlobalProperty(const char* string, double value) { | |
62 SetGlobalProperty(string, isolate()->factory()->NewNumber(value)); | |
63 } | |
64 | |
65 void SetGlobalProperty(const char* string, Handle<Object> value) { | |
66 Handle<JSObject> global(isolate()->context()->global_object(), isolate()); | |
67 Handle<String> name = | |
68 isolate()->factory()->NewStringFromAsciiChecked(string); | |
69 MaybeHandle<Object> result = | |
70 JSReceiver::SetProperty(global, name, value, SLOPPY); | |
71 result.Assert(); | |
72 } | |
73 | |
74 Node* ReturnLoadNamedFromGlobal(const char* string, Node* effect, | |
75 Node* control) { | |
76 VectorSlotPair feedback(Handle<TypeFeedbackVector>::null(), | |
77 FeedbackVectorICSlot::Invalid()); | |
78 Node* global = Parameter(Type::GlobalObject()); | |
79 Node* context = UndefinedConstant(); | |
80 | |
81 Unique<Name> name = Unique<Name>::CreateUninitialized( | |
82 isolate()->factory()->NewStringFromAsciiChecked(string)); | |
83 Node* load = graph()->NewNode(javascript()->LoadNamed(name, feedback), | |
84 global, context); | |
85 if (FLAG_turbo_deoptimization) { | |
86 load->AppendInput(zone(), EmptyFrameState()); | |
87 } | |
88 load->AppendInput(zone(), effect); | |
89 load->AppendInput(zone(), control); | |
90 Node* if_success = graph()->NewNode(common()->IfSuccess(), load); | |
91 return graph()->NewNode(common()->Return(), load, load, if_success); | |
92 } | |
93 | |
94 CompilationDependencies* dependencies() { return &dependencies_; } | |
95 | |
96 private: | |
97 JSOperatorBuilder javascript_; | |
98 CompilationDependencies dependencies_; | |
99 }; | |
100 | |
101 #define WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION \ | |
102 for (int i = FLAG_turbo_deoptimization = 0; i < 2; \ | |
103 FLAG_turbo_deoptimization = ++i) | |
104 | |
105 | |
106 TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_smi) { | |
107 const int const_value = 111; | |
108 const char* property_name = "banana"; | |
109 SetGlobalProperty(property_name, const_value); | |
110 | |
111 WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION { | |
112 Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(), | |
113 graph()->start()); | |
114 graph()->SetEnd(graph()->NewNode(common()->End(), ret)); | |
115 | |
116 Reduction r = Reduce(ret->InputAt(0)); | |
117 | |
118 if (FLAG_turbo_deoptimization) { | |
119 // Check LoadNamed(global) => HeapConstant[const_value] | |
120 ASSERT_TRUE(r.Changed()); | |
121 EXPECT_THAT(r.replacement(), IsNumberConstant(const_value)); | |
122 | |
123 EXPECT_THAT(ret, IsReturn(IsNumberConstant(const_value), graph()->start(), | |
124 graph()->start())); | |
125 EXPECT_THAT(graph()->end(), IsEnd(ret)); | |
126 | |
127 EXPECT_FALSE(dependencies()->IsEmpty()); | |
128 dependencies()->Rollback(); | |
129 } else { | |
130 ASSERT_FALSE(r.Changed()); | |
131 EXPECT_TRUE(dependencies()->IsEmpty()); | |
132 } | |
133 } | |
134 } | |
135 | |
136 | |
137 TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_derble) { | |
138 const double const_value = -11.25; | |
139 const char* property_name = "kiwi"; | |
140 SetGlobalProperty(property_name, const_value); | |
141 | |
142 WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION { | |
143 Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(), | |
144 graph()->start()); | |
145 graph()->SetEnd(graph()->NewNode(common()->End(), ret)); | |
146 | |
147 Reduction r = Reduce(ret->InputAt(0)); | |
148 | |
149 if (FLAG_turbo_deoptimization) { | |
150 // Check LoadNamed(global) => HeapConstant[const_value] | |
151 ASSERT_TRUE(r.Changed()); | |
152 EXPECT_THAT(r.replacement(), IsNumberConstant(const_value)); | |
153 | |
154 EXPECT_THAT(ret, IsReturn(IsNumberConstant(const_value), graph()->start(), | |
155 graph()->start())); | |
156 EXPECT_THAT(graph()->end(), IsEnd(ret)); | |
157 | |
158 EXPECT_FALSE(dependencies()->IsEmpty()); | |
159 } else { | |
160 ASSERT_FALSE(r.Changed()); | |
161 EXPECT_TRUE(dependencies()->IsEmpty()); | |
162 } | |
163 } | |
164 } | |
165 | |
166 | |
167 TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalConst_string) { | |
168 Unique<HeapObject> const_value = Unique<HeapObject>::CreateImmovable( | |
169 isolate()->factory()->undefined_string()); | |
170 const char* property_name = "mango"; | |
171 SetGlobalProperty(property_name, const_value.handle()); | |
172 | |
173 WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION { | |
174 Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(), | |
175 graph()->start()); | |
176 graph()->SetEnd(graph()->NewNode(common()->End(), ret)); | |
177 | |
178 Reduction r = Reduce(ret->InputAt(0)); | |
179 | |
180 if (FLAG_turbo_deoptimization) { | |
181 // Check LoadNamed(global) => HeapConstant[const_value] | |
182 ASSERT_TRUE(r.Changed()); | |
183 EXPECT_THAT(r.replacement(), IsHeapConstant(const_value)); | |
184 | |
185 EXPECT_THAT(ret, IsReturn(IsHeapConstant(const_value), graph()->start(), | |
186 graph()->start())); | |
187 EXPECT_THAT(graph()->end(), IsEnd(ret)); | |
188 | |
189 EXPECT_FALSE(dependencies()->IsEmpty()); | |
190 dependencies()->Rollback(); | |
191 } else { | |
192 ASSERT_FALSE(r.Changed()); | |
193 EXPECT_TRUE(dependencies()->IsEmpty()); | |
194 } | |
195 } | |
196 } | |
197 | |
198 | |
199 TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalPropertyCell_smi) { | |
200 const char* property_name = "melon"; | |
201 SetGlobalProperty(property_name, 123); | |
202 SetGlobalProperty(property_name, 124); | |
203 | |
204 WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION { | |
205 Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(), | |
206 graph()->start()); | |
207 graph()->SetEnd(graph()->NewNode(common()->End(), ret)); | |
208 | |
209 Reduction r = Reduce(ret->InputAt(0)); | |
210 | |
211 if (FLAG_turbo_deoptimization) { | |
212 // Check LoadNamed(global) => LoadField[PropertyCell::value](cell) | |
213 ASSERT_TRUE(r.Changed()); | |
214 FieldAccess access = AccessBuilder::ForPropertyCellValue(); | |
215 Capture<Node*> cell_capture; | |
216 Matcher<Node*> load_field_match = IsLoadField( | |
217 access, CaptureEq(&cell_capture), graph()->start(), graph()->start()); | |
218 EXPECT_THAT(r.replacement(), load_field_match); | |
219 | |
220 HeapObjectMatcher<PropertyCell> cell(cell_capture.value()); | |
221 EXPECT_TRUE(cell.HasValue()); | |
222 EXPECT_TRUE(cell.Value().handle()->IsPropertyCell()); | |
223 | |
224 EXPECT_THAT( | |
225 ret, IsReturn(load_field_match, load_field_match, graph()->start())); | |
226 EXPECT_THAT(graph()->end(), IsEnd(ret)); | |
227 | |
228 EXPECT_FALSE(dependencies()->IsEmpty()); | |
229 dependencies()->Rollback(); | |
230 } else { | |
231 ASSERT_FALSE(r.Changed()); | |
232 EXPECT_TRUE(dependencies()->IsEmpty()); | |
233 } | |
234 } | |
235 } | |
236 | |
237 | |
238 TEST_F(JSTypeFeedbackTest, JSLoadNamedGlobalPropertyCell_string) { | |
239 const char* property_name = "pineapple"; | |
240 SetGlobalProperty(property_name, isolate()->factory()->undefined_string()); | |
241 SetGlobalProperty(property_name, isolate()->factory()->undefined_value()); | |
242 | |
243 WITH_AND_WITHOUT_TURBO_DEOPTIMIZATION { | |
244 Node* ret = ReturnLoadNamedFromGlobal(property_name, graph()->start(), | |
245 graph()->start()); | |
246 graph()->SetEnd(graph()->NewNode(common()->End(), ret)); | |
247 | |
248 Reduction r = Reduce(ret->InputAt(0)); | |
249 | |
250 if (FLAG_turbo_deoptimization) { | |
251 // Check LoadNamed(global) => LoadField[PropertyCell::value](cell) | |
252 ASSERT_TRUE(r.Changed()); | |
253 FieldAccess access = AccessBuilder::ForPropertyCellValue(); | |
254 Capture<Node*> cell_capture; | |
255 Matcher<Node*> load_field_match = IsLoadField( | |
256 access, CaptureEq(&cell_capture), graph()->start(), graph()->start()); | |
257 EXPECT_THAT(r.replacement(), load_field_match); | |
258 | |
259 HeapObjectMatcher<PropertyCell> cell(cell_capture.value()); | |
260 EXPECT_TRUE(cell.HasValue()); | |
261 EXPECT_TRUE(cell.Value().handle()->IsPropertyCell()); | |
262 | |
263 EXPECT_THAT( | |
264 ret, IsReturn(load_field_match, load_field_match, graph()->start())); | |
265 EXPECT_THAT(graph()->end(), IsEnd(ret)); | |
266 | |
267 EXPECT_FALSE(dependencies()->IsEmpty()); | |
268 dependencies()->Rollback(); | |
269 } else { | |
270 ASSERT_FALSE(r.Changed()); | |
271 EXPECT_TRUE(dependencies()->IsEmpty()); | |
272 } | |
273 } | |
274 } | |
275 } | |
276 } | |
277 } | |
OLD | NEW |