test-representation-change.cc 31 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 8
#include "src/compiler/access-info.h"
#include "src/compiler/js-heap-broker.h"
9 10
#include "src/compiler/node-matchers.h"
#include "src/compiler/representation-change.h"
11
#include "src/compiler/type-cache.h"
12
#include "src/objects/objects-inl.h"
13
#include "test/cctest/cctest.h"
14
#include "test/cctest/compiler/codegen-tester.h"
15
#include "test/cctest/compiler/graph-and-builders.h"
16
#include "test/cctest/compiler/value-helper.h"
17

18
namespace v8 {
19 20 21 22 23 24
namespace internal {
namespace compiler {

class RepresentationChangerTester : public HandleAndZoneScope,
                                    public GraphAndBuilders {
 public:
25
  explicit RepresentationChangerTester(int num_parameters = 0)
26 27
      : HandleAndZoneScope(kCompressGraphZone),
        GraphAndBuilders(main_zone()),
sigurds@chromium.org's avatar
sigurds@chromium.org committed
28
        javascript_(main_zone()),
29
        jsgraph_(main_isolate(), main_graph_, &main_common_, &javascript_,
30
                 &main_simplified_, &main_machine_),
31
        broker_(main_isolate(), main_zone()),
32
        changer_(&jsgraph_, &broker_) {
33 34
    Node* s = graph()->NewNode(common()->Start(num_parameters));
    graph()->SetStart(s);
35 36
  }

sigurds@chromium.org's avatar
sigurds@chromium.org committed
37
  JSOperatorBuilder javascript_;
38
  JSGraph jsgraph_;
39
  JSHeapBroker broker_;
40 41 42 43 44 45 46 47 48 49
  RepresentationChanger changer_;

  Isolate* isolate() { return main_isolate(); }
  Graph* graph() { return main_graph_; }
  CommonOperatorBuilder* common() { return &main_common_; }
  JSGraph* jsgraph() { return &jsgraph_; }
  RepresentationChanger* changer() { return &changer_; }

  // TODO(titzer): use ValueChecker / ValueUtil
  void CheckInt32Constant(Node* n, int32_t expected) {
50
    Int32Matcher m(n);
51 52
    CHECK(m.HasResolvedValue());
    CHECK_EQ(expected, m.ResolvedValue());
53 54
  }

55 56
  void CheckInt64Constant(Node* n, int64_t expected) {
    Int64Matcher m(n);
57 58
    CHECK(m.HasResolvedValue());
    CHECK_EQ(expected, m.ResolvedValue());
59 60
  }

61 62
  void CheckUint32Constant(Node* n, uint32_t expected) {
    Uint32Matcher m(n);
63 64
    CHECK(m.HasResolvedValue());
    CHECK_EQ(static_cast<int>(expected), static_cast<int>(m.ResolvedValue()));
65 66 67 68
  }

  void CheckFloat64Constant(Node* n, double expected) {
    Float64Matcher m(n);
69 70
    CHECK(m.HasResolvedValue());
    CHECK_DOUBLE_EQ(expected, m.ResolvedValue());
71 72 73 74 75
  }

  void CheckFloat32Constant(Node* n, float expected) {
    CHECK_EQ(IrOpcode::kFloat32Constant, n->opcode());
    float fval = OpParameter<float>(n->op());
76
    CHECK_FLOAT_EQ(expected, fval);
77 78
  }

79
  void CheckHeapConstant(Node* n, HeapObject expected) {
80
    HeapObjectMatcher m(n);
81 82
    CHECK(m.HasResolvedValue());
    CHECK_EQ(expected, *m.ResolvedValue());
83 84 85
  }

  void CheckNumberConstant(Node* n, double expected) {
86
    NumberMatcher m(n);
87
    CHECK_EQ(IrOpcode::kNumberConstant, n->opcode());
88 89
    CHECK(m.HasResolvedValue());
    CHECK_DOUBLE_EQ(expected, m.ResolvedValue());
90 91
  }

92
  Node* Parameter(int index = 0) {
93 94 95
    Node* n = graph()->NewNode(common()->Parameter(index), graph()->start());
    NodeProperties::SetType(n, Type::Any());
    return n;
96 97
  }

98
  Node* Return(Node* input) {
99 100
    Node* n = graph()->NewNode(common()->Return(), jsgraph()->Int32Constant(0),
                               input, graph()->start(), graph()->start());
101 102 103
    return n;
  }

104
  void CheckTypeError(MachineRepresentation from, Type from_type,
105
                      MachineRepresentation to) {
106 107 108
    changer()->testing_type_errors_ = true;
    changer()->type_error_ = false;
    Node* n = Parameter(0);
109 110 111
    Node* use = Return(n);
    Node* c = changer()->GetRepresentationFor(n, from, from_type, use,
                                              UseInfo(to, Truncation::None()));
112
    CHECK(changer()->type_error_);
113
    CHECK_EQ(n, c);
114 115
  }

116
  void CheckNop(MachineRepresentation from, Type from_type,
117
                MachineRepresentation to) {
118
    Node* n = Parameter(0);
119 120 121
    Node* use = Return(n);
    Node* c = changer()->GetRepresentationFor(n, from, from_type, use,
                                              UseInfo(to, Truncation::None()));
122 123 124 125
    CHECK_EQ(n, c);
  }
};

126
const MachineType kMachineTypes[] = {
127 128 129 130
    MachineType::Float32(), MachineType::Float64(),  MachineType::Int8(),
    MachineType::Uint8(),   MachineType::Int16(),    MachineType::Uint16(),
    MachineType::Int32(),   MachineType::Uint32(),   MachineType::Int64(),
    MachineType::Uint64(),  MachineType::AnyTagged()};
131

132
TEST(BoolToBit_constant) {
133 134 135
  RepresentationChangerTester r;

  Node* true_node = r.jsgraph()->TrueConstant();
136
  Node* true_use = r.Return(true_node);
137
  Node* true_bit = r.changer()->GetRepresentationFor(
138 139
      true_node, MachineRepresentation::kTagged, Type::None(), true_use,
      UseInfo(MachineRepresentation::kBit, Truncation::None()));
140 141 142
  r.CheckInt32Constant(true_bit, 1);

  Node* false_node = r.jsgraph()->FalseConstant();
143
  Node* false_use = r.Return(false_node);
144
  Node* false_bit = r.changer()->GetRepresentationFor(
145 146
      false_node, MachineRepresentation::kTagged, Type::None(), false_use,
      UseInfo(MachineRepresentation::kBit, Truncation::None()));
147 148 149 150 151 152
  r.CheckInt32Constant(false_bit, 0);
}

TEST(ToTagged_constant) {
  RepresentationChangerTester r;

153 154 155 156 157 158 159
  for (double i : ValueHelper::float64_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kFloat64, Type::None(), use,
        UseInfo(MachineRepresentation::kTagged, Truncation::None()));
    r.CheckNumberConstant(c, i);
160 161
  }

162 163 164 165 166 167 168
  for (int i : ValueHelper::int32_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Signed32(), use,
        UseInfo(MachineRepresentation::kTagged, Truncation::None()));
    r.CheckNumberConstant(c, i);
169 170
  }

171 172 173 174 175 176 177
  for (uint32_t i : ValueHelper::uint32_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Unsigned32(), use,
        UseInfo(MachineRepresentation::kTagged, Truncation::None()));
    r.CheckNumberConstant(c, i);
178 179 180 181 182 183
  }
}

TEST(ToFloat64_constant) {
  RepresentationChangerTester r;

184 185 186 187 188 189 190
  for (double i : ValueHelper::float64_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kTagged, Type::None(), use,
        UseInfo(MachineRepresentation::kFloat64, Truncation::None()));
    r.CheckFloat64Constant(c, i);
191 192
  }

193 194 195 196 197 198 199
  for (int i : ValueHelper::int32_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Signed32(), use,
        UseInfo(MachineRepresentation::kFloat64, Truncation::None()));
    r.CheckFloat64Constant(c, i);
200 201
  }

202 203 204 205 206 207 208
  for (uint32_t i : ValueHelper::uint32_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Unsigned32(), use,
        UseInfo(MachineRepresentation::kFloat64, Truncation::None()));
    r.CheckFloat64Constant(c, i);
209
  }
210 211 212 213 214 215 216 217 218

  {
    Node* n = r.jsgraph()->Constant(0);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord64, Type::Range(0, 0, r.zone()), use,
        UseInfo(MachineRepresentation::kFloat64, Truncation::None()));
    r.CheckFloat64Constant(c, 0);
  }
219 220 221 222 223 224 225 226 227 228 229 230 231 232
}


static bool IsFloat32Int32(int32_t val) {
  return val >= -(1 << 23) && val <= (1 << 23);
}


static bool IsFloat32Uint32(uint32_t val) { return val <= (1 << 23); }


TEST(ToFloat32_constant) {
  RepresentationChangerTester r;

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  for (double i : ValueHelper::float32_vector()) {
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kTagged, Type::None(), use,
        UseInfo(MachineRepresentation::kFloat32, Truncation::None()));
    r.CheckFloat32Constant(c, i);
  }

  for (int i : ValueHelper::int32_vector()) {
    if (!IsFloat32Int32(i)) continue;
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Signed32(), use,
        UseInfo(MachineRepresentation::kFloat32, Truncation::None()));
    r.CheckFloat32Constant(c, static_cast<float>(i));
  }

  for (uint32_t i : ValueHelper::uint32_vector()) {
    if (!IsFloat32Uint32(i)) continue;
    Node* n = r.jsgraph()->Constant(i);
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kWord32, Type::Unsigned32(), use,
        UseInfo(MachineRepresentation::kFloat32, Truncation::None()));
    r.CheckFloat32Constant(c, static_cast<float>(i));
260 261 262 263 264 265 266
  }
}

TEST(ToInt32_constant) {
  RepresentationChangerTester r;
  {
    FOR_INT32_INPUTS(i) {
267
      Node* n = r.jsgraph()->Constant(i);
268
      Node* use = r.Return(n);
269
      Node* c = r.changer()->GetRepresentationFor(
270 271
          n, MachineRepresentation::kTagged, Type::Signed32(), use,
          UseInfo(MachineRepresentation::kWord32, Truncation::None()));
272
      r.CheckInt32Constant(c, i);
273 274 275 276 277 278
    }
  }
}

TEST(ToUint32_constant) {
  RepresentationChangerTester r;
279
  FOR_UINT32_INPUTS(i) {
280
    Node* n = r.jsgraph()->Constant(static_cast<double>(i));
281 282 283 284
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
        n, MachineRepresentation::kTagged, Type::Unsigned32(), use,
        UseInfo(MachineRepresentation::kWord32, Truncation::None()));
285
    r.CheckUint32Constant(c, i);
286 287 288
  }
}

289 290 291
TEST(ToInt64_constant) {
  RepresentationChangerTester r;
  FOR_INT32_INPUTS(i) {
292
    Node* n = r.jsgraph()->Constant(i);
293 294
    Node* use = r.Return(n);
    Node* c = r.changer()->GetRepresentationFor(
295
        n, MachineRepresentation::kTagged, TypeCache::Get()->kSafeInteger, use,
296
        UseInfo(MachineRepresentation::kWord64, Truncation::None()));
297
    r.CheckInt64Constant(c, i);
298 299 300
  }
}

301
static void CheckChange(IrOpcode::Value expected, MachineRepresentation from,
302
                        Type from_type, UseInfo use_info) {
303 304
  RepresentationChangerTester r;

305
  Node* n = r.Parameter();
306
  Node* use = r.Return(n);
307 308
  Node* c =
      r.changer()->GetRepresentationFor(n, from, from_type, use, use_info);
309 310 311 312

  CHECK_NE(c, n);
  CHECK_EQ(expected, c->opcode());
  CHECK_EQ(n, c->InputAt(0));
313

314 315
  if (expected == IrOpcode::kCheckedFloat64ToInt32 ||
      expected == IrOpcode::kCheckedFloat64ToInt64) {
316
    CheckForMinusZeroMode mode =
317
        from_type.Maybe(Type::MinusZero())
318 319
            ? use_info.minus_zero_check()
            : CheckForMinusZeroMode::kDontCheckForMinusZero;
320
    CHECK_EQ(mode, CheckMinusZeroParametersOf(c->op()).mode());
321
  }
322 323
}

324
static void CheckChange(IrOpcode::Value expected, MachineRepresentation from,
325
                        Type from_type, MachineRepresentation to) {
326
  CheckChange(expected, from, from_type, UseInfo(to, Truncation::Any()));
327
}
328

329
static void CheckTwoChanges(IrOpcode::Value expected2,
330
                            IrOpcode::Value expected1,
331
                            MachineRepresentation from, Type from_type,
332
                            MachineRepresentation to, UseInfo use_info) {
333 334
  RepresentationChangerTester r;

335
  Node* n = r.Parameter();
336
  Node* use = r.Return(n);
337 338
  Node* c1 =
      r.changer()->GetRepresentationFor(n, from, from_type, use, use_info);
339 340 341 342 343 344 345 346 347

  CHECK_NE(c1, n);
  CHECK_EQ(expected1, c1->opcode());
  Node* c2 = c1->InputAt(0);
  CHECK_NE(c2, n);
  CHECK_EQ(expected2, c2->opcode());
  CHECK_EQ(n, c2->InputAt(0));
}

348 349 350 351 352 353 354 355
static void CheckTwoChanges(IrOpcode::Value expected2,
                            IrOpcode::Value expected1,
                            MachineRepresentation from, Type from_type,
                            MachineRepresentation to) {
  CheckTwoChanges(expected2, expected1, from, from_type, to,
                  UseInfo(to, Truncation::None()));
}

356
static void CheckChange(IrOpcode::Value expected, MachineRepresentation from,
357
                        Type from_type, MachineRepresentation to,
358 359 360 361 362 363 364 365 366 367 368 369
                        UseInfo use_info) {
  RepresentationChangerTester r;

  Node* n = r.Parameter();
  Node* use = r.Return(n);
  Node* c =
      r.changer()->GetRepresentationFor(n, from, from_type, use, use_info);

  CHECK_NE(c, n);
  CHECK_EQ(expected, c->opcode());
  CHECK_EQ(n, c->InputAt(0));
}
370

371
TEST(Word64) {
372
  CheckChange(IrOpcode::kChangeInt32ToInt64, MachineRepresentation::kWord8,
373
              TypeCache::Get()->kInt8, MachineRepresentation::kWord64);
374
  CheckChange(IrOpcode::kChangeUint32ToUint64, MachineRepresentation::kWord8,
375
              TypeCache::Get()->kUint8, MachineRepresentation::kWord64);
376
  CheckChange(IrOpcode::kChangeInt32ToInt64, MachineRepresentation::kWord16,
377
              TypeCache::Get()->kInt16, MachineRepresentation::kWord64);
378
  CheckChange(IrOpcode::kChangeUint32ToUint64, MachineRepresentation::kWord16,
379
              TypeCache::Get()->kUint16, MachineRepresentation::kWord64);
380 381
  CheckChange(IrOpcode::kChangeInt32ToInt64, MachineRepresentation::kWord32,
              Type::Signed32(), MachineRepresentation::kWord64);
382 383 384 385
  CheckChange(
      IrOpcode::kChangeInt32ToInt64, MachineRepresentation::kWord32,
      Type::Signed32OrMinusZero(), MachineRepresentation::kWord64,
      UseInfo(MachineRepresentation::kWord64, Truncation::Any(kIdentifyZeros)));
386 387
  CheckChange(IrOpcode::kChangeUint32ToUint64, MachineRepresentation::kWord32,
              Type::Unsigned32(), MachineRepresentation::kWord64);
388 389 390 391
  CheckChange(
      IrOpcode::kChangeUint32ToUint64, MachineRepresentation::kWord32,
      Type::Unsigned32OrMinusZero(), MachineRepresentation::kWord64,
      UseInfo(MachineRepresentation::kWord64, Truncation::Any(kIdentifyZeros)));
392

393 394 395 396 397
  CheckChange(IrOpcode::kTruncateInt64ToInt32, MachineRepresentation::kWord64,
              Type::Signed32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kTruncateInt64ToInt32, MachineRepresentation::kWord64,
              Type::Unsigned32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kTruncateInt64ToInt32, MachineRepresentation::kWord64,
398
              TypeCache::Get()->kSafeInteger, MachineRepresentation::kWord32,
399 400 401
              UseInfo::TruncatingWord32());
  CheckChange(
      IrOpcode::kCheckedInt64ToInt32, MachineRepresentation::kWord64,
402
      TypeCache::Get()->kSafeInteger, MachineRepresentation::kWord32,
403
      UseInfo::CheckedSigned32AsWord32(kIdentifyZeros, FeedbackSource()));
404 405
  CheckChange(
      IrOpcode::kCheckedUint64ToInt32, MachineRepresentation::kWord64,
406
      TypeCache::Get()->kPositiveSafeInteger, MachineRepresentation::kWord32,
407
      UseInfo::CheckedSigned32AsWord32(kIdentifyZeros, FeedbackSource()));
408

409 410 411 412 413
  CheckChange(IrOpcode::kChangeFloat64ToInt64, MachineRepresentation::kFloat64,
              Type::Signed32(), MachineRepresentation::kWord64);
  CheckChange(IrOpcode::kChangeFloat64ToInt64, MachineRepresentation::kFloat64,
              Type::Unsigned32(), MachineRepresentation::kWord64);
  CheckChange(IrOpcode::kChangeFloat64ToInt64, MachineRepresentation::kFloat64,
414
              TypeCache::Get()->kSafeInteger, MachineRepresentation::kWord64);
415
  CheckChange(IrOpcode::kChangeFloat64ToInt64, MachineRepresentation::kFloat64,
416 417
              TypeCache::Get()->kDoubleRepresentableInt64,
              MachineRepresentation::kWord64);
418
  CheckChange(IrOpcode::kChangeFloat64ToUint64, MachineRepresentation::kFloat64,
419 420
              TypeCache::Get()->kDoubleRepresentableUint64,
              MachineRepresentation::kWord64);
421 422 423
  CheckChange(
      IrOpcode::kCheckedFloat64ToInt64, MachineRepresentation::kFloat64,
      Type::Number(), MachineRepresentation::kWord64,
424
      UseInfo::CheckedSigned64AsWord64(kIdentifyZeros, FeedbackSource()));
425

426 427 428 429 430
  CheckChange(IrOpcode::kChangeInt64ToFloat64, MachineRepresentation::kWord64,
              Type::Signed32(), MachineRepresentation::kFloat64);
  CheckChange(IrOpcode::kChangeInt64ToFloat64, MachineRepresentation::kWord64,
              Type::Unsigned32(), MachineRepresentation::kFloat64);
  CheckChange(IrOpcode::kChangeInt64ToFloat64, MachineRepresentation::kWord64,
431
              TypeCache::Get()->kSafeInteger, MachineRepresentation::kFloat64);
432

433 434 435 436 437 438 439 440
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToInt64,
                  MachineRepresentation::kFloat32, Type::Signed32(),
                  MachineRepresentation::kWord64);
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToInt64,
                  MachineRepresentation::kFloat32, Type::Unsigned32(),
                  MachineRepresentation::kWord64);
441 442
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToInt64,
443 444
                  MachineRepresentation::kFloat32,
                  TypeCache::Get()->kDoubleRepresentableInt64,
445 446 447
                  MachineRepresentation::kWord64);
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToUint64,
448 449
                  MachineRepresentation::kFloat32,
                  TypeCache::Get()->kDoubleRepresentableUint64,
450
                  MachineRepresentation::kWord64);
451 452 453 454
  CheckTwoChanges(
      IrOpcode::kChangeFloat32ToFloat64, IrOpcode::kCheckedFloat64ToInt64,
      MachineRepresentation::kFloat32, Type::Number(),
      MachineRepresentation::kWord64,
455
      UseInfo::CheckedSigned64AsWord64(kIdentifyZeros, FeedbackSource()));
456

457 458 459 460 461
  CheckTwoChanges(IrOpcode::kChangeInt64ToFloat64,
                  IrOpcode::kTruncateFloat64ToFloat32,
                  MachineRepresentation::kWord64, Type::Signed32(),
                  MachineRepresentation::kFloat32);

462 463 464 465 466
  CheckChange(IrOpcode::kChangeTaggedToInt64, MachineRepresentation::kTagged,
              Type::Signed32(), MachineRepresentation::kWord64);
  CheckChange(IrOpcode::kChangeTaggedToInt64, MachineRepresentation::kTagged,
              Type::Unsigned32(), MachineRepresentation::kWord64);
  CheckChange(IrOpcode::kChangeTaggedToInt64, MachineRepresentation::kTagged,
467
              TypeCache::Get()->kSafeInteger, MachineRepresentation::kWord64);
468
  CheckChange(IrOpcode::kChangeTaggedToInt64, MachineRepresentation::kTagged,
469 470
              TypeCache::Get()->kDoubleRepresentableInt64,
              MachineRepresentation::kWord64);
471 472 473
  CheckChange(IrOpcode::kChangeTaggedSignedToInt64,
              MachineRepresentation::kTaggedSigned, Type::SignedSmall(),
              MachineRepresentation::kWord64);
474 475 476
  CheckChange(
      IrOpcode::kCheckedTaggedToInt64, MachineRepresentation::kTagged,
      Type::Number(), MachineRepresentation::kWord64,
477
      UseInfo::CheckedSigned64AsWord64(kIdentifyZeros, FeedbackSource()));
478 479 480
  CheckChange(
      IrOpcode::kCheckedTaggedToInt64, MachineRepresentation::kTaggedPointer,
      Type::Number(), MachineRepresentation::kWord64,
481
      UseInfo::CheckedSigned64AsWord64(kIdentifyZeros, FeedbackSource()));
482 483 484 485 486 487 488 489 490 491 492 493 494 495

  CheckTwoChanges(IrOpcode::kTruncateInt64ToInt32,
                  IrOpcode::kChangeInt31ToTaggedSigned,
                  MachineRepresentation::kWord64, Type::Signed31(),
                  MachineRepresentation::kTagged);
  CheckTwoChanges(IrOpcode::kTruncateInt64ToInt32,
                  IrOpcode::kChangeInt32ToTagged,
                  MachineRepresentation::kWord64, Type::Signed32(),
                  MachineRepresentation::kTagged);
  CheckTwoChanges(IrOpcode::kTruncateInt64ToInt32,
                  IrOpcode::kChangeUint32ToTagged,
                  MachineRepresentation::kWord64, Type::Unsigned32(),
                  MachineRepresentation::kTagged);
  CheckChange(IrOpcode::kChangeInt64ToTagged, MachineRepresentation::kWord64,
496
              TypeCache::Get()->kSafeInteger, MachineRepresentation::kTagged);
497
  CheckChange(IrOpcode::kChangeUint64ToTagged, MachineRepresentation::kWord64,
498
              TypeCache::Get()->kPositiveSafeInteger,
499 500 501 502 503 504 505 506 507 508 509 510 511
              MachineRepresentation::kTagged);

  CheckTwoChanges(IrOpcode::kTruncateInt64ToInt32,
                  IrOpcode::kChangeInt31ToTaggedSigned,
                  MachineRepresentation::kWord64, Type::Signed31(),
                  MachineRepresentation::kTaggedSigned);
  if (SmiValuesAre32Bits()) {
    CheckTwoChanges(IrOpcode::kTruncateInt64ToInt32,
                    IrOpcode::kChangeInt32ToTagged,
                    MachineRepresentation::kWord64, Type::Signed32(),
                    MachineRepresentation::kTaggedSigned);
  }
  CheckChange(IrOpcode::kCheckedInt64ToTaggedSigned,
512
              MachineRepresentation::kWord64, TypeCache::Get()->kSafeInteger,
513
              MachineRepresentation::kTaggedSigned,
514
              UseInfo::CheckedSignedSmallAsTaggedSigned(FeedbackSource()));
515 516
  CheckChange(IrOpcode::kCheckedUint64ToTaggedSigned,
              MachineRepresentation::kWord64,
517
              TypeCache::Get()->kPositiveSafeInteger,
518
              MachineRepresentation::kTaggedSigned,
519
              UseInfo::CheckedSignedSmallAsTaggedSigned(FeedbackSource()));
520

521 522 523 524
  CheckTwoChanges(
      IrOpcode::kChangeInt64ToFloat64, IrOpcode::kChangeFloat64ToTaggedPointer,
      MachineRepresentation::kWord64, TypeCache::Get()->kSafeInteger,
      MachineRepresentation::kTaggedPointer);
525 526
}

527
TEST(SingleChanges) {
528
  CheckChange(IrOpcode::kChangeTaggedToBit, MachineRepresentation::kTagged,
529
              Type::Boolean(), MachineRepresentation::kBit);
530
  CheckChange(IrOpcode::kChangeBitToTagged, MachineRepresentation::kBit,
531
              Type::Boolean(), MachineRepresentation::kTagged);
532

533 534 535
  CheckChange(IrOpcode::kChangeInt31ToTaggedSigned,
              MachineRepresentation::kWord32, Type::Signed31(),
              MachineRepresentation::kTagged);
536 537 538 539 540
  CheckChange(IrOpcode::kChangeInt32ToTagged, MachineRepresentation::kWord32,
              Type::Signed32(), MachineRepresentation::kTagged);
  CheckChange(IrOpcode::kChangeUint32ToTagged, MachineRepresentation::kWord32,
              Type::Unsigned32(), MachineRepresentation::kTagged);
  CheckChange(IrOpcode::kChangeFloat64ToTagged, MachineRepresentation::kFloat64,
541 542
              Type::Number(), MachineRepresentation::kTagged);
  CheckTwoChanges(IrOpcode::kChangeFloat64ToInt32,
543
                  IrOpcode::kChangeInt31ToTaggedSigned,
544 545 546 547 548 549 550 551 552 553
                  MachineRepresentation::kFloat64, Type::Signed31(),
                  MachineRepresentation::kTagged);
  CheckTwoChanges(IrOpcode::kChangeFloat64ToInt32,
                  IrOpcode::kChangeInt32ToTagged,
                  MachineRepresentation::kFloat64, Type::Signed32(),
                  MachineRepresentation::kTagged);
  CheckTwoChanges(IrOpcode::kChangeFloat64ToUint32,
                  IrOpcode::kChangeUint32ToTagged,
                  MachineRepresentation::kFloat64, Type::Unsigned32(),
                  MachineRepresentation::kTagged);
554 555 556 557 558 559

  CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
              Type::Signed32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
              Type::Unsigned32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kChangeTaggedToFloat64, MachineRepresentation::kTagged,
560
              Type::Number(), MachineRepresentation::kFloat64);
561 562
  CheckChange(IrOpcode::kTruncateTaggedToFloat64,
              MachineRepresentation::kTagged, Type::NumberOrUndefined(),
563 564
              UseInfo(MachineRepresentation::kFloat64,
                      Truncation::OddballAndBigIntToNumber()));
565 566
  CheckChange(IrOpcode::kChangeTaggedToFloat64, MachineRepresentation::kTagged,
              Type::Signed31(), MachineRepresentation::kFloat64);
567 568

  // Int32,Uint32 <-> Float64 are actually machine conversions.
569 570
  CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
              Type::Signed32(), MachineRepresentation::kFloat64);
571 572 573 574
  CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
              Type::Signed32OrMinusZero(), MachineRepresentation::kFloat64,
              UseInfo(MachineRepresentation::kFloat64,
                      Truncation::Any(kIdentifyZeros)));
575 576 577 578 579 580 581 582
  CheckChange(IrOpcode::kChangeUint32ToFloat64, MachineRepresentation::kWord32,
              Type::Unsigned32(), MachineRepresentation::kFloat64);
  CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
              Type::Signed32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kChangeFloat64ToUint32, MachineRepresentation::kFloat64,
              Type::Unsigned32(), MachineRepresentation::kWord32);

  CheckChange(IrOpcode::kTruncateFloat64ToFloat32,
583
              MachineRepresentation::kFloat64, Type::Number(),
584
              MachineRepresentation::kFloat32);
585

586 587
  // Int32,Uint32 <-> Float32 require two changes.
  CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
588 589
                  IrOpcode::kTruncateFloat64ToFloat32,
                  MachineRepresentation::kWord32, Type::Signed32(),
590
                  MachineRepresentation::kFloat32);
591
  CheckTwoChanges(IrOpcode::kChangeUint32ToFloat64,
592 593
                  IrOpcode::kTruncateFloat64ToFloat32,
                  MachineRepresentation::kWord32, Type::Unsigned32(),
594
                  MachineRepresentation::kFloat32);
595 596 597 598 599 600 601 602
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToInt32,
                  MachineRepresentation::kFloat32, Type::Signed32(),
                  MachineRepresentation::kWord32);
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
                  IrOpcode::kChangeFloat64ToUint32,
                  MachineRepresentation::kFloat32, Type::Unsigned32(),
                  MachineRepresentation::kWord32);
603 604 605

  // Float32 <-> Tagged require two changes.
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
606
                  IrOpcode::kChangeFloat64ToTagged,
607
                  MachineRepresentation::kFloat32, Type::Number(),
608
                  MachineRepresentation::kTagged);
609
  CheckTwoChanges(IrOpcode::kChangeTaggedToFloat64,
610
                  IrOpcode::kTruncateFloat64ToFloat32,
611
                  MachineRepresentation::kTagged, Type::Number(),
612
                  MachineRepresentation::kFloat32);
613 614 615 616 617 618
}


TEST(SignednessInWord32) {
  RepresentationChangerTester r;

619 620 621 622 623
  CheckChange(IrOpcode::kChangeTaggedToInt32, MachineRepresentation::kTagged,
              Type::Signed32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kChangeTaggedToUint32, MachineRepresentation::kTagged,
              Type::Unsigned32(), MachineRepresentation::kWord32);
  CheckChange(IrOpcode::kChangeInt32ToFloat64, MachineRepresentation::kWord32,
624
              Type::Signed32(), MachineRepresentation::kFloat64);
625 626
  CheckChange(IrOpcode::kChangeFloat64ToInt32, MachineRepresentation::kFloat64,
              Type::Signed32(), MachineRepresentation::kWord32);
627
  CheckChange(IrOpcode::kTruncateFloat64ToWord32,
628
              MachineRepresentation::kFloat64, Type::Number(),
629 630
              MachineRepresentation::kWord32,
              UseInfo(MachineRepresentation::kWord32, Truncation::Word32()));
631
  CheckChange(IrOpcode::kCheckedTruncateTaggedToWord32,
632
              MachineRepresentation::kTagged, Type::NonInternal(),
633
              MachineRepresentation::kWord32,
634
              UseInfo::CheckedNumberOrOddballAsWord32(FeedbackSource()));
635 636

  CheckTwoChanges(IrOpcode::kChangeInt32ToFloat64,
637
                  IrOpcode::kTruncateFloat64ToFloat32,
638
                  MachineRepresentation::kWord32, Type::Signed32(),
639
                  MachineRepresentation::kFloat32);
640
  CheckTwoChanges(IrOpcode::kChangeFloat32ToFloat64,
641
                  IrOpcode::kTruncateFloat64ToWord32,
642
                  MachineRepresentation::kFloat32, Type::Number(),
643
                  MachineRepresentation::kWord32);
644 645 646 647

  CheckChange(
      IrOpcode::kCheckedUint32ToInt32, MachineRepresentation::kWord32,
      Type::Unsigned32(),
648
      UseInfo::CheckedSigned32AsWord32(kIdentifyZeros, FeedbackSource()));
649 650
}

651
static void TestMinusZeroCheck(IrOpcode::Value expected, Type from_type) {
652 653
  RepresentationChangerTester r;

654 655
  CheckChange(
      expected, MachineRepresentation::kFloat64, from_type,
656
      UseInfo::CheckedSignedSmallAsWord32(kDistinguishZeros, FeedbackSource()));
657

658 659
  CheckChange(
      expected, MachineRepresentation::kFloat64, from_type,
660
      UseInfo::CheckedSignedSmallAsWord32(kIdentifyZeros, FeedbackSource()));
661

662 663
  CheckChange(
      expected, MachineRepresentation::kFloat64, from_type,
664
      UseInfo::CheckedSigned32AsWord32(kDistinguishZeros, FeedbackSource()));
665

666 667
  CheckChange(
      expected, MachineRepresentation::kFloat64, from_type,
668
      UseInfo::CheckedSigned32AsWord32(kDistinguishZeros, FeedbackSource()));
669 670 671 672 673 674 675 676
}

TEST(MinusZeroCheck) {
  TestMinusZeroCheck(IrOpcode::kCheckedFloat64ToInt32, Type::NumberOrOddball());
  // PlainNumber cannot be minus zero so the minus zero check should be
  // eliminated.
  TestMinusZeroCheck(IrOpcode::kCheckedFloat64ToInt32, Type::PlainNumber());
}
677 678 679 680 681

TEST(Nops) {
  RepresentationChangerTester r;

  // X -> X is always a nop for any single representation X.
682
  for (size_t i = 0; i < arraysize(kMachineTypes); i++) {
683
    r.CheckNop(kMachineTypes[i].representation(), Type::Number(),
684
               kMachineTypes[i].representation());
685 686
  }

687
  // 32-bit floats.
688 689
  r.CheckNop(MachineRepresentation::kFloat32, Type::Number(),
             MachineRepresentation::kFloat32);
690

691 692 693
  // 32-bit words can be used as smaller word sizes and vice versa, because
  // loads from memory implicitly sign or zero extend the value to the
  // full machine word size, and stores implicitly truncate.
694 695 696 697 698 699 700 701 702 703
  r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
             MachineRepresentation::kWord8);
  r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
             MachineRepresentation::kWord16);
  r.CheckNop(MachineRepresentation::kWord32, Type::Signed32(),
             MachineRepresentation::kWord32);
  r.CheckNop(MachineRepresentation::kWord8, Type::Signed32(),
             MachineRepresentation::kWord32);
  r.CheckNop(MachineRepresentation::kWord16, Type::Signed32(),
             MachineRepresentation::kWord32);
704 705

  // kRepBit (result of comparison) is implicitly a wordish thing.
706 707 708 709 710 711
  r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
             MachineRepresentation::kWord8);
  r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
             MachineRepresentation::kWord16);
  r.CheckNop(MachineRepresentation::kBit, Type::Boolean(),
             MachineRepresentation::kWord32);
712 713 714 715 716 717
}


TEST(TypeErrors) {
  RepresentationChangerTester r;

718
  // Floats cannot be implicitly converted to/from comparison conditions.
719
  r.CheckTypeError(MachineRepresentation::kBit, Type::Number(),
720 721 722
                   MachineRepresentation::kFloat32);
  r.CheckTypeError(MachineRepresentation::kBit, Type::Boolean(),
                   MachineRepresentation::kFloat32);
723

724
  // Word64 is internal and shouldn't be implicitly converted.
725
  r.CheckTypeError(MachineRepresentation::kWord64, Type::Internal(),
726
                   MachineRepresentation::kTagged);
727
  r.CheckTypeError(MachineRepresentation::kTagged, Type::Number(),
728 729 730
                   MachineRepresentation::kWord64);
  r.CheckTypeError(MachineRepresentation::kTagged, Type::Boolean(),
                   MachineRepresentation::kWord64);
731
  r.CheckTypeError(MachineRepresentation::kWord64, Type::Internal(),
732
                   MachineRepresentation::kWord32);
733
  r.CheckTypeError(MachineRepresentation::kWord32, Type::Number(),
734
                   MachineRepresentation::kWord64);
735
}
736 737 738 739

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