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