roland@5991: /* roland@5991: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. roland@5991: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. roland@5991: * roland@5991: * This code is free software; you can redistribute it and/or modify it roland@5991: * under the terms of the GNU General Public License version 2 only, as roland@5991: * published by the Free Software Foundation. roland@5991: * roland@5991: * This code is distributed in the hope that it will be useful, but WITHOUT roland@5991: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or roland@5991: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License roland@5991: * version 2 for more details (a copy is included in the LICENSE file that roland@5991: * accompanied this code). roland@5991: * roland@5991: * You should have received a copy of the GNU General Public License version roland@5991: * 2 along with this work; if not, write to the Free Software Foundation, roland@5991: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. roland@5991: * roland@5991: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA roland@5991: * or visit www.oracle.com if you need additional information or have any roland@5991: * questions. roland@5991: */ roland@5991: roland@5991: /* roland@5991: * @test roland@5991: * @bug 8024070 roland@5991: * @summary Test that type speculation doesn't cause incorrect execution roland@6212: * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:TypeProfileLevel=222 -XX:+UnlockExperimentalVMOptions -XX:+UseTypeSpeculation TypeSpeculation roland@5991: * roland@5991: */ roland@5991: roland@5991: public class TypeSpeculation { roland@5991: roland@5991: interface I { roland@5991: } roland@5991: roland@5991: static class A { roland@5991: int m() { roland@5991: return 1; roland@5991: } roland@5991: } roland@5991: roland@5991: static class B extends A implements I { roland@5991: int m() { roland@5991: return 2; roland@5991: } roland@5991: } roland@5991: roland@5991: static class C extends B { roland@5991: int m() { roland@5991: return 3; roland@5991: } roland@5991: } roland@5991: roland@5991: static int test1_invokevirtual(A a) { roland@5991: return a.m(); roland@5991: } roland@5991: roland@5991: static int test1_1(A a) { roland@5991: return test1_invokevirtual(a); roland@5991: } roland@5991: roland@5991: static boolean test1() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: // pollute profile at test1_invokevirtual to make sure the roland@5991: // compiler cannot rely on it roland@5991: for (int i = 0; i < 5000; i++) { roland@5991: test1_invokevirtual(a); roland@5991: test1_invokevirtual(b); roland@5991: test1_invokevirtual(c); roland@5991: } roland@5991: roland@5991: // profiling + speculation should make test1_invokevirtual roland@5991: // inline A.m() with a guard roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: int res = test1_1(b); roland@5991: if (res != b.m()) { roland@5991: System.out.println("test1 failed with class B"); roland@5991: return false; roland@5991: } roland@5991: } roland@5991: // check that the guard works as expected by passing a roland@5991: // different type roland@5991: int res = test1_1(a); roland@5991: if (res != a.m()) { roland@5991: System.out.println("test1 failed with class A"); roland@5991: return false; roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: static int test2_invokevirtual(A a) { roland@5991: return a.m(); roland@5991: } roland@5991: roland@5991: static int test2_1(A a, boolean t) { roland@5991: A aa; roland@5991: if (t) { roland@5991: aa = (B)a; roland@5991: } else { roland@5991: aa = a; roland@5991: } roland@5991: // if a of type B is passed to test2_1, the static type of aa roland@5991: // here is no better than A but the profiled type is B so this roland@5991: // should inline roland@5991: return test2_invokevirtual(aa); roland@5991: } roland@5991: roland@5991: static boolean test2() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: // pollute profile at test2_invokevirtual to make sure the roland@5991: // compiler cannot rely on it roland@5991: for (int i = 0; i < 5000; i++) { roland@5991: test2_invokevirtual(a); roland@5991: test2_invokevirtual(b); roland@5991: test2_invokevirtual(c); roland@5991: } roland@5991: roland@5991: // profiling + speculation should make test2_invokevirtual roland@5991: // inline A.m() with a guard roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: int res = test2_1(b, (i % 2) == 0); roland@5991: if (res != b.m()) { roland@5991: System.out.println("test2 failed with class B"); roland@5991: return false; roland@5991: } roland@5991: } roland@5991: // check that the guard works as expected by passing a roland@5991: // different type roland@5991: int res = test2_1(a, false); roland@5991: if (res != a.m()) { roland@5991: System.out.println("test2 failed with class A"); roland@5991: return false; roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: static int test3_invokevirtual(A a) { roland@5991: return a.m(); roland@5991: } roland@5991: roland@5991: static void test3_2(A a) { roland@5991: } roland@5991: roland@5991: static int test3_1(A a, int i) { roland@5991: if (i == 0) { roland@5991: return 0; roland@5991: } roland@5991: // If we come here and a is of type B but parameter profiling roland@5991: // is polluted, both branches of the if below should have roland@5991: // profiling that tell us and inlining of the virtual call roland@5991: // should happen roland@5991: if (i == 1) { roland@5991: test3_2(a); roland@5991: } else { roland@5991: test3_2(a); roland@5991: } roland@5991: return test3_invokevirtual(a); roland@5991: } roland@5991: roland@5991: static boolean test3() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: // pollute profile at test3_invokevirtual and test3_1 to make roland@5991: // sure the compiler cannot rely on it roland@5991: for (int i = 0; i < 3000; i++) { roland@5991: test3_invokevirtual(a); roland@5991: test3_invokevirtual(b); roland@5991: test3_invokevirtual(c); roland@5991: test3_1(a, 0); roland@5991: test3_1(b, 0); roland@5991: } roland@5991: roland@5991: // profiling + speculation should make test3_invokevirtual roland@5991: // inline A.m() with a guard roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: int res = test3_1(b, (i % 2) + 1); roland@5991: if (res != b.m()) { roland@5991: System.out.println("test3 failed with class B"); roland@5991: return false; roland@5991: } roland@5991: } roland@5991: // check that the guard works as expected by passing a roland@5991: // different type roland@5991: int res = test3_1(a, 1); roland@5991: if (res != a.m()) { roland@5991: System.out.println("test3 failed with class A"); roland@5991: return false; roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix 2 incompatible profiled types roland@5991: static int test4_invokevirtual(A a) { roland@5991: return a.m(); roland@5991: } roland@5991: roland@5991: static void test4_2(A a) { roland@5991: } roland@5991: roland@5991: static int test4_1(A a, boolean b) { roland@5991: if (b) { roland@5991: test4_2(a); roland@5991: } else { roland@5991: test4_2(a); roland@5991: } roland@5991: // shouldn't inline roland@5991: return test4_invokevirtual(a); roland@5991: } roland@5991: roland@5991: static boolean test4() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: // pollute profile at test3_invokevirtual and test3_1 to make roland@5991: // sure the compiler cannot rely on it roland@5991: for (int i = 0; i < 3000; i++) { roland@5991: test4_invokevirtual(a); roland@5991: test4_invokevirtual(b); roland@5991: test4_invokevirtual(c); roland@5991: } roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: if ((i % 2) == 0) { roland@5991: int res = test4_1(a, true); roland@5991: if (res != a.m()) { roland@5991: System.out.println("test4 failed with class A"); roland@5991: return false; roland@5991: } roland@5991: } else { roland@5991: int res = test4_1(b, false); roland@5991: if (res != b.m()) { roland@5991: System.out.println("test4 failed with class B"); roland@5991: return false; roland@5991: } roland@5991: } roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix one profiled type with an incompatible type roland@5991: static int test5_invokevirtual(A a) { roland@5991: return a.m(); roland@5991: } roland@5991: roland@5991: static void test5_2(A a) { roland@5991: } roland@5991: roland@5991: static int test5_1(A a, boolean b) { roland@5991: if (b) { roland@5991: test5_2(a); roland@5991: } else { roland@5991: A aa = (B)a; roland@5991: } roland@5991: // shouldn't inline roland@5991: return test5_invokevirtual(a); roland@5991: } roland@5991: roland@5991: static boolean test5() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: // pollute profile at test3_invokevirtual and test3_1 to make roland@5991: // sure the compiler cannot rely on it roland@5991: for (int i = 0; i < 3000; i++) { roland@5991: test5_invokevirtual(a); roland@5991: test5_invokevirtual(b); roland@5991: test5_invokevirtual(c); roland@5991: } roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: if ((i % 2) == 0) { roland@5991: int res = test5_1(a, true); roland@5991: if (res != a.m()) { roland@5991: System.out.println("test5 failed with class A"); roland@5991: return false; roland@5991: } roland@5991: } else { roland@5991: int res = test5_1(b, false); roland@5991: if (res != b.m()) { roland@5991: System.out.println("test5 failed with class B"); roland@5991: return false; roland@5991: } roland@5991: } roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix incompatible profiled types roland@5991: static void test6_2(Object o) { roland@5991: } roland@5991: roland@5991: static Object test6_1(Object o, boolean b) { roland@5991: if (b) { roland@5991: test6_2(o); roland@5991: } else { roland@5991: test6_2(o); roland@5991: } roland@5991: return o; roland@5991: } roland@5991: roland@5991: static boolean test6() { roland@5991: A a = new A(); roland@5991: A[] aa = new A[10]; roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: if ((i % 2) == 0) { roland@5991: test6_1(a, true); roland@5991: } else { roland@5991: test6_1(aa, false); roland@5991: } roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix a profiled type with an incompatible type roland@5991: static void test7_2(Object o) { roland@5991: } roland@5991: roland@5991: static Object test7_1(Object o, boolean b) { roland@5991: if (b) { roland@5991: test7_2(o); roland@5991: } else { roland@5991: Object oo = (A[])o; roland@5991: } roland@5991: return o; roland@5991: } roland@5991: roland@5991: static boolean test7() { roland@5991: A a = new A(); roland@5991: A[] aa = new A[10]; roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: if ((i % 2) == 0) { roland@5991: test7_1(a, true); roland@5991: } else { roland@5991: test7_1(aa, false); roland@5991: } roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix a profiled type with an interface roland@5991: static void test8_2(Object o) { roland@5991: } roland@5991: roland@5991: static I test8_1(Object o) { roland@5991: test8_2(o); roland@5991: return (I)o; roland@5991: } roland@5991: roland@5991: static boolean test8() { roland@5991: A a = new A(); roland@5991: B b = new B(); roland@5991: C c = new C(); roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: test8_1(b); roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@5991: // Mix a profiled type with a constant roland@5991: static void test9_2(Object o) { roland@5991: } roland@5991: roland@5991: static Object test9_1(Object o, boolean b) { roland@5991: Object oo; roland@5991: if (b) { roland@5991: test9_2(o); roland@5991: oo = o; roland@5991: } else { roland@5991: oo = "some string"; roland@5991: } roland@5991: return oo; roland@5991: } roland@5991: roland@5991: static boolean test9() { roland@5991: A a = new A(); roland@5991: roland@5991: for (int i = 0; i < 20000; i++) { roland@5991: if ((i % 2) == 0) { roland@5991: test9_1(a, true); roland@5991: } else { roland@5991: test9_1(a, false); roland@5991: } roland@5991: } roland@5991: return true; roland@5991: } roland@5991: roland@6212: // java/lang/Object:AnyNull:exact *,iid=top roland@6212: // meets roland@6212: // stable:bottom[int:max..0]:NotNull * roland@6212: static void test10_4(Object o) { roland@6212: } roland@6212: roland@6212: static void test10_3(Object o, boolean b) { roland@6212: if (b) { roland@6212: test10_4(o); roland@6212: } roland@6212: } roland@6212: roland@6212: static void test10_2(Object o, boolean b1, boolean b2) { roland@6212: if (b1) { roland@6212: test10_3(o, b2); roland@6212: } roland@6212: } roland@6212: roland@6212: static void test10_1(B[] b, boolean b1, boolean b2) { roland@6212: test10_2(b, b1, b2); roland@6212: } roland@6212: roland@6212: static boolean test10() { roland@6212: Object o = new Object(); roland@6212: A[] a = new A[10]; roland@6212: B[] b = new B[10]; roland@6212: B[] c = new C[10]; roland@6212: for (int i = 0; i < 20000; i++) { roland@6212: test10_1(b, false, false); roland@6212: test10_1(c, false, false); roland@6212: test10_2(a, true, false); roland@6212: test10_3(o, true); roland@6212: } roland@6212: return true; roland@6212: } roland@6212: roland@6212: // stable:TypeSpeculation$B:TopPTR *,iid=top[int:max..0]:TopPTR *,iid=top roland@6212: // meets roland@6212: // java/lang/Object:AnyNull:exact *,iid=top roland@6212: static void test11_3(Object o) { roland@6212: } roland@6212: roland@6212: static void test11_2(Object o, boolean b) { roland@6212: if (b) { roland@6212: test11_3(o); roland@6212: } roland@6212: } roland@6212: roland@6212: static void test11_1(B[] b, boolean bb) { roland@6212: test11_2(b, bb); roland@6212: } roland@6212: roland@6212: static boolean test11() { roland@6212: Object o = new Object(); roland@6212: B[] b = new B[10]; roland@6212: B[] c = new C[10]; roland@6212: for (int i = 0; i < 20000; i++) { roland@6212: test11_1(b, false); roland@6212: test11_1(c, false); roland@6212: test11_2(o, true); roland@6212: } roland@6212: return true; roland@6212: } roland@6212: roland@6212: // TypeSpeculation$I * roland@6212: // meets roland@6212: // java/lang/Object:AnyNull *,iid=top roland@6212: static void test12_3(Object o) { roland@6212: } roland@6212: roland@6212: static void test12_2(Object o, boolean b) { roland@6212: if (b) { roland@6212: test12_3(o); roland@6212: } roland@6212: } roland@6212: roland@6212: static void test12_1(I i, boolean b) { roland@6212: test12_2(i, b); roland@6212: } roland@6212: roland@6212: static boolean test12() { roland@6212: Object o = new Object(); roland@6212: B b = new B(); roland@6212: C c = new C(); roland@6212: for (int i = 0; i < 20000; i++) { roland@6212: test12_1(b, false); roland@6212: test12_1(c, false); roland@6212: test12_2(o, true); roland@6212: } roland@6212: return true; roland@6212: } roland@6212: roland@6212: // stable:bottom[int:max..0]:NotNull * roland@6212: // meets roland@6212: // stable:TypeSpeculation$A:TopPTR *,iid=top[int:max..0]:AnyNull:exact *,iid=top roland@6212: static Object test13_3(Object o, boolean b) { roland@6212: Object oo; roland@6212: if (b) { roland@6212: oo = o; roland@6212: } else { roland@6212: oo = new A[10]; roland@6212: } roland@6212: return oo; roland@6212: } roland@6212: roland@6212: static void test13_2(Object o, boolean b1, boolean b2) { roland@6212: if (b1) { roland@6212: test13_3(o, b2); roland@6212: } roland@6212: } roland@6212: roland@6212: static void test13_1(B[] b, boolean b1, boolean b2) { roland@6212: test13_2(b, b1, b2); roland@6212: } roland@6212: roland@6212: static boolean test13() { roland@6212: A[] a = new A[10]; roland@6212: B[] b = new B[10]; roland@6212: B[] c = new C[10]; roland@6212: for (int i = 0; i < 20000; i++) { roland@6212: test13_1(b, false, false); roland@6212: test13_1(c, false, false); roland@6212: test13_2(a, true, (i%2) == 0); roland@6212: } roland@6212: return true; roland@6212: } roland@6212: roland@5991: static public void main(String[] args) { roland@5991: boolean success = true; roland@5991: roland@5991: success = test1() && success; roland@5991: roland@5991: success = test2() && success; roland@5991: roland@5991: success = test3() && success; roland@5991: roland@5991: success = test4() && success; roland@5991: roland@5991: success = test5() && success; roland@5991: roland@5991: success = test6() && success; roland@5991: roland@5991: success = test7() && success; roland@5991: roland@5991: success = test8() && success; roland@5991: roland@5991: success = test9() && success; roland@5991: roland@6212: success = test10() && success; roland@6212: roland@6212: success = test11() && success; roland@6212: roland@6212: success = test12() && success; roland@6212: roland@6212: success = test13() && success; roland@6212: roland@5991: if (success) { roland@5991: System.out.println("TEST PASSED"); roland@5991: } else { roland@5991: throw new RuntimeException("TEST FAILED: erroneous bound check elimination"); roland@5991: } roland@5991: } roland@5991: }