<!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>[193987] 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/193987">193987</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2015-12-11 15:20:20 -0800 (Fri, 11 Dec 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>B3 should have CSE
https://bugs.webkit.org/show_bug.cgi?id=150961

Reviewed by Benjamin Poulain.

This implements a very simple CSE for pure values. I need this as a prerequisite for other
optimizations that I'm implementing. For now, this is neutral on imaging-gaussian-blur but a
slow-down on asm.js code. I suspect that the asm.js slow-down is because of other things that are
still going wrong, and anyway, I need CSE to be able to do even the most basic asm.js strength
reductions.

* b3/B3ReduceStrength.cpp:
* b3/B3ReduceStrength.h:
* b3/B3Value.cpp:
(JSC::B3::Value::replaceWithIdentity):
(JSC::B3::Value::key):</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="#trunkSourceJavaScriptCoreb3B3ReduceStrengthh">trunk/Source/JavaScriptCore/b3/B3ReduceStrength.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3Valuecpp">trunk/Source/JavaScriptCore/b3/B3Value.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (193986 => 193987)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-12-11 23:09:35 UTC (rev 193986)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-12-11 23:20:20 UTC (rev 193987)
</span><span class="lines">@@ -1,3 +1,22 @@
</span><ins>+2015-12-11  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        B3 should have CSE
+        https://bugs.webkit.org/show_bug.cgi?id=150961
+
+        Reviewed by Benjamin Poulain.
+
+        This implements a very simple CSE for pure values. I need this as a prerequisite for other
+        optimizations that I'm implementing. For now, this is neutral on imaging-gaussian-blur but a
+        slow-down on asm.js code. I suspect that the asm.js slow-down is because of other things that are
+        still going wrong, and anyway, I need CSE to be able to do even the most basic asm.js strength
+        reductions.
+
+        * b3/B3ReduceStrength.cpp:
+        * b3/B3ReduceStrength.h:
+        * b3/B3Value.cpp:
+        (JSC::B3::Value::replaceWithIdentity):
+        (JSC::B3::Value::key):
+
</ins><span class="cx"> 2015-12-11  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Refactoring to reduce potential cut-paste errors with the FTL ICs.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp (193986 => 193987)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2015-12-11 23:09:35 UTC (rev 193986)
+++ trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2015-12-11 23:20:20 UTC (rev 193987)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;B3BasicBlockInlines.h&quot;
</span><span class="cx"> #include &quot;B3ControlValue.h&quot;
</span><ins>+#include &quot;B3Dominators.h&quot;
</ins><span class="cx"> #include &quot;B3IndexSet.h&quot;
</span><span class="cx"> #include &quot;B3InsertionSetInlines.h&quot;
</span><span class="cx"> #include &quot;B3MemoryValue.h&quot;
</span><span class="lines">@@ -37,8 +38,10 @@
</span><span class="cx"> #include &quot;B3ProcedureInlines.h&quot;
</span><span class="cx"> #include &quot;B3UpsilonValue.h&quot;
</span><span class="cx"> #include &quot;B3UseCounts.h&quot;
</span><ins>+#include &quot;B3ValueKey.h&quot;
</ins><span class="cx"> #include &quot;B3ValueInlines.h&quot;
</span><span class="cx"> #include &lt;wtf/GraphNodeWorklist.h&gt;
</span><ins>+#include &lt;wtf/HashMap.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace JSC { namespace B3 {
</span><span class="cx"> 
</span><span class="lines">@@ -105,11 +108,20 @@
</span><span class="cx">                 dataLog(m_proc);
</span><span class="cx">             }
</span><span class="cx"> 
</span><ins>+            m_proc.resetValueOwners();
+            m_dominators = &amp;m_proc.dominators(); // Recompute if necessary.
+            m_pureValues.clear();
+
</ins><span class="cx">             for (BasicBlock* block : m_proc.blocksInPreOrder()) {
</span><span class="cx">                 m_block = block;
</span><span class="cx">                 
</span><del>-                for (m_index = 0; m_index &lt; block-&gt;size(); ++m_index)
-                    process();
</del><ins>+                for (m_index = 0; m_index &lt; block-&gt;size(); ++m_index) {
+                    m_value = m_block-&gt;at(m_index);
+                    m_value-&gt;performSubstitution();
+                    
+                    reduceValueStrength();
+                    replaceIfRedundant();
+                }
</ins><span class="cx">                 m_insertionSet.execute(m_block);
</span><span class="cx">             }
</span><span class="cx"> 
</span><span class="lines">@@ -129,11 +141,8 @@
</span><span class="cx">     }
</span><span class="cx">     
</span><span class="cx"> private:
</span><del>-    void process()
</del><ins>+    void reduceValueStrength()
</ins><span class="cx">     {
</span><del>-        m_value = m_block-&gt;at(m_index);
-        m_value-&gt;performSubstitution();
-        
</del><span class="cx">         switch (m_value-&gt;opcode()) {
</span><span class="cx">         case Add:
</span><span class="cx">             handleCommutativity();
</span><span class="lines">@@ -1014,6 +1023,35 @@
</span><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    void replaceIfRedundant()
+    {
+        // This does a very simple pure dominator-based CSE. In the future we could add load elimination.
+        // Note that if we add load elimination, we should do it by directly matching load and store
+        // instructions instead of using the ValueKey functionality or doing DFG HeapLocation-like
+        // things.
+
+        // Don't bother with identities. We kill those anyway.
+        if (m_value-&gt;opcode() == Identity)
+            return;
+
+        ValueKey key = m_value-&gt;key();
+        if (!key)
+            return;
+        
+        Vector&lt;Value*, 1&gt;&amp; matches = m_pureValues.add(key, Vector&lt;Value*, 1&gt;()).iterator-&gt;value;
+
+        // Replace this value with whichever value dominates us.
+        for (Value* match : matches) {
+            if (m_dominators-&gt;dominates(match-&gt;owner, m_value-&gt;owner)) {
+                m_value-&gt;replaceWithIdentity(match);
+                m_changed = true;
+                return;
+            }
+        }
+
+        matches.append(m_value);
+    }
+
</ins><span class="cx">     void simplifyCFG()
</span><span class="cx">     {
</span><span class="cx">         if (verbose) {
</span><span class="lines">@@ -1212,11 +1250,13 @@
</span><span class="cx"> 
</span><span class="cx">     Procedure&amp; m_proc;
</span><span class="cx">     InsertionSet m_insertionSet;
</span><del>-    BasicBlock* m_block;
-    unsigned m_index;
-    Value* m_value;
-    bool m_changed;
-    bool m_changedCFG;
</del><ins>+    BasicBlock* m_block { nullptr };
+    unsigned m_index { 0 };
+    Value* m_value { nullptr };
+    Dominators* m_dominators { nullptr };
+    HashMap&lt;ValueKey, Vector&lt;Value*, 1&gt;&gt; m_pureValues;
+    bool m_changed { false };
+    bool m_changedCFG { false };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // anonymous namespace
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ReduceStrengthh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ReduceStrength.h (193986 => 193987)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ReduceStrength.h        2015-12-11 23:09:35 UTC (rev 193986)
+++ trunk/Source/JavaScriptCore/b3/B3ReduceStrength.h        2015-12-11 23:20:20 UTC (rev 193987)
</span><span class="lines">@@ -32,8 +32,11 @@
</span><span class="cx"> 
</span><span class="cx"> class Procedure;
</span><span class="cx"> 
</span><del>-// Does strength reduction, constant folding, canonicalization, CFG simplification, and DCE. In the
-// future we may also have it do CSE.
</del><ins>+// Does strength reduction, constant folding, canonicalization, CFG simplification, DCE, and CSE. This
+// phase runs those optimizations to fixpoint. The goal of the phase is to dramatically reduce the
+// complexity of the code. In the future, it's preferable to add optimizations to this phase rather than
+// creating new optimizations because then the optimizations can participate in the fixpoint. However,
+// this phase shouldn't become too expensive, so expensive optimizations should be separate.
</ins><span class="cx"> 
</span><span class="cx"> bool reduceStrength(Procedure&amp;);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3Valuecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3Value.cpp (193986 => 193987)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3Value.cpp        2015-12-11 23:09:35 UTC (rev 193986)
+++ trunk/Source/JavaScriptCore/b3/B3Value.cpp        2015-12-11 23:20:20 UTC (rev 193987)
</span><span class="lines">@@ -54,6 +54,13 @@
</span><span class="cx">     // a plain Identity Value. We first collect all of the information we need, then we destruct the
</span><span class="cx">     // previous value in place, and then we construct the Identity Value in place.
</span><span class="cx"> 
</span><ins>+    ASSERT(m_type == value-&gt;m_type);
+
+    if (m_type == Void) {
+        replaceWithNop();
+        return;
+    }
+
</ins><span class="cx">     unsigned index = m_index;
</span><span class="cx">     Type type = m_type;
</span><span class="cx">     Origin origin = m_origin;
</span><span class="lines">@@ -438,6 +445,7 @@
</span><span class="cx">     case FloatToDouble:
</span><span class="cx">     case DoubleToFloat:
</span><span class="cx">     case Check:
</span><ins>+    case BitwiseCast:
</ins><span class="cx">         return ValueKey(opcode(), type(), child(0));
</span><span class="cx">     case Add:
</span><span class="cx">     case Sub:
</span></span></pre>
</div>
</div>

</body>
</html>