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

#include <limits>

7
#include "src/compiler/access-builder.h"
8
#include "src/compiler/change-lowering.h"
9
#include "src/compiler/control-builders.h"
10
#include "src/compiler/graph-reducer.h"
11
#include "src/compiler/graph-visualizer.h"
12
#include "src/compiler/node-properties.h"
13
#include "src/compiler/pipeline.h"
14
#include "src/compiler/representation-change.h"
15
#include "src/compiler/simplified-lowering.h"
16
#include "src/compiler/source-position.h"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "src/compiler/typer.h"
#include "src/compiler/verifier.h"
#include "src/execution.h"
#include "src/parser.h"
#include "src/rewriter.h"
#include "src/scopes.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/codegen-tester.h"
#include "test/cctest/compiler/graph-builder-tester.h"
#include "test/cctest/compiler/value-helper.h"

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

template <typename ReturnType>
32
class SimplifiedLoweringTester : public GraphBuilderTester<ReturnType> {
33
 public:
34 35 36 37 38
  SimplifiedLoweringTester(MachineType p0 = kMachNone,
                           MachineType p1 = kMachNone,
                           MachineType p2 = kMachNone,
                           MachineType p3 = kMachNone,
                           MachineType p4 = kMachNone)
39
      : GraphBuilderTester<ReturnType>(p0, p1, p2, p3, p4),
40
        typer(this->isolate(), this->graph(), MaybeHandle<Context>()),
sigurds@chromium.org's avatar
sigurds@chromium.org committed
41
        javascript(this->zone()),
42 43
        jsgraph(this->isolate(), this->graph(), this->common(), &javascript,
                this->machine()),
44 45
        source_positions(jsgraph.graph()),
        lowering(&jsgraph, this->zone(), &source_positions) {}
46 47

  Typer typer;
sigurds@chromium.org's avatar
sigurds@chromium.org committed
48
  JSOperatorBuilder javascript;
49
  JSGraph jsgraph;
50
  SourcePositionTable source_positions;
51
  SimplifiedLowering lowering;
52

53
  void LowerAllNodes() {
54
    this->End();
55
    typer.Run();
56
    lowering.LowerAllNodes();
57 58
  }

59 60
  void LowerAllNodesAndLowerChanges() {
    this->End();
61
    typer.Run();
62 63
    lowering.LowerAllNodes();

64
    ChangeLowering lowering(&jsgraph);
65
    GraphReducer reducer(this->graph(), this->zone());
66 67 68 69 70
    reducer.AddReducer(&lowering);
    reducer.ReduceGraph();
    Verifier::Run(this->graph());
  }

71 72 73 74 75 76 77 78 79
  void CheckNumberCall(double expected, double input) {
    // TODO(titzer): make calls to NewNumber work in cctests.
    if (expected <= Smi::kMinValue) return;
    if (expected >= Smi::kMaxValue) return;
    Handle<Object> num = factory()->NewNumber(input);
    Object* result = this->Call(*num);
    CHECK(factory()->NewNumber(expected)->SameValue(result));
  }

80 81 82 83 84
  Factory* factory() { return this->isolate()->factory(); }
  Heap* heap() { return this->isolate()->heap(); }
};


85 86 87 88 89 90 91 92 93 94
// TODO(titzer): factor these tests out to test-run-simplifiedops.cc.
// TODO(titzer): test tagged representation for input to NumberToInt32.
TEST(RunNumberToInt32_float64) {
  // TODO(titzer): explicit load/stores here are only because of representations
  double input;
  int32_t result;
  SimplifiedLoweringTester<Object*> t;
  FieldAccess load = {kUntaggedBase, 0, Handle<Name>(), Type::Number(),
                      kMachFloat64};
  Node* loaded = t.LoadField(load, t.PointerConstant(&input));
95
  NodeProperties::SetBounds(loaded, Bounds(Type::Number()));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  Node* convert = t.NumberToInt32(loaded);
  FieldAccess store = {kUntaggedBase, 0, Handle<Name>(), Type::Signed32(),
                       kMachInt32};
  t.StoreField(store, t.PointerConstant(&result), convert);
  t.Return(t.jsgraph.TrueConstant());
  t.LowerAllNodes();
  t.GenerateCode();

  if (Pipeline::SupportedTarget()) {
    FOR_FLOAT64_INPUTS(i) {
      input = *i;
      int32_t expected = DoubleToInt32(*i);
      t.Call();
      CHECK_EQ(expected, result);
    }
  }
}


// TODO(titzer): test tagged representation for input to NumberToUint32.
TEST(RunNumberToUint32_float64) {
  // TODO(titzer): explicit load/stores here are only because of representations
  double input;
  uint32_t result;
  SimplifiedLoweringTester<Object*> t;
  FieldAccess load = {kUntaggedBase, 0, Handle<Name>(), Type::Number(),
                      kMachFloat64};
  Node* loaded = t.LoadField(load, t.PointerConstant(&input));
124
  NodeProperties::SetBounds(loaded, Bounds(Type::Number()));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  Node* convert = t.NumberToUint32(loaded);
  FieldAccess store = {kUntaggedBase, 0, Handle<Name>(), Type::Unsigned32(),
                       kMachUint32};
  t.StoreField(store, t.PointerConstant(&result), convert);
  t.Return(t.jsgraph.TrueConstant());
  t.LowerAllNodes();
  t.GenerateCode();

  if (Pipeline::SupportedTarget()) {
    FOR_FLOAT64_INPUTS(i) {
      input = *i;
      uint32_t expected = DoubleToUint32(*i);
      t.Call();
      CHECK_EQ(static_cast<int32_t>(expected), static_cast<int32_t>(result));
    }
  }
}


144 145 146 147 148 149 150 151 152 153
// Create a simple JSObject with a unique map.
static Handle<JSObject> TestObject() {
  static int index = 0;
  char buffer[50];
  v8::base::OS::SNPrintF(buffer, 50, "({'a_%d':1})", index++);
  return Handle<JSObject>::cast(v8::Utils::OpenHandle(*CompileRun(buffer)));
}


TEST(RunLoadMap) {
154
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
155
  FieldAccess access = AccessBuilder::ForMap();
156 157 158 159
  Node* load = t.LoadField(access, t.Parameter(0));
  t.Return(load);

  t.LowerAllNodes();
160
  t.GenerateCode();
161

162 163 164 165 166 167
  if (Pipeline::SupportedTarget()) {
    Handle<JSObject> src = TestObject();
    Handle<Map> src_map(src->map());
    Object* result = t.Call(*src);  // TODO(titzer): raw pointers in call
    CHECK_EQ(*src_map, result);
  }
168 169 170 171
}


TEST(RunStoreMap) {
172
  SimplifiedLoweringTester<int32_t> t(kMachAnyTagged, kMachAnyTagged);
173
  FieldAccess access = AccessBuilder::ForMap();
174
  t.StoreField(access, t.Parameter(1), t.Parameter(0));
175
  t.Return(t.jsgraph.TrueConstant());
176 177

  t.LowerAllNodes();
178
  t.GenerateCode();
179

180 181 182 183 184 185 186 187
  if (Pipeline::SupportedTarget()) {
    Handle<JSObject> src = TestObject();
    Handle<Map> src_map(src->map());
    Handle<JSObject> dst = TestObject();
    CHECK(src->map() != dst->map());
    t.Call(*src_map, *dst);  // TODO(titzer): raw pointers in call
    CHECK(*src_map == dst->map());
  }
188 189 190 191
}


TEST(RunLoadProperties) {
192
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
193
  FieldAccess access = AccessBuilder::ForJSObjectProperties();
194 195 196 197
  Node* load = t.LoadField(access, t.Parameter(0));
  t.Return(load);

  t.LowerAllNodes();
198
  t.GenerateCode();
199

200 201 202 203 204 205
  if (Pipeline::SupportedTarget()) {
    Handle<JSObject> src = TestObject();
    Handle<FixedArray> src_props(src->properties());
    Object* result = t.Call(*src);  // TODO(titzer): raw pointers in call
    CHECK_EQ(*src_props, result);
  }
206 207 208 209
}


TEST(RunLoadStoreMap) {
210
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged, kMachAnyTagged);
211
  FieldAccess access = AccessBuilder::ForMap();
212 213 214 215 216
  Node* load = t.LoadField(access, t.Parameter(0));
  t.StoreField(access, t.Parameter(1), load);
  t.Return(load);

  t.LowerAllNodes();
217
  t.GenerateCode();
218

219 220 221 222 223 224 225 226 227 228
  if (Pipeline::SupportedTarget()) {
    Handle<JSObject> src = TestObject();
    Handle<Map> src_map(src->map());
    Handle<JSObject> dst = TestObject();
    CHECK(src->map() != dst->map());
    Object* result = t.Call(*src, *dst);  // TODO(titzer): raw pointers in call
    CHECK(result->IsMap());
    CHECK_EQ(*src_map, result);
    CHECK(*src_map == dst->map());
  }
229 230 231 232
}


TEST(RunLoadStoreFixedArrayIndex) {
233
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
234
  ElementAccess access = AccessBuilder::ForFixedArrayElement();
235 236
  Node* load = t.LoadElement(access, t.Parameter(0), t.Int32Constant(0));
  t.StoreElement(access, t.Parameter(0), t.Int32Constant(1), load);
237 238 239
  t.Return(load);

  t.LowerAllNodes();
240
  t.GenerateCode();
241

242 243 244 245 246 247 248 249 250 251 252
  if (Pipeline::SupportedTarget()) {
    Handle<FixedArray> array = t.factory()->NewFixedArray(2);
    Handle<JSObject> src = TestObject();
    Handle<JSObject> dst = TestObject();
    array->set(0, *src);
    array->set(1, *dst);
    Object* result = t.Call(*array);
    CHECK_EQ(*src, result);
    CHECK_EQ(*src, array->get(0));
    CHECK_EQ(*src, array->get(1));
  }
253 254 255 256
}


TEST(RunLoadStoreArrayBuffer) {
257
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
258
  const int index = 12;
259
  const int array_length = 2 * index;
260
  ElementAccess buffer_access =
261
      AccessBuilder::ForTypedArrayElement(v8::kExternalInt8Array, true);
262 263
  Node* backing_store = t.LoadField(
      AccessBuilder::ForJSArrayBufferBackingStore(), t.Parameter(0));
264
  Node* load =
265
      t.LoadElement(buffer_access, backing_store, t.Int32Constant(index));
266
  t.StoreElement(buffer_access, backing_store, t.Int32Constant(index + 1),
267
                 load);
268
  t.Return(t.jsgraph.TrueConstant());
269 270

  t.LowerAllNodes();
271
  t.GenerateCode();
272

273 274 275 276 277 278 279
  if (Pipeline::SupportedTarget()) {
    Handle<JSArrayBuffer> array = t.factory()->NewJSArrayBuffer();
    Runtime::SetupArrayBufferAllocatingData(t.isolate(), array, array_length);
    uint8_t* data = reinterpret_cast<uint8_t*>(array->backing_store());
    for (int i = 0; i < array_length; i++) {
      data[i] = i;
    }
280

281 282 283 284 285 286 287 288
    // TODO(titzer): raw pointers in call
    Object* result = t.Call(*array);
    CHECK_EQ(t.isolate()->heap()->true_value(), result);
    for (int i = 0; i < array_length; i++) {
      uint8_t expected = i;
      if (i == (index + 1)) expected = index;
      CHECK_EQ(data[i], expected);
    }
289 290
  }
}
291 292 293 294 295


TEST(RunLoadFieldFromUntaggedBase) {
  Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)};

296
  for (size_t i = 0; i < arraysize(smis); i++) {
297 298
    int offset = static_cast<int>(i * sizeof(Smi*));
    FieldAccess access = {kUntaggedBase, offset, Handle<Name>(),
299
                          Type::Integral32(), kMachAnyTagged};
300

301
    SimplifiedLoweringTester<Object*> t;
302 303 304 305
    Node* load = t.LoadField(access, t.PointerConstant(smis));
    t.Return(load);
    t.LowerAllNodes();

306 307
    if (!Pipeline::SupportedTarget()) continue;

308 309 310 311 312 313 314 315 316 317 318 319
    for (int j = -5; j <= 5; j++) {
      Smi* expected = Smi::FromInt(j);
      smis[i] = expected;
      CHECK_EQ(expected, t.Call());
    }
  }
}


TEST(RunStoreFieldToUntaggedBase) {
  Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3)};

320
  for (size_t i = 0; i < arraysize(smis); i++) {
321 322
    int offset = static_cast<int>(i * sizeof(Smi*));
    FieldAccess access = {kUntaggedBase, offset, Handle<Name>(),
323
                          Type::Integral32(), kMachAnyTagged};
324

325
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
326 327 328 329 330
    Node* p0 = t.Parameter(0);
    t.StoreField(access, t.PointerConstant(smis), p0);
    t.Return(p0);
    t.LowerAllNodes();

331 332
    if (!Pipeline::SupportedTarget()) continue;

333 334 335 336 337 338 339 340 341 342 343 344 345 346
    for (int j = -5; j <= 5; j++) {
      Smi* expected = Smi::FromInt(j);
      smis[i] = Smi::FromInt(-100);
      CHECK_EQ(expected, t.Call(expected));
      CHECK_EQ(expected, smis[i]);
    }
  }
}


TEST(RunLoadElementFromUntaggedBase) {
  Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3),
                 Smi::FromInt(4), Smi::FromInt(5)};

347 348
  for (size_t i = 0; i < arraysize(smis); i++) {    // for header sizes
    for (size_t j = 0; (i + j) < arraysize(smis); j++) {  // for element index
349
      int offset = static_cast<int>(i * sizeof(Smi*));
350 351
      ElementAccess access = {kUntaggedBase, offset, Type::Integral32(),
                              kMachAnyTagged};
352

353
      SimplifiedLoweringTester<Object*> t;
354 355
      Node* load = t.LoadElement(access, t.PointerConstant(smis),
                                 t.Int32Constant(static_cast<int>(j)));
356 357 358
      t.Return(load);
      t.LowerAllNodes();

359 360
      if (!Pipeline::SupportedTarget()) continue;

361 362 363 364 365 366 367 368 369 370 371 372 373 374
      for (int k = -5; k <= 5; k++) {
        Smi* expected = Smi::FromInt(k);
        smis[i + j] = expected;
        CHECK_EQ(expected, t.Call());
      }
    }
  }
}


TEST(RunStoreElementFromUntaggedBase) {
  Smi* smis[] = {Smi::FromInt(1), Smi::FromInt(2), Smi::FromInt(3),
                 Smi::FromInt(4), Smi::FromInt(5)};

375 376
  for (size_t i = 0; i < arraysize(smis); i++) {    // for header sizes
    for (size_t j = 0; (i + j) < arraysize(smis); j++) {  // for element index
377
      int offset = static_cast<int>(i * sizeof(Smi*));
378 379
      ElementAccess access = {kUntaggedBase, offset, Type::Integral32(),
                              kMachAnyTagged};
380

381
      SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
382
      Node* p0 = t.Parameter(0);
383
      t.StoreElement(access, t.PointerConstant(smis),
384
                     t.Int32Constant(static_cast<int>(j)), p0);
385 386 387
      t.Return(p0);
      t.LowerAllNodes();

388 389
      if (!Pipeline::SupportedTarget()) continue;

390 391 392 393 394 395
      for (int k = -5; k <= 5; k++) {
        Smi* expected = Smi::FromInt(k);
        smis[i + j] = Smi::FromInt(-100);
        CHECK_EQ(expected, t.Call(expected));
        CHECK_EQ(expected, smis[i + j]);
      }
396 397

      // TODO(titzer): assert the contents of the array.
398 399 400
    }
  }
}
401 402 403 404 405 406 407 408 409


// A helper class for accessing fields and elements of various types, on both
// tagged and untagged base pointers. Contains both tagged and untagged buffers
// for testing direct memory access from generated code.
template <typename E>
class AccessTester : public HandleAndZoneScope {
 public:
  bool tagged;
410
  MachineType rep;
411 412 413 414 415
  E* original_elements;
  size_t num_elements;
  E* untagged_array;
  Handle<ByteArray> tagged_array;  // TODO(titzer): use FixedArray for tagged.

416
  AccessTester(bool t, MachineType r, E* orig, size_t num)
417 418 419 420 421
      : tagged(t),
        rep(r),
        original_elements(orig),
        num_elements(num),
        untagged_array(static_cast<E*>(malloc(ByteSize()))),
422 423
        tagged_array(main_isolate()->factory()->NewByteArray(
            static_cast<int>(ByteSize()))) {
424 425 426 427 428 429 430 431 432 433
    Reinitialize();
  }

  ~AccessTester() { free(untagged_array); }

  size_t ByteSize() { return num_elements * sizeof(E); }

  // Nuke both {untagged_array} and {tagged_array} with {original_elements}.
  void Reinitialize() {
    memcpy(untagged_array, original_elements, ByteSize());
434
    CHECK_EQ(static_cast<int>(ByteSize()), tagged_array->length());
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    E* raw = reinterpret_cast<E*>(tagged_array->GetDataStartAddress());
    memcpy(raw, original_elements, ByteSize());
  }

  // Create and run code that copies the element in either {untagged_array}
  // or {tagged_array} at index {from_index} to index {to_index}.
  void RunCopyElement(int from_index, int to_index) {
    // TODO(titzer): test element and field accesses where the base is not
    // a constant in the code.
    BoundsCheck(from_index);
    BoundsCheck(to_index);
    ElementAccess access = GetElementAccess();

    SimplifiedLoweringTester<Object*> t;
    Node* ptr = GetBaseNode(&t);
450 451
    Node* load = t.LoadElement(access, ptr, t.Int32Constant(from_index));
    t.StoreElement(access, ptr, t.Int32Constant(to_index), load);
452 453
    t.Return(t.jsgraph.TrueConstant());
    t.LowerAllNodes();
454
    t.GenerateCode();
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    if (Pipeline::SupportedTarget()) {
      Object* result = t.Call();
      CHECK_EQ(t.isolate()->heap()->true_value(), result);
    }
  }

  // Create and run code that copies the field in either {untagged_array}
  // or {tagged_array} at index {from_index} to index {to_index}.
  void RunCopyField(int from_index, int to_index) {
    BoundsCheck(from_index);
    BoundsCheck(to_index);
    FieldAccess from_access = GetFieldAccess(from_index);
    FieldAccess to_access = GetFieldAccess(to_index);

    SimplifiedLoweringTester<Object*> t;
    Node* ptr = GetBaseNode(&t);
    Node* load = t.LoadField(from_access, ptr);
    t.StoreField(to_access, ptr, load);
    t.Return(t.jsgraph.TrueConstant());
    t.LowerAllNodes();
476
    t.GenerateCode();
477 478 479 480 481 482 483 484 485

    if (Pipeline::SupportedTarget()) {
      Object* result = t.Call();
      CHECK_EQ(t.isolate()->heap()->true_value(), result);
    }
  }

  // Create and run code that copies the elements from {this} to {that}.
  void RunCopyElements(AccessTester<E>* that) {
486 487
// TODO(titzer): Rewrite this test without StructuredGraphBuilder support.
#if 0
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    SimplifiedLoweringTester<Object*> t;

    Node* one = t.Int32Constant(1);
    Node* index = t.Int32Constant(0);
    Node* limit = t.Int32Constant(static_cast<int>(num_elements));
    t.environment()->Push(index);
    Node* src = this->GetBaseNode(&t);
    Node* dst = that->GetBaseNode(&t);
    {
      LoopBuilder loop(&t);
      loop.BeginLoop();
      // Loop exit condition
      index = t.environment()->Top();
      Node* condition = t.Int32LessThan(index, limit);
      loop.BreakUnless(condition);
      // dst[index] = src[index]
      index = t.environment()->Pop();
      Node* load = t.LoadElement(this->GetElementAccess(), src, index);
      t.StoreElement(that->GetElementAccess(), dst, index, load);
      // index++
      index = t.Int32Add(index, one);
      t.environment()->Push(index);
      // continue
      loop.EndBody();
      loop.EndLoop();
    }
    index = t.environment()->Pop();
    t.Return(t.jsgraph.TrueConstant());
    t.LowerAllNodes();
517
    t.GenerateCode();
518 519 520 521 522

    if (Pipeline::SupportedTarget()) {
      Object* result = t.Call();
      CHECK_EQ(t.isolate()->heap()->true_value(), result);
    }
523
#endif
524 525 526 527 528 529 530 531 532 533 534 535 536 537
  }

  E GetElement(int index) {
    BoundsCheck(index);
    if (tagged) {
      E* raw = reinterpret_cast<E*>(tagged_array->GetDataStartAddress());
      return raw[index];
    } else {
      return untagged_array[index];
    }
  }

 private:
  ElementAccess GetElementAccess() {
538 539 540
    ElementAccess access = {tagged ? kTaggedBase : kUntaggedBase,
                            tagged ? FixedArrayBase::kHeaderSize : 0,
                            Type::Any(), rep};
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    return access;
  }

  FieldAccess GetFieldAccess(int field) {
    int offset = field * sizeof(E);
    FieldAccess access = {tagged ? kTaggedBase : kUntaggedBase,
                          offset + (tagged ? FixedArrayBase::kHeaderSize : 0),
                          Handle<Name>(), Type::Any(), rep};
    return access;
  }

  template <typename T>
  Node* GetBaseNode(SimplifiedLoweringTester<T>* t) {
    return tagged ? t->HeapConstant(tagged_array)
                  : t->PointerConstant(untagged_array);
  }

  void BoundsCheck(int index) {
    CHECK_GE(index, 0);
    CHECK_LT(index, static_cast<int>(num_elements));
561
    CHECK_EQ(static_cast<int>(ByteSize()), tagged_array->length());
562 563 564 565 566
  }
};


template <typename E>
567
static void RunAccessTest(MachineType rep, E* original_elements, size_t num) {
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  int num_elements = static_cast<int>(num);

  for (int taggedness = 0; taggedness < 2; taggedness++) {
    AccessTester<E> a(taggedness == 1, rep, original_elements, num);
    for (int field = 0; field < 2; field++) {
      for (int i = 0; i < num_elements - 1; i++) {
        a.Reinitialize();
        if (field == 0) {
          a.RunCopyField(i, i + 1);  // Test field read/write.
        } else {
          a.RunCopyElement(i, i + 1);  // Test element read/write.
        }
        if (Pipeline::SupportedTarget()) {  // verify.
          for (int j = 0; j < num_elements; j++) {
            E expect =
                j == (i + 1) ? original_elements[i] : original_elements[j];
            CHECK_EQ(expect, a.GetElement(j));
          }
        }
      }
    }
  }
  // Test array copy.
  for (int tf = 0; tf < 2; tf++) {
    for (int tt = 0; tt < 2; tt++) {
      AccessTester<E> a(tf == 1, rep, original_elements, num);
      AccessTester<E> b(tt == 1, rep, original_elements, num);
      a.RunCopyElements(&b);
      if (Pipeline::SupportedTarget()) {  // verify.
        for (int i = 0; i < num_elements; i++) {
          CHECK_EQ(a.GetElement(i), b.GetElement(i));
        }
      }
    }
  }
}


TEST(RunAccessTests_uint8) {
  uint8_t data[] = {0x07, 0x16, 0x25, 0x34, 0x43, 0x99,
                    0xab, 0x78, 0x89, 0x19, 0x2b, 0x38};
609
  RunAccessTest<uint8_t>(kMachInt8, data, arraysize(data));
610 611 612 613 614
}


TEST(RunAccessTests_uint16) {
  uint16_t data[] = {0x071a, 0x162b, 0x253c, 0x344d, 0x435e, 0x7777};
615
  RunAccessTest<uint16_t>(kMachInt16, data, arraysize(data));
616 617 618 619 620
}


TEST(RunAccessTests_int32) {
  int32_t data[] = {-211, 211, 628347, 2000000000, -2000000000, -1, -100000034};
621
  RunAccessTest<int32_t>(kMachInt32, data, arraysize(data));
622 623 624 625 626 627 628 629 630 631 632 633 634
}


#define V8_2PART_INT64(a, b) (((static_cast<int64_t>(a) << 32) + 0x##b##u))


TEST(RunAccessTests_int64) {
  if (kPointerSize != 8) return;
  int64_t data[] = {V8_2PART_INT64(0x10111213, 14151617),
                    V8_2PART_INT64(0x20212223, 24252627),
                    V8_2PART_INT64(0x30313233, 34353637),
                    V8_2PART_INT64(0xa0a1a2a3, a4a5a6a7),
                    V8_2PART_INT64(0xf0f1f2f3, f4f5f6f7)};
635
  RunAccessTest<int64_t>(kMachInt64, data, arraysize(data));
636 637 638 639 640
}


TEST(RunAccessTests_float64) {
  double data[] = {1.25, -1.25, 2.75, 11.0, 11100.8};
641
  RunAccessTest<double>(kMachFloat64, data, arraysize(data));
642 643 644 645 646 647 648
}


TEST(RunAccessTests_Smi) {
  Smi* data[] = {Smi::FromInt(-1),    Smi::FromInt(-9),
                 Smi::FromInt(0),     Smi::FromInt(666),
                 Smi::FromInt(77777), Smi::FromInt(Smi::kMaxValue)};
649
  RunAccessTest<Smi*>(kMachAnyTagged, data, arraysize(data));
650 651 652 653 654 655 656
}


// Fills in most of the nodes of the graph in order to make tests shorter.
class TestingGraph : public HandleAndZoneScope, public GraphAndBuilders {
 public:
  Typer typer;
sigurds@chromium.org's avatar
sigurds@chromium.org committed
657
  JSOperatorBuilder javascript;
658 659 660
  JSGraph jsgraph;
  Node* p0;
  Node* p1;
661
  Node* p2;
662 663 664 665
  Node* start;
  Node* end;
  Node* ret;

666 667
  explicit TestingGraph(Type* p0_type, Type* p1_type = Type::None(),
                        Type* p2_type = Type::None())
668
      : GraphAndBuilders(main_zone()),
669
        typer(main_isolate(), graph(), MaybeHandle<Context>()),
sigurds@chromium.org's avatar
sigurds@chromium.org committed
670
        javascript(main_zone()),
671
        jsgraph(main_isolate(), graph(), common(), &javascript, machine()) {
672 673 674 675 676 677 678 679
    start = graph()->NewNode(common()->Start(2));
    graph()->SetStart(start);
    ret =
        graph()->NewNode(common()->Return(), jsgraph.Constant(0), start, start);
    end = graph()->NewNode(common()->End(), ret);
    graph()->SetEnd(end);
    p0 = graph()->NewNode(common()->Parameter(0), start);
    p1 = graph()->NewNode(common()->Parameter(1), start);
680
    p2 = graph()->NewNode(common()->Parameter(2), start);
681
    typer.Run();
682 683
    NodeProperties::SetBounds(p0, Bounds(p0_type));
    NodeProperties::SetBounds(p1, Bounds(p1_type));
684
    NodeProperties::SetBounds(p2, Bounds(p2_type));
685 686
  }

687
  void CheckLoweringBinop(IrOpcode::Value expected, const Operator* op) {
688 689 690 691 692
    Node* node = Return(graph()->NewNode(op, p0, p1));
    Lower();
    CHECK_EQ(expected, node->opcode());
  }

693 694
  void CheckLoweringTruncatedBinop(IrOpcode::Value expected, const Operator* op,
                                   const Operator* trunc) {
695 696 697 698 699 700
    Node* node = graph()->NewNode(op, p0, p1);
    Return(graph()->NewNode(trunc, node));
    Lower();
    CHECK_EQ(expected, node->opcode());
  }

701 702 703 704
  void Lower() {
    SourcePositionTable table(jsgraph.graph());
    SimplifiedLowering(&jsgraph, jsgraph.zone(), &table).LowerAllNodes();
  }
705 706 707 708 709 710 711 712 713 714

  // Inserts the node as the return value of the graph.
  Node* Return(Node* node) {
    ret->ReplaceInput(0, node);
    return node;
  }

  // Inserts the node as the effect input to the return of the graph.
  void Effect(Node* node) { ret->ReplaceInput(1, node); }

715
  Node* ExampleWithOutput(MachineType type) {
716
    // TODO(titzer): use parameters with guaranteed representations.
717
    if (type & kTypeInt32) {
718 719
      return graph()->NewNode(machine()->Int32Add(), jsgraph.Int32Constant(1),
                              jsgraph.Int32Constant(1));
720
    } else if (type & kTypeUint32) {
721 722
      return graph()->NewNode(machine()->Word32Shr(), jsgraph.Int32Constant(1),
                              jsgraph.Int32Constant(1));
723
    } else if (type & kRepFloat64) {
724 725 726
      return graph()->NewNode(machine()->Float64Add(),
                              jsgraph.Float64Constant(1),
                              jsgraph.Float64Constant(1));
727
    } else if (type & kRepBit) {
728 729 730
      return graph()->NewNode(machine()->Word32Equal(),
                              jsgraph.Int32Constant(1),
                              jsgraph.Int32Constant(1));
731
    } else if (type & kRepWord64) {
732 733 734
      return graph()->NewNode(machine()->Int64Add(), Int64Constant(1),
                              Int64Constant(1));
    } else {
735
      CHECK(type & kRepTagged);
736 737 738 739
      return p0;
    }
  }

740 741 742 743 744 745 746 747 748 749 750
  Node* ExampleWithTypeAndRep(Type* type, MachineType mach_type) {
    FieldAccess access = {kUntaggedBase, 0, Handle<Name>::null(), type,
                          mach_type};
    // TODO(titzer): using loads here just to force the representation is ugly.
    Node* node = graph()->NewNode(simplified()->LoadField(access),
                                  jsgraph.IntPtrConstant(0), graph()->start(),
                                  graph()->start());
    NodeProperties::SetBounds(node, Bounds(type));
    return node;
  }

751 752
  Node* Use(Node* node, MachineType type) {
    if (type & kTypeInt32) {
753 754
      return graph()->NewNode(machine()->Int32LessThan(), node,
                              jsgraph.Int32Constant(1));
755
    } else if (type & kTypeUint32) {
756 757
      return graph()->NewNode(machine()->Uint32LessThan(), node,
                              jsgraph.Int32Constant(1));
758
    } else if (type & kRepFloat64) {
759 760
      return graph()->NewNode(machine()->Float64Add(), node,
                              jsgraph.Float64Constant(1));
761
    } else if (type & kRepWord64) {
762 763
      return graph()->NewNode(machine()->Int64LessThan(), node,
                              Int64Constant(1));
764 765 766
    } else if (type & kRepWord32) {
      return graph()->NewNode(machine()->Word32Equal(), node,
                              jsgraph.Int32Constant(1));
767 768 769 770 771 772 773 774 775 776 777
    } else {
      return graph()->NewNode(simplified()->ReferenceEqual(Type::Any()), node,
                              jsgraph.TrueConstant());
    }
  }

  Node* Branch(Node* cond) {
    Node* br = graph()->NewNode(common()->Branch(), cond, start);
    Node* tb = graph()->NewNode(common()->IfTrue(), br);
    Node* fb = graph()->NewNode(common()->IfFalse(), br);
    Node* m = graph()->NewNode(common()->Merge(2), tb, fb);
778
    NodeProperties::ReplaceControlInput(ret, m);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    return br;
  }

  Node* Int64Constant(int64_t v) {
    return graph()->NewNode(common()->Int64Constant(v));
  }

  SimplifiedOperatorBuilder* simplified() { return &main_simplified_; }
  MachineOperatorBuilder* machine() { return &main_machine_; }
  CommonOperatorBuilder* common() { return &main_common_; }
  Graph* graph() { return main_graph_; }
};


TEST(LowerBooleanNot_bit_bit) {
794
  // BooleanNot(x: kRepBit) used as kRepBit
795
  TestingGraph t(Type::Boolean());
796
  Node* b = t.ExampleWithOutput(kRepBit);
797 798 799 800
  Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
  Node* use = t.Branch(inv);
  t.Lower();
  Node* cmp = use->InputAt(0);
801
  CHECK_EQ(t.machine()->Word32Equal()->opcode(), cmp->opcode());
802 803 804 805 806 807 808
  CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
  Node* f = t.jsgraph.Int32Constant(0);
  CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
}


TEST(LowerBooleanNot_bit_tagged) {
809
  // BooleanNot(x: kRepBit) used as kRepTagged
810
  TestingGraph t(Type::Boolean());
811
  Node* b = t.ExampleWithOutput(kRepBit);
812
  Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
813
  Node* use = t.Use(inv, kRepTagged);
814 815 816 817
  t.Return(use);
  t.Lower();
  CHECK_EQ(IrOpcode::kChangeBitToBool, use->InputAt(0)->opcode());
  Node* cmp = use->InputAt(0)->InputAt(0);
818
  CHECK_EQ(t.machine()->Word32Equal()->opcode(), cmp->opcode());
819 820 821 822 823 824 825
  CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
  Node* f = t.jsgraph.Int32Constant(0);
  CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
}


TEST(LowerBooleanNot_tagged_bit) {
826
  // BooleanNot(x: kRepTagged) used as kRepBit
827 828 829 830 831 832 833 834 835 836 837 838 839 840
  TestingGraph t(Type::Boolean());
  Node* b = t.p0;
  Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
  Node* use = t.Branch(inv);
  t.Lower();
  Node* cmp = use->InputAt(0);
  CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
  CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
  Node* f = t.jsgraph.FalseConstant();
  CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
}


TEST(LowerBooleanNot_tagged_tagged) {
841
  // BooleanNot(x: kRepTagged) used as kRepTagged
842 843 844
  TestingGraph t(Type::Boolean());
  Node* b = t.p0;
  Node* inv = t.graph()->NewNode(t.simplified()->BooleanNot(), b);
845
  Node* use = t.Use(inv, kRepTagged);
846 847 848 849 850 851 852 853 854 855 856
  t.Return(use);
  t.Lower();
  CHECK_EQ(IrOpcode::kChangeBitToBool, use->InputAt(0)->opcode());
  Node* cmp = use->InputAt(0)->InputAt(0);
  CHECK_EQ(t.machine()->WordEqual()->opcode(), cmp->opcode());
  CHECK(b == cmp->InputAt(0) || b == cmp->InputAt(1));
  Node* f = t.jsgraph.FalseConstant();
  CHECK(f == cmp->InputAt(0) || f == cmp->InputAt(1));
}


857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
TEST(LowerBooleanToNumber_bit_int32) {
  // BooleanToNumber(x: kRepBit) used as kMachInt32
  TestingGraph t(Type::Boolean());
  Node* b = t.ExampleWithOutput(kRepBit);
  Node* cnv = t.graph()->NewNode(t.simplified()->BooleanToNumber(), b);
  Node* use = t.Use(cnv, kMachInt32);
  t.Return(use);
  t.Lower();
  CHECK_EQ(b, use->InputAt(0));
}


TEST(LowerBooleanToNumber_tagged_int32) {
  // BooleanToNumber(x: kRepTagged) used as kMachInt32
  TestingGraph t(Type::Boolean());
  Node* b = t.p0;
  Node* cnv = t.graph()->NewNode(t.simplified()->BooleanToNumber(), b);
  Node* use = t.Use(cnv, kMachInt32);
  t.Return(use);
  t.Lower();
  CHECK_EQ(t.machine()->WordEqual()->opcode(), cnv->opcode());
  CHECK(b == cnv->InputAt(0) || b == cnv->InputAt(1));
  Node* c = t.jsgraph.TrueConstant();
  CHECK(c == cnv->InputAt(0) || c == cnv->InputAt(1));
}


TEST(LowerBooleanToNumber_bit_tagged) {
  // BooleanToNumber(x: kRepBit) used as kMachAnyTagged
  TestingGraph t(Type::Boolean());
  Node* b = t.ExampleWithOutput(kRepBit);
  Node* cnv = t.graph()->NewNode(t.simplified()->BooleanToNumber(), b);
  Node* use = t.Use(cnv, kMachAnyTagged);
  t.Return(use);
  t.Lower();
  CHECK_EQ(b, use->InputAt(0)->InputAt(0));
  CHECK_EQ(IrOpcode::kChangeInt32ToTagged, use->InputAt(0)->opcode());
}


TEST(LowerBooleanToNumber_tagged_tagged) {
  // BooleanToNumber(x: kRepTagged) used as kMachAnyTagged
  TestingGraph t(Type::Boolean());
  Node* b = t.p0;
  Node* cnv = t.graph()->NewNode(t.simplified()->BooleanToNumber(), b);
  Node* use = t.Use(cnv, kMachAnyTagged);
  t.Return(use);
  t.Lower();
  CHECK_EQ(cnv, use->InputAt(0)->InputAt(0));
  CHECK_EQ(IrOpcode::kChangeInt32ToTagged, use->InputAt(0)->opcode());
  CHECK_EQ(t.machine()->WordEqual()->opcode(), cnv->opcode());
  CHECK(b == cnv->InputAt(0) || b == cnv->InputAt(1));
  Node* c = t.jsgraph.TrueConstant();
  CHECK(c == cnv->InputAt(0) || c == cnv->InputAt(1));
}


914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
static Type* test_types[] = {Type::Signed32(), Type::Unsigned32(),
                             Type::Number(), Type::Any()};


TEST(LowerNumberCmp_to_int32) {
  TestingGraph t(Type::Signed32(), Type::Signed32());

  t.CheckLoweringBinop(IrOpcode::kWord32Equal, t.simplified()->NumberEqual());
  t.CheckLoweringBinop(IrOpcode::kInt32LessThan,
                       t.simplified()->NumberLessThan());
  t.CheckLoweringBinop(IrOpcode::kInt32LessThanOrEqual,
                       t.simplified()->NumberLessThanOrEqual());
}


TEST(LowerNumberCmp_to_uint32) {
  TestingGraph t(Type::Unsigned32(), Type::Unsigned32());

  t.CheckLoweringBinop(IrOpcode::kWord32Equal, t.simplified()->NumberEqual());
  t.CheckLoweringBinop(IrOpcode::kUint32LessThan,
                       t.simplified()->NumberLessThan());
  t.CheckLoweringBinop(IrOpcode::kUint32LessThanOrEqual,
                       t.simplified()->NumberLessThanOrEqual());
}


TEST(LowerNumberCmp_to_float64) {
  static Type* types[] = {Type::Number(), Type::Any()};

943
  for (size_t i = 0; i < arraysize(types); i++) {
944 945 946 947 948 949 950 951 952 953 954 955 956
    TestingGraph t(types[i], types[i]);

    t.CheckLoweringBinop(IrOpcode::kFloat64Equal,
                         t.simplified()->NumberEqual());
    t.CheckLoweringBinop(IrOpcode::kFloat64LessThan,
                         t.simplified()->NumberLessThan());
    t.CheckLoweringBinop(IrOpcode::kFloat64LessThanOrEqual,
                         t.simplified()->NumberLessThanOrEqual());
  }
}


TEST(LowerNumberAddSub_to_int32) {
957
  HandleAndZoneScope scope;
958 959
  Type* small_range = Type::Range(1, 10, scope.main_zone());
  Type* large_range = Type::Range(-1e+13, 1e+14, scope.main_zone());
960 961 962 963 964 965 966 967 968 969 970 971 972 973
  static Type* types[] = {Type::Signed32(), Type::Integral32(), small_range,
                          large_range};

  for (size_t i = 0; i < arraysize(types); i++) {
    for (size_t j = 0; j < arraysize(types); j++) {
      TestingGraph t(types[i], types[j]);
      t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Add,
                                    t.simplified()->NumberAdd(),
                                    t.simplified()->NumberToInt32());
      t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Sub,
                                    t.simplified()->NumberSubtract(),
                                    t.simplified()->NumberToInt32());
    }
  }
974 975 976 977
}


TEST(LowerNumberAddSub_to_uint32) {
978
  HandleAndZoneScope scope;
979 980
  Type* small_range = Type::Range(1, 10, scope.main_zone());
  Type* large_range = Type::Range(-1e+13, 1e+14, scope.main_zone());
981 982 983 984 985 986 987 988 989 990 991 992 993 994
  static Type* types[] = {Type::Signed32(), Type::Integral32(), small_range,
                          large_range};

  for (size_t i = 0; i < arraysize(types); i++) {
    for (size_t j = 0; j < arraysize(types); j++) {
      TestingGraph t(types[i], types[j]);
      t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Add,
                                    t.simplified()->NumberAdd(),
                                    t.simplified()->NumberToUint32());
      t.CheckLoweringTruncatedBinop(IrOpcode::kInt32Sub,
                                    t.simplified()->NumberSubtract(),
                                    t.simplified()->NumberToUint32());
    }
  }
995 996 997 998
}


TEST(LowerNumberAddSub_to_float64) {
999
  for (size_t i = 0; i < arraysize(test_types); i++) {
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    TestingGraph t(test_types[i], test_types[i]);

    t.CheckLoweringBinop(IrOpcode::kFloat64Add, t.simplified()->NumberAdd());
    t.CheckLoweringBinop(IrOpcode::kFloat64Sub,
                         t.simplified()->NumberSubtract());
  }
}


TEST(LowerNumberDivMod_to_float64) {
1010
  for (size_t i = 0; i < arraysize(test_types); i++) {
1011 1012 1013
    TestingGraph t(test_types[i], test_types[i]);

    t.CheckLoweringBinop(IrOpcode::kFloat64Div, t.simplified()->NumberDivide());
1014 1015 1016 1017
    if (!test_types[i]->Is(Type::Unsigned32())) {
      t.CheckLoweringBinop(IrOpcode::kFloat64Mod,
                           t.simplified()->NumberModulus());
    }
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  }
}


static void CheckChangeOf(IrOpcode::Value change, Node* of, Node* node) {
  CHECK_EQ(change, node->opcode());
  CHECK_EQ(of, node->InputAt(0));
}


TEST(LowerNumberToInt32_to_nop) {
1029
  // NumberToInt32(x: kRepTagged | kTypeInt32) used as kRepTagged
1030 1031
  TestingGraph t(Type::Signed32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
1032
  Node* use = t.Use(trunc, kRepTagged);
1033 1034 1035 1036 1037 1038 1039
  t.Return(use);
  t.Lower();
  CHECK_EQ(t.p0, use->InputAt(0));
}


TEST(LowerNumberToInt32_to_ChangeTaggedToFloat64) {
1040
  // NumberToInt32(x: kRepTagged | kTypeInt32) used as kRepFloat64
1041 1042
  TestingGraph t(Type::Signed32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
1043
  Node* use = t.Use(trunc, kRepFloat64);
1044 1045 1046 1047 1048 1049 1050
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p0, use->InputAt(0));
}


TEST(LowerNumberToInt32_to_ChangeTaggedToInt32) {
1051
  // NumberToInt32(x: kRepTagged | kTypeInt32) used as kRepWord32
1052 1053
  TestingGraph t(Type::Signed32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
1054
  Node* use = t.Use(trunc, kTypeInt32);
1055 1056 1057 1058 1059 1060
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p0, use->InputAt(0));
}


1061 1062 1063
TEST(LowerNumberToInt32_to_TruncateFloat64ToInt32) {
  // NumberToInt32(x: kRepFloat64) used as kMachInt32
  TestingGraph t(Type::Number());
1064
  Node* p0 = t.ExampleWithTypeAndRep(Type::Number(), kMachFloat64);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), p0);
  Node* use = t.Use(trunc, kMachInt32);
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kTruncateFloat64ToInt32, p0, use->InputAt(0));
}


TEST(LowerNumberToInt32_to_TruncateFloat64ToInt32_with_change) {
  // NumberToInt32(x: kTypeNumber | kRepTagged) used as kMachInt32
  TestingGraph t(Type::Number());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), t.p0);
  Node* use = t.Use(trunc, kMachInt32);
  t.Return(use);
  t.Lower();
  Node* node = use->InputAt(0);
  CHECK_EQ(IrOpcode::kTruncateFloat64ToInt32, node->opcode());
  Node* of = node->InputAt(0);
  CHECK_EQ(IrOpcode::kChangeTaggedToFloat64, of->opcode());
  CHECK_EQ(t.p0, of->InputAt(0));
}


1088
TEST(LowerNumberToUint32_to_nop) {
1089
  // NumberToUint32(x: kRepTagged | kTypeUint32) used as kRepTagged
1090 1091
  TestingGraph t(Type::Unsigned32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
1092
  Node* use = t.Use(trunc, kRepTagged);
1093 1094 1095 1096 1097 1098 1099
  t.Return(use);
  t.Lower();
  CHECK_EQ(t.p0, use->InputAt(0));
}


TEST(LowerNumberToUint32_to_ChangeTaggedToFloat64) {
1100
  // NumberToUint32(x: kRepTagged | kTypeUint32) used as kRepWord32
1101 1102
  TestingGraph t(Type::Unsigned32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
1103
  Node* use = t.Use(trunc, kRepFloat64);
1104 1105 1106 1107 1108 1109 1110
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p0, use->InputAt(0));
}


TEST(LowerNumberToUint32_to_ChangeTaggedToUint32) {
1111
  // NumberToUint32(x: kRepTagged | kTypeUint32) used as kRepWord32
1112 1113
  TestingGraph t(Type::Unsigned32());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
1114
  Node* use = t.Use(trunc, kTypeUint32);
1115 1116 1117 1118 1119 1120
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kChangeTaggedToUint32, t.p0, use->InputAt(0));
}


1121 1122 1123 1124
TEST(LowerNumberToUint32_to_TruncateFloat64ToInt32) {
  // NumberToUint32(x: kRepFloat64) used as kMachUint32
  TestingGraph t(Type::Number());
  Node* p0 = t.ExampleWithOutput(kMachFloat64);
1125 1126
  // TODO(titzer): run the typer here, or attach machine type to param.
  NodeProperties::SetBounds(p0, Bounds(Type::Number()));
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), p0);
  Node* use = t.Use(trunc, kMachUint32);
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kTruncateFloat64ToInt32, p0, use->InputAt(0));
}


TEST(LowerNumberToUint32_to_TruncateFloat64ToInt32_with_change) {
  // NumberToInt32(x: kTypeNumber | kRepTagged) used as kMachUint32
  TestingGraph t(Type::Number());
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), t.p0);
  Node* use = t.Use(trunc, kMachUint32);
  t.Return(use);
  t.Lower();
  Node* node = use->InputAt(0);
  CHECK_EQ(IrOpcode::kTruncateFloat64ToInt32, node->opcode());
  Node* of = node->InputAt(0);
  CHECK_EQ(IrOpcode::kChangeTaggedToFloat64, of->opcode());
  CHECK_EQ(t.p0, of->InputAt(0));
}


1150 1151 1152 1153 1154 1155 1156 1157 1158
TEST(LowerNumberToUint32_to_TruncateFloat64ToInt32_uint32) {
  // NumberToUint32(x: kRepFloat64) used as kRepWord32
  TestingGraph t(Type::Unsigned32());
  Node* input = t.ExampleWithTypeAndRep(Type::Number(), kMachFloat64);
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), input);
  Node* use = t.Use(trunc, kRepWord32);
  t.Return(use);
  t.Lower();
  CheckChangeOf(IrOpcode::kTruncateFloat64ToInt32, input, use->InputAt(0));
1159 1160 1161
}


1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
TEST(LowerNumberToUI32_of_Float64_used_as_word32) {
  // NumberTo(Int,Uint)32(x: kRepFloat64 | kType(Int,Uint)32) used as
  // kType(Int,Uint)32 | kRepWord32
  Type* types[] = {Type::Signed32(), Type::Unsigned32()};
  MachineType mach[] = {kTypeInt32, kTypeUint32, kMachNone};

  for (int i = 0; i < 2; i++) {
    for (int u = 0; u < 3; u++) {
      TestingGraph t(types[i]);
      Node* input = t.ExampleWithTypeAndRep(
          types[i], static_cast<MachineType>(kRepFloat64 | mach[i]));
      const Operator* op = i == 0 ? t.simplified()->NumberToInt32()
                                  : t.simplified()->NumberToUint32();
      Node* trunc = t.graph()->NewNode(op, input);
      Node* use = t.Use(trunc, static_cast<MachineType>(kRepWord32 | mach[u]));
      t.Return(use);
      t.Lower();
      IrOpcode::Value opcode = i == 0 ? IrOpcode::kChangeFloat64ToInt32
                                      : IrOpcode::kChangeFloat64ToUint32;
      CheckChangeOf(opcode, input, use->InputAt(0));
    }
  }
1184 1185 1186
}


1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
TEST(LowerNumberToUI32_of_Float64_used_as_tagged) {
  // NumberTo(Int,Uint)32(x: kRepFloat64 | kType(Int,Uint)32) used as
  // kType(Int,Uint)32 | kRepTagged
  Type* types[] = {Type::Signed32(), Type::Unsigned32(), Type::Any()};
  MachineType mach[] = {kTypeInt32, kTypeUint32, kMachNone};

  for (int i = 0; i < 2; i++) {
    for (int u = 0; u < 3; u++) {
      TestingGraph t(types[i]);
      Node* input = t.ExampleWithTypeAndRep(
          types[i], static_cast<MachineType>(kRepFloat64 | mach[i]));
      const Operator* op = i == 0 ? t.simplified()->NumberToInt32()
                                  : t.simplified()->NumberToUint32();
      Node* trunc = t.graph()->NewNode(op, input);
      // TODO(titzer): we use the store here to force the representation.
      FieldAccess access = {kTaggedBase, 0, Handle<Name>(), types[u],
                            static_cast<MachineType>(mach[u] | kRepTagged)};
      Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
                                       trunc, t.start, t.start);
      t.Effect(store);
      t.Lower();
      CheckChangeOf(IrOpcode::kChangeFloat64ToTagged, input, store->InputAt(2));
    }
  }
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
}


TEST(LowerReferenceEqual_to_wordeq) {
  TestingGraph t(Type::Any(), Type::Any());
  IrOpcode::Value opcode =
      static_cast<IrOpcode::Value>(t.machine()->WordEqual()->opcode());
  t.CheckLoweringBinop(opcode, t.simplified()->ReferenceEqual(Type::Any()));
}


1222
TEST(LowerStringOps_to_call_and_compare) {
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  if (Pipeline::SupportedTarget()) {
    // These tests need linkage for the calls.
    TestingGraph t(Type::String(), Type::String());
    IrOpcode::Value compare_eq =
        static_cast<IrOpcode::Value>(t.machine()->WordEqual()->opcode());
    IrOpcode::Value compare_lt =
        static_cast<IrOpcode::Value>(t.machine()->IntLessThan()->opcode());
    IrOpcode::Value compare_le = static_cast<IrOpcode::Value>(
        t.machine()->IntLessThanOrEqual()->opcode());
    t.CheckLoweringBinop(compare_eq, t.simplified()->StringEqual());
    t.CheckLoweringBinop(compare_lt, t.simplified()->StringLessThan());
    t.CheckLoweringBinop(compare_le, t.simplified()->StringLessThanOrEqual());
    t.CheckLoweringBinop(IrOpcode::kCall, t.simplified()->StringAdd());
  }
1237 1238 1239
}


1240 1241
void CheckChangeInsertion(IrOpcode::Value expected, MachineType from,
                          MachineType to) {
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  TestingGraph t(Type::Any());
  Node* in = t.ExampleWithOutput(from);
  Node* use = t.Use(in, to);
  t.Return(use);
  t.Lower();
  CHECK_EQ(expected, use->InputAt(0)->opcode());
  CHECK_EQ(in, use->InputAt(0)->InputAt(0));
}


TEST(InsertBasicChanges) {
1253 1254 1255 1256 1257 1258 1259
  CheckChangeInsertion(IrOpcode::kChangeFloat64ToInt32, kRepFloat64,
                       kTypeInt32);
  CheckChangeInsertion(IrOpcode::kChangeFloat64ToUint32, kRepFloat64,
                       kTypeUint32);
  CheckChangeInsertion(IrOpcode::kChangeTaggedToInt32, kRepTagged, kTypeInt32);
  CheckChangeInsertion(IrOpcode::kChangeTaggedToUint32, kRepTagged,
                       kTypeUint32);
1260

1261 1262 1263 1264
  CheckChangeInsertion(IrOpcode::kChangeFloat64ToTagged, kRepFloat64,
                       kRepTagged);
  CheckChangeInsertion(IrOpcode::kChangeTaggedToFloat64, kRepTagged,
                       kRepFloat64);
1265

1266 1267 1268
  CheckChangeInsertion(IrOpcode::kChangeInt32ToFloat64, kTypeInt32,
                       kRepFloat64);
  CheckChangeInsertion(IrOpcode::kChangeInt32ToTagged, kTypeInt32, kRepTagged);
1269

1270 1271 1272 1273
  CheckChangeInsertion(IrOpcode::kChangeUint32ToFloat64, kTypeUint32,
                       kRepFloat64);
  CheckChangeInsertion(IrOpcode::kChangeUint32ToTagged, kTypeUint32,
                       kRepTagged);
1274 1275 1276
}


1277
static void CheckChangesAroundBinop(TestingGraph* t, const Operator* op,
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
                                    IrOpcode::Value input_change,
                                    IrOpcode::Value output_change) {
  Node* binop = t->graph()->NewNode(op, t->p0, t->p1);
  t->Return(binop);
  t->Lower();
  CHECK_EQ(input_change, binop->InputAt(0)->opcode());
  CHECK_EQ(input_change, binop->InputAt(1)->opcode());
  CHECK_EQ(t->p0, binop->InputAt(0)->InputAt(0));
  CHECK_EQ(t->p1, binop->InputAt(1)->InputAt(0));
  CHECK_EQ(output_change, t->ret->InputAt(0)->opcode());
  CHECK_EQ(binop, t->ret->InputAt(0)->InputAt(0));
}


TEST(InsertChangesAroundInt32Binops) {
  TestingGraph t(Type::Signed32(), Type::Signed32());

1295 1296 1297 1298 1299
  const Operator* ops[] = {t.machine()->Int32Add(),  t.machine()->Int32Sub(),
                           t.machine()->Int32Mul(),  t.machine()->Int32Div(),
                           t.machine()->Int32Mod(),  t.machine()->Word32And(),
                           t.machine()->Word32Or(),  t.machine()->Word32Xor(),
                           t.machine()->Word32Shl(), t.machine()->Word32Sar()};
1300

1301
  for (size_t i = 0; i < arraysize(ops); i++) {
1302 1303 1304 1305 1306 1307 1308 1309 1310
    CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToInt32,
                            IrOpcode::kChangeInt32ToTagged);
  }
}


TEST(InsertChangesAroundInt32Cmp) {
  TestingGraph t(Type::Signed32(), Type::Signed32());

1311 1312
  const Operator* ops[] = {t.machine()->Int32LessThan(),
                           t.machine()->Int32LessThanOrEqual()};
1313

1314
  for (size_t i = 0; i < arraysize(ops); i++) {
1315 1316 1317 1318 1319 1320 1321 1322 1323
    CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToInt32,
                            IrOpcode::kChangeBitToBool);
  }
}


TEST(InsertChangesAroundUint32Cmp) {
  TestingGraph t(Type::Unsigned32(), Type::Unsigned32());

1324 1325
  const Operator* ops[] = {t.machine()->Uint32LessThan(),
                           t.machine()->Uint32LessThanOrEqual()};
1326

1327
  for (size_t i = 0; i < arraysize(ops); i++) {
1328 1329 1330 1331 1332 1333 1334 1335 1336
    CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToUint32,
                            IrOpcode::kChangeBitToBool);
  }
}


TEST(InsertChangesAroundFloat64Binops) {
  TestingGraph t(Type::Number(), Type::Number());

1337
  const Operator* ops[] = {
1338 1339 1340 1341 1342
      t.machine()->Float64Add(), t.machine()->Float64Sub(),
      t.machine()->Float64Mul(), t.machine()->Float64Div(),
      t.machine()->Float64Mod(),
  };

1343
  for (size_t i = 0; i < arraysize(ops); i++) {
1344 1345 1346 1347 1348 1349 1350 1351 1352
    CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToFloat64,
                            IrOpcode::kChangeFloat64ToTagged);
  }
}


TEST(InsertChangesAroundFloat64Cmp) {
  TestingGraph t(Type::Number(), Type::Number());

1353 1354 1355
  const Operator* ops[] = {t.machine()->Float64Equal(),
                           t.machine()->Float64LessThan(),
                           t.machine()->Float64LessThanOrEqual()};
1356

1357
  for (size_t i = 0; i < arraysize(ops); i++) {
1358 1359 1360 1361 1362 1363
    CheckChangesAroundBinop(&t, ops[i], IrOpcode::kChangeTaggedToFloat64,
                            IrOpcode::kChangeBitToBool);
  }
}


1364 1365
namespace {

1366
void CheckFieldAccessArithmetic(FieldAccess access, Node* load_or_store) {
1367 1368
  IntPtrMatcher mindex(load_or_store->InputAt(1));
  CHECK(mindex.Is(access.offset - access.tag()));
1369 1370 1371 1372
}


Node* CheckElementAccessArithmetic(ElementAccess access, Node* load_or_store) {
1373 1374 1375 1376 1377
  Node* index = load_or_store->InputAt(1);
  if (kPointerSize == 8) {
    CHECK_EQ(IrOpcode::kChangeUint32ToUint64, index->opcode());
    index = index->InputAt(0);
  }
1378

1379 1380 1381
  Int32BinopMatcher mindex(index);
  CHECK_EQ(IrOpcode::kInt32Add, mindex.node()->opcode());
  CHECK(mindex.right().Is(access.header_size - access.tag()));
1382

1383 1384 1385 1386 1387 1388
  const int element_size_shift = ElementSizeLog2Of(access.machine_type);
  if (element_size_shift) {
    Int32BinopMatcher shl(mindex.left().node());
    CHECK_EQ(IrOpcode::kWord32Shl, shl.node()->opcode());
    CHECK(shl.right().Is(element_size_shift));
    return shl.left().node();
1389
  } else {
1390
    return mindex.left().node();
1391 1392 1393 1394
  }
}


1395 1396 1397 1398 1399
const MachineType kMachineReps[] = {kRepBit,       kMachInt8,  kMachInt16,
                                    kMachInt32,    kMachInt64, kMachFloat64,
                                    kMachAnyTagged};

}  // namespace
1400 1401 1402 1403 1404


TEST(LowerLoadField_to_load) {
  TestingGraph t(Type::Any(), Type::Signed32());

1405
  for (size_t i = 0; i < arraysize(kMachineReps); i++) {
1406
    FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1407
                          Handle<Name>::null(), Type::Any(), kMachineReps[i]};
1408 1409 1410

    Node* load =
        t.graph()->NewNode(t.simplified()->LoadField(access), t.p0, t.start);
1411
    Node* use = t.Use(load, kMachineReps[i]);
1412 1413 1414 1415 1416 1417
    t.Return(use);
    t.Lower();
    CHECK_EQ(IrOpcode::kLoad, load->opcode());
    CHECK_EQ(t.p0, load->InputAt(0));
    CheckFieldAccessArithmetic(access, load);

1418
    MachineType rep = OpParameter<MachineType>(load);
1419
    CHECK_EQ(kMachineReps[i], rep);
1420 1421 1422 1423 1424
  }
}


TEST(LowerStoreField_to_store) {
1425 1426
  {
    TestingGraph t(Type::Any(), Type::Signed32());
1427

1428 1429 1430
    for (size_t i = 0; i < arraysize(kMachineReps); i++) {
      FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
                            Handle<Name>::null(), Type::Any(), kMachineReps[i]};
1431 1432


1433 1434 1435 1436 1437 1438 1439 1440
      Node* val = t.ExampleWithOutput(kMachineReps[i]);
      Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
                                       val, t.start, t.start);
      t.Effect(store);
      t.Lower();
      CHECK_EQ(IrOpcode::kStore, store->opcode());
      CHECK_EQ(val, store->InputAt(2));
      CheckFieldAccessArithmetic(access, store);
1441

1442 1443 1444 1445 1446
      StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
      if (kMachineReps[i] & kRepTagged) {
        CHECK_EQ(kFullWriteBarrier, rep.write_barrier_kind());
      }
      CHECK_EQ(kMachineReps[i], rep.machine_type());
1447 1448
    }
  }
1449 1450 1451
  {
    TestingGraph t(Type::Any(),
                   Type::Intersect(Type::SignedSmall(), Type::TaggedSigned()));
1452 1453 1454
    FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
                          Handle<Name>::null(), Type::Any(), kMachAnyTagged};
    Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
1455
                                     t.p1, t.start, t.start);
1456 1457 1458
    t.Effect(store);
    t.Lower();
    CHECK_EQ(IrOpcode::kStore, store->opcode());
1459
    CHECK_EQ(t.p1, store->InputAt(2));
1460 1461 1462
    StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
    CHECK_EQ(kNoWriteBarrier, rep.write_barrier_kind());
  }
1463 1464 1465 1466 1467 1468
}


TEST(LowerLoadElement_to_load) {
  TestingGraph t(Type::Any(), Type::Signed32());

1469
  for (size_t i = 0; i < arraysize(kMachineReps); i++) {
1470 1471
    ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
                            Type::Any(), kMachineReps[i]};
1472

1473 1474
    Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
                                    t.p1, t.start, t.start);
1475
    Node* use = t.Use(load, kMachineReps[i]);
1476 1477 1478 1479 1480 1481
    t.Return(use);
    t.Lower();
    CHECK_EQ(IrOpcode::kLoad, load->opcode());
    CHECK_EQ(t.p0, load->InputAt(0));
    CheckElementAccessArithmetic(access, load);

1482
    MachineType rep = OpParameter<MachineType>(load);
1483
    CHECK_EQ(kMachineReps[i], rep);
1484 1485 1486 1487 1488
  }
}


TEST(LowerStoreElement_to_store) {
1489 1490
  {
    TestingGraph t(Type::Any(), Type::Signed32());
1491

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
    for (size_t i = 0; i < arraysize(kMachineReps); i++) {
      ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
                              Type::Any(), kMachineReps[i]};

      Node* val = t.ExampleWithOutput(kMachineReps[i]);
      Node* store = t.graph()->NewNode(t.simplified()->StoreElement(access),
                                       t.p0, t.p1, val, t.start, t.start);
      t.Effect(store);
      t.Lower();
      CHECK_EQ(IrOpcode::kStore, store->opcode());
      CHECK_EQ(val, store->InputAt(2));
      CheckElementAccessArithmetic(access, store);
1504

1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
      StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
      if (kMachineReps[i] & kRepTagged) {
        CHECK_EQ(kFullWriteBarrier, rep.write_barrier_kind());
      }
      CHECK_EQ(kMachineReps[i], rep.machine_type());
    }
  }
  {
    TestingGraph t(Type::Any(), Type::Signed32(),
                   Type::Intersect(Type::SignedSmall(), Type::TaggedSigned()));
    ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
                            Type::Any(), kMachAnyTagged};
1517
    Node* store = t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0,
1518
                                     t.p1, t.p2, t.start, t.start);
1519 1520 1521
    t.Effect(store);
    t.Lower();
    CHECK_EQ(IrOpcode::kStore, store->opcode());
1522
    CHECK_EQ(t.p2, store->InputAt(2));
1523
    StoreRepresentation rep = OpParameter<StoreRepresentation>(store);
1524
    CHECK_EQ(kNoWriteBarrier, rep.write_barrier_kind());
1525 1526 1527 1528 1529
  }
}


TEST(InsertChangeForLoadElementIndex) {
1530
  // LoadElement(obj: Tagged, index: kTypeInt32 | kRepTagged, length) =>
1531
  //   Load(obj, Int32Add(Int32Mul(ChangeTaggedToInt32(index), #k), #k))
1532 1533
  TestingGraph t(Type::Any(), Type::Signed32());
  ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1534
                          kMachAnyTagged};
1535 1536

  Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
1537
                                  t.p1, t.start, t.start);
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  t.Return(load);
  t.Lower();
  CHECK_EQ(IrOpcode::kLoad, load->opcode());
  CHECK_EQ(t.p0, load->InputAt(0));

  Node* index = CheckElementAccessArithmetic(access, load);
  CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p1, index);
}


TEST(InsertChangeForStoreElementIndex) {
1549
  // StoreElement(obj: Tagged, index: kTypeInt32 | kRepTagged, length, val) =>
1550
  //   Store(obj, Int32Add(Int32Mul(ChangeTaggedToInt32(index), #k), #k), val)
1551 1552
  TestingGraph t(Type::Any(), Type::Signed32());
  ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1553
                          kMachAnyTagged};
1554 1555

  Node* store =
1556
      t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0, t.p1,
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
                         t.jsgraph.TrueConstant(), t.start, t.start);
  t.Effect(store);
  t.Lower();
  CHECK_EQ(IrOpcode::kStore, store->opcode());
  CHECK_EQ(t.p0, store->InputAt(0));

  Node* index = CheckElementAccessArithmetic(access, store);
  CheckChangeOf(IrOpcode::kChangeTaggedToInt32, t.p1, index);
}


TEST(InsertChangeForLoadElement) {
  // TODO(titzer): test all load/store representation change insertions.
1570
  TestingGraph t(Type::Any(), Type::Signed32(), Type::Any());
1571
  ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1572
                          kMachFloat64};
1573 1574

  Node* load = t.graph()->NewNode(t.simplified()->LoadElement(access), t.p0,
1575
                                  t.p1, t.start, t.start);
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
  t.Return(load);
  t.Lower();
  CHECK_EQ(IrOpcode::kLoad, load->opcode());
  CHECK_EQ(t.p0, load->InputAt(0));
  CheckChangeOf(IrOpcode::kChangeFloat64ToTagged, load, t.ret->InputAt(0));
}


TEST(InsertChangeForLoadField) {
  // TODO(titzer): test all load/store representation change insertions.
  TestingGraph t(Type::Any(), Type::Signed32());
  FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1588
                        Handle<Name>::null(), Type::Any(), kMachFloat64};
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601

  Node* load =
      t.graph()->NewNode(t.simplified()->LoadField(access), t.p0, t.start);
  t.Return(load);
  t.Lower();
  CHECK_EQ(IrOpcode::kLoad, load->opcode());
  CHECK_EQ(t.p0, load->InputAt(0));
  CheckChangeOf(IrOpcode::kChangeFloat64ToTagged, load, t.ret->InputAt(0));
}


TEST(InsertChangeForStoreElement) {
  // TODO(titzer): test all load/store representation change insertions.
1602 1603
  TestingGraph t(Type::Any(), Type::Signed32());
  ElementAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize, Type::Any(),
1604
                          kMachFloat64};
1605

1606 1607 1608
  Node* store =
      t.graph()->NewNode(t.simplified()->StoreElement(access), t.p0,
                         t.jsgraph.Int32Constant(0), t.p1, t.start, t.start);
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
  t.Effect(store);
  t.Lower();

  CHECK_EQ(IrOpcode::kStore, store->opcode());
  CHECK_EQ(t.p0, store->InputAt(0));
  CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p1, store->InputAt(2));
}


TEST(InsertChangeForStoreField) {
  // TODO(titzer): test all load/store representation change insertions.
  TestingGraph t(Type::Any(), Type::Signed32());
  FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1622
                        Handle<Name>::null(), Type::Any(), kMachFloat64};
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632

  Node* store = t.graph()->NewNode(t.simplified()->StoreField(access), t.p0,
                                   t.p1, t.start, t.start);
  t.Effect(store);
  t.Lower();

  CHECK_EQ(IrOpcode::kStore, store->opcode());
  CHECK_EQ(t.p0, store->InputAt(0));
  CheckChangeOf(IrOpcode::kChangeTaggedToFloat64, t.p1, store->InputAt(2));
}
1633 1634 1635 1636 1637 1638


TEST(UpdatePhi) {
  TestingGraph t(Type::Any(), Type::Signed32());
  static const MachineType kMachineTypes[] = {kMachInt32, kMachUint32,
                                              kMachFloat64};
1639
  Type* kTypes[] = {Type::Signed32(), Type::Unsigned32(), Type::Number()};
1640 1641 1642

  for (size_t i = 0; i < arraysize(kMachineTypes); i++) {
    FieldAccess access = {kTaggedBase, FixedArrayBase::kHeaderSize,
1643
                          Handle<Name>::null(), kTypes[i], kMachineTypes[i]};
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

    Node* load0 =
        t.graph()->NewNode(t.simplified()->LoadField(access), t.p0, t.start);
    Node* load1 =
        t.graph()->NewNode(t.simplified()->LoadField(access), t.p1, t.start);
    Node* phi = t.graph()->NewNode(t.common()->Phi(kMachAnyTagged, 2), load0,
                                   load1, t.start);
    t.Return(t.Use(phi, kMachineTypes[i]));
    t.Lower();

    CHECK_EQ(IrOpcode::kPhi, phi->opcode());
    CHECK_EQ(RepresentationOf(kMachineTypes[i]),
             RepresentationOf(OpParameter<MachineType>(phi)));
  }
}
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673


TEST(RunNumberDivide_minus_1_TruncatingToInt32) {
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
  Node* num = t.NumberToInt32(t.Parameter(0));
  Node* div = t.NumberDivide(num, t.jsgraph.Constant(-1));
  Node* trunc = t.NumberToInt32(div);
  t.Return(trunc);

  if (Pipeline::SupportedTarget()) {
    t.LowerAllNodesAndLowerChanges();
    t.GenerateCode();

    FOR_INT32_INPUTS(i) {
      int32_t x = 0 - *i;
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 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
      t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
    }
  }
}


TEST(NumberMultiply_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, -1, 0, 1, 100, 1000};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Signed32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* mul = t.graph()->NewNode(t.simplified()->NumberMultiply(), t.p0, k);
    Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), mul);
    t.Return(trunc);
    t.Lower();

    CHECK_EQ(IrOpcode::kInt32Mul, mul->opcode());
  }
}


TEST(RunNumberMultiply_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, -1, 0, 1, 100, 1000, 3000999};

  for (size_t i = 0; i < arraysize(constants); i++) {
    double k = static_cast<double>(constants[i]);
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToInt32(t.Parameter(0));
    Node* mul = t.NumberMultiply(num, t.jsgraph.Constant(k));
    Node* trunc = t.NumberToInt32(mul);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_INT32_INPUTS(i) {
        int32_t x = DoubleToInt32(static_cast<double>(*i) * k);
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
    }
  }
}


TEST(RunNumberMultiply_TruncatingToUint32) {
  uint32_t constants[] = {0, 1, 2, 3, 4, 100, 1000, 1024, 2048, 3000999};

  for (size_t i = 0; i < arraysize(constants); i++) {
    double k = static_cast<double>(constants[i]);
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToUint32(t.Parameter(0));
    Node* mul = t.NumberMultiply(num, t.jsgraph.Constant(k));
    Node* trunc = t.NumberToUint32(mul);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_UINT32_INPUTS(i) {
        uint32_t x = DoubleToUint32(static_cast<double>(*i) * k);
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
    }
  }
}


TEST(RunNumberDivide_2_TruncatingToUint32) {
  SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
  Node* num = t.NumberToUint32(t.Parameter(0));
  Node* div = t.NumberDivide(num, t.jsgraph.Constant(2));
  Node* trunc = t.NumberToUint32(div);
  t.Return(trunc);

  if (Pipeline::SupportedTarget()) {
    t.LowerAllNodesAndLowerChanges();
    t.GenerateCode();

    FOR_UINT32_INPUTS(i) {
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
      uint32_t x = DoubleToUint32(static_cast<double>(*i / 2.0));
      t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
    }
  }
}


TEST(NumberMultiply_ConstantOutOfRange) {
  TestingGraph t(Type::Signed32());
  Node* k = t.jsgraph.Constant(1000000023);
  Node* mul = t.graph()->NewNode(t.simplified()->NumberMultiply(), t.p0, k);
  Node* trunc = t.graph()->NewNode(t.simplified()->NumberToInt32(), mul);
  t.Return(trunc);
  t.Lower();

  CHECK_EQ(IrOpcode::kFloat64Mul, mul->opcode());
}


TEST(NumberMultiply_NonTruncating) {
  TestingGraph t(Type::Signed32());
  Node* k = t.jsgraph.Constant(111);
  Node* mul = t.graph()->NewNode(t.simplified()->NumberMultiply(), t.p0, k);
  t.Return(mul);
  t.Lower();

  CHECK_EQ(IrOpcode::kFloat64Mul, mul->opcode());
}


TEST(NumberDivide_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, 1, 4, 100, 1000};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Signed32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* div = t.graph()->NewNode(t.simplified()->NumberDivide(), t.p0, k);
1793 1794
    Node* use = t.Use(div, kMachInt32);
    t.Return(use);
1795 1796
    t.Lower();

1797
    CHECK_EQ(IrOpcode::kInt32Div, use->InputAt(0)->opcode());
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
  }
}


TEST(RunNumberDivide_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, -1, 1, 2, 100, 1000, 1024, 2048};

  for (size_t i = 0; i < arraysize(constants); i++) {
    int32_t k = constants[i];
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToInt32(t.Parameter(0));
    Node* div = t.NumberDivide(num, t.jsgraph.Constant(k));
    Node* trunc = t.NumberToInt32(div);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_INT32_INPUTS(i) {
        if (*i == INT_MAX) continue;  // exclude max int.
        int32_t x = DoubleToInt32(static_cast<double>(*i) / k);
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
    }
  }
}


TEST(NumberDivide_TruncatingToUint32) {
  double constants[] = {1, 3, 100, 1000, 100998348};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Unsigned32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* div = t.graph()->NewNode(t.simplified()->NumberDivide(), t.p0, k);
1834 1835
    Node* use = t.Use(div, kMachUint32);
    t.Return(use);
1836 1837
    t.Lower();

1838
    CHECK_EQ(IrOpcode::kUint32Div, use->InputAt(0)->opcode());
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
  }
}


TEST(RunNumberDivide_TruncatingToUint32) {
  uint32_t constants[] = {100, 10, 1, 1, 2, 4, 1000, 1024, 2048};

  for (size_t i = 0; i < arraysize(constants); i++) {
    uint32_t k = constants[i];
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToUint32(t.Parameter(0));
    Node* div = t.NumberDivide(num, t.jsgraph.Constant(static_cast<double>(k)));
    Node* trunc = t.NumberToUint32(div);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_UINT32_INPUTS(i) {
        uint32_t x = *i / k;
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
1862 1863 1864
    }
  }
}
1865 1866 1867


TEST(NumberDivide_BadConstants) {
1868 1869 1870 1871 1872 1873 1874
  {
    TestingGraph t(Type::Signed32());
    Node* k = t.jsgraph.Constant(-1);
    Node* div = t.graph()->NewNode(t.simplified()->NumberDivide(), t.p0, k);
    Node* use = t.Use(div, kMachInt32);
    t.Return(use);
    t.Lower();
1875

1876 1877 1878 1879
    CHECK_EQ(IrOpcode::kInt32Sub, use->InputAt(0)->opcode());
  }

  {
1880
    TestingGraph t(Type::Signed32());
1881
    Node* k = t.jsgraph.Constant(0);
1882
    Node* div = t.graph()->NewNode(t.simplified()->NumberDivide(), t.p0, k);
1883 1884
    Node* use = t.Use(div, kMachInt32);
    t.Return(use);
1885 1886
    t.Lower();

1887 1888
    CHECK_EQ(IrOpcode::kInt32Constant, use->InputAt(0)->opcode());
    CHECK_EQ(0, OpParameter<int32_t>(use->InputAt(0)));
1889 1890 1891 1892 1893 1894
  }

  {
    TestingGraph t(Type::Unsigned32());
    Node* k = t.jsgraph.Constant(0);
    Node* div = t.graph()->NewNode(t.simplified()->NumberDivide(), t.p0, k);
1895 1896
    Node* use = t.Use(div, kMachUint32);
    t.Return(use);
1897 1898
    t.Lower();

1899 1900
    CHECK_EQ(IrOpcode::kInt32Constant, use->InputAt(0)->opcode());
    CHECK_EQ(0, OpParameter<int32_t>(use->InputAt(0)));
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
  }
}


TEST(NumberModulus_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, 1, 4, 100, 1000};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Signed32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* mod = t.graph()->NewNode(t.simplified()->NumberModulus(), t.p0, k);
1912 1913
    Node* use = t.Use(mod, kMachInt32);
    t.Return(use);
1914 1915
    t.Lower();

1916
    CHECK_EQ(IrOpcode::kInt32Mod, use->InputAt(0)->opcode());
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
  }
}


TEST(RunNumberModulus_TruncatingToInt32) {
  int32_t constants[] = {-100, -10, -1, 1, 2, 100, 1000, 1024, 2048};

  for (size_t i = 0; i < arraysize(constants); i++) {
    int32_t k = constants[i];
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToInt32(t.Parameter(0));
    Node* mod = t.NumberModulus(num, t.jsgraph.Constant(k));
    Node* trunc = t.NumberToInt32(mod);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_INT32_INPUTS(i) {
        if (*i == INT_MAX) continue;  // exclude max int.
        int32_t x = DoubleToInt32(std::fmod(static_cast<double>(*i), k));
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
    }
  }
}


TEST(NumberModulus_TruncatingToUint32) {
  double constants[] = {1, 3, 100, 1000, 100998348};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Unsigned32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* mod = t.graph()->NewNode(t.simplified()->NumberModulus(), t.p0, k);
    Node* trunc = t.graph()->NewNode(t.simplified()->NumberToUint32(), mod);
1954
    t.Return(trunc);
1955 1956
    t.Lower();

1957
    CHECK_EQ(IrOpcode::kUint32Mod, t.ret->InputAt(0)->InputAt(0)->opcode());
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
  }
}


TEST(RunNumberModulus_TruncatingToUint32) {
  uint32_t constants[] = {1, 2, 100, 1000, 1024, 2048};

  for (size_t i = 0; i < arraysize(constants); i++) {
    uint32_t k = constants[i];
    SimplifiedLoweringTester<Object*> t(kMachAnyTagged);
    Node* num = t.NumberToUint32(t.Parameter(0));
    Node* mod =
        t.NumberModulus(num, t.jsgraph.Constant(static_cast<double>(k)));
    Node* trunc = t.NumberToUint32(mod);
    t.Return(trunc);

    if (Pipeline::SupportedTarget()) {
      t.LowerAllNodesAndLowerChanges();
      t.GenerateCode();

      FOR_UINT32_INPUTS(i) {
        uint32_t x = *i % k;
        t.CheckNumberCall(static_cast<double>(x), static_cast<double>(*i));
      }
    }
  }
}


TEST(NumberModulus_Int32) {
  int32_t constants[] = {-100, -10, 1, 4, 100, 1000};

  for (size_t i = 0; i < arraysize(constants); i++) {
    TestingGraph t(Type::Signed32());
    Node* k = t.jsgraph.Constant(constants[i]);
    Node* mod = t.graph()->NewNode(t.simplified()->NumberModulus(), t.p0, k);
    t.Return(mod);
    t.Lower();

    CHECK_EQ(IrOpcode::kFloat64Mod, mod->opcode());  // Pesky -0 behavior.
  }
}
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018


TEST(NumberModulus_Uint32) {
  const double kConstants[] = {2, 100, 1000, 1024, 2048};
  const MachineType kTypes[] = {kMachInt32, kMachUint32};

  for (auto const type : kTypes) {
    for (auto const c : kConstants) {
      TestingGraph t(Type::Unsigned32());
      Node* k = t.jsgraph.Constant(c);
      Node* mod = t.graph()->NewNode(t.simplified()->NumberModulus(), t.p0, k);
      Node* use = t.Use(mod, type);
      t.Return(use);
      t.Lower();

      CHECK_EQ(IrOpcode::kUint32Mod, use->InputAt(0)->opcode());
    }
  }
}
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034


TEST(PhiRepresentation) {
  HandleAndZoneScope scope;
  Zone* z = scope.main_zone();

  struct TestData {
    Type* arg1;
    Type* arg2;
    MachineType use;
    MachineTypeUnion expected;
  };

  TestData test_data[] = {
      {Type::Signed32(), Type::Unsigned32(), kMachInt32,
       kRepWord32 | kTypeNumber},
2035 2036
      {Type::Signed32(), Type::Unsigned32(), kMachUint32,
       kRepWord32 | kTypeNumber},
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
      {Type::Signed32(), Type::Signed32(), kMachInt32, kMachInt32},
      {Type::Unsigned32(), Type::Unsigned32(), kMachInt32, kMachUint32},
      {Type::Number(), Type::Signed32(), kMachInt32, kMachFloat64},
      {Type::Signed32(), Type::String(), kMachInt32, kMachAnyTagged}};

  for (auto const d : test_data) {
    TestingGraph t(d.arg1, d.arg2, Type::Boolean());

    Node* br = t.graph()->NewNode(t.common()->Branch(), t.p2, t.start);
    Node* tb = t.graph()->NewNode(t.common()->IfTrue(), br);
    Node* fb = t.graph()->NewNode(t.common()->IfFalse(), br);
    Node* m = t.graph()->NewNode(t.common()->Merge(2), tb, fb);

    Node* phi =
        t.graph()->NewNode(t.common()->Phi(kMachAnyTagged, 2), t.p0, t.p1, m);

    Bounds phi_bounds = Bounds::Either(Bounds(d.arg1), Bounds(d.arg2), z);
    NodeProperties::SetBounds(phi, phi_bounds);

    Node* use = t.Use(phi, d.use);
    t.Return(use);
    t.Lower();

    CHECK_EQ(d.expected, OpParameter<MachineType>(phi));
  }
}