test-control-reducer.cc 40 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 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/v8.h"
#include "test/cctest/cctest.h"

8
#include "src/base/bits.h"
9 10
#include "src/compiler/common-operator.h"
#include "src/compiler/control-reducer.h"
11
#include "src/compiler/graph.h"
12
#include "src/compiler/js-graph.h"
13
#include "src/compiler/node-properties.h"
14 15 16 17

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

18 19
static const size_t kNumLeafs = 4;

20 21
enum Decision { kFalse, kUnknown, kTrue };

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// TODO(titzer): convert this whole file into unit tests.

static int CheckInputs(Node* node, Node* i0 = NULL, Node* i1 = NULL,
                       Node* i2 = NULL) {
  int 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));
  return count;
}


static int CheckMerge(Node* node, Node* i0 = NULL, Node* i1 = NULL,
                      Node* i2 = NULL) {
  CHECK_EQ(IrOpcode::kMerge, node->opcode());
  int count = CheckInputs(node, i0, i1, i2);
42
  CHECK_EQ(count, node->op()->ControlInputCount());
43 44 45 46 47 48 49 50
  return count;
}


static int CheckLoop(Node* node, Node* i0 = NULL, Node* i1 = NULL,
                     Node* i2 = NULL) {
  CHECK_EQ(IrOpcode::kLoop, node->opcode());
  int count = CheckInputs(node, i0, i1, i2);
51
  CHECK_EQ(count, node->op()->ControlInputCount());
52 53 54 55 56
  return count;
}


bool IsUsedBy(Node* a, Node* b) {
57 58
  auto const uses = a->uses();
  return std::find(uses.begin(), uses.end(), b) != uses.end();
59 60 61 62 63
}


// A helper for all tests dealing with ControlTester.
class ControlReducerTester : HandleAndZoneScope {
64
 public:
65
  ControlReducerTester()
66 67 68
      : isolate(main_isolate()),
        common(main_zone()),
        graph(main_zone()),
69
        jsgraph(main_isolate(), &graph, &common, NULL, NULL),
70
        start(graph.NewNode(common.Start(1))),
71
        end(graph.NewNode(common.End(), start)),
72
        p0(graph.NewNode(common.Parameter(0), start)),
73
        zero(jsgraph.Int32Constant(0)),
74
        one(jsgraph.OneConstant()),
75 76 77 78
        half(jsgraph.Constant(0.5)),
        self(graph.NewNode(common.Int32Constant(0xaabbccdd))),
        dead(graph.NewNode(common.Dead())) {
    graph.SetEnd(end);
79
    graph.SetStart(start);
80 81 82 83
    leaf[0] = zero;
    leaf[1] = one;
    leaf[2] = half;
    leaf[3] = p0;
84 85 86 87 88 89 90
  }

  Isolate* isolate;
  CommonOperatorBuilder common;
  Graph graph;
  JSGraph jsgraph;
  Node* start;
91
  Node* end;
92
  Node* p0;
93
  Node* zero;
94 95
  Node* one;
  Node* half;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  Node* self;
  Node* dead;
  Node* leaf[kNumLeafs];

  Node* Phi(Node* a) {
    return SetSelfReferences(graph.NewNode(op(1, false), a, start));
  }

  Node* Phi(Node* a, Node* b) {
    return SetSelfReferences(graph.NewNode(op(2, false), a, b, start));
  }

  Node* Phi(Node* a, Node* b, Node* c) {
    return SetSelfReferences(graph.NewNode(op(3, false), a, b, c, start));
  }

  Node* Phi(Node* a, Node* b, Node* c, Node* d) {
    return SetSelfReferences(graph.NewNode(op(4, false), a, b, c, d, start));
  }

  Node* EffectPhi(Node* a) {
    return SetSelfReferences(graph.NewNode(op(1, true), a, start));
  }

  Node* EffectPhi(Node* a, Node* b) {
    return SetSelfReferences(graph.NewNode(op(2, true), a, b, start));
  }

  Node* EffectPhi(Node* a, Node* b, Node* c) {
    return SetSelfReferences(graph.NewNode(op(3, true), a, b, c, start));
  }

  Node* EffectPhi(Node* a, Node* b, Node* c, Node* d) {
    return SetSelfReferences(graph.NewNode(op(4, true), a, b, c, d, start));
  }

  Node* SetSelfReferences(Node* node) {
danno's avatar
danno committed
133 134
    for (Edge edge : node->input_edges()) {
      if (edge.to() == self) node->ReplaceInput(edge.index(), node);
135 136 137 138 139 140 141
    }
    return node;
  }

  const Operator* op(int count, bool effect) {
    return effect ? common.EffectPhi(count) : common.Phi(kMachAnyTagged, count);
  }
142

143
  void Trim() { ControlReducer::TrimGraph(main_zone(), &jsgraph); }
144

145 146 147
  void ReduceGraph() {
    ControlReducer::ReduceGraph(main_zone(), &jsgraph, &common);
  }
148

149 150 151 152 153 154 155
  // Checks one-step reduction of a phi.
  void ReducePhi(Node* expect, Node* phi) {
    Node* result = ControlReducer::ReducePhiForTesting(&jsgraph, &common, phi);
    CHECK_EQ(expect, result);
    ReducePhiIterative(expect, phi);  // iterative should give the same result.
  }

156 157 158 159 160 161
  // Checks one-step reduction of a phi.
  void ReducePhiNonIterative(Node* expect, Node* phi) {
    Node* result = ControlReducer::ReducePhiForTesting(&jsgraph, &common, phi);
    CHECK_EQ(expect, result);
  }

162 163 164 165 166 167 168 169 170 171 172
  void ReducePhiIterative(Node* expect, Node* phi) {
    p0->ReplaceInput(0, start);  // hack: parameters may be trimmed.
    Node* ret = graph.NewNode(common.Return(), phi, start, start);
    Node* end = graph.NewNode(common.End(), ret);
    graph.SetEnd(end);
    ControlReducer::ReduceGraph(main_zone(), &jsgraph, &common);
    CheckInputs(end, ret);
    CheckInputs(ret, expect, start, start);
  }

  void ReduceMerge(Node* expect, Node* merge) {
173
    Node* result = ControlReducer::ReduceMerge(&jsgraph, &common, merge);
174 175 176 177 178 179 180 181 182
    CHECK_EQ(expect, result);
  }

  void ReduceMergeIterative(Node* expect, Node* merge) {
    p0->ReplaceInput(0, start);  // hack: parameters may be trimmed.
    Node* end = graph.NewNode(common.End(), merge);
    graph.SetEnd(end);
    ReduceGraph();
    CheckInputs(end, expect);
183
  }
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  void ReduceBranch(Decision expected, Node* branch) {
    Node* control = branch->InputAt(1);
    for (Node* use : branch->uses()) {
      if (use->opcode() == IrOpcode::kIfTrue) {
        Node* result =
            ControlReducer::ReduceIfNodeForTesting(&jsgraph, &common, use);
        if (expected == kTrue) CHECK_EQ(control, result);
        if (expected == kFalse) CHECK_EQ(IrOpcode::kDead, result->opcode());
        if (expected == kUnknown) CHECK_EQ(use, result);
      } else if (use->opcode() == IrOpcode::kIfFalse) {
        Node* result =
            ControlReducer::ReduceIfNodeForTesting(&jsgraph, &common, use);
        if (expected == kFalse) CHECK_EQ(control, result);
        if (expected == kTrue) CHECK_EQ(IrOpcode::kDead, result->opcode());
        if (expected == kUnknown) CHECK_EQ(use, result);
      } else {
        UNREACHABLE();
      }
    }
204 205 206 207 208 209 210 211
  }

  Node* Return(Node* val, Node* effect, Node* control) {
    Node* ret = graph.NewNode(common.Return(), val, effect, control);
    end->ReplaceInput(0, ret);
    return ret;
  }
};
212 213 214


TEST(Trim1_live) {
215
  ControlReducerTester T;
216 217 218 219
  CHECK(IsUsedBy(T.start, T.p0));
  T.graph.SetEnd(T.p0);
  T.Trim();
  CHECK(IsUsedBy(T.start, T.p0));
220
  CheckInputs(T.p0, T.start);
221 222 223 224
}


TEST(Trim1_dead) {
225
  ControlReducerTester T;
226 227 228
  CHECK(IsUsedBy(T.start, T.p0));
  T.Trim();
  CHECK(!IsUsedBy(T.start, T.p0));
229
  CHECK(!T.p0->InputAt(0));
230 231 232 233
}


TEST(Trim2_live) {
234
  ControlReducerTester T;
235 236 237 238 239 240 241 242 243 244
  Node* phi =
      T.graph.NewNode(T.common.Phi(kMachAnyTagged, 2), T.one, T.half, T.start);
  CHECK(IsUsedBy(T.one, phi));
  CHECK(IsUsedBy(T.half, phi));
  CHECK(IsUsedBy(T.start, phi));
  T.graph.SetEnd(phi);
  T.Trim();
  CHECK(IsUsedBy(T.one, phi));
  CHECK(IsUsedBy(T.half, phi));
  CHECK(IsUsedBy(T.start, phi));
245
  CheckInputs(phi, T.one, T.half, T.start);
246 247 248 249
}


TEST(Trim2_dead) {
250
  ControlReducerTester T;
251 252 253 254 255 256 257 258 259
  Node* phi =
      T.graph.NewNode(T.common.Phi(kMachAnyTagged, 2), T.one, T.half, T.start);
  CHECK(IsUsedBy(T.one, phi));
  CHECK(IsUsedBy(T.half, phi));
  CHECK(IsUsedBy(T.start, phi));
  T.Trim();
  CHECK(!IsUsedBy(T.one, phi));
  CHECK(!IsUsedBy(T.half, phi));
  CHECK(!IsUsedBy(T.start, phi));
260 261 262
  CHECK(!phi->InputAt(0));
  CHECK(!phi->InputAt(1));
  CHECK(!phi->InputAt(2));
263 264 265 266
}


TEST(Trim_chain1) {
267
  ControlReducerTester T;
268 269 270 271 272 273 274 275 276 277 278 279 280 281
  const int kDepth = 15;
  Node* live[kDepth];
  Node* dead[kDepth];
  Node* end = T.start;
  for (int i = 0; i < kDepth; i++) {
    live[i] = end = T.graph.NewNode(T.common.Merge(1), end);
    dead[i] = T.graph.NewNode(T.common.Merge(1), end);
  }
  // end         -> live[last] ->  live[last-1] -> ... -> start
  //     dead[last] ^ dead[last-1] ^ ...                  ^
  T.graph.SetEnd(end);
  T.Trim();
  for (int i = 0; i < kDepth; i++) {
    CHECK(!IsUsedBy(live[i], dead[i]));
282
    CHECK(!dead[i]->InputAt(0));
283 284 285 286 287 288
    CHECK_EQ(i == 0 ? T.start : live[i - 1], live[i]->InputAt(0));
  }
}


TEST(Trim_chain2) {
289
  ControlReducerTester T;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  const int kDepth = 15;
  Node* live[kDepth];
  Node* dead[kDepth];
  Node* l = T.start;
  Node* d = T.start;
  for (int i = 0; i < kDepth; i++) {
    live[i] = l = T.graph.NewNode(T.common.Merge(1), l);
    dead[i] = d = T.graph.NewNode(T.common.Merge(1), d);
  }
  // end -> live[last] -> live[last-1] -> ... -> start
  //        dead[last] -> dead[last-1] -> ... -> start
  T.graph.SetEnd(l);
  T.Trim();
  CHECK(!IsUsedBy(T.start, dead[0]));
  for (int i = 0; i < kDepth; i++) {
    CHECK_EQ(i == 0 ? NULL : dead[i - 1], dead[i]->InputAt(0));
    CHECK_EQ(i == 0 ? T.start : live[i - 1], live[i]->InputAt(0));
  }
}


TEST(Trim_cycle1) {
312
  ControlReducerTester T;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  Node* loop = T.graph.NewNode(T.common.Loop(1), T.start, T.start);
  loop->ReplaceInput(1, loop);
  Node* end = T.graph.NewNode(T.common.End(), loop);
  T.graph.SetEnd(end);

  CHECK(IsUsedBy(T.start, loop));
  CHECK(IsUsedBy(loop, end));
  CHECK(IsUsedBy(loop, loop));

  T.Trim();

  // nothing should have happened to the loop itself.
  CHECK(IsUsedBy(T.start, loop));
  CHECK(IsUsedBy(loop, end));
  CHECK(IsUsedBy(loop, loop));
328 329
  CheckInputs(loop, T.start, loop);
  CheckInputs(end, loop);
330 331 332 333
}


TEST(Trim_cycle2) {
334
  ControlReducerTester T;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  Node* loop = T.graph.NewNode(T.common.Loop(2), T.start, T.start);
  loop->ReplaceInput(1, loop);
  Node* end = T.graph.NewNode(T.common.End(), loop);
  Node* phi =
      T.graph.NewNode(T.common.Phi(kMachAnyTagged, 2), T.one, T.half, loop);
  T.graph.SetEnd(end);

  CHECK(IsUsedBy(T.start, loop));
  CHECK(IsUsedBy(loop, end));
  CHECK(IsUsedBy(loop, loop));
  CHECK(IsUsedBy(loop, phi));
  CHECK(IsUsedBy(T.one, phi));
  CHECK(IsUsedBy(T.half, phi));

  T.Trim();

  // nothing should have happened to the loop itself.
  CHECK(IsUsedBy(T.start, loop));
  CHECK(IsUsedBy(loop, end));
  CHECK(IsUsedBy(loop, loop));
355 356
  CheckInputs(loop, T.start, loop);
  CheckInputs(end, loop);
357 358 359 360 361

  // phi should have been trimmed away.
  CHECK(!IsUsedBy(loop, phi));
  CHECK(!IsUsedBy(T.one, phi));
  CHECK(!IsUsedBy(T.half, phi));
362 363 364
  CHECK(!phi->InputAt(0));
  CHECK(!phi->InputAt(1));
  CHECK(!phi->InputAt(2));
365 366 367
}


368
void CheckTrimConstant(ControlReducerTester* T, Node* k) {
369 370 371 372
  Node* phi = T->graph.NewNode(T->common.Phi(kMachInt32, 1), k, T->start);
  CHECK(IsUsedBy(k, phi));
  T->Trim();
  CHECK(!IsUsedBy(k, phi));
373 374
  CHECK(!phi->InputAt(0));
  CHECK(!phi->InputAt(1));
375 376 377 378
}


TEST(Trim_constants) {
379
  ControlReducerTester T;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  int32_t int32_constants[] = {
      0, -1,  -2,  2,  2,  3,  3,  4,  4,  5,  5,  4,  5,  6, 6, 7, 8, 7, 8, 9,
      0, -11, -12, 12, 12, 13, 13, 14, 14, 15, 15, 14, 15, 6, 6, 7, 8, 7, 8, 9};

  for (size_t i = 0; i < arraysize(int32_constants); i++) {
    CheckTrimConstant(&T, T.jsgraph.Int32Constant(int32_constants[i]));
    CheckTrimConstant(&T, T.jsgraph.Float64Constant(int32_constants[i]));
    CheckTrimConstant(&T, T.jsgraph.Constant(int32_constants[i]));
  }

  Node* other_constants[] = {
      T.jsgraph.UndefinedConstant(), T.jsgraph.TheHoleConstant(),
      T.jsgraph.TrueConstant(),      T.jsgraph.FalseConstant(),
      T.jsgraph.NullConstant(),      T.jsgraph.ZeroConstant(),
      T.jsgraph.OneConstant(),       T.jsgraph.NaNConstant(),
      T.jsgraph.Constant(21),        T.jsgraph.Constant(22.2)};

  for (size_t i = 0; i < arraysize(other_constants); i++) {
    CheckTrimConstant(&T, other_constants[i]);
  }
}
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 450 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


TEST(CReducePhi1) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.Phi(R.leaf[0]));
  R.ReducePhi(R.leaf[1], R.Phi(R.leaf[1]));
  R.ReducePhi(R.leaf[2], R.Phi(R.leaf[2]));
  R.ReducePhi(R.leaf[3], R.Phi(R.leaf[3]));
}


TEST(CReducePhi1_dead) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.Phi(R.leaf[0], R.dead));
  R.ReducePhi(R.leaf[1], R.Phi(R.leaf[1], R.dead));
  R.ReducePhi(R.leaf[2], R.Phi(R.leaf[2], R.dead));
  R.ReducePhi(R.leaf[3], R.Phi(R.leaf[3], R.dead));

  R.ReducePhi(R.leaf[0], R.Phi(R.dead, R.leaf[0]));
  R.ReducePhi(R.leaf[1], R.Phi(R.dead, R.leaf[1]));
  R.ReducePhi(R.leaf[2], R.Phi(R.dead, R.leaf[2]));
  R.ReducePhi(R.leaf[3], R.Phi(R.dead, R.leaf[3]));
}


TEST(CReducePhi1_dead2) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.Phi(R.leaf[0], R.dead, R.dead));
  R.ReducePhi(R.leaf[0], R.Phi(R.dead, R.leaf[0], R.dead));
  R.ReducePhi(R.leaf[0], R.Phi(R.dead, R.dead, R.leaf[0]));
}


TEST(CReducePhi2a) {
  ControlReducerTester R;

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(a, a));
  }
}


TEST(CReducePhi2b) {
  ControlReducerTester R;

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(R.self, a));
    R.ReducePhi(a, R.Phi(a, R.self));
  }
}


TEST(CReducePhi2c) {
  ControlReducerTester R;

  for (size_t i = 1; i < kNumLeafs; i++) {
    Node* a = R.leaf[i], *b = R.leaf[0];
    Node* phi1 = R.Phi(b, a);
    R.ReducePhi(phi1, phi1);

    Node* phi2 = R.Phi(a, b);
    R.ReducePhi(phi2, phi2);
  }
}


TEST(CReducePhi2_dead) {
  ControlReducerTester R;

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(a, a, R.dead));
    R.ReducePhi(a, R.Phi(a, R.dead, a));
    R.ReducePhi(a, R.Phi(R.dead, a, a));
  }

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(R.self, a));
    R.ReducePhi(a, R.Phi(a, R.self));
    R.ReducePhi(a, R.Phi(R.self, a, R.dead));
    R.ReducePhi(a, R.Phi(a, R.self, R.dead));
  }

  for (size_t i = 1; i < kNumLeafs; i++) {
    Node* a = R.leaf[i], *b = R.leaf[0];
    Node* phi1 = R.Phi(b, a, R.dead);
493
    R.ReducePhiNonIterative(phi1, phi1);
494 495

    Node* phi2 = R.Phi(a, b, R.dead);
496
    R.ReducePhiNonIterative(phi2, phi2);
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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
  }
}


TEST(CReducePhi3) {
  ControlReducerTester R;

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(a, a, a));
  }

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(R.self, a, a));
    R.ReducePhi(a, R.Phi(a, R.self, a));
    R.ReducePhi(a, R.Phi(a, a, R.self));
  }

  for (size_t i = 1; i < kNumLeafs; i++) {
    Node* a = R.leaf[i], *b = R.leaf[0];
    Node* phi1 = R.Phi(b, a, a);
    R.ReducePhi(phi1, phi1);

    Node* phi2 = R.Phi(a, b, a);
    R.ReducePhi(phi2, phi2);

    Node* phi3 = R.Phi(a, a, b);
    R.ReducePhi(phi3, phi3);
  }
}


TEST(CReducePhi4) {
  ControlReducerTester R;

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(a, a, a, a));
  }

  for (size_t i = 0; i < kNumLeafs; i++) {
    Node* a = R.leaf[i];
    R.ReducePhi(a, R.Phi(R.self, a, a, a));
    R.ReducePhi(a, R.Phi(a, R.self, a, a));
    R.ReducePhi(a, R.Phi(a, a, R.self, a));
    R.ReducePhi(a, R.Phi(a, a, a, R.self));

    R.ReducePhi(a, R.Phi(R.self, R.self, a, a));
    R.ReducePhi(a, R.Phi(a, R.self, R.self, a));
    R.ReducePhi(a, R.Phi(a, a, R.self, R.self));
    R.ReducePhi(a, R.Phi(R.self, a, a, R.self));
  }

  for (size_t i = 1; i < kNumLeafs; i++) {
    Node* a = R.leaf[i], *b = R.leaf[0];
    Node* phi1 = R.Phi(b, a, a, a);
    R.ReducePhi(phi1, phi1);

    Node* phi2 = R.Phi(a, b, a, a);
    R.ReducePhi(phi2, phi2);

    Node* phi3 = R.Phi(a, a, b, a);
    R.ReducePhi(phi3, phi3);

    Node* phi4 = R.Phi(a, a, a, b);
    R.ReducePhi(phi4, phi4);
  }
}


TEST(CReducePhi_iterative1) {
  ControlReducerTester R;

  R.ReducePhiIterative(R.leaf[0], R.Phi(R.leaf[0], R.Phi(R.leaf[0])));
  R.ReducePhiIterative(R.leaf[0], R.Phi(R.Phi(R.leaf[0]), R.leaf[0]));
}


TEST(CReducePhi_iterative2) {
  ControlReducerTester R;

  R.ReducePhiIterative(R.leaf[0], R.Phi(R.Phi(R.leaf[0]), R.Phi(R.leaf[0])));
}


TEST(CReducePhi_iterative3) {
  ControlReducerTester R;

  R.ReducePhiIterative(R.leaf[0],
                       R.Phi(R.leaf[0], R.Phi(R.leaf[0], R.leaf[0])));
  R.ReducePhiIterative(R.leaf[0],
                       R.Phi(R.Phi(R.leaf[0], R.leaf[0]), R.leaf[0]));
}


TEST(CReducePhi_iterative4) {
  ControlReducerTester R;

  R.ReducePhiIterative(R.leaf[0], R.Phi(R.Phi(R.leaf[0], R.leaf[0]),
                                        R.Phi(R.leaf[0], R.leaf[0])));

  Node* p1 = R.Phi(R.leaf[0], R.leaf[0]);
  R.ReducePhiIterative(R.leaf[0], R.Phi(p1, p1));

  Node* p2 = R.Phi(R.leaf[0], R.leaf[0], R.leaf[0]);
  R.ReducePhiIterative(R.leaf[0], R.Phi(p2, p2, p2));

  Node* p3 = R.Phi(R.leaf[0], R.leaf[0], R.leaf[0]);
  R.ReducePhiIterative(R.leaf[0], R.Phi(p3, p3, R.leaf[0]));
}


TEST(CReducePhi_iterative_self1) {
  ControlReducerTester R;

  R.ReducePhiIterative(R.leaf[0], R.Phi(R.leaf[0], R.Phi(R.leaf[0], R.self)));
  R.ReducePhiIterative(R.leaf[0], R.Phi(R.Phi(R.leaf[0], R.self), R.leaf[0]));
}


TEST(CReducePhi_iterative_self2) {
  ControlReducerTester R;

  R.ReducePhiIterative(
      R.leaf[0], R.Phi(R.Phi(R.leaf[0], R.self), R.Phi(R.leaf[0], R.self)));
  R.ReducePhiIterative(
      R.leaf[0], R.Phi(R.Phi(R.self, R.leaf[0]), R.Phi(R.self, R.leaf[0])));

  Node* p1 = R.Phi(R.leaf[0], R.self);
  R.ReducePhiIterative(R.leaf[0], R.Phi(p1, p1));

  Node* p2 = R.Phi(R.self, R.leaf[0]);
  R.ReducePhiIterative(R.leaf[0], R.Phi(p2, p2));
}


TEST(EReducePhi1) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.EffectPhi(R.leaf[0]));
  R.ReducePhi(R.leaf[1], R.EffectPhi(R.leaf[1]));
  R.ReducePhi(R.leaf[2], R.EffectPhi(R.leaf[2]));
  R.ReducePhi(R.leaf[3], R.EffectPhi(R.leaf[3]));
}


TEST(EReducePhi1_dead) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.EffectPhi(R.leaf[0], R.dead));
  R.ReducePhi(R.leaf[1], R.EffectPhi(R.leaf[1], R.dead));
  R.ReducePhi(R.leaf[2], R.EffectPhi(R.leaf[2], R.dead));
  R.ReducePhi(R.leaf[3], R.EffectPhi(R.leaf[3], R.dead));

  R.ReducePhi(R.leaf[0], R.EffectPhi(R.dead, R.leaf[0]));
  R.ReducePhi(R.leaf[1], R.EffectPhi(R.dead, R.leaf[1]));
  R.ReducePhi(R.leaf[2], R.EffectPhi(R.dead, R.leaf[2]));
  R.ReducePhi(R.leaf[3], R.EffectPhi(R.dead, R.leaf[3]));
}


TEST(EReducePhi1_dead2) {
  ControlReducerTester R;

  R.ReducePhi(R.leaf[0], R.EffectPhi(R.leaf[0], R.dead, R.dead));
  R.ReducePhi(R.leaf[0], R.EffectPhi(R.dead, R.leaf[0], R.dead));
  R.ReducePhi(R.leaf[0], R.EffectPhi(R.dead, R.dead, R.leaf[0]));
}


TEST(CMergeReduce_simple1) {
  ControlReducerTester R;

  Node* merge = R.graph.NewNode(R.common.Merge(1), R.start);
  R.ReduceMerge(R.start, merge);
}


TEST(CMergeReduce_simple2) {
  ControlReducerTester R;

  Node* merge1 = R.graph.NewNode(R.common.Merge(1), R.start);
  Node* merge2 = R.graph.NewNode(R.common.Merge(1), merge1);
  R.ReduceMerge(merge1, merge2);
  R.ReduceMergeIterative(R.start, merge2);
}


TEST(CMergeReduce_none1) {
  ControlReducerTester R;

  Node* merge = R.graph.NewNode(R.common.Merge(2), R.start, R.start);
  R.ReduceMerge(merge, merge);
}


TEST(CMergeReduce_none2) {
  ControlReducerTester R;

  Node* t = R.graph.NewNode(R.common.IfTrue(), R.start);
  Node* f = R.graph.NewNode(R.common.IfFalse(), R.start);
  Node* merge = R.graph.NewNode(R.common.Merge(2), t, f);
  R.ReduceMerge(merge, merge);
}


TEST(CMergeReduce_self3) {
  ControlReducerTester R;

  Node* merge =
      R.SetSelfReferences(R.graph.NewNode(R.common.Merge(2), R.start, R.self));
  R.ReduceMerge(merge, merge);
}


TEST(CMergeReduce_dead1) {
  ControlReducerTester R;

  Node* merge = R.graph.NewNode(R.common.Merge(2), R.start, R.dead);
  R.ReduceMerge(R.start, merge);
}


TEST(CMergeReduce_dead2) {
  ControlReducerTester R;

  Node* merge1 = R.graph.NewNode(R.common.Merge(1), R.start);
  Node* merge2 = R.graph.NewNode(R.common.Merge(2), merge1, R.dead);
  R.ReduceMerge(merge1, merge2);
  R.ReduceMergeIterative(R.start, merge2);
}


TEST(CMergeReduce_dead_rm1a) {
  ControlReducerTester R;

  for (int i = 0; i < 3; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.start, R.start, R.start);
    merge->ReplaceInput(i, R.dead);
    R.ReduceMerge(merge, merge);
    CheckMerge(merge, R.start, R.start);
  }
}


TEST(CMergeReduce_dead_rm1b) {
  ControlReducerTester R;

  Node* t = R.graph.NewNode(R.common.IfTrue(), R.start);
  Node* f = R.graph.NewNode(R.common.IfFalse(), R.start);
  for (int i = 0; i < 2; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.dead, R.dead, R.dead);
    for (int j = i + 1; j < 3; j++) {
      merge->ReplaceInput(i, t);
      merge->ReplaceInput(j, f);
      R.ReduceMerge(merge, merge);
      CheckMerge(merge, t, f);
    }
  }
}


TEST(CMergeReduce_dead_rm2) {
  ControlReducerTester R;

  for (int i = 0; i < 3; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.dead, R.dead, R.dead);
    merge->ReplaceInput(i, R.start);
    R.ReduceMerge(R.start, merge);
  }
}


TEST(CLoopReduce_dead_rm1) {
  ControlReducerTester R;

  for (int i = 0; i < 3; i++) {
    Node* loop = R.graph.NewNode(R.common.Loop(3), R.dead, R.start, R.start);
    R.ReduceMerge(loop, loop);
    CheckLoop(loop, R.start, R.start);
  }
}


TEST(CMergeReduce_edit_phi1) {
  ControlReducerTester R;

  for (int i = 0; i < 3; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.start, R.start, R.start);
    merge->ReplaceInput(i, R.dead);
    Node* phi = R.graph.NewNode(R.common.Phi(kMachAnyTagged, 3), R.leaf[0],
                                R.leaf[1], R.leaf[2], merge);
    R.ReduceMerge(merge, merge);
    CHECK_EQ(IrOpcode::kPhi, phi->opcode());
792
    CHECK_EQ(2, phi->op()->ValueInputCount());
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    CHECK_EQ(3, phi->InputCount());
    CHECK_EQ(R.leaf[i < 1 ? 1 : 0], phi->InputAt(0));
    CHECK_EQ(R.leaf[i < 2 ? 2 : 1], phi->InputAt(1));
    CHECK_EQ(merge, phi->InputAt(2));
  }
}


TEST(CMergeReduce_edit_effect_phi1) {
  ControlReducerTester R;

  for (int i = 0; i < 3; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.start, R.start, R.start);
    merge->ReplaceInput(i, R.dead);
    Node* phi = R.graph.NewNode(R.common.EffectPhi(3), R.leaf[0], R.leaf[1],
                                R.leaf[2], merge);
    R.ReduceMerge(merge, merge);
    CHECK_EQ(IrOpcode::kEffectPhi, phi->opcode());
811
    CHECK_EQ(0, phi->op()->ValueInputCount());
812
    CHECK_EQ(2, phi->op()->EffectInputCount());
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    CHECK_EQ(3, phi->InputCount());
    CHECK_EQ(R.leaf[i < 1 ? 1 : 0], phi->InputAt(0));
    CHECK_EQ(R.leaf[i < 2 ? 2 : 1], phi->InputAt(1));
    CHECK_EQ(merge, phi->InputAt(2));
  }
}


static const int kSelectorSize = 4;

// Helper to select K of N nodes according to a mask, useful for the test below.
struct Selector {
  int mask;
  int count;
  explicit Selector(int m) {
    mask = m;
    count = v8::base::bits::CountPopulation32(m);
  }
  bool is_selected(int i) { return (mask & (1 << i)) != 0; }
  void CheckNode(Node* node, IrOpcode::Value opcode, Node** inputs,
                 Node* control) {
    CHECK_EQ(opcode, node->opcode());
    CHECK_EQ(count + (control != NULL ? 1 : 0), node->InputCount());
    int index = 0;
    for (int i = 0; i < kSelectorSize; i++) {
      if (mask & (1 << i)) {
        CHECK_EQ(inputs[i], node->InputAt(index++));
      }
    }
    CHECK_EQ(count, index);
    if (control != NULL) CHECK_EQ(control, node->InputAt(index++));
  }
  int single_index() {
    CHECK_EQ(1, count);
    return WhichPowerOf2(mask);
  }
};


TEST(CMergeReduce_exhaustive_4) {
  ControlReducerTester R;
  Node* controls[] = {
      R.graph.NewNode(R.common.Start(1)), R.graph.NewNode(R.common.Start(2)),
      R.graph.NewNode(R.common.Start(3)), R.graph.NewNode(R.common.Start(4))};
  Node* values[] = {R.jsgraph.Int32Constant(11), R.jsgraph.Int32Constant(22),
                    R.jsgraph.Int32Constant(33), R.jsgraph.Int32Constant(44)};
  Node* effects[] = {
      R.jsgraph.Float64Constant(123.4), R.jsgraph.Float64Constant(223.4),
      R.jsgraph.Float64Constant(323.4), R.jsgraph.Float64Constant(423.4)};

  for (int mask = 0; mask < (1 << (kSelectorSize - 1)); mask++) {
    // Reduce a single merge with a given mask.
    Node* merge = R.graph.NewNode(R.common.Merge(4), controls[0], controls[1],
                                  controls[2], controls[3]);
    Node* phi = R.graph.NewNode(R.common.Phi(kMachAnyTagged, 4), values[0],
                                values[1], values[2], values[3], merge);
    Node* ephi = R.graph.NewNode(R.common.EffectPhi(4), effects[0], effects[1],
                                 effects[2], effects[3], merge);

    Node* phi_use =
        R.graph.NewNode(R.common.Phi(kMachAnyTagged, 1), phi, R.start);
    Node* ephi_use = R.graph.NewNode(R.common.EffectPhi(1), ephi, R.start);

    Selector selector(mask);

    for (int i = 0; i < kSelectorSize; i++) {  // set up dead merge inputs.
      if (!selector.is_selected(i)) merge->ReplaceInput(i, R.dead);
    }

882
    Node* result = ControlReducer::ReduceMerge(&R.jsgraph, &R.common, merge);
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

    int count = selector.count;
    if (count == 0) {
      // result should be dead.
      CHECK_EQ(IrOpcode::kDead, result->opcode());
    } else if (count == 1) {
      // merge should be replaced with one of the controls.
      CHECK_EQ(controls[selector.single_index()], result);
      // Phis should have been directly replaced.
      CHECK_EQ(values[selector.single_index()], phi_use->InputAt(0));
      CHECK_EQ(effects[selector.single_index()], ephi_use->InputAt(0));
    } else {
      // Otherwise, nodes should be edited in place.
      CHECK_EQ(merge, result);
      selector.CheckNode(merge, IrOpcode::kMerge, controls, NULL);
      selector.CheckNode(phi, IrOpcode::kPhi, values, merge);
      selector.CheckNode(ephi, IrOpcode::kEffectPhi, effects, merge);
      CHECK_EQ(phi, phi_use->InputAt(0));
      CHECK_EQ(ephi, ephi_use->InputAt(0));
902
      CHECK_EQ(count, phi->op()->ValueInputCount());
903
      CHECK_EQ(count + 1, phi->InputCount());
904
      CHECK_EQ(count, ephi->op()->EffectInputCount());
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
      CHECK_EQ(count + 1, ephi->InputCount());
    }
  }
}


TEST(CMergeReduce_edit_many_phis1) {
  ControlReducerTester R;

  const int kPhiCount = 10;
  Node* phis[kPhiCount];

  for (int i = 0; i < 3; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(3), R.start, R.start, R.start);
    merge->ReplaceInput(i, R.dead);
    for (int j = 0; j < kPhiCount; j++) {
      phis[j] = R.graph.NewNode(R.common.Phi(kMachAnyTagged, 3), R.leaf[0],
                                R.leaf[1], R.leaf[2], merge);
    }
    R.ReduceMerge(merge, merge);
    for (int j = 0; j < kPhiCount; j++) {
      Node* phi = phis[j];
      CHECK_EQ(IrOpcode::kPhi, phi->opcode());
928
      CHECK_EQ(2, phi->op()->ValueInputCount());
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
      CHECK_EQ(3, phi->InputCount());
      CHECK_EQ(R.leaf[i < 1 ? 1 : 0], phi->InputAt(0));
      CHECK_EQ(R.leaf[i < 2 ? 2 : 1], phi->InputAt(1));
      CHECK_EQ(merge, phi->InputAt(2));
    }
  }
}


TEST(CMergeReduce_simple_chain1) {
  ControlReducerTester R;
  for (int i = 0; i < 5; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(1), R.start);
    for (int j = 0; j < i; j++) {
      merge = R.graph.NewNode(R.common.Merge(1), merge);
    }
    R.ReduceMergeIterative(R.start, merge);
  }
}


TEST(CMergeReduce_dead_chain1) {
  ControlReducerTester R;
  for (int i = 0; i < 5; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(1), R.dead);
    for (int j = 0; j < i; j++) {
      merge = R.graph.NewNode(R.common.Merge(1), merge);
    }
    Node* end = R.graph.NewNode(R.common.End(), merge);
    R.graph.SetEnd(end);
    R.ReduceGraph();
    CHECK(merge->IsDead());
961
    CHECK(!end->InputAt(0));  // end dies.
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
  }
}


TEST(CMergeReduce_dead_chain2) {
  ControlReducerTester R;
  for (int i = 0; i < 5; i++) {
    Node* merge = R.graph.NewNode(R.common.Merge(1), R.start);
    for (int j = 0; j < i; j++) {
      merge = R.graph.NewNode(R.common.Merge(2), merge, R.dead);
    }
    R.ReduceMergeIterative(R.start, merge);
  }
}


struct Branch {
  Node* branch;
  Node* if_true;
  Node* if_false;

  Branch(ControlReducerTester& R, Node* cond, Node* control = NULL) {
    if (control == NULL) control = R.start;
    branch = R.graph.NewNode(R.common.Branch(), cond, control);
    if_true = R.graph.NewNode(R.common.IfTrue(), branch);
    if_false = R.graph.NewNode(R.common.IfFalse(), branch);
  }
};


992
// TODO(titzer): use the diamonds from src/compiler/diamond.h here.
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
struct Diamond {
  Node* branch;
  Node* if_true;
  Node* if_false;
  Node* merge;
  Node* phi;

  Diamond(ControlReducerTester& R, Node* cond) {
    branch = R.graph.NewNode(R.common.Branch(), cond, R.start);
    if_true = R.graph.NewNode(R.common.IfTrue(), branch);
    if_false = R.graph.NewNode(R.common.IfFalse(), branch);
    merge = R.graph.NewNode(R.common.Merge(2), if_true, if_false);
    phi = NULL;
  }

  Diamond(ControlReducerTester& R, Node* cond, Node* tv, Node* fv) {
    branch = R.graph.NewNode(R.common.Branch(), cond, R.start);
    if_true = R.graph.NewNode(R.common.IfTrue(), branch);
    if_false = R.graph.NewNode(R.common.IfFalse(), branch);
    merge = R.graph.NewNode(R.common.Merge(2), if_true, if_false);
    phi = R.graph.NewNode(R.common.Phi(kMachAnyTagged, 2), tv, fv, merge);
  }

  void chain(Diamond& that) { branch->ReplaceInput(1, that.merge); }

  // Nest {this} into either the if_true or if_false branch of {that}.
  void nest(Diamond& that, bool if_true) {
    if (if_true) {
      branch->ReplaceInput(1, that.if_true);
      that.merge->ReplaceInput(0, merge);
    } else {
      branch->ReplaceInput(1, that.if_false);
      that.merge->ReplaceInput(1, merge);
    }
  }
};


struct While {
  Node* branch;
  Node* if_true;
  Node* exit;
  Node* loop;

  While(ControlReducerTester& R, Node* cond) {
    loop = R.graph.NewNode(R.common.Loop(2), R.start, R.start);
    branch = R.graph.NewNode(R.common.Branch(), cond, loop);
    if_true = R.graph.NewNode(R.common.IfTrue(), branch);
    exit = R.graph.NewNode(R.common.IfFalse(), branch);
    loop->ReplaceInput(1, if_true);
  }

  void chain(Node* control) { loop->ReplaceInput(0, control); }
};


TEST(CBranchReduce_none1) {
  ControlReducerTester R;
  Diamond d(R, R.p0);
1052
  R.ReduceBranch(kUnknown, d.branch);
1053 1054 1055 1056 1057 1058 1059 1060
}


TEST(CBranchReduce_none2) {
  ControlReducerTester R;
  Diamond d1(R, R.p0);
  Diamond d2(R, R.p0);
  d2.chain(d1);
1061
  R.ReduceBranch(kUnknown, d2.branch);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}


TEST(CBranchReduce_true) {
  ControlReducerTester R;
  Node* true_values[] = {
      R.one,                               R.jsgraph.Int32Constant(2),
      R.jsgraph.Int32Constant(0x7fffffff), R.jsgraph.Constant(1.0),
      R.jsgraph.Constant(22.1),            R.jsgraph.TrueConstant()};

  for (size_t i = 0; i < arraysize(true_values); i++) {
    Diamond d(R, true_values[i]);
1074
    R.ReduceBranch(kTrue, d.branch);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  }
}


TEST(CBranchReduce_false) {
  ControlReducerTester R;
  Node* false_values[] = {R.zero, R.jsgraph.Constant(0.0),
                          R.jsgraph.Constant(-0.0), R.jsgraph.FalseConstant()};

  for (size_t i = 0; i < arraysize(false_values); i++) {
    Diamond d(R, false_values[i]);
1086
    R.ReduceBranch(kFalse, d.branch);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  }
}


TEST(CDiamondReduce_true) {
  ControlReducerTester R;
  Diamond d1(R, R.one);
  R.ReduceMergeIterative(R.start, d1.merge);
}


TEST(CDiamondReduce_false) {
  ControlReducerTester R;
  Diamond d2(R, R.zero);
  R.ReduceMergeIterative(R.start, d2.merge);
}


TEST(CChainedDiamondsReduce_true_false) {
  ControlReducerTester R;
  Diamond d1(R, R.one);
  Diamond d2(R, R.zero);
  d2.chain(d1);

  R.ReduceMergeIterative(R.start, d2.merge);
}


TEST(CChainedDiamondsReduce_x_false) {
  ControlReducerTester R;
  Diamond d1(R, R.p0);
  Diamond d2(R, R.zero);
  d2.chain(d1);

  R.ReduceMergeIterative(d1.merge, d2.merge);
}


TEST(CChainedDiamondsReduce_false_x) {
  ControlReducerTester R;
  Diamond d1(R, R.zero);
  Diamond d2(R, R.p0);
  d2.chain(d1);

  R.ReduceMergeIterative(d2.merge, d2.merge);
  CheckInputs(d2.branch, R.p0, R.start);
}


TEST(CChainedDiamondsReduce_phi1) {
  ControlReducerTester R;
  Diamond d1(R, R.zero, R.one, R.zero);  // foldable branch, phi.
  Diamond d2(R, d1.phi);
  d2.chain(d1);

  R.ReduceMergeIterative(R.start, d2.merge);
}


TEST(CChainedDiamondsReduce_phi2) {
  ControlReducerTester R;
  Diamond d1(R, R.p0, R.one, R.one);  // redundant phi.
  Diamond d2(R, d1.phi);
  d2.chain(d1);

  R.ReduceMergeIterative(d1.merge, d2.merge);
}


TEST(CNestedDiamondsReduce_true_true_false) {
  ControlReducerTester R;
  Diamond d1(R, R.one);
  Diamond d2(R, R.zero);
  d2.nest(d1, true);

  R.ReduceMergeIterative(R.start, d1.merge);
}


TEST(CNestedDiamondsReduce_false_true_false) {
  ControlReducerTester R;
  Diamond d1(R, R.one);
  Diamond d2(R, R.zero);
  d2.nest(d1, false);

  R.ReduceMergeIterative(R.start, d1.merge);
}


TEST(CNestedDiamonds_xyz) {
  ControlReducerTester R;

  for (int a = 0; a < 2; a++) {
    for (int b = 0; b < 2; b++) {
      for (int c = 0; c < 2; c++) {
        Diamond d1(R, R.jsgraph.Int32Constant(a));
        Diamond d2(R, R.jsgraph.Int32Constant(b));
        d2.nest(d1, c);

        R.ReduceMergeIterative(R.start, d1.merge);
      }
    }
  }
}


TEST(CDeadLoop1) {
  ControlReducerTester R;

  Node* loop = R.graph.NewNode(R.common.Loop(1), R.start);
  Branch b(R, R.p0, loop);
  loop->ReplaceInput(0, b.if_true);  // loop is not connected to start.
  Node* merge = R.graph.NewNode(R.common.Merge(2), R.start, b.if_false);
  R.ReduceMergeIterative(R.start, merge);
  CHECK(b.if_true->IsDead());
  CHECK(b.if_false->IsDead());
}


TEST(CDeadLoop2) {
  ControlReducerTester R;

  While w(R, R.p0);
  Diamond d(R, R.zero);
  // if (0) { while (p0) ; } else { }
  w.branch->ReplaceInput(1, d.if_true);
  d.merge->ReplaceInput(0, w.exit);

  R.ReduceMergeIterative(R.start, d.merge);
  CHECK(d.if_true->IsDead());
  CHECK(d.if_false->IsDead());
}


TEST(Return1) {
  ControlReducerTester R;
  Node* ret = R.Return(R.one, R.start, R.start);
  R.ReduceGraph();
  CheckInputs(R.graph.end(), ret);
  CheckInputs(ret, R.one, R.start, R.start);
}


TEST(Return2) {
  ControlReducerTester R;
  Diamond d(R, R.one);
  Node* ret = R.Return(R.half, R.start, d.merge);
  R.ReduceGraph();
  CHECK(d.branch->IsDead());
  CHECK(d.if_true->IsDead());
  CHECK(d.if_false->IsDead());
  CHECK(d.merge->IsDead());

  CheckInputs(R.graph.end(), ret);
  CheckInputs(ret, R.half, R.start, R.start);
}


TEST(Return_true1) {
  ControlReducerTester R;
  Diamond d(R, R.one, R.half, R.zero);
  Node* ret = R.Return(d.phi, R.start, d.merge);
  R.ReduceGraph();
  CHECK(d.branch->IsDead());
  CHECK(d.if_true->IsDead());
  CHECK(d.if_false->IsDead());
  CHECK(d.merge->IsDead());
  CHECK(d.phi->IsDead());

  CheckInputs(R.graph.end(), ret);
  CheckInputs(ret, R.half, R.start, R.start);
}


TEST(Return_false1) {
  ControlReducerTester R;
  Diamond d(R, R.zero, R.one, R.half);
  Node* ret = R.Return(d.phi, R.start, d.merge);
  R.ReduceGraph();
  CHECK(d.branch->IsDead());
  CHECK(d.if_true->IsDead());
  CHECK(d.if_false->IsDead());
  CHECK(d.merge->IsDead());
  CHECK(d.phi->IsDead());

  CheckInputs(R.graph.end(), ret);
  CheckInputs(ret, R.half, R.start, R.start);
}


void CheckDeadDiamond(Diamond& d) {
  CHECK(d.branch->IsDead());
  CHECK(d.if_true->IsDead());
  CHECK(d.if_false->IsDead());
  CHECK(d.merge->IsDead());
  if (d.phi != NULL) CHECK(d.phi->IsDead());
}


void CheckLiveDiamond(Diamond& d, bool live_phi = true) {
  CheckInputs(d.merge, d.if_true, d.if_false);
  CheckInputs(d.if_true, d.branch);
  CheckInputs(d.if_false, d.branch);
  if (d.phi != NULL) {
    if (live_phi) {
      CHECK_EQ(3, d.phi->InputCount());
      CHECK_EQ(d.merge, d.phi->InputAt(2));
    } else {
      CHECK(d.phi->IsDead());
    }
  }
}


TEST(Return_effect1) {
  ControlReducerTester R;
  Diamond d(R, R.one);
  Node* e1 = R.jsgraph.Float64Constant(-100.1);
  Node* e2 = R.jsgraph.Float64Constant(+100.1);
  Node* effect = R.graph.NewNode(R.common.EffectPhi(2), e1, e2, d.merge);
  Node* ret = R.Return(R.p0, effect, d.merge);
  R.ReduceGraph();
  CheckDeadDiamond(d);
  CHECK(effect->IsDead());

  CheckInputs(R.graph.end(), ret);
  CheckInputs(ret, R.p0, e1, R.start);
}


TEST(Return_nested_diamonds1) {
  ControlReducerTester R;
  Diamond d1(R, R.p0, R.one, R.zero);
  Diamond d2(R, R.p0);
  Diamond d3(R, R.p0);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // nothing should happen.

  CheckInputs(ret, d1.phi, R.start, d1.merge);
  CheckInputs(d1.phi, R.one, R.zero, d1.merge);
  CheckInputs(d1.merge, d2.merge, d3.merge);
  CheckLiveDiamond(d2);
  CheckLiveDiamond(d3);
}


TEST(Return_nested_diamonds_true1) {
  ControlReducerTester R;
  Diamond d1(R, R.one, R.one, R.zero);
  Diamond d2(R, R.p0);
  Diamond d3(R, R.p0);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded true.

  CheckInputs(ret, R.one, R.start, d2.merge);
  CheckInputs(d2.branch, R.p0, R.start);
  CheckDeadDiamond(d1);
  CheckLiveDiamond(d2);
  CheckDeadDiamond(d3);
}


TEST(Return_nested_diamonds_false1) {
  ControlReducerTester R;
  Diamond d1(R, R.zero, R.one, R.zero);
  Diamond d2(R, R.p0);
  Diamond d3(R, R.p0);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded false.

  CheckInputs(ret, R.zero, R.start, d3.merge);
  CheckInputs(d3.branch, R.p0, R.start);
  CheckDeadDiamond(d1);
  CheckDeadDiamond(d2);
  CheckLiveDiamond(d3);
}


TEST(Return_nested_diamonds_true_true1) {
  ControlReducerTester R;
  Diamond d1(R, R.one, R.one, R.zero);
  Diamond d2(R, R.one);
  Diamond d3(R, R.p0);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 and d2 both get folded true.

  CheckInputs(ret, R.one, R.start, R.start);
  CheckDeadDiamond(d1);
  CheckDeadDiamond(d2);
  CheckDeadDiamond(d3);
}


TEST(Return_nested_diamonds_true_false1) {
  ControlReducerTester R;
  Diamond d1(R, R.one, R.one, R.zero);
  Diamond d2(R, R.zero);
  Diamond d3(R, R.p0);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded true and d2 gets folded false.

  CheckInputs(ret, R.one, R.start, R.start);
  CheckDeadDiamond(d1);
  CheckDeadDiamond(d2);
  CheckDeadDiamond(d3);
}


TEST(Return_nested_diamonds2) {
  ControlReducerTester R;
  Node* x2 = R.jsgraph.Float64Constant(11.1);
  Node* y2 = R.jsgraph.Float64Constant(22.2);
  Node* x3 = R.jsgraph.Float64Constant(33.3);
  Node* y3 = R.jsgraph.Float64Constant(44.4);

  Diamond d2(R, R.p0, x2, y2);
  Diamond d3(R, R.p0, x3, y3);
  Diamond d1(R, R.p0, d2.phi, d3.phi);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // nothing should happen.

  CheckInputs(ret, d1.phi, R.start, d1.merge);
  CheckInputs(d1.phi, d2.phi, d3.phi, d1.merge);
  CheckInputs(d1.merge, d2.merge, d3.merge);
  CheckLiveDiamond(d2);
  CheckLiveDiamond(d3);
}


TEST(Return_nested_diamonds_true2) {
  ControlReducerTester R;
  Node* x2 = R.jsgraph.Float64Constant(11.1);
  Node* y2 = R.jsgraph.Float64Constant(22.2);
  Node* x3 = R.jsgraph.Float64Constant(33.3);
  Node* y3 = R.jsgraph.Float64Constant(44.4);

  Diamond d2(R, R.p0, x2, y2);
  Diamond d3(R, R.p0, x3, y3);
  Diamond d1(R, R.one, d2.phi, d3.phi);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded true.

  CheckInputs(ret, d2.phi, R.start, d2.merge);
  CheckInputs(d2.branch, R.p0, R.start);
  CheckDeadDiamond(d1);
  CheckLiveDiamond(d2);
  CheckDeadDiamond(d3);
}


TEST(Return_nested_diamonds_true_true2) {
  ControlReducerTester R;
  Node* x2 = R.jsgraph.Float64Constant(11.1);
  Node* y2 = R.jsgraph.Float64Constant(22.2);
  Node* x3 = R.jsgraph.Float64Constant(33.3);
  Node* y3 = R.jsgraph.Float64Constant(44.4);

  Diamond d2(R, R.one, x2, y2);
  Diamond d3(R, R.p0, x3, y3);
  Diamond d1(R, R.one, d2.phi, d3.phi);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded true.

  CheckInputs(ret, x2, R.start, R.start);
  CheckDeadDiamond(d1);
  CheckDeadDiamond(d2);
  CheckDeadDiamond(d3);
}


TEST(Return_nested_diamonds_true_false2) {
  ControlReducerTester R;
  Node* x2 = R.jsgraph.Float64Constant(11.1);
  Node* y2 = R.jsgraph.Float64Constant(22.2);
  Node* x3 = R.jsgraph.Float64Constant(33.3);
  Node* y3 = R.jsgraph.Float64Constant(44.4);

  Diamond d2(R, R.zero, x2, y2);
  Diamond d3(R, R.p0, x3, y3);
  Diamond d1(R, R.one, d2.phi, d3.phi);

  d2.nest(d1, true);
  d3.nest(d1, false);

  Node* ret = R.Return(d1.phi, R.start, d1.merge);

  R.ReduceGraph();  // d1 gets folded true.

  CheckInputs(ret, y2, R.start, R.start);
  CheckDeadDiamond(d1);
  CheckDeadDiamond(d2);
  CheckDeadDiamond(d3);
}