[turbofan] Optimize Uint32LessThan with Word32Sar.
authorbmeurer@chromium.org <bmeurer@chromium.org>
Fri, 10 Oct 2014 10:23:04 +0000 (10:23 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org>
Fri, 10 Oct 2014 10:23:04 +0000 (10:23 +0000)
TEST=unittests
R=jarin@chromium.org

Review URL: https://codereview.chromium.org/648663002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24520 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/compiler/machine-operator-reducer.cc
test/unittests/compiler/graph-unittest.cc
test/unittests/compiler/graph-unittest.h
test/unittests/compiler/machine-operator-reducer-unittest.cc

index 193b033..f728dbf 100644 (file)
@@ -339,6 +339,20 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
         return ReplaceBool(m.left().Value() < m.right().Value());
       }
       if (m.LeftEqualsRight()) return ReplaceBool(false);  // x < x => false
+      if (m.left().IsWord32Sar() && m.right().HasValue()) {
+        Int32BinopMatcher mleft(m.left().node());
+        if (mleft.right().HasValue()) {
+          // (x >> K) < C => x < (C << K) | (2^K - 1)
+          // when C < (M >> K)
+          const uint32_t c = m.right().Value();
+          const uint32_t k = mleft.right().Value() & 0x1f;
+          if (c < static_cast<uint32_t>(kMaxInt >> k)) {
+            node->ReplaceInput(0, mleft.left().node());
+            node->ReplaceInput(1, Uint32Constant((c << k) | ((1 << k) - 1)));
+            return Changed(node);
+          }
+        }
+      }
       break;
     }
     case IrOpcode::kUint32LessThanOrEqual: {
index 7c6232e..090d572 100644 (file)
@@ -949,6 +949,7 @@ IS_BINOP_MATCHER(Word64Shl)
 IS_BINOP_MATCHER(Word64Equal)
 IS_BINOP_MATCHER(Int32AddWithOverflow)
 IS_BINOP_MATCHER(Int32Mul)
+IS_BINOP_MATCHER(Uint32LessThan)
 IS_BINOP_MATCHER(Uint32LessThanOrEqual)
 #undef IS_BINOP_MATCHER
 
index bfed062..37fbe8a 100644 (file)
@@ -150,6 +150,8 @@ Matcher<Node*> IsInt32AddWithOverflow(const Matcher<Node*>& lhs_matcher,
                                       const Matcher<Node*>& rhs_matcher);
 Matcher<Node*> IsInt32Mul(const Matcher<Node*>& lhs_matcher,
                           const Matcher<Node*>& rhs_matcher);
+Matcher<Node*> IsUint32LessThan(const Matcher<Node*>& lhs_matcher,
+                                const Matcher<Node*>& rhs_matcher);
 Matcher<Node*> IsUint32LessThanOrEqual(const Matcher<Node*>& lhs_matcher,
                                        const Matcher<Node*>& rhs_matcher);
 Matcher<Node*> IsChangeFloat64ToInt32(const Matcher<Node*>& input_matcher);
index 3a5eb1a..6c46a5b 100644 (file)
@@ -656,6 +656,29 @@ TEST_F(MachineOperatorReducerTest, Int32SubWithOverflowWithConstant) {
 
 
 // -----------------------------------------------------------------------------
+// Uint32LessThan
+
+
+TEST_F(MachineOperatorReducerTest, Uint32LessThanWithWord32Sar) {
+  Node* const p0 = Parameter(0);
+  TRACED_FORRANGE(uint32_t, shift, 1, 3) {
+    const uint32_t limit = (kMaxInt >> shift) - 1;
+    Node* const node = graph()->NewNode(
+        machine()->Uint32LessThan(),
+        graph()->NewNode(machine()->Word32Sar(), p0, Uint32Constant(shift)),
+        Uint32Constant(limit));
+
+    Reduction r = Reduce(node);
+    ASSERT_TRUE(r.Changed());
+    EXPECT_THAT(
+        r.replacement(),
+        IsUint32LessThan(p0, IsInt32Constant(bit_cast<int32_t>(
+                                 (limit << shift) | ((1u << shift) - 1)))));
+  }
+}
+
+
+// -----------------------------------------------------------------------------
 // Store