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 /// Analysis to determine how to generate code for `LookupMap`s. | 5 /// Analysis to determine how to generate code for `LookupMap`s. |
6 library compiler.src.js_backend.lookup_map_analysis; | 6 library compiler.src.js_backend.lookup_map_analysis; |
7 | 7 |
8 import 'package:pub_semver/pub_semver.dart'; | 8 import 'package:pub_semver/pub_semver.dart'; |
9 | 9 |
10 import '../common.dart'; | 10 import '../common.dart'; |
11 import '../common/registry.dart' show Registry; | 11 import '../common/registry.dart' show Registry; |
12 import '../compiler.dart' show Compiler; | 12 import '../compiler.dart' show Compiler; |
13 import '../constants/values.dart' | 13 import '../constants/values.dart' |
14 show | 14 show |
15 ConstantValue, | 15 ConstantValue, |
16 ConstructedConstantValue, | 16 ConstructedConstantValue, |
17 ListConstantValue, | 17 ListConstantValue, |
18 NullConstantValue, | 18 NullConstantValue, |
19 StringConstantValue, | 19 StringConstantValue, |
20 TypeConstantValue; | 20 TypeConstantValue; |
21 import '../dart_types.dart' show DartType; | 21 import '../dart_types.dart' show DartType; |
22 import '../dart_types.dart' show InterfaceType; | 22 import '../dart_types.dart' show InterfaceType; |
23 import '../elements/elements.dart' | 23 import '../elements/elements.dart' |
24 show ClassElement, FieldElement, LibraryElement, VariableElement; | 24 show ClassElement, FieldElement, LibraryElement, VariableElement; |
25 import '../enqueue.dart'; | |
26 import '../universe/world_impact.dart' | |
27 show WorldImpact, StagedWorldImpactBuilder; | |
28 import 'js_backend.dart' show JavaScriptBackend; | 25 import 'js_backend.dart' show JavaScriptBackend; |
29 | 26 |
30 /// An analysis and optimization to remove unused entries from a `LookupMap`. | 27 /// An analysis and optimization to remove unused entries from a `LookupMap`. |
31 /// | 28 /// |
32 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are | 29 /// `LookupMaps` are defined in `package:lookup_map/lookup_map.dart`. They are |
33 /// simple maps that contain constant expressions as keys, and that only support | 30 /// simple maps that contain constant expressions as keys, and that only support |
34 /// the lookup operation. | 31 /// the lookup operation. |
35 /// | 32 /// |
36 /// This analysis and optimization will tree-shake the contents of the maps by | 33 /// This analysis and optimization will tree-shake the contents of the maps by |
37 /// looking at the program and finding which keys are clearly unused. Not all | 34 /// looking at the program and finding which keys are clearly unused. Not all |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 /// runtime. Technically if we limit lookup-maps to check for identical keys, | 111 /// runtime. Technically if we limit lookup-maps to check for identical keys, |
115 /// we could allow const instances of these types. However, we internally use | 112 /// we could allow const instances of these types. However, we internally use |
116 /// a hash map within lookup-maps today, so we need this restriction. | 113 /// a hash map within lookup-maps today, so we need this restriction. |
117 final _typesWithEquals = <ClassElement, bool>{}; | 114 final _typesWithEquals = <ClassElement, bool>{}; |
118 | 115 |
119 /// Pending work to do if we discover that a new key is in use. For each key | 116 /// Pending work to do if we discover that a new key is in use. For each key |
120 /// that we haven't seen, we record the list of lookup-maps that contain an | 117 /// that we haven't seen, we record the list of lookup-maps that contain an |
121 /// entry with that key. | 118 /// entry with that key. |
122 final _pending = <ConstantValue, List<_LookupMapInfo>>{}; | 119 final _pending = <ConstantValue, List<_LookupMapInfo>>{}; |
123 | 120 |
124 final StagedWorldImpactBuilder impactBuilder = new StagedWorldImpactBuilder(); | |
125 | |
126 /// Whether the backend is currently processing the codegen queue. | 121 /// Whether the backend is currently processing the codegen queue. |
127 bool _inCodegen = false; | 122 bool _inCodegen = false; |
128 | 123 |
129 LookupMapAnalysis(this.backend, this.reporter); | 124 LookupMapAnalysis(this.backend, this.reporter); |
130 | 125 |
131 void onQueueEmpty(Enqueuer enqueuer) { | |
132 if (enqueuer.isResolutionQueue) return; | |
133 enqueuer.applyImpact(null, impactBuilder.flush()); | |
134 } | |
135 | |
136 /// Whether this analysis and optimization is enabled. | 126 /// Whether this analysis and optimization is enabled. |
137 bool get _isEnabled { | 127 bool get _isEnabled { |
138 // `lookupMap==off` kept here to make it easy to test disabling this feature | 128 // `lookupMap==off` kept here to make it easy to test disabling this feature |
139 if (const String.fromEnvironment('lookupMap') == 'off') return false; | 129 if (const String.fromEnvironment('lookupMap') == 'off') return false; |
140 return typeLookupMapClass != null; | 130 return typeLookupMapClass != null; |
141 } | 131 } |
142 | 132 |
143 /// Initializes this analysis by providing the resolved library. This is | 133 /// Initializes this analysis by providing the resolved library. This is |
144 /// invoked during resolution when the `lookup_map` library is discovered. | 134 /// invoked during resolution when the `lookup_map` library is discovered. |
145 void init(LibraryElement library) { | 135 void init(LibraryElement library) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 } | 241 } |
252 | 242 |
253 /// Callback from the enqueuer, invoked when [element] is instantiated. | 243 /// Callback from the enqueuer, invoked when [element] is instantiated. |
254 void registerInstantiatedClass(ClassElement element) { | 244 void registerInstantiatedClass(ClassElement element) { |
255 if (!_isEnabled || !_inCodegen) return; | 245 if (!_isEnabled || !_inCodegen) return; |
256 // TODO(sigmund): only add if .runtimeType is ever used | 246 // TODO(sigmund): only add if .runtimeType is ever used |
257 _addClassUse(element); | 247 _addClassUse(element); |
258 } | 248 } |
259 | 249 |
260 /// Callback from the enqueuer, invoked when [type] is instantiated. | 250 /// Callback from the enqueuer, invoked when [type] is instantiated. |
261 void registerInstantiatedType(InterfaceType type) { | 251 void registerInstantiatedType(InterfaceType type, Registry registry) { |
262 if (!_isEnabled || !_inCodegen) return; | 252 if (!_isEnabled || !_inCodegen) return; |
263 // TODO(sigmund): only add if .runtimeType is ever used | 253 // TODO(sigmund): only add if .runtimeType is ever used |
264 _addClassUse(type.element); | 254 _addClassUse(type.element); |
265 // TODO(sigmund): only do this when type-argument expressions are used? | 255 // TODO(sigmund): only do this when type-argument expressions are used? |
266 _addGenerics(type); | 256 _addGenerics(type, registry); |
267 } | 257 } |
268 | 258 |
269 /// Records generic type arguments in [type], in case they are retrieved and | 259 /// Records generic type arguments in [type], in case they are retrieved and |
270 /// returned using a type-argument expression. | 260 /// returned using a type-argument expression. |
271 void _addGenerics(InterfaceType type) { | 261 void _addGenerics(InterfaceType type, Registry registry) { |
272 if (!type.isGeneric) return; | 262 if (!type.isGeneric) return; |
273 for (var arg in type.typeArguments) { | 263 for (var arg in type.typeArguments) { |
274 if (arg is InterfaceType) { | 264 if (arg is InterfaceType) { |
275 _addClassUse(arg.element); | 265 _addClassUse(arg.element); |
276 // Note: this call was needed to generate correct code for | 266 // Note: this call was needed to generate correct code for |
277 // type_lookup_map/generic_type_test | 267 // type_lookup_map/generic_type_test |
278 // TODO(sigmund): can we get rid of this? | 268 // TODO(sigmund): can we get rid of this? |
279 backend.computeImpactForInstantiatedConstantType( | 269 backend.registerInstantiatedConstantType( |
280 backend.typeImplementation.rawType, impactBuilder); | 270 backend.typeImplementation.rawType, registry); |
281 _addGenerics(arg); | 271 _addGenerics(arg, registry); |
282 } | 272 } |
283 } | 273 } |
284 } | 274 } |
285 | 275 |
286 /// Callback from the codegen enqueuer, invoked when a constant (which is | 276 /// Callback from the codegen enqueuer, invoked when a constant (which is |
287 /// possibly a const key or a type literal) is used in the program. | 277 /// possibly a const key or a type literal) is used in the program. |
288 void registerTypeConstant(ClassElement element) { | 278 void registerTypeConstant(ClassElement element) { |
289 if (!_isEnabled || !_inCodegen) return; | 279 if (!_isEnabled || !_inCodegen) return; |
290 _addClassUse(element); | 280 _addClassUse(element); |
291 } | 281 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 } | 400 } |
411 | 401 |
412 /// Marks that [key] has been seen, and thus, the corresponding entry in this | 402 /// Marks that [key] has been seen, and thus, the corresponding entry in this |
413 /// map should be considered reachable. | 403 /// map should be considered reachable. |
414 _markUsed(ConstantValue key) { | 404 _markUsed(ConstantValue key) { |
415 assert(!emitted); | 405 assert(!emitted); |
416 assert(unusedEntries.containsKey(key)); | 406 assert(unusedEntries.containsKey(key)); |
417 assert(!usedEntries.containsKey(key)); | 407 assert(!usedEntries.containsKey(key)); |
418 ConstantValue constant = unusedEntries.remove(key); | 408 ConstantValue constant = unusedEntries.remove(key); |
419 usedEntries[key] = constant; | 409 usedEntries[key] = constant; |
420 analysis.backend.computeImpactForCompileTimeConstant( | 410 analysis.backend.registerCompileTimeConstant( |
421 constant, analysis.impactBuilder, false); | 411 constant, analysis.backend.compiler.globalDependencies); |
422 } | 412 } |
423 | 413 |
424 /// Restores [original] to contain all of the entries marked as possibly used. | 414 /// Restores [original] to contain all of the entries marked as possibly used. |
425 void _prepareForEmission() { | 415 void _prepareForEmission() { |
426 ListConstantValue originalEntries = original.fields[analysis.entriesField]; | 416 ListConstantValue originalEntries = original.fields[analysis.entriesField]; |
427 DartType listType = originalEntries.type; | 417 DartType listType = originalEntries.type; |
428 List<ConstantValue> keyValuePairs = <ConstantValue>[]; | 418 List<ConstantValue> keyValuePairs = <ConstantValue>[]; |
429 usedEntries.forEach((key, value) { | 419 usedEntries.forEach((key, value) { |
430 keyValuePairs.add(key); | 420 keyValuePairs.add(key); |
431 keyValuePairs.add(value); | 421 keyValuePairs.add(value); |
432 }); | 422 }); |
433 | 423 |
434 // Note: we are restoring the entries here, see comment in [original]. | 424 // Note: we are restoring the entries here, see comment in [original]. |
435 if (singlePair) { | 425 if (singlePair) { |
436 assert(keyValuePairs.length == 0 || keyValuePairs.length == 2); | 426 assert(keyValuePairs.length == 0 || keyValuePairs.length == 2); |
437 if (keyValuePairs.length == 2) { | 427 if (keyValuePairs.length == 2) { |
438 original.fields[analysis.keyField] = keyValuePairs[0]; | 428 original.fields[analysis.keyField] = keyValuePairs[0]; |
439 original.fields[analysis.valueField] = keyValuePairs[1]; | 429 original.fields[analysis.valueField] = keyValuePairs[1]; |
440 } | 430 } |
441 } else { | 431 } else { |
442 original.fields[analysis.entriesField] = | 432 original.fields[analysis.entriesField] = |
443 new ListConstantValue(listType, keyValuePairs); | 433 new ListConstantValue(listType, keyValuePairs); |
444 } | 434 } |
445 } | 435 } |
446 } | 436 } |
447 | 437 |
448 final _validLookupMapVersionConstraint = new VersionConstraint.parse('^0.0.1'); | 438 final _validLookupMapVersionConstraint = new VersionConstraint.parse('^0.0.1'); |
OLD | NEW |