<!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>[214827] 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/214827">214827</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2017-04-03 11:55:34 -0700 (Mon, 03 Apr 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>Inst::forEachArg could compile to more compact code
https://bugs.webkit.org/show_bug.cgi?id=170406

Reviewed by Sam Weinig.
        
Prior to this change, Inst::forEachArg compiled to a ginormous ALWAYS_INLINE switch statement.
It had one case for each opcode, and then each of those cases would have a switch statement over
the number of operands. Then the cases of that switch statement would have a sequence of calls to
the passed lambda. This meant that every user of forEachArg would generate an insane amount of
code. It also meant that the inlining achieved nothing, since the lambda would surely then not
be inlined - and if it was, then the icache pressure due to code bloat would surely negate any
benefits.
        
This replaces that code with a loop over a compact look-up table. We use the opcode and number of
operands as keys into that look-up table. The table only takes about 20KB. It has one byte for
each argument in each overload of each opcode.
        
I can't measure any reproducible change in performance, but the JavaScriptCore framework binary
shrinks by 2.7 MB. This is a 15% reduction in JavaScriptCore binary size.

* JavaScriptCore.xcodeproj/project.pbxproj:
* b3/B3Width.h:
* b3/air/AirCustom.h:
(JSC::B3::Air::PatchCustom::forEachArg):
* b3/air/AirFormTable.h: Added.
(JSC::B3::Air::decodeFormRole):
(JSC::B3::Air::decodeFormBank):
(JSC::B3::Air::decodeFormWidth):
* b3/air/AirInst.h:
* b3/air/opcode_generator.rb:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreJavaScriptCorexcodeprojprojectpbxproj">trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3Widthh">trunk/Source/JavaScriptCore/b3/B3Width.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3airAirCustomh">trunk/Source/JavaScriptCore/b3/air/AirCustom.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3airAirInsth">trunk/Source/JavaScriptCore/b3/air/AirInst.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3airopcode_generatorrb">trunk/Source/JavaScriptCore/b3/air/opcode_generator.rb</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreb3airAirFormTableh">trunk/Source/JavaScriptCore/b3/air/AirFormTable.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2017-04-03  Filip Pizlo  &lt;fpizlo@apple.com&gt;
+
+        Inst::forEachArg could compile to more compact code
+        https://bugs.webkit.org/show_bug.cgi?id=170406
+
+        Reviewed by Sam Weinig.
+        
+        Prior to this change, Inst::forEachArg compiled to a ginormous ALWAYS_INLINE switch statement.
+        It had one case for each opcode, and then each of those cases would have a switch statement over
+        the number of operands. Then the cases of that switch statement would have a sequence of calls to
+        the passed lambda. This meant that every user of forEachArg would generate an insane amount of
+        code. It also meant that the inlining achieved nothing, since the lambda would surely then not
+        be inlined - and if it was, then the icache pressure due to code bloat would surely negate any
+        benefits.
+        
+        This replaces that code with a loop over a compact look-up table. We use the opcode and number of
+        operands as keys into that look-up table. The table only takes about 20KB. It has one byte for
+        each argument in each overload of each opcode.
+        
+        I can't measure any reproducible change in performance, but the JavaScriptCore framework binary
+        shrinks by 2.7 MB. This is a 15% reduction in JavaScriptCore binary size.
+
+        * JavaScriptCore.xcodeproj/project.pbxproj:
+        * b3/B3Width.h:
+        * b3/air/AirCustom.h:
+        (JSC::B3::Air::PatchCustom::forEachArg):
+        * b3/air/AirFormTable.h: Added.
+        (JSC::B3::Air::decodeFormRole):
+        (JSC::B3::Air::decodeFormBank):
+        (JSC::B3::Air::decodeFormWidth):
+        * b3/air/AirInst.h:
+        * b3/air/opcode_generator.rb:
+
</ins><span class="cx"> 2017-04-03  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         WebAssembly: remove lastAllocatedMode from Memory
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreJavaScriptCorexcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -189,6 +189,7 @@
</span><span class="cx">                 0F2AC56B1E8A0BD50001EE3F /* AirAllocateRegistersByLinearScan.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2AC5691E8A0BD10001EE3F /* AirAllocateRegistersByLinearScan.h */; };
</span><span class="cx">                 0F2AC56E1E8D7B000001EE3F /* AirPhaseInsertionSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F2AC56C1E8D7AFF0001EE3F /* AirPhaseInsertionSet.cpp */; };
</span><span class="cx">                 0F2AC56F1E8D7B030001EE3F /* AirPhaseInsertionSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2AC56D1E8D7AFF0001EE3F /* AirPhaseInsertionSet.h */; };
</span><ins>+                0F2AC5711E8EE4540001EE3F /* AirFormTable.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2AC5701E8EE4520001EE3F /* AirFormTable.h */; };
</ins><span class="cx">                 0F2B66AC17B6B53F00A7AE3F /* GCIncomingRefCounted.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66A817B6B53D00A7AE3F /* GCIncomingRefCounted.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 0F2B66AD17B6B54500A7AE3F /* GCIncomingRefCountedInlines.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66A917B6B53D00A7AE3F /* GCIncomingRefCountedInlines.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="cx">                 0F2B66AE17B6B54500A7AE3F /* GCIncomingRefCountedSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66AA17B6B53D00A7AE3F /* GCIncomingRefCountedSet.h */; settings = {ATTRIBUTES = (Private, ); }; };
</span><span class="lines">@@ -2723,6 +2724,7 @@
</span><span class="cx">                 0F2AC5691E8A0BD10001EE3F /* AirAllocateRegistersByLinearScan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AirAllocateRegistersByLinearScan.h; path = b3/air/AirAllocateRegistersByLinearScan.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F2AC56C1E8D7AFF0001EE3F /* AirPhaseInsertionSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AirPhaseInsertionSet.cpp; path = b3/air/AirPhaseInsertionSet.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F2AC56D1E8D7AFF0001EE3F /* AirPhaseInsertionSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AirPhaseInsertionSet.h; path = b3/air/AirPhaseInsertionSet.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                0F2AC5701E8EE4520001EE3F /* AirFormTable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AirFormTable.h; path = b3/air/AirFormTable.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 0F2B66A817B6B53D00A7AE3F /* GCIncomingRefCounted.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GCIncomingRefCounted.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F2B66A917B6B53D00A7AE3F /* GCIncomingRefCountedInlines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GCIncomingRefCountedInlines.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 0F2B66AA17B6B53D00A7AE3F /* GCIncomingRefCountedSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GCIncomingRefCountedSet.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -5592,6 +5594,7 @@
</span><span class="cx">                                 262D85B51C0D650F006ACB61 /* AirFixPartialRegisterStalls.h */,
</span><span class="cx">                                 0F2AC5641E8A0B760001EE3F /* AirFixSpillsAfterTerminals.cpp */,
</span><span class="cx">                                 0F2AC5651E8A0B760001EE3F /* AirFixSpillsAfterTerminals.h */,
</span><ins>+                                0F2AC5701E8EE4520001EE3F /* AirFormTable.h */,
</ins><span class="cx">                                 0FEC85521BDACDC70080FF74 /* AirFrequentedBlock.h */,
</span><span class="cx">                                 0FEC85531BDACDC70080FF74 /* AirGenerate.cpp */,
</span><span class="cx">                                 0FEC85541BDACDC70080FF74 /* AirGenerate.h */,
</span><span class="lines">@@ -9336,6 +9339,7 @@
</span><span class="cx">                                 0F2B670B17B6B5AB00A7AE3F /* TypedArrayType.h in Headers */,
</span><span class="cx">                                 0FB7F39D15ED8E4600F167B2 /* TypeError.h in Headers */,
</span><span class="cx">                                 0F2D4DEA19832DAC007D4B19 /* TypeLocation.h in Headers */,
</span><ins>+                                0F2AC5711E8EE4540001EE3F /* AirFormTable.h in Headers */,
</ins><span class="cx">                                 52B311011975B4670080857C /* TypeLocationCache.h in Headers */,
</span><span class="cx">                                 0FFB6C391AF48DDC00DB1BF7 /* TypeofType.h in Headers */,
</span><span class="cx">                                 52C952B719A289850069B386 /* TypeProfiler.h in Headers */,
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3Widthh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3Width.h (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3Width.h        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/b3/B3Width.h        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -51,6 +51,13 @@
</span><span class="cx">     return Width32;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+// Don't use this unless the compiler forces you to.
+#if CPU(X86_64) || CPU(ARM64)
+#define POINTER_WIDTH Width64
+#else
+#define POINTER_WIDTH Width32
+#endif
+
</ins><span class="cx"> inline Width widthForType(Type type)
</span><span class="cx"> {
</span><span class="cx">     switch (type) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3airAirCustomh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/air/AirCustom.h (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/air/AirCustom.h        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/b3/air/AirCustom.h        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -57,14 +57,13 @@
</span><span class="cx"> // Definition of Patch instruction. Patch is used to delegate the behavior of the instruction to the
</span><span class="cx"> // Special object, which will be the first argument to the instruction.
</span><span class="cx"> struct PatchCustom {
</span><del>-    template&lt;typename Functor&gt;
-    static void forEachArg(Inst&amp; inst, const Functor&amp; functor)
</del><ins>+    static void forEachArg(Inst&amp; inst, ScopedLambda&lt;Inst::EachArgCallback&gt; lambda)
</ins><span class="cx">     {
</span><span class="cx">         // This is basically bogus, but it works for analyses that model Special as an
</span><span class="cx">         // immediate.
</span><del>-        functor(inst.args[0], Arg::Use, GP, pointerWidth());
</del><ins>+        lambda(inst.args[0], Arg::Use, GP, pointerWidth());
</ins><span class="cx">         
</span><del>-        inst.args[0].special()-&gt;forEachArg(inst, scopedLambda&lt;Inst::EachArgCallback&gt;(functor));
</del><ins>+        inst.args[0].special()-&gt;forEachArg(inst, lambda);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     template&lt;typename... Arguments&gt;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3airAirFormTableh"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/b3/air/AirFormTable.h (0 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/air/AirFormTable.h                                (rev 0)
+++ trunk/Source/JavaScriptCore/b3/air/AirFormTable.h        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -0,0 +1,66 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#pragma once
+
+#if ENABLE(B3_JIT)
+
+#include &quot;AirArg.h&quot;
+
+namespace JSC { namespace B3 { namespace Air {
+
+static const uint8_t formRoleShift = 0;
+static const uint8_t formRoleMask = 15;
+static const uint8_t formBankShift = 4;
+static const uint8_t formBankMask = 1;
+static const uint8_t formWidthShift = 5;
+static const uint8_t formWidthMask = 3;
+static const uint8_t formInvalidShift = 7;
+
+#define ENCODE_INST_FORM(role, bank, width) (static_cast&lt;uint8_t&gt;(role) &lt;&lt; formRoleShift | static_cast&lt;uint8_t&gt;(bank) &lt;&lt; formBankShift | static_cast&lt;uint8_t&gt;(width) &lt;&lt; formWidthShift)
+
+#define INVALID_INST_FORM (1 &lt;&lt; formInvalidShift)
+
+JS_EXPORT_PRIVATE extern uint8_t g_formTable[];
+
+inline Arg::Role decodeFormRole(uint8_t value)
+{
+    return static_cast&lt;Arg::Role&gt;((value &gt;&gt; formRoleShift) &amp; formRoleMask);
+}
+
+inline Bank decodeFormBank(uint8_t value)
+{
+    return static_cast&lt;Bank&gt;((value &gt;&gt; formBankShift) &amp; formBankMask);
+}
+
+inline Width decodeFormWidth(uint8_t value)
+{
+    return static_cast&lt;Width&gt;((value &gt;&gt; formWidthShift) &amp; formWidthMask);
+}
+
+} } } // namespace JSC::B3::Air
+
+#endif // ENABLE(B3_JIT)
+
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3airAirInsth"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/air/AirInst.h (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/air/AirInst.h        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/b3/air/AirInst.h        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -45,7 +45,6 @@
</span><span class="cx"> struct GenerationContext;
</span><span class="cx"> 
</span><span class="cx"> struct Inst {
</span><del>-public:
</del><span class="cx">     typedef Vector&lt;Arg, 3&gt; ArgList;
</span><span class="cx"> 
</span><span class="cx">     Inst()
</span><span class="lines">@@ -207,6 +206,11 @@
</span><span class="cx">     ArgList args;
</span><span class="cx">     Value* origin; // The B3::Value that this originated from.
</span><span class="cx">     Kind kind;
</span><ins>+
+private:
+    template&lt;typename Func&gt;
+    void forEachArgSimple(const Func&amp;);
+    void forEachArgCustom(ScopedLambda&lt;EachArgCallback&gt;);
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } } } // namespace JSC::B3::Air
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3airopcode_generatorrb"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/air/opcode_generator.rb (214826 => 214827)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/air/opcode_generator.rb        2017-04-03 18:44:05 UTC (rev 214826)
+++ trunk/Source/JavaScriptCore/b3/air/opcode_generator.rb        2017-04-03 18:55:34 UTC (rev 214827)
</span><span class="lines">@@ -54,7 +54,7 @@
</span><span class="cx">     
</span><span class="cx">     def self.widthCode(width)
</span><span class="cx">         if width == &quot;Ptr&quot;
</span><del>-            &quot;pointerWidth()&quot;
</del><ins>+            &quot;POINTER_WIDTH&quot;
</ins><span class="cx">         else
</span><span class="cx">             &quot;Width#{width}&quot;
</span><span class="cx">         end
</span><span class="lines">@@ -94,6 +94,10 @@
</span><span class="cx">     def roleCode
</span><span class="cx">         Arg.roleCode(role)
</span><span class="cx">     end
</span><ins>+    
+    def to_s
+        &quot;#{role}:#{bank}:#{width}&quot;
+    end
</ins><span class="cx"> end
</span><span class="cx"> 
</span><span class="cx"> class Overload
</span><span class="lines">@@ -514,7 +518,7 @@
</span><span class="cx">     | outp |
</span><span class="cx">     outp.puts &quot;namespace JSC { namespace B3 { namespace Air {&quot;
</span><span class="cx">     outp.puts &quot;enum Opcode : int16_t {&quot;
</span><del>-    $opcodes.keys.sort.each {
</del><ins>+    $opcodes.keys.each {
</ins><span class="cx">         | opcode |
</span><span class="cx">         outp.puts &quot;    #{opcode},&quot;
</span><span class="cx">     }
</span><span class="lines">@@ -645,10 +649,23 @@
</span><span class="cx">     outp.puts &quot;#endif&quot;
</span><span class="cx"> end
</span><span class="cx"> 
</span><ins>+maxNumOperands = 0
+$opcodes.values.each {
+    | opcode |
+    next if opcode.custom
+    opcode.overloads.each {
+        | overload |
+        maxNumOperands = overload.signature.length if overload.signature.length &gt; maxNumOperands
+    }
+}
+
+formTableWidth = (maxNumOperands + 1) * maxNumOperands / 2
+
</ins><span class="cx"> writeH(&quot;OpcodeUtils&quot;) {
</span><span class="cx">     | outp |
</span><span class="cx">     outp.puts &quot;#include \&quot;AirCustom.h\&quot;&quot;
</span><span class="cx">     outp.puts &quot;#include \&quot;AirInst.h\&quot;&quot;
</span><ins>+    outp.puts &quot;#include \&quot;AirFormTable.h\&quot;&quot;
</ins><span class="cx">     outp.puts &quot;namespace JSC { namespace B3 { namespace Air {&quot;
</span><span class="cx">     
</span><span class="cx">     outp.puts &quot;inline bool opgenHiddenTruth() { return true; }&quot;
</span><span class="lines">@@ -660,20 +677,35 @@
</span><span class="cx">     outp.puts &quot;} while (false)&quot;
</span><span class="cx"> 
</span><span class="cx">     outp.puts &quot;template&lt;typename Functor&gt;&quot;
</span><del>-    outp.puts &quot;void Inst::forEachArg(const Functor&amp; functor)&quot;
</del><ins>+    outp.puts &quot;ALWAYS_INLINE void Inst::forEachArg(const Functor&amp; functor)&quot;
</ins><span class="cx">     outp.puts &quot;{&quot;
</span><del>-    matchInstOverload(outp, :fast, &quot;this&quot;) {
-        | opcode, overload |
</del><ins>+    outp.puts &quot;switch (kind.opcode) {&quot;
+    $opcodes.values.each {
+        | opcode |
</ins><span class="cx">         if opcode.custom
</span><del>-            outp.puts &quot;#{opcode.name}Custom::forEachArg(*this, functor);&quot;
-        else
-            overload.signature.each_with_index {
-                | arg, index |
-                outp.puts &quot;functor(args[#{index}], Arg::#{arg.roleCode}, #{arg.bank}P, #{arg.widthCode});&quot;
-            }
</del><ins>+            outp.puts &quot;case Opcode::#{opcode.name}:&quot;
</ins><span class="cx">         end
</span><span class="cx">     }
</span><ins>+    outp.puts &quot;forEachArgCustom(scopedLambdaRef&lt;EachArgCallback&gt;(functor));&quot;
+    outp.puts &quot;return;&quot;
+    outp.puts &quot;default:&quot;
+    outp.puts &quot;forEachArgSimple(functor);&quot;
+    outp.puts &quot;return;&quot;
</ins><span class="cx">     outp.puts &quot;}&quot;
</span><ins>+    outp.puts &quot;}&quot;
+    
+    outp.puts &quot;template&lt;typename Func&gt;&quot;
+    outp.puts &quot;ALWAYS_INLINE void Inst::forEachArgSimple(const Func&amp; func)&quot;
+    outp.puts &quot;{&quot;
+    outp.puts &quot;    size_t numOperands = args.size();&quot;
+    outp.puts &quot;    size_t formOffset = (numOperands - 1) * numOperands / 2;&quot;
+    outp.puts &quot;    uint8_t* formBase = g_formTable + kind.opcode * #{formTableWidth} + formOffset;&quot;
+    outp.puts &quot;    for (size_t i = 0; i &lt; numOperands; ++i) {&quot;
+    outp.puts &quot;        uint8_t form = formBase[i];&quot;
+    outp.puts &quot;        ASSERT(!(form &amp; (1 &lt;&lt; formInvalidShift)));&quot;
+    outp.puts &quot;        func(args[i], decodeFormRole(form), decodeFormBank(form), decodeFormWidth(form));&quot;
+    outp.puts &quot;    }&quot;
+    outp.puts &quot;}&quot;
</ins><span class="cx"> 
</span><span class="cx">     outp.puts &quot;template&lt;typename... Arguments&gt;&quot;
</span><span class="cx">     outp.puts &quot;ALWAYS_INLINE bool isValidForm(Opcode opcode, Arguments... arguments)&quot;
</span><span class="lines">@@ -789,6 +821,56 @@
</span><span class="cx">     outp.puts &quot;}&quot;
</span><span class="cx">     outp.puts &quot;} // namespace WTF&quot;
</span><span class="cx">     outp.puts &quot;namespace JSC { namespace B3 { namespace Air {&quot;
</span><ins>+    
+    outp.puts &quot;uint8_t g_formTable[#{$opcodes.size * formTableWidth}] = {&quot;
+    $opcodes.values.each {
+        | opcode |
+        overloads = [nil] * (maxNumOperands + 1)
+        unless opcode.custom
+            opcode.overloads.each {
+                | overload |
+                overloads[overload.signature.length] = overload
+            }
+        end
+        
+        (0..maxNumOperands).each {
+            | numOperands |
+            overload = overloads[numOperands]
+            if overload
+                outp.puts &quot;// #{opcode.name} #{overload.signature.join(', ')}&quot;
+                numOperands.times {
+                    | index |
+                    arg = overload.signature[index]
+                    outp.print &quot;ENCODE_INST_FORM(Arg::#{arg.roleCode}, #{arg.bank}P, #{arg.widthCode}), &quot;
+                }
+            else
+                outp.puts &quot;// Invalid: #{opcode.name} with numOperands = #{numOperands}&quot;
+                numOperands.times {
+                    outp.print &quot;INVALID_INST_FORM, &quot;
+                }
+            end
+            outp.puts
+        }
+    }
+    outp.puts &quot;};&quot;
+    
+    outp.puts &quot;void Inst::forEachArgCustom(ScopedLambda&lt;EachArgCallback&gt; lambda)&quot;
+    outp.puts &quot;{&quot;
+    outp.puts &quot;switch (kind.opcode) {&quot;
+    $opcodes.values.each {
+        | opcode |
+        if opcode.custom
+            outp.puts &quot;case Opcode::#{opcode.name}:&quot;
+            outp.puts &quot;#{opcode.name}Custom::forEachArg(*this, lambda);&quot;
+            outp.puts &quot;break;&quot;
+        end
+    }
+    outp.puts &quot;default:&quot;
+    outp.puts &quot;dataLog(\&quot;Bad call to forEachArgCustom, not custom opcode: \&quot;, kind, \&quot;\\n\&quot;);&quot;
+    outp.puts &quot;RELEASE_ASSERT_NOT_REACHED();&quot;
+    outp.puts &quot;}&quot;
+    outp.puts &quot;}&quot;
+    
</ins><span class="cx">     outp.puts &quot;bool Inst::isValidForm()&quot;
</span><span class="cx">     outp.puts &quot;{&quot;
</span><span class="cx">     matchInstOverloadForm(outp, :safe, &quot;this&quot;) {
</span><span class="lines">@@ -1140,89 +1222,3 @@
</span><span class="cx">     outp.puts &quot;} } } // namespace JSC::B3::Air&quot;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-# This is a hack for JSAir. It's a joke.
-File.open(&quot;JSAir_opcode.js&quot;, &quot;w&quot;) {
-    | outp |
-    outp.puts &quot;\&quot;use strict\&quot;;&quot;
-    outp.puts &quot;// Generated by opcode_generator.rb from #{$fileName} -- do not edit!&quot;
-    
-    $opcodes.values.each {
-        | opcode |
-        outp.puts &quot;const #{opcode.name} = Symbol(#{opcode.name.inspect});&quot;
-    }
-    
-    outp.puts &quot;function Inst_forEachArg(inst, func)&quot;
-    outp.puts &quot;{&quot;
-    outp.puts &quot;let replacement;&quot;
-    outp.puts &quot;switch (inst.opcode) {&quot;
-    $opcodes.values.each {
-        | opcode |
-        outp.puts &quot;case Opcode::#{opcode.name}:&quot;
-        if opcode.custom
-            outp.puts &quot;#{opcode.name}Custom.forEachArg(inst, func);&quot;
-        else
-            needOverloadSwitch = opcode.overloads.size != 1
-            outp.puts &quot;switch (inst.args.length) {&quot; if needOverloadSwitch
-            opcode.overloads.each {
-                | overload |
-                outp.puts &quot;case #{overload.signature.length}:&quot; if needOverloadSwitch
-                overload.signature.each_with_index {
-                    | arg, index |
-                    outp.puts &quot;inst.visitArg(#{index}, func, Arg.#{arg.roleCode}, #{arg.bank}P, #{arg.width});&quot;
-                }
-                outp.puts &quot;break;&quot;
-            }
-            if needOverloadSwitch
-                outp.puts &quot;default:&quot;
-                outp.puts &quot;throw new Error(\&quot;Bad overload\&quot;);&quot;
-                outp.puts &quot;break;&quot;
-                outp.puts &quot;}&quot;
-            end
-        end
-        outp.puts &quot;break;&quot;
-    }
-    outp.puts &quot;default:&quot;
-    outp.puts &quot;throw \&quot;Bad opcode\&quot;;&quot;
-    outp.puts &quot;}&quot;
-    outp.puts &quot;}&quot;
-    
-    outp.puts &quot;function Inst_hasNonArgEffects(inst)&quot;
-    outp.puts &quot;{&quot;
-    outp.puts &quot;switch (inst.opcode) {&quot;
-    foundTrue = false
-    $opcodes.values.each {
-        | opcode |
-        if opcode.attributes[:terminal] or opcode.attributes[:effects]
-            outp.puts &quot;case Opcode::#{opcode.name}:&quot;
-            foundTrue = true
-        end
-    }
-    if foundTrue
-        outp.puts &quot;return true;&quot;
-    end
-    $opcodes.values.each {
-        | opcode |
-        if opcode.custom
-            outp.puts &quot;case Opcode::#{opcode.name}:&quot;
-            outp.puts &quot;return #{opcode.name}Custom.hasNonArgNonControlEffects(inst);&quot;
-        end
-    }
-    outp.puts &quot;default:&quot;
-    outp.puts &quot;return false;&quot;
-    outp.puts &quot;}&quot;
-    outp.puts &quot;}&quot;
-    
-    outp.puts &quot;function opcodeCode(opcode)&quot;
-    outp.puts &quot;{&quot;
-    outp.puts &quot;switch (opcode) {&quot;
-    $opcodes.keys.sort.each_with_index {
-        | opcode, index |
-        outp.puts &quot;case Opcode::#{opcode}:&quot;
-        outp.puts &quot;return #{index}&quot;
-    }
-    outp.puts &quot;default:&quot;
-    outp.puts &quot;throw new Error(\&quot;bad opcode\&quot;);&quot;
-    outp.puts &quot;}&quot;
-    outp.puts &quot;}&quot;
-}
-
</del></span></pre>
</div>
</div>

</body>
</html>