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

Side by Side Diff: pkg/compiler/lib/src/cps_ir/share_interceptors.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_interceptors;
6
7 import 'optimizers.dart';
8 import 'cps_ir_nodes.dart';
9 import 'loop_hierarchy.dart';
10 import 'cps_fragment.dart';
11 import '../constants/values.dart';
12 import '../elements/elements.dart';
13 import '../js_backend/backend_helpers.dart' show BackendHelpers;
14 import '../js_backend/js_backend.dart' show JavaScriptBackend;
15 import '../types/types.dart' show TypeMask;
16 import '../io/source_information.dart' show SourceInformation;
17
18 /// Removes redundant `getInterceptor` calls.
19 ///
20 /// The pass performs three optimizations for interceptors:
21 ///- pull interceptors out of loops
22 ///- replace interceptors with constants
23 ///- share interceptors when one is in scope of the other
24 class ShareInterceptors extends TrampolineRecursiveVisitor implements Pass {
25 String get passName => 'Share interceptors';
26
27 /// The innermost loop containing a given primitive.
28 final Map<Primitive, Continuation> loopHeaderFor =
29 <Primitive, Continuation>{};
30
31 /// An interceptor currently in scope for a given primitive.
32 final Map<Primitive, Interceptor> interceptorFor = <Primitive, Interceptor>{};
33
34 /// Interceptors that have been hoisted out of a given loop.
35 final Map<Continuation, List<Interceptor>> loopHoistedInterceptors =
36 <Continuation, List<Interceptor>>{};
37
38 JavaScriptBackend backend;
39 LoopHierarchy loopHierarchy;
40 Continuation currentLoopHeader;
41
42 ShareInterceptors(this.backend);
43
44 BackendHelpers get helpers => backend.helpers;
45
46 void rewrite(FunctionDefinition node) {
47 loopHierarchy = new LoopHierarchy(node);
48 visit(node.body);
49 new ShareConstants().visit(node);
50 }
51
52 @override
53 Expression traverseContinuation(Continuation cont) {
54 Continuation oldLoopHeader = currentLoopHeader;
55 currentLoopHeader = loopHierarchy.getLoopHeader(cont);
56 for (Parameter param in cont.parameters) {
57 loopHeaderFor[param] = currentLoopHeader;
58 }
59 if (cont.isRecursive) {
60 pushAction(() {
61 // After the loop body has been processed, all interceptors hoisted
62 // to this loop fall out of scope and should be removed from the
63 // environment.
64 List<Interceptor> hoisted = loopHoistedInterceptors[cont];
65 if (hoisted != null) {
66 for (Interceptor interceptor in hoisted) {
67 Primitive input = interceptor.input.definition;
68 assert(interceptorFor[input] == interceptor);
69 interceptorFor.remove(input);
70 constifyInterceptor(interceptor);
71 }
72 }
73 });
74 }
75 pushAction(() {
76 currentLoopHeader = oldLoopHeader;
77 });
78 return cont.body;
79 }
80
81 /// If only one method table can be returned by the given interceptor,
82 /// returns a constant for that method table.
83 InterceptorConstantValue getInterceptorConstant(Interceptor node) {
84 if (node.interceptedClasses.length == 1 &&
85 node.isInterceptedClassAlwaysExact) {
86 ClassElement interceptorClass = node.interceptedClasses.single;
87 return new InterceptorConstantValue(interceptorClass.rawType);
88 }
89 return null;
90 }
91
92 bool hasNoFalsyValues(ClassElement class_) {
93 return class_ != helpers.jsInterceptorClass &&
94 class_ != helpers.jsNullClass &&
95 class_ != helpers.jsBoolClass &&
96 class_ != helpers.jsStringClass &&
97 !class_.isSubclassOf(helpers.jsNumberClass);
98 }
99
100 Continuation getCurrentOuterLoop({Continuation scope}) {
101 Continuation inner = null, outer = currentLoopHeader;
102 while (outer != scope) {
103 inner = outer;
104 outer = loopHierarchy.getEnclosingLoop(outer);
105 }
106 return inner;
107 }
108
109 /// Binds the given constant in a primitive, in scope of the [useSite].
110 ///
111 /// The constant will be hoisted out of loops, and shared with other requests
112 /// for the same constant as long as it is in scope.
113 Primitive makeConstantFor(ConstantValue constant,
114 {Expression useSite,
115 TypeMask type,
116 SourceInformation sourceInformation,
117 Entity hint}) {
118 Constant prim =
119 new Constant(constant, sourceInformation: sourceInformation);
120 prim.hint = hint;
121 prim.type = type;
122 LetPrim letPrim = new LetPrim(prim);
123 Continuation loop = getCurrentOuterLoop();
124 if (loop != null) {
125 LetCont loopBinding = loop.parent;
126 letPrim.insertAbove(loopBinding);
127 } else {
128 letPrim.insertAbove(useSite);
129 }
130 return prim;
131 }
132
133 void constifyInterceptor(Interceptor interceptor) {
134 LetPrim let = interceptor.parent;
135 InterceptorConstantValue constant = getInterceptorConstant(interceptor);
136
137 if (constant == null) return;
138
139 if (interceptor.isAlwaysIntercepted) {
140 Primitive constantPrim = makeConstantFor(constant,
141 useSite: let,
142 type: interceptor.type,
143 sourceInformation: interceptor.sourceInformation);
144 constantPrim.useElementAsHint(interceptor.hint);
145 interceptor..replaceUsesWith(constantPrim)..destroy();
146 let.remove();
147 } else if (interceptor.isAlwaysNullOrIntercepted) {
148 Primitive input = interceptor.input.definition;
149 Primitive constantPrim = makeConstantFor(constant,
150 useSite: let,
151 type: interceptor.type.nonNullable(),
152 sourceInformation: interceptor.sourceInformation);
153 CpsFragment cps = new CpsFragment(interceptor.sourceInformation);
154 Parameter param = new Parameter(interceptor.hint);
155 Continuation cont = cps.letCont(<Parameter>[param]);
156 if (interceptor.interceptedClasses.every(hasNoFalsyValues)) {
157 // If null is the only falsy value, compile as "x && CONST".
158 cps.ifFalsy(input).invokeContinuation(cont, [input]);
159 } else {
160 // If there are other falsy values compile as "x == null ? x : CONST".
161 Primitive condition = cps.applyBuiltin(
162 BuiltinOperator.LooseEq,
163 [input, cps.makeNull()]);
164 cps.ifTruthy(condition).invokeContinuation(cont, [input]);
165 }
166 cps.invokeContinuation(cont, [constantPrim]);
167 cps.context = cont;
168 cps.insertAbove(let);
169 interceptor..replaceUsesWith(param)..destroy();
170 let.remove();
171 }
172 }
173
174 @override
175 Expression traverseLetPrim(LetPrim node) {
176 loopHeaderFor[node.primitive] = currentLoopHeader;
177 Expression next = node.body;
178 if (node.primitive is! Interceptor) {
179 return next;
180 }
181 Interceptor interceptor = node.primitive;
182 Primitive input = interceptor.input.definition;
183
184 // Try to reuse an existing interceptor for the same input.
185 Interceptor existing = interceptorFor[input];
186 if (existing != null) {
187 existing.interceptedClasses.addAll(interceptor.interceptedClasses);
188 existing.flags |= interceptor.flags;
189 interceptor..replaceUsesWith(existing)..destroy();
190 node.remove();
191 return next;
192 }
193
194 // Put this interceptor in the environment.
195 interceptorFor[input] = interceptor;
196
197 // Determine how far the interceptor can be lifted. The outermost loop
198 // that contains the input binding should also contain the interceptor
199 // binding.
200 Continuation referencedLoop =
201 lowestCommonAncestor(loopHeaderFor[input], currentLoopHeader);
202 if (referencedLoop != currentLoopHeader) {
203 Continuation hoistTarget = getCurrentOuterLoop(scope: referencedLoop);
204 LetCont loopBinding = hoistTarget.parent;
205 node.remove();
206 node.insertAbove(loopBinding);
207 // Remove the interceptor from the environment after processing the loop.
208 loopHoistedInterceptors
209 .putIfAbsent(hoistTarget, () => <Interceptor>[])
210 .add(interceptor);
211 } else {
212 // Remove the interceptor from the environment when it falls out of scope.
213 pushAction(() {
214 assert(interceptorFor[input] == interceptor);
215 interceptorFor.remove(input);
216
217 // Now that the final set of intercepted classes has been seen, try to
218 // replace it with a constant.
219 constifyInterceptor(interceptor);
220 });
221 }
222
223 return next;
224 }
225
226 /// Returns the the innermost loop that effectively encloses both
227 /// c1 and c2 (or `null` if there is no such loop).
228 Continuation lowestCommonAncestor(Continuation c1, Continuation c2) {
229 int d1 = getDepth(c1), d2 = getDepth(c2);
230 while (c1 != c2) {
231 if (d1 <= d2) {
232 c2 = loopHierarchy.getEnclosingLoop(c2);
233 d2 = getDepth(c2);
234 } else {
235 c1 = loopHierarchy.getEnclosingLoop(c1);
236 d1 = getDepth(c1);
237 }
238 }
239 return c1;
240 }
241
242 int getDepth(Continuation loop) {
243 if (loop == null) return -1;
244 return loopHierarchy.loopDepth[loop];
245 }
246 }
247
248 class ShareConstants extends TrampolineRecursiveVisitor {
249 Map<ConstantValue, Constant> sharedConstantFor = <ConstantValue, Constant>{};
250
251 Expression traverseLetPrim(LetPrim node) {
252 Expression next = node.body;
253 if (node.primitive is Constant && shouldShareConstant(node.primitive)) {
254 Constant prim = node.primitive;
255 Constant existing = sharedConstantFor[prim.value];
256 if (existing != null) {
257 existing.useElementAsHint(prim.hint);
258 prim..replaceUsesWith(existing)..destroy();
259 node.remove();
260 return next;
261 }
262 sharedConstantFor[prim.value] = prim;
263 pushAction(() {
264 assert(sharedConstantFor[prim.value] == prim);
265 sharedConstantFor.remove(prim.value);
266 });
267 }
268 return next;
269 }
270
271 bool shouldShareConstant(Constant constant) {
272 return constant.value.isInterceptor;
273 }
274 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/cps_ir/share_final_fields.dart ('k') | pkg/compiler/lib/src/cps_ir/type_mask_system.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698