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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 /// handler (i.e., if the code would be moved into a try from outside it). | 200 /// handler (i.e., if the code would be moved into a try from outside it). |
201 Set<Label> safeForInlining = new Set<Label>(); | 201 Set<Label> safeForInlining = new Set<Label>(); |
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(), {bool keepConstants: true}) { |
211 List oldEnvironment = environment; | 211 List oldEnvironment = environment; |
212 Map oldConstantEnvironment = constantEnvironment; | 212 Map oldConstantEnvironment = constantEnvironment; |
213 environment = <Expression>[]; | 213 environment = <Expression>[]; |
214 constantEnvironment = <Variable, Expression>{}; | 214 if (!keepConstants) { |
| 215 constantEnvironment = <Variable, Expression>{}; |
| 216 } |
215 action(); | 217 action(); |
216 assert(environment.isEmpty); | 218 assert(environment.isEmpty); |
217 environment = oldEnvironment; | 219 environment = oldEnvironment; |
218 constantEnvironment = oldConstantEnvironment; | 220 if (!keepConstants) { |
| 221 constantEnvironment = oldConstantEnvironment; |
| 222 } |
219 } | 223 } |
220 | 224 |
221 /// Left-hand side of the given assignment, or `null` if not an assignment. | 225 /// Left-hand side of the given assignment, or `null` if not an assignment. |
222 Variable getLeftHand(Expression e) { | 226 Variable getLeftHand(Expression e) { |
223 return e is Assign ? e.variable : null; | 227 return e is Assign ? e.variable : null; |
224 } | 228 } |
225 | 229 |
226 /// If the given expression always returns the value of one of its | 230 /// If the given expression always returns the value of one of its |
227 /// subexpressions, returns that subexpression, otherwise `null`. | 231 /// subexpressions, returns that subexpression, otherwise `null`. |
228 Expression getValueSubexpression(Expression e) { | 232 Expression getValueSubexpression(Expression e) { |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 if (reduced != null) { | 654 if (reduced != null) { |
651 return reduced; | 655 return reduced; |
652 } | 656 } |
653 | 657 |
654 return node; | 658 return node; |
655 } | 659 } |
656 | 660 |
657 Statement visitWhileTrue(WhileTrue node) { | 661 Statement visitWhileTrue(WhileTrue node) { |
658 // Do not propagate assignments into loops. Doing so is not safe for | 662 // Do not propagate assignments into loops. Doing so is not safe for |
659 // variables modified in the loop (the initial value will be propagated). | 663 // variables modified in the loop (the initial value will be propagated). |
| 664 // Do not propagate effective constant expressions into loops, since |
| 665 // computing them is not free (e.g. interceptors are expensive). |
660 inEmptyEnvironment(() { | 666 inEmptyEnvironment(() { |
661 node.body = visitStatement(node.body); | 667 node.body = visitStatement(node.body); |
662 }); | 668 }, keepConstants: false); |
663 return node; | 669 return node; |
664 } | 670 } |
665 | 671 |
666 Statement visitFor(For node) { | 672 Statement visitFor(For node) { |
667 // Not introduced yet | 673 // Not introduced yet |
668 throw "Unexpected For in StatementRewriter"; | 674 throw "Unexpected For in StatementRewriter"; |
669 } | 675 } |
670 | 676 |
671 Statement visitTry(Try node) { | 677 Statement visitTry(Try node) { |
672 inEmptyEnvironment(() { | 678 inEmptyEnvironment(() { |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1219 VariableUseVisitor(this.callback); | 1225 VariableUseVisitor(this.callback); |
1220 | 1226 |
1221 visitVariableUse(VariableUse use) => callback(use); | 1227 visitVariableUse(VariableUse use) => callback(use); |
1222 | 1228 |
1223 visitInnerFunction(FunctionDefinition node) {} | 1229 visitInnerFunction(FunctionDefinition node) {} |
1224 | 1230 |
1225 static void visit(Expression node, VariableUseCallback callback) { | 1231 static void visit(Expression node, VariableUseCallback callback) { |
1226 new VariableUseVisitor(callback).visitExpression(node); | 1232 new VariableUseVisitor(callback).visitExpression(node); |
1227 } | 1233 } |
1228 } | 1234 } |
OLD | NEW |