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

Side by Side Diff: test/unittests/compiler/bytecode-graph-builder-unittest.cc

Issue 1468003002: [Interpreter] Add support for cast operators to bytecode graph builder and (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 1 month 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
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 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 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 <iostream> 5 #include <iostream>
6 6
7 #include "src/compiler/bytecode-graph-builder.h" 7 #include "src/compiler/bytecode-graph-builder.h"
8 #include "src/compiler/common-operator.h" 8 #include "src/compiler/common-operator.h"
9 #include "src/compiler/graph-visualizer.h" 9 #include "src/compiler/graph-visualizer.h"
10 #include "src/compiler/instruction.h" 10 #include "src/compiler/instruction.h"
(...skipping 10 matching lines...) Expand all
21 21
22 using ::testing::_; 22 using ::testing::_;
23 23
24 namespace v8 { 24 namespace v8 {
25 namespace internal { 25 namespace internal {
26 namespace compiler { 26 namespace compiler {
27 27
28 static const LanguageMode kLanguageModes[] = {LanguageMode::SLOPPY, 28 static const LanguageMode kLanguageModes[] = {LanguageMode::SLOPPY,
29 LanguageMode::STRICT}; 29 LanguageMode::STRICT};
30 30
31 static const Token::Value kBinaryOperators[] = {
32 Token::Value::BIT_OR, Token::Value::BIT_XOR, Token::Value::BIT_AND,
33 Token::Value::SHL, Token::Value::SAR, Token::Value::SHR,
34 Token::Value::ADD, Token::Value::SUB, Token::Value::MUL,
35 Token::Value::DIV, Token::Value::MOD};
36
37 static IrOpcode::Value getIrOpcode(Token::Value token) {
38 switch (token) {
39 case Token::Value::BIT_OR:
40 return IrOpcode::Value::kJSBitwiseOr;
41 case Token::Value::BIT_XOR:
42 return IrOpcode::Value::kJSBitwiseXor;
43 case Token::Value::BIT_AND:
44 return IrOpcode::Value::kJSBitwiseAnd;
45 case Token::Value::SHL:
46 return IrOpcode::Value::kJSShiftLeft;
47 case Token::Value::SAR:
48 return IrOpcode::Value::kJSShiftRight;
49 case Token::Value::SHR:
50 return IrOpcode::Value::kJSShiftRightLogical;
51 case Token::Value::ADD:
52 return IrOpcode::Value::kJSAdd;
53 case Token::Value::SUB:
54 return IrOpcode::Value::kJSSubtract;
55 case Token::Value::MUL:
56 return IrOpcode::Value::kJSMultiply;
57 case Token::Value::DIV:
58 return IrOpcode::Value::kJSDivide;
59 case Token::Value::MOD:
60 return IrOpcode::Value::kJSModulus;
61 default:
62 UNREACHABLE();
63 }
64 }
65
31 Handle<TypeFeedbackVector> NewTypeFeedbackVector(Isolate* isolate, 66 Handle<TypeFeedbackVector> NewTypeFeedbackVector(Isolate* isolate,
32 FeedbackVectorSpec* spec) { 67 FeedbackVectorSpec* spec) {
33 Handle<TypeFeedbackMetadata> vector_metadata = 68 Handle<TypeFeedbackMetadata> vector_metadata =
34 TypeFeedbackMetadata::New(isolate, spec); 69 TypeFeedbackMetadata::New(isolate, spec);
35 return TypeFeedbackVector::New(isolate, vector_metadata); 70 return TypeFeedbackVector::New(isolate, vector_metadata);
36 } 71 }
37 72
38 73
39 class BytecodeGraphBuilderTest : public TestWithIsolateAndZone { 74 class BytecodeGraphBuilderTest : public TestWithIsolateAndZone {
40 public: 75 public:
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 array_builder.set_locals_count(1); 302 array_builder.set_locals_count(1);
268 array_builder.set_context_count(0); 303 array_builder.set_context_count(0);
269 array_builder.set_parameter_count(3); 304 array_builder.set_parameter_count(3);
270 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1)) 305 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
271 .BinaryOperation(Token::Value::ADD, array_builder.Parameter(2), 306 .BinaryOperation(Token::Value::ADD, array_builder.Parameter(2),
272 Strength::WEAK) 307 Strength::WEAK)
273 .StoreAccumulatorInRegister(interpreter::Register(0)) 308 .StoreAccumulatorInRegister(interpreter::Register(0))
274 .Return(); 309 .Return();
275 310
276 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray()); 311 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
312 Node* start = graph->start();
277 Node* end = graph->end(); 313 Node* end = graph->end();
278 EXPECT_EQ(1, end->InputCount()); 314 EXPECT_EQ(1, end->InputCount());
279 Node* ret = end->InputAt(0); 315 Node* ret = end->InputAt(0);
280 // NB binary operation is <reg> <op> <acc>. The register represents 316 // NB binary operation is <reg> <op> <acc>. The register represents
281 // the left-hand side, which is why parameters appear in opposite 317 // the left-hand side, which is why parameters appear in opposite
282 // order to construction via the builder. 318 // order to construction via the builder.
283 EXPECT_THAT(ret, IsReturn(IsJSAdd(IsParameter(2), IsParameter(1)), _, _)); 319 Matcher<Node*> js_add_node = IsJSBinaryOperation(
320 IrOpcode::kJSAdd, IsParameter(2), IsParameter(1), start, start);
321 EXPECT_THAT(ret, IsReturn(js_add_node, _, _));
284 } 322 }
285 323
286 324
287 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) { 325 TEST_F(BytecodeGraphBuilderTest, SimpleExpressionWithRegister) {
326 TRACED_FOREACH(Token::Value, token, kBinaryOperators) {
327 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
328 static const int kLeft = -655371;
329 static const int kRight = +2000000;
330 array_builder.set_locals_count(1);
331 array_builder.set_context_count(0);
332 array_builder.set_parameter_count(1);
333 array_builder.LoadLiteral(Smi::FromInt(kLeft))
334 .StoreAccumulatorInRegister(interpreter::Register(0))
335 .LoadLiteral(Smi::FromInt(kRight))
336 .BinaryOperation(token, interpreter::Register(0), Strength::WEAK)
337 .Return();
338
339 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
340 Node* end = graph->end();
341 EXPECT_EQ(1, end->InputCount());
342 Node* start = graph->start();
343 Node* ret = end->InputAt(0);
344 Matcher<Node*> compute_node =
345 IsJSBinaryOperation(getIrOpcode(token), IsNumberConstant(kLeft),
346 IsNumberConstant(kRight), start, start);
347 EXPECT_THAT(ret, IsReturn(compute_node, _, _));
348 }
349 }
350
351
352 TEST_F(BytecodeGraphBuilderTest, ToBoolean) {
288 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone()); 353 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
289 static const int kLeft = -655371;
290 static const int kRight = +2000000;
291 array_builder.set_locals_count(1); 354 array_builder.set_locals_count(1);
292 array_builder.set_context_count(0); 355 array_builder.set_context_count(0);
293 array_builder.set_parameter_count(1); 356 array_builder.set_parameter_count(2);
294 array_builder.LoadLiteral(Smi::FromInt(kLeft)) 357 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
295 .StoreAccumulatorInRegister(interpreter::Register(0)) 358 .CastAccumulatorToBoolean()
296 .LoadLiteral(Smi::FromInt(kRight))
297 .BinaryOperation(Token::Value::ADD, interpreter::Register(0),
298 Strength::WEAK)
299 .Return(); 359 .Return();
300 360
301 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray()); 361 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
302 Node* end = graph->end(); 362 Node* ret = graph->end()->InputAt(0);
303 EXPECT_EQ(1, end->InputCount()); 363 Node* start = graph->start();
304 Node* ret = end->InputAt(0); 364
305 EXPECT_THAT( 365 Matcher<Node*> to_boolean_node = IsJSToBoolean(IsParameter(1), start);
306 ret, IsReturn(IsJSAdd(IsNumberConstant(kLeft), IsNumberConstant(kRight)), 366 EXPECT_THAT(ret, IsReturn(to_boolean_node, _, _));
307 _, _));
308 } 367 }
309 368
310 369
370 TEST_F(BytecodeGraphBuilderTest, ToName) {
371 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
372 array_builder.set_locals_count(1);
373 array_builder.set_context_count(0);
374 array_builder.set_parameter_count(2);
375 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
376 .CastAccumulatorToName()
377 .Return();
378
379 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
380 Node* ret = graph->end()->InputAt(0);
381 Node* start = graph->start();
382
383 Matcher<Node*> to_name_node = IsJSToName(IsParameter(1), start, start);
384 EXPECT_THAT(ret, IsReturn(to_name_node, _, _));
385 }
386
387
388 TEST_F(BytecodeGraphBuilderTest, ToObject) {
389 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
390 array_builder.set_locals_count(1);
391 array_builder.set_context_count(0);
392 array_builder.set_parameter_count(2);
393 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
394 .CastAccumulatorToJSObject()
395 .Return();
396
397 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
398 Node* ret = graph->end()->InputAt(0);
399 Node* start = graph->start();
400
401 Matcher<Node*> to_object_node = IsJSToObject(IsParameter(1), start, start);
402 EXPECT_THAT(ret, IsReturn(to_object_node, _, _));
403 }
404
405
406 TEST_F(BytecodeGraphBuilderTest, ToNumber) {
407 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
408 array_builder.set_locals_count(1);
409 array_builder.set_context_count(0);
410 array_builder.set_parameter_count(2);
411 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
412 .CastAccumulatorToNumber()
413 .Return();
414
415 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray());
416 Node* ret = graph->end()->InputAt(0);
417 Node* start = graph->start();
418
419 Matcher<Node*> to_number_node = IsToNumber(IsParameter(1), _, start, start);
420 EXPECT_THAT(ret, IsReturn(to_number_node, _, _));
421 }
422
423
311 TEST_F(BytecodeGraphBuilderTest, NamedLoad) { 424 TEST_F(BytecodeGraphBuilderTest, NamedLoad) {
312 const bool kWideBytecode[] = {false, true}; 425 const bool kWideBytecode[] = {false, true};
313 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { 426 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
314 TRACED_FOREACH(bool, wide_bytecode, kWideBytecode) { 427 TRACED_FOREACH(bool, wide_bytecode, kWideBytecode) {
315 FeedbackVectorSpec feedback_spec(zone()); 428 FeedbackVectorSpec feedback_spec(zone());
316 if (wide_bytecode) { 429 if (wide_bytecode) {
317 for (int i = 0; i < 128; i++) { 430 for (int i = 0; i < 128; i++) {
318 feedback_spec.AddLoadICSlot(); 431 feedback_spec.AddLoadICSlot();
319 } 432 }
320 } 433 }
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 array_builder.set_parameter_count(2); 647 array_builder.set_parameter_count(2);
535 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1)) 648 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
536 .LogicalNot() 649 .LogicalNot()
537 .Return(); 650 .Return();
538 651
539 FeedbackVectorSpec feedback_spec(zone()); 652 FeedbackVectorSpec feedback_spec(zone());
540 Handle<TypeFeedbackVector> vector = 653 Handle<TypeFeedbackVector> vector =
541 NewTypeFeedbackVector(isolate(), &feedback_spec); 654 NewTypeFeedbackVector(isolate(), &feedback_spec);
542 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray(), vector); 655 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray(), vector);
543 656
657 Node* start = graph->start();
544 Node* ret = graph->end()->InputAt(0); 658 Node* ret = graph->end()->InputAt(0);
545 EXPECT_THAT(ret, IsReturn(IsJSUnaryNot(IsParameter(1)), _, _)); 659 EXPECT_THAT(ret, IsReturn(IsJSUnaryNot(IsParameter(1), start), _, _));
546 } 660 }
547 661
548 662
549 TEST_F(BytecodeGraphBuilderTest, TypeOf) { 663 TEST_F(BytecodeGraphBuilderTest, TypeOf) {
550 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone()); 664 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
551 array_builder.set_locals_count(1); 665 array_builder.set_locals_count(1);
552 array_builder.set_context_count(0); 666 array_builder.set_context_count(0);
553 array_builder.set_parameter_count(2); 667 array_builder.set_parameter_count(2);
554 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1)) 668 array_builder.LoadAccumulatorWithRegister(array_builder.Parameter(1))
555 .TypeOf() 669 .TypeOf()
556 .Return(); 670 .Return();
557 671
558 FeedbackVectorSpec feedback_spec(zone()); 672 FeedbackVectorSpec feedback_spec(zone());
559 Handle<TypeFeedbackVector> vector = 673 Handle<TypeFeedbackVector> vector =
560 NewTypeFeedbackVector(isolate(), &feedback_spec); 674 NewTypeFeedbackVector(isolate(), &feedback_spec);
561 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray(), vector); 675 Graph* graph = GetCompletedGraph(array_builder.ToBytecodeArray(), vector);
562 676
677 Node* start = graph->start();
563 Node* ret = graph->end()->InputAt(0); 678 Node* ret = graph->end()->InputAt(0);
564 EXPECT_THAT(ret, IsReturn(IsJSTypeOf(IsParameter(1)), _, _)); 679 EXPECT_THAT(ret, IsReturn(IsJSTypeOf(IsParameter(1), start), _, _));
565 } 680 }
566 681
567 682
568 TEST_F(BytecodeGraphBuilderTest, Delete) { 683 TEST_F(BytecodeGraphBuilderTest, Delete) {
569 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) { 684 TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
570 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone()); 685 interpreter::BytecodeArrayBuilder array_builder(isolate(), zone());
571 array_builder.set_locals_count(1); 686 array_builder.set_locals_count(1);
572 array_builder.set_context_count(0); 687 array_builder.set_context_count(0);
573 array_builder.set_parameter_count(2); 688 array_builder.set_parameter_count(2);
574 Handle<Name> name = GetName(isolate(), "val"); 689 Handle<Name> name = GetName(isolate(), "val");
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 construct_inputs.push_back(IsParameter(4)); 918 construct_inputs.push_back(IsParameter(4));
804 construct_inputs.push_back(IsParameter(1)); 919 construct_inputs.push_back(IsParameter(1));
805 Matcher<Node*> call_construct = 920 Matcher<Node*> call_construct =
806 IsJSCallConstruct(construct_inputs, start, start); 921 IsJSCallConstruct(construct_inputs, start, start);
807 EXPECT_THAT(ret, IsReturn(call_construct, call_construct, IsIfSuccess(_))); 922 EXPECT_THAT(ret, IsReturn(call_construct, call_construct, IsIfSuccess(_)));
808 } 923 }
809 924
810 } // namespace compiler 925 } // namespace compiler
811 } // namespace internal 926 } // namespace internal
812 } // namespace v8 927 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698