Title: [199866] trunk/Source/_javascript_Core
Revision
199866
Author
commit-qu...@webkit.org
Date
2016-04-21 21:46:53 -0700 (Thu, 21 Apr 2016)

Log Message

[JSC] Commute FDiv-by-constant into FMul-by-reciprocal when it is safe
https://bugs.webkit.org/show_bug.cgi?id=156871

Patch by Benjamin Poulain <bpoul...@webkit.org> on 2016-04-21
Reviewed by Filip Pizlo.

FMul is significantly faster than FDiv.
For example, on Haswell, FMul has a latency of 5, a throughput of 1
while FDiv has latency 10-24, throughput 8-18.

Fortunately for us, Sunspider and Kraken have plenty of division
by a simple power of 2 constant. Those are just exponent operations
and can be easily reversed to use FMul instead of FDiv.

LLVM does something similar in InstCombine.

* dfg/DFGStrengthReductionPhase.cpp:
(JSC::DFG::StrengthReductionPhase::handleNode):
* jit/JITDivGenerator.cpp:
(JSC::JITDivGenerator::loadOperand):
(JSC::JITDivGenerator::generateFastPath):
* jit/SnippetOperand.h:
(JSC::SnippetOperand::asConstNumber):
* runtime/MathCommon.h:
(JSC::safeReciprocalForDivByConst):
* tests/stress/floating-point-div-to-mul.js: Added.
(opaqueDivBy2):
(opaqueDivBy3):
(opaqueDivBy4):
(opaqueDivBySafeMaxMinusOne):
(opaqueDivBySafeMax):
(opaqueDivBySafeMaxPlusOne):
(opaqueDivBySafeMin):
(opaqueDivBySafeMinMinusOne):
(i.catch):
(i.result.opaqueDivBySafeMin.valueOf):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (199865 => 199866)


--- trunk/Source/_javascript_Core/ChangeLog	2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-04-22 04:46:53 UTC (rev 199866)
@@ -1,3 +1,41 @@
+2016-04-21  Benjamin Poulain  <bpoul...@webkit.org>
+
+        [JSC] Commute FDiv-by-constant into FMul-by-reciprocal when it is safe
+        https://bugs.webkit.org/show_bug.cgi?id=156871
+
+        Reviewed by Filip Pizlo.
+
+        FMul is significantly faster than FDiv.
+        For example, on Haswell, FMul has a latency of 5, a throughput of 1
+        while FDiv has latency 10-24, throughput 8-18.
+
+        Fortunately for us, Sunspider and Kraken have plenty of division
+        by a simple power of 2 constant. Those are just exponent operations
+        and can be easily reversed to use FMul instead of FDiv.
+
+        LLVM does something similar in InstCombine.
+
+        * dfg/DFGStrengthReductionPhase.cpp:
+        (JSC::DFG::StrengthReductionPhase::handleNode):
+        * jit/JITDivGenerator.cpp:
+        (JSC::JITDivGenerator::loadOperand):
+        (JSC::JITDivGenerator::generateFastPath):
+        * jit/SnippetOperand.h:
+        (JSC::SnippetOperand::asConstNumber):
+        * runtime/MathCommon.h:
+        (JSC::safeReciprocalForDivByConst):
+        * tests/stress/floating-point-div-to-mul.js: Added.
+        (opaqueDivBy2):
+        (opaqueDivBy3):
+        (opaqueDivBy4):
+        (opaqueDivBySafeMaxMinusOne):
+        (opaqueDivBySafeMax):
+        (opaqueDivBySafeMaxPlusOne):
+        (opaqueDivBySafeMin):
+        (opaqueDivBySafeMinMinusOne):
+        (i.catch):
+        (i.result.opaqueDivBySafeMin.valueOf):
+
 2016-04-21  Benjamin Poulain  <benja...@webkit.org>
 
         [JSC] Improve the absThunkGenerator() for 64bit

Modified: trunk/Source/_javascript_Core/dfg/DFGStrengthReductionPhase.cpp (199865 => 199866)


--- trunk/Source/_javascript_Core/dfg/DFGStrengthReductionPhase.cpp	2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/_javascript_Core/dfg/DFGStrengthReductionPhase.cpp	2016-04-22 04:46:53 UTC (rev 199866)
@@ -36,6 +36,7 @@
 #include "DFGPredictionPropagationPhase.h"
 #include "DFGVariableAccessDataDump.h"
 #include "JSCInlines.h"
+#include "MathCommon.h"
 #include "RegExpConstructor.h"
 #include "StringPrototype.h"
 #include <cstdlib>
@@ -192,6 +193,25 @@
             }
             break;
 
+        case ArithDiv:
+            // Transform
+            //    ArithDiv(x, constant)
+            // Into
+            //    ArithMul(x, 1 / constant)
+            // if the operation has the same result.
+            if (m_node->isBinaryUseKind(DoubleRepUse)
+                && m_node->child2()->isNumberConstant()) {
+
+                if (Optional<double> reciprocal = safeReciprocalForDivByConst(m_node->child2()->asNumber())) {
+                    Node* reciprocalNode = m_insertionSet.insertConstant(m_nodeIndex, m_node->origin, jsDoubleNumber(*reciprocal), DoubleConstant);
+                    m_node->setOp(ArithMul);
+                    m_node->child2() = Edge(reciprocalNode, DoubleRepUse);
+                    m_changed = true;
+                    break;
+                }
+            }
+            break;
+
         case ValueRep:
         case Int52Rep:
         case DoubleRep: {

Modified: trunk/Source/_javascript_Core/jit/JITDivGenerator.cpp (199865 => 199866)


--- trunk/Source/_javascript_Core/jit/JITDivGenerator.cpp	2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/_javascript_Core/jit/JITDivGenerator.cpp	2016-04-22 04:46:53 UTC (rev 199866)
@@ -29,12 +29,17 @@
 #if ENABLE(JIT)
 
 #include "JSCJSValueInlines.h"
+#include "MathCommon.h"
 
 namespace JSC {
 
 void JITDivGenerator::loadOperand(CCallHelpers& jit, SnippetOperand& opr, JSValueRegs oprRegs, FPRReg destFPR)
 {
     if (opr.isConstInt32()) {
+        // FIXME: this does not looks right.
+        //     -On x86_64, CVTSI2SD has partial register stall on its FPR.
+        //      A move or load might be a tiny bit larger but safer.
+        //     -On ARM64 we also have FMOV that can load small immediates.
         jit.move(CCallHelpers::Imm32(opr.asConstInt32()), m_scratchGPR);
         jit.convertInt32ToDouble(m_scratchGPR, destFPR);
 #if USE(JSVALUE64)
@@ -74,10 +79,27 @@
     ASSERT(!m_leftOperand.isConstInt32() || !m_rightOperand.isConstInt32());
     m_didEmitFastPath = true;
     loadOperand(jit, m_leftOperand, m_left, m_leftFPR);
-    loadOperand(jit, m_rightOperand, m_right, m_rightFPR);
 
-    jit.divDouble(m_rightFPR, m_leftFPR);
+#if USE(JSVALUE64)
+    Optional<double> safeReciprocal;
+    if (m_rightOperand.isConst()) {
+        double constant = m_rightOperand.asConstNumber();
+        safeReciprocal = safeReciprocalForDivByConst(constant);
+    }
 
+    if (safeReciprocal) {
+        jit.move(CCallHelpers::Imm64(bitwise_cast<int64_t>(*safeReciprocal)), m_scratchGPR);
+        jit.move64ToDouble(m_scratchGPR, m_rightFPR);
+
+        jit.mulDouble(m_rightFPR, m_leftFPR);
+    } else
+#endif
+    {
+        loadOperand(jit, m_rightOperand, m_right, m_rightFPR);
+
+        jit.divDouble(m_rightFPR, m_leftFPR);
+    }
+
     // Is the result actually an integer? The DFG JIT would really like to know. If it's
     // not an integer, we increment a count. If this together with the slow case counter
     // are below threshold then the DFG JIT will compile this division with a speculation

Modified: trunk/Source/_javascript_Core/jit/SnippetOperand.h (199865 => 199866)


--- trunk/Source/_javascript_Core/jit/SnippetOperand.h	2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/_javascript_Core/jit/SnippetOperand.h	2016-04-22 04:46:53 UTC (rev 199866)
@@ -70,6 +70,14 @@
         return m_val.doubleVal;
     }
 
+    double asConstNumber() const
+    {
+        if (isConstInt32())
+            return asConstInt32();
+        ASSERT(isConstDouble());
+        return asConstDouble();
+    }
+
     void setConstInt32(int32_t value)
     {
         m_type = ConstInt32;

Modified: trunk/Source/_javascript_Core/runtime/MathCommon.h (199865 => 199866)


--- trunk/Source/_javascript_Core/runtime/MathCommon.h	2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/_javascript_Core/runtime/MathCommon.h	2016-04-22 04:46:53 UTC (rev 199866)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -27,6 +27,7 @@
 #define MathCommon_h
 
 #include "JITOperations.h"
+#include <wtf/Optional.h>
 
 #ifndef JIT_OPERATION
 #define JIT_OPERATION
@@ -54,6 +55,34 @@
 #endif
 }
 
+inline Optional<double> safeReciprocalForDivByConst(double constant)
+{
+    // No "weird" numbers (NaN, Denormal, etc).
+    if (!constant || !isnormal(constant))
+        return Nullopt;
+
+    int exponent;
+    if (frexp(constant, &exponent) != 0.5)
+        return Nullopt;
+
+    // Note that frexp() returns the value divided by two
+    // so we to offset this exponent by one.
+    exponent -= 1;
+
+    // A double exponent is between -1022 and 1023.
+    // Nothing we can do to invert 1023.
+    if (exponent == 1023)
+        return Nullopt;
+
+    double reciprocal = ldexp(1, -exponent);
+    ASSERT(isnormal(reciprocal));
+    ASSERT(1. / constant == reciprocal);
+    ASSERT(constant == 1. / reciprocal);
+    ASSERT(1. == constant * reciprocal);
+
+    return reciprocal;
+}
+
 extern "C" {
 double JIT_OPERATION jsRound(double value) REFERENCED_FROM_ASM WTF_INTERNAL;
 }

Added: trunk/Source/_javascript_Core/tests/stress/floating-point-div-to-mul.js (0 => 199866)


--- trunk/Source/_javascript_Core/tests/stress/floating-point-div-to-mul.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/floating-point-div-to-mul.js	2016-04-22 04:46:53 UTC (rev 199866)
@@ -0,0 +1,216 @@
+function opaqueDivBy2(a)
+{
+    return a / 2;
+}
+noInline(opaqueDivBy2);
+
+function opaqueDivBy3(a)
+{
+    return a / 3;
+}
+noInline(opaqueDivBy3);
+
+function opaqueDivBy4(a)
+{
+    return a / 4;
+}
+noInline(opaqueDivBy4);
+
+function opaqueDivBySafeMaxMinusOne(a)
+{
+    // 2^1022
+    return a / 44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304;
+}
+noInline(opaqueDivBySafeMaxMinusOne);
+
+function opaqueDivBySafeMax(a)
+{
+    // 2^1023
+    return a / 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608;
+}
+noInline(opaqueDivBySafeMax);
+
+function opaqueDivBySafeMaxPlusOne(a)
+{
+    // 2^1024
+    return a / 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216;
+}
+noInline(opaqueDivBySafeMaxPlusOne);
+
+function opaqueDivBySafeMin(a)
+{
+    // 2^-1022
+    return a / (1 / 44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304);
+}
+noInline(opaqueDivBySafeMin);
+
+function opaqueDivBySafeMinMinusOne(a)
+{
+    // 2^-1023
+    return a / (1 / 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608);
+}
+noInline(opaqueDivBySafeMinMinusOne);
+
+
+for (let i = 0; i < 1e4; ++i) {
+    let result = opaqueDivBy2(Math.PI);
+    if (result !== 1.5707963267948966)
+        throw "Failed opaqueDivBy2(Math.PI). Result = " + result;
+    result = opaqueDivBy2(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBy2(NaN). Result = " + result;
+    result = opaqueDivBy2(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBy2(Infinity). Result = " + result;
+    result = opaqueDivBy2(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBy2(-Infinity). Result = " + result;
+    result = opaqueDivBy2(Math.E);
+    if (result !== 1.3591409142295225)
+        throw "Failed opaqueDivBy2(Math.E). Result = " + result;
+
+    result = opaqueDivBy3(Math.PI);
+    if (result !== 1.0471975511965976)
+        throw "Failed opaqueDivBy3(Math.PI). Result = " + result;
+    result = opaqueDivBy3(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBy3(NaN). Result = " + result;
+    result = opaqueDivBy3(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBy3(Infinity). Result = " + result;
+    result = opaqueDivBy3(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBy3(-Infinity). Result = " + result;
+    result = opaqueDivBy3(Math.E);
+    if (result !== 0.9060939428196817)
+        throw "Failed opaqueDivBy3(Math.E). Result = " + result;
+
+    result = opaqueDivBy4(Math.PI);
+    if (result !== 0.7853981633974483)
+        throw "Failed opaqueDivBy4(Math.PI). Result = " + result;
+    result = opaqueDivBy4(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBy4(NaN). Result = " + result;
+    result = opaqueDivBy4(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBy4(Infinity). Result = " + result;
+    result = opaqueDivBy4(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBy4(-Infinity). Result = " + result;
+    result = opaqueDivBy4(Math.E);
+    if (result !== 0.6795704571147613)
+        throw "Failed opaqueDivBy4(Math.E). Result = " + result;
+
+    result = opaqueDivBySafeMaxMinusOne(Math.PI);
+    if (result !== 6.990275687580919e-308)
+        throw "Failed opaqueDivBySafeMaxMinusOne(Math.PI). Result = " + result;
+    result = opaqueDivBySafeMaxMinusOne(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMaxMinusOne(NaN). Result = " + result;
+    result = opaqueDivBySafeMaxMinusOne(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMaxMinusOne(Infinity). Result = " + result;
+    result = opaqueDivBySafeMaxMinusOne(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBySafeMaxMinusOne(-Infinity). Result = " + result;
+    result = opaqueDivBySafeMaxMinusOne(Math.E);
+    if (result !== 6.048377836559378e-308)
+        throw "Failed opaqueDivBySafeMaxMinusOne(Math.E). Result = " + result;
+
+    result = opaqueDivBySafeMax(Math.PI);
+    if (result !== 3.4951378437904593e-308)
+        throw "Failed opaqueDivBySafeMax(Math.PI). Result = " + result;
+    result = opaqueDivBySafeMax(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMax(NaN). Result = " + result;
+    result = opaqueDivBySafeMax(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMax(Infinity). Result = " + result;
+    result = opaqueDivBySafeMax(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBySafeMax(-Infinity). Result = " + result;
+    result = opaqueDivBySafeMax(Math.E);
+    if (result !== 3.024188918279689e-308)
+        throw "Failed opaqueDivBySafeMax(Math.E). Result = " + result;
+
+    result = opaqueDivBySafeMaxPlusOne(Math.PI);
+    if (result !== 0)
+        throw "Failed opaqueDivBySafeMaxPlusOne(Math.PI). Result = " + result;
+    result = opaqueDivBySafeMaxPlusOne(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMaxPlusOne(NaN). Result = " + result;
+    result = opaqueDivBySafeMaxPlusOne(Infinity);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMaxPlusOne(Infinity). Result = " + result;
+    result = opaqueDivBySafeMaxPlusOne(-Infinity);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMaxPlusOne(-Infinity). Result = " + result;
+    result = opaqueDivBySafeMaxPlusOne(Math.E);
+    if (result !== 0)
+        throw "Failed opaqueDivBySafeMaxPlusOne(Math.E). Result = " + result;
+
+    result = opaqueDivBySafeMin(Math.PI);
+    if (result !== 1.4119048864730642e+308)
+        throw "Failed opaqueDivBySafeMin(Math.PI). Result = " + result;
+    result = opaqueDivBySafeMin(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMin(NaN). Result = " + result;
+    result = opaqueDivBySafeMin(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMin(Infinity). Result = " + result;
+    result = opaqueDivBySafeMin(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBySafeMin(-Infinity). Result = " + result;
+    result = opaqueDivBySafeMin(Math.E);
+    if (result !== 1.2216591454104522e+308)
+        throw "Failed opaqueDivBySafeMin(Math.E). Result = " + result;
+
+    result = opaqueDivBySafeMinMinusOne(Math.PI);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMinMinusOne(Math.PI). Result = " + result;
+    result = opaqueDivBySafeMinMinusOne(NaN);
+    if (result === result)
+        throw "Failed opaqueDivBySafeMinMinusOne(NaN). Result = " + result;
+    result = opaqueDivBySafeMinMinusOne(Infinity);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMinMinusOne(Infinity). Result = " + result;
+    result = opaqueDivBySafeMinMinusOne(-Infinity);
+    if (result !== -Infinity)
+        throw "Failed opaqueDivBySafeMinMinusOne(-Infinity). Result = " + result;
+    result = opaqueDivBySafeMinMinusOne(Math.E);
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMinMinusOne(Math.E). Result = " + result;
+}
+
+
+// Check that we don't do anything crazy with crazy types.
+for (let i = 0; i < 1e3; ++i) {
+    let result = opaqueDivBy2();
+    if (result === result)
+        throw "Failed opaqueDivBy2()";
+    result = opaqueDivBy4(null);
+    if (result !== 0)
+        throw "Failed opaqueDivBy4(null)";
+    result = opaqueDivBySafeMaxMinusOne("WebKit!");
+    if (result === result)
+        throw "Failed opaqueDivBy4(null)";
+    result = opaqueDivBySafeMin("");
+    if (result !== 0)
+        throw "Failed opaqueDivBySafeMin('')";
+    try {
+        result = opaqueDivBy2(Symbol());
+        throw "Failed opaqueDivBy2(Symbol())";
+    } catch (exception) {
+        if (exception != "TypeError: Type error")
+            throw "Wrong exception: " + exception;
+    }
+    result = opaqueDivBy4(true);
+    if (result !== 0.25)
+        throw "Failed opaqueDivBy4(true)";
+    result = opaqueDivBySafeMaxMinusOne(false);
+    if (result !== 0)
+        throw "Failed opaqueDivBySafeMaxMinusOne(false)";
+    result = opaqueDivBySafeMin({ valueOf: function() { return 42; }});
+    if (result !== Infinity)
+        throw "Failed opaqueDivBySafeMin({ valueOf: function() { return 42; }})";
+}
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to