Changeset 180360 in webkit


Ignore:
Timestamp:
Feb 19, 2015, 1:08:21 PM (10 years ago)
Author:
benjamin@webkit.org
Message:

Refine the FTL part of ArithPow
https://bugs.webkit.org/show_bug.cgi?id=141792

Patch by Benjamin Poulain <bpoulain@apple.com> on 2015-02-19
Reviewed by Filip Pizlo.

This patch refines the FTL lowering of ArithPow. This was left out
of the original patch to keep it simpler.

  • ftl/FTLLowerDFGToLLVM.cpp:

(JSC::FTL::LowerDFGToLLVM::compileArithPow):
Two improvements here:
1) Do not generate the NaN check unless we know the exponent might be a NaN.
2) Use one BasicBlock per check with the appropriate weight. Now that we have

one branch per test, move the Infinity check before the check for 1 since
it is the less common case.

  • tests/stress/math-pow-becomes-custom-function.js: Added.

Test for changing the Math.pow() function after it has been optimized.

  • tests/stress/math-pow-nan-behaviors.js:

The previous tests were only going as far as the DFGAbstractInterpreter
were the operations were replaced by the equivalent constant.

I duplicated the test functions to also test the dynamic behavior of DFG
and FTL.

  • tests/stress/math-pow-with-constants.js:

Add cases covering exponent constants. LLVM removes many value
checks for those.

  • tests/stress/math-pow-with-never-NaN-exponent.js: Added.

Test for the new optimization removing the NaN check.

Location:
trunk/Source/JavaScriptCore
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r180345 r180360  
     12015-02-19  Benjamin Poulain  <bpoulain@apple.com>
     2
     3        Refine the FTL part of ArithPow
     4        https://bugs.webkit.org/show_bug.cgi?id=141792
     5
     6        Reviewed by Filip Pizlo.
     7
     8        This patch refines the FTL lowering of ArithPow. This was left out
     9        of the original patch to keep it simpler.
     10
     11        * ftl/FTLLowerDFGToLLVM.cpp:
     12        (JSC::FTL::LowerDFGToLLVM::compileArithPow):
     13        Two improvements here:
     14        1) Do not generate the NaN check unless we know the exponent might be a NaN.
     15        2) Use one BasicBlock per check with the appropriate weight. Now that we have
     16           one branch per test, move the Infinity check before the check for 1 since
     17           it is the less common case.
     18
     19        * tests/stress/math-pow-becomes-custom-function.js: Added.
     20        Test for changing the Math.pow() function after it has been optimized.
     21
     22        * tests/stress/math-pow-nan-behaviors.js:
     23        The previous tests were only going as far as the DFGAbstractInterpreter
     24        were the operations were replaced by the equivalent constant.
     25
     26        I duplicated the test functions to also test the dynamic behavior of DFG
     27        and FTL.
     28
     29        * tests/stress/math-pow-with-constants.js:
     30        Add cases covering exponent constants. LLVM removes many value
     31        checks for those.
     32
     33        * tests/stress/math-pow-with-never-NaN-exponent.js: Added.
     34        Test for the new optimization removing the NaN check.
     35
    1362015-02-19  Csaba Osztrogonác  <ossy@webkit.org>
    237
  • trunk/Source/JavaScriptCore/ftl/FTLLowerDFGToLLVM.cpp

    r180317 r180360  
    16601660            LBasicBlock integerExponentIsSmallBlock = FTL_NEW_BLOCK(m_out, ("ArithPow test integer exponent is small."));
    16611661            LBasicBlock integerExponentPowBlock = FTL_NEW_BLOCK(m_out, ("ArithPow pow(double, (int)double)."));
    1662             LBasicBlock doubleExponentPowBlock = FTL_NEW_BLOCK(m_out, ("ArithPow pow(double, double)."));
     1662            LBasicBlock doubleExponentPowBlockEntry = FTL_NEW_BLOCK(m_out, ("ArithPow pow(double, double)."));
     1663            LBasicBlock nanExceptionExponentIsInfinity = FTL_NEW_BLOCK(m_out, ("ArithPow NaN Exception, check exponent is infinity."));
     1664            LBasicBlock nanExceptionBaseIsOne = FTL_NEW_BLOCK(m_out, ("ArithPow NaN Exception, check base is one."));
    16631665            LBasicBlock powBlock = FTL_NEW_BLOCK(m_out, ("ArithPow regular pow"));
    1664             LBasicBlock nanException = FTL_NEW_BLOCK(m_out, ("ArithPow NaN Exception"));
     1666            LBasicBlock nanExceptionResultIsNaN = FTL_NEW_BLOCK(m_out, ("ArithPow NaN Exception, result is NaN."));
    16651667            LBasicBlock continuation = FTL_NEW_BLOCK(m_out, ("ArithPow continuation"));
    16661668
     
    16681670            LValue integerExponentConvertedToDouble = m_out.intToDouble(integerExponent);
    16691671            LValue exponentIsInteger = m_out.doubleEqual(exponent, integerExponentConvertedToDouble);
    1670             m_out.branch(exponentIsInteger, unsure(integerExponentIsSmallBlock), unsure(doubleExponentPowBlock));
     1672            m_out.branch(exponentIsInteger, unsure(integerExponentIsSmallBlock), unsure(doubleExponentPowBlockEntry));
    16711673
    16721674            LBasicBlock lastNext = m_out.appendTo(integerExponentIsSmallBlock, integerExponentPowBlock);
    16731675            LValue integerExponentBelow1000 = m_out.below(integerExponent, m_out.constInt32(1000));
    1674             m_out.branch(integerExponentBelow1000, usually(integerExponentPowBlock), rarely(doubleExponentPowBlock));
    1675 
    1676             m_out.appendTo(integerExponentPowBlock, doubleExponentPowBlock);
     1676            m_out.branch(integerExponentBelow1000, usually(integerExponentPowBlock), rarely(doubleExponentPowBlockEntry));
     1677
     1678            m_out.appendTo(integerExponentPowBlock, doubleExponentPowBlockEntry);
    16771679            ValueFromBlock powDoubleIntResult = m_out.anchor(m_out.doublePowi(base, integerExponent));
    16781680            m_out.jump(continuation);
    16791681
    1680             m_out.appendTo(doubleExponentPowBlock, powBlock);
    16811682            // If y is NaN, the result is NaN.
    1682             // FIXME: shouldn't we only check that if the type of child2() might have NaN?
    1683             LValue exponentIsNaN = m_out.doubleNotEqualOrUnordered(exponent, exponent);
     1683            m_out.appendTo(doubleExponentPowBlockEntry, nanExceptionExponentIsInfinity);
     1684            LValue exponentIsNaN;
     1685            if (m_state.forNode(m_node->child2()).m_type & SpecDoubleNaN)
     1686                exponentIsNaN = m_out.doubleNotEqualOrUnordered(exponent, exponent);
     1687            else
     1688                exponentIsNaN = m_out.booleanFalse;
     1689            m_out.branch(exponentIsNaN, rarely(nanExceptionResultIsNaN), usually(nanExceptionExponentIsInfinity));
    16841690
    16851691            // If abs(x) is 1 and y is +infinity, the result is NaN.
    16861692            // If abs(x) is 1 and y is -infinity, the result is NaN.
     1693            m_out.appendTo(nanExceptionExponentIsInfinity, nanExceptionBaseIsOne);
    16871694            LValue absoluteExponent = m_out.doubleAbs(exponent);
    16881695            LValue absoluteExponentIsInfinity = m_out.doubleEqual(absoluteExponent, m_out.constDouble(std::numeric_limits<double>::infinity()));
     1696            m_out.branch(absoluteExponentIsInfinity, rarely(nanExceptionBaseIsOne), usually(powBlock));
     1697
     1698            m_out.appendTo(nanExceptionBaseIsOne, powBlock);
    16891699            LValue absoluteBase = m_out.doubleAbs(base);
    16901700            LValue absoluteBaseIsOne = m_out.doubleEqual(absoluteBase, m_out.constDouble(1));
    1691             LValue oneBaseInfiniteExponent = m_out.bitAnd(absoluteExponentIsInfinity, absoluteBaseIsOne);
    1692 
    1693             m_out.branch(m_out.bitOr(exponentIsNaN, oneBaseInfiniteExponent), rarely(nanException), usually(powBlock));
    1694 
    1695             m_out.appendTo(powBlock, nanException);
     1701            m_out.branch(absoluteBaseIsOne, unsure(nanExceptionResultIsNaN), unsure(powBlock));
     1702
     1703            m_out.appendTo(powBlock, nanExceptionResultIsNaN);
    16961704            ValueFromBlock powResult = m_out.anchor(m_out.doublePow(base, exponent));
    16971705            m_out.jump(continuation);
    16981706
    1699             m_out.appendTo(nanException, continuation);
     1707            m_out.appendTo(nanExceptionResultIsNaN, continuation);
    17001708            ValueFromBlock pureNan = m_out.anchor(m_out.constDouble(PNaN));
    17011709            m_out.jump(continuation);
  • trunk/Source/JavaScriptCore/tests/stress/math-pow-nan-behaviors.js

    r180098 r180360  
    11// If y is NaN, the result is NaN.
    2 function testIntegerBaseWithNaNExponent() {
     2function testIntegerBaseWithNaNExponentStatic() {
    33    for (var i = 0; i < 10000; ++i) {
    44        var result = Math.pow(5, NaN);
     
    1212    }
    1313}
    14 noInline(testIntegerBaseWithNaNExponent);
    15 testIntegerBaseWithNaNExponent();
    16 
    17 function testFloatingPointBaseWithNaNExponent() {
     14noInline(testIntegerBaseWithNaNExponentStatic);
     15testIntegerBaseWithNaNExponentStatic();
     16
     17function mathPowIntegerBaseWithNaNExponentDynamic(x, y) {
     18    return Math.pow(x, y);
     19}
     20noInline(mathPowIntegerBaseWithNaNExponentDynamic);
     21function testIntegerBaseWithNaNExponentDynamic() {
     22    // Warm up with 2 integers.
     23    for (var i = 0; i < 10000; ++i) {
     24        var result = mathPowIntegerBaseWithNaNExponentDynamic(2, 5);
     25        if (result !== 32)
     26            throw "Error: bad result, mathPowIntegerBaseWithNaNExponentDynamic(2, 5) = " + result + ", expected 32."
     27    }
     28
     29    for (var i = 0; i < 10000; ++i) {
     30        var result = mathPowIntegerBaseWithNaNExponentDynamic(i, NaN);
     31        if (!isNaN(result))
     32            throw "Error: bad result, mathPowIntegerBaseWithNaNExponentDynamic(i, NaN) = " + result + " with i = " + i + ", expected NaN";
     33    }
     34}
     35noInline(testIntegerBaseWithNaNExponentDynamic);
     36testIntegerBaseWithNaNExponentDynamic();
     37
     38function testFloatingPointBaseWithNaNExponentStatic() {
    1839    for (var i = 0; i < 10000; ++i) {
    1940        var result = Math.pow(5.5, NaN);
     
    2243    }
    2344    for (var i = 0; i < 10000; ++i) {
    24         var result = Math.pow(i + 1, NaN);
     45        var result = Math.pow(i + 0.5, NaN);
    2546        if (!isNaN(result))
    2647            throw "Error: bad result, Math.pow(i + 0.5, NaN) = " + result + " with i = " + i;
    2748    }
    2849}
    29 noInline(testFloatingPointBaseWithNaNExponent);
    30 testFloatingPointBaseWithNaNExponent();
     50noInline(testFloatingPointBaseWithNaNExponentStatic);
     51testFloatingPointBaseWithNaNExponentStatic();
     52
     53function mathPowFloatingPointBaseWithNaNExponentDynamic(x, y) {
     54    return Math.pow(x, y);
     55}
     56noInline(mathPowFloatingPointBaseWithNaNExponentDynamic);
     57function testFloatingPointBaseWithNaNExponentDynamic() {
     58    // Warm up with 2 double.
     59    for (var i = 0; i < 10000; ++i) {
     60        var result = mathPowFloatingPointBaseWithNaNExponentDynamic(2.5, 5.1);
     61        if (result !== 107.02717054543135)
     62            throw "Error: bad result, mathPowFloatingPointBaseWithNaNExponentDynamic(2.5, 5.1) = " + result + ", expected 107.02717054543135."
     63    }
     64
     65    for (var i = 0; i < 10000; ++i) {
     66        var result = mathPowFloatingPointBaseWithNaNExponentDynamic(i + 0.5, NaN);
     67        if (!isNaN(result))
     68            throw "Error: bad result, mathPowFloatingPointBaseWithNaNExponentDynamic(i + 0.5, NaN) = " + result + " with i = " + i + ", expected NaN";
     69    }
     70}
     71noInline(testFloatingPointBaseWithNaNExponentDynamic);
     72testFloatingPointBaseWithNaNExponentDynamic();
    3173
    3274// If y is +0, the result is 1, even if x is NaN.
    3375// If y is −0, the result is 1, even if x is NaN.
    3476// If x is NaN and y is nonzero, the result is NaN.
    35 function testNaNBase() {
     77function testNaNBaseStatic() {
    3678    for (var i = 0; i < 10000; ++i) {
    3779        var result = Math.pow(NaN, i + 1);
     
    5597    }
    5698}
    57 noInline(testNaNBase);
    58 testNaNBase();
     99noInline(testNaNBaseStatic);
     100testNaNBaseStatic();
     101
     102function mathPowNaNBaseDynamic1(x, y) {
     103    return Math.pow(x, y);
     104}
     105function mathPowNaNBaseDynamic2(x, y) {
     106    return Math.pow(x, y);
     107}
     108function mathPowNaNBaseDynamic3(x, y) {
     109    return Math.pow(x, y);
     110}
     111function mathPowNaNBaseDynamic4(x, y) {
     112    return Math.pow(x, y);
     113}
     114noInline(mathPowNaNBaseDynamic1);
     115noInline(mathPowNaNBaseDynamic2);
     116noInline(mathPowNaNBaseDynamic3);
     117noInline(mathPowNaNBaseDynamic4);
     118function testNaNBaseDynamic() {
     119    for (var i = 0; i < 10000; ++i) {
     120        var result = mathPowNaNBaseDynamic1(NaN, i + 1);
     121        if (!isNaN(result))
     122            throw "Error: bad result, mathPowNaNBaseDynamic1(NaN, i + 1) = " + result + " with i = " + i;
     123    }
     124    for (var i = 0; i < 10000; ++i) {
     125        var result = mathPowNaNBaseDynamic2(NaN, i + 1.5);
     126        if (!isNaN(result))
     127            throw "Error: bad result, mathPowNaNBaseDynamic2(NaN, i + 1.5) = " + result + " with i = " + i;
     128    }
     129    for (var i = 0; i < 10000; ++i) {
     130        var result = mathPowNaNBaseDynamic3(NaN, 0);
     131        if (result !== 1)
     132            throw "Error: bad result, mathPowNaNBaseDynamic3(NaN, 0) = " + result;
     133    }
     134    for (var i = 0; i < 10000; ++i) {
     135        var result = mathPowNaNBaseDynamic4(NaN, -0);
     136        if (result !== 1)
     137            throw "Error: bad result, mathPowNaNBaseDynamic4(NaN, -0) = " + result;
     138    }
     139}
     140noInline(testNaNBaseDynamic);
     141testNaNBaseDynamic();
    59142
    60143// If abs(x) is 1 and y is +∞, the result is NaN.
    61144// If abs(x) is 1 and y is −∞, the result is NaN.
    62 function infiniteExponents() {
     145function infiniteExponentsStatic() {
    63146    for (var i = 0; i < 10000; ++i) {
    64147        var result = Math.pow(1, Number.POSITIVE_INFINITY);
     
    82165    }
    83166}
    84 noInline(infiniteExponents);
    85 infiniteExponents();
     167noInline(infiniteExponentsStatic);
     168infiniteExponentsStatic();
     169
     170function mathPowInfiniteExponentsDynamic1(x, y) {
     171    return Math.pow(x, y);
     172}
     173function mathPowInfiniteExponentsDynamic2(x, y) {
     174    return Math.pow(x, y);
     175}
     176function mathPowInfiniteExponentsDynamic3(x, y) {
     177    return Math.pow(x, y);
     178}
     179function mathPowInfiniteExponentsDynamic4(x, y) {
     180    return Math.pow(x, y);
     181}
     182noInline(mathPowInfiniteExponentsDynamic1);
     183noInline(mathPowInfiniteExponentsDynamic2);
     184noInline(mathPowInfiniteExponentsDynamic3);
     185noInline(mathPowInfiniteExponentsDynamic4);
     186function infiniteExponentsDynamic() {
     187    for (var i = 0; i < 10000; ++i) {
     188        var result = mathPowInfiniteExponentsDynamic1(1, Number.POSITIVE_INFINITY);
     189        if (!isNaN(result))
     190            throw "Error: bad result, mathPowInfiniteExponentsDynamic1(1, Number.POSITIVE_INFINITY) = " + result;
     191    }
     192    for (var i = 0; i < 10000; ++i) {
     193        var result = mathPowInfiniteExponentsDynamic2(-1, Number.POSITIVE_INFINITY);
     194        if (!isNaN(result))
     195            throw "Error: bad result, mathPowInfiniteExponentsDynamic2(-1, Number.POSITIVE_INFINITY) = " + result;
     196    }
     197    for (var i = 0; i < 10000; ++i) {
     198        var result = mathPowInfiniteExponentsDynamic3(1, Number.NEGATIVE_INFINITY);
     199        if (!isNaN(result))
     200            throw "Error: bad result, mathPowInfiniteExponentsDynamic3(1, Number.NEGATIVE_INFINITY) = " + result;
     201    }
     202    for (var i = 0; i < 10000; ++i) {
     203        var result = mathPowInfiniteExponentsDynamic4(-1, Number.NEGATIVE_INFINITY);
     204        if (!isNaN(result))
     205            throw "Error: bad result, mathPowInfiniteExponentsDynamic4(-1, Number.NEGATIVE_INFINITY) = " + result;
     206    }
     207}
     208noInline(infiniteExponentsDynamic);
     209infiniteExponentsDynamic();
  • trunk/Source/JavaScriptCore/tests/stress/math-pow-with-constants.js

    r180098 r180360  
    114114}
    115115testBaseAndExponentConstantLiterals();
     116
     117
     118function exponentIsIntegerConstant(x) {
     119    return Math.pow(x, 42);
     120}
     121noInline(exponentIsIntegerConstant);
     122
     123function testExponentIsIntegerConstant() {
     124    for (var i = 0; i < 1000; ++i) {
     125        var result = exponentIsIntegerConstant(2);
     126        if (result !== 4398046511104)
     127            throw "Error: exponentIsIntegerConstant(2) should be 4398046511104, was = " + result;
     128    }
     129    for (var i = 0; i < 1000; ++i) {
     130        var result = exponentIsIntegerConstant(5);
     131        if (result !== 2.2737367544323207e+29)
     132            throw "Error: exponentIsIntegerConstant(5) should be 2.2737367544323207e+29, was = " + result;
     133    }
     134    for (var i = 0; i < 1000; ++i) {
     135        var result = exponentIsIntegerConstant(2.1);
     136        if (result !== 34135823067412.42)
     137            throw "Error: exponentIsIntegerConstant(2.1) should be 34135823067412.42, was = " + result;
     138    }
     139}
     140testExponentIsIntegerConstant();
     141
     142
     143function exponentIsDoubleConstant(x) {
     144    return Math.pow(x, 42.5);
     145}
     146noInline(exponentIsDoubleConstant);
     147
     148function testExponentIsDoubleConstant() {
     149    for (var i = 0; i < 1000; ++i) {
     150        var result = exponentIsDoubleConstant(2);
     151        if (result !== 6219777023950.95)
     152            throw "Error: exponentIsDoubleConstant(2) should be 6219777023950.95, was = " + result;
     153    }
     154    for (var i = 0; i < 1000; ++i) {
     155        var result = exponentIsDoubleConstant(5);
     156        if (result !== 5.084229945850415e+29)
     157            throw "Error: exponentIsDoubleConstant(5) should be 5.084229945850415e+29, was = " + result;
     158    }
     159    for (var i = 0; i < 1000; ++i) {
     160        var result = exponentIsDoubleConstant(2.1);
     161        if (result !== 49467507261113.805)
     162            throw "Error: exponentIsDoubleConstant(2.1) should be 49467507261113.805, was = " + result;
     163    }
     164}
     165testExponentIsDoubleConstant();
     166
     167
     168function exponentIsInfinityConstant(x) {
     169    return Math.pow(x, Infinity);
     170}
     171noInline(exponentIsInfinityConstant);
     172
     173function testExponentIsInfinityConstant() {
     174    for (var i = 0; i < 1000; ++i) {
     175        var result = exponentIsInfinityConstant(2);
     176        if (result !== Infinity)
     177            throw "Error: exponentIsInfinityConstant(2) should be Infinity, was = " + result;
     178    }
     179    for (var i = 0; i < 1000; ++i) {
     180        var result = exponentIsInfinityConstant(5);
     181        if (result !== Infinity)
     182            throw "Error: exponentIsInfinityConstant(5) should be Infinity, was = " + result;
     183    }
     184    for (var i = 0; i < 1000; ++i) {
     185        var result = exponentIsInfinityConstant(2.1);
     186        if (result !== Infinity)
     187            throw "Error: exponentIsInfinityConstant(2.1) should be Infinity, was = " + result;
     188    }
     189}
     190testExponentIsInfinityConstant();
     191
     192
     193function exponentIsNegativeInfinityConstant(x) {
     194    return Math.pow(x, -Infinity);
     195}
     196noInline(exponentIsNegativeInfinityConstant);
     197
     198function testExponentIsNegativeInfinityConstant() {
     199    for (var i = 0; i < 1000; ++i) {
     200        var result = exponentIsNegativeInfinityConstant(2);
     201        if (result !== 0)
     202            throw "Error: exponentIsNegativeInfinityConstant(2) should be zero, was = " + result;
     203    }
     204    for (var i = 0; i < 1000; ++i) {
     205        var result = exponentIsNegativeInfinityConstant(5);
     206        if (result !== 0)
     207            throw "Error: exponentIsNegativeInfinityConstant(5) should be zero, was = " + result;
     208    }
     209    for (var i = 0; i < 1000; ++i) {
     210        var result = exponentIsNegativeInfinityConstant(2.1);
     211        if (result !== 0)
     212            throw "Error: exponentIsNegativeInfinityConstant(2.1) should be zero, was = " + result;
     213    }
     214}
     215testExponentIsNegativeInfinityConstant();
Note: See TracChangeset for help on using the changeset viewer.