typer.cc 67.3 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/base/flags.h"
6
#include "src/bootstrapper.h"
7
#include "src/compiler/graph-reducer.h"
8 9 10 11
#include "src/compiler/js-operator.h"
#include "src/compiler/node.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/simplified-operator.h"
12
#include "src/compiler/typer.h"
13 14 15 16 17

namespace v8 {
namespace internal {
namespace compiler {

18 19 20 21 22 23 24 25 26 27
#define NATIVE_TYPES(V) \
  V(Int8)               \
  V(Uint8)              \
  V(Int16)              \
  V(Uint16)             \
  V(Int32)              \
  V(Uint32)             \
  V(Float32)            \
  V(Float64)

28 29 30 31 32 33 34
enum LazyCachedType {
  kNumberFunc0,
  kNumberFunc1,
  kNumberFunc2,
  kImulFunc,
  kClz32Func,
  kArrayBufferFunc,
35 36 37 38 39
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
  k##Type, k##Type##Array, k##Type##ArrayFunc,
  TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
      kNumLazyCachedTypes
40 41 42 43 44
};


// Constructs and caches types lazily.
// TODO(turbofan): these types could be globally cached or cached per isolate.
45
class LazyTypeCache final : public ZoneObject {
46
 public:
47 48
  explicit LazyTypeCache(Isolate* isolate, Zone* zone)
      : isolate_(isolate), zone_(zone) {
49 50 51 52 53 54 55 56 57 58
    memset(cache_, 0, sizeof(cache_));
  }

  inline Type* Get(LazyCachedType type) {
    int index = static_cast<int>(type);
    DCHECK(index < kNumLazyCachedTypes);
    if (cache_[index] == NULL) cache_[index] = Create(type);
    return cache_[index];
  }

59
 private:
60 61
  Type* Create(LazyCachedType type) {
    switch (type) {
62
      case kInt8:
63
        return CreateNative(CreateRange<int8_t>(), Type::UntaggedSigned8());
64
      case kUint8:
65
        return CreateNative(CreateRange<uint8_t>(), Type::UntaggedUnsigned8());
66
      case kInt16:
67
        return CreateNative(CreateRange<int16_t>(), Type::UntaggedSigned16());
68
      case kUint16:
69 70
        return CreateNative(CreateRange<uint16_t>(),
                            Type::UntaggedUnsigned16());
71
      case kInt32:
72
        return CreateNative(Type::Signed32(), Type::UntaggedSigned32());
73
      case kUint32:
74
        return CreateNative(Type::Unsigned32(), Type::UntaggedUnsigned32());
75 76 77 78
      case kFloat32:
        return CreateNative(Type::Number(), Type::UntaggedFloat32());
      case kFloat64:
        return CreateNative(Type::Number(), Type::UntaggedFloat64());
79 80
      case kUint8Clamped:
        return Get(kUint8);
81 82 83 84 85
      case kNumberFunc0:
        return Type::Function(Type::Number(), zone());
      case kNumberFunc1:
        return Type::Function(Type::Number(), Type::Number(), zone());
      case kNumberFunc2:
86
        return Type::Function(Type::Number(), Type::Number(), Type::Number(),
87 88
                              zone());
      case kImulFunc:
89
        return Type::Function(Type::Signed32(), Type::Integral32(),
90 91 92 93
                              Type::Integral32(), zone());
      case kClz32Func:
        return Type::Function(CreateRange(0, 32), Type::Number(), zone());
      case kArrayBufferFunc:
94
        return Type::Function(Type::Object(zone()), Type::Unsigned32(), zone());
95 96 97 98
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
  case k##Type##Array:                                  \
    return CreateArray(Get(k##Type));                   \
  case k##Type##ArrayFunc:                              \
99
    return CreateArrayFunction(Get(k##Type##Array));
100 101
        TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
102
      case kNumLazyCachedTypes:
103 104 105 106 107 108
        break;
    }
    UNREACHABLE();
    return NULL;
  }

109 110 111 112 113 114 115
  Type* CreateArray(Type* element) const {
    return Type::Array(element, zone());
  }

  Type* CreateArrayFunction(Type* array) const {
    Type* arg1 = Type::Union(Type::Unsigned32(), Type::Object(), zone());
    Type* arg2 = Type::Union(Type::Unsigned32(), Type::Undefined(), zone());
116
    Type* arg3 = arg2;
117 118 119 120 121 122 123 124 125 126 127
    return Type::Function(array, arg1, arg2, arg3, zone());
  }

  Type* CreateNative(Type* semantic, Type* representation) const {
    return Type::Intersect(semantic, representation, zone());
  }

  template <typename T>
  Type* CreateRange() const {
    return CreateRange(std::numeric_limits<T>::min(),
                       std::numeric_limits<T>::max());
128
  }
129 130

  Type* CreateRange(double min, double max) const {
131
    return Type::Range(min, max, zone());
132 133 134
  }

  Factory* factory() const { return isolate()->factory(); }
135
  Isolate* isolate() const { return isolate_; }
136 137 138
  Zone* zone() const { return zone_; }

  Type* cache_[kNumLazyCachedTypes];
139
  Isolate* isolate_;
140
  Zone* zone_;
141 142
};

143

144
class Typer::Decorator final : public GraphDecorator {
145 146
 public:
  explicit Decorator(Typer* typer) : typer_(typer) {}
147
  void Decorate(Node* node, bool incomplete) final;
148 149 150 151 152 153

 private:
  Typer* typer_;
};


154 155 156
Typer::Typer(Isolate* isolate, Graph* graph, MaybeHandle<Context> context)
    : isolate_(isolate),
      graph_(graph),
157 158
      context_(context),
      decorator_(NULL),
159
      cache_(new (graph->zone()) LazyTypeCache(isolate, graph->zone())) {
160
  Zone* zone = this->zone();
161
  Factory* f = isolate->factory();
162

163 164
  Handle<Object> infinity = f->NewNumber(+V8_INFINITY);
  Handle<Object> minusinfinity = f->NewNumber(-V8_INFINITY);
165

166 167 168 169 170 171 172 173 174
  Type* number = Type::Number();
  Type* signed32 = Type::Signed32();
  Type* unsigned32 = Type::Unsigned32();
  Type* nan_or_minuszero = Type::Union(Type::NaN(), Type::MinusZero(), zone);
  Type* truncating_to_zero =
      Type::Union(Type::Union(Type::Constant(infinity, zone),
                              Type::Constant(minusinfinity, zone), zone),
                  nan_or_minuszero, zone);

175 176 177
  boolean_or_number = Type::Union(Type::Boolean(), Type::Number(), zone);
  undefined_or_null = Type::Union(Type::Undefined(), Type::Null(), zone);
  undefined_or_number = Type::Union(Type::Undefined(), Type::Number(), zone);
178 179
  singleton_false = Type::Constant(f->false_value(), zone);
  singleton_true = Type::Constant(f->true_value(), zone);
180 181
  singleton_zero = Type::Range(0.0, 0.0, zone);
  singleton_one = Type::Range(1.0, 1.0, zone);
182
  zero_or_one = Type::Union(singleton_zero, singleton_one, zone);
183 184 185
  zeroish = Type::Union(singleton_zero, nan_or_minuszero, zone);
  signed32ish = Type::Union(signed32, truncating_to_zero, zone);
  unsigned32ish = Type::Union(unsigned32, truncating_to_zero, zone);
186
  falsish = Type::Union(Type::Undetectable(),
187 188 189
                        Type::Union(Type::Union(singleton_false, zeroish, zone),
                                    undefined_or_null, zone),
                        zone);
190 191 192
  truish = Type::Union(
      singleton_true,
      Type::Union(Type::DetectableReceiver(), Type::Symbol(), zone), zone);
193
  integer = Type::Range(-V8_INFINITY, V8_INFINITY, zone);
194
  weakint = Type::Union(integer, nan_or_minuszero, zone);
195

196 197 198
  number_fun0_ = Type::Function(number, zone);
  number_fun1_ = Type::Function(number, number, zone);
  number_fun2_ = Type::Function(number, number, number, zone);
199

200
  weakint_fun1_ = Type::Function(weakint, number, zone);
201
  random_fun_ = Type::Function(Type::OrderedNumber(), zone);
202

203 204 205 206 207 208 209
  decorator_ = new (zone) Decorator(this);
  graph_->AddDecorator(decorator_);
}


Typer::~Typer() {
  graph_->RemoveDecorator(decorator_);
210 211 212
}


213
class Typer::Visitor : public Reducer {
214
 public:
215 216
  explicit Visitor(Typer* typer)
      : typer_(typer), weakened_nodes_(typer->zone()) {}
217

218
  Reduction Reduce(Node* node) override {
219 220 221 222 223 224 225 226 227 228 229 230
    if (node->op()->ValueOutputCount() == 0) return NoChange();
    switch (node->opcode()) {
#define DECLARE_CASE(x) \
  case IrOpcode::k##x:  \
    return UpdateBounds(node, TypeBinaryOp(node, x##Typer));
      JS_SIMPLE_BINOP_LIST(DECLARE_CASE)
#undef DECLARE_CASE

#define DECLARE_CASE(x) \
  case IrOpcode::k##x:  \
    return UpdateBounds(node, Type##x(node));
      DECLARE_CASE(Start)
231
      DECLARE_CASE(IfException)
232 233 234 235 236 237 238 239 240 241 242
      // VALUE_OP_LIST without JS_SIMPLE_BINOP_LIST:
      COMMON_OP_LIST(DECLARE_CASE)
      SIMPLIFIED_OP_LIST(DECLARE_CASE)
      MACHINE_OP_LIST(DECLARE_CASE)
      JS_SIMPLE_UNOP_LIST(DECLARE_CASE)
      JS_OBJECT_OP_LIST(DECLARE_CASE)
      JS_CONTEXT_OP_LIST(DECLARE_CASE)
      JS_OTHER_OP_LIST(DECLARE_CASE)
#undef DECLARE_CASE

#define DECLARE_CASE(x) case IrOpcode::k##x:
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
      DECLARE_CASE(Dead)
      DECLARE_CASE(Loop)
      DECLARE_CASE(Branch)
      DECLARE_CASE(IfTrue)
      DECLARE_CASE(IfFalse)
      DECLARE_CASE(IfSuccess)
      DECLARE_CASE(Switch)
      DECLARE_CASE(IfValue)
      DECLARE_CASE(IfDefault)
      DECLARE_CASE(Merge)
      DECLARE_CASE(Deoptimize)
      DECLARE_CASE(Return)
      DECLARE_CASE(OsrNormalEntry)
      DECLARE_CASE(OsrLoopEntry)
      DECLARE_CASE(Throw)
258 259 260 261 262 263 264
      DECLARE_CASE(End)
#undef DECLARE_CASE
      break;
    }
    return NoChange();
  }

265 266
  Bounds TypeNode(Node* node) {
    switch (node->opcode()) {
267 268 269 270 271
#define DECLARE_CASE(x) \
      case IrOpcode::k##x: return TypeBinaryOp(node, x##Typer);
      JS_SIMPLE_BINOP_LIST(DECLARE_CASE)
#undef DECLARE_CASE

272
#define DECLARE_CASE(x) case IrOpcode::k##x: return Type##x(node);
273
      DECLARE_CASE(Start)
274
      DECLARE_CASE(IfException)
275 276 277 278 279 280 281 282
      // VALUE_OP_LIST without JS_SIMPLE_BINOP_LIST:
      COMMON_OP_LIST(DECLARE_CASE)
      SIMPLIFIED_OP_LIST(DECLARE_CASE)
      MACHINE_OP_LIST(DECLARE_CASE)
      JS_SIMPLE_UNOP_LIST(DECLARE_CASE)
      JS_OBJECT_OP_LIST(DECLARE_CASE)
      JS_CONTEXT_OP_LIST(DECLARE_CASE)
      JS_OTHER_OP_LIST(DECLARE_CASE)
283 284 285
#undef DECLARE_CASE

#define DECLARE_CASE(x) case IrOpcode::k##x:
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      DECLARE_CASE(Dead)
      DECLARE_CASE(Loop)
      DECLARE_CASE(Branch)
      DECLARE_CASE(IfTrue)
      DECLARE_CASE(IfFalse)
      DECLARE_CASE(IfSuccess)
      DECLARE_CASE(Switch)
      DECLARE_CASE(IfValue)
      DECLARE_CASE(IfDefault)
      DECLARE_CASE(Merge)
      DECLARE_CASE(Deoptimize)
      DECLARE_CASE(Return)
      DECLARE_CASE(OsrNormalEntry)
      DECLARE_CASE(OsrLoopEntry)
      DECLARE_CASE(Throw)
301
      DECLARE_CASE(End)
302 303 304
#undef DECLARE_CASE
      break;
    }
305 306
    UNREACHABLE();
    return Bounds();
307 308 309 310
  }

  Type* TypeConstant(Handle<Object> value);

311 312 313
 private:
  Typer* typer_;
  MaybeHandle<Context> context_;
314
  ZoneSet<NodeId> weakened_nodes_;
315

316
#define DECLARE_METHOD(x) inline Bounds Type##x(Node* node);
317
  DECLARE_METHOD(Start)
318
  DECLARE_METHOD(IfException)
319 320 321
  VALUE_OP_LIST(DECLARE_METHOD)
#undef DECLARE_METHOD

322
  Bounds BoundsOrNone(Node* node) {
323 324
    return NodeProperties::IsTyped(node) ? NodeProperties::GetBounds(node)
                                         : Bounds(Type::None());
325 326
  }

327 328 329 330 331
  Bounds Operand(Node* node, int i) {
    Node* operand_node = NodeProperties::GetValueInput(node, i);
    return BoundsOrNone(operand_node);
  }

332
  Bounds WrapContextBoundsForInput(Node* node);
333
  Type* Weaken(Node* node, Type* current_type, Type* previous_type);
334

335 336
  Zone* zone() { return typer_->zone(); }
  Isolate* isolate() { return typer_->isolate(); }
337 338
  Graph* graph() { return typer_->graph(); }
  MaybeHandle<Context> context() { return typer_->context(); }
339

340 341 342 343 344
  void SetWeakened(NodeId node_id) { weakened_nodes_.insert(node_id); }
  bool IsWeakened(NodeId node_id) {
    return weakened_nodes_.find(node_id) != weakened_nodes_.end();
  }

345 346 347 348 349 350
  typedef Type* (*UnaryTyperFun)(Type*, Typer* t);
  typedef Type* (*BinaryTyperFun)(Type*, Type*, Typer* t);

  Bounds TypeUnaryOp(Node* node, UnaryTyperFun);
  Bounds TypeBinaryOp(Node* node, BinaryTyperFun);

351 352 353 354 355 356 357 358
  enum ComparisonOutcomeFlags {
    kComparisonTrue = 1,
    kComparisonFalse = 2,
    kComparisonUndefined = 4
  };
  typedef base::Flags<ComparisonOutcomeFlags> ComparisonOutcome;

  static ComparisonOutcome Invert(ComparisonOutcome, Typer*);
359
  static Type* Invert(Type*, Typer*);
360
  static Type* FalsifyUndefined(ComparisonOutcome, Typer*);
361
  static Type* Rangify(Type*, Typer*);
362 363 364 365 366 367 368 369 370 371 372 373

  static Type* ToPrimitive(Type*, Typer*);
  static Type* ToBoolean(Type*, Typer*);
  static Type* ToNumber(Type*, Typer*);
  static Type* ToString(Type*, Typer*);
  static Type* NumberToInt32(Type*, Typer*);
  static Type* NumberToUint32(Type*, Typer*);

  static Type* JSAddRanger(Type::RangeType*, Type::RangeType*, Typer*);
  static Type* JSSubtractRanger(Type::RangeType*, Type::RangeType*, Typer*);
  static Type* JSMultiplyRanger(Type::RangeType*, Type::RangeType*, Typer*);
  static Type* JSDivideRanger(Type::RangeType*, Type::RangeType*, Typer*);
374
  static Type* JSModulusRanger(Type::RangeType*, Type::RangeType*, Typer*);
375

376
  static ComparisonOutcome JSCompareTyper(Type*, Type*, Typer*);
377 378 379 380 381 382 383 384

#define DECLARE_METHOD(x) static Type* x##Typer(Type*, Type*, Typer*);
  JS_SIMPLE_BINOP_LIST(DECLARE_METHOD)
#undef DECLARE_METHOD

  static Type* JSUnaryNotTyper(Type*, Typer*);
  static Type* JSLoadPropertyTyper(Type*, Type*, Typer*);
  static Type* JSCallFunctionTyper(Type*, Typer*);
385

386 387 388 389
  Reduction UpdateBounds(Node* node, Bounds current) {
    if (NodeProperties::IsTyped(node)) {
      // Widen the bounds of a previously typed node.
      Bounds previous = NodeProperties::GetBounds(node);
390 391
      if (node->opcode() == IrOpcode::kPhi) {
        // Speed up termination in the presence of range types:
392 393
        current.upper = Weaken(node, current.upper, previous.upper);
        current.lower = Weaken(node, current.lower, previous.lower);
394
      }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

      // Types should not get less precise.
      DCHECK(previous.lower->Is(current.lower));
      DCHECK(previous.upper->Is(current.upper));

      NodeProperties::SetBounds(node, current);
      if (!(previous.Narrows(current) && current.Narrows(previous))) {
        // If something changed, revisit all uses.
        return Changed(node);
      }
      return NoChange();
    } else {
      // No previous type, simply update the bounds.
      NodeProperties::SetBounds(node, current);
      return Changed(node);
410 411 412 413 414
    }
  }
};


415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
void Typer::Run() {
  {
    // TODO(titzer): this is a hack. Reset types for interior nodes first.
    NodeDeque deque(zone());
    NodeMarker<bool> marked(graph(), 2);
    deque.push_front(graph()->end());
    marked.Set(graph()->end(), true);
    while (!deque.empty()) {
      Node* node = deque.front();
      deque.pop_front();
      // TODO(titzer): there shouldn't be a need to retype constants.
      if (node->op()->ValueOutputCount() > 0)
        NodeProperties::RemoveBounds(node);
      for (Node* input : node->inputs()) {
        if (!marked.Get(input)) {
          marked.Set(input, true);
          deque.push_back(input);
432 433 434
        }
      }
    }
435
  }
436

437 438 439 440
  Visitor visitor(this);
  GraphReducer graph_reducer(graph(), zone());
  graph_reducer.AddReducer(&visitor);
  graph_reducer.ReduceGraph();
441 442 443
}


444 445
void Typer::Decorator::Decorate(Node* node, bool incomplete) {
  if (incomplete) return;
446
  if (node->op()->ValueOutputCount() > 0) {
447 448 449 450 451 452 453 454 455 456 457 458
    // Only eagerly type-decorate nodes with known input types.
    // Other cases will generally require a proper fixpoint iteration with Run.
    bool is_typed = NodeProperties::IsTyped(node);
    if (is_typed || NodeProperties::AllValueInputsAreTyped(node)) {
      Visitor typing(typer_);
      Bounds bounds = typing.TypeNode(node);
      if (is_typed) {
        bounds =
          Bounds::Both(bounds, NodeProperties::GetBounds(node), typer_->zone());
      }
      NodeProperties::SetBounds(node, bounds);
    }
459 460 461 462
  }
}


463 464 465 466 467 468 469 470
// -----------------------------------------------------------------------------

// Helper functions that lift a function f on types to a function on bounds,
// and uses that to type the given node.  Note that f is never called with None
// as an argument.


Bounds Typer::Visitor::TypeUnaryOp(Node* node, UnaryTyperFun f) {
471
  Bounds input = Operand(node, 0);
472 473 474 475 476 477 478 479
  Type* upper =
      input.upper->IsInhabited() ? f(input.upper, typer_) : Type::None();
  Type* lower = input.lower->IsInhabited()
                    ? ((input.lower == input.upper || upper->IsConstant())
                           ? upper  // TODO(neis): Extend this to Range(x,x),
                                    // NaN, MinusZero, ...?
                           : f(input.lower, typer_))
                    : Type::None();
480 481 482 483 484 485
  // TODO(neis): Figure out what to do with lower bound.
  return Bounds(lower, upper);
}


Bounds Typer::Visitor::TypeBinaryOp(Node* node, BinaryTyperFun f) {
486 487
  Bounds left = Operand(node, 0);
  Bounds right = Operand(node, 1);
488 489 490 491 492 493 494 495 496 497
  Type* upper = left.upper->IsInhabited() && right.upper->IsInhabited()
                    ? f(left.upper, right.upper, typer_)
                    : Type::None();
  Type* lower =
      left.lower->IsInhabited() && right.lower->IsInhabited()
          ? (((left.lower == left.upper && right.lower == right.upper) ||
              upper->IsConstant())
                 ? upper
                 : f(left.lower, right.lower, typer_))
          : Type::None();
498 499 500 501 502 503
  // TODO(neis): Figure out what to do with lower bound.
  return Bounds(lower, upper);
}


Type* Typer::Visitor::Invert(Type* type, Typer* t) {
504 505
  DCHECK(type->Is(Type::Boolean()));
  DCHECK(type->IsInhabited());
506 507 508 509 510 511
  if (type->Is(t->singleton_false)) return t->singleton_true;
  if (type->Is(t->singleton_true)) return t->singleton_false;
  return type;
}


512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
Typer::Visitor::ComparisonOutcome Typer::Visitor::Invert(
    ComparisonOutcome outcome, Typer* t) {
  ComparisonOutcome result(0);
  if ((outcome & kComparisonUndefined) != 0) result |= kComparisonUndefined;
  if ((outcome & kComparisonTrue) != 0) result |= kComparisonFalse;
  if ((outcome & kComparisonFalse) != 0) result |= kComparisonTrue;
  return result;
}


Type* Typer::Visitor::FalsifyUndefined(ComparisonOutcome outcome, Typer* t) {
  if ((outcome & kComparisonFalse) != 0 ||
      (outcome & kComparisonUndefined) != 0) {
    return (outcome & kComparisonTrue) != 0 ? Type::Boolean()
                                            : t->singleton_false;
  }
  // Type should be non empty, so we know it should be true.
  DCHECK((outcome & kComparisonTrue) != 0);
  return t->singleton_true;
531 532 533
}


534 535
Type* Typer::Visitor::Rangify(Type* type, Typer* t) {
  if (type->IsRange()) return type;        // Shortcut.
536 537 538 539 540 541 542 543 544 545 546
  if (!type->Is(t->integer) && !type->Is(Type::Integral32())) {
    return type;  // Give up on non-integer types.
  }
  double min = type->Min();
  double max = type->Max();
  // Handle the degenerate case of empty bitset types (such as
  // OtherUnsigned31 and OtherSigned32 on 64-bit architectures).
  if (std::isnan(min)) {
    DCHECK(std::isnan(max));
    return type;
  }
547
  return Type::Range(min, max, t->zone());
548 549 550
}


551 552 553 554 555 556 557 558 559 560 561 562 563 564
// Type conversion.


Type* Typer::Visitor::ToPrimitive(Type* type, Typer* t) {
  if (type->Is(Type::Primitive()) && !type->Maybe(Type::Receiver())) {
    return type;
  }
  return Type::Primitive();
}


Type* Typer::Visitor::ToBoolean(Type* type, Typer* t) {
  if (type->Is(Type::Boolean())) return type;
  if (type->Is(t->falsish)) return t->singleton_false;
565 566
  if (type->Is(t->truish)) return t->singleton_true;
  if (type->Is(Type::PlainNumber()) && (type->Max() < 0 || 0 < type->Min())) {
567 568 569 570 571 572 573 574
    return t->singleton_true;  // Ruled out nan, -0 and +0.
  }
  return Type::Boolean();
}


Type* Typer::Visitor::ToNumber(Type* type, Typer* t) {
  if (type->Is(Type::Number())) return type;
575
  if (type->Is(Type::Null())) return t->singleton_zero;
576
  if (type->Is(Type::Undefined())) return Type::NaN();
577 578 579 580 581 582 583
  if (type->Is(t->undefined_or_null)) {
    return Type::Union(Type::NaN(), t->singleton_zero, t->zone());
  }
  if (type->Is(t->undefined_or_number)) {
    return Type::Union(Type::Intersect(type, Type::Number(), t->zone()),
                       Type::NaN(), t->zone());
  }
584 585 586
  if (type->Is(t->singleton_false)) return t->singleton_zero;
  if (type->Is(t->singleton_true)) return t->singleton_one;
  if (type->Is(Type::Boolean())) return t->zero_or_one;
587 588 589 590
  if (type->Is(t->boolean_or_number)) {
    return Type::Union(Type::Intersect(type, Type::Number(), t->zone()),
                       t->zero_or_one, t->zone());
  }
591 592 593 594 595 596 597 598 599 600 601 602 603 604
  return Type::Number();
}


Type* Typer::Visitor::ToString(Type* type, Typer* t) {
  if (type->Is(Type::String())) return type;
  return Type::String();
}


Type* Typer::Visitor::NumberToInt32(Type* type, Typer* t) {
  // TODO(neis): DCHECK(type->Is(Type::Number()));
  if (type->Is(Type::Signed32())) return type;
  if (type->Is(t->zeroish)) return t->singleton_zero;
605 606 607 608
  if (type->Is(t->signed32ish)) {
    return Type::Intersect(Type::Union(type, t->singleton_zero, t->zone()),
                           Type::Signed32(), t->zone());
  }
609 610 611 612 613 614 615 616
  return Type::Signed32();
}


Type* Typer::Visitor::NumberToUint32(Type* type, Typer* t) {
  // TODO(neis): DCHECK(type->Is(Type::Number()));
  if (type->Is(Type::Unsigned32())) return type;
  if (type->Is(t->zeroish)) return t->singleton_zero;
617 618 619 620
  if (type->Is(t->unsigned32ish)) {
    return Type::Intersect(Type::Union(type, t->singleton_zero, t->zone()),
                           Type::Unsigned32(), t->zone());
  }
621 622 623 624
  return Type::Unsigned32();
}


625 626 627 628 629
// -----------------------------------------------------------------------------


// Control operators.

630

631
Bounds Typer::Visitor::TypeStart(Node* node) {
632
  return Bounds(Type::None(zone()), Type::Internal(zone()));
633 634 635
}


636 637 638 639 640
Bounds Typer::Visitor::TypeIfException(Node* node) {
  return Bounds::Unbounded(zone());
}


641
// Common operators.
642

643

644 645 646 647 648
Bounds Typer::Visitor::TypeAlways(Node* node) {
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
}


649 650 651 652 653
Bounds Typer::Visitor::TypeParameter(Node* node) {
  return Bounds::Unbounded(zone());
}


654 655
Bounds Typer::Visitor::TypeOsrValue(Node* node) {
  if (node->InputAt(0)->opcode() == IrOpcode::kOsrLoopEntry) {
656 657
    // Before deconstruction, OSR values have type {None} to avoid polluting
    // the types of phis and other nodes in the graph.
658 659
    return Bounds(Type::None(), Type::None());
  }
660 661 662 663 664
  if (NodeProperties::IsTyped(node)) {
    // After deconstruction, OSR values may have had a type explicitly set.
    return NodeProperties::GetBounds(node);
  }
  // Otherwise, be conservative.
665 666 667 668
  return Bounds::Unbounded(zone());
}


669
Bounds Typer::Visitor::TypeInt32Constant(Node* node) {
670
  double number = OpParameter<int32_t>(node);
671
  return Bounds(Type::Intersect(
672
      Type::Range(number, number, zone()), Type::UntaggedSigned32(), zone()));
673 674 675 676
}


Bounds Typer::Visitor::TypeInt64Constant(Node* node) {
677
  // TODO(rossberg): This actually seems to be a PointerConstant so far...
678
  return Bounds(Type::Internal());  // TODO(rossberg): Add int64 bitset type?
679 680 681
}


682
Bounds Typer::Visitor::TypeFloat32Constant(Node* node) {
683 684 685
  return Bounds(Type::Intersect(
      Type::Of(OpParameter<float>(node), zone()),
      Type::UntaggedFloat32(), zone()));
686 687 688
}


689
Bounds Typer::Visitor::TypeFloat64Constant(Node* node) {
690 691 692
  return Bounds(Type::Intersect(
      Type::Of(OpParameter<double>(node), zone()),
      Type::UntaggedFloat64(), zone()));
693 694 695 696
}


Bounds Typer::Visitor::TypeNumberConstant(Node* node) {
697
  Factory* f = isolate()->factory();
698 699
  return Bounds(Type::Constant(
      f->NewNumber(OpParameter<double>(node)), zone()));
700 701 702 703
}


Bounds Typer::Visitor::TypeHeapConstant(Node* node) {
svenpanne's avatar
svenpanne committed
704
  return Bounds(TypeConstant(OpParameter<Unique<HeapObject> >(node).handle()));
705 706 707 708
}


Bounds Typer::Visitor::TypeExternalConstant(Node* node) {
709
  return Bounds(Type::None(zone()), Type::Internal(zone()));
710 711 712
}


713 714 715 716 717
Bounds Typer::Visitor::TypeSelect(Node* node) {
  return Bounds::Either(Operand(node, 1), Operand(node, 2), zone());
}


718
Bounds Typer::Visitor::TypePhi(Node* node) {
719
  int arity = node->op()->ValueInputCount();
720
  Bounds bounds = Operand(node, 0);
721
  for (int i = 1; i < arity; ++i) {
722
    bounds = Bounds::Either(bounds, Operand(node, i), zone());
723 724 725 726 727 728
  }
  return bounds;
}


Bounds Typer::Visitor::TypeEffectPhi(Node* node) {
729 730
  UNREACHABLE();
  return Bounds();
731 732 733
}


734 735 736 737 738 739
Bounds Typer::Visitor::TypeEffectSet(Node* node) {
  UNREACHABLE();
  return Bounds();
}


740
Bounds Typer::Visitor::TypeValueEffect(Node* node) {
741 742
  UNREACHABLE();
  return Bounds();
743 744 745
}


746
Bounds Typer::Visitor::TypeFinish(Node* node) {
747
  return Operand(node, 0);
748
}
749 750


751
Bounds Typer::Visitor::TypeFrameState(Node* node) {
752
  // TODO(rossberg): Ideally FrameState wouldn't have a value output.
753
  return Bounds(Type::None(zone()), Type::Internal(zone()));
754 755 756
}


757
Bounds Typer::Visitor::TypeStateValues(Node* node) {
758
  return Bounds(Type::None(zone()), Type::Internal(zone()));
759 760 761
}


762 763 764 765 766
Bounds Typer::Visitor::TypeTypedStateValues(Node* node) {
  return Bounds(Type::None(zone()), Type::Internal(zone()));
}


767 768 769 770 771 772 773 774 775 776 777 778 779
Bounds Typer::Visitor::TypeCall(Node* node) {
  return Bounds::Unbounded(zone());
}


Bounds Typer::Visitor::TypeProjection(Node* node) {
  // TODO(titzer): use the output type of the input to determine the bounds.
  return Bounds::Unbounded(zone());
}


// JS comparison operators.

780 781 782 783 784 785 786 787 788

Type* Typer::Visitor::JSEqualTyper(Type* lhs, Type* rhs, Typer* t) {
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return t->singleton_false;
  if (lhs->Is(t->undefined_or_null) && rhs->Is(t->undefined_or_null)) {
    return t->singleton_true;
  }
  if (lhs->Is(Type::Number()) && rhs->Is(Type::Number()) &&
      (lhs->Max() < rhs->Min() || lhs->Min() > rhs->Max())) {
      return t->singleton_false;
789
  }
790 791 792 793 794 795 796 797
  if (lhs->IsConstant() && rhs->Is(lhs)) {
    // Types are equal and are inhabited only by a single semantic value,
    // which is not nan due to the earlier check.
    // TODO(neis): Extend this to Range(x,x), MinusZero, ...?
    return t->singleton_true;
  }
  return Type::Boolean();
}
798 799


800 801 802
Type* Typer::Visitor::JSNotEqualTyper(Type* lhs, Type* rhs, Typer* t) {
  return Invert(JSEqualTyper(lhs, rhs, t), t);
}
803

804 805 806 807 808 809 810 811 812 813

static Type* JSType(Type* type) {
  if (type->Is(Type::Boolean())) return Type::Boolean();
  if (type->Is(Type::String())) return Type::String();
  if (type->Is(Type::Number())) return Type::Number();
  if (type->Is(Type::Undefined())) return Type::Undefined();
  if (type->Is(Type::Null())) return Type::Null();
  if (type->Is(Type::Symbol())) return Type::Symbol();
  if (type->Is(Type::Receiver())) return Type::Receiver();  // JS "Object"
  return Type::Any();
814 815 816
}


817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
Type* Typer::Visitor::JSStrictEqualTyper(Type* lhs, Type* rhs, Typer* t) {
  if (!JSType(lhs)->Maybe(JSType(rhs))) return t->singleton_false;
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return t->singleton_false;
  if (lhs->Is(Type::Number()) && rhs->Is(Type::Number()) &&
      (lhs->Max() < rhs->Min() || lhs->Min() > rhs->Max())) {
      return t->singleton_false;
  }
  if (lhs->IsConstant() && rhs->Is(lhs)) {
    // Types are equal and are inhabited only by a single semantic value,
    // which is not nan due to the earlier check.
    return t->singleton_true;
  }
  return Type::Boolean();
}


Type* Typer::Visitor::JSStrictNotEqualTyper(Type* lhs, Type* rhs, Typer* t) {
  return Invert(JSStrictEqualTyper(lhs, rhs, t), t);
}


// The EcmaScript specification defines the four relational comparison operators
// (<, <=, >=, >) with the help of a single abstract one.  It behaves like <
// but returns undefined when the inputs cannot be compared.
// We implement the typing analogously.
842 843 844
Typer::Visitor::ComparisonOutcome Typer::Visitor::JSCompareTyper(Type* lhs,
                                                                 Type* rhs,
                                                                 Typer* t) {
845 846 847
  lhs = ToPrimitive(lhs, t);
  rhs = ToPrimitive(rhs, t);
  if (lhs->Maybe(Type::String()) && rhs->Maybe(Type::String())) {
848 849
    return ComparisonOutcome(kComparisonTrue) |
           ComparisonOutcome(kComparisonFalse);
850 851 852
  }
  lhs = ToNumber(lhs, t);
  rhs = ToNumber(rhs, t);
853 854 855 856 857

  // Shortcut for NaNs.
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return kComparisonUndefined;

  ComparisonOutcome result;
858
  if (lhs->IsConstant() && rhs->Is(lhs)) {
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    // Types are equal and are inhabited only by a single semantic value.
    result = kComparisonFalse;
  } else if (lhs->Min() >= rhs->Max()) {
    result = kComparisonFalse;
  } else if (lhs->Max() < rhs->Min()) {
    result = kComparisonTrue;
  } else {
    // We cannot figure out the result, return both true and false. (We do not
    // have to return undefined because that cannot affect the result of
    // FalsifyUndefined.)
    return ComparisonOutcome(kComparisonTrue) |
           ComparisonOutcome(kComparisonFalse);
  }
  // Add the undefined if we could see NaN.
  if (lhs->Maybe(Type::NaN()) || rhs->Maybe(Type::NaN())) {
    result |= kComparisonUndefined;
  }
  return result;
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
}


Type* Typer::Visitor::JSLessThanTyper(Type* lhs, Type* rhs, Typer* t) {
  return FalsifyUndefined(JSCompareTyper(lhs, rhs, t), t);
}


Type* Typer::Visitor::JSGreaterThanTyper(Type* lhs, Type* rhs, Typer* t) {
  return FalsifyUndefined(JSCompareTyper(rhs, lhs, t), t);
}


Type* Typer::Visitor::JSLessThanOrEqualTyper(Type* lhs, Type* rhs, Typer* t) {
  return FalsifyUndefined(Invert(JSCompareTyper(rhs, lhs, t), t), t);
}


Type* Typer::Visitor::JSGreaterThanOrEqualTyper(
    Type* lhs, Type* rhs, Typer* t) {
  return FalsifyUndefined(Invert(JSCompareTyper(lhs, rhs, t), t), t);
}


// JS bitwise operators.


Type* Typer::Visitor::JSBitwiseOrTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = NumberToInt32(ToNumber(lhs, t), t);
  rhs = NumberToInt32(ToNumber(rhs, t), t);
  double lmin = lhs->Min();
  double rmin = rhs->Min();
  double lmax = lhs->Max();
  double rmax = rhs->Max();
  // Or-ing any two values results in a value no smaller than their minimum.
  // Even no smaller than their maximum if both values are non-negative.
913 914 915 916 917 918 919 920 921 922 923 924 925 926
  double min =
      lmin >= 0 && rmin >= 0 ? std::max(lmin, rmin) : std::min(lmin, rmin);
  double max = Type::Signed32()->Max();

  // Or-ing with 0 is essentially a conversion to int32.
  if (rmin == 0 && rmax == 0) {
    min = lmin;
    max = lmax;
  }
  if (lmin == 0 && lmax == 0) {
    min = rmin;
    max = rmax;
  }

927 928 929
  if (lmax < 0 || rmax < 0) {
    // Or-ing two values of which at least one is negative results in a negative
    // value.
930
    max = std::min(max, -1.0);
931
  }
932
  return Type::Range(min, max, t->zone());
933 934 935 936 937 938 939 940 941 942 943
  // TODO(neis): Be precise for singleton inputs, here and elsewhere.
}


Type* Typer::Visitor::JSBitwiseAndTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = NumberToInt32(ToNumber(lhs, t), t);
  rhs = NumberToInt32(ToNumber(rhs, t), t);
  double lmin = lhs->Min();
  double rmin = rhs->Min();
  double lmax = lhs->Max();
  double rmax = rhs->Max();
944
  double min = Type::Signed32()->Min();
945 946
  // And-ing any two values results in a value no larger than their maximum.
  // Even no larger than their minimum if both values are non-negative.
947 948 949 950 951 952 953 954 955 956 957 958
  double max =
      lmin >= 0 && rmin >= 0 ? std::min(lmax, rmax) : std::max(lmax, rmax);
  // And-ing with a non-negative value x causes the result to be between
  // zero and x.
  if (lmin >= 0) {
    min = 0;
    max = std::min(max, lmax);
  }
  if (rmin >= 0) {
    min = 0;
    max = std::min(max, rmax);
  }
959
  return Type::Range(min, max, t->zone());
960 961 962
}


963 964 965 966 967 968 969 970 971
Type* Typer::Visitor::JSBitwiseXorTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = NumberToInt32(ToNumber(lhs, t), t);
  rhs = NumberToInt32(ToNumber(rhs, t), t);
  double lmin = lhs->Min();
  double rmin = rhs->Min();
  double lmax = lhs->Max();
  double rmax = rhs->Max();
  if ((lmin >= 0 && rmin >= 0) || (lmax < 0 && rmax < 0)) {
    // Xor-ing negative or non-negative values results in a non-negative value.
972
    return Type::Unsigned31();
973 974 975
  }
  if ((lmax < 0 && rmin >= 0) || (lmin >= 0 && rmax < 0)) {
    // Xor-ing a negative and a non-negative value results in a negative value.
976
    // TODO(jarin) Use a range here.
977
    return Type::Negative32();
978 979
  }
  return Type::Signed32();
980 981 982
}


983 984
Type* Typer::Visitor::JSShiftLeftTyper(Type* lhs, Type* rhs, Typer* t) {
  return Type::Signed32();
985 986 987
}


988 989
Type* Typer::Visitor::JSShiftRightTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = NumberToInt32(ToNumber(lhs, t), t);
990 991 992
  rhs = NumberToUint32(ToNumber(rhs, t), t);
  double min = kMinInt;
  double max = kMaxInt;
993 994
  if (lhs->Min() >= 0) {
    // Right-shifting a non-negative value cannot make it negative, nor larger.
995 996
    min = std::max(min, 0.0);
    max = std::min(max, lhs->Max());
997 998 999
    if (rhs->Min() > 0 && rhs->Max() <= 31) {
      max = static_cast<int>(max) >> static_cast<int>(rhs->Min());
    }
1000 1001 1002
  }
  if (lhs->Max() < 0) {
    // Right-shifting a negative value cannot make it non-negative, nor smaller.
1003 1004
    min = std::max(min, lhs->Min());
    max = std::min(max, -1.0);
1005 1006 1007
    if (rhs->Min() > 0 && rhs->Max() <= 31) {
      min = static_cast<int>(min) >> static_cast<int>(rhs->Min());
    }
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
  }
  if (rhs->Min() > 0 && rhs->Max() <= 31) {
    // Right-shifting by a positive value yields a small integer value.
    double shift_min = kMinInt >> static_cast<int>(rhs->Min());
    double shift_max = kMaxInt >> static_cast<int>(rhs->Min());
    min = std::max(min, shift_min);
    max = std::min(max, shift_max);
  }
  // TODO(jarin) Ideally, the following micro-optimization should be performed
  // by the type constructor.
  if (max != Type::Signed32()->Max() || min != Type::Signed32()->Min()) {
1019
    return Type::Range(min, max, t->zone());
1020 1021
  }
  return Type::Signed32();
1022 1023 1024
}


1025 1026 1027
Type* Typer::Visitor::JSShiftRightLogicalTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = NumberToUint32(ToNumber(lhs, t), t);
  // Logical right-shifting any value cannot make it larger.
1028
  return Type::Range(0.0, lhs->Max(), t->zone());
1029 1030 1031 1032 1033
}


// JS arithmetic operators.

1034

1035 1036 1037 1038 1039
// Returns the array's least element, ignoring NaN.
// There must be at least one non-NaN element.
// Any -0 is converted to 0.
static double array_min(double a[], size_t n) {
  DCHECK(n != 0);
1040
  double x = +V8_INFINITY;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  for (size_t i = 0; i < n; ++i) {
    if (!std::isnan(a[i])) {
      x = std::min(a[i], x);
    }
  }
  DCHECK(!std::isnan(x));
  return x == 0 ? 0 : x;  // -0 -> 0
}


// Returns the array's greatest element, ignoring NaN.
// There must be at least one non-NaN element.
// Any -0 is converted to 0.
static double array_max(double a[], size_t n) {
  DCHECK(n != 0);
1056
  double x = -V8_INFINITY;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  for (size_t i = 0; i < n; ++i) {
    if (!std::isnan(a[i])) {
      x = std::max(a[i], x);
    }
  }
  DCHECK(!std::isnan(x));
  return x == 0 ? 0 : x;  // -0 -> 0
}


Type* Typer::Visitor::JSAddRanger(Type::RangeType* lhs, Type::RangeType* rhs,
                                  Typer* t) {
  double results[4];
1070 1071 1072 1073
  results[0] = lhs->Min() + rhs->Min();
  results[1] = lhs->Min() + rhs->Max();
  results[2] = lhs->Max() + rhs->Min();
  results[3] = lhs->Max() + rhs->Max();
1074 1075 1076 1077 1078 1079 1080 1081 1082
  // Since none of the inputs can be -0, the result cannot be -0 either.
  // However, it can be nan (the sum of two infinities of opposite sign).
  // On the other hand, if none of the "results" above is nan, then the actual
  // result cannot be nan either.
  int nans = 0;
  for (int i = 0; i < 4; ++i) {
    if (std::isnan(results[i])) ++nans;
  }
  if (nans == 4) return Type::NaN();  // [-inf..-inf] + [inf..inf] or vice versa
1083 1084
  Type* range =
      Type::Range(array_min(results, 4), array_max(results, 4), t->zone());
1085 1086 1087 1088 1089 1090 1091 1092 1093
  return nans == 0 ? range : Type::Union(range, Type::NaN(), t->zone());
  // Examples:
  //   [-inf, -inf] + [+inf, +inf] = NaN
  //   [-inf, -inf] + [n, +inf] = [-inf, -inf] \/ NaN
  //   [-inf, +inf] + [n, +inf] = [-inf, +inf] \/ NaN
  //   [-inf, m] + [n, +inf] = [-inf, +inf] \/ NaN
}


1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
Type* Typer::Visitor::JSAddTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = ToPrimitive(lhs, t);
  rhs = ToPrimitive(rhs, t);
  if (lhs->Maybe(Type::String()) || rhs->Maybe(Type::String())) {
    if (lhs->Is(Type::String()) || rhs->Is(Type::String())) {
      return Type::String();
    } else {
      return Type::NumberOrString();
    }
  }
1104 1105
  lhs = Rangify(ToNumber(lhs, t), t);
  rhs = Rangify(ToNumber(rhs, t), t);
1106
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
1107 1108 1109
  if (lhs->IsRange() && rhs->IsRange()) {
    return JSAddRanger(lhs->AsRange(), rhs->AsRange(), t);
  }
1110 1111
  // TODO(neis): Deal with numeric bitsets here and elsewhere.
  return Type::Number();
1112 1113 1114
}


1115 1116 1117
Type* Typer::Visitor::JSSubtractRanger(Type::RangeType* lhs,
                                       Type::RangeType* rhs, Typer* t) {
  double results[4];
1118 1119 1120 1121
  results[0] = lhs->Min() - rhs->Min();
  results[1] = lhs->Min() - rhs->Max();
  results[2] = lhs->Max() - rhs->Min();
  results[3] = lhs->Max() - rhs->Max();
1122 1123 1124 1125 1126 1127 1128 1129 1130
  // Since none of the inputs can be -0, the result cannot be -0.
  // However, it can be nan (the subtraction of two infinities of same sign).
  // On the other hand, if none of the "results" above is nan, then the actual
  // result cannot be nan either.
  int nans = 0;
  for (int i = 0; i < 4; ++i) {
    if (std::isnan(results[i])) ++nans;
  }
  if (nans == 4) return Type::NaN();  // [inf..inf] - [inf..inf] (all same sign)
1131 1132
  Type* range =
      Type::Range(array_min(results, 4), array_max(results, 4), t->zone());
1133 1134 1135 1136 1137 1138 1139 1140 1141
  return nans == 0 ? range : Type::Union(range, Type::NaN(), t->zone());
  // Examples:
  //   [-inf, +inf] - [-inf, +inf] = [-inf, +inf] \/ NaN
  //   [-inf, -inf] - [-inf, -inf] = NaN
  //   [-inf, -inf] - [n, +inf] = [-inf, -inf] \/ NaN
  //   [m, +inf] - [-inf, n] = [-inf, +inf] \/ NaN
}


1142
Type* Typer::Visitor::JSSubtractTyper(Type* lhs, Type* rhs, Typer* t) {
1143 1144
  lhs = Rangify(ToNumber(lhs, t), t);
  rhs = Rangify(ToNumber(rhs, t), t);
1145
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
1146 1147 1148
  if (lhs->IsRange() && rhs->IsRange()) {
    return JSSubtractRanger(lhs->AsRange(), rhs->AsRange(), t);
  }
1149
  return Type::Number();
1150 1151 1152
}


1153 1154 1155
Type* Typer::Visitor::JSMultiplyRanger(Type::RangeType* lhs,
                                       Type::RangeType* rhs, Typer* t) {
  double results[4];
1156 1157 1158 1159
  double lmin = lhs->Min();
  double lmax = lhs->Max();
  double rmin = rhs->Min();
  double rmax = rhs->Max();
1160 1161 1162 1163 1164 1165 1166 1167 1168
  results[0] = lmin * rmin;
  results[1] = lmin * rmax;
  results[2] = lmax * rmin;
  results[3] = lmax * rmax;
  // If the result may be nan, we give up on calculating a precise type, because
  // the discontinuity makes it too complicated.  Note that even if none of the
  // "results" above is nan, the actual result may still be, so we have to do a
  // different check:
  bool maybe_nan = (lhs->Maybe(t->singleton_zero) &&
1169
                    (rmin == -V8_INFINITY || rmax == +V8_INFINITY)) ||
1170
                   (rhs->Maybe(t->singleton_zero) &&
1171
                    (lmin == -V8_INFINITY || lmax == +V8_INFINITY));
1172 1173 1174
  if (maybe_nan) return t->weakint;  // Giving up.
  bool maybe_minuszero = (lhs->Maybe(t->singleton_zero) && rmin < 0) ||
                         (rhs->Maybe(t->singleton_zero) && lmin < 0);
1175 1176
  Type* range =
      Type::Range(array_min(results, 4), array_max(results, 4), t->zone());
1177 1178 1179 1180 1181
  return maybe_minuszero ? Type::Union(range, Type::MinusZero(), t->zone())
                         : range;
}


1182
Type* Typer::Visitor::JSMultiplyTyper(Type* lhs, Type* rhs, Typer* t) {
1183 1184
  lhs = Rangify(ToNumber(lhs, t), t);
  rhs = Rangify(ToNumber(rhs, t), t);
1185
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
1186 1187 1188
  if (lhs->IsRange() && rhs->IsRange()) {
    return JSMultiplyRanger(lhs->AsRange(), rhs->AsRange(), t);
  }
1189
  return Type::Number();
1190 1191 1192
}


1193 1194 1195 1196
Type* Typer::Visitor::JSDivideTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = ToNumber(lhs, t);
  rhs = ToNumber(rhs, t);
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
1197 1198
  // Division is tricky, so all we do is try ruling out nan.
  // TODO(neis): try ruling out -0 as well?
1199 1200 1201 1202
  bool maybe_nan =
      lhs->Maybe(Type::NaN()) || rhs->Maybe(t->zeroish) ||
      ((lhs->Min() == -V8_INFINITY || lhs->Max() == +V8_INFINITY) &&
       (rhs->Min() == -V8_INFINITY || rhs->Max() == +V8_INFINITY));
1203
  return maybe_nan ? Type::Number() : Type::OrderedNumber();
1204 1205 1206
}


1207 1208
Type* Typer::Visitor::JSModulusRanger(Type::RangeType* lhs,
                                      Type::RangeType* rhs, Typer* t) {
1209 1210 1211 1212
  double lmin = lhs->Min();
  double lmax = lhs->Max();
  double rmin = rhs->Min();
  double rmax = rhs->Max();
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

  double labs = std::max(std::abs(lmin), std::abs(lmax));
  double rabs = std::max(std::abs(rmin), std::abs(rmax)) - 1;
  double abs = std::min(labs, rabs);
  bool maybe_minus_zero = false;
  double omin = 0;
  double omax = 0;
  if (lmin >= 0) {  // {lhs} positive.
    omin = 0;
    omax = abs;
  } else if (lmax <= 0) {  // {lhs} negative.
    omin = 0 - abs;
    omax = 0;
    maybe_minus_zero = true;
  } else {
    omin = 0 - abs;
    omax = abs;
    maybe_minus_zero = true;
  }

1233
  Type* result = Type::Range(omin, omax, t->zone());
1234 1235 1236 1237 1238 1239
  if (maybe_minus_zero)
    result = Type::Union(result, Type::MinusZero(), t->zone());
  return result;
}


1240 1241 1242 1243
Type* Typer::Visitor::JSModulusTyper(Type* lhs, Type* rhs, Typer* t) {
  lhs = ToNumber(lhs, t);
  rhs = ToNumber(rhs, t);
  if (lhs->Is(Type::NaN()) || rhs->Is(Type::NaN())) return Type::NaN();
1244 1245

  if (lhs->Maybe(Type::NaN()) || rhs->Maybe(t->zeroish) ||
1246
      lhs->Min() == -V8_INFINITY || lhs->Max() == +V8_INFINITY) {
1247 1248 1249 1250 1251 1252 1253
    // Result maybe NaN.
    return Type::Number();
  }

  lhs = Rangify(lhs, t);
  rhs = Rangify(rhs, t);
  if (lhs->IsRange() && rhs->IsRange()) {
1254
    return JSModulusRanger(lhs->AsRange(), rhs->AsRange(), t);
1255 1256
  }
  return Type::OrderedNumber();
1257 1258 1259 1260 1261
}


// JS unary operators.

1262 1263 1264 1265 1266 1267

Type* Typer::Visitor::JSUnaryNotTyper(Type* type, Typer* t) {
  return Invert(ToBoolean(type, t), t);
}


1268
Bounds Typer::Visitor::TypeJSUnaryNot(Node* node) {
1269
  return TypeUnaryOp(node, JSUnaryNotTyper);
1270 1271 1272 1273
}


Bounds Typer::Visitor::TypeJSTypeOf(Node* node) {
1274
  return Bounds(Type::None(zone()), Type::InternalizedString(zone()));
1275 1276 1277 1278 1279
}


// JS conversion operators.

1280

1281
Bounds Typer::Visitor::TypeJSToBoolean(Node* node) {
1282
  return TypeUnaryOp(node, ToBoolean);
1283 1284 1285 1286
}


Bounds Typer::Visitor::TypeJSToNumber(Node* node) {
1287
  return TypeUnaryOp(node, ToNumber);
1288 1289 1290 1291
}


Bounds Typer::Visitor::TypeJSToString(Node* node) {
1292
  return TypeUnaryOp(node, ToString);
1293 1294 1295 1296
}


Bounds Typer::Visitor::TypeJSToName(Node* node) {
1297
  return Bounds(Type::None(), Type::Name());
1298 1299 1300 1301
}


Bounds Typer::Visitor::TypeJSToObject(Node* node) {
1302
  return Bounds(Type::None(), Type::Receiver());
1303 1304 1305 1306 1307
}


// JS object operators.

1308

1309
Bounds Typer::Visitor::TypeJSCreate(Node* node) {
1310
  return Bounds(Type::None(), Type::Object());
1311 1312 1313
}


1314
Type* Typer::Visitor::JSLoadPropertyTyper(Type* object, Type* name, Typer* t) {
1315
  // TODO(rossberg): Use range types and sized array types to filter undefined.
1316 1317 1318
  if (object->IsArray() && name->Is(Type::Integral32())) {
    return Type::Union(
        object->AsArray()->Element(), Type::Undefined(), t->zone());
1319
  }
1320 1321 1322 1323 1324 1325
  return Type::Any();
}


Bounds Typer::Visitor::TypeJSLoadProperty(Node* node) {
  return TypeBinaryOp(node, JSLoadPropertyTyper);
1326 1327 1328 1329 1330 1331 1332 1333
}


Bounds Typer::Visitor::TypeJSLoadNamed(Node* node) {
  return Bounds::Unbounded(zone());
}


1334 1335 1336 1337 1338
// Returns a somewhat larger range if we previously assigned
// a (smaller) range to this node. This is used  to speed up
// the fixpoint calculation in case there appears to be a loop
// in the graph. In the current implementation, we are
// increasing the limits to the closest power of two.
1339 1340
Type* Typer::Visitor::Weaken(Node* node, Type* current_type,
                             Type* previous_type) {
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  static const double kWeakenMinLimits[] = {
      0.0, -1073741824.0, -2147483648.0, -4294967296.0, -8589934592.0,
      -17179869184.0, -34359738368.0, -68719476736.0, -137438953472.0,
      -274877906944.0, -549755813888.0, -1099511627776.0, -2199023255552.0,
      -4398046511104.0, -8796093022208.0, -17592186044416.0, -35184372088832.0,
      -70368744177664.0, -140737488355328.0, -281474976710656.0,
      -562949953421312.0};
  static const double kWeakenMaxLimits[] = {
      0.0, 1073741823.0, 2147483647.0, 4294967295.0, 8589934591.0,
      17179869183.0, 34359738367.0, 68719476735.0, 137438953471.0,
      274877906943.0, 549755813887.0, 1099511627775.0, 2199023255551.0,
      4398046511103.0, 8796093022207.0, 17592186044415.0, 35184372088831.0,
      70368744177663.0, 140737488355327.0, 281474976710655.0,
      562949953421311.0};
  STATIC_ASSERT(arraysize(kWeakenMinLimits) == arraysize(kWeakenMaxLimits));

1357
  // If the types have nothing to do with integers, return the types.
1358
  if (!previous_type->Maybe(typer_->integer)) {
1359 1360
    return current_type;
  }
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
  DCHECK(current_type->Maybe(typer_->integer));

  Type* current_integer =
      Type::Intersect(current_type, typer_->integer, zone());
  Type* previous_integer =
      Type::Intersect(previous_type, typer_->integer, zone());

  // Once we start weakening a node, we should always weaken.
  if (!IsWeakened(node->id())) {
    // Only weaken if there is range involved; we should converge quickly
    // for all other types (the exception is a union of many constants,
    // but we currently do not increase the number of constants in unions).
    Type::RangeType* previous = previous_integer->GetRange();
    Type::RangeType* current = current_integer->GetRange();
    if (current == nullptr || previous == nullptr) {
      return current_type;
    }
    // Range is involved => we are weakening.
    SetWeakened(node->id());
1380 1381
  }

1382
  double current_min = current_integer->Min();
1383
  double new_min = current_min;
1384 1385
  // Find the closest lower entry in the list of allowed
  // minima (or negative infinity if there is no such entry).
1386
  if (current_min != previous_integer->Min()) {
1387
    new_min = typer_->integer->AsRange()->Min();
1388 1389 1390
    for (double const min : kWeakenMinLimits) {
      if (min <= current_min) {
        new_min = min;
1391
        break;
1392 1393
      }
    }
1394
  }
1395

1396
  double current_max = current_integer->Max();
1397
  double new_max = current_max;
1398 1399
  // Find the closest greater entry in the list of allowed
  // maxima (or infinity if there is no such entry).
1400
  if (current_max != previous_integer->Max()) {
1401
    new_max = typer_->integer->AsRange()->Max();
1402 1403 1404
    for (double const max : kWeakenMaxLimits) {
      if (max >= current_max) {
        new_max = max;
1405
        break;
1406 1407
      }
    }
1408
  }
1409 1410 1411 1412

  return Type::Union(current_type,
                     Type::Range(new_min, new_max, typer_->zone()),
                     typer_->zone());
1413 1414 1415
}


1416
Bounds Typer::Visitor::TypeJSStoreProperty(Node* node) {
1417 1418
  UNREACHABLE();
  return Bounds();
1419 1420 1421 1422
}


Bounds Typer::Visitor::TypeJSStoreNamed(Node* node) {
1423 1424
  UNREACHABLE();
  return Bounds();
1425 1426 1427 1428
}


Bounds Typer::Visitor::TypeJSDeleteProperty(Node* node) {
1429
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1430 1431 1432 1433
}


Bounds Typer::Visitor::TypeJSHasProperty(Node* node) {
1434
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1435 1436 1437 1438
}


Bounds Typer::Visitor::TypeJSInstanceOf(Node* node) {
1439
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1440 1441 1442 1443 1444
}


// JS context operators.

1445

1446
Bounds Typer::Visitor::TypeJSLoadContext(Node* node) {
1447
  ContextAccess access = OpParameter<ContextAccess>(node);
1448
  Bounds outer = Operand(node, 0);
1449
  Type* context_type = outer.upper;
1450 1451 1452
  Type* upper = (access.index() == Context::GLOBAL_OBJECT_INDEX)
                    ? Type::GlobalObject()
                    : Type::Any();
1453 1454
  if (context_type->Is(Type::None())) {
    // Upper bound of context is not yet known.
1455
    return Bounds(Type::None(), upper);
1456 1457 1458
  }

  DCHECK(context_type->Maybe(Type::Internal()));
1459 1460 1461
  // TODO(rossberg): More precisely, instead of the above assertion, we should
  // back-propagate the constraint that it has to be a subtype of Internal.

1462 1463 1464 1465 1466 1467 1468
  MaybeHandle<Context> context;
  if (context_type->IsConstant()) {
    context = Handle<Context>::cast(context_type->AsConstant()->Value());
  }
  // Walk context chain (as far as known), mirroring dynamic lookup.
  // Since contexts are mutable, the information is only useful as a lower
  // bound.
1469
  for (size_t i = access.depth(); i > 0; --i) {
1470 1471 1472 1473 1474
    if (context_type->IsContext()) {
      context_type = context_type->AsContext()->Outer();
      if (context_type->IsConstant()) {
        context = Handle<Context>::cast(context_type->AsConstant()->Value());
      }
1475
    } else if (!context.is_null()) {
1476 1477 1478
      context = handle(context.ToHandleChecked()->previous(), isolate());
    }
  }
1479 1480 1481
  Type* lower = Type::None();
  if (!context.is_null()) {
    lower = TypeConstant(
1482
        handle(context.ToHandleChecked()->get(static_cast<int>(access.index())),
1483
               isolate()));
1484
  }
1485
  return Bounds(lower, upper);
1486 1487 1488 1489
}


Bounds Typer::Visitor::TypeJSStoreContext(Node* node) {
1490 1491
  UNREACHABLE();
  return Bounds();
1492 1493 1494
}


1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
Bounds Typer::Visitor::WrapContextBoundsForInput(Node* node) {
  Bounds outer = BoundsOrNone(NodeProperties::GetContextInput(node));
  if (outer.upper->Is(Type::None())) {
    return Bounds(Type::None());
  } else {
    DCHECK(outer.upper->Maybe(Type::Internal()));
    return Bounds(Type::Context(outer.upper, zone()));
  }
}


1506
Bounds Typer::Visitor::TypeJSCreateFunctionContext(Node* node) {
1507
  return WrapContextBoundsForInput(node);
1508 1509 1510 1511
}


Bounds Typer::Visitor::TypeJSCreateCatchContext(Node* node) {
1512
  return WrapContextBoundsForInput(node);
1513 1514 1515 1516
}


Bounds Typer::Visitor::TypeJSCreateWithContext(Node* node) {
1517
  return WrapContextBoundsForInput(node);
1518 1519 1520 1521
}


Bounds Typer::Visitor::TypeJSCreateBlockContext(Node* node) {
1522
  return WrapContextBoundsForInput(node);
1523 1524 1525 1526 1527
}


Bounds Typer::Visitor::TypeJSCreateModuleContext(Node* node) {
  // TODO(rossberg): this is probably incorrect
1528
  return WrapContextBoundsForInput(node);
1529 1530 1531
}


1532
Bounds Typer::Visitor::TypeJSCreateScriptContext(Node* node) {
1533
  return WrapContextBoundsForInput(node);
1534 1535 1536 1537 1538
}


// JS other operators.

1539

1540 1541 1542 1543 1544 1545
Bounds Typer::Visitor::TypeJSYield(Node* node) {
  return Bounds::Unbounded(zone());
}


Bounds Typer::Visitor::TypeJSCallConstruct(Node* node) {
1546 1547 1548 1549 1550 1551
  return Bounds(Type::None(), Type::Receiver());
}


Type* Typer::Visitor::JSCallFunctionTyper(Type* fun, Typer* t) {
  return fun->IsFunction() ? fun->AsFunction()->Result() : Type::Any();
1552 1553 1554 1555
}


Bounds Typer::Visitor::TypeJSCallFunction(Node* node) {
1556
  return TypeUnaryOp(node, JSCallFunctionTyper);  // We ignore argument types.
1557 1558 1559 1560
}


Bounds Typer::Visitor::TypeJSCallRuntime(Node* node) {
1561 1562 1563 1564 1565 1566 1567
  switch (CallRuntimeParametersOf(node->op()).id()) {
    case Runtime::kInlineIsSmi:
    case Runtime::kInlineIsNonNegativeSmi:
    case Runtime::kInlineIsArray:
    case Runtime::kInlineIsFunction:
    case Runtime::kInlineIsRegExp:
      return Bounds(Type::None(zone()), Type::Boolean(zone()));
1568 1569
    case Runtime::kInlineDoubleLo:
    case Runtime::kInlineDoubleHi:
1570
      return Bounds(Type::None(zone()), Type::Signed32());
1571
    case Runtime::kInlineConstructDouble:
1572
    case Runtime::kInlineMathFloor:
1573
    case Runtime::kInlineMathSqrt:
1574 1575 1576 1577
    case Runtime::kInlineMathAcos:
    case Runtime::kInlineMathAsin:
    case Runtime::kInlineMathAtan:
    case Runtime::kInlineMathAtan2:
1578
      return Bounds(Type::None(zone()), Type::Number());
1579 1580
    case Runtime::kInlineMathClz32:
      return Bounds(Type::None(), Type::Range(0, 32, zone()));
1581
    case Runtime::kInlineStringGetLength:
1582
      return Bounds(Type::None(), Type::Range(0, String::kMaxLength, zone()));
1583 1584 1585
    default:
      break;
  }
1586 1587 1588 1589
  return Bounds::Unbounded(zone());
}


1590
Bounds Typer::Visitor::TypeJSStackCheck(Node* node) {
1591 1592 1593 1594 1595 1596
  return Bounds::Unbounded(zone());
}


// Simplified operators.

1597

1598
Bounds Typer::Visitor::TypeBooleanNot(Node* node) {
1599
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1600 1601 1602
}


1603
Bounds Typer::Visitor::TypeBooleanToNumber(Node* node) {
1604
  return TypeUnaryOp(node, ToNumber);
1605 1606 1607
}


1608
Bounds Typer::Visitor::TypeNumberEqual(Node* node) {
1609
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1610 1611 1612 1613
}


Bounds Typer::Visitor::TypeNumberLessThan(Node* node) {
1614
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1615 1616 1617 1618
}


Bounds Typer::Visitor::TypeNumberLessThanOrEqual(Node* node) {
1619
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1620 1621 1622 1623
}


Bounds Typer::Visitor::TypeNumberAdd(Node* node) {
1624
  return Bounds(Type::None(zone()), Type::Number(zone()));
1625 1626 1627 1628
}


Bounds Typer::Visitor::TypeNumberSubtract(Node* node) {
1629
  return Bounds(Type::None(zone()), Type::Number(zone()));
1630 1631 1632 1633
}


Bounds Typer::Visitor::TypeNumberMultiply(Node* node) {
1634
  return Bounds(Type::None(zone()), Type::Number(zone()));
1635 1636 1637 1638
}


Bounds Typer::Visitor::TypeNumberDivide(Node* node) {
1639
  return Bounds(Type::None(zone()), Type::Number(zone()));
1640 1641 1642 1643
}


Bounds Typer::Visitor::TypeNumberModulus(Node* node) {
1644
  return Bounds(Type::None(zone()), Type::Number(zone()));
1645 1646 1647 1648
}


Bounds Typer::Visitor::TypeNumberToInt32(Node* node) {
1649
  return TypeUnaryOp(node, NumberToInt32);
1650 1651 1652 1653
}


Bounds Typer::Visitor::TypeNumberToUint32(Node* node) {
1654
  return TypeUnaryOp(node, NumberToUint32);
1655 1656 1657
}


1658 1659 1660 1661 1662
Bounds Typer::Visitor::TypePlainPrimitiveToNumber(Node* node) {
  return TypeUnaryOp(node, ToNumber);
}


1663
Bounds Typer::Visitor::TypeReferenceEqual(Node* node) {
1664
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1665 1666 1667 1668
}


Bounds Typer::Visitor::TypeStringEqual(Node* node) {
1669
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1670 1671 1672 1673
}


Bounds Typer::Visitor::TypeStringLessThan(Node* node) {
1674
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1675 1676 1677 1678
}


Bounds Typer::Visitor::TypeStringLessThanOrEqual(Node* node) {
1679
  return Bounds(Type::None(zone()), Type::Boolean(zone()));
1680 1681 1682 1683
}


Bounds Typer::Visitor::TypeStringAdd(Node* node) {
1684
  return Bounds(Type::None(zone()), Type::String(zone()));
1685 1686 1687
}


1688 1689 1690 1691 1692
namespace {

Type* ChangeRepresentation(Type* type, Type* rep, Zone* zone) {
  return Type::Union(Type::Semantic(type, zone),
                     Type::Representation(rep, zone), zone);
1693 1694
}

1695 1696
}  // namespace

1697 1698

Bounds Typer::Visitor::TypeChangeTaggedToInt32(Node* node) {
1699
  Bounds arg = Operand(node, 0);
1700 1701
  // TODO(neis): DCHECK(arg.upper->Is(Type::Signed32()));
  return Bounds(
1702 1703
      ChangeRepresentation(arg.lower, Type::UntaggedSigned32(), zone()),
      ChangeRepresentation(arg.upper, Type::UntaggedSigned32(), zone()));
1704 1705 1706 1707
}


Bounds Typer::Visitor::TypeChangeTaggedToUint32(Node* node) {
1708
  Bounds arg = Operand(node, 0);
1709 1710
  // TODO(neis): DCHECK(arg.upper->Is(Type::Unsigned32()));
  return Bounds(
1711 1712
      ChangeRepresentation(arg.lower, Type::UntaggedUnsigned32(), zone()),
      ChangeRepresentation(arg.upper, Type::UntaggedUnsigned32(), zone()));
1713 1714 1715 1716
}


Bounds Typer::Visitor::TypeChangeTaggedToFloat64(Node* node) {
1717
  Bounds arg = Operand(node, 0);
1718 1719 1720 1721
  // TODO(neis): DCHECK(arg.upper->Is(Type::Number()));
  return Bounds(
      ChangeRepresentation(arg.lower, Type::UntaggedFloat64(), zone()),
      ChangeRepresentation(arg.upper, Type::UntaggedFloat64(), zone()));
1722 1723 1724 1725
}


Bounds Typer::Visitor::TypeChangeInt32ToTagged(Node* node) {
1726
  Bounds arg = Operand(node, 0);
1727
  // TODO(neis): DCHECK(arg.upper->Is(Type::Signed32()));
1728 1729 1730 1731 1732 1733
  Type* lower_rep = arg.lower->Is(Type::SignedSmall()) ? Type::TaggedSigned()
                                                       : Type::Tagged();
  Type* upper_rep = arg.upper->Is(Type::SignedSmall()) ? Type::TaggedSigned()
                                                       : Type::Tagged();
  return Bounds(ChangeRepresentation(arg.lower, lower_rep, zone()),
                ChangeRepresentation(arg.upper, upper_rep, zone()));
1734 1735 1736 1737
}


Bounds Typer::Visitor::TypeChangeUint32ToTagged(Node* node) {
1738
  Bounds arg = Operand(node, 0);
1739 1740 1741 1742
  // TODO(neis): DCHECK(arg.upper->Is(Type::Unsigned32()));
  return Bounds(
      ChangeRepresentation(arg.lower, Type::Tagged(), zone()),
      ChangeRepresentation(arg.upper, Type::Tagged(), zone()));
1743 1744 1745 1746
}


Bounds Typer::Visitor::TypeChangeFloat64ToTagged(Node* node) {
1747
  Bounds arg = Operand(node, 0);
1748 1749 1750 1751
  // TODO(neis): CHECK(arg.upper->Is(Type::Number()));
  return Bounds(
      ChangeRepresentation(arg.lower, Type::Tagged(), zone()),
      ChangeRepresentation(arg.upper, Type::Tagged(), zone()));
1752 1753 1754 1755
}


Bounds Typer::Visitor::TypeChangeBoolToBit(Node* node) {
1756
  Bounds arg = Operand(node, 0);
1757 1758
  // TODO(neis): DCHECK(arg.upper->Is(Type::Boolean()));
  return Bounds(
1759 1760
      ChangeRepresentation(arg.lower, Type::UntaggedBit(), zone()),
      ChangeRepresentation(arg.upper, Type::UntaggedBit(), zone()));
1761 1762 1763
}


1764 1765 1766 1767 1768
Bounds Typer::Visitor::TypeChangeBitToBool(Node* node) {
  Bounds arg = Operand(node, 0);
  // TODO(neis): DCHECK(arg.upper->Is(Type::Boolean()));
  return Bounds(ChangeRepresentation(arg.lower, Type::TaggedPointer(), zone()),
                ChangeRepresentation(arg.upper, Type::TaggedPointer(), zone()));
1769 1770 1771 1772 1773 1774 1775 1776
}


Bounds Typer::Visitor::TypeLoadField(Node* node) {
  return Bounds(FieldAccessOf(node->op()).type);
}


1777
Bounds Typer::Visitor::TypeLoadBuffer(Node* node) {
1778 1779
  // TODO(bmeurer): This typing is not yet correct. Since we can still access
  // out of bounds, the type in the general case has to include Undefined.
1780
  switch (BufferAccessOf(node->op()).external_array_type()) {
1781 1782
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
  case kExternal##Type##Array:                          \
1783
    return Bounds(typer_->cache_->Get(k##Type));
1784 1785
    TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
1786 1787 1788 1789 1790 1791
  }
  UNREACHABLE();
  return Bounds();
}


1792 1793 1794 1795 1796 1797
Bounds Typer::Visitor::TypeLoadElement(Node* node) {
  return Bounds(ElementAccessOf(node->op()).type);
}


Bounds Typer::Visitor::TypeStoreField(Node* node) {
1798 1799
  UNREACHABLE();
  return Bounds();
1800 1801 1802
}


1803 1804 1805 1806 1807 1808
Bounds Typer::Visitor::TypeStoreBuffer(Node* node) {
  UNREACHABLE();
  return Bounds();
}


1809
Bounds Typer::Visitor::TypeStoreElement(Node* node) {
1810 1811
  UNREACHABLE();
  return Bounds();
1812 1813 1814
}


1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
Bounds Typer::Visitor::TypeObjectIsSmi(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeObjectIsNonNegativeSmi(Node* node) {
  return Bounds(Type::Boolean());
}


1825 1826
// Machine operators.

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
Bounds Typer::Visitor::TypeLoad(Node* node) {
  return Bounds::Unbounded(zone());
}


Bounds Typer::Visitor::TypeStore(Node* node) {
  UNREACHABLE();
  return Bounds();
}


Bounds Typer::Visitor::TypeWord32And(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Or(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Xor(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Shl(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Shr(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Sar(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Ror(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeWord32Equal(Node* node) {
  return Bounds(Type::Boolean());
}


1878 1879 1880 1881 1882
Bounds Typer::Visitor::TypeWord32Clz(Node* node) {
  return Bounds(Type::Integral32());
}


1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
Bounds Typer::Visitor::TypeWord64And(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Or(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Xor(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Shl(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Shr(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Sar(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Ror(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeWord64Equal(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeInt32Add(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeInt32AddWithOverflow(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt32Sub(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeInt32SubWithOverflow(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt32Mul(Node* node) {
  return Bounds(Type::Integral32());
}


1948
Bounds Typer::Visitor::TypeInt32MulHigh(Node* node) {
1949
  return Bounds(Type::Signed32());
1950 1951 1952
}


1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
Bounds Typer::Visitor::TypeInt32Div(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeInt32Mod(Node* node) {
  return Bounds(Type::Integral32());
}


Bounds Typer::Visitor::TypeInt32LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeInt32LessThanOrEqual(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeUint32Div(Node* node) {
  return Bounds(Type::Unsigned32());
}


Bounds Typer::Visitor::TypeUint32LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeUint32LessThanOrEqual(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeUint32Mod(Node* node) {
  return Bounds(Type::Unsigned32());
}


1993 1994 1995 1996 1997
Bounds Typer::Visitor::TypeUint32MulHigh(Node* node) {
  return Bounds(Type::Unsigned32());
}


1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
Bounds Typer::Visitor::TypeInt64Add(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt64Sub(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt64Mul(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt64Div(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt64Mod(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeInt64LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeInt64LessThanOrEqual(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeUint64Div(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeUint64LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeUint64Mod(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeChangeFloat32ToFloat64(Node* node) {
  return Bounds(Type::Intersect(
      Type::Number(), Type::UntaggedFloat64(), zone()));
}


Bounds Typer::Visitor::TypeChangeFloat64ToInt32(Node* node) {
  return Bounds(Type::Intersect(
2056
      Type::Signed32(), Type::UntaggedSigned32(), zone()));
2057 2058 2059 2060 2061
}


Bounds Typer::Visitor::TypeChangeFloat64ToUint32(Node* node) {
  return Bounds(Type::Intersect(
2062
      Type::Unsigned32(), Type::UntaggedUnsigned32(), zone()));
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
}


Bounds Typer::Visitor::TypeChangeInt32ToFloat64(Node* node) {
  return Bounds(Type::Intersect(
      Type::Signed32(), Type::UntaggedFloat64(), zone()));
}


Bounds Typer::Visitor::TypeChangeInt32ToInt64(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeChangeUint32ToFloat64(Node* node) {
  return Bounds(Type::Intersect(
      Type::Unsigned32(), Type::UntaggedFloat64(), zone()));
}


Bounds Typer::Visitor::TypeChangeUint32ToUint64(Node* node) {
  return Bounds(Type::Internal());
}


Bounds Typer::Visitor::TypeTruncateFloat64ToFloat32(Node* node) {
  return Bounds(Type::Intersect(
      Type::Number(), Type::UntaggedFloat32(), zone()));
}


Bounds Typer::Visitor::TypeTruncateFloat64ToInt32(Node* node) {
  return Bounds(Type::Intersect(
2096
      Type::Signed32(), Type::UntaggedSigned32(), zone()));
2097 2098 2099 2100 2101
}


Bounds Typer::Visitor::TypeTruncateInt64ToInt32(Node* node) {
  return Bounds(Type::Intersect(
2102
      Type::Signed32(), Type::UntaggedSigned32(), zone()));
2103 2104 2105
}


2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
Bounds Typer::Visitor::TypeFloat32Add(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Sub(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Mul(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Div(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Max(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Min(Node* node) {
  return Bounds(Type::Number());
}


2136 2137 2138 2139 2140 2141
Bounds Typer::Visitor::TypeFloat32Abs(Node* node) {
  // TODO(turbofan): We should be able to infer a better type here.
  return Bounds(Type::Number());
}


2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
Bounds Typer::Visitor::TypeFloat32Sqrt(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat32Equal(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeFloat32LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeFloat32LessThanOrEqual(Node* node) {
  return Bounds(Type::Boolean());
}


2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
Bounds Typer::Visitor::TypeFloat64Add(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Sub(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Mul(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Div(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Mod(Node* node) {
  return Bounds(Type::Number());
}


2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
Bounds Typer::Visitor::TypeFloat64Max(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Min(Node* node) {
  return Bounds(Type::Number());
}


2197 2198 2199 2200 2201 2202
Bounds Typer::Visitor::TypeFloat64Abs(Node* node) {
  // TODO(turbofan): We should be able to infer a better type here.
  return Bounds(Type::Number());
}


2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
Bounds Typer::Visitor::TypeFloat64Sqrt(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64Equal(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeFloat64LessThan(Node* node) {
  return Bounds(Type::Boolean());
}


Bounds Typer::Visitor::TypeFloat64LessThanOrEqual(Node* node) {
  return Bounds(Type::Boolean());
}


2223
Bounds Typer::Visitor::TypeFloat64RoundDown(Node* node) {
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
  // TODO(sigurds): We could have a tighter bound here.
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64RoundTruncate(Node* node) {
  // TODO(sigurds): We could have a tighter bound here.
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64RoundTiesAway(Node* node) {
  // TODO(sigurds): We could have a tighter bound here.
  return Bounds(Type::Number());
}


2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
Bounds Typer::Visitor::TypeFloat64ExtractLowWord32(Node* node) {
  return Bounds(Type::Signed32());
}


Bounds Typer::Visitor::TypeFloat64ExtractHighWord32(Node* node) {
  return Bounds(Type::Signed32());
}


Bounds Typer::Visitor::TypeFloat64InsertLowWord32(Node* node) {
  return Bounds(Type::Number());
}


Bounds Typer::Visitor::TypeFloat64InsertHighWord32(Node* node) {
  return Bounds(Type::Number());
}


2261 2262 2263
Bounds Typer::Visitor::TypeLoadStackPointer(Node* node) {
  return Bounds(Type::Internal());
}
2264 2265


2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
Bounds Typer::Visitor::TypeCheckedLoad(Node* node) {
  return Bounds::Unbounded(zone());
}


Bounds Typer::Visitor::TypeCheckedStore(Node* node) {
  UNREACHABLE();
  return Bounds();
}


2277 2278
// Heap constants.

2279

2280
Type* Typer::Visitor::TypeConstant(Handle<Object> value) {
2281 2282 2283
  if (value->IsJSFunction()) {
    if (JSFunction::cast(*value)->shared()->HasBuiltinFunctionId()) {
      switch (JSFunction::cast(*value)->shared()->builtin_function_id()) {
2284 2285 2286 2287 2288 2289 2290 2291
        case kMathRandom:
          return typer_->random_fun_;
        case kMathFloor:
          return typer_->weakint_fun1_;
        case kMathRound:
          return typer_->weakint_fun1_;
        case kMathCeil:
          return typer_->weakint_fun1_;
2292 2293
        // Unary math functions.
        case kMathAbs:  // TODO(rossberg): can't express overloading
2294 2295 2296
        case kMathLog:
        case kMathExp:
        case kMathSqrt:
2297
        case kMathCos:
2298 2299 2300 2301 2302
        case kMathSin:
        case kMathTan:
        case kMathAcos:
        case kMathAsin:
        case kMathAtan:
2303 2304 2305
        case kMathFround:
          return typer_->cache_->Get(kNumberFunc1);
        // Binary math functions.
2306
        case kMathAtan2:
2307 2308 2309 2310
        case kMathPow:
        case kMathMax:
        case kMathMin:
          return typer_->cache_->Get(kNumberFunc2);
2311
        case kMathImul:
2312
          return typer_->cache_->Get(kImulFunc);
2313
        case kMathClz32:
2314
          return typer_->cache_->Get(kClz32Func);
2315 2316 2317 2318 2319 2320 2321
        default:
          break;
      }
    } else if (JSFunction::cast(*value)->IsBuiltin() && !context().is_null()) {
      Handle<Context> native =
          handle(context().ToHandleChecked()->native_context(), isolate());
      if (*value == native->array_buffer_fun()) {
2322
        return typer_->cache_->Get(kArrayBufferFunc);
2323
      } else if (*value == native->int8_array_fun()) {
2324
        return typer_->cache_->Get(kInt8ArrayFunc);
2325
      } else if (*value == native->int16_array_fun()) {
2326
        return typer_->cache_->Get(kInt16ArrayFunc);
2327
      } else if (*value == native->int32_array_fun()) {
2328
        return typer_->cache_->Get(kInt32ArrayFunc);
2329
      } else if (*value == native->uint8_array_fun()) {
2330
        return typer_->cache_->Get(kUint8ArrayFunc);
2331
      } else if (*value == native->uint16_array_fun()) {
2332
        return typer_->cache_->Get(kUint16ArrayFunc);
2333
      } else if (*value == native->uint32_array_fun()) {
2334
        return typer_->cache_->Get(kUint32ArrayFunc);
2335
      } else if (*value == native->float32_array_fun()) {
2336
        return typer_->cache_->Get(kFloat32ArrayFunc);
2337
      } else if (*value == native->float64_array_fun()) {
2338
        return typer_->cache_->Get(kFloat64ArrayFunc);
2339
      }
2340
    }
2341 2342
  } else if (value->IsJSTypedArray()) {
    switch (JSTypedArray::cast(*value)->type()) {
2343 2344
#define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
  case kExternal##Type##Array:                          \
2345
    return typer_->cache_->Get(k##Type##Array);
2346 2347
      TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE
2348
    }
2349 2350 2351 2352
  }
  return Type::Constant(value, zone());
}

2353 2354 2355
}  // namespace compiler
}  // namespace internal
}  // namespace v8