<!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>[204849] trunk</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/204849">204849</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-08-23 12:09:50 -0700 (Tue, 23 Aug 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[JSC] Make Math.cos() and Math.sin() work with any argument type
https://bugs.webkit.org/show_bug.cgi?id=161069

Patch by Benjamin Poulain &lt;bpoulain@apple.com&gt; on 2016-08-23
Reviewed by Geoffrey Garen.

JSTests:

* stress/arith-cos-on-various-types.js: Added.
* stress/arith-sin-on-various-types.js: Added.

Source/JavaScriptCore:

Same as the ArithSqrt patch: add a generic path if the argument
is not a number.

* dfg/DFGAbstractInterpreterInlines.h:
(JSC::DFG::AbstractInterpreter&lt;AbstractStateType&gt;::executeEffects):
* dfg/DFGClobberize.h:
(JSC::DFG::clobberize):
* dfg/DFGFixupPhase.cpp:
(JSC::DFG::FixupPhase::fixupNode):
* dfg/DFGNodeType.h:
* dfg/DFGOperations.cpp:
* dfg/DFGOperations.h:
* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileArithCos):
(JSC::DFG::SpeculativeJIT::compileArithSin):
* dfg/DFGSpeculativeJIT.h:
* dfg/DFGSpeculativeJIT32_64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* dfg/DFGSpeculativeJIT64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArithSin):
(JSC::FTL::DFG::LowerDFGToB3::compileArithCos):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<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="#trunkSourceJavaScriptCoredfgDFGClobberizeh">trunk/Source/JavaScriptCore/dfg/DFGClobberize.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGFixupPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGNodeTypeh">trunk/Source/JavaScriptCore/dfg/DFGNodeType.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOperationscpp">trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOperationsh">trunk/Source/JavaScriptCore/dfg/DFGOperations.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJITcpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJITh">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJIT32_64cpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp">trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreftlFTLLowerDFGToB3cpp">trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestsstressarithcosonvarioustypesjs">trunk/JSTests/stress/arith-cos-on-various-types.js</a></li>
<li><a href="#trunkJSTestsstressarithsinonvarioustypesjs">trunk/JSTests/stress/arith-sin-on-various-types.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/JSTests/ChangeLog        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2016-08-23  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
+
+        [JSC] Make Math.cos() and Math.sin() work with any argument type
+        https://bugs.webkit.org/show_bug.cgi?id=161069
+
+        Reviewed by Geoffrey Garen.
+
+        * stress/arith-cos-on-various-types.js: Added.
+        * stress/arith-sin-on-various-types.js: Added.
+
</ins><span class="cx"> 2016-08-23  Yusuke Suzuki  &lt;utatane.tea@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [ES6] Module namespace object's Symbol.iterator method should only accept module namespace objects
</span></span></pre></div>
<a id="trunkJSTestsstressarithcosonvarioustypesjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/stress/arith-cos-on-various-types.js (0 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/stress/arith-cos-on-various-types.js                                (rev 0)
+++ trunk/JSTests/stress/arith-cos-on-various-types.js        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -0,0 +1,168 @@
</span><ins>+&quot;use strict&quot;;
+
+let cosOfFour = Math.cos(4);
+
+let validInputTestCases = [
+    // input as string, expected result as string.
+    [&quot;undefined&quot;, &quot;NaN&quot;],
+    [&quot;null&quot;, &quot;1&quot;],
+    [&quot;0&quot;, &quot;1&quot;],
+    [&quot;-0.&quot;, &quot;1&quot;],
+    [&quot;4&quot;, &quot;&quot; + cosOfFour],
+    [&quot;Math.PI&quot;, &quot;-1&quot;],
+    [&quot;Infinity&quot;, &quot;NaN&quot;],
+    [&quot;-Infinity&quot;, &quot;NaN&quot;],
+    [&quot;NaN&quot;, &quot;NaN&quot;],
+    [&quot;\&quot;WebKit\&quot;&quot;, &quot;NaN&quot;],
+    [&quot;\&quot;4\&quot;&quot;, &quot;&quot; + cosOfFour],
+    [&quot;{ valueOf: () =&gt; { return 4; } }&quot;, &quot;&quot; + cosOfFour],
+];
+
+let validInputTypedTestCases = validInputTestCases.map((element) =&gt; { return [eval(&quot;(&quot; + element[0] + &quot;)&quot;), eval(element[1])] });
+
+function isIdentical(result, expected)
+{
+    if (expected === expected) {
+        if (result !== expected)
+            return false;
+        if (!expected &amp;&amp; 1 / expected === -Infinity &amp;&amp; 1 / result !== -Infinity)
+            return false;
+
+        return true;
+    }
+    return result !== result;
+}
+
+
+// Test Math.cos() with a very polymorphic input. All test cases are seen at each iteration.
+function opaqueAllTypesCos(argument) {
+    return Math.cos(argument);
+}
+noInline(opaqueAllTypesCos);
+noOSRExitFuzzing(opaqueAllTypesCos);
+
+function testAllTypesCall() {
+    for (let i = 0; i &lt; 1e3; ++i) {
+        for (let testCaseInput of validInputTypedTestCases) {
+            let output = opaqueAllTypesCos(testCaseInput[0]);
+            if (!isIdentical(output, testCaseInput[1]))
+                throw &quot;Failed testAllTypesCall for input &quot; + testCaseInput[0] + &quot; expected &quot; + testCaseInput[1] + &quot; got &quot; + output;
+        }
+    }
+    if (numberOfDFGCompiles(opaqueAllTypesCos) &gt; 2)
+        throw &quot;We should have detected cos() was polymorphic and generated a generic version.&quot;;
+}
+testAllTypesCall();
+
+
+// Test Math.cos() on a completely typed input. Every call see only one type.
+function testSingleTypeCall() {
+    for (let testCaseInput of validInputTestCases) {
+        eval(`
+            function opaqueCos(argument) {
+                return Math.cos(argument);
+            }
+            noInline(opaqueCos);
+            noOSRExitFuzzing(opaqueCos);
+
+            for (let i = 0; i &lt; 1e4; ++i) {
+                if (!isIdentical(opaqueCos(${testCaseInput[0]}), ${testCaseInput[1]})) {
+                    throw &quot;Failed testSingleTypeCall()&quot;;
+                }
+            }
+            if (numberOfDFGCompiles(opaqueCos) &gt; 1)
+                throw &quot;We should have compiled a single cos for the expected type.&quot;;
+        `);
+    }
+}
+testSingleTypeCall();
+
+
+// Verify we call valueOf() exactly once per call.
+function opaqueCosForSideEffects(argument) {
+    return Math.cos(argument);
+}
+noInline(opaqueCosForSideEffects);
+noOSRExitFuzzing(opaqueCosForSideEffects);
+
+function testSideEffect() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let cos16 = Math.cos(16);
+    for (let i = 0; i &lt; 1e4; ++i) {
+        if (opaqueCosForSideEffects(testObject) !== cos16)
+            throw &quot;Incorrect result in testSideEffect()&quot;;
+    }
+    if (testObject.counter !== 1e4)
+        throw &quot;Failed testSideEffect()&quot;;
+    if (numberOfDFGCompiles(opaqueCosForSideEffects) &gt; 1)
+        throw &quot;opaqueCosForSideEffects() is predictable, it should only be compiled once.&quot;;
+}
+testSideEffect();
+
+
+// Verify SQRT is not subject to CSE if the argument has side effects.
+function opaqueCosForCSE(argument) {
+    return Math.cos(argument) + Math.cos(argument) + Math.cos(argument);
+}
+noInline(opaqueCosForCSE);
+noOSRExitFuzzing(opaqueCosForCSE);
+
+function testCSE() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let cos16 = Math.cos(16);
+    let threeCos16 = cos16 + cos16 + cos16;
+    for (let i = 0; i &lt; 1e4; ++i) {
+        if (opaqueCosForCSE(testObject) !== threeCos16)
+            throw &quot;Incorrect result in testCSE()&quot;;
+    }
+    if (testObject.counter !== 3e4)
+        throw &quot;Failed testCSE()&quot;;
+    if (numberOfDFGCompiles(opaqueCosForCSE) &gt; 1)
+        throw &quot;opaqueCosForCSE() is predictable, it should only be compiled once.&quot;;
+}
+testCSE();
+
+
+// Test exceptions in the argument.
+function testException() {
+    let counter = 0;
+    function opaqueCosWithException(argument) {
+        let result = Math.cos(argument);
+        ++counter;
+        return result;
+    }
+    noInline(opaqueCosWithException);
+
+    let testObject = { valueOf: () =&gt; {  return 64; } };
+    let cos64 = Math.cos(64);
+
+    // Warm up without exception.
+    for (let i = 0; i &lt; 1e3; ++i) {
+        if (opaqueCosWithException(testObject) !== cos64)
+            throw &quot;Incorrect result in opaqueCosWithException()&quot;;
+    }
+
+    let testThrowObject = { valueOf: () =&gt; { throw testObject; return 64; } };
+
+    for (let i = 0; i &lt; 1e2; ++i) {
+        try {
+            if (opaqueCosWithException(testThrowObject) !== 8)
+                throw &quot;This code should not be reached!!&quot;;
+        } catch (e) {
+            if (e !== testObject) {
+                throw &quot;Wrong object thrown from opaqueCosWithException.&quot;
+            }
+        }
+    }
+
+    if (counter !== 1e3) {
+        throw &quot;Invalid count in testException()&quot;;
+    }
+}
+testException();
</ins></span></pre></div>
<a id="trunkJSTestsstressarithsinonvarioustypesjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/stress/arith-sin-on-various-types.js (0 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/stress/arith-sin-on-various-types.js                                (rev 0)
+++ trunk/JSTests/stress/arith-sin-on-various-types.js        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -0,0 +1,168 @@
</span><ins>+&quot;use strict&quot;;
+
+let sinOfFour = Math.sin(4);
+
+let validInputTestCases = [
+    // input as string, expected result as string.
+    [&quot;undefined&quot;, &quot;NaN&quot;],
+    [&quot;null&quot;, &quot;0&quot;],
+    [&quot;0&quot;, &quot;0&quot;],
+    [&quot;-0.&quot;, &quot;-0&quot;],
+    [&quot;4&quot;, &quot;&quot; + sinOfFour],
+    [&quot;Math.PI&quot;, &quot;&quot; + Math.sin(Math.PI)],
+    [&quot;Infinity&quot;, &quot;NaN&quot;],
+    [&quot;-Infinity&quot;, &quot;NaN&quot;],
+    [&quot;NaN&quot;, &quot;NaN&quot;],
+    [&quot;\&quot;WebKit\&quot;&quot;, &quot;NaN&quot;],
+    [&quot;\&quot;4\&quot;&quot;, &quot;&quot; + sinOfFour],
+    [&quot;{ valueOf: () =&gt; { return 4; } }&quot;, &quot;&quot; + sinOfFour],
+];
+
+let validInputTypedTestCases = validInputTestCases.map((element) =&gt; { return [eval(&quot;(&quot; + element[0] + &quot;)&quot;), eval(element[1])] });
+
+function isIdentical(result, expected)
+{
+    if (expected === expected) {
+        if (result !== expected)
+            return false;
+        if (!expected &amp;&amp; 1 / expected === -Infinity &amp;&amp; 1 / result !== -Infinity)
+            return false;
+
+        return true;
+    }
+    return result !== result;
+}
+
+
+// Test Math.sin() with a very polymorphic input. All test cases are seen at each iteration.
+function opaqueAllTypesSin(argument) {
+    return Math.sin(argument);
+}
+noInline(opaqueAllTypesSin);
+noOSRExitFuzzing(opaqueAllTypesSin);
+
+function testAllTypesCall() {
+    for (let i = 0; i &lt; 1e3; ++i) {
+        for (let testCaseInput of validInputTypedTestCases) {
+            let output = opaqueAllTypesSin(testCaseInput[0]);
+            if (!isIdentical(output, testCaseInput[1]))
+                throw &quot;Failed testAllTypesCall for input &quot; + testCaseInput[0] + &quot; expected &quot; + testCaseInput[1] + &quot; got &quot; + output;
+        }
+    }
+    if (numberOfDFGCompiles(opaqueAllTypesSin) &gt; 2)
+        throw &quot;We should have detected sin() was polymorphic and generated a generic version.&quot;;
+}
+testAllTypesCall();
+
+
+// Test Math.sin() on a completely typed input. Every call see only one type.
+function testSingleTypeCall() {
+    for (let testCaseInput of validInputTestCases) {
+        eval(`
+            function opaqueSin(argument) {
+                return Math.sin(argument);
+            }
+            noInline(opaqueSin);
+            noOSRExitFuzzing(opaqueSin);
+
+            for (let i = 0; i &lt; 1e4; ++i) {
+                if (!isIdentical(opaqueSin(${testCaseInput[0]}), ${testCaseInput[1]})) {
+                    throw &quot;Failed testSingleTypeCall()&quot;;
+                }
+            }
+            if (numberOfDFGCompiles(opaqueSin) &gt; 1)
+                throw &quot;We should have compiled a single sin for the expected type.&quot;;
+        `);
+    }
+}
+testSingleTypeCall();
+
+
+// Verify we call valueOf() exactly once per call.
+function opaqueSinForSideEffects(argument) {
+    return Math.sin(argument);
+}
+noInline(opaqueSinForSideEffects);
+noOSRExitFuzzing(opaqueSinForSideEffects);
+
+function testSideEffect() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let sin16 = Math.sin(16);
+    for (let i = 0; i &lt; 1e4; ++i) {
+        if (opaqueSinForSideEffects(testObject) !== sin16)
+            throw &quot;Incorrect result in testSideEffect()&quot;;
+    }
+    if (testObject.counter !== 1e4)
+        throw &quot;Failed testSideEffect()&quot;;
+    if (numberOfDFGCompiles(opaqueSinForSideEffects) &gt; 1)
+        throw &quot;opaqueSinForSideEffects() is predictable, it should only be compiled once.&quot;;
+}
+testSideEffect();
+
+
+// Verify SQRT is not subject to CSE if the argument has side effects.
+function opaqueSinForCSE(argument) {
+    return Math.sin(argument) + Math.sin(argument) + Math.sin(argument);
+}
+noInline(opaqueSinForCSE);
+noOSRExitFuzzing(opaqueSinForCSE);
+
+function testCSE() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let sin16 = Math.sin(16);
+    let threeSin16 = sin16 + sin16 + sin16;
+    for (let i = 0; i &lt; 1e4; ++i) {
+        if (opaqueSinForCSE(testObject) !== threeSin16)
+            throw &quot;Incorrect result in testCSE()&quot;;
+    }
+    if (testObject.counter !== 3e4)
+        throw &quot;Failed testCSE()&quot;;
+    if (numberOfDFGCompiles(opaqueSinForCSE) &gt; 1)
+        throw &quot;opaqueSinForCSE() is predictable, it should only be compiled once.&quot;;
+}
+testCSE();
+
+
+// Test exceptions in the argument.
+function testException() {
+    let counter = 0;
+    function opaqueSinWithException(argument) {
+        let result = Math.sin(argument);
+        ++counter;
+        return result;
+    }
+    noInline(opaqueSinWithException);
+
+    let testObject = { valueOf: () =&gt; {  return 64; } };
+    let sin64 = Math.sin(64);
+
+    // Warm up without exception.
+    for (let i = 0; i &lt; 1e3; ++i) {
+        if (opaqueSinWithException(testObject) !== sin64)
+            throw &quot;Incorrect result in opaqueSinWithException()&quot;;
+    }
+
+    let testThrowObject = { valueOf: () =&gt; { throw testObject; return 64; } };
+
+    for (let i = 0; i &lt; 1e2; ++i) {
+        try {
+            if (opaqueSinWithException(testThrowObject) !== 8)
+                throw &quot;This code should not be reached!!&quot;;
+        } catch (e) {
+            if (e !== testObject) {
+                throw &quot;Wrong object thrown from opaqueSinWithException.&quot;
+            }
+        }
+    }
+
+    if (counter !== 1e3) {
+        throw &quot;Invalid count in testException()&quot;;
+    }
+}
+testException();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2016-08-23  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
+
+        [JSC] Make Math.cos() and Math.sin() work with any argument type
+        https://bugs.webkit.org/show_bug.cgi?id=161069
+
+        Reviewed by Geoffrey Garen.
+
+        Same as the ArithSqrt patch: add a generic path if the argument
+        is not a number.
+
+        * dfg/DFGAbstractInterpreterInlines.h:
+        (JSC::DFG::AbstractInterpreter&lt;AbstractStateType&gt;::executeEffects):
+        * dfg/DFGClobberize.h:
+        (JSC::DFG::clobberize):
+        * dfg/DFGFixupPhase.cpp:
+        (JSC::DFG::FixupPhase::fixupNode):
+        * dfg/DFGNodeType.h:
+        * dfg/DFGOperations.cpp:
+        * dfg/DFGOperations.h:
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileArithCos):
+        (JSC::DFG::SpeculativeJIT::compileArithSin):
+        * dfg/DFGSpeculativeJIT.h:
+        * dfg/DFGSpeculativeJIT32_64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * dfg/DFGSpeculativeJIT64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileArithSin):
+        (JSC::FTL::DFG::LowerDFGToB3::compileArithCos):
+
</ins><span class="cx"> 2016-08-23  Yusuke Suzuki  &lt;utatane.tea@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [ES6] Module namespace object's Symbol.iterator method should only accept module namespace objects
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGAbstractInterpreterInlinesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -984,7 +984,10 @@
</span><span class="cx">             setConstant(node, jsDoubleNumber(sin(child.asNumber())));
</span><span class="cx">             break;
</span><span class="cx">         }
</span><del>-        forNode(node).setType(typeOfDoubleUnaryOp(forNode(node-&gt;child1()).m_type));
</del><ins>+        SpeculatedType sinType = SpecFullNumber;
+        if (node-&gt;child1().useKind() == DoubleRepUse)
+            sinType = typeOfDoubleUnaryOp(forNode(node-&gt;child1()).m_type);
+        forNode(node).setType(sinType);
</ins><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx">     
</span><span class="lines">@@ -994,7 +997,10 @@
</span><span class="cx">             setConstant(node, jsDoubleNumber(cos(child.asNumber())));
</span><span class="cx">             break;
</span><span class="cx">         }
</span><del>-        forNode(node).setType(typeOfDoubleUnaryOp(forNode(node-&gt;child1()).m_type));
</del><ins>+        SpeculatedType cosType = SpecFullNumber;
+        if (node-&gt;child1().useKind() == DoubleRepUse)
+            cosType = typeOfDoubleUnaryOp(forNode(node-&gt;child1()).m_type);
+        forNode(node).setType(cosType);
</ins><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGClobberizeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGClobberize.h (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGClobberize.h        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGClobberize.h        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -155,8 +155,6 @@
</span><span class="cx">     case ArithMax:
</span><span class="cx">     case ArithPow:
</span><span class="cx">     case ArithFRound:
</span><del>-    case ArithSin:
-    case ArithCos:
</del><span class="cx">     case ArithLog:
</span><span class="cx">     case GetScope:
</span><span class="cx">     case SkipScope:
</span><span class="lines">@@ -189,6 +187,8 @@
</span><span class="cx">         def(PureValue(node));
</span><span class="cx">         return;
</span><span class="cx"> 
</span><ins>+    case ArithCos:
+    case ArithSin:
</ins><span class="cx">     case ArithSqrt:
</span><span class="cx">         if (node-&gt;child1().useKind() == DoubleRepUse)
</span><span class="cx">             def(PureValue(node));
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGFixupPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGFixupPhase.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -385,7 +385,9 @@
</span><span class="cx">             }
</span><span class="cx">             break;
</span><span class="cx">         }
</span><del>-            
</del><ins>+
+        case ArithCos:
+        case ArithSin:
</ins><span class="cx">         case ArithSqrt: {
</span><span class="cx">             Edge&amp; child1 = node-&gt;child1();
</span><span class="cx">             if (child1-&gt;shouldSpeculateNumberOrBoolean())
</span><span class="lines">@@ -395,8 +397,6 @@
</span><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx">         case ArithFRound:
</span><del>-        case ArithSin:
-        case ArithCos:
</del><span class="cx">         case ArithLog: {
</span><span class="cx">             fixDoubleOrBooleanEdge(node-&gt;child1());
</span><span class="cx">             node-&gt;setResult(NodeResultDouble);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGNodeTypeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGNodeType.h (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGNodeType.h        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGNodeType.h        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -160,8 +160,8 @@
</span><span class="cx">     macro(ArithCeil, NodeResultNumber) \
</span><span class="cx">     macro(ArithTrunc, NodeResultNumber) \
</span><span class="cx">     macro(ArithSqrt, NodeResultDouble) \
</span><del>-    macro(ArithSin, NodeResultNumber) \
-    macro(ArithCos, NodeResultNumber) \
</del><ins>+    macro(ArithSin, NodeResultDouble) \
+    macro(ArithCos, NodeResultDouble) \
</ins><span class="cx">     macro(ArithLog, NodeResultNumber) \
</span><span class="cx">     \
</span><span class="cx">     /* Add of values may either be arithmetic, or result in string concatenation. */\
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOperationscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -322,6 +322,30 @@
</span><span class="cx">     return JSValue::encode(jsNumber(a / b));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+double JIT_OPERATION operationArithCos(ExecState* exec, EncodedJSValue encodedOp1)
+{
+    VM* vm = &amp;exec-&gt;vm();
+    NativeCallFrameTracer tracer(vm, exec);
+
+    JSValue op1 = JSValue::decode(encodedOp1);
+    double a = op1.toNumber(exec);
+    if (UNLIKELY(vm-&gt;exception()))
+        return JSValue::encode(JSValue());
+    return cos(a);
+}
+
+double JIT_OPERATION operationArithSin(ExecState* exec, EncodedJSValue encodedOp1)
+{
+    VM* vm = &amp;exec-&gt;vm();
+    NativeCallFrameTracer tracer(vm, exec);
+
+    JSValue op1 = JSValue::decode(encodedOp1);
+    double a = op1.toNumber(exec);
+    if (UNLIKELY(vm-&gt;exception()))
+        return JSValue::encode(JSValue());
+    return sin(a);
+}
+
</ins><span class="cx"> double JIT_OPERATION operationArithSqrt(ExecState* exec, EncodedJSValue encodedOp1)
</span><span class="cx"> {
</span><span class="cx">     VM* vm = &amp;exec-&gt;vm();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOperationsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOperations.h (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOperations.h        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGOperations.h        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -53,6 +53,8 @@
</span><span class="cx"> EncodedJSValue JIT_OPERATION operationValueBitURShift(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
</span><span class="cx"> EncodedJSValue JIT_OPERATION operationValueAddNotNumber(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
</span><span class="cx"> EncodedJSValue JIT_OPERATION operationValueDiv(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
</span><ins>+double JIT_OPERATION operationArithCos(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
+double JIT_OPERATION operationArithSin(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
</ins><span class="cx"> double JIT_OPERATION operationArithSqrt(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
</span><span class="cx"> EncodedJSValue JIT_OPERATION operationGetByVal(ExecState*, EncodedJSValue encodedBase, EncodedJSValue encodedProperty) WTF_INTERNAL;
</span><span class="cx"> EncodedJSValue JIT_OPERATION operationGetByValCell(ExecState*, JSCell*, EncodedJSValue encodedProperty) WTF_INTERNAL;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJITcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -3885,6 +3885,29 @@
</span><span class="cx">     int32Result(resultReg, node);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SpeculativeJIT::compileArithCos(Node* node)
+{
+    if (node-&gt;child1().useKind() == DoubleRepUse) {
+        SpeculateDoubleOperand op1(this, node-&gt;child1());
+        FPRReg op1FPR = op1.fpr();
+
+        flushRegisters();
+        
+        FPRResult result(this);
+        callOperation(cos, result.fpr(), op1FPR);
+        doubleResult(result.fpr(), node);
+        return;
+    }
+
+    JSValueOperand op1(this, node-&gt;child1());
+    JSValueRegs op1Regs = op1.jsValueRegs();
+    flushRegisters();
+    FPRResult result(this);
+    callOperation(operationArithCos, result.fpr(), op1Regs);
+    m_jit.exceptionCheck();
+    doubleResult(result.fpr(), node);
+}
+
</ins><span class="cx"> void SpeculativeJIT::compileArithSub(Node* node)
</span><span class="cx"> {
</span><span class="cx">     switch (node-&gt;binaryUseKind()) {
</span><span class="lines">@@ -4888,6 +4911,29 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SpeculativeJIT::compileArithSin(Node* node)
+{
+    if (node-&gt;child1().useKind() == DoubleRepUse) {
+        SpeculateDoubleOperand op1(this, node-&gt;child1());
+        FPRReg op1FPR = op1.fpr();
+
+        flushRegisters();
+        
+        FPRResult result(this);
+        callOperation(sin, result.fpr(), op1FPR);
+        doubleResult(result.fpr(), node);
+        return;
+    }
+
+    JSValueOperand op1(this, node-&gt;child1());
+    JSValueRegs op1Regs = op1.jsValueRegs();
+    flushRegisters();
+    FPRResult result(this);
+    callOperation(operationArithSin, result.fpr(), op1Regs);
+    m_jit.exceptionCheck();
+    doubleResult(result.fpr(), node);
+}
+
</ins><span class="cx"> void SpeculativeJIT::compileArithSqrt(Node* node)
</span><span class="cx"> {
</span><span class="cx">     if (node-&gt;child1().useKind() == DoubleRepUse) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJITh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -2469,6 +2469,7 @@
</span><span class="cx">     void compileArithAdd(Node*);
</span><span class="cx">     void compileMakeRope(Node*);
</span><span class="cx">     void compileArithClz32(Node*);
</span><ins>+    void compileArithCos(Node*);
</ins><span class="cx">     void compileArithSub(Node*);
</span><span class="cx">     void compileArithNegate(Node*);
</span><span class="cx">     void compileArithMul(Node*);
</span><span class="lines">@@ -2477,6 +2478,7 @@
</span><span class="cx">     void compileArithPow(Node*);
</span><span class="cx">     void compileArithRounding(Node*);
</span><span class="cx">     void compileArithRandom(Node*);
</span><ins>+    void compileArithSin(Node*);
</ins><span class="cx">     void compileArithSqrt(Node*);
</span><span class="cx">     void compileArithLog(Node*);
</span><span class="cx">     void compileConstantStoragePointer(Node*);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJIT32_64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -2389,29 +2389,13 @@
</span><span class="cx">         compileArithRounding(node);
</span><span class="cx">         break;
</span><span class="cx"> 
</span><del>-    case ArithSin: {
-        SpeculateDoubleOperand op1(this, node-&gt;child1());
-        FPRReg op1FPR = op1.fpr();
-
-        flushRegisters();
-        
-        FPRResult result(this);
-        callOperation(sin, result.fpr(), op1FPR);
-        doubleResult(result.fpr(), node);
</del><ins>+    case ArithSin:
+        compileArithSin(node);
</ins><span class="cx">         break;
</span><del>-    }
</del><span class="cx"> 
</span><del>-    case ArithCos: {
-        SpeculateDoubleOperand op1(this, node-&gt;child1());
-        FPRReg op1FPR = op1.fpr();
-
-        flushRegisters();
-        
-        FPRResult result(this);
-        callOperation(cos, result.fpr(), op1FPR);
-        doubleResult(result.fpr(), node);
</del><ins>+    case ArithCos:
+        compileArithCos(node);
</ins><span class="cx">         break;
</span><del>-    }
</del><span class="cx"> 
</span><span class="cx">     case ArithLog:
</span><span class="cx">         compileArithLog(node);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGSpeculativeJIT64cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -2513,29 +2513,13 @@
</span><span class="cx">         compileArithRounding(node);
</span><span class="cx">         break;
</span><span class="cx"> 
</span><del>-    case ArithSin: {
-        SpeculateDoubleOperand op1(this, node-&gt;child1());
-        FPRReg op1FPR = op1.fpr();
-
-        flushRegisters();
-        
-        FPRResult result(this);
-        callOperation(sin, result.fpr(), op1FPR);
-        doubleResult(result.fpr(), node);
</del><ins>+    case ArithSin:
+        compileArithSin(node);
</ins><span class="cx">         break;
</span><del>-    }
</del><span class="cx"> 
</span><del>-    case ArithCos: {
-        SpeculateDoubleOperand op1(this, node-&gt;child1());
-        FPRReg op1FPR = op1.fpr();
-
-        flushRegisters();
-        
-        FPRResult result(this);
-        callOperation(cos, result.fpr(), op1FPR);
-        doubleResult(result.fpr(), node);
</del><ins>+    case ArithCos:
+        compileArithCos(node);
</ins><span class="cx">         break;
</span><del>-    }
</del><span class="cx"> 
</span><span class="cx">     case ArithLog:
</span><span class="cx">         compileArithLog(node);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreftlFTLLowerDFGToB3cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp (204848 => 204849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp        2016-08-23 19:07:33 UTC (rev 204848)
+++ trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp        2016-08-23 19:09:50 UTC (rev 204849)
</span><span class="lines">@@ -2020,9 +2020,27 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    void compileArithSin() { setDouble(m_out.doubleSin(lowDouble(m_node-&gt;child1()))); }
</del><ins>+    void compileArithSin()
+    {
+        if (m_node-&gt;child1().useKind() == DoubleRepUse) {
+            setDouble(m_out.doubleSin(lowDouble(m_node-&gt;child1())));
+            return;
+        }
+        LValue argument = lowJSValue(m_node-&gt;child1());
+        LValue result = vmCall(Double, m_out.operation(operationArithSin), m_callFrame, argument);
+        setDouble(result);
+    }
</ins><span class="cx"> 
</span><del>-    void compileArithCos() { setDouble(m_out.doubleCos(lowDouble(m_node-&gt;child1()))); }
</del><ins>+    void compileArithCos()
+    {
+        if (m_node-&gt;child1().useKind() == DoubleRepUse) {
+            setDouble(m_out.doubleCos(lowDouble(m_node-&gt;child1())));
+            return;
+        }
+        LValue argument = lowJSValue(m_node-&gt;child1());
+        LValue result = vmCall(Double, m_out.operation(operationArithCos), m_callFrame, argument);
+        setDouble(result);
+    }
</ins><span class="cx"> 
</span><span class="cx">     void compileArithPow()
</span><span class="cx">     {
</span></span></pre>
</div>
</div>

</body>
</html>