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