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