VarargsForwardingPhase should use bytecode liveness in addition to other uses to...
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGPhantomInsertionPhase.cpp
1 /*
2  * Copyright (C) 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "DFGPhantomInsertionPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "BytecodeLivenessAnalysisInlines.h"
32 #include "DFGForAllKills.h"
33 #include "DFGGraph.h"
34 #include "DFGInsertionSet.h"
35 #include "DFGMayExit.h"
36 #include "DFGPhase.h"
37 #include "DFGPredictionPropagationPhase.h"
38 #include "DFGVariableAccessDataDump.h"
39 #include "JSCInlines.h"
40 #include "OperandsInlines.h"
41
42 namespace JSC { namespace DFG {
43
44 namespace {
45
46 bool verbose = false;
47
48 class PhantomInsertionPhase : public Phase {
49 public:
50     PhantomInsertionPhase(Graph& graph)
51         : Phase(graph, "phantom insertion")
52         , m_insertionSet(graph)
53         , m_values(OperandsLike, graph.block(0)->variablesAtHead)
54     {
55     }
56     
57     bool run()
58     {
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);
62         
63         if (verbose) {
64             dataLog("Graph before Phantom insertion:\n");
65             m_graph.dump();
66         }
67         
68         m_graph.clearEpochs();
69         
70         for (BasicBlock* block : m_graph.blocksInNaturalOrder())
71             handleBlock(block);
72         
73         if (verbose) {
74             dataLog("Graph after Phantom insertion:\n");
75             m_graph.dump();
76         }
77         
78         return true;
79     }
80
81 private:
82     void handleBlock(BasicBlock* block)
83     {
84         m_values.fill(nullptr);
85
86         Epoch currentEpoch = Epoch::first();
87         unsigned lastExitingIndex = 0;
88         for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
89             Node* node = block->at(nodeIndex);
90             if (verbose)
91                 dataLog("Considering ", node, "\n");
92             
93             switch (node->op()) {
94             case MovHint:
95                 m_values.operand(node->unlinkedLocal()) = node->child1().node();
96                 break;
97                 
98             case ZombieHint:
99                 m_values.operand(node->unlinkedLocal()) = nullptr;
100                 break;
101
102             case SetLocal:
103             case GetLocal:
104             case SetArgument:
105                 m_values.operand(node->local()) = nullptr;
106                 break;
107                 
108             default:
109                 break;
110             }
111             
112             if (mayExit(m_graph, node)) {
113                 currentEpoch.bump();
114                 lastExitingIndex = nodeIndex;
115             }
116             
117             m_graph.doToChildren(
118                 node,
119                 [&] (Edge edge) {
120                     edge->setEpoch(currentEpoch);
121                 });
122             
123             node->setEpoch(currentEpoch);
124
125             forAllKilledOperands(
126                 m_graph, node, block->tryAt(nodeIndex + 1),
127                 [&] (VirtualRegister reg) {
128                     if (verbose)
129                         dataLog("    Killed operand: ", reg, "\n");
130                     
131                     Node* killedNode = m_values.operand(reg);
132                     if (!killedNode)
133                         return;
134                     
135                     // We only need to insert a Phantom if the node hasn't been used since the last
136                     // exit, and was born before the last exit.
137                     if (killedNode->epoch() == currentEpoch)
138                         return;
139                     
140                     if (verbose) {
141                         dataLog(
142                             "    Inserting Phantom on ", killedNode, " after ",
143                             block->at(lastExitingIndex), "\n");
144                     }
145                     
146                     // We have exact ref counts, so creating a new use means that we have to
147                     // increment the ref count.
148                     killedNode->postfixRef();
149                     
150                     m_insertionSet.insertNode(
151                         lastExitingIndex + 1, SpecNone, Phantom,
152                         block->at(lastExitingIndex)->origin, killedNode->defaultEdge());
153             });
154         }
155         
156         m_insertionSet.execute(block);
157     }
158     
159     InsertionSet m_insertionSet;
160     Operands<Node*> m_values;
161 };
162
163 } // anonymous namespace
164     
165 bool performPhantomInsertion(Graph& graph)
166 {
167     SamplingRegion samplingRegion("DFG Phantom Insertion Phase");
168     return runPhase<PhantomInsertionPhase>(graph);
169 }
170
171 } } // namespace JSC::DFG
172
173 #endif // ENABLE(DFG_JIT)
174