test-jump-threading.cc 16 KB
Newer Older
1 2 3 4
// 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.

5
#include "src/codegen/source-position.h"
6 7 8
#include "src/compiler/backend/instruction-codes.h"
#include "src/compiler/backend/instruction.h"
#include "src/compiler/backend/jump-threading.h"
9
#include "test/cctest/cctest.h"
10 11 12 13 14 15 16 17 18 19

namespace v8 {
namespace internal {
namespace compiler {

class TestCode : public HandleAndZoneScope {
 public:
  TestCode()
      : HandleAndZoneScope(),
        blocks_(main_zone()),
20
        sequence_(main_isolate(), main_zone(), &blocks_),
21
        rpo_number_(RpoNumber::FromInt(0)),
22
        current_(nullptr) {}
23 24 25 26 27 28 29 30

  ZoneVector<InstructionBlock*> blocks_;
  InstructionSequence sequence_;
  RpoNumber rpo_number_;
  InstructionBlock* current_;

  int Jump(int target) {
    Start();
31
    InstructionOperand ops[] = {UseRpo(target)};
32 33
    sequence_.AddInstruction(Instruction::New(main_zone(), kArchJmp, 0, nullptr,
                                              1, ops, 0, nullptr));
34 35 36 37 38 39 40 41 42 43
    int pos = static_cast<int>(sequence_.instructions().size() - 1);
    End();
    return pos;
  }
  void Fallthru() {
    Start();
    End();
  }
  int Branch(int ttarget, int ftarget) {
    Start();
44
    InstructionOperand ops[] = {UseRpo(ttarget), UseRpo(ftarget)};
45 46
    InstructionCode code = 119 | FlagsModeField::encode(kFlags_branch) |
                           FlagsConditionField::encode(kEqual);
47
    sequence_.AddInstruction(
48
        Instruction::New(main_zone(), code, 0, nullptr, 2, ops, 0, nullptr));
49 50 51 52 53 54 55 56 57 58 59
    int pos = static_cast<int>(sequence_.instructions().size() - 1);
    End();
    return pos;
  }
  void Nop() {
    Start();
    sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
  }
  void RedundantMoves() {
    Start();
    sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
60
    int index = static_cast<int>(sequence_.instructions().size()) - 1;
61 62 63 64
    AddGapMove(index, AllocatedOperand(LocationOperand::REGISTER,
                                       MachineRepresentation::kWord32, 13),
               AllocatedOperand(LocationOperand::REGISTER,
                                MachineRepresentation::kWord32, 13));
65 66 67 68
  }
  void NonRedundantMoves() {
    Start();
    sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
69
    int index = static_cast<int>(sequence_.instructions().size()) - 1;
70
    AddGapMove(index, ConstantOperand(11),
71 72
               AllocatedOperand(LocationOperand::REGISTER,
                                MachineRepresentation::kWord32, 11));
73 74 75 76 77 78 79
  }
  void Other() {
    Start();
    sequence_.AddInstruction(Instruction::New(main_zone(), 155));
  }
  void End() {
    Start();
80 81 82 83
    int end = static_cast<int>(sequence_.instructions().size());
    if (current_->code_start() == end) {  // Empty block.  Insert a nop.
      sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
    }
84
    sequence_.EndBlock(current_->rpo_number());
85
    current_ = nullptr;
86 87
    rpo_number_ = RpoNumber::FromInt(rpo_number_.ToInt() + 1);
  }
88
  InstructionOperand UseRpo(int num) {
89
    return sequence_.AddImmediate(Constant(RpoNumber::FromInt(num)));
90 91
  }
  void Start(bool deferred = false) {
92
    if (current_ == nullptr) {
93 94
      current_ = new (main_zone())
          InstructionBlock(main_zone(), rpo_number_, RpoNumber::Invalid(),
95
                           RpoNumber::Invalid(), deferred, false);
96 97 98 99 100
      blocks_.push_back(current_);
      sequence_.StartBlock(rpo_number_);
    }
  }
  void Defer() {
101
    CHECK_NULL(current_);
102 103
    Start(true);
  }
104 105
  void AddGapMove(int index, const InstructionOperand& from,
                  const InstructionOperand& to) {
106 107
    sequence_.InstructionAt(index)
        ->GetOrCreateParallelMove(Instruction::START, main_zone())
108
        ->AddMove(from, to);
109
  }
110 111
};

112
void VerifyForwarding(TestCode* code, int count, int* expected) {
113
  v8::internal::AccountingAllocator allocator;
114
  Zone local_zone(&allocator, ZONE_NAME);
115
  ZoneVector<RpoNumber> result(&local_zone);
116 117
  JumpThreading::ComputeForwarding(&local_zone, &result, &code->sequence_,
                                   true);
118 119 120

  CHECK(count == static_cast<int>(result.size()));
  for (int i = 0; i < count; i++) {
121
    CHECK_EQ(expected[i], result[i].ToInt());
122 123 124 125 126 127 128 129 130 131 132 133 134 135
  }
}

TEST(FwEmpty1) {
  TestCode code;

  // B0
  code.Jump(1);
  // B1
  code.Jump(2);
  // B2
  code.End();

  static int expected[] = {2, 2, 2};
136
  VerifyForwarding(&code, 3, expected);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}


TEST(FwEmptyN) {
  for (int i = 0; i < 9; i++) {
    TestCode code;

    // B0
    code.Jump(1);
    // B1
    for (int j = 0; j < i; j++) code.Nop();
    code.Jump(2);
    // B2
    code.End();

    static int expected[] = {2, 2, 2};
153
    VerifyForwarding(&code, 3, expected);
154 155 156 157 158 159 160 161 162 163 164
  }
}


TEST(FwNone1) {
  TestCode code;

  // B0
  code.End();

  static int expected[] = {0};
165
  VerifyForwarding(&code, 1, expected);
166 167 168 169 170 171 172 173 174 175 176
}


TEST(FwMoves1) {
  TestCode code;

  // B0
  code.RedundantMoves();
  code.End();

  static int expected[] = {0};
177
  VerifyForwarding(&code, 1, expected);
178 179 180 181 182 183 184 185 186 187 188 189 190
}


TEST(FwMoves2) {
  TestCode code;

  // B0
  code.RedundantMoves();
  code.Fallthru();
  // B1
  code.End();

  static int expected[] = {1, 1};
191
  VerifyForwarding(&code, 2, expected);
192 193 194 195 196 197 198 199 200 201 202 203 204
}


TEST(FwMoves2b) {
  TestCode code;

  // B0
  code.NonRedundantMoves();
  code.Fallthru();
  // B1
  code.End();

  static int expected[] = {0, 1};
205
  VerifyForwarding(&code, 2, expected);
206 207 208 209 210 211 212 213 214 215 216 217 218
}


TEST(FwOther2) {
  TestCode code;

  // B0
  code.Other();
  code.Fallthru();
  // B1
  code.End();

  static int expected[] = {0, 1};
219
  VerifyForwarding(&code, 2, expected);
220 221 222 223 224 225 226 227 228 229 230 231
}


TEST(FwNone2a) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.End();

  static int expected[] = {1, 1};
232
  VerifyForwarding(&code, 2, expected);
233 234 235 236 237 238 239 240 241 242 243 244
}


TEST(FwNone2b) {
  TestCode code;

  // B0
  code.Jump(1);
  // B1
  code.End();

  static int expected[] = {1, 1};
245
  VerifyForwarding(&code, 2, expected);
246 247 248 249 250 251 252 253 254 255
}


TEST(FwLoop1) {
  TestCode code;

  // B0
  code.Jump(0);

  static int expected[] = {0};
256
  VerifyForwarding(&code, 1, expected);
257 258 259 260 261 262 263 264 265 266 267 268
}


TEST(FwLoop2) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Jump(0);

  static int expected[] = {0, 0};
269
  VerifyForwarding(&code, 2, expected);
270 271 272 273 274 275 276 277 278 279 280 281 282 283
}


TEST(FwLoop3) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Jump(0);

  static int expected[] = {0, 0, 0};
284
  VerifyForwarding(&code, 3, expected);
285 286 287 288 289 290 291 292 293 294 295 296
}


TEST(FwLoop1b) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Jump(1);

  static int expected[] = {1, 1};
297
  VerifyForwarding(&code, 2, expected);
298 299 300 301 302 303 304 305 306 307 308 309 310 311
}


TEST(FwLoop2b) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Jump(1);

  static int expected[] = {1, 1, 1};
312
  VerifyForwarding(&code, 3, expected);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}


TEST(FwLoop3b) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Fallthru();
  // B3
  code.Jump(1);

  static int expected[] = {1, 1, 1, 1};
329
  VerifyForwarding(&code, 4, expected);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}


TEST(FwLoop2_1a) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Fallthru();
  // B3
  code.Jump(1);
  // B4
  code.Jump(2);

  static int expected[] = {1, 1, 1, 1, 1};
348
  VerifyForwarding(&code, 5, expected);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
}


TEST(FwLoop2_1b) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Jump(4);
  // B3
  code.Jump(1);
  // B4
  code.Jump(2);

  static int expected[] = {2, 2, 2, 2, 2};
367
  VerifyForwarding(&code, 5, expected);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
}


TEST(FwLoop2_1c) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Jump(4);
  // B3
  code.Jump(2);
  // B4
  code.Jump(1);

  static int expected[] = {1, 1, 1, 1, 1};
386
  VerifyForwarding(&code, 5, expected);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
}


TEST(FwLoop2_1d) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Jump(1);
  // B3
  code.Jump(1);
  // B4
  code.Jump(1);

  static int expected[] = {1, 1, 1, 1, 1};
405
  VerifyForwarding(&code, 5, expected);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
}


TEST(FwLoop3_1a) {
  TestCode code;

  // B0
  code.Fallthru();
  // B1
  code.Fallthru();
  // B2
  code.Fallthru();
  // B3
  code.Jump(2);
  // B4
  code.Jump(1);
  // B5
  code.Jump(0);

  static int expected[] = {2, 2, 2, 2, 2, 2};
426
  VerifyForwarding(&code, 6, expected);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
}


TEST(FwDiamonds) {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      TestCode code;
      // B0
      code.Branch(1, 2);
      // B1
      if (i) code.Other();
      code.Jump(3);
      // B2
      if (j) code.Other();
      code.Jump(3);
      // B3
      code.End();

      int expected[] = {0, i ? 1 : 3, j ? 2 : 3, 3};
446
      VerifyForwarding(&code, 4, expected);
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
    }
  }
}


TEST(FwDiamonds2) {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        TestCode code;
        // B0
        code.Branch(1, 2);
        // B1
        if (i) code.Other();
        code.Jump(3);
        // B2
        if (j) code.Other();
        code.Jump(3);
        // B3
        if (k) code.NonRedundantMoves();
        code.Jump(4);
        // B4
        code.End();

        int merge = k ? 3 : 4;
        int expected[] = {0, i ? 1 : merge, j ? 2 : merge, merge, 4};
473
        VerifyForwarding(&code, 5, expected);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
      }
    }
  }
}


TEST(FwDoubleDiamonds) {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 2; y++) {
          TestCode code;
          // B0
          code.Branch(1, 2);
          // B1
          if (i) code.Other();
          code.Jump(3);
          // B2
          if (j) code.Other();
          code.Jump(3);
          // B3
          code.Branch(4, 5);
          // B4
          if (x) code.Other();
          code.Jump(6);
          // B5
          if (y) code.Other();
          code.Jump(6);
          // B6
          code.End();

          int expected[] = {0,         i ? 1 : 3, j ? 2 : 3, 3,
                            x ? 4 : 6, y ? 5 : 6, 6};
507
          VerifyForwarding(&code, 7, expected);
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
        }
      }
    }
  }
}

template <int kSize>
void RunPermutationsRecursive(int outer[kSize], int start,
                              void (*run)(int*, int)) {
  int permutation[kSize];

  for (int i = 0; i < kSize; i++) permutation[i] = outer[i];

  int count = kSize - start;
  if (count == 0) return run(permutation, kSize);
  for (int i = start; i < kSize; i++) {
    permutation[start] = outer[i];
    permutation[i] = outer[start];
    RunPermutationsRecursive<kSize>(permutation, start + 1, run);
    permutation[i] = outer[i];
    permutation[start] = outer[start];
  }
}


template <int kSize>
void RunAllPermutations(void (*run)(int*, int)) {
  int permutation[kSize];
  for (int i = 0; i < kSize; i++) permutation[i] = i;
  RunPermutationsRecursive<kSize>(permutation, 0, run);
}


void PrintPermutation(int* permutation, int size) {
  printf("{ ");
  for (int i = 0; i < size; i++) {
    if (i > 0) printf(", ");
    printf("%d", permutation[i]);
  }
  printf(" }\n");
}


int find(int x, int* permutation, int size) {
  for (int i = 0; i < size; i++) {
    if (permutation[i] == x) return i;
  }
  return size;
}


void RunPermutedChain(int* permutation, int size) {
  TestCode code;
  int cur = -1;
  for (int i = 0; i < size; i++) {
    code.Jump(find(cur + 1, permutation, size) + 1);
    cur = permutation[i];
  }
  code.Jump(find(cur + 1, permutation, size) + 1);
  code.End();

  int expected[] = {size + 1, size + 1, size + 1, size + 1,
                    size + 1, size + 1, size + 1};
571
  VerifyForwarding(&code, size + 2, expected);
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
}


TEST(FwPermuted_chain) {
  RunAllPermutations<3>(RunPermutedChain);
  RunAllPermutations<4>(RunPermutedChain);
  RunAllPermutations<5>(RunPermutedChain);
}


void RunPermutedDiamond(int* permutation, int size) {
  TestCode code;
  int br = 1 + find(0, permutation, size);
  code.Jump(br);
  for (int i = 0; i < size; i++) {
    switch (permutation[i]) {
      case 0:
        code.Branch(1 + find(1, permutation, size),
                    1 + find(2, permutation, size));
        break;
      case 1:
        code.Jump(1 + find(3, permutation, size));
        break;
      case 2:
        code.Jump(1 + find(3, permutation, size));
        break;
      case 3:
        code.Jump(5);
        break;
    }
  }
  code.End();

  int expected[] = {br, 5, 5, 5, 5, 5};
  expected[br] = br;
607
  VerifyForwarding(&code, 6, expected);
608 609 610 611 612
}


TEST(FwPermuted_diamond) { RunAllPermutations<4>(RunPermutedDiamond); }

613 614 615
void ApplyForwarding(TestCode* code, int size, int* forward) {
  code->sequence_.RecomputeAssemblyOrderForTesting();
  ZoneVector<RpoNumber> vector(code->main_zone());
616 617 618
  for (int i = 0; i < size; i++) {
    vector.push_back(RpoNumber::FromInt(forward[i]));
  }
619
  JumpThreading::ApplyForwarding(code->main_zone(), vector, &code->sequence_);
620 621
}

622 623
void CheckJump(TestCode* code, int pos, int target) {
  Instruction* instr = code->sequence_.InstructionAt(pos);
624 625 626 627
  CHECK_EQ(kArchJmp, instr->arch_opcode());
  CHECK_EQ(1, static_cast<int>(instr->InputCount()));
  CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
  CHECK_EQ(0, static_cast<int>(instr->TempCount()));
628
  CHECK_EQ(target, code->sequence_.InputRpo(instr, 0).ToInt());
629 630
}

631 632
void CheckNop(TestCode* code, int pos) {
  Instruction* instr = code->sequence_.InstructionAt(pos);
633 634 635 636 637 638
  CHECK_EQ(kArchNop, instr->arch_opcode());
  CHECK_EQ(0, static_cast<int>(instr->InputCount()));
  CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
  CHECK_EQ(0, static_cast<int>(instr->TempCount()));
}

639 640
void CheckBranch(TestCode* code, int pos, int t1, int t2) {
  Instruction* instr = code->sequence_.InstructionAt(pos);
641 642 643
  CHECK_EQ(2, static_cast<int>(instr->InputCount()));
  CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
  CHECK_EQ(0, static_cast<int>(instr->TempCount()));
644 645
  CHECK_EQ(t1, code->sequence_.InputRpo(instr, 0).ToInt());
  CHECK_EQ(t2, code->sequence_.InputRpo(instr, 1).ToInt());
646 647
}

648
void CheckAssemblyOrder(TestCode* code, int size, int* expected) {
649
  int i = 0;
650
  for (auto const block : code->sequence_.instruction_blocks()) {
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    CHECK_EQ(expected[i++], block->ao_number().ToInt());
  }
}

TEST(Rewire1) {
  TestCode code;

  // B0
  int j1 = code.Jump(1);
  // B1
  int j2 = code.Jump(2);
  // B2
  code.End();

  static int forward[] = {2, 2, 2};
666 667 668
  ApplyForwarding(&code, 3, forward);
  CheckJump(&code, j1, 2);
  CheckNop(&code, j2);
669 670

  static int assembly[] = {0, 1, 1};
671
  CheckAssemblyOrder(&code, 3, assembly);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
}


TEST(Rewire1_deferred) {
  TestCode code;

  // B0
  int j1 = code.Jump(1);
  // B1
  int j2 = code.Jump(2);
  // B2
  code.Defer();
  int j3 = code.Jump(3);
  // B3
  code.End();

  static int forward[] = {3, 3, 3, 3};
689 690 691 692
  ApplyForwarding(&code, 4, forward);
  CheckJump(&code, j1, 3);
  CheckNop(&code, j2);
  CheckNop(&code, j3);
693 694

  static int assembly[] = {0, 1, 2, 1};
695
  CheckAssemblyOrder(&code, 4, assembly);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
}


TEST(Rewire2_deferred) {
  TestCode code;

  // B0
  code.Other();
  int j1 = code.Jump(1);
  // B1
  code.Defer();
  code.Fallthru();
  // B2
  code.Defer();
  int j2 = code.Jump(3);
  // B3
  code.End();

  static int forward[] = {0, 1, 2, 3};
715 716 717
  ApplyForwarding(&code, 4, forward);
  CheckJump(&code, j1, 1);
  CheckJump(&code, j2, 3);
718 719

  static int assembly[] = {0, 2, 3, 1};
720
  CheckAssemblyOrder(&code, 4, assembly);
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
}


TEST(Rewire_diamond) {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      TestCode code;
      // B0
      int j1 = code.Jump(1);
      // B1
      int b1 = code.Branch(2, 3);
      // B2
      int j2 = code.Jump(4);
      // B3
      int j3 = code.Jump(4);
      // B5
      code.End();

      int forward[] = {0, 1, i ? 4 : 2, j ? 4 : 3, 4};
740 741 742
      ApplyForwarding(&code, 5, forward);
      CheckJump(&code, j1, 1);
      CheckBranch(&code, b1, i ? 4 : 2, j ? 4 : 3);
743
      if (i) {
744
        CheckNop(&code, j2);
745
      } else {
746
        CheckJump(&code, j2, 4);
747 748
      }
      if (j) {
749
        CheckNop(&code, j3);
750
      } else {
751
        CheckJump(&code, j3, 4);
752 753 754 755 756 757 758 759 760
      }

      int assembly[] = {0, 1, 2, 3, 4};
      if (i) {
        for (int k = 3; k < 5; k++) assembly[k]--;
      }
      if (j) {
        for (int k = 4; k < 5; k++) assembly[k]--;
      }
761
      CheckAssemblyOrder(&code, 5, assembly);
762 763 764 765 766 767 768
    }
  }
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8