| 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 library dart2js.cps_ir.scalar_replacement; | 4 library dart2js.cps_ir.scalar_replacement; |
| 5 | 5 |
| 6 import 'optimizers.dart'; | 6 import 'optimizers.dart'; |
| 7 | 7 |
| 8 import 'dart:collection' show Queue; | 8 import 'dart:collection' show Queue; |
| 9 | 9 |
| 10 import '../closure.dart' show | 10 import '../closure.dart' show |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 final dart2js.InternalErrorFunction _internalError; | 42 final dart2js.InternalErrorFunction _internalError; |
| 43 final World _classWorld; | 43 final World _classWorld; |
| 44 | 44 |
| 45 ScalarReplacer(dart2js.Compiler compiler) | 45 ScalarReplacer(dart2js.Compiler compiler) |
| 46 : _internalError = compiler.internalError, | 46 : _internalError = compiler.internalError, |
| 47 _classWorld = compiler.world; | 47 _classWorld = compiler.world; |
| 48 | 48 |
| 49 @override | 49 @override |
| 50 void rewrite(FunctionDefinition root) { | 50 void rewrite(FunctionDefinition root) { |
| 51 // Set all parent pointers. | |
| 52 new ParentVisitor().visit(root); | |
| 53 ScalarReplacementVisitor analyzer = | 51 ScalarReplacementVisitor analyzer = |
| 54 new ScalarReplacementVisitor(_internalError, _classWorld); | 52 new ScalarReplacementVisitor(_internalError, _classWorld); |
| 55 analyzer.analyze(root); | 53 analyzer.analyze(root); |
| 56 analyzer.process(); | 54 analyzer.process(); |
| 57 } | 55 } |
| 58 } | 56 } |
| 59 | 57 |
| 60 /** | 58 /** |
| 61 * Do scalar replacement of aggregates on instances. Since scalar replacement | 59 * Do scalar replacement of aggregates on instances. Since scalar replacement |
| 62 * can create new candidiates, iterate until all scalar replacements are done. | 60 * can create new candidiates, iterate until all scalar replacements are done. |
| 63 */ | 61 */ |
| 64 class ScalarReplacementVisitor extends RecursiveVisitor { | 62 class ScalarReplacementVisitor extends TrampolineRecursiveVisitor { |
| 65 | 63 |
| 66 final dart2js.InternalErrorFunction internalError; | 64 final dart2js.InternalErrorFunction internalError; |
| 67 final World classWorld; | 65 final World classWorld; |
| 68 ScalarReplacementRemovalVisitor removalVisitor; | 66 ScalarReplacementRemovalVisitor removalVisitor; |
| 69 | 67 |
| 70 Primitive _current = null; | 68 Primitive _current = null; |
| 71 Set<Primitive> _allocations = new Set<Primitive>(); | 69 Set<Primitive> _allocations = new Set<Primitive>(); |
| 72 Queue<Primitive> _queue = new Queue<Primitive>(); | 70 Queue<Primitive> _queue = new Queue<Primitive>(); |
| 73 | 71 |
| 74 ScalarReplacementVisitor(this.internalError, this.classWorld) { | 72 ScalarReplacementVisitor(this.internalError, this.classWorld) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 183 |
| 186 // Delete [allocation] since that might 'free' another scalar replacement | 184 // Delete [allocation] since that might 'free' another scalar replacement |
| 187 // candidate by deleting the last non-field-access. | 185 // candidate by deleting the last non-field-access. |
| 188 deleteLetPrimOf(allocation); | 186 deleteLetPrimOf(allocation); |
| 189 } | 187 } |
| 190 | 188 |
| 191 /// Replaces [old] with [primitive] in [old]'s parent [LetPrim]. | 189 /// Replaces [old] with [primitive] in [old]'s parent [LetPrim]. |
| 192 void replacePrimitive(Primitive old, Primitive primitive) { | 190 void replacePrimitive(Primitive old, Primitive primitive) { |
| 193 LetPrim letPrim = old.parent; | 191 LetPrim letPrim = old.parent; |
| 194 letPrim.primitive = primitive; | 192 letPrim.primitive = primitive; |
| 193 primitive.parent = letPrim; |
| 195 } | 194 } |
| 196 | 195 |
| 197 void deleteLetPrimOf(Primitive primitive) { | 196 void deleteLetPrimOf(Primitive primitive) { |
| 198 assert(primitive.hasNoUses); | 197 assert(primitive.hasNoUses); |
| 199 LetPrim letPrim = primitive.parent; | 198 LetPrim letPrim = primitive.parent; |
| 200 letPrim.remove(); | 199 letPrim.remove(); |
| 201 deletePrimitive(primitive); | 200 deletePrimitive(primitive); |
| 202 } | 201 } |
| 203 | 202 |
| 204 void deletePrimitive(Primitive primitive) { | 203 void deletePrimitive(Primitive primitive) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 226 } | 225 } |
| 227 | 226 |
| 228 void visitCreateBox(CreateBox node) { | 227 void visitCreateBox(CreateBox node) { |
| 229 enqueue(node); | 228 enqueue(node); |
| 230 } | 229 } |
| 231 } | 230 } |
| 232 | 231 |
| 233 | 232 |
| 234 /// Visit a just-deleted subterm and unlink all [Reference]s in it. Reconsider | 233 /// Visit a just-deleted subterm and unlink all [Reference]s in it. Reconsider |
| 235 /// allocations for scalar replacement. | 234 /// allocations for scalar replacement. |
| 236 class ScalarReplacementRemovalVisitor extends RecursiveVisitor { | 235 class ScalarReplacementRemovalVisitor extends TrampolineRecursiveVisitor { |
| 237 ScalarReplacementVisitor process; | 236 ScalarReplacementVisitor process; |
| 238 | 237 |
| 239 ScalarReplacementRemovalVisitor(this.process); | 238 ScalarReplacementRemovalVisitor(this.process); |
| 240 | 239 |
| 241 processReference(Reference reference) { | 240 processReference(Reference reference) { |
| 242 process.reconsider(reference.definition); | 241 process.reconsider(reference.definition); |
| 243 reference.unlink(); | 242 reference.unlink(); |
| 244 } | 243 } |
| 245 } | 244 } |
| OLD | NEW |