OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of ssa; | 5 part of ssa; |
6 | 6 |
7 /** | 7 /** |
8 * This phase simplifies interceptors in multiple ways: | 8 * This phase simplifies interceptors in multiple ways: |
9 * | 9 * |
10 * 1) If the interceptor is for an object whose type is known, it | 10 * 1) If the interceptor is for an object whose type is known, it |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 // use only the selector of that instruction. | 179 // use only the selector of that instruction. |
180 if (dominator != null) { | 180 if (dominator != null) { |
181 interceptedClasses = | 181 interceptedClasses = |
182 backend.getInterceptedClassesOn(dominator.selector.name); | 182 backend.getInterceptedClassesOn(dominator.selector.name); |
183 | 183 |
184 // If we found that we need number, we must still go through all | 184 // If we found that we need number, we must still go through all |
185 // uses to check if they require int, or double. | 185 // uses to check if they require int, or double. |
186 if (interceptedClasses.contains(backend.jsNumberClass) | 186 if (interceptedClasses.contains(backend.jsNumberClass) |
187 && !(interceptedClasses.contains(backend.jsDoubleClass) | 187 && !(interceptedClasses.contains(backend.jsDoubleClass) |
188 || interceptedClasses.contains(backend.jsIntClass))) { | 188 || interceptedClasses.contains(backend.jsIntClass))) { |
189 for (var user in node.usedBy) { | 189 for (HInstruction user in node.usedBy) { |
190 if (user is! HInvoke) continue; | 190 if (user is! HInvoke) continue; |
191 Set<ClassElement> intercepted = | 191 Set<ClassElement> intercepted = |
192 backend.getInterceptedClassesOn(user.selector.name); | 192 backend.getInterceptedClassesOn(user.selector.name); |
193 if (intercepted.contains(backend.jsIntClass)) { | 193 if (intercepted.contains(backend.jsIntClass)) { |
194 interceptedClasses.add(backend.jsIntClass); | 194 interceptedClasses.add(backend.jsIntClass); |
195 } | 195 } |
196 if (intercepted.contains(backend.jsDoubleClass)) { | 196 if (intercepted.contains(backend.jsDoubleClass)) { |
197 interceptedClasses.add(backend.jsDoubleClass); | 197 interceptedClasses.add(backend.jsDoubleClass); |
198 } | 198 } |
199 } | 199 } |
200 } | 200 } |
201 } else { | 201 } else { |
202 interceptedClasses = new Set<ClassElement>(); | 202 interceptedClasses = new Set<ClassElement>(); |
203 for (var user in node.usedBy) { | 203 for (HInstruction user in node.usedBy) { |
204 if (user is HIs) { | 204 if (user is HIs) { |
205 // Is-checks can be performed on any intercepted class. | 205 // Is-checks can be performed on any intercepted class. |
206 interceptedClasses.addAll(backend.interceptedClasses); | 206 interceptedClasses.addAll(backend.interceptedClasses); |
207 break; | 207 break; |
208 } | 208 } |
209 if (user is! HInvoke) continue; | 209 if (user is! HInvoke) continue; |
210 // We don't handle escaping interceptors yet. | 210 // We don't handle escaping interceptors yet. |
211 interceptedClasses.addAll( | 211 interceptedClasses.addAll( |
212 backend.getInterceptedClassesOn(user.selector.name)); | 212 backend.getInterceptedClassesOn(user.selector.name)); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 HInstruction receiver = node.receiver; | 216 HInstruction receiver = node.receiver; |
217 if (canUseSelfForInterceptor(receiver, interceptedClasses)) { | 217 if (canUseSelfForInterceptor(receiver, interceptedClasses)) { |
218 node.block.rewrite(node, receiver); | 218 return rewriteToUseSelfAsInterceptor(node, receiver); |
219 return false; | |
220 } | 219 } |
221 | 220 |
222 // Try computing a constant interceptor. | 221 // Try computing a constant interceptor. |
223 HInstruction constantInterceptor = | 222 HInstruction constantInterceptor = |
224 tryComputeConstantInterceptor(receiver, interceptedClasses); | 223 tryComputeConstantInterceptor(receiver, interceptedClasses); |
225 if (constantInterceptor != null) { | 224 if (constantInterceptor != null) { |
226 node.block.rewrite(node, constantInterceptor); | 225 node.block.rewrite(node, constantInterceptor); |
227 return false; | 226 return false; |
228 } | 227 } |
229 | 228 |
(...skipping 17 matching lines...) Expand all Loading... | |
247 interceptor.sourcePosition = user.sourcePosition; | 246 interceptor.sourcePosition = user.sourcePosition; |
248 interceptor.sourceElement = user.sourceElement; | 247 interceptor.sourceElement = user.sourceElement; |
249 | 248 |
250 HBasicBlock block = user.block; | 249 HBasicBlock block = user.block; |
251 block.addAfter(user, interceptor); | 250 block.addAfter(user, interceptor); |
252 block.rewrite(user, interceptor); | 251 block.rewrite(user, interceptor); |
253 block.remove(user); | 252 block.remove(user); |
254 return true; | 253 return true; |
255 } | 254 } |
256 | 255 |
256 bool rewriteToUseSelfAsInterceptor(HInterceptor node, HInstruction receiver) { | |
257 List<HInstruction> users = node.usedBy.toList(); | |
floitsch
2013/12/19 10:29:16
Add comment explaining that we need to get the lis
| |
258 | |
259 node.block.rewrite(node, receiver); | |
260 | |
261 // We have just rewritten: | |
262 // | |
263 // m = getInterceptor(a) | |
264 // m.foo$1(a, x) | |
265 // --> | |
266 // m = getInterceptor(a) | |
267 // a.foo$1(a, x) | |
268 // | |
269 // For the rewritten calls, if the selector matches only methods that ignore | |
270 // the explicit receiver parameter, replace occurences of the receiver | |
271 // argument with a dummy receiver '0': | |
272 // | |
273 // a.foo$1(a, x) --> a.foo$1(0, x) | |
274 // | |
275 JavaScriptBackend backend = compiler.backend; | |
276 for (HInstruction user in users) { | |
277 if (user is HInvokeDynamic) { | |
278 HInvokeDynamic invoke = user; | |
279 if (invoke.receiver == receiver && | |
280 !backend.isInterceptedMixinSelector(invoke.selector)) { | |
281 HInstruction receiverArgument = invoke.inputs[1]; | |
282 // TODO(15720): The test here should be | |
283 // | |
284 // invoke.receiver.nonCheck() == receiverArgument.nonCheck() | |
285 // | |
286 if (invoke.receiver == receiverArgument) { // recognize a.foo(a,...) | |
287 Constant constant = new DummyReceiverConstant( | |
288 receiverArgument.instructionType); | |
289 HConstant dummy = graph.addConstant(constant, compiler); | |
290 receiverArgument.usedBy.remove(invoke); | |
291 invoke.inputs[1] = dummy; | |
292 dummy.usedBy.add(invoke); | |
293 } | |
294 } | |
295 } | |
296 } | |
297 return false; | |
298 } | |
299 | |
257 bool visitOneShotInterceptor(HOneShotInterceptor node) { | 300 bool visitOneShotInterceptor(HOneShotInterceptor node) { |
258 HInstruction constant = tryComputeConstantInterceptor( | 301 HInstruction constant = tryComputeConstantInterceptor( |
259 node.inputs[1], node.interceptedClasses); | 302 node.inputs[1], node.interceptedClasses); |
260 | 303 |
261 if (constant == null) return false; | 304 if (constant == null) return false; |
262 | 305 |
263 Selector selector = node.selector; | 306 Selector selector = node.selector; |
264 HInstruction instruction; | 307 HInstruction instruction; |
265 if (selector.isGetter()) { | 308 if (selector.isGetter()) { |
266 instruction = new HInvokeDynamicGetter( | 309 instruction = new HInvokeDynamicGetter( |
(...skipping 13 matching lines...) Expand all Loading... | |
280 instruction = new HInvokeDynamicMethod( | 323 instruction = new HInvokeDynamicMethod( |
281 selector, inputs, node.instructionType, true); | 324 selector, inputs, node.instructionType, true); |
282 } | 325 } |
283 | 326 |
284 HBasicBlock block = node.block; | 327 HBasicBlock block = node.block; |
285 block.addAfter(node, instruction); | 328 block.addAfter(node, instruction); |
286 block.rewrite(node, instruction); | 329 block.rewrite(node, instruction); |
287 return true; | 330 return true; |
288 } | 331 } |
289 } | 332 } |
OLD | NEW |