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/hoist03.
  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.hoist03.hoist03
  34  */
  35 
  36 package vm.compiler.jbe.hoist.hoist03;
  37 
  38 
  39 // hoist03.java
  40 
  41 /* -- Loop invariant if-then code where the "then" branch is always executed.
  42       Example:
  43 
  44       double  foo_static[5000];
  45       for(i=0 ; i<100 ; i++) {
  46          if (i < 0) {
  47                 foo_static[k] = -1.;
  48          }
  49          else {
  50                 foo_static[k] = +1.;
  51          }
  52       }
  53 
  54       Can the optimizer break the code up to avoid re-checking the conditional assignment      within the loop when it is known that i will never be < 0 during that loop.
  55 
  56  */
  57 
  58 public class hoist03 {
  59     int LEN = 60000;
  60     int a[] = new int[LEN];
  61     int aopt[] = new int[LEN];
  62 
  63     public static void main(String args[]) {
  64         hoist03 hst = new hoist03();
  65 
  66         hst.f();
  67         hst.fopt();
  68         if (hst.eCheck()) {
  69             System.out.println("Test hoist03 Passed.");
  70         } else {
  71             throw new Error("Test hoist03 Failed.");
  72         }
  73     }
  74 
  75     void f() {
  76         // i is always > 0, hence the conditional statement i < 0 is loop invariant.
  77         int k = 0;
  78 
  79         for (int j = 0; j < 200; j++) {
  80             for (int i = 0; i < 300; i++) {
  81                 if (i < 0) {
  82                     a[k++] = -1;
  83                 }
  84                 else {
  85                     a[k++] = 1;
  86                 }
  87             }
  88         }
  89     }
  90 
  91 
  92     // Code fragment after the invariant expression is hoisted out of the loop.
  93     void fopt() {
  94         int k = 0;
  95 
  96         for (int j = 0; j < 200; j++) {
  97             for (int i = 0; i < 300; i++) {
  98                 aopt[k++] = 1;
  99             }
 100         }
 101     }
 102 
 103 
 104     // Check Loop Hoisting results
 105     boolean eCheck() {
 106         for (int i = 0; i < a.length; i++)
 107             if (a[i] != aopt[i]) {
 108                 System.out.println("a["+i+"]="+a[i]+"; aopt["+i+"]="+aopt[i]);
 109                 return false;
 110             }
 111 
 112         return true;
 113     }
 114 }