| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library dart2js.cps_ir.eagerly_load_statics; | |
| 6 | |
| 7 import '../elements/elements.dart'; | |
| 8 import 'cps_fragment.dart'; | |
| 9 import 'cps_ir_nodes.dart'; | |
| 10 import 'optimizers.dart' show Pass; | |
| 11 | |
| 12 /// Replaces [GetLazyStatic] with [GetStatic] when the static field is known | |
| 13 /// to have been initialized. | |
| 14 /// | |
| 15 /// Apart from [GetStatic] generating better code, this improves the side-effect | |
| 16 /// analysis in the [GVN] pass, since [GetStatic] has no effects. | |
| 17 class EagerlyLoadStatics extends TrampolineRecursiveVisitor implements Pass { | |
| 18 String get passName => 'Eagerly load statics'; | |
| 19 | |
| 20 Map<FieldElement, Primitive> initializerFor = <FieldElement, Primitive>{}; | |
| 21 | |
| 22 final Map<Continuation, Map<FieldElement, Primitive>> initializersAt = | |
| 23 <Continuation, Map<FieldElement, Primitive>>{}; | |
| 24 | |
| 25 static Map<FieldElement, Primitive> cloneFieldMap( | |
| 26 Map<FieldElement, Primitive> map) { | |
| 27 return new Map<FieldElement, Primitive>.from(map); | |
| 28 } | |
| 29 | |
| 30 void rewrite(FunctionDefinition node) { | |
| 31 visit(node.body); | |
| 32 } | |
| 33 | |
| 34 Expression traverseLetPrim(LetPrim node) { | |
| 35 Expression next = node.body; | |
| 36 visit(node.primitive); | |
| 37 return next; | |
| 38 } | |
| 39 | |
| 40 Expression traverseLetCont(LetCont node) { | |
| 41 for (Continuation cont in node.continuations) { | |
| 42 initializersAt[cont] = cloneFieldMap(initializerFor); | |
| 43 push(cont); | |
| 44 } | |
| 45 return node.body; | |
| 46 } | |
| 47 | |
| 48 Expression traverseLetHandler(LetHandler node) { | |
| 49 initializersAt[node.handler] = cloneFieldMap(initializerFor); | |
| 50 push(node.handler); | |
| 51 return node.body; | |
| 52 } | |
| 53 | |
| 54 Expression traverseContinuation(Continuation cont) { | |
| 55 initializerFor = initializersAt[cont]; | |
| 56 return cont.body; | |
| 57 } | |
| 58 | |
| 59 void visitGetLazyStatic(GetLazyStatic node) { | |
| 60 Primitive initializer = initializerFor[node.element]; | |
| 61 if (initializer is GetLazyStatic && initializer.isFinal) { | |
| 62 // No reason to create a GetStatic when the field is final. | |
| 63 node.replaceWithFragment(new CpsFragment(), initializer); | |
| 64 } else if (initializer != null) { | |
| 65 GetStatic newNode = new GetStatic.witnessed(node.element, initializer, | |
| 66 sourceInformation: node.sourceInformation)..type = node.type; | |
| 67 node.replaceWith(newNode); | |
| 68 } else { | |
| 69 initializerFor[node.element] = node; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void visitSetStatic(SetStatic node) { | |
| 74 initializerFor.putIfAbsent(node.element, () => node); | |
| 75 } | |
| 76 } | |
| OLD | NEW |