Title: [252789] trunk
Revision
252789
Author
[email protected]
Date
2019-11-22 11:55:27 -0800 (Fri, 22 Nov 2019)

Log Message

[JSC] DFG terminal's liveness should respect caller's opcodeID
https://bugs.webkit.org/show_bug.cgi?id=204317

Reviewed by Saam Barati.

JSTests:

* stress/call-var-args-phantom-arguments-handler-strict.js: Added.
(shouldBe):
(inlined):
(test):
* stress/call-var-args-phantom-arguments-handler.js: Added.
(shouldBe):
(inlined):
(test):
* stress/call-var-args-phantom-arguments-strict.js: Added.
(shouldBe):
(inlined):
(test):
* stress/call-var-args-phantom-arguments.js: Added.
(shouldBe):
(inlined):
(test):
* stress/derived-class-construct-varargs.js: Added.
(shouldThrow):
(B):
* stress/tail-call-var-args-phantom-arguments-handler-strict.js: Added.
(shouldBe):
(inlined):
(test):
* stress/tail-call-var-args-phantom-arguments-handler.js: Added.
(shouldBe):
(inlined):
(test):
* stress/tail-call-var-args-phantom-arguments-strict.js: Added.
(shouldBe):
(inlined):
(test):
* stress/tail-call-var-args-phantom-arguments.js: Added.
(shouldBe):
(inlined):
(test):

Source/_javascript_Core:

Let's consider the following example, which is freqneutly seen in Speedometer2/EmberJS-Debug.

    "use strict";

    function assertImpl(cond)
    {
        if (!cond)
            throw new Error();
    }

    function assert()
    {
        assertImpl.apply(undefined, arguments);
    }
    noInline(assert);

When compiling `throw`, we emit a terminal node and put Phantom/PhantomLocal based on the bytecode liveness.
When collecting liveness for each frame, we use the liveness information of the bytecode `op_call_varargs` in assert function.
This means that op_call_varargs's uses are considered as live (like, `arguments` in this example).
But it is not necessary to mark it "live": if we are in assertImpl, `arguments` is already loaded into the stack, and we no longer
use `arguments` when exiting, and the execution after the exit. Marking this `arguments` live makes this `arguments` allocated
in DFG, but this is wasteful.

In this patch, we introduce BeforeUse and AfterUse concept into bytecode liveness information. And use AfterUse information when
collecting liveness in the caller's frame in DFG. We only enable this for varargs for now since (1) applying this to the other ones
is not profitable, and (2) we need to be careful to make stack arguments live to allow materialization of arguments objects.
In op_call_varargs / op_tail_call_varargs / op_construct_varargs cases, uses are happen only for |callee|, |this|, and |arguments|.
And these are no longer necessary after calling.

We don't use liveness information in the next bytecode since it misses uses marked by exception handlers.

* bytecode/BytecodeLivenessAnalysis.cpp:
(JSC::BytecodeLivenessAnalysis::computeFullLiveness):
* bytecode/BytecodeLivenessAnalysis.h:
(JSC::BytecodeLivenessAnalysis::graph):
* bytecode/BytecodeLivenessAnalysisInlines.h:
(JSC::BytecodeLivenessPropagation::stepOverInstructionDef):
(JSC::BytecodeLivenessPropagation::stepOverInstructionUse):
(JSC::BytecodeLivenessPropagation::stepOverInstructionUseInExceptionHandler):
(JSC::BytecodeLivenessPropagation::stepOverInstruction):
* bytecode/BytecodeUseDef.h:
(JSC::computeUsesForBytecodeIndex):
(JSC::computeDefsForBytecodeIndex):
* bytecode/FullBytecodeLiveness.h:
(JSC::FullBytecodeLiveness::getLiveness const):
(JSC::FullBytecodeLiveness::operandIsLive const):
* bytecompiler/BytecodeGenerator.cpp:
(JSC::ForInContext::finalize):
* dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::flushForTerminalImpl):
* dfg/DFGForAllKills.h:
(JSC::DFG::forAllKilledOperands):
* dfg/DFGGraph.cpp:
(JSC::DFG::Graph::isLiveInBytecode):
* dfg/DFGGraph.h:
(JSC::DFG::Graph::forAllLocalsLiveInBytecode):
(JSC::DFG::Graph::appropriateLivenessCalculationPoint):
* llint/LowLevelInterpreter32_64.asm:
* llint/LowLevelInterpreter64.asm:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (252788 => 252789)


--- trunk/JSTests/ChangeLog	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/JSTests/ChangeLog	2019-11-22 19:55:27 UTC (rev 252789)
@@ -1,3 +1,46 @@
+2019-11-22  Yusuke Suzuki  <[email protected]>
+
+        [JSC] DFG terminal's liveness should respect caller's opcodeID
+        https://bugs.webkit.org/show_bug.cgi?id=204317
+
+        Reviewed by Saam Barati.
+
+        * stress/call-var-args-phantom-arguments-handler-strict.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/call-var-args-phantom-arguments-handler.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/call-var-args-phantom-arguments-strict.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/call-var-args-phantom-arguments.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/derived-class-construct-varargs.js: Added.
+        (shouldThrow):
+        (B):
+        * stress/tail-call-var-args-phantom-arguments-handler-strict.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/tail-call-var-args-phantom-arguments-handler.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/tail-call-var-args-phantom-arguments-strict.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+        * stress/tail-call-var-args-phantom-arguments.js: Added.
+        (shouldBe):
+        (inlined):
+        (test):
+
 2019-11-22  Guillaume Emont  <[email protected]>
 
         Skip tests that fail on arm and mips devices

Added: trunk/JSTests/stress/call-var-args-phantom-arguments-handler-strict.js (0 => 252789)


--- trunk/JSTests/stress/call-var-args-phantom-arguments-handler-strict.js	                        (rev 0)
+++ trunk/JSTests/stress/call-var-args-phantom-arguments-handler-strict.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,34 @@
+"use strict";
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+        shouldBe(arguments.length, 3);
+        shouldBe(arguments[0], 0);
+        shouldBe(arguments[1], 2);
+        shouldBe(arguments[2], 3);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/call-var-args-phantom-arguments-handler.js (0 => 252789)


--- trunk/JSTests/stress/call-var-args-phantom-arguments-handler.js	                        (rev 0)
+++ trunk/JSTests/stress/call-var-args-phantom-arguments-handler.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,32 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+        shouldBe(arguments.length, 3);
+        shouldBe(arguments[0], 0);
+        shouldBe(arguments[1], 2);
+        shouldBe(arguments[2], 3);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/call-var-args-phantom-arguments-strict.js (0 => 252789)


--- trunk/JSTests/stress/call-var-args-phantom-arguments-strict.js	                        (rev 0)
+++ trunk/JSTests/stress/call-var-args-phantom-arguments-strict.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,30 @@
+"use strict";
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/call-var-args-phantom-arguments.js (0 => 252789)


--- trunk/JSTests/stress/call-var-args-phantom-arguments.js	                        (rev 0)
+++ trunk/JSTests/stress/call-var-args-phantom-arguments.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,28 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/derived-class-construct-varargs.js (0 => 252789)


--- trunk/JSTests/stress/derived-class-construct-varargs.js	                        (rev 0)
+++ trunk/JSTests/stress/derived-class-construct-varargs.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,28 @@
+function shouldThrow(func, errorMessage) {
+    var errorThrown = false;
+    var error = null;
+    try {
+        func();
+    } catch (e) {
+        errorThrown = true;
+        error = e;
+    }
+    if (!errorThrown)
+        throw new Error('not thrown');
+    if (String(error) !== errorMessage)
+        throw new Error(`bad error: ${String(error)}`);
+}
+
+class A {
+    constructor(cond)
+    {
+        if (!cond)
+            throw new Error("OK");
+    }
+}
+
+class B extends A {}
+noInline(B);
+for (var i = 0; i < 1e6; ++i)
+    new B(1);
+shouldThrow(() => new B(0), `Error: OK`);

Added: trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler-strict.js (0 => 252789)


--- trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler-strict.js	                        (rev 0)
+++ trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler-strict.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,34 @@
+"use strict";
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        return inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+        shouldBe(arguments.length, 3);
+        shouldBe(arguments[0], 0);
+        shouldBe(arguments[1], 2);
+        shouldBe(arguments[2], 3);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler.js (0 => 252789)


--- trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler.js	                        (rev 0)
+++ trunk/JSTests/stress/tail-call-var-args-phantom-arguments-handler.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,32 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        return inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+        shouldBe(arguments.length, 3);
+        shouldBe(arguments[0], 0);
+        shouldBe(arguments[1], 2);
+        shouldBe(arguments[2], 3);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/tail-call-var-args-phantom-arguments-strict.js (0 => 252789)


--- trunk/JSTests/stress/tail-call-var-args-phantom-arguments-strict.js	                        (rev 0)
+++ trunk/JSTests/stress/tail-call-var-args-phantom-arguments-strict.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,30 @@
+"use strict";
+
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        return inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Added: trunk/JSTests/stress/tail-call-var-args-phantom-arguments.js (0 => 252789)


--- trunk/JSTests/stress/tail-call-var-args-phantom-arguments.js	                        (rev 0)
+++ trunk/JSTests/stress/tail-call-var-args-phantom-arguments.js	2019-11-22 19:55:27 UTC (rev 252789)
@@ -0,0 +1,28 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+function inlined(cond)
+{
+    if (!cond)
+        throw new Error("OK");
+}
+function test()
+{
+    try {
+        return inlined.apply(undefined, arguments);
+    } catch (error) {
+        shouldBe(String(error), `Error: OK`);
+    }
+}
+noInline(test);
+
+for (var i = 0; i < 1e7; ++i) {
+    test(1, 2, 3);
+}
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);
+test(0, 2, 3);

Modified: trunk/Source/_javascript_Core/ChangeLog (252788 => 252789)


--- trunk/Source/_javascript_Core/ChangeLog	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-11-22 19:55:27 UTC (rev 252789)
@@ -1,3 +1,70 @@
+2019-11-22  Yusuke Suzuki  <[email protected]>
+
+        [JSC] DFG terminal's liveness should respect caller's opcodeID
+        https://bugs.webkit.org/show_bug.cgi?id=204317
+
+        Reviewed by Saam Barati.
+
+        Let's consider the following example, which is freqneutly seen in Speedometer2/EmberJS-Debug.
+
+            "use strict";
+
+            function assertImpl(cond)
+            {
+                if (!cond)
+                    throw new Error();
+            }
+
+            function assert()
+            {
+                assertImpl.apply(undefined, arguments);
+            }
+            noInline(assert);
+
+        When compiling `throw`, we emit a terminal node and put Phantom/PhantomLocal based on the bytecode liveness.
+        When collecting liveness for each frame, we use the liveness information of the bytecode `op_call_varargs` in assert function.
+        This means that op_call_varargs's uses are considered as live (like, `arguments` in this example).
+        But it is not necessary to mark it "live": if we are in assertImpl, `arguments` is already loaded into the stack, and we no longer
+        use `arguments` when exiting, and the execution after the exit. Marking this `arguments` live makes this `arguments` allocated
+        in DFG, but this is wasteful.
+
+        In this patch, we introduce BeforeUse and AfterUse concept into bytecode liveness information. And use AfterUse information when
+        collecting liveness in the caller's frame in DFG. We only enable this for varargs for now since (1) applying this to the other ones
+        is not profitable, and (2) we need to be careful to make stack arguments live to allow materialization of arguments objects.
+        In op_call_varargs / op_tail_call_varargs / op_construct_varargs cases, uses are happen only for |callee|, |this|, and |arguments|.
+        And these are no longer necessary after calling.
+
+        We don't use liveness information in the next bytecode since it misses uses marked by exception handlers.
+
+        * bytecode/BytecodeLivenessAnalysis.cpp:
+        (JSC::BytecodeLivenessAnalysis::computeFullLiveness):
+        * bytecode/BytecodeLivenessAnalysis.h:
+        (JSC::BytecodeLivenessAnalysis::graph):
+        * bytecode/BytecodeLivenessAnalysisInlines.h:
+        (JSC::BytecodeLivenessPropagation::stepOverInstructionDef):
+        (JSC::BytecodeLivenessPropagation::stepOverInstructionUse):
+        (JSC::BytecodeLivenessPropagation::stepOverInstructionUseInExceptionHandler):
+        (JSC::BytecodeLivenessPropagation::stepOverInstruction):
+        * bytecode/BytecodeUseDef.h:
+        (JSC::computeUsesForBytecodeIndex):
+        (JSC::computeDefsForBytecodeIndex):
+        * bytecode/FullBytecodeLiveness.h:
+        (JSC::FullBytecodeLiveness::getLiveness const):
+        (JSC::FullBytecodeLiveness::operandIsLive const):
+        * bytecompiler/BytecodeGenerator.cpp:
+        (JSC::ForInContext::finalize):
+        * dfg/DFGByteCodeParser.cpp:
+        (JSC::DFG::ByteCodeParser::flushForTerminalImpl):
+        * dfg/DFGForAllKills.h:
+        (JSC::DFG::forAllKilledOperands):
+        * dfg/DFGGraph.cpp:
+        (JSC::DFG::Graph::isLiveInBytecode):
+        * dfg/DFGGraph.h:
+        (JSC::DFG::Graph::forAllLocalsLiveInBytecode):
+        (JSC::DFG::Graph::appropriateLivenessCalculationPoint):
+        * llint/LowLevelInterpreter32_64.asm:
+        * llint/LowLevelInterpreter64.asm:
+
 2019-11-22  Carlos Garcia Campos  <[email protected]>
 
         Unreviewed. Fix GTK/WPE debug build after r252770

Modified: trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.cpp (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.cpp	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.cpp	2019-11-22 19:55:27 UTC (rev 252789)
@@ -67,7 +67,8 @@
 {
     FastBitVector out;
 
-    result.m_map.resize(codeBlock->instructions().size());
+    result.m_beforeUseVector.resize(codeBlock->instructions().size());
+    result.m_afterUseVector.resize(codeBlock->instructions().size());
     
     for (std::unique_ptr<BytecodeBasicBlock>& block : m_graph.basicBlocksInReverseOrder()) {
         if (block->isEntryBlock() || block->isExitBlock())
@@ -75,10 +76,25 @@
         
         out = block->out();
         
+        auto use = [&] (unsigned bitIndex) {
+            // This is the use functor, so we set the bit.
+            out[bitIndex] = true;
+        };
+
+        auto def = [&] (unsigned bitIndex) {
+            // This is the def functor, so we clear the bit.
+            out[bitIndex] = false;
+        };
+
+        auto& instructions = codeBlock->instructions();
         for (unsigned i = block->offsets().size(); i--;) {
-            unsigned bytecodeOffset = block->offsets()[i];
-            stepOverInstruction(codeBlock, codeBlock->instructions(), m_graph, BytecodeIndex(bytecodeOffset), out);
-            result.m_map[bytecodeOffset] = out;
+            BytecodeIndex bytecodeIndex = BytecodeIndex(block->offsets()[i]);
+
+            stepOverInstructionDef(codeBlock, instructions, m_graph, bytecodeIndex, def);
+            stepOverInstructionUseInExceptionHandler(codeBlock, instructions, m_graph, bytecodeIndex, use);
+            result.m_afterUseVector[bytecodeIndex.offset()] = out; // AfterUse point.
+            stepOverInstructionUse(codeBlock, instructions, m_graph, bytecodeIndex, use);
+            result.m_beforeUseVector[bytecodeIndex.offset()] = out; // BeforeUse point.
         }
     }
 }

Modified: trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.h (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysis.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -35,19 +35,44 @@
 class BytecodeKills;
 class FullBytecodeLiveness;
 
+// We model our bytecode effects like the following and insert the liveness calculation points.
+//
+// <- BeforeUse
+//     Use
+// <- AfterUse
+//     Use by exception handlers
+//     Def
+enum class LivenessCalculationPoint : uint8_t {
+    BeforeUse,
+    AfterUse,
+};
+
 class BytecodeLivenessPropagation {
-protected:
-    template<typename CodeBlockType, typename UseFunctor, typename DefFunctor> void stepOverInstruction(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, const UseFunctor&, const DefFunctor&);
+public:
+    template<typename CodeBlockType, typename UseFunctor>
+    static void stepOverInstructionUse(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, const UseFunctor&);
+    template<typename CodeBlockType, typename UseFunctor>
+    static void stepOverInstructionUseInExceptionHandler(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, const UseFunctor&);
+    template<typename CodeBlockType, typename DefFunctor>
+    static void stepOverInstructionDef(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, const DefFunctor&);
 
-    template<typename CodeBlockType> void stepOverInstruction(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, FastBitVector& out);
+    template<typename CodeBlockType, typename UseFunctor, typename DefFunctor>
+    static void stepOverInstruction(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, const UseFunctor&, const DefFunctor&);
 
-    template<typename CodeBlockType, typename Instructions> bool computeLocalLivenessForBytecodeIndex(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeBasicBlock*, BytecodeIndex, FastBitVector& result);
+    template<typename CodeBlockType>
+    static void stepOverInstruction(CodeBlockType*, const InstructionStream&, BytecodeGraph&, BytecodeIndex, FastBitVector& out);
 
-    template<typename CodeBlockType, typename Instructions> bool computeLocalLivenessForBlock(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeBasicBlock*);
+    template<typename CodeBlockType, typename Instructions>
+    static bool computeLocalLivenessForBytecodeIndex(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeBasicBlock*, BytecodeIndex, FastBitVector& result);
 
-    template<typename CodeBlockType, typename Instructions> FastBitVector getLivenessInfoAtBytecodeIndex(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeIndex);
+    template<typename CodeBlockType, typename Instructions>
+    static bool computeLocalLivenessForBlock(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeBasicBlock*);
 
-    template<typename CodeBlockType, typename Instructions> void runLivenessFixpoint(CodeBlockType*, const Instructions&, BytecodeGraph&);
+    template<typename CodeBlockType, typename Instructions>
+    static FastBitVector getLivenessInfoAtBytecodeIndex(CodeBlockType*, const Instructions&, BytecodeGraph&, BytecodeIndex);
+
+    template<typename CodeBlockType, typename Instructions>
+    static void runLivenessFixpoint(CodeBlockType*, const Instructions&, BytecodeGraph&);
 };
 
 class BytecodeLivenessAnalysis : private BytecodeLivenessPropagation {
@@ -62,6 +87,8 @@
     void computeFullLiveness(CodeBlock*, FullBytecodeLiveness& result);
     void computeKills(CodeBlock*, BytecodeKills& result);
 
+    BytecodeGraph& graph() { return m_graph; }
+
 private:
     void dumpResults(CodeBlock*);
 

Modified: trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysisInlines.h (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysisInlines.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecode/BytecodeLivenessAnalysisInlines.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -58,43 +58,33 @@
     return operand.isLocal();
 }
 
-// Simplified interface to bytecode use/def, which determines defs first and then uses, and includes
-// exception handlers in the uses.
-template<typename CodeBlockType, typename UseFunctor, typename DefFunctor>
-inline void BytecodeLivenessPropagation::stepOverInstruction(CodeBlockType* codeBlock, const InstructionStream& instructions, BytecodeGraph& graph, BytecodeIndex bytecodeIndex, const UseFunctor& use, const DefFunctor& def)
+template<typename CodeBlockType, typename DefFunctor>
+inline void BytecodeLivenessPropagation::stepOverInstructionDef(CodeBlockType* codeBlock, const InstructionStream& instructions, BytecodeGraph&, BytecodeIndex bytecodeIndex, const DefFunctor& def)
 {
-    // This abstractly execute the instruction in reverse. Instructions logically first use operands and
-    // then define operands. This logical ordering is necessary for operations that use and def the same
-    // operand, like:
-    //
-    //     op_add loc1, loc1, loc2
-    //
-    // The use of loc1 happens before the def of loc1. That's a semantic requirement since the add
-    // operation cannot travel forward in time to read the value that it will produce after reading that
-    // value. Since we are executing in reverse, this means that we must do defs before uses (reverse of
-    // uses before defs).
-    //
-    // Since this is a liveness analysis, this ordering ends up being particularly important: if we did
-    // uses before defs, then the add operation above would appear to not have loc1 live, since we'd
-    // first add it to the out set (the use), and then we'd remove it (the def).
-
     auto* instruction = instructions.at(bytecodeIndex).ptr();
-    OpcodeID opcodeID = instruction->opcodeID();
-
     computeDefsForBytecodeIndex(
-        codeBlock, opcodeID, instruction,
+        codeBlock, instruction,
         [&] (VirtualRegister operand) {
             if (isValidRegisterForLiveness(operand))
                 def(operand.toLocal());
         });
+}
 
+template<typename CodeBlockType, typename UseFunctor>
+inline void BytecodeLivenessPropagation::stepOverInstructionUse(CodeBlockType* codeBlock, const InstructionStream& instructions, BytecodeGraph&, BytecodeIndex bytecodeIndex, const UseFunctor& use)
+{
+    auto* instruction = instructions.at(bytecodeIndex).ptr();
     computeUsesForBytecodeIndex(
-        codeBlock, opcodeID, instruction,
+        codeBlock, instruction,
         [&] (VirtualRegister operand) {
             if (isValidRegisterForLiveness(operand))
                 use(operand.toLocal());
         });
+}
 
+template<typename CodeBlockType, typename UseFunctor>
+inline void BytecodeLivenessPropagation::stepOverInstructionUseInExceptionHandler(CodeBlockType* codeBlock, const InstructionStream&, BytecodeGraph& graph, BytecodeIndex bytecodeIndex, const UseFunctor& use)
+{
     // If we have an exception handler, we want the live-in variables of the 
     // exception handler block to be included in the live-in of this particular bytecode.
     if (auto* handler = codeBlock->handlerForBytecodeIndex(bytecodeIndex)) {
@@ -104,6 +94,31 @@
     }
 }
 
+// Simplified interface to bytecode use/def, which determines defs first and then uses, and includes
+// exception handlers in the uses.
+template<typename CodeBlockType, typename UseFunctor, typename DefFunctor>
+inline void BytecodeLivenessPropagation::stepOverInstruction(CodeBlockType* codeBlock, const InstructionStream& instructions, BytecodeGraph& graph, BytecodeIndex bytecodeIndex, const UseFunctor& use, const DefFunctor& def)
+{
+    // This abstractly execute the instruction in reverse. Instructions logically first use operands and
+    // then define operands. This logical ordering is necessary for operations that use and def the same
+    // operand, like:
+    //
+    //     op_add loc1, loc1, loc2
+    //
+    // The use of loc1 happens before the def of loc1. That's a semantic requirement since the add
+    // operation cannot travel forward in time to read the value that it will produce after reading that
+    // value. Since we are executing in reverse, this means that we must do defs before uses (reverse of
+    // uses before defs).
+    //
+    // Since this is a liveness analysis, this ordering ends up being particularly important: if we did
+    // uses before defs, then the add operation above would appear to not have loc1 live, since we'd
+    // first add it to the out set (the use), and then we'd remove it (the def).
+
+    stepOverInstructionDef(codeBlock, instructions, graph, bytecodeIndex, def);
+    stepOverInstructionUseInExceptionHandler(codeBlock, instructions, graph, bytecodeIndex, use);
+    stepOverInstructionUse(codeBlock, instructions, graph, bytecodeIndex, use);
+}
+
 template<typename CodeBlockType>
 inline void BytecodeLivenessPropagation::stepOverInstruction(CodeBlockType* codeBlock, const InstructionStream& instructions, BytecodeGraph& graph, BytecodeIndex bytecodeIndex, FastBitVector& out)
 {

Modified: trunk/Source/_javascript_Core/bytecode/BytecodeUseDef.h (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecode/BytecodeUseDef.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecode/BytecodeUseDef.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -46,8 +46,9 @@
 #define DEFS USES_OR_DEFS
 
 template<typename Block, typename Functor>
-void computeUsesForBytecodeIndex(Block* codeBlock, OpcodeID opcodeID, const Instruction* instruction, const Functor& functor)
+void computeUsesForBytecodeIndex(Block* codeBlock, const Instruction* instruction, const Functor& functor)
 {
+    OpcodeID opcodeID = instruction->opcodeID();
     if (opcodeID != op_enter && (codeBlock->wasCompiledWithDebuggingOpcodes() || codeBlock->usesEval()) && codeBlock->scopeRegister().isValid())
         functor(codeBlock->scopeRegister());
 
@@ -298,9 +299,9 @@
 }
 
 template<typename Block, typename Functor>
-void computeDefsForBytecodeIndex(Block* codeBlock, OpcodeID opcodeID, const Instruction* instruction, const Functor& functor)
+void computeDefsForBytecodeIndex(Block* codeBlock, const Instruction* instruction, const Functor& functor)
 {
-    switch (opcodeID) {
+    switch (instruction->opcodeID()) {
     case op_wide16:
     case op_wide32:
         RELEASE_ASSERT_NOT_REACHED();

Modified: trunk/Source/_javascript_Core/bytecode/FullBytecodeLiveness.h (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecode/FullBytecodeLiveness.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecode/FullBytecodeLiveness.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "BytecodeLivenessAnalysis.h"
 #include <wtf/FastBitVector.h>
 
 namespace JSC {
@@ -31,26 +32,33 @@
 
 class BytecodeLivenessAnalysis;
 
-typedef HashMap<BytecodeIndex, FastBitVector> BytecodeToBitmapMap;
-
 class FullBytecodeLiveness {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    const FastBitVector& getLiveness(BytecodeIndex bytecodeIndex) const
+    const FastBitVector& getLiveness(BytecodeIndex bytecodeIndex, LivenessCalculationPoint point) const
     {
         // FIXME: What should this do when we have checkpoints?
-        return m_map[bytecodeIndex.offset()];
+        switch (point) {
+        case LivenessCalculationPoint::BeforeUse:
+            return m_beforeUseVector[bytecodeIndex.offset()];
+        case LivenessCalculationPoint::AfterUse:
+            return m_afterUseVector[bytecodeIndex.offset()];
+        }
+        ASSERT_NOT_REACHED();
     }
     
-    bool operandIsLive(int operand, BytecodeIndex bytecodeIndex) const
+    bool operandIsLive(int operand, BytecodeIndex bytecodeIndex, LivenessCalculationPoint point) const
     {
-        return operandIsAlwaysLive(operand) || operandThatIsNotAlwaysLiveIsLive(getLiveness(bytecodeIndex), operand);
+        return operandIsAlwaysLive(operand) || operandThatIsNotAlwaysLiveIsLive(getLiveness(bytecodeIndex, point), operand);
     }
     
 private:
     friend class BytecodeLivenessAnalysis;
     
-    Vector<FastBitVector, 0, UnsafeVectorOverflow> m_map;
+    // FIXME: Use FastBitVector's view mechansim to make them compact.
+    // https://bugs.webkit.org/show_bug.cgi?id=204427<Paste>
+    Vector<FastBitVector, 0, UnsafeVectorOverflow> m_beforeUseVector;
+    Vector<FastBitVector, 0, UnsafeVectorOverflow> m_afterUseVector;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp (252788 => 252789)


--- trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp	2019-11-22 19:55:27 UTC (rev 252789)
@@ -4891,10 +4891,8 @@
 
     for (unsigned offset = bodyBytecodeStartOffset(); isValid() && offset < bodyBytecodeEndOffset;) {
         auto instruction = generator.instructions().at(offset);
-        OpcodeID opcodeID = instruction->opcodeID();
-
-        ASSERT(opcodeID != op_enter);
-        computeDefsForBytecodeIndex(codeBlock, opcodeID, instruction.ptr(), [&] (VirtualRegister operand) {
+        ASSERT(!instruction->is<OpEnter>());
+        computeDefsForBytecodeIndex(codeBlock, instruction.ptr(), [&] (VirtualRegister operand) {
             if (local()->virtualRegister() == operand)
                 invalidate();
         });

Modified: trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp (252788 => 252789)


--- trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp	2019-11-22 19:55:27 UTC (rev 252789)
@@ -33,6 +33,7 @@
 #include "BasicBlockLocation.h"
 #include "BuiltinNames.h"
 #include "BytecodeGenerator.h"
+#include "BytecodeUseDef.h"
 #include "CallLinkStatus.h"
 #include "CodeBlock.h"
 #include "CodeBlockWithJITType.h"
@@ -74,6 +75,7 @@
 #include <wtf/CommaPrinter.h>
 #include <wtf/HashMap.h>
 #include <wtf/MathExtras.h>
+#include <wtf/Scope.h>
 #include <wtf/SetForScope.h>
 #include <wtf/StdLibExtras.h>
 
@@ -565,6 +567,7 @@
     template<typename AddFlushDirectFunc, typename AddPhantomLocalDirectFunc>
     void flushForTerminalImpl(CodeOrigin origin, const AddFlushDirectFunc& addFlushDirect, const AddPhantomLocalDirectFunc& addPhantomLocalDirect)
     {
+        bool isCallerOrigin = false;
         origin.walkUpInlineStack(
             [&] (CodeOrigin origin) {
                 BytecodeIndex bytecodeIndex = origin.bytecodeIndex();
@@ -573,12 +576,12 @@
 
                 CodeBlock* codeBlock = m_graph.baselineCodeBlockFor(inlineCallFrame);
                 FullBytecodeLiveness& fullLiveness = m_graph.livenessFor(codeBlock);
-                const FastBitVector& livenessAtBytecode = fullLiveness.getLiveness(bytecodeIndex);
-
+                const auto& livenessAtBytecode = fullLiveness.getLiveness(bytecodeIndex, m_graph.appropriateLivenessCalculationPoint(origin, isCallerOrigin));
                 for (unsigned local = codeBlock->numCalleeLocals(); local--;) {
                     if (livenessAtBytecode[local])
                         addPhantomLocalDirect(inlineCallFrame, remapOperand(inlineCallFrame, virtualRegisterForLocal(local)));
                 }
+                isCallerOrigin = true;
             });
     }
 

Modified: trunk/Source/_javascript_Core/dfg/DFGForAllKills.h (252788 => 252789)


--- trunk/Source/_javascript_Core/dfg/DFGForAllKills.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/dfg/DFGForAllKills.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -73,8 +73,8 @@
         int stackOffset = beforeInlineCallFrame ? beforeInlineCallFrame->stackOffset : 0;
         CodeBlock* codeBlock = graph.baselineCodeBlockFor(beforeInlineCallFrame);
         FullBytecodeLiveness& fullLiveness = graph.livenessFor(codeBlock);
-        const FastBitVector& liveBefore = fullLiveness.getLiveness(before.bytecodeIndex());
-        const FastBitVector& liveAfter = fullLiveness.getLiveness(after.bytecodeIndex());
+        const FastBitVector& liveBefore = fullLiveness.getLiveness(before.bytecodeIndex(), LivenessCalculationPoint::BeforeUse);
+        const FastBitVector& liveAfter = fullLiveness.getLiveness(after.bytecodeIndex(), LivenessCalculationPoint::BeforeUse);
         
         (liveBefore & ~liveAfter).forEachSetBit(
             [&] (size_t relativeLocal) {

Modified: trunk/Source/_javascript_Core/dfg/DFGGraph.cpp (252788 => 252789)


--- trunk/Source/_javascript_Core/dfg/DFGGraph.cpp	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/dfg/DFGGraph.cpp	2019-11-22 19:55:27 UTC (rev 252789)
@@ -1139,6 +1139,7 @@
     
     if (verbose)
         dataLog("Checking of operand is live: ", operand, "\n");
+    bool isCallerOrigin = false;
     CodeOrigin* codeOriginPtr = &codeOrigin;
     for (;;) {
         VirtualRegister reg = VirtualRegister(
@@ -1172,7 +1173,10 @@
 
             if (verbose)
                 dataLog("Asking the bytecode liveness.\n");
-            return livenessFor(inlineCallFrame).operandIsLive(reg.offset(), codeOriginPtr->bytecodeIndex());
+            CodeBlock* codeBlock = baselineCodeBlockFor(inlineCallFrame);
+            FullBytecodeLiveness& fullLiveness = livenessFor(codeBlock);
+            BytecodeIndex bytecodeIndex = codeOriginPtr->bytecodeIndex();
+            return fullLiveness.operandIsLive(reg.offset(), bytecodeIndex, appropriateLivenessCalculationPoint(*codeOriginPtr, isCallerOrigin));
         }
 
         if (!inlineCallFrame) {
@@ -1193,6 +1197,7 @@
         // We need to handle tail callers because we may decide to exit to the
         // the return bytecode following the tail call.
         codeOriginPtr = &inlineCallFrame->directCaller;
+        isCallerOrigin = true;
     }
     
     RELEASE_ASSERT_NOT_REACHED();

Modified: trunk/Source/_javascript_Core/dfg/DFGGraph.h (252788 => 252789)


--- trunk/Source/_javascript_Core/dfg/DFGGraph.h	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/dfg/DFGGraph.h	2019-11-22 19:55:27 UTC (rev 252789)
@@ -856,6 +856,7 @@
 
         CodeOrigin* codeOriginPtr = &codeOrigin;
         
+        bool isCallerOrigin = false;
         for (;;) {
             InlineCallFrame* inlineCallFrame = codeOriginPtr->inlineCallFrame();
             VirtualRegister stackOffset(inlineCallFrame ? inlineCallFrame->stackOffset : 0);
@@ -869,7 +870,7 @@
             
             CodeBlock* codeBlock = baselineCodeBlockFor(inlineCallFrame);
             FullBytecodeLiveness& fullLiveness = livenessFor(codeBlock);
-            const FastBitVector& liveness = fullLiveness.getLiveness(codeOriginPtr->bytecodeIndex());
+            const auto& livenessAtBytecode = fullLiveness.getLiveness(codeOriginPtr->bytecodeIndex(), appropriateLivenessCalculationPoint(*codeOriginPtr, isCallerOrigin));
             for (unsigned relativeLocal = codeBlock->numCalleeLocals(); relativeLocal--;) {
                 VirtualRegister reg = stackOffset + virtualRegisterForLocal(relativeLocal);
                 
@@ -877,7 +878,7 @@
                 if (reg >= exclusionStart && reg < exclusionEnd)
                     continue;
                 
-                if (liveness[relativeLocal])
+                if (livenessAtBytecode[relativeLocal])
                     functor(reg);
             }
             
@@ -899,6 +900,7 @@
             // We need to handle tail callers because we may decide to exit to the
             // the return bytecode following the tail call.
             codeOriginPtr = &inlineCallFrame->directCaller;
+            isCallerOrigin = true;
         }
     }
     
@@ -905,6 +907,36 @@
     // Get a BitVector of all of the non-argument locals live right now. This is mostly useful if
     // you want to compare two sets of live locals from two different CodeOrigins.
     BitVector localsLiveInBytecode(CodeOrigin);
+
+    LivenessCalculationPoint appropriateLivenessCalculationPoint(CodeOrigin origin, bool isCallerOrigin)
+    {
+        if (isCallerOrigin) {
+            // We do not need to keep used registers of call bytecodes live when terminating in inlined function,
+            // except for inlining invoked by non call bytecodes including getter/setter calls.
+            BytecodeIndex bytecodeIndex = origin.bytecodeIndex();
+            InlineCallFrame* inlineCallFrame = origin.inlineCallFrame();
+            CodeBlock* codeBlock = baselineCodeBlockFor(inlineCallFrame);
+            auto instruction = codeBlock->instructions().at(bytecodeIndex.offset());
+            switch (instruction->opcodeID()) {
+            case op_call_varargs:
+            case op_tail_call_varargs:
+            case op_construct_varargs:
+                // When inlining varargs call, uses include array used for varargs. But when we are in inlined function,
+                // the content of this is already read and flushed to the stack. So, at this point, we no longer need to
+                // keep these use registers. We can use the liveness at LivenessCalculationPoint::AfterUse point.
+                // This is important to kill arguments allocations in DFG (not in FTL) when calling a function in a
+                // `func.apply(undefined, arguments)` manner.
+                return LivenessCalculationPoint::AfterUse;
+            default:
+                // We could list up the other bytecodes here, like, `op_call`, `op_get_by_id` (getter inlining). But we don't do that.
+                // To list up bytecodes here, we must ensure that these bytecodes never use `uses` registers after inlining. So we cannot
+                // return LivenessCalculationPoint::AfterUse blindly if isCallerOrigin = true. And since excluding liveness in the other
+                // bytecodes does not offer practical benefit, we do not try it.
+                break;
+            }
+        }
+        return LivenessCalculationPoint::BeforeUse;
+    }
     
     // Tells you all of the arguments and locals live at the given CodeOrigin. This is a small
     // extension to forAllLocalsLiveInBytecode(), since all arguments are always presumed live.

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm (252788 => 252789)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2019-11-22 19:55:27 UTC (rev 252789)
@@ -91,6 +91,7 @@
 end
 
 
+# After calling, calling bytecode is claiming input registers are not used.
 macro dispatchAfterCall(size, opcodeStruct, dispatch)
     loadi ArgumentCount + TagOffset[cfr], PC
     get(size, opcodeStruct, m_dst, t3)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm (252788 => 252789)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2019-11-22 19:55:01 UTC (rev 252788)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2019-11-22 19:55:27 UTC (rev 252789)
@@ -90,6 +90,7 @@
     storeq value, %opcodeStruct%::Metadata::m_profile.m_buckets[metadata]
 end
 
+# After calling, calling bytecode is claiming input registers are not used.
 macro dispatchAfterCall(size, opcodeStruct, dispatch)
     loadi ArgumentCount + TagOffset[cfr], PC
     loadp CodeBlock[cfr], PB
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to