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/hoist/hoist02.
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.hoist.hoist02.hoist02
34  */
35 
36 package vm.compiler.jbe.hoist.hoist02;
37 
38 // hoist02.java
39 
40 /* -- Test hoist invariant code of integer expression. This eliminates unnecessary recomputations of invariant expressions in loops.
41 
42      Example:
43 
44      int a[];
45      int x, y, z;
46      for (i = 0; i < 100; i++)
47         a[i] = (x + y)*z;
48  */
49 
50 public class hoist02 {
51     int LEN = 5000;
52     int a[] = new int[LEN];
53     int aopt[] = new int[LEN];
54     boolean bool_val = true;
55     int i1 = 1, i2 = 2, i3 = 3, i4 = 4, i5 = 5, i6 = 6, i7 = 7, i8 = 8, i9 = 9;
56 
main(String args[])57     public static void main(String args[]) {
58         hoist02 hst = new hoist02();
59 
60         hst.f();
61         hst.fopt();
62         if (hst.eCheck()) {
63             System.out.println("Test hoist02 Passed.");
64         } else {
65             throw new Error("Test hoist02 Failed.");
66         }
67     }
68 
f()69     void f() {
70         // Non-optimized version: i1 through i9 are invariants
71         int i = 0;
72 
73         do
74             a[i++] = bool_val ? (i1+i2+i3+i4+i5+i6+i7+i8+i9) : (i1*i2*i3*i4*i5*i6*i7*i8*i9);
75         while (i < a.length);
76     }
77 
78     // Code fragment after the invariant expression is hoisted out of the loop.
fopt()79     void fopt() {
80         int i = 0;
81         int t =  bool_val ? (i1+i2+i3+i4+i5+i6+i7+i8+i9) : (i1*i2*i3*i4*i5*i6*i7*i8*i9);
82 
83         do
84             aopt[i++] = t;
85         while (i < aopt.length);
86     }
87 
88     // Check Loop Hoisting results
eCheck()89     boolean eCheck() {
90         for (int i = 0; i < a.length; i++)
91             if (a[i] != aopt[i]) {
92                 System.out.println("a["+i+"]="+a[i]+"; aopt["+i+"]="+aopt[i]);
93                 return false;
94             }
95 
96         return true;
97     }
98 }
99