Title: [243065] trunk/Source/_javascript_Core
Revision
243065
Author
[email protected]
Date
2019-03-18 08:29:10 -0700 (Mon, 18 Mar 2019)

Log Message

B3 should reduce Shl(<S|Z>Shr(@x, @const), @const) to BitAnd(@x, -(1<<@const))
https://bugs.webkit.org/show_bug.cgi?id=152164

Reviewed by Filip Pizlo.

Turn this: Shl(<S|Z>Shr(@x, @const), @const)
Into this: BitAnd(@x, -(1<<@const))

I added two tests: one for ZShr/32 bits, and one for SShr/64 bits, I think it is enough coverage (no reason for any interaction between the signedness of the shift and the bitwidth).
I also modified a few adjacent tests to remove undefined behaviours.

* b3/B3ReduceStrength.cpp:
* b3/testb3.cpp:
(JSC::B3::testShlImms):
(JSC::B3::testShlArgImm):
(JSC::B3::testShlSShrArgImm):
(JSC::B3::testShlImms32):
(JSC::B3::testShlArgImm32):
(JSC::B3::testShlZShrArgImm32):
(JSC::B3::run):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (243064 => 243065)


--- trunk/Source/_javascript_Core/ChangeLog	2019-03-18 15:26:16 UTC (rev 243064)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-03-18 15:29:10 UTC (rev 243065)
@@ -1,3 +1,26 @@
+2019-03-18  Robin Morisset  <[email protected]>
+
+        B3 should reduce Shl(<S|Z>Shr(@x, @const), @const) to BitAnd(@x, -(1<<@const))
+        https://bugs.webkit.org/show_bug.cgi?id=152164
+
+        Reviewed by Filip Pizlo.
+
+        Turn this: Shl(<S|Z>Shr(@x, @const), @const)
+        Into this: BitAnd(@x, -(1<<@const))
+
+        I added two tests: one for ZShr/32 bits, and one for SShr/64 bits, I think it is enough coverage (no reason for any interaction between the signedness of the shift and the bitwidth).
+        I also modified a few adjacent tests to remove undefined behaviours.
+
+        * b3/B3ReduceStrength.cpp:
+        * b3/testb3.cpp:
+        (JSC::B3::testShlImms):
+        (JSC::B3::testShlArgImm):
+        (JSC::B3::testShlSShrArgImm):
+        (JSC::B3::testShlImms32):
+        (JSC::B3::testShlArgImm32):
+        (JSC::B3::testShlZShrArgImm32):
+        (JSC::B3::run):
+
 2019-03-17  Robin Morisset  <[email protected]>
 
         ParserError can be shrunk by 8 bytes

Modified: trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp (243064 => 243065)


--- trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp	2019-03-18 15:26:16 UTC (rev 243064)
+++ trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp	2019-03-18 15:29:10 UTC (rev 243065)
@@ -1200,6 +1200,19 @@
                 break;
             }
 
+            // Turn this: Shl(<S|Z>Shr(@x, @const), @const)
+            // Into this: BitAnd(@x, -(1<<@const))
+            if ((m_value->child(0)->opcode() == SShr || m_value->child(0)->opcode() == ZShr)
+                && m_value->child(0)->child(1)->hasInt()
+                && m_value->child(1)->hasInt()
+                && m_value->child(0)->child(1)->asInt() == m_value->child(1)->asInt()) {
+                int shiftAmount = m_value->child(1)->asInt() & (m_value->type() == Int32 ? 31 : 63);
+                Value* newConst = m_proc.addIntConstant(m_value, - static_cast<int64_t>(1ull << shiftAmount));
+                m_insertionSet.insertValue(m_index, newConst);
+                replaceWithNew<Value>(BitAnd, m_value->origin(), m_value->child(0)->child(0), newConst);
+                break;
+            }
+
             handleShiftAmount();
             break;
 

Modified: trunk/Source/_javascript_Core/b3/testb3.cpp (243064 => 243065)


--- trunk/Source/_javascript_Core/b3/testb3.cpp	2019-03-18 15:26:16 UTC (rev 243064)
+++ trunk/Source/_javascript_Core/b3/testb3.cpp	2019-03-18 15:29:10 UTC (rev 243065)
@@ -3777,6 +3777,7 @@
             root->appendNew<Const64Value>(proc, Origin(), a),
             root->appendNew<Const32Value>(proc, Origin(), b)));
 
+    b = b & 0x3f; // to avoid undefined behaviour below
     CHECK(compileAndRun<int64_t>(proc) == (a << b));
 }
 
@@ -3791,9 +3792,28 @@
             root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
             root->appendNew<Const32Value>(proc, Origin(), b)));
 
+    b = b & 0x3f; // to avoid undefined behaviour below
     CHECK(compileAndRun<int64_t>(proc, a) == (a << b));
 }
 
+void testShlSShrArgImm(int64_t a, int64_t b)
+{
+    Procedure proc;
+    BasicBlock* root = proc.addBlock();
+    Value* argA = root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0);
+    Value* constB = root->appendNew<Const32Value>(proc, Origin(), b);
+    Value* innerShift = root->appendNew<Value>(proc, SShr, Origin(), argA, constB);
+    root->appendNewControlValue(
+        proc, Return, Origin(),
+        root->appendNew<Value>(
+            proc, Shl, Origin(),
+            innerShift,
+            constB));
+
+    b = b & 0x3f; // to avoid undefined behaviour below
+    CHECK(compileAndRun<int64_t>(proc, a) == ((a >> b) << b));
+}
+
 void testShlArg32(int32_t a)
 {
     Procedure proc;
@@ -3837,6 +3857,7 @@
             root->appendNew<Const32Value>(proc, Origin(), a),
             root->appendNew<Const32Value>(proc, Origin(), b)));
 
+    b = b & 0x1f; // to avoid undefined behaviour below
     CHECK(compileAndRun<int32_t>(proc) == (a << b));
 }
 
@@ -3853,9 +3874,30 @@
                 root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)),
             root->appendNew<Const32Value>(proc, Origin(), b)));
 
+    b = b & 0x1f; // to avoid undefined behaviour below
     CHECK(compileAndRun<int32_t>(proc, a) == (a << b));
 }
 
+void testShlZShrArgImm32(int32_t a, int32_t b)
+{
+    Procedure proc;
+    BasicBlock* root = proc.addBlock();
+    Value* argA = root->appendNew<Value>(
+        proc, Trunc, Origin(),
+        root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0));
+    Value* constB = root->appendNew<Const32Value>(proc, Origin(), b);
+    Value* innerShift = root->appendNew<Value>(proc, ZShr, Origin(), argA, constB);
+    root->appendNewControlValue(
+        proc, Return, Origin(),
+        root->appendNew<Value>(
+            proc, Shl, Origin(),
+            innerShift,
+            constB));
+
+    b = b & 0x1f; // to avoid undefined behaviour below
+    CHECK(compileAndRun<int32_t>(proc, a) == static_cast<int32_t>((static_cast<uint32_t>(a) >> b) << b));
+}
+
 void testSShrArgs(int64_t a, int64_t b)
 {
     Procedure proc;
@@ -17309,6 +17351,13 @@
     RUN(testShlArgImm(0xffffffffffffffff, 0));
     RUN(testShlArgImm(0xffffffffffffffff, 1));
     RUN(testShlArgImm(0xffffffffffffffff, 63));
+    RUN(testShlSShrArgImm(1, 0));
+    RUN(testShlSShrArgImm(1, 1));
+    RUN(testShlSShrArgImm(1, 62));
+    RUN(testShlSShrArgImm(1, 65));
+    RUN(testShlSShrArgImm(0xffffffffffffffff, 0));
+    RUN(testShlSShrArgImm(0xffffffffffffffff, 1));
+    RUN(testShlSShrArgImm(0xffffffffffffffff, 63));
     RUN(testShlArg32(2));
     RUN(testShlArgs32(1, 0));
     RUN(testShlArgs32(1, 1));
@@ -17327,9 +17376,17 @@
     RUN(testShlArgImm32(1, 0));
     RUN(testShlArgImm32(1, 1));
     RUN(testShlArgImm32(1, 62));
+    RUN(testShlArgImm32(1, 33));
     RUN(testShlArgImm32(0xffffffff, 0));
     RUN(testShlArgImm32(0xffffffff, 1));
     RUN(testShlArgImm32(0xffffffff, 63));
+    RUN(testShlZShrArgImm32(1, 0));
+    RUN(testShlZShrArgImm32(1, 1));
+    RUN(testShlZShrArgImm32(1, 62));
+    RUN(testShlZShrArgImm32(1, 33));
+    RUN(testShlZShrArgImm32(0xffffffff, 0));
+    RUN(testShlZShrArgImm32(0xffffffff, 1));
+    RUN(testShlZShrArgImm32(0xffffffff, 63));
 
     RUN(testSShrArgs(1, 0));
     RUN(testSShrArgs(1, 1));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to