-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
OptimizeReturns.java
236 lines (214 loc) · 7.48 KB
/
OptimizeReturns.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
/*
* 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.jscomp.OptimizeCalls.ReferenceMap;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
/**
* A compiler pass for optimize function return results. Currently this
* pass looks for results that are complete unused and rewrite them to be:
* "return x()" -->"x(); return"
*
* Future work: expanded this to look for use context to avoid unneeded type coercion:
* - "return x.toString()" --> "return x"
* - "return !!x" --> "return x"
* @author johnlenz@google.com (John Lenz)
*/
class OptimizeReturns implements OptimizeCalls.CallGraphCompilerPass, CompilerPass {
private final AbstractCompiler compiler;
OptimizeReturns(AbstractCompiler compiler) {
this.compiler = compiler;
}
@Override
@VisibleForTesting
public void process(Node externs, Node root) {
OptimizeCalls.builder()
.setCompiler(compiler)
.setConsiderExterns(false)
.addPass(this)
.build()
.process(externs, root);
}
@Override
public void process(Node externs, Node root, ReferenceMap definitions) {
// Find all function nodes whose callers ignore the return values.
List<ArrayList<Node>> toOptimize = new ArrayList<>();
// Find all the candidates before modifying the AST.
for (Entry<String, ArrayList<Node>> entry : definitions.getNameReferences()) {
String key = entry.getKey();
ArrayList<Node> refs = entry.getValue();
if (isCandidate(key, refs)) {
toOptimize.add(refs);
}
}
for (Entry<String, ArrayList<Node>> entry : definitions.getPropReferences()) {
String key = entry.getKey();
ArrayList<Node> refs = entry.getValue();
if (isCandidate(key, refs)) {
toOptimize.add(refs);
}
}
// Now modify the AST
for (ArrayList<Node> refs : toOptimize) {
for (Node fn : ReferenceMap.getFunctionNodes(refs).values()) {
rewriteReturns(fn);
}
}
}
/**
* This reference set is a candidate for return-value-removal if:
* - if the all call sites are known (not aliased, not exported)
* - if all call sites do not use the return value
* - if there is at least one known function definition
* - if there is at least one use
* NOTE: unknown definitions are allowed, as only known
* definitions will be removed.
*/
private boolean isCandidate(String name, List<Node> refs) {
if (!OptimizeCalls.mayBeOptimizableName(compiler, name)) {
return false;
}
boolean seenCandidateDefiniton = false;
boolean seenUse = false;
for (Node n : refs) {
// Assume indirect definitions references use the result
if (ReferenceMap.isCallTarget(n)) {
Node callNode = ReferenceMap.getCallOrNewNodeForTarget(n);
if (NodeUtil.isExpressionResultUsed(callNode)) {
// At least one call site uses the return value, this
// is not a candidate.
return false;
}
seenUse = true;
} else if (isCandidateDefinition(n)) {
// NOTE: While is is possible to optimize calls to functions for which we know
// only some of the definition are candidates but to keep things simple, only
// optimize if all of the definitions are known.
seenCandidateDefiniton = true;
} else {
// If this isn't an non-aliasing reference (typeof, instanceof, etc)
// then there is nothing that can be done.
if (!OptimizeCalls.isAllowedReference(n)) {
return false;
}
}
}
return seenUse && seenCandidateDefiniton;
}
private boolean isCandidateDefinition(Node n) {
Node parent = n.getParent();
if (parent.isFunction() && NodeUtil.isFunctionDeclaration(parent)) {
return true;
} else if (ReferenceMap.isSimpleAssignmentTarget(n)) {
if (isCandidateFunction(parent.getLastChild())) {
return true;
}
} else if (n.isName()) {
if (n.hasChildren() && isCandidateFunction(n.getFirstChild())) {
return true;
}
} else if (isClassMemberDefinition(n)) {
return true;
}
return false;
}
private boolean isClassMemberDefinition(Node n) {
return n.isMemberFunctionDef() && n.getParent().isClassMembers();
}
private static boolean isCandidateFunction(Node n) {
switch (n.getToken()) {
case FUNCTION:
// Named function expression can be recursive, this creates an alias of the name, meaning
// it might be used in an unexpected way.
return !NodeUtil.isNamedFunctionExpression(n);
case COMMA:
case CAST:
return isCandidateFunction(n.getLastChild());
case HOOK:
return isCandidateFunction(n.getSecondChild()) && isCandidateFunction(n.getLastChild());
case OR:
case AND:
return isCandidateFunction(n.getFirstChild()) && isCandidateFunction(n.getLastChild());
default:
return false;
}
}
/**
* For the supplied function node, rewrite all the return expressions so that:
* return foo();
* becomes:
* foo(); return;
* Useless return will be removed later by the peephole optimization passes.
*/
private void rewriteReturns(Node fnNode) {
checkState(fnNode.isFunction());
final Node body = fnNode.getLastChild();
NodeUtil.visitPostOrder(
body,
new NodeUtil.Visitor() {
@Override
public void visit(Node n) {
if (n.isReturn() && n.hasOneChild()) {
Node result = n.getFirstChild();
boolean keepValue = !isRemovableValue(result);
result.detach();
if (keepValue) {
n.getParent().addChildBefore(IR.exprResult(result).srcref(result), n);
} else {
NodeUtil.markFunctionsDeleted(result, compiler);
}
compiler.reportChangeToEnclosingScope(body);
}
}
},
new NodeUtil.MatchShallowStatement());
}
// Just remove objects that don't reference properties (object literals) or names (functions)
// So we don't need to update the graph.
private boolean isRemovableValue(Node n) {
switch (n.getToken()) {
case TEMPLATELIT:
case ARRAYLIT:
for (Node child = n.getFirstChild(); child != null; child = child.getNext()) {
if ((!child.isEmpty()) && !isRemovableValue(child)) {
return false;
}
}
return true;
case REGEXP:
case STRING:
case NUMBER:
case NULL:
case TRUE:
case FALSE:
case TEMPLATELIT_STRING:
return true;
case TEMPLATELIT_SUB:
case CAST:
case NOT:
case VOID:
case NEG:
return isRemovableValue(n.getFirstChild());
default:
return false;
}
}
}