<!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>[207039] 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/207039">207039</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-10-10 20:39:05 -0700 (Mon, 10 Oct 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>B3->Air lowering should be able to emit complex leas on x86
https://bugs.webkit.org/show_bug.cgi?id=163234
Reviewed by Saam Barati.
This adds comprehensive support for emitting lea on x86.
When adding this, I found that it was useful to also finally add more reassociation. That
reduces the amount of patterns that the instruction selector has to deal with.
* assembler/MacroAssembler.h:
(JSC::MacroAssembler::lea32):
(JSC::MacroAssembler::lea64):
(JSC::MacroAssembler::lea): Deleted.
* b3/B3LowerToAir.cpp:
(JSC::B3::Air::LowerToAir::commitInternal):
(JSC::B3::Air::LowerToAir::tryAppendLea):
(JSC::B3::Air::LowerToAir::lower):
(JSC::B3::Air::LowerToAir::createSelect): Deleted.
* b3/B3ReduceStrength.cpp:
* b3/B3Value.h:
* b3/B3ValueInlines.h:
(JSC::B3::Value::isRepresentableAs):
(JSC::B3::Value::representableAs): Deleted.
* b3/air/AirOpcode.opcodes:
* b3/testb3.cpp: Lots of tests for lea and reassociation.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreassemblerMacroAssemblerh">trunk/Source/JavaScriptCore/assembler/MacroAssembler.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3LowerToAircpp">trunk/Source/JavaScriptCore/b3/B3LowerToAir.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp">trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3Valueh">trunk/Source/JavaScriptCore/b3/B3Value.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3B3ValueInlinesh">trunk/Source/JavaScriptCore/b3/B3ValueInlines.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3airAirOpcodeopcodes">trunk/Source/JavaScriptCore/b3/air/AirOpcode.opcodes</a></li>
<li><a href="#trunkSourceJavaScriptCoreb3testb3cpp">trunk/Source/JavaScriptCore/b3/testb3.cpp</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2016-10-10 Filip Pizlo <fpizlo@apple.com>
+
+ B3->Air lowering should be able to emit complex leas on x86
+ https://bugs.webkit.org/show_bug.cgi?id=163234
+
+ Reviewed by Saam Barati.
+
+ This adds comprehensive support for emitting lea on x86.
+
+ When adding this, I found that it was useful to also finally add more reassociation. That
+ reduces the amount of patterns that the instruction selector has to deal with.
+
+ * assembler/MacroAssembler.h:
+ (JSC::MacroAssembler::lea32):
+ (JSC::MacroAssembler::lea64):
+ (JSC::MacroAssembler::lea): Deleted.
+ * b3/B3LowerToAir.cpp:
+ (JSC::B3::Air::LowerToAir::commitInternal):
+ (JSC::B3::Air::LowerToAir::tryAppendLea):
+ (JSC::B3::Air::LowerToAir::lower):
+ (JSC::B3::Air::LowerToAir::createSelect): Deleted.
+ * b3/B3ReduceStrength.cpp:
+ * b3/B3Value.h:
+ * b3/B3ValueInlines.h:
+ (JSC::B3::Value::isRepresentableAs):
+ (JSC::B3::Value::representableAs): Deleted.
+ * b3/air/AirOpcode.opcodes:
+ * b3/testb3.cpp: Lots of tests for lea and reassociation.
+
</ins><span class="cx"> 2016-10-10 Mark Lam <mark.lam@apple.com>
</span><span class="cx">
</span><span class="cx"> Change ArrayPrototype.cpp's putLength() and setLength() to take a VM& so that we can use vm.propertyNames.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreassemblerMacroAssemblerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/assembler/MacroAssembler.h (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/assembler/MacroAssembler.h        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/assembler/MacroAssembler.h        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -1358,11 +1358,18 @@
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx">
</span><del>- void lea(Address address, RegisterID dest)
</del><ins>+ void lea32(Address address, RegisterID dest)
</ins><span class="cx"> {
</span><del>- addPtr(TrustedImm32(address.offset), address.base, dest);
</del><ins>+ add32(TrustedImm32(address.offset), address.base, dest);
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+#if CPU(X86_64) || CPU(ARM64)
+ void lea64(Address address, RegisterID dest)
+ {
+ add64(TrustedImm32(address.offset), address.base, dest);
+ }
+#endif // CPU(X86_64) || CPU(ARM64)
+
</ins><span class="cx"> bool shouldBlind(Imm32 imm)
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(FORCED_JIT_BLINDING)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3LowerToAircpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3LowerToAir.cpp (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3LowerToAir.cpp        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/B3LowerToAir.cpp        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -399,7 +399,8 @@
</span><span class="cx"> // short, you should avoid this by using the pattern matcher to match patterns.
</span><span class="cx"> void commitInternal(Value* value)
</span><span class="cx"> {
</span><del>- m_locked.add(value);
</del><ins>+ if (value)
+ m_locked.add(value);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> bool crossesInterference(Value* value)
</span><span class="lines">@@ -1798,6 +1799,148 @@
</span><span class="cx"> },
</span><span class="cx"> false);
</span><span class="cx"> }
</span><ins>+
+ bool tryAppendLea()
+ {
+ Air::Opcode leaOpcode = tryOpcodeForType(Lea32, Lea64, m_value->type());
+ if (!isValidForm(leaOpcode, Arg::Index, Arg::Tmp))
+ return false;
+
+ // This lets us turn things like this:
+ //
+ // Add(Add(@x, Shl(@y, $2)), $100)
+ //
+ // Into this:
+ //
+ // lea 100(%rdi,%rsi,4), %rax
+ //
+ // We have a choice here between committing the internal bits of an index or sharing
+ // them. There are solid arguments for both.
+ //
+ // Sharing: The word on the street is that the cost of a lea is one cycle no matter
+ // what it does. Every experiment I've ever seen seems to confirm this. So, sharing
+ // helps us in situations where Wasm input did this:
+ //
+ // x = a[i].x;
+ // y = a[i].y;
+ //
+ // With sharing we would do:
+ //
+ // leal (%a,%i,4), %tmp
+ // cmp (%size, %tmp)
+ // ja _fail
+ // movl (%base, %tmp), %x
+ // leal 4(%a,%i,4), %tmp
+ // cmp (%size, %tmp)
+ // ja _fail
+ // movl (%base, %tmp), %y
+ //
+ // In the absence of sharing, we may find ourselves needing separate registers for
+ // the innards of the index. That's relatively unlikely to be a thing due to other
+ // optimizations that we already have, but it could happen
+ //
+ // Committing: The worst case is that there is a complicated graph of additions and
+ // shifts, where each value has multiple uses. In that case, it's better to compute
+ // each one separately from the others since that way, each calculation will use a
+ // relatively nearby tmp as its input. That seems uncommon, but in those cases,
+ // committing is a clear winner: it would result in a simple interference graph
+ // while sharing would result in a complex one. Interference sucks because it means
+ // more time in IRC and it means worse code.
+ //
+ // It's not super clear if any of these corner cases would ever arise. Committing
+ // has the benefit that it's easier to reason about, and protects a much darker
+ // corner case (more interference).
+
+ // Here are the things we want to match:
+ // Add(Add(@x, @y), $c)
+ // Add(Shl(@x, $c), @y)
+ // Add(@x, Shl(@y, $c))
+ // Add(Add(@x, Shl(@y, $c)), $d)
+ // Add(Add(Shl(@x, $c), @y), $d)
+ //
+ // Note that if you do Add(Shl(@x, $c), $d) then we will treat $d as a non-constant and
+ // force it to materialize. You'll get something like this:
+ //
+ // movl $d, %tmp
+ // leal (%tmp,%x,1<<c), %result
+ //
+ // Which is pretty close to optimal and has the nice effect of being able to handle large
+ // constants gracefully.
+
+ Value* innerAdd = nullptr;
+
+ Value* value = m_value;
+
+ // We're going to consume Add(Add(_), $c). If we succeed at consuming it then we have these
+ // patterns left (i.e. in the Add(_)):
+ //
+ // Add(Add(@x, @y), $c)
+ // Add(Add(@x, Shl(@y, $c)), $d)
+ // Add(Add(Shl(@x, $c), @y), $d)
+ //
+ // Otherwise we are looking at these patterns:
+ //
+ // Add(Shl(@x, $c), @y)
+ // Add(@x, Shl(@y, $c))
+ //
+ // This means that the subsequent code only has to worry about three patterns:
+ //
+ // Add(Shl(@x, $c), @y)
+ // Add(@x, Shl(@y, $c))
+ // Add(@x, @y) (only if offset != 0)
+ int32_t offset = 0;
+ if (value->child(1)->isRepresentableAs<int32_t>()
+ && canBeInternal(value->child(0))
+ && value->child(0)->opcode() == Add) {
+ innerAdd = value->child(0);
+ offset = value->child(1)->asInt32();
+ value = value->child(0);
+ }
+
+ auto tryShl = [&] (Value* shl, Value* other) -> bool {
+ if (shl->opcode() != Shl)
+ return false;
+ if (!canBeInternal(shl))
+ return false;
+ if (!shl->child(1)->hasInt32())
+ return false;
+ unsigned logScale = shl->child(1)->asInt32();
+ if (m_value->type() == Int32)
+ logScale &= 31;
+ else
+ logScale &= 63;
+ // Use 64-bit math to perform the shift so that <<32 does the right thing.
+ int64_t bigScale = static_cast<uint64_t>(1) << static_cast<uint64_t>(logScale);
+ if (!isRepresentableAs<int32_t>(bigScale))
+ return false;
+ unsigned scale = static_cast<int32_t>(bigScale);
+ if (!Arg::isValidIndexForm(scale, offset))
+ return false;
+
+ ASSERT(!m_locked.contains(shl->child(0)));
+ ASSERT(!m_locked.contains(other));
+
+ append(leaOpcode, Arg::index(tmp(other), tmp(shl->child(0)), scale, offset), tmp(m_value));
+ commitInternal(innerAdd);
+ commitInternal(shl);
+ return true;
+ };
+
+ if (tryShl(value->child(0), value->child(1)))
+ return true;
+ if (tryShl(value->child(1), value->child(0)))
+ return true;
+
+ // The remaining pattern is just:
+ // Add(@x, @y) (only if offset != 0)
+ if (!offset)
+ return false;
+ ASSERT(!m_locked.contains(value->child(0)));
+ ASSERT(!m_locked.contains(value->child(1)));
+ append(leaOpcode, Arg::index(tmp(value->child(0)), tmp(value->child(1)), 1, offset), tmp(m_value));
+ commitInternal(innerAdd);
+ return true;
+ }
</ins><span class="cx">
</span><span class="cx"> void lower()
</span><span class="cx"> {
</span><span class="lines">@@ -1834,9 +1977,11 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> case Add: {
</span><ins>+ if (tryAppendLea())
+ return;
+
</ins><span class="cx"> Air::Opcode multiplyAddOpcode = tryOpcodeForType(MultiplyAdd32, MultiplyAdd64, m_value->type());
</span><del>- if (multiplyAddOpcode != Air::Oops
- && isValidForm(multiplyAddOpcode, Arg::Tmp, Arg::Tmp, Arg::Tmp, Arg::Tmp)) {
</del><ins>+ if (isValidForm(multiplyAddOpcode, Arg::Tmp, Arg::Tmp, Arg::Tmp, Arg::Tmp)) {
</ins><span class="cx"> Value* left = m_value->child(0);
</span><span class="cx"> Value* right = m_value->child(1);
</span><span class="cx"> if (!imm(right) || m_valueToTmp[right]) {
</span><span class="lines">@@ -1846,7 +1991,7 @@
</span><span class="cx">
</span><span class="cx"> Value* multiplyLeft = left->child(0);
</span><span class="cx"> Value* multiplyRight = left->child(1);
</span><del>- if (m_locked.contains(multiplyLeft) || m_locked.contains(multiplyRight))
</del><ins>+ if (canBeInternal(multiplyLeft) || canBeInternal(multiplyRight))
</ins><span class="cx"> return false;
</span><span class="cx">
</span><span class="cx"> append(multiplyAddOpcode, tmp(multiplyLeft), tmp(multiplyRight), tmp(right), tmp(m_value));
</span><span class="lines">@@ -2215,7 +2360,7 @@
</span><span class="cx">
</span><span class="cx"> case SlotBase: {
</span><span class="cx"> append(
</span><del>- Lea,
</del><ins>+ pointerType() == Int64 ? Lea64 : Lea32,
</ins><span class="cx"> Arg::stack(m_stackToStack.get(m_value->as<SlotBaseValue>()->slot())),
</span><span class="cx"> tmp(m_value));
</span><span class="cx"> return;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ReduceStrengthcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -482,10 +482,10 @@
</span><span class="cx"> switch (m_value->opcode()) {
</span><span class="cx"> case Add:
</span><span class="cx"> handleCommutativity();
</span><del>-
- // Turn this: Add(Add(value, constant1), constant2)
- // Into this: Add(value, constant1 + constant2)
</del><ins>+
</ins><span class="cx"> if (m_value->child(0)->opcode() == Add && isInt(m_value->type())) {
</span><ins>+ // Turn this: Add(Add(value, constant1), constant2)
+ // Into this: Add(value, constant1 + constant2)
</ins><span class="cx"> Value* newSum = m_value->child(1)->addConstant(m_proc, m_value->child(0)->child(1));
</span><span class="cx"> if (newSum) {
</span><span class="cx"> m_insertionSet.insertValue(m_index, newSum);
</span><span class="lines">@@ -492,9 +492,46 @@
</span><span class="cx"> m_value->child(0) = m_value->child(0)->child(0);
</span><span class="cx"> m_value->child(1) = newSum;
</span><span class="cx"> m_changed = true;
</span><ins>+ break;
</ins><span class="cx"> }
</span><ins>+
+ // Turn this: Add(Add(value, constant), otherValue)
+ // Into this: Add(Add(value, otherValue), constant)
+ if (!m_value->child(1)->hasInt() && m_value->child(0)->child(1)->hasInt()) {
+ Value* value = m_value->child(0)->child(0);
+ Value* constant = m_value->child(0)->child(1);
+ Value* otherValue = m_value->child(1);
+ // This could create duplicate code if Add(value, constant) is used elsewhere.
+ // However, we already model adding a constant as if it was free in other places
+ // so let's just roll with it. The alternative would mean having to do good use
+ // counts, which reduceStrength() currently doesn't have.
+ m_value->child(0) =
+ m_insertionSet.insert<Value>(
+ m_index, Add, m_value->origin(), value, otherValue);
+ m_value->child(1) = constant;
+ m_changed = true;
+ break;
+ }
</ins><span class="cx"> }
</span><del>-
</del><ins>+
+ // Turn this: Add(otherValue, Add(value, constant))
+ // Into this: Add(Add(value, otherValue), constant)
+ if (isInt(m_value->type())
+ && !m_value->child(0)->hasInt()
+ && m_value->child(1)->opcode() == Add
+ && m_value->child(1)->child(1)->hasInt()) {
+ Value* value = m_value->child(1)->child(0);
+ Value* constant = m_value->child(1)->child(1);
+ Value* otherValue = m_value->child(0);
+ // This creates a duplicate add. That's dangerous but probably fine, see above.
+ m_value->child(0) =
+ m_insertionSet.insert<Value>(
+ m_index, Add, m_value->origin(), value, otherValue);
+ m_value->child(1) = constant;
+ m_changed = true;
+ break;
+ }
+
</ins><span class="cx"> // Turn this: Add(constant1, constant2)
</span><span class="cx"> // Into this: constant1 + constant2
</span><span class="cx"> if (Value* constantAdd = m_value->child(0)->addConstant(m_proc, m_value->child(1))) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3Valueh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3Value.h (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3Value.h        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/B3Value.h        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -225,7 +225,7 @@
</span><span class="cx"> float asFloat() const;
</span><span class="cx">
</span><span class="cx"> bool hasNumber() const;
</span><del>- template<typename T> bool representableAs() const;
</del><ins>+ template<typename T> bool isRepresentableAs() const;
</ins><span class="cx"> template<typename T> T asNumber() const;
</span><span class="cx">
</span><span class="cx"> // Booleans in B3 are Const32(0) or Const32(1). So this is true if the type is Int32 and the only
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3B3ValueInlinesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/B3ValueInlines.h (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/B3ValueInlines.h        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/B3ValueInlines.h        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -186,17 +186,17 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> template<typename T>
</span><del>-inline bool Value::representableAs() const
</del><ins>+inline bool Value::isRepresentableAs() const
</ins><span class="cx"> {
</span><span class="cx"> switch (opcode()) {
</span><span class="cx"> case Const32:
</span><del>- return isRepresentableAs<T>(asInt32());
</del><ins>+ return B3::isRepresentableAs<T>(asInt32());
</ins><span class="cx"> case Const64:
</span><del>- return isRepresentableAs<T>(asInt64());
</del><ins>+ return B3::isRepresentableAs<T>(asInt64());
</ins><span class="cx"> case ConstDouble:
</span><del>- return isRepresentableAs<T>(asDouble());
</del><ins>+ return B3::isRepresentableAs<T>(asDouble());
</ins><span class="cx"> case ConstFloat:
</span><del>- return isRepresentableAs<T>(asFloat());
</del><ins>+ return B3::isRepresentableAs<T>(asFloat());
</ins><span class="cx"> default:
</span><span class="cx"> return false;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3airAirOpcodeopcodes"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/air/AirOpcode.opcodes (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/air/AirOpcode.opcodes        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/air/AirOpcode.opcodes        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -300,9 +300,14 @@
</span><span class="cx"> x86_64: X86Div64 UZD:G:64, UZD:G:64, U:G:64
</span><span class="cx"> Tmp*, Tmp*, Tmp
</span><span class="cx">
</span><del>-Lea UA:G:Ptr, D:G:Ptr
</del><ins>+Lea32 UA:G:32, D:G:32
</ins><span class="cx"> Addr, Tmp
</span><ins>+ x86: Index, Tmp as x86Lea32
</ins><span class="cx">
</span><ins>+Lea64 UA:G:64, D:G:64
+ Addr, Tmp
+ x86: Index, Tmp as x86Lea64
+
</ins><span class="cx"> And32 U:G:32, U:G:32, ZD:G:32
</span><span class="cx"> Tmp, Tmp, Tmp
</span><span class="cx"> arm64: BitImm, Tmp, Tmp
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreb3testb3cpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/b3/testb3.cpp (207038 => 207039)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/b3/testb3.cpp        2016-10-11 02:55:26 UTC (rev 207038)
+++ trunk/Source/JavaScriptCore/b3/testb3.cpp        2016-10-11 03:39:05 UTC (rev 207039)
</span><span class="lines">@@ -154,28 +154,37 @@
</span><span class="cx"> Air::validate(proc.code());
</span><span class="cx"> }
</span><span class="cx">
</span><del>-void checkUsesInstruction(Compilation& compilation, const char* text)
</del><ins>+template<typename Func>
+void checkDisassembly(Compilation& compilation, const Func& func, CString failText)
</ins><span class="cx"> {
</span><span class="cx"> CString disassembly = compilation.disassembly();
</span><del>- if (strstr(disassembly.data(), text))
</del><ins>+ if (func(disassembly.data()))
</ins><span class="cx"> return;
</span><del>-
</del><ins>+
</ins><span class="cx"> crashLock.lock();
</span><del>- dataLog("Bad lowering! Expected to find ", text, " but didn't:\n");
</del><ins>+ dataLog("Bad lowering! ", failText, "\n");
</ins><span class="cx"> dataLog(disassembly);
</span><span class="cx"> CRASH();
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+void checkUsesInstruction(Compilation& compilation, const char* text)
+{
+ checkDisassembly(
+ compilation,
+ [&] (const char* disassembly) -> bool {
+ return strstr(disassembly, text);
+ },
+ toCString("Expected to find ", text, " but didnt!"));
+}
+
</ins><span class="cx"> void checkDoesNotUseInstruction(Compilation& compilation, const char* text)
</span><span class="cx"> {
</span><del>- CString disassembly = compilation.disassembly();
- if (!strstr(disassembly.data(), text))
- return;
-
- crashLock.lock();
- dataLog("Bad lowering! Did not expected to find ", text, " but it's there:\n");
- dataLog(disassembly);
- CRASH();
</del><ins>+ checkDisassembly(
+ compilation,
+ [&] (const char* disassembly) -> bool {
+ return !strstr(disassembly, text);
+ },
+ toCString("Did not expected to find ", text, " but it's there!"));
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> template<typename Type>
</span><span class="lines">@@ -13381,6 +13390,275 @@
</span><span class="cx"> go(false);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+void testX86LeaAddAddShlLeft()
+{
+ // Add(Add(Shl(@x, $c), @y), $d)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 2)),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)),
+ root->appendNew<ConstPtrValue>(proc, Origin(), 100));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea 0x64(%rdi,%rsi,4), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), (1 + (2 << 2)) + 100);
+}
+
+void testX86LeaAddAddShlRight()
+{
+ // Add(Add(@x, Shl(@y, $c)), $d)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 2))),
+ root->appendNew<ConstPtrValue>(proc, Origin(), 100));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea 0x64(%rdi,%rsi,4), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), (1 + (2 << 2)) + 100);
+}
+
+void testX86LeaAddAdd()
+{
+ // Add(Add(@x, @y), $c)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)),
+ root->appendNew<ConstPtrValue>(proc, Origin(), 100));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkDisassembly(
+ *code,
+ [&] (const char* disassembly) -> bool {
+ return strstr(disassembly, "lea 0x64(%rdi,%rsi), %rax")
+ || strstr(disassembly, "lea 0x64(%rsi,%rdi), %rax");
+ },
+ "Expected to find something like lea 0x64(%rdi,%rsi), %rax but didn't!");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), (1 + 2) + 100);
+}
+
+void testX86LeaAddShlRight()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 2)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea (%rdi,%rsi,4), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (2 << 2));
+}
+
+void testX86LeaAddShlLeftScale1()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 0)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkDisassembly(
+ *code,
+ [&] (const char* disassembly) -> bool {
+ return strstr(disassembly, "lea (%rdi,%rsi), %rax")
+ || strstr(disassembly, "lea (%rsi,%rdi), %rax");
+ },
+ "Expected to find something like lea (%rdi,%rsi), %rax but didn't!");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + 2);
+}
+
+void testX86LeaAddShlLeftScale2()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 1)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea (%rdi,%rsi,2), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (2 << 1));
+}
+
+void testX86LeaAddShlLeftScale4()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 2)),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea (%rdi,%rsi,4), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (2 << 2));
+}
+
+void testX86LeaAddShlLeftScale8()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 3)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ checkUsesInstruction(*code, "lea (%rdi,%rsi,8), %rax");
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (2 << 3));
+}
+
+void testAddShl32()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 32)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (static_cast<intptr_t>(2) << static_cast<intptr_t>(32)));
+}
+
+void testAddShl64()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 64)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + 2);
+}
+
+void testAddShl65()
+{
+ // Add(Shl(@x, $c), @y)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* result = root->appendNew<Value>(
+ proc, Add, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
+ root->appendNew<Value>(
+ proc, Shl, Origin(),
+ root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1),
+ root->appendNew<Const32Value>(proc, Origin(), 65)));
+ root->appendNew<Value>(proc, Return, Origin(), result);
+
+ auto code = compile(proc);
+ CHECK_EQ(invoke<intptr_t>(*code, 1, 2), 1 + (2 << 1));
+}
+
+void testReduceStrengthReassociation(bool flip)
+{
+ // Add(Add(@x, $c), @y) -> Add(Add(@x, @y), $c)
+ // and
+ // Add(@y, Add(@x, $c)) -> Add(Add(@x, @y), $c)
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* arg1 = root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0);
+ Value* arg2 = root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1);
+
+ Value* innerAdd = root->appendNew<Value>(
+ proc, Add, Origin(), arg1,
+ root->appendNew<ConstPtrValue>(proc, Origin(), 42));
+
+ Value* outerAdd;
+ if (flip)
+ outerAdd = root->appendNew<Value>(proc, Add, Origin(), arg2, innerAdd);
+ else
+ outerAdd = root->appendNew<Value>(proc, Add, Origin(), innerAdd, arg2);
+
+ root->appendNew<Value>(proc, Return, Origin(), outerAdd);
+
+ proc.resetReachability();
+
+ if (shouldBeVerbose()) {
+ dataLog("IR before reduceStrength:\n");
+ dataLog(proc);
+ }
+
+ reduceStrength(proc);
+
+ if (shouldBeVerbose()) {
+ dataLog("IR after reduceStrength:\n");
+ dataLog(proc);
+ }
+
+ CHECK_EQ(root->last()->opcode(), Return);
+ CHECK_EQ(root->last()->child(0)->opcode(), Add);
+ CHECK(root->last()->child(0)->child(1)->isIntPtr(42));
+ CHECK_EQ(root->last()->child(0)->child(0)->opcode(), Add);
+ CHECK(
+ (root->last()->child(0)->child(0)->child(0) == arg1 && root->last()->child(0)->child(0)->child(1) == arg2) ||
+ (root->last()->child(0)->child(0)->child(0) == arg2 && root->last()->child(0)->child(0)->child(1) == arg1));
+}
+
</ins><span class="cx"> // Make sure the compiler does not try to optimize anything out.
</span><span class="cx"> NEVER_INLINE double zero()
</span><span class="cx"> {
</span><span class="lines">@@ -14801,6 +15079,23 @@
</span><span class="cx"> RUN(testPatchpointTerminalReturnValue(true));
</span><span class="cx"> RUN(testPatchpointTerminalReturnValue(false));
</span><span class="cx">
</span><ins>+ RUN(testMemoryFence());
+ RUN(testStoreFence());
+ RUN(testLoadFence());
+ RUN(testTrappingLoad());
+ RUN(testTrappingStore());
+ RUN(testTrappingLoadAddStore());
+ RUN(testTrappingLoadDCE());
+ RUN(testTrappingStoreElimination());
+ RUN(testMoveConstants());
+ RUN(testPCOriginMapDoesntInsertNops());
+ RUN(testPinRegisters());
+ RUN(testReduceStrengthReassociation(true));
+ RUN(testReduceStrengthReassociation(false));
+ RUN(testAddShl32());
+ RUN(testAddShl64());
+ RUN(testAddShl65());
+
</ins><span class="cx"> if (isX86()) {
</span><span class="cx"> RUN(testBranchBitAndImmFusion(Identity, Int64, 1, Air::BranchTest32, Air::Arg::Tmp));
</span><span class="cx"> RUN(testBranchBitAndImmFusion(Identity, Int64, 0xff, Air::BranchTest32, Air::Arg::Tmp));
</span><span class="lines">@@ -14810,7 +15105,14 @@
</span><span class="cx"> RUN(testBranchBitAndImmFusion(Load8Z, Int32, 1, Air::BranchTest8, Air::Arg::Addr));
</span><span class="cx"> RUN(testBranchBitAndImmFusion(Load, Int32, 1, Air::BranchTest32, Air::Arg::Addr));
</span><span class="cx"> RUN(testBranchBitAndImmFusion(Load, Int64, 1, Air::BranchTest32, Air::Arg::Addr));
</span><del>-
</del><ins>+ RUN(testX86LeaAddAddShlLeft());
+ RUN(testX86LeaAddAddShlRight());
+ RUN(testX86LeaAddAdd());
+ RUN(testX86LeaAddShlRight());
+ RUN(testX86LeaAddShlLeftScale1());
+ RUN(testX86LeaAddShlLeftScale2());
+ RUN(testX86LeaAddShlLeftScale4());
+ RUN(testX86LeaAddShlLeftScale8());
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> if (isARM64()) {
</span><span class="lines">@@ -14818,18 +15120,6 @@
</span><span class="cx"> RUN(testTernarySubInstructionSelection(Trunc, Int32, Air::Sub32));
</span><span class="cx"> }
</span><span class="cx">
</span><del>- RUN(testMemoryFence());
- RUN(testStoreFence());
- RUN(testLoadFence());
- RUN(testTrappingLoad());
- RUN(testTrappingStore());
- RUN(testTrappingLoadAddStore());
- RUN(testTrappingLoadDCE());
- RUN(testTrappingStoreElimination());
- RUN(testMoveConstants());
- RUN(testPCOriginMapDoesntInsertNops());
- RUN(testPinRegisters());
-
</del><span class="cx"> if (tasks.isEmpty())
</span><span class="cx"> usage();
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>