Allow clinits that create a singleton instance to be postponed
Bug: 142762129
Change-Id: Iaa472dcf1b52dad304488ed3855fbceb16f20f9d
diff --git a/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java b/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java
index 223ff37..7de7a1f 100644
--- a/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java
+++ b/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java
@@ -12,6 +12,17 @@
B b = new InterfaceImpl();
b.foo();
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(a);
+ escape(b);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
public interface A {
diff --git a/src/test/examples/classmerging/MethodCollisionTest.java b/src/test/examples/classmerging/MethodCollisionTest.java
index a9010a4..4456498 100644
--- a/src/test/examples/classmerging/MethodCollisionTest.java
+++ b/src/test/examples/classmerging/MethodCollisionTest.java
@@ -7,8 +7,22 @@
public class MethodCollisionTest {
public static void main(String[] args) {
- new B().m();
- new D().m();
+ B b = new B();
+ b.m();
+
+ D d = new D();
+ d.m();
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(b);
+ escape(d);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
public static class A {
diff --git a/src/test/examples/classmerging/NeverInline.java b/src/test/examples/classmerging/NeverInline.java
new file mode 100644
index 0000000..9438902
--- /dev/null
+++ b/src/test/examples/classmerging/NeverInline.java
@@ -0,0 +1,10 @@
+// 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 classmerging;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD})
+public @interface NeverInline {}
diff --git a/src/test/examples/classmerging/SimpleInterfaceAccessTest.java b/src/test/examples/classmerging/SimpleInterfaceAccessTest.java
index ce7bf14..6fde90e 100644
--- a/src/test/examples/classmerging/SimpleInterfaceAccessTest.java
+++ b/src/test/examples/classmerging/SimpleInterfaceAccessTest.java
@@ -18,6 +18,17 @@
// package references OtherSimpleInterface.
OtherSimpleInterface y = new OtherSimpleInterfaceImpl();
y.bar();
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(x);
+ escape(y);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
// Should only be merged into OtherSimpleInterfaceImpl if access modifications are allowed.
diff --git a/src/test/examples/classmerging/SuperCallRewritingTest.java b/src/test/examples/classmerging/SuperCallRewritingTest.java
index 7a3d45c..6f59419 100644
--- a/src/test/examples/classmerging/SuperCallRewritingTest.java
+++ b/src/test/examples/classmerging/SuperCallRewritingTest.java
@@ -7,6 +7,17 @@
public class SuperCallRewritingTest {
public static void main(String[] args) {
System.out.println("Calling referencedMethod on SubClassThatReferencesSuperMethod");
- System.out.println(new SubClassThatReferencesSuperMethod().referencedMethod());
+ SubClassThatReferencesSuperMethod obj = new SubClassThatReferencesSuperMethod();
+ System.out.println(obj.referencedMethod());
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(obj);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
}
diff --git a/src/test/examples/classmerging/SyntheticBridgeSignaturesTest.java b/src/test/examples/classmerging/SyntheticBridgeSignaturesTest.java
index a17d30a..c263e98 100644
--- a/src/test/examples/classmerging/SyntheticBridgeSignaturesTest.java
+++ b/src/test/examples/classmerging/SyntheticBridgeSignaturesTest.java
@@ -15,6 +15,17 @@
BSub b = new BSub();
a.m(b);
b.m(a);
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(a);
+ escape(b);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
private static class A {
diff --git a/src/test/examples/classmerging/TemplateMethodTest.java b/src/test/examples/classmerging/TemplateMethodTest.java
index ac9de15..fe7de76 100644
--- a/src/test/examples/classmerging/TemplateMethodTest.java
+++ b/src/test/examples/classmerging/TemplateMethodTest.java
@@ -9,6 +9,16 @@
public static void main(String[] args) {
AbstractClass obj = new AbstractClassImpl();
obj.foo();
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(obj);
+ }
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
}
private abstract static class AbstractClass {
diff --git a/src/test/examples/classmerging/Test.java b/src/test/examples/classmerging/Test.java
index 6d5c51f..d3b2762 100644
--- a/src/test/examples/classmerging/Test.java
+++ b/src/test/examples/classmerging/Test.java
@@ -13,8 +13,17 @@
ConflictingInterfaceImpl impl = new ConflictingInterfaceImpl();
callMethodOnIface(impl);
System.out.println(new SubClassThatReferencesSuperMethod().referencedMethod());
- System.out.println(new Outer().getInstance().method());
+ Outer outer = new Outer();
+ Outer.SubClass inner = outer.getInstance();
+ System.out.println(outer.getInstance().method());
System.out.println(new SubClass(42));
+
+ // Ensure that the instantiations are not dead code eliminated.
+ escape(clazz);
+ escape(iface);
+ escape(impl);
+ escape(inner);
+ escape(outer);
}
private static void callMethodOnIface(GenericInterface iface) {
@@ -31,4 +40,11 @@
System.out.println(ClassWithConflictingMethod.conflict(null));
System.out.println(OtherClassWithConflictingMethod.conflict(null));
}
+
+ @NeverInline
+ static void escape(Object o) {
+ if (System.currentTimeMillis() < 0) {
+ System.out.println(o);
+ }
+ }
}
diff --git a/src/test/examples/classmerging/keep-rules.txt b/src/test/examples/classmerging/keep-rules.txt
index c75058d..11a66c6 100644
--- a/src/test/examples/classmerging/keep-rules.txt
+++ b/src/test/examples/classmerging/keep-rules.txt
@@ -68,4 +68,8 @@
public static void main(...);
}
+-neverinline class * {
+ @classmerging.NeverInline <methods>;
+}
+
-printmapping