]> git.saurik.com Git - apple/javascriptcore.git/blame - dfg/DFGCFAPhase.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGCFAPhase.cpp
CommitLineData
6fe7ccc8 1/*
ed1e77d3 2 * Copyright (C) 2011, 2013-2015 Apple Inc. All rights reserved.
6fe7ccc8
A
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 "DFGCFAPhase.h"
28
29#if ENABLE(DFG_JIT)
30
81345200 31#include "DFGAbstractInterpreterInlines.h"
6fe7ccc8 32#include "DFGGraph.h"
81345200 33#include "DFGInPlaceAbstractState.h"
6fe7ccc8 34#include "DFGPhase.h"
81345200
A
35#include "DFGSafeToExecute.h"
36#include "OperandsInlines.h"
37#include "JSCInlines.h"
6fe7ccc8
A
38
39namespace JSC { namespace DFG {
40
41class CFAPhase : public Phase {
42public:
43 CFAPhase(Graph& graph)
44 : Phase(graph, "control flow analysis")
45 , m_state(graph)
81345200
A
46 , m_interpreter(graph, m_state)
47 , m_verbose(Options::verboseCFA())
6fe7ccc8
A
48 {
49 }
50
93a37866 51 bool run()
6fe7ccc8 52 {
81345200 53 ASSERT(m_graph.m_form == ThreadedCPS || m_graph.m_form == SSA);
93a37866
A
54 ASSERT(m_graph.m_unificationState == GloballyUnified);
55 ASSERT(m_graph.m_refCountState == EverythingIsLive);
56
6fe7ccc8 57 m_count = 0;
81345200
A
58
59 if (m_verbose && !shouldDumpGraphAtEachPhase()) {
60 dataLog("Graph before CFA:\n");
61 m_graph.dump();
62 }
6fe7ccc8
A
63
64 // This implements a pseudo-worklist-based forward CFA, except that the visit order
65 // of blocks is the bytecode program order (which is nearly topological), and
66 // instead of a worklist we just walk all basic blocks checking if cfaShouldRevisit
67 // is set to true. This is likely to balance the efficiency properties of both
68 // worklist-based and forward fixpoint-based approaches. Like a worklist-based
69 // approach, it won't visit code if it's meaningless to do so (nothing changed at
70 // the head of the block or the predecessors have not been visited). Like a forward
71 // fixpoint-based approach, it has a high probability of only visiting a block
72 // after all predecessors have been visited. Only loops will cause this analysis to
73 // revisit blocks, and the amount of revisiting is proportional to loop depth.
74
81345200 75 m_state.initialize();
6fe7ccc8
A
76
77 do {
78 m_changed = false;
79 performForwardCFA();
80 } while (m_changed);
93a37866 81
ed1e77d3
A
82 if (m_graph.m_form != SSA) {
83 ASSERT(!m_changed);
84
85 // Widen the abstract values at the block that serves as the must-handle OSR entry.
86 for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
87 BasicBlock* block = m_graph.block(blockIndex);
88 if (!block)
89 continue;
90
91 if (!block->isOSRTarget)
92 continue;
93 if (block->bytecodeBegin != m_graph.m_plan.osrEntryBytecodeIndex)
94 continue;
95
96 bool changed = false;
97 for (size_t i = m_graph.m_plan.mustHandleValues.size(); i--;) {
98 int operand = m_graph.m_plan.mustHandleValues.operandForIndex(i);
99 JSValue value = m_graph.m_plan.mustHandleValues[i];
100 Node* node = block->variablesAtHead.operand(operand);
101 if (!node)
102 continue;
103
104 AbstractValue& target = block->valuesAtHead.operand(operand);
105 changed |= target.mergeOSREntryValue(m_graph, value);
106 target.fixTypeForRepresentation(
107 m_graph, resultFor(node->variableAccessData()->flushFormat()));
108 }
109
110 if (changed || !block->cfaHasVisited) {
111 m_changed = true;
112 block->cfaShouldRevisit = true;
113 }
114 }
115
116 // Propagate any of the changes we just introduced.
117 while (m_changed) {
118 m_changed = false;
119 performForwardCFA();
120 }
121
122 // Make sure we record the intersection of all proofs that we ever allowed the
123 // compiler to rely upon.
124 for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
125 BasicBlock* block = m_graph.block(blockIndex);
126 if (!block)
127 continue;
128
129 block->intersectionOfCFAHasVisited &= block->cfaHasVisited;
130 for (unsigned i = block->intersectionOfPastValuesAtHead.size(); i--;)
131 block->intersectionOfPastValuesAtHead[i].filter(block->valuesAtHead[i]);
132 }
133 }
134
93a37866 135 return true;
6fe7ccc8
A
136 }
137
138private:
81345200 139 void performBlockCFA(BasicBlock* block)
6fe7ccc8 140 {
93a37866
A
141 if (!block)
142 return;
6fe7ccc8
A
143 if (!block->cfaShouldRevisit)
144 return;
81345200
A
145 if (m_verbose)
146 dataLog(" Block ", *block, ":\n");
6fe7ccc8 147 m_state.beginBasicBlock(block);
ed1e77d3 148 if (m_verbose) {
81345200 149 dataLog(" head vars: ", block->valuesAtHead, "\n");
ed1e77d3
A
150 if (m_graph.m_form == SSA)
151 dataLog(" head regs: ", mapDump(block->ssa->valuesAtHead), "\n");
152 }
6fe7ccc8 153 for (unsigned i = 0; i < block->size(); ++i) {
81345200
A
154 if (m_verbose) {
155 Node* node = block->at(i);
156 dataLogF(" %s @%u: ", Graph::opName(node->op()), node->index());
157
158 if (!safeToExecute(m_state, m_graph, node))
159 dataLog("(UNSAFE) ");
160
ed1e77d3 161 dataLog(m_state.variables(), " ", m_interpreter);
81345200 162
81345200
A
163 dataLogF("\n");
164 }
165 if (!m_interpreter.execute(i)) {
166 if (m_verbose)
167 dataLogF(" Expect OSR exit.\n");
6fe7ccc8 168 break;
93a37866 169 }
6fe7ccc8 170 }
81345200
A
171 if (m_verbose) {
172 dataLogF(" tail regs: ");
173 m_interpreter.dump(WTF::dataFile());
174 dataLogF("\n");
175 }
176 m_changed |= m_state.endBasicBlock(MergeToSuccessors);
177
ed1e77d3 178 if (m_verbose) {
81345200 179 dataLog(" tail vars: ", block->valuesAtTail, "\n");
ed1e77d3
A
180 if (m_graph.m_form == SSA)
181 dataLog(" head regs: ", mapDump(block->ssa->valuesAtTail), "\n");
182 }
6fe7ccc8
A
183 }
184
185 void performForwardCFA()
186 {
81345200
A
187 ++m_count;
188 if (m_verbose)
189 dataLogF("CFA [%u]\n", ++m_count);
6fe7ccc8 190
81345200
A
191 for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex)
192 performBlockCFA(m_graph.block(blockIndex));
6fe7ccc8
A
193 }
194
195private:
81345200
A
196 InPlaceAbstractState m_state;
197 AbstractInterpreter<InPlaceAbstractState> m_interpreter;
198
199 bool m_verbose;
6fe7ccc8
A
200
201 bool m_changed;
6fe7ccc8 202 unsigned m_count;
6fe7ccc8
A
203};
204
93a37866 205bool performCFA(Graph& graph)
6fe7ccc8 206{
93a37866
A
207 SamplingRegion samplingRegion("DFG CFA Phase");
208 return runPhase<CFAPhase>(graph);
6fe7ccc8
A
209}
210
211} } // namespace JSC::DFG
212
213#endif // ENABLE(DFG_JIT)