OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library tree_ir.optimization.statement_rewriter; | 5 library tree_ir.optimization.statement_rewriter; |
6 | 6 |
7 import 'optimization.dart' show Pass; | 7 import 'optimization.dart' show Pass; |
8 import '../tree_ir_nodes.dart'; | 8 import '../tree_ir_nodes.dart'; |
9 import '../../io/source_information.dart'; | 9 import '../../io/source_information.dart'; |
10 | 10 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 /// Assignments with constant right-hand sides (see [isEffectivelyConstant]) | 158 /// Assignments with constant right-hand sides (see [isEffectivelyConstant]) |
159 /// are not considered impure and are put in [constantEnvironment] instead. | 159 /// are not considered impure and are put in [constantEnvironment] instead. |
160 /// | 160 /// |
161 /// Except for [Conditional]s, expressions in the environment have | 161 /// Except for [Conditional]s, expressions in the environment have |
162 /// not been processed, and all their subexpressions must therefore be | 162 /// not been processed, and all their subexpressions must therefore be |
163 /// variables uses. | 163 /// variables uses. |
164 List<Expression> environment = <Expression>[]; | 164 List<Expression> environment = <Expression>[]; |
165 | 165 |
166 /// Binding environment for variables that are assigned to effectively | 166 /// Binding environment for variables that are assigned to effectively |
167 /// constant expressions (see [isEffectivelyConstant]). | 167 /// constant expressions (see [isEffectivelyConstant]). |
168 final Map<Variable, Expression> constantEnvironment; | 168 Map<Variable, Expression> constantEnvironment; |
169 | 169 |
170 /// Substitution map for labels. Any break to a label L should be substituted | 170 /// Substitution map for labels. Any break to a label L should be substituted |
171 /// for a break to L' if L maps to L'. | 171 /// for a break to L' if L maps to L'. |
172 Map<Label, Jump> labelRedirects = <Label, Jump>{}; | 172 Map<Label, Jump> labelRedirects = <Label, Jump>{}; |
173 | 173 |
174 /// Number of uses of the given variable that are still unseen. | 174 /// Number of uses of the given variable that are still unseen. |
175 /// Used to detect the first use of a variable (since we do backwards | 175 /// Used to detect the first use of a variable (since we do backwards |
176 /// traversal, the first use is the last one seen). | 176 /// traversal, the first use is the last one seen). |
177 Map<Variable, int> unseenUses = <Variable, int>{}; | 177 Map<Variable, int> unseenUses = <Variable, int>{}; |
178 | 178 |
(...skipping 23 matching lines...) Expand all Loading... |
202 | 202 |
203 /// Returns the redirect target of [jump] or [jump] itself if it should not | 203 /// Returns the redirect target of [jump] or [jump] itself if it should not |
204 /// be redirected. | 204 /// be redirected. |
205 Jump redirect(Jump jump) { | 205 Jump redirect(Jump jump) { |
206 Jump newJump = labelRedirects[jump.target]; | 206 Jump newJump = labelRedirects[jump.target]; |
207 return newJump != null ? newJump : jump; | 207 return newJump != null ? newJump : jump; |
208 } | 208 } |
209 | 209 |
210 void inEmptyEnvironment(void action()) { | 210 void inEmptyEnvironment(void action()) { |
211 List oldEnvironment = environment; | 211 List oldEnvironment = environment; |
| 212 Map oldConstantEnvironment = constantEnvironment; |
212 environment = <Expression>[]; | 213 environment = <Expression>[]; |
| 214 constantEnvironment = <Variable, Expression>{}; |
213 action(); | 215 action(); |
214 assert(environment.isEmpty); | 216 assert(environment.isEmpty); |
215 environment = oldEnvironment; | 217 environment = oldEnvironment; |
| 218 constantEnvironment = oldConstantEnvironment; |
216 } | 219 } |
217 | 220 |
218 /// Left-hand side of the given assignment, or `null` if not an assignment. | 221 /// Left-hand side of the given assignment, or `null` if not an assignment. |
219 Variable getLeftHand(Expression e) { | 222 Variable getLeftHand(Expression e) { |
220 return e is Assign ? e.variable : null; | 223 return e is Assign ? e.variable : null; |
221 } | 224 } |
222 | 225 |
223 /// If the given expression always returns the value of one of its | 226 /// If the given expression always returns the value of one of its |
224 /// subexpressions, returns that subexpression, otherwise `null`. | 227 /// subexpressions, returns that subexpression, otherwise `null`. |
225 Expression getValueSubexpression(Expression e) { | 228 Expression getValueSubexpression(Expression e) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 /// Returns true if [exp] has no side effects and has a constant value within | 348 /// Returns true if [exp] has no side effects and has a constant value within |
346 /// any given activation of the enclosing method. | 349 /// any given activation of the enclosing method. |
347 bool isEffectivelyConstant(Expression exp) { | 350 bool isEffectivelyConstant(Expression exp) { |
348 // TODO(asgerf): Can be made more aggressive e.g. by checking conditional | 351 // TODO(asgerf): Can be made more aggressive e.g. by checking conditional |
349 // expressions recursively. Determine if that is a valuable optimization | 352 // expressions recursively. Determine if that is a valuable optimization |
350 // and/or if it is better handled at the CPS level. | 353 // and/or if it is better handled at the CPS level. |
351 return exp is Constant || | 354 return exp is Constant || |
352 exp is This || | 355 exp is This || |
353 exp is CreateInvocationMirror || | 356 exp is CreateInvocationMirror || |
354 exp is GetStatic && exp.element.isFunction || | 357 exp is GetStatic && exp.element.isFunction || |
| 358 exp is GetField && exp.objectIsNotNull && exp.field.isFinal || |
355 exp is Interceptor || | 359 exp is Interceptor || |
356 exp is ApplyBuiltinOperator || | 360 exp is ApplyBuiltinOperator || |
357 exp is VariableUse && constantEnvironment.containsKey(exp.variable); | 361 exp is VariableUse && constantEnvironment.containsKey(exp.variable); |
358 } | 362 } |
359 | 363 |
360 /// True if [node] is an assignment that can be propagated as a constant. | 364 /// True if [node] is an assignment that can be propagated as a constant. |
361 bool isEffectivelyConstantAssignment(Expression node) { | 365 bool isEffectivelyConstantAssignment(Expression node) { |
362 return node is Assign && | 366 return node is Assign && |
363 node.variable.writeCount == 1 && | 367 node.variable.writeCount == 1 && |
364 isEffectivelyConstant(node.value); | 368 isEffectivelyConstant(node.value); |
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1165 VariableUseVisitor(this.callback); | 1169 VariableUseVisitor(this.callback); |
1166 | 1170 |
1167 visitVariableUse(VariableUse use) => callback(use); | 1171 visitVariableUse(VariableUse use) => callback(use); |
1168 | 1172 |
1169 visitInnerFunction(FunctionDefinition node) {} | 1173 visitInnerFunction(FunctionDefinition node) {} |
1170 | 1174 |
1171 static void visit(Expression node, VariableUseCallback callback) { | 1175 static void visit(Expression node, VariableUseCallback callback) { |
1172 new VariableUseVisitor(callback).visitExpression(node); | 1176 new VariableUseVisitor(callback).visitExpression(node); |
1173 } | 1177 } |
1174 } | 1178 } |
OLD | NEW |