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/branch-elimination.h" | 5 #include "src/compiler/branch-elimination.h" |
6 #include "src/compiler/js-graph.h" | 6 #include "src/compiler/js-graph.h" |
7 #include "src/compiler/linkage.h" | 7 #include "src/compiler/linkage.h" |
8 #include "src/compiler/node-properties.h" | 8 #include "src/compiler/node-properties.h" |
9 #include "test/unittests/compiler/compiler-test-utils.h" | 9 #include "test/unittests/compiler/compiler-test-utils.h" |
10 #include "test/unittests/compiler/graph-unittest.h" | 10 #include "test/unittests/compiler/graph-unittest.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), | 58 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), |
59 Int32Constant(1), Int32Constant(2), inner_merge); | 59 Int32Constant(1), Int32Constant(2), inner_merge); |
60 | 60 |
61 Node* outer_if_false = graph()->NewNode(common()->IfFalse(), outer_branch); | 61 Node* outer_if_false = graph()->NewNode(common()->IfFalse(), outer_branch); |
62 Node* outer_merge = | 62 Node* outer_merge = |
63 graph()->NewNode(common()->Merge(2), inner_merge, outer_if_false); | 63 graph()->NewNode(common()->Merge(2), inner_merge, outer_if_false); |
64 Node* outer_phi = | 64 Node* outer_phi = |
65 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), | 65 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), |
66 inner_phi, Int32Constant(3), outer_merge); | 66 inner_phi, Int32Constant(3), outer_merge); |
67 | 67 |
68 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 68 Node* ret = graph()->NewNode(common()->Return(), outer_phi, graph()->start(), |
69 Node* ret = graph()->NewNode(common()->Return(), zero, outer_phi, | 69 outer_merge); |
70 graph()->start(), outer_merge); | |
71 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); | 70 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); |
72 | 71 |
73 Reduce(); | 72 Reduce(); |
74 | 73 |
75 // Outer branch should not be rewritten, the inner branch should be discarded. | 74 // Outer branch should not be rewritten, the inner branch should be discarded. |
76 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); | 75 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); |
77 EXPECT_THAT(inner_phi, | 76 EXPECT_THAT(inner_phi, |
78 IsPhi(MachineRepresentation::kWord32, IsInt32Constant(1), | 77 IsPhi(MachineRepresentation::kWord32, IsInt32Constant(1), |
79 IsInt32Constant(2), IsMerge(outer_if_true, IsDead()))); | 78 IsInt32Constant(2), IsMerge(outer_if_true, IsDead()))); |
80 } | 79 } |
(...skipping 19 matching lines...) Expand all Loading... |
100 Node* inner_phi = | 99 Node* inner_phi = |
101 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), | 100 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), |
102 Int32Constant(2), Int32Constant(3), inner_merge); | 101 Int32Constant(2), Int32Constant(3), inner_merge); |
103 | 102 |
104 Node* outer_merge = | 103 Node* outer_merge = |
105 graph()->NewNode(common()->Merge(2), outer_if_true, inner_merge); | 104 graph()->NewNode(common()->Merge(2), outer_if_true, inner_merge); |
106 Node* outer_phi = | 105 Node* outer_phi = |
107 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), | 106 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), |
108 Int32Constant(1), inner_phi, outer_merge); | 107 Int32Constant(1), inner_phi, outer_merge); |
109 | 108 |
110 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 109 Node* ret = graph()->NewNode(common()->Return(), outer_phi, graph()->start(), |
111 Node* ret = graph()->NewNode(common()->Return(), zero, outer_phi, | 110 outer_merge); |
112 graph()->start(), outer_merge); | |
113 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); | 111 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); |
114 | 112 |
115 Reduce(); | 113 Reduce(); |
116 | 114 |
117 // Outer branch should not be rewritten, the inner branch should be discarded. | 115 // Outer branch should not be rewritten, the inner branch should be discarded. |
118 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); | 116 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); |
119 EXPECT_THAT(inner_phi, | 117 EXPECT_THAT(inner_phi, |
120 IsPhi(MachineRepresentation::kWord32, IsInt32Constant(2), | 118 IsPhi(MachineRepresentation::kWord32, IsInt32Constant(2), |
121 IsInt32Constant(3), IsMerge(IsDead(), outer_if_false))); | 119 IsInt32Constant(3), IsMerge(IsDead(), outer_if_false))); |
122 } | 120 } |
(...skipping 16 matching lines...) Expand all Loading... |
139 Node* branch2 = graph()->NewNode(common()->Branch(), condition, merge1); | 137 Node* branch2 = graph()->NewNode(common()->Branch(), condition, merge1); |
140 Node* if_true2 = graph()->NewNode(common()->IfTrue(), branch2); | 138 Node* if_true2 = graph()->NewNode(common()->IfTrue(), branch2); |
141 Node* if_false2 = graph()->NewNode(common()->IfFalse(), branch2); | 139 Node* if_false2 = graph()->NewNode(common()->IfFalse(), branch2); |
142 Node* merge2 = graph()->NewNode(common()->Merge(2), if_true2, if_false2); | 140 Node* merge2 = graph()->NewNode(common()->Merge(2), if_true2, if_false2); |
143 Node* phi2 = | 141 Node* phi2 = |
144 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), | 142 graph()->NewNode(common()->Phi(MachineRepresentation::kWord32, 2), |
145 Int32Constant(3), Int32Constant(4), merge1); | 143 Int32Constant(3), Int32Constant(4), merge1); |
146 | 144 |
147 | 145 |
148 Node* add = graph()->NewNode(machine()->Int32Add(), phi1, phi2); | 146 Node* add = graph()->NewNode(machine()->Int32Add(), phi1, phi2); |
149 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | |
150 Node* ret = | 147 Node* ret = |
151 graph()->NewNode(common()->Return(), zero, add, graph()->start(), merge2); | 148 graph()->NewNode(common()->Return(), add, graph()->start(), merge2); |
152 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); | 149 graph()->SetEnd(graph()->NewNode(common()->End(1), ret)); |
153 | 150 |
154 Reduce(); | 151 Reduce(); |
155 | 152 |
156 // Outer branch should not be rewritten, the inner branch condition should | 153 // Outer branch should not be rewritten, the inner branch condition should |
157 // be true. | 154 // be true. |
158 EXPECT_THAT(branch1, IsBranch(condition, graph()->start())); | 155 EXPECT_THAT(branch1, IsBranch(condition, graph()->start())); |
159 EXPECT_THAT(branch2, IsBranch(condition, merge1)); | 156 EXPECT_THAT(branch2, IsBranch(condition, merge1)); |
160 } | 157 } |
161 | 158 |
(...skipping 10 matching lines...) Expand all Loading... |
172 Node* outer_if_true = graph()->NewNode(common()->IfTrue(), outer_branch); | 169 Node* outer_if_true = graph()->NewNode(common()->IfTrue(), outer_branch); |
173 | 170 |
174 | 171 |
175 Node* loop = graph()->NewNode(common()->Loop(1), outer_if_true); | 172 Node* loop = graph()->NewNode(common()->Loop(1), outer_if_true); |
176 Node* effect = | 173 Node* effect = |
177 graph()->NewNode(common()->EffectPhi(1), graph()->start(), loop); | 174 graph()->NewNode(common()->EffectPhi(1), graph()->start(), loop); |
178 | 175 |
179 Node* inner_branch = graph()->NewNode(common()->Branch(), condition, loop); | 176 Node* inner_branch = graph()->NewNode(common()->Branch(), condition, loop); |
180 | 177 |
181 Node* inner_if_true = graph()->NewNode(common()->IfTrue(), inner_branch); | 178 Node* inner_if_true = graph()->NewNode(common()->IfTrue(), inner_branch); |
182 Node* zero = graph()->NewNode(common()->Int32Constant(0)); | 179 Node* ret1 = graph()->NewNode(common()->Return(), Int32Constant(2), effect, |
183 Node* ret1 = graph()->NewNode(common()->Return(), zero, Int32Constant(2), | 180 inner_if_true); |
184 effect, inner_if_true); | |
185 | 181 |
186 Node* inner_if_false = graph()->NewNode(common()->IfFalse(), inner_branch); | 182 Node* inner_if_false = graph()->NewNode(common()->IfFalse(), inner_branch); |
187 loop->AppendInput(zone(), inner_if_false); | 183 loop->AppendInput(zone(), inner_if_false); |
188 NodeProperties::ChangeOp(loop, common()->Loop(2)); | 184 NodeProperties::ChangeOp(loop, common()->Loop(2)); |
189 effect->InsertInput(zone(), 1, effect); | 185 effect->InsertInput(zone(), 1, effect); |
190 NodeProperties::ChangeOp(effect, common()->EffectPhi(2)); | 186 NodeProperties::ChangeOp(effect, common()->EffectPhi(2)); |
191 | 187 |
192 Node* outer_if_false = graph()->NewNode(common()->IfFalse(), outer_branch); | 188 Node* outer_if_false = graph()->NewNode(common()->IfFalse(), outer_branch); |
193 Node* outer_merge = | 189 Node* outer_merge = |
194 graph()->NewNode(common()->Merge(2), loop, outer_if_false); | 190 graph()->NewNode(common()->Merge(2), loop, outer_if_false); |
195 Node* outer_ephi = graph()->NewNode(common()->EffectPhi(2), effect, | 191 Node* outer_ephi = graph()->NewNode(common()->EffectPhi(2), effect, |
196 graph()->start(), outer_merge); | 192 graph()->start(), outer_merge); |
197 | 193 |
198 Node* ret2 = graph()->NewNode(common()->Return(), zero, Int32Constant(1), | 194 Node* ret2 = graph()->NewNode(common()->Return(), Int32Constant(1), |
199 outer_ephi, outer_merge); | 195 outer_ephi, outer_merge); |
200 | 196 |
201 Node* terminate = graph()->NewNode(common()->Terminate(), effect, loop); | 197 Node* terminate = graph()->NewNode(common()->Terminate(), effect, loop); |
202 graph()->SetEnd(graph()->NewNode(common()->End(3), ret1, ret2, terminate)); | 198 graph()->SetEnd(graph()->NewNode(common()->End(3), ret1, ret2, terminate)); |
203 | 199 |
204 Reduce(); | 200 Reduce(); |
205 | 201 |
206 // Outer branch should not be rewritten, the inner branch should be discarded. | 202 // Outer branch should not be rewritten, the inner branch should be discarded. |
207 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); | 203 EXPECT_THAT(outer_branch, IsBranch(condition, graph()->start())); |
208 EXPECT_THAT(ret1, IsReturn(IsInt32Constant(2), effect, loop)); | 204 EXPECT_THAT(ret1, IsReturn(IsInt32Constant(2), effect, loop)); |
209 } | 205 } |
210 | 206 |
211 } // namespace compiler | 207 } // namespace compiler |
212 } // namespace internal | 208 } // namespace internal |
213 } // namespace v8 | 209 } // namespace v8 |
OLD | NEW |