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