blob: 05d1329ec30ebafca17dbb90a001e3fa537944fb [file] [log] [blame]
// 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.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.graph.ProgramMethod;
import com.android.tools.r8.utils.BooleanUtils;
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 java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class InvokeDirectNegativeTest extends TestBase {
private static final Class<?> MAIN = Main.class;
@Parameters(name = "{1}, experimental: {0}")
public static List<Object[]> data() {
return buildParameters(
BooleanUtils.values(), getTestParameters().withAllRuntimesAndApiLevels().build());
}
private final boolean enableExperimentalArgumentPropagation;
private final TestParameters parameters;
public InvokeDirectNegativeTest(
boolean enableExperimentalArgumentPropagation, TestParameters parameters) {
this.enableExperimentalArgumentPropagation = enableExperimentalArgumentPropagation;
this.parameters = parameters;
}
@Test
public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addInnerClasses(InvokeDirectNegativeTest.class)
.addKeepMainRule(MAIN)
.enableNeverClassInliningAnnotations()
.enableInliningAnnotations()
.addOptionsModification(
o -> {
o.testing.callSiteOptimizationInfoInspector = this::callSiteOptimizationInfoInspect;
o.callSiteOptimizationOptions()
.setEnableExperimentalArgumentPropagation(enableExperimentalArgumentPropagation);
})
.setMinApi(parameters.getApiLevel())
.run(parameters.getRuntime(), MAIN)
.assertSuccessWithOutputLines("null", "non-null")
.inspect(this::inspect);
}
private void callSiteOptimizationInfoInspect(ProgramMethod method) {
assert false : "Unexpected revisit: " + method.toSourceString();
}
private void inspect(CodeInspector inspector) {
ClassSubject main = inspector.clazz(MAIN);
assertThat(main, isPresent());
MethodSubject test = main.uniqueMethodWithName("test");
assertThat(test, isPresent());
// Should not optimize branches since the nullability of `arg` is unsure.
assertTrue(test.streamInstructions().anyMatch(InstructionSubject::isIf));
}
@NeverClassInline
static class Main {
public static void main(String... args) {
Main obj = new Main();
obj.test(null); // calls test with null.
obj.test(new Object()); // calls test with non-null instance.
}
@NeverInline
private void test(Object arg) {
if (arg != null) {
System.out.println("non-null");
} else {
System.out.println("null");
}
}
}
}