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 (HInstruction user in node.usedBy) { | 189 for (var 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 (HInstruction user in node.usedBy) { | 203 for (var 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 return rewriteToUseSelfAsInterceptor(node, receiver); | 218 node.block.rewrite(node, receiver); |
| 219 return false; |
219 } | 220 } |
220 | 221 |
221 // Try computing a constant interceptor. | 222 // Try computing a constant interceptor. |
222 HInstruction constantInterceptor = | 223 HInstruction constantInterceptor = |
223 tryComputeConstantInterceptor(receiver, interceptedClasses); | 224 tryComputeConstantInterceptor(receiver, interceptedClasses); |
224 if (constantInterceptor != null) { | 225 if (constantInterceptor != null) { |
225 node.block.rewrite(node, constantInterceptor); | 226 node.block.rewrite(node, constantInterceptor); |
226 return false; | 227 return false; |
227 } | 228 } |
228 | 229 |
(...skipping 17 matching lines...) Expand all Loading... |
246 interceptor.sourcePosition = user.sourcePosition; | 247 interceptor.sourcePosition = user.sourcePosition; |
247 interceptor.sourceElement = user.sourceElement; | 248 interceptor.sourceElement = user.sourceElement; |
248 | 249 |
249 HBasicBlock block = user.block; | 250 HBasicBlock block = user.block; |
250 block.addAfter(user, interceptor); | 251 block.addAfter(user, interceptor); |
251 block.rewrite(user, interceptor); | 252 block.rewrite(user, interceptor); |
252 block.remove(user); | 253 block.remove(user); |
253 return true; | 254 return true; |
254 } | 255 } |
255 | 256 |
256 bool rewriteToUseSelfAsInterceptor(HInterceptor node, HInstruction receiver) { | |
257 // `rewrite` below clears `node.usedBy`. | |
258 List<HInstruction> originalUsers = node.usedBy.toList(); | |
259 | |
260 node.block.rewrite(node, receiver); | |
261 | |
262 // We have just rewritten: | |
263 // | |
264 // m = getInterceptor(a) | |
265 // m.foo$1(a, x) | |
266 // --> | |
267 // m = getInterceptor(a) | |
268 // a.foo$1(a, x) | |
269 // | |
270 // For the rewritten calls, if the selector matches only methods that ignore | |
271 // the explicit receiver parameter, replace occurences of the receiver | |
272 // argument with a dummy receiver '0': | |
273 // | |
274 // a.foo$1(a, x) --> a.foo$1(0, x) | |
275 // | |
276 JavaScriptBackend backend = compiler.backend; | |
277 for (HInstruction user in originalUsers) { | |
278 if (user is HInvokeDynamic) { | |
279 HInvokeDynamic invoke = user; | |
280 if (invoke.receiver == receiver && | |
281 !backend.isInterceptedMixinSelector(invoke.selector)) { | |
282 HInstruction receiverArgument = invoke.inputs[1]; | |
283 // TODO(15720): The test here should be | |
284 // | |
285 // invoke.receiver.nonCheck() == receiverArgument.nonCheck() | |
286 // | |
287 if (invoke.receiver == receiverArgument) { // recognize a.foo(a,...) | |
288 Constant constant = new DummyReceiverConstant( | |
289 receiverArgument.instructionType); | |
290 HConstant dummy = graph.addConstant(constant, compiler); | |
291 receiverArgument.usedBy.remove(invoke); | |
292 invoke.inputs[1] = dummy; | |
293 dummy.usedBy.add(invoke); | |
294 } | |
295 } | |
296 } | |
297 } | |
298 return false; | |
299 } | |
300 | |
301 bool visitOneShotInterceptor(HOneShotInterceptor node) { | 257 bool visitOneShotInterceptor(HOneShotInterceptor node) { |
302 HInstruction constant = tryComputeConstantInterceptor( | 258 HInstruction constant = tryComputeConstantInterceptor( |
303 node.inputs[1], node.interceptedClasses); | 259 node.inputs[1], node.interceptedClasses); |
304 | 260 |
305 if (constant == null) return false; | 261 if (constant == null) return false; |
306 | 262 |
307 Selector selector = node.selector; | 263 Selector selector = node.selector; |
308 HInstruction instruction; | 264 HInstruction instruction; |
309 if (selector.isGetter()) { | 265 if (selector.isGetter()) { |
310 instruction = new HInvokeDynamicGetter( | 266 instruction = new HInvokeDynamicGetter( |
(...skipping 13 matching lines...) Expand all Loading... |
324 instruction = new HInvokeDynamicMethod( | 280 instruction = new HInvokeDynamicMethod( |
325 selector, inputs, node.instructionType, true); | 281 selector, inputs, node.instructionType, true); |
326 } | 282 } |
327 | 283 |
328 HBasicBlock block = node.block; | 284 HBasicBlock block = node.block; |
329 block.addAfter(node, instruction); | 285 block.addAfter(node, instruction); |
330 block.rewrite(node, instruction); | 286 block.rewrite(node, instruction); |
331 return true; | 287 return true; |
332 } | 288 } |
333 } | 289 } |
OLD | NEW |