Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.base.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.rhino.Node;
22
23 import java.util.ArrayList;
24
25 /**
26 * A compiler pass to run various peephole optimizations (e.g. constant folding,
27 * some useless code removal, some minimizations).
28 *
29 * @author [email protected] (Devin Coughlin)
30 * @author [email protected] (Alan Leung)(
31 */
32 class PeepholeOptimizationsPass
33 implements CompilerPass {
34 private AbstractCompiler compiler;
35
36 // Use an array here for faster iteration compared to ImmutableSet
37 private final AbstractPeepholeOptimization[] peepholeOptimizations;
38
39 // Track whether the a scope has been modified so that it can be revisited
40 // immediately.
41 private StateStack traversalState = new StateStack();
42
43 private boolean retraverseOnChange = true;
44
45 private static class ScopeState {
46 boolean changed;
47 boolean traverseChildScopes;
48 ScopeState() {
49 reset();
50 }
51
52 void reset() {
53 changed = false;
54 traverseChildScopes = true;
55 }
56 }
57
58 private static class StateStack {
59 private ArrayList<ScopeState> states = Lists.newArrayList();
60 private int currentDepth = 0;
61
62 StateStack() {
63 states.add(new ScopeState());
64 }
65
66 ScopeState peek() {
67 return states.get(currentDepth);
68 }
69
70 void push() {
71 currentDepth++;
72 if (states.size() <= currentDepth) {
73 states.add(new ScopeState());
74 } else {
75 states.get(currentDepth).reset();
76 }
77 }
78
79 void pop() {
80 currentDepth--;
81 }
82 }
83
84 private class PeepholeChangeHandler extends CodeChangeHandler {
85 @Override
86 public void reportChange() {
87 traversalState.peek().changed = true;
88 }
89 }
90
91 /**
92 * Creates a peephole optimization pass that runs the given
93 * optimizations.
94 */
95 PeepholeOptimizationsPass(AbstractCompiler compiler,
96 AbstractPeepholeOptimization... optimizations) {
97 this.compiler = compiler;
98 this.peepholeOptimizations = optimizations;
99 }
100
101 PeepholeOptimizationsPass setRetraverseOnChange(boolean retraverse) {
102 this.retraverseOnChange = retraverse;
103 return this;
104 }
105
106 public AbstractCompiler getCompiler() {
107 return compiler;
108 }
109
110 @Override
111 public void process(Node externs, Node root) {
112 PeepholeChangeHandler handler = new PeepholeChangeHandler();
113 compiler.addChangeHandler(handler);
114 beginTraversal();
115 traverse(root);
116 endTraversal();
117 compiler.removeChangeHandler(handler);
118 }
119
120 private void traverse(Node node) {
121 // The goal here is to avoid retraversing
122 // the entire AST to catch newly created opportunities.
123 // So we track whether a "unit of code" has changed,
124 // and revisit immediately.
125 if (!shouldVisit(node)) {
126 return;
127 }
128
129 int visits = 0;
130 do {
131 Node c = node.getFirstChild();
132 while (c != null) {
133 Node next = c.getNext();
134 traverse(c);
135 c = next;
136 }
137
138 visit(node);
139 visits++;
140
141 Preconditions.checkState(visits < 10000, "too many interations");
142 } while (shouldRetraverse(node));
143
144 exitNode(node);
145 }
146
147 private boolean shouldRetraverse(Node node) {
148 if (retraverseOnChange
149 && node.getParent() != null
150 && (node.isFunction() || node.isScript())) {
151 ScopeState state = traversalState.peek();
152 if (state.changed) {
153 // prepare to re-visit the scope:
154 // when revisiting, only visit the immediate scope
155 // this reduces the cost of getting to a fixed
156 // point in global scope.
157 state.changed = false;
158 state.traverseChildScopes = false;
159 return true;
160 }
161 }
162 return false;
163 }
164
165 private boolean shouldVisit(Node node) {
166 if (node.isFunction() || node.isScript()) {
167 ScopeState previous = traversalState.peek();
168 if (!previous.traverseChildScopes) {
169 return false;
170 }
171 traversalState.push();
172 }
173 return true;
174 }
175
176 private void exitNode(Node node) {
177 if (node.isFunction() || node.isScript()) {
178 traversalState.pop();
179 }
180 }
181
182 public void visit(Node n) {
183 Node currentVersionOfNode = n;
184 boolean somethingChanged = false;
185
186 do {
187 somethingChanged = false;
188 for (AbstractPeepholeOptimization optimization : peepholeOptimizations) {
189 Node newVersionOfNode =
190 optimization.optimizeSubtree(currentVersionOfNode);
191
192 if (newVersionOfNode != currentVersionOfNode) {
193 somethingChanged = true;
194
195 currentVersionOfNode = newVersionOfNode;
196 }
197
198 if (currentVersionOfNode == null) {
199 return;
200 }
201 }
202 } while(somethingChanged);
203 }
204
205 /**
206 * Make sure that all the optimizations have the current traversal so they
207 * can report errors.
208 */
209 private void beginTraversal() {
210 for (AbstractPeepholeOptimization optimization : peepholeOptimizations) {
211 optimization.beginTraversal(compiler);
212 }
213 }
214
215 private void endTraversal() {
216 for (AbstractPeepholeOptimization optimization : peepholeOptimizations) {
217 optimization.endTraversal(compiler);
218 }
219 }
220 }