source: webkit/trunk/Source/JavaScriptCore/dfg/DFGCFGSimplificationPhase.cpp

Last change on this file was 266101, checked in by [email protected], 5 years ago

DFG should always run CFG Simplification after Constant Folding.
https://bugs.webkit.org/show_bug.cgi?id=215286

Reviewed by Robin Morisset.

We didn't do this originally because LICM, many years ago, was
unsound if the CFG didn't have exactly the right shape around
loops. This is no longer true so we don't have to worry about
changing the CFG anymore. While, this doesn't appear to be a
speedup on JetStream 2 CFG, probably because we'd eventually
simplify the graph in B3, CFG Simplification is very cheap and
make other DFG optimizations easier in the future.

Also, remove unecessary validation rule that no exitOKs can come
before any Phi nodes in DFG. This isn't required and fails after
merging two basic blocks where the latter block has a Phi.

  • dfg/DFGCFGSimplificationPhase.cpp:

(JSC::DFG::CFGSimplificationPhase::run):

  • dfg/DFGPlan.cpp:

(JSC::DFG::Plan::compileInThreadImpl):

  • dfg/DFGValidate.cpp:
File size: 17.0 KB
Line 
1/*
2 * Copyright (C) 2012-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 "DFGCFGSimplificationPhase.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGBasicBlockInlines.h"
32#include "DFGGraph.h"
33#include "DFGPhase.h"
34#include "JSCJSValueInlines.h"
35
36namespace JSC { namespace DFG {
37
38class CFGSimplificationPhase : public Phase {
39public:
40 CFGSimplificationPhase(Graph& graph)
41 : Phase(graph, "CFG simplification")
42 {
43 }
44
45 bool canMergeBlocks(BasicBlock* block, BasicBlock* targetBlock)
46 {
47 return targetBlock->predecessors.size() == 1 && targetBlock != block;
48 }
49
50 bool run()
51 {
52 const bool extremeLogging = false;
53
54 bool outerChanged = false;
55 bool innerChanged;
56
57 do {
58 innerChanged = false;
59 for (BlockIndex blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
60 BasicBlock* block = m_graph.block(blockIndex);
61 if (!block)
62 continue;
63 ASSERT(block->isReachable);
64
65 auto canMergeWithBlock = [&] (BasicBlock* targetBlock) {
66 return canMergeBlocks(block, targetBlock);
67 };
68
69 switch (block->terminal()->op()) {
70 case Jump: {
71 // Successor with one predecessor -> merge.
72 if (canMergeWithBlock(block->successor(0))) {
73 ASSERT(block->successor(0)->predecessors[0] == block);
74 if (extremeLogging)
75 m_graph.dump();
76 m_graph.dethread();
77 mergeBlocks(block, block->successor(0), noBlocks());
78 innerChanged = outerChanged = true;
79 break;
80 }
81
82 // FIXME: Block only has a jump -> remove. This is tricky though because of
83 // liveness. What we really want is to slam in a phantom at the end of the
84 // block, after the terminal. But we can't right now. :-(
85 // Idea: what if I slam the ghosties into my successor? Nope, that's
86 // suboptimal, because if my successor has multiple predecessors then we'll
87 // be keeping alive things on other predecessor edges unnecessarily.
88 // What we really need is the notion of end-of-block ghosties!
89 // FIXME: Allow putting phantoms after terminals.
90 // https://bugs.webkit.org/show_bug.cgi?id=126778
91 break;
92 }
93
94 case Branch: {
95 // Branch on constant -> jettison the not-taken block and merge.
96 if (isKnownDirection(block->cfaBranchDirection)) {
97 bool condition = branchCondition(block->cfaBranchDirection);
98 BasicBlock* targetBlock = block->successorForCondition(condition);
99 BasicBlock* jettisonedBlock = block->successorForCondition(!condition);
100 if (canMergeWithBlock(targetBlock)) {
101 if (extremeLogging)
102 m_graph.dump();
103 m_graph.dethread();
104 if (targetBlock == jettisonedBlock)
105 mergeBlocks(block, targetBlock, noBlocks());
106 else
107 mergeBlocks(block, targetBlock, oneBlock(jettisonedBlock));
108 } else {
109 if (extremeLogging)
110 m_graph.dump();
111 m_graph.dethread();
112
113 Node* terminal = block->terminal();
114 ASSERT(terminal->isTerminal());
115 NodeOrigin boundaryNodeOrigin = terminal->origin;
116
117 if (targetBlock != jettisonedBlock)
118 jettisonBlock(block, jettisonedBlock, boundaryNodeOrigin);
119
120 block->replaceTerminal(
121 m_graph, SpecNone, Jump, boundaryNodeOrigin,
122 OpInfo(targetBlock));
123
124 ASSERT(block->terminal());
125
126 }
127 innerChanged = outerChanged = true;
128 break;
129 }
130
131 if (block->successor(0) == block->successor(1)) {
132 convertToJump(block, block->successor(0));
133 innerChanged = outerChanged = true;
134 break;
135 }
136
137 // Branch to same destination -> jump.
138 // FIXME: this will currently not be hit because of the lack of jump-only
139 // block simplification.
140
141 break;
142 }
143
144 case Switch: {
145 SwitchData* data = block->terminal()->switchData();
146
147 // Prune out cases that end up jumping to default.
148 for (unsigned i = 0; i < data->cases.size(); ++i) {
149 if (data->cases[i].target.block == data->fallThrough.block) {
150 data->fallThrough.count += data->cases[i].target.count;
151 data->cases[i--] = data->cases.last();
152 data->cases.removeLast();
153 }
154 }
155
156 // If there are no cases other than default then this turns
157 // into a jump.
158 if (data->cases.isEmpty()) {
159 convertToJump(block, data->fallThrough.block);
160 innerChanged = outerChanged = true;
161 break;
162 }
163
164 // Switch on constant -> jettison all other targets and merge.
165 Node* terminal = block->terminal();
166 if (terminal->child1()->hasConstant()) {
167 FrozenValue* value = terminal->child1()->constant();
168 TriState found = TriState::False;
169 BasicBlock* targetBlock = nullptr;
170 for (unsigned i = data->cases.size(); found == TriState::False && i--;) {
171 found = data->cases[i].value.strictEqual(value);
172 if (found == TriState::True)
173 targetBlock = data->cases[i].target.block;
174 }
175
176 if (found == TriState::Indeterminate)
177 break;
178 if (found == TriState::False)
179 targetBlock = data->fallThrough.block;
180 ASSERT(targetBlock);
181
182 Vector<BasicBlock*, 1> jettisonedBlocks;
183 for (BasicBlock* successor : terminal->successors()) {
184 if (successor != targetBlock && !jettisonedBlocks.contains(successor))
185 jettisonedBlocks.append(successor);
186 }
187
188 if (canMergeWithBlock(targetBlock)) {
189 if (extremeLogging)
190 m_graph.dump();
191 m_graph.dethread();
192
193 mergeBlocks(block, targetBlock, jettisonedBlocks);
194 } else {
195 if (extremeLogging)
196 m_graph.dump();
197 m_graph.dethread();
198
199 NodeOrigin boundaryNodeOrigin = terminal->origin;
200
201 for (unsigned i = jettisonedBlocks.size(); i--;)
202 jettisonBlock(block, jettisonedBlocks[i], boundaryNodeOrigin);
203
204 block->replaceTerminal(
205 m_graph, SpecNone, Jump, boundaryNodeOrigin, OpInfo(targetBlock));
206 }
207 innerChanged = outerChanged = true;
208 break;
209 }
210 break;
211 }
212
213 default:
214 break;
215 }
216 }
217
218 if (innerChanged) {
219 // Here's the reason for this pass:
220 // Blocks: A, B, C, D, E, F
221 // A -> B, C
222 // B -> F
223 // C -> D, E
224 // D -> F
225 // E -> F
226 //
227 // Assume that A's branch is determined to go to B. Then the rest of this phase
228 // is smart enough to simplify down to:
229 // A -> B
230 // B -> F
231 // C -> D, E
232 // D -> F
233 // E -> F
234 //
235 // We will also merge A and B. But then we don't have any other mechanism to
236 // remove D, E as predecessors for F. Worse, the rest of this phase does not
237 // know how to fix the Phi functions of F to ensure that they no longer refer
238 // to variables in D, E. In general, we need a way to handle Phi simplification
239 // upon:
240 // 1) Removal of a predecessor due to branch simplification. The branch
241 // simplifier already does that.
242 // 2) Invalidation of a predecessor because said predecessor was rendered
243 // unreachable. We do this here.
244 //
245 // This implies that when a block is unreachable, we must inspect its
246 // successors' Phi functions to remove any references from them into the
247 // removed block.
248
249 m_graph.invalidateCFG();
250 m_graph.resetReachability();
251 m_graph.killUnreachableBlocks();
252 }
253
254 if (Options::validateGraphAtEachPhase())
255 validate();
256 } while (innerChanged);
257
258 return outerChanged;
259 }
260
261private:
262 void convertToJump(BasicBlock* block, BasicBlock* targetBlock)
263 {
264 ASSERT(targetBlock);
265 ASSERT(targetBlock->isReachable);
266 if (canMergeBlocks(block, targetBlock)) {
267 m_graph.dethread();
268 mergeBlocks(block, targetBlock, noBlocks());
269 } else {
270 Node* branch = block->terminal();
271 ASSERT(branch->op() == Branch || branch->op() == Switch);
272
273 block->replaceTerminal(
274 m_graph, SpecNone, Jump, branch->origin, OpInfo(targetBlock));
275 }
276 }
277
278 void keepOperandAlive(BasicBlock* block, BasicBlock* jettisonedBlock, NodeOrigin nodeOrigin, Operand operand)
279 {
280 Node* livenessNode = jettisonedBlock->variablesAtHead.operand(operand);
281 if (!livenessNode)
282 return;
283 NodeType nodeType;
284 if (livenessNode->flags() & NodeIsFlushed)
285 nodeType = Flush;
286 else {
287 // This seems like it shouldn't be necessary because we could just rematerialize
288 // PhantomLocals or something similar using bytecode liveness. However, in ThreadedCPS, it's
289 // worth the sanity to maintain this eagerly. See
290 // https://bugs.webkit.org/show_bug.cgi?id=144086
291 nodeType = PhantomLocal;
292 }
293 block->appendNode(
294 m_graph, SpecNone, nodeType, nodeOrigin,
295 OpInfo(livenessNode->variableAccessData()));
296 }
297
298 void jettisonBlock(BasicBlock* block, BasicBlock* jettisonedBlock, NodeOrigin boundaryNodeOrigin)
299 {
300 for (size_t i = 0; i < jettisonedBlock->variablesAtHead.size(); ++i)
301 keepOperandAlive(block, jettisonedBlock, boundaryNodeOrigin, jettisonedBlock->variablesAtHead.operandForIndex(i));
302
303 fixJettisonedPredecessors(block, jettisonedBlock);
304 }
305
306 void fixJettisonedPredecessors(BasicBlock* block, BasicBlock* jettisonedBlock)
307 {
308 jettisonedBlock->removePredecessor(block);
309 }
310
311 Vector<BasicBlock*, 1> noBlocks()
312 {
313 return Vector<BasicBlock*, 1>();
314 }
315
316 Vector<BasicBlock*, 1> oneBlock(BasicBlock* block)
317 {
318 Vector<BasicBlock*, 1> result;
319 result.append(block);
320 return result;
321 }
322
323 void mergeBlocks(
324 BasicBlock* firstBlock, BasicBlock* secondBlock,
325 Vector<BasicBlock*, 1> jettisonedBlocks)
326 {
327 RELEASE_ASSERT(canMergeBlocks(firstBlock, secondBlock));
328 // This will add all of the nodes in secondBlock to firstBlock, but in so doing
329 // it will also ensure that any GetLocals from the second block that refer to
330 // SetLocals in the first block are relinked. If jettisonedBlock is not NoBlock,
331 // then Phantoms are inserted for anything that the jettisonedBlock would have
332 // kept alive.
333
334 // Remove the terminal of firstBlock since we don't need it anymore. Well, we don't
335 // really remove it; we actually turn it into a check.
336 Node* terminal = firstBlock->terminal();
337 ASSERT(terminal->isTerminal());
338 NodeOrigin boundaryNodeOrigin = terminal->origin;
339 terminal->remove(m_graph);
340 ASSERT(terminal->refCount() == 1);
341
342 for (unsigned i = jettisonedBlocks.size(); i--;) {
343 BasicBlock* jettisonedBlock = jettisonedBlocks[i];
344
345 // Time to insert ghosties for things that need to be kept alive in case we OSR
346 // exit prior to hitting the firstBlock's terminal, and end up going down a
347 // different path than secondBlock.
348 for (size_t i = 0; i < jettisonedBlock->variablesAtHead.size(); ++i)
349 keepOperandAlive(firstBlock, jettisonedBlock, boundaryNodeOrigin, jettisonedBlock->variablesAtHead.operandForIndex(i));
350 }
351
352 for (size_t i = 0; i < secondBlock->phis.size(); ++i)
353 firstBlock->phis.append(secondBlock->phis[i]);
354
355 for (size_t i = 0; i < secondBlock->size(); ++i)
356 firstBlock->append(secondBlock->at(i));
357
358 ASSERT(firstBlock->terminal()->isTerminal());
359
360 // Fix the predecessors of my new successors. This is tricky, since we are going to reset
361 // all predecessors anyway due to reachability analysis. But we need to fix the
362 // predecessors eagerly to ensure that we know what they are in case the next block we
363 // consider in this phase wishes to query the predecessors of one of the blocks we
364 // affected.
365 for (unsigned i = firstBlock->numSuccessors(); i--;) {
366 BasicBlock* successor = firstBlock->successor(i);
367 for (unsigned j = 0; j < successor->predecessors.size(); ++j) {
368 if (successor->predecessors[j] == secondBlock)
369 successor->predecessors[j] = firstBlock;
370 }
371 }
372
373 // Fix the predecessors of my former successors. Again, we'd rather not do this, but it's
374 // an unfortunate necessity. See above comment.
375 for (unsigned i = jettisonedBlocks.size(); i--;)
376 fixJettisonedPredecessors(firstBlock, jettisonedBlocks[i]);
377
378 firstBlock->valuesAtTail = secondBlock->valuesAtTail;
379 firstBlock->cfaBranchDirection = secondBlock->cfaBranchDirection;
380
381 m_graph.killBlock(secondBlock);
382 }
383};
384
385bool performCFGSimplification(Graph& graph)
386{
387 return runPhase<CFGSimplificationPhase>(graph);
388}
389
390} } // namespace JSC::DFG
391
392#endif // ENABLE(DFG_JIT)
393
394
Note: See TracBrowser for help on using the repository browser.