| 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 /** | 7 /** |
| 8 * Performs the following transformations on the tree: | 8 * Performs the following transformations on the tree: |
| 9 * - Assignment propagation | 9 * - Assignment propagation |
| 10 * - If-to-conditional conversion | 10 * - If-to-conditional conversion |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 * to their label are redirected. | 84 * to their label are redirected. |
| 85 * For example, where 'jump' is either break or continue: | 85 * For example, where 'jump' is either break or continue: |
| 86 * | 86 * |
| 87 * L0: {... break L0 ...}; jump L1 | 87 * L0: {... break L0 ...}; jump L1 |
| 88 * ==> | 88 * ==> |
| 89 * {... jump L1 ...} | 89 * {... jump L1 ...} |
| 90 * | 90 * |
| 91 * This may trigger a flattening of nested ifs in case the eliminated label | 91 * This may trigger a flattening of nested ifs in case the eliminated label |
| 92 * separated two ifs. | 92 * separated two ifs. |
| 93 */ | 93 */ |
| 94 class StatementRewriter extends Visitor<Statement, Expression> implements Pass { | 94 class StatementRewriter extends Visitor<Statement, Expression> with PassMixin { |
| 95 // The binding environment. The rightmost element of the list is the nearest | 95 // The binding environment. The rightmost element of the list is the nearest |
| 96 // available enclosing binding. | 96 // available enclosing binding. |
| 97 List<Assign> environment; | 97 List<Assign> environment; |
| 98 | 98 |
| 99 /// Substitution map for labels. Any break to a label L should be substituted | 99 /// Substitution map for labels. Any break to a label L should be substituted |
| 100 /// for a break to L' if L maps to L'. | 100 /// for a break to L' if L maps to L'. |
| 101 Map<Label, Jump> labelRedirects = <Label, Jump>{}; | 101 Map<Label, Jump> labelRedirects = <Label, Jump>{}; |
| 102 | 102 |
| 103 /// Returns the redirect target of [label] or [label] itself if it should not | 103 /// Returns the redirect target of [label] or [label] itself if it should not |
| 104 /// be redirected. | 104 /// be redirected. |
| 105 Jump redirect(Jump jump) { | 105 Jump redirect(Jump jump) { |
| 106 Jump newJump = labelRedirects[jump.target]; | 106 Jump newJump = labelRedirects[jump.target]; |
| 107 return newJump != null ? newJump : jump; | 107 return newJump != null ? newJump : jump; |
| 108 } | 108 } |
| 109 | 109 |
| 110 void rewrite(ExecutableDefinition definition) => definition.applyPass(this); | |
| 111 | 110 |
| 112 void rewriteFieldDefinition(FieldDefinition definition) { | 111 rewriteExecutableDefinition(ExecutableDefinition definition) { |
| 113 if (!definition.hasInitializer) return; | 112 definition.body = rewriteInEmptyEnvironment(definition.body); |
| 113 } |
| 114 | 114 |
| 115 void rewriteConstructorDefinition(ConstructorDefinition definition) { |
| 116 if (definition.isAbstract) return; |
| 117 definition.initializers.forEach(visitExpression); |
| 118 rewriteExecutableDefinition(definition); |
| 119 } |
| 120 |
| 121 Statement rewriteInEmptyEnvironment(Statement body) { |
| 122 List<Assign> oldEnvironment = environment; |
| 115 environment = <Assign>[]; | 123 environment = <Assign>[]; |
| 116 definition.body = visitStatement(definition.body); | |
| 117 | 124 |
| 125 Statement result = visitStatement(body); |
| 118 // TODO(kmillikin): Allow definitions that are not propagated. Here, | 126 // TODO(kmillikin): Allow definitions that are not propagated. Here, |
| 119 // this means rebuilding the binding with a recursively unnamed definition, | 127 // this means rebuilding the binding with a recursively unnamed definition, |
| 120 // or else introducing a variable definition and an assignment. | 128 // or else introducing a variable definition and an assignment. |
| 121 assert(environment.isEmpty); | 129 assert(environment.isEmpty); |
| 130 environment = oldEnvironment; |
| 131 return result; |
| 122 } | 132 } |
| 123 | 133 |
| 124 void rewriteFunctionDefinition(FunctionDefinition definition) { | 134 Expression visitFieldInitializer(FieldInitializer node) { |
| 125 if (definition.isAbstract) return; | 135 node.body = rewriteInEmptyEnvironment(node.body); |
| 126 | 136 return node; |
| 127 environment = <Assign>[]; | |
| 128 definition.body = visitStatement(definition.body); | |
| 129 | |
| 130 // TODO(kmillikin): Allow definitions that are not propagated. Here, | |
| 131 // this means rebuilding the binding with a recursively unnamed definition, | |
| 132 // or else introducing a variable definition and an assignment. | |
| 133 assert(environment.isEmpty); | |
| 134 } | 137 } |
| 135 | 138 |
| 139 Expression visitSuperInitializer(SuperInitializer node) { |
| 140 for (int i = node.arguments.length - 1; i >= 0; --i) { |
| 141 node.arguments[i] = rewriteInEmptyEnvironment(node.arguments[i]); |
| 142 } |
| 143 return node; |
| 144 } |
| 136 | 145 |
| 137 Expression visitExpression(Expression e) => e.processed ? e : e.accept(this); | 146 Expression visitExpression(Expression e) => e.processed ? e : e.accept(this); |
| 138 | 147 |
| 139 Expression visitVariable(Variable node) { | 148 Expression visitVariable(Variable node) { |
| 140 // Propagate a variable's definition to its use site if: | 149 // Propagate a variable's definition to its use site if: |
| 141 // 1. It has a single use, to avoid code growth and potential duplication | 150 // 1. It has a single use, to avoid code growth and potential duplication |
| 142 // of side effects, AND | 151 // of side effects, AND |
| 143 // 2. It was the most recent expression evaluated so that we do not | 152 // 2. It was the most recent expression evaluated so that we do not |
| 144 // reorder expressions with side effects. | 153 // reorder expressions with side effects. |
| 145 if (!environment.isEmpty && | 154 if (!environment.isEmpty && |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 } | 566 } |
| 558 | 567 |
| 559 Expression makeCondition(Expression e, bool polarity) { | 568 Expression makeCondition(Expression e, bool polarity) { |
| 560 return polarity ? e : new Not(e); | 569 return polarity ? e : new Not(e); |
| 561 } | 570 } |
| 562 | 571 |
| 563 Statement getBranch(If node, bool polarity) { | 572 Statement getBranch(If node, bool polarity) { |
| 564 return polarity ? node.thenStatement : node.elseStatement; | 573 return polarity ? node.thenStatement : node.elseStatement; |
| 565 } | 574 } |
| 566 } | 575 } |
| OLD | NEW |