<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[195728] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/195728">195728</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-01-27 21:21:57 -0800 (Wed, 27 Jan 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>B3 IntRange analysis should know more about shifting
https://bugs.webkit.org/show_bug.cgi?id=153568

Reviewed by Benjamin Poulain.

This teaches the IntRange analysis that the result of a right shift is usually better than
the worst-case mask based on the shift amount. In fact, you can reach useful conclusions
from looking at the IntRange of the input. This helps because Octane/crypto does something
like:

    CheckMul((@x &amp; $268435455) &gt;&gt; 14, @y &gt;&gt; 14, ...)

If you consider just the shifts, then this may overflow. But if you consider that @x is
first masked, then the IntRange coming out of the first shift is tight enough to prove that
the CheckMul cannot overflow.

* b3/B3ReduceStrength.cpp:
* b3/testb3.cpp:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp">trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3testb3cpp">trunk/Source/JavaScriptCore/b3/testb3.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (195727 => 195728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-01-28 04:42:36 UTC (rev 195727)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-01-28 05:21:57 UTC (rev 195728)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2016-01-27  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        B3 IntRange analysis should know more about shifting
+        https://bugs.webkit.org/show_bug.cgi?id=153568
+
+        Reviewed by Benjamin Poulain.
+
+        This teaches the IntRange analysis that the result of a right shift is usually better than
+        the worst-case mask based on the shift amount. In fact, you can reach useful conclusions
+        from looking at the IntRange of the input. This helps because Octane/crypto does something
+        like:
+
+            CheckMul((@x &amp; $268435455) &gt;&gt; 14, @y &gt;&gt; 14, ...)
+
+        If you consider just the shifts, then this may overflow. But if you consider that @x is
+        first masked, then the IntRange coming out of the first shift is tight enough to prove that
+        the CheckMul cannot overflow.
+
+        * b3/B3ReduceStrength.cpp:
+        * b3/testb3.cpp:
+
</ins><span class="cx"> 2016-01-27  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] adjustFrameAndStackInOSRExitCompilerThunk() can trash values in FTL
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp (195727 => 195728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2016-01-28 04:42:36 UTC (rev 195727)
+++ trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2016-01-28 05:21:57 UTC (rev 195728)
</span><span class="lines">@@ -88,6 +88,8 @@
</span><span class="cx"> 
</span><span class="cx"> bool verbose = false;
</span><span class="cx"> 
</span><ins>+// FIXME: This IntRange stuff should be refactored into a general constant propagator. It's weird
+// that it's just sitting here in this file.
</ins><span class="cx"> class IntRange {
</span><span class="cx"> public:
</span><span class="cx">     IntRange()
</span><span class="lines">@@ -162,25 +164,6 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    template&lt;typename T&gt;
-    static IntRange rangeForSShr(int32_t shiftAmount)
-    {
-        return IntRange(top&lt;T&gt;().min() &gt;&gt; shiftAmount, top&lt;T&gt;().max() &gt;&gt; shiftAmount);
-    }
-
-    static IntRange rangeForSShr(int32_t shiftAmount, Type type)
-    {
-        switch (type) {
-        case Int32:
-            return rangeForSShr&lt;int32_t&gt;(shiftAmount);
-        case Int64:
-            return rangeForSShr&lt;int64_t&gt;(shiftAmount);
-        default:
-            RELEASE_ASSERT_NOT_REACHED();
-            return IntRange();
-        }
-    }
-
</del><span class="cx">     int64_t min() const { return m_min; }
</span><span class="cx">     int64_t max() const { return m_max; }
</span><span class="cx"> 
</span><span class="lines">@@ -255,8 +238,8 @@
</span><span class="cx">     template&lt;typename T&gt;
</span><span class="cx">     IntRange shl(int32_t shiftAmount)
</span><span class="cx">     {
</span><del>-        T newMin = static_cast&lt;T&gt;(m_min) &lt;&lt; shiftAmount;
-        T newMax = static_cast&lt;T&gt;(m_max) &lt;&lt; shiftAmount;
</del><ins>+        T newMin = static_cast&lt;T&gt;(m_min) &lt;&lt; static_cast&lt;T&gt;(shiftAmount);
+        T newMax = static_cast&lt;T&gt;(m_max) &lt;&lt; static_cast&lt;T&gt;(shiftAmount);
</ins><span class="cx"> 
</span><span class="cx">         if ((newMin &gt;&gt; shiftAmount) != static_cast&lt;T&gt;(m_min))
</span><span class="cx">             newMin = std::numeric_limits&lt;T&gt;::min();
</span><span class="lines">@@ -280,6 +263,61 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     template&lt;typename T&gt;
</span><ins>+    IntRange sShr(int32_t shiftAmount)
+    {
+        T newMin = static_cast&lt;T&gt;(m_min) &gt;&gt; static_cast&lt;T&gt;(shiftAmount);
+        T newMax = static_cast&lt;T&gt;(m_max) &gt;&gt; static_cast&lt;T&gt;(shiftAmount);
+
+        return IntRange(newMin, newMax);
+    }
+
+    IntRange sShr(int32_t shiftAmount, Type type)
+    {
+        switch (type) {
+        case Int32:
+            return sShr&lt;int32_t&gt;(shiftAmount);
+        case Int64:
+            return sShr&lt;int64_t&gt;(shiftAmount);
+        default:
+            RELEASE_ASSERT_NOT_REACHED();
+            return IntRange();
+        }
+    }
+
+    template&lt;typename T&gt;
+    IntRange zShr(int32_t shiftAmount)
+    {
+        // This is an awkward corner case for all of the other logic.
+        if (!shiftAmount)
+            return *this;
+
+        // If the input range may be negative, then all we can say about the output range is that it
+        // will be masked. That's because -1 right shifted just produces that mask.
+        if (m_min &lt; 0)
+            return rangeForZShr&lt;T&gt;(shiftAmount);
+
+        // If the input range is non-negative, then this just brings the range closer to zero.
+        typedef typename std::make_unsigned&lt;T&gt;::type UnsignedT;
+        UnsignedT newMin = static_cast&lt;UnsignedT&gt;(m_min) &gt;&gt; static_cast&lt;UnsignedT&gt;(shiftAmount);
+        UnsignedT newMax = static_cast&lt;UnsignedT&gt;(m_max) &gt;&gt; static_cast&lt;UnsignedT&gt;(shiftAmount);
+        
+        return IntRange(newMin, newMax);
+    }
+
+    IntRange zShr(int32_t shiftAmount, Type type)
+    {
+        switch (type) {
+        case Int32:
+            return zShr&lt;int32_t&gt;(shiftAmount);
+        case Int64:
+            return zShr&lt;int64_t&gt;(shiftAmount);
+        default:
+            RELEASE_ASSERT_NOT_REACHED();
+            return IntRange();
+        }
+    }
+
+    template&lt;typename T&gt;
</ins><span class="cx">     IntRange add(const IntRange&amp; other)
</span><span class="cx">     {
</span><span class="cx">         if (couldOverflowAdd&lt;T&gt;(other))
</span><span class="lines">@@ -1955,19 +1993,24 @@
</span><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         case SShr:
</span><del>-            if (value-&gt;child(1)-&gt;hasInt32())
-                return IntRange::rangeForSShr(value-&gt;child(1)-&gt;asInt32(), value-&gt;type());
</del><ins>+            if (value-&gt;child(1)-&gt;hasInt32()) {
+                return rangeFor(value-&gt;child(0), timeToLive - 1).sShr(
+                    value-&gt;child(1)-&gt;asInt32(), value-&gt;type());
+            }
</ins><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         case ZShr:
</span><del>-            if (value-&gt;child(1)-&gt;hasInt32())
-                return IntRange::rangeForZShr(value-&gt;child(1)-&gt;asInt32(), value-&gt;type());
</del><ins>+            if (value-&gt;child(1)-&gt;hasInt32()) {
+                return rangeFor(value-&gt;child(0), timeToLive - 1).zShr(
+                    value-&gt;child(1)-&gt;asInt32(), value-&gt;type());
+            }
</ins><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         case Shl:
</span><del>-            if (value-&gt;child(1)-&gt;hasInt32())
</del><ins>+            if (value-&gt;child(1)-&gt;hasInt32()) {
</ins><span class="cx">                 return rangeFor(value-&gt;child(0), timeToLive - 1).shl(
</span><span class="cx">                     value-&gt;child(1)-&gt;asInt32(), value-&gt;type());
</span><ins>+            }
</ins><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         case Add:
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3testb3cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/testb3.cpp (195727 => 195728)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/testb3.cpp        2016-01-28 04:42:36 UTC (rev 195727)
+++ trunk/Source/JavaScriptCore/b3/testb3.cpp        2016-01-28 05:21:57 UTC (rev 195728)
</span><span class="lines">@@ -7908,6 +7908,45 @@
</span><span class="cx">     CHECK(invoke&lt;int&gt;(*code) == 42);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void testCheckMul64SShr()
+{
+    Procedure proc;
+    BasicBlock* root = proc.addBlock();
+    Value* arg1 = root-&gt;appendNew&lt;Value&gt;(
+        proc, SShr, Origin(),
+        root-&gt;appendNew&lt;ArgumentRegValue&gt;(proc, Origin(), GPRInfo::argumentGPR0),
+        root-&gt;appendNew&lt;Const32Value&gt;(proc, Origin(), 1));
+    Value* arg2 = root-&gt;appendNew&lt;Value&gt;(
+        proc, SShr, Origin(),
+        root-&gt;appendNew&lt;ArgumentRegValue&gt;(proc, Origin(), GPRInfo::argumentGPR1),
+        root-&gt;appendNew&lt;Const32Value&gt;(proc, Origin(), 1));
+    CheckValue* checkMul = root-&gt;appendNew&lt;CheckValue&gt;(proc, CheckMul, Origin(), arg1, arg2);
+    checkMul-&gt;append(arg1);
+    checkMul-&gt;append(arg2);
+    checkMul-&gt;setGenerator(
+        [&amp;] (CCallHelpers&amp; jit, const StackmapGenerationParams&amp; params) {
+            AllowMacroScratchRegisterUsage allowScratch(jit);
+            CHECK(params.size() == 2);
+            CHECK(params[0].isGPR());
+            CHECK(params[1].isGPR());
+            jit.convertInt64ToDouble(params[0].gpr(), FPRInfo::fpRegT0);
+            jit.convertInt64ToDouble(params[1].gpr(), FPRInfo::fpRegT1);
+            jit.mulDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0);
+            jit.emitFunctionEpilogue();
+            jit.ret();
+        });
+    root-&gt;appendNew&lt;ControlValue&gt;(
+        proc, Return, Origin(),
+        root-&gt;appendNew&lt;Value&gt;(proc, IToD, Origin(), checkMul));
+
+    auto code = compile(proc);
+
+    CHECK(invoke&lt;double&gt;(*code, 0ll, 42ll) == 0.0);
+    CHECK(invoke&lt;double&gt;(*code, 1ll, 42ll) == 0.0);
+    CHECK(invoke&lt;double&gt;(*code, 42ll, 42ll) == (42.0 / 2.0) * (42.0 / 2.0));
+    CHECK(invoke&lt;double&gt;(*code, 10000000000ll, 10000000000ll) == 25000000000000000000.0);
+}
+
</ins><span class="cx"> template&lt;typename LeftFunctor, typename RightFunctor, typename InputType&gt;
</span><span class="cx"> void genericTestCompare(
</span><span class="cx">     B3::Opcode opcode, const LeftFunctor&amp; leftFunctor, const RightFunctor&amp; rightFunctor,
</span><span class="lines">@@ -11340,6 +11379,8 @@
</span><span class="cx">     RUN(testSShrShl64(42000000000, 8, 8));
</span><span class="cx">     RUN(testSShrShl64(-42000000000, 8, 8));
</span><span class="cx"> 
</span><ins>+    RUN(testCheckMul64SShr());
+
</ins><span class="cx">     if (tasks.isEmpty())
</span><span class="cx">         usage();
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>