<!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>[194548] 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/194548">194548</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-01-04 12:49:33 -0800 (Mon, 04 Jan 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>FTL B3 should do the ArithSub binary snippet
https://bugs.webkit.org/show_bug.cgi?id=152705

Reviewed by Saam Barati.

This implements the ArithSub binary snippet generator in FTL B3.

While doing this, I discovered that the DFG type inference logic for ArithSub contains a
classic mistake: it causes the snippets to kick in when the type set does not contain numbers
rather than kicking in when the type set contains non-numbers. So, the original test that I
wrote for this doesn't work right (it runs to completion but OSR exits ad infinitum). I wrote
a second test that is simpler, and that one shows that the binary snippets &quot;work&quot;. That's
sort of a joke though, since the only way to trigger binary snippets is to never pass numbers
and the only way to actually cause a binary snippet to do meaninful work is to pass numbers.
I filed a bug about this mess: https://bugs.webkit.org/show_bug.cgi?id=152708.

* ftl/FTLLowerDFGToLLVM.cpp:
(JSC::FTL::DFG::LowerDFGToLLVM::compileUntypedBinaryOp):
(JSC::FTL::DFG::LowerDFGToLLVM::compileArithAddOrSub):
(JSC::FTL::DFG::LowerDFGToLLVM::nonSpeculativeCompare):
(JSC::FTL::DFG::LowerDFGToLLVM::emitBinarySnippet):
(JSC::FTL::DFG::LowerDFGToLLVM::allocateCell):
(JSC::FTL::DFG::LowerDFGToLLVM::lowBlock):
(JSC::FTL::DFG::LowerDFGToLLVM::appendOSRExitDescriptor):
* tests/stress/object-sub.js: Added.
(foo):
(things.valueOf):
* tests/stress/untyped-sub.js: Added.
(foo):
(valueOf):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp">trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressobjectsubjs">trunk/Source/JavaScriptCore/tests/stress/object-sub.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressuntypedsubjs">trunk/Source/JavaScriptCore/tests/stress/untyped-sub.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (194547 => 194548)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-01-04 20:39:52 UTC (rev 194547)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-01-04 20:49:33 UTC (rev 194548)
</span><span class="lines">@@ -1,5 +1,38 @@
</span><span class="cx"> 2016-01-04  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        FTL B3 should do the ArithSub binary snippet
+        https://bugs.webkit.org/show_bug.cgi?id=152705
+
+        Reviewed by Saam Barati.
+
+        This implements the ArithSub binary snippet generator in FTL B3.
+
+        While doing this, I discovered that the DFG type inference logic for ArithSub contains a
+        classic mistake: it causes the snippets to kick in when the type set does not contain numbers
+        rather than kicking in when the type set contains non-numbers. So, the original test that I
+        wrote for this doesn't work right (it runs to completion but OSR exits ad infinitum). I wrote
+        a second test that is simpler, and that one shows that the binary snippets &quot;work&quot;. That's
+        sort of a joke though, since the only way to trigger binary snippets is to never pass numbers
+        and the only way to actually cause a binary snippet to do meaninful work is to pass numbers.
+        I filed a bug about this mess: https://bugs.webkit.org/show_bug.cgi?id=152708.
+
+        * ftl/FTLLowerDFGToLLVM.cpp:
+        (JSC::FTL::DFG::LowerDFGToLLVM::compileUntypedBinaryOp):
+        (JSC::FTL::DFG::LowerDFGToLLVM::compileArithAddOrSub):
+        (JSC::FTL::DFG::LowerDFGToLLVM::nonSpeculativeCompare):
+        (JSC::FTL::DFG::LowerDFGToLLVM::emitBinarySnippet):
+        (JSC::FTL::DFG::LowerDFGToLLVM::allocateCell):
+        (JSC::FTL::DFG::LowerDFGToLLVM::lowBlock):
+        (JSC::FTL::DFG::LowerDFGToLLVM::appendOSRExitDescriptor):
+        * tests/stress/object-sub.js: Added.
+        (foo):
+        (things.valueOf):
+        * tests/stress/untyped-sub.js: Added.
+        (foo):
+        (valueOf):
+
+2016-01-04  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
</ins><span class="cx">         Unreviewed, disable FTL B3 for now. I didn't intend to enable it yet.
</span><span class="cx"> 
</span><span class="cx">         * dfg/DFGCommon.h:
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp (194547 => 194548)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2016-01-04 20:39:52 UTC (rev 194547)
+++ trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2016-01-04 20:49:33 UTC (rev 194548)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -51,6 +51,7 @@
</span><span class="cx"> #include &quot;FTLOutput.h&quot;
</span><span class="cx"> #include &quot;FTLThunks.h&quot;
</span><span class="cx"> #include &quot;FTLWeightedTarget.h&quot;
</span><ins>+#include &quot;JITSubGenerator.h&quot;
</ins><span class="cx"> #include &quot;JSArrowFunction.h&quot;
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><span class="cx"> #include &quot;JSGeneratorFunction.h&quot;
</span><span class="lines">@@ -1087,20 +1088,16 @@
</span><span class="cx"> 
</span><span class="cx">         if (constInt32Opt == HasConstInt32OperandOptimization &amp;&amp; leftChild-&gt;isInt32Constant())
</span><span class="cx">             leftOperand.setConstInt32(leftChild-&gt;asInt32());
</span><del>-#if USE(JSVALUE64)
</del><span class="cx">         else if (constDoubleOpt == HasConstDoubleOperandOptimization &amp;&amp; leftChild-&gt;isDoubleConstant())
</span><span class="cx">             leftOperand.setConstDouble(leftChild-&gt;asNumber());
</span><del>-#endif
</del><span class="cx"> 
</span><span class="cx">         if (leftOperand.isConst()) {
</span><span class="cx">             // Because the snippet does not support both operands being constant, if the left
</span><span class="cx">             // operand is already a constant, we'll just pretend the right operand is not.
</span><span class="cx">         } else if (constInt32Opt == HasConstInt32OperandOptimization &amp;&amp; rightChild-&gt;isInt32Constant())
</span><span class="cx">             rightOperand.setConstInt32(rightChild-&gt;asInt32());
</span><del>-#if USE(JSVALUE64)
</del><span class="cx">         else if (constDoubleOpt == HasConstDoubleOperandOptimization &amp;&amp; rightChild-&gt;isDoubleConstant())
</span><span class="cx">             rightOperand.setConstDouble(rightChild-&gt;asNumber());
</span><del>-#endif
</del><span class="cx"> 
</span><span class="cx">         RELEASE_ASSERT(!leftOperand.isConst() || !rightOperand.isConst());
</span><span class="cx"> 
</span><span class="lines">@@ -1747,8 +1744,12 @@
</span><span class="cx">                 DFG_CRASH(m_graph, m_node, &quot;Bad use kind&quot;);
</span><span class="cx">                 break;
</span><span class="cx">             }
</span><del>-            
</del><ins>+
+#if FTL_USES_B3
+            emitBinarySnippet&lt;JITSubGenerator&gt;(operationValueSub);
+#else // FTL_USES_B3
</ins><span class="cx">             compileUntypedBinaryOp&lt;ArithSubDescriptor, DoesNotHaveConstInt32OperandOptimization, DoesNotHaveConstDoubleOperandOptimization&gt;();
</span><ins>+#endif // FTL_USES_B3
</ins><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -7375,6 +7376,70 @@
</span><span class="cx">         setBoolean(m_out.phi(m_out.boolean, fastResult, slowResult));
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+#if FTL_USES_B3
+    enum ScratchFPRUsage {
+        DontNeedScratchFPR,
+        NeedScratchFPR
+    };
+    template&lt;typename BinaryArithOpGenerator, ScratchFPRUsage scratchFPRUsage = DontNeedScratchFPR&gt;
+    void emitBinarySnippet(J_JITOperation_EJJ slowPathFunction)
+    {
+        Node* node = m_node;
+        
+        // FIXME: Make this do exceptions.
+        // https://bugs.webkit.org/show_bug.cgi?id=151686
+            
+        LValue left = lowJSValue(node-&gt;child1());
+        LValue right = lowJSValue(node-&gt;child2());
+
+        SnippetOperand leftOperand(m_state.forNode(node-&gt;child1()).resultType());
+        SnippetOperand rightOperand(m_state.forNode(node-&gt;child2()).resultType());
+            
+        PatchpointValue* patchpoint = m_out.patchpoint(Int64);
+        patchpoint-&gt;append(left, ValueRep::SomeRegister);
+        patchpoint-&gt;append(right, ValueRep::SomeRegister);
+        patchpoint-&gt;append(m_tagMask, ValueRep::reg(GPRInfo::tagMaskRegister));
+        patchpoint-&gt;append(m_tagTypeNumber, ValueRep::reg(GPRInfo::tagTypeNumberRegister));
+        patchpoint-&gt;numGPScratchRegisters = 1;
+        patchpoint-&gt;numFPScratchRegisters = 2;
+        if (scratchFPRUsage == NeedScratchFPR)
+            patchpoint-&gt;numFPScratchRegisters++;
+        patchpoint-&gt;clobber(RegisterSet::macroScratchRegisters());
+        State* state = &amp;m_ftlState;
+        patchpoint-&gt;setGenerator(
+            [=] (CCallHelpers&amp; jit, const StackmapGenerationParams&amp; params) {
+                AllowMacroScratchRegisterUsage allowScratch(jit);
+                    
+                auto generator = Box&lt;BinaryArithOpGenerator&gt;::create(
+                    leftOperand, rightOperand, JSValueRegs(params[0].gpr()),
+                    JSValueRegs(params[1].gpr()), JSValueRegs(params[2].gpr()),
+                    params.fpScratch(0), params.fpScratch(1), params.gpScratch(0),
+                    scratchFPRUsage == NeedScratchFPR ? params.fpScratch(2) : InvalidFPRReg);
+
+                generator-&gt;generateFastPath(jit);
+                generator-&gt;endJumpList().link(&amp;jit);
+                CCallHelpers::Label done = jit.label();
+
+                params.addLatePath(
+                    [=] (CCallHelpers&amp; jit) {
+                        AllowMacroScratchRegisterUsage allowScratch(jit);
+                            
+                        // FIXME: Make this do something.
+                        CCallHelpers::JumpList exceptions;
+
+                        generator-&gt;slowPathJumpList().link(&amp;jit);
+                        callOperation(
+                            *state, params.unavailableRegisters(), jit, node-&gt;origin.semantic,
+                            &amp;exceptions, slowPathFunction, params[0].gpr(), params[1].gpr(),
+                            params[2].gpr());
+                        jit.jump().linkTo(done, &amp;jit);
+                    });
+            });
+
+        setJSValue(patchpoint);
+    }
+#endif // FTL_USES_B3
+
</ins><span class="cx">     LValue allocateCell(LValue allocator, LBasicBlock slowPath)
</span><span class="cx">     {
</span><span class="cx">         LBasicBlock success = FTL_NEW_BLOCK(m_out, (&quot;object allocation success&quot;));
</span><span class="lines">@@ -9860,7 +9925,6 @@
</span><span class="cx">         return m_blocks.get(block);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> #if FTL_USES_B3
</span><span class="cx">     OSRExitDescriptor* appendOSRExitDescriptor(FormattedValue lowValue, Node* highValue)
</span><span class="cx">     {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressobjectsubjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/object-sub.js (0 => 194548)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/object-sub.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/object-sub.js        2016-01-04 20:49:33 UTC (rev 194548)
</span><span class="lines">@@ -0,0 +1,16 @@
</span><ins>+function foo(a, b) {
+    return a - b;
+}
+
+noInline(foo);
+
+var things = [{valueOf: function() { return 4; }}];
+var results = [3];
+
+for (var i = 0; i &lt; 100000; ++i) {
+    var result = foo(things[i % things.length], 1);
+    var expected = results[i % results.length];
+    if (result != expected)
+        throw &quot;Error: bad result for i = &quot; + i + &quot;: &quot; + result;
+}
+
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressuntypedsubjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/untyped-sub.js (0 => 194548)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/untyped-sub.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/untyped-sub.js        2016-01-04 20:49:33 UTC (rev 194548)
</span><span class="lines">@@ -0,0 +1,16 @@
</span><ins>+function foo(a, b) {
+    return a - b;
+}
+
+noInline(foo);
+
+var things = [1, 2.5, &quot;3&quot;, {valueOf: function() { return 4; }}];
+var results = [0, 1.5, 2, 3];
+
+for (var i = 0; i &lt; 100000; ++i) {
+    var result = foo(things[i % things.length], 1);
+    var expected = results[i % results.length];
+    if (result != expected)
+        throw &quot;Error: bad result for i = &quot; + i + &quot;: &quot; + result;
+}
+
</ins></span></pre>
</div>
</div>

</body>
</html>