-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MinimizeExitPoints.java
363 lines (327 loc) · 12 KB
/
MinimizeExitPoints.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright 2009 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.TernaryValue;
import javax.annotation.Nullable;
/**
* Transform the structure of the AST so that the number of explicit exits
* are minimized and instead flows to implicit exits conditions.
*
* @author johnlenz@google.com (John Lenz)
*/
class MinimizeExitPoints extends AbstractPeepholeOptimization {
AbstractCompiler compiler;
MinimizeExitPoints(AbstractCompiler compiler) {
this.compiler = compiler;
}
@VisibleForTesting
final CompilerPass asCompilerPass() {
return new PeepholeOptimizationsPass(compiler, this.getClass().getSimpleName(), this);
}
@Override
Node optimizeSubtree(Node n) {
switch (n.getToken()) {
case LABEL:
tryMinimizeExits(
n.getLastChild(), Token.BREAK, n.getFirstChild().getString());
break;
case FOR:
case FOR_IN:
case WHILE:
tryMinimizeExits(NodeUtil.getLoopCodeBlock(n), Token.CONTINUE, null);
break;
case DO:
tryMinimizeExits(NodeUtil.getLoopCodeBlock(n), Token.CONTINUE, null);
Node cond = NodeUtil.getConditionExpression(n);
if (NodeUtil.getPureBooleanValue(cond) == TernaryValue.FALSE) {
// Normally, we wouldn't be able to optimize BREAKs inside a loop
// but as we know the condition will always be false, we can treat them
// as we would a CONTINUE.
tryMinimizeExits(n.getFirstChild(), Token.BREAK, null);
}
break;
case BLOCK:
if (n.getParent() != null && n.getParent().isFunction()) {
tryMinimizeExits(n, Token.RETURN, null);
}
break;
case SWITCH:
tryMinimizeSwitchExits(n, Token.BREAK, null);
break;
// TODO(johnlenz): Minimize any block that ends in a optimizable statements:
// break, continue, return
default:
break;
}
return n;
}
/**
* Attempts to minimize the number of explicit exit points in a control
* structure to take advantage of the implied exit at the end of the
* structure. This is accomplished by removing redundant statements, and
* moving statements following a qualifying IF node into that node.
* For example:
*
* function () {
* if (x) return;
* else blah();
* foo();
* }
*
* becomes:
*
* function () {
* if (x) ;
* else {
* blah();
* foo();
* }
*
* @param n The execution node of a parent to inspect.
* @param exitType The type of exit to look for.
* @param labelName If parent is a label the name of the label to look for,
* null otherwise. Non-null only for breaks within labels.
*/
void tryMinimizeExits(Node n, Token exitType, @Nullable String labelName) {
// Just an 'exit'.
if (matchingExitNode(n, exitType, labelName)) {
compiler.reportChangeToEnclosingScope(n);
NodeUtil.removeChild(n.getParent(), n);
return;
}
// Just an 'if'.
if (n.isIf()) {
Node ifBlock = n.getSecondChild();
tryMinimizeExits(ifBlock, exitType, labelName);
Node elseBlock = ifBlock.getNext();
if (elseBlock != null) {
tryMinimizeExits(elseBlock, exitType, labelName);
}
return;
}
// Just a 'try/catch/finally'.
if (n.isTry()) {
Node tryBlock = n.getFirstChild();
tryMinimizeExits(tryBlock, exitType, labelName);
Node allCatchNodes = NodeUtil.getCatchBlock(n);
if (NodeUtil.hasCatchHandler(allCatchNodes)) {
checkState(allCatchNodes.hasOneChild());
Node catchNode = allCatchNodes.getFirstChild();
Node catchCodeBlock = catchNode.getLastChild();
tryMinimizeExits(catchCodeBlock, exitType, labelName);
}
/* Don't try to minimize the exits of finally blocks, as this
* can cause problems if it changes the completion type of the finally
* block. See ECMA 262 Sections 8.9 & 12.14
*/
}
// Just a 'label'.
if (n.isLabel()) {
Node labelBlock = n.getLastChild();
tryMinimizeExits(labelBlock, exitType, labelName);
}
// We can only minimize switch cases if we are not trying to remove unlabeled breaks.
if (n.isSwitch() && (exitType != Token.BREAK || labelName != null)) {
tryMinimizeSwitchExits(n, exitType, labelName);
return;
}
// The rest assumes a block with at least one child, bail on anything else.
if (!n.isNormalBlock() || !n.hasChildren()) {
return;
}
// Multiple if-exits can be converted in a single pass.
// Convert "if (blah) break; if (blah2) break; other_stmt;" to
// become "if (blah); else { if (blah2); else { other_stmt; } }"
// which will get converted to "if (!blah && !blah2) { other_stmt; }".
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
// An 'if' block to process below.
if (c.isIf()) {
Node ifTree = c;
Node trueBlock, falseBlock;
// First, the true condition block.
trueBlock = ifTree.getSecondChild();
falseBlock = trueBlock.getNext();
tryMinimizeIfBlockExits(trueBlock, falseBlock,
ifTree, exitType, labelName);
// Now the else block.
// The if blocks may have changed, get them again.
trueBlock = ifTree.getSecondChild();
falseBlock = trueBlock.getNext();
if (falseBlock != null) {
tryMinimizeIfBlockExits(falseBlock, trueBlock,
ifTree, exitType, labelName);
}
}
if (c == n.getLastChild()) {
break;
}
}
// Now try to minimize the exits of the last child, if it is removed
// look at what has become the last child.
for (Node c = n.getLastChild(); c != null; c = n.getLastChild()) {
tryMinimizeExits(c, exitType, labelName);
// If the node is still the last child, we are done.
if (c == n.getLastChild()) {
break;
}
}
}
void tryMinimizeSwitchExits(Node n, Token exitType, @Nullable String labelName) {
checkState(n.isSwitch());
// Skipping the switch condition, visit all the children.
for (Node c = n.getSecondChild(); c != null; c = c.getNext()) {
if (c != n.getLastChild()) {
tryMinimizeSwitchCaseExits(c, exitType, labelName);
} else {
// Last case, the last case block can be optimized more aggressively.
tryMinimizeExits(c.getLastChild(), exitType, labelName);
}
}
}
/**
* Attempt to remove explicit exits from switch cases that also occur implicitly
* after the switch.
*/
void tryMinimizeSwitchCaseExits(Node n, Token exitType, @Nullable String labelName) {
checkState(NodeUtil.isSwitchCase(n));
checkState(n != n.getParent().getLastChild());
Node block = n.getLastChild();
Node maybeBreak = block.getLastChild();
if (maybeBreak == null || !maybeBreak.isBreak() || maybeBreak.hasChildren()) {
// Can not minimize exits from a case without an explicit break from the switch.
return;
}
// Now try to minimize the exits of the last child before the break, if it is removed
// look at what has become the child before the break.
Node childBeforeBreak = maybeBreak.getPrevious();
while (childBeforeBreak != null) {
Node c = childBeforeBreak;
tryMinimizeExits(c, exitType, labelName);
// If the node is still the last child, we are done.
childBeforeBreak = maybeBreak.getPrevious();
if (c == childBeforeBreak) {
break;
}
}
}
/**
* Look for exits (returns, breaks, or continues, depending on the context) at
* the end of a block and removes them by moving the if node's siblings,
* if any, into the opposite condition block.
*
* @param srcBlock The block to inspect.
* @param destBlock The block to move sibling nodes into.
* @param ifNode The if node to work with.
* @param exitType The type of exit to look for.
* @param labelName The name associated with the exit, if any. null for anything excepted for
* named-break associated with a label.
*/
private void tryMinimizeIfBlockExits(Node srcBlock, Node destBlock,
Node ifNode, Token exitType, @Nullable String labelName) {
Node exitNodeParent = null;
Node exitNode = null;
// Pick an exit node candidate.
if (srcBlock.isNormalBlock()) {
if (!srcBlock.hasChildren()) {
return;
}
exitNodeParent = srcBlock;
exitNode = exitNodeParent.getLastChild();
} else {
// Just a single statement, if it isn't an exit bail.
exitNodeParent = ifNode;
exitNode = srcBlock;
}
// Verify the candidate.
if (!matchingExitNode(exitNode, exitType, labelName)) {
return;
}
// Take case of the if nodes siblings, if any.
if (ifNode.getNext() != null) {
// Move siblings of the if block into the opposite
// logic block of the exit.
Node newDestBlock = IR.block().srcref(ifNode);
if (destBlock == null) {
// Only possible if this is the false block.
ifNode.addChildToBack(newDestBlock);
} else if (destBlock.isEmpty()) {
// Use the new block.
ifNode.replaceChild(destBlock, newDestBlock);
} else if (destBlock.isNormalBlock()) {
// Reuse the existing block.
newDestBlock = destBlock;
} else {
// Add the existing statement to the new block.
ifNode.replaceChild(destBlock, newDestBlock);
newDestBlock.addChildToBack(destBlock);
}
// Move all the if node's following siblings.
moveAllFollowing(ifNode, ifNode.getParent(), newDestBlock);
compiler.reportChangeToEnclosingScope(ifNode);
}
}
/**
* Determines if n matches the type and name for the following types of
* "exits":
* - return without values
* - continues and breaks with or without names.
* @param n The node to inspect.
* @param type The Token type to look for.
* @param labelName The name that must be associated with the exit type.
* non-null only for breaks associated with labels.
* @return Whether the node matches the specified block-exit type.
*/
private static boolean matchingExitNode(Node n, Token type, @Nullable String labelName) {
if (n.getToken() == type) {
if (type == Token.RETURN) {
// only returns without expressions.
return !n.hasChildren();
} else {
if (labelName == null) {
return !n.hasChildren();
} else {
return n.hasChildren()
&& labelName.equals(n.getFirstChild().getString());
}
}
}
return false;
}
/**
* Move all the child nodes following start in srcParent to the end of
* destParent's child list.
* @param start The start point in the srcParent child list.
* @param srcParent The parent node of start.
* @param destParent The destination node.
*/
private static void moveAllFollowing(
Node start, Node srcParent, Node destParent) {
for (Node n = start.getNext(); n != null; n = start.getNext()) {
boolean isFunctionDeclaration = NodeUtil.isFunctionDeclaration(n);
srcParent.removeChild(n);
if (isFunctionDeclaration) {
destParent.addChildToFront(n);
} else {
destParent.addChildToBack(n);
}
}
}
}