Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(783)

Side by Side Diff: test/cctest/compiler/test-simplified-lowering.cc

Issue 425003004: Implement representation selection as part of SimplifiedLowering. Representation selection also req… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/compiler/test-changes-lowering.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <limits> 5 #include <limits>
6 6
7 #include "src/compiler/control-builders.h" 7 #include "src/compiler/control-builders.h"
8 #include "src/compiler/generic-node-inl.h" 8 #include "src/compiler/generic-node-inl.h"
9 #include "src/compiler/graph-visualizer.h"
9 #include "src/compiler/node-properties-inl.h" 10 #include "src/compiler/node-properties-inl.h"
10 #include "src/compiler/pipeline.h" 11 #include "src/compiler/pipeline.h"
12 #include "src/compiler/representation-change.h"
11 #include "src/compiler/simplified-lowering.h" 13 #include "src/compiler/simplified-lowering.h"
12 #include "src/compiler/simplified-node-factory.h" 14 #include "src/compiler/simplified-node-factory.h"
13 #include "src/compiler/typer.h" 15 #include "src/compiler/typer.h"
14 #include "src/compiler/verifier.h" 16 #include "src/compiler/verifier.h"
15 #include "src/execution.h" 17 #include "src/execution.h"
16 #include "src/parser.h" 18 #include "src/parser.h"
17 #include "src/rewriter.h" 19 #include "src/rewriter.h"
18 #include "src/scopes.h" 20 #include "src/scopes.h"
19 #include "test/cctest/cctest.h" 21 #include "test/cctest/cctest.h"
20 #include "test/cctest/compiler/codegen-tester.h" 22 #include "test/cctest/compiler/codegen-tester.h"
21 #include "test/cctest/compiler/graph-builder-tester.h" 23 #include "test/cctest/compiler/graph-builder-tester.h"
22 #include "test/cctest/compiler/value-helper.h" 24 #include "test/cctest/compiler/value-helper.h"
23 25
24 using namespace v8::internal; 26 using namespace v8::internal;
25 using namespace v8::internal::compiler; 27 using namespace v8::internal::compiler;
26 28
27 // TODO(titzer): rename this to VMLoweringTester
28 template <typename ReturnType> 29 template <typename ReturnType>
29 class SimplifiedGraphBuilderTester : public GraphBuilderTester<ReturnType> { 30 class SimplifiedLoweringTester : public GraphBuilderTester<ReturnType> {
30 public: 31 public:
31 SimplifiedGraphBuilderTester(MachineRepresentation p0 = kMachineLast, 32 SimplifiedLoweringTester(MachineRepresentation p0 = kMachineLast,
32 MachineRepresentation p1 = kMachineLast, 33 MachineRepresentation p1 = kMachineLast,
33 MachineRepresentation p2 = kMachineLast, 34 MachineRepresentation p2 = kMachineLast,
34 MachineRepresentation p3 = kMachineLast, 35 MachineRepresentation p3 = kMachineLast,
35 MachineRepresentation p4 = kMachineLast) 36 MachineRepresentation p4 = kMachineLast)
36 : GraphBuilderTester<ReturnType>(p0, p1, p2, p3, p4), 37 : GraphBuilderTester<ReturnType>(p0, p1, p2, p3, p4),
37 typer(this->zone()), 38 typer(this->zone()),
38 source_positions(this->graph()), 39 source_positions(this->graph()),
39 jsgraph(this->graph(), this->common(), &typer), 40 jsgraph(this->graph(), this->common(), &typer),
40 lowering(&jsgraph, &source_positions) {} 41 lowering(&jsgraph, &source_positions) {}
41 42
42 Typer typer; 43 Typer typer;
43 SourcePositionTable source_positions; 44 SourcePositionTable source_positions;
44 JSGraph jsgraph; 45 JSGraph jsgraph;
45 SimplifiedLowering lowering; 46 SimplifiedLowering lowering;
46 47
47 // Close graph and lower one node. 48 void LowerAllNodes() {
48 void Lower(Node* node) {
49 this->End(); 49 this->End();
50 if (node == NULL) { 50 lowering.LowerAllNodes();
51 lowering.LowerAllNodes();
52 } else {
53 lowering.Lower(node);
54 }
55 }
56
57 // Close graph and lower all nodes.
58 void LowerAllNodes() { Lower(NULL); }
59
60 void StoreFloat64(Node* node, double* ptr) {
61 Node* ptr_node = this->PointerConstant(ptr);
62 this->Store(kMachineFloat64, ptr_node, node);
63 }
64
65 Node* LoadInt32(int32_t* ptr) {
66 Node* ptr_node = this->PointerConstant(ptr);
67 return this->Load(kMachineWord32, ptr_node);
68 }
69
70 Node* LoadUint32(uint32_t* ptr) {
71 Node* ptr_node = this->PointerConstant(ptr);
72 return this->Load(kMachineWord32, ptr_node);
73 }
74
75 Node* LoadFloat64(double* ptr) {
76 Node* ptr_node = this->PointerConstant(ptr);
77 return this->Load(kMachineFloat64, ptr_node);
78 } 51 }
79 52
80 Factory* factory() { return this->isolate()->factory(); } 53 Factory* factory() { return this->isolate()->factory(); }
81 Heap* heap() { return this->isolate()->heap(); } 54 Heap* heap() { return this->isolate()->heap(); }
82 }; 55 };
83 56
84 57
85 // TODO(dcarney): find a home for these functions. 58 // TODO(dcarney): find a home for these functions.
86 namespace { 59 namespace {
87 60
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 // Create a simple JSObject with a unique map. 101 // Create a simple JSObject with a unique map.
129 static Handle<JSObject> TestObject() { 102 static Handle<JSObject> TestObject() {
130 static int index = 0; 103 static int index = 0;
131 char buffer[50]; 104 char buffer[50];
132 v8::base::OS::SNPrintF(buffer, 50, "({'a_%d':1})", index++); 105 v8::base::OS::SNPrintF(buffer, 50, "({'a_%d':1})", index++);
133 return Handle<JSObject>::cast(v8::Utils::OpenHandle(*CompileRun(buffer))); 106 return Handle<JSObject>::cast(v8::Utils::OpenHandle(*CompileRun(buffer)));
134 } 107 }
135 108
136 109
137 TEST(RunLoadMap) { 110 TEST(RunLoadMap) {
138 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged); 111 SimplifiedLoweringTester<Object*> t(kMachineTagged);
139 FieldAccess access = ForJSObjectMap(); 112 FieldAccess access = ForJSObjectMap();
140 Node* load = t.LoadField(access, t.Parameter(0)); 113 Node* load = t.LoadField(access, t.Parameter(0));
141 t.Return(load); 114 t.Return(load);
142 115
143 t.LowerAllNodes(); 116 t.LowerAllNodes();
144 117
145 if (!Pipeline::SupportedTarget()) return; 118 if (Pipeline::SupportedTarget()) {
146 119 t.GenerateCode();
147 Handle<JSObject> src = TestObject(); 120 Handle<JSObject> src = TestObject();
148 Handle<Map> src_map(src->map()); 121 Handle<Map> src_map(src->map());
149 Object* result = t.Call(*src); 122 Object* result = t.Call(*src); // TODO(titzer): raw pointers in call
150 CHECK_EQ(*src_map, result); 123 CHECK_EQ(*src_map, result);
124 }
151 } 125 }
152 126
153 127
154 TEST(RunStoreMap) { 128 TEST(RunStoreMap) {
155 SimplifiedGraphBuilderTester<int32_t> t(kMachineTagged, kMachineTagged); 129 SimplifiedLoweringTester<int32_t> t(kMachineTagged, kMachineTagged);
156 FieldAccess access = ForJSObjectMap(); 130 FieldAccess access = ForJSObjectMap();
157 t.StoreField(access, t.Parameter(1), t.Parameter(0)); 131 t.StoreField(access, t.Parameter(1), t.Parameter(0));
158 t.Return(t.Int32Constant(0)); 132 t.Return(t.jsgraph.TrueConstant());
159 133
160 t.LowerAllNodes(); 134 t.LowerAllNodes();
161 135
162 if (!Pipeline::SupportedTarget()) return; 136 if (Pipeline::SupportedTarget()) {
163 137 t.GenerateCode();
164 Handle<JSObject> src = TestObject(); 138 Handle<JSObject> src = TestObject();
165 Handle<Map> src_map(src->map()); 139 Handle<Map> src_map(src->map());
166 Handle<JSObject> dst = TestObject(); 140 Handle<JSObject> dst = TestObject();
167 CHECK(src->map() != dst->map()); 141 CHECK(src->map() != dst->map());
168 t.Call(*src_map, *dst); 142 t.Call(*src_map, *dst); // TODO(titzer): raw pointers in call
169 CHECK(*src_map == dst->map()); 143 CHECK(*src_map == dst->map());
144 }
170 } 145 }
171 146
172 147
173 TEST(RunLoadProperties) { 148 TEST(RunLoadProperties) {
174 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged); 149 SimplifiedLoweringTester<Object*> t(kMachineTagged);
175 FieldAccess access = ForJSObjectProperties(); 150 FieldAccess access = ForJSObjectProperties();
176 Node* load = t.LoadField(access, t.Parameter(0)); 151 Node* load = t.LoadField(access, t.Parameter(0));
177 t.Return(load); 152 t.Return(load);
178 153
179 t.LowerAllNodes(); 154 t.LowerAllNodes();
180 155
181 if (!Pipeline::SupportedTarget()) return; 156 if (Pipeline::SupportedTarget()) {
182 157 t.GenerateCode();
183 Handle<JSObject> src = TestObject(); 158 Handle<JSObject> src = TestObject();
184 Handle<FixedArray> src_props(src->properties()); 159 Handle<FixedArray> src_props(src->properties());
185 Object* result = t.Call(*src); 160 Object* result = t.Call(*src); // TODO(titzer): raw pointers in call
186 CHECK_EQ(*src_props, result); 161 CHECK_EQ(*src_props, result);
162 }
187 } 163 }
188 164
189 165
190 TEST(RunLoadStoreMap) { 166 TEST(RunLoadStoreMap) {
191 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged, kMachineTagged); 167 SimplifiedLoweringTester<Object*> t(kMachineTagged, kMachineTagged);
192 FieldAccess access = ForJSObjectMap(); 168 FieldAccess access = ForJSObjectMap();
193 Node* load = t.LoadField(access, t.Parameter(0)); 169 Node* load = t.LoadField(access, t.Parameter(0));
194 t.StoreField(access, t.Parameter(1), load); 170 t.StoreField(access, t.Parameter(1), load);
195 t.Return(load); 171 t.Return(load);
196 172
197 t.LowerAllNodes(); 173 t.LowerAllNodes();
198 174
199 if (!Pipeline::SupportedTarget()) return; 175 if (Pipeline::SupportedTarget()) {
200 176 t.GenerateCode();
201 Handle<JSObject> src = TestObject(); 177 Handle<JSObject> src = TestObject();
202 Handle<Map> src_map(src->map()); 178 Handle<Map> src_map(src->map());
203 Handle<JSObject> dst = TestObject(); 179 Handle<JSObject> dst = TestObject();
204 CHECK(src->map() != dst->map()); 180 CHECK(src->map() != dst->map());
205 Object* result = t.Call(*src, *dst); 181 Object* result = t.Call(*src, *dst); // TODO(titzer): raw pointers in call
206 CHECK(result->IsMap()); 182 CHECK(result->IsMap());
207 CHECK_EQ(*src_map, result); 183 CHECK_EQ(*src_map, result);
208 CHECK(*src_map == dst->map()); 184 CHECK(*src_map == dst->map());
185 }
209 } 186 }
210 187
211 188
212 TEST(RunLoadStoreFixedArrayIndex) { 189 TEST(RunLoadStoreFixedArrayIndex) {
213 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged); 190 SimplifiedLoweringTester<Object*> t(kMachineTagged);
214 ElementAccess access = ForFixedArrayElement(); 191 ElementAccess access = ForFixedArrayElement();
215 Node* load = t.LoadElement(access, t.Parameter(0), t.Int32Constant(0)); 192 Node* load = t.LoadElement(access, t.Parameter(0), t.Int32Constant(0));
216 t.StoreElement(access, t.Parameter(0), t.Int32Constant(1), load); 193 t.StoreElement(access, t.Parameter(0), t.Int32Constant(1), load);
217 t.Return(load); 194 t.Return(load);
218 195
219 t.LowerAllNodes(); 196 t.LowerAllNodes();
220 197
221 if (!Pipeline::SupportedTarget()) return; 198 if (Pipeline::SupportedTarget()) {
222 199 t.GenerateCode();
223 Handle<FixedArray> array = t.factory()->NewFixedArray(2); 200 Handle<FixedArray> array = t.factory()->NewFixedArray(2);
224 Handle<JSObject> src = TestObject(); 201 Handle<JSObject> src = TestObject();
225 Handle<JSObject> dst = TestObject(); 202 Handle<JSObject> dst = TestObject();
226 array->set(0, *src); 203 array->set(0, *src);
227 array->set(1, *dst); 204 array->set(1, *dst);
228 Object* result = t.Call(*array); 205 Object* result = t.Call(*array);
229 CHECK_EQ(*src, result); 206 CHECK_EQ(*src, result);
230 CHECK_EQ(*src, array->get(0)); 207 CHECK_EQ(*src, array->get(0));
231 CHECK_EQ(*src, array->get(1)); 208 CHECK_EQ(*src, array->get(1));
209 }
232 } 210 }
233 211
234 212
235 TEST(RunLoadStoreArrayBuffer) { 213 TEST(RunLoadStoreArrayBuffer) {
236 SimplifiedGraphBuilderTester<int32_t> t(kMachineTagged); 214 SimplifiedLoweringTester<Object*> t(kMachineTagged);
237 const int index = 12; 215 const int index = 12;
238 FieldAccess access = ForArrayBufferBackingStore();
239 Node* backing_store = t.LoadField(access, t.Parameter(0));
240 ElementAccess buffer_access = ForBackingStoreElement(kMachineWord8); 216 ElementAccess buffer_access = ForBackingStoreElement(kMachineWord8);
217 Node* backing_store =
218 t.LoadField(ForArrayBufferBackingStore(), t.Parameter(0));
241 Node* load = 219 Node* load =
242 t.LoadElement(buffer_access, backing_store, t.Int32Constant(index)); 220 t.LoadElement(buffer_access, backing_store, t.Int32Constant(index));
243 t.StoreElement(buffer_access, backing_store, t.Int32Constant(index + 1), 221 t.StoreElement(buffer_access, backing_store, t.Int32Constant(index + 1),
244 load); 222 load);
245 t.Return(load); 223 t.Return(t.jsgraph.TrueConstant());
246 224
247 t.LowerAllNodes(); 225 t.LowerAllNodes();
248 226
249 if (!Pipeline::SupportedTarget()) return; 227 if (Pipeline::SupportedTarget()) {
228 t.GenerateCode();
229 Handle<JSArrayBuffer> array = t.factory()->NewJSArrayBuffer();
230 const int array_length = 2 * index;
231 Runtime::SetupArrayBufferAllocatingData(t.isolate(), array, array_length);
232 uint8_t* data = reinterpret_cast<uint8_t*>(array->backing_store());
233 for (int i = 0; i < array_length; i++) {
234 data[i] = i;
235 }
250 236
251 Handle<JSArrayBuffer> array = t.factory()->NewJSArrayBuffer(); 237 // TODO(titzer): raw pointers in call
252 const int array_length = 2 * index; 238 Object* result = t.Call(*array);
253 Runtime::SetupArrayBufferAllocatingData(t.isolate(), array, array_length); 239 CHECK_EQ(t.isolate()->heap()->true_value(), result);
254 uint8_t* data = reinterpret_cast<uint8_t*>(array->backing_store()); 240 for (int i = 0; i < array_length; i++) {
255 for (int i = 0; i < array_length; i++) { 241 uint8_t expected = i;
256 data[i] = i; 242 if (i == (index + 1)) expected = index;
257 } 243 CHECK_EQ(data[i], expected);
258 int32_t result = t.Call(*array); 244 }
259 CHECK_EQ(index, result);
260 for (int i = 0; i < array_length; i++) {
261 uint8_t expected = i;
262 if (i == (index + 1)) expected = result;
263 CHECK_EQ(data[i], expected);
264 } 245 }
265 } 246 }
266 247
267
268 TEST(RunCopyFixedArray) {
269 SimplifiedGraphBuilderTester<int32_t> t(kMachineTagged, kMachineTagged);
270
271 const int kArraySize = 15;
272 Node* one = t.Int32Constant(1);
273 Node* index = t.Int32Constant(0);
274 Node* limit = t.Int32Constant(kArraySize);
275 t.environment()->Push(index);
276 {
277 LoopBuilder loop(&t);
278 loop.BeginLoop();
279 // Loop exit condition.
280 index = t.environment()->Top();
281 Node* condition = t.Int32LessThan(index, limit);
282 loop.BreakUnless(condition);
283 // src[index] = dst[index].
284 index = t.environment()->Pop();
285 ElementAccess access = ForFixedArrayElement();
286 Node* src = t.Parameter(0);
287 Node* load = t.LoadElement(access, src, index);
288 Node* dst = t.Parameter(1);
289 t.StoreElement(access, dst, index, load);
290 // index++
291 index = t.Int32Add(index, one);
292 t.environment()->Push(index);
293 // continue.
294 loop.EndBody();
295 loop.EndLoop();
296 }
297 index = t.environment()->Pop();
298 t.Return(index);
299
300 t.LowerAllNodes();
301
302 if (!Pipeline::SupportedTarget()) return;
303
304 Handle<FixedArray> src = t.factory()->NewFixedArray(kArraySize);
305 Handle<FixedArray> src_copy = t.factory()->NewFixedArray(kArraySize);
306 Handle<FixedArray> dst = t.factory()->NewFixedArray(kArraySize);
307 for (int i = 0; i < kArraySize; i++) {
308 src->set(i, *TestObject());
309 src_copy->set(i, src->get(i));
310 dst->set(i, *TestObject());
311 CHECK_NE(src_copy->get(i), dst->get(i));
312 }
313 CHECK_EQ(kArraySize, t.Call(*src, *dst));
314 for (int i = 0; i < kArraySize; i++) {
315 CHECK_EQ(src_copy->get(i), dst->get(i));
316 }
317 }
318
319 248
320 TEST(RunLoadFieldFromUntaggedBase) { 249 TEST(RunLoadFieldFromUntaggedBase) {
321 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)}; 250 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)};
322 251
323 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { 252 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) {
324 int offset = static_cast<int>(i * sizeof(Smi*)); 253 int offset = static_cast<int>(i * sizeof(Smi*));
325 FieldAccess access = {kUntaggedBase, offset, Handle<Name>(), 254 FieldAccess access = {kUntaggedBase, offset, Handle<Name>(),
326 Type::Integral32(), kMachineTagged}; 255 Type::Integral32(), kMachineTagged};
327 256
328 SimplifiedGraphBuilderTester<Object*> t; 257 SimplifiedLoweringTester<Object*> t;
329 Node* load = t.LoadField(access, t.PointerConstant(smis)); 258 Node* load = t.LoadField(access, t.PointerConstant(smis));
330 t.Return(load); 259 t.Return(load);
331 t.LowerAllNodes(); 260 t.LowerAllNodes();
332 261
333 if (!Pipeline::SupportedTarget()) continue; 262 if (!Pipeline::SupportedTarget()) continue;
334 263
335 for (int j = -5; j <= 5; j++) { 264 for (int j = -5; j <= 5; j++) {
336 Smi* expected = Smi::FromInt(j); 265 Smi* expected = Smi::FromInt(j);
337 smis[i] = expected; 266 smis[i] = expected;
338 CHECK_EQ(expected, t.Call()); 267 CHECK_EQ(expected, t.Call());
339 } 268 }
340 } 269 }
341 } 270 }
342 271
343 272
344 TEST(RunStoreFieldToUntaggedBase) { 273 TEST(RunStoreFieldToUntaggedBase) {
345 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)}; 274 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)};
346 275
347 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { 276 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) {
348 int offset = static_cast<int>(i * sizeof(Smi*)); 277 int offset = static_cast<int>(i * sizeof(Smi*));
349 FieldAccess access = {kUntaggedBase, offset, Handle<Name>(), 278 FieldAccess access = {kUntaggedBase, offset, Handle<Name>(),
350 Type::Integral32(), kMachineTagged}; 279 Type::Integral32(), kMachineTagged};
351 280
352 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged); 281 SimplifiedLoweringTester<Object*> t(kMachineTagged);
353 Node* p0 = t.Parameter(0); 282 Node* p0 = t.Parameter(0);
354 t.StoreField(access, t.PointerConstant(smis), p0); 283 t.StoreField(access, t.PointerConstant(smis), p0);
355 t.Return(p0); 284 t.Return(p0);
356 t.LowerAllNodes(); 285 t.LowerAllNodes();
357 286
358 if (!Pipeline::SupportedTarget()) continue; 287 if (!Pipeline::SupportedTarget()) continue;
359 288
360 for (int j = -5; j <= 5; j++) { 289 for (int j = -5; j <= 5; j++) {
361 Smi* expected = Smi::FromInt(j); 290 Smi* expected = Smi::FromInt(j);
362 smis[i] = Smi::FromInt(-100); 291 smis[i] = Smi::FromInt(-100);
363 CHECK_EQ(expected, t.Call(expected)); 292 CHECK_EQ(expected, t.Call(expected));
364 CHECK_EQ(expected, smis[i]); 293 CHECK_EQ(expected, smis[i]);
365 } 294 }
366 } 295 }
367 } 296 }
368 297
369 298
370 TEST(RunLoadElementFromUntaggedBase) { 299 TEST(RunLoadElementFromUntaggedBase) {
371 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3), 300 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3),
372 Smi::FromInt(4), Smi::FromInt(5)}; 301 Smi::FromInt(4), Smi::FromInt(5)};
373 302
374 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { // for header sizes 303 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { // for header sizes
375 for (size_t j = 0; (i + j) < ARRAY_SIZE(smis); j++) { // for element index 304 for (size_t j = 0; (i + j) < ARRAY_SIZE(smis); j++) { // for element index
376 int offset = static_cast<int>(i * sizeof(Smi*)); 305 int offset = static_cast<int>(i * sizeof(Smi*));
377 ElementAccess access = {kUntaggedBase, offset, Type::Integral32(), 306 ElementAccess access = {kUntaggedBase, offset, Type::Integral32(),
378 kMachineTagged}; 307 kMachineTagged};
379 308
380 SimplifiedGraphBuilderTester<Object*> t; 309 SimplifiedLoweringTester<Object*> t;
381 Node* load = t.LoadElement(access, t.PointerConstant(smis), 310 Node* load = t.LoadElement(access, t.PointerConstant(smis),
382 t.Int32Constant(static_cast<int>(j))); 311 t.Int32Constant(static_cast<int>(j)));
383 t.Return(load); 312 t.Return(load);
384 t.LowerAllNodes(); 313 t.LowerAllNodes();
385 314
386 if (!Pipeline::SupportedTarget()) continue; 315 if (!Pipeline::SupportedTarget()) continue;
387 316
388 for (int k = -5; k <= 5; k++) { 317 for (int k = -5; k <= 5; k++) {
389 Smi* expected = Smi::FromInt(k); 318 Smi* expected = Smi::FromInt(k);
390 smis[i + j] = expected; 319 smis[i + j] = expected;
391 CHECK_EQ(expected, t.Call()); 320 CHECK_EQ(expected, t.Call());
392 } 321 }
393 } 322 }
394 } 323 }
395 } 324 }
396 325
397 326
398 TEST(RunStoreElementFromUntaggedBase) { 327 TEST(RunStoreElementFromUntaggedBase) {
399 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3), 328 Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3),
400 Smi::FromInt(4), Smi::FromInt(5)}; 329 Smi::FromInt(4), Smi::FromInt(5)};
401 330
402 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { // for header sizes 331 for (size_t i = 0; i < ARRAY_SIZE(smis); i++) { // for header sizes
403 for (size_t j = 0; (i + j) < ARRAY_SIZE(smis); j++) { // for element index 332 for (size_t j = 0; (i + j) < ARRAY_SIZE(smis); j++) { // for element index
404 int offset = static_cast<int>(i * sizeof(Smi*)); 333 int offset = static_cast<int>(i * sizeof(Smi*));
405 ElementAccess access = {kUntaggedBase, offset, Type::Integral32(), 334 ElementAccess access = {kUntaggedBase, offset, Type::Integral32(),
406 kMachineTagged}; 335 kMachineTagged};
407 336
408 SimplifiedGraphBuilderTester<Object*> t(kMachineTagged); 337 SimplifiedLoweringTester<Object*> t(kMachineTagged);
409 Node* p0 = t.Parameter(0); 338 Node* p0 = t.Parameter(0);
410 t.StoreElement(access, t.PointerConstant(smis), 339 t.StoreElement(access, t.PointerConstant(smis),
411 t.Int32Constant(static_cast<int>(j)), p0); 340 t.Int32Constant(static_cast<int>(j)), p0);
412 t.Return(p0); 341 t.Return(p0);
413 t.LowerAllNodes(); 342 t.LowerAllNodes();
414 343
415 if (!Pipeline::SupportedTarget()) continue; 344 if (!Pipeline::SupportedTarget()) continue;
416 345
417 for (int k = -5; k <= 5; k++) { 346 for (int k = -5; k <= 5; k++) {
418 Smi* expected = Smi::FromInt(k); 347 Smi* expected = Smi::FromInt(k);
419 smis[i + j] = Smi::FromInt(-100); 348 smis[i + j] = Smi::FromInt(-100);
420 CHECK_EQ(expected, t.Call(expected)); 349 CHECK_EQ(expected, t.Call(expected));
421 CHECK_EQ(expected, smis[i + j]); 350 CHECK_EQ(expected, smis[i + j]);
422 } 351 }
423 352
424 // TODO(titzer): assert the contents of the array. 353 // TODO(titzer): assert the contents of the array.
425 } 354 }
426 } 355 }
427 } 356 }
357
358
359 // A helper class for accessing fields and elements of various types, on both
360 // tagged and untagged base pointers. Contains both tagged and untagged buffers
361 // for testing direct memory access from generated code.
362 template <typename E>
363 class AccessTester : public HandleAndZoneScope {
364 public:
365 bool tagged;
366 MachineRepresentation rep;
367 E* original_elements;
368 size_t num_elements;
369 E* untagged_array;
370 Handle<ByteArray> tagged_array; // TODO(titzer): use FixedArray for tagged.
371
372 AccessTester(bool t, MachineRepresentation r, E* orig, size_t num)
373 : tagged(t),
374 rep(r),
375 original_elements(orig),
376 num_elements(num),
377 untagged_array(static_cast<E*>(malloc(ByteSize()))),
378 tagged_array(main_isolate()->factory()->NewByteArray(ByteSize())) {
379 Reinitialize();
380 }
381
382 ~AccessTester() { free(untagged_array); }
383
384 size_t ByteSize() { return num_elements * sizeof(E); }
385
386 // Nuke both {untagged_array} and {tagged_array} with {original_elements}.
387 void Reinitialize() {
388 memcpy(untagged_array, original_elements, ByteSize());
389 CHECK_EQ(ByteSize(), tagged_array->length());
390 E* raw = reinterpret_cast<E*>(tagged_array->GetDataStartAddress());
391 memcpy(raw, original_elements, ByteSize());
392 }
393
394 // Create and run code that copies the element in either {untagged_array}
395 // or {tagged_array} at index {from_index} to index {to_index}.
396 void RunCopyElement(int from_index, int to_index) {
397 // TODO(titzer): test element and field accesses where the base is not
398 // a constant in the code.
399 BoundsCheck(from_index);
400 BoundsCheck(to_index);
401 ElementAccess access = GetElementAccess();
402
403 SimplifiedLoweringTester<Object*> t;
404 Node* ptr = GetBaseNode(&t);
405 Node* load = t.LoadElement(access, ptr, t.Int32Constant(from_index));
406 t.StoreElement(access, ptr, t.Int32Constant(to_index), load);
407 t.Return(t.jsgraph.TrueConstant());
408 t.LowerAllNodes();
409 t.GenerateCode();
410
411 if (Pipeline::SupportedTarget()) {
412 Object* result = t.Call();
413 CHECK_EQ(t.isolate()->heap()->true_value(), result);
414 }
415 }
416
417 // Create and run code that copies the field in either {untagged_array}
418 // or {tagged_array} at index {from_index} to index {to_index}.
419 void RunCopyField(int from_index, int to_index) {
420 BoundsCheck(from_index);
421 BoundsCheck(to_index);
422 FieldAccess from_access = GetFieldAccess(from_index);
423 FieldAccess to_access = GetFieldAccess(to_index);
424
425 SimplifiedLoweringTester<Object*> t;
426 Node* ptr = GetBaseNode(&t);
427 Node* load = t.LoadField(from_access, ptr);
428 t.StoreField(to_access, ptr, load);
429 t.Return(t.jsgraph.TrueConstant());
430 t.LowerAllNodes();
431 t.GenerateCode();
432
433 if (Pipeline::SupportedTarget()) {
434 Object* result = t.Call();
435 CHECK_EQ(t.isolate()->heap()->true_value(), result);
436 }
437 }
438
439 // Create and run code that copies the elements from {this} to {that}.
440 void RunCopyElements(AccessTester<E>* that) {
441 SimplifiedLoweringTester<Object*> t;
442
443 Node* one = t.Int32Constant(1);
444 Node* index = t.Int32Constant(0);
445 Node* limit = t.Int32Constant(static_cast<int>(num_elements));
446 t.environment()->Push(index);
447 Node* src = this->GetBaseNode(&t);
448 Node* dst = that->GetBaseNode(&t);
449 {
450 LoopBuilder loop(&t);
451 loop.BeginLoop();
452 // Loop exit condition
453 index = t.environment()->Top();
454 Node* condition = t.Int32LessThan(index, limit);
455 loop.BreakUnless(condition);
456 // dst[index] = src[index]
457 index = t.environment()->Pop();
458 Node* load = t.LoadElement(this->GetElementAccess(), src, index);
459 t.StoreElement(that->GetElementAccess(), dst, index, load);
460 // index++
461 index = t.Int32Add(index, one);
462 t.environment()->Push(index);
463 // continue
464 loop.EndBody();
465 loop.EndLoop();
466 }
467 index = t.environment()->Pop();
468 t.Return(t.jsgraph.TrueConstant());
469 t.LowerAllNodes();
470 t.GenerateCode();
471
472 if (Pipeline::SupportedTarget()) {
473 Object* result = t.Call();
474 CHECK_EQ(t.isolate()->heap()->true_value(), result);
475 }
476 }
477
478 E GetElement(int index) {
479 BoundsCheck(index);
480 if (tagged) {
481 E* raw = reinterpret_cast<E*>(tagged_array->GetDataStartAddress());
482 return raw[index];
483 } else {
484 return untagged_array[index];
485 }
486 }
487
488 private:
489 ElementAccess GetElementAccess() {
490 ElementAccess access = {tagged ? kTaggedBase : kUntaggedBase,
491 tagged ? FixedArrayBase::kHeaderSize : 0,
492 Type::Any(), rep};
493 return access;
494 }
495
496 FieldAccess GetFieldAccess(int field) {
497 int offset = field * sizeof(E);
498 FieldAccess access = {tagged ? kTaggedBase : kUntaggedBase,
499 offset + (tagged ? FixedArrayBase::kHeaderSize : 0),
500 Handle<Name>(), Type::Any(), rep};
501 return access;
502 }
503
504 template <typename T>
505 Node* GetBaseNode(SimplifiedLoweringTester<T>* t) {
506 return tagged ? t->HeapConstant(tagged_array)
507 : t->PointerConstant(untagged_array);
508 }
509
510 void BoundsCheck(int index) {
511 CHECK_GE(index, 0);
512 CHECK_LT(index, static_cast<int>(num_elements));
513 CHECK_EQ(ByteSize(), tagged_array->length());
514 }
515 };
516
517
518 template <typename E>
519 static void RunAccessTest(MachineRepresentation rep, E* original_elements,
520 size_t num) {
521 int num_elements = static_cast<int>(num);
522
523 for (int taggedness = 0; taggedness < 2; taggedness++) {
524 AccessTester<E> a(taggedness == 1, rep, original_elements, num);
525 for (int field = 0; field < 2; field++) {
526 for (int i = 0; i < num_elements - 1; i++) {
527 a.Reinitialize();
528 if (field == 0) {
529 a.RunCopyField(i, i + 1); // Test field read/write.
530 } else {
531 a.RunCopyElement(i, i + 1); // Test element read/write.
532 }
533 if (Pipeline::SupportedTarget()) { // verify.
534 for (int j = 0; j < num_elements; j++) {
535 E expect =
536 j == (i + 1) ? original_elements[i] : original_elements[j];
537 CHECK_EQ(expect, a.GetElement(j));
538 }
539 }
540 }
541 }
542 }
543 // Test array copy.
544 for (int tf = 0; tf < 2; tf++) {
545 for (int tt = 0; tt < 2; tt++) {
546 AccessTester<E> a(tf == 1, rep, original_elements, num);
547 AccessTester<E> b(tt == 1, rep, original_elements, num);
548 a.RunCopyElements(&b);
549 if (Pipeline::SupportedTarget()) { // verify.
550 for (int i = 0; i < num_elements; i++) {
551 CHECK_EQ(a.GetElement(i), b.GetElement(i));
552 }
553 }
554 }
555 }
556 }
557
558
559 TEST(RunAccessTests_uint8) {
560 uint8_t data[] = {0x07, 0x16, 0x25, 0x34, 0x43, 0x99,
561 0xab, 0x78, 0x89, 0x19, 0x2b, 0x38};
562 RunAccessTest<uint8_t>(kMachineWord8, data, ARRAY_SIZE(data));
563 }
564
565
566 TEST(RunAccessTests_uint16) {
567 uint16_t data[] = {0x071a, 0x162b, 0x253c, 0x344d, 0x435e, 0x7777};
568 RunAccessTest<uint16_t>(kMachineWord16, data, ARRAY_SIZE(data));
569 }
570
571
572 TEST(RunAccessTests_int32) {
573 int32_t data[] = {-211, 211, 628347, 2000000000, -2000000000, -1, -100000034};
574 RunAccessTest<int32_t>(kMachineWord32, data, ARRAY_SIZE(data));
575 }
576
577
578 #define V8_2PART_INT64(a, b) (((static_cast<int64_t>(a) << 32) + 0x##b##u))
579
580
581 TEST(RunAccessTests_int64) {
582 if (kPointerSize != 8) return;
583 int64_t data[] = {V8_2PART_INT64(0x10111213, 14151617),
584 V8_2PART_INT64(0x20212223, 24252627),
585 V8_2PART_INT64(0x30313233, 34353637),
586 V8_2PART_INT64(0xa0a1a2a3, a4a5a6a7),
587 V8_2PART_INT64(0xf0f1f2f3, f4f5f6f7)};
588 RunAccessTest<int64_t>(kMachineWord64, data, ARRAY_SIZE(data));
589 }
590
591
592 TEST(RunAccessTests_float64) {
593 double data[] = {1.25, -1.25, 2.75, 11.0, 11100.8};
594 RunAccessTest<double>(kMachineFloat64, data, ARRAY_SIZE(data));
595 }
596
597
598 TEST(RunAccessTests_Smi) {
599 Smi* data[] = {Smi::FromInt(-1), Smi::FromInt(-9),
600 Smi::FromInt(0), Smi::FromInt(666),
601 Smi::FromInt(77777), Smi::FromInt(Smi::kMaxValue)};
602 RunAccessTest<Smi*>(kMachineTagged, data, ARRAY_SIZE(data));
603 }
604
605
606 // Fills in most of the nodes of the graph in order to make tests shorter.
607 class TestingGraph : public HandleAndZoneScope, public GraphAndBuilders {
608 public:
609 Typer typer;
610 JSGraph jsgraph;
611 Node* p0;
612 Node* p1;
613 Node* start;
614 Node* end;
615 Node* ret;
616
617 TestingGraph(Type* p0_type, Type* p1_type = Type::None())
618 : GraphAndBuilders(main_zone()),
619 typer(main_zone()),
620 jsgraph(graph(), common(), &typer) {
621 start = graph()->NewNode(common()->Start(2));
622 graph()->SetStart(start);
623 ret =
624 graph()->NewNode(common()->Return(), jsgraph.Constant(0), start, start);
625 end = graph()->NewNode(common()->End(), ret);
626 graph()->SetEnd(end);
627 p0 = graph()->NewNode(common()->Parameter(0), start);
628 p1 = graph()->NewNode(common()->Parameter(1), start);
629 NodeProperties::SetBounds(p0, Bounds(p0_type));
630 NodeProperties::SetBounds(p1, Bounds(p1_type));
631 }
632
633 void CheckLoweringBinop(IrOpcode::Value expected, Operator* op) {
634 Node* node = Return(graph()->NewNode(op, p0, p1));
635 Lower();
636 CHECK_EQ(expected, node->opcode());
637 }
638
639 void CheckLoweringTruncatedBinop(IrOpcode::Value expected, Operator* op,
640 Operator* trunc) {
641 Node* node = graph()->NewNode(op, p0, p1);
642 Return(graph()->NewNode(trunc, node));
643 Lower();
644 CHECK_EQ(expected, node->opcode());
645 }
646
647 void Lower() {
648 SimplifiedLowering lowering(&jsgraph, NULL);
649 lowering.LowerAllNodes();
650 }
651
652 // Inserts the node as the return value of the graph.
653 Node* Return(Node* node) {
654 ret->ReplaceInput(0, node);
655 return node;
656 }
657
658 // Inserts the node as the effect input to the return of the graph.
659 void Effect(Node* node) { ret->ReplaceInput(1, node); }
660
661 Node* ExampleWithOutput(RepType type) {
662 // TODO(titzer): use parameters with guaranteed representations.
663 if (type & tInt32) {
664 return graph()->NewNode(machine()->Int32Add(), jsgraph.Int32Constant(1),
665 jsgraph.Int32Constant(1));
666 } else if (type & tUint32) {
667 return graph()->NewNode(machine()->Word32Shr(), jsgraph.Int32Constant(1),
668 jsgraph.Int32Constant(1));
669 } else if (type & rFloat64) {
670 return graph()->NewNode(machine()->Float64Add(),
671 jsgraph.Float64Constant(1),
672 jsgraph.Float64Constant(1));
673 } else if (type & rBit) {
674 return graph()->NewNode(machine()->Word32Equal(),
675 jsgraph.Int32Constant(1),
676 jsgraph.Int32Constant(1));
677 } else if (type & rWord64) {
678 return graph()->NewNode(machine()->Int64Add(), Int64Constant(1),
679 Int64Constant(1));
680 } else {
681 CHECK(type & rTagged);
682 return p0;
683 }
684 }
685
686 Node* Use(Node* node, RepType type) {
687 if (type & tInt32) {
688 return graph()->NewNode(machine()->Int32LessThan(), node,
689 jsgraph.Int32Constant(1));
690 } else if (type & tUint32) {
691 return graph()->NewNode(machine()->Uint32LessThan(), node,
692 jsgraph.Int32Constant(1));
693 } else if (type & rFloat64) {
694 return graph()->NewNode(machine()->Float64Add(), node,
695 jsgraph.Float64Constant(1));
696 } else if (type & rWord64) {
697 return graph()->NewNode(machine()->Int64LessThan(), node,
698 Int64Constant(1));
699 } else {
700 return graph()->NewNode(simplified()->ReferenceEqual(Type::Any()), node,
701 jsgraph.TrueConstant());
702 }
703 }
704
705 Node* Branch(Node* cond) {
706 Node* br = graph()->NewNode(common()->Branch(), cond, start);
707 Node* tb = graph()->NewNode(common()->IfTrue(), br);
708 Node* fb = graph()->NewNode(common()->IfFalse(), br);
709 Node* m = graph()->NewNode(common()->Merge(2), tb, fb);
710 ret->ReplaceInput(NodeProperties::FirstControlIndex(ret), m);
711 return br;
712 }
713
714 Node* Int64Constant(int64_t v) {
715 return graph()->NewNode(common()->Int64Constant(v));
716 }
717
718 SimplifiedOperatorBuilder* simplified() { return &main_simplified_; }
719 MachineOperatorBuilder* machine() { return &main_machine_; }
720 CommonOperatorBuilder* common() { return &main_common_; }
721 Graph* graph() { return main_graph_; }
722 };
723
724
725 TEST(LowerBooleanNot_bit_bit) {
726 // BooleanNot(x: rBit) used as rBit
727 TestingGraph t(Type::Boolean());
728 Node* b = t.ExampleWithOutput(rBit);
729 Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
730 Node* use = t.Branch(inv);
731 t.Lower();
732 Node* cmp = use->InputAt(0);
733 CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
734 CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
735 Node* f = t.jsgraph.Int32Constant(0);
736 CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
737 }
738
739
740 TEST(LowerBooleanNot_bit_tagged) {
741 // BooleanNot(x: rBit) used as rTagged
742 TestingGraph t(Type::Boolean());
743 Node* b = t.ExampleWithOutput(rBit);
744 Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
745 Node* use = t.Use(inv, rTagged);
746 t.Return(use);
747 t.Lower();
748 CHECK_EQ(IrOpcode::kChangeBitToBool, use->InputAt(0)->opcode());
749 Node* cmp = use->InputAt(0)->InputAt(0);
750 CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
751 CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
752 Node* f = t.jsgraph.Int32Constant(0);
753 CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
754 }
755
756
757 TEST(LowerBooleanNot_tagged_bit) {
758 // BooleanNot(x: rTagged) used as rBit
759 TestingGraph t(Type::Boolean());
760 Node* b = t.p0;
761 Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
762 Node* use = t.Branch(inv);
763 t.Lower();
764 Node* cmp = use->InputAt(0);
765 CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
766 CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
767 Node* f = t.jsgraph.FalseConstant();
768 CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
769 }
770
771
772 TEST(LowerBooleanNot_tagged_tagged) {
773 // BooleanNot(x: rTagged) used as rTagged
774 TestingGraph t(Type::Boolean());
775 Node* b = t.p0;
776 Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
777 Node* use = t.Use(inv, rTagged);
778 t.Return(use);
779 t.Lower();
780 CHECK_EQ(IrOpcode::kChangeBitToBool, use->InputAt(0)->opcode());
781 Node* cmp = use->InputAt(0)->InputAt(0);
782 CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
783 CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
784 Node* f = t.jsgraph.FalseConstant();
785 CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
786 }
787
788
789 static Type* test_types[] = {Type::Signed32(), Type::Unsigned32(),
790 Type::Number(), Type::Any()};
791
792
793 TEST(LowerNumberCmp_to_int32) {
794 TestingGraph t(Type::Signed32(), Type::Signed32());
795
796 t.CheckLoweringBinop(IrOpcode::kWord32Equal, t.simplified()->NumberEqual());
797 t.CheckLoweringBinop(IrOpcode::kInt32LessThan,
798 t.simplified()->NumberLessThan());
799 t.CheckLoweringBinop(IrOpcode::kInt32LessThanOrEqual,
800 t.simplified()->NumberLessThanOrEqual());
801 }
802
803
804 TEST(LowerNumberCmp_to_uint32) {
805 TestingGraph t(Type::Unsigned32(), Type::Unsigned32());
806
807 t.CheckLoweringBinop(IrOpcode::kWord32Equal, t.simplified()->NumberEqual());
808 t.CheckLoweringBinop(IrOpcode::kUint32LessThan,
809 t.simplified()->NumberLessThan());
810 t.CheckLoweringBinop(IrOpcode::kUint32LessThanOrEqual,
811 t.simplified()->NumberLessThanOrEqual());
812 }
813
814
815 TEST(LowerNumberCmp_to_float64) {
816 static Type* types[] = {Type::Number(), Type::Any()};
817
818 for (size_t i = 0; i < ARRAY_SIZE(types); i++) {
819 TestingGraph t(types[i], types[i]);
820
821 t.CheckLoweringBinop(IrOpcode::kFloat64Equal,
822 t.simplified()->NumberEqual());
823 t.CheckLoweringBinop(IrOpcode::kFloat64LessThan,
824 t.simplified()->NumberLessThan());
825 t.CheckLoweringBinop(IrOpcode::kFloat64LessThanOrEqual,
826 t.simplified()->NumberLessThanOrEqual());
827 }
828 }
829
830
831 TEST(LowerNumberAddSub_to_int32) {
832 TestingGraph t(Type::Signed32(), Type::Signed32());
833 t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Add,
834 t.simplified()->NumberAdd(),
835 t.simplified()->NumberToInt32());
836 t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Sub,
837 t.simplified()->NumberSubtract(),
838 t.simplified()->NumberToInt32());
839 }
840
841
842 TEST(LowerNumberAddSub_to_uint32) {
843 TestingGraph t(Type::Unsigned32(), Type::Unsigned32());
844 t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Add,
845 t.simplified()->NumberAdd(),
846 t.simplified()->NumberToUint32());
847 t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Sub,
848 t.simplified()->NumberSubtract(),
849 t.simplified()->NumberToUint32());
850 }
851
852
853 TEST(LowerNumberAddSub_to_float64) {
854 for (size_t i = 0; i < ARRAY_SIZE(test_types); i++) {
855 TestingGraph t(test_types[i], test_types[i]);
856
857 t.CheckLoweringBinop(IrOpcode::kFloat64Add, t.simplified()->NumberAdd());
858 t.CheckLoweringBinop(IrOpcode::kFloat64Sub,
859 t.simplified()->NumberSubtract());
860 }
861 }
862
863
864 TEST(LowerNumberDivMod_to_float64) {
865 for (size_t i = 0; i < ARRAY_SIZE(test_types); i++) {
866 TestingGraph t(test_types[i], test_types[i]);
867
868 t.CheckLoweringBinop(IrOpcode::kFloat64Div, t.simplified()->NumberDivide());
869 t.CheckLoweringBinop(IrOpcode::kFloat64Mod,
870 t.simplified()->NumberModulus());
871 }
872 }
873
874
875 static void CheckChangeOf(IrOpcode::Value change, Node* of, Node* node) {
876 CHECK_EQ(change, node->opcode());
877 CHECK_EQ(of, node->InputAt(0));
878 }
879
880
881 TEST(LowerNumberToInt32_to_nop) {
882 // NumberToInt32(x: rTagged | tInt32) used as rTagged
883 TestingGraph t(Type::Signed32());
884 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
885 Node* use = t.Use(trunc, rTagged);
886 t.Return(use);
887 t.Lower();
888 CHECK_EQ(t.p0, use->InputAt(0));
889 }
890
891
892 TEST(LowerNumberToInt32_to_ChangeTaggedToFloat64) {
893 // NumberToInt32(x: rTagged | tInt32) used as rFloat64
894 TestingGraph t(Type::Signed32());
895 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
896 Node* use = t.Use(trunc, rFloat64);
897 t.Return(use);
898 t.Lower();
899 CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p0, use->InputAt(0));
900 }
901
902
903 TEST(LowerNumberToInt32_to_ChangeTaggedToInt32) {
904 // NumberToInt32(x: rTagged | tInt32) used as rWord32
905 TestingGraph t(Type::Signed32());
906 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
907 Node* use = t.Use(trunc, tInt32);
908 t.Return(use);
909 t.Lower();
910 CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p0, use->InputAt(0));
911 }
912
913
914 TEST(LowerNumberToInt32_to_ChangeFloat64ToTagged) {
915 // TODO(titzer): NumberToInt32(x: rFloat64 | tInt32) used as rTagged
916 }
917
918
919 TEST(LowerNumberToInt32_to_ChangeFloat64ToInt32) {
920 // TODO(titzer): NumberToInt32(x: rFloat64 | tInt32) used as rWord32 | tInt32
921 }
922
923
924 TEST(LowerNumberToInt32_to_TruncateFloat64ToInt32) {
925 // TODO(titzer): NumberToInt32(x: rFloat64) used as rWord32 | tUint32
926 }
927
928
929 TEST(LowerNumberToUint32_to_nop) {
930 // NumberToUint32(x: rTagged | tUint32) used as rTagged
931 TestingGraph t(Type::Unsigned32());
932 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
933 Node* use = t.Use(trunc, rTagged);
934 t.Return(use);
935 t.Lower();
936 CHECK_EQ(t.p0, use->InputAt(0));
937 }
938
939
940 TEST(LowerNumberToUint32_to_ChangeTaggedToFloat64) {
941 // NumberToUint32(x: rTagged | tUint32) used as rWord32
942 TestingGraph t(Type::Unsigned32());
943 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
944 Node* use = t.Use(trunc, rFloat64);
945 t.Return(use);
946 t.Lower();
947 CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p0, use->InputAt(0));
948 }
949
950
951 TEST(LowerNumberToUint32_to_ChangeTaggedToUint32) {
952 // NumberToUint32(x: rTagged | tUint32) used as rWord32
953 TestingGraph t(Type::Unsigned32());
954 Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
955 Node* use = t.Use(trunc, tUint32);
956 t.Return(use);
957 t.Lower();
958 CheckChangeOf(IrOpcode::kChangeTaggedToUint32, t.p0, use->InputAt(0));
959 }
960
961
962 TEST(LowerNumberToUint32_to_ChangeFloat64ToTagged) {
963 // TODO(titzer): NumberToUint32(x: rFloat64 | tUint32) used as rTagged
964 }
965
966
967 TEST(LowerNumberToUint32_to_ChangeFloat64ToUint32) {
968 // TODO(titzer): NumberToUint32(x: rFloat64 | tUint32) used as rWord32
969 }
970
971
972 TEST(LowerNumberToUint32_to_TruncateFloat64ToUint32) {
973 // TODO(titzer): NumberToUint32(x: rFloat64) used as rWord32
974 }
975
976
977 TEST(LowerReferenceEqual_to_wordeq) {
978 TestingGraph t(Type::Any(), Type::Any());
979 IrOpcode::Value opcode =
980 static_cast<IrOpcode::Value>(t.machine()->WordEqual()->opcode());
981 t.CheckLoweringBinop(opcode, t.simplified()->ReferenceEqual(Type::Any()));
982 }
983
984
985 TEST(LowerStringOps_to_rtcalls) {
986 if (false) { // TODO(titzer): lower StringOps to runtime calls
987 TestingGraph t(Type::String(), Type::String());
988 t.CheckLoweringBinop(IrOpcode::kCall, t.simplified()->StringEqual());
989 t.CheckLoweringBinop(IrOpcode::kCall, t.simplified()->StringLessThan());
990 t.CheckLoweringBinop(IrOpcode::kCall,
991 t.simplified()->StringLessThanOrEqual());
992 t.CheckLoweringBinop(IrOpcode::kCall, t.simplified()->StringAdd());
993 }
994 }
995
996
997 void CheckChangeInsertion(IrOpcode::Value expected, RepType from, RepType to) {
998 TestingGraph t(Type::Any());
999 Node* in = t.ExampleWithOutput(from);
1000 Node* use = t.Use(in, to);
1001 t.Return(use);
1002 t.Lower();
1003 CHECK_EQ(expected, use->InputAt(0)->opcode());
1004 CHECK_EQ(in, use->InputAt(0)->InputAt(0));
1005 }
1006
1007
1008 TEST(InsertBasicChanges) {
1009 if (false) {
1010 // TODO(titzer): these changes need the output to have the right type.
1011 CheckChangeInsertion(IrOpcode::kChangeFloat64ToInt32, rFloat64, tInt32);
1012 CheckChangeInsertion(IrOpcode::kChangeFloat64ToUint32, rFloat64, tUint32);
1013 CheckChangeInsertion(IrOpcode::kChangeTaggedToInt32, rTagged, tInt32);
1014 CheckChangeInsertion(IrOpcode::kChangeTaggedToUint32, rTagged, tUint32);
1015 }
1016
1017 CheckChangeInsertion(IrOpcode::kChangeFloat64ToTagged, rFloat64, rTagged);
1018 CheckChangeInsertion(IrOpcode::kChangeTaggedToFloat64, rTagged, rFloat64);
1019
1020 CheckChangeInsertion(IrOpcode::kChangeInt32ToFloat64, tInt32, rFloat64);
1021 CheckChangeInsertion(IrOpcode::kChangeInt32ToTagged, tInt32, rTagged);
1022
1023 CheckChangeInsertion(IrOpcode::kChangeUint32ToFloat64, tUint32, rFloat64);
1024 CheckChangeInsertion(IrOpcode::kChangeUint32ToTagged, tUint32, rTagged);
1025 }
1026
1027
1028 static void CheckChangesAroundBinop(TestingGraph* t, Operator* op,
1029 IrOpcode::Value input_change,
1030 IrOpcode::Value output_change) {
1031 Node* binop = t->graph()->NewNode(op, t->p0, t->p1);
1032 t->Return(binop);
1033 t->Lower();
1034 CHECK_EQ(input_change, binop->InputAt(0)->opcode());
1035 CHECK_EQ(input_change, binop->InputAt(1)->opcode());
1036 CHECK_EQ(t->p0, binop->InputAt(0)->InputAt(0));
1037 CHECK_EQ(t->p1, binop->InputAt(1)->InputAt(0));
1038 CHECK_EQ(output_change, t->ret->InputAt(0)->opcode());
1039 CHECK_EQ(binop, t->ret->InputAt(0)->InputAt(0));
1040 }
1041
1042
1043 TEST(InsertChangesAroundInt32Binops) {
1044 TestingGraph t(Type::Signed32(), Type::Signed32());
1045
1046 Operator* ops[] = {t.machine()->Int32Add(), t.machine()->Int32Sub(),
1047 t.machine()->Int32Mul(), t.machine()->Int32Div(),
1048 t.machine()->Int32Mod(), t.machine()->Word32And(),
1049 t.machine()->Word32Or(), t.machine()->Word32Xor(),
1050 t.machine()->Word32Shl(), t.machine()->Word32Sar()};
1051
1052 for (size_t i = 0; i < ARRAY_SIZE(ops); i++) {
1053 CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToInt32,
1054 IrOpcode::kChangeInt32ToTagged);
1055 }
1056 }
1057
1058
1059 TEST(InsertChangesAroundInt32Cmp) {
1060 TestingGraph t(Type::Signed32(), Type::Signed32());
1061
1062 Operator* ops[] = {t.machine()->Int32LessThan(),
1063 t.machine()->Int32LessThanOrEqual()};
1064
1065 for (size_t i = 0; i < ARRAY_SIZE(ops); i++) {
1066 CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToInt32,
1067 IrOpcode::kChangeBitToBool);
1068 }
1069 }
1070
1071
1072 TEST(InsertChangesAroundUint32Cmp) {
1073 TestingGraph t(Type::Unsigned32(), Type::Unsigned32());
1074
1075 Operator* ops[] = {t.machine()->Uint32LessThan(),
1076 t.machine()->Uint32LessThanOrEqual()};
1077
1078 for (size_t i = 0; i < ARRAY_SIZE(ops); i++) {
1079 CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToUint32,
1080 IrOpcode::kChangeBitToBool);
1081 }
1082 }
1083
1084
1085 TEST(InsertChangesAroundFloat64Binops) {
1086 TestingGraph t(Type::Number(), Type::Number());
1087
1088 Operator* ops[] = {
1089 t.machine()->Float64Add(), t.machine()->Float64Sub(),
1090 t.machine()->Float64Mul(), t.machine()->Float64Div(),
1091 t.machine()->Float64Mod(),
1092 };
1093
1094 for (size_t i = 0; i < ARRAY_SIZE(ops); i++) {
1095 CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToFloat64,
1096 IrOpcode::kChangeFloat64ToTagged);
1097 }
1098 }
1099
1100
1101 TEST(InsertChangesAroundFloat64Cmp) {
1102 TestingGraph t(Type::Number(), Type::Number());
1103
1104 Operator* ops[] = {t.machine()->Float64Equal(),
1105 t.machine()->Float64LessThan(),
1106 t.machine()->Float64LessThanOrEqual()};
1107
1108 for (size_t i = 0; i < ARRAY_SIZE(ops); i++) {
1109 CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToFloat64,
1110 IrOpcode::kChangeBitToBool);
1111 }
1112 }
1113
1114
1115 void CheckFieldAccessArithmetic(FieldAccess access, Node* load_or_store) {
1116 Int32Matcher index = Int32Matcher(load_or_store->InputAt(1));
1117 CHECK(index.Is(access.offset - access.tag()));
1118 }
1119
1120
1121 Node* CheckElementAccessArithmetic(ElementAccess access, Node* load_or_store) {
1122 Int32BinopMatcher index(load_or_store->InputAt(1));
1123 CHECK_EQ(IrOpcode::kInt32Add, index.node()->opcode());
1124 CHECK(index.right().Is(access.header_size - access.tag()));
1125
1126 int element_size = 0;
1127 switch (access.representation) {
1128 case kMachineTagged:
1129 element_size = kPointerSize;
1130 break;
1131 case kMachineWord8:
1132 element_size = 1;
1133 break;
1134 case kMachineWord16:
1135 element_size = 2;
1136 break;
1137 case kMachineWord32:
1138 element_size = 4;
1139 break;
1140 case kMachineWord64:
1141 case kMachineFloat64:
1142 element_size = 8;
1143 break;
1144 case kMachineLast:
1145 UNREACHABLE();
1146 break;
1147 }
1148
1149 if (element_size != 1) {
1150 Int32BinopMatcher mul(index.left().node());
1151 CHECK_EQ(IrOpcode::kInt32Mul, mul.node()->opcode());
1152 CHECK(mul.right().Is(element_size));
1153 return mul.left().node();
1154 } else {
1155 return index.left().node();
1156 }
1157 }
1158
1159
1160 static const MachineRepresentation machine_reps[] = {
1161 kMachineWord8, kMachineWord16, kMachineWord32,
1162 kMachineWord64, kMachineFloat64, kMachineTagged};
1163
1164
1165 // Representation types corresponding to those above.
1166 static const RepType rep_types[] = {static_cast<RepType>(rWord32 | tUint32),
1167 static_cast<RepType>(rWord32 | tUint32),
1168 static_cast<RepType>(rWord32 | tInt32),
1169 static_cast<RepType>(rWord64),
1170 static_cast<RepType>(rFloat64 | tNumber),
1171 static_cast<RepType>(rTagged | tAny)};
1172
1173
1174 TEST(LowerLoadField_to_load) {
1175 TestingGraph t(Type::Any(), Type::Signed32());
1176
1177 for (size_t i = 0; i < ARRAY_SIZE(machine_reps); i++) {
1178 FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1179 Handle<Name>::null(), Type::Any(), machine_reps[i]};
1180
1181 Node* load =
1182 t.graph()->NewNode(t.simplified()->LoadField(access), t.p0, t.start);
1183 Node* use = t.Use(load, rep_types[i]);
1184 t.Return(use);
1185 t.Lower();
1186 CHECK_EQ(IrOpcode::kLoad, load->opcode());
1187 CHECK_EQ(t.p0, load->InputAt(0));
1188 CheckFieldAccessArithmetic(access, load);
1189
1190 MachineRepresentation rep = OpParameter<MachineRepresentation>(load);
1191 CHECK_EQ(machine_reps[i], rep);
1192 }
1193 }
1194
1195
1196 TEST(LowerStoreField_to_store) {
1197 TestingGraph t(Type::Any(), Type::Signed32());
1198
1199 for (size_t i = 0; i < ARRAY_SIZE(machine_reps); i++) {
1200 FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1201 Handle<Name>::null(), Type::Any(), machine_reps[i]};
1202
1203
1204 Node* val = t.ExampleWithOutput(rep_types[i]);
1205 Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
1206 val, t.start, t.start);
1207 t.Effect(store);
1208 t.Lower();
1209 CHECK_EQ(IrOpcode::kStore, store->opcode());
1210 CHECK_EQ(val, store->InputAt(2));
1211 CheckFieldAccessArithmetic(access, store);
1212
1213 StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
1214 if (rep_types[i] & rTagged) {
1215 CHECK_EQ(kFullWriteBarrier, rep.write_barrier_kind);
1216 }
1217 CHECK_EQ(machine_reps[i], rep.rep);
1218 }
1219 }
1220
1221
1222 TEST(LowerLoadElement_to_load) {
1223 TestingGraph t(Type::Any(), Type::Signed32());
1224
1225 for (size_t i = 0; i < ARRAY_SIZE(machine_reps); i++) {
1226 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1227 Type::Any(), machine_reps[i]};
1228
1229 Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
1230 t.p1, t.start);
1231 Node* use = t.Use(load, rep_types[i]);
1232 t.Return(use);
1233 t.Lower();
1234 CHECK_EQ(IrOpcode::kLoad, load->opcode());
1235 CHECK_EQ(t.p0, load->InputAt(0));
1236 CheckElementAccessArithmetic(access, load);
1237
1238 MachineRepresentation rep = OpParameter<MachineRepresentation>(load);
1239 CHECK_EQ(machine_reps[i], rep);
1240 }
1241 }
1242
1243
1244 TEST(LowerStoreElement_to_store) {
1245 TestingGraph t(Type::Any(), Type::Signed32());
1246
1247 for (size_t i = 0; i < ARRAY_SIZE(machine_reps); i++) {
1248 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1249 Type::Any(), machine_reps[i]};
1250
1251 Node* val = t.ExampleWithOutput(rep_types[i]);
1252 Node* store = t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0,
1253 t.p1, val, t.start, t.start);
1254 t.Effect(store);
1255 t.Lower();
1256 CHECK_EQ(IrOpcode::kStore, store->opcode());
1257 CHECK_EQ(val, store->InputAt(2));
1258 CheckElementAccessArithmetic(access, store);
1259
1260 StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
1261 if (rep_types[i] & rTagged) {
1262 CHECK_EQ(kFullWriteBarrier, rep.write_barrier_kind);
1263 }
1264 CHECK_EQ(machine_reps[i], rep.rep);
1265 }
1266 }
1267
1268
1269 TEST(InsertChangeForLoadElementIndex) {
1270 // LoadElement(obj: Tagged, index: tInt32 | rTagged) =>
1271 // Load(obj, Int32Add(Int32Mul(ChangeTaggedToInt32(index), #k), #k))
1272 TestingGraph t(Type::Any(), Type::Signed32());
1273 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1274 kMachineTagged};
1275
1276 Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
1277 t.p1, t.start);
1278 t.Return(load);
1279 t.Lower();
1280 CHECK_EQ(IrOpcode::kLoad, load->opcode());
1281 CHECK_EQ(t.p0, load->InputAt(0));
1282
1283 Node* index = CheckElementAccessArithmetic(access, load);
1284 CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p1, index);
1285 }
1286
1287
1288 TEST(InsertChangeForStoreElementIndex) {
1289 // StoreElement(obj: Tagged, index: tInt32 | rTagged, val) =>
1290 // Store(obj, Int32Add(Int32Mul(ChangeTaggedToInt32(index), #k), #k), val)
1291 TestingGraph t(Type::Any(), Type::Signed32());
1292 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1293 kMachineTagged};
1294
1295 Node* store =
1296 t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0, t.p1,
1297 t.jsgraph.TrueConstant(), t.start, t.start);
1298 t.Effect(store);
1299 t.Lower();
1300 CHECK_EQ(IrOpcode::kStore, store->opcode());
1301 CHECK_EQ(t.p0, store->InputAt(0));
1302
1303 Node* index = CheckElementAccessArithmetic(access, store);
1304 CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p1, index);
1305 }
1306
1307
1308 TEST(InsertChangeForLoadElement) {
1309 // TODO(titzer): test all load/store representation change insertions.
1310 TestingGraph t(Type::Any(), Type::Signed32());
1311 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1312 kMachineFloat64};
1313
1314 Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
1315 t.p1, t.start);
1316 t.Return(load);
1317 t.Lower();
1318 CHECK_EQ(IrOpcode::kLoad, load->opcode());
1319 CHECK_EQ(t.p0, load->InputAt(0));
1320 CheckChangeOf(IrOpcode::kChangeFloat64ToTagged, load, t.ret->InputAt(0));
1321 }
1322
1323
1324 TEST(InsertChangeForLoadField) {
1325 // TODO(titzer): test all load/store representation change insertions.
1326 TestingGraph t(Type::Any(), Type::Signed32());
1327 FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1328 Handle<Name>::null(), Type::Any(), kMachineFloat64};
1329
1330 Node* load =
1331 t.graph()->NewNode(t.simplified()->LoadField(access), t.p0, t.start);
1332 t.Return(load);
1333 t.Lower();
1334 CHECK_EQ(IrOpcode::kLoad, load->opcode());
1335 CHECK_EQ(t.p0, load->InputAt(0));
1336 CheckChangeOf(IrOpcode::kChangeFloat64ToTagged, load, t.ret->InputAt(0));
1337 }
1338
1339
1340 TEST(InsertChangeForStoreElement) {
1341 // TODO(titzer): test all load/store representation change insertions.
1342 TestingGraph t(Type::Any(), Type::Signed32());
1343 ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1344 kMachineFloat64};
1345
1346 Node* store =
1347 t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0,
1348 t.jsgraph.Int32Constant(0), t.p1, t.start, t.start);
1349 t.Effect(store);
1350 t.Lower();
1351
1352 CHECK_EQ(IrOpcode::kStore, store->opcode());
1353 CHECK_EQ(t.p0, store->InputAt(0));
1354 CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p1, store->InputAt(2));
1355 }
1356
1357
1358 TEST(InsertChangeForStoreField) {
1359 // TODO(titzer): test all load/store representation change insertions.
1360 TestingGraph t(Type::Any(), Type::Signed32());
1361 FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1362 Handle<Name>::null(), Type::Any(), kMachineFloat64};
1363
1364 Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
1365 t.p1, t.start, t.start);
1366 t.Effect(store);
1367 t.Lower();
1368
1369 CHECK_EQ(IrOpcode::kStore, store->opcode());
1370 CHECK_EQ(t.p0, store->InputAt(0));
1371 CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p1, store->InputAt(2));
1372 }
OLDNEW
« no previous file with comments | « test/cctest/compiler/test-changes-lowering.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698