| 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 library tree_ir.optimization.loop_rewriter; |
| 6 |
| 7 import 'optimization.dart' show Pass; |
| 8 import '../tree_ir_nodes.dart'; |
| 6 | 9 |
| 7 /// Rewrites [WhileTrue] statements. | 10 /// Rewrites [WhileTrue] statements. |
| 8 /// | 11 /// |
| 9 /// Before this phase, loops usually contain a lot of "exit code", that is, | 12 /// Before this phase, loops usually contain a lot of "exit code", that is, |
| 10 /// code that happens at a point where a [Continue] can no longer be reached, | 13 /// code that happens at a point where a [Continue] can no longer be reached, |
| 11 /// and is therefore not really part of the loop. | 14 /// and is therefore not really part of the loop. |
| 12 /// Exit code is moved down after the loop using the following rewrites rules: | 15 /// Exit code is moved down after the loop using the following rewrites rules: |
| 13 /// | 16 /// |
| 14 /// EXTRACT LABELED STATEMENT: | 17 /// EXTRACT LABELED STATEMENT: |
| 15 /// | 18 /// |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 /// Note that the last pattern above needs no iteration since nested ifs | 53 /// Note that the last pattern above needs no iteration since nested ifs |
| 51 /// have been collapsed previously in the [StatementRewriter] phase. | 54 /// have been collapsed previously in the [StatementRewriter] phase. |
| 52 /// | 55 /// |
| 53 /// [WhileCondition] statements exist only after this phase. | 56 /// [WhileCondition] statements exist only after this phase. |
| 54 class LoopRewriter extends RecursiveTransformer | 57 class LoopRewriter extends RecursiveTransformer |
| 55 implements Pass { | 58 implements Pass { |
| 56 String get passName => 'Loop rewriter'; | 59 String get passName => 'Loop rewriter'; |
| 57 | 60 |
| 58 Set<Label> usedContinueLabels = new Set<Label>(); | 61 Set<Label> usedContinueLabels = new Set<Label>(); |
| 59 | 62 |
| 60 void rewrite(RootNode root) { | 63 void rewrite(FunctionDefinition root) { |
| 61 root.replaceEachBody(visitStatement); | 64 root.body = visitStatement(root.body); |
| 62 } | 65 } |
| 63 | 66 |
| 64 @override | 67 @override |
| 65 void visitInnerFunction(FunctionDefinition node) { | 68 void visitInnerFunction(FunctionDefinition node) { |
| 66 node.body = new LoopRewriter().visitStatement(node.body); | 69 node.body = new LoopRewriter().visitStatement(node.body); |
| 67 } | 70 } |
| 68 | 71 |
| 69 Statement visitContinue(Continue node) { | 72 Statement visitContinue(Continue node) { |
| 70 usedContinueLabels.add(node.target); | 73 usedContinueLabels.add(node.target); |
| 71 return node; | 74 return node; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } else { | 127 } else { |
| 125 node.body = visitStatement(node.body); | 128 node.body = visitStatement(node.body); |
| 126 usedContinueLabels.remove(node.label); | 129 usedContinueLabels.remove(node.label); |
| 127 } | 130 } |
| 128 | 131 |
| 129 if (head == null) return loop; | 132 if (head == null) return loop; |
| 130 tail.body = loop; | 133 tail.body = loop; |
| 131 return head; | 134 return head; |
| 132 } | 135 } |
| 133 } | 136 } |
| OLD | NEW |