Reviewers: Sven Panne,

Description:
[turbofan] Push JSToNumber conversions into Phis.

This essentially performs the following transformation

  JSToNumber(phi(x1,...,xn,control):primitive)
    => phi(JSToNumber(x1),...,JSToNumber(xn),control):number

which is similar to what we already do for JSToBoolean.

TEST=mjsunit/asm
[email protected]
BUG=

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

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

Affected files (+87, -10 lines):
  M src/compiler/js-typed-lowering.h
  M src/compiler/js-typed-lowering.cc
  A test/mjsunit/asm/if-tonumber.js


Index: src/compiler/js-typed-lowering.cc
diff --git a/src/compiler/js-typed-lowering.cc b/src/compiler/js-typed-lowering.cc index 4765bc2e5fc618a6ddbed3983f8c4c9a678beb90..745aafd811e8f94c84749eb58135078b709331c5 100644
--- a/src/compiler/js-typed-lowering.cc
+++ b/src/compiler/js-typed-lowering.cc
@@ -466,11 +466,8 @@ Reduction JSTypedLowering::ReduceJSStrictEqual(Node* node, bool invert) {
 Reduction JSTypedLowering::ReduceJSToNumberInput(Node* input) {
   if (input->opcode() == IrOpcode::kJSToNumber) {
     // Recursively try to reduce the input first.
-    Reduction result = ReduceJSToNumberInput(input->InputAt(0));
-    if (result.Changed()) {
-      RelaxEffects(input);
-      return result;
-    }
+    Reduction result = ReduceJSToNumber(input);
+    if (result.Changed()) return result;
     return Changed(input);  // JSToNumber(JSToNumber(x)) => JSToNumber(x)
   }
   Type* input_type = NodeProperties::GetBounds(input).upper;
@@ -496,6 +493,55 @@ Reduction JSTypedLowering::ReduceJSToNumberInput(Node* input) {
 }


+Reduction JSTypedLowering::ReduceJSToNumber(Node* node) {
+  // Try to reduce the input first.
+  Node* const input = node->InputAt(0);
+  Reduction reduction = ReduceJSToNumberInput(input);
+  if (reduction.Changed()) {
+    NodeProperties::ReplaceWithValue(node, reduction.replacement());
+    return reduction;
+  }
+  Type* const input_type = NodeProperties::GetBounds(input).upper;
+ if (input->opcode() == IrOpcode::kPhi && input_type->Is(Type::Primitive())) {
+    Node* const context = node->InputAt(1);
+    // JSToNumber(phi(x1,...,xn,control):primitive)
+    //   => phi(JSToNumber(x1),...,JSToNumber(xn),control):number
+    RelaxEffects(node);
+    int const input_count = input->InputCount() - 1;
+    Node* const control = input->InputAt(input_count);
+    DCHECK_LE(0, input_count);
+    DCHECK(NodeProperties::IsControl(control));
+    DCHECK(NodeProperties::GetBounds(node).upper->Is(Type::Number()));
+    DCHECK(!NodeProperties::GetBounds(input).upper->Is(Type::Number()));
+    node->set_op(common()->Phi(kMachAnyTagged, input_count));
+    for (int i = 0; i < input_count; ++i) {
+      Node* value = input->InputAt(i);
+      // Recursively try to reduce the value first.
+      Reduction reduction = ReduceJSToNumberInput(value);
+      if (reduction.Changed()) {
+        value = reduction.replacement();
+      } else {
+        value = graph()->NewNode(javascript()->ToNumber(), value, context,
+                                 graph()->start(), graph()->start());
+      }
+      if (i < node->InputCount()) {
+        node->ReplaceInput(i, value);
+      } else {
+        node->AppendInput(graph()->zone(), value);
+      }
+    }
+    if (input_count < node->InputCount()) {
+      node->ReplaceInput(input_count, control);
+    } else {
+      node->AppendInput(graph()->zone(), control);
+    }
+    node->TrimInputCount(input_count + 1);
+    return Changed(node);
+  }
+  return NoChange();
+}
+
+
 Reduction JSTypedLowering::ReduceJSToStringInput(Node* input) {
   if (input->opcode() == IrOpcode::kJSToString) {
     // Recursively try to reduce the input first.
@@ -582,6 +628,7 @@ Reduction JSTypedLowering::ReduceJSToBoolean(Node* node) {
   }
   Type* const input_type = NodeProperties::GetBounds(input).upper;
if (input->opcode() == IrOpcode::kPhi && input_type->Is(Type::Primitive())) {
+    Node* const context = node->InputAt(1);
     // JSToBoolean(phi(x1,...,xn,control):primitive)
     //   => phi(JSToBoolean(x1),...,JSToBoolean(xn),control):boolean
     RelaxEffects(node);
@@ -599,9 +646,8 @@ Reduction JSTypedLowering::ReduceJSToBoolean(Node* node) {
       if (reduction.Changed()) {
         value = reduction.replacement();
       } else {
-        value = graph()->NewNode(javascript()->ToBoolean(), value,
- jsgraph()->ZeroConstant(), graph()->start(),
-                                 graph()->start());
+        value = graph()->NewNode(javascript()->ToBoolean(), value, context,
+                                 graph()->start(), graph()->start());
       }
       if (i < node->InputCount()) {
         node->ReplaceInput(i, value);
@@ -769,8 +815,7 @@ Reduction JSTypedLowering::Reduce(Node* node) {
     case IrOpcode::kJSToBoolean:
       return ReduceJSToBoolean(node);
     case IrOpcode::kJSToNumber:
-      return ReplaceWithReduction(node,
-                                  ReduceJSToNumberInput(node->InputAt(0)));
+      return ReduceJSToNumber(node);
     case IrOpcode::kJSToString:
       return ReplaceWithReduction(node,
                                   ReduceJSToStringInput(node->InputAt(0)));
Index: src/compiler/js-typed-lowering.h
diff --git a/src/compiler/js-typed-lowering.h b/src/compiler/js-typed-lowering.h index ba160c491faa9b9acb0391c896c716976dda996b..3dfcefdfc4fab248a91c763f8aeb457bdfbb8f72 100644
--- a/src/compiler/js-typed-lowering.h
+++ b/src/compiler/js-typed-lowering.h
@@ -41,6 +41,7 @@ class JSTypedLowering FINAL : public Reducer {
   Reduction ReduceJSEqual(Node* node, bool invert);
   Reduction ReduceJSStrictEqual(Node* node, bool invert);
   Reduction ReduceJSToNumberInput(Node* input);
+  Reduction ReduceJSToNumber(Node* node);
   Reduction ReduceJSToStringInput(Node* input);
   Reduction ReduceJSToBooleanInput(Node* input);
   Reduction ReduceJSToBoolean(Node* node);
Index: test/mjsunit/asm/if-tonumber.js
diff --git a/test/mjsunit/asm/if-tonumber.js b/test/mjsunit/asm/if-tonumber.js
new file mode 100644
index 0000000000000000000000000000000000000000..dd3f73b2c2a7ff2a05e5eabd3decf374b07d12c8
--- /dev/null
+++ b/test/mjsunit/asm/if-tonumber.js
@@ -0,0 +1,31 @@
+// 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.
+
+var stdlib = this;
+var buffer = new ArrayBuffer(64 * 1024);
+var foreign = {}
+
+function Module(stdlib, foreign, heap) {
+  "use asm";
+  function foo(i) {
+    i = i|0;
+    if (i > 0) {
+      i = i == 1;
+    } else {
+      i = 1;
+    }
+    return i & 1|0;
+  }
+  return { foo: foo };
+}
+
+var m = Module(stdlib, foreign, buffer);
+
+assertEquals(1, m.foo(-1));
+assertEquals(1, m.foo(-0));
+assertEquals(1, m.foo(0));
+assertEquals(1, m.foo(1));
+assertEquals(0, m.foo(2));
+assertEquals(1, m.foo(true));
+assertEquals(1, m.foo(false));


--
--
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