Debian Codegraph @

src/com/google/javascript/jscomp/OptimizeReturns.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Lists;
22 import com.google.javascript.jscomp.DefinitionsRemover.Definition;
23 import com.google.javascript.rhino.IR;
24 import com.google.javascript.rhino.Node;
25 import java.util.Collection;
26 import java.util.List;
27
28 /**
29 * A compiler pass for optimize function return results. Currently this
30 * pass looks for results that are complete unused and rewrite then to be:
31 * "return x()" -->"x(); return"
32 * , but it can easily be
33 * expanded to look for use context to avoid unneeded type coercion:
34 * - "return x.toString()" --> "return x"
35 * - "return !!x" --> "return x"
36 * @author [email protected] (John Lenz)
37 */
38 class OptimizeReturns
39 implements OptimizeCalls.CallGraphCompilerPass, CompilerPass {
40
41 private AbstractCompiler compiler;
42
43 OptimizeReturns(AbstractCompiler compiler) {
44 this.compiler = compiler;
45 }
46
47 @Override
48 @VisibleForTesting
49 public void process(Node externs, Node root) {
50 SimpleDefinitionFinder defFinder = new SimpleDefinitionFinder(compiler);
51 defFinder.process(externs, root);
52 process(externs, root, defFinder);
53 }
54
55 @Override
56 public void process(
57 Node externs, Node root, SimpleDefinitionFinder definitions) {
58 // Find all function nodes whose callers ignore the return values.
59 List<Node> toOptimize = Lists.newArrayList();
60 for (DefinitionSite defSite : definitions.getDefinitionSites()) {
61 if (!defSite.inExterns && !callResultsMaybeUsed(definitions, defSite)) {
62 toOptimize.add(defSite.definition.getRValue());
63 }
64 }
65 // Optimize the return statements.
66 for (Node node : toOptimize) {
67 rewriteReturns(definitions, node);
68 }
69 }
70
71 /**
72 * Determines if a function result might be used. A result might be use if:
73 * - Function must is exported.
74 * - The definition is never accessed outside a function call context.
75 */
76 private boolean callResultsMaybeUsed(
77 SimpleDefinitionFinder defFinder, DefinitionSite definitionSite) {
78
79 Definition definition = definitionSite.definition;
80
81 // Assume non-function definitions results are used.
82 Node rValue = definition.getRValue();
83 if (rValue == null || !rValue.isFunction()) {
84 return true;
85 }
86
87 // Be conservative, don't try to optimize any declaration that isn't as
88 // simple function declaration or assignment.
89 if (!SimpleDefinitionFinder.isSimpleFunctionDeclaration(rValue)) {
90 return true;
91 }
92
93 if (!defFinder.canModifyDefinition(definition)) {
94 return true;
95 }
96
97 Collection<UseSite> useSites = defFinder.getUseSites(definition);
98 for (UseSite site : useSites) {
99 // Assume indirect definitions references use the result
100 Node useNodeParent = site.node.getParent();
101 if (isCall(site)) {
102 Node callNode = useNodeParent;
103 Preconditions.checkState(callNode.isCall());
104 if (NodeUtil.isExpressionResultUsed(callNode)) {
105 return true;
106 }
107 } else {
108 // Allow a standalone name reference.
109 // var a;
110 if (!useNodeParent.isVar()) {
111 return true;
112 }
113 }
114
115 // TODO(johnlenz): Add specialization support.
116 }
117
118 // No possible use of the definition result
119 return false;
120 }
121
122 /**
123 * For the supplied function node, rewrite all the return expressions so that:
124 * return foo();
125 * becomes:
126 * foo(); return;
127 * Useless return will be removed later by the peephole optimization passes.
128 */
129 private void rewriteReturns(
130 final SimpleDefinitionFinder defFinder, Node fnNode) {
131 Preconditions.checkState(fnNode.isFunction());
132 NodeUtil.visitPostOrder(
133 fnNode.getLastChild(),
134 new NodeUtil.Visitor() {
135 @Override
136 public void visit(Node node) {
137 if (node.isReturn() && node.hasOneChild()) {
138 boolean keepValue = NodeUtil.mayHaveSideEffects(
139 node.getFirstChild(), compiler);
140 if (!keepValue) {
141 defFinder.removeReferences(node.getFirstChild());
142 }
143 Node result = node.removeFirstChild();
144 if (keepValue) {
145 node.getParent().addChildBefore(
146 IR.exprResult(result).srcref(result), node);
147 }
148 compiler.reportCodeChange();
149 }
150 }
151 },
152 new NodeUtil.MatchShallowStatement());
153 }
154
155 /**
156 * Determines if the name node acts as the function name in a call expression.
157 */
158 private static boolean isCall(UseSite site) {
159 Node node = site.node;
160 Node parent = node.getParent();
161 return (parent.getFirstChild() == node) && parent.isCall();
162 }
163 }