| 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/diamond.h" |
| 7 #include "src/compiler/graph-visualizer.h" | 7 #include "src/compiler/graph-visualizer.h" |
| 8 #include "src/compiler/js-graph.h" | 8 #include "src/compiler/js-graph.h" |
| 9 #include "src/compiler/js-operator.h" | 9 #include "src/compiler/js-operator.h" |
| 10 #include "src/compiler/machine-operator.h" | 10 #include "src/compiler/machine-operator.h" |
| 11 #include "src/compiler/node.h" | 11 #include "src/compiler/node.h" |
| 12 #include "test/unittests/compiler/graph-unittest.h" | 12 #include "test/unittests/compiler/graph-unittest.h" |
| 13 #include "test/unittests/compiler/node-test-utils.h" | 13 #include "test/unittests/compiler/node-test-utils.h" |
| 14 #include "testing/gmock-support.h" | 14 #include "testing/gmock-support.h" |
| 15 | 15 |
| 16 using testing::_; | 16 using testing::_; |
| 17 using testing::AllOf; | 17 using testing::AllOf; |
| 18 using testing::Capture; | 18 using testing::Capture; |
| 19 using testing::CaptureEq; | 19 using testing::CaptureEq; |
| 20 | 20 |
| 21 namespace v8 { | 21 namespace v8 { |
| 22 namespace internal { | 22 namespace internal { |
| 23 namespace compiler { | 23 namespace compiler { |
| 24 | 24 |
| 25 class ControlReducerTest : public TypedGraphTest { | 25 class ControlReducerTest : public GraphTest { |
| 26 public: | 26 public: |
| 27 ControlReducerTest() | 27 ControlReducerTest() |
| 28 : TypedGraphTest(1), | 28 : GraphTest(1), |
| 29 machine_(zone()), | 29 machine_(zone()), |
| 30 javascript_(zone()), | 30 javascript_(zone()), |
| 31 jsgraph_(isolate(), graph(), common(), &javascript_, &machine_) {} | 31 jsgraph_(isolate(), graph(), common(), &javascript_, &machine_) {} |
| 32 | 32 |
| 33 protected: | 33 protected: |
| 34 MachineOperatorBuilder machine_; | 34 MachineOperatorBuilder machine_; |
| 35 JSOperatorBuilder javascript_; | 35 JSOperatorBuilder javascript_; |
| 36 JSGraph jsgraph_; | 36 JSGraph jsgraph_; |
| 37 | 37 |
| 38 void ReduceGraph(int max_phis_for_select = 0) { | 38 void ReduceGraph(int max_phis_for_select = 0) { |
| 39 if (FLAG_trace_turbo_graph) { | 39 if (FLAG_trace_turbo_graph) { |
| 40 OFStream os(stdout); | 40 OFStream os(stdout); |
| 41 os << "-- Graph before control reduction" << std::endl; | 41 os << "-- Graph before control reduction" << std::endl; |
| 42 os << AsRPO(*graph()); | 42 os << AsRPO(*graph()); |
| 43 } | 43 } |
| 44 ControlReducer::ReduceGraph(zone(), jsgraph(), max_phis_for_select); | 44 ControlReducer::ReduceGraph(zone(), jsgraph(), max_phis_for_select); |
| 45 if (FLAG_trace_turbo_graph) { | 45 if (FLAG_trace_turbo_graph) { |
| 46 OFStream os(stdout); | 46 OFStream os(stdout); |
| 47 os << "-- Graph after control reduction" << std::endl; | 47 os << "-- Graph after control reduction" << std::endl; |
| 48 os << AsRPO(*graph()); | 48 os << AsRPO(*graph()); |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 | 51 |
| 52 JSGraph* jsgraph() { return &jsgraph_; } | 52 JSGraph* jsgraph() { return &jsgraph_; } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 | 55 |
| 56 TEST_F(ControlReducerTest, PhiAsInputToBranch_true) { | |
| 57 Node* p0 = Parameter(0); | |
| 58 Node* branch1 = graph()->NewNode(common()->Branch(), p0, graph()->start()); | |
| 59 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); | |
| 60 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); | |
| 61 Node* merge1 = graph()->NewNode(common()->Merge(2), if_true1, if_false1); | |
| 62 Node* phi1 = graph()->NewNode(common()->Phi(kMachInt32, 2), | |
| 63 jsgraph()->Int32Constant(1), | |
| 64 jsgraph()->Int32Constant(2), merge1); | |
| 65 | |
| 66 Node* branch2 = graph()->NewNode(common()->Branch(), phi1, merge1); | |
| 67 Node* if_true2 = graph()->NewNode(common()->IfTrue(), branch2); | |
| 68 Node* if_false2 = graph()->NewNode(common()->IfFalse(), branch2); | |
| 69 Node* merge2 = graph()->NewNode(common()->Merge(2), if_true2, if_false2); | |
| 70 Node* result = graph()->NewNode(common()->Phi(kMachInt32, 2), | |
| 71 jsgraph()->Int32Constant(11), | |
| 72 jsgraph()->Int32Constant(22), merge2); | |
| 73 | |
| 74 Node* ret = | |
| 75 graph()->NewNode(common()->Return(), result, graph()->start(), merge2); | |
| 76 graph()->end()->ReplaceInput(0, ret); | |
| 77 | |
| 78 ReduceGraph(); | |
| 79 | |
| 80 // First diamond is not reduced. | |
| 81 EXPECT_THAT(merge1, IsMerge(IsIfTrue(branch1), IsIfFalse(branch1))); | |
| 82 | |
| 83 // Second diamond should be folded away. | |
| 84 EXPECT_THAT(graph()->end(), | |
| 85 IsEnd(IsReturn(IsInt32Constant(11), graph()->start(), merge1))); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 TEST_F(ControlReducerTest, PhiAsInputToBranch_false) { | |
| 90 Node* p0 = Parameter(0); | |
| 91 Node* branch1 = graph()->NewNode(common()->Branch(), p0, graph()->start()); | |
| 92 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); | |
| 93 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); | |
| 94 Node* merge1 = graph()->NewNode(common()->Merge(2), if_true1, if_false1); | |
| 95 Node* phi1 = graph()->NewNode(common()->Phi(kMachInt32, 2), | |
| 96 jsgraph()->Int32Constant(0), | |
| 97 jsgraph()->BooleanConstant(false), merge1); | |
| 98 | |
| 99 Node* branch2 = graph()->NewNode(common()->Branch(), phi1, merge1); | |
| 100 Node* if_true2 = graph()->NewNode(common()->IfTrue(), branch2); | |
| 101 Node* if_false2 = graph()->NewNode(common()->IfFalse(), branch2); | |
| 102 Node* merge2 = graph()->NewNode(common()->Merge(2), if_true2, if_false2); | |
| 103 Node* result = graph()->NewNode(common()->Phi(kMachInt32, 2), | |
| 104 jsgraph()->Int32Constant(11), | |
| 105 jsgraph()->Int32Constant(22), merge2); | |
| 106 | |
| 107 Node* ret = | |
| 108 graph()->NewNode(common()->Return(), result, graph()->start(), merge2); | |
| 109 graph()->end()->ReplaceInput(0, ret); | |
| 110 | |
| 111 ReduceGraph(); | |
| 112 | |
| 113 // First diamond is not reduced. | |
| 114 EXPECT_THAT(merge1, IsMerge(IsIfTrue(branch1), IsIfFalse(branch1))); | |
| 115 | |
| 116 // Second diamond should be folded away. | |
| 117 EXPECT_THAT(graph()->end(), | |
| 118 IsEnd(IsReturn(IsInt32Constant(22), graph()->start(), merge1))); | |
| 119 } | |
| 120 | |
| 121 | |
| 122 TEST_F(ControlReducerTest, PhiAsInputToBranch_unknown_true) { | |
| 123 Node* p0 = Parameter(0); | |
| 124 Node* phi0 = graph()->NewNode(common()->Phi(kMachInt32, 2), p0, | |
| 125 jsgraph()->Int32Constant(1), graph()->start()); | |
| 126 Node* branch1 = graph()->NewNode(common()->Branch(), phi0, graph()->start()); | |
| 127 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); | |
| 128 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); | |
| 129 Node* merge1 = graph()->NewNode(common()->Merge(2), if_true1, if_false1); | |
| 130 Node* phi1 = graph()->NewNode(common()->Phi(kMachInt32, 2), | |
| 131 jsgraph()->Int32Constant(111), | |
| 132 jsgraph()->Int32Constant(222), merge1); | |
| 133 | |
| 134 Node* ret = | |
| 135 graph()->NewNode(common()->Return(), phi1, graph()->start(), merge1); | |
| 136 graph()->end()->ReplaceInput(0, ret); | |
| 137 | |
| 138 ReduceGraph(); | |
| 139 | |
| 140 // Branch should not be folded. | |
| 141 EXPECT_THAT(phi1, | |
| 142 IsPhi(kMachInt32, IsInt32Constant(111), IsInt32Constant(222), | |
| 143 IsMerge(IsIfTrue(branch1), IsIfFalse(branch1)))); | |
| 144 EXPECT_THAT(graph()->end(), IsEnd(IsReturn(phi1, graph()->start(), merge1))); | |
| 145 } | |
| 146 | |
| 147 | |
| 148 TEST_F(ControlReducerTest, SelectPhi) { | 56 TEST_F(ControlReducerTest, SelectPhi) { |
| 149 Node* p0 = Parameter(0); | 57 Node* p0 = Parameter(0); |
| 150 const MachineType kType = kMachInt32; | 58 const MachineType kType = kMachInt32; |
| 151 Diamond d(graph(), common(), p0); | 59 Diamond d(graph(), common(), p0); |
| 152 Node* phi = | 60 Node* phi = |
| 153 d.Phi(kType, jsgraph()->Int32Constant(1), jsgraph()->Int32Constant(2)); | 61 d.Phi(kType, jsgraph()->Int32Constant(1), jsgraph()->Int32Constant(2)); |
| 154 | 62 |
| 155 Node* ret = | 63 Node* ret = |
| 156 graph()->NewNode(common()->Return(), phi, graph()->start(), d.merge); | 64 graph()->NewNode(common()->Return(), phi, graph()->start(), d.merge); |
| 157 graph()->end()->ReplaceInput(0, ret); | 65 graph()->end()->ReplaceInput(0, ret); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 IsReturn(IsInt32Add( | 117 IsReturn(IsInt32Add( |
| 210 IsSelect(kType, p0, IsInt32Constant(1), IsInt32Constant(2)), | 118 IsSelect(kType, p0, IsInt32Constant(1), IsInt32Constant(2)), |
| 211 IsSelect(kType, p0, IsInt32Constant(2), IsInt32Constant(3))), | 119 IsSelect(kType, p0, IsInt32Constant(2), IsInt32Constant(3))), |
| 212 graph()->start(), graph()->start())); | 120 graph()->start(), graph()->start())); |
| 213 EXPECT_THAT(graph()->end(), IsEnd(ret)); | 121 EXPECT_THAT(graph()->end(), IsEnd(ret)); |
| 214 } | 122 } |
| 215 | 123 |
| 216 } // namespace compiler | 124 } // namespace compiler |
| 217 } // namespace internal | 125 } // namespace internal |
| 218 } // namespace v8 | 126 } // namespace v8 |
| OLD | NEW |