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

5
#include <vector>
6

7
#include "src/compiler/types.h"
8
#include "src/execution/isolate.h"
9
#include "src/heap/factory-inl.h"
10
#include "src/heap/heap.h"
11
#include "src/objects/objects.h"
12
#include "test/cctest/cctest.h"
13
#include "test/common/types-fuzz.h"
14

15 16 17
namespace v8 {
namespace internal {
namespace compiler {
18

bmeurer's avatar
bmeurer committed
19
namespace {
20

21
// Testing auxiliaries (breaking the Type abstraction).
22 23 24 25 26 27


static bool IsInteger(double x) {
  return nearbyint(x) == x && !i::IsMinusZero(x);  // Allows for infinities.
}

28
using bitset = uint32_t;
29

30
struct Tests {
31 32
  using TypeIterator = Types::TypeVector::iterator;
  using ValueIterator = Types::ValueVector::iterator;
33

34 35
  Isolate* isolate;
  HandleScope scope;
36
  CanonicalHandleScope canonical;
37
  Zone zone;
38
  Types T;
39

40
  Tests()
41
      : isolate(CcTest::InitIsolateOnce()),
42
        scope(isolate),
43
        canonical(isolate),
44
        zone(isolate->allocator(), ZONE_NAME),
45 46
        T(&zone, isolate, isolate->random_number_generator()) {}

47 48 49 50
  bool IsBitset(Type type) { return type.IsBitset(); }
  bool IsUnion(Type type) { return type.IsUnionForTesting(); }
  BitsetType::bitset AsBitset(Type type) { return type.AsBitsetForTesting(); }
  const UnionType* AsUnion(Type type) { return type.AsUnionForTesting(); }
51

52
  bool Equal(Type type1, Type type2) {
53
    return type1.Equals(type2) &&
54 55
           this->IsBitset(type1) == this->IsBitset(type2) &&
           this->IsUnion(type1) == this->IsUnion(type2) &&
56
           type1.NumConstants() == type2.NumConstants() &&
57 58 59 60 61
           (!this->IsBitset(type1) ||
            this->AsBitset(type1) == this->AsBitset(type2)) &&
           (!this->IsUnion(type1) ||
            this->AsUnion(type1)->LengthForTesting() ==
                this->AsUnion(type2)->LengthForTesting());
62 63
  }

64
  void CheckEqual(Type type1, Type type2) { CHECK(Equal(type1, type2)); }
65

66
  void CheckSub(Type type1, Type type2) {
67 68
    CHECK(type1.Is(type2));
    CHECK(!type2.Is(type1));
69 70
    if (this->IsBitset(type1) && this->IsBitset(type2)) {
      CHECK(this->AsBitset(type1) != this->AsBitset(type2));
71
    }
72
  }
73

74
  void CheckSubOrEqual(Type type1, Type type2) {
75
    CHECK(type1.Is(type2));
76 77 78 79 80 81
    if (this->IsBitset(type1) && this->IsBitset(type2)) {
      CHECK((this->AsBitset(type1) | this->AsBitset(type2))
            == this->AsBitset(type2));
    }
  }

82
  void CheckUnordered(Type type1, Type type2) {
83 84
    CHECK(!type1.Is(type2));
    CHECK(!type2.Is(type1));
85 86
    if (this->IsBitset(type1) && this->IsBitset(type2)) {
      CHECK(this->AsBitset(type1) != this->AsBitset(type2));
87 88
    }
  }
89

90
  void CheckOverlap(Type type1, Type type2) {
91 92
    CHECK(type1.Maybe(type2));
    CHECK(type2.Maybe(type1));
93 94
  }

95
  void CheckDisjoint(Type type1, Type type2) {
96 97 98 99
    CHECK(!type1.Is(type2));
    CHECK(!type2.Is(type1));
    CHECK(!type1.Maybe(type2));
    CHECK(!type2.Maybe(type1));
100 101 102 103
  }

  void IsSomeType() {
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
104
      Type t = *it;
105 106
      CHECK_EQ(1, this->IsBitset(t) + t.IsHeapConstant() + t.IsRange() +
                      t.IsOtherNumberConstant() + this->IsUnion(t));
107
    }
108 109
  }

110
  void Bitset() {
111
    // None and Any are bitsets.
dslomov@chromium.org's avatar
dslomov@chromium.org committed
112 113 114
    CHECK(this->IsBitset(T.None));
    CHECK(this->IsBitset(T.Any));

115
    CHECK(bitset(0) == this->AsBitset(T.None));
116
    CHECK(bitset(0xFFFFFFFEu) == this->AsBitset(T.Any));
117

118
    // Union(T1, T2) is bitset for bitsets T1,T2
119 120
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
121 122 123
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
124
        CHECK(!(this->IsBitset(type1) && this->IsBitset(type2)) ||
125
              this->IsBitset(union12));
126 127 128
      }
    }

129
    // Intersect(T1, T2) is bitset for bitsets T1,T2
130 131
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
132 133 134
        Type type1 = *it1;
        Type type2 = *it2;
        Type intersect12 = T.Intersect(type1, type2);
135 136 137 138 139
        CHECK(!(this->IsBitset(type1) && this->IsBitset(type2)) ||
              this->IsBitset(intersect12));
      }
    }

140
    // Union(T1, T2) is bitset if T2 is bitset and T1.Is(T2)
141 142
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
143 144 145
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
146
        CHECK(!(this->IsBitset(type2) && type1.Is(type2)) ||
147
              this->IsBitset(union12));
148 149 150
      }
    }

151
    // Union(T1, T2) is bitwise disjunction for bitsets T1,T2
152 153
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
154 155 156
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
157
        if (this->IsBitset(type1) && this->IsBitset(type2)) {
158 159
          CHECK(
              (this->AsBitset(type1) | this->AsBitset(type2)) ==
160
              this->AsBitset(union12));
161 162 163 164
        }
      }
    }

165
    // Intersect(T1, T2) is bitwise conjunction for bitsets T1,T2 (modulo None)
166 167
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
168 169
        Type type1 = *it1;
        Type type2 = *it2;
170
        if (this->IsBitset(type1) && this->IsBitset(type2)) {
171
          Type intersect12 = T.Intersect(type1, type2);
172
          bitset bits = this->AsBitset(type1) & this->AsBitset(type2);
173
          CHECK(bits == this->AsBitset(intersect12));
174 175 176
        }
      }
    }
177 178
  }

179
  void Constant() {
180 181 182
    // Constructor
    for (ValueIterator vt = T.values.begin(); vt != T.values.end(); ++vt) {
      Handle<i::Object> value = *vt;
183 184 185
      Type type = T.Constant(value);
      CHECK(type.IsBitset() || type.IsHeapConstant() ||
            type.IsOtherNumberConstant() || type.IsRange());
186
    }
dslomov@chromium.org's avatar
dslomov@chromium.org committed
187

188 189 190
    // Value attribute
    for (ValueIterator vt = T.values.begin(); vt != T.values.end(); ++vt) {
      Handle<i::Object> value = *vt;
191
      Type type = T.Constant(value);
192 193 194
      if (type.IsHeapConstant()) {
        CHECK(value.address() == type.AsHeapConstant()->Value().address());
      } else if (type.IsOtherNumberConstant()) {
195
        CHECK(value->IsHeapNumber());
196
        CHECK(value->Number() == type.AsOtherNumberConstant()->Value());
197 198
      } else if (type.IsBitset()) {
        CHECK(type.IsSingleton());
199
      } else {
200
        CHECK(type.IsRange());
201
        double v = value->Number();
202
        CHECK(v == type.AsRange()->Min() && v == type.AsRange()->Max());
203
      }
204 205
    }

206
    // Functionality & Injectivity: Constant(V1) = Constant(V2) iff V1 = V2
207 208
    for (ValueIterator vt1 = T.values.begin(); vt1 != T.values.end(); ++vt1) {
      for (ValueIterator vt2 = T.values.begin(); vt2 != T.values.end(); ++vt2) {
209 210
        Handle<i::Object> value1 = *vt1;
        Handle<i::Object> value2 = *vt2;
211 212
        Type type1 = T.Constant(value1);
        Type type2 = T.Constant(value2);
213
        if (type1.IsOtherNumberConstant() && type2.IsOtherNumberConstant()) {
214
          CHECK(Equal(type1, type2) ==
215 216 217
                (type1.AsOtherNumberConstant()->Value() ==
                 type2.AsOtherNumberConstant()->Value()));
        } else if (type1.IsRange() && type2.IsRange()) {
218
          CHECK(Equal(type1, type2) ==
219 220
                ((type1.AsRange()->Min() == type2.AsRange()->Min()) &&
                 (type1.AsRange()->Max() == type2.AsRange()->Max())));
221 222 223
        } else {
          CHECK(Equal(type1, type2) == (*value1 == *value2));
        }
224 225
      }
    }
226 227 228

    // Typing of numbers
    Factory* fac = isolate->factory();
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    CHECK(T.Constant(fac->NewNumber(0)).Is(T.UnsignedSmall));
    CHECK(T.Constant(fac->NewNumber(1)).Is(T.UnsignedSmall));
    CHECK(T.Constant(fac->NewNumber(42)).Equals(T.Range(42, 42)));
    CHECK(T.Constant(fac->NewNumber(0x3FFFFFFF)).Is(T.UnsignedSmall));
    CHECK(T.Constant(fac->NewNumber(-1)).Is(T.Negative31));
    CHECK(T.Constant(fac->NewNumber(-0x3FFFFFFF)).Is(T.Negative31));
    CHECK(T.Constant(fac->NewNumber(-0x40000000)).Is(T.Negative31));
    CHECK(T.Constant(fac->NewNumber(0x40000000)).Is(T.Unsigned31));
    CHECK(!T.Constant(fac->NewNumber(0x40000000)).Is(T.Unsigned30));
    CHECK(T.Constant(fac->NewNumber(0x7FFFFFFF)).Is(T.Unsigned31));
    CHECK(!T.Constant(fac->NewNumber(0x7FFFFFFF)).Is(T.Unsigned30));
    CHECK(T.Constant(fac->NewNumber(-0x40000001)).Is(T.Negative32));
    CHECK(!T.Constant(fac->NewNumber(-0x40000001)).Is(T.Negative31));
    CHECK(T.Constant(fac->NewNumber(-0x7FFFFFFF)).Is(T.Negative32));
    CHECK(!T.Constant(fac->NewNumber(-0x7FFFFFFF - 1)).Is(T.Negative31));
244
    if (SmiValuesAre31Bits()) {
245 246 247 248
      CHECK(!T.Constant(fac->NewNumber(0x40000000)).Is(T.UnsignedSmall));
      CHECK(!T.Constant(fac->NewNumber(0x7FFFFFFF)).Is(T.UnsignedSmall));
      CHECK(!T.Constant(fac->NewNumber(-0x40000001)).Is(T.SignedSmall));
      CHECK(!T.Constant(fac->NewNumber(-0x7FFFFFFF - 1)).Is(T.SignedSmall));
249 250
    } else {
      CHECK(SmiValuesAre32Bits());
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      CHECK(T.Constant(fac->NewNumber(0x40000000)).Is(T.UnsignedSmall));
      CHECK(T.Constant(fac->NewNumber(0x7FFFFFFF)).Is(T.UnsignedSmall));
      CHECK(T.Constant(fac->NewNumber(-0x40000001)).Is(T.SignedSmall));
      CHECK(T.Constant(fac->NewNumber(-0x7FFFFFFF - 1)).Is(T.SignedSmall));
    }
    CHECK(T.Constant(fac->NewNumber(0x80000000u)).Is(T.Unsigned32));
    CHECK(!T.Constant(fac->NewNumber(0x80000000u)).Is(T.Unsigned31));
    CHECK(T.Constant(fac->NewNumber(0xFFFFFFFFu)).Is(T.Unsigned32));
    CHECK(!T.Constant(fac->NewNumber(0xFFFFFFFFu)).Is(T.Unsigned31));
    CHECK(T.Constant(fac->NewNumber(0xFFFFFFFFu + 1.0)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(0xFFFFFFFFu + 1.0)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(-0x7FFFFFFF - 2.0)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(-0x7FFFFFFF - 2.0)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(0.1)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(0.1)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(-10.1)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(-10.1)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(10e60)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(10e60)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(-1.0 * 0.0)).Is(T.MinusZero));
    CHECK(T.Constant(fac->NewNumber(V8_INFINITY)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(V8_INFINITY)).Is(T.Integral32));
    CHECK(T.Constant(fac->NewNumber(-V8_INFINITY)).Is(T.PlainNumber));
    CHECK(!T.Constant(fac->NewNumber(-V8_INFINITY)).Is(T.Integral32));
275 276 277

    // Typing of Strings
    Handle<String> s1 = fac->NewStringFromAsciiChecked("a");
278
    CHECK(T.Constant(s1).Is(T.InternalizedString));
279 280 281
    const uc16 two_byte[1] = {0x2603};
    Handle<String> s2 =
        fac->NewTwoByteInternalizedString(Vector<const uc16>(two_byte, 1), 1);
282 283 284 285 286 287 288 289 290 291 292
    CHECK(T.Constant(s2).Is(T.InternalizedString));

    // Typing of special constants
    CHECK(T.Constant(fac->the_hole_value()).Equals(T.Hole));
    CHECK(T.Constant(fac->null_value()).Equals(T.Null));
    CHECK(T.Constant(fac->undefined_value()).Equals(T.Undefined));
    CHECK(T.Constant(fac->minus_zero_value()).Equals(T.MinusZero));
    CHECK(T.Constant(fac->NewNumber(-0.0)).Equals(T.MinusZero));
    CHECK(T.Constant(fac->nan_value()).Equals(T.NaN));
    CHECK(T.Constant(fac->NewNumber(std::numeric_limits<double>::quiet_NaN()))
              .Equals(T.NaN));
293 294
  }

295 296
  void Range() {
    // Constructor
297 298
    for (ValueIterator i = T.integers.begin(); i != T.integers.end(); ++i) {
      for (ValueIterator j = T.integers.begin(); j != T.integers.end(); ++j) {
299 300 301
        double min = (*i)->Number();
        double max = (*j)->Number();
        if (min > max) std::swap(min, max);
302
        Type type = T.Range(min, max);
303
        CHECK(type.IsRange());
304 305 306 307
      }
    }

    // Range attributes
308 309
    for (ValueIterator i = T.integers.begin(); i != T.integers.end(); ++i) {
      for (ValueIterator j = T.integers.begin(); j != T.integers.end(); ++j) {
310 311 312
        double min = (*i)->Number();
        double max = (*j)->Number();
        if (min > max) std::swap(min, max);
313
        Type type = T.Range(min, max);
314 315
        CHECK(min == type.AsRange()->Min());
        CHECK(max == type.AsRange()->Max());
316 317 318 319 320 321 322
      }
    }

    // Functionality & Injectivity:
    // Range(min1, max1) = Range(min2, max2) <=> min1 = min2 /\ max1 = max2
    for (ValueIterator i1 = T.integers.begin();
        i1 != T.integers.end(); ++i1) {
323
      for (ValueIterator j1 = i1;
324 325 326
          j1 != T.integers.end(); ++j1) {
        for (ValueIterator i2 = T.integers.begin();
            i2 != T.integers.end(); ++i2) {
327
          for (ValueIterator j2 = i2;
328
              j2 != T.integers.end(); ++j2) {
329 330 331 332 333 334
            double min1 = (*i1)->Number();
            double max1 = (*j1)->Number();
            double min2 = (*i2)->Number();
            double max2 = (*j2)->Number();
            if (min1 > max1) std::swap(min1, max1);
            if (min2 > max2) std::swap(min2, max2);
335 336
            Type type1 = T.Range(min1, max1);
            Type type2 = T.Range(min2, max2);
337
            CHECK(Equal(type1, type2) == (min1 == min2 && max1 == max2));
338 339 340 341
          }
        }
      }
    }
342 343
  }

344
  void MinMax() {
345
    // If b is regular numeric bitset, then Range(b.Min(), b.Max()).Is(b).
346 347 348
    // TODO(neis): Need to ignore representation for this to be true.
    /*
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
349
      Type type = *it;
350 351
      if (this->IsBitset(type) && type.Is(T.Number) &&
          !type.Is(T.None) && !type.Is(T.NaN)) {
352
        Type range = T.Range(
353 354 355
            isolate->factory()->NewNumber(type.Min()),
            isolate->factory()->NewNumber(type.Max()));
        CHECK(range.Is(type));
356 357 358 359
      }
    }
    */

360
    // If b is regular numeric bitset, then b.Min() and b.Max() are integers.
361
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
362
      Type type = *it;
363 364
      if (this->IsBitset(type) && type.Is(T.Number) && !type.Is(T.NaN)) {
        CHECK(IsInteger(type.Min()) && IsInteger(type.Max()));
365 366 367
      }
    }

368 369
    // If b1 and b2 are regular numeric bitsets with b1.Is(b2), then
    // b1.Min() >= b2.Min() and b1.Max() <= b2.Max().
370 371
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
372 373
        Type type1 = *it1;
        Type type2 = *it2;
374 375 376 377
        if (this->IsBitset(type1) && type1.Is(type2) && type2.Is(T.Number) &&
            !type1.Is(T.NaN) && !type2.Is(T.NaN)) {
          CHECK(type1.Min() >= type2.Min());
          CHECK(type1.Max() <= type2.Max());
378 379 380 381
        }
      }
    }

382
    // Lub(Range(x,y)).Min() <= x and y <= Lub(Range(x,y)).Max()
383
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
384
      Type type = *it;
385 386 387
      if (type.IsRange()) {
        Type lub = type.BitsetLubForTesting();
        CHECK(lub.Min() <= type.Min() && type.Max() <= lub.Max());
388 389
      }
    }
390

391 392
    // Rangification: If T.Is(Range(-inf,+inf)) and T is inhabited, then
    // T.Is(Range(T.Min(), T.Max())).
393
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
394
      Type type = *it;
395 396
      CHECK(!type.Is(T.Integer) || type.IsNone() ||
            type.Is(T.Range(type.Min(), type.Max())));
397
    }
398 399
  }

400
  void BitsetGlb() {
401
    // Lower: (T->BitsetGlb()).Is(T)
402
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
403
      Type type = *it;
404 405
      Type glb = type.BitsetGlbForTesting();
      CHECK(glb.Is(type));
406 407
    }

408
    // Greatest: If T1.IsBitset() and T1.Is(T2), then T1.Is(T2->BitsetGlb())
409 410
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
411 412
        Type type1 = *it1;
        Type type2 = *it2;
413 414
        Type glb2 = type2.BitsetGlbForTesting();
        CHECK(!this->IsBitset(type1) || !type1.Is(type2) || type1.Is(glb2));
415 416 417
      }
    }

418
    // Monotonicity: T1.Is(T2) implies (T1->BitsetGlb()).Is(T2->BitsetGlb())
419 420
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
421 422
        Type type1 = *it1;
        Type type2 = *it2;
423 424 425
        Type glb1 = type1.BitsetGlbForTesting();
        Type glb2 = type2.BitsetGlbForTesting();
        CHECK(!type1.Is(type2) || glb1.Is(glb2));
426
      }
427
    }
428
  }
429

430
  void BitsetLub() {
431
    // Upper: T.Is(T->BitsetLub())
432
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
433
      Type type = *it;
434 435
      Type lub = type.BitsetLubForTesting();
      CHECK(type.Is(lub));
436 437
    }

438
    // Least: If T2.IsBitset() and T1.Is(T2), then (T1->BitsetLub()).Is(T2)
439 440
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
441 442
        Type type1 = *it1;
        Type type2 = *it2;
443 444
        Type lub1 = type1.BitsetLubForTesting();
        CHECK(!this->IsBitset(type2) || !type1.Is(type2) || lub1.Is(type2));
445 446 447
      }
    }

448
    // Monotonicity: T1.Is(T2) implies (T1->BitsetLub()).Is(T2->BitsetLub())
449 450
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
451 452
        Type type1 = *it1;
        Type type2 = *it2;
453 454 455
        Type lub1 = type1.BitsetLubForTesting();
        Type lub2 = type2.BitsetLubForTesting();
        CHECK(!type1.Is(type2) || lub1.Is(lub2));
456
      }
457 458 459
    }
  }

460
  void Is1() {
461
    // Least Element (Bottom): None.Is(T)
462
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
463
      Type type = *it;
464
      CHECK(T.None.Is(type));
465 466
    }

467
    // Greatest Element (Top): T.Is(Any)
468
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
469
      Type type = *it;
470
      CHECK(type.Is(T.Any));
471 472
    }

473
    // Bottom Uniqueness: T.Is(None) implies T = None
474
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
475
      Type type = *it;
476
      if (type.Is(T.None)) CheckEqual(type, T.None);
477 478
    }

479
    // Top Uniqueness: Any.Is(T) implies T = Any
480
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
481
      Type type = *it;
482
      if (T.Any.Is(type)) CheckEqual(type, T.Any);
483 484
    }

485
    // Reflexivity: T.Is(T)
486
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
487
      Type type = *it;
488
      CHECK(type.Is(type));
489
    }
490

491
    // Transitivity: T1.Is(T2) and T2.Is(T3) implies T1.Is(T3)
492 493 494
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
495 496 497
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
498
          CHECK(!(type1.Is(type2) && type2.Is(type3)) || type1.Is(type3));
499 500 501
        }
      }
    }
502

503
    // Antisymmetry: T1.Is(T2) and T2.Is(T1) iff T1 = T2
504 505
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
506 507
        Type type1 = *it1;
        Type type2 = *it2;
508
        CHECK((type1.Is(type2) && type2.Is(type1)) == Equal(type1, type2));
509 510 511
      }
    }

512 513 514
    // (In-)Compatibilities.
    for (TypeIterator i = T.types.begin(); i != T.types.end(); ++i) {
      for (TypeIterator j = T.types.begin(); j != T.types.end(); ++j) {
515 516
        Type type1 = *i;
        Type type2 = *j;
517 518 519 520 521 522 523 524
        CHECK(
            !type1.Is(type2) || this->IsBitset(type2) || this->IsUnion(type2) ||
            this->IsUnion(type1) ||
            (type1.IsHeapConstant() && type2.IsHeapConstant()) ||
            (this->IsBitset(type1) && type2.IsRange()) ||
            (type1.IsRange() && type2.IsRange()) ||
            (type1.IsOtherNumberConstant() && type2.IsOtherNumberConstant()) ||
            type1.IsNone());
525 526 527 528 529
      }
    }
  }

  void Is2() {
530
    // Range(X1, Y1).Is(Range(X2, Y2)) iff X1 >= X2 /\ Y1 <= Y2
531 532
    for (ValueIterator i1 = T.integers.begin();
        i1 != T.integers.end(); ++i1) {
533
      for (ValueIterator j1 = i1;
534 535 536
          j1 != T.integers.end(); ++j1) {
        for (ValueIterator i2 = T.integers.begin();
             i2 != T.integers.end(); ++i2) {
537
          for (ValueIterator j2 = i2;
538
               j2 != T.integers.end(); ++j2) {
539 540 541 542 543 544
            double min1 = (*i1)->Number();
            double max1 = (*j1)->Number();
            double min2 = (*i2)->Number();
            double max2 = (*j2)->Number();
            if (min1 > max1) std::swap(min1, max1);
            if (min2 > max2) std::swap(min2, max2);
545 546
            Type type1 = T.Range(min1, max1);
            Type type2 = T.Range(min2, max2);
547
            CHECK(type1.Is(type2) == (min1 >= min2 && max1 <= max2));
548 549
          }
        }
550 551 552
      }
    }

553
    // Constant(V1).Is(Constant(V2)) iff V1 = V2
554 555 556 557
    for (ValueIterator vt1 = T.values.begin(); vt1 != T.values.end(); ++vt1) {
      for (ValueIterator vt2 = T.values.begin(); vt2 != T.values.end(); ++vt2) {
        Handle<i::Object> value1 = *vt1;
        Handle<i::Object> value2 = *vt2;
558 559
        Type const_type1 = T.Constant(value1);
        Type const_type2 = T.Constant(value2);
560 561 562 563 564 565 566 567 568 569
        if (const_type1.IsOtherNumberConstant() &&
            const_type2.IsOtherNumberConstant()) {
          CHECK(const_type1.Is(const_type2) ==
                (const_type1.AsOtherNumberConstant()->Value() ==
                 const_type2.AsOtherNumberConstant()->Value()));
        } else if (const_type1.IsRange() && const_type2.IsRange()) {
          CHECK(
              Equal(const_type1, const_type2) ==
              ((const_type1.AsRange()->Min() == const_type2.AsRange()->Min()) &&
               (const_type1.AsRange()->Max() == const_type2.AsRange()->Max())));
570
        } else {
571
          CHECK(const_type1.Is(const_type2) == (*value1 == *value2));
572
        }
573 574 575 576 577
      }
    }

    // Range-specific subtyping

578
    // Lub(Range(x,y)).Is(T.Union(T.Integral32, T.OtherNumber))
579
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
580
      Type type = *it;
581 582 583
      if (type.IsRange()) {
        Type lub = type.BitsetLubForTesting();
        CHECK(lub.Is(T.PlainNumber));
584 585 586 587 588 589
      }
    }


    // Subtyping between concrete basic types

590 591 592 593
    CheckUnordered(T.Boolean, T.Null);
    CheckUnordered(T.Undefined, T.Null);
    CheckUnordered(T.Boolean, T.Undefined);

594
    CheckSub(T.SignedSmall, T.Number);
595
    CheckSub(T.Signed32, T.Number);
596
    CheckSubOrEqual(T.SignedSmall, T.Signed32);
597 598
    CheckUnordered(T.SignedSmall, T.MinusZero);
    CheckUnordered(T.Signed32, T.Unsigned32);
599 600 601 602 603 604 605 606 607 608 609 610 611 612

    CheckSub(T.UniqueName, T.Name);
    CheckSub(T.String, T.Name);
    CheckSub(T.InternalizedString, T.String);
    CheckSub(T.InternalizedString, T.UniqueName);
    CheckSub(T.InternalizedString, T.Name);
    CheckSub(T.Symbol, T.UniqueName);
    CheckSub(T.Symbol, T.Name);
    CheckUnordered(T.String, T.UniqueName);
    CheckUnordered(T.String, T.Symbol);
    CheckUnordered(T.InternalizedString, T.Symbol);

    CheckSub(T.Object, T.Receiver);
    CheckSub(T.Proxy, T.Receiver);
613
    CheckSub(T.Array, T.Object);
614
    CheckSub(T.OtherObject, T.Object);
615
    CheckSub(T.OtherUndetectable, T.Object);
616

617
    CheckUnordered(T.Object, T.Proxy);
618
    CheckUnordered(T.Array, T.Undetectable);
619
    CheckUnordered(T.OtherObject, T.Undetectable);
620 621 622

    // Subtyping between concrete structural types

623
    CheckSub(T.SmiConstant, T.SignedSmall);
624 625 626 627
    CheckSub(T.SmiConstant, T.Signed32);
    CheckSub(T.SmiConstant, T.Number);
    CheckSub(T.ObjectConstant1, T.Object);
    CheckSub(T.ObjectConstant2, T.Object);
628
    CheckSub(T.ArrayConstant, T.Object);
629
    CheckSub(T.ArrayConstant, T.Array);
630
    CheckSub(T.ArrayConstant, T.Receiver);
631
    CheckSub(T.UninitializedConstant, T.Internal);
632
    CheckUnordered(T.ObjectConstant1, T.ObjectConstant2);
633
    CheckUnordered(T.ObjectConstant1, T.ArrayConstant);
634 635
    CheckUnordered(T.UninitializedConstant, T.Null);
    CheckUnordered(T.UninitializedConstant, T.Undefined);
636
  }
637

638
  void Maybe() {
639
    // T.Maybe(Any) iff T inhabited
640
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
641
      Type type = *it;
642
      CHECK(type.Maybe(T.Any) == !type.IsNone());
643 644
    }

645
    // T.Maybe(None) never
646
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
647
      Type type = *it;
648
      CHECK(!type.Maybe(T.None));
649 650
    }

651
    // Reflexivity upto Inhabitation: T.Maybe(T) iff T inhabited
652
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
653
      Type type = *it;
654
      CHECK(type.Maybe(type) == !type.IsNone());
655 656
    }

657
    // Symmetry: T1.Maybe(T2) iff T2.Maybe(T1)
658 659
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
660 661
        Type type1 = *it1;
        Type type2 = *it2;
662
        CHECK(type1.Maybe(type2) == type2.Maybe(type1));
663 664 665
      }
    }

666
    // T1.Maybe(T2) implies T1, T2 inhabited
667 668
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
669 670
        Type type1 = *it1;
        Type type2 = *it2;
671
        CHECK(!type1.Maybe(type2) || (!type1.IsNone() && !type2.IsNone()));
672 673 674
      }
    }

675
    // T1.Maybe(T2) implies Intersect(T1, T2) inhabited
676 677
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
678 679 680
        Type type1 = *it1;
        Type type2 = *it2;
        Type intersect12 = T.Intersect(type1, type2);
681
        CHECK(!type1.Maybe(type2) || !intersect12.IsNone());
682 683 684
      }
    }

685
    // T1.Is(T2) and T1 inhabited implies T1.Maybe(T2)
686 687
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
688 689
        Type type1 = *it1;
        Type type2 = *it2;
690
        CHECK(!(type1.Is(type2) && !type1.IsNone()) || type1.Maybe(type2));
691 692 693
      }
    }

694
    // Constant(V1).Maybe(Constant(V2)) iff V1 = V2
695 696
    for (ValueIterator vt1 = T.values.begin(); vt1 != T.values.end(); ++vt1) {
      for (ValueIterator vt2 = T.values.begin(); vt2 != T.values.end(); ++vt2) {
697 698
        Handle<i::Object> value1 = *vt1;
        Handle<i::Object> value2 = *vt2;
699 700
        Type const_type1 = T.Constant(value1);
        Type const_type2 = T.Constant(value2);
701 702 703 704 705 706 707 708 709 710
        if (const_type1.IsOtherNumberConstant() &&
            const_type2.IsOtherNumberConstant()) {
          CHECK(const_type1.Maybe(const_type2) ==
                (const_type1.AsOtherNumberConstant()->Value() ==
                 const_type2.AsOtherNumberConstant()->Value()));
        } else if (const_type1.IsRange() && const_type2.IsRange()) {
          CHECK(
              Equal(const_type1, const_type2) ==
              ((const_type1.AsRange()->Min() == const_type2.AsRange()->Min()) &&
               (const_type1.AsRange()->Max() == const_type2.AsRange()->Max())));
711
        } else {
712
          CHECK(const_type1.Maybe(const_type2) == (*value1 == *value2));
713
        }
714 715
      }
    }
716

717
    // Basic types
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    CheckDisjoint(T.Boolean, T.Null);
    CheckDisjoint(T.Undefined, T.Null);
    CheckDisjoint(T.Boolean, T.Undefined);
    CheckOverlap(T.SignedSmall, T.Number);
    CheckOverlap(T.NaN, T.Number);
    CheckDisjoint(T.Signed32, T.NaN);
    CheckOverlap(T.UniqueName, T.Name);
    CheckOverlap(T.String, T.Name);
    CheckOverlap(T.InternalizedString, T.String);
    CheckOverlap(T.InternalizedString, T.UniqueName);
    CheckOverlap(T.InternalizedString, T.Name);
    CheckOverlap(T.Symbol, T.UniqueName);
    CheckOverlap(T.Symbol, T.Name);
    CheckOverlap(T.String, T.UniqueName);
    CheckDisjoint(T.String, T.Symbol);
    CheckDisjoint(T.InternalizedString, T.Symbol);
    CheckOverlap(T.Object, T.Receiver);
735
    CheckOverlap(T.OtherObject, T.Object);
736 737
    CheckOverlap(T.Proxy, T.Receiver);
    CheckDisjoint(T.Object, T.Proxy);
738

739
    // Structural types
740 741 742 743 744 745
    CheckOverlap(T.SmiConstant, T.SignedSmall);
    CheckOverlap(T.SmiConstant, T.Signed32);
    CheckOverlap(T.SmiConstant, T.Number);
    CheckOverlap(T.ObjectConstant1, T.Object);
    CheckOverlap(T.ObjectConstant2, T.Object);
    CheckOverlap(T.ArrayConstant, T.Object);
746
    CheckOverlap(T.ArrayConstant, T.Receiver);
747 748 749
    CheckOverlap(T.ObjectConstant1, T.ObjectConstant1);
    CheckDisjoint(T.ObjectConstant1, T.ObjectConstant2);
    CheckDisjoint(T.ObjectConstant1, T.ArrayConstant);
750
  }
751

752
  void Union1() {
753 754
    // Identity: Union(T, None) = T
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
755 756
      Type type = *it;
      Type union_type = T.Union(type, T.None);
757 758 759 760 761
      CheckEqual(union_type, type);
    }

    // Domination: Union(T, Any) = Any
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
762 763
      Type type = *it;
      Type union_type = T.Union(type, T.Any);
764 765 766 767 768
      CheckEqual(union_type, T.Any);
    }

    // Idempotence: Union(T, T) = T
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
769 770
      Type type = *it;
      Type union_type = T.Union(type, type);
771 772
      CheckEqual(union_type, type);
    }
773

774 775 776
    // Commutativity: Union(T1, T2) = Union(T2, T1)
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
777 778 779 780
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
        Type union21 = T.Union(type2, type1);
781 782 783 784 785
        CheckEqual(union12, union21);
      }
    }

    // Associativity: Union(T1, Union(T2, T3)) = Union(Union(T1, T2), T3)
786 787 788
    // This does NOT hold!  For example:
    // (Unsigned32 \/ Range(0,5)) \/ Range(-5,0) = Unsigned32 \/ Range(-5,0)
    // Unsigned32 \/ (Range(0,5) \/ Range(-5,0)) = Unsigned32 \/ Range(-5,5)
789
    /*
790 791 792
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
793 794 795 796 797 798 799
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type union12 = T.Union(type1, type2);
          Type union23 = T.Union(type2, type3);
          Type union1_23 = T.Union(type1, union23);
          Type union12_3 = T.Union(union12, type3);
800 801 802 803
          CheckEqual(union1_23, union12_3);
        }
      }
    }
804
    */
805

806
    // Meet: T1.Is(Union(T1, T2)) and T2.Is(Union(T1, T2))
807 808
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
809 810 811
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
812 813
        CHECK(type1.Is(union12));
        CHECK(type2.Is(union12));
814 815 816
      }
    }

817
    // Upper Boundedness: T1.Is(T2) implies Union(T1, T2) = T2
818 819
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
820 821 822
        Type type1 = *it1;
        Type type2 = *it2;
        Type union12 = T.Union(type1, type2);
823
        if (type1.Is(type2)) CheckEqual(union12, type2);
824 825 826
      }
    }

827
    // Monotonicity: T1.Is(T2) implies Union(T1, T3).Is(Union(T2, T3))
828 829 830
    // This does NOT hold.  For example:
    // Range(-5,-1) <= Signed32
    // Range(-5,-1) \/ Range(1,5) = Range(-5,5) </= Signed32 \/ Range(1,5)
831
    /*
832 833 834
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
835 836 837 838 839
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type union13 = T.Union(type1, type3);
          Type union23 = T.Union(type2, type3);
840
          CHECK(!type1.Is(type2) || union13.Is(union23));
841 842 843
        }
      }
    }
844 845
    */
  }
846

847
  void Union2() {
848
    // Monotonicity: T1.Is(T3) and T2.Is(T3) implies Union(T1, T2).Is(T3)
849 850 851 852
    // This does NOT hold.  For example:
    // Range(-2^33, -2^33) <= OtherNumber
    // Range(2^33, 2^33) <= OtherNumber
    // Range(-2^33, 2^33) </= OtherNumber
853
    /*
854 855 856
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
857 858 859 860
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type union12 = T.Union(type1, type2);
861
          CHECK(!(type1.Is(type3) && type2.Is(type3)) || union12.Is(type3));
862 863 864
        }
      }
    }
865 866
    */
  }
867

868
  void Union3() {
869
    // Monotonicity: T1.Is(T2) or T1.Is(T3) implies T1.Is(Union(T2, T3))
870
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
871
      HandleScope scope(isolate);
872
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
873
        for (TypeIterator it3 = it2; it3 != T.types.end(); ++it3) {
874 875 876 877
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type union23 = T.Union(type2, type3);
878
          CHECK(!(type1.Is(type2) || type1.Is(type3)) || type1.Is(union23));
879 880 881
        }
      }
    }
882
  }
883

884
  void Union4() {
885 886
    // Constant-constant
    CheckSub(T.Union(T.ObjectConstant1, T.ObjectConstant2), T.Object);
887 888
    CheckOverlap(T.Union(T.ObjectConstant1, T.ArrayConstant), T.OtherObject);
    CheckOverlap(T.Union(T.ObjectConstant1, T.ArrayConstant), T.OtherObject);
889
    CheckDisjoint(
890
        T.Union(T.ObjectConstant1, T.ArrayConstant), T.Number);
891 892 893 894

    // Bitset-constant
    CheckSub(
        T.Union(T.ObjectConstant1, T.Signed32), T.Union(T.Object, T.Number));
895 896
    CheckSub(T.Union(T.ObjectConstant1, T.OtherObject), T.Object);
    CheckUnordered(T.Union(T.ObjectConstant1, T.String), T.OtherObject);
897 898
    CheckOverlap(T.Union(T.ObjectConstant1, T.String), T.Object);
    CheckDisjoint(T.Union(T.ObjectConstant1, T.String), T.Number);
899 900 901 902 903 904 905 906

    // Constant-union
    CheckEqual(
        T.Union(
            T.ObjectConstant1, T.Union(T.ObjectConstant1, T.ObjectConstant2)),
        T.Union(T.ObjectConstant2, T.ObjectConstant1));
    CheckEqual(
        T.Union(
907
            T.Union(T.ArrayConstant, T.ObjectConstant2), T.ObjectConstant1),
908
        T.Union(
909
            T.ObjectConstant2, T.Union(T.ArrayConstant, T.ObjectConstant1)));
910 911 912 913 914 915 916

    // Union-union
    CheckEqual(
        T.Union(
            T.Union(T.ObjectConstant2, T.ObjectConstant1),
            T.Union(T.ObjectConstant1, T.ObjectConstant2)),
        T.Union(T.ObjectConstant2, T.ObjectConstant1));
917
  }
918

919
  void Intersect() {
920 921
    // Identity: Intersect(T, Any) = T
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
922 923
      Type type = *it;
      Type intersect_type = T.Intersect(type, T.Any);
924 925
      CheckEqual(intersect_type, type);
    }
926

927 928
    // Domination: Intersect(T, None) = None
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
929 930
      Type type = *it;
      Type intersect_type = T.Intersect(type, T.None);
931 932
      CheckEqual(intersect_type, T.None);
    }
933

934 935
    // Idempotence: Intersect(T, T) = T
    for (TypeIterator it = T.types.begin(); it != T.types.end(); ++it) {
936 937
      Type type = *it;
      Type intersect_type = T.Intersect(type, type);
938 939
      CheckEqual(intersect_type, type);
    }
940

941 942 943
    // Commutativity: Intersect(T1, T2) = Intersect(T2, T1)
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
944 945 946 947
        Type type1 = *it1;
        Type type2 = *it2;
        Type intersect12 = T.Intersect(type1, type2);
        Type intersect21 = T.Intersect(type2, type1);
948 949 950
        CheckEqual(intersect12, intersect21);
      }
    }
951

952
    // Lower Boundedness: T1.Is(T2) implies Intersect(T1, T2) = T1
953 954
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
955 956 957
        Type type1 = *it1;
        Type type2 = *it2;
        Type intersect12 = T.Intersect(type1, type2);
958
        if (type1.Is(type2)) CheckEqual(intersect12, type1);
959 960 961
      }
    }

962
    // Monotonicity: T1.Is(T2) and T1.Is(T3) implies T1.Is(Intersect(T2, T3))
963
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
964
      HandleScope scope(isolate);
965 966
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
967 968 969 970
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type intersect23 = T.Intersect(type2, type3);
971
          CHECK(!(type1.Is(type2) && type1.Is(type3)) || type1.Is(intersect23));
972 973 974 975
        }
      }
    }

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    // Constant-union
    CheckEqual(
        T.Intersect(
            T.ObjectConstant1, T.Union(T.ObjectConstant1, T.ObjectConstant2)),
        T.ObjectConstant1);
    CheckEqual(
        T.Intersect(T.SmiConstant, T.Union(T.Number, T.ObjectConstant2)),
        T.SmiConstant);

    // Union-union
    CheckEqual(
        T.Intersect(
            T.Union(T.ObjectConstant2, T.ObjectConstant1),
            T.Union(T.ObjectConstant1, T.ObjectConstant2)),
        T.Union(T.ObjectConstant2, T.ObjectConstant1));
  }
992

993
  void Distributivity() {
994
    // Union(T1, Intersect(T2, T3)) = Intersect(Union(T1, T2), Union(T1, T3))
995 996 997 998 999
    // This does NOT hold.  For example:
    // Untagged \/ (Untagged /\ Class(../Tagged)) = Untagged \/ Class(../Tagged)
    // (Untagged \/ Untagged) /\ (Untagged \/ Class(../Tagged)) =
    // Untagged /\ (Untagged \/ Class(../Tagged)) = Untagged
    // because Untagged <= Untagged \/ Class(../Tagged)
1000
    /*
1001 1002 1003
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
1004 1005 1006 1007 1008 1009 1010 1011
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type union12 = T.Union(type1, type2);
          Type union13 = T.Union(type1, type3);
          Type intersect23 = T.Intersect(type2, type3);
          Type union1_23 = T.Union(type1, intersect23);
          Type intersect12_13 = T.Intersect(union12, union13);
1012 1013 1014 1015
          CHECK(Equal(union1_23, intersect12_13));
        }
      }
    }
1016
    */
1017 1018

    // Intersect(T1, Union(T2, T3)) = Union(Intersect(T1, T2), Intersect(T1,T3))
1019 1020 1021 1022
    // This does NOT hold.  For example:
    // Untagged /\ (Untagged \/ Class(../Tagged)) = Untagged
    // (Untagged /\ Untagged) \/ (Untagged /\ Class(../Tagged)) =
    // Untagged \/ Class(../Tagged)
1023
    /*
1024 1025 1026
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
      for (TypeIterator it2 = T.types.begin(); it2 != T.types.end(); ++it2) {
        for (TypeIterator it3 = T.types.begin(); it3 != T.types.end(); ++it3) {
1027 1028 1029 1030 1031 1032 1033 1034
          Type type1 = *it1;
          Type type2 = *it2;
          Type type3 = *it3;
          Type intersect12 = T.Intersect(type1, type2);
          Type intersect13 = T.Intersect(type1, type3);
          Type union23 = T.Union(type2, type3);
          Type intersect1_23 = T.Intersect(type1, union23);
          Type union12_13 = T.Union(intersect12, intersect13);
1035 1036 1037 1038
          CHECK(Equal(intersect1_23, union12_13));
        }
      }
    }
1039
    */
1040 1041
  }

1042 1043 1044
  void GetRange() {
    // GetRange(Range(a, b)) = Range(a, b).
    for (TypeIterator it1 = T.types.begin(); it1 != T.types.end(); ++it1) {
1045
      Type type1 = *it1;
1046 1047 1048 1049
      if (type1.IsRange()) {
        const RangeType* range = type1.GetRange().AsRange();
        CHECK(type1.Min() == range->Min());
        CHECK(type1.Max() == range->Max());
1050 1051 1052
      }
    }
  }
1053 1054
};

bmeurer's avatar
bmeurer committed
1055
}  // namespace
1056

bmeurer's avatar
bmeurer committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
TEST(IsSomeType) { Tests().IsSomeType(); }
TEST(BitsetType) { Tests().Bitset(); }
TEST(ConstantType) { Tests().Constant(); }
TEST(RangeType) { Tests().Range(); }
TEST(MinMax) { Tests().MinMax(); }
TEST(BitsetGlb) { Tests().BitsetGlb(); }
TEST(BitsetLub) { Tests().BitsetLub(); }
TEST(Is1) { Tests().Is1(); }
TEST(Is2) { Tests().Is2(); }
TEST(Maybe) { Tests().Maybe(); }
TEST(Union1) { Tests().Union1(); }
TEST(Union2) { Tests().Union2(); }
TEST(Union3) { Tests().Union3(); }
TEST(Union4) { Tests().Union4(); }
TEST(Intersect) { Tests().Intersect(); }
TEST(Distributivity) { Tests().Distributivity(); }
TEST(GetRange) { Tests().GetRange(); }
1074 1075 1076 1077

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