<!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>[164243] 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/164243">164243</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2014-02-17 12:23:34 -0800 (Mon, 17 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>FTL should support ToPrimitive and the DFG should fold it correctly
https://bugs.webkit.org/show_bug.cgi?id=128892

Reviewed by Geoffrey Garen.

* dfg/DFGAbstractInterpreterInlines.h:
(JSC::DFG::AbstractInterpreter&lt;AbstractStateType&gt;::executeEffects):
* dfg/DFGConstantFoldingPhase.cpp:
(JSC::DFG::ConstantFoldingPhase::foldConstants):
* dfg/DFGSpeculativeJIT64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* ftl/FTLCapabilities.cpp:
(JSC::FTL::canCompile):
* ftl/FTLLowerDFGToLLVM.cpp:
(JSC::FTL::LowerDFGToLLVM::compileNode):
(JSC::FTL::LowerDFGToLLVM::compileToPrimitive):
* tests/stress/fold-to-primitive-in-cfa.js: Added.
(foo):
(.result.foo):
* tests/stress/fold-to-primitive-to-identity-in-cfa.js: Added.
(foo):
(.result.foo):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGAbstractInterpreterInlinesh">trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGConstantFoldingPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLCapabilitiescpp">trunk/Source/JavaScriptCore/ftl/FTLCapabilities.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp">trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressfoldtoprimitiveincfajs">trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-in-cfa.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressfoldtoprimitivetoidentityincfajs">trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-to-identity-in-cfa.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/ChangeLog        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -1,5 +1,30 @@
</span><span class="cx"> 2014-02-17  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        FTL should support ToPrimitive and the DFG should fold it correctly
+        https://bugs.webkit.org/show_bug.cgi?id=128892
+
+        Reviewed by Geoffrey Garen.
+
+        * dfg/DFGAbstractInterpreterInlines.h:
+        (JSC::DFG::AbstractInterpreter&lt;AbstractStateType&gt;::executeEffects):
+        * dfg/DFGConstantFoldingPhase.cpp:
+        (JSC::DFG::ConstantFoldingPhase::foldConstants):
+        * dfg/DFGSpeculativeJIT64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * ftl/FTLCapabilities.cpp:
+        (JSC::FTL::canCompile):
+        * ftl/FTLLowerDFGToLLVM.cpp:
+        (JSC::FTL::LowerDFGToLLVM::compileNode):
+        (JSC::FTL::LowerDFGToLLVM::compileToPrimitive):
+        * tests/stress/fold-to-primitive-in-cfa.js: Added.
+        (foo):
+        (.result.foo):
+        * tests/stress/fold-to-primitive-to-identity-in-cfa.js: Added.
+        (foo):
+        (.result.foo):
+
+2014-02-17  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
</ins><span class="cx">         Register preservation wrapper should know about the possibility of callee-saved FPRs
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=128923
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGAbstractInterpreterInlinesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -1176,39 +1176,20 @@
</span><span class="cx">         
</span><span class="cx">         ASSERT(node-&gt;child1().useKind() == UntypedUse);
</span><span class="cx">         
</span><del>-        AbstractValue&amp; source = forNode(node-&gt;child1());
-        AbstractValue&amp; destination = forNode(node);
</del><ins>+        if (!forNode(node-&gt;child1()).m_type) {
+            m_state.setIsValid(false);
+            break;
+        }
</ins><span class="cx">         
</span><del>-        // NB. The more canonical way of writing this would have been:
-        //
-        // destination = source;
-        // if (destination.m_type &amp; !(SpecFullNumber | SpecString | SpecBoolean)) {
-        //     destination.filter(SpecFullNumber | SpecString | SpecBoolean);
-        //     AbstractValue string;
-        //     string.set(vm-&gt;stringStructure);
-        //     destination.merge(string);
-        // }
-        //
-        // The reason why this would, in most other cases, have been better is that
-        // then destination would preserve any non-SpeculatedType knowledge of source.
-        // As it stands, the code below forgets any non-SpeculatedType knowledge that
-        // source would have had. Fortunately, though, for things like strings and
-        // numbers and booleans, we don't care about the non-SpeculatedType knowedge:
-        // the structure won't tell us anything we don't already know, and neither
-        // will ArrayModes. And if the source was a meaningful constant then we
-        // would have handled that above. Unfortunately, this does mean that
-        // ToPrimitive will currently forget string constants. But that's not a big
-        // deal since we don't do any optimization on those currently.
</del><ins>+        if (!(forNode(node-&gt;child1()).m_type &amp; ~(SpecFullNumber | SpecBoolean | SpecString))) {
+            m_state.setFoundConstants(true);
+            forNode(node) = forNode(node-&gt;child1());
+            break;
+        }
</ins><span class="cx">         
</span><span class="cx">         clobberWorld(node-&gt;origin.semantic, clobberLimit);
</span><span class="cx">         
</span><del>-        SpeculatedType type = source.m_type;
-        if (type &amp; ~(SpecFullNumber | SpecString | SpecBoolean))
-            type = (SpecHeapTop &amp; ~SpecCell) | SpecString;
-
-        destination.setType(type);
-        if (destination.isClear())
-            m_state.setIsValid(false);
</del><ins>+        forNode(node).setType((SpecHeapTop &amp; ~SpecCell) | SpecString);
</ins><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx">         
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGConstantFoldingPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/dfg/DFGConstantFoldingPhase.cpp        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -326,8 +326,11 @@
</span><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="cx"> 
</span><del>-            case StoreBarrier:
-            case StoreBarrierWithNullCheck: {
</del><ins>+            case ToPrimitive: {
+                if (m_state.forNode(node-&gt;child1()).m_type &amp; ~(SpecFullNumber | SpecBoolean | SpecString))
+                    break;
+                
+                node-&gt;convertToIdentity();
</ins><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -3416,19 +3416,15 @@
</span><span class="cx">         
</span><span class="cx">         op1.use();
</span><span class="cx">         
</span><del>-        if (!(m_state.forNode(node-&gt;child1()).m_type &amp; ~(SpecFullNumber | SpecBoolean)))
-            m_jit.move(op1GPR, resultGPR);
-        else {
-            MacroAssembler::Jump alreadyPrimitive = m_jit.branchTest64(MacroAssembler::NonZero, op1GPR, GPRInfo::tagMaskRegister);
-            MacroAssembler::Jump notPrimitive = m_jit.branchPtr(MacroAssembler::NotEqual, MacroAssembler::Address(op1GPR, JSCell::structureOffset()), MacroAssembler::TrustedImmPtr(m_jit.vm()-&gt;stringStructure.get()));
-            
-            alreadyPrimitive.link(&amp;m_jit);
-            m_jit.move(op1GPR, resultGPR);
-            
-            addSlowPathGenerator(
-                slowPathCall(notPrimitive, this, operationToPrimitive, resultGPR, op1GPR));
-        }
</del><ins>+        MacroAssembler::Jump alreadyPrimitive = m_jit.branchTest64(MacroAssembler::NonZero, op1GPR, GPRInfo::tagMaskRegister);
+        MacroAssembler::Jump notPrimitive = m_jit.branchPtr(MacroAssembler::NotEqual, MacroAssembler::Address(op1GPR, JSCell::structureOffset()), MacroAssembler::TrustedImmPtr(m_jit.vm()-&gt;stringStructure.get()));
</ins><span class="cx">         
</span><ins>+        alreadyPrimitive.link(&amp;m_jit);
+        m_jit.move(op1GPR, resultGPR);
+        
+        addSlowPathGenerator(
+            slowPathCall(notPrimitive, this, operationToPrimitive, resultGPR, op1GPR));
+        
</ins><span class="cx">         jsValueResult(resultGPR, node, UseChildrenCalledExplicitly);
</span><span class="cx">         break;
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLCapabilitiescpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLCapabilities.cpp (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLCapabilities.cpp        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/ftl/FTLCapabilities.cpp        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -138,6 +138,7 @@
</span><span class="cx">     case GetById:
</span><span class="cx">     case ToThis:
</span><span class="cx">     case MultiGetByOffset:
</span><ins>+    case ToPrimitive:
</ins><span class="cx">         // These are OK.
</span><span class="cx">         break;
</span><span class="cx">     case PutByIdDirect:
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLLowerDFGToLLVMcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp (164242 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2014-02-17 20:19:42 UTC (rev 164242)
+++ trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -452,6 +452,9 @@
</span><span class="cx">         case ToString:
</span><span class="cx">             compileToString();
</span><span class="cx">             break;
</span><ins>+        case ToPrimitive:
+            compileToPrimitive();
+            break;
</ins><span class="cx">         case MakeRope:
</span><span class="cx">             compileMakeRope();
</span><span class="cx">             break;
</span><span class="lines">@@ -2854,6 +2857,32 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx">     
</span><ins>+    void compileToPrimitive()
+    {
+        LValue value = lowJSValue(m_node-&gt;child1());
+        
+        LBasicBlock isCellCase = FTL_NEW_BLOCK(m_out, (&quot;ToPrimitive cell case&quot;));
+        LBasicBlock isObjectCase = FTL_NEW_BLOCK(m_out, (&quot;ToPrimitive object case&quot;));
+        LBasicBlock continuation = FTL_NEW_BLOCK(m_out, (&quot;ToPrimitive continuation&quot;));
+        
+        Vector&lt;ValueFromBlock, 3&gt; results;
+        
+        results.append(m_out.anchor(value));
+        m_out.branch(isCell(value), isCellCase, continuation);
+        
+        LBasicBlock lastNext = m_out.appendTo(isCellCase, isObjectCase);
+        results.append(m_out.anchor(value));
+        m_out.branch(isObject(value), isObjectCase, continuation);
+        
+        m_out.appendTo(isObjectCase, continuation);
+        results.append(m_out.anchor(vmCall(
+            m_out.operation(operationToPrimitive), m_callFrame, value)));
+        m_out.jump(continuation);
+        
+        m_out.appendTo(continuation, lastNext);
+        setJSValue(m_out.phi(m_out.int64, results));
+    }
+    
</ins><span class="cx">     void compileMakeRope()
</span><span class="cx">     {
</span><span class="cx">         LValue kids[3];
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressfoldtoprimitiveincfajs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-in-cfa.js (0 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-in-cfa.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-in-cfa.js        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -0,0 +1,14 @@
</span><ins>+function foo(x) {
+    if (DFGTrue())
+        x = &quot;hello&quot;;
+    return x + &quot; world&quot;;
+}
+
+noInline(foo);
+
+for (var i = 0; i &lt; 100000; ++i) {
+    var result = foo({toString:function() { return &quot;foo&quot; }});
+    if (result != &quot;foo world&quot; &amp;&amp; result != &quot;hello world&quot;)
+        throw &quot;Error: bad result: &quot; + result;
+}
+
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressfoldtoprimitivetoidentityincfajs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-to-identity-in-cfa.js (0 => 164243)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-to-identity-in-cfa.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/fold-to-primitive-to-identity-in-cfa.js        2014-02-17 20:23:34 UTC (rev 164243)
</span><span class="lines">@@ -0,0 +1,14 @@
</span><ins>+function foo(x, p) {
+    if (DFGTrue())
+        x = p ? &quot;hello&quot; : &quot;bar&quot;;
+    return x + &quot; world&quot;;
+}
+
+noInline(foo);
+
+for (var i = 0; i &lt; 100000; ++i) {
+    var result = foo({toString:function() { return &quot;foo&quot; }}, i &amp; 1);
+    if (result != &quot;foo world&quot; &amp;&amp; result != &quot;hello world&quot; &amp;&amp; result != &quot;bar world&quot;)
+        throw &quot;Error: bad result: &quot; + result;
+}
+
</ins></span></pre>
</div>
</div>

</body>
</html>