| 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 part of tree_ir.optimization; | 5 part of tree_ir.optimization; |
| 6 | 6 |
| 7 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition], | 7 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition], |
| 8 /// 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 |
| 9 /// loop. Schematically: | 9 /// loop. Schematically: |
| 10 /// | 10 /// |
| 11 /// L: | 11 /// L: |
| 12 /// while (true) { | 12 /// while (true) { |
| 13 /// if (E) { | 13 /// if (E) { |
| 14 /// S1 (has references to L) | 14 /// S1 (has references to L) |
| 15 /// } else { | 15 /// } else { |
| 16 /// S2 (has no references to L) | 16 /// S2 (has no references to L) |
| 17 /// } | 17 /// } |
| 18 /// } | 18 /// } |
| 19 /// ==> | 19 /// ==> |
| 20 /// L: | 20 /// L: |
| 21 /// while (E) { | 21 /// while (E) { |
| 22 /// S1 | 22 /// S1 |
| 23 /// }; | 23 /// }; |
| 24 /// S2 | 24 /// S2 |
| 25 /// | 25 /// |
| 26 /// 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. |
| 27 /// | 27 /// |
| 28 /// Note that the above pattern needs no iteration since nested ifs | 28 /// Note that the above pattern needs no iteration since nested ifs |
| 29 /// have been collapsed previously in the [StatementRewriter] phase. | 29 /// have been collapsed previously in the [StatementRewriter] phase. |
| 30 class LoopRewriter extends RecursiveVisitor implements Pass { | 30 class LoopRewriter extends RecursiveVisitor with PassMixin { |
| 31 | 31 |
| 32 Set<Label> usedContinueLabels = new Set<Label>(); | 32 Set<Label> usedContinueLabels = new Set<Label>(); |
| 33 | 33 |
| 34 void rewrite(FunctionDefinition function) { | 34 void rewriteExecutableDefinition(ExecutableDefinition root) { |
| 35 if (function.isAbstract) return; | 35 root.body = visitStatement(root.body); |
| 36 | |
| 37 function.body = visitStatement(function.body); | |
| 38 } | 36 } |
| 39 | 37 |
| 40 Statement visitLabeledStatement(LabeledStatement node) { | 38 Statement visitLabeledStatement(LabeledStatement node) { |
| 41 node.body = visitStatement(node.body); | 39 node.body = visitStatement(node.body); |
| 42 node.next = visitStatement(node.next); | 40 node.next = visitStatement(node.next); |
| 43 return node; | 41 return node; |
| 44 } | 42 } |
| 45 | 43 |
| 46 Statement visitAssign(Assign node) { | 44 Statement visitAssign(Assign node) { |
| 47 // Clean up redundant assignments left behind in the previous phase. | 45 // Clean up redundant assignments left behind in the previous phase. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 Statement visitFunctionDeclaration(FunctionDeclaration node) { | 121 Statement visitFunctionDeclaration(FunctionDeclaration node) { |
| 124 new LoopRewriter().rewrite(node.definition); | 122 new LoopRewriter().rewrite(node.definition); |
| 125 node.next = visitStatement(node.next); | 123 node.next = visitStatement(node.next); |
| 126 return node; | 124 return node; |
| 127 } | 125 } |
| 128 | 126 |
| 129 void visitFunctionExpression(FunctionExpression node) { | 127 void visitFunctionExpression(FunctionExpression node) { |
| 130 new LoopRewriter().rewrite(node.definition); | 128 new LoopRewriter().rewrite(node.definition); |
| 131 } | 129 } |
| 132 } | 130 } |
| OLD | NEW |