test/compiler/types/TypeSpeculation.java

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/types/TypeSpeculation.java	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,563 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8024070
    1.30 + * @summary Test that type speculation doesn't cause incorrect execution
    1.31 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation TypeSpeculation
    1.32 + *
    1.33 + */
    1.34 +
    1.35 +public class TypeSpeculation {
    1.36 +
    1.37 +    interface I {
    1.38 +    }
    1.39 +
    1.40 +    static class A {
    1.41 +        int m() {
    1.42 +            return 1;
    1.43 +        }
    1.44 +    }
    1.45 +
    1.46 +    static class B extends A implements I {
    1.47 +        int m() {
    1.48 +            return 2;
    1.49 +        }
    1.50 +    }
    1.51 +
    1.52 +    static class C extends B {
    1.53 +        int m() {
    1.54 +            return 3;
    1.55 +        }
    1.56 +    }
    1.57 +
    1.58 +    static int test1_invokevirtual(A a) {
    1.59 +        return a.m();
    1.60 +    }
    1.61 +
    1.62 +    static int test1_1(A a) {
    1.63 +        return test1_invokevirtual(a);
    1.64 +    }
    1.65 +
    1.66 +    static boolean test1() {
    1.67 +        A a = new A();
    1.68 +        B b = new B();
    1.69 +        C c = new C();
    1.70 +
    1.71 +        // pollute profile at test1_invokevirtual to make sure the
    1.72 +        // compiler cannot rely on it
    1.73 +        for (int i = 0; i < 5000; i++) {
    1.74 +            test1_invokevirtual(a);
    1.75 +            test1_invokevirtual(b);
    1.76 +            test1_invokevirtual(c);
    1.77 +        }
    1.78 +
    1.79 +        // profiling + speculation should make test1_invokevirtual
    1.80 +        // inline A.m() with a guard
    1.81 +        for (int i = 0; i < 20000; i++) {
    1.82 +            int res = test1_1(b);
    1.83 +            if (res != b.m()) {
    1.84 +                System.out.println("test1 failed with class B");
    1.85 +                return false;
    1.86 +            }
    1.87 +        }
    1.88 +        // check that the guard works as expected by passing a
    1.89 +        // different type
    1.90 +        int res = test1_1(a);
    1.91 +        if (res != a.m()) {
    1.92 +            System.out.println("test1 failed with class A");
    1.93 +            return false;
    1.94 +        }
    1.95 +        return true;
    1.96 +    }
    1.97 +
    1.98 +    static int test2_invokevirtual(A a) {
    1.99 +        return a.m();
   1.100 +    }
   1.101 +
   1.102 +    static int test2_1(A a, boolean t) {
   1.103 +        A aa;
   1.104 +        if (t) {
   1.105 +            aa = (B)a;
   1.106 +        } else {
   1.107 +            aa = a;
   1.108 +        }
   1.109 +        // if a of type B is passed to test2_1, the static type of aa
   1.110 +        // here is no better than A but the profiled type is B so this
   1.111 +        // should inline
   1.112 +        return test2_invokevirtual(aa);
   1.113 +    }
   1.114 +
   1.115 +    static boolean test2() {
   1.116 +        A a = new A();
   1.117 +        B b = new B();
   1.118 +        C c = new C();
   1.119 +
   1.120 +        // pollute profile at test2_invokevirtual to make sure the
   1.121 +        // compiler cannot rely on it
   1.122 +        for (int i = 0; i < 5000; i++) {
   1.123 +            test2_invokevirtual(a);
   1.124 +            test2_invokevirtual(b);
   1.125 +            test2_invokevirtual(c);
   1.126 +        }
   1.127 +
   1.128 +        // profiling + speculation should make test2_invokevirtual
   1.129 +        // inline A.m() with a guard
   1.130 +        for (int i = 0; i < 20000; i++) {
   1.131 +            int res = test2_1(b, (i % 2) == 0);
   1.132 +            if (res != b.m()) {
   1.133 +                System.out.println("test2 failed with class B");
   1.134 +                return false;
   1.135 +            }
   1.136 +        }
   1.137 +        // check that the guard works as expected by passing a
   1.138 +        // different type
   1.139 +        int res = test2_1(a, false);
   1.140 +        if (res != a.m()) {
   1.141 +            System.out.println("test2 failed with class A");
   1.142 +            return false;
   1.143 +        }
   1.144 +        return true;
   1.145 +    }
   1.146 +
   1.147 +    static int test3_invokevirtual(A a) {
   1.148 +        return a.m();
   1.149 +    }
   1.150 +
   1.151 +    static void test3_2(A a) {
   1.152 +    }
   1.153 +
   1.154 +    static int test3_1(A a, int i) {
   1.155 +        if (i == 0) {
   1.156 +            return 0;
   1.157 +        }
   1.158 +        // If we come here and a is of type B but parameter profiling
   1.159 +        // is polluted, both branches of the if below should have
   1.160 +        // profiling that tell us and inlining of the virtual call
   1.161 +        // should happen
   1.162 +        if (i == 1) {
   1.163 +            test3_2(a);
   1.164 +        } else {
   1.165 +            test3_2(a);
   1.166 +        }
   1.167 +        return test3_invokevirtual(a);
   1.168 +    }
   1.169 +
   1.170 +    static boolean test3() {
   1.171 +        A a = new A();
   1.172 +        B b = new B();
   1.173 +        C c = new C();
   1.174 +
   1.175 +        // pollute profile at test3_invokevirtual and test3_1 to make
   1.176 +        // sure the compiler cannot rely on it
   1.177 +        for (int i = 0; i < 3000; i++) {
   1.178 +            test3_invokevirtual(a);
   1.179 +            test3_invokevirtual(b);
   1.180 +            test3_invokevirtual(c);
   1.181 +            test3_1(a, 0);
   1.182 +            test3_1(b, 0);
   1.183 +        }
   1.184 +
   1.185 +        // profiling + speculation should make test3_invokevirtual
   1.186 +        // inline A.m() with a guard
   1.187 +        for (int i = 0; i < 20000; i++) {
   1.188 +            int res = test3_1(b, (i % 2) + 1);
   1.189 +            if (res != b.m()) {
   1.190 +                System.out.println("test3 failed with class B");
   1.191 +                return false;
   1.192 +            }
   1.193 +        }
   1.194 +        // check that the guard works as expected by passing a
   1.195 +        // different type
   1.196 +        int res = test3_1(a, 1);
   1.197 +        if (res != a.m()) {
   1.198 +            System.out.println("test3 failed with class A");
   1.199 +            return false;
   1.200 +        }
   1.201 +        return true;
   1.202 +    }
   1.203 +
   1.204 +    // Mix 2 incompatible profiled types
   1.205 +    static int test4_invokevirtual(A a) {
   1.206 +        return a.m();
   1.207 +    }
   1.208 +
   1.209 +    static void test4_2(A a) {
   1.210 +    }
   1.211 +
   1.212 +    static int test4_1(A a, boolean b) {
   1.213 +        if (b) {
   1.214 +            test4_2(a);
   1.215 +        } else {
   1.216 +            test4_2(a);
   1.217 +        }
   1.218 +        // shouldn't inline
   1.219 +        return test4_invokevirtual(a);
   1.220 +    }
   1.221 +
   1.222 +    static boolean test4() {
   1.223 +        A a = new A();
   1.224 +        B b = new B();
   1.225 +        C c = new C();
   1.226 +
   1.227 +        // pollute profile at test3_invokevirtual and test3_1 to make
   1.228 +        // sure the compiler cannot rely on it
   1.229 +        for (int i = 0; i < 3000; i++) {
   1.230 +            test4_invokevirtual(a);
   1.231 +            test4_invokevirtual(b);
   1.232 +            test4_invokevirtual(c);
   1.233 +        }
   1.234 +
   1.235 +        for (int i = 0; i < 20000; i++) {
   1.236 +            if ((i % 2) == 0) {
   1.237 +                int res = test4_1(a, true);
   1.238 +                if (res != a.m()) {
   1.239 +                    System.out.println("test4 failed with class A");
   1.240 +                    return false;
   1.241 +                }
   1.242 +            } else {
   1.243 +                int res = test4_1(b, false);
   1.244 +                if (res != b.m()) {
   1.245 +                    System.out.println("test4 failed with class B");
   1.246 +                    return false;
   1.247 +                }
   1.248 +            }
   1.249 +        }
   1.250 +        return true;
   1.251 +    }
   1.252 +
   1.253 +    // Mix one profiled type with an incompatible type
   1.254 +    static int test5_invokevirtual(A a) {
   1.255 +        return a.m();
   1.256 +    }
   1.257 +
   1.258 +    static void test5_2(A a) {
   1.259 +    }
   1.260 +
   1.261 +    static int test5_1(A a, boolean b) {
   1.262 +        if (b) {
   1.263 +            test5_2(a);
   1.264 +        } else {
   1.265 +            A aa = (B)a;
   1.266 +        }
   1.267 +        // shouldn't inline
   1.268 +        return test5_invokevirtual(a);
   1.269 +    }
   1.270 +
   1.271 +    static boolean test5() {
   1.272 +        A a = new A();
   1.273 +        B b = new B();
   1.274 +        C c = new C();
   1.275 +
   1.276 +        // pollute profile at test3_invokevirtual and test3_1 to make
   1.277 +        // sure the compiler cannot rely on it
   1.278 +        for (int i = 0; i < 3000; i++) {
   1.279 +            test5_invokevirtual(a);
   1.280 +            test5_invokevirtual(b);
   1.281 +            test5_invokevirtual(c);
   1.282 +        }
   1.283 +
   1.284 +        for (int i = 0; i < 20000; i++) {
   1.285 +            if ((i % 2) == 0) {
   1.286 +                int res = test5_1(a, true);
   1.287 +                if (res != a.m()) {
   1.288 +                    System.out.println("test5 failed with class A");
   1.289 +                    return false;
   1.290 +                }
   1.291 +            } else {
   1.292 +                int res = test5_1(b, false);
   1.293 +                if (res != b.m()) {
   1.294 +                    System.out.println("test5 failed with class B");
   1.295 +                    return false;
   1.296 +                }
   1.297 +            }
   1.298 +        }
   1.299 +        return true;
   1.300 +    }
   1.301 +
   1.302 +    // Mix incompatible profiled types
   1.303 +    static void test6_2(Object o) {
   1.304 +    }
   1.305 +
   1.306 +    static Object test6_1(Object o, boolean b) {
   1.307 +        if (b) {
   1.308 +            test6_2(o);
   1.309 +        } else {
   1.310 +            test6_2(o);
   1.311 +        }
   1.312 +        return o;
   1.313 +    }
   1.314 +
   1.315 +    static boolean test6() {
   1.316 +        A a = new A();
   1.317 +        A[] aa = new A[10];
   1.318 +
   1.319 +        for (int i = 0; i < 20000; i++) {
   1.320 +            if ((i % 2) == 0) {
   1.321 +                test6_1(a, true);
   1.322 +            } else {
   1.323 +                test6_1(aa, false);
   1.324 +            }
   1.325 +        }
   1.326 +        return true;
   1.327 +    }
   1.328 +
   1.329 +    // Mix a profiled type with an incompatible type
   1.330 +    static void test7_2(Object o) {
   1.331 +    }
   1.332 +
   1.333 +    static Object test7_1(Object o, boolean b) {
   1.334 +        if (b) {
   1.335 +            test7_2(o);
   1.336 +        } else {
   1.337 +            Object oo = (A[])o;
   1.338 +        }
   1.339 +        return o;
   1.340 +    }
   1.341 +
   1.342 +    static boolean test7() {
   1.343 +        A a = new A();
   1.344 +        A[] aa = new A[10];
   1.345 +
   1.346 +        for (int i = 0; i < 20000; i++) {
   1.347 +            if ((i % 2) == 0) {
   1.348 +                test7_1(a, true);
   1.349 +            } else {
   1.350 +                test7_1(aa, false);
   1.351 +            }
   1.352 +        }
   1.353 +        return true;
   1.354 +    }
   1.355 +
   1.356 +    // Mix a profiled type with an interface
   1.357 +    static void test8_2(Object o) {
   1.358 +    }
   1.359 +
   1.360 +    static I test8_1(Object o) {
   1.361 +        test8_2(o);
   1.362 +        return (I)o;
   1.363 +    }
   1.364 +
   1.365 +    static boolean test8() {
   1.366 +        A a = new A();
   1.367 +        B b = new B();
   1.368 +        C c = new C();
   1.369 +
   1.370 +        for (int i = 0; i < 20000; i++) {
   1.371 +            test8_1(b);
   1.372 +        }
   1.373 +        return true;
   1.374 +    }
   1.375 +
   1.376 +    // Mix a profiled type with a constant
   1.377 +    static void test9_2(Object o) {
   1.378 +    }
   1.379 +
   1.380 +    static Object test9_1(Object o, boolean b) {
   1.381 +        Object oo;
   1.382 +        if (b) {
   1.383 +            test9_2(o);
   1.384 +            oo = o;
   1.385 +        } else {
   1.386 +            oo = "some string";
   1.387 +        }
   1.388 +        return oo;
   1.389 +    }
   1.390 +
   1.391 +    static boolean test9() {
   1.392 +        A a = new A();
   1.393 +
   1.394 +        for (int i = 0; i < 20000; i++) {
   1.395 +            if ((i % 2) == 0) {
   1.396 +                test9_1(a, true);
   1.397 +            } else {
   1.398 +                test9_1(a, false);
   1.399 +            }
   1.400 +        }
   1.401 +        return true;
   1.402 +    }
   1.403 +
   1.404 +    // java/lang/Object:AnyNull:exact *,iid=top
   1.405 +    // meets
   1.406 +    // stable:bottom[int:max..0]:NotNull *
   1.407 +    static void test10_4(Object o) {
   1.408 +    }
   1.409 +
   1.410 +    static void test10_3(Object o, boolean b) {
   1.411 +        if (b) {
   1.412 +            test10_4(o);
   1.413 +        }
   1.414 +    }
   1.415 +
   1.416 +    static void test10_2(Object o, boolean b1, boolean b2) {
   1.417 +        if (b1) {
   1.418 +            test10_3(o, b2);
   1.419 +        }
   1.420 +    }
   1.421 +
   1.422 +    static void test10_1(B[] b, boolean b1, boolean b2) {
   1.423 +        test10_2(b, b1, b2);
   1.424 +    }
   1.425 +
   1.426 +    static boolean test10() {
   1.427 +        Object o = new Object();
   1.428 +        A[] a = new A[10];
   1.429 +        B[] b = new B[10];
   1.430 +        B[] c = new C[10];
   1.431 +        for (int i = 0; i < 20000; i++) {
   1.432 +            test10_1(b, false, false);
   1.433 +            test10_1(c, false, false);
   1.434 +            test10_2(a, true, false);
   1.435 +            test10_3(o, true);
   1.436 +        }
   1.437 +        return true;
   1.438 +    }
   1.439 +
   1.440 +    // stable:TypeSpeculation$B:TopPTR *,iid=top[int:max..0]:TopPTR *,iid=top
   1.441 +    // meets
   1.442 +    // java/lang/Object:AnyNull:exact *,iid=top
   1.443 +    static void test11_3(Object o) {
   1.444 +    }
   1.445 +
   1.446 +    static void test11_2(Object o, boolean b) {
   1.447 +        if (b) {
   1.448 +            test11_3(o);
   1.449 +        }
   1.450 +    }
   1.451 +
   1.452 +    static void test11_1(B[] b, boolean bb) {
   1.453 +        test11_2(b, bb);
   1.454 +    }
   1.455 +
   1.456 +    static boolean test11() {
   1.457 +        Object o = new Object();
   1.458 +        B[] b = new B[10];
   1.459 +        B[] c = new C[10];
   1.460 +        for (int i = 0; i < 20000; i++) {
   1.461 +            test11_1(b, false);
   1.462 +            test11_1(c, false);
   1.463 +            test11_2(o, true);
   1.464 +        }
   1.465 +        return true;
   1.466 +    }
   1.467 +
   1.468 +    // TypeSpeculation$I *
   1.469 +    // meets
   1.470 +    // java/lang/Object:AnyNull *,iid=top
   1.471 +    static void test12_3(Object o) {
   1.472 +    }
   1.473 +
   1.474 +    static void test12_2(Object o, boolean b) {
   1.475 +        if (b) {
   1.476 +            test12_3(o);
   1.477 +        }
   1.478 +    }
   1.479 +
   1.480 +    static void test12_1(I i, boolean b) {
   1.481 +        test12_2(i, b);
   1.482 +    }
   1.483 +
   1.484 +    static boolean test12() {
   1.485 +        Object o = new Object();
   1.486 +        B b = new B();
   1.487 +        C c = new C();
   1.488 +        for (int i = 0; i < 20000; i++) {
   1.489 +            test12_1(b, false);
   1.490 +            test12_1(c, false);
   1.491 +            test12_2(o, true);
   1.492 +        }
   1.493 +        return true;
   1.494 +    }
   1.495 +
   1.496 +    // stable:bottom[int:max..0]:NotNull *
   1.497 +    // meets
   1.498 +    // stable:TypeSpeculation$A:TopPTR *,iid=top[int:max..0]:AnyNull:exact *,iid=top
   1.499 +    static Object test13_3(Object o, boolean b) {
   1.500 +        Object oo;
   1.501 +        if (b) {
   1.502 +            oo = o;
   1.503 +        } else {
   1.504 +            oo = new A[10];
   1.505 +        }
   1.506 +        return oo;
   1.507 +    }
   1.508 +
   1.509 +    static void test13_2(Object o, boolean b1, boolean b2) {
   1.510 +        if (b1) {
   1.511 +            test13_3(o, b2);
   1.512 +        }
   1.513 +    }
   1.514 +
   1.515 +    static void test13_1(B[] b, boolean b1, boolean b2) {
   1.516 +        test13_2(b, b1, b2);
   1.517 +    }
   1.518 +
   1.519 +    static boolean test13() {
   1.520 +        A[] a = new A[10];
   1.521 +        B[] b = new B[10];
   1.522 +        B[] c = new C[10];
   1.523 +        for (int i = 0; i < 20000; i++) {
   1.524 +            test13_1(b, false, false);
   1.525 +            test13_1(c, false, false);
   1.526 +            test13_2(a, true, (i%2) == 0);
   1.527 +        }
   1.528 +        return true;
   1.529 +    }
   1.530 +
   1.531 +    static public void main(String[] args) {
   1.532 +        boolean success = true;
   1.533 +
   1.534 +        success = test1() && success;
   1.535 +
   1.536 +        success = test2() && success;
   1.537 +
   1.538 +        success = test3() && success;
   1.539 +
   1.540 +        success = test4() && success;
   1.541 +
   1.542 +        success = test5() && success;
   1.543 +
   1.544 +        success = test6() && success;
   1.545 +
   1.546 +        success = test7() && success;
   1.547 +
   1.548 +        success = test8() && success;
   1.549 +
   1.550 +        success = test9() && success;
   1.551 +
   1.552 +        success = test10() && success;
   1.553 +
   1.554 +        success = test11() && success;
   1.555 +
   1.556 +        success = test12() && success;
   1.557 +
   1.558 +        success = test13() && success;
   1.559 +
   1.560 +        if (success) {
   1.561 +            System.out.println("TEST PASSED");
   1.562 +        } else {
   1.563 +            throw new RuntimeException("TEST FAILED: erroneous bound check elimination");
   1.564 +        }
   1.565 +    }
   1.566 +}

mercurial