[JSC] Make FRound work with any type
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Aug 2016 01:21:43 +0000 (01:21 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Aug 2016 01:21:43 +0000 (01:21 +0000)
https://bugs.webkit.org/show_bug.cgi?id=161129

Reviewed by Geoffrey Garen.

JSTests:

* stress/arith-fround-on-various-types.js: Added.

Source/JavaScriptCore:

Math.fround() does nothing with arguments past the first one
(https://tc39.github.io/ecma262/#sec-math.fround).
We can unify ArithFRound with the other single-input intrinsics.

Everything else is same old: if the input type is not a number,
be pessimistic about everything and do a C call.

* dfg/DFGAbstractInterpreterInlines.h:
(JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
* dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleIntrinsicCall):
* dfg/DFGClobberize.h:
(JSC::DFG::clobberize):
* dfg/DFGFixupPhase.cpp:
(JSC::DFG::FixupPhase::fixupNode):
* dfg/DFGNodeType.h:
* dfg/DFGOperations.cpp:
* dfg/DFGOperations.h:
* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileArithFRound):
* dfg/DFGSpeculativeJIT.h:
* dfg/DFGSpeculativeJIT32_64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* dfg/DFGSpeculativeJIT64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArithFRound):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@204947 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
JSTests/ChangeLog
JSTests/stress/arith-fround-on-various-types.js [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h
Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp
Source/JavaScriptCore/dfg/DFGClobberize.h
Source/JavaScriptCore/dfg/DFGFixupPhase.cpp
Source/JavaScriptCore/dfg/DFGNodeType.h
Source/JavaScriptCore/dfg/DFGOperations.cpp
Source/JavaScriptCore/dfg/DFGOperations.h
Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp
Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h
Source/JavaScriptCore/dfg/DFGSpeculativeJIT32_64.cpp
Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp
Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp

index a67d006..6410087 100644 (file)
@@ -1,3 +1,12 @@
+2016-08-24  Benjamin Poulain  <benjamin@webkit.org>
+
+        [JSC] Make FRound work with any type
+        https://bugs.webkit.org/show_bug.cgi?id=161129
+
+        Reviewed by Geoffrey Garen.
+
+        * stress/arith-fround-on-various-types.js: Added.
+
 2016-08-24  Filip Pizlo  <fpizlo@apple.com>
 
         Unreviewed, roll out r204901, r204897, r204866, r204856, r204854.
diff --git a/JSTests/stress/arith-fround-on-various-types.js b/JSTests/stress/arith-fround-on-various-types.js
new file mode 100644 (file)
index 0000000..3e17ab2
--- /dev/null
@@ -0,0 +1,190 @@
+"use strict";
+
+let froundOfPi = Math.fround(Math.PI);
+let froundOfE = Math.fround(Math.E);
+
+let validInputTestCases = [
+    // input as string, expected result as string.
+    ["undefined", "NaN"],
+    ["null", "0"],
+    ["0", "0"],
+    ["-0.", "-0"],
+    ["1.", "1"],
+    ["Math.PI", "" + froundOfPi],
+    ["Math.E", "" + froundOfE],
+    ["Infinity", "Infinity"],
+    ["-Infinity", "-Infinity"],
+    ["NaN", "NaN"],
+    ["\"WebKit\"", "NaN"],
+    ["\"" + Math.PI + "\"", "" + froundOfPi],
+    ["{ valueOf: () => { return Math.E; } }", "" + froundOfE],
+    ["{ valueOf: () => { return 1; } }", "1"],
+    ["{ valueOf: () => { return Math.PI; } }", "" + froundOfPi],
+];
+
+let validInputTypedTestCases = validInputTestCases.map((element) => { return [eval("(" + element[0] + ")"), eval(element[1])] });
+
+function isIdentical(result, expected)
+{
+    if (expected === expected) {
+        if (result !== expected)
+            return false;
+        if (!expected && 1 / expected === -Infinity && 1 / result !== -Infinity)
+            return false;
+
+        return true;
+    }
+    return result !== result;
+}
+
+// Test Math.fround() without arguments.
+function opaqueFRoundNoArgument() {
+    return Math.fround();
+}
+noInline(opaqueFRoundNoArgument);
+noOSRExitFuzzing(opaqueFRoundNoArgument);
+
+function testNoArgument() {
+    for (let i = 0; i < 1e4; ++i) {
+        let output = opaqueFRoundNoArgument();
+        if (output === output) {
+            throw "Failed opaqueFRoundNoArgument";
+        }
+    }
+    if (numberOfDFGCompiles(opaqueFRoundNoArgument) > 1)
+        throw "The call without arguments should never exit.";
+}
+testNoArgument();
+
+// Test Math.fround() with a very polymorphic input. All test cases are seen at each iteration.
+function opaqueAllTypesFround(argument) {
+    return Math.fround(argument);
+}
+noInline(opaqueAllTypesFround);
+noOSRExitFuzzing(opaqueAllTypesFround);
+
+function testAllTypesCall() {
+    for (let i = 0; i < 1e3; ++i) {
+        for (let testCaseInput of validInputTypedTestCases) {
+            let output = opaqueAllTypesFround(testCaseInput[0]);
+            if (!isIdentical(output, testCaseInput[1]))
+                throw "Failed testAllTypesCall for input " + testCaseInput[0] + " expected " + testCaseInput[1] + " got " + output;
+        }
+    }
+    if (numberOfDFGCompiles(opaqueAllTypesFround) > 2)
+        throw "We should have detected fround() was polymorphic and generated a generic version.";
+}
+testAllTypesCall();
+
+
+// Test Math.fround() on a completely typed input. Every call see only one type.
+function testSingleTypeCall() {
+    for (let testCaseInput of validInputTestCases) {
+        eval(`
+            function opaqueFround(argument) {
+                return Math.fround(argument);
+            }
+            noInline(opaqueFround);
+            noOSRExitFuzzing(opaqueFround);
+
+            for (let i = 0; i < 1e4; ++i) {
+                if (!isIdentical(opaqueFround(${testCaseInput[0]}), ${testCaseInput[1]})) {
+                    throw "Failed testSingleTypeCall()";
+                }
+            }
+            if (numberOfDFGCompiles(opaqueFround) > 1)
+                throw "We should have compiled a single fround for the expected type.";
+        `);
+    }
+}
+testSingleTypeCall();
+
+
+// Verify we call valueOf() exactly once per call.
+function opaqueFroundForSideEffects(argument) {
+    return Math.fround(argument);
+}
+noInline(opaqueFroundForSideEffects);
+noOSRExitFuzzing(opaqueFroundForSideEffects);
+
+function testSideEffect() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let fround16 = Math.fround(16);
+    for (let i = 0; i < 1e4; ++i) {
+        if (opaqueFroundForSideEffects(testObject) !== fround16)
+            throw "Incorrect result in testSideEffect()";
+    }
+    if (testObject.counter !== 1e4)
+        throw "Failed testSideEffect()";
+    if (numberOfDFGCompiles(opaqueFroundForSideEffects) > 1)
+        throw "opaqueFroundForSideEffects() is predictable, it should only be compiled once.";
+}
+testSideEffect();
+
+
+// Verify SQRT is not subject to CSE if the argument has side effects.
+function opaqueFroundForCSE(argument) {
+    return Math.fround(argument) + Math.fround(argument) + Math.fround(argument);
+}
+noInline(opaqueFroundForCSE);
+noOSRExitFuzzing(opaqueFroundForCSE);
+
+function testCSE() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let fround16 = Math.fround(16);
+    let threeFround16 = fround16 + fround16 + fround16;
+    for (let i = 0; i < 1e4; ++i) {
+        if (opaqueFroundForCSE(testObject) !== threeFround16)
+            throw "Incorrect result in testCSE()";
+    }
+    if (testObject.counter !== 3e4)
+        throw "Failed testCSE()";
+    if (numberOfDFGCompiles(opaqueFroundForCSE) > 1)
+        throw "opaqueFroundForCSE() is predictable, it should only be compiled once.";
+}
+testCSE();
+
+
+// Test exceptions in the argument.
+function testException() {
+    let counter = 0;
+    function opaqueFroundWithException(argument) {
+        let result = Math.fround(argument);
+        ++counter;
+        return result;
+    }
+    noInline(opaqueFroundWithException);
+
+    let testObject = { valueOf: () => {  return 64; } };
+    let fround64 = Math.fround(64);
+
+    // Warm up without exception.
+    for (let i = 0; i < 1e3; ++i) {
+        if (opaqueFroundWithException(testObject) !== fround64)
+            throw "Incorrect result in opaqueFroundWithException()";
+    }
+
+    let testThrowObject = { valueOf: () => { throw testObject; return 64; } };
+
+    for (let i = 0; i < 1e2; ++i) {
+        try {
+            if (opaqueFroundWithException(testThrowObject) !== 8)
+                throw "This code should not be reached!!";
+        } catch (e) {
+            if (e !== testObject) {
+                throw "Wrong object thrown from opaqueFroundWithException."
+            }
+        }
+    }
+
+    if (counter !== 1e3) {
+        throw "Invalid count in testException()";
+    }
+}
+testException();
index b1b7334..2888545 100644 (file)
@@ -1,3 +1,38 @@
+2016-08-24  Benjamin Poulain  <benjamin@webkit.org>
+
+        [JSC] Make FRound work with any type
+        https://bugs.webkit.org/show_bug.cgi?id=161129
+
+        Reviewed by Geoffrey Garen.
+
+        Math.fround() does nothing with arguments past the first one
+        (https://tc39.github.io/ecma262/#sec-math.fround).
+        We can unify ArithFRound with the other single-input intrinsics.
+
+        Everything else is same old: if the input type is not a number,
+        be pessimistic about everything and do a C call.
+
+        * dfg/DFGAbstractInterpreterInlines.h:
+        (JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
+        * dfg/DFGByteCodeParser.cpp:
+        (JSC::DFG::ByteCodeParser::handleIntrinsicCall):
+        * dfg/DFGClobberize.h:
+        (JSC::DFG::clobberize):
+        * dfg/DFGFixupPhase.cpp:
+        (JSC::DFG::FixupPhase::fixupNode):
+        * dfg/DFGNodeType.h:
+        * dfg/DFGOperations.cpp:
+        * dfg/DFGOperations.h:
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileArithFRound):
+        * dfg/DFGSpeculativeJIT.h:
+        * dfg/DFGSpeculativeJIT32_64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * dfg/DFGSpeculativeJIT64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileArithFRound):
+
 2016-08-24  Andreas Kling  <akling@apple.com>
 
         Shrink DFG::OSRExit a bit.
index 2d50518..3297e37 100644 (file)
@@ -974,7 +974,10 @@ bool AbstractInterpreter<AbstractStateType>::executeEffects(unsigned clobberLimi
             setConstant(node, jsDoubleNumber(static_cast<float>(child.asNumber())));
             break;
         }
-        forNode(node).setType(typeOfDoubleRounding(forNode(node->child1()).m_type));
+        SpeculatedType froundType = SpecFullNumber;
+        if (node->child1().useKind() == DoubleRepUse)
+            froundType = typeOfDoubleUnaryOp(forNode(node->child1()).m_type);
+        forNode(node).setType(froundType);
         break;
     }
         
index 78450ad..2cca55d 100644 (file)
@@ -2140,41 +2140,40 @@ bool ByteCodeParser::handleIntrinsicCall(Node* callee, int resultOperand, Intrin
     case MaxIntrinsic:
         return handleMinMax(resultOperand, ArithMax, registerOffset, argumentCountIncludingThis, insertChecks);
 
-    case SqrtIntrinsic:
     case CosIntrinsic:
+    case FRoundIntrinsic:
+    case LogIntrinsic:
     case SinIntrinsic:
-    case LogIntrinsic: {
+    case SqrtIntrinsic: {
         if (argumentCountIncludingThis == 1) {
             insertChecks();
             set(VirtualRegister(resultOperand), addToGraph(JSConstant, OpInfo(m_constantNaN)));
             return true;
         }
-        
+
+        NodeType nodeType = Unreachable;
         switch (intrinsic) {
-        case SqrtIntrinsic:
-            insertChecks();
-            set(VirtualRegister(resultOperand), addToGraph(ArithSqrt, get(virtualRegisterForArgument(1, registerOffset))));
-            return true;
-            
         case CosIntrinsic:
-            insertChecks();
-            set(VirtualRegister(resultOperand), addToGraph(ArithCos, get(virtualRegisterForArgument(1, registerOffset))));
-            return true;
-            
-        case SinIntrinsic:
-            insertChecks();
-            set(VirtualRegister(resultOperand), addToGraph(ArithSin, get(virtualRegisterForArgument(1, registerOffset))));
-            return true;
-
+            nodeType = ArithCos;
+            break;
+        case FRoundIntrinsic:
+            nodeType = ArithFRound;
+            break;
         case LogIntrinsic:
-            insertChecks();
-            set(VirtualRegister(resultOperand), addToGraph(ArithLog, get(virtualRegisterForArgument(1, registerOffset))));
-            return true;
-            
+            nodeType = ArithLog;
+            break;
+        case SinIntrinsic:
+            nodeType = ArithSin;
+            break;
+        case SqrtIntrinsic:
+            nodeType = ArithSqrt;
+            break;
         default:
             RELEASE_ASSERT_NOT_REACHED();
-            return false;
         }
+        insertChecks();
+        set(VirtualRegister(resultOperand), addToGraph(nodeType, get(virtualRegisterForArgument(1, registerOffset))));
+        return true;
     }
 
     case PowIntrinsic: {
@@ -2476,15 +2475,6 @@ bool ByteCodeParser::handleIntrinsicCall(Node* callee, int resultOperand, Intrin
         return true;
     }
         
-    case FRoundIntrinsic: {
-        if (argumentCountIncludingThis != 2)
-            return false;
-        insertChecks();
-        VirtualRegister operand = virtualRegisterForArgument(1, registerOffset);
-        set(VirtualRegister(resultOperand), addToGraph(ArithFRound, get(operand)));
-        return true;
-    }
-        
     case DFGTrueIntrinsic: {
         insertChecks();
         set(VirtualRegister(resultOperand), jsConstant(jsBoolean(true)));
index 5964753..c1cf97b 100644 (file)
@@ -154,7 +154,6 @@ void clobberize(Graph& graph, Node* node, const ReadFunctor& read, const WriteFu
     case ArithMin:
     case ArithMax:
     case ArithPow:
-    case ArithFRound:
     case GetScope:
     case SkipScope:
     case GetGlobalObject:
@@ -187,6 +186,7 @@ void clobberize(Graph& graph, Node* node, const ReadFunctor& read, const WriteFu
         return;
 
     case ArithCos:
+    case ArithFRound:
     case ArithLog:
     case ArithSin:
     case ArithSqrt:
index dda2f59..23f1a7a 100644 (file)
@@ -387,6 +387,7 @@ private:
         }
 
         case ArithCos:
+        case ArithFRound:
         case ArithLog:
         case ArithSin:
         case ArithSqrt: {
@@ -397,11 +398,6 @@ private:
                 fixEdge<UntypedUse>(child1);
             break;
         }
-        case ArithFRound: {
-            fixDoubleOrBooleanEdge(node->child1());
-            node->setResult(NodeResultDouble);
-            break;
-        }
             
         case LogicalNot: {
             if (node->child1()->shouldSpeculateBoolean()) {
index 733c384..084a37e 100644 (file)
@@ -152,7 +152,7 @@ namespace JSC { namespace DFG {
     macro(ArithAbs, NodeResultNumber | NodeMustGenerate) \
     macro(ArithMin, NodeResultNumber) \
     macro(ArithMax, NodeResultNumber) \
-    macro(ArithFRound, NodeResultNumber) \
+    macro(ArithFRound, NodeResultDouble) \
     macro(ArithPow, NodeResultDouble) \
     macro(ArithRandom, NodeResultDouble | NodeMustGenerate) \
     macro(ArithRound, NodeResultNumber) \
index 7cf6870..ce20d28 100644 (file)
@@ -334,6 +334,18 @@ double JIT_OPERATION operationArithCos(ExecState* exec, EncodedJSValue encodedOp
     return cos(a);
 }
 
+double JIT_OPERATION operationArithFRound(ExecState* exec, EncodedJSValue encodedOp1)
+{
+    VM* vm = &exec->vm();
+    NativeCallFrameTracer tracer(vm, exec);
+
+    JSValue op1 = JSValue::decode(encodedOp1);
+    double a = op1.toNumber(exec);
+    if (UNLIKELY(vm->exception()))
+        return JSValue::encode(JSValue());
+    return static_cast<float>(a);
+}
+
 double JIT_OPERATION operationArithLog(ExecState* exec, EncodedJSValue encodedOp1)
 {
     VM* vm = &exec->vm();
index f219357..f0117a8 100644 (file)
@@ -54,6 +54,7 @@ EncodedJSValue JIT_OPERATION operationValueBitURShift(ExecState*, EncodedJSValue
 EncodedJSValue JIT_OPERATION operationValueAddNotNumber(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
 EncodedJSValue JIT_OPERATION operationValueDiv(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
 double JIT_OPERATION operationArithCos(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
+double JIT_OPERATION operationArithFRound(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 double JIT_OPERATION operationArithLog(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 double JIT_OPERATION operationArithSin(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 double JIT_OPERATION operationArithSqrt(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
index ba9d95f..819d0a9 100644 (file)
@@ -4538,6 +4538,26 @@ void SpeculativeJIT::compileArithDiv(Node* node)
     }
 }
 
+void SpeculativeJIT::compileArithFRound(Node* node)
+{
+    if (node->child1().useKind() == DoubleRepUse) {
+        SpeculateDoubleOperand op1(this, node->child1());
+        FPRTemporary result(this, op1);
+        m_jit.convertDoubleToFloat(op1.fpr(), result.fpr());
+        m_jit.convertFloatToDouble(result.fpr(), result.fpr());
+        doubleResult(result.fpr(), node);
+        return;
+    }
+
+    JSValueOperand op1(this, node->child1());
+    JSValueRegs op1Regs = op1.jsValueRegs();
+    flushRegisters();
+    FPRResult result(this);
+    callOperation(operationArithFRound, result.fpr(), op1Regs);
+    m_jit.exceptionCheck();
+    doubleResult(result.fpr(), node);
+}
+
 void SpeculativeJIT::compileArithMod(Node* node)
 {
     switch (node->binaryUseKind()) {
index 5db0fd8..28da890 100644 (file)
@@ -2474,6 +2474,7 @@ public:
     void compileArithNegate(Node*);
     void compileArithMul(Node*);
     void compileArithDiv(Node*);
+    void compileArithFRound(Node*);
     void compileArithMod(Node*);
     void compileArithPow(Node*);
     void compileArithRounding(Node*);
index f2bb673..bf7f9fc 100644 (file)
@@ -2367,16 +2367,9 @@ void SpeculativeJIT::compile(Node* node)
         compileArithSqrt(node);
         break;
 
-    case ArithFRound: {
-        SpeculateDoubleOperand op1(this, node->child1());
-        FPRTemporary result(this, op1);
-        
-        m_jit.convertDoubleToFloat(op1.fpr(), result.fpr());
-        m_jit.convertFloatToDouble(result.fpr(), result.fpr());
-        
-        doubleResult(result.fpr(), node);
+    case ArithFRound:
+        compileArithFRound(node);
         break;
-    }
 
     case ArithRandom:
         compileArithRandom(node);
index b6c2d01..cae5139 100644 (file)
@@ -2491,16 +2491,9 @@ void SpeculativeJIT::compile(Node* node)
         compileArithSqrt(node);
         break;
 
-    case ArithFRound: {
-        SpeculateDoubleOperand op1(this, node->child1());
-        FPRTemporary result(this, op1);
-        
-        m_jit.convertDoubleToFloat(op1.fpr(), result.fpr());
-        m_jit.convertFloatToDouble(result.fpr(), result.fpr());
-        
-        doubleResult(result.fpr(), node);
+    case ArithFRound:
+        compileArithFRound(node);
         break;
-    }
 
     case ArithRandom:
         compileArithRandom(node);
index a61d36f..659430e 100644 (file)
@@ -2315,7 +2315,13 @@ private:
     
     void compileArithFRound()
     {
-        setDouble(m_out.fround(lowDouble(m_node->child1())));
+        if (m_node->child1().useKind() == DoubleRepUse) {
+            setDouble(m_out.fround(lowDouble(m_node->child1())));
+            return;
+        }
+        LValue argument = lowJSValue(m_node->child1());
+        LValue result = vmCall(Double, m_out.operation(operationArithFRound), m_callFrame, argument);
+        setDouble(result);
     }
     
     void compileArithNegate()