| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 mutableVariables.removeLast(); | 103 mutableVariables.removeLast(); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 } | 106 } |
| 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 || cont.firstRef.parent is InvokeContinuation; |
| 114 cont.firstRef.parent is InvokeContinuation; | |
| 115 } | 114 } |
| 116 | 115 |
| 117 /// If some useful source information is attached to exactly one of the | 116 /// If some useful source information is attached to exactly one of the |
| 118 /// two definitions, the information is copied onto the other. | 117 /// two definitions, the information is copied onto the other. |
| 119 void mergeHints(MutableVariable variable, Primitive value) { | 118 void mergeHints(MutableVariable variable, Primitive value) { |
| 120 if (variable.hint == null) { | 119 if (variable.hint == null) { |
| 121 variable.hint = value.hint; | 120 variable.hint = value.hint; |
| 122 } else if (value.hint == null) { | 121 } else if (value.hint == null) { |
| 123 value.hint = variable.hint; | 122 value.hint = variable.hint; |
| 124 } | 123 } |
| 125 } | 124 } |
| 126 | 125 |
| 127 /// Processes a basic block, replacing mutable variable uses with direct | 126 /// Processes a basic block, replacing mutable variable uses with direct |
| 128 /// references to their values. | 127 /// references to their values. |
| 129 /// | 128 /// |
| 130 /// [environment] is the current value of each mutable variable. The map | 129 /// [environment] is the current value of each mutable variable. The map |
| 131 /// will be mutated during the processing. | 130 /// will be mutated during the processing. |
| 132 /// | 131 /// |
| 133 /// Continuations to be processed are put on the stack for later processing. | 132 /// Continuations to be processed are put on the stack for later processing. |
| 134 void processBlock(Expression node, | 133 void processBlock( |
| 135 Map<MutableVariable, Primitive> environment) { | 134 Expression node, Map<MutableVariable, Primitive> environment) { |
| 136 Expression next = node.next; | 135 Expression next = node.next; |
| 137 for (; node is! TailExpression; node = next, next = node.next) { | 136 for (; node is! TailExpression; node = next, next = node.next) { |
| 138 if (node is LetMutable && shouldRewrite(node.variable)) { | 137 if (node is LetMutable && shouldRewrite(node.variable)) { |
| 139 // Put the new mutable variable on the stack while processing the body, | 138 // Put the new mutable variable on the stack while processing the body, |
| 140 // and pop it off again when done with the body. | 139 // and pop it off again when done with the body. |
| 141 mutableVariables.add(node.variable); | 140 mutableVariables.add(node.variable); |
| 142 stack.add(new VariableItem()); | 141 stack.add(new VariableItem()); |
| 143 | 142 |
| 144 // Put the initial value into the environment. | 143 // Put the initial value into the environment. |
| 145 Primitive value = node.value; | 144 Primitive value = node.value; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 for (int i = 0; i < phiCount; ++i) { | 213 for (int i = 0; i < phiCount; ++i) { |
| 215 Primitive value = environment[mutableVariables[i]]; | 214 Primitive value = environment[mutableVariables[i]]; |
| 216 Reference<Primitive> arg = new Reference<Primitive>(value); | 215 Reference<Primitive> arg = new Reference<Primitive>(value); |
| 217 node.argumentRefs.add(arg); | 216 node.argumentRefs.add(arg); |
| 218 arg.parent = node; | 217 arg.parent = node; |
| 219 } | 218 } |
| 220 } else if (node is Branch) { | 219 } else if (node is Branch) { |
| 221 // Enqueue both branches with the current environment. | 220 // Enqueue both branches with the current environment. |
| 222 // Clone the environments once so the processing of one branch does not | 221 // Clone the environments once so the processing of one branch does not |
| 223 // mutate the environment needed to process the other branch. | 222 // mutate the environment needed to process the other branch. |
| 224 stack.add(new ContinuationItem( | 223 stack.add(new ContinuationItem(node.trueContinuation, |
| 225 node.trueContinuation, | |
| 226 new Map<MutableVariable, Primitive>.from(environment))); | 224 new Map<MutableVariable, Primitive>.from(environment))); |
| 227 stack.add(new ContinuationItem( | 225 stack.add(new ContinuationItem(node.falseContinuation, environment)); |
| 228 node.falseContinuation, | |
| 229 environment)); | |
| 230 } else { | 226 } else { |
| 231 assert(node is Throw || node is Unreachable); | 227 assert(node is Throw || node is Unreachable); |
| 232 } | 228 } |
| 233 } | 229 } |
| 234 } | 230 } |
| 235 | 231 |
| 236 abstract class StackItem {} | 232 abstract class StackItem {} |
| 237 | 233 |
| 238 /// Represents a mutable variable that is in scope. | 234 /// Represents a mutable variable that is in scope. |
| 239 /// | 235 /// |
| 240 /// The topmost mutable variable falls out of scope when this item is | 236 /// The topmost mutable variable falls out of scope when this item is |
| 241 /// taken off the stack. | 237 /// taken off the stack. |
| 242 class VariableItem extends StackItem {} | 238 class VariableItem extends StackItem {} |
| 243 | 239 |
| 244 /// Represents a yet unprocessed continuation together with the | 240 /// Represents a yet unprocessed continuation together with the |
| 245 /// environment in which to process it. | 241 /// environment in which to process it. |
| 246 class ContinuationItem extends StackItem { | 242 class ContinuationItem extends StackItem { |
| 247 final Continuation continuation; | 243 final Continuation continuation; |
| 248 final Map<MutableVariable, Primitive> environment; | 244 final Map<MutableVariable, Primitive> environment; |
| 249 | 245 |
| 250 ContinuationItem(this.continuation, this.environment); | 246 ContinuationItem(this.continuation, this.environment); |
| 251 } | 247 } |
| OLD | NEW |