| // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| package com.android.tools.r8.ir.optimize.callsites.nullability; |
| |
| import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent; |
| import static org.hamcrest.MatcherAssert.assertThat; |
| import static org.junit.Assert.assertTrue; |
| |
| import com.android.tools.r8.NeverClassInline; |
| import com.android.tools.r8.NeverInline; |
| import com.android.tools.r8.NeverMerge; |
| import com.android.tools.r8.TestBase; |
| import com.android.tools.r8.TestParameters; |
| import com.android.tools.r8.TestParametersCollection; |
| import com.android.tools.r8.graph.ProgramMethod; |
| import com.android.tools.r8.ir.analysis.type.TypeElement; |
| import com.android.tools.r8.ir.optimize.info.CallSiteOptimizationInfo; |
| import com.android.tools.r8.utils.codeinspector.ClassSubject; |
| import com.android.tools.r8.utils.codeinspector.CodeInspector; |
| import com.android.tools.r8.utils.codeinspector.InstructionSubject; |
| import com.android.tools.r8.utils.codeinspector.MethodSubject; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.Parameterized; |
| |
| @RunWith(Parameterized.class) |
| public class InvokeVirtualPositiveTest extends TestBase { |
| private static final Class<?> MAIN = Main.class; |
| |
| @Parameterized.Parameters(name = "{0}") |
| public static TestParametersCollection data() { |
| return getTestParameters().withAllRuntimesAndApiLevels().build(); |
| } |
| |
| private final TestParameters parameters; |
| |
| public InvokeVirtualPositiveTest(TestParameters parameters) { |
| this.parameters = parameters; |
| } |
| |
| @Test |
| public void testR8() throws Exception { |
| testForR8(parameters.getBackend()) |
| .addInnerClasses(InvokeVirtualPositiveTest.class) |
| .addKeepMainRule(MAIN) |
| .enableMergeAnnotations() |
| .enableNeverClassInliningAnnotations() |
| .enableInliningAnnotations() |
| .addOptionsModification( |
| o -> { |
| o.testing.callSiteOptimizationInfoInspector = this::callSiteOptimizationInfoInspect; |
| }) |
| .setMinApi(parameters.getApiLevel()) |
| .run(parameters.getRuntime(), MAIN) |
| .assertSuccessWithOutputLines("A", "null") |
| .inspect(this::inspect); |
| } |
| |
| private void callSiteOptimizationInfoInspect(ProgramMethod method) { |
| assert method.getReference().name.toString().equals("m") |
| : "Unexpected revisit: " + method.toSourceString(); |
| CallSiteOptimizationInfo callSiteOptimizationInfo = |
| method.getDefinition().getCallSiteOptimizationInfo(); |
| TypeElement upperBoundType = callSiteOptimizationInfo.getDynamicUpperBoundType(1); |
| assert upperBoundType.isClassType() |
| && upperBoundType.asClassType().getClassType().toSourceString().endsWith("$A"); |
| if (method.getHolderType().toSourceString().endsWith("$A")) { |
| assert upperBoundType.isDefinitelyNotNull(); |
| } else { |
| assert method.getHolderType().toSourceString().endsWith("$B"); |
| assert upperBoundType.isNullable(); |
| } |
| } |
| |
| private void inspect(CodeInspector inspector) { |
| ClassSubject a = inspector.clazz(A.class); |
| assertThat(a, isPresent()); |
| |
| MethodSubject a_m = a.uniqueMethodWithName("m"); |
| assertThat(a_m, isPresent()); |
| // Can optimize branches since `arg` is definitely not null. |
| assertTrue(a_m.streamInstructions().noneMatch(InstructionSubject::isIf)); |
| |
| ClassSubject b = inspector.clazz(B.class); |
| assertThat(b, isPresent()); |
| |
| MethodSubject b_m = b.uniqueMethodWithName("m"); |
| assertThat(b_m, isPresent()); |
| // Should not optimize branches since the nullability of `arg` is unsure. |
| assertTrue(b_m.streamInstructions().anyMatch(InstructionSubject::isIf)); |
| } |
| |
| @NeverMerge |
| @NeverClassInline |
| static class A { |
| @NeverInline |
| void m(Object arg) { |
| // Technically same as String#valueOf. |
| if (arg != null) { |
| System.out.println(arg.toString()); |
| } else { |
| System.out.println("null"); |
| } |
| } |
| |
| @NeverInline |
| @Override |
| public String toString() { |
| return "A"; |
| } |
| } |
| |
| @NeverClassInline |
| static class B extends A { |
| @NeverInline |
| @Override |
| void m(Object arg) { |
| // Same as A#m. |
| if (arg != null) { |
| System.out.println(arg.toString()); |
| } else { |
| System.out.println("null"); |
| } |
| } |
| |
| @NeverInline |
| @Override |
| public String toString() { |
| return "B"; |
| } |
| } |
| |
| static class Main { |
| public static void main(String... args) { |
| A a = System.currentTimeMillis() > 0 ? new A() : new B(); |
| a.m(a); // calls A.m() with non-null instance. |
| |
| A b = new B(); // with the exact type: |
| b.m(null); // calls B.m() with null. |
| } |
| } |
| } |