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.share_interceptors; | 5 library dart2js.cps_ir.share_interceptors; |
6 | 6 |
7 import 'optimizers.dart'; | 7 import 'optimizers.dart'; |
8 import 'cps_ir_nodes.dart'; | 8 import 'cps_ir_nodes.dart'; |
9 import 'loop_hierarchy.dart'; | 9 import 'loop_hierarchy.dart'; |
10 import 'cps_fragment.dart'; | |
10 import '../constants/values.dart'; | 11 import '../constants/values.dart'; |
12 import '../elements/elements.dart'; | |
13 import '../js_backend/js_backend.dart' show JavaScriptBackend; | |
14 import '../types/types.dart' show TypeMask; | |
15 import '../io/source_information.dart' show SourceInformation; | |
11 | 16 |
12 /// Removes redundant `getInterceptor` calls. | 17 /// Removes redundant `getInterceptor` calls. |
13 /// | 18 /// |
14 /// The pass performs three optimizations for interceptors: | 19 /// The pass performs three optimizations for interceptors: |
15 ///- pull interceptors out of loops | 20 ///- pull interceptors out of loops |
16 ///- replace interceptors with constants | 21 ///- replace interceptors with constants |
17 ///- share interceptors when one is in scope of the other | 22 ///- share interceptors when one is in scope of the other |
18 class ShareInterceptors extends TrampolineRecursiveVisitor implements Pass { | 23 class ShareInterceptors extends TrampolineRecursiveVisitor implements Pass { |
19 String get passName => 'Share interceptors'; | 24 String get passName => 'Share interceptors'; |
20 | 25 |
21 /// The innermost loop containing a given primitive. | 26 /// The innermost loop containing a given primitive. |
22 final Map<Primitive, Continuation> loopHeaderFor = | 27 final Map<Primitive, Continuation> loopHeaderFor = |
23 <Primitive, Continuation>{}; | 28 <Primitive, Continuation>{}; |
24 | 29 |
25 /// An interceptor currently in scope for a given primitive. | 30 /// An interceptor currently in scope for a given primitive or constant value. |
26 final Map<Primitive, Primitive> interceptorFor = | 31 final Map<dynamic, Primitive> interceptorFor = <dynamic, Primitive>{}; |
27 <Primitive, Primitive>{}; | |
28 | 32 |
29 /// A primitive currently in scope holding a given interceptor constant. | 33 /// Primitives and constant values that have been hoisted out of the given |
30 final Map<ConstantValue, Primitive> sharedConstantFor = | 34 /// loop. These are keys in [interceptorFor] that must be removed when we |
31 <ConstantValue, Primitive>{}; | 35 /// exit the loop. |
36 final Map<Continuation, List<dynamic>> loopHoistedInterceptors = | |
37 <Continuation, List<dynamic>>{}; | |
32 | 38 |
33 /// Interceptors to be hoisted out of the given loop. | 39 JavaScriptBackend backend; |
34 final Map<Continuation, List<Primitive>> loopHoistedInterceptors = | |
35 <Continuation, List<Primitive>>{}; | |
36 | |
37 LoopHierarchy loopHierarchy; | 40 LoopHierarchy loopHierarchy; |
38 Continuation currentLoopHeader; | 41 Continuation currentLoopHeader; |
39 | 42 |
43 ShareInterceptors(this.backend); | |
44 | |
40 void rewrite(FunctionDefinition node) { | 45 void rewrite(FunctionDefinition node) { |
41 loopHierarchy = new LoopHierarchy(node); | 46 loopHierarchy = new LoopHierarchy(node); |
42 visit(node.body); | 47 visit(node.body); |
43 } | 48 } |
44 | 49 |
45 @override | 50 @override |
46 Expression traverseContinuation(Continuation cont) { | 51 Expression traverseContinuation(Continuation cont) { |
47 Continuation oldLoopHeader = currentLoopHeader; | 52 Continuation oldLoopHeader = currentLoopHeader; |
48 pushAction(() { | 53 pushAction(() { |
49 currentLoopHeader = oldLoopHeader; | 54 currentLoopHeader = oldLoopHeader; |
50 }); | 55 }); |
51 currentLoopHeader = loopHierarchy.getLoopHeader(cont); | 56 currentLoopHeader = loopHierarchy.getLoopHeader(cont); |
52 for (Parameter param in cont.parameters) { | 57 for (Parameter param in cont.parameters) { |
53 loopHeaderFor[param] = currentLoopHeader; | 58 loopHeaderFor[param] = currentLoopHeader; |
54 } | 59 } |
55 if (cont.isRecursive) { | 60 if (cont.isRecursive) { |
56 pushAction(() { | 61 pushAction(() { |
57 // After the loop body has been processed, all interceptors hoisted | 62 // After the loop body has been processed, all interceptors hoisted |
58 // to this loop fall out of scope and should be removed from the | 63 // to this loop fall out of scope and should be removed from the |
59 // environment. | 64 // environment. |
60 List<Primitive> hoisted = loopHoistedInterceptors[cont]; | 65 List hoisted = loopHoistedInterceptors[cont]; |
61 if (hoisted != null) { | 66 if (hoisted != null) { |
62 for (Primitive interceptor in hoisted) { | 67 for (var input in hoisted) { |
63 if (interceptor is Interceptor) { | 68 assert(interceptorFor.containsKey(input)); |
64 Primitive input = interceptor.input.definition; | 69 interceptorFor.remove(input); |
65 assert(interceptorFor[input] == interceptor); | |
66 interceptorFor.remove(input); | |
67 } else if (interceptor is Constant) { | |
68 assert(sharedConstantFor[interceptor.value] == interceptor); | |
69 sharedConstantFor.remove(interceptor.value); | |
70 } else { | |
71 throw "Unexpected interceptor: $interceptor"; | |
72 } | |
73 } | 70 } |
74 } | 71 } |
75 }); | 72 }); |
76 } | 73 } |
77 return cont.body; | 74 return cont.body; |
78 } | 75 } |
79 | 76 |
77 /// If only one method table can be returned by the given interceptor, | |
78 /// returns a constant for that method table. | |
79 InterceptorConstantValue getInterceptorConstant(Interceptor node) { | |
80 if (node.interceptedClasses.length != 1) { | |
81 return null; | |
82 } | |
83 ClassElement interceptorClass = node.interceptedClasses.single; | |
84 if (Elements.isNativeOrExtendsNative(interceptorClass)) { | |
85 // There might exist self-intercepting subclasses of native classes. | |
sra1
2015/10/21 00:46:25
I don't think this is true. Custom elements shoul
sra1
2015/10/21 01:29:38
Specifically, this is an important regression:
asgerf
2015/10/21 11:52:46
Should be fixed now.
sra1
2015/10/21 21:46:56
I'm still seeing some cases of regressions:
| |
86 return null; | |
87 } | |
88 return new InterceptorConstantValue(interceptorClass.rawType); | |
89 } | |
90 | |
91 bool hasNoFalsyValues(ClassElement class_) { | |
92 return class_ != backend.jsInterceptorClass && | |
93 class_ != backend.jsNullClass && | |
94 class_ != backend.jsBoolClass && | |
95 class_ != backend.jsStringClass && | |
96 !class_.isSubclassOf(backend.jsNumberClass); | |
97 } | |
98 | |
99 Continuation getCurrentOuterLoop({Continuation scope}) { | |
100 Continuation inner = null, outer = currentLoopHeader; | |
101 while (outer != scope) { | |
102 inner = outer; | |
103 outer = loopHierarchy.getEnclosingLoop(outer); | |
104 } | |
105 return inner; | |
106 } | |
107 | |
108 /// Binds the given constant in a primitive, in scope of the [useSite]. | |
109 /// | |
110 /// The constant will be hoisted out of loops, and shared with other requests | |
111 /// for the same constant as long as it is in scope. | |
112 Primitive makeConstantFor(ConstantValue constant, | |
113 {Expression useSite, | |
114 TypeMask type, | |
115 SourceInformation sourceInformation, | |
116 Entity hint}) { | |
117 Constant prim = interceptorFor[constant]; | |
118 if (prim != null) { | |
119 return prim; | |
120 } | |
121 prim = new Constant(constant, sourceInformation: sourceInformation); | |
122 prim.hint = hint; | |
123 prim.type = type; | |
124 interceptorFor[constant] = prim; | |
125 LetPrim letPrim = new LetPrim(prim); | |
126 Continuation loop = getCurrentOuterLoop(); | |
127 if (loop != null) { | |
128 LetCont loopBinding = loop.parent; | |
129 letPrim.insertAbove(loopBinding); | |
130 loopHoistedInterceptors | |
131 .putIfAbsent(loop, () => []) | |
132 .add(constant); | |
133 } else { | |
134 letPrim.insertAbove(useSite); | |
135 pushAction(() { | |
136 assert(interceptorFor[constant] == prim); | |
137 interceptorFor.remove(constant); | |
138 }); | |
139 } | |
140 return prim; | |
141 } | |
142 | |
80 @override | 143 @override |
81 Expression traverseLetPrim(LetPrim node) { | 144 Expression traverseLetPrim(LetPrim node) { |
82 loopHeaderFor[node.primitive] = currentLoopHeader; | 145 loopHeaderFor[node.primitive] = currentLoopHeader; |
83 Expression next = node.body; | 146 Expression next = node.body; |
84 if (node.primitive is! Interceptor) { | 147 if (node.primitive is! Interceptor) { |
85 return next; | 148 return next; |
86 } | 149 } |
87 Interceptor interceptor = node.primitive; | 150 Interceptor interceptor = node.primitive; |
88 Primitive input = interceptor.input.definition; | 151 Primitive input = interceptor.input.definition; |
89 | 152 |
90 // Try to reuse an existing interceptor for the same input. | 153 // Try to reuse an existing interceptor for the same input. |
91 Primitive existing = interceptorFor[input]; | 154 Primitive existing = interceptorFor[input]; |
92 if (existing != null) { | 155 if (existing != null) { |
93 if (existing is Interceptor) { | 156 if (existing is Interceptor) { |
94 existing.interceptedClasses.addAll(interceptor.interceptedClasses); | 157 existing.interceptedClasses.addAll(interceptor.interceptedClasses); |
95 } | 158 } |
96 existing.substituteFor(interceptor); | 159 existing.substituteFor(interceptor); |
97 interceptor.destroy(); | 160 interceptor.destroy(); |
98 node.remove(); | 161 node.remove(); |
99 return next; | 162 return next; |
100 } | 163 } |
101 | 164 |
102 // There is no interceptor obtained from this particular input, but | 165 // There is no interceptor obtained from this particular input, but |
103 // there might one obtained from another input that is known to | 166 // there might one obtained from another input that is known to |
104 // have the same result, so try to reuse that. | 167 // have the same result, so try to reuse that. |
105 InterceptorConstantValue constant = interceptor.constantValue; | 168 InterceptorConstantValue constant = getInterceptorConstant(interceptor); |
106 if (constant != null) { | 169 if (constant != null && interceptor.isAlwaysIntercepted) { |
107 existing = sharedConstantFor[constant]; | 170 Primitive constantPrim = makeConstantFor(constant, |
108 if (existing != null) { | 171 useSite: node, |
109 existing.substituteFor(interceptor); | 172 type: interceptor.type, |
110 interceptor.destroy(); | 173 sourceInformation: interceptor.sourceInformation); |
111 node.remove(); | 174 constantPrim.useElementAsHint(interceptor.hint); |
112 return next; | |
113 } | |
114 | |
115 // The interceptor could not be shared. Replace it with a constant. | |
116 Constant constantPrim = new Constant(constant); | |
117 node.primitive = constantPrim; | |
118 constantPrim.parent = node; | |
119 constantPrim.hint = interceptor.hint; | |
120 constantPrim.type = interceptor.type; | |
121 constantPrim.substituteFor(interceptor); | 175 constantPrim.substituteFor(interceptor); |
122 interceptor.destroy(); | 176 interceptor.destroy(); |
123 sharedConstantFor[constant] = constantPrim; | 177 node.remove(); |
124 } else { | 178 return next; |
125 interceptorFor[input] = interceptor; | |
126 } | 179 } |
127 | 180 |
128 // Determine the outermost loop where the input to the interceptor call | 181 Primitive interceptorValue = interceptor; |
129 // is available. Constant interceptors take no input and can thus be | 182 |
130 // hoisted all way to the top-level. | 183 // Determine how far the interceptor can be lifted. The outermost loop |
131 Continuation referencedLoop = constant != null | 184 // that contains the input binding should also contain the interceptor |
132 ? null | 185 // binding. |
133 : lowestCommonAncestor(loopHeaderFor[input], currentLoopHeader); | 186 Expression insertionPoint = node; |
187 Continuation hoistTarget; | |
188 Continuation referencedLoop = | |
189 lowestCommonAncestor(loopHeaderFor[input], currentLoopHeader); | |
134 if (referencedLoop != currentLoopHeader) { | 190 if (referencedLoop != currentLoopHeader) { |
135 // [referencedLoop] contains the binding for [input], so we cannot hoist | 191 hoistTarget = getCurrentOuterLoop(scope: referencedLoop); |
136 // the interceptor outside that loop. Find the loop nested one level | 192 LetCont letCont = hoistTarget.parent; |
137 // inside referencedLoop, and hoist the interceptor just outside that one. | 193 insertionPoint = letCont; |
138 Continuation loop = currentLoopHeader; | 194 } |
139 Continuation enclosing = loopHierarchy.getEnclosingLoop(loop); | 195 |
140 while (enclosing != referencedLoop) { | 196 // If the interceptor is either a constant or null, replace the expression |
141 assert(loop != null); | 197 // with a constant guarded by a null-check. |
142 loop = enclosing; | 198 if (constant != null && interceptor.isAlwaysNullOrIntercepted) { |
143 enclosing = loopHierarchy.getEnclosingLoop(loop); | 199 Primitive constantPrim = makeConstantFor(constant, |
200 useSite: node, | |
201 type: interceptor.type.nonNullable(), | |
202 sourceInformation: interceptor.sourceInformation); | |
203 CpsFragment cps = new CpsFragment(interceptor.sourceInformation); | |
204 Parameter param = new Parameter(interceptor.hint); | |
205 Continuation cont = cps.letCont(<Parameter>[param]); | |
206 if (interceptor.interceptedClasses.every(hasNoFalsyValues)) { | |
207 // If null is the only falsy value, compile as "x && CONST". | |
208 cps.ifFalsy(input).invokeContinuation(cont, [input]); | |
209 } else { | |
210 // If there are other falsy values compile as "x == null ? x : CONST". | |
211 Primitive condition = cps.applyBuiltin( | |
212 BuiltinOperator.LooseEq, | |
213 [input, cps.makeNull()]); | |
214 cps.ifTruthy(condition).invokeContinuation(cont, [input]); | |
144 } | 215 } |
145 assert(loop != null); | 216 cps.invokeContinuation(cont, [constantPrim]); |
217 cps.context = cont; | |
218 cps.insertAbove(insertionPoint); | |
219 param.substituteFor(interceptor); | |
220 interceptor.destroy(); | |
221 node.remove(); | |
222 interceptorValue = param; | |
223 } else if (hoistTarget != null) { | |
224 // No constify optimization applied, but we can hoist the interceptor | |
225 // call out of the loop. | |
226 node.remove(); | |
227 node.insertAbove(insertionPoint); | |
228 } | |
146 | 229 |
147 // Move the LetPrim above the loop binding. | 230 // Put the interceptor in the environment, and make sure it gets removed |
148 LetCont loopBinding = loop.parent; | 231 // when the binding falls out of scope. |
149 node.remove(); | 232 interceptorFor[input] = interceptorValue; |
150 node.insertAbove(loopBinding); | 233 if (hoistTarget != null) { |
151 | |
152 // A different loop now contains the interceptor. | |
153 loopHeaderFor[node.primitive] = enclosing; | |
154 | |
155 // Register the interceptor as hoisted to that loop, so it will be | |
156 // removed from the environment when it falls out of scope. | |
157 loopHoistedInterceptors | 234 loopHoistedInterceptors |
158 .putIfAbsent(loop, () => <Primitive>[]) | 235 .putIfAbsent(hoistTarget, () => []) |
159 .add(node.primitive); | 236 .add(input); |
160 } else if (constant != null) { | |
161 // The LetPrim was not hoisted. Remove the bound interceptor from the | |
162 // environment when leaving the LetPrim body. | |
163 pushAction(() { | |
164 assert(sharedConstantFor[constant] == node.primitive); | |
165 sharedConstantFor.remove(constant); | |
166 }); | |
167 } else { | 237 } else { |
168 pushAction(() { | 238 pushAction(() { |
169 assert(interceptorFor[input] == node.primitive); | 239 assert(interceptorFor[input] == interceptorValue); |
170 interceptorFor.remove(input); | 240 interceptorFor.remove(input); |
171 }); | 241 }); |
172 } | 242 } |
243 | |
173 return next; | 244 return next; |
174 } | 245 } |
175 | 246 |
176 /// Returns the the innermost loop that effectively encloses both | 247 /// Returns the the innermost loop that effectively encloses both |
177 /// c1 and c2 (or `null` if there is no such loop). | 248 /// c1 and c2 (or `null` if there is no such loop). |
178 Continuation lowestCommonAncestor(Continuation c1, Continuation c2) { | 249 Continuation lowestCommonAncestor(Continuation c1, Continuation c2) { |
179 int d1 = getDepth(c1), d2 = getDepth(c2); | 250 int d1 = getDepth(c1), d2 = getDepth(c2); |
180 while (c1 != c2) { | 251 while (c1 != c2) { |
181 if (d1 <= d2) { | 252 if (d1 <= d2) { |
182 c2 = loopHierarchy.getEnclosingLoop(c2); | 253 c2 = loopHierarchy.getEnclosingLoop(c2); |
183 d2 = getDepth(c2); | 254 d2 = getDepth(c2); |
184 } else { | 255 } else { |
185 c1 = loopHierarchy.getEnclosingLoop(c1); | 256 c1 = loopHierarchy.getEnclosingLoop(c1); |
186 d1 = getDepth(c1); | 257 d1 = getDepth(c1); |
187 } | 258 } |
188 } | 259 } |
189 return c1; | 260 return c1; |
190 } | 261 } |
191 | 262 |
192 int getDepth(Continuation loop) { | 263 int getDepth(Continuation loop) { |
193 if (loop == null) return -1; | 264 if (loop == null) return -1; |
194 return loopHierarchy.loopDepth[loop]; | 265 return loopHierarchy.loopDepth[loop]; |
195 } | 266 } |
196 } | 267 } |
OLD | NEW |