2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "DFGPhantomInsertionPhase.h"
31 #include "BytecodeLivenessAnalysisInlines.h"
32 #include "DFGForAllKills.h"
34 #include "DFGInsertionSet.h"
35 #include "DFGMayExit.h"
37 #include "DFGPredictionPropagationPhase.h"
38 #include "DFGVariableAccessDataDump.h"
39 #include "JSCInlines.h"
40 #include "OperandsInlines.h"
42 namespace JSC
{ namespace DFG
{
48 class PhantomInsertionPhase
: public Phase
{
50 PhantomInsertionPhase(Graph
& graph
)
51 : Phase(graph
, "phantom insertion")
52 , m_insertionSet(graph
)
53 , m_values(OperandsLike
, graph
.block(0)->variablesAtHead
)
59 // We assume that DCE has already run. If we run before DCE then we think that all
60 // SetLocals execute, which is inaccurate. That causes us to insert too few Phantoms.
61 DFG_ASSERT(m_graph
, nullptr, m_graph
.m_refCountState
== ExactRefCount
);
64 dataLog("Graph before Phantom insertion:\n");
68 m_graph
.clearEpochs();
70 for (BasicBlock
* block
: m_graph
.blocksInNaturalOrder())
74 dataLog("Graph after Phantom insertion:\n");
82 void handleBlock(BasicBlock
* block
)
84 // FIXME: For blocks that have low register pressure, it would make the most sense to
85 // simply insert Phantoms at the last point possible since that would obviate the need to
86 // query bytecode liveness:
88 // - If we MovHint @x into loc42 then put a Phantom on the last MovHinted value in loc42.
89 // - At the end of the block put Phantoms for each MovHinted value.
91 // This will definitely not work if there are any phantom allocations. For those blocks
92 // where this would be legal, it remains to be seen how profitable it would be even if there
93 // was high register pressure. After all, a Phantom would cause a spill but it wouldn't
96 // https://bugs.webkit.org/show_bug.cgi?id=144524
98 m_values
.fill(nullptr);
100 Epoch currentEpoch
= Epoch::first();
101 unsigned lastExitingIndex
= 0;
102 for (unsigned nodeIndex
= 0; nodeIndex
< block
->size(); ++nodeIndex
) {
103 Node
* node
= block
->at(nodeIndex
);
105 dataLog("Considering ", node
, "\n");
107 switch (node
->op()) {
109 m_values
.operand(node
->unlinkedLocal()) = node
->child1().node();
113 m_values
.operand(node
->unlinkedLocal()) = nullptr;
119 m_values
.operand(node
->local()) = nullptr;
126 if (mayExit(m_graph
, node
)) {
128 lastExitingIndex
= nodeIndex
;
131 m_graph
.doToChildren(
134 edge
->setEpoch(currentEpoch
);
137 node
->setEpoch(currentEpoch
);
139 forAllKilledOperands(
140 m_graph
, node
, block
->tryAt(nodeIndex
+ 1),
141 [&] (VirtualRegister reg
) {
143 dataLog(" Killed operand: ", reg
, "\n");
145 Node
* killedNode
= m_values
.operand(reg
);
149 // We only need to insert a Phantom if the node hasn't been used since the last
150 // exit, and was born before the last exit.
151 if (killedNode
->epoch() == currentEpoch
)
156 " Inserting Phantom on ", killedNode
, " after ",
157 block
->at(lastExitingIndex
), "\n");
160 // We have exact ref counts, so creating a new use means that we have to
161 // increment the ref count.
162 killedNode
->postfixRef();
164 m_insertionSet
.insertNode(
165 lastExitingIndex
+ 1, SpecNone
, Phantom
,
166 block
->at(lastExitingIndex
)->origin
, killedNode
->defaultEdge());
170 m_insertionSet
.execute(block
);
173 InsertionSet m_insertionSet
;
174 Operands
<Node
*> m_values
;
177 } // anonymous namespace
179 bool performPhantomInsertion(Graph
& graph
)
181 SamplingRegion
samplingRegion("DFG Phantom Insertion Phase");
182 return runPhase
<PhantomInsertionPhase
>(graph
);
185 } } // namespace JSC::DFG
187 #endif // ENABLE(DFG_JIT)