1 /*
2  * Copyright (c) 2002, 2018, 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  *
27  * @summary converted from VM Testbase runtime/jbe/dead/dead03.
28  * VM Testbase keywords: [quick, runtime]
29  *
30  * @library /vmTestbase
31  *          /test/lib
32  * @run driver jdk.test.lib.FileInstaller . .
33  * @run main/othervm vm.compiler.jbe.dead.dead03.dead03
34  */
35 
36 package vm.compiler.jbe.dead.dead03;
37 
38 /* -- Test the elimination of dead assignment to global variables
39 In the example below, the value assigned to i is never used, all dead stores to global can be eliminated, and the last return statement in f() is unreachable; Both dead/unused stores and unreachable statement can be eliminated.
40 
41  */
42 
43 public class dead03 {
44   int global;
45   int i;
46 
main(String args[])47   public static void main(String args[]) {
48     dead03 dce = new dead03();
49 
50     System.out.println("f()="+dce.f()+"; fopt()="+dce.fopt());
51     if (dce.f() == dce.fopt()) {
52       System.out.println("Test dead03 Passed.");
53     } else {
54       throw new Error("Test dead03 Failed: f()=" + dce.f() + " != fopt()=" + dce.fopt());
55     }
56   }
57 
f()58   int f() {
59 
60     i = 1;           /* dead store */
61     global = 8;      /* dead store */
62     global = 7;      /* dead store */
63     global = 6;      /* dead store */
64     global = 5;      /* dead store */
65     global = 4;      /* dead store */
66     global = 3;      /* dead store */
67     global = 2;      /* dead store */
68     global = 1;      /* dead store */
69     global = 0;      /* dead store */
70     global = -1;     /* dead store */
71     global = -2;     /* dead store */
72 
73     i = 1;           /* dead store */
74     global = 8;      /* dead store */
75     global = 7;      /* dead store */
76     global = 6;      /* dead store */
77     global = 5;      /* dead store */
78     global = 4;      /* dead store */
79     global = 3;      /* dead store */
80     global = 2;      /* dead store */
81     global = 1;      /* dead store */
82     global = 0;      /* dead store */
83     global = -1;     /* dead store */
84     global = -2;     /* dead store */
85 
86     i = 1;           /* dead store */
87     global = 8;      /* dead store */
88     global = 7;      /* dead store */
89     global = 6;      /* dead store */
90     global = 5;      /* dead store */
91     global = 4;      /* dead store */
92     global = 3;      /* dead store */
93     global = 2;      /* dead store */
94     global = 1;      /* dead store */
95     global = 0;      /* dead store */
96     global = -1;     /* dead store */
97     global = -2;
98 
99     if (Math.abs(global) >= 0)
100       return global;
101     return global;   /* unreachable */
102   }
103 
104   // Code fragment after dead code elimination
fopt()105   int fopt() {
106 
107     global = -2;
108     return global;
109   }
110 }
111