test-run-machops.cc 144 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 <cmath>
6
#include <functional>
7 8
#include <limits>

9
#include "src/base/bits.h"
10
#include "src/base/utils/random-number-generator.h"
11
#include "src/codegen.h"
12 13 14 15 16 17
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/codegen-tester.h"
#include "test/cctest/compiler/value-helper.h"

#if V8_TURBOFAN_TARGET

18
using namespace v8::base;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
using namespace v8::internal;
using namespace v8::internal::compiler;

typedef RawMachineAssembler::Label MLabel;

TEST(RunInt32Add) {
  RawMachineAssemblerTester<int32_t> m;
  Node* add = m.Int32Add(m.Int32Constant(0), m.Int32Constant(1));
  m.Return(add);
  CHECK_EQ(1, m.Call());
}


static Node* Int32Input(RawMachineAssemblerTester<int32_t>* m, int index) {
  switch (index) {
    case 0:
      return m->Parameter(0);
    case 1:
      return m->Parameter(1);
    case 2:
      return m->Int32Constant(0);
    case 3:
      return m->Int32Constant(1);
    case 4:
      return m->Int32Constant(-1);
    case 5:
      return m->Int32Constant(0xff);
    case 6:
      return m->Int32Constant(0x01234567);
    case 7:
49
      return m->Load(kMachInt32, m->PointerConstant(NULL));
50 51 52 53 54 55 56 57 58
    default:
      return NULL;
  }
}


TEST(CodeGenInt32Binop) {
  RawMachineAssemblerTester<void> m;

59
  const Operator* kOps[] = {
60 61 62 63 64 65 66 67 68 69
      m.machine()->Word32And(),      m.machine()->Word32Or(),
      m.machine()->Word32Xor(),      m.machine()->Word32Shl(),
      m.machine()->Word32Shr(),      m.machine()->Word32Sar(),
      m.machine()->Word32Equal(),    m.machine()->Int32Add(),
      m.machine()->Int32Sub(),       m.machine()->Int32Mul(),
      m.machine()->Int32MulHigh(),   m.machine()->Int32Div(),
      m.machine()->Uint32Div(),      m.machine()->Int32Mod(),
      m.machine()->Uint32Mod(),      m.machine()->Uint32MulHigh(),
      m.machine()->Int32LessThan(),  m.machine()->Int32LessThanOrEqual(),
      m.machine()->Uint32LessThan(), m.machine()->Uint32LessThanOrEqual()};
70 71

  for (size_t i = 0; i < arraysize(kOps); ++i) {
72 73
    for (int j = 0; j < 8; j++) {
      for (int k = 0; k < 8; k++) {
74
        RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
75 76
        Node* a = Int32Input(&m, j);
        Node* b = Int32Input(&m, k);
77
        m.Return(m.NewNode(kOps[i], a, b));
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        m.GenerateCode();
      }
    }
  }
}


TEST(RunGoto) {
  RawMachineAssemblerTester<int32_t> m;
  int constant = 99999;

  MLabel next;
  m.Goto(&next);
  m.Bind(&next);
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


TEST(RunGotoMultiple) {
  RawMachineAssemblerTester<int32_t> m;
  int constant = 9999977;

  MLabel labels[10];
103
  for (size_t i = 0; i < arraysize(labels); i++) {
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    m.Goto(&labels[i]);
    m.Bind(&labels[i]);
  }
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


TEST(RunBranch) {
  RawMachineAssemblerTester<int32_t> m;
  int constant = 999777;

  MLabel blocka, blockb;
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Return(m.Int32Constant(0 - constant));
  m.Bind(&blockb);
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


TEST(RunDiamond2) {
  RawMachineAssemblerTester<int32_t> m;

  int constant = 995666;

  MLabel blocka, blockb, end;
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


TEST(RunLoop) {
  RawMachineAssemblerTester<int32_t> m;
  int constant = 999555;

  MLabel header, body, exit;
  m.Goto(&header);
  m.Bind(&header);
  m.Branch(m.Int32Constant(0), &body, &exit);
  m.Bind(&body);
  m.Goto(&header);
  m.Bind(&exit);
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


template <typename R>
static void BuildDiamondPhi(RawMachineAssemblerTester<R>* m, Node* cond_node,
165 166
                            MachineType type, Node* true_node,
                            Node* false_node) {
167 168 169 170 171 172 173 174 175
  MLabel blocka, blockb;
  MLabel* end = m->Exit();
  m->Branch(cond_node, &blocka, &blockb);
  m->Bind(&blocka);
  m->Goto(end);
  m->Bind(&blockb);
  m->Goto(end);

  m->Bind(end);
176
  Node* phi = m->Phi(type, true_node, false_node);
177 178 179 180 181
  m->Return(phi);
}


TEST(RunDiamondPhiConst) {
182
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
183 184 185 186
  int false_val = 0xFF666;
  int true_val = 0x00DDD;
  Node* true_node = m.Int32Constant(true_val);
  Node* false_node = m.Int32Constant(false_val);
187
  BuildDiamondPhi(&m, m.Parameter(0), kMachInt32, true_node, false_node);
188 189 190 191 192 193
  CHECK_EQ(false_val, m.Call(0));
  CHECK_EQ(true_val, m.Call(1));
}


TEST(RunDiamondPhiNumber) {
194
  RawMachineAssemblerTester<Object*> m(kMachInt32);
195 196 197 198
  double false_val = -11.1;
  double true_val = 200.1;
  Node* true_node = m.NumberConstant(true_val);
  Node* false_node = m.NumberConstant(false_val);
199
  BuildDiamondPhi(&m, m.Parameter(0), kMachAnyTagged, true_node, false_node);
200 201 202 203 204 205
  m.CheckNumber(false_val, m.Call(0));
  m.CheckNumber(true_val, m.Call(1));
}


TEST(RunDiamondPhiString) {
206
  RawMachineAssemblerTester<Object*> m(kMachInt32);
207 208 209 210
  const char* false_val = "false";
  const char* true_val = "true";
  Node* true_node = m.StringConstant(true_val);
  Node* false_node = m.StringConstant(false_val);
211
  BuildDiamondPhi(&m, m.Parameter(0), kMachAnyTagged, true_node, false_node);
212 213 214 215 216 217
  m.CheckString(false_val, m.Call(0));
  m.CheckString(true_val, m.Call(1));
}


TEST(RunDiamondPhiParam) {
218
  RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
219 220
  BuildDiamondPhi(&m, m.Parameter(0), kMachInt32, m.Parameter(1),
                  m.Parameter(2));
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  int32_t c1 = 0x260cb75a;
  int32_t c2 = 0xcd3e9c8b;
  int result = m.Call(0, c1, c2);
  CHECK_EQ(c2, result);
  result = m.Call(1, c1, c2);
  CHECK_EQ(c1, result);
}


TEST(RunLoopPhiConst) {
  RawMachineAssemblerTester<int32_t> m;
  int true_val = 0x44000;
  int false_val = 0x00888;

  Node* cond_node = m.Int32Constant(0);
  Node* true_node = m.Int32Constant(true_val);
  Node* false_node = m.Int32Constant(false_val);

  // x = false_val; while(false) { x = true_val; } return x;
  MLabel body, header;
  MLabel* end = m.Exit();

  m.Goto(&header);
  m.Bind(&header);
245
  Node* phi = m.Phi(kMachInt32, false_node, true_node);
246 247 248 249 250 251 252 253 254 255 256
  m.Branch(cond_node, &body, end);
  m.Bind(&body);
  m.Goto(&header);
  m.Bind(end);
  m.Return(phi);

  CHECK_EQ(false_val, m.Call());
}


TEST(RunLoopPhiParam) {
257
  RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
258 259 260 261 262 263 264

  MLabel blocka, blockb;
  MLabel* end = m.Exit();

  m.Goto(&blocka);

  m.Bind(&blocka);
265 266
  Node* phi = m.Phi(kMachInt32, m.Parameter(1), m.Parameter(2));
  Node* cond = m.Phi(kMachInt32, m.Parameter(0), m.Int32Constant(0));
267 268 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
  m.Branch(cond, &blockb, end);

  m.Bind(&blockb);
  m.Goto(&blocka);

  m.Bind(end);
  m.Return(phi);

  int32_t c1 = 0xa81903b4;
  int32_t c2 = 0x5a1207da;
  int result = m.Call(0, c1, c2);
  CHECK_EQ(c1, result);
  result = m.Call(1, c1, c2);
  CHECK_EQ(c2, result);
}


TEST(RunLoopPhiInduction) {
  RawMachineAssemblerTester<int32_t> m;

  int false_val = 0x10777;

  // x = false_val; while(false) { x++; } return x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* false_node = m.Int32Constant(false_val);

  m.Goto(&header);

  m.Bind(&header);
297
  Node* phi = m.Phi(kMachInt32, false_node, false_node);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  m.Branch(m.Int32Constant(0), &body, end);

  m.Bind(&body);
  Node* add = m.Int32Add(phi, m.Int32Constant(1));
  phi->ReplaceInput(1, add);
  m.Goto(&header);

  m.Bind(end);
  m.Return(phi);

  CHECK_EQ(false_val, m.Call());
}


TEST(RunLoopIncrement) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);

  // x = 0; while(x ^ param) { x++; } return x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* zero = m.Int32Constant(0);

  m.Goto(&header);

  m.Bind(&header);
324
  Node* phi = m.Phi(kMachInt32, zero, zero);
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
  m.Branch(m.WordXor(phi, bt.param0), &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Int32Add(phi, m.Int32Constant(1)));
  m.Goto(&header);

  m.Bind(end);
  bt.AddReturn(phi);

  CHECK_EQ(11, bt.call(11, 0));
  CHECK_EQ(110, bt.call(110, 0));
  CHECK_EQ(176, bt.call(176, 0));
}


TEST(RunLoopIncrement2) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);

  // x = 0; while(x < param) { x++; } return x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* zero = m.Int32Constant(0);

  m.Goto(&header);

  m.Bind(&header);
352
  Node* phi = m.Phi(kMachInt32, zero, zero);
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
  m.Branch(m.Int32LessThan(phi, bt.param0), &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Int32Add(phi, m.Int32Constant(1)));
  m.Goto(&header);

  m.Bind(end);
  bt.AddReturn(phi);

  CHECK_EQ(11, bt.call(11, 0));
  CHECK_EQ(110, bt.call(110, 0));
  CHECK_EQ(176, bt.call(176, 0));
  CHECK_EQ(0, bt.call(-200, 0));
}


TEST(RunLoopIncrement3) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);

  // x = 0; while(x < param) { x++; } return x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* zero = m.Int32Constant(0);

  m.Goto(&header);

  m.Bind(&header);
381
  Node* phi = m.Phi(kMachInt32, zero, zero);
382 383 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
  m.Branch(m.Uint32LessThan(phi, bt.param0), &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Int32Add(phi, m.Int32Constant(1)));
  m.Goto(&header);

  m.Bind(end);
  bt.AddReturn(phi);

  CHECK_EQ(11, bt.call(11, 0));
  CHECK_EQ(110, bt.call(110, 0));
  CHECK_EQ(176, bt.call(176, 0));
  CHECK_EQ(200, bt.call(200, 0));
}


TEST(RunLoopDecrement) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);

  // x = param; while(x) { x--; } return x;
  MLabel header, body;
  MLabel* end = m.Exit();

  m.Goto(&header);

  m.Bind(&header);
409
  Node* phi = m.Phi(kMachInt32, bt.param0, m.Int32Constant(0));
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  m.Branch(phi, &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Int32Sub(phi, m.Int32Constant(1)));
  m.Goto(&header);

  m.Bind(end);
  bt.AddReturn(phi);

  CHECK_EQ(0, bt.call(11, 0));
  CHECK_EQ(0, bt.call(110, 0));
  CHECK_EQ(0, bt.call(197, 0));
}


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
TEST(RunLoopIncrementFloat32) {
  RawMachineAssemblerTester<int32_t> m;

  // x = -3.0f; while(x < 10f) { x = x + 0.5f; } return (int) (double) x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* minus_3 = m.Float32Constant(-3.0f);
  Node* ten = m.Float32Constant(10.0f);

  m.Goto(&header);

  m.Bind(&header);
  Node* phi = m.Phi(kMachFloat32, minus_3, ten);
  m.Branch(m.Float32LessThan(phi, ten), &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Float32Add(phi, m.Float32Constant(0.5f)));
  m.Goto(&header);

  m.Bind(end);
  m.Return(m.ChangeFloat64ToInt32(m.ChangeFloat32ToFloat64(phi)));

  CHECK_EQ(10, m.Call());
}


451 452 453 454 455 456 457 458 459 460 461 462
TEST(RunLoopIncrementFloat64) {
  RawMachineAssemblerTester<int32_t> m;

  // x = -3.0; while(x < 10) { x = x + 0.5; } return (int) x;
  MLabel header, body;
  MLabel* end = m.Exit();
  Node* minus_3 = m.Float64Constant(-3.0);
  Node* ten = m.Float64Constant(10.0);

  m.Goto(&header);

  m.Bind(&header);
463
  Node* phi = m.Phi(kMachFloat64, minus_3, ten);
464 465 466 467 468 469 470
  m.Branch(m.Float64LessThan(phi, ten), &body, end);

  m.Bind(&body);
  phi->ReplaceInput(1, m.Float64Add(phi, m.Float64Constant(0.5)));
  m.Goto(&header);

  m.Bind(end);
471
  m.Return(m.ChangeFloat64ToInt32(phi));
472 473 474 475 476

  CHECK_EQ(10, m.Call());
}


477 478 479 480 481
TEST(RunSwitch1) {
  RawMachineAssemblerTester<int32_t> m;

  int constant = 11223344;

482 483 484 485 486
  MLabel block0, block1, def, end;
  MLabel* case_labels[] = {&block0, &block1};
  int32_t case_values[] = {0, 1};
  m.Switch(m.Int32Constant(0), &def, case_values, case_labels,
           arraysize(case_labels));
487 488 489 490
  m.Bind(&block0);
  m.Goto(&end);
  m.Bind(&block1);
  m.Goto(&end);
491 492
  m.Bind(&def);
  m.Goto(&end);
493 494 495 496 497 498 499 500 501 502
  m.Bind(&end);
  m.Return(m.Int32Constant(constant));

  CHECK_EQ(constant, m.Call());
}


TEST(RunSwitch2) {
  RawMachineAssemblerTester<int32_t> m(kMachInt32);

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
  MLabel blocka, blockb, blockc;
  MLabel* case_labels[] = {&blocka, &blockb};
  int32_t case_values[] = {std::numeric_limits<int32_t>::min(),
                           std::numeric_limits<int32_t>::max()};
  m.Switch(m.Parameter(0), &blockc, case_values, case_labels,
           arraysize(case_labels));
  m.Bind(&blocka);
  m.Return(m.Int32Constant(-1));
  m.Bind(&blockb);
  m.Return(m.Int32Constant(1));
  m.Bind(&blockc);
  m.Return(m.Int32Constant(0));

  CHECK_EQ(1, m.Call(std::numeric_limits<int32_t>::max()));
  CHECK_EQ(-1, m.Call(std::numeric_limits<int32_t>::min()));
  for (int i = -100; i < 100; i += 25) {
    CHECK_EQ(0, m.Call(i));
  }
}


TEST(RunSwitch3) {
  RawMachineAssemblerTester<int32_t> m(kMachInt32);

  MLabel blocka, blockb, blockc;
  MLabel* case_labels[] = {&blocka, &blockb};
  int32_t case_values[] = {std::numeric_limits<int32_t>::min() + 0,
                           std::numeric_limits<int32_t>::min() + 1};
  m.Switch(m.Parameter(0), &blockc, case_values, case_labels,
           arraysize(case_labels));
  m.Bind(&blocka);
  m.Return(m.Int32Constant(0));
  m.Bind(&blockb);
  m.Return(m.Int32Constant(1));
  m.Bind(&blockc);
  m.Return(m.Int32Constant(2));

  CHECK_EQ(0, m.Call(std::numeric_limits<int32_t>::min() + 0));
  CHECK_EQ(1, m.Call(std::numeric_limits<int32_t>::min() + 1));
  for (int i = -100; i < 100; i += 25) {
    CHECK_EQ(2, m.Call(i));
  }
}


TEST(RunSwitch4) {
  RawMachineAssemblerTester<int32_t> m(kMachInt32);

  const size_t kNumCases = 512;
  const size_t kNumValues = kNumCases + 1;
  int32_t values[kNumValues];
554 555
  m.main_isolate()->random_number_generator()->NextBytes(values,
                                                         sizeof(values));
556 557 558 559
  MLabel end, def;
  int32_t case_values[kNumCases];
  MLabel* case_labels[kNumCases];
  Node* results[kNumValues];
560
  for (size_t i = 0; i < kNumCases; ++i) {
561 562
    case_values[i] = static_cast<int32_t>(i);
    case_labels[i] = new (m.main_zone()->New(sizeof(MLabel))) MLabel;
563
  }
564 565
  m.Switch(m.Parameter(0), &def, case_values, case_labels,
           arraysize(case_labels));
566
  for (size_t i = 0; i < kNumCases; ++i) {
567
    m.Bind(case_labels[i]);
568 569 570
    results[i] = m.Int32Constant(values[i]);
    m.Goto(&end);
  }
571 572 573
  m.Bind(&def);
  results[kNumCases] = m.Int32Constant(values[kNumCases]);
  m.Goto(&end);
574 575 576 577 578 579
  m.Bind(&end);
  const int num_results = static_cast<int>(arraysize(results));
  Node* phi =
      m.NewNode(m.common()->Phi(kMachInt32, num_results), num_results, results);
  m.Return(phi);

580
  for (size_t i = 0; i < kNumValues; ++i) {
581 582 583 584 585
    CHECK_EQ(values[i], m.Call(static_cast<int>(i)));
  }
}


586 587 588 589
TEST(RunLoadInt32) {
  RawMachineAssemblerTester<int32_t> m;

  int32_t p1 = 0;  // loads directly from this location.
590
  m.Return(m.LoadFromPointer(&p1, kMachInt32));
591 592 593 594 595 596 597 598 599 600 601 602 603 604

  FOR_INT32_INPUTS(i) {
    p1 = *i;
    CHECK_EQ(p1, m.Call());
  }
}


TEST(RunLoadInt32Offset) {
  int32_t p1 = 0;  // loads directly from this location.

  int32_t offsets[] = {-2000000, -100, -101, 1,          3,
                       7,        120,  2000, 2000000000, 0xff};

605
  for (size_t i = 0; i < arraysize(offsets); i++) {
606 607 608 609
    RawMachineAssemblerTester<int32_t> m;
    int32_t offset = offsets[i];
    byte* pointer = reinterpret_cast<byte*>(&p1) - offset;
    // generate load [#base + #index]
610
    m.Return(m.LoadFromPointer(pointer, kMachInt32, offset));
611 612 613 614 615 616 617 618 619

    FOR_INT32_INPUTS(j) {
      p1 = *j;
      CHECK_EQ(p1, m.Call());
    }
  }
}


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
TEST(RunLoadStoreFloat32Offset) {
  float p1 = 0.0f;  // loads directly from this location.
  float p2 = 0.0f;  // and stores directly into this location.

  FOR_INT32_INPUTS(i) {
    int32_t magic = 0x2342aabb + *i * 3;
    RawMachineAssemblerTester<int32_t> m;
    int32_t offset = *i;
    byte* from = reinterpret_cast<byte*>(&p1) - offset;
    byte* to = reinterpret_cast<byte*>(&p2) - offset;
    // generate load [#base + #index]
    Node* load =
        m.Load(kMachFloat32, m.PointerConstant(from), m.IntPtrConstant(offset));
    m.Store(kMachFloat32, m.PointerConstant(to), m.IntPtrConstant(offset),
            load);
    m.Return(m.Int32Constant(magic));

    FOR_FLOAT32_INPUTS(j) {
      p1 = *j;
      p2 = *j - 5;
      CHECK_EQ(magic, m.Call());
      CheckDoubleEq(p1, p2);
    }
  }
}


647 648 649 650 651 652 653 654 655 656 657
TEST(RunLoadStoreFloat64Offset) {
  double p1 = 0;  // loads directly from this location.
  double p2 = 0;  // and stores directly into this location.

  FOR_INT32_INPUTS(i) {
    int32_t magic = 0x2342aabb + *i * 3;
    RawMachineAssemblerTester<int32_t> m;
    int32_t offset = *i;
    byte* from = reinterpret_cast<byte*>(&p1) - offset;
    byte* to = reinterpret_cast<byte*>(&p2) - offset;
    // generate load [#base + #index]
658
    Node* load =
659 660 661
        m.Load(kMachFloat64, m.PointerConstant(from), m.IntPtrConstant(offset));
    m.Store(kMachFloat64, m.PointerConstant(to), m.IntPtrConstant(offset),
            load);
662 663 664 665 666 667
    m.Return(m.Int32Constant(magic));

    FOR_FLOAT64_INPUTS(j) {
      p1 = *j;
      p2 = *j - 5;
      CHECK_EQ(magic, m.Call());
668
      CheckDoubleEq(p1, p2);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    }
  }
}


TEST(RunInt32AddP) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);

  bt.AddReturn(m.Int32Add(bt.param0, bt.param1));

  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      // Use uint32_t because signed overflow is UB in C.
      int expected = static_cast<int32_t>(*i + *j);
      CHECK_EQ(expected, bt.call(*i, *j));
    }
  }
}


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 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
TEST(RunInt32AddAndWord32EqualP) {
  {
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
    m.Return(m.Int32Add(m.Parameter(0),
                        m.Word32Equal(m.Parameter(1), m.Parameter(2))));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>(bit_cast<uint32_t>(*i) + (*j == *k));
          CHECK_EQ(expected, m.Call(*i, *j, *k));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
    m.Return(m.Int32Add(m.Word32Equal(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>((*i == *j) + bit_cast<uint32_t>(*k));
          CHECK_EQ(expected, m.Call(*i, *j, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddAndWord32EqualImm) {
  {
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
      m.Return(m.Int32Add(m.Int32Constant(*i),
                          m.Word32Equal(m.Parameter(0), m.Parameter(1))));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>(bit_cast<uint32_t>(*i) + (*j == *k));
          CHECK_EQ(expected, m.Call(*j, *k));
        }
      }
    }
  }
  {
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
      m.Return(m.Int32Add(m.Word32Equal(m.Int32Constant(*i), m.Parameter(0)),
                          m.Parameter(1)));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>((*i == *j) + bit_cast<uint32_t>(*k));
          CHECK_EQ(expected, m.Call(*j, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddAndWord32NotEqualP) {
  {
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
    m.Return(m.Int32Add(m.Parameter(0),
                        m.Word32NotEqual(m.Parameter(1), m.Parameter(2))));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>(bit_cast<uint32_t>(*i) + (*j != *k));
          CHECK_EQ(expected, m.Call(*i, *j, *k));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
    m.Return(m.Int32Add(m.Word32NotEqual(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>((*i != *j) + bit_cast<uint32_t>(*k));
          CHECK_EQ(expected, m.Call(*i, *j, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddAndWord32NotEqualImm) {
  {
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
      m.Return(m.Int32Add(m.Int32Constant(*i),
                          m.Word32NotEqual(m.Parameter(0), m.Parameter(1))));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>(bit_cast<uint32_t>(*i) + (*j != *k));
          CHECK_EQ(expected, m.Call(*j, *k));
        }
      }
    }
  }
  {
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
      m.Return(m.Int32Add(m.Word32NotEqual(m.Int32Constant(*i), m.Parameter(0)),
                          m.Parameter(1)));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t const expected =
              bit_cast<int32_t>((*i != *j) + bit_cast<uint32_t>(*k));
          CHECK_EQ(expected, m.Call(*j, *k));
        }
      }
    }
  }
}


826 827
TEST(RunInt32AddAndWord32SarP) {
  {
828
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32, kMachUint32);
829 830 831 832
    m.Return(m.Int32Add(m.Parameter(0),
                        m.Word32Sar(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
833
        FOR_UINT32_SHIFTS(shift) {
834 835 836 837 838 839 840 841
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = *i + (*j >> shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
842
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachUint32, kMachUint32);
843 844 845
    m.Return(m.Int32Add(m.Word32Sar(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
846
      FOR_UINT32_SHIFTS(shift) {
847 848 849 850 851 852 853 854 855 856 857 858 859
        FOR_UINT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = (*i >> shift) + *k;
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddAndWord32ShlP) {
  {
860
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32, kMachUint32);
861 862 863 864
    m.Return(m.Int32Add(m.Parameter(0),
                        m.Word32Shl(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
865
        FOR_UINT32_SHIFTS(shift) {
866 867 868 869 870 871 872 873
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = *i + (*j << shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
874
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachUint32, kMachUint32);
875 876 877
    m.Return(m.Int32Add(m.Word32Shl(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
878
      FOR_UINT32_SHIFTS(shift) {
879 880 881 882 883 884 885 886 887 888 889 890 891
        FOR_UINT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = (*i << shift) + *k;
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddAndWord32ShrP) {
  {
892
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
893 894 895 896
    m.Return(m.Int32Add(m.Parameter(0),
                        m.Word32Shr(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
897
        FOR_UINT32_SHIFTS(shift) {
898 899 900 901 902 903 904 905
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = *i + (*j >> shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
906
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
907 908 909
    m.Return(m.Int32Add(m.Word32Shr(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_UINT32_INPUTS(i) {
910
      FOR_UINT32_SHIFTS(shift) {
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
        FOR_UINT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = (*i >> shift) + *k;
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32AddInBranch) {
  static const int32_t constant = 987654321;
  {
    RawMachineAssemblerTester<int32_t> m;
926
    Int32BinopTester bt(&m);
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    MLabel blocka, blockb;
    m.Branch(
        m.Word32Equal(m.Int32Add(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i + *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
944
    Int32BinopTester bt(&m);
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
    MLabel blocka, blockb;
    m.Branch(
        m.Word32NotEqual(m.Int32Add(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i + *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
962
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
963 964 965 966 967 968 969 970 971
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Int32Add(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
972
        uint32_t expected = (*i + *j) == 0 ? constant : 0 - constant;
973
        CHECK_EQ(expected, m.Call(*j));
974 975 976 977 978
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
979
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
980 981 982 983 984 985 986 987 988
      MLabel blocka, blockb;
      m.Branch(m.Word32NotEqual(m.Int32Add(m.Int32Constant(*i), m.Parameter(0)),
                                m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
989
        uint32_t expected = (*i + *j) != 0 ? constant : 0 - constant;
990
        CHECK_EQ(expected, m.Call(*j));
991 992 993 994 995
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
996 997 998
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
999
    for (size_t n = 0; n < arraysize(shops); n++) {
1000 1001
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Int32Add(m.Parameter(0),
                                        m.NewNode(shops[n], m.Parameter(1),
                                                  m.Parameter(2))),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
1014
          FOR_UINT32_SHIFTS(shift) {
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
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = ((*i + right) == 0) ? constant : 0 - constant;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunInt32AddInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
1042
    Uint32BinopTester bt(&m);
1043 1044 1045 1046
    bt.AddReturn(
        m.Word32Equal(m.Int32Add(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1047
        uint32_t expected = (*i + *j) == 0;
1048
        CHECK_EQ(expected, bt.call(*i, *j));
1049 1050 1051 1052 1053
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1054
    Uint32BinopTester bt(&m);
1055 1056 1057 1058
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Int32Add(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1059
        uint32_t expected = (*i + *j) == 0;
1060
        CHECK_EQ(expected, bt.call(*i, *j));
1061 1062 1063 1064 1065
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1066
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1067 1068 1069
      m.Return(m.Word32Equal(m.Int32Add(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
1070
        uint32_t expected = (*i + *j) == 0;
1071
        CHECK_EQ(expected, m.Call(*j));
1072 1073 1074 1075 1076
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1077
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1078 1079 1080
      m.Return(m.Word32Equal(m.Int32Add(m.Parameter(0), m.Int32Constant(*i)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
1081
        uint32_t expected = (*j + *i) == 0;
1082
        CHECK_EQ(expected, m.Call(*j));
1083 1084 1085 1086 1087
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
1088 1089 1090
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
1091
    for (size_t n = 0; n < arraysize(shops); n++) {
1092 1093
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
1094 1095 1096 1097 1098 1099
      m.Return(m.Word32Equal(
          m.Int32Add(m.Parameter(0),
                     m.NewNode(shops[n], m.Parameter(1), m.Parameter(2))),
          m.Int32Constant(0)));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
1100
          FOR_UINT32_SHIFTS(shift) {
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
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = (*i + right) == 0;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunInt32SubP) {
  RawMachineAssemblerTester<int32_t> m;
1127
  Uint32BinopTester bt(&m);
1128 1129 1130 1131 1132

  m.Return(m.Int32Sub(bt.param0, bt.param1));

  FOR_UINT32_INPUTS(i) {
    FOR_UINT32_INPUTS(j) {
1133
      uint32_t expected = static_cast<int32_t>(*i - *j);
1134
      CHECK_EQ(expected, bt.call(*i, *j));
1135 1136 1137 1138 1139 1140 1141 1142
    }
  }
}


TEST(RunInt32SubImm) {
  {
    FOR_UINT32_INPUTS(i) {
1143
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1144 1145
      m.Return(m.Int32Sub(m.Int32Constant(*i), m.Parameter(0)));
      FOR_UINT32_INPUTS(j) {
1146
        uint32_t expected = *i - *j;
1147
        CHECK_EQ(expected, m.Call(*j));
1148 1149 1150 1151 1152
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1153
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1154 1155
      m.Return(m.Int32Sub(m.Parameter(0), m.Int32Constant(*i)));
      FOR_UINT32_INPUTS(j) {
1156
        uint32_t expected = *j - *i;
1157
        CHECK_EQ(expected, m.Call(*j));
1158 1159 1160 1161 1162 1163 1164 1165
      }
    }
  }
}


TEST(RunInt32SubAndWord32SarP) {
  {
1166
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32, kMachUint32);
1167 1168 1169 1170
    m.Return(m.Int32Sub(m.Parameter(0),
                        m.Word32Sar(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
1171
        FOR_UINT32_SHIFTS(shift) {
1172 1173 1174 1175 1176 1177 1178
          int32_t expected = *i - (*j >> shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
1179
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachUint32, kMachUint32);
1180 1181 1182
    m.Return(m.Int32Sub(m.Word32Sar(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
1183
      FOR_UINT32_SHIFTS(shift) {
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
        FOR_UINT32_INPUTS(k) {
          int32_t expected = (*i >> shift) - *k;
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32SubAndWord32ShlP) {
  {
1196
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32, kMachUint32);
1197 1198 1199 1200
    m.Return(m.Int32Sub(m.Parameter(0),
                        m.Word32Shl(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
1201
        FOR_UINT32_SHIFTS(shift) {
1202 1203 1204 1205 1206 1207 1208
          int32_t expected = *i - (*j << shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
1209
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachUint32, kMachUint32);
1210 1211 1212
    m.Return(m.Int32Sub(m.Word32Shl(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
1213
      FOR_UINT32_SHIFTS(shift) {
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        FOR_UINT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
          int32_t expected = (*i << shift) - *k;
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32SubAndWord32ShrP) {
  {
1227 1228
    RawMachineAssemblerTester<uint32_t> m(kMachUint32, kMachUint32,
                                          kMachUint32);
1229 1230 1231 1232
    m.Return(m.Int32Sub(m.Parameter(0),
                        m.Word32Shr(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1233
        FOR_UINT32_SHIFTS(shift) {
1234
          // Use uint32_t because signed overflow is UB in C.
1235 1236
          uint32_t expected = *i - (*j >> shift);
          CHECK_EQ(expected, m.Call(*i, *j, shift));
1237 1238 1239 1240 1241
        }
      }
    }
  }
  {
1242 1243
    RawMachineAssemblerTester<uint32_t> m(kMachUint32, kMachUint32,
                                          kMachUint32);
1244 1245 1246
    m.Return(m.Int32Sub(m.Word32Shr(m.Parameter(0), m.Parameter(1)),
                        m.Parameter(2)));
    FOR_UINT32_INPUTS(i) {
1247
      FOR_UINT32_SHIFTS(shift) {
1248 1249
        FOR_UINT32_INPUTS(k) {
          // Use uint32_t because signed overflow is UB in C.
1250
          uint32_t expected = (*i >> shift) - *k;
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunInt32SubInBranch) {
  static const int constant = 987654321;
  {
    RawMachineAssemblerTester<int32_t> m;
1263
    Int32BinopTester bt(&m);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    MLabel blocka, blockb;
    m.Branch(
        m.Word32Equal(m.Int32Sub(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i - *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1281
    Int32BinopTester bt(&m);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    MLabel blocka, blockb;
    m.Branch(
        m.Word32NotEqual(m.Int32Sub(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i - *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1299
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1300 1301 1302 1303 1304 1305 1306 1307 1308
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Int32Sub(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
1309
        uint32_t expected = (*i - *j) == 0 ? constant : 0 - constant;
1310 1311 1312 1313 1314 1315
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1316
      RawMachineAssemblerTester<int32_t> m(kMachUint32);
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
      MLabel blocka, blockb;
      m.Branch(m.Word32NotEqual(m.Int32Sub(m.Int32Constant(*i), m.Parameter(0)),
                                m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i - *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
1333 1334 1335
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
1336
    for (size_t n = 0; n < arraysize(shops); n++) {
1337 1338
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Int32Sub(m.Parameter(0),
                                        m.NewNode(shops[n], m.Parameter(1),
                                                  m.Parameter(2))),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
1351
          FOR_UINT32_SHIFTS(shift) {
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
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = ((*i - right) == 0) ? constant : 0 - constant;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunInt32SubInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
1379
    Uint32BinopTester bt(&m);
1380 1381 1382 1383
    bt.AddReturn(
        m.Word32Equal(m.Int32Sub(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1384
        uint32_t expected = (*i - *j) == 0;
1385
        CHECK_EQ(expected, bt.call(*i, *j));
1386 1387 1388 1389 1390
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1391
    Uint32BinopTester bt(&m);
1392 1393 1394 1395
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Int32Sub(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1396
        uint32_t expected = (*i - *j) == 0;
1397
        CHECK_EQ(expected, bt.call(*i, *j));
1398 1399 1400 1401 1402
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1403
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1404 1405 1406
      m.Return(m.Word32Equal(m.Int32Sub(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
1407
        uint32_t expected = (*i - *j) == 0;
1408
        CHECK_EQ(expected, m.Call(*j));
1409 1410 1411 1412 1413
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1414
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1415 1416 1417
      m.Return(m.Word32Equal(m.Int32Sub(m.Parameter(0), m.Int32Constant(*i)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
1418
        uint32_t expected = (*j - *i) == 0;
1419
        CHECK_EQ(expected, m.Call(*j));
1420 1421 1422 1423 1424
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
1425 1426 1427
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
1428
    for (size_t n = 0; n < arraysize(shops); n++) {
1429 1430
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
1431 1432 1433 1434 1435 1436
      m.Return(m.Word32Equal(
          m.Int32Sub(m.Parameter(0),
                     m.NewNode(shops[n], m.Parameter(1), m.Parameter(2))),
          m.Int32Constant(0)));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
1437
          FOR_UINT32_SHIFTS(shift) {
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
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = (*i - right) == 0;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunInt32MulP) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Int32Mul(bt.param0, bt.param1));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int expected = static_cast<int32_t>(*i * *j);
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1476
    Uint32BinopTester bt(&m);
1477 1478 1479
    bt.AddReturn(m.Int32Mul(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1480
        uint32_t expected = *i * *j;
1481
        CHECK_EQ(expected, bt.call(*i, *j));
1482 1483 1484 1485 1486 1487
      }
    }
  }
}


1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
TEST(RunInt32MulHighP) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
  bt.AddReturn(m.Int32MulHigh(bt.param0, bt.param1));
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      int32_t expected = static_cast<int32_t>(
          (static_cast<int64_t>(*i) * static_cast<int64_t>(*j)) >> 32);
      CHECK_EQ(expected, bt.call(*i, *j));
    }
  }
}


1502 1503 1504
TEST(RunInt32MulImm) {
  {
    FOR_UINT32_INPUTS(i) {
1505
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1506 1507
      m.Return(m.Int32Mul(m.Int32Constant(*i), m.Parameter(0)));
      FOR_UINT32_INPUTS(j) {
1508
        uint32_t expected = *i * *j;
1509
        CHECK_EQ(expected, m.Call(*j));
1510 1511 1512 1513 1514
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1515
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1516 1517
      m.Return(m.Int32Mul(m.Parameter(0), m.Int32Constant(*i)));
      FOR_UINT32_INPUTS(j) {
1518
        uint32_t expected = *j * *i;
1519
        CHECK_EQ(expected, m.Call(*j));
1520 1521 1522 1523 1524 1525 1526
      }
    }
  }
}


TEST(RunInt32MulAndInt32AddP) {
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
  {
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        RawMachineAssemblerTester<int32_t> m(kMachInt32);
        int32_t p0 = *i;
        int32_t p1 = *j;
        m.Return(m.Int32Add(m.Int32Constant(p0),
                            m.Int32Mul(m.Parameter(0), m.Int32Constant(p1))));
        FOR_INT32_INPUTS(k) {
          int32_t p2 = *k;
          int expected = p0 + static_cast<int32_t>(p1 * p2);
          CHECK_EQ(expected, m.Call(p2));
        }
      }
    }
  }
1543
  {
1544
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    m.Return(
        m.Int32Add(m.Parameter(0), m.Int32Mul(m.Parameter(1), m.Parameter(2))));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          int32_t p0 = *i;
          int32_t p1 = *j;
          int32_t p2 = *k;
          int expected = p0 + static_cast<int32_t>(p1 * p2);
          CHECK_EQ(expected, m.Call(p0, p1, p2));
        }
      }
    }
  }
  {
1560
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachInt32);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    m.Return(
        m.Int32Add(m.Int32Mul(m.Parameter(0), m.Parameter(1)), m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          int32_t p0 = *i;
          int32_t p1 = *j;
          int32_t p2 = *k;
          int expected = static_cast<int32_t>(p0 * p1) + p2;
          CHECK_EQ(expected, m.Call(p0, p1, p2));
        }
      }
    }
  }
  {
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m;
      Int32BinopTester bt(&m);
      bt.AddReturn(
          m.Int32Add(m.Int32Constant(*i), m.Int32Mul(bt.param0, bt.param1)));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          int32_t p0 = *j;
          int32_t p1 = *k;
          int expected = *i + static_cast<int32_t>(p0 * p1);
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


TEST(RunInt32MulAndInt32SubP) {
  {
1596
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32, kMachInt32);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
    m.Return(
        m.Int32Sub(m.Parameter(0), m.Int32Mul(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          uint32_t p0 = *i;
          int32_t p1 = *j;
          int32_t p2 = *k;
          // Use uint32_t because signed overflow is UB in C.
          int expected = p0 - static_cast<uint32_t>(p1 * p2);
          CHECK_EQ(expected, m.Call(p0, p1, p2));
        }
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m;
      Int32BinopTester bt(&m);
      bt.AddReturn(
          m.Int32Sub(m.Int32Constant(*i), m.Int32Mul(bt.param0, bt.param1)));
      FOR_INT32_INPUTS(j) {
        FOR_INT32_INPUTS(k) {
          int32_t p0 = *j;
          int32_t p1 = *k;
          // Use uint32_t because signed overflow is UB in C.
          int expected = *i - static_cast<uint32_t>(p0 * p1);
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
TEST(RunUint32MulHighP) {
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
  bt.AddReturn(m.Uint32MulHigh(bt.param0, bt.param1));
  FOR_UINT32_INPUTS(i) {
    FOR_UINT32_INPUTS(j) {
      int32_t expected = bit_cast<int32_t>(static_cast<uint32_t>(
          (static_cast<uint64_t>(*i) * static_cast<uint64_t>(*j)) >> 32));
      CHECK_EQ(expected, bt.call(bit_cast<int32_t>(*i), bit_cast<int32_t>(*j)));
    }
  }
}


1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
TEST(RunInt32DivP) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Int32Div(bt.param0, bt.param1));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int p0 = *i;
        int p1 = *j;
        if (p1 != 0 && (static_cast<uint32_t>(p0) != 0x80000000 || p1 != -1)) {
          int expected = static_cast<int32_t>(p0 / p1);
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Int32Add(bt.param0, m.Int32Div(bt.param0, bt.param1)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int p0 = *i;
        int p1 = *j;
        if (p1 != 0 && (static_cast<uint32_t>(p0) != 0x80000000 || p1 != -1)) {
          int expected = static_cast<int32_t>(p0 + (p0 / p1));
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


1680
TEST(RunUint32DivP) {
1681 1682 1683
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
1684
    bt.AddReturn(m.Uint32Div(bt.param0, bt.param1));
1685 1686 1687 1688 1689
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t p0 = *i;
        uint32_t p1 = *j;
        if (p1 != 0) {
1690
          int32_t expected = bit_cast<int32_t>(p0 / p1);
1691 1692 1693 1694 1695 1696 1697 1698
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
1699
    bt.AddReturn(m.Int32Add(bt.param0, m.Uint32Div(bt.param0, bt.param1)));
1700 1701 1702 1703 1704
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t p0 = *i;
        uint32_t p1 = *j;
        if (p1 != 0) {
1705
          int32_t expected = bit_cast<int32_t>(p0 + (p0 / p1));
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


TEST(RunInt32ModP) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Int32Mod(bt.param0, bt.param1));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int p0 = *i;
        int p1 = *j;
        if (p1 != 0 && (static_cast<uint32_t>(p0) != 0x80000000 || p1 != -1)) {
          int expected = static_cast<int32_t>(p0 % p1);
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Int32Add(bt.param0, m.Int32Mod(bt.param0, bt.param1)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int p0 = *i;
        int p1 = *j;
        if (p1 != 0 && (static_cast<uint32_t>(p0) != 0x80000000 || p1 != -1)) {
          int expected = static_cast<int32_t>(p0 + (p0 % p1));
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


1748
TEST(RunUint32ModP) {
1749 1750
  {
    RawMachineAssemblerTester<int32_t> m;
1751
    Uint32BinopTester bt(&m);
1752
    bt.AddReturn(m.Uint32Mod(bt.param0, bt.param1));
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t p0 = *i;
        uint32_t p1 = *j;
        if (p1 != 0) {
          uint32_t expected = static_cast<uint32_t>(p0 % p1);
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1766
    Uint32BinopTester bt(&m);
1767
    bt.AddReturn(m.Int32Add(bt.param0, m.Uint32Mod(bt.param0, bt.param1)));
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t p0 = *i;
        uint32_t p1 = *j;
        if (p1 != 0) {
          uint32_t expected = static_cast<uint32_t>(p0 + (p0 % p1));
          CHECK_EQ(expected, bt.call(p0, p1));
        }
      }
    }
  }
}


TEST(RunWord32AndP) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32And(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1789
        int32_t expected = *i & *j;
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32And(bt.param0, m.Word32Not(bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1800
        int32_t expected = *i & ~(*j);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32And(m.Word32Not(bt.param0), bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
1811
        int32_t expected = ~(*i) & *j;
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
}


TEST(RunWord32AndAndWord32ShlP) {
  {
    RawMachineAssemblerTester<int32_t> m;
1822
    Uint32BinopTester bt(&m);
1823 1824 1825 1826 1827
    bt.AddReturn(
        m.Word32Shl(bt.param0, m.Word32And(bt.param1, m.Int32Constant(0x1f))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i << (*j & 0x1f);
1828
        CHECK_EQ(expected, bt.call(*i, *j));
1829 1830 1831 1832 1833
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1834
    Uint32BinopTester bt(&m);
1835 1836 1837 1838 1839
    bt.AddReturn(
        m.Word32Shl(bt.param0, m.Word32And(m.Int32Constant(0x1f), bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i << (0x1f & *j);
1840
        CHECK_EQ(expected, bt.call(*i, *j));
1841 1842 1843 1844 1845 1846 1847 1848 1849
      }
    }
  }
}


TEST(RunWord32AndAndWord32ShrP) {
  {
    RawMachineAssemblerTester<int32_t> m;
1850
    Uint32BinopTester bt(&m);
1851 1852 1853 1854 1855
    bt.AddReturn(
        m.Word32Shr(bt.param0, m.Word32And(bt.param1, m.Int32Constant(0x1f))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i >> (*j & 0x1f);
1856
        CHECK_EQ(expected, bt.call(*i, *j));
1857 1858 1859 1860 1861
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1862
    Uint32BinopTester bt(&m);
1863 1864 1865 1866 1867
    bt.AddReturn(
        m.Word32Shr(bt.param0, m.Word32And(m.Int32Constant(0x1f), bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i >> (0x1f & *j);
1868
        CHECK_EQ(expected, bt.call(*i, *j));
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
      }
    }
  }
}


TEST(RunWord32AndAndWord32SarP) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Sar(bt.param0, m.Word32And(bt.param1, m.Int32Constant(0x1f))));
    FOR_INT32_INPUTS(i) {
1882 1883
      FOR_INT32_INPUTS(j) {
        int32_t expected = *i >> (*j & 0x1f);
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Sar(bt.param0, m.Word32And(m.Int32Constant(0x1f), bt.param1)));
    FOR_INT32_INPUTS(i) {
1894
      FOR_INT32_INPUTS(j) {
1895
        int32_t expected = *i >> (0x1f & *j);
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
}


TEST(RunWord32AndImm) {
  {
    FOR_UINT32_INPUTS(i) {
1906
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1907 1908 1909
      m.Return(m.Word32And(m.Int32Constant(*i), m.Parameter(0)));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i & *j;
1910
        CHECK_EQ(expected, m.Call(*j));
1911 1912 1913 1914 1915
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1916
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
1917 1918 1919
      m.Return(m.Word32And(m.Int32Constant(*i), m.Word32Not(m.Parameter(0))));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i & ~(*j);
1920
        CHECK_EQ(expected, m.Call(*j));
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
      }
    }
  }
}


TEST(RunWord32AndInBranch) {
  static const int constant = 987654321;
  {
    RawMachineAssemblerTester<int32_t> m;
1931
    Int32BinopTester bt(&m);
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
    MLabel blocka, blockb;
    m.Branch(
        m.Word32Equal(m.Word32And(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i & *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
1949
    Int32BinopTester bt(&m);
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
    MLabel blocka, blockb;
    m.Branch(
        m.Word32NotEqual(m.Word32And(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i & *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1967
      RawMachineAssemblerTester<int32_t> m(kMachUint32);
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32And(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i & *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
1984
      RawMachineAssemblerTester<int32_t> m(kMachUint32);
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
      MLabel blocka, blockb;
      m.Branch(
          m.Word32NotEqual(m.Word32And(m.Int32Constant(*i), m.Parameter(0)),
                           m.Int32Constant(0)),
          &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i & *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
2002 2003 2004
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
2005
    for (size_t n = 0; n < arraysize(shops); n++) {
2006 2007
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32And(m.Parameter(0),
                                         m.NewNode(shops[n], m.Parameter(1),
                                                   m.Parameter(2))),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
2020
          FOR_UINT32_SHIFTS(shift) {
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = ((*i & right) == 0) ? constant : 0 - constant;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunWord32AndInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
2048
    Uint32BinopTester bt(&m);
2049 2050 2051 2052
    bt.AddReturn(
        m.Word32Equal(m.Word32And(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2053
        uint32_t expected = (*i & *j) == 0;
2054
        CHECK_EQ(expected, bt.call(*i, *j));
2055 2056 2057 2058 2059
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2060
    Uint32BinopTester bt(&m);
2061 2062 2063 2064
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32And(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2065
        uint32_t expected = (*i & *j) == 0;
2066
        CHECK_EQ(expected, bt.call(*i, *j));
2067 2068 2069 2070 2071
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2072
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2073 2074 2075
      m.Return(m.Word32Equal(m.Word32And(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
2076
        uint32_t expected = (*i & *j) == 0;
2077
        CHECK_EQ(expected, m.Call(*j));
2078 2079 2080 2081 2082
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2083
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2084 2085 2086
      m.Return(m.Word32Equal(m.Word32And(m.Parameter(0), m.Int32Constant(*i)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
2087
        uint32_t expected = (*j & *i) == 0;
2088
        CHECK_EQ(expected, m.Call(*j));
2089 2090 2091 2092 2093 2094 2095 2096 2097
      }
    }
  }
}


TEST(RunWord32OrP) {
  {
    RawMachineAssemblerTester<int32_t> m;
2098
    Uint32BinopTester bt(&m);
2099 2100 2101 2102
    bt.AddReturn(m.Word32Or(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i | *j;
2103
        CHECK_EQ(expected, bt.call(*i, *j));
2104 2105 2106 2107 2108
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2109
    Uint32BinopTester bt(&m);
2110 2111 2112 2113
    bt.AddReturn(m.Word32Or(bt.param0, m.Word32Not(bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i | ~(*j);
2114
        CHECK_EQ(expected, bt.call(*i, *j));
2115 2116 2117 2118 2119
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2120
    Uint32BinopTester bt(&m);
2121 2122 2123 2124
    bt.AddReturn(m.Word32Or(m.Word32Not(bt.param0), bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = ~(*i) | *j;
2125
        CHECK_EQ(expected, bt.call(*i, *j));
2126 2127 2128 2129 2130 2131 2132 2133 2134
      }
    }
  }
}


TEST(RunWord32OrImm) {
  {
    FOR_UINT32_INPUTS(i) {
2135
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2136 2137 2138
      m.Return(m.Word32Or(m.Int32Constant(*i), m.Parameter(0)));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i | *j;
2139
        CHECK_EQ(expected, m.Call(*j));
2140 2141 2142 2143 2144
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2145
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2146 2147 2148
      m.Return(m.Word32Or(m.Int32Constant(*i), m.Word32Not(m.Parameter(0))));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i | ~(*j);
2149
        CHECK_EQ(expected, m.Call(*j));
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
      }
    }
  }
}


TEST(RunWord32OrInBranch) {
  static const int constant = 987654321;
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    MLabel blocka, blockb;
    m.Branch(
        m.Word32Equal(m.Word32Or(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
2169 2170
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
        int32_t expected = (*i | *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    MLabel blocka, blockb;
    m.Branch(
        m.Word32NotEqual(m.Word32Or(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
2187 2188
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
2189 2190 2191 2192 2193 2194
        int32_t expected = (*i | *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
2195 2196
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
2197 2198 2199 2200 2201 2202 2203 2204
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32Or(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
2205
      FOR_INT32_INPUTS(j) {
2206 2207 2208 2209 2210 2211
        int32_t expected = (*i | *j) == 0 ? constant : 0 - constant;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
2212 2213
    FOR_INT32_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
2214 2215 2216 2217 2218 2219 2220 2221
      MLabel blocka, blockb;
      m.Branch(m.Word32NotEqual(m.Word32Or(m.Int32Constant(*i), m.Parameter(0)),
                                m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
2222
      FOR_INT32_INPUTS(j) {
2223 2224 2225 2226 2227 2228 2229
        int32_t expected = (*i | *j) != 0 ? constant : 0 - constant;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
2230 2231 2232
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
2233
    for (size_t n = 0; n < arraysize(shops); n++) {
2234 2235
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32Or(m.Parameter(0),
                                        m.NewNode(shops[n], m.Parameter(1),
                                                  m.Parameter(2))),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
2248
          FOR_UINT32_SHIFTS(shift) {
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = ((*i | right) == 0) ? constant : 0 - constant;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunWord32OrInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
2276
    Int32BinopTester bt(&m);
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
    bt.AddReturn(
        m.Word32Equal(m.Word32Or(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i | *j) == 0;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2288
    Int32BinopTester bt(&m);
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32Or(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
        int32_t expected = (*i | *j) == 0;
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2300
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2301 2302 2303
      m.Return(m.Word32Equal(m.Word32Or(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
2304
        uint32_t expected = (*i | *j) == 0;
2305
        CHECK_EQ(expected, m.Call(*j));
2306 2307 2308 2309 2310
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2311
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2312 2313 2314
      m.Return(m.Word32Equal(m.Word32Or(m.Parameter(0), m.Int32Constant(*i)),
                             m.Int32Constant(0)));
      FOR_UINT32_INPUTS(j) {
2315
        uint32_t expected = (*j | *i) == 0;
2316
        CHECK_EQ(expected, m.Call(*j));
2317 2318 2319 2320 2321 2322 2323 2324 2325
      }
    }
  }
}


TEST(RunWord32XorP) {
  {
    FOR_UINT32_INPUTS(i) {
2326
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2327 2328 2329
      m.Return(m.Word32Xor(m.Int32Constant(*i), m.Parameter(0)));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i ^ *j;
2330
        CHECK_EQ(expected, m.Call(*j));
2331 2332 2333 2334 2335
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2336
    Uint32BinopTester bt(&m);
2337 2338 2339
    bt.AddReturn(m.Word32Xor(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2340 2341
        uint32_t expected = *i ^ *j;
        CHECK_EQ(expected, bt.call(*i, *j));
2342 2343 2344 2345 2346 2347 2348
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32Xor(bt.param0, m.Word32Not(bt.param1)));
2349 2350 2351
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int32_t expected = *i ^ ~(*j);
2352 2353 2354 2355 2356 2357 2358 2359
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32Xor(m.Word32Not(bt.param0), bt.param1));
2360 2361 2362
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
        int32_t expected = ~(*i) ^ *j;
2363 2364 2365 2366 2367 2368
        CHECK_EQ(expected, bt.call(*i, *j));
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2369
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2370 2371 2372
      m.Return(m.Word32Xor(m.Int32Constant(*i), m.Word32Not(m.Parameter(0))));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *i ^ ~(*j);
2373
        CHECK_EQ(expected, m.Call(*j));
2374 2375 2376 2377 2378 2379 2380
      }
    }
  }
}


TEST(RunWord32XorInBranch) {
2381
  static const uint32_t constant = 987654321;
2382 2383
  {
    RawMachineAssemblerTester<int32_t> m;
2384
    Uint32BinopTester bt(&m);
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
    MLabel blocka, blockb;
    m.Branch(
        m.Word32Equal(m.Word32Xor(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2395
        uint32_t expected = (*i ^ *j) == 0 ? constant : 0 - constant;
2396
        CHECK_EQ(expected, bt.call(*i, *j));
2397 2398 2399 2400 2401
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2402
    Uint32BinopTester bt(&m);
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
    MLabel blocka, blockb;
    m.Branch(
        m.Word32NotEqual(m.Word32Xor(bt.param0, bt.param1), m.Int32Constant(0)),
        &blocka, &blockb);
    m.Bind(&blocka);
    bt.AddReturn(m.Int32Constant(constant));
    m.Bind(&blockb);
    bt.AddReturn(m.Int32Constant(0 - constant));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2413
        uint32_t expected = (*i ^ *j) != 0 ? constant : 0 - constant;
2414
        CHECK_EQ(expected, bt.call(*i, *j));
2415 2416 2417 2418 2419
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2420
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2421 2422 2423 2424 2425 2426 2427 2428 2429
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32Xor(m.Int32Constant(*i), m.Parameter(0)),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
2430
        uint32_t expected = (*i ^ *j) == 0 ? constant : 0 - constant;
2431
        CHECK_EQ(expected, m.Call(*j));
2432 2433 2434 2435 2436
      }
    }
  }
  {
    FOR_UINT32_INPUTS(i) {
2437
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
      MLabel blocka, blockb;
      m.Branch(
          m.Word32NotEqual(m.Word32Xor(m.Int32Constant(*i), m.Parameter(0)),
                           m.Int32Constant(0)),
          &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(j) {
2448
        uint32_t expected = (*i ^ *j) != 0 ? constant : 0 - constant;
2449
        CHECK_EQ(expected, m.Call(*j));
2450 2451 2452 2453 2454
      }
    }
  }
  {
    RawMachineAssemblerTester<void> m;
2455 2456 2457
    const Operator* shops[] = {m.machine()->Word32Sar(),
                               m.machine()->Word32Shl(),
                               m.machine()->Word32Shr()};
2458
    for (size_t n = 0; n < arraysize(shops); n++) {
2459 2460
      RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachInt32,
                                           kMachUint32);
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
      MLabel blocka, blockb;
      m.Branch(m.Word32Equal(m.Word32Xor(m.Parameter(0),
                                         m.NewNode(shops[n], m.Parameter(1),
                                                   m.Parameter(2))),
                             m.Int32Constant(0)),
               &blocka, &blockb);
      m.Bind(&blocka);
      m.Return(m.Int32Constant(constant));
      m.Bind(&blockb);
      m.Return(m.Int32Constant(0 - constant));
      FOR_UINT32_INPUTS(i) {
        FOR_INT32_INPUTS(j) {
2473
          FOR_UINT32_SHIFTS(shift) {
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
            int32_t right;
            switch (shops[n]->opcode()) {
              default:
                UNREACHABLE();
              case IrOpcode::kWord32Sar:
                right = *j >> shift;
                break;
              case IrOpcode::kWord32Shl:
                right = *j << shift;
                break;
              case IrOpcode::kWord32Shr:
                right = static_cast<uint32_t>(*j) >> shift;
                break;
            }
            int32_t expected = ((*i ^ right) == 0) ? constant : 0 - constant;
            CHECK_EQ(expected, m.Call(*i, *j, shift));
          }
        }
      }
    }
  }
}


TEST(RunWord32ShlP) {
  {
2500
    FOR_UINT32_SHIFTS(shift) {
2501
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2502 2503 2504
      m.Return(m.Word32Shl(m.Parameter(0), m.Int32Constant(shift)));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *j << shift;
2505
        CHECK_EQ(expected, m.Call(*j));
2506 2507 2508 2509 2510
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2511
    Uint32BinopTester bt(&m);
2512 2513
    bt.AddReturn(m.Word32Shl(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
2514
      FOR_UINT32_SHIFTS(shift) {
2515
        uint32_t expected = *i << shift;
2516
        CHECK_EQ(expected, bt.call(*i, shift));
2517 2518 2519 2520 2521 2522
      }
    }
  }
}


2523 2524 2525 2526 2527 2528 2529 2530 2531
TEST(RunWord32ShlInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Word32Shl(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == (*i << shift);
2532
        CHECK_EQ(expected, bt.call(*i, shift));
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32Shl(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == (*i << shift);
2544
        CHECK_EQ(expected, bt.call(*i, shift));
2545 2546 2547 2548 2549
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2550
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2551 2552 2553 2554 2555
      m.Return(
          m.Word32Equal(m.Int32Constant(0),
                        m.Word32Shl(m.Parameter(0), m.Int32Constant(shift))));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == (*i << shift);
2556
        CHECK_EQ(expected, m.Call(*i));
2557 2558 2559 2560 2561
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2562
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2563 2564 2565 2566 2567
      m.Return(
          m.Word32Equal(m.Word32Shl(m.Parameter(0), m.Int32Constant(shift)),
                        m.Int32Constant(0)));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == (*i << shift);
2568
        CHECK_EQ(expected, m.Call(*i));
2569 2570 2571 2572 2573 2574
      }
    }
  }
}


2575 2576
TEST(RunWord32ShrP) {
  {
2577
    FOR_UINT32_SHIFTS(shift) {
2578
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2579 2580 2581
      m.Return(m.Word32Shr(m.Parameter(0), m.Int32Constant(shift)));
      FOR_UINT32_INPUTS(j) {
        uint32_t expected = *j >> shift;
2582
        CHECK_EQ(expected, m.Call(*j));
2583 2584 2585 2586 2587
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2588
    Uint32BinopTester bt(&m);
2589 2590
    bt.AddReturn(m.Word32Shr(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
2591
      FOR_UINT32_SHIFTS(shift) {
2592
        uint32_t expected = *i >> shift;
2593
        CHECK_EQ(expected, bt.call(*i, shift));
2594 2595
      }
    }
2596
    CHECK_EQ(0x00010000u, bt.call(0x80000000, 15));
2597 2598 2599 2600
  }
}


2601 2602 2603 2604 2605 2606 2607 2608 2609
TEST(RunWord32ShrInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Word32Shr(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == (*i >> shift);
2610
        CHECK_EQ(expected, bt.call(*i, shift));
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32Shr(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == (*i >> shift);
2622
        CHECK_EQ(expected, bt.call(*i, shift));
2623 2624 2625 2626 2627
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2628
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2629 2630 2631 2632 2633
      m.Return(
          m.Word32Equal(m.Int32Constant(0),
                        m.Word32Shr(m.Parameter(0), m.Int32Constant(shift))));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == (*i >> shift);
2634
        CHECK_EQ(expected, m.Call(*i));
2635 2636 2637 2638 2639
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2640
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2641 2642 2643 2644 2645
      m.Return(
          m.Word32Equal(m.Word32Shr(m.Parameter(0), m.Int32Constant(shift)),
                        m.Int32Constant(0)));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == (*i >> shift);
2646
        CHECK_EQ(expected, m.Call(*i));
2647 2648 2649 2650 2651 2652
      }
    }
  }
}


2653 2654
TEST(RunWord32SarP) {
  {
2655
    FOR_INT32_SHIFTS(shift) {
2656
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
      m.Return(m.Word32Sar(m.Parameter(0), m.Int32Constant(shift)));
      FOR_INT32_INPUTS(j) {
        int32_t expected = *j >> shift;
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(m.Word32Sar(bt.param0, bt.param1));
    FOR_INT32_INPUTS(i) {
2669
      FOR_INT32_SHIFTS(shift) {
2670 2671 2672 2673
        int32_t expected = *i >> shift;
        CHECK_EQ(expected, bt.call(*i, shift));
      }
    }
2674
    CHECK_EQ(bit_cast<int32_t>(0xFFFF0000), bt.call(0x80000000, 15));
2675 2676 2677 2678
  }
}


2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
TEST(RunWord32SarInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Word32Sar(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_SHIFTS(shift) {
        int32_t expected = 0 == (*i >> shift);
        CHECK_EQ(expected, bt.call(*i, shift));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Int32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32Sar(bt.param0, bt.param1)));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_SHIFTS(shift) {
        int32_t expected = 0 == (*i >> shift);
        CHECK_EQ(expected, bt.call(*i, shift));
      }
    }
  }
  {
    FOR_INT32_SHIFTS(shift) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
      m.Return(
          m.Word32Equal(m.Int32Constant(0),
                        m.Word32Sar(m.Parameter(0), m.Int32Constant(shift))));
      FOR_INT32_INPUTS(i) {
        int32_t expected = 0 == (*i >> shift);
        CHECK_EQ(expected, m.Call(*i));
      }
    }
  }
  {
    FOR_INT32_SHIFTS(shift) {
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
      m.Return(
          m.Word32Equal(m.Word32Sar(m.Parameter(0), m.Int32Constant(shift)),
                        m.Int32Constant(0)));
      FOR_INT32_INPUTS(i) {
2723
        int32_t expected = 0 == (*i >> shift);
2724 2725 2726 2727 2728 2729 2730
        CHECK_EQ(expected, m.Call(*i));
      }
    }
  }
}


2731 2732 2733
TEST(RunWord32RorP) {
  {
    FOR_UINT32_SHIFTS(shift) {
2734
      RawMachineAssemblerTester<int32_t> m(kMachUint32);
2735 2736 2737 2738 2739 2740 2741 2742 2743
      m.Return(m.Word32Ror(m.Parameter(0), m.Int32Constant(shift)));
      FOR_UINT32_INPUTS(j) {
        int32_t expected = bits::RotateRight32(*j, shift);
        CHECK_EQ(expected, m.Call(*j));
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
2744
    Uint32BinopTester bt(&m);
2745 2746 2747
    bt.AddReturn(m.Word32Ror(bt.param0, bt.param1));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
2748
        uint32_t expected = bits::RotateRight32(*i, shift);
2749
        CHECK_EQ(expected, bt.call(*i, shift));
2750 2751 2752 2753 2754 2755
      }
    }
  }
}


2756 2757 2758 2759 2760 2761 2762 2763 2764
TEST(RunWord32RorInComparison) {
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Word32Ror(bt.param0, bt.param1), m.Int32Constant(0)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == bits::RotateRight32(*i, shift);
2765
        CHECK_EQ(expected, bt.call(*i, shift));
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
    Uint32BinopTester bt(&m);
    bt.AddReturn(
        m.Word32Equal(m.Int32Constant(0), m.Word32Ror(bt.param0, bt.param1)));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_SHIFTS(shift) {
        uint32_t expected = 0 == bits::RotateRight32(*i, shift);
2777
        CHECK_EQ(expected, bt.call(*i, shift));
2778 2779 2780 2781 2782
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2783
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2784 2785 2786 2787 2788
      m.Return(
          m.Word32Equal(m.Int32Constant(0),
                        m.Word32Ror(m.Parameter(0), m.Int32Constant(shift))));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == bits::RotateRight32(*i, shift);
2789
        CHECK_EQ(expected, m.Call(*i));
2790 2791 2792 2793 2794
      }
    }
  }
  {
    FOR_UINT32_SHIFTS(shift) {
2795
      RawMachineAssemblerTester<uint32_t> m(kMachUint32);
2796 2797 2798 2799 2800
      m.Return(
          m.Word32Equal(m.Word32Ror(m.Parameter(0), m.Int32Constant(shift)),
                        m.Int32Constant(0)));
      FOR_UINT32_INPUTS(i) {
        uint32_t expected = 0 == bits::RotateRight32(*i, shift);
2801
        CHECK_EQ(expected, m.Call(*i));
2802 2803 2804 2805 2806 2807
      }
    }
  }
}


2808
TEST(RunWord32NotP) {
2809
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
2810
  m.Return(m.Word32Not(m.Parameter(0)));
2811
  FOR_INT32_INPUTS(i) {
2812 2813 2814 2815 2816 2817 2818
    int expected = ~(*i);
    CHECK_EQ(expected, m.Call(*i));
  }
}


TEST(RunInt32NegP) {
2819
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
  m.Return(m.Int32Neg(m.Parameter(0)));
  FOR_INT32_INPUTS(i) {
    int expected = -*i;
    CHECK_EQ(expected, m.Call(*i));
  }
}


TEST(RunWord32EqualAndWord32SarP) {
  {
2830
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32, kMachUint32);
2831 2832 2833 2834
    m.Return(m.Word32Equal(m.Parameter(0),
                           m.Word32Sar(m.Parameter(1), m.Parameter(2))));
    FOR_INT32_INPUTS(i) {
      FOR_INT32_INPUTS(j) {
2835
        FOR_UINT32_SHIFTS(shift) {
2836 2837 2838 2839 2840 2841 2842
          int32_t expected = (*i == (*j >> shift));
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
2843
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachUint32, kMachInt32);
2844 2845 2846
    m.Return(m.Word32Equal(m.Word32Sar(m.Parameter(0), m.Parameter(1)),
                           m.Parameter(2)));
    FOR_INT32_INPUTS(i) {
2847
      FOR_UINT32_SHIFTS(shift) {
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
        FOR_INT32_INPUTS(k) {
          int32_t expected = ((*i >> shift) == *k);
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunWord32EqualAndWord32ShlP) {
  {
2860
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
2861 2862 2863 2864
    m.Return(m.Word32Equal(m.Parameter(0),
                           m.Word32Shl(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2865
        FOR_UINT32_SHIFTS(shift) {
2866 2867 2868 2869 2870 2871 2872
          int32_t expected = (*i == (*j << shift));
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
2873
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
2874 2875 2876
    m.Return(m.Word32Equal(m.Word32Shl(m.Parameter(0), m.Parameter(1)),
                           m.Parameter(2)));
    FOR_UINT32_INPUTS(i) {
2877
      FOR_UINT32_SHIFTS(shift) {
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
        FOR_UINT32_INPUTS(k) {
          int32_t expected = ((*i << shift) == *k);
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunWord32EqualAndWord32ShrP) {
  {
2890
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
2891 2892 2893 2894
    m.Return(m.Word32Equal(m.Parameter(0),
                           m.Word32Shr(m.Parameter(1), m.Parameter(2))));
    FOR_UINT32_INPUTS(i) {
      FOR_UINT32_INPUTS(j) {
2895
        FOR_UINT32_SHIFTS(shift) {
2896 2897 2898 2899 2900 2901 2902
          int32_t expected = (*i == (*j >> shift));
          CHECK_EQ(expected, m.Call(*i, *j, shift));
        }
      }
    }
  }
  {
2903
    RawMachineAssemblerTester<int32_t> m(kMachUint32, kMachUint32, kMachUint32);
2904 2905 2906
    m.Return(m.Word32Equal(m.Word32Shr(m.Parameter(0), m.Parameter(1)),
                           m.Parameter(2)));
    FOR_UINT32_INPUTS(i) {
2907
      FOR_UINT32_SHIFTS(shift) {
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
        FOR_UINT32_INPUTS(k) {
          int32_t expected = ((*i >> shift) == *k);
          CHECK_EQ(expected, m.Call(*i, shift, *k));
        }
      }
    }
  }
}


TEST(RunDeadNodes) {
  for (int i = 0; true; i++) {
2920
    RawMachineAssemblerTester<int32_t> m(i == 5 ? kMachInt32 : kMachNone);
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
    int constant = 0x55 + i;
    switch (i) {
      case 0:
        m.Int32Constant(44);
        break;
      case 1:
        m.StringConstant("unused");
        break;
      case 2:
        m.NumberConstant(11.1);
        break;
      case 3:
        m.PointerConstant(&constant);
        break;
      case 4:
2936
        m.LoadFromPointer(&constant, kMachInt32);
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
        break;
      case 5:
        m.Parameter(0);
        break;
      default:
        return;
    }
    m.Return(m.Int32Constant(constant));
    if (i != 5) {
      CHECK_EQ(constant, m.Call());
    } else {
      CHECK_EQ(constant, m.Call(0));
    }
  }
}


TEST(RunDeadInt32Binops) {
  RawMachineAssemblerTester<int32_t> m;

2957
  const Operator* kOps[] = {
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
      m.machine()->Word32And(),            m.machine()->Word32Or(),
      m.machine()->Word32Xor(),            m.machine()->Word32Shl(),
      m.machine()->Word32Shr(),            m.machine()->Word32Sar(),
      m.machine()->Word32Ror(),            m.machine()->Word32Equal(),
      m.machine()->Int32Add(),             m.machine()->Int32Sub(),
      m.machine()->Int32Mul(),             m.machine()->Int32MulHigh(),
      m.machine()->Int32Div(),             m.machine()->Uint32Div(),
      m.machine()->Int32Mod(),             m.machine()->Uint32Mod(),
      m.machine()->Uint32MulHigh(),        m.machine()->Int32LessThan(),
      m.machine()->Int32LessThanOrEqual(), m.machine()->Uint32LessThan(),
      m.machine()->Uint32LessThanOrEqual()};
2969 2970

  for (size_t i = 0; i < arraysize(kOps); ++i) {
2971
    RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
2972 2973
    int32_t constant = static_cast<int32_t>(0x55555 + i);
    m.NewNode(kOps[i], m.Parameter(0), m.Parameter(1));
2974 2975 2976 2977 2978 2979 2980
    m.Return(m.Int32Constant(constant));

    CHECK_EQ(constant, m.Call(1, 1));
  }
}


2981
template <typename Type>
2982
static void RunLoadImmIndex(MachineType rep) {
2983
  const int kNumElems = 3;
2984
  Type buffer[kNumElems];
2985 2986 2987 2988

  // initialize the buffer with raw data.
  byte* raw = reinterpret_cast<byte*>(buffer);
  for (size_t i = 0; i < sizeof(buffer); i++) {
2989
    raw[i] = static_cast<byte>((i + sizeof(buffer)) ^ 0xAA);
2990 2991 2992 2993 2994
  }

  // Test with various large and small offsets.
  for (int offset = -1; offset <= 200000; offset *= -5) {
    for (int i = 0; i < kNumElems; i++) {
2995
      RawMachineAssemblerTester<Type> m;
2996 2997 2998 2999
      Node* base = m.PointerConstant(buffer - offset);
      Node* index = m.Int32Constant((offset + i) * sizeof(buffer[0]));
      m.Return(m.Load(rep, base, index));

3000
      Type expected = buffer[i];
3001 3002
      Type actual = m.Call();
      CHECK(expected == actual);
3003 3004 3005 3006 3007 3008
    }
  }
}


TEST(RunLoadImmIndex) {
3009 3010 3011 3012 3013 3014 3015
  RunLoadImmIndex<int8_t>(kMachInt8);
  RunLoadImmIndex<uint8_t>(kMachUint8);
  RunLoadImmIndex<int16_t>(kMachInt16);
  RunLoadImmIndex<uint16_t>(kMachUint16);
  RunLoadImmIndex<int32_t>(kMachInt32);
  RunLoadImmIndex<uint32_t>(kMachUint32);
  RunLoadImmIndex<int32_t*>(kMachAnyTagged);
3016

3017 3018
  // TODO(titzer): test kRepBit loads
  // TODO(titzer): test kMachFloat64 loads
3019 3020 3021 3022 3023
  // TODO(titzer): test various indexing modes.
}


template <typename CType>
3024
static void RunLoadStore(MachineType rep) {
3025 3026 3027 3028 3029 3030 3031 3032
  const int kNumElems = 4;
  CType buffer[kNumElems];

  for (int32_t x = 0; x < kNumElems; x++) {
    int32_t y = kNumElems - x - 1;
    // initialize the buffer with raw data.
    byte* raw = reinterpret_cast<byte*>(buffer);
    for (size_t i = 0; i < sizeof(buffer); i++) {
3033
      raw[i] = static_cast<byte>((i + sizeof(buffer)) ^ 0xAA);
3034 3035 3036 3037 3038
    }

    RawMachineAssemblerTester<int32_t> m;
    int32_t OK = 0x29000 + x;
    Node* base = m.PointerConstant(buffer);
3039
    Node* index0 = m.IntPtrConstant(x * sizeof(buffer[0]));
3040
    Node* load = m.Load(rep, base, index0);
3041
    Node* index1 = m.IntPtrConstant(y * sizeof(buffer[0]));
3042 3043 3044
    m.Store(rep, base, index1, load);
    m.Return(m.Int32Constant(OK));

3045
    CHECK(buffer[x] != buffer[y]);
3046
    CHECK_EQ(OK, m.Call());
3047
    CHECK(buffer[x] == buffer[y]);
3048 3049 3050 3051 3052
  }
}


TEST(RunLoadStore) {
3053
  RunLoadStore<int8_t>(kMachInt8);
3054
  RunLoadStore<uint8_t>(kMachUint8);
3055
  RunLoadStore<int16_t>(kMachInt16);
3056
  RunLoadStore<uint16_t>(kMachUint16);
3057
  RunLoadStore<int32_t>(kMachInt32);
3058
  RunLoadStore<uint32_t>(kMachUint32);
3059
  RunLoadStore<void*>(kMachAnyTagged);
3060
  RunLoadStore<float>(kMachFloat32);
3061
  RunLoadStore<double>(kMachFloat64);
3062 3063 3064
}


3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
TEST(RunFloat32Binop) {
  RawMachineAssemblerTester<int32_t> m;
  float result;

  const Operator* ops[] = {m.machine()->Float32Add(), m.machine()->Float32Sub(),
                           m.machine()->Float32Mul(), m.machine()->Float32Div(),
                           NULL};

  float inf = std::numeric_limits<float>::infinity();
  const Operator* inputs[] = {
      m.common()->Float32Constant(0.0f),   m.common()->Float32Constant(1.0f),
      m.common()->Float32Constant(1.0f),   m.common()->Float32Constant(0.0f),
      m.common()->Float32Constant(0.0f),   m.common()->Float32Constant(-1.0f),
      m.common()->Float32Constant(-1.0f),  m.common()->Float32Constant(0.0f),
      m.common()->Float32Constant(0.22f),  m.common()->Float32Constant(-1.22f),
      m.common()->Float32Constant(-1.22f), m.common()->Float32Constant(0.22f),
      m.common()->Float32Constant(inf),    m.common()->Float32Constant(0.22f),
      m.common()->Float32Constant(inf),    m.common()->Float32Constant(-inf),
      NULL};

  for (int i = 0; ops[i] != NULL; i++) {
    for (int j = 0; inputs[j] != NULL; j += 2) {
      RawMachineAssemblerTester<int32_t> m;
      Node* a = m.NewNode(inputs[j]);
      Node* b = m.NewNode(inputs[j + 1]);
      Node* binop = m.NewNode(ops[i], a, b);
      Node* base = m.PointerConstant(&result);
      Node* zero = m.IntPtrConstant(0);
      m.Store(kMachFloat32, base, zero, binop);
      m.Return(m.Int32Constant(i + j));
      CHECK_EQ(i + j, m.Call());
    }
  }
}


3101 3102 3103 3104
TEST(RunFloat64Binop) {
  RawMachineAssemblerTester<int32_t> m;
  double result;

3105 3106 3107
  const Operator* ops[] = {m.machine()->Float64Add(), m.machine()->Float64Sub(),
                           m.machine()->Float64Mul(), m.machine()->Float64Div(),
                           m.machine()->Float64Mod(), NULL};
3108

3109
  double inf = V8_INFINITY;
3110
  const Operator* inputs[] = {
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128
      m.common()->Float64Constant(0),     m.common()->Float64Constant(1),
      m.common()->Float64Constant(1),     m.common()->Float64Constant(0),
      m.common()->Float64Constant(0),     m.common()->Float64Constant(-1),
      m.common()->Float64Constant(-1),    m.common()->Float64Constant(0),
      m.common()->Float64Constant(0.22),  m.common()->Float64Constant(-1.22),
      m.common()->Float64Constant(-1.22), m.common()->Float64Constant(0.22),
      m.common()->Float64Constant(inf),   m.common()->Float64Constant(0.22),
      m.common()->Float64Constant(inf),   m.common()->Float64Constant(-inf),
      NULL};

  for (int i = 0; ops[i] != NULL; i++) {
    for (int j = 0; inputs[j] != NULL; j += 2) {
      RawMachineAssemblerTester<int32_t> m;
      Node* a = m.NewNode(inputs[j]);
      Node* b = m.NewNode(inputs[j + 1]);
      Node* binop = m.NewNode(ops[i], a, b);
      Node* base = m.PointerConstant(&result);
      Node* zero = m.Int32Constant(0);
3129
      m.Store(kMachFloat64, base, zero, binop);
3130 3131 3132 3133 3134 3135 3136
      m.Return(m.Int32Constant(i + j));
      CHECK_EQ(i + j, m.Call());
    }
  }
}


3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
TEST(RunDeadFloat32Binops) {
  RawMachineAssemblerTester<int32_t> m;

  const Operator* ops[] = {m.machine()->Float32Add(), m.machine()->Float32Sub(),
                           m.machine()->Float32Mul(), m.machine()->Float32Div(),
                           NULL};

  for (int i = 0; ops[i] != NULL; i++) {
    RawMachineAssemblerTester<int32_t> m;
    int constant = 0x53355 + i;
    m.NewNode(ops[i], m.Float32Constant(0.1f), m.Float32Constant(1.11f));
    m.Return(m.Int32Constant(constant));
    CHECK_EQ(constant, m.Call());
  }
}


3154 3155 3156
TEST(RunDeadFloat64Binops) {
  RawMachineAssemblerTester<int32_t> m;

3157 3158 3159
  const Operator* ops[] = {m.machine()->Float64Add(), m.machine()->Float64Sub(),
                           m.machine()->Float64Mul(), m.machine()->Float64Div(),
                           m.machine()->Float64Mod(), NULL};
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170

  for (int i = 0; ops[i] != NULL; i++) {
    RawMachineAssemblerTester<int32_t> m;
    int constant = 0x53355 + i;
    m.NewNode(ops[i], m.Float64Constant(0.1), m.Float64Constant(1.11));
    m.Return(m.Int32Constant(constant));
    CHECK_EQ(constant, m.Call());
  }
}


3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
TEST(RunFloat32AddP) {
  RawMachineAssemblerTester<int32_t> m;
  Float32BinopTester bt(&m);

  bt.AddReturn(m.Float32Add(bt.param0, bt.param1));

  FOR_FLOAT32_INPUTS(pl) {
    FOR_FLOAT32_INPUTS(pr) {
      float expected = *pl + *pr;
      CheckFloatEq(expected, bt.call(*pl, *pr));
    }
  }
}


3186 3187 3188 3189 3190 3191 3192 3193 3194
TEST(RunFloat64AddP) {
  RawMachineAssemblerTester<int32_t> m;
  Float64BinopTester bt(&m);

  bt.AddReturn(m.Float64Add(bt.param0, bt.param1));

  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      double expected = *pl + *pr;
3195
      CheckDoubleEq(expected, bt.call(*pl, *pr));
3196 3197 3198 3199 3200
    }
  }
}


3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215
TEST(RunFloat32SubP) {
  RawMachineAssemblerTester<int32_t> m;
  Float32BinopTester bt(&m);

  bt.AddReturn(m.Float32Sub(bt.param0, bt.param1));

  FOR_FLOAT32_INPUTS(pl) {
    FOR_FLOAT32_INPUTS(pr) {
      float expected = *pl - *pr;
      CheckFloatEq(expected, bt.call(*pl, *pr));
    }
  }
}


3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
TEST(RunFloat32SubImm1) {
  float input = 0.0f;
  float output = 0.0f;

  FOR_FLOAT32_INPUTS(i) {
    RawMachineAssemblerTester<int32_t> m;
    Node* t0 = m.LoadFromPointer(&input, kMachFloat32);
    Node* t1 = m.Float32Sub(m.Float32Constant(*i), t0);
    m.StoreToPointer(&output, kMachFloat32, t1);
    m.Return(m.Int32Constant(0));
    FOR_FLOAT32_INPUTS(j) {
      input = *j;
      float expected = *i - input;
      CHECK_EQ(0, m.Call());
      CheckFloatEq(expected, output);
    }
  }
}


TEST(RunFloat32SubImm2) {
  float input = 0.0f;
  float output = 0.0f;

  FOR_FLOAT32_INPUTS(i) {
    RawMachineAssemblerTester<int32_t> m;
    Node* t0 = m.LoadFromPointer(&input, kMachFloat32);
    Node* t1 = m.Float32Sub(t0, m.Float32Constant(*i));
    m.StoreToPointer(&output, kMachFloat32, t1);
    m.Return(m.Int32Constant(0));
    FOR_FLOAT32_INPUTS(j) {
      input = *j;
      float expected = input - *i;
      CHECK_EQ(0, m.Call());
      CheckFloatEq(expected, output);
    }
  }
}


3256 3257 3258 3259 3260 3261 3262 3263 3264
TEST(RunFloat64SubP) {
  RawMachineAssemblerTester<int32_t> m;
  Float64BinopTester bt(&m);

  bt.AddReturn(m.Float64Sub(bt.param0, bt.param1));

  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      double expected = *pl - *pr;
3265
      CheckDoubleEq(expected, bt.call(*pl, *pr));
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
    }
  }
}


TEST(RunFloat64SubImm1) {
  double input = 0.0;
  double output = 0.0;

  FOR_FLOAT64_INPUTS(i) {
    RawMachineAssemblerTester<int32_t> m;
3277
    Node* t0 = m.LoadFromPointer(&input, kMachFloat64);
3278
    Node* t1 = m.Float64Sub(m.Float64Constant(*i), t0);
3279
    m.StoreToPointer(&output, kMachFloat64, t1);
3280 3281 3282 3283 3284
    m.Return(m.Int32Constant(0));
    FOR_FLOAT64_INPUTS(j) {
      input = *j;
      double expected = *i - input;
      CHECK_EQ(0, m.Call());
3285
      CheckDoubleEq(expected, output);
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
    }
  }
}


TEST(RunFloat64SubImm2) {
  double input = 0.0;
  double output = 0.0;

  FOR_FLOAT64_INPUTS(i) {
    RawMachineAssemblerTester<int32_t> m;
3297
    Node* t0 = m.LoadFromPointer(&input, kMachFloat64);
3298
    Node* t1 = m.Float64Sub(t0, m.Float64Constant(*i));
3299
    m.StoreToPointer(&output, kMachFloat64, t1);
3300 3301 3302 3303 3304
    m.Return(m.Int32Constant(0));
    FOR_FLOAT64_INPUTS(j) {
      input = *j;
      double expected = input - *i;
      CHECK_EQ(0, m.Call());
3305
      CheckDoubleEq(expected, output);
3306 3307 3308 3309 3310
    }
  }
}


3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
TEST(RunFloat32MulP) {
  RawMachineAssemblerTester<int32_t> m;
  Float32BinopTester bt(&m);

  bt.AddReturn(m.Float32Mul(bt.param0, bt.param1));

  FOR_FLOAT32_INPUTS(pl) {
    FOR_FLOAT32_INPUTS(pr) {
      float expected = *pl * *pr;
      CheckFloatEq(expected, bt.call(*pl, *pr));
    }
  }
}


3326 3327 3328 3329 3330 3331 3332 3333 3334
TEST(RunFloat64MulP) {
  RawMachineAssemblerTester<int32_t> m;
  Float64BinopTester bt(&m);

  bt.AddReturn(m.Float64Mul(bt.param0, bt.param1));

  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      double expected = *pl * *pr;
3335
      CheckDoubleEq(expected, bt.call(*pl, *pr));
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
    }
  }
}


TEST(RunFloat64MulAndFloat64AddP) {
  double input_a = 0.0;
  double input_b = 0.0;
  double input_c = 0.0;
  double output = 0.0;

  {
    RawMachineAssemblerTester<int32_t> m;
3349 3350 3351 3352
    Node* a = m.LoadFromPointer(&input_a, kMachFloat64);
    Node* b = m.LoadFromPointer(&input_b, kMachFloat64);
    Node* c = m.LoadFromPointer(&input_c, kMachFloat64);
    m.StoreToPointer(&output, kMachFloat64,
3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
                     m.Float64Add(m.Float64Mul(a, b), c));
    m.Return(m.Int32Constant(0));
    FOR_FLOAT64_INPUTS(i) {
      FOR_FLOAT64_INPUTS(j) {
        FOR_FLOAT64_INPUTS(k) {
          input_a = *i;
          input_b = *j;
          input_c = *k;
          volatile double temp = input_a * input_b;
          volatile double expected = temp + input_c;
          CHECK_EQ(0, m.Call());
3364
          CheckDoubleEq(expected, output);
3365 3366 3367 3368 3369 3370
        }
      }
    }
  }
  {
    RawMachineAssemblerTester<int32_t> m;
3371 3372 3373 3374
    Node* a = m.LoadFromPointer(&input_a, kMachFloat64);
    Node* b = m.LoadFromPointer(&input_b, kMachFloat64);
    Node* c = m.LoadFromPointer(&input_c, kMachFloat64);
    m.StoreToPointer(&output, kMachFloat64,
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
                     m.Float64Add(a, m.Float64Mul(b, c)));
    m.Return(m.Int32Constant(0));
    FOR_FLOAT64_INPUTS(i) {
      FOR_FLOAT64_INPUTS(j) {
        FOR_FLOAT64_INPUTS(k) {
          input_a = *i;
          input_b = *j;
          input_c = *k;
          volatile double temp = input_b * input_c;
          volatile double expected = input_a + temp;
          CHECK_EQ(0, m.Call());
3386
          CheckDoubleEq(expected, output);
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
        }
      }
    }
  }
}


TEST(RunFloat64MulAndFloat64SubP) {
  double input_a = 0.0;
  double input_b = 0.0;
  double input_c = 0.0;
  double output = 0.0;

  RawMachineAssemblerTester<int32_t> m;
3401 3402 3403 3404
  Node* a = m.LoadFromPointer(&input_a, kMachFloat64);
  Node* b = m.LoadFromPointer(&input_b, kMachFloat64);
  Node* c = m.LoadFromPointer(&input_c, kMachFloat64);
  m.StoreToPointer(&output, kMachFloat64, m.Float64Sub(a, m.Float64Mul(b, c)));
3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
  m.Return(m.Int32Constant(0));

  FOR_FLOAT64_INPUTS(i) {
    FOR_FLOAT64_INPUTS(j) {
      FOR_FLOAT64_INPUTS(k) {
        input_a = *i;
        input_b = *j;
        input_c = *k;
        volatile double temp = input_b * input_c;
        volatile double expected = input_a - temp;
        CHECK_EQ(0, m.Call());
3416
        CheckDoubleEq(expected, output);
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429
      }
    }
  }
}


TEST(RunFloat64MulImm) {
  double input = 0.0;
  double output = 0.0;

  {
    FOR_FLOAT64_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m;
3430
      Node* t0 = m.LoadFromPointer(&input, kMachFloat64);
3431
      Node* t1 = m.Float64Mul(m.Float64Constant(*i), t0);
3432
      m.StoreToPointer(&output, kMachFloat64, t1);
3433 3434 3435 3436 3437
      m.Return(m.Int32Constant(0));
      FOR_FLOAT64_INPUTS(j) {
        input = *j;
        double expected = *i * input;
        CHECK_EQ(0, m.Call());
3438
        CheckDoubleEq(expected, output);
3439 3440 3441 3442 3443 3444
      }
    }
  }
  {
    FOR_FLOAT64_INPUTS(i) {
      RawMachineAssemblerTester<int32_t> m;
3445
      Node* t0 = m.LoadFromPointer(&input, kMachFloat64);
3446
      Node* t1 = m.Float64Mul(t0, m.Float64Constant(*i));
3447
      m.StoreToPointer(&output, kMachFloat64, t1);
3448 3449 3450 3451 3452
      m.Return(m.Int32Constant(0));
      FOR_FLOAT64_INPUTS(j) {
        input = *j;
        double expected = input * *i;
        CHECK_EQ(0, m.Call());
3453
        CheckDoubleEq(expected, output);
3454 3455 3456 3457 3458 3459
      }
    }
  }
}


3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474
TEST(RunFloat32DivP) {
  RawMachineAssemblerTester<int32_t> m;
  Float32BinopTester bt(&m);

  bt.AddReturn(m.Float32Div(bt.param0, bt.param1));

  FOR_FLOAT32_INPUTS(pl) {
    FOR_FLOAT32_INPUTS(pr) {
      float expected = *pl / *pr;
      CheckFloatEq(expected, bt.call(*pl, *pr));
    }
  }
}


3475 3476 3477 3478 3479 3480 3481 3482 3483
TEST(RunFloat64DivP) {
  RawMachineAssemblerTester<int32_t> m;
  Float64BinopTester bt(&m);

  bt.AddReturn(m.Float64Div(bt.param0, bt.param1));

  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      double expected = *pl / *pr;
3484
      CheckDoubleEq(expected, bt.call(*pl, *pr));
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
    }
  }
}


TEST(RunFloat64ModP) {
  RawMachineAssemblerTester<int32_t> m;
  Float64BinopTester bt(&m);

  bt.AddReturn(m.Float64Mod(bt.param0, bt.param1));

  FOR_FLOAT64_INPUTS(i) {
    FOR_FLOAT64_INPUTS(j) {
      double expected = modulo(*i, *j);
      double found = bt.call(*i, *j);
3500
      CheckDoubleEq(expected, found);
3501 3502 3503 3504 3505
    }
  }
}


3506
TEST(RunChangeInt32ToFloat64_A) {
3507 3508 3509 3510
  RawMachineAssemblerTester<int32_t> m;
  int32_t magic = 0x986234;
  double result = 0;

3511
  Node* convert = m.ChangeInt32ToFloat64(m.Int32Constant(magic));
3512
  m.Store(kMachFloat64, m.PointerConstant(&result), m.Int32Constant(0),
3513 3514 3515 3516 3517 3518 3519 3520
          convert);
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK_EQ(static_cast<double>(magic), result);
}


3521
TEST(RunChangeInt32ToFloat64_B) {
3522
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
3523 3524
  double output = 0;

3525
  Node* convert = m.ChangeInt32ToFloat64(m.Parameter(0));
3526
  m.Store(kMachFloat64, m.PointerConstant(&output), m.Int32Constant(0),
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
          convert);
  m.Return(m.Parameter(0));

  FOR_INT32_INPUTS(i) {
    int32_t expect = *i;
    CHECK_EQ(expect, m.Call(expect));
    CHECK_EQ(static_cast<double>(expect), output);
  }
}


3538
TEST(RunChangeUint32ToFloat64_B) {
3539
  RawMachineAssemblerTester<uint32_t> m(kMachUint32);
3540 3541
  double output = 0;

3542
  Node* convert = m.ChangeUint32ToFloat64(m.Parameter(0));
3543
  m.Store(kMachFloat64, m.PointerConstant(&output), m.Int32Constant(0),
3544 3545 3546 3547 3548 3549 3550 3551 3552
          convert);
  m.Return(m.Parameter(0));

  FOR_UINT32_INPUTS(i) {
    uint32_t expect = *i;
    CHECK_EQ(expect, m.Call(expect));
    CHECK_EQ(static_cast<double>(expect), output);
  }
}
3553 3554


3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
TEST(RunChangeUint32ToFloat64_spilled) {
  RawMachineAssemblerTester<int32_t> m;
  const int kNumInputs = 32;
  int32_t magic = 0x786234;
  uint32_t input[kNumInputs];
  double result[kNumInputs];
  Node* input_node[kNumInputs];

  for (int i = 0; i < kNumInputs; i++) {
    input_node[i] =
        m.Load(kMachUint32, m.PointerConstant(&input), m.Int32Constant(i * 4));
  }

  for (int i = 0; i < kNumInputs; i++) {
    m.Store(kMachFloat64, m.PointerConstant(&result), m.Int32Constant(i * 8),
            m.ChangeUint32ToFloat64(input_node[i]));
  }

  m.Return(m.Int32Constant(magic));

  for (int i = 0; i < kNumInputs; i++) {
    input[i] = 100 + i;
  }

  CHECK_EQ(magic, m.Call());

  for (int i = 0; i < kNumInputs; i++) {
    CHECK_EQ(result[i], static_cast<double>(100 + i));
  }
}


3587
TEST(RunChangeFloat64ToInt32_A) {
3588 3589 3590 3591 3592
  RawMachineAssemblerTester<int32_t> m;
  int32_t magic = 0x786234;
  double input = 11.1;
  int32_t result = 0;

3593
  m.Store(kMachInt32, m.PointerConstant(&result), m.Int32Constant(0),
3594
          m.ChangeFloat64ToInt32(m.Float64Constant(input)));
3595 3596 3597 3598 3599 3600 3601
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK_EQ(static_cast<int32_t>(input), result);
}


3602
TEST(RunChangeFloat64ToInt32_B) {
3603 3604 3605 3606 3607
  RawMachineAssemblerTester<int32_t> m;
  double input = 0;
  int32_t output = 0;

  Node* load =
3608
      m.Load(kMachFloat64, m.PointerConstant(&input), m.Int32Constant(0));
3609
  Node* convert = m.ChangeFloat64ToInt32(load);
3610
  m.Store(kMachInt32, m.PointerConstant(&output), m.Int32Constant(0), convert);
3611 3612 3613 3614 3615
  m.Return(convert);

  {
    FOR_INT32_INPUTS(i) {
      input = *i;
3616
      int32_t expect = *i;
3617 3618 3619 3620 3621
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }
  }

3622 3623 3624 3625
  // Check various powers of 2.
  for (int32_t n = 1; n < 31; ++n) {
    {
      input = 1 << n;
3626
      int32_t expect = static_cast<int32_t>(input);
3627 3628 3629 3630 3631 3632
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }

    {
      input = 3 << n;
3633
      int32_t expect = static_cast<int32_t>(input);
3634 3635 3636 3637
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }
  }
3638 3639
  // Note we don't check fractional inputs, because these Convert operators
  // really should be Change operators.
3640 3641 3642
}


3643
TEST(RunChangeFloat64ToUint32_B) {
3644
  RawMachineAssemblerTester<int32_t> m;
3645 3646
  double input = 0;
  int32_t output = 0;
3647

3648
  Node* load =
3649
      m.Load(kMachFloat64, m.PointerConstant(&input), m.Int32Constant(0));
3650
  Node* convert = m.ChangeFloat64ToUint32(load);
3651
  m.Store(kMachInt32, m.PointerConstant(&output), m.Int32Constant(0), convert);
3652
  m.Return(convert);
3653

3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
  {
    FOR_UINT32_INPUTS(i) {
      input = *i;
      // TODO(titzer): add a CheckEqualsHelper overload for uint32_t.
      int32_t expect = static_cast<int32_t>(*i);
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }
  }

  // Check various powers of 2.
  for (int32_t n = 1; n < 31; ++n) {
    {
      input = 1u << n;
      int32_t expect = static_cast<int32_t>(static_cast<uint32_t>(input));
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }

    {
      input = 3u << n;
      int32_t expect = static_cast<int32_t>(static_cast<uint32_t>(input));
      CHECK_EQ(expect, m.Call());
      CHECK_EQ(expect, output);
    }
3679
  }
3680 3681
  // Note we don't check fractional inputs, because these Convert operators
  // really should be Change operators.
3682 3683 3684
}


3685
TEST(RunChangeFloat64ToInt32_spilled) {
3686 3687 3688 3689 3690 3691 3692 3693
  RawMachineAssemblerTester<int32_t> m;
  const int kNumInputs = 32;
  int32_t magic = 0x786234;
  double input[kNumInputs];
  int32_t result[kNumInputs];
  Node* input_node[kNumInputs];

  for (int i = 0; i < kNumInputs; i++) {
3694 3695
    input_node[i] =
        m.Load(kMachFloat64, m.PointerConstant(&input), m.Int32Constant(i * 8));
3696 3697 3698
  }

  for (int i = 0; i < kNumInputs; i++) {
3699
    m.Store(kMachInt32, m.PointerConstant(&result), m.Int32Constant(i * 4),
3700
            m.ChangeFloat64ToInt32(input_node[i]));
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
  }

  m.Return(m.Int32Constant(magic));

  for (int i = 0; i < kNumInputs; i++) {
    input[i] = 100.9 + i;
  }

  CHECK_EQ(magic, m.Call());

  for (int i = 0; i < kNumInputs; i++) {
    CHECK_EQ(result[i], 100 + i);
  }
}


3717 3718 3719
TEST(RunChangeFloat64ToUint32_spilled) {
  RawMachineAssemblerTester<uint32_t> m;
  const int kNumInputs = 32;
3720
  uint32_t magic = 0x786234;
3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
  double input[kNumInputs];
  uint32_t result[kNumInputs];
  Node* input_node[kNumInputs];

  for (int i = 0; i < kNumInputs; i++) {
    input_node[i] =
        m.Load(kMachFloat64, m.PointerConstant(&input), m.Int32Constant(i * 8));
  }

  for (int i = 0; i < kNumInputs; i++) {
    m.Store(kMachUint32, m.PointerConstant(&result), m.Int32Constant(i * 4),
            m.ChangeFloat64ToUint32(input_node[i]));
  }

  m.Return(m.Int32Constant(magic));

  for (int i = 0; i < kNumInputs; i++) {
    if (i % 2) {
titzer@chromium.org's avatar
titzer@chromium.org committed
3739
      input[i] = 100 + i + 2147483648u;
3740 3741 3742 3743 3744 3745 3746 3747 3748
    } else {
      input[i] = 100 + i;
    }
  }

  CHECK_EQ(magic, m.Call());

  for (int i = 0; i < kNumInputs; i++) {
    if (i % 2) {
3749
      CHECK_EQ(result[i], static_cast<uint32_t>(100 + i + 2147483648u));
3750
    } else {
3751
      CHECK_EQ(result[i], static_cast<uint32_t>(100 + i));
3752 3753 3754 3755 3756
    }
  }
}


3757 3758 3759
TEST(RunTruncateFloat64ToFloat32_spilled) {
  RawMachineAssemblerTester<uint32_t> m;
  const int kNumInputs = 32;
3760
  uint32_t magic = 0x786234;
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
  double input[kNumInputs];
  float result[kNumInputs];
  Node* input_node[kNumInputs];

  for (int i = 0; i < kNumInputs; i++) {
    input_node[i] =
        m.Load(kMachFloat64, m.PointerConstant(&input), m.Int32Constant(i * 8));
  }

  for (int i = 0; i < kNumInputs; i++) {
    m.Store(kMachFloat32, m.PointerConstant(&result), m.Int32Constant(i * 4),
            m.TruncateFloat64ToFloat32(input_node[i]));
  }

  m.Return(m.Int32Constant(magic));

  for (int i = 0; i < kNumInputs; i++) {
    input[i] = 0.1 + i;
  }

  CHECK_EQ(magic, m.Call());

  for (int i = 0; i < kNumInputs; i++) {
    CHECK_EQ(result[i], DoubleToFloat32(input[i]));
  }
}


3789
TEST(RunDeadChangeFloat64ToInt32) {
3790 3791
  RawMachineAssemblerTester<int32_t> m;
  const int magic = 0x88abcda4;
3792
  m.ChangeFloat64ToInt32(m.Float64Constant(999.78));
3793 3794 3795 3796 3797
  m.Return(m.Int32Constant(magic));
  CHECK_EQ(magic, m.Call());
}


3798
TEST(RunDeadChangeInt32ToFloat64) {
3799 3800
  RawMachineAssemblerTester<int32_t> m;
  const int magic = 0x8834abcd;
3801
  m.ChangeInt32ToFloat64(m.Int32Constant(magic - 6888));
3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
  m.Return(m.Int32Constant(magic));
  CHECK_EQ(magic, m.Call());
}


TEST(RunLoopPhiInduction2) {
  RawMachineAssemblerTester<int32_t> m;

  int false_val = 0x10777;

  // x = false_val; while(false) { x++; } return x;
  MLabel header, body, end;
  Node* false_node = m.Int32Constant(false_val);
  m.Goto(&header);
  m.Bind(&header);
3817
  Node* phi = m.Phi(kMachInt32, false_node, false_node);
3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
  m.Branch(m.Int32Constant(0), &body, &end);
  m.Bind(&body);
  Node* add = m.Int32Add(phi, m.Int32Constant(1));
  phi->ReplaceInput(1, add);
  m.Goto(&header);
  m.Bind(&end);
  m.Return(phi);

  CHECK_EQ(false_val, m.Call());
}


3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
TEST(RunFloatDiamond) {
  RawMachineAssemblerTester<int32_t> m;

  const int magic = 99645;
  float buffer = 0.1f;
  float constant = 99.99f;

  MLabel blocka, blockb, end;
  Node* k1 = m.Float32Constant(constant);
  Node* k2 = m.Float32Constant(0 - constant);
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
  Node* phi = m.Phi(kMachFloat32, k2, k1);
  m.Store(kMachFloat32, m.PointerConstant(&buffer), m.IntPtrConstant(0), phi);
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK(constant == buffer);
}


3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870
TEST(RunDoubleDiamond) {
  RawMachineAssemblerTester<int32_t> m;

  const int magic = 99645;
  double buffer = 0.1;
  double constant = 99.99;

  MLabel blocka, blockb, end;
  Node* k1 = m.Float64Constant(constant);
  Node* k2 = m.Float64Constant(0 - constant);
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
3871
  Node* phi = m.Phi(kMachFloat64, k2, k1);
3872
  m.Store(kMachFloat64, m.PointerConstant(&buffer), m.Int32Constant(0), phi);
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK_EQ(constant, buffer);
}


TEST(RunRefDiamond) {
  RawMachineAssemblerTester<int32_t> m;

  const int magic = 99644;
  Handle<String> rexpected =
      CcTest::i_isolate()->factory()->InternalizeUtf8String("A");
  String* buffer;

  MLabel blocka, blockb, end;
  Node* k1 = m.StringConstant("A");
  Node* k2 = m.StringConstant("B");
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
3897
  Node* phi = m.Phi(kMachAnyTagged, k2, k1);
3898
  m.Store(kMachAnyTagged, m.PointerConstant(&buffer), m.Int32Constant(0), phi);
3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK(rexpected->SameValue(buffer));
}


TEST(RunDoubleRefDiamond) {
  RawMachineAssemblerTester<int32_t> m;

  const int magic = 99648;
  double dbuffer = 0.1;
  double dconstant = 99.99;
  Handle<String> rexpected =
      CcTest::i_isolate()->factory()->InternalizeUtf8String("AX");
  String* rbuffer;

  MLabel blocka, blockb, end;
  Node* d1 = m.Float64Constant(dconstant);
  Node* d2 = m.Float64Constant(0 - dconstant);
  Node* r1 = m.StringConstant("AX");
  Node* r2 = m.StringConstant("BX");
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);
  m.Bind(&end);
3927 3928
  Node* dphi = m.Phi(kMachFloat64, d2, d1);
  Node* rphi = m.Phi(kMachAnyTagged, r2, r1);
3929 3930
  m.Store(kMachFloat64, m.PointerConstant(&dbuffer), m.Int32Constant(0), dphi);
  m.Store(kMachAnyTagged, m.PointerConstant(&rbuffer), m.Int32Constant(0),
3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
          rphi);
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK_EQ(dconstant, dbuffer);
  CHECK(rexpected->SameValue(rbuffer));
}


TEST(RunDoubleRefDoubleDiamond) {
  RawMachineAssemblerTester<int32_t> m;

  const int magic = 99649;
  double dbuffer = 0.1;
  double dconstant = 99.997;
  Handle<String> rexpected =
      CcTest::i_isolate()->factory()->InternalizeUtf8String("AD");
  String* rbuffer;

  MLabel blocka, blockb, mid, blockd, blocke, end;
  Node* d1 = m.Float64Constant(dconstant);
  Node* d2 = m.Float64Constant(0 - dconstant);
  Node* r1 = m.StringConstant("AD");
  Node* r2 = m.StringConstant("BD");
  m.Branch(m.Int32Constant(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&mid);
  m.Bind(&blockb);
  m.Goto(&mid);
  m.Bind(&mid);
3961 3962
  Node* dphi1 = m.Phi(kMachFloat64, d2, d1);
  Node* rphi1 = m.Phi(kMachAnyTagged, r2, r1);
3963 3964 3965 3966 3967 3968 3969
  m.Branch(m.Int32Constant(0), &blockd, &blocke);

  m.Bind(&blockd);
  m.Goto(&end);
  m.Bind(&blocke);
  m.Goto(&end);
  m.Bind(&end);
3970 3971
  Node* dphi2 = m.Phi(kMachFloat64, d1, dphi1);
  Node* rphi2 = m.Phi(kMachAnyTagged, r1, rphi1);
3972

3973 3974
  m.Store(kMachFloat64, m.PointerConstant(&dbuffer), m.Int32Constant(0), dphi2);
  m.Store(kMachAnyTagged, m.PointerConstant(&rbuffer), m.Int32Constant(0),
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996
          rphi2);
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
  CHECK_EQ(dconstant, dbuffer);
  CHECK(rexpected->SameValue(rbuffer));
}


TEST(RunDoubleLoopPhi) {
  RawMachineAssemblerTester<int32_t> m;
  MLabel header, body, end;

  int magic = 99773;
  double buffer = 0.99;
  double dconstant = 777.1;

  Node* zero = m.Int32Constant(0);
  Node* dk = m.Float64Constant(dconstant);

  m.Goto(&header);
  m.Bind(&header);
3997
  Node* phi = m.Phi(kMachFloat64, dk, dk);
3998 3999 4000 4001 4002
  phi->ReplaceInput(1, phi);
  m.Branch(zero, &body, &end);
  m.Bind(&body);
  m.Goto(&header);
  m.Bind(&end);
4003
  m.Store(kMachFloat64, m.PointerConstant(&buffer), m.Int32Constant(0), phi);
4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021
  m.Return(m.Int32Constant(magic));

  CHECK_EQ(magic, m.Call());
}


TEST(RunCountToTenAccRaw) {
  RawMachineAssemblerTester<int32_t> m;

  Node* zero = m.Int32Constant(0);
  Node* ten = m.Int32Constant(10);
  Node* one = m.Int32Constant(1);

  MLabel header, body, body_cont, end;

  m.Goto(&header);

  m.Bind(&header);
4022 4023
  Node* i = m.Phi(kMachInt32, zero, zero);
  Node* j = m.Phi(kMachInt32, zero, zero);
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
  m.Goto(&body);

  m.Bind(&body);
  Node* next_i = m.Int32Add(i, one);
  Node* next_j = m.Int32Add(j, one);
  m.Branch(m.Word32Equal(next_i, ten), &end, &body_cont);

  m.Bind(&body_cont);
  i->ReplaceInput(1, next_i);
  j->ReplaceInput(1, next_j);
  m.Goto(&header);

  m.Bind(&end);
  m.Return(ten);

  CHECK_EQ(10, m.Call());
}


TEST(RunCountToTenAccRaw2) {
  RawMachineAssemblerTester<int32_t> m;

  Node* zero = m.Int32Constant(0);
  Node* ten = m.Int32Constant(10);
  Node* one = m.Int32Constant(1);

  MLabel header, body, body_cont, end;

  m.Goto(&header);

  m.Bind(&header);
4055 4056 4057
  Node* i = m.Phi(kMachInt32, zero, zero);
  Node* j = m.Phi(kMachInt32, zero, zero);
  Node* k = m.Phi(kMachInt32, zero, zero);
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
  m.Goto(&body);

  m.Bind(&body);
  Node* next_i = m.Int32Add(i, one);
  Node* next_j = m.Int32Add(j, one);
  Node* next_k = m.Int32Add(j, one);
  m.Branch(m.Word32Equal(next_i, ten), &end, &body_cont);

  m.Bind(&body_cont);
  i->ReplaceInput(1, next_i);
  j->ReplaceInput(1, next_j);
  k->ReplaceInput(1, next_k);
  m.Goto(&header);

  m.Bind(&end);
  m.Return(ten);

  CHECK_EQ(10, m.Call());
}


TEST(RunAddTree) {
  RawMachineAssemblerTester<int32_t> m;
  int32_t inputs[] = {11, 12, 13, 14, 15, 16, 17, 18};

  Node* base = m.PointerConstant(inputs);
4084 4085 4086 4087 4088 4089 4090 4091
  Node* n0 = m.Load(kMachInt32, base, m.Int32Constant(0 * sizeof(int32_t)));
  Node* n1 = m.Load(kMachInt32, base, m.Int32Constant(1 * sizeof(int32_t)));
  Node* n2 = m.Load(kMachInt32, base, m.Int32Constant(2 * sizeof(int32_t)));
  Node* n3 = m.Load(kMachInt32, base, m.Int32Constant(3 * sizeof(int32_t)));
  Node* n4 = m.Load(kMachInt32, base, m.Int32Constant(4 * sizeof(int32_t)));
  Node* n5 = m.Load(kMachInt32, base, m.Int32Constant(5 * sizeof(int32_t)));
  Node* n6 = m.Load(kMachInt32, base, m.Int32Constant(6 * sizeof(int32_t)));
  Node* n7 = m.Load(kMachInt32, base, m.Int32Constant(7 * sizeof(int32_t)));
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122

  Node* i1 = m.Int32Add(n0, n1);
  Node* i2 = m.Int32Add(n2, n3);
  Node* i3 = m.Int32Add(n4, n5);
  Node* i4 = m.Int32Add(n6, n7);

  Node* i5 = m.Int32Add(i1, i2);
  Node* i6 = m.Int32Add(i3, i4);

  Node* i7 = m.Int32Add(i5, i6);

  m.Return(i7);

  CHECK_EQ(116, m.Call());
}


static const int kFloat64CompareHelperTestCases = 15;
static const int kFloat64CompareHelperNodeType = 4;

static int Float64CompareHelper(RawMachineAssemblerTester<int32_t>* m,
                                int test_case, int node_type, double x,
                                double y) {
  static double buffer[2];
  buffer[0] = x;
  buffer[1] = y;
  CHECK(0 <= test_case && test_case < kFloat64CompareHelperTestCases);
  CHECK(0 <= node_type && node_type < kFloat64CompareHelperNodeType);
  CHECK(x < y);
  bool load_a = node_type / 2 == 1;
  bool load_b = node_type % 2 == 1;
4123
  Node* a = load_a ? m->Load(kMachFloat64, m->PointerConstant(&buffer[0]))
4124
                   : m->Float64Constant(x);
4125
  Node* b = load_b ? m->Load(kMachFloat64, m->PointerConstant(&buffer[1]))
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204
                   : m->Float64Constant(y);
  Node* cmp = NULL;
  bool expected = false;
  switch (test_case) {
    // Equal tests.
    case 0:
      cmp = m->Float64Equal(a, b);
      expected = false;
      break;
    case 1:
      cmp = m->Float64Equal(a, a);
      expected = true;
      break;
    // LessThan tests.
    case 2:
      cmp = m->Float64LessThan(a, b);
      expected = true;
      break;
    case 3:
      cmp = m->Float64LessThan(b, a);
      expected = false;
      break;
    case 4:
      cmp = m->Float64LessThan(a, a);
      expected = false;
      break;
    // LessThanOrEqual tests.
    case 5:
      cmp = m->Float64LessThanOrEqual(a, b);
      expected = true;
      break;
    case 6:
      cmp = m->Float64LessThanOrEqual(b, a);
      expected = false;
      break;
    case 7:
      cmp = m->Float64LessThanOrEqual(a, a);
      expected = true;
      break;
    // NotEqual tests.
    case 8:
      cmp = m->Float64NotEqual(a, b);
      expected = true;
      break;
    case 9:
      cmp = m->Float64NotEqual(b, a);
      expected = true;
      break;
    case 10:
      cmp = m->Float64NotEqual(a, a);
      expected = false;
      break;
    // GreaterThan tests.
    case 11:
      cmp = m->Float64GreaterThan(a, a);
      expected = false;
      break;
    case 12:
      cmp = m->Float64GreaterThan(a, b);
      expected = false;
      break;
    // GreaterThanOrEqual tests.
    case 13:
      cmp = m->Float64GreaterThanOrEqual(a, a);
      expected = true;
      break;
    case 14:
      cmp = m->Float64GreaterThanOrEqual(b, a);
      expected = true;
      break;
    default:
      UNREACHABLE();
  }
  m->Return(cmp);
  return expected;
}


TEST(RunFloat64Compare) {
4205
  double inf = V8_INFINITY;
4206 4207 4208 4209 4210 4211 4212
  // All pairs (a1, a2) are of the form a1 < a2.
  double inputs[] = {0.0,  1.0,  -1.0, 0.22, -1.22, 0.22,
                     -inf, 0.22, 0.22, inf,  -inf,  inf};

  for (int test = 0; test < kFloat64CompareHelperTestCases; test++) {
    for (int node_type = 0; node_type < kFloat64CompareHelperNodeType;
         node_type++) {
4213
      for (size_t input = 0; input < arraysize(inputs); input += 2) {
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226
        RawMachineAssemblerTester<int32_t> m;
        int expected = Float64CompareHelper(&m, test, node_type, inputs[input],
                                            inputs[input + 1]);
        CHECK_EQ(expected, m.Call());
      }
    }
  }
}


TEST(RunFloat64UnorderedCompare) {
  RawMachineAssemblerTester<int32_t> m;

4227 4228 4229
  const Operator* operators[] = {m.machine()->Float64Equal(),
                                 m.machine()->Float64LessThan(),
                                 m.machine()->Float64LessThanOrEqual()};
4230

4231
  double nan = std::numeric_limits<double>::quiet_NaN();
4232 4233

  FOR_FLOAT64_INPUTS(i) {
4234
    for (size_t o = 0; o < arraysize(operators); ++o) {
4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252
      for (int j = 0; j < 2; j++) {
        RawMachineAssemblerTester<int32_t> m;
        Node* a = m.Float64Constant(*i);
        Node* b = m.Float64Constant(nan);
        if (j == 1) std::swap(a, b);
        m.Return(m.NewNode(operators[o], a, b));
        CHECK_EQ(0, m.Call());
      }
    }
  }
}


TEST(RunFloat64Equal) {
  double input_a = 0.0;
  double input_b = 0.0;

  RawMachineAssemblerTester<int32_t> m;
4253 4254
  Node* a = m.LoadFromPointer(&input_a, kMachFloat64);
  Node* b = m.LoadFromPointer(&input_b, kMachFloat64);
4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
  m.Return(m.Float64Equal(a, b));

  CompareWrapper cmp(IrOpcode::kFloat64Equal);
  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      input_a = *pl;
      input_b = *pr;
      int32_t expected = cmp.Float64Compare(input_a, input_b) ? 1 : 0;
      CHECK_EQ(expected, m.Call());
    }
  }
}


TEST(RunFloat64LessThan) {
  double input_a = 0.0;
  double input_b = 0.0;

  RawMachineAssemblerTester<int32_t> m;
4274 4275
  Node* a = m.LoadFromPointer(&input_a, kMachFloat64);
  Node* b = m.LoadFromPointer(&input_b, kMachFloat64);
4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
  m.Return(m.Float64LessThan(a, b));

  CompareWrapper cmp(IrOpcode::kFloat64LessThan);
  FOR_FLOAT64_INPUTS(pl) {
    FOR_FLOAT64_INPUTS(pr) {
      input_a = *pl;
      input_b = *pr;
      int32_t expected = cmp.Float64Compare(input_a, input_b) ? 1 : 0;
      CHECK_EQ(expected, m.Call());
    }
  }
}


4290
template <typename IntType, MachineType kRepresentation>
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
static void LoadStoreTruncation() {
  IntType input;

  RawMachineAssemblerTester<int32_t> m;
  Node* a = m.LoadFromPointer(&input, kRepresentation);
  Node* ap1 = m.Int32Add(a, m.Int32Constant(1));
  m.StoreToPointer(&input, kRepresentation, ap1);
  m.Return(ap1);

  const IntType max = std::numeric_limits<IntType>::max();
  const IntType min = std::numeric_limits<IntType>::min();

  // Test upper bound.
  input = max;
  CHECK_EQ(max + 1, m.Call());
  CHECK_EQ(min, input);

  // Test lower bound.
  input = min;
4310
  CHECK_EQ(static_cast<IntType>(max + 2), m.Call());
4311 4312 4313 4314 4315 4316
  CHECK_EQ(min + 1, input);

  // Test all one byte values that are not one byte bounds.
  for (int i = -127; i < 127; i++) {
    input = i;
    int expected = i >= 0 ? i + 1 : max + (i - min) + 2;
4317 4318
    CHECK_EQ(static_cast<IntType>(expected), m.Call());
    CHECK_EQ(static_cast<IntType>(i + 1), input);
4319 4320 4321 4322 4323
  }
}


TEST(RunLoadStoreTruncation) {
4324 4325
  LoadStoreTruncation<int8_t, kMachInt8>();
  LoadStoreTruncation<int16_t, kMachInt16>();
4326 4327 4328 4329 4330
}


static void IntPtrCompare(intptr_t left, intptr_t right) {
  for (int test = 0; test < 7; test++) {
4331
    RawMachineAssemblerTester<bool> m(kMachPtr, kMachPtr);
4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380
    Node* p0 = m.Parameter(0);
    Node* p1 = m.Parameter(1);
    Node* res = NULL;
    bool expected = false;
    switch (test) {
      case 0:
        res = m.IntPtrLessThan(p0, p1);
        expected = true;
        break;
      case 1:
        res = m.IntPtrLessThanOrEqual(p0, p1);
        expected = true;
        break;
      case 2:
        res = m.IntPtrEqual(p0, p1);
        expected = false;
        break;
      case 3:
        res = m.IntPtrGreaterThanOrEqual(p0, p1);
        expected = false;
        break;
      case 4:
        res = m.IntPtrGreaterThan(p0, p1);
        expected = false;
        break;
      case 5:
        res = m.IntPtrEqual(p0, p0);
        expected = true;
        break;
      case 6:
        res = m.IntPtrNotEqual(p0, p1);
        expected = true;
        break;
      default:
        UNREACHABLE();
        break;
    }
    m.Return(res);
    CHECK_EQ(expected, m.Call(reinterpret_cast<int32_t*>(left),
                              reinterpret_cast<int32_t*>(right)));
  }
}


TEST(RunIntPtrCompare) {
  intptr_t min = std::numeric_limits<intptr_t>::min();
  intptr_t max = std::numeric_limits<intptr_t>::max();
  // An ascending chain of intptr_t
  intptr_t inputs[] = {min, min / 2, -1, 0, 1, max / 2, max};
4381
  for (size_t i = 0; i < arraysize(inputs) - 1; i++) {
4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397
    IntPtrCompare(inputs[i], inputs[i + 1]);
  }
}


TEST(RunTestIntPtrArithmetic) {
  static const int kInputSize = 10;
  int32_t inputs[kInputSize];
  int32_t outputs[kInputSize];
  for (int i = 0; i < kInputSize; i++) {
    inputs[i] = i;
    outputs[i] = -1;
  }
  RawMachineAssemblerTester<int32_t*> m;
  Node* input = m.PointerConstant(&inputs[0]);
  Node* output = m.PointerConstant(&outputs[kInputSize - 1]);
4398
  Node* elem_size = m.IntPtrConstant(sizeof(inputs[0]));
4399
  for (int i = 0; i < kInputSize; i++) {
4400
    m.Store(kMachInt32, output, m.Load(kMachInt32, input));
4401 4402 4403 4404 4405 4406 4407 4408 4409
    input = m.IntPtrAdd(input, elem_size);
    output = m.IntPtrSub(output, elem_size);
  }
  m.Return(input);
  CHECK_EQ(&inputs[kInputSize], m.Call());
  for (int i = 0; i < kInputSize; i++) {
    CHECK_EQ(i, inputs[i]);
    CHECK_EQ(kInputSize - i - 1, outputs[i]);
  }
4410 4411 4412
}


4413 4414
TEST(RunSpillLotsOfThings) {
  static const int kInputSize = 1000;
4415
  RawMachineAssemblerTester<int32_t> m;
4416 4417 4418 4419 4420 4421 4422 4423 4424
  Node* accs[kInputSize];
  int32_t outputs[kInputSize];
  Node* one = m.Int32Constant(1);
  Node* acc = one;
  for (int i = 0; i < kInputSize; i++) {
    acc = m.Int32Add(acc, one);
    accs[i] = acc;
  }
  for (int i = 0; i < kInputSize; i++) {
4425
    m.StoreToPointer(&outputs[i], kMachInt32, accs[i]);
4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
  }
  m.Return(one);
  m.Call();
  for (int i = 0; i < kInputSize; i++) {
    CHECK_EQ(outputs[i], i + 2);
  }
}


TEST(RunSpillConstantsAndParameters) {
4436
  static const int kInputSize = 1000;
4437
  static const int32_t kBase = 987;
4438
  RawMachineAssemblerTester<int32_t> m(kMachInt32, kMachInt32);
4439 4440 4441 4442
  int32_t outputs[kInputSize];
  Node* csts[kInputSize];
  Node* accs[kInputSize];
  Node* acc = m.Int32Constant(0);
4443
  for (int i = 0; i < kInputSize; i++) {
4444 4445
    csts[i] = m.Int32Constant(static_cast<int32_t>(kBase + i));
  }
4446
  for (int i = 0; i < kInputSize; i++) {
4447 4448 4449
    acc = m.Int32Add(acc, csts[i]);
    accs[i] = acc;
  }
4450
  for (int i = 0; i < kInputSize; i++) {
4451
    m.StoreToPointer(&outputs[i], kMachInt32, accs[i]);
4452 4453 4454 4455 4456
  }
  m.Return(m.Int32Add(acc, m.Int32Add(m.Parameter(0), m.Parameter(1))));
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      int32_t expected = *i + *j;
4457
      for (int k = 0; k < kInputSize; k++) {
4458 4459 4460 4461
        expected += kBase + k;
      }
      CHECK_EQ(expected, m.Call(*i, *j));
      expected = 0;
4462
      for (int k = 0; k < kInputSize; k++) {
4463 4464 4465 4466 4467 4468 4469 4470 4471
        expected += kBase + k;
        CHECK_EQ(expected, outputs[k]);
      }
    }
  }
}


TEST(RunNewSpaceConstantsInPhi) {
4472
  RawMachineAssemblerTester<Object*> m(kMachInt32);
4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487

  Isolate* isolate = CcTest::i_isolate();
  Handle<HeapNumber> true_val = isolate->factory()->NewHeapNumber(11.2);
  Handle<HeapNumber> false_val = isolate->factory()->NewHeapNumber(11.3);
  Node* true_node = m.HeapConstant(true_val);
  Node* false_node = m.HeapConstant(false_val);

  MLabel blocka, blockb, end;
  m.Branch(m.Parameter(0), &blocka, &blockb);
  m.Bind(&blocka);
  m.Goto(&end);
  m.Bind(&blockb);
  m.Goto(&end);

  m.Bind(&end);
4488
  Node* phi = m.Phi(kMachAnyTagged, true_node, false_node);
4489 4490 4491 4492 4493 4494 4495
  m.Return(phi);

  CHECK_EQ(*false_val, m.Call(0));
  CHECK_EQ(*true_val, m.Call(1));
}


4496 4497 4498 4499
TEST(RunInt32AddWithOverflowP) {
  int32_t actual_val = -1;
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
4500 4501 4502
  Node* add = m.Int32AddWithOverflow(bt.param0, bt.param1);
  Node* val = m.Projection(0, add);
  Node* ovf = m.Projection(1, add);
4503
  m.StoreToPointer(&actual_val, kMachInt32, val);
4504 4505 4506 4507
  bt.AddReturn(ovf);
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      int32_t expected_val;
4508
      int expected_ovf = bits::SignedAddOverflow32(*i, *j, &expected_val);
4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
      CHECK_EQ(expected_ovf, bt.call(*i, *j));
      CHECK_EQ(expected_val, actual_val);
    }
  }
}


TEST(RunInt32AddWithOverflowImm) {
  int32_t actual_val = -1, expected_val = 0;
  FOR_INT32_INPUTS(i) {
    {
4520
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
4521 4522 4523
      Node* add = m.Int32AddWithOverflow(m.Int32Constant(*i), m.Parameter(0));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4524
      m.StoreToPointer(&actual_val, kMachInt32, val);
4525 4526
      m.Return(ovf);
      FOR_INT32_INPUTS(j) {
4527
        int expected_ovf = bits::SignedAddOverflow32(*i, *j, &expected_val);
4528 4529 4530 4531 4532
        CHECK_EQ(expected_ovf, m.Call(*j));
        CHECK_EQ(expected_val, actual_val);
      }
    }
    {
4533
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
4534 4535 4536
      Node* add = m.Int32AddWithOverflow(m.Parameter(0), m.Int32Constant(*i));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4537
      m.StoreToPointer(&actual_val, kMachInt32, val);
4538 4539
      m.Return(ovf);
      FOR_INT32_INPUTS(j) {
4540
        int expected_ovf = bits::SignedAddOverflow32(*i, *j, &expected_val);
4541 4542 4543 4544 4545 4546
        CHECK_EQ(expected_ovf, m.Call(*j));
        CHECK_EQ(expected_val, actual_val);
      }
    }
    FOR_INT32_INPUTS(j) {
      RawMachineAssemblerTester<int32_t> m;
4547 4548 4549 4550
      Node* add =
          m.Int32AddWithOverflow(m.Int32Constant(*i), m.Int32Constant(*j));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4551
      m.StoreToPointer(&actual_val, kMachInt32, val);
4552
      m.Return(ovf);
4553
      int expected_ovf = bits::SignedAddOverflow32(*i, *j, &expected_val);
4554 4555 4556 4557 4558 4559 4560 4561
      CHECK_EQ(expected_ovf, m.Call());
      CHECK_EQ(expected_val, actual_val);
    }
  }
}


TEST(RunInt32AddWithOverflowInBranchP) {
4562
  int constant = 911777;
4563 4564 4565
  MLabel blocka, blockb;
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
4566 4567
  Node* add = m.Int32AddWithOverflow(bt.param0, bt.param1);
  Node* ovf = m.Projection(1, add);
4568 4569
  m.Branch(ovf, &blocka, &blockb);
  m.Bind(&blocka);
4570
  bt.AddReturn(m.Int32Constant(constant));
4571
  m.Bind(&blockb);
4572
  Node* val = m.Projection(0, add);
4573
  bt.AddReturn(val);
4574 4575
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
4576
      int32_t expected;
4577
      if (bits::SignedAddOverflow32(*i, *j, &expected)) expected = constant;
4578 4579 4580 4581 4582
      CHECK_EQ(expected, bt.call(*i, *j));
    }
  }
}

4583 4584 4585 4586 4587

TEST(RunInt32SubWithOverflowP) {
  int32_t actual_val = -1;
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
4588 4589 4590
  Node* add = m.Int32SubWithOverflow(bt.param0, bt.param1);
  Node* val = m.Projection(0, add);
  Node* ovf = m.Projection(1, add);
4591
  m.StoreToPointer(&actual_val, kMachInt32, val);
4592 4593 4594 4595
  bt.AddReturn(ovf);
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      int32_t expected_val;
4596
      int expected_ovf = bits::SignedSubOverflow32(*i, *j, &expected_val);
4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
      CHECK_EQ(expected_ovf, bt.call(*i, *j));
      CHECK_EQ(expected_val, actual_val);
    }
  }
}


TEST(RunInt32SubWithOverflowImm) {
  int32_t actual_val = -1, expected_val = 0;
  FOR_INT32_INPUTS(i) {
    {
4608
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
4609 4610 4611
      Node* add = m.Int32SubWithOverflow(m.Int32Constant(*i), m.Parameter(0));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4612
      m.StoreToPointer(&actual_val, kMachInt32, val);
4613 4614
      m.Return(ovf);
      FOR_INT32_INPUTS(j) {
4615
        int expected_ovf = bits::SignedSubOverflow32(*i, *j, &expected_val);
4616 4617 4618 4619 4620
        CHECK_EQ(expected_ovf, m.Call(*j));
        CHECK_EQ(expected_val, actual_val);
      }
    }
    {
4621
      RawMachineAssemblerTester<int32_t> m(kMachInt32);
4622 4623 4624
      Node* add = m.Int32SubWithOverflow(m.Parameter(0), m.Int32Constant(*i));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4625
      m.StoreToPointer(&actual_val, kMachInt32, val);
4626 4627
      m.Return(ovf);
      FOR_INT32_INPUTS(j) {
4628
        int expected_ovf = bits::SignedSubOverflow32(*j, *i, &expected_val);
4629 4630 4631 4632 4633 4634
        CHECK_EQ(expected_ovf, m.Call(*j));
        CHECK_EQ(expected_val, actual_val);
      }
    }
    FOR_INT32_INPUTS(j) {
      RawMachineAssemblerTester<int32_t> m;
4635 4636 4637 4638
      Node* add =
          m.Int32SubWithOverflow(m.Int32Constant(*i), m.Int32Constant(*j));
      Node* val = m.Projection(0, add);
      Node* ovf = m.Projection(1, add);
4639
      m.StoreToPointer(&actual_val, kMachInt32, val);
4640
      m.Return(ovf);
4641
      int expected_ovf = bits::SignedSubOverflow32(*i, *j, &expected_val);
4642 4643 4644 4645 4646 4647 4648 4649
      CHECK_EQ(expected_ovf, m.Call());
      CHECK_EQ(expected_val, actual_val);
    }
  }
}


TEST(RunInt32SubWithOverflowInBranchP) {
4650
  int constant = 911999;
4651 4652 4653
  MLabel blocka, blockb;
  RawMachineAssemblerTester<int32_t> m;
  Int32BinopTester bt(&m);
4654 4655
  Node* sub = m.Int32SubWithOverflow(bt.param0, bt.param1);
  Node* ovf = m.Projection(1, sub);
4656 4657
  m.Branch(ovf, &blocka, &blockb);
  m.Bind(&blocka);
4658
  bt.AddReturn(m.Int32Constant(constant));
4659
  m.Bind(&blockb);
4660
  Node* val = m.Projection(0, sub);
4661
  bt.AddReturn(val);
4662 4663
  FOR_INT32_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
4664
      int32_t expected;
4665
      if (bits::SignedSubOverflow32(*i, *j, &expected)) expected = constant;
4666 4667 4668 4669 4670
      CHECK_EQ(expected, bt.call(*i, *j));
    }
  }
}

4671

4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
TEST(RunWord64EqualInBranchP) {
  int64_t input;
  MLabel blocka, blockb;
  RawMachineAssemblerTester<int64_t> m;
  if (!m.machine()->Is64()) return;
  Node* value = m.LoadFromPointer(&input, kMachInt64);
  m.Branch(m.Word64Equal(value, m.Int64Constant(0)), &blocka, &blockb);
  m.Bind(&blocka);
  m.Return(m.Int32Constant(1));
  m.Bind(&blockb);
  m.Return(m.Int32Constant(2));
  input = V8_INT64_C(0);
  CHECK_EQ(1, m.Call());
  input = V8_INT64_C(1);
  CHECK_EQ(2, m.Call());
  input = V8_INT64_C(0x100000000);
  CHECK_EQ(2, m.Call());
}


4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
TEST(RunChangeInt32ToInt64P) {
  if (kPointerSize < 8) return;
  int64_t actual = -1;
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
  m.StoreToPointer(&actual, kMachInt64, m.ChangeInt32ToInt64(m.Parameter(0)));
  m.Return(m.Int32Constant(0));
  FOR_INT32_INPUTS(i) {
    int64_t expected = *i;
    CHECK_EQ(0, m.Call(*i));
    CHECK_EQ(expected, actual);
  }
}


TEST(RunChangeUint32ToUint64P) {
  if (kPointerSize < 8) return;
  int64_t actual = -1;
  RawMachineAssemblerTester<int32_t> m(kMachUint32);
  m.StoreToPointer(&actual, kMachUint64,
                   m.ChangeUint32ToUint64(m.Parameter(0)));
  m.Return(m.Int32Constant(0));
  FOR_UINT32_INPUTS(i) {
    int64_t expected = static_cast<uint64_t>(*i);
    CHECK_EQ(0, m.Call(*i));
    CHECK_EQ(expected, actual);
  }
}


TEST(RunTruncateInt64ToInt32P) {
  if (kPointerSize < 8) return;
  int64_t expected = -1;
  RawMachineAssemblerTester<int32_t> m;
  m.Return(m.TruncateInt64ToInt32(m.LoadFromPointer(&expected, kMachInt64)));
  FOR_UINT32_INPUTS(i) {
    FOR_UINT32_INPUTS(j) {
      expected = (static_cast<uint64_t>(*j) << 32) | *i;
4729
      CHECK_EQ(static_cast<int32_t>(expected), m.Call());
4730 4731 4732 4733
    }
  }
}

4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745

TEST(RunTruncateFloat64ToInt32P) {
  struct {
    double from;
    double raw;
  } kValues[] = {{0, 0},
                 {0.5, 0},
                 {-0.5, 0},
                 {1.5, 1},
                 {-1.5, -1},
                 {5.5, 5},
                 {-5.0, -5},
4746
                 {std::numeric_limits<double>::quiet_NaN(), 0},
4747
                 {std::numeric_limits<double>::infinity(), 0},
4748
                 {-std::numeric_limits<double>::quiet_NaN(), 0},
4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
                 {-std::numeric_limits<double>::infinity(), 0},
                 {4.94065645841e-324, 0},
                 {-4.94065645841e-324, 0},
                 {0.9999999999999999, 0},
                 {-0.9999999999999999, 0},
                 {4294967296.0, 0},
                 {-4294967296.0, 0},
                 {9223372036854775000.0, 4294966272.0},
                 {-9223372036854775000.0, -4294966272.0},
                 {4.5036e+15, 372629504},
                 {-4.5036e+15, -372629504},
                 {287524199.5377777, 0x11234567},
                 {-287524199.5377777, -0x11234567},
                 {2300193596.302222, 2300193596.0},
                 {-2300193596.302222, -2300193596.0},
                 {4600387192.604444, 305419896},
                 {-4600387192.604444, -305419896},
                 {4823855600872397.0, 1737075661},
                 {-4823855600872397.0, -1737075661},
                 {4503603922337791.0, -1},
                 {-4503603922337791.0, 1},
                 {4503601774854143.0, 2147483647},
                 {-4503601774854143.0, -2147483647},
                 {9007207844675582.0, -2},
                 {-9007207844675582.0, 2},
                 {2.4178527921507624e+24, -536870912},
                 {-2.4178527921507624e+24, 536870912},
                 {2.417853945072267e+24, -536870912},
                 {-2.417853945072267e+24, 536870912},
                 {4.8357055843015248e+24, -1073741824},
                 {-4.8357055843015248e+24, 1073741824},
                 {4.8357078901445341e+24, -1073741824},
                 {-4.8357078901445341e+24, 1073741824},
                 {2147483647.0, 2147483647.0},
                 {-2147483648.0, -2147483648.0},
                 {9.6714111686030497e+24, -2147483648.0},
                 {-9.6714111686030497e+24, -2147483648.0},
                 {9.6714157802890681e+24, -2147483648.0},
                 {-9.6714157802890681e+24, -2147483648.0},
                 {1.9342813113834065e+25, 2147483648.0},
                 {-1.9342813113834065e+25, 2147483648.0},
                 {3.868562622766813e+25, 0},
                 {-3.868562622766813e+25, 0},
                 {1.7976931348623157e+308, 0},
                 {-1.7976931348623157e+308, 0}};
  double input = -1.0;
  RawMachineAssemblerTester<int32_t> m;
  m.Return(m.TruncateFloat64ToInt32(m.LoadFromPointer(&input, kMachFloat64)));
4797
  for (size_t i = 0; i < arraysize(kValues); ++i) {
4798 4799 4800 4801 4802 4803
    input = kValues[i].from;
    uint64_t expected = static_cast<int64_t>(kValues[i].raw);
    CHECK_EQ(static_cast<int>(expected), m.Call());
  }
}

4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820

TEST(RunChangeFloat32ToFloat64) {
  double actual = 0.0f;
  float expected = 0.0;
  RawMachineAssemblerTester<int32_t> m;
  m.StoreToPointer(
      &actual, kMachFloat64,
      m.ChangeFloat32ToFloat64(m.LoadFromPointer(&expected, kMachFloat32)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT32_INPUTS(i) {
    expected = *i;
    CHECK_EQ(0, m.Call());
    CHECK_EQ(expected, actual);
  }
}


4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
TEST(RunChangeFloat32ToFloat64_spilled) {
  RawMachineAssemblerTester<int32_t> m;
  const int kNumInputs = 32;
  int32_t magic = 0x786234;
  float input[kNumInputs];
  double result[kNumInputs];
  Node* input_node[kNumInputs];

  for (int i = 0; i < kNumInputs; i++) {
    input_node[i] =
        m.Load(kMachFloat32, m.PointerConstant(&input), m.Int32Constant(i * 4));
  }

  for (int i = 0; i < kNumInputs; i++) {
    m.Store(kMachFloat64, m.PointerConstant(&result), m.Int32Constant(i * 8),
            m.ChangeFloat32ToFloat64(input_node[i]));
  }

  m.Return(m.Int32Constant(magic));

  for (int i = 0; i < kNumInputs; i++) {
    input[i] = 100.9f + i;
  }

  CHECK_EQ(magic, m.Call());

  for (int i = 0; i < kNumInputs; i++) {
    CHECK_EQ(result[i], static_cast<double>(input[i]));
  }
}


4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864
TEST(RunTruncateFloat64ToFloat32) {
  float actual = 0.0f;
  double input = 0.0;
  RawMachineAssemblerTester<int32_t> m;
  m.StoreToPointer(
      &actual, kMachFloat32,
      m.TruncateFloat64ToFloat32(m.LoadFromPointer(&input, kMachFloat64)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT64_INPUTS(i) {
    input = *i;
    volatile double expected = DoubleToFloat32(input);
    CHECK_EQ(0, m.Call());
4865
    CheckDoubleEq(expected, actual);
4866 4867 4868
  }
}

4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881

TEST(RunFloat32Constant) {
  FOR_FLOAT32_INPUTS(i) {
    float expected = *i;
    float actual = *i;
    RawMachineAssemblerTester<int32_t> m;
    m.StoreToPointer(&actual, kMachFloat32, m.Float32Constant(expected));
    m.Return(m.Int32Constant(0));
    CHECK_EQ(0, m.Call());
    CHECK_EQ(expected, actual);
  }
}

4882

4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948
TEST(RunFloat64ExtractLowWord32) {
  uint64_t input = 0;
  RawMachineAssemblerTester<int32_t> m;
  m.Return(m.Float64ExtractLowWord32(m.LoadFromPointer(&input, kMachFloat64)));
  FOR_FLOAT64_INPUTS(i) {
    input = bit_cast<uint64_t>(*i);
    int32_t expected = bit_cast<int32_t>(static_cast<uint32_t>(input));
    CHECK_EQ(expected, m.Call());
  }
}


TEST(RunFloat64ExtractHighWord32) {
  uint64_t input = 0;
  RawMachineAssemblerTester<int32_t> m;
  m.Return(m.Float64ExtractHighWord32(m.LoadFromPointer(&input, kMachFloat64)));
  FOR_FLOAT64_INPUTS(i) {
    input = bit_cast<uint64_t>(*i);
    int32_t expected = bit_cast<int32_t>(static_cast<uint32_t>(input >> 32));
    CHECK_EQ(expected, m.Call());
  }
}


TEST(RunFloat64InsertLowWord32) {
  uint64_t input = 0;
  uint64_t result = 0;
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
  m.StoreToPointer(
      &result, kMachFloat64,
      m.Float64InsertLowWord32(m.LoadFromPointer(&input, kMachFloat64),
                               m.Parameter(0)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT64_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      input = bit_cast<uint64_t>(*i);
      uint64_t expected = (input & ~(V8_UINT64_C(0xFFFFFFFF))) |
                          (static_cast<uint64_t>(bit_cast<uint32_t>(*j)));
      CHECK_EQ(0, m.Call(*j));
      CHECK_EQ(expected, result);
    }
  }
}


TEST(RunFloat64InsertHighWord32) {
  uint64_t input = 0;
  uint64_t result = 0;
  RawMachineAssemblerTester<int32_t> m(kMachInt32);
  m.StoreToPointer(
      &result, kMachFloat64,
      m.Float64InsertHighWord32(m.LoadFromPointer(&input, kMachFloat64),
                                m.Parameter(0)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT64_INPUTS(i) {
    FOR_INT32_INPUTS(j) {
      input = bit_cast<uint64_t>(*i);
      uint64_t expected = (input & ~(V8_UINT64_C(0xFFFFFFFF) << 32)) |
                          (static_cast<uint64_t>(bit_cast<uint32_t>(*j)) << 32);
      CHECK_EQ(0, m.Call(*j));
      CHECK_EQ(expected, result);
    }
  }
}


4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
TEST(RunFloat32Abs) {
  float input = -1.0;
  float result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
  m.StoreToPointer(&result, kMachFloat32,
                   m.Float32Abs(m.LoadFromPointer(&input, kMachFloat32)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT32_INPUTS(i) {
    input = *i;
    float expected = std::abs(input);
    CHECK_EQ(0, m.Call());
    CheckFloatEq(expected, result);
  }
}


TEST(RunFloat64Abs) {
  double input = -1.0;
  double result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
  m.StoreToPointer(&result, kMachFloat64,
                   m.Float64Abs(m.LoadFromPointer(&input, kMachFloat64)));
  m.Return(m.Int32Constant(0));
  FOR_FLOAT64_INPUTS(i) {
    input = *i;
    double expected = std::abs(input);
    CHECK_EQ(0, m.Call());
    CheckDoubleEq(expected, result);
  }
}


4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078
static double two_30 = 1 << 30;             // 2^30 is a smi boundary.
static double two_52 = two_30 * (1 << 22);  // 2^52 is a precision boundary.
static double kValues[] = {0.1,
                           0.2,
                           0.49999999999999994,
                           0.5,
                           0.7,
                           1.0 - std::numeric_limits<double>::epsilon(),
                           -0.1,
                           -0.49999999999999994,
                           -0.5,
                           -0.7,
                           1.1,
                           1.0 + std::numeric_limits<double>::epsilon(),
                           1.5,
                           1.7,
                           -1,
                           -1 + std::numeric_limits<double>::epsilon(),
                           -1 - std::numeric_limits<double>::epsilon(),
                           -1.1,
                           -1.5,
                           -1.7,
                           std::numeric_limits<double>::min(),
                           -std::numeric_limits<double>::min(),
                           std::numeric_limits<double>::max(),
                           -std::numeric_limits<double>::max(),
                           std::numeric_limits<double>::infinity(),
                           -std::numeric_limits<double>::infinity(),
                           two_30,
                           two_30 + 0.1,
                           two_30 + 0.5,
                           two_30 + 0.7,
                           two_30 - 1,
                           two_30 - 1 + 0.1,
                           two_30 - 1 + 0.5,
                           two_30 - 1 + 0.7,
                           -two_30,
                           -two_30 + 0.1,
                           -two_30 + 0.5,
                           -two_30 + 0.7,
                           -two_30 + 1,
                           -two_30 + 1 + 0.1,
                           -two_30 + 1 + 0.5,
                           -two_30 + 1 + 0.7,
                           two_52,
                           two_52 + 0.1,
                           two_52 + 0.5,
                           two_52 + 0.5,
                           two_52 + 0.7,
                           two_52 + 0.7,
                           two_52 - 1,
                           two_52 - 1 + 0.1,
                           two_52 - 1 + 0.5,
                           two_52 - 1 + 0.7,
                           -two_52,
                           -two_52 + 0.1,
                           -two_52 + 0.5,
                           -two_52 + 0.7,
                           -two_52 + 1,
                           -two_52 + 1 + 0.1,
                           -two_52 + 1 + 0.5,
                           -two_52 + 1 + 0.7,
                           two_30,
                           two_30 - 0.1,
                           two_30 - 0.5,
                           two_30 - 0.7,
                           two_30 - 1,
                           two_30 - 1 - 0.1,
                           two_30 - 1 - 0.5,
                           two_30 - 1 - 0.7,
                           -two_30,
                           -two_30 - 0.1,
                           -two_30 - 0.5,
                           -two_30 - 0.7,
                           -two_30 + 1,
                           -two_30 + 1 - 0.1,
                           -two_30 + 1 - 0.5,
                           -two_30 + 1 - 0.7,
                           two_52,
                           two_52 - 0.1,
                           two_52 - 0.5,
                           two_52 - 0.5,
                           two_52 - 0.7,
                           two_52 - 0.7,
                           two_52 - 1,
                           two_52 - 1 - 0.1,
                           two_52 - 1 - 0.5,
                           two_52 - 1 - 0.7,
                           -two_52,
                           -two_52 - 0.1,
                           -two_52 - 0.5,
                           -two_52 - 0.7,
                           -two_52 + 1,
                           -two_52 + 1 - 0.1,
                           -two_52 + 1 - 0.5,
                           -two_52 + 1 - 0.7};


5079
TEST(RunFloat64RoundDown1) {
5080 5081 5082
  double input = -1.0;
  double result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
5083
  if (!m.machine()->HasFloat64RoundDown()) return;
5084
  m.StoreToPointer(&result, kMachFloat64,
5085
                   m.Float64RoundDown(m.LoadFromPointer(&input, kMachFloat64)));
5086 5087 5088 5089 5090 5091 5092 5093 5094 5095
  m.Return(m.Int32Constant(0));
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    input = kValues[i];
    CHECK_EQ(0, m.Call());
    double expected = std::floor(kValues[i]);
    CHECK_EQ(expected, result);
  }
}


5096
TEST(RunFloat64RoundDown2) {
5097 5098 5099
  double input = -1.0;
  double result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
5100
  if (!m.machine()->HasFloat64RoundDown()) return;
5101
  m.StoreToPointer(&result, kMachFloat64,
5102 5103 5104 5105
                   m.Float64Sub(m.Float64Constant(-0.0),
                                m.Float64RoundDown(m.Float64Sub(
                                    m.Float64Constant(-0.0),
                                    m.LoadFromPointer(&input, kMachFloat64)))));
5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119
  m.Return(m.Int32Constant(0));
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    input = kValues[i];
    CHECK_EQ(0, m.Call());
    double expected = std::ceil(kValues[i]);
    CHECK_EQ(expected, result);
  }
}


TEST(RunFloat64RoundTruncate) {
  double input = -1.0;
  double result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
5120
  if (!m.machine()->HasFloat64RoundTruncate()) return;
5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
  m.StoreToPointer(
      &result, kMachFloat64,
      m.Float64RoundTruncate(m.LoadFromPointer(&input, kMachFloat64)));
  m.Return(m.Int32Constant(0));
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    input = kValues[i];
    CHECK_EQ(0, m.Call());
    double expected = trunc(kValues[i]);
    CHECK_EQ(expected, result);
  }
}


TEST(RunFloat64RoundTiesAway) {
  double input = -1.0;
  double result = 0.0;
  RawMachineAssemblerTester<int32_t> m;
  if (!m.machine()->HasFloat64RoundTiesAway()) return;
  m.StoreToPointer(
      &result, kMachFloat64,
      m.Float64RoundTiesAway(m.LoadFromPointer(&input, kMachFloat64)));
  m.Return(m.Int32Constant(0));
  for (size_t i = 0; i < arraysize(kValues); ++i) {
    input = kValues[i];
    CHECK_EQ(0, m.Call());
    double expected = round(kValues[i]);
    CHECK_EQ(expected, result);
  }
}
5150

5151
#endif  // V8_TURBOFAN_TARGET