| 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.redundant_join_elimination; | 5 library dart2js.cps_ir.redundant_join_elimination; |
| 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 /// Eliminates redundant join points. | 10 /// Eliminates redundant join points. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 while (true) { | 57 while (true) { |
| 58 Node parent = node.parent; | 58 Node parent = node.parent; |
| 59 if (parent is LetCont) { | 59 if (parent is LetCont) { |
| 60 node = parent; | 60 node = parent; |
| 61 } else { | 61 } else { |
| 62 return parent; | 62 return parent; |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 /// Removes [movedNode] from its current position and inserts it | |
| 68 /// before [target]. | |
| 69 void moveToBefore(Expression target, LetCont movedNode) { | |
| 70 if (movedNode.parent != null) { | |
| 71 movedNode.parent.body = movedNode.body; | |
| 72 movedNode.body.parent = movedNode.parent; | |
| 73 } | |
| 74 InteriorNode parent = target.parent; | |
| 75 parent.body = movedNode; | |
| 76 movedNode.body = target; | |
| 77 target.parent = movedNode; | |
| 78 movedNode.parent = parent; | |
| 79 } | |
| 80 | |
| 81 void rewriteBranch(Branch branch) { | 67 void rewriteBranch(Branch branch) { |
| 82 InteriorNode parent = getEffectiveParent(branch); | 68 InteriorNode parent = getEffectiveParent(branch); |
| 83 if (parent is! Continuation) return; | 69 if (parent is! Continuation) return; |
| 84 Continuation branchCont = parent; | 70 Continuation branchCont = parent; |
| 85 | 71 |
| 86 // Other optimizations take care of single-use continuations. | 72 // Other optimizations take care of single-use continuations. |
| 87 if (!branchCont.hasMultipleUses) return; | 73 if (!branchCont.hasMultipleUses) return; |
| 88 | 74 |
| 89 // It might be beneficial to rewrite calls to recursive continuations, | 75 // It might be beneficial to rewrite calls to recursive continuations, |
| 90 // but we currently do not support this. | 76 // but we currently do not support this. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 if (use is InvokeContinuation) { | 140 if (use is InvokeContinuation) { |
| 155 for (Parameter param in branchCont.parameters) { | 141 for (Parameter param in branchCont.parameters) { |
| 156 use.arguments.add(new Reference<Primitive>(param)); | 142 use.arguments.add(new Reference<Primitive>(param)); |
| 157 } | 143 } |
| 158 } else { | 144 } else { |
| 159 // The branch will be eliminated, so don't worry about updating it. | 145 // The branch will be eliminated, so don't worry about updating it. |
| 160 assert(use == branch); | 146 assert(use == branch); |
| 161 } | 147 } |
| 162 } | 148 } |
| 163 } | 149 } |
| 164 moveToBefore(outerLetCont, innerLetCont); | 150 innerLetCont.remove(); |
| 151 innerLetCont.insertAbove(outerLetCont); |
| 165 } | 152 } |
| 166 | 153 |
| 167 assert(branchCont.body == branch); | 154 assert(branchCont.body == branch); |
| 168 | 155 |
| 169 Continuation trueCont = branch.trueContinuation.definition; | 156 Continuation trueCont = branch.trueContinuation.definition; |
| 170 Continuation falseCont = branch.falseContinuation.definition; | 157 Continuation falseCont = branch.falseContinuation.definition; |
| 171 | 158 |
| 172 assert(branchCont != trueCont); | 159 assert(branchCont != trueCont); |
| 173 assert(branchCont != falseCont); | 160 assert(branchCont != falseCont); |
| 174 | 161 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 189 } | 176 } |
| 190 assert(branchCont.firstRef != reference); | 177 assert(branchCont.firstRef != reference); |
| 191 } | 178 } |
| 192 | 179 |
| 193 // Remove the now-unused branchCont continuation. | 180 // Remove the now-unused branchCont continuation. |
| 194 assert(branchCont.hasNoUses); | 181 assert(branchCont.hasNoUses); |
| 195 branch.trueContinuation.unlink(); | 182 branch.trueContinuation.unlink(); |
| 196 branch.falseContinuation.unlink(); | 183 branch.falseContinuation.unlink(); |
| 197 outerLetCont.continuations.remove(branchCont); | 184 outerLetCont.continuations.remove(branchCont); |
| 198 if (outerLetCont.continuations.isEmpty) { | 185 if (outerLetCont.continuations.isEmpty) { |
| 199 InteriorNode parent = outerLetCont.parent; | 186 outerLetCont.remove(); |
| 200 parent.body = outerLetCont.body; | |
| 201 outerLetCont.body.parent = parent; | |
| 202 } | 187 } |
| 203 | 188 |
| 204 // We may have created new redundant join points in the two branches. | 189 // We may have created new redundant join points in the two branches. |
| 205 enqueueContinuation(trueCont); | 190 enqueueContinuation(trueCont); |
| 206 enqueueContinuation(falseCont); | 191 enqueueContinuation(falseCont); |
| 207 } | 192 } |
| 208 | 193 |
| 209 void enqueueContinuation(Continuation cont) { | 194 void enqueueContinuation(Continuation cont) { |
| 210 Expression body = getEffectiveBody(cont); | 195 Expression body = getEffectiveBody(cont); |
| 211 if (body is Branch) { | 196 if (body is Branch) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 }); | 251 }); |
| 267 } | 252 } |
| 268 | 253 |
| 269 processReference(Reference ref) { | 254 processReference(Reference ref) { |
| 270 Parameter target = renaming[ref.definition]; | 255 Parameter target = renaming[ref.definition]; |
| 271 if (target != null) { | 256 if (target != null) { |
| 272 ref.changeTo(target); | 257 ref.changeTo(target); |
| 273 } | 258 } |
| 274 } | 259 } |
| 275 } | 260 } |
| OLD | NEW |