OLD | NEW |
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 "src/compiler/control-reducer.h" | 5 #include "src/compiler/control-reducer.h" |
| 6 #include "src/compiler/diamond.h" |
6 #include "src/compiler/graph-visualizer.h" | 7 #include "src/compiler/graph-visualizer.h" |
7 #include "src/compiler/js-graph.h" | 8 #include "src/compiler/js-graph.h" |
8 #include "src/compiler/js-operator.h" | 9 #include "src/compiler/js-operator.h" |
9 #include "src/compiler/machine-operator.h" | 10 #include "src/compiler/machine-operator.h" |
10 #include "src/compiler/node.h" | 11 #include "src/compiler/node.h" |
11 #include "test/unittests/compiler/graph-unittest.h" | 12 #include "test/unittests/compiler/graph-unittest.h" |
12 #include "test/unittests/compiler/node-test-utils.h" | 13 #include "test/unittests/compiler/node-test-utils.h" |
13 #include "testing/gmock-support.h" | 14 #include "testing/gmock-support.h" |
14 | 15 |
15 using testing::_; | 16 using testing::_; |
(...skipping 11 matching lines...) Expand all Loading... |
27 : TypedGraphTest(1), | 28 : TypedGraphTest(1), |
28 machine_(zone()), | 29 machine_(zone()), |
29 javascript_(zone()), | 30 javascript_(zone()), |
30 jsgraph_(isolate(), graph(), common(), &javascript_, &machine_) {} | 31 jsgraph_(isolate(), graph(), common(), &javascript_, &machine_) {} |
31 | 32 |
32 protected: | 33 protected: |
33 MachineOperatorBuilder machine_; | 34 MachineOperatorBuilder machine_; |
34 JSOperatorBuilder javascript_; | 35 JSOperatorBuilder javascript_; |
35 JSGraph jsgraph_; | 36 JSGraph jsgraph_; |
36 | 37 |
37 void ReduceGraph() { | 38 void ReduceGraph(int max_phis_for_select = 0) { |
38 if (FLAG_trace_turbo_graph) { | 39 if (FLAG_trace_turbo_graph) { |
39 OFStream os(stdout); | 40 OFStream os(stdout); |
40 os << "-- Graph before control reduction" << std::endl; | 41 os << "-- Graph before control reduction" << std::endl; |
41 os << AsRPO(*graph()); | 42 os << AsRPO(*graph()); |
42 } | 43 } |
43 ControlReducer::ReduceGraph(zone(), jsgraph(), common()); | 44 ControlReducer::ReduceGraph(zone(), jsgraph(), common(), |
| 45 max_phis_for_select); |
44 if (FLAG_trace_turbo_graph) { | 46 if (FLAG_trace_turbo_graph) { |
45 OFStream os(stdout); | 47 OFStream os(stdout); |
46 os << "-- Graph after control reduction" << std::endl; | 48 os << "-- Graph after control reduction" << std::endl; |
47 os << AsRPO(*graph()); | 49 os << AsRPO(*graph()); |
48 } | 50 } |
49 } | 51 } |
50 | 52 |
51 JSGraph* jsgraph() { return &jsgraph_; } | 53 JSGraph* jsgraph() { return &jsgraph_; } |
52 }; | 54 }; |
53 | 55 |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 274 |
273 ReduceGraph(); | 275 ReduceGraph(); |
274 | 276 |
275 // Diamond should be folded away. | 277 // Diamond should be folded away. |
276 EXPECT_THAT( | 278 EXPECT_THAT( |
277 graph()->end(), | 279 graph()->end(), |
278 IsEnd(IsReturn(IsInt32Constant(11), graph()->start(), graph()->start()))); | 280 IsEnd(IsReturn(IsInt32Constant(11), graph()->start(), graph()->start()))); |
279 } | 281 } |
280 | 282 |
281 | 283 |
| 284 TEST_F(ControlReducerTest, SelectPhi) { |
| 285 Node* p0 = Parameter(0); |
| 286 const MachineType kType = kMachInt32; |
| 287 Diamond d(graph(), common(), p0); |
| 288 Node* phi = |
| 289 d.Phi(kType, jsgraph()->Int32Constant(1), jsgraph()->Int32Constant(2)); |
| 290 |
| 291 Node* ret = |
| 292 graph()->NewNode(common()->Return(), phi, graph()->start(), d.merge); |
| 293 graph()->end()->ReplaceInput(0, ret); |
| 294 |
| 295 ReduceGraph(1); |
| 296 |
| 297 // Phi should be replaced with a select. |
| 298 EXPECT_THAT(graph()->end(), |
| 299 IsEnd(IsReturn( |
| 300 IsSelect(kType, p0, IsInt32Constant(1), IsInt32Constant(2)), |
| 301 graph()->start(), graph()->start()))); |
| 302 } |
| 303 |
| 304 |
| 305 TEST_F(ControlReducerTest, SelectPhis_fail) { |
| 306 Node* p0 = Parameter(0); |
| 307 const MachineType kType = kMachInt32; |
| 308 Diamond d(graph(), common(), p0); |
| 309 Node* phi = |
| 310 d.Phi(kType, jsgraph()->Int32Constant(1), jsgraph()->Int32Constant(2)); |
| 311 Node* phi2 = |
| 312 d.Phi(kType, jsgraph()->Int32Constant(11), jsgraph()->Int32Constant(22)); |
| 313 USE(phi2); |
| 314 Node* ret = |
| 315 graph()->NewNode(common()->Return(), phi, graph()->start(), d.merge); |
| 316 graph()->end()->ReplaceInput(0, ret); |
| 317 |
| 318 ReduceGraph(1); |
| 319 |
| 320 // Diamond should not be replaced with a select (too many phis). |
| 321 EXPECT_THAT(ret, IsReturn(phi, graph()->start(), d.merge)); |
| 322 EXPECT_THAT(graph()->end(), IsEnd(ret)); |
| 323 } |
| 324 |
| 325 |
| 326 TEST_F(ControlReducerTest, SelectTwoPhis) { |
| 327 Node* p0 = Parameter(0); |
| 328 const MachineType kType = kMachInt32; |
| 329 Diamond d(graph(), common(), p0); |
| 330 Node* phi1 = |
| 331 d.Phi(kType, jsgraph()->Int32Constant(1), jsgraph()->Int32Constant(2)); |
| 332 Node* phi2 = |
| 333 d.Phi(kType, jsgraph()->Int32Constant(2), jsgraph()->Int32Constant(3)); |
| 334 MachineOperatorBuilder machine(zone()); |
| 335 Node* add = graph()->NewNode(machine.Int32Add(), phi1, phi2); |
| 336 Node* ret = |
| 337 graph()->NewNode(common()->Return(), add, graph()->start(), d.merge); |
| 338 graph()->end()->ReplaceInput(0, ret); |
| 339 |
| 340 ReduceGraph(2); |
| 341 |
| 342 // Phis should be replaced with two selects. |
| 343 EXPECT_THAT( |
| 344 ret, |
| 345 IsReturn(IsInt32Add( |
| 346 IsSelect(kType, p0, IsInt32Constant(1), IsInt32Constant(2)), |
| 347 IsSelect(kType, p0, IsInt32Constant(2), IsInt32Constant(3))), |
| 348 graph()->start(), graph()->start())); |
| 349 EXPECT_THAT(graph()->end(), IsEnd(ret)); |
| 350 } |
| 351 |
| 352 |
282 } // namespace compiler | 353 } // namespace compiler |
283 } // namespace internal | 354 } // namespace internal |
284 } // namespace v8 | 355 } // namespace v8 |
OLD | NEW |