Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: pkg/compiler/lib/src/cps_ir/share_final_fields.dart

Issue 1444363002: dart2js cps: Global value numbering and loop-invariant code motion. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.share_final_fields;
6
7 import 'optimizers.dart';
8 import 'cps_ir_nodes.dart';
9 import 'loop_hierarchy.dart';
10 import '../elements/elements.dart';
11 import '../js_backend/js_backend.dart' show JavaScriptBackend;
12 import '../types/types.dart' show TypeMask;
13
14 /// Removes redundant GetField operations.
15 ///
16 /// The pass performs these optimizations for field loads:
17 /// - share GetFields of final fields when one is in scope of the other.
18 /// - pull GetField operations of final fields out of loops when safe (object is
19 /// not null).
20 ///
21 /// This pass only optimizes final fields and should be replaced with a full
22 /// load elimination pass.
23 class ShareFinalFields extends TrampolineRecursiveVisitor implements Pass {
24 String get passName => 'Share final fields';
25
26 /// The innermost loop containing a given primitive.
27 final Map<Primitive, Continuation> loopHeaderFor =
28 <Primitive, Continuation>{};
29
30 // field -> receiver -> GetField
31 Map<FieldElement, Map<Primitive, Primitive>> fieldValues =
32 <FieldElement, Map<Primitive, Primitive>>{};
33
34 /// Interceptors that have been hoisted out of a given loop.
35 final Map<Continuation, List<GetField>> loopHoistedGetters =
36 <Continuation, List<GetField>>{};
37
38 JavaScriptBackend backend;
39 LoopHierarchy loopHierarchy;
40 Continuation currentLoopHeader;
41
42 ShareFinalFields(this.backend);
43
44 void rewrite(FunctionDefinition node) {
45 loopHierarchy = new LoopHierarchy(node);
46 visit(node.body);
47 }
48
49 @override
50 Expression traverseContinuation(Continuation cont) {
51 Continuation oldLoopHeader = currentLoopHeader;
52 currentLoopHeader = loopHierarchy.getLoopHeader(cont);
53 for (Parameter parameter in cont.parameters) {
54 loopHeaderFor[parameter] = currentLoopHeader;
55 }
56 if (cont.isRecursive) {
57 pushAction(() {
58 // After the loop body has been processed, all values hoisted to this
59 // loop fall out of scope and should be removed from the environment.
60 List<GetField> hoisted = loopHoistedGetters[cont];
61 if (hoisted != null) {
62 for (GetField primitive in hoisted) {
63 Primitive refinedReceiver = primitive.object.definition;
64 Primitive receiver = refinedReceiver.effectiveDefinition;
65 var map = fieldValues[primitive.field];
66 assert(map[receiver] == primitive);
67 map.remove(receiver);
68 }
69 }
70 });
71 }
72 pushAction(() {
73 currentLoopHeader = oldLoopHeader;
74 });
75 return cont.body;
76 }
77
78 Continuation getCurrentOuterLoop({Continuation scope}) {
79 Continuation inner = null, outer = currentLoopHeader;
80 while (outer != scope) {
81 inner = outer;
82 outer = loopHierarchy.getEnclosingLoop(outer);
83 }
84 return inner;
85 }
86
87 @override
88 Expression traverseLetPrim(LetPrim node) {
89 loopHeaderFor[node.primitive] = currentLoopHeader;
90 Expression next = node.body;
91 if (node.primitive is! GetField) {
92 return next;
93 }
94 GetField primitive = node.primitive;
95 FieldElement field = primitive.field;
96
97 if (!shouldShareField(field)) {
98 return next;
99 }
100
101 Primitive refinedReceiver = primitive.object.definition;
102 Primitive receiver = refinedReceiver.effectiveDefinition;
103
104 // Try to reuse an existing load for the same input.
105 var map = fieldValues.putIfAbsent(field, () => <Primitive,Primitive>{});
106 Primitive existing = map[receiver];
107 if (existing != null) {
108 primitive..replaceUsesWith(existing)..destroy();
109 node.remove();
110 return next;
111 }
112
113 map[receiver] = primitive;
114
115 if (primitive.objectIsNotNull) {
116 // Determine how far the GetField can be lifted. The outermost loop that
117 // contains the input binding should also contain the load.
118 // Don't move above a refinement guard since that might be unsafe.
119
120 // TODO(sra): We can move above a refinement guard provided the input is
121 // still in scope and safe (non-null). We will have to replace
122 // primitive.object with the most constrained refinement still in scope.
123 Continuation referencedLoop =
124 lowestCommonAncestor(loopHeaderFor[refinedReceiver],
125 currentLoopHeader);
126 if (referencedLoop != currentLoopHeader) {
127 Continuation hoistTarget = getCurrentOuterLoop(scope: referencedLoop);
128 LetCont loopBinding = hoistTarget.parent;
129 node.remove();
130 node.insertAbove(loopBinding);
131 // Remove the hoisted operations from the environment after processing
132 // the loop.
133 loopHoistedGetters
134 .putIfAbsent(hoistTarget, () => <GetField>[])
135 .add(primitive);
136 return next;
137 }
138 }
139
140 pushAction(() {
141 var map = fieldValues[field];
142 assert(map[receiver] == primitive);
143 map.remove(receiver);
144 });
145 return next;
146 }
147
148 bool shouldShareField(FieldElement field) {
149 // TODO(24781): This query is incorrect for fields assigned only via
150 //
151 // super.field = ...
152 //
153 // return backend.compiler.world.fieldNeverChanges(field);
154
155 // Native fields are getters with side effects (e.g. layout).
156 if (backend.isNative(field)) return false;
157 return field.isFinal || field.isConst;
158 }
159
160 /// Returns the the innermost loop that effectively encloses both
161 /// c1 and c2 (or `null` if there is no such loop).
162 Continuation lowestCommonAncestor(Continuation c1, Continuation c2) {
163 int d1 = getDepth(c1), d2 = getDepth(c2);
164 while (c1 != c2) {
165 if (d1 <= d2) {
166 c2 = loopHierarchy.getEnclosingLoop(c2);
167 d2 = getDepth(c2);
168 } else {
169 c1 = loopHierarchy.getEnclosingLoop(c1);
170 d1 = getDepth(c1);
171 }
172 }
173 return c1;
174 }
175
176 int getDepth(Continuation loop) {
177 if (loop == null) return -1;
178 return loopHierarchy.loopDepth[loop];
179 }
180 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/cps_ir/redundant_refinement.dart ('k') | pkg/compiler/lib/src/cps_ir/share_interceptors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698