| 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 /// Eliminates moving assignments, such as w := v, by assigning directly to w | 7 /// Eliminates moving assignments, such as w := v, by assigning directly to w |
| 8 /// at the definition of v. | 8 /// at the definition of v. |
| 9 /// | 9 /// |
| 10 /// This compensates for suboptimal register allocation, and merges closure | 10 /// This compensates for suboptimal register allocation, and merges closure |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 /// Like [move], except w is the key instead of v. | 21 /// Like [move], except w is the key instead of v. |
| 22 Map<Variable, Assign> inverseMove = <Variable, Assign>{}; | 22 Map<Variable, Assign> inverseMove = <Variable, Assign>{}; |
| 23 | 23 |
| 24 ExecutableElement currentElement; | 24 ExecutableElement currentElement; |
| 25 | 25 |
| 26 void rewriteExecutableDefinition(ExecutableDefinition root) { | 26 void rewriteExecutableDefinition(ExecutableDefinition root) { |
| 27 currentElement = root.element; | 27 currentElement = root.element; |
| 28 root.body = visitStatement(root.body); | 28 root.body = visitStatement(root.body); |
| 29 } | 29 } |
| 30 | 30 |
| 31 rewriteFunctionDefinition(FunctionDefinition function) { | 31 rewriteFunctionDefinition(FunctionDefinition node) { |
| 32 if (function.isAbstract) return; | 32 if (node.isAbstract) return; |
| 33 rewriteExecutableDefinition(function); | 33 rewriteExecutableDefinition(node); |
| 34 | 34 |
| 35 // Try to propagate moving assignments into function parameters. | 35 // Try to propagate moving assignments into function parameters. |
| 36 // For example: | 36 // For example: |
| 37 // foo(x) { | 37 // foo(x) { |
| 38 // var v1 = x; | 38 // var v1 = x; |
| 39 // BODY | 39 // BODY |
| 40 // } | 40 // } |
| 41 // ==> | 41 // ==> |
| 42 // foo(v1) { | 42 // foo(v1) { |
| 43 // BODY | 43 // BODY |
| 44 // } | 44 // } |
| 45 | 45 |
| 46 // Variables must not occur more than once in the parameter list, so | 46 // Variables must not occur more than once in the parameter list, so |
| 47 // invalidate all moving assignments that would propagate a parameter | 47 // invalidate all moving assignments that would propagate a parameter |
| 48 // into another parameter. For example: | 48 // into another parameter. For example: |
| 49 // foo(x,y) { | 49 // foo(x,y) { |
| 50 // y = x; | 50 // y = x; |
| 51 // BODY | 51 // BODY |
| 52 // } | 52 // } |
| 53 // Cannot declare function as foo(x,x)! | 53 // Cannot declare function as foo(x,x)! |
| 54 function.parameters.forEach(visitVariable); | 54 node.parameters.forEach(visitVariable); |
| 55 | 55 |
| 56 // Now do the propagation. | 56 // Now do the propagation. |
| 57 for (int i = 0; i < function.parameters.length; i++) { | 57 for (int i = 0; i < node.parameters.length; i++) { |
| 58 Variable param = function.parameters[i]; | 58 Variable param = node.parameters[i]; |
| 59 Variable replacement = copyPropagateVariable(param); | 59 Variable replacement = copyPropagateVariable(param); |
| 60 replacement.element = param.element; // Preserve parameter name. | 60 replacement.element = param.element; // Preserve parameter name. |
| 61 function.parameters[i] = replacement; | 61 node.parameters[i] = replacement; |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 rewriteConstructorDefinition(ConstructorDefinition node) { |
| 66 if (node.isAbstract) return; |
| 67 node.initializers.forEach(visitExpression); |
| 68 rewriteExecutableDefinition(node); |
| 69 |
| 70 |
| 71 // Try to propagate moving assignments into function parameters. |
| 72 // For example: |
| 73 // foo(x) { |
| 74 // var v1 = x; |
| 75 // BODY |
| 76 // } |
| 77 // ==> |
| 78 // foo(v1) { |
| 79 // BODY |
| 80 // } |
| 81 |
| 82 // Variables must not occur more than once in the parameter list, so |
| 83 // invalidate all moving assignments that would propagate a parameter |
| 84 // into another parameter. For example: |
| 85 // foo(x,y) { |
| 86 // y = x; |
| 87 // BODY |
| 88 // } |
| 89 // Cannot declare function as foo(x,x)! |
| 90 node.parameters.forEach(visitVariable); |
| 91 |
| 92 // Now do the propagation. |
| 93 for (int i = 0; i < node.parameters.length; i++) { |
| 94 Variable param = node.parameters[i]; |
| 95 Variable replacement = copyPropagateVariable(param); |
| 96 replacement.element = param.element; // Preserve parameter name. |
| 97 node.parameters[i] = replacement; |
| 98 } |
| 99 |
| 100 } |
| 101 |
| 102 |
| 65 Statement visitBasicBlock(Statement node) { | 103 Statement visitBasicBlock(Statement node) { |
| 66 node = visitStatement(node); | 104 node = visitStatement(node); |
| 67 move.clear(); | 105 move.clear(); |
| 68 inverseMove.clear(); | 106 inverseMove.clear(); |
| 69 return node; | 107 return node; |
| 70 } | 108 } |
| 71 | 109 |
| 72 void visitVariable(Variable variable) { | 110 void visitVariable(Variable variable) { |
| 73 // We have found a use of w. | 111 // We have found a use of w. |
| 74 // Remove assignments of form w := v from the move maps. | 112 // Remove assignments of form w := v from the move maps. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 Statement visitExpressionStatement(ExpressionStatement node) { | 221 Statement visitExpressionStatement(ExpressionStatement node) { |
| 184 node.next = visitStatement(node.next); | 222 node.next = visitStatement(node.next); |
| 185 visitExpression(node.expression); | 223 visitExpression(node.expression); |
| 186 return node; | 224 return node; |
| 187 } | 225 } |
| 188 | 226 |
| 189 void visitFunctionExpression(FunctionExpression node) { | 227 void visitFunctionExpression(FunctionExpression node) { |
| 190 new CopyPropagator().rewrite(node.definition); | 228 new CopyPropagator().rewrite(node.definition); |
| 191 } | 229 } |
| 192 | 230 |
| 231 void visitFieldInitializer(FieldInitializer node) { |
| 232 visitStatement(node.body); |
| 233 } |
| 234 |
| 193 } | 235 } |
| OLD | NEW |