Chromium Code Reviews| 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 // TODO(jochen): Remove this after the setting is turned on globally. | 5 // TODO(jochen): Remove this after the setting is turned on globally. |
| 6 #define V8_IMMINENT_DEPRECATION_WARNINGS | 6 #define V8_IMMINENT_DEPRECATION_WARNINGS |
| 7 | 7 |
| 8 #include "src/compiler/js-graph.h" | 8 #include "src/compiler/js-graph.h" |
| 9 #include "src/compiler/js-typed-lowering.h" | 9 #include "src/compiler/js-typed-lowering.h" |
| 10 #include "src/compiler/machine-operator.h" | 10 #include "src/compiler/machine-operator.h" |
| 11 #include "src/compiler/node-properties.h" | 11 #include "src/compiler/node-properties.h" |
| 12 #include "src/compiler/opcodes.h" | 12 #include "src/compiler/opcodes.h" |
| 13 #include "src/compiler/operator-properties.h" | 13 #include "src/compiler/operator-properties.h" |
| 14 #include "src/compiler/simplified-operator.h" | 14 #include "src/compiler/simplified-operator.h" |
| 15 #include "src/compiler/typer.h" | 15 #include "src/compiler/typer.h" |
| 16 #include "test/cctest/cctest.h" | 16 #include "test/cctest/cctest.h" |
| 17 #include "test/cctest/compiler/function-tester.h" | |
| 17 | 18 |
| 18 namespace v8 { | 19 namespace v8 { |
| 19 namespace internal { | 20 namespace internal { |
| 20 namespace compiler { | 21 namespace compiler { |
| 21 | 22 |
| 22 #ifndef TEST_WITH_STRONG | 23 #ifndef TEST_WITH_STRONG |
| 23 #define TEST_WITH_STRONG(Name) \ | 24 #define TEST_WITH_STRONG(Name) \ |
| 24 static void Test##Name(); \ | 25 static void Test##Name(); \ |
| 25 static void TestWithStrong##Name(LanguageMode language_mode); \ | 26 static void TestWithStrong##Name(LanguageMode language_mode); \ |
| 26 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, true); \ | 27 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true, true); \ |
| 27 static void Test##Name() { \ | 28 static void Test##Name() { \ |
| 28 TestWithStrong##Name(LanguageMode::SLOPPY); \ | 29 TestWithStrong##Name(LanguageMode::SLOPPY); \ |
| 29 TestWithStrong##Name(LanguageMode::STRONG); \ | 30 TestWithStrong##Name(LanguageMode::STRONG); \ |
| 30 } \ | 31 } \ |
| 31 static void TestWithStrong##Name(LanguageMode language_mode) | 32 static void TestWithStrong##Name(LanguageMode language_mode) |
| 32 #endif | 33 #endif |
| 33 | 34 |
| 34 | 35 |
| 35 class JSTypedLoweringTester : public HandleAndZoneScope { | 36 class JSTypedLoweringTester : public HandleAndZoneScope { |
| 36 public: | 37 public: |
| 37 explicit JSTypedLoweringTester(int num_parameters = 0) | 38 explicit JSTypedLoweringTester(int num_parameters = 0) |
| 38 : isolate(main_isolate()), | 39 : isolate(main_isolate()), |
| 39 binop(NULL), | 40 binop(NULL), |
| 40 unop(NULL), | 41 unop(NULL), |
| 41 javascript(main_zone()), | 42 javascript(main_zone()), |
| 42 machine(main_zone()), | 43 machine(main_zone()), |
| 43 simplified(main_zone()), | 44 simplified(main_zone()), |
| 44 common(main_zone()), | 45 common(main_zone()), |
| 46 deps(main_isolate(), main_zone()), | |
| 45 graph(main_zone()), | 47 graph(main_zone()), |
| 46 typer(main_isolate(), &graph), | 48 typer(main_isolate(), &graph), |
| 47 context_node(NULL) { | 49 context_node(NULL) { |
| 48 graph.SetStart(graph.NewNode(common.Start(num_parameters))); | 50 graph.SetStart(graph.NewNode(common.Start(num_parameters))); |
| 49 graph.SetEnd(graph.NewNode(common.End(1), graph.start())); | 51 graph.SetEnd(graph.NewNode(common.End(1), graph.start())); |
| 50 typer.Run(); | 52 typer.Run(); |
| 51 } | 53 } |
| 52 | 54 |
| 53 Isolate* isolate; | 55 Isolate* isolate; |
| 54 const Operator* binop; | 56 const Operator* binop; |
| 55 const Operator* unop; | 57 const Operator* unop; |
| 56 JSOperatorBuilder javascript; | 58 JSOperatorBuilder javascript; |
| 57 MachineOperatorBuilder machine; | 59 MachineOperatorBuilder machine; |
| 58 SimplifiedOperatorBuilder simplified; | 60 SimplifiedOperatorBuilder simplified; |
| 59 CommonOperatorBuilder common; | 61 CommonOperatorBuilder common; |
| 62 CompilationDependencies deps; | |
| 60 Graph graph; | 63 Graph graph; |
| 61 Typer typer; | 64 Typer typer; |
| 62 Node* context_node; | 65 Node* context_node; |
| 63 | 66 |
| 64 Node* Parameter(Type* t, int32_t index = 0) { | 67 Node* Parameter(Type* t, int32_t index = 0) { |
| 65 Node* n = graph.NewNode(common.Parameter(index), graph.start()); | 68 Node* n = graph.NewNode(common.Parameter(index), graph.start()); |
| 66 NodeProperties::SetType(n, t); | 69 NodeProperties::SetType(n, t); |
| 67 return n; | 70 return n; |
| 68 } | 71 } |
| 69 | 72 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 87 parameters, locals, stack, context, UndefinedConstant(), graph.start()); | 90 parameters, locals, stack, context, UndefinedConstant(), graph.start()); |
| 88 | 91 |
| 89 return state_node; | 92 return state_node; |
| 90 } | 93 } |
| 91 | 94 |
| 92 Node* reduce(Node* node) { | 95 Node* reduce(Node* node) { |
| 93 JSGraph jsgraph(main_isolate(), &graph, &common, &javascript, &simplified, | 96 JSGraph jsgraph(main_isolate(), &graph, &common, &javascript, &simplified, |
| 94 &machine); | 97 &machine); |
| 95 // TODO(titzer): mock the GraphReducer here for better unit testing. | 98 // TODO(titzer): mock the GraphReducer here for better unit testing. |
| 96 GraphReducer graph_reducer(main_zone(), &graph); | 99 GraphReducer graph_reducer(main_zone(), &graph); |
| 97 JSTypedLowering reducer(&graph_reducer, &jsgraph, main_zone()); | 100 JSTypedLowering reducer(&graph_reducer, &deps, &jsgraph, main_zone()); |
| 98 Reduction reduction = reducer.Reduce(node); | 101 Reduction reduction = reducer.Reduce(node); |
| 99 if (reduction.Changed()) return reduction.replacement(); | 102 if (reduction.Changed()) return reduction.replacement(); |
| 100 return node; | 103 return node; |
| 101 } | 104 } |
| 102 | 105 |
| 103 Node* start() { return graph.start(); } | 106 Node* start() { return graph.start(); } |
| 104 | 107 |
| 105 Node* context() { | 108 Node* context() { |
| 106 if (context_node == NULL) { | 109 if (context_node == NULL) { |
| 107 context_node = graph.NewNode(common.Parameter(-1), graph.start()); | 110 context_node = graph.NewNode(common.Parameter(-1), graph.start()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 122 Node* ReduceUnop(const Operator* op, Type* input_type) { | 125 Node* ReduceUnop(const Operator* op, Type* input_type) { |
| 123 return reduce(Unop(op, Parameter(input_type))); | 126 return reduce(Unop(op, Parameter(input_type))); |
| 124 } | 127 } |
| 125 | 128 |
| 126 Node* ReduceBinop(const Operator* op, Type* left_type, Type* right_type) { | 129 Node* ReduceBinop(const Operator* op, Type* left_type, Type* right_type) { |
| 127 return reduce(Binop(op, Parameter(left_type, 0), Parameter(right_type, 1))); | 130 return reduce(Binop(op, Parameter(left_type, 0), Parameter(right_type, 1))); |
| 128 } | 131 } |
| 129 | 132 |
| 130 Node* Binop(const Operator* op, Node* left, Node* right) { | 133 Node* Binop(const Operator* op, Node* left, Node* right) { |
| 131 // JS binops also require context, effect, and control | 134 // JS binops also require context, effect, and control |
| 135 DCHECK_EQ(op->ValueInputCount(), 2); | |
| 132 std::vector<Node*> inputs; | 136 std::vector<Node*> inputs; |
| 133 inputs.push_back(left); | 137 inputs.push_back(left); |
| 134 inputs.push_back(right); | 138 inputs.push_back(right); |
| 135 if (OperatorProperties::HasContextInput(op)) { | 139 if (OperatorProperties::HasContextInput(op)) { |
| 136 inputs.push_back(context()); | 140 inputs.push_back(context()); |
| 137 } | 141 } |
| 138 for (int i = 0; i < OperatorProperties::GetFrameStateInputCount(op); i++) { | 142 for (int i = 0; i < OperatorProperties::GetFrameStateInputCount(op); i++) { |
| 139 inputs.push_back(EmptyFrameState(context())); | 143 inputs.push_back(EmptyFrameState(context())); |
| 140 } | 144 } |
| 141 if (op->EffectInputCount() > 0) { | 145 if (op->EffectInputCount() > 0) { |
| (...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1257 CHECK_EQ(p0, r->InputAt(1)); | 1261 CHECK_EQ(p0, r->InputAt(1)); |
| 1258 } else { | 1262 } else { |
| 1259 CHECK_EQ(p0, r->InputAt(0)); | 1263 CHECK_EQ(p0, r->InputAt(0)); |
| 1260 CHECK_EQ(p1, r->InputAt(1)); | 1264 CHECK_EQ(p1, r->InputAt(1)); |
| 1261 } | 1265 } |
| 1262 } | 1266 } |
| 1263 } | 1267 } |
| 1264 } | 1268 } |
| 1265 } | 1269 } |
| 1266 | 1270 |
| 1271 | |
| 1272 // Test that instanceOf is reduced if and only if the right-hand side is a | |
| 1273 // function constant. Functional correctness is ensured elsewhere. | |
| 1274 TEST(InstanceOfSpecialization) { | |
|
Michael Starzinger
2015/11/09 19:59:59
Since these tests only check that "something is re
sigurds
2015/11/10 09:39:44
Done.
| |
| 1275 JSTypedLoweringTester R; | |
| 1276 FunctionTester F( | |
| 1277 "(function (){" | |
| 1278 " return function Bar(x){" | |
| 1279 " this.x= x;" | |
| 1280 " }" | |
| 1281 "})();"); | |
| 1282 | |
| 1283 // Reduce if left-hand side is known to be an object. | |
| 1284 Node* instanceOf = | |
| 1285 R.Binop(R.javascript.InstanceOf(), R.Parameter(Type::Object(), 0), | |
| 1286 R.HeapConstant(F.function)); | |
| 1287 Node* dummy = R.Unop(R.javascript.ToObject(), instanceOf); | |
| 1288 Node* replacement = R.reduce(instanceOf); | |
| 1289 CHECK(IrOpcode::IsPhiOpcode(replacement->opcode())); | |
| 1290 CHECK_EQ(replacement, dummy->InputAt(0)); | |
| 1291 CHECK_NE(instanceOf, dummy->InputAt(0)); | |
| 1292 | |
| 1293 // Reduce if left-hand side could be a Smi. | |
| 1294 instanceOf = R.Binop(R.javascript.InstanceOf(), R.Parameter(Type::Any(), 0), | |
| 1295 R.HeapConstant(F.function)); | |
| 1296 dummy = R.Unop(R.javascript.ToObject(), instanceOf); | |
| 1297 replacement = R.reduce(instanceOf); | |
| 1298 CHECK(IrOpcode::IsPhiOpcode(replacement->opcode())); | |
| 1299 CHECK_EQ(replacement, dummy->InputAt(0)); | |
| 1300 CHECK_NE(instanceOf, dummy->InputAt(0)); | |
| 1301 | |
| 1302 // Do not reduce if right-hand side is not a function constant. | |
| 1303 instanceOf = R.Binop(R.javascript.InstanceOf(), R.Parameter(Type::Any(), 0), | |
| 1304 R.Parameter(Type::Function(), 0)); | |
| 1305 dummy = R.Unop(R.javascript.ToObject(), instanceOf); | |
| 1306 replacement = R.reduce(instanceOf); | |
| 1307 CHECK_EQ(replacement, instanceOf); | |
| 1308 CHECK_EQ(instanceOf, dummy->InputAt(0)); | |
| 1309 } | |
| 1310 | |
| 1267 } // namespace compiler | 1311 } // namespace compiler |
| 1268 } // namespace internal | 1312 } // namespace internal |
| 1269 } // namespace v8 | 1313 } // namespace v8 |
| OLD | NEW |