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 '../common.dart'; | 10 import '../common.dart'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 final InternalErrorFunction _internalError; | 26 final InternalErrorFunction _internalError; |
27 final World _classWorld; | 27 final World _classWorld; |
28 | 28 |
29 ScalarReplacer(dart2js.Compiler compiler) | 29 ScalarReplacer(dart2js.Compiler compiler) |
30 : _internalError = compiler.reporter.internalError, | 30 : _internalError = compiler.reporter.internalError, |
31 _classWorld = compiler.world; | 31 _classWorld = compiler.world; |
32 | 32 |
33 @override | 33 @override |
34 void rewrite(FunctionDefinition root) { | 34 void rewrite(FunctionDefinition root) { |
35 // Set all parent pointers. | |
36 new ParentVisitor().visit(root); | |
37 ScalarReplacementVisitor analyzer = | 35 ScalarReplacementVisitor analyzer = |
38 new ScalarReplacementVisitor(_internalError, _classWorld); | 36 new ScalarReplacementVisitor(_internalError, _classWorld); |
39 analyzer.analyze(root); | 37 analyzer.analyze(root); |
40 analyzer.process(); | 38 analyzer.process(); |
41 } | 39 } |
42 } | 40 } |
43 | 41 |
44 /** | 42 /** |
45 * Do scalar replacement of aggregates on instances. Since scalar replacement | 43 * Do scalar replacement of aggregates on instances. Since scalar replacement |
46 * can create new candidiates, iterate until all scalar replacements are done. | 44 * can create new candidiates, iterate until all scalar replacements are done. |
47 */ | 45 */ |
48 class ScalarReplacementVisitor extends RecursiveVisitor { | 46 class ScalarReplacementVisitor extends TrampolineRecursiveVisitor { |
49 | 47 |
50 final InternalErrorFunction internalError; | 48 final InternalErrorFunction internalError; |
51 final World classWorld; | 49 final World classWorld; |
52 ScalarReplacementRemovalVisitor removalVisitor; | 50 ScalarReplacementRemovalVisitor removalVisitor; |
53 | 51 |
54 Primitive _current = null; | 52 Primitive _current = null; |
55 Set<Primitive> _allocations = new Set<Primitive>(); | 53 Set<Primitive> _allocations = new Set<Primitive>(); |
56 Queue<Primitive> _queue = new Queue<Primitive>(); | 54 Queue<Primitive> _queue = new Queue<Primitive>(); |
57 | 55 |
58 ScalarReplacementVisitor(this.internalError, this.classWorld) { | 56 ScalarReplacementVisitor(this.internalError, this.classWorld) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 167 |
170 // Delete [allocation] since that might 'free' another scalar replacement | 168 // Delete [allocation] since that might 'free' another scalar replacement |
171 // candidate by deleting the last non-field-access. | 169 // candidate by deleting the last non-field-access. |
172 deleteLetPrimOf(allocation); | 170 deleteLetPrimOf(allocation); |
173 } | 171 } |
174 | 172 |
175 /// Replaces [old] with [primitive] in [old]'s parent [LetPrim]. | 173 /// Replaces [old] with [primitive] in [old]'s parent [LetPrim]. |
176 void replacePrimitive(Primitive old, Primitive primitive) { | 174 void replacePrimitive(Primitive old, Primitive primitive) { |
177 LetPrim letPrim = old.parent; | 175 LetPrim letPrim = old.parent; |
178 letPrim.primitive = primitive; | 176 letPrim.primitive = primitive; |
| 177 primitive.parent = letPrim; |
179 } | 178 } |
180 | 179 |
181 void deleteLetPrimOf(Primitive primitive) { | 180 void deleteLetPrimOf(Primitive primitive) { |
182 assert(primitive.hasNoUses); | 181 assert(primitive.hasNoUses); |
183 LetPrim letPrim = primitive.parent; | 182 LetPrim letPrim = primitive.parent; |
184 letPrim.remove(); | 183 letPrim.remove(); |
185 deletePrimitive(primitive); | 184 deletePrimitive(primitive); |
186 } | 185 } |
187 | 186 |
188 void deletePrimitive(Primitive primitive) { | 187 void deletePrimitive(Primitive primitive) { |
(...skipping 21 matching lines...) Expand all Loading... |
210 } | 209 } |
211 | 210 |
212 void visitCreateBox(CreateBox node) { | 211 void visitCreateBox(CreateBox node) { |
213 enqueue(node); | 212 enqueue(node); |
214 } | 213 } |
215 } | 214 } |
216 | 215 |
217 | 216 |
218 /// Visit a just-deleted subterm and unlink all [Reference]s in it. Reconsider | 217 /// Visit a just-deleted subterm and unlink all [Reference]s in it. Reconsider |
219 /// allocations for scalar replacement. | 218 /// allocations for scalar replacement. |
220 class ScalarReplacementRemovalVisitor extends RecursiveVisitor { | 219 class ScalarReplacementRemovalVisitor extends TrampolineRecursiveVisitor { |
221 ScalarReplacementVisitor process; | 220 ScalarReplacementVisitor process; |
222 | 221 |
223 ScalarReplacementRemovalVisitor(this.process); | 222 ScalarReplacementRemovalVisitor(this.process); |
224 | 223 |
225 processReference(Reference reference) { | 224 processReference(Reference reference) { |
226 process.reconsider(reference.definition); | 225 process.reconsider(reference.definition); |
227 reference.unlink(); | 226 reference.unlink(); |
228 } | 227 } |
229 } | 228 } |
OLD | NEW |