test-osr.cc 19.4 KB
Newer Older
1 2 3 4 5
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/codegen.h"
6
#include "src/compiler/all-nodes.h"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "src/compiler/common-operator.h"
#include "src/compiler/diamond.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/operator.h"
#include "src/compiler/osr.h"
#include "test/cctest/cctest.h"

using namespace v8::internal;
using namespace v8::internal::compiler;

// TODO(titzer): move this method to a common testing place.

static int CheckInputs(Node* node, Node* i0 = NULL, Node* i1 = NULL,
                       Node* i2 = NULL, Node* i3 = NULL) {
  int count = 4;
  if (i3 == NULL) count = 3;
  if (i2 == NULL) count = 2;
  if (i1 == NULL) count = 1;
  if (i0 == NULL) count = 0;
  CHECK_EQ(count, node->InputCount());
  if (i0 != NULL) CHECK_EQ(i0, node->InputAt(0));
  if (i1 != NULL) CHECK_EQ(i1, node->InputAt(1));
  if (i2 != NULL) CHECK_EQ(i2, node->InputAt(2));
  if (i3 != NULL) CHECK_EQ(i3, node->InputAt(3));
  return count;
}


static Operator kIntLt(IrOpcode::kInt32LessThan, Operator::kPure,
                       "Int32LessThan", 2, 0, 0, 1, 0, 0);
39 40
static Operator kIntAdd(IrOpcode::kInt32Add, Operator::kPure, "Int32Add", 2, 0,
                        0, 1, 0, 0);
41 42 43 44 45 46 47 48 49 50


static const int kMaxOsrValues = 10;

class OsrDeconstructorTester : public HandleAndZoneScope {
 public:
  explicit OsrDeconstructorTester(int num_values)
      : isolate(main_isolate()),
        common(main_zone()),
        graph(main_zone()),
51
        jsgraph(main_isolate(), &graph, &common, NULL, NULL),
52 53 54
        start(graph.NewNode(common.Start(1))),
        p0(graph.NewNode(common.Parameter(0), start)),
        end(graph.NewNode(common.End(), start)),
55 56
        osr_normal_entry(graph.NewNode(common.OsrNormalEntry(), start, start)),
        osr_loop_entry(graph.NewNode(common.OsrLoopEntry(), start, start)),
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        self(graph.NewNode(common.Int32Constant(0xaabbccdd))) {
    CHECK(num_values <= kMaxOsrValues);
    graph.SetStart(start);
    for (int i = 0; i < num_values; i++) {
      osr_values[i] = graph.NewNode(common.OsrValue(i), osr_loop_entry);
    }
  }

  Isolate* isolate;
  CommonOperatorBuilder common;
  Graph graph;
  JSGraph jsgraph;
  Node* start;
  Node* p0;
  Node* end;
  Node* osr_normal_entry;
  Node* osr_loop_entry;
  Node* self;
  Node* osr_values[kMaxOsrValues];

  Node* NewOsrPhi(Node* loop, Node* incoming, int osr_value, Node* back1 = NULL,
                  Node* back2 = NULL, Node* back3 = NULL) {
    int count = 5;
    if (back3 == NULL) count = 4;
    if (back2 == NULL) count = 3;
    if (back1 == NULL) count = 2;
    CHECK_EQ(loop->InputCount(), count);
    CHECK_EQ(osr_loop_entry, loop->InputAt(1));

    Node* inputs[6];
    inputs[0] = incoming;
    inputs[1] = osr_values[osr_value];
    if (count > 2) inputs[2] = back1;
    if (count > 3) inputs[3] = back2;
    if (count > 4) inputs[4] = back3;
    inputs[count] = loop;
    return graph.NewNode(common.Phi(kMachAnyTagged, count), count + 1, inputs);
  }

96
  Node* NewLoop(bool is_osr, int num_backedges, Node* entry = NULL) {
97 98
    CHECK_LT(num_backedges, 4);
    CHECK_GE(num_backedges, 0);
99
    int count = 1 + num_backedges;
100
    if (entry == NULL) entry = osr_normal_entry;
101 102 103 104 105
    Node* inputs[5] = {entry, self, self, self, self};
    if (is_osr) {
      count = 2 + num_backedges;
      inputs[1] = osr_loop_entry;
    }
106 107

    Node* loop = graph.NewNode(common.Loop(count), count, inputs);
108 109
    for (int i = 0; i < loop->InputCount(); i++) {
      if (loop->InputAt(i) == self) loop->ReplaceInput(i, loop);
110 111 112 113
    }

    return loop;
  }
114 115 116 117

  Node* NewOsrLoop(int num_backedges, Node* entry = NULL) {
    return NewLoop(true, num_backedges, entry);
  }
118 119 120 121 122 123 124 125 126

  void DeconstructOsr() {
    OsrHelper helper(0, 0);
    helper.Deconstruct(&jsgraph, &common, main_zone());
    AllNodes nodes(main_zone(), &graph);
    // Should be edited out.
    CHECK(!nodes.IsLive(osr_normal_entry));
    CHECK(!nodes.IsLive(osr_loop_entry));
    // No dangling nodes should be left over.
127 128 129 130 131 132
    for (Node* const node : nodes.live) {
      for (Node* const use : node->uses()) {
        CHECK(std::find(nodes.live.begin(), nodes.live.end(), use) !=
              nodes.live.end());
      }
    }
133
  }
134 135 136 137 138 139 140 141 142 143
};


TEST(Deconstruct_osr0) {
  OsrDeconstructorTester T(0);

  Node* loop = T.NewOsrLoop(1);

  T.graph.SetEnd(loop);

144
  T.DeconstructOsr();
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  CheckInputs(loop, T.start, loop);
}


TEST(Deconstruct_osr1) {
  OsrDeconstructorTester T(1);

  Node* loop = T.NewOsrLoop(1);
  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant());

  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, loop);
  T.graph.SetEnd(ret);

160
  T.DeconstructOsr();
161 162 163 164 165 166 167

  CheckInputs(loop, T.start, loop);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, loop);
}


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
TEST(Deconstruct_osr1_type) {
  OsrDeconstructorTester T(1);

  Node* loop = T.NewOsrLoop(1);
  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant());
  Type* type = Type::Signed32();
  NodeProperties::SetBounds(osr_phi, Bounds(type, type));

  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, loop);
  T.graph.SetEnd(ret);

  OsrHelper helper(0, 0);
  helper.Deconstruct(&T.jsgraph, &T.common, T.main_zone());

  CHECK_EQ(type, NodeProperties::GetBounds(T.osr_values[0]).lower);
  CHECK_EQ(type, NodeProperties::GetBounds(T.osr_values[0]).upper);

  CheckInputs(loop, T.start, loop);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, loop);
}


192 193 194 195 196 197 198 199 200 201 202 203
TEST(Deconstruct_osr_remove_prologue) {
  OsrDeconstructorTester T(1);
  Diamond d(&T.graph, &T.common, T.p0);
  d.Chain(T.osr_normal_entry);

  Node* loop = T.NewOsrLoop(1, d.merge);
  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant());

  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, loop);
  T.graph.SetEnd(ret);

204
  T.DeconstructOsr();
205 206 207 208 209 210

  CheckInputs(loop, T.start, loop);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, loop);

  // The control before the loop should have been removed.
211 212 213 214 215
  AllNodes nodes(T.main_zone(), &T.graph);
  CHECK(!nodes.IsLive(d.branch));
  CHECK(!nodes.IsLive(d.if_true));
  CHECK(!nodes.IsLive(d.if_false));
  CHECK(!nodes.IsLive(d.merge));
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}


TEST(Deconstruct_osr_with_body1) {
  OsrDeconstructorTester T(1);

  Node* loop = T.NewOsrLoop(1);

  Node* branch = T.graph.NewNode(T.common.Branch(), T.p0, loop);
  Node* if_true = T.graph.NewNode(T.common.IfTrue(), branch);
  Node* if_false = T.graph.NewNode(T.common.IfFalse(), branch);
  loop->ReplaceInput(2, if_true);

  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant());

  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, if_false);
  T.graph.SetEnd(ret);

235
  T.DeconstructOsr();
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

  CheckInputs(loop, T.start, if_true);
  CheckInputs(branch, T.p0, loop);
  CheckInputs(if_true, branch);
  CheckInputs(if_false, branch);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, if_false);
}


TEST(Deconstruct_osr_with_body2) {
  OsrDeconstructorTester T(1);

  Node* loop = T.NewOsrLoop(1);

  // Two chained branches in the the body of the loop.
  Node* branch1 = T.graph.NewNode(T.common.Branch(), T.p0, loop);
  Node* if_true1 = T.graph.NewNode(T.common.IfTrue(), branch1);
  Node* if_false1 = T.graph.NewNode(T.common.IfFalse(), branch1);

  Node* branch2 = T.graph.NewNode(T.common.Branch(), T.p0, if_true1);
  Node* if_true2 = T.graph.NewNode(T.common.IfTrue(), branch2);
  Node* if_false2 = T.graph.NewNode(T.common.IfFalse(), branch2);
  loop->ReplaceInput(2, if_true2);

  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant());

  Node* merge = T.graph.NewNode(T.common.Merge(2), if_false1, if_false2);
  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, merge);
  T.graph.SetEnd(ret);

268
  T.DeconstructOsr();
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

  CheckInputs(loop, T.start, if_true2);
  CheckInputs(branch1, T.p0, loop);
  CheckInputs(branch2, T.p0, if_true1);
  CheckInputs(if_true1, branch1);
  CheckInputs(if_false1, branch1);
  CheckInputs(if_true2, branch2);
  CheckInputs(if_false2, branch2);

  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, merge);
  CheckInputs(merge, if_false1, if_false2);
}


TEST(Deconstruct_osr_with_body3) {
  OsrDeconstructorTester T(1);

  Node* loop = T.NewOsrLoop(2);

  // Two branches that create two different backedges.
  Node* branch1 = T.graph.NewNode(T.common.Branch(), T.p0, loop);
  Node* if_true1 = T.graph.NewNode(T.common.IfTrue(), branch1);
  Node* if_false1 = T.graph.NewNode(T.common.IfFalse(), branch1);

  Node* branch2 = T.graph.NewNode(T.common.Branch(), T.p0, if_true1);
  Node* if_true2 = T.graph.NewNode(T.common.IfTrue(), branch2);
  Node* if_false2 = T.graph.NewNode(T.common.IfFalse(), branch2);
  loop->ReplaceInput(2, if_false1);
  loop->ReplaceInput(3, if_true2);

  Node* osr_phi =
      T.NewOsrPhi(loop, T.jsgraph.OneConstant(), 0, T.jsgraph.ZeroConstant(),
                  T.jsgraph.ZeroConstant());

  Node* ret = T.graph.NewNode(T.common.Return(), osr_phi, T.start, if_false2);
  T.graph.SetEnd(ret);

307
  T.DeconstructOsr();
308 309 310 311 312 313 314 315 316 317 318 319 320

  CheckInputs(loop, T.start, if_false1, if_true2);
  CheckInputs(branch1, T.p0, loop);
  CheckInputs(branch2, T.p0, if_true1);
  CheckInputs(if_true1, branch1);
  CheckInputs(if_false1, branch1);
  CheckInputs(if_true2, branch2);
  CheckInputs(if_false2, branch2);

  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(),
              T.jsgraph.ZeroConstant(), loop);
  CheckInputs(ret, osr_phi, T.start, if_false2);
}
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382


struct While {
  OsrDeconstructorTester& t;
  Node* branch;
  Node* if_true;
  Node* exit;
  Node* loop;

  While(OsrDeconstructorTester& R, Node* cond, bool is_osr, int backedges = 1)
      : t(R) {
    loop = t.NewLoop(is_osr, backedges);
    branch = t.graph.NewNode(t.common.Branch(), cond, loop);
    if_true = t.graph.NewNode(t.common.IfTrue(), branch);
    exit = t.graph.NewNode(t.common.IfFalse(), branch);
    loop->ReplaceInput(loop->InputCount() - 1, if_true);
  }

  void Nest(While& that) {
    that.loop->ReplaceInput(that.loop->InputCount() - 1, exit);
    this->loop->ReplaceInput(0, that.if_true);
  }

  Node* Phi(Node* i1, Node* i2, Node* i3) {
    if (loop->InputCount() == 2) {
      return t.graph.NewNode(t.common.Phi(kMachAnyTagged, 2), i1, i2, loop);
    } else {
      return t.graph.NewNode(t.common.Phi(kMachAnyTagged, 3), i1, i2, i3, loop);
    }
  }
};


static Node* FindSuccessor(Node* node, IrOpcode::Value opcode) {
  for (Node* use : node->uses()) {
    if (use->opcode() == opcode) return use;
  }
  UNREACHABLE();  // should have been found.
  return nullptr;
}


TEST(Deconstruct_osr_nested1) {
  OsrDeconstructorTester T(1);

  While outer(T, T.p0, false);
  While inner(T, T.p0, true);
  inner.Nest(outer);

  Node* outer_phi = outer.Phi(T.p0, T.p0, nullptr);
  outer.branch->ReplaceInput(0, outer_phi);

  Node* osr_phi = inner.Phi(T.jsgraph.OneConstant(), T.osr_values[0],
                            T.jsgraph.ZeroConstant());
  inner.branch->ReplaceInput(0, osr_phi);
  outer_phi->ReplaceInput(1, osr_phi);

  Node* ret =
      T.graph.NewNode(T.common.Return(), outer_phi, T.start, outer.exit);
  Node* end = T.graph.NewNode(T.common.End(), ret);
  T.graph.SetEnd(end);

383
  T.DeconstructOsr();
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

  // Check structure of deconstructed graph.
  // Check inner OSR loop is directly connected to start.
  CheckInputs(inner.loop, T.start, inner.if_true);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), inner.loop);

  // Check control transfer to copy of outer loop.
  Node* new_outer_loop = FindSuccessor(inner.exit, IrOpcode::kLoop);
  Node* new_outer_phi = FindSuccessor(new_outer_loop, IrOpcode::kPhi);
  CHECK_NE(new_outer_loop, outer.loop);
  CHECK_NE(new_outer_phi, outer_phi);

  CheckInputs(new_outer_loop, inner.exit, new_outer_loop->InputAt(1));

  // Check structure of outer loop.
  Node* new_outer_branch = FindSuccessor(new_outer_loop, IrOpcode::kBranch);
  CHECK_NE(new_outer_branch, outer.branch);
  CheckInputs(new_outer_branch, new_outer_phi, new_outer_loop);
  Node* new_outer_exit = FindSuccessor(new_outer_branch, IrOpcode::kIfFalse);
  Node* new_outer_if_true = FindSuccessor(new_outer_branch, IrOpcode::kIfTrue);

  // Check structure of return.
  end = T.graph.end();
  Node* new_ret = end->InputAt(0);
  CHECK_EQ(IrOpcode::kReturn, new_ret->opcode());
  CheckInputs(new_ret, new_outer_phi, T.start, new_outer_exit);

  // Check structure of inner loop.
  Node* new_inner_loop = FindSuccessor(new_outer_if_true, IrOpcode::kLoop);
  Node* new_inner_phi = FindSuccessor(new_inner_loop, IrOpcode::kPhi);

  CheckInputs(new_inner_phi, T.jsgraph.OneConstant(), T.jsgraph.ZeroConstant(),
              new_inner_loop);
  CheckInputs(new_outer_phi, osr_phi, new_inner_phi, new_outer_loop);
}


TEST(Deconstruct_osr_nested2) {
  OsrDeconstructorTester T(1);

  // Test multiple backedge outer loop.
  While outer(T, T.p0, false, 2);
  While inner(T, T.p0, true);
  inner.Nest(outer);

  Node* outer_phi = outer.Phi(T.p0, T.p0, T.p0);
  outer.branch->ReplaceInput(0, outer_phi);

  Node* osr_phi = inner.Phi(T.jsgraph.OneConstant(), T.osr_values[0],
                            T.jsgraph.ZeroConstant());
  inner.branch->ReplaceInput(0, osr_phi);
  outer_phi->ReplaceInput(1, osr_phi);
  outer_phi->ReplaceInput(2, T.jsgraph.ZeroConstant());

  Node* x_branch = T.graph.NewNode(T.common.Branch(), osr_phi, inner.exit);
  Node* x_true = T.graph.NewNode(T.common.IfTrue(), x_branch);
  Node* x_false = T.graph.NewNode(T.common.IfFalse(), x_branch);

  outer.loop->ReplaceInput(1, x_true);
  outer.loop->ReplaceInput(2, x_false);

  Node* ret =
      T.graph.NewNode(T.common.Return(), outer_phi, T.start, outer.exit);
  Node* end = T.graph.NewNode(T.common.End(), ret);
  T.graph.SetEnd(end);

450
  T.DeconstructOsr();
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

  // Check structure of deconstructed graph.
  // Check inner OSR loop is directly connected to start.
  CheckInputs(inner.loop, T.start, inner.if_true);
  CheckInputs(osr_phi, T.osr_values[0], T.jsgraph.ZeroConstant(), inner.loop);

  // Check control transfer to copy of outer loop.
  Node* new_merge = FindSuccessor(x_true, IrOpcode::kMerge);
  CHECK_EQ(new_merge, FindSuccessor(x_false, IrOpcode::kMerge));
  CheckInputs(new_merge, x_true, x_false);

  Node* new_outer_loop = FindSuccessor(new_merge, IrOpcode::kLoop);
  Node* new_outer_phi = FindSuccessor(new_outer_loop, IrOpcode::kPhi);
  CHECK_NE(new_outer_loop, outer.loop);
  CHECK_NE(new_outer_phi, outer_phi);

  Node* new_entry_phi = FindSuccessor(new_merge, IrOpcode::kPhi);
  CheckInputs(new_entry_phi, osr_phi, T.jsgraph.ZeroConstant(), new_merge);

  CHECK_EQ(new_merge, new_outer_loop->InputAt(0));

  // Check structure of outer loop.
  Node* new_outer_branch = FindSuccessor(new_outer_loop, IrOpcode::kBranch);
  CHECK_NE(new_outer_branch, outer.branch);
  CheckInputs(new_outer_branch, new_outer_phi, new_outer_loop);
  Node* new_outer_exit = FindSuccessor(new_outer_branch, IrOpcode::kIfFalse);
  Node* new_outer_if_true = FindSuccessor(new_outer_branch, IrOpcode::kIfTrue);

  // Check structure of return.
  end = T.graph.end();
  Node* new_ret = end->InputAt(0);
  CHECK_EQ(IrOpcode::kReturn, new_ret->opcode());
  CheckInputs(new_ret, new_outer_phi, T.start, new_outer_exit);

  // Check structure of inner loop.
  Node* new_inner_loop = FindSuccessor(new_outer_if_true, IrOpcode::kLoop);
  Node* new_inner_phi = FindSuccessor(new_inner_loop, IrOpcode::kPhi);

  CheckInputs(new_inner_phi, T.jsgraph.OneConstant(), T.jsgraph.ZeroConstant(),
              new_inner_loop);
  CheckInputs(new_outer_phi, new_entry_phi, new_inner_phi,
              T.jsgraph.ZeroConstant(), new_outer_loop);
}
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595


Node* MakeCounter(JSGraph* jsgraph, Node* start, Node* loop) {
  int count = loop->InputCount();
  NodeVector tmp_inputs(jsgraph->graph()->zone());
  for (int i = 0; i < count; i++) {
    tmp_inputs.push_back(start);
  }
  tmp_inputs.push_back(loop);

  Node* phi = jsgraph->graph()->NewNode(
      jsgraph->common()->Phi(kMachInt32, count), count + 1, &tmp_inputs[0]);
  Node* inc = jsgraph->graph()->NewNode(&kIntAdd, phi, jsgraph->OneConstant());

  for (int i = 1; i < count; i++) {
    phi->ReplaceInput(i, inc);
  }
  return phi;
}


TEST(Deconstruct_osr_nested3) {
  OsrDeconstructorTester T(1);

  // outermost loop.
  While loop0(T, T.p0, false, 1);
  Node* loop0_cntr = MakeCounter(&T.jsgraph, T.p0, loop0.loop);
  loop0.branch->ReplaceInput(0, loop0_cntr);

  // middle loop.
  Node* loop1 = T.graph.NewNode(T.common.Loop(2), loop0.if_true, T.self);
  loop1->ReplaceInput(0, loop0.if_true);
  Node* loop1_phi =
      T.graph.NewNode(T.common.Phi(kMachAnyTagged, 2), loop0_cntr, loop0_cntr);

  // innermost (OSR) loop.
  While loop2(T, T.p0, true, 1);
  loop2.loop->ReplaceInput(0, loop1);

  Node* loop2_cntr = MakeCounter(&T.jsgraph, loop1_phi, loop2.loop);
  loop2_cntr->ReplaceInput(1, T.osr_values[0]);
  Node* osr_phi = loop2_cntr;
  Node* loop2_inc = loop2_cntr->InputAt(2);
  loop2.branch->ReplaceInput(0, loop2_cntr);

  loop1_phi->ReplaceInput(1, loop2_cntr);
  loop0_cntr->ReplaceInput(1, loop2_cntr);

  // Branch to either the outer or middle loop.
  Node* branch = T.graph.NewNode(T.common.Branch(), loop2_cntr, loop2.exit);
  Node* if_true = T.graph.NewNode(T.common.IfTrue(), branch);
  Node* if_false = T.graph.NewNode(T.common.IfFalse(), branch);

  loop0.loop->ReplaceInput(1, if_true);
  loop1->ReplaceInput(1, if_false);

  Node* ret =
      T.graph.NewNode(T.common.Return(), loop0_cntr, T.start, loop0.exit);
  Node* end = T.graph.NewNode(T.common.End(), ret);
  T.graph.SetEnd(end);

  T.DeconstructOsr();

  // Check structure of deconstructed graph.
  // Check loop2 (OSR loop) is directly connected to start.
  CheckInputs(loop2.loop, T.start, loop2.if_true);
  CheckInputs(osr_phi, T.osr_values[0], loop2_inc, loop2.loop);
  CheckInputs(loop2.branch, osr_phi, loop2.loop);
  CheckInputs(loop2.if_true, loop2.branch);
  CheckInputs(loop2.exit, loop2.branch);
  CheckInputs(branch, osr_phi, loop2.exit);
  CheckInputs(if_true, branch);
  CheckInputs(if_false, branch);

  // Check structure of new_loop1.
  Node* new_loop1_loop = FindSuccessor(if_false, IrOpcode::kLoop);
  // TODO(titzer): check the internal copy of loop2.
  USE(new_loop1_loop);

  // Check structure of new_loop0.
  Node* new_loop0_loop_entry = FindSuccessor(if_true, IrOpcode::kMerge);
  Node* new_loop0_loop = FindSuccessor(new_loop0_loop_entry, IrOpcode::kLoop);
  // TODO(titzer): check the internal copies of loop1 and loop2.

  Node* new_loop0_branch = FindSuccessor(new_loop0_loop, IrOpcode::kBranch);
  Node* new_loop0_if_true = FindSuccessor(new_loop0_branch, IrOpcode::kIfTrue);
  Node* new_loop0_exit = FindSuccessor(new_loop0_branch, IrOpcode::kIfFalse);

  USE(new_loop0_if_true);

  Node* new_ret = T.graph.end()->InputAt(0);
  CHECK_EQ(IrOpcode::kReturn, new_ret->opcode());

  Node* new_loop0_phi = new_ret->InputAt(0);
  CHECK_EQ(IrOpcode::kPhi, new_loop0_phi->opcode());
  CHECK_EQ(new_loop0_loop, NodeProperties::GetControlInput(new_loop0_phi));
  CHECK_EQ(new_loop0_phi, FindSuccessor(new_loop0_loop, IrOpcode::kPhi));

  // Check that the return returns the phi from the OSR loop and control
  // depends on the copy of the outer loop0.
  CheckInputs(new_ret, new_loop0_phi, T.graph.start(), new_loop0_exit);
}