Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/codegen.h" |
| 6 | #include "src/compiler/all-nodes.h" |
| 7 | #include "src/compiler/common-operator.h" |
| 8 | #include "src/compiler/diamond.h" |
| 9 | #include "src/compiler/graph.h" |
| 10 | #include "src/compiler/js-graph.h" |
| 11 | #include "src/compiler/js-operator.h" |
| 12 | #include "src/compiler/operator.h" |
| 13 | #include "src/compiler/osr.h" |
| 14 | #include "test/cctest/cctest.h" |
| 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | namespace compiler { |
| 19 | |
| 20 | // TODO(titzer): move this method to a common testing place. |
| 21 | |
| 22 | static int CheckInputs(Node* node, Node* i0 = NULL, Node* i1 = NULL, |
| 23 | Node* i2 = NULL, Node* i3 = NULL) { |
| 24 | int count = 4; |
| 25 | if (i3 == NULL) count = 3; |
| 26 | if (i2 == NULL) count = 2; |
| 27 | if (i1 == NULL) count = 1; |
| 28 | if (i0 == NULL) count = 0; |
| 29 | CHECK_EQ(count, node->InputCount()); |
| 30 | if (i0 != NULL) CHECK_EQ(i0, node->InputAt(0)); |
| 31 | if (i1 != NULL) CHECK_EQ(i1, node->InputAt(1)); |
| 32 | if (i2 != NULL) CHECK_EQ(i2, node->InputAt(2)); |
| 33 | if (i3 != NULL) CHECK_EQ(i3, node->InputAt(3)); |
| 34 | return count; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static Operator kIntLt(IrOpcode::kInt32LessThan, Operator::kPure, |
| 39 | "Int32LessThan", 2, 0, 0, 1, 0, 0); |
| 40 | static Operator kIntAdd(IrOpcode::kInt32Add, Operator::kPure, "Int32Add", 2, 0, |
| 41 | 0, 1, 0, 0); |
| 42 | |
| 43 | |
| 44 | static const int kMaxOsrValues = 10; |
| 45 | |
| 46 | class OsrDeconstructorTester : public HandleAndZoneScope { |
| 47 | public: |
| 48 | explicit OsrDeconstructorTester(int num_values) |
| 49 | : isolate(main_isolate()), |
| 50 | common(main_zone()), |
| 51 | graph(main_zone()), |
| 52 | jsgraph(main_isolate(), &graph, &common, nullptr, nullptr, nullptr), |
| 53 | start(graph.NewNode(common.Start(1))), |
| 54 | p0(graph.NewNode(common.Parameter(0), start)), |
| 55 | end(graph.NewNode(common.End(1), start)), |
| 56 | osr_normal_entry(graph.NewNode(common.OsrNormalEntry(), start, start)), |
| 57 | osr_loop_entry(graph.NewNode(common.OsrLoopEntry(), start, start)), |
| 58 | self(graph.NewNode(common.Int32Constant(0xaabbccdd))) { |
| 59 | CHECK(num_values <= kMaxOsrValues); |
| 60 | graph.SetStart(start); |
| 61 | for (int i = 0; i < num_values; i++) { |
| 62 | osr_values[i] = graph.NewNode(common.OsrValue(i), osr_loop_entry); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Isolate* isolate; |
| 67 | CommonOperatorBuilder common; |
| 68 | Graph graph; |
| 69 | JSGraph jsgraph; |
| 70 | Node* start; |
| 71 | Node* p0; |
| 72 | Node* end; |
| 73 | Node* osr_normal_entry; |
| 74 | Node* osr_loop_entry; |
| 75 | Node* self; |
| 76 | Node* osr_values[kMaxOsrValues]; |
| 77 | |
| 78 | Node* NewOsrPhi(Node* loop, Node* incoming, int osr_value, Node* back1 = NULL, |
| 79 | Node* back2 = NULL, Node* back3 = NULL) { |
| 80 | int count = 5; |
| 81 | if (back3 == NULL) count = 4; |
| 82 | if (back2 == NULL) count = 3; |
| 83 | if (back1 == NULL) count = 2; |
| 84 | CHECK_EQ(loop->InputCount(), count); |
| 85 | CHECK_EQ(osr_loop_entry, loop->InputAt(1)); |
| 86 | |
| 87 | Node* inputs[6]; |
| 88 | inputs[0] = incoming; |
| 89 | inputs[1] = osr_values[osr_value]; |
| 90 | if (count > 2) inputs[2] = back1; |
| 91 | if (count > 3) inputs[3] = back2; |
| 92 | if (count > 4) inputs[4] = back3; |
| 93 | inputs[count] = loop; |
| 94 | return graph.NewNode(common.Phi(MachineRepresentation::kTagged, count), |
| 95 | count + 1, inputs); |
| 96 | } |
| 97 | |
| 98 | Node* NewLoop(bool is_osr, int num_backedges, Node* entry = nullptr) { |
| 99 | if (entry == nullptr) entry = osr_normal_entry; |
| 100 | Node* loop = graph.NewNode(common.Loop(1), entry); |
| 101 | if (is_osr) { |
| 102 | loop->AppendInput(graph.zone(), osr_loop_entry); |
| 103 | } |
| 104 | for (int i = 0; i < num_backedges; i++) { |
| 105 | loop->AppendInput(graph.zone(), loop); |
| 106 | } |
| 107 | NodeProperties::ChangeOp(loop, common.Loop(loop->InputCount())); |
| 108 | return loop; |
| 109 | } |
| 110 | |
| 111 | Node* NewOsrLoop(int num_backedges, Node* entry = NULL) { |
| 112 | return NewLoop(true, num_backedges, entry); |
| 113 | } |
| 114 | |
| 115 | void DeconstructOsr() { |
| 116 | OsrHelper helper(0, 0); |
| 117 | helper.Deconstruct(&jsgraph, &common, main_zone()); |
| 118 | AllNodes nodes(main_zone(), &graph); |
| 119 | // Should be edited out. |
| 120 | CHECK(!nodes.IsLive(osr_normal_entry)); |
| 121 | CHECK(!nodes.IsLive(osr_loop_entry)); |
| 122 | // No dangling nodes should be left over. |
| 123 | for (Node* const node : nodes.live) { |
| 124 | for (Node* const use : node->uses()) { |
| 125 | CHECK(std::find(nodes.live.begin(), nodes.live.end(), use) != |
| 126 | nodes.live.end()); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | TEST(Deconstruct_osr0) { |
| 134 | OsrDeconstructorTester T(0); |
| 135 | |
| 136 | Node* loop = T.NewOsrLoop(1); |
| 137 | |
| 138 | T.graph.SetEnd(loop); |
| 139 | |
| 140 | T.DeconstructOsr(); |
| 141 | |
| 142 | CheckInputs(loop, T.start, loop); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | TEST(Deconstruct_osr1) { |
| 147 | OsrDeconstructorTester T(1); |
| 148 | |
| 149 | Node* loop = T.NewOsrLoop(1); |
| 150 | Node* osr_phi = |
| 151 | T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant()); |
| 152 | |
| 153 | Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, loop); |
| 154 | T.graph.SetEnd(ret); |
| 155 | |
| 156 | T.DeconstructOsr(); |
| 157 | |
| 158 | CheckInputs(loop, T.start, loop); |
| 159 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop); |
| 160 | CheckInputs(ret, osr_phi, T.start, loop); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | TEST(Deconstruct_osr_remove_prologue) { |
| 165 | OsrDeconstructorTester T(1); |
| 166 | Diamond d(&T.graph, &T.common, T.p0); |
| 167 | d.Chain(T.osr_normal_entry); |
| 168 | |
| 169 | Node* loop = T.NewOsrLoop(1, d.merge); |
| 170 | Node* osr_phi = |
| 171 | T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant()); |
| 172 | |
| 173 | Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, loop); |
| 174 | T.graph.SetEnd(ret); |
| 175 | |
| 176 | T.DeconstructOsr(); |
| 177 | |
| 178 | CheckInputs(loop, T.start, loop); |
| 179 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop); |
| 180 | CheckInputs(ret, osr_phi, T.start, loop); |
| 181 | |
| 182 | // The control before the loop should have been removed. |
| 183 | AllNodes nodes(T.main_zone(), &T.graph); |
| 184 | CHECK(!nodes.IsLive(d.branch)); |
| 185 | CHECK(!nodes.IsLive(d.if_true)); |
| 186 | CHECK(!nodes.IsLive(d.if_false)); |
| 187 | CHECK(!nodes.IsLive(d.merge)); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | TEST(Deconstruct_osr_with_body1) { |
| 192 | OsrDeconstructorTester T(1); |
| 193 | |
| 194 | Node* loop = T.NewOsrLoop(1); |
| 195 | |
| 196 | Node* branch = T.graph.NewNode(T.common.Branch(), T.p0, loop); |
| 197 | Node* if_true = T.graph.NewNode(T.common.IfTrue(), branch); |
| 198 | Node* if_false = T.graph.NewNode(T.common.IfFalse(), branch); |
| 199 | loop->ReplaceInput(2, if_true); |
| 200 | |
| 201 | Node* osr_phi = |
| 202 | T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant()); |
| 203 | |
| 204 | Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, if_false); |
| 205 | T.graph.SetEnd(ret); |
| 206 | |
| 207 | T.DeconstructOsr(); |
| 208 | |
| 209 | CheckInputs(loop, T.start, if_true); |
| 210 | CheckInputs(branch, T.p0, loop); |
| 211 | CheckInputs(if_true, branch); |
| 212 | CheckInputs(if_false, branch); |
| 213 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop); |
| 214 | CheckInputs(ret, osr_phi, T.start, if_false); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | TEST(Deconstruct_osr_with_body2) { |
| 219 | OsrDeconstructorTester T(1); |
| 220 | |
| 221 | Node* loop = T.NewOsrLoop(1); |
| 222 | |
| 223 | // Two chained branches in the the body of the loop. |
| 224 | Node* branch1 = T.graph.NewNode(T.common.Branch(), T.p0, loop); |
| 225 | Node* if_true1 = T.graph.NewNode(T.common.IfTrue(), branch1); |
| 226 | Node* if_false1 = T.graph.NewNode(T.common.IfFalse(), branch1); |
| 227 | |
| 228 | Node* branch2 = T.graph.NewNode(T.common.Branch(), T.p0, if_true1); |
| 229 | Node* if_true2 = T.graph.NewNode(T.common.IfTrue(), branch2); |
| 230 | Node* if_false2 = T.graph.NewNode(T.common.IfFalse(), branch2); |
| 231 | loop->ReplaceInput(2, if_true2); |
| 232 | |
| 233 | Node* osr_phi = |
| 234 | T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant()); |
| 235 | |
| 236 | Node* merge = T.graph.NewNode(T.common.Merge(2), if_false1, if_false2); |
| 237 | Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, merge); |
| 238 | T.graph.SetEnd(ret); |
| 239 | |
| 240 | T.DeconstructOsr(); |
| 241 | |
| 242 | CheckInputs(loop, T.start, if_true2); |
| 243 | CheckInputs(branch1, T.p0, loop); |
| 244 | CheckInputs(branch2, T.p0, if_true1); |
| 245 | CheckInputs(if_true1, branch1); |
| 246 | CheckInputs(if_false1, branch1); |
| 247 | CheckInputs(if_true2, branch2); |
| 248 | CheckInputs(if_false2, branch2); |
| 249 | |
| 250 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop); |
| 251 | CheckInputs(ret, osr_phi, T.start, merge); |
| 252 | CheckInputs(merge, if_false1, if_false2); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | TEST(Deconstruct_osr_with_body3) { |
| 257 | OsrDeconstructorTester T(1); |
| 258 | |
| 259 | Node* loop = T.NewOsrLoop(2); |
| 260 | |
| 261 | // Two branches that create two different backedges. |
| 262 | Node* branch1 = T.graph.NewNode(T.common.Branch(), T.p0, loop); |
| 263 | Node* if_true1 = T.graph.NewNode(T.common.IfTrue(), branch1); |
| 264 | Node* if_false1 = T.graph.NewNode(T.common.IfFalse(), branch1); |
| 265 | |
| 266 | Node* branch2 = T.graph.NewNode(T.common.Branch(), T.p0, if_true1); |
| 267 | Node* if_true2 = T.graph.NewNode(T.common.IfTrue(), branch2); |
| 268 | Node* if_false2 = T.graph.NewNode(T.common.IfFalse(), branch2); |
| 269 | loop->ReplaceInput(2, if_false1); |
| 270 | loop->ReplaceInput(3, if_true2); |
| 271 | |
| 272 | Node* osr_phi = |
| 273 | T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant(), |
| 274 | T.jsgraph.ZeroConstant()); |
| 275 | |
| 276 | Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, if_false2); |
| 277 | T.graph.SetEnd(ret); |
| 278 | |
| 279 | T.DeconstructOsr(); |
| 280 | |
| 281 | CheckInputs(loop, T.start, if_false1, if_true2); |
| 282 | CheckInputs(branch1, T.p0, loop); |
| 283 | CheckInputs(branch2, T.p0, if_true1); |
| 284 | CheckInputs(if_true1, branch1); |
| 285 | CheckInputs(if_false1, branch1); |
| 286 | CheckInputs(if_true2, branch2); |
| 287 | CheckInputs(if_false2, branch2); |
| 288 | |
| 289 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), |
| 290 | T.jsgraph.ZeroConstant(), loop); |
| 291 | CheckInputs(ret, osr_phi, T.start, if_false2); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | struct While { |
| 296 | OsrDeconstructorTester& t; |
| 297 | Node* branch; |
| 298 | Node* if_true; |
| 299 | Node* exit; |
| 300 | Node* loop; |
| 301 | |
| 302 | While(OsrDeconstructorTester& R, Node* cond, bool is_osr, int backedges = 1) |
| 303 | : t(R) { |
| 304 | loop = t.NewLoop(is_osr, backedges); |
| 305 | branch = t.graph.NewNode(t.common.Branch(), cond, loop); |
| 306 | if_true = t.graph.NewNode(t.common.IfTrue(), branch); |
| 307 | exit = t.graph.NewNode(t.common.IfFalse(), branch); |
| 308 | loop->ReplaceInput(loop->InputCount() - 1, if_true); |
| 309 | } |
| 310 | |
| 311 | void Nest(While& that) { |
| 312 | that.loop->ReplaceInput(that.loop->InputCount() - 1, exit); |
| 313 | this->loop->ReplaceInput(0, that.if_true); |
| 314 | } |
| 315 | |
| 316 | Node* Phi(Node* i1, Node* i2, Node* i3) { |
| 317 | if (loop->InputCount() == 2) { |
| 318 | return t.graph.NewNode(t.common.Phi(MachineRepresentation::kTagged, 2), |
| 319 | i1, i2, loop); |
| 320 | } else { |
| 321 | return t.graph.NewNode(t.common.Phi(MachineRepresentation::kTagged, 3), |
| 322 | i1, i2, i3, loop); |
| 323 | } |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | static Node* FindSuccessor(Node* node, IrOpcode::Value opcode) { |
| 329 | for (Node* use : node->uses()) { |
| 330 | if (use->opcode() == opcode) return use; |
| 331 | } |
| 332 | UNREACHABLE(); // should have been found. |
| 333 | return nullptr; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | TEST(Deconstruct_osr_nested1) { |
| 338 | OsrDeconstructorTester T(1); |
| 339 | |
| 340 | While outer(T, T.p0, false); |
| 341 | While inner(T, T.p0, true); |
| 342 | inner.Nest(outer); |
| 343 | |
| 344 | Node* outer_phi = outer.Phi(T.p0, T.p0, nullptr); |
| 345 | outer.branch->ReplaceInput(0, outer_phi); |
| 346 | |
| 347 | Node* osr_phi = inner.Phi(T.jsgraph.TrueConstant(), T.osr_values[0], |
| 348 | T.jsgraph.FalseConstant()); |
| 349 | inner.branch->ReplaceInput(0, osr_phi); |
| 350 | outer_phi->ReplaceInput(1, osr_phi); |
| 351 | |
| 352 | Node* ret = |
| 353 | T.graph.NewNode(T.common.Return(), outer_phi, T.start, outer.exit); |
| 354 | Node* end = T.graph.NewNode(T.common.End(1), ret); |
| 355 | T.graph.SetEnd(end); |
| 356 | |
| 357 | T.DeconstructOsr(); |
| 358 | |
| 359 | // Check structure of deconstructed graph. |
| 360 | // Check inner OSR loop is directly connected to start. |
| 361 | CheckInputs(inner.loop, T.start, inner.if_true); |
| 362 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.FalseConstant(), inner.loop); |
| 363 | |
| 364 | // Check control transfer to copy of outer loop. |
| 365 | Node* new_outer_loop = FindSuccessor(inner.exit, IrOpcode::kLoop); |
| 366 | Node* new_outer_phi = FindSuccessor(new_outer_loop, IrOpcode::kPhi); |
| 367 | CHECK_NE(new_outer_loop, outer.loop); |
| 368 | CHECK_NE(new_outer_phi, outer_phi); |
| 369 | |
| 370 | CheckInputs(new_outer_loop, inner.exit, new_outer_loop->InputAt(1)); |
| 371 | |
| 372 | // Check structure of outer loop. |
| 373 | Node* new_outer_branch = FindSuccessor(new_outer_loop, IrOpcode::kBranch); |
| 374 | CHECK_NE(new_outer_branch, outer.branch); |
| 375 | CheckInputs(new_outer_branch, new_outer_phi, new_outer_loop); |
| 376 | Node* new_outer_exit = FindSuccessor(new_outer_branch, IrOpcode::kIfFalse); |
| 377 | Node* new_outer_if_true = FindSuccessor(new_outer_branch, IrOpcode::kIfTrue); |
| 378 | |
| 379 | // Check structure of return. |
| 380 | end = T.graph.end(); |
| 381 | Node* new_ret = end->InputAt(0); |
| 382 | CHECK_EQ(IrOpcode::kReturn, new_ret->opcode()); |
| 383 | CheckInputs(new_ret, new_outer_phi, T.start, new_outer_exit); |
| 384 | |
| 385 | // Check structure of inner loop. |
| 386 | Node* new_inner_loop = FindSuccessor(new_outer_if_true, IrOpcode::kLoop); |
| 387 | Node* new_inner_phi = FindSuccessor(new_inner_loop, IrOpcode::kPhi); |
| 388 | |
| 389 | CheckInputs(new_inner_phi, T.jsgraph.TrueConstant(), |
| 390 | T.jsgraph.FalseConstant(), new_inner_loop); |
| 391 | CheckInputs(new_outer_phi, osr_phi, new_inner_phi, new_outer_loop); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | TEST(Deconstruct_osr_nested2) { |
| 396 | OsrDeconstructorTester T(1); |
| 397 | |
| 398 | // Test multiple backedge outer loop. |
| 399 | While outer(T, T.p0, false, 2); |
| 400 | While inner(T, T.p0, true); |
| 401 | inner.Nest(outer); |
| 402 | |
| 403 | Node* outer_phi = outer.Phi(T.p0, T.p0, T.p0); |
| 404 | outer.branch->ReplaceInput(0, outer_phi); |
| 405 | |
| 406 | Node* osr_phi = inner.Phi(T.jsgraph.TrueConstant(), T.osr_values[0], |
| 407 | T.jsgraph.FalseConstant()); |
| 408 | inner.branch->ReplaceInput(0, osr_phi); |
| 409 | outer_phi->ReplaceInput(1, osr_phi); |
| 410 | outer_phi->ReplaceInput(2, T.jsgraph.FalseConstant()); |
| 411 | |
| 412 | Node* x_branch = T.graph.NewNode(T.common.Branch(), osr_phi, inner.exit); |
| 413 | Node* x_true = T.graph.NewNode(T.common.IfTrue(), x_branch); |
| 414 | Node* x_false = T.graph.NewNode(T.common.IfFalse(), x_branch); |
| 415 | |
| 416 | outer.loop->ReplaceInput(1, x_true); |
| 417 | outer.loop->ReplaceInput(2, x_false); |
| 418 | |
| 419 | Node* ret = |
| 420 | T.graph.NewNode(T.common.Return(), outer_phi, T.start, outer.exit); |
| 421 | Node* end = T.graph.NewNode(T.common.End(1), ret); |
| 422 | T.graph.SetEnd(end); |
| 423 | |
| 424 | T.DeconstructOsr(); |
| 425 | |
| 426 | // Check structure of deconstructed graph. |
| 427 | // Check inner OSR loop is directly connected to start. |
| 428 | CheckInputs(inner.loop, T.start, inner.if_true); |
| 429 | CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.FalseConstant(), inner.loop); |
| 430 | |
| 431 | // Check control transfer to copy of outer loop. |
| 432 | Node* new_merge = FindSuccessor(x_true, IrOpcode::kMerge); |
| 433 | CHECK_EQ(new_merge, FindSuccessor(x_false, IrOpcode::kMerge)); |
| 434 | CheckInputs(new_merge, x_true, x_false); |
| 435 | |
| 436 | Node* new_outer_loop = FindSuccessor(new_merge, IrOpcode::kLoop); |
| 437 | Node* new_outer_phi = FindSuccessor(new_outer_loop, IrOpcode::kPhi); |
| 438 | CHECK_NE(new_outer_loop, outer.loop); |
| 439 | CHECK_NE(new_outer_phi, outer_phi); |
| 440 | |
| 441 | Node* new_entry_phi = FindSuccessor(new_merge, IrOpcode::kPhi); |
| 442 | CheckInputs(new_entry_phi, osr_phi, T.jsgraph.FalseConstant(), new_merge); |
| 443 | |
| 444 | CHECK_EQ(new_merge, new_outer_loop->InputAt(0)); |
| 445 | |
| 446 | // Check structure of outer loop. |
| 447 | Node* new_outer_branch = FindSuccessor(new_outer_loop, IrOpcode::kBranch); |
| 448 | CHECK_NE(new_outer_branch, outer.branch); |
| 449 | CheckInputs(new_outer_branch, new_outer_phi, new_outer_loop); |
| 450 | Node* new_outer_exit = FindSuccessor(new_outer_branch, IrOpcode::kIfFalse); |
| 451 | Node* new_outer_if_true = FindSuccessor(new_outer_branch, IrOpcode::kIfTrue); |
| 452 | |
| 453 | // Check structure of return. |
| 454 | end = T.graph.end(); |
| 455 | Node* new_ret = end->InputAt(0); |
| 456 | CHECK_EQ(IrOpcode::kReturn, new_ret->opcode()); |
| 457 | CheckInputs(new_ret, new_outer_phi, T.start, new_outer_exit); |
| 458 | |
| 459 | // Check structure of inner loop. |
| 460 | Node* new_inner_loop = FindSuccessor(new_outer_if_true, IrOpcode::kLoop); |
| 461 | Node* new_inner_phi = FindSuccessor(new_inner_loop, IrOpcode::kPhi); |
| 462 | |
| 463 | CheckInputs(new_inner_phi, T.jsgraph.TrueConstant(), |
| 464 | T.jsgraph.FalseConstant(), new_inner_loop); |
| 465 | CheckInputs(new_outer_phi, new_entry_phi, new_inner_phi, |
| 466 | T.jsgraph.FalseConstant(), new_outer_loop); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | Node* MakeCounter(JSGraph* jsgraph, Node* start, Node* loop) { |
| 471 | int count = loop->InputCount(); |
| 472 | NodeVector tmp_inputs(jsgraph->graph()->zone()); |
| 473 | for (int i = 0; i < count; i++) { |
| 474 | tmp_inputs.push_back(start); |
| 475 | } |
| 476 | tmp_inputs.push_back(loop); |
| 477 | |
| 478 | Node* phi = jsgraph->graph()->NewNode( |
| 479 | jsgraph->common()->Phi(MachineRepresentation::kWord32, count), count + 1, |
| 480 | &tmp_inputs[0]); |
| 481 | Node* inc = jsgraph->graph()->NewNode(&kIntAdd, phi, jsgraph->OneConstant()); |
| 482 | |
| 483 | for (int i = 1; i < count; i++) { |
| 484 | phi->ReplaceInput(i, inc); |
| 485 | } |
| 486 | return phi; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | TEST(Deconstruct_osr_nested3) { |
| 491 | OsrDeconstructorTester T(1); |
| 492 | |
| 493 | // outermost loop. |
| 494 | While loop0(T, T.p0, false, 1); |
| 495 | Node* loop0_cntr = MakeCounter(&T.jsgraph, T.p0, loop0.loop); |
| 496 | loop0.branch->ReplaceInput(0, loop0_cntr); |
| 497 | |
| 498 | // middle loop. |
| 499 | Node* loop1 = T.graph.NewNode(T.common.Loop(1), loop0.if_true); |
| 500 | Node* loop1_phi = |
| 501 | T.graph.NewNode(T.common.Phi(MachineRepresentation::kTagged, 2), |
| 502 | loop0_cntr, loop0_cntr, loop1); |
| 503 | |
| 504 | // innermost (OSR) loop. |
| 505 | While loop2(T, T.p0, true, 1); |
| 506 | loop2.loop->ReplaceInput(0, loop1); |
| 507 | |
| 508 | Node* loop2_cntr = MakeCounter(&T.jsgraph, loop1_phi, loop2.loop); |
| 509 | loop2_cntr->ReplaceInput(1, T.osr_values[0]); |
| 510 | Node* osr_phi = loop2_cntr; |
| 511 | Node* loop2_inc = loop2_cntr->InputAt(2); |
| 512 | loop2.branch->ReplaceInput(0, loop2_cntr); |
| 513 | |
| 514 | loop1_phi->ReplaceInput(1, loop2_cntr); |
| 515 | loop0_cntr->ReplaceInput(1, loop2_cntr); |
| 516 | |
| 517 | // Branch to either the outer or middle loop. |
| 518 | Node* branch = T.graph.NewNode(T.common.Branch(), loop2_cntr, loop2.exit); |
| 519 | Node* if_true = T.graph.NewNode(T.common.IfTrue(), branch); |
| 520 | Node* if_false = T.graph.NewNode(T.common.IfFalse(), branch); |
| 521 | |
| 522 | loop0.loop->ReplaceInput(1, if_true); |
| 523 | loop1->AppendInput(T.graph.zone(), if_false); |
| 524 | NodeProperties::ChangeOp(loop1, T.common.Loop(2)); |
| 525 | |
| 526 | Node* ret = |
| 527 | T.graph.NewNode(T.common.Return(), loop0_cntr, T.start, loop0.exit); |
| 528 | Node* end = T.graph.NewNode(T.common.End(1), ret); |
| 529 | T.graph.SetEnd(end); |
| 530 | |
| 531 | T.DeconstructOsr(); |
| 532 | |
| 533 | // Check structure of deconstructed graph. |
| 534 | // Check loop2 (OSR loop) is directly connected to start. |
| 535 | CheckInputs(loop2.loop, T.start, loop2.if_true); |
| 536 | CheckInputs(osr_phi, T.osr_values[0], loop2_inc, loop2.loop); |
| 537 | CheckInputs(loop2.branch, osr_phi, loop2.loop); |
| 538 | CheckInputs(loop2.if_true, loop2.branch); |
| 539 | CheckInputs(loop2.exit, loop2.branch); |
| 540 | CheckInputs(branch, osr_phi, loop2.exit); |
| 541 | CheckInputs(if_true, branch); |
| 542 | CheckInputs(if_false, branch); |
| 543 | |
| 544 | // Check structure of new_loop1. |
| 545 | Node* new_loop1_loop = FindSuccessor(if_false, IrOpcode::kLoop); |
| 546 | // TODO(titzer): check the internal copy of loop2. |
| 547 | USE(new_loop1_loop); |
| 548 | |
| 549 | // Check structure of new_loop0. |
| 550 | Node* new_loop0_loop_entry = FindSuccessor(if_true, IrOpcode::kMerge); |
| 551 | Node* new_loop0_loop = FindSuccessor(new_loop0_loop_entry, IrOpcode::kLoop); |
| 552 | // TODO(titzer): check the internal copies of loop1 and loop2. |
| 553 | |
| 554 | Node* new_loop0_branch = FindSuccessor(new_loop0_loop, IrOpcode::kBranch); |
| 555 | Node* new_loop0_if_true = FindSuccessor(new_loop0_branch, IrOpcode::kIfTrue); |
| 556 | Node* new_loop0_exit = FindSuccessor(new_loop0_branch, IrOpcode::kIfFalse); |
| 557 | |
| 558 | USE(new_loop0_if_true); |
| 559 | |
| 560 | Node* new_ret = T.graph.end()->InputAt(0); |
| 561 | CHECK_EQ(IrOpcode::kReturn, new_ret->opcode()); |
| 562 | |
| 563 | Node* new_loop0_phi = new_ret->InputAt(0); |
| 564 | CHECK_EQ(IrOpcode::kPhi, new_loop0_phi->opcode()); |
| 565 | CHECK_EQ(new_loop0_loop, NodeProperties::GetControlInput(new_loop0_phi)); |
| 566 | CHECK_EQ(new_loop0_phi, FindSuccessor(new_loop0_loop, IrOpcode::kPhi)); |
| 567 | |
| 568 | // Check that the return returns the phi from the OSR loop and control |
| 569 | // depends on the copy of the outer loop0. |
| 570 | CheckInputs(new_ret, new_loop0_phi, T.graph.start(), new_loop0_exit); |
| 571 | } |
| 572 | |
| 573 | } // namespace compiler |
| 574 | } // namespace internal |
| 575 | } // namespace v8 |