Reviewers: jarin,

Message:
PTAL

Description:
[turbofan] Generalize constant propagation.

Also support additional number types (singleton ranges, NaN and minus
zero) for constant propagation in typed lowering.

TEST=unittests

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

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

Affected files (+61, -11 lines):
  M src/compiler/js-typed-lowering.cc
  M test/unittests/compiler/js-typed-lowering-unittest.cc
  M test/unittests/compiler/simplified-operator-reducer-unittest.cc
  M testing/gmock-support.h


Index: src/compiler/js-typed-lowering.cc
diff --git a/src/compiler/js-typed-lowering.cc b/src/compiler/js-typed-lowering.cc index 2338866d6d2274830dff5b132128ca3f9216659b..5b9e3dbb98fa05335f8c7e88f590e7888e745de3 100644
--- a/src/compiler/js-typed-lowering.cc
+++ b/src/compiler/js-typed-lowering.cc
@@ -927,14 +927,21 @@ Reduction JSTypedLowering::ReduceJSStoreContext(Node* node) {

 Reduction JSTypedLowering::Reduce(Node* node) {
// Check if the output type is a singleton. In that case we already know the
-  // result value and can simply replace the node unless there are effects.
+  // result value and can simply replace the node if it's eliminatable.
   if (NodeProperties::IsTyped(node) &&
-      NodeProperties::GetBounds(node).upper->IsConstant() &&
       !IrOpcode::IsLeafOpcode(node->opcode()) &&
-      node->op()->EffectOutputCount() == 0) {
-    return ReplaceEagerly(node, jsgraph()->Constant(
-        NodeProperties::GetBounds(node).upper->AsConstant()->Value()));
-    // TODO(neis): Extend this to Range(x,x), NaN, MinusZero, ...?
+      node->op()->HasProperty(Operator::kEliminatable)) {
+    Type* upper = NodeProperties::GetBounds(node).upper;
+    if (upper->IsConstant()) {
+ Node* replacement = jsgraph()->Constant(upper->AsConstant()->Value());
+      NodeProperties::ReplaceWithValue(node, replacement);
+      return Changed(replacement);
+    } else if (upper->Is(Type::Number()) &&
+               base::bit_equal_to<double>()(upper->Min(), upper->Max())) {
+      Node* replacement = jsgraph()->Constant(upper->Min());
+      NodeProperties::ReplaceWithValue(node, replacement);
+      return Changed(replacement);
+    }
   }
   switch (node->opcode()) {
     case IrOpcode::kJSEqual:
Index: test/unittests/compiler/js-typed-lowering-unittest.cc
diff --git a/test/unittests/compiler/js-typed-lowering-unittest.cc b/test/unittests/compiler/js-typed-lowering-unittest.cc index e4ea4a581f6b3e937ed8cbd8fa5043db5749d2ae..618764a1748b87c7499f40071c6c1d68981c4137 100644
--- a/test/unittests/compiler/js-typed-lowering-unittest.cc
+++ b/test/unittests/compiler/js-typed-lowering-unittest.cc
@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.

+#include <climits>
+
 #include "src/compiler/access-builder.h"
 #include "src/compiler/js-graph.h"
 #include "src/compiler/js-operator.h"
@@ -12,6 +14,10 @@
 #include "test/unittests/compiler/compiler-test-utils.h"
 #include "test/unittests/compiler/graph-unittest.h"
 #include "test/unittests/compiler/node-test-utils.h"
+#include "testing/gmock-support.h"
+
+using testing::IsNaN;
+

 namespace v8 {
 namespace internal {
@@ -69,6 +75,37 @@ class JSTypedLoweringTest : public TypedGraphTest {


// -----------------------------------------------------------------------------
+// Constant propagation
+
+
+TEST_F(JSTypedLoweringTest, ParameterWithMinusZero) {
+  Reduction r = Reduce(Parameter(Type::MinusZero()));
+  ASSERT_TRUE(r.Changed());
+  EXPECT_THAT(r.replacement(), IsNumberConstant(-0.0));
+}
+
+
+TEST_F(JSTypedLoweringTest, ParameterWithNaN) {
+  Reduction r = Reduce(Parameter(Type::NaN()));
+  ASSERT_TRUE(r.Changed());
+  EXPECT_THAT(r.replacement(), IsNumberConstant(IsNaN()));
+}
+
+
+TEST_F(JSTypedLoweringTest, ParameterWithSingletonRange) {
+  static const double kValues[] = {
+      -V8_INFINITY, INT_MIN, -1000.0, -42.0,   -1.0,     0.0,
+      1.0,          42.0,    1000.0,  INT_MAX, UINT_MAX, +V8_INFINITY};
+  TRACED_FOREACH(double, value, kValues) {
+    Handle<Object> constant = factory()->NewNumber(value);
+ Reduction r = Reduce(Parameter(Type::Range(constant, constant, zone())));
+    ASSERT_TRUE(r.Changed());
+    EXPECT_THAT(r.replacement(), IsNumberConstant(value));
+  }
+}
+
+
+// -----------------------------------------------------------------------------
 // JSToBoolean


Index: test/unittests/compiler/simplified-operator-reducer-unittest.cc
diff --git a/test/unittests/compiler/simplified-operator-reducer-unittest.cc b/test/unittests/compiler/simplified-operator-reducer-unittest.cc index 066cbe9337e5d216e185944c487ecbbdd585c5cb..666837d1ca025164f11c77dc76231dd8e7d654d3 100644
--- a/test/unittests/compiler/simplified-operator-reducer-unittest.cc
+++ b/test/unittests/compiler/simplified-operator-reducer-unittest.cc
@@ -9,6 +9,10 @@
 #include "src/types.h"
 #include "test/unittests/compiler/graph-unittest.h"
 #include "test/unittests/compiler/node-test-utils.h"
+#include "testing/gmock-support.h"
+
+using testing::IsNaN;
+

 namespace v8 {
 namespace internal {
@@ -114,11 +118,6 @@ static const uint32_t kUint32Values[] = {
     0xbeb15c0d, 0xc171c53d, 0xc743dd38, 0xc8e2af50, 0xc98e2df0, 0xd9d1cdf9,
0xdcc91049, 0xe46f396d, 0xee991950, 0xef64e521, 0xf7aeefc9, 0xffffffff};

-
-MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " NaN") {
-  return std::isnan(arg);
-}
-
 }  // namespace


Index: testing/gmock-support.h
diff --git a/testing/gmock-support.h b/testing/gmock-support.h
index 44348b60e0e98bfe56c639f71f70fb1f5e114472..2cdd5b110573d350fe32fcd586a8c86ff99622eb 100644
--- a/testing/gmock-support.h
+++ b/testing/gmock-support.h
@@ -5,6 +5,8 @@
 #ifndef V8_TESTING_GMOCK_SUPPORT_H_
 #define V8_TESTING_GMOCK_SUPPORT_H_

+#include <cmath>
+
 #include "testing/gmock/include/gmock/gmock.h"

 namespace testing {
@@ -29,6 +31,11 @@ class Capture {
 };


+MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " NaN") {
+  return std::isnan(arg);
+}
+
+
 namespace internal {

 template <typename T>


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