Reviewers: jarin,

Description:
[turbofan] Smartify typing of NumberToInt32 and NumberToUint32.

According to ES5 9.5 and 9.6, NaN, -inf, +inf, -0 and 0 all truncate to
zero for both ToInt32 and ToUint32, so we can be a lot smarter in the
typer, loosing less information upon truncation (i.e. x | 0 and x >>> 0).

[email protected]

Please review this at https://codereview.chromium.org/739743003/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+26, -11 lines):
  M src/compiler/typer.h
  M src/compiler/typer.cc


Index: src/compiler/typer.cc
diff --git a/src/compiler/typer.cc b/src/compiler/typer.cc
index 996eecd361caacde8ae062304ac1180d68902e75..dd9a02cd92e8decc728c6c15df7d877ae161141f 100644
--- a/src/compiler/typer.cc
+++ b/src/compiler/typer.cc
@@ -39,6 +39,18 @@ Typer::Typer(Graph* graph, MaybeHandle<Context> context)
   Handle<Object> infinity = f->NewNumber(+V8_INFINITY);
   Handle<Object> minusinfinity = f->NewNumber(-V8_INFINITY);

+  Type* number = Type::Number();
+  Type* signed32 = Type::Signed32();
+  Type* unsigned32 = Type::Unsigned32();
+  Type* integral32 = Type::Integral32();
+  Type* object = Type::Object();
+  Type* undefined = Type::Undefined();
+ 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);
+
   negative_signed32 = Type::Union(
       Type::SignedSmall(), Type::OtherSigned32(), zone);
   non_negative_signed32 = Type::Union(
@@ -49,20 +61,13 @@ Typer::Typer(Graph* graph, MaybeHandle<Context> context)
   singleton_zero = Type::Range(zero, zero, zone);
   singleton_one = Type::Range(one, one, zone);
   zero_or_one = Type::Union(singleton_zero, singleton_one, zone);
-  zeroish = Type::Union(
- singleton_zero, Type::Union(Type::NaN(), Type::MinusZero(), zone), zone);
+  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);
   falsish = Type::Union(Type::Undetectable(),
       Type::Union(zeroish, undefined_or_null, zone), zone);
   integer = Type::Range(minusinfinity, infinity, zone);
-  weakint = Type::Union(
-      integer, Type::Union(Type::NaN(), Type::MinusZero(), zone), zone);
-
-  Type* number = Type::Number();
-  Type* signed32 = Type::Signed32();
-  Type* unsigned32 = Type::Unsigned32();
-  Type* integral32 = Type::Integral32();
-  Type* object = Type::Object();
-  Type* undefined = Type::Undefined();
+  weakint = Type::Union(integer, nan_or_minuszero, zone);

   number_fun0_ = Type::Function(number, zone);
   number_fun1_ = Type::Function(number, number, zone);
@@ -467,6 +472,10 @@ 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;
+  if (type->Is(t->signed32ish)) {
+    return Type::Intersect(Type::Union(type, t->singleton_zero, t->zone()),
+                           Type::Signed32(), t->zone());
+  }
   return Type::Signed32();
 }

@@ -475,6 +484,10 @@ 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;
+  if (type->Is(t->unsigned32ish)) {
+    return Type::Intersect(Type::Union(type, t->singleton_zero, t->zone()),
+                           Type::Unsigned32(), t->zone());
+  }
   return Type::Unsigned32();
 }

Index: src/compiler/typer.h
diff --git a/src/compiler/typer.h b/src/compiler/typer.h
index 8491d914af058c87e1f8cfce1aa3959befdf50f0..bfa4bd85d926b15664c2d09f3017838d986005c1 100644
--- a/src/compiler/typer.h
+++ b/src/compiler/typer.h
@@ -47,6 +47,8 @@ class Typer {
   Type* singleton_one;
   Type* zero_or_one;
   Type* zeroish;
+  Type* signed32ish;
+  Type* unsigned32ish;
   Type* falsish;
   Type* integer;
   Type* weakint;


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to