1 /*
2  * Copyright (c) 2014, 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  * @bug 8046698
27  * @summary PhiNode inserted between AllocateNode and Initialization node confuses allocation elimination
28  * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement
29  *                   compiler.macronodes.TestEliminateAllocationPhi
30  */
31 
32 package compiler.macronodes;
33 
34 public class TestEliminateAllocationPhi {
35 
36     // This will return I when called from m(0 and once optimized will
37     // go away but this will confuse escape analysis in m(): it will
38     // find I as non escaping but non scalar replaceable. In its own
39     // method so that we can make the profile of the if() branch look
40     // like it's taken sometimes.
m2(Integer I, int i)41     static Integer m2(Integer I, int i) {
42         for (; i < 10; i=(i+2)*(i+2)) {
43         }
44         if (i == 121) {
45             return II;
46         }
47         return I;
48     }
49 
50     static Integer II = new Integer(42);
51 
m(int[] integers, boolean flag)52     static int m(int[] integers, boolean flag) {
53         int j = 0;
54         while(true) {
55             try {
56                 int k = integers[j++];
57                 // A branch that will cause loop unswitching
58                 if (flag) {
59                     k += 42;
60                 }
61                 if (k < 1000) {
62                     throw new Exception();
63                 }
64                 // Because of the try/catch the Allocate node for this
65                 // new will be in the loop while the Initialization
66                 // node will be outside the loop. When loop
67                 // unswitching happens, the Allocate node will be
68                 // cloned and the results of both will be inputs to a
69                 // Phi that will be between the Allocate nodes and the
70                 // Initialization nodes.
71                 Integer I = new Integer(k);
72 
73                 I = m2(I, 0);
74 
75                 int i = I.intValue();
76                 return i;
77             } catch(Exception e) {
78             }
79         }
80     }
81 
main(String[] args)82     static public void main(String[] args) {
83         for (int i = 0; i < 5000; i++) {
84             m2(null, 1);
85         }
86 
87         int[] integers = { 2000 };
88         for (int i = 0; i < 6000; i++) {
89             m(integers, (i%2) == 0);
90         }
91         int[] integers2 = { 1, 2, 3, 4, 5, 2000 };
92         for (int i = 0; i < 10000; i++) {
93             m(integers2, (i%2) == 0);
94         }
95     }
96 }
97