Changeset 241126 in webkit


Ignore:
Timestamp:
Feb 7, 2019 10:36:36 AM (5 years ago)
Author:
rmorisset@apple.com
Message:

B3ReduceStrength: missing peephole optimizations for Neg and Sub
https://bugs.webkit.org/show_bug.cgi?id=194250

Reviewed by Saam Barati.

Adds the following optimizations for integers:

  • Sub(x, x) => 0

Already covered by the test testSubArg

  • Sub(x1, Neg(x2)) => Add (x1, x2)

Added test: testSubNeg

  • Neg(Sub(x1, x2)) => Sub(x2, x1)

Added test: testNegSub

  • Add(Neg(x1), x2) => Sub(x2, x1)

Added test: testAddNeg1

  • Add(x1, Neg(x2)) => Sub(x1, x2)

Added test: testAddNeg2

Adds the following optimization for floating point values:

  • Abs(Neg(x)) => Abs(x)

Added test: testAbsNegArg
Adds the following optimization:

Also did some trivial refactoring, using m_value->isInteger() everywhere instead of isInt(m_value->type()), and using replaceWithNew<Value> instead of replaceWithNewValue(m_proc.add<Value(..))

  • b3/B3ReduceStrength.cpp:
  • b3/testb3.cpp:

(JSC::B3::testAddNeg1):
(JSC::B3::testAddNeg2):
(JSC::B3::testSubNeg):
(JSC::B3::testNegSub):
(JSC::B3::testAbsAbsArg):
(JSC::B3::testAbsNegArg):
(JSC::B3::run):

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r241117 r241126  
     12019-02-07  Robin Morisset  <rmorisset@apple.com>
     2
     3        B3ReduceStrength: missing peephole optimizations for Neg and Sub
     4        https://bugs.webkit.org/show_bug.cgi?id=194250
     5
     6        Reviewed by Saam Barati.
     7
     8        Adds the following optimizations for integers:
     9        - Sub(x, x) => 0
     10            Already covered by the test testSubArg
     11        - Sub(x1, Neg(x2)) => Add (x1, x2)
     12            Added test: testSubNeg
     13        - Neg(Sub(x1, x2)) => Sub(x2, x1)
     14            Added test: testNegSub
     15        - Add(Neg(x1), x2) => Sub(x2, x1)
     16            Added test: testAddNeg1
     17        - Add(x1, Neg(x2)) => Sub(x1, x2)
     18            Added test: testAddNeg2
     19        Adds the following optimization for floating point values:
     20        - Abs(Neg(x)) => Abs(x)
     21            Added test: testAbsNegArg
     22            Adds the following optimization:
     23
     24        Also did some trivial refactoring, using m_value->isInteger() everywhere instead of isInt(m_value->type()), and using replaceWithNew<Value> instead of replaceWithNewValue(m_proc.add<Value(..))
     25
     26        * b3/B3ReduceStrength.cpp:
     27        * b3/testb3.cpp:
     28        (JSC::B3::testAddNeg1):
     29        (JSC::B3::testAddNeg2):
     30        (JSC::B3::testSubNeg):
     31        (JSC::B3::testNegSub):
     32        (JSC::B3::testAbsAbsArg):
     33        (JSC::B3::testAbsNegArg):
     34        (JSC::B3::run):
     35
    1362019-02-06  Yusuke Suzuki  <ysuzuki@apple.com>
    237
  • trunk/Source/JavaScriptCore/b3/B3ReduceStrength.cpp

    r230098 r241126  
    500500            handleCommutativity();
    501501           
    502             if (m_value->child(0)->opcode() == Add && isInt(m_value->type())) {
     502            if (m_value->child(0)->opcode() == Add && m_value->isInteger()) {
    503503                // Turn this: Add(Add(value, constant1), constant2)
    504504                // Into this: Add(value, constant1 + constant2)
     
    533533            // Turn this: Add(otherValue, Add(value, constant))
    534534            // Into this: Add(Add(value, otherValue), constant)
    535             if (isInt(m_value->type())
     535            if (m_value->isInteger()
    536536                && !m_value->child(0)->hasInt()
    537537                && m_value->child(1)->opcode() == Add
     
    580580            }
    581581
    582             // Turn this: Integer Add(Sub(0, value), -1)
    583             // Into this: BitXor(value, -1)
    584             if (m_value->isInteger()
    585                 && m_value->child(0)->opcode() == Sub
    586                 && m_value->child(1)->isInt(-1)
    587                 && m_value->child(0)->child(0)->isInt(0)) {
    588                 replaceWithNewValue(m_proc.add<Value>(BitXor, m_value->origin(), m_value->child(0)->child(1), m_value->child(1)));
    589                 break;
     582            if (m_value->isInteger()) {
     583                // Turn this: Integer Add(value, Neg(otherValue))
     584                // Into this: Sub(value, otherValue)
     585                if (m_value->child(1)->opcode() == Neg) {
     586                    replaceWithNew<Value>(Sub, m_value->origin(), m_value->child(0), m_value->child(1)->child(0));
     587                    break;
     588                }
     589
     590                // Turn this: Integer Add(Neg(value), otherValue)
     591                // Into this: Sub(otherValue, value)
     592                if (m_value->child(0)->opcode() == Neg) {
     593                    replaceWithNew<Value>(Sub, m_value->origin(), m_value->child(1), m_value->child(0)->child(0));
     594                    break;
     595                }
     596
     597                // Turn this: Integer Add(Sub(0, value), -1)
     598                // Into this: BitXor(value, -1)
     599                if (m_value->child(0)->opcode() == Sub
     600                    && m_value->child(1)->isInt(-1)
     601                    && m_value->child(0)->child(0)->isInt(0)) {
     602                    replaceWithNew<Value>(BitXor, m_value->origin(), m_value->child(0)->child(1), m_value->child(1));
     603                    break;
     604                }
    590605            }
    591606
     
    600615            }
    601616
    602             if (isInt(m_value->type())) {
     617            if (m_value->isInteger()) {
    603618                // Turn this: Sub(value, constant)
    604619                // Into this: Add(value, -constant)
     
    616631                    break;
    617632                }
     633
     634                // Turn this: Sub(value, value)
     635                // Into this: 0
     636                if (m_value->child(0) == m_value->child(1)) {
     637                    replaceWithNewValue(m_proc.addIntConstant(m_value, 0));
     638                    break;
     639                }
     640
     641                // Turn this: Sub(value, Neg(otherValue))
     642                // Into this: Add(value, otherValue)
     643                if (m_value->child(1)->opcode() == Neg) {
     644                    replaceWithNew<Value>(Add, m_value->origin(), m_value->child(0), m_value->child(1)->child(0));
     645                    break;
     646                }
    618647            }
    619648
     
    635664            }
    636665           
     666            // Turn this: Integer Neg(Sub(value, otherValue))
     667            // Into this: Sub(otherValue, value)
     668            if (m_value->isInteger() && m_value->child(0)->opcode() == Sub) {
     669                replaceWithNew<Value>(Sub, m_value->origin(), m_value->child(0)->child(1), m_value->child(0)->child(0));
     670                break;
     671            }
     672
    637673            break;
    638674
     
    12001236            if (m_value->child(0)->opcode() == Abs) {
    12011237                replaceWithIdentity(m_value->child(0));
     1238                break;
     1239            }
     1240               
     1241            // Turn this: Abs(Neg(value))
     1242            // Into this: Abs(value)
     1243            if (m_value->child(0)->opcode() == Neg) {
     1244                replaceWithIdentity(m_value->child(0)->child(0));
    12021245                break;
    12031246            }
  • trunk/Source/JavaScriptCore/b3/testb3.cpp

    r239187 r241126  
    610610}
    611611
     612void testAddNeg1(int a, int b)
     613{
     614    Procedure proc;
     615    BasicBlock* root = proc.addBlock();
     616    root->appendNewControlValue(
     617        proc, Return, Origin(),
     618        root->appendNew<Value>(
     619            proc, Add, Origin(),
     620            root->appendNew<Value>(proc, Neg, Origin(),
     621                root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)),
     622            root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1)));
     623   
     624    CHECK(compileAndRun<int>(proc, a, b) == (- a) + b);
     625}
     626
     627void testAddNeg2(int a, int b)
     628{
     629    Procedure proc;
     630    BasicBlock* root = proc.addBlock();
     631    root->appendNewControlValue(
     632        proc, Return, Origin(),
     633        root->appendNew<Value>(
     634            proc, Add, Origin(),
     635            root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
     636            root->appendNew<Value>(proc, Neg, Origin(),
     637                root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1))));
     638
     639    CHECK(compileAndRun<int>(proc, a, b) == a + (- b));
     640}
     641
    612642void testAddArgZeroImmZDef()
    613643{
     
    18981928}
    18991929
     1930void testSubNeg(int a, int b)
     1931{
     1932    Procedure proc;
     1933    BasicBlock* root = proc.addBlock();
     1934    root->appendNewControlValue(
     1935        proc, Return, Origin(),
     1936        root->appendNew<Value>(
     1937            proc, Sub, Origin(),
     1938            root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
     1939            root->appendNew<Value>(proc, Neg, Origin(),
     1940                root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1))));
     1941   
     1942    CHECK(compileAndRun<int>(proc, a, b) == a - (- b));
     1943}
     1944
     1945void testNegSub(int a, int b)
     1946{
     1947    Procedure proc;
     1948    BasicBlock* root = proc.addBlock();
     1949    root->appendNewControlValue(
     1950        proc, Return, Origin(),
     1951        root->appendNew<Value>(
     1952            proc, Neg, Origin(),
     1953            root->appendNew<Value>(proc, Sub, Origin(),
     1954                root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0),
     1955                root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR1))));
     1956
     1957    CHECK(compileAndRun<int>(proc, a, b) == -(a - b));
     1958}
     1959
    19001960void testNegValueSubOne(int a)
    19011961{
     
    39183978    root->appendNewControlValue(proc, Return, Origin(), secondAbs);
    39193979
    3920     CHECK(isIdentical(compileAndRun<double>(proc, a), fabs(a)));
     3980    CHECK(isIdentical(compileAndRun<double>(proc, a), fabs(fabs(a))));
     3981}
     3982
     3983void testAbsNegArg(double a)
     3984{
     3985    Procedure proc;
     3986    BasicBlock* root = proc.addBlock();
     3987    Value* neg = root->appendNew<Value>(proc, Abs, Origin(),
     3988        root->appendNew<ArgumentRegValue>(proc, Origin(), FPRInfo::argumentFPR0));
     3989    Value* abs = root->appendNew<Value>(proc, Abs, Origin(), neg);
     3990    root->appendNewControlValue(proc, Return, Origin(), abs);
     3991   
     3992    CHECK(isIdentical(compileAndRun<double>(proc, a), fabs(- a)));
    39213993}
    39223994
     
    39984070    root->appendNewControlValue(proc, Return, Origin(), secondAbs);
    39994071
    4000     CHECK(isIdentical(compileAndRun<float>(proc, bitwise_cast<int32_t>(a)), static_cast<float>(fabs(a))));
     4072    CHECK(isIdentical(compileAndRun<float>(proc, bitwise_cast<int32_t>(a)), static_cast<float>(fabs(fabs(a)))));
     4073}
     4074
     4075void testAbsNegArg(float a)
     4076{
     4077    Procedure proc;
     4078    BasicBlock* root = proc.addBlock();
     4079    Value* argument32 = root->appendNew<Value>(proc, Trunc, Origin(),
     4080        root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0));
     4081    Value* argument = root->appendNew<Value>(proc, BitwiseCast, Origin(), argument32);
     4082    Value* neg = root->appendNew<Value>(proc, Neg, Origin(), argument);
     4083    Value* abs = root->appendNew<Value>(proc, Abs, Origin(), neg);
     4084    root->appendNewControlValue(proc, Return, Origin(), abs);
     4085   
     4086    CHECK(isIdentical(compileAndRun<float>(proc, bitwise_cast<int32_t>(a)), static_cast<float>(fabs(- a))));
    40014087}
    40024088
     
    1638316469    RUN_BINARY(testAddMemArg32, int32Operands(), int32Operands());
    1638416470    RUN_BINARY(testAddImmMem32, int32Operands(), int32Operands());
     16471    RUN_BINARY(testAddNeg1, int32Operands(), int32Operands());
     16472    RUN_BINARY(testAddNeg2, int32Operands(), int32Operands());
    1638516473    RUN(testAddArgZeroImmZDef());
    1638616474    RUN(testAddLoadTwice());
     
    1655016638    RUN_BINARY(testSubImmMem, int32Operands(), int32Operands());
    1655116639    RUN_BINARY(testSubMemImm, int32Operands(), int32Operands());
     16640    RUN_BINARY(testSubNeg, int32Operands(), int32Operands());
     16641    RUN_BINARY(testNegSub, int32Operands(), int32Operands());
    1655216642    RUN_UNARY(testNegValueSubOne, int32Operands());
    1655316643
     
    1690616996    RUN_UNARY(testAbsMem, floatingPointOperands<double>());
    1690716997    RUN_UNARY(testAbsAbsArg, floatingPointOperands<double>());
     16998    RUN_UNARY(testAbsNegArg, floatingPointOperands<double>());
    1690816999    RUN_UNARY(testAbsBitwiseCastArg, floatingPointOperands<double>());
    1690917000    RUN_UNARY(testBitwiseCastAbsBitwiseCastArg, floatingPointOperands<double>());
     
    1691217003    RUN_UNARY(testAbsMem, floatingPointOperands<float>());
    1691317004    RUN_UNARY(testAbsAbsArg, floatingPointOperands<float>());
     17005    RUN_UNARY(testAbsNegArg, floatingPointOperands<float>());
    1691417006    RUN_UNARY(testAbsBitwiseCastArg, floatingPointOperands<float>());
    1691517007    RUN_UNARY(testBitwiseCastAbsBitwiseCastArg, floatingPointOperands<float>());
Note: See TracChangeset for help on using the changeset viewer.