| 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 loop_rewriter; | 5 part of tree_ir.optimization; |
| 6 | |
| 7 import '../tree_ir/tree_ir_nodes.dart'; | |
| 8 | 6 |
| 9 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition], | 7 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition], |
| 10 /// in situations where only one of the branches contains a [Continue] to the | 8 /// in situations where only one of the branches contains a [Continue] to the |
| 11 /// loop. Schematically: | 9 /// loop. Schematically: |
| 12 /// | 10 /// |
| 13 /// L: | 11 /// L: |
| 14 /// while (true) { | 12 /// while (true) { |
| 15 /// if (E) { | 13 /// if (E) { |
| 16 /// S1 (has references to L) | 14 /// S1 (has references to L) |
| 17 /// } else { | 15 /// } else { |
| 18 /// S2 (has no references to L) | 16 /// S2 (has no references to L) |
| 19 /// } | 17 /// } |
| 20 /// } | 18 /// } |
| 21 /// ==> | 19 /// ==> |
| 22 /// L: | 20 /// L: |
| 23 /// while (E) { | 21 /// while (E) { |
| 24 /// S1 | 22 /// S1 |
| 25 /// }; | 23 /// }; |
| 26 /// S2 | 24 /// S2 |
| 27 /// | 25 /// |
| 28 /// A similar transformation is used when S2 occurs in the 'then' position. | 26 /// A similar transformation is used when S2 occurs in the 'then' position. |
| 29 /// | 27 /// |
| 30 /// Note that the above pattern needs no iteration since nested ifs | 28 /// Note that the above pattern needs no iteration since nested ifs |
| 31 /// have been collapsed previously in the [StatementRewriter] phase. | 29 /// have been collapsed previously in the [StatementRewriter] phase. |
| 32 class LoopRewriter extends RecursiveVisitor { | 30 class LoopRewriter extends RecursiveVisitor implements Pass { |
| 33 | 31 |
| 34 Set<Label> usedContinueLabels = new Set<Label>(); | 32 Set<Label> usedContinueLabels = new Set<Label>(); |
| 35 | 33 |
| 36 void rewrite(FunctionDefinition function) { | 34 void rewrite(FunctionDefinition function) { |
| 37 if (function.isAbstract) return; | 35 if (function.isAbstract) return; |
| 38 | 36 |
| 39 function.body = visitStatement(function.body); | 37 function.body = visitStatement(function.body); |
| 40 } | 38 } |
| 41 | 39 |
| 42 Statement visitLabeledStatement(LabeledStatement node) { | 40 Statement visitLabeledStatement(LabeledStatement node) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 Statement visitFunctionDeclaration(FunctionDeclaration node) { | 123 Statement visitFunctionDeclaration(FunctionDeclaration node) { |
| 126 new LoopRewriter().rewrite(node.definition); | 124 new LoopRewriter().rewrite(node.definition); |
| 127 node.next = visitStatement(node.next); | 125 node.next = visitStatement(node.next); |
| 128 return node; | 126 return node; |
| 129 } | 127 } |
| 130 | 128 |
| 131 void visitFunctionExpression(FunctionExpression node) { | 129 void visitFunctionExpression(FunctionExpression node) { |
| 132 new LoopRewriter().rewrite(node.definition); | 130 new LoopRewriter().rewrite(node.definition); |
| 133 } | 131 } |
| 134 } | 132 } |
| OLD | NEW |