Changeset 37405 in webkit for trunk/JavaScriptCore/kjs


Ignore:
Timestamp:
Oct 7, 2008, 8:46:34 PM (17 years ago)
Author:
[email protected]
Message:

2008-10-07 Maciej Stachowiak <[email protected]>

Reviewed by Mark Rowe.


  • optimize away multiplication by constant 1.0


2.3% speedup on v8 RayTrace benchmark

Apparently it's not uncommon for JavaScript code to multiply by
constant 1.0 in the mistaken belief that this converts integer to
floating point and that there is any operational difference.


  • VM/CTI.cpp: (JSC::CTI::privateCompileMainPass): Optimize to_jsnumber for case where parameter is already number. (JSC::CTI::privateCompileSlowCases): ditto
  • VM/Machine.cpp: (JSC::Machine::privateExecute): ditto
  • kjs/grammar.y: (makeMultNode): Transform as follows: +FOO * BAR ==> FOO * BAR FOO * +BAR ==> FOO * BAR FOO * 1 ==> +FOO 1 * FOO ==> +FOO (makeDivNode): Transform as follows: +FOO / BAR ==> FOO / BAR FOO / +BAR ==> FOO / BAR (makeSubNode): Transform as follows: +FOO - BAR ==> FOO - BAR FOO - +BAR ==> FOO - BAR
  • kjs/nodes.h: (JSC::ExpressionNode::stripUnaryPlus): Helper for above grammar.y changes (JSC::UnaryPlusNode::stripUnaryPlus): ditto
Location:
trunk/JavaScriptCore/kjs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/grammar.y

    r37402 r37405  
    14251425static ExpressionNode* makeMultNode(void* globalPtr, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
    14261426{
     1427    // these transforms are valid because unary + only does a toNumber
     1428    // conversion, which * does anyway:
     1429    expr1 = expr1->stripUnaryPlus(); // +FOO * BAR ==> FOO * BAR
     1430    expr2 = expr2->stripUnaryPlus(); // FOO * +BAR ==> FOO * BAR
     1431
    14271432    if (expr1->isNumber() && expr2->isNumber())
    14281433        return makeNumberNode(globalPtr, static_cast<NumberNode*>(expr1)->value() * static_cast<NumberNode*>(expr2)->value());
     1434
     1435    // these transforms are valid because multiplying by 1 has no
     1436    // effect but toNumber conversion
     1437    if (expr1->isNumber() && static_cast<NumberNode*>(expr1)->value() == 1)
     1438        return new UnaryPlusNode(GLOBAL_DATA, expr2); // 1 * FOO ==> +FOO
     1439    if (expr2->isNumber() && static_cast<NumberNode*>(expr2)->value() == 1)
     1440        return new UnaryPlusNode(GLOBAL_DATA, expr1); // FOO * 1 ==> +FOO
     1441
    14291442    return new MultNode(GLOBAL_DATA, expr1, expr2, rightHasAssignments);
    14301443}
     
    14321445static ExpressionNode* makeDivNode(void* globalPtr, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
    14331446{
     1447    // these transforms are valid because unary + only does a toNumber
     1448    // conversion, which / does anyway:
     1449    expr1 = expr1->stripUnaryPlus(); // +FOO / BAR ==> FOO / BAR
     1450    expr2 = expr2->stripUnaryPlus(); // FOO / +BAR ==> FOO / BAR
     1451
    14341452    if (expr1->isNumber() && expr2->isNumber())
    14351453        return makeNumberNode(globalPtr, static_cast<NumberNode*>(expr1)->value() / static_cast<NumberNode*>(expr2)->value());
     
    14461464static ExpressionNode* makeSubNode(void* globalPtr, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
    14471465{
     1466    // these transforms are valid because unary + only does a toNumber
     1467    // conversion, which - does anyway:
     1468    expr1 = expr1->stripUnaryPlus(); // +FOO - BAR ==> FOO - BAR
     1469    expr2 = expr2->stripUnaryPlus(); // FOO - +BAR ==> FOO - BAR
     1470
    14481471    if (expr1->isNumber() && expr2->isNumber())
    14491472        return makeNumberNode(globalPtr, static_cast<NumberNode*>(expr1)->value() - static_cast<NumberNode*>(expr2)->value());
  • trunk/JavaScriptCore/kjs/nodes.h

    r37326 r37405  
    227227        virtual bool isDotAccessorNode() const JSC_FAST_CALL { return false; }
    228228
     229        virtual ExpressionNode* stripUnaryPlus() { return this; }
     230
    229231        ResultType resultDescriptor() const JSC_FAST_CALL { return m_resultDesc; }
    230232
     
    11791181        {
    11801182        }
     1183
     1184        virtual ExpressionNode* stripUnaryPlus() { return m_expr.get(); }
    11811185
    11821186        virtual OpcodeID opcode() const JSC_FAST_CALL { return op_to_jsnumber; }
Note: See TracChangeset for help on using the changeset viewer.