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

5
#include "src/compilation-dependencies.h"
6
#include "src/compiler/js-graph.h"
7
#include "src/compiler/js-typed-lowering.h"
8
#include "src/compiler/machine-operator.h"
9
#include "src/compiler/node-properties.h"
10
#include "src/compiler/opcodes.h"
11
#include "src/compiler/operator-properties.h"
12
#include "src/compiler/simplified-operator.h"
13
#include "src/compiler/typer.h"
14 15 16 17
#include "src/factory.h"
#include "src/isolate.h"
#include "src/objects-inl.h"
// FIXME(mstarzinger, marja): This is weird, but required because of the missing
18 19 20
// (disallowed) include: src/feedback-vector.h ->
// src/feedback-vector-inl.h
#include "src/feedback-vector-inl.h"
21
#include "test/cctest/cctest.h"
22

23 24 25
namespace v8 {
namespace internal {
namespace compiler {
26 27 28

class JSTypedLoweringTester : public HandleAndZoneScope {
 public:
29 30 31
  JSTypedLoweringTester(
      int num_parameters = 0,
      JSTypedLowering::Flags flags = JSTypedLowering::kDeoptimizationEnabled)
32 33 34 35
      : isolate(main_isolate()),
        binop(NULL),
        unop(NULL),
        javascript(main_zone()),
36
        machine(main_zone()),
37 38
        simplified(main_zone()),
        common(main_zone()),
39
        deps(main_isolate(), main_zone()),
40
        graph(main_zone()),
41
        typer(main_isolate(), Typer::kNoFlags, &graph),
42 43
        context_node(NULL),
        flags(flags) {
44
    graph.SetStart(graph.NewNode(common.Start(num_parameters)));
45
    graph.SetEnd(graph.NewNode(common.End(1), graph.start()));
46
    typer.Run();
47 48 49
  }

  Isolate* isolate;
50 51
  const Operator* binop;
  const Operator* unop;
52 53 54 55
  JSOperatorBuilder javascript;
  MachineOperatorBuilder machine;
  SimplifiedOperatorBuilder simplified;
  CommonOperatorBuilder common;
56
  CompilationDependencies deps;
57 58 59
  Graph graph;
  Typer typer;
  Node* context_node;
60
  JSTypedLowering::Flags flags;
61 62
  BinaryOperationHint const binop_hints = BinaryOperationHint::kAny;
  CompareOperationHint const compare_hints = CompareOperationHint::kAny;
63 64

  Node* Parameter(Type* t, int32_t index = 0) {
65
    Node* n = graph.NewNode(common.Parameter(index), graph.start());
66
    NodeProperties::SetType(n, t);
67 68 69
    return n;
  }

70
  Node* UndefinedConstant() {
71 72
    Handle<HeapObject> value = isolate->factory()->undefined_value();
    return graph.NewNode(common.HeapConstant(value));
73 74
  }

75
  Node* HeapConstant(Handle<HeapObject> constant) {
76
    return graph.NewNode(common.HeapConstant(constant));
77 78
  }

79
  Node* EmptyFrameState(Node* context) {
80 81 82 83 84 85
    Node* parameters =
        graph.NewNode(common.StateValues(0, SparseInputMask::Dense()));
    Node* locals =
        graph.NewNode(common.StateValues(0, SparseInputMask::Dense()));
    Node* stack =
        graph.NewNode(common.StateValues(0, SparseInputMask::Dense()));
86

87 88 89
    Node* state_node = graph.NewNode(
        common.FrameState(BailoutId::None(), OutputFrameStateCombine::Ignore(),
                          nullptr),
90
        parameters, locals, stack, context, UndefinedConstant(), graph.start());
91 92 93 94

    return state_node;
  }

95
  Node* reduce(Node* node) {
96 97
    JSGraph jsgraph(main_isolate(), &graph, &common, &javascript, &simplified,
                    &machine);
98
    // TODO(titzer): mock the GraphReducer here for better unit testing.
99
    GraphReducer graph_reducer(main_zone(), &graph);
100
    JSTypedLowering reducer(&graph_reducer, &deps, flags, &jsgraph,
101
                            main_zone());
102 103 104 105 106
    Reduction reduction = reducer.Reduce(node);
    if (reduction.Changed()) return reduction.replacement();
    return node;
  }

107
  Node* start() { return graph.start(); }
108 109 110

  Node* context() {
    if (context_node == NULL) {
111
      context_node = graph.NewNode(common.Parameter(-1), graph.start());
112 113 114 115 116 117
    }
    return context_node;
  }

  Node* control() { return start(); }

118
  void CheckBinop(IrOpcode::Value expected, Node* node) {
119 120 121
    CHECK_EQ(expected, node->opcode());
  }

122
  void CheckBinop(const Operator* expected, Node* node) {
123 124 125
    CHECK_EQ(expected->opcode(), node->op()->opcode());
  }

126
  Node* ReduceUnop(const Operator* op, Type* input_type) {
127 128 129
    return reduce(Unop(op, Parameter(input_type)));
  }

130
  Node* ReduceBinop(const Operator* op, Type* left_type, Type* right_type) {
131 132 133
    return reduce(Binop(op, Parameter(left_type, 0), Parameter(right_type, 1)));
  }

134
  Node* Binop(const Operator* op, Node* left, Node* right) {
135
    // JS binops also require context, effect, and control
136 137 138 139 140 141 142 143 144 145 146 147 148 149
    std::vector<Node*> inputs;
    inputs.push_back(left);
    inputs.push_back(right);
    if (OperatorProperties::HasContextInput(op)) {
      inputs.push_back(context());
    }
    for (int i = 0; i < OperatorProperties::GetFrameStateInputCount(op); i++) {
      inputs.push_back(EmptyFrameState(context()));
    }
    if (op->EffectInputCount() > 0) {
      inputs.push_back(start());
    }
    if (op->ControlInputCount() > 0) {
      inputs.push_back(control());
150
    }
151 152
    return graph.NewNode(op, static_cast<int>(inputs.size()),
                         &(inputs.front()));
153 154
  }

155
  Node* Unop(const Operator* op, Node* input) {
156
    // JS unops also require context, effect, and control
157
    if (OperatorProperties::GetFrameStateInputCount(op) > 0) {
158
      CHECK_EQ(1, OperatorProperties::GetFrameStateInputCount(op));
159 160 161 162 163
      return graph.NewNode(op, input, context(), EmptyFrameState(context()),
                           start(), control());
    } else {
      return graph.NewNode(op, input, context(), start(), control());
    }
164 165 166
  }

  Node* UseForEffect(Node* node) {
167 168
    Node* merge = graph.NewNode(common.Merge(1), start());
    return graph.NewNode(common.EffectPhi(1), node, merge);
169 170 171 172 173 174 175 176
  }

  void CheckEffectInput(Node* effect, Node* use) {
    CHECK_EQ(effect, NodeProperties::GetEffectInput(use));
  }

  void CheckNumberConstant(double expected, Node* result) {
    CHECK_EQ(IrOpcode::kNumberConstant, result->opcode());
177
    CHECK_EQ(expected, OpParameter<double>(result));
178 179 180 181
  }

  void CheckNaN(Node* result) {
    CHECK_EQ(IrOpcode::kNumberConstant, result->opcode());
182
    double value = OpParameter<double>(result);
183 184 185 186 187 188 189 190 191 192 193
    CHECK(std::isnan(value));
  }

  void CheckTrue(Node* result) {
    CheckHandle(isolate->factory()->true_value(), result);
  }

  void CheckFalse(Node* result) {
    CheckHandle(isolate->factory()->false_value(), result);
  }

194
  void CheckHandle(Handle<HeapObject> expected, Node* result) {
195
    CHECK_EQ(IrOpcode::kHeapConstant, result->opcode());
196
    Handle<HeapObject> value = OpParameter<Handle<HeapObject>>(result);
197 198 199 200 201 202 203 204
    CHECK_EQ(*expected, *value);
  }
};

static Type* kStringTypes[] = {Type::InternalizedString(), Type::OtherString(),
                               Type::String()};


205 206 207 208
static Type* kInt32Types[] = {Type::UnsignedSmall(), Type::Negative32(),
                              Type::Unsigned31(),    Type::SignedSmall(),
                              Type::Signed32(),      Type::Unsigned32(),
                              Type::Integral32()};
209 210 211


static Type* kNumberTypes[] = {
212 213 214 215
    Type::UnsignedSmall(), Type::Negative32(),  Type::Unsigned31(),
    Type::SignedSmall(),   Type::Signed32(),    Type::Unsigned32(),
    Type::Integral32(),    Type::MinusZero(),   Type::NaN(),
    Type::OrderedNumber(), Type::PlainNumber(), Type::Number()};
216 217 218 219 220 221 222 223 224 225 226 227


static Type* I32Type(bool is_signed) {
  return is_signed ? Type::Signed32() : Type::Unsigned32();
}


static IrOpcode::Value NumberToI32(bool is_signed) {
  return is_signed ? IrOpcode::kNumberToInt32 : IrOpcode::kNumberToUint32;
}


228 229
// TODO(turbofan): Lowering of StringAdd is disabled for now.
#if 0
230
TEST(StringBinops) {
231 232
  JSTypedLoweringTester R;

233
  for (size_t i = 0; i < arraysize(kStringTypes); ++i) {
234 235
    Node* p0 = R.Parameter(kStringTypes[i], 0);

236
    for (size_t j = 0; j < arraysize(kStringTypes); ++j) {
237 238
      Node* p1 = R.Parameter(kStringTypes[j], 1);

239
      Node* add = R.Binop(R.javascript.Add(), p0, p1);
240 241
      Node* r = R.reduce(add);

242
      R.CheckBinop(IrOpcode::kStringAdd, r);
243 244 245 246 247
      CHECK_EQ(p0, r->InputAt(0));
      CHECK_EQ(p1, r->InputAt(1));
    }
  }
}
248
#endif
249

250
TEST(AddNumber1) {
251
  JSTypedLoweringTester R;
252
  for (size_t i = 0; i < arraysize(kNumberTypes); ++i) {
253 254
    Node* p0 = R.Parameter(kNumberTypes[i], 0);
    Node* p1 = R.Parameter(kNumberTypes[i], 1);
255
    Node* add = R.Binop(R.javascript.Add(BinaryOperationHint::kAny), p0, p1);
256 257
    Node* r = R.reduce(add);

258
    R.CheckBinop(IrOpcode::kNumberAdd, r);
259 260 261 262 263
    CHECK_EQ(p0, r->InputAt(0));
    CHECK_EQ(p1, r->InputAt(1));
  }
}

264
TEST(NumberBinops) {
265
  JSTypedLoweringTester R;
266
  const Operator* ops[] = {
267 268 269 270 271
      R.javascript.Add(R.binop_hints), R.simplified.NumberAdd(),
      R.javascript.Subtract(),         R.simplified.NumberSubtract(),
      R.javascript.Multiply(),         R.simplified.NumberMultiply(),
      R.javascript.Divide(),           R.simplified.NumberDivide(),
      R.javascript.Modulus(),          R.simplified.NumberModulus(),
272 273
  };

274
  for (size_t i = 0; i < arraysize(kNumberTypes); ++i) {
275 276
    Node* p0 = R.Parameter(kNumberTypes[i], 0);

277
    for (size_t j = 0; j < arraysize(kNumberTypes); ++j) {
278 279
      Node* p1 = R.Parameter(kNumberTypes[j], 1);

280
      for (size_t k = 0; k < arraysize(ops); k += 2) {
281 282 283
        Node* add = R.Binop(ops[k], p0, p1);
        Node* r = R.reduce(add);

284
        R.CheckBinop(ops[k + 1], r);
285 286 287 288 289 290 291 292 293
        CHECK_EQ(p0, r->InputAt(0));
        CHECK_EQ(p1, r->InputAt(1));
      }
    }
  }
}


static void CheckToI32(Node* old_input, Node* new_input, bool is_signed) {
294 295
  Type* old_type = NodeProperties::GetType(old_input);
  Type* new_type = NodeProperties::GetType(new_input);
296
  Type* expected_type = I32Type(is_signed);
297
  CHECK(new_type->Is(expected_type));
298 299 300
  if (old_type->Is(expected_type)) {
    CHECK_EQ(old_input, new_input);
  } else if (new_input->opcode() == IrOpcode::kNumberConstant) {
301
    double v = OpParameter<double>(new_input);
302 303 304 305 306 307 308 309 310
    double e = static_cast<double>(is_signed ? FastD2I(v) : FastD2UI(v));
    CHECK_EQ(e, v);
  }
}


// A helper class for testing lowering of bitwise shift operators.
class JSBitwiseShiftTypedLoweringTester : public JSTypedLoweringTester {
 public:
311
  JSBitwiseShiftTypedLoweringTester() : JSTypedLoweringTester() {
312
    int i = 0;
313
    set(i++, javascript.ShiftLeft(), true);
314
    set(i++, simplified.NumberShiftLeft(), false);
315
    set(i++, javascript.ShiftRight(), true);
316
    set(i++, simplified.NumberShiftRight(), false);
317
    set(i++, javascript.ShiftRightLogical(), false);
318
    set(i++, simplified.NumberShiftRightLogical(), false);
319
  }
320 321 322
  static const int kNumberOps = 6;
  const Operator* ops[kNumberOps];
  bool signedness[kNumberOps];
323

324
 private:
325
  void set(int idx, const Operator* op, bool s) {
326 327
    ops[idx] = op;
    signedness[idx] = s;
328 329 330 331 332
  }
};


TEST(Int32BitwiseShifts) {
333
  JSBitwiseShiftTypedLoweringTester R;
334

335 336 337 338 339 340
  Type* types[] = {
      Type::SignedSmall(), Type::UnsignedSmall(), Type::Negative32(),
      Type::Unsigned31(),  Type::Unsigned32(),    Type::Signed32(),
      Type::MinusZero(),   Type::NaN(),           Type::Undefined(),
      Type::Null(),        Type::Boolean(),       Type::Number(),
      Type::PlainNumber(), Type::String()};
341

342
  for (size_t i = 0; i < arraysize(types); ++i) {
343 344
    Node* p0 = R.Parameter(types[i], 0);

345
    for (size_t j = 0; j < arraysize(types); ++j) {
346 347 348 349 350 351
      Node* p1 = R.Parameter(types[j], 1);

      for (int k = 0; k < R.kNumberOps; k += 2) {
        Node* add = R.Binop(R.ops[k], p0, p1);
        Node* r = R.reduce(add);

352
        R.CheckBinop(R.ops[k + 1], r);
353 354 355 356
        Node* r0 = r->InputAt(0);
        Node* r1 = r->InputAt(1);

        CheckToI32(p0, r0, R.signedness[k]);
357
        CheckToI32(p1, r1, false);
358 359 360 361 362 363 364 365 366
      }
    }
  }
}


// A helper class for testing lowering of bitwise operators.
class JSBitwiseTypedLoweringTester : public JSTypedLoweringTester {
 public:
367
  JSBitwiseTypedLoweringTester() : JSTypedLoweringTester() {
368
    int i = 0;
369
    set(i++, javascript.BitwiseOr(), true);
370
    set(i++, simplified.NumberBitwiseOr(), true);
371
    set(i++, javascript.BitwiseXor(), true);
372
    set(i++, simplified.NumberBitwiseXor(), true);
373
    set(i++, javascript.BitwiseAnd(), true);
374
    set(i++, simplified.NumberBitwiseAnd(), true);
375
  }
376 377 378
  static const int kNumberOps = 6;
  const Operator* ops[kNumberOps];
  bool signedness[kNumberOps];
379

380
 private:
381
  void set(int idx, const Operator* op, bool s) {
382 383
    ops[idx] = op;
    signedness[idx] = s;
384 385 386 387 388
  }
};


TEST(Int32BitwiseBinops) {
389
  JSBitwiseTypedLoweringTester R;
390 391

  Type* types[] = {
392 393 394 395 396
      Type::SignedSmall(),   Type::UnsignedSmall(), Type::Unsigned32(),
      Type::Signed32(),      Type::MinusZero(),     Type::NaN(),
      Type::OrderedNumber(), Type::PlainNumber(),   Type::Undefined(),
      Type::Null(),          Type::Boolean(),       Type::Number(),
      Type::String()};
397

398
  for (size_t i = 0; i < arraysize(types); ++i) {
399 400
    Node* p0 = R.Parameter(types[i], 0);

401
    for (size_t j = 0; j < arraysize(types); ++j) {
402 403 404 405 406 407
      Node* p1 = R.Parameter(types[j], 1);

      for (int k = 0; k < R.kNumberOps; k += 2) {
        Node* add = R.Binop(R.ops[k], p0, p1);
        Node* r = R.reduce(add);

408
        R.CheckBinop(R.ops[k + 1], r);
409 410 411 412 413 414 415 416 417 418 419

        CheckToI32(p0, r->InputAt(0), R.signedness[k]);
        CheckToI32(p1, r->InputAt(1), R.signedness[k + 1]);
      }
    }
  }
}


TEST(JSToNumber1) {
  JSTypedLoweringTester R;
420
  const Operator* ton = R.javascript.ToNumber();
421

422
  for (size_t i = 0; i < arraysize(kNumberTypes); i++) {  // ToNumber(number)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    Node* r = R.ReduceUnop(ton, kNumberTypes[i]);
    CHECK_EQ(IrOpcode::kParameter, r->opcode());
  }

  {  // ToNumber(undefined)
    Node* r = R.ReduceUnop(ton, Type::Undefined());
    R.CheckNaN(r);
  }

  {  // ToNumber(null)
    Node* r = R.ReduceUnop(ton, Type::Null());
    R.CheckNumberConstant(0.0, r);
  }
}


TEST(JSToNumber_replacement) {
  JSTypedLoweringTester R;

  Type* types[] = {Type::Null(), Type::Undefined(), Type::Number()};

444
  for (size_t i = 0; i < arraysize(types); i++) {
445
    Node* n = R.Parameter(types[i]);
446 447 448
    Node* c =
        R.graph.NewNode(R.javascript.ToNumber(), n, R.context(),
                        R.EmptyFrameState(R.context()), R.start(), R.start());
449
    Node* effect_use = R.UseForEffect(c);
450
    Node* add = R.graph.NewNode(R.simplified.ReferenceEqual(), n, c);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

    R.CheckEffectInput(c, effect_use);
    Node* r = R.reduce(c);

    if (types[i]->Is(Type::Number())) {
      CHECK_EQ(n, r);
    } else {
      CHECK_EQ(IrOpcode::kNumberConstant, r->opcode());
    }

    CHECK_EQ(n, add->InputAt(0));
    CHECK_EQ(r, add->InputAt(1));
    R.CheckEffectInput(R.start(), effect_use);
  }
}


TEST(JSToNumberOfConstant) {
  JSTypedLoweringTester R;

471 472 473
  const Operator* ops[] = {R.common.NumberConstant(0),
                           R.common.NumberConstant(-1),
                           R.common.NumberConstant(0.1)};
474

475
  for (size_t i = 0; i < arraysize(ops); i++) {
476 477 478 479 480 481
    Node* n = R.graph.NewNode(ops[i]);
    Node* convert = R.Unop(R.javascript.ToNumber(), n);
    Node* r = R.reduce(convert);
    // Note that either outcome below is correct. It only depends on whether
    // the types of constants are eagerly computed or only computed by the
    // typing pass.
482
    if (NodeProperties::GetType(n)->Is(Type::Number())) {
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
      // If number constants are eagerly typed, then reduction should
      // remove the ToNumber.
      CHECK_EQ(n, r);
    } else {
      // Otherwise, type-based lowering should only look at the type, and
      // *not* try to constant fold.
      CHECK_EQ(convert, r);
    }
  }
}


TEST(JSToNumberOfNumberOrOtherPrimitive) {
  JSTypedLoweringTester R;
  Type* others[] = {Type::Undefined(), Type::Null(), Type::Boolean(),
                    Type::String()};

500
  for (size_t i = 0; i < arraysize(others); i++) {
501 502
    Type* t = Type::Union(Type::Number(), others[i], R.main_zone());
    Node* r = R.ReduceUnop(R.javascript.ToNumber(), t);
503
    CHECK_EQ(IrOpcode::kPlainPrimitiveToNumber, r->opcode());
504 505 506 507 508 509 510
  }
}


TEST(JSToString1) {
  JSTypedLoweringTester R;

511
  for (size_t i = 0; i < arraysize(kStringTypes); i++) {
512 513 514 515
    Node* r = R.ReduceUnop(R.javascript.ToString(), kStringTypes[i]);
    CHECK_EQ(IrOpcode::kParameter, r->opcode());
  }

516
  const Operator* op = R.javascript.ToString();
517 518 519 520 521 522 523 524 525 526 527 528 529

  {  // ToString(undefined) => "undefined"
    Node* r = R.ReduceUnop(op, Type::Undefined());
    R.CheckHandle(R.isolate->factory()->undefined_string(), r);
  }

  {  // ToString(null) => "null"
    Node* r = R.ReduceUnop(op, Type::Null());
    R.CheckHandle(R.isolate->factory()->null_string(), r);
  }

  {  // ToString(boolean)
    Node* r = R.ReduceUnop(op, Type::Boolean());
530
    CHECK_EQ(IrOpcode::kSelect, r->opcode());
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  }

  {  // ToString(number)
    Node* r = R.ReduceUnop(op, Type::Number());
    CHECK_EQ(IrOpcode::kJSToString, r->opcode());
  }

  {  // ToString(string)
    Node* r = R.ReduceUnop(op, Type::String());
    CHECK_EQ(IrOpcode::kParameter, r->opcode());  // No-op
  }

  {  // ToString(object)
    Node* r = R.ReduceUnop(op, Type::Object());
    CHECK_EQ(IrOpcode::kJSToString, r->opcode());  // No reduction.
  }
}


TEST(JSToString_replacement) {
  JSTypedLoweringTester R;

  Type* types[] = {Type::Null(), Type::Undefined(), Type::String()};

555
  for (size_t i = 0; i < arraysize(types); i++) {
556
    Node* n = R.Parameter(types[i]);
557 558 559
    Node* c =
        R.graph.NewNode(R.javascript.ToString(), n, R.context(),
                        R.EmptyFrameState(R.context()), R.start(), R.start());
560
    Node* effect_use = R.UseForEffect(c);
561
    Node* add = R.graph.NewNode(R.simplified.ReferenceEqual(), n, c);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

    R.CheckEffectInput(c, effect_use);
    Node* r = R.reduce(c);

    if (types[i]->Is(Type::String())) {
      CHECK_EQ(n, r);
    } else {
      CHECK_EQ(IrOpcode::kHeapConstant, r->opcode());
    }

    CHECK_EQ(n, add->InputAt(0));
    CHECK_EQ(r, add->InputAt(1));
    R.CheckEffectInput(R.start(), effect_use);
  }
}

578
TEST(StringComparison) {
579 580
  JSTypedLoweringTester R;

581
  const Operator* ops[] = {
582
      R.javascript.LessThan(CompareOperationHint::kAny),
583
      R.simplified.StringLessThan(),
584
      R.javascript.LessThanOrEqual(CompareOperationHint::kAny),
585
      R.simplified.StringLessThanOrEqual(),
586
      R.javascript.GreaterThan(CompareOperationHint::kAny),
587
      R.simplified.StringLessThan(),
588
      R.javascript.GreaterThanOrEqual(CompareOperationHint::kAny),
589
      R.simplified.StringLessThanOrEqual()};
590

591
  for (size_t i = 0; i < arraysize(kStringTypes); i++) {
592
    Node* p0 = R.Parameter(kStringTypes[i], 0);
593
    for (size_t j = 0; j < arraysize(kStringTypes); j++) {
594 595
      Node* p1 = R.Parameter(kStringTypes[j], 1);

596
      for (size_t k = 0; k < arraysize(ops); k += 2) {
597 598 599
        Node* cmp = R.Binop(ops[k], p0, p1);
        Node* r = R.reduce(cmp);

600
        R.CheckBinop(ops[k + 1], r);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        if (k >= 4) {
          // GreaterThan and GreaterThanOrEqual commute the inputs
          // and use the LessThan and LessThanOrEqual operators.
          CHECK_EQ(p1, r->InputAt(0));
          CHECK_EQ(p0, r->InputAt(1));
        } else {
          CHECK_EQ(p0, r->InputAt(0));
          CHECK_EQ(p1, r->InputAt(1));
        }
      }
    }
  }
}


static void CheckIsConvertedToNumber(Node* val, Node* converted) {
617
  if (NodeProperties::GetType(val)->Is(Type::Number())) {
618 619 620 621 622 623 624 625
    CHECK_EQ(val, converted);
  } else {
    if (converted->opcode() == IrOpcode::kNumberConstant) return;
    CHECK_EQ(IrOpcode::kJSToNumber, converted->opcode());
    CHECK_EQ(val, converted->InputAt(0));
  }
}

626
TEST(NumberComparison) {
627 628
  JSTypedLoweringTester R;

629
  const Operator* ops[] = {
630
      R.javascript.LessThan(CompareOperationHint::kAny),
631
      R.simplified.NumberLessThan(),
632
      R.javascript.LessThanOrEqual(CompareOperationHint::kAny),
633
      R.simplified.NumberLessThanOrEqual(),
634
      R.javascript.GreaterThan(CompareOperationHint::kAny),
635
      R.simplified.NumberLessThan(),
636
      R.javascript.GreaterThanOrEqual(CompareOperationHint::kAny),
637
      R.simplified.NumberLessThanOrEqual()};
638

639 640
  Node* const p0 = R.Parameter(Type::Number(), 0);
  Node* const p1 = R.Parameter(Type::Number(), 1);
641

642 643 644
  for (size_t k = 0; k < arraysize(ops); k += 2) {
    Node* cmp = R.Binop(ops[k], p0, p1);
    Node* r = R.reduce(cmp);
645

646
    R.CheckBinop(ops[k + 1], r);
647 648 649 650 651 652 653 654
    if (k >= 4) {
      // GreaterThan and GreaterThanOrEqual commute the inputs
      // and use the LessThan and LessThanOrEqual operators.
      CheckIsConvertedToNumber(p1, r->InputAt(0));
      CheckIsConvertedToNumber(p0, r->InputAt(1));
    } else {
      CheckIsConvertedToNumber(p0, r->InputAt(0));
      CheckIsConvertedToNumber(p1, r->InputAt(1));
655 656 657 658
    }
  }
}

659
TEST(MixedComparison1) {
660 661 662 663 664
  JSTypedLoweringTester R;

  Type* types[] = {Type::Number(), Type::String(),
                   Type::Union(Type::Number(), Type::String(), R.main_zone())};

665
  for (size_t i = 0; i < arraysize(types); i++) {
666 667
    Node* p0 = R.Parameter(types[i], 0);

668
    for (size_t j = 0; j < arraysize(types); j++) {
669 670
      Node* p1 = R.Parameter(types[j], 1);
      {
671
        const Operator* less_than =
672
            R.javascript.LessThan(CompareOperationHint::kAny);
673
        Node* cmp = R.Binop(less_than, p0, p1);
674
        Node* r = R.reduce(cmp);
675
        if (types[i]->Is(Type::String()) && types[j]->Is(Type::String())) {
676
          R.CheckBinop(R.simplified.StringLessThan(), r);
677 678
        } else if ((types[i]->Is(Type::Number()) &&
                    types[j]->Is(Type::Number())) ||
679 680
                   (!types[i]->Maybe(Type::String()) ||
                    !types[j]->Maybe(Type::String()))) {
681
          R.CheckBinop(R.simplified.NumberLessThan(), r);
682
        } else {
683 684
          // No reduction of mixed types.
          CHECK_EQ(r->op(), less_than);
685 686 687 688 689 690
        }
      }
    }
  }
}

691
TEST(RemoveToNumberEffects) {
692 693 694
  JSTypedLoweringTester R;

  Node* effect_use = NULL;
695
  Node* zero = R.graph.NewNode(R.common.NumberConstant(0));
696 697 698
  for (int i = 0; i < 10; i++) {
    Node* p0 = R.Parameter(Type::Number());
    Node* ton = R.Unop(R.javascript.ToNumber(), p0);
699
    Node* frame_state = R.EmptyFrameState(R.context());
700 701 702 703
    effect_use = NULL;

    switch (i) {
      case 0:
704 705
        CHECK_EQ(1, OperatorProperties::GetFrameStateInputCount(
                        R.javascript.ToNumber()));
706
        effect_use = R.graph.NewNode(R.javascript.ToNumber(), p0, R.context(),
707
                                     frame_state, ton, R.start());
708 709
        break;
      case 1:
710 711
        CHECK_EQ(1, OperatorProperties::GetFrameStateInputCount(
                        R.javascript.ToNumber()));
712 713
        effect_use = R.graph.NewNode(R.javascript.ToNumber(), ton, R.context(),
                                     frame_state, ton, R.start());
714 715 716 717
        break;
      case 2:
        effect_use = R.graph.NewNode(R.common.EffectPhi(1), ton, R.start());
      case 3:
718
        effect_use = R.graph.NewNode(R.javascript.Add(R.binop_hints), ton, ton,
719
                                     R.context(), frame_state, ton, R.start());
720 721
        break;
      case 4:
722
        effect_use = R.graph.NewNode(R.javascript.Add(R.binop_hints), p0, p0,
723
                                     R.context(), frame_state, ton, R.start());
724 725
        break;
      case 5:
726 727
        effect_use =
            R.graph.NewNode(R.common.Return(), zero, p0, ton, R.start());
728 729
        break;
      case 6:
730 731
        effect_use =
            R.graph.NewNode(R.common.Return(), zero, ton, ton, R.start());
732 733 734 735 736 737 738 739 740 741 742 743
    }

    R.CheckEffectInput(R.start(), ton);
    if (effect_use != NULL) R.CheckEffectInput(ton, effect_use);

    Node* r = R.reduce(ton);
    CHECK_EQ(p0, r);
    CHECK_NE(R.start(), r);

    if (effect_use != NULL) {
      R.CheckEffectInput(R.start(), effect_use);
      // Check that value uses of ToNumber() do not go to start().
744
      for (int i = 0; i < effect_use->op()->ValueInputCount(); i++) {
745 746 747 748 749
        CHECK_NE(R.start(), effect_use->InputAt(i));
      }
    }
  }

750
  CHECK(!effect_use);  // should have done all cases above.
751 752 753 754 755 756
}


// Helper class for testing the reduction of a single binop.
class BinopEffectsTester {
 public:
757 758 759 760
  BinopEffectsTester(
      const Operator* op, Type* t0, Type* t1,
      JSTypedLowering::Flags flags = JSTypedLowering::kDeoptimizationEnabled)
      : R(0, flags),
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
        p0(R.Parameter(t0, 0)),
        p1(R.Parameter(t1, 1)),
        binop(R.Binop(op, p0, p1)),
        effect_use(R.graph.NewNode(R.common.EffectPhi(1), binop, R.start())) {
    // Effects should be ordered start -> binop -> effect_use
    R.CheckEffectInput(R.start(), binop);
    R.CheckEffectInput(binop, effect_use);
    result = R.reduce(binop);
  }

  JSTypedLoweringTester R;
  Node* p0;
  Node* p1;
  Node* binop;
  Node* effect_use;
  Node* result;

  void CheckEffectsRemoved() { R.CheckEffectInput(R.start(), effect_use); }

  void CheckEffectOrdering(Node* n0) {
    R.CheckEffectInput(R.start(), n0);
    R.CheckEffectInput(n0, effect_use);
  }

  void CheckEffectOrdering(Node* n0, Node* n1) {
    R.CheckEffectInput(R.start(), n0);
    R.CheckEffectInput(n0, n1);
    R.CheckEffectInput(n1, effect_use);
  }

  Node* CheckConvertedInput(IrOpcode::Value opcode, int which, bool effects) {
    return CheckConverted(opcode, result->InputAt(which), effects);
  }

  Node* CheckConverted(IrOpcode::Value opcode, Node* node, bool effects) {
    CHECK_EQ(opcode, node->opcode());
    if (effects) {
798
      CHECK_LT(0, node->op()->EffectInputCount());
799
    } else {
800
      CHECK_EQ(0, node->op()->EffectInputCount());
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    }
    return node;
  }

  Node* CheckNoOp(int which) {
    CHECK_EQ(which == 0 ? p0 : p1, result->InputAt(which));
    return result->InputAt(which);
  }
};


// Helper function for strict and non-strict equality reductions.
void CheckEqualityReduction(JSTypedLoweringTester* R, bool strict, Node* l,
                            Node* r, IrOpcode::Value expected) {
  for (int j = 0; j < 2; j++) {
    Node* p0 = j == 0 ? l : r;
    Node* p1 = j == 1 ? l : r;

    {
820
      const Operator* op =
821 822
          strict ? R->javascript.StrictEqual(CompareOperationHint::kAny)
                 : R->javascript.Equal(CompareOperationHint::kAny);
823
      Node* eq = R->Binop(op, p0, p1);
824
      Node* r = R->reduce(eq);
825
      R->CheckBinop(expected, r);
826 827 828
    }

    {
829
      const Operator* op =
830 831
          strict ? R->javascript.StrictNotEqual(CompareOperationHint::kAny)
                 : R->javascript.NotEqual(CompareOperationHint::kAny);
832
      Node* ne = R->Binop(op, p0, p1);
833 834 835
      Node* n = R->reduce(ne);
      CHECK_EQ(IrOpcode::kBooleanNot, n->opcode());
      Node* r = n->InputAt(0);
836
      R->CheckBinop(expected, r);
837 838 839 840 841 842 843 844 845 846 847 848 849
    }
  }
}


TEST(EqualityForNumbers) {
  JSTypedLoweringTester R;

  Type* simple_number_types[] = {Type::UnsignedSmall(), Type::SignedSmall(),
                                 Type::Signed32(), Type::Unsigned32(),
                                 Type::Number()};


850
  for (size_t i = 0; i < arraysize(simple_number_types); ++i) {
851 852
    Node* p0 = R.Parameter(simple_number_types[i], 0);

853
    for (size_t j = 0; j < arraysize(simple_number_types); ++j) {
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
      Node* p1 = R.Parameter(simple_number_types[j], 1);

      CheckEqualityReduction(&R, true, p0, p1, IrOpcode::kNumberEqual);
      CheckEqualityReduction(&R, false, p0, p1, IrOpcode::kNumberEqual);
    }
  }
}


TEST(StrictEqualityForRefEqualTypes) {
  JSTypedLoweringTester R;

  Type* types[] = {Type::Undefined(), Type::Null(), Type::Boolean(),
                   Type::Object(), Type::Receiver()};

  Node* p0 = R.Parameter(Type::Any());
870
  for (size_t i = 0; i < arraysize(types); i++) {
871 872 873 874 875
    Node* p1 = R.Parameter(types[i]);
    CheckEqualityReduction(&R, true, p0, p1, IrOpcode::kReferenceEqual);
  }
}

876 877 878 879 880 881 882 883
TEST(StrictEqualityForUnique) {
  JSTypedLoweringTester R;

  Node* p0 = R.Parameter(Type::Unique());
  Node* p1 = R.Parameter(Type::Unique());
  CheckEqualityReduction(&R, true, p0, p1, IrOpcode::kReferenceEqual);
  CheckEqualityReduction(&R, true, p1, p0, IrOpcode::kReferenceEqual);
}
884 885 886 887 888 889 890 891 892 893

TEST(StringEquality) {
  JSTypedLoweringTester R;
  Node* p0 = R.Parameter(Type::String());
  Node* p1 = R.Parameter(Type::String());

  CheckEqualityReduction(&R, true, p0, p1, IrOpcode::kStringEqual);
  CheckEqualityReduction(&R, false, p0, p1, IrOpcode::kStringEqual);
}

894
TEST(RemovePureNumberBinopEffects) {
895 896
  JSTypedLoweringTester R;

897
  const Operator* ops[] = {
898 899 900 901
      R.javascript.Equal(R.compare_hints),
      R.simplified.NumberEqual(),
      R.javascript.Add(R.binop_hints),
      R.simplified.NumberAdd(),
902
      R.javascript.Subtract(),
903
      R.simplified.NumberSubtract(),
904
      R.javascript.Multiply(),
905
      R.simplified.NumberMultiply(),
906
      R.javascript.Divide(),
907
      R.simplified.NumberDivide(),
908
      R.javascript.Modulus(),
909 910 911 912 913
      R.simplified.NumberModulus(),
      R.javascript.LessThan(R.compare_hints),
      R.simplified.NumberLessThan(),
      R.javascript.LessThanOrEqual(R.compare_hints),
      R.simplified.NumberLessThanOrEqual(),
914 915
  };

916
  for (size_t j = 0; j < arraysize(ops); j += 2) {
917 918 919
    BinopEffectsTester B(ops[j], Type::Number(), Type::Number());
    CHECK_EQ(ops[j + 1]->opcode(), B.result->op()->opcode());

920
    B.R.CheckBinop(B.result->opcode(), B.result);
921 922 923 924 925 926 927 928 929 930 931 932

    B.CheckNoOp(0);
    B.CheckNoOp(1);

    B.CheckEffectsRemoved();
  }
}


TEST(OrderNumberBinopEffects1) {
  JSTypedLoweringTester R;

933
  const Operator* ops[] = {
934 935
      R.javascript.Subtract(), R.simplified.NumberSubtract(),
      R.javascript.Multiply(), R.simplified.NumberMultiply(),
936 937
  };

938
  for (size_t j = 0; j < arraysize(ops); j += 2) {
939 940
    BinopEffectsTester B(ops[j], Type::Symbol(), Type::Symbol(),
                         JSTypedLowering::kNoFlags);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    CHECK_EQ(ops[j + 1]->opcode(), B.result->op()->opcode());

    Node* i0 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 0, true);
    Node* i1 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 1, true);

    CHECK_EQ(B.p0, i0->InputAt(0));
    CHECK_EQ(B.p1, i1->InputAt(0));

    // Effects should be ordered start -> i0 -> i1 -> effect_use
    B.CheckEffectOrdering(i0, i1);
  }
}


TEST(OrderNumberBinopEffects2) {
  JSTypedLoweringTester R;

958
  const Operator* ops[] = {
959 960 961
      R.javascript.Add(R.binop_hints), R.simplified.NumberAdd(),
      R.javascript.Subtract(),         R.simplified.NumberSubtract(),
      R.javascript.Multiply(),         R.simplified.NumberMultiply(),
962 963
  };

964
  for (size_t j = 0; j < arraysize(ops); j += 2) {
965 966
    BinopEffectsTester B(ops[j], Type::Number(), Type::Symbol(),
                         JSTypedLowering::kNoFlags);
967 968 969 970 971 972 973 974 975 976 977

    Node* i0 = B.CheckNoOp(0);
    Node* i1 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 1, true);

    CHECK_EQ(B.p0, i0);
    CHECK_EQ(B.p1, i1->InputAt(0));

    // Effects should be ordered start -> i1 -> effect_use
    B.CheckEffectOrdering(i1);
  }

978
  for (size_t j = 0; j < arraysize(ops); j += 2) {
979 980
    BinopEffectsTester B(ops[j], Type::Symbol(), Type::Number(),
                         JSTypedLowering::kNoFlags);
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996

    Node* i0 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 0, true);
    Node* i1 = B.CheckNoOp(1);

    CHECK_EQ(B.p0, i0->InputAt(0));
    CHECK_EQ(B.p1, i1);

    // Effects should be ordered start -> i0 -> effect_use
    B.CheckEffectOrdering(i0);
  }
}


TEST(OrderCompareEffects) {
  JSTypedLoweringTester R;

997
  const Operator* ops[] = {
998 999 1000
      R.javascript.GreaterThan(R.compare_hints), R.simplified.NumberLessThan(),
      R.javascript.GreaterThanOrEqual(R.compare_hints),
      R.simplified.NumberLessThanOrEqual(),
1001 1002
  };

1003
  for (size_t j = 0; j < arraysize(ops); j += 2) {
1004 1005
    BinopEffectsTester B(ops[j], Type::Symbol(), Type::String(),
                         JSTypedLowering::kNoFlags);
1006 1007
    CHECK_EQ(ops[j + 1]->opcode(), B.result->op()->opcode());

1008 1009
    Node* i0 =
        B.CheckConvertedInput(IrOpcode::kPlainPrimitiveToNumber, 0, false);
1010 1011 1012 1013 1014 1015
    Node* i1 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 1, true);

    // Inputs should be commuted.
    CHECK_EQ(B.p1, i0->InputAt(0));
    CHECK_EQ(B.p0, i1->InputAt(0));

1016 1017
    // But effects should be ordered start -> i1 -> effect_use
    B.CheckEffectOrdering(i1);
1018 1019
  }

1020
  for (size_t j = 0; j < arraysize(ops); j += 2) {
1021 1022
    BinopEffectsTester B(ops[j], Type::Number(), Type::Symbol(),
                         JSTypedLowering::kNoFlags);
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

    Node* i0 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 0, true);
    Node* i1 = B.result->InputAt(1);

    CHECK_EQ(B.p1, i0->InputAt(0));  // Should be commuted.
    CHECK_EQ(B.p0, i1);

    // Effects should be ordered start -> i1 -> effect_use
    B.CheckEffectOrdering(i0);
  }

1034
  for (size_t j = 0; j < arraysize(ops); j += 2) {
1035 1036
    BinopEffectsTester B(ops[j], Type::Symbol(), Type::Number(),
                         JSTypedLowering::kNoFlags);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

    Node* i0 = B.result->InputAt(0);
    Node* i1 = B.CheckConvertedInput(IrOpcode::kJSToNumber, 1, true);

    CHECK_EQ(B.p1, i0);  // Should be commuted.
    CHECK_EQ(B.p0, i1->InputAt(0));

    // Effects should be ordered start -> i0 -> effect_use
    B.CheckEffectOrdering(i1);
  }
}


TEST(Int32BinopEffects) {
1051
  JSBitwiseTypedLoweringTester R;
1052 1053
  for (int j = 0; j < R.kNumberOps; j += 2) {
    bool signed_left = R.signedness[j], signed_right = R.signedness[j + 1];
1054 1055
    BinopEffectsTester B(R.ops[j], I32Type(signed_left), I32Type(signed_right),
                         JSTypedLowering::kNoFlags);
1056 1057
    CHECK_EQ(R.ops[j + 1]->opcode(), B.result->op()->opcode());

1058
    B.R.CheckBinop(B.result->opcode(), B.result);
1059 1060 1061 1062 1063 1064 1065 1066 1067

    B.CheckNoOp(0);
    B.CheckNoOp(1);

    B.CheckEffectsRemoved();
  }

  for (int j = 0; j < R.kNumberOps; j += 2) {
    bool signed_left = R.signedness[j], signed_right = R.signedness[j + 1];
1068 1069
    BinopEffectsTester B(R.ops[j], Type::Number(), Type::Number(),
                         JSTypedLowering::kNoFlags);
1070 1071
    CHECK_EQ(R.ops[j + 1]->opcode(), B.result->op()->opcode());

1072
    B.R.CheckBinop(B.result->opcode(), B.result);
1073 1074 1075 1076 1077 1078 1079 1080 1081

    B.CheckConvertedInput(NumberToI32(signed_left), 0, false);
    B.CheckConvertedInput(NumberToI32(signed_right), 1, false);

    B.CheckEffectsRemoved();
  }

  for (int j = 0; j < R.kNumberOps; j += 2) {
    bool signed_left = R.signedness[j], signed_right = R.signedness[j + 1];
1082 1083
    BinopEffectsTester B(R.ops[j], Type::Number(), Type::Primitive(),
                         JSTypedLowering::kNoFlags);
1084

1085
    B.R.CheckBinop(B.result->opcode(), B.result);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

    Node* i0 = B.CheckConvertedInput(NumberToI32(signed_left), 0, false);
    Node* i1 = B.CheckConvertedInput(NumberToI32(signed_right), 1, false);

    CHECK_EQ(B.p0, i0->InputAt(0));
    Node* ii1 = B.CheckConverted(IrOpcode::kJSToNumber, i1->InputAt(0), true);

    CHECK_EQ(B.p1, ii1->InputAt(0));

    B.CheckEffectOrdering(ii1);
  }

  for (int j = 0; j < R.kNumberOps; j += 2) {
    bool signed_left = R.signedness[j], signed_right = R.signedness[j + 1];
1100 1101
    BinopEffectsTester B(R.ops[j], Type::Primitive(), Type::Number(),
                         JSTypedLowering::kNoFlags);
1102

1103
    B.R.CheckBinop(B.result->opcode(), B.result);
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

    Node* i0 = B.CheckConvertedInput(NumberToI32(signed_left), 0, false);
    Node* i1 = B.CheckConvertedInput(NumberToI32(signed_right), 1, false);

    Node* ii0 = B.CheckConverted(IrOpcode::kJSToNumber, i0->InputAt(0), true);
    CHECK_EQ(B.p1, i1->InputAt(0));

    CHECK_EQ(B.p0, ii0->InputAt(0));

    B.CheckEffectOrdering(ii0);
  }

  for (int j = 0; j < R.kNumberOps; j += 2) {
    bool signed_left = R.signedness[j], signed_right = R.signedness[j + 1];
1118 1119
    BinopEffectsTester B(R.ops[j], Type::Primitive(), Type::Primitive(),
                         JSTypedLowering::kNoFlags);
1120

1121
    B.R.CheckBinop(B.result->opcode(), B.result);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

    Node* i0 = B.CheckConvertedInput(NumberToI32(signed_left), 0, false);
    Node* i1 = B.CheckConvertedInput(NumberToI32(signed_right), 1, false);

    Node* ii0 = B.CheckConverted(IrOpcode::kJSToNumber, i0->InputAt(0), true);
    Node* ii1 = B.CheckConverted(IrOpcode::kJSToNumber, i1->InputAt(0), true);

    CHECK_EQ(B.p0, ii0->InputAt(0));
    CHECK_EQ(B.p1, ii1->InputAt(0));

    B.CheckEffectOrdering(ii0, ii1);
  }
}

1136
TEST(Int32AddNarrowing) {
1137
  {
1138
    JSBitwiseTypedLoweringTester R;
1139 1140

    for (int o = 0; o < R.kNumberOps; o += 2) {
1141
      for (size_t i = 0; i < arraysize(kInt32Types); i++) {
1142
        Node* n0 = R.Parameter(kInt32Types[i]);
1143
        for (size_t j = 0; j < arraysize(kInt32Types); j++) {
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
          Node* n1 = R.Parameter(kInt32Types[j]);
          Node* one = R.graph.NewNode(R.common.NumberConstant(1));

          for (int l = 0; l < 2; l++) {
            Node* add_node = R.Binop(R.simplified.NumberAdd(), n0, n1);
            Node* or_node =
                R.Binop(R.ops[o], l ? add_node : one, l ? one : add_node);
            Node* r = R.reduce(or_node);

            CHECK_EQ(R.ops[o + 1]->opcode(), r->op()->opcode());
1154
            CHECK_EQ(IrOpcode::kNumberAdd, add_node->opcode());
1155 1156 1157 1158 1159 1160
          }
        }
      }
    }
  }
  {
1161
    JSBitwiseShiftTypedLoweringTester R;
1162 1163

    for (int o = 0; o < R.kNumberOps; o += 2) {
1164
      for (size_t i = 0; i < arraysize(kInt32Types); i++) {
1165
        Node* n0 = R.Parameter(kInt32Types[i]);
1166
        for (size_t j = 0; j < arraysize(kInt32Types); j++) {
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
          Node* n1 = R.Parameter(kInt32Types[j]);
          Node* one = R.graph.NewNode(R.common.NumberConstant(1));

          for (int l = 0; l < 2; l++) {
            Node* add_node = R.Binop(R.simplified.NumberAdd(), n0, n1);
            Node* or_node =
                R.Binop(R.ops[o], l ? add_node : one, l ? one : add_node);
            Node* r = R.reduce(or_node);

            CHECK_EQ(R.ops[o + 1]->opcode(), r->op()->opcode());
1177
            CHECK_EQ(IrOpcode::kNumberAdd, add_node->opcode());
1178 1179 1180 1181 1182
          }
        }
      }
    }
  }
1183
  {
1184
    JSBitwiseTypedLoweringTester R;
1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    for (int o = 0; o < R.kNumberOps; o += 2) {
      Node* n0 = R.Parameter(I32Type(R.signedness[o]));
      Node* n1 = R.Parameter(I32Type(R.signedness[o + 1]));
      Node* one = R.graph.NewNode(R.common.NumberConstant(1));

      Node* add_node = R.Binop(R.simplified.NumberAdd(), n0, n1);
      Node* or_node = R.Binop(R.ops[o], add_node, one);
      Node* other_use = R.Binop(R.simplified.NumberAdd(), add_node, one);
      Node* r = R.reduce(or_node);
      CHECK_EQ(R.ops[o + 1]->opcode(), r->op()->opcode());
      CHECK_EQ(IrOpcode::kNumberAdd, add_node->opcode());
      // Conversion to int32 should be done.
      CheckToI32(add_node, r->InputAt(0), R.signedness[o]);
      CheckToI32(one, r->InputAt(1), R.signedness[o + 1]);
      // The other use should also not be touched.
      CHECK_EQ(add_node, other_use->InputAt(0));
      CHECK_EQ(one, other_use->InputAt(1));
    }
1204 1205 1206
  }
}

1207
TEST(Int32Comparisons) {
1208 1209 1210
  JSTypedLoweringTester R;

  struct Entry {
1211 1212
    const Operator* js_op;
    const Operator* num_op;
1213 1214 1215
    bool commute;
  };

1216 1217 1218 1219 1220 1221 1222 1223
  Entry ops[] = {{R.javascript.LessThan(R.compare_hints),
                  R.simplified.NumberLessThan(), false},
                 {R.javascript.LessThanOrEqual(R.compare_hints),
                  R.simplified.NumberLessThanOrEqual(), false},
                 {R.javascript.GreaterThan(R.compare_hints),
                  R.simplified.NumberLessThan(), true},
                 {R.javascript.GreaterThanOrEqual(R.compare_hints),
                  R.simplified.NumberLessThanOrEqual(), true}};
1224

1225 1226
  for (size_t o = 0; o < arraysize(ops); o++) {
    for (size_t i = 0; i < arraysize(kNumberTypes); i++) {
1227 1228 1229
      Type* t0 = kNumberTypes[i];
      Node* p0 = R.Parameter(t0, 0);

1230
      for (size_t j = 0; j < arraysize(kNumberTypes); j++) {
1231 1232 1233 1234 1235 1236
        Type* t1 = kNumberTypes[j];
        Node* p1 = R.Parameter(t1, 1);

        Node* cmp = R.Binop(ops[o].js_op, p0, p1);
        Node* r = R.reduce(cmp);

1237
        R.CheckBinop(ops[o].num_op, r);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
        if (ops[o].commute) {
          CHECK_EQ(p1, r->InputAt(0));
          CHECK_EQ(p0, r->InputAt(1));
        } else {
          CHECK_EQ(p0, r->InputAt(0));
          CHECK_EQ(p1, r->InputAt(1));
        }
      }
    }
  }
}
1249 1250 1251 1252

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