Reviewers: Sven Panne,

Description:
[turbofan] Improve reduction of Word32And and Int32Add.

TEST=unittests
[email protected]

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

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

Affected files (+45, -29 lines):
  M src/compiler/machine-operator-reducer.h
  M src/compiler/machine-operator-reducer.cc
  M test/unittests/compiler/machine-operator-reducer-unittest.cc


Index: src/compiler/machine-operator-reducer.cc
diff --git a/src/compiler/machine-operator-reducer.cc b/src/compiler/machine-operator-reducer.cc index f8555da33bb7dc1864c3d42caaf8be5defe11d01..8d893beb3e5cfe482671ff184f4965530a7d3bf2 100644
--- a/src/compiler/machine-operator-reducer.cc
+++ b/src/compiler/machine-operator-reducer.cc
@@ -43,8 +43,10 @@ Node* MachineOperatorReducer::Int64Constant(int64_t value) {
 }


-Node* MachineOperatorReducer::Word32And(Node* lhs, uint32_t rhs) {
- return graph()->NewNode(machine()->Word32And(), lhs, Uint32Constant(rhs));
+Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) {
+  Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs);
+  Reduction const reduction = ReduceWord32And(node);
+  return reduction.Changed() ? reduction.replacement() : node;
 }


@@ -66,7 +68,9 @@ Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {


 Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
-  return graph()->NewNode(machine()->Int32Add(), lhs, rhs);
+  Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs);
+  Reduction const reduction = ReduceInt32Add(node);
+  return reduction.Changed() ? reduction.replacement() : node;
 }


@@ -210,15 +214,8 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
       if (m.LeftEqualsRight()) return ReplaceBool(true);  // x == x => true
       break;
     }
-    case IrOpcode::kInt32Add: {
-      Int32BinopMatcher m(node);
-      if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => x
-      if (m.IsFoldable()) {                                  // K + K => K
-        return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) +
-                            static_cast<uint32_t>(m.right().Value()));
-      }
-      break;
-    }
+    case IrOpcode::kInt32Add:
+      return ReduceInt32Add(node);
     case IrOpcode::kInt32Sub: {
       Int32BinopMatcher m(node);
       if (m.right().Is(0)) return Replace(m.left().node());  // x - 0 => x
@@ -466,6 +463,18 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
 }


+Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
+  DCHECK_EQ(IrOpcode::kInt32Add, node->opcode());
+  Int32BinopMatcher m(node);
+  if (m.right().Is(0)) return Replace(m.left().node());  // x + 0 => x
+  if (m.IsFoldable()) {                                  // K + K => K
+    return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) +
+                         bit_cast<uint32_t>(m.right().Value()));
+  }
+  return NoChange();
+}
+
+
 Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
   Int32BinopMatcher m(node);
   if (m.left().Is(0)) return Replace(m.left().node());    // 0 / x => 0
@@ -771,7 +780,7 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
       node->ReplaceInput(0, mleft.left().node());
       node->ReplaceInput(
           1, Int32Constant(m.right().Value() & mleft.right().Value()));
-      Reduction reduction = ReduceWord32And(node);
+      Reduction const reduction = ReduceWord32And(node);
       return reduction.Changed() ? reduction : Changed(node);
     }
   }
@@ -780,22 +789,23 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
     if (mleft.right().HasValue() &&
(mleft.right().Value() & m.right().Value()) == mleft.right().Value()) {
       // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
-      return Replace(graph()->NewNode(
-          machine()->Int32Add(),
-          graph()->NewNode(machine()->Word32And(), mleft.left().node(),
-                           m.right().node()),
-          mleft.right().node()));
+      node->set_op(machine()->Int32Add());
+ node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node()));
+      node->ReplaceInput(1, mleft.right().node());
+      Reduction const reduction = ReduceInt32Add(node);
+      return reduction.Changed() ? reduction : Changed(node);
     }
     if (mleft.left().IsWord32Shl()) {
       Int32BinopMatcher mleftleft(mleft.left().node());
       if (mleftleft.right().Is(
               base::bits::CountTrailingZeros32(m.right().Value()))) {
         // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
-        return Replace(graph()->NewNode(
-            machine()->Int32Add(),
-            graph()->NewNode(machine()->Word32And(), mleft.right().node(),
-                             m.right().node()),
-            mleftleft.node()));
+        node->set_op(machine()->Int32Add());
+        node->ReplaceInput(0,
+ Word32And(mleft.right().node(), m.right().node()));
+        node->ReplaceInput(1, mleftleft.node());
+        Reduction const reduction = ReduceInt32Add(node);
+        return reduction.Changed() ? reduction : Changed(node);
       }
     }
     if (mleft.right().IsWord32Shl()) {
@@ -803,11 +813,11 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
       if (mleftright.right().Is(
               base::bits::CountTrailingZeros32(m.right().Value()))) {
         // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
-        return Replace(graph()->NewNode(
-            machine()->Int32Add(),
-            graph()->NewNode(machine()->Word32And(), mleft.left().node(),
-                             m.right().node()),
-            mleftright.node()));
+        node->set_op(machine()->Int32Add());
+ node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node()));
+        node->ReplaceInput(1, mleftright.node());
+        Reduction const reduction = ReduceInt32Add(node);
+        return reduction.Changed() ? reduction : Changed(node);
       }
     }
   }
Index: src/compiler/machine-operator-reducer.h
diff --git a/src/compiler/machine-operator-reducer.h b/src/compiler/machine-operator-reducer.h index 11887f52004b0d4a00810fedbbeb1a49b68826d2..8200abbf9573553ad74f259f94d89239f7551244 100644
--- a/src/compiler/machine-operator-reducer.h
+++ b/src/compiler/machine-operator-reducer.h
@@ -34,7 +34,10 @@ class MachineOperatorReducer FINAL : public Reducer {
   Node* Uint32Constant(uint32_t value) {
     return Int32Constant(bit_cast<uint32_t>(value));
   }
-  Node* Word32And(Node* lhs, uint32_t rhs);
+  Node* Word32And(Node* lhs, Node* rhs);
+  Node* Word32And(Node* lhs, uint32_t rhs) {
+    return Word32And(lhs, Uint32Constant(rhs));
+  }
   Node* Word32Sar(Node* lhs, uint32_t rhs);
   Node* Word32Shr(Node* lhs, uint32_t rhs);
   Node* Word32Equal(Node* lhs, Node* rhs);
@@ -61,6 +64,7 @@ class MachineOperatorReducer FINAL : public Reducer {
     return Replace(Int64Constant(value));
   }

+  Reduction ReduceInt32Add(Node* node);
   Reduction ReduceInt32Div(Node* node);
   Reduction ReduceUint32Div(Node* node);
   Reduction ReduceInt32Mod(Node* node);
Index: test/unittests/compiler/machine-operator-reducer-unittest.cc
diff --git a/test/unittests/compiler/machine-operator-reducer-unittest.cc b/test/unittests/compiler/machine-operator-reducer-unittest.cc index 3e47ce8d95d8bda7333a295a895e8a00f8769fe2..259841137284cebe7ca7b97865d3ece20375e75c 100644
--- a/test/unittests/compiler/machine-operator-reducer-unittest.cc
+++ b/test/unittests/compiler/machine-operator-reducer-unittest.cc
@@ -536,6 +536,7 @@ TEST_F(MachineOperatorReducerTest, Word32AndWithInt32AddAndConstant) {

   TRACED_FORRANGE(int32_t, l, 1, 31) {
     TRACED_FOREACH(int32_t, k, kInt32Values) {
+      if ((k << l) == 0) continue;
       // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
       Reduction const r = Reduce(graph()->NewNode(
           machine()->Word32And(),
@@ -768,6 +769,7 @@ TEST_F(MachineOperatorReducerTest,
   Node* const p0 = Parameter(0);
   TRACED_FOREACH(int32_t, k, kInt32Values) {
     TRACED_FORRANGE(int32_t, l, 1, 31) {
+      if ((k << l) == 0) continue;
       // (x + (K << L)) >> L << L => (x & (-1 << L)) + (K << L)
       Reduction const r = Reduce(graph()->NewNode(
           machine()->Word32Shl(),


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