| 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'; |
| 11 import '../compiler.dart' as dart2js show | 11 import '../compiler.dart' as dart2js show |
| 12 Compiler; | 12 Compiler; |
| 13 import '../constants/values.dart'; | 13 import '../constants/values.dart'; |
| 14 import '../elements/elements.dart'; | 14 import '../elements/elements.dart'; |
| 15 import '../types/types.dart'; | 15 import '../types/types.dart'; |
| 16 import '../world.dart' show World; | 16 import '../world.dart' show World; |
| 17 import 'cps_ir_nodes.dart'; | 17 import 'cps_ir_nodes.dart'; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Replaces aggregates with a set of local values. Performs inlining of | 20 * Replaces aggregates with a set of local values. Performs inlining of |
| 21 * single-use closures to generate more replacable aggregates. | 21 * single-use closures to generate more replaceable aggregates. |
| 22 */ | 22 */ |
| 23 class ScalarReplacer extends Pass { | 23 class ScalarReplacer extends Pass { |
| 24 String get passName => 'Scalar replacement'; | 24 String get passName => 'Scalar replacement'; |
| 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 ScalarReplacementVisitor analyzer = | 35 ScalarReplacementVisitor analyzer = |
| 36 new ScalarReplacementVisitor(_internalError, _classWorld); | 36 new ScalarReplacementVisitor(_internalError, _classWorld); |
| 37 analyzer.analyze(root); | 37 analyzer.analyze(root); |
| 38 analyzer.process(); | 38 analyzer.process(); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Do scalar replacement of aggregates on instances. Since scalar replacement | 43 * Do scalar replacement of aggregates on instances. Since scalar replacement |
| 44 * can create new candidiates, iterate until all scalar replacements are done. | 44 * can create new candidates, iterate until all scalar replacements are done. |
| 45 */ | 45 */ |
| 46 class ScalarReplacementVisitor extends TrampolineRecursiveVisitor { | 46 class ScalarReplacementVisitor extends TrampolineRecursiveVisitor { |
| 47 | 47 |
| 48 final InternalErrorFunction internalError; | 48 final InternalErrorFunction internalError; |
| 49 final World classWorld; | 49 final World classWorld; |
| 50 ScalarReplacementRemovalVisitor removalVisitor; | 50 ScalarReplacementRemovalVisitor removalVisitor; |
| 51 | 51 |
| 52 Primitive _current = null; | 52 Primitive _current = null; |
| 53 Set<Primitive> _allocations = new Set<Primitive>(); | 53 Set<Primitive> _allocations = new Set<Primitive>(); |
| 54 Queue<Primitive> _queue = new Queue<Primitive>(); | 54 Queue<Primitive> _queue = new Queue<Primitive>(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // Find the initial values of the fields. A CreateBox has no initial | 97 // Find the initial values of the fields. A CreateBox has no initial |
| 98 // values. CreateInstance has initial values in the order of the fields. | 98 // values. CreateInstance has initial values in the order of the fields. |
| 99 Map<FieldElement, Primitive> fieldInitialValues = | 99 Map<FieldElement, Primitive> fieldInitialValues = |
| 100 <FieldElement, Primitive>{}; | 100 <FieldElement, Primitive>{}; |
| 101 if (allocation is CreateInstance) { | 101 if (allocation is CreateInstance) { |
| 102 int i = 0; | 102 int i = 0; |
| 103 allocation.classElement.forEachInstanceField( | 103 allocation.classElement.forEachInstanceField( |
| 104 (ClassElement enclosingClass, FieldElement field) { | 104 (ClassElement enclosingClass, FieldElement field) { |
| 105 Primitive argument = allocation.arguments[i++].definition; | 105 Primitive argument = allocation.arguments[i++].definition; |
| 106 fieldInitialValues[field] = argument; | 106 fieldInitialValues[field] = argument; |
| 107 }); | 107 }, |
| 108 includeSuperAndInjectedMembers: true); |
| 108 } | 109 } |
| 109 | 110 |
| 110 // Create [MutableVariable]s for each written field. Initialize the | 111 // Create [MutableVariable]s for each written field. Initialize the |
| 111 // MutableVariable with the value from the allocator, or initialize with a | 112 // MutableVariable with the value from the allocator, or initialize with a |
| 112 // `null` constant if there is not initial value. | 113 // `null` constant if there is not initial value. |
| 113 Map<FieldElement, MutableVariable> cells = | 114 Map<FieldElement, MutableVariable> cells = |
| 114 <FieldElement, MutableVariable>{}; | 115 <FieldElement, MutableVariable>{}; |
| 115 InteriorNode insertionPoint = allocation.parent; // LetPrim | 116 InteriorNode insertionPoint = allocation.parent; // LetPrim |
| 116 for (FieldElement field in writes) { | 117 for (FieldElement field in writes) { |
| 117 MutableVariable variable = new MutableVariable(field); | 118 MutableVariable variable = new MutableVariable(field); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 class ScalarReplacementRemovalVisitor extends TrampolineRecursiveVisitor { | 220 class ScalarReplacementRemovalVisitor extends TrampolineRecursiveVisitor { |
| 220 ScalarReplacementVisitor process; | 221 ScalarReplacementVisitor process; |
| 221 | 222 |
| 222 ScalarReplacementRemovalVisitor(this.process); | 223 ScalarReplacementRemovalVisitor(this.process); |
| 223 | 224 |
| 224 processReference(Reference reference) { | 225 processReference(Reference reference) { |
| 225 process.reconsider(reference.definition); | 226 process.reconsider(reference.definition); |
| 226 reference.unlink(); | 227 reference.unlink(); |
| 227 } | 228 } |
| 228 } | 229 } |
| OLD | NEW |