<!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>[245035] 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/245035">245035</a></dd>
<dt>Author</dt> <dd>rmorisset@apple.com</dd>
<dt>Date</dt> <dd>2019-05-07 14:28:38 -0700 (Tue, 07 May 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>[B3] Constants should be hoisted to the root block until moveConstants
https://bugs.webkit.org/show_bug.cgi?id=197265

Reviewed by Saam Barati.

This patch does the following:
- B3ReduceStrength now hoists all constants to the root BB, and de-duplicates them along the way
- B3PureCSE no longer bothers with constants, since they are already de-duplicated by the time it gets to see them
- We now run eliminateDeadCode just after moveConstants, so that the Nops that moveConstants generates are freed instead of staying live throughout Air compilation, reducing memory pressure.
- I also took the opportunity to fix typos in comments in various parts of the code base.

Here are a few numbers to justify this patch:
- In JetStream2, about 27% of values at the beginning of B3 are constants
- In JetStream2, about 11% of values at the end of B3 are Nops
- In JetStream2, this patch increases the number of times that tail duplication happens from a bit less than 24k to a bit more than 25k (hoisting constants makes blocks smaller).

When I tried measuring the total effect on JetStream2 I got a tiny and almost certainly non-significant progression.

* b3/B3Generate.cpp:
(JSC::B3::generateToAir):
* b3/B3MoveConstants.cpp:
* b3/B3PureCSE.cpp:
(JSC::B3::PureCSE::process):
* b3/B3PureCSE.h:
* b3/B3ReduceStrength.cpp:
* bytecode/GetByIdStatus.cpp:
(JSC::GetByIdStatus::computeForStubInfoWithoutExitSiteFeedback):
* dfg/DFGCSEPhase.cpp:
* dfg/DFGOSRAvailabilityAnalysisPhase.h:
* dfg/DFGOSRExit.cpp:
(JSC::DFG::OSRExit::executeOSRExit):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3Generatecpp">trunk/Source/JavaScriptCore/b3/B3Generate.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3MoveConstantscpp">trunk/Source/JavaScriptCore/b3/B3MoveConstants.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3PureCSEcpp">trunk/Source/JavaScriptCore/b3/B3PureCSE.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3PureCSEh">trunk/Source/JavaScriptCore/b3/B3PureCSE.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp">trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorebytecodeGetByIdStatuscpp">trunk/Source/JavaScriptCore/bytecode/GetByIdStatus.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGCSEPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGCSEPhase.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOSRAvailabilityAnalysisPhaseh">trunk/Source/JavaScriptCore/dfg/DFGOSRAvailabilityAnalysisPhase.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOSRExitcpp">trunk/Source/JavaScriptCore/dfg/DFGOSRExit.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog    2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/ChangeLog       2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -1,5 +1,39 @@
</span><span class="cx"> 2019-05-07  Robin Morisset  <rmorisset@apple.com>
</span><span class="cx"> 
</span><ins>+        [B3] Constants should be hoisted to the root block until moveConstants
+        https://bugs.webkit.org/show_bug.cgi?id=197265
+
+        Reviewed by Saam Barati.
+
+        This patch does the following:
+        - B3ReduceStrength now hoists all constants to the root BB, and de-duplicates them along the way
+        - B3PureCSE no longer bothers with constants, since they are already de-duplicated by the time it gets to see them
+        - We now run eliminateDeadCode just after moveConstants, so that the Nops that moveConstants generates are freed instead of staying live throughout Air compilation, reducing memory pressure.
+        - I also took the opportunity to fix typos in comments in various parts of the code base.
+
+        Here are a few numbers to justify this patch:
+        - In JetStream2, about 27% of values at the beginning of B3 are constants
+        - In JetStream2, about 11% of values at the end of B3 are Nops
+        - In JetStream2, this patch increases the number of times that tail duplication happens from a bit less than 24k to a bit more than 25k (hoisting constants makes blocks smaller).
+
+        When I tried measuring the total effect on JetStream2 I got a tiny and almost certainly non-significant progression.
+
+        * b3/B3Generate.cpp:
+        (JSC::B3::generateToAir):
+        * b3/B3MoveConstants.cpp:
+        * b3/B3PureCSE.cpp:
+        (JSC::B3::PureCSE::process):
+        * b3/B3PureCSE.h:
+        * b3/B3ReduceStrength.cpp:
+        * bytecode/GetByIdStatus.cpp:
+        (JSC::GetByIdStatus::computeForStubInfoWithoutExitSiteFeedback):
+        * dfg/DFGCSEPhase.cpp:
+        * dfg/DFGOSRAvailabilityAnalysisPhase.h:
+        * dfg/DFGOSRExit.cpp:
+        (JSC::DFG::OSRExit::executeOSRExit):
+
+2019-05-07  Robin Morisset  <rmorisset@apple.com>
+
</ins><span class="cx">         All prototypes should call didBecomePrototype()
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=196315
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3Generatecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3Generate.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3Generate.cpp    2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/b3/B3Generate.cpp       2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -116,6 +116,7 @@
</span><span class="cx">     lowerMacrosAfterOptimizations(procedure);
</span><span class="cx">     legalizeMemoryOffsets(procedure);
</span><span class="cx">     moveConstants(procedure);
</span><ins>+    eliminateDeadCode(procedure);
</ins><span class="cx"> 
</span><span class="cx">     // FIXME: We should run pureCSE here to clean up some platform specific changes from the previous phases.
</span><span class="cx">     // https://bugs.webkit.org/show_bug.cgi?id=164873
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3MoveConstantscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3MoveConstants.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3MoveConstants.cpp       2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/b3/B3MoveConstants.cpp  2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -154,7 +154,7 @@
</span><span class="cx">                 };
</span><span class="cx">                 
</span><span class="cx">                 // We call this when we have found a constant that we'd like to use. It's possible that
</span><del>-                // we have computed that the constant should be meterialized in this block, but we
</del><ins>+                // we have computed that the constant should be materialized in this block, but we
</ins><span class="cx">                 // haven't inserted it yet. This inserts the constant if necessary.
</span><span class="cx">                 auto materialize = [&] (Value* child) {
</span><span class="cx">                     ValueKey key = child->key();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3PureCSEcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3PureCSE.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3PureCSE.cpp     2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/b3/B3PureCSE.cpp        2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -68,7 +68,7 @@
</span><span class="cx"> 
</span><span class="cx"> bool PureCSE::process(Value* value, Dominators& dominators)
</span><span class="cx"> {
</span><del>-    if (value->opcode() == Identity)
</del><ins>+    if (value->opcode() == Identity || value->isConstant())
</ins><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     ValueKey key = value->key();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3PureCSEh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3PureCSE.h (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3PureCSE.h       2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/b3/B3PureCSE.h  2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -40,7 +40,7 @@
</span><span class="cx"> typedef Vector<Value*, 1> Matches;
</span><span class="cx"> 
</span><span class="cx"> // This is a reusable utility for doing pure CSE. You can use it to do pure CSE on a program by just
</span><del>-// proceeding in order an calling process().
</del><ins>+// proceeding in order and calling process().
</ins><span class="cx"> class PureCSE {
</span><span class="cx"> public:
</span><span class="cx">     PureCSE();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp      2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp 2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -402,6 +402,7 @@
</span><span class="cx">         : m_proc(proc)
</span><span class="cx">         , m_insertionSet(proc)
</span><span class="cx">         , m_blockInsertionSet(proc)
</span><ins>+        , m_root(proc.at(0))
</ins><span class="cx">     {
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -442,6 +443,7 @@
</span><span class="cx">             // keep @thing. That's better, since we usually want things to stay wherever the client
</span><span class="cx">             // put them. We're not actually smart enough to move things around at random.
</span><span class="cx">             m_changed |= eliminateDeadCodeImpl(m_proc);
</span><ins>+            m_valueForConstant.clear();
</ins><span class="cx">             
</span><span class="cx">             simplifySSA();
</span><span class="cx">             
</span><span class="lines">@@ -2145,7 +2147,29 @@
</span><span class="cx">             }
</span><span class="cx">             break;
</span><span class="cx">         }
</span><del>-            
</del><ins>+
+        case Const32:
+        case Const64:
+        case ConstFloat:
+        case ConstDouble: {
+            ValueKey key = m_value->key();
+            if (Value* constInRoot = m_valueForConstant.get(key)) {
+                if (constInRoot != m_value) {
+                    m_value->replaceWithIdentity(constInRoot);
+                    m_changed = true;
+                }
+            } else if (m_block == m_root)
+                m_valueForConstant.add(key, m_value);
+            else {
+                Value* constInRoot = m_proc.clone(m_value);
+                m_root->appendNonTerminal(constInRoot);
+                m_valueForConstant.add(key, constInRoot);
+                m_value->replaceWithIdentity(constInRoot);
+                m_changed = true;
+            }
+            break;
+        }
+
</ins><span class="cx">         default:
</span><span class="cx">             break;
</span><span class="cx">         }
</span><span class="lines">@@ -2794,6 +2818,8 @@
</span><span class="cx">     Procedure& m_proc;
</span><span class="cx">     InsertionSet m_insertionSet;
</span><span class="cx">     BlockInsertionSet m_blockInsertionSet;
</span><ins>+    HashMap<ValueKey, Value*> m_valueForConstant;
+    BasicBlock* m_root { nullptr };
</ins><span class="cx">     BasicBlock* m_block { nullptr };
</span><span class="cx">     unsigned m_index { 0 };
</span><span class="cx">     Value* m_value { nullptr };
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorebytecodeGetByIdStatuscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/bytecode/GetByIdStatus.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/bytecode/GetByIdStatus.cpp   2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/bytecode/GetByIdStatus.cpp      2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -276,7 +276,7 @@
</span><span class="cx">                     return GetByIdStatus(JSC::slowVersion(summary));
</span><span class="cx"> 
</span><span class="cx">                 if (domAttribute) {
</span><del>-                    // Give up when cutom accesses are not merged into one.
</del><ins>+                    // Give up when custom accesses are not merged into one.
</ins><span class="cx">                     if (result.numVariants() != 1)
</span><span class="cx">                         return GetByIdStatus(JSC::slowVersion(summary));
</span><span class="cx">                 } else {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGCSEPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGCSEPhase.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGCSEPhase.cpp  2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/dfg/DFGCSEPhase.cpp     2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -257,7 +257,7 @@
</span><span class="cx">         });
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // The majority of Impure Stack Slotsare unique per value.
</del><ins>+    // The majority of Impure Stack Slots are unique per value.
</ins><span class="cx">     // This is very useful for fast clobber(), we can just remove the slot addressed by AbstractHeap
</span><span class="cx">     // in O(1).
</span><span class="cx">     //
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOSRAvailabilityAnalysisPhaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOSRAvailabilityAnalysisPhase.h (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOSRAvailabilityAnalysisPhase.h        2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/dfg/DFGOSRAvailabilityAnalysisPhase.h   2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -33,7 +33,7 @@
</span><span class="cx"> 
</span><span class="cx"> class Graph;
</span><span class="cx"> 
</span><del>-// Computes BasicBlock::ssa->availabiltiyAtHead/Tail. This is a forward flow type inference
</del><ins>+// Computes BasicBlock::ssa->availabilityAtHead/Tail. This is a forward flow type inference
</ins><span class="cx"> // over MovHints and SetLocals. This analysis is run directly by the Plan for preparing for
</span><span class="cx"> // lowering to B3 IR, but it can also be used as a utility. Note that if you run it before
</span><span class="cx"> // stack layout, all of the flush availability will omit the virtual register - but it will
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOSRExitcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOSRExit.cpp (245034 => 245035)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOSRExit.cpp   2019-05-07 21:09:54 UTC (rev 245034)
+++ trunk/Source/JavaScriptCore/dfg/DFGOSRExit.cpp      2019-05-07 21:28:38 UTC (rev 245035)
</span><span class="lines">@@ -452,7 +452,7 @@
</span><span class="cx"> 
</span><span class="cx">     context.sp() = context.fp<uint8_t*>() + exitState.stackPointerOffset;
</span><span class="cx"> 
</span><del>-    // The only reason for using this do while look is so we can break out midway when appropriate.
</del><ins>+    // The only reason for using this do while loop is so we can break out midway when appropriate.
</ins><span class="cx">     do {
</span><span class="cx">         auto extraInitializationLevel = static_cast<ExtraInitializationLevel>(exitState.extraInitializationLevel);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>