| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dart2js.cps_ir.mutable_ssa; | 5 library dart2js.cps_ir.mutable_ssa; |
| 6 | 6 |
| 7 import 'cps_ir_nodes.dart'; | 7 import 'cps_ir_nodes.dart'; |
| 8 import 'optimizers.dart'; | 8 import 'optimizers.dart'; |
| 9 | 9 |
| 10 /// Determines which mutable variables should be rewritten to phi assignments | 10 /// Determines which mutable variables should be rewritten to phi assignments |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 107 |
| 108 bool shouldRewrite(MutableVariable variable) { | 108 bool shouldRewrite(MutableVariable variable) { |
| 109 return !analysis.hasAssignmentInTry.contains(variable); | 109 return !analysis.hasAssignmentInTry.contains(variable); |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool isJoinContinuation(Continuation cont) { | 112 bool isJoinContinuation(Continuation cont) { |
| 113 return !cont.hasExactlyOneUse || | 113 return !cont.hasExactlyOneUse || |
| 114 cont.firstRef.parent is InvokeContinuation; | 114 cont.firstRef.parent is InvokeContinuation; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void removeNode(InteriorNode node) { | |
| 118 InteriorNode parent = node.parent; | |
| 119 parent.body = node.body; | |
| 120 node.body.parent = parent; | |
| 121 } | |
| 122 | |
| 123 /// If some useful source information is attached to exactly one of the | 117 /// If some useful source information is attached to exactly one of the |
| 124 /// two definitions, the information is copied onto the other. | 118 /// two definitions, the information is copied onto the other. |
| 125 void mergeHints(MutableVariable variable, Primitive value) { | 119 void mergeHints(MutableVariable variable, Primitive value) { |
| 126 if (variable.hint == null) { | 120 if (variable.hint == null) { |
| 127 variable.hint = value.hint; | 121 variable.hint = value.hint; |
| 128 } else if (value.hint == null) { | 122 } else if (value.hint == null) { |
| 129 value.hint = variable.hint; | 123 value.hint = variable.hint; |
| 130 } | 124 } |
| 131 } | 125 } |
| 132 | 126 |
| 133 /// Processes a basic block, replacing mutable variable uses with direct | 127 /// Processes a basic block, replacing mutable variable uses with direct |
| 134 /// references to their values. | 128 /// references to their values. |
| 135 /// | 129 /// |
| 136 /// [environment] is the current value of each mutable variable. The map | 130 /// [environment] is the current value of each mutable variable. The map |
| 137 /// will be mutated during the processing. | 131 /// will be mutated during the processing. |
| 138 /// | 132 /// |
| 139 /// Continuations to be processed are put on the stack for later processing. | 133 /// Continuations to be processed are put on the stack for later processing. |
| 140 void processBlock(Expression node, | 134 void processBlock(Expression node, |
| 141 Map<MutableVariable, Primitive> environment) { | 135 Map<MutableVariable, Primitive> environment) { |
| 142 for (; node is! TailExpression; node = node.next) { | 136 Expression next = node.next; |
| 137 for (; node is! TailExpression; node = next, next = node.next) { |
| 143 if (node is LetMutable && shouldRewrite(node.variable)) { | 138 if (node is LetMutable && shouldRewrite(node.variable)) { |
| 144 // Put the new mutable variable on the stack while processing the body, | 139 // Put the new mutable variable on the stack while processing the body, |
| 145 // and pop it off again when done with the body. | 140 // and pop it off again when done with the body. |
| 146 mutableVariables.add(node.variable); | 141 mutableVariables.add(node.variable); |
| 147 stack.add(new VariableItem()); | 142 stack.add(new VariableItem()); |
| 148 | 143 |
| 149 // Put the initial value into the environment. | 144 // Put the initial value into the environment. |
| 150 Primitive value = node.value.definition; | 145 Primitive value = node.value.definition; |
| 151 environment[node.variable] = value; | 146 environment[node.variable] = value; |
| 152 | 147 |
| 153 // Preserve variable names. | 148 // Preserve variable names. |
| 154 mergeHints(node.variable, value); | 149 mergeHints(node.variable, value); |
| 155 | 150 |
| 156 // Remove the mutable variable binding. | 151 // Remove the mutable variable binding. |
| 157 node.value.unlink(); | 152 node.value.unlink(); |
| 158 removeNode(node); | 153 node.remove(); |
| 159 } else if (node is LetPrim && node.primitive is SetMutable) { | 154 } else if (node is LetPrim && node.primitive is SetMutable) { |
| 160 SetMutable setter = node.primitive; | 155 SetMutable setter = node.primitive; |
| 161 MutableVariable variable = setter.variable.definition; | 156 MutableVariable variable = setter.variable.definition; |
| 162 if (shouldRewrite(variable)) { | 157 if (shouldRewrite(variable)) { |
| 163 // As above, update the environment, preserve variables and remove | 158 // As above, update the environment, preserve variables and remove |
| 164 // the mutable variable assignment. | 159 // the mutable variable assignment. |
| 165 environment[variable] = setter.value.definition; | 160 environment[variable] = setter.value.definition; |
| 166 mergeHints(variable, setter.value.definition); | 161 mergeHints(variable, setter.value.definition); |
| 167 setter.value.unlink(); | 162 setter.value.unlink(); |
| 168 removeNode(node); | 163 node.remove(); |
| 169 } | 164 } |
| 170 } else if (node is LetPrim && node.primitive is GetMutable) { | 165 } else if (node is LetPrim && node.primitive is GetMutable) { |
| 171 GetMutable getter = node.primitive; | 166 GetMutable getter = node.primitive; |
| 172 MutableVariable variable = getter.variable.definition; | 167 MutableVariable variable = getter.variable.definition; |
| 173 if (shouldRewrite(variable)) { | 168 if (shouldRewrite(variable)) { |
| 174 // Replace with the reaching definition from the environment. | 169 // Replace with the reaching definition from the environment. |
| 175 Primitive value = environment[variable]; | 170 Primitive value = environment[variable]; |
| 176 value.substituteFor(getter); | 171 value.substituteFor(getter); |
| 177 mergeHints(variable, value); | 172 mergeHints(variable, value); |
| 178 removeNode(node); | 173 node.remove(); |
| 179 } | 174 } |
| 180 } else if (node is LetCont) { | 175 } else if (node is LetCont) { |
| 181 // Create phi parameters for each join continuation bound here, and put | 176 // Create phi parameters for each join continuation bound here, and put |
| 182 // them on the stack for later processing. | 177 // them on the stack for later processing. |
| 183 // Note that non-join continuations are handled at the use-site. | 178 // Note that non-join continuations are handled at the use-site. |
| 184 for (Continuation cont in node.continuations) { | 179 for (Continuation cont in node.continuations) { |
| 185 if (!isJoinContinuation(cont)) continue; | 180 if (!isJoinContinuation(cont)) continue; |
| 186 // Create a phi parameter for every mutable variable in scope. | 181 // Create a phi parameter for every mutable variable in scope. |
| 187 // At the same time, build the environment to use for processing | 182 // At the same time, build the environment to use for processing |
| 188 // the continuation (mapping mutables to phi parameters). | 183 // the continuation (mapping mutables to phi parameters). |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 class VariableItem extends StackItem {} | 242 class VariableItem extends StackItem {} |
| 248 | 243 |
| 249 /// Represents a yet unprocessed continuation together with the | 244 /// Represents a yet unprocessed continuation together with the |
| 250 /// environment in which to process it. | 245 /// environment in which to process it. |
| 251 class ContinuationItem extends StackItem { | 246 class ContinuationItem extends StackItem { |
| 252 final Continuation continuation; | 247 final Continuation continuation; |
| 253 final Map<MutableVariable, Primitive> environment; | 248 final Map<MutableVariable, Primitive> environment; |
| 254 | 249 |
| 255 ContinuationItem(this.continuation, this.environment); | 250 ContinuationItem(this.continuation, this.environment); |
| 256 } | 251 } |
| OLD | NEW |