Reviewers: jarin,

Message:
PTAL

Description:
[turbofan] Combine Word32And with Int32Add and negative power of two.

TEST=unittests

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

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

Affected files (+36, -0 lines):
  M src/compiler/machine-operator-reducer.cc
  M src/compiler/node-matchers.h
  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 6c4d5dcb953725ec4ba114c25c8add56ce7bceef..470e71d6738cf8a2a51d45f767d3ef3fc429144d 100644
--- a/src/compiler/machine-operator-reducer.cc
+++ b/src/compiler/machine-operator-reducer.cc
@@ -137,6 +137,19 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
           return Changed(node);
         }
       }
+      if (m.left().IsInt32Add() && m.right().IsNegativePowerOf2()) {
+        Int32BinopMatcher mleft(m.left().node());
+        if (mleft.right().HasValue() &&
+            (mleft.right().Value() & m.right().Value()) ==
+                mleft.right().Value()) {
+          // (x + K) & K => (x & K) + K
+          return Replace(graph()->NewNode(
+              machine()->Int32Add(),
+              graph()->NewNode(machine()->Word32And(), mleft.left().node(),
+                               m.right().node()),
+              mleft.right().node()));
+        }
+      }
       break;
     }
     case IrOpcode::kWord32Or: {
Index: src/compiler/node-matchers.h
diff --git a/src/compiler/node-matchers.h b/src/compiler/node-matchers.h
index c748c61e6a9cc0624c074bde0a579378b067abff..33ddf9a10df26d868b1f7915642a0aacc73a1f23 100644
--- a/src/compiler/node-matchers.h
+++ b/src/compiler/node-matchers.h
@@ -80,6 +80,10 @@ struct IntMatcher FINAL : public ValueMatcher<T, kOpcode> {
     return this->HasValue() && this->Value() > 0 &&
            (this->Value() & (this->Value() - 1)) == 0;
   }
+  bool IsNegativePowerOf2() const {
+    return this->HasValue() && this->Value() < 0 &&
+           (-this->Value() & (-this->Value() - 1)) == 0;
+  }
 };

 typedef IntMatcher<int32_t, IrOpcode::kInt32Constant> Int32Matcher;
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 d8640163b3e9d9d6e977f0aacc4d9177882cc36c..6e1b12f79e0449c1fb974855e2edff1a9c6100be 100644
--- a/test/unittests/compiler/machine-operator-reducer-unittest.cc
+++ b/test/unittests/compiler/machine-operator-reducer-unittest.cc
@@ -526,6 +526,25 @@ TEST_F(MachineOperatorReducerTest, Word32AndWithWord32AndWithConstant) {
 }


+TEST_F(MachineOperatorReducerTest, Word32AndWithInt32AddAndConstant) {
+  Node* const p0 = Parameter(0);
+
+  TRACED_FOREACH(int32_t, k, kInt32Values) {
+    TRACED_FORRANGE(int32_t, l, 1, 31) {
+      // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
+      Reduction const r = Reduce(graph()->NewNode(
+          machine()->Word32And(),
+ graph()->NewNode(machine()->Int32Add(), p0, Int32Constant(k << l)),
+          Int32Constant(-1 << l)));
+      ASSERT_TRUE(r.Changed());
+      EXPECT_THAT(r.replacement(),
+                  IsInt32Add(IsWord32And(p0, IsInt32Constant(-1 << l)),
+                             IsInt32Constant(k << l)));
+    }
+  }
+}
+
+
// -----------------------------------------------------------------------------
 // Word32Xor



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