1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @key stress randomness
27  * @bug 8139771
28  * @summary Eliminating CastPP nodes at Phis when they all come from a unique input may cause crash
29  * @requires vm.gc=="Serial" | vm.gc=="Parallel"
30  *
31  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
32  *      -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions -XX:+StressGCM
33  *      compiler.controldependency.TestEliminatedCastPPAtPhi
34  *
35  */
36 
37 package compiler.controldependency;
38 
39 public class TestEliminatedCastPPAtPhi {
40 
41     static TestEliminatedCastPPAtPhi saved;
42     static TestEliminatedCastPPAtPhi saved_not_null;
43 
44     int f;
45 
test(TestEliminatedCastPPAtPhi obj, int[] array, boolean flag)46     static int test(TestEliminatedCastPPAtPhi obj, int[] array, boolean flag) {
47         int ret = array[0] + array[20];
48         saved = obj;
49         if (obj == null) {
50             return ret;
51         }
52         saved_not_null = obj;
53 
54         // empty loop to be optimized out. Delays range check smearing
55         // for the array access below until the if diamond is
56         // optimized out
57         int i = 0;
58         for (; i < 10; i++);
59 
60         ret += array[i];
61 
62         TestEliminatedCastPPAtPhi res;
63         if (flag) {
64             // load is optimized out and res is obj here
65             res = saved;
66         } else {
67             // load is optimized out and res is non null CastPP of res here
68             res = saved_not_null;
69         }
70         // null check + CastPP here for res field load below
71 
72         // 1) null check is pushed in the branches of the if above by
73         // split through phi because res is non null in the second
74         // branch and the null check can be optimized out in that
75         // branch. The Castpp stays here.
76 
77         // 2) null check in the first branch is also optimized out
78         // because a dominating null check is found (the explicit null
79         // check at the beggining of the test)
80 
81         // 3) the Phi for the if above merges a CastPP'ed value and
82         // the same value so it's optimized out and replaced by the
83         // uncasted value: obj
84 
85         // 4) the if above has 2 empty branches so it's optimized
86         // out. The control of the CastPP that is still here is now
87         // the success branch of the range check for the array access
88         // above
89 
90         // 5) the loop above is optimized out, i = 10, the range check
91         // for the array access above is optimized out and all its
92         // uses are replaced by the range check for the array accesses
93         // at the beginning of the method. The castPP here is one of
94         // the uses and so its control is now the range check at the
95         // beginning of the method: the control of the CastPP bypasses
96         // the explicit null check
97 
98         return ret + res.f;
99     }
100 
main(String[] args)101     static public void main(String[] args) {
102         int[] array = new int[100];
103         TestEliminatedCastPPAtPhi obj = new TestEliminatedCastPPAtPhi();
104         for (int i = 0; i < 20000; i++) {
105             test(obj, array, (i%2) == 0);
106         }
107         test(null, array, true);
108     }
109 
110 }
111