| 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-flow-optimizer.h" | 5 #include "src/compiler/control-flow-optimizer.h" |
| 6 #include "src/compiler/js-graph.h" | 6 #include "src/compiler/js-graph.h" |
| 7 #include "src/compiler/js-operator.h" | 7 #include "src/compiler/js-operator.h" |
| 8 #include "src/compiler/machine-operator.h" | 8 #include "src/compiler/machine-operator.h" |
| 9 #include "test/unittests/compiler/graph-unittest.h" | 9 #include "test/unittests/compiler/graph-unittest.h" |
| 10 #include "test/unittests/compiler/node-test-utils.h" | 10 #include "test/unittests/compiler/node-test-utils.h" |
| 11 #include "testing/gmock-support.h" | 11 #include "testing/gmock-support.h" |
| 12 | 12 |
| 13 using testing::AllOf; | 13 using testing::AllOf; |
| 14 using testing::Capture; | 14 using testing::Capture; |
| 15 using testing::CaptureEq; | 15 using testing::CaptureEq; |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace internal { | 18 namespace internal { |
| 19 namespace compiler { | 19 namespace compiler { |
| 20 | 20 |
| 21 class ControlFlowOptimizerTest : public GraphTest { | 21 class ControlFlowOptimizerTest : public GraphTest { |
| 22 public: | 22 public: |
| 23 explicit ControlFlowOptimizerTest(int num_parameters = 3) | 23 explicit ControlFlowOptimizerTest(int num_parameters = 3) |
| 24 : GraphTest(num_parameters), machine_(zone()) {} | 24 : GraphTest(num_parameters), |
| 25 machine_(zone()), |
| 26 javascript_(zone()), |
| 27 jsgraph_(isolate(), graph(), common(), javascript(), machine()) {} |
| 25 ~ControlFlowOptimizerTest() OVERRIDE {} | 28 ~ControlFlowOptimizerTest() OVERRIDE {} |
| 26 | 29 |
| 27 protected: | 30 protected: |
| 28 void Optimize() { | 31 void Optimize() { |
| 29 JSOperatorBuilder javascript(zone()); | 32 ControlFlowOptimizer optimizer(jsgraph(), zone()); |
| 30 JSGraph jsgraph(isolate(), graph(), common(), &javascript, machine()); | |
| 31 ControlFlowOptimizer optimizer(&jsgraph, zone()); | |
| 32 optimizer.Optimize(); | 33 optimizer.Optimize(); |
| 33 } | 34 } |
| 34 | 35 |
| 36 Node* EmptyFrameState() { return jsgraph()->EmptyFrameState(); } |
| 37 |
| 38 JSGraph* jsgraph() { return &jsgraph_; } |
| 39 JSOperatorBuilder* javascript() { return &javascript_; } |
| 35 MachineOperatorBuilder* machine() { return &machine_; } | 40 MachineOperatorBuilder* machine() { return &machine_; } |
| 36 | 41 |
| 37 private: | 42 private: |
| 38 MachineOperatorBuilder machine_; | 43 MachineOperatorBuilder machine_; |
| 44 JSOperatorBuilder javascript_; |
| 45 JSGraph jsgraph_; |
| 39 }; | 46 }; |
| 40 | 47 |
| 41 | 48 |
| 42 TEST_F(ControlFlowOptimizerTest, BuildSwitch) { | 49 TEST_F(ControlFlowOptimizerTest, BuildSwitch1) { |
| 43 Node* index = Parameter(0); | 50 Node* index = Parameter(0); |
| 44 Node* branch0 = graph()->NewNode( | 51 Node* branch0 = graph()->NewNode( |
| 45 common()->Branch(), | 52 common()->Branch(), |
| 46 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)), | 53 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)), |
| 47 start()); | 54 start()); |
| 48 Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0); | 55 Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0); |
| 49 Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0); | 56 Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0); |
| 50 Node* branch1 = graph()->NewNode( | 57 Node* branch1 = graph()->NewNode( |
| 51 common()->Branch(), | 58 common()->Branch(), |
| 52 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)), | 59 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)), |
| 53 if_false0); | 60 if_false0); |
| 54 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); | 61 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); |
| 55 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); | 62 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); |
| 56 Node* merge = | 63 Node* merge = |
| 57 graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1); | 64 graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1); |
| 58 graph()->SetEnd(graph()->NewNode(common()->End(), merge)); | 65 graph()->SetEnd(graph()->NewNode(common()->End(), merge)); |
| 59 Optimize(); | 66 Optimize(); |
| 60 Capture<Node*> switch_capture; | 67 Capture<Node*> switch_capture; |
| 61 EXPECT_THAT(end(), | 68 EXPECT_THAT(end(), |
| 62 IsEnd(IsMerge(IsIfValue(0, CaptureEq(&switch_capture)), | 69 IsEnd(IsMerge(IsIfValue(0, CaptureEq(&switch_capture)), |
| 63 IsIfValue(1, CaptureEq(&switch_capture)), | 70 IsIfValue(1, CaptureEq(&switch_capture)), |
| 64 IsIfDefault(AllOf(CaptureEq(&switch_capture), | 71 IsIfDefault(AllOf(CaptureEq(&switch_capture), |
| 65 IsSwitch(index, start())))))); | 72 IsSwitch(index, start())))))); |
| 66 } | 73 } |
| 67 | 74 |
| 68 | 75 |
| 76 TEST_F(ControlFlowOptimizerTest, BuildSwitch2) { |
| 77 Node* input = Parameter(0); |
| 78 Node* context = Parameter(1); |
| 79 Node* index = FLAG_turbo_deoptimization |
| 80 ? graph()->NewNode(javascript()->ToNumber(), input, context, |
| 81 EmptyFrameState(), start(), start()) |
| 82 : graph()->NewNode(javascript()->ToNumber(), input, context, |
| 83 start(), start()); |
| 84 Node* if_success = graph()->NewNode(common()->IfSuccess(), index); |
| 85 Node* branch0 = graph()->NewNode( |
| 86 common()->Branch(), |
| 87 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)), |
| 88 if_success); |
| 89 Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0); |
| 90 Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0); |
| 91 Node* branch1 = graph()->NewNode( |
| 92 common()->Branch(), |
| 93 graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)), |
| 94 if_false0); |
| 95 Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1); |
| 96 Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1); |
| 97 Node* merge = |
| 98 graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1); |
| 99 graph()->SetEnd(graph()->NewNode(common()->End(), merge)); |
| 100 Optimize(); |
| 101 Capture<Node*> switch_capture; |
| 102 EXPECT_THAT( |
| 103 end(), |
| 104 IsEnd(IsMerge(IsIfValue(0, CaptureEq(&switch_capture)), |
| 105 IsIfValue(1, CaptureEq(&switch_capture)), |
| 106 IsIfDefault(AllOf(CaptureEq(&switch_capture), |
| 107 IsSwitch(index, IsIfSuccess(index))))))); |
| 108 } |
| 109 |
| 110 |
| 69 TEST_F(ControlFlowOptimizerTest, CloneBranch) { | 111 TEST_F(ControlFlowOptimizerTest, CloneBranch) { |
| 70 Node* cond0 = Parameter(0); | 112 Node* cond0 = Parameter(0); |
| 71 Node* cond1 = Parameter(1); | 113 Node* cond1 = Parameter(1); |
| 72 Node* cond2 = Parameter(2); | 114 Node* cond2 = Parameter(2); |
| 73 Node* branch0 = graph()->NewNode(common()->Branch(), cond0, start()); | 115 Node* branch0 = graph()->NewNode(common()->Branch(), cond0, start()); |
| 74 Node* control1 = graph()->NewNode(common()->IfTrue(), branch0); | 116 Node* control1 = graph()->NewNode(common()->IfTrue(), branch0); |
| 75 Node* control2 = graph()->NewNode(common()->IfFalse(), branch0); | 117 Node* control2 = graph()->NewNode(common()->IfFalse(), branch0); |
| 76 Node* merge0 = graph()->NewNode(common()->Merge(2), control1, control2); | 118 Node* merge0 = graph()->NewNode(common()->Merge(2), control1, control2); |
| 77 Node* phi0 = | 119 Node* phi0 = |
| 78 graph()->NewNode(common()->Phi(kRepBit, 2), cond1, cond2, merge0); | 120 graph()->NewNode(common()->Phi(kRepBit, 2), cond1, cond2, merge0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 89 IsIfTrue(CaptureEq(&branch2_capture))), | 131 IsIfTrue(CaptureEq(&branch2_capture))), |
| 90 IsMerge(IsIfFalse(AllOf(CaptureEq(&branch1_capture), | 132 IsMerge(IsIfFalse(AllOf(CaptureEq(&branch1_capture), |
| 91 IsBranch(cond1, control1))), | 133 IsBranch(cond1, control1))), |
| 92 IsIfFalse(AllOf(CaptureEq(&branch2_capture), | 134 IsIfFalse(AllOf(CaptureEq(&branch2_capture), |
| 93 IsBranch(cond2, control2))))))); | 135 IsBranch(cond2, control2))))))); |
| 94 } | 136 } |
| 95 | 137 |
| 96 } // namespace compiler | 138 } // namespace compiler |
| 97 } // namespace internal | 139 } // namespace internal |
| 98 } // namespace v8 | 140 } // namespace v8 |
| OLD | NEW |