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