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/subcommon/subcommon01.
  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.subcommon.subcommon01.subcommon01
  34  */
  35 
  36 package vm.compiler.jbe.subcommon.subcommon01;
  37 
  38 /* -- Common subexpression elimination testing
  39 Using both global and local common subexpressions in methods b5()
  40 and b6() below to test common subexpression.
  41  */
  42 
  43 import java.io.PrintStream;
  44 import java.lang.Exception;
  45 
  46 public class subcommon01 {
  47   static final int LEN = 500;
  48 
  49   public static void main(String args[]) {
  50     double a[]  = new double[LEN];
  51     double ao[] = new double[LEN];
  52 
  53     Preopt preOptRun = new Preopt();
  54     initAr(a);
  55     preOptRun.b1(a);
  56     preOptRun.b2(a);
  57     preOptRun.b3(a);
  58     preOptRun.b5(a);
  59     preOptRun.b2(a);
  60     preOptRun.b6(a);
  61 
  62     Opt optRun = new Opt();
  63     initAr(ao);
  64     optRun.b1(ao);
  65     optRun.b2(ao);
  66     optRun.b3(ao);
  67     optRun.b5(ao);
  68     optRun.b2(ao);
  69     optRun.b6(ao);
  70 
  71     eCheck(a, ao);
  72   }
  73 
  74   static int getLen() {
  75     return LEN;
  76   }
  77 
  78   static void initAr(double arr[]) {
  79     double r1, r2, r3;
  80     for (int r=0; r<LEN; r++) {
  81       r1 = LEN/(r+1);
  82       r2 = Math.sqrt(r1*r1);
  83       r3 = Math.sin(r2);
  84       arr[r] = r2/r3;
  85     }
  86   }
  87 
  88   static void pt(double arr[]) {
  89     System.out.println();
  90     for (int k=0; k<10; k++)
  91       System.out.print("a["+k+"]="+arr[k]);
  92   }
  93 
  94   static void eCheck(double b1[], double b2[]) {
  95     for (int k = 0; k < LEN; k++) {
  96       if (b1[k] != b2[k]) {
  97         System.out.println("eCheck fails in a["+k+"]");
  98         System.out.println("a ["+k+"]="+b1[k]);
  99         System.out.println("ao["+k+"]="+b2[k]);
 100         System.out.println("Test subcommon01 Failed.");
 101         throw new Error("Test subcommon01 Failed: eCheck fails in a[" + k + "]. a[" + k + "]=" + b1[k] + " != ao[" + k + "]=" + b2[k]);
 102       }
 103     }
 104     System.out.println("Test subcommon01 Passed.");
 105   }
 106 }
 107 
 108 class A {
 109   int m = 81;
 110   int n = 1;
 111   int i = 0;
 112   int j = 0;
 113   double v;
 114   double x = 1;
 115   double t3 = 0;
 116   double t5, t9, t14;
 117   int t1, t2, t4;
 118   int t6, t7, t8, t10;
 119   int t11, t12, t13, t15;
 120 
 121   void b1(double arr[]) {
 122     i = m-1;
 123     j = n;
 124     t1 = 4*n;
 125     try {
 126       v = arr[t1];
 127     }
 128     catch (ArrayIndexOutOfBoundsException e) {
 129       System.err.println("Bad input to subcommon01.b1. " +
 130                          "Expected a smaller number than " + subcommon01.getLen() +
 131                          ", got: ->" + t1 + "<-");
 132       System.out.println( e );
 133       System.exit(1);
 134     }
 135     //    System.out.println("Finish b1");
 136   }
 137 
 138   void b2(double arr[]) {
 139       t2 = 4*(++i);
 140       try {
 141         t3 = arr[t2];
 142       }
 143       catch (ArrayIndexOutOfBoundsException e) {
 144         System.err.println("Bad input to subcommon01.b2. " +
 145                            "Expected a smaller number than " + subcommon01.getLen() +
 146                            ", got: ->" + t2 + "<-");
 147         System.out.println( e );
 148         System.exit(1);
 149       }
 150       //    System.out.println("Finish b2");
 151   }
 152 
 153 
 154   void b3(double arr[]) {
 155       t4 = 4*(--j);
 156       try {
 157         t5 = arr[t4];
 158       }
 159       catch (ArrayIndexOutOfBoundsException e) {
 160         System.err.println("Bad input to subcommon01.b3. " +
 161                            "Expected a smaller number than " + subcommon01.getLen() +
 162                            ", got: ->" + t4 + "<-");
 163         System.out.println( e );
 164         System.exit(1);
 165       }
 166       //    System.out.println("Finish b3");
 167   }
 168 }
 169 
 170 
 171 class Preopt extends A {
 172   // Pre-optimized code
 173   void b5(double arr[]) {
 174     t6 = 4*i;
 175     try {
 176       x = arr[t6];
 177     }
 178     catch (ArrayIndexOutOfBoundsException e) {
 179       System.err.println("Bad input to subcommon01.b5. " +
 180                          "Expected a smaller number than " + subcommon01.getLen() +
 181                          ", got: ->" + t6 + "<-");
 182       System.out.println( e );
 183       System.exit(1);
 184     }
 185     t7 = 4*i;
 186     t8 = 4*j;
 187     try {
 188       t9 = arr[t8];
 189       arr[t7] = t9;
 190       t10 = 4*j;
 191       arr[t10] = x;
 192     }
 193     catch (ArrayIndexOutOfBoundsException e) {
 194       System.out.println( e );
 195       System.exit(1);
 196     }
 197     //    System.out.println("Finish b5");
 198   }
 199 
 200   void b6(double arr[]) {
 201     t11 = 4*i;
 202     t12 = 4*i;
 203     t13 = 4*n;
 204     try {
 205       x = arr[t11];
 206       t14 = arr[t13];
 207       arr[t12] = t14;
 208       t15 = 4*n;
 209       arr[t15] = x;
 210     }
 211     catch (ArrayIndexOutOfBoundsException e) {
 212       System.out.println( e );
 213       System.exit(1);
 214     }
 215     //    System.out.println("Finish b6");
 216   }
 217 }
 218 
 219 class Opt extends A {
 220   // The result of eliminating both global and local common subexpressions
 221   // from methods b5() and b6() resides in methods bopt5() and bopt6() respectively.
 222   void b5(double arropt[]) {
 223     x = t3;
 224     try {
 225       arropt[t2] = t5;
 226       arropt[t4] = x;
 227     }
 228     catch (ArrayIndexOutOfBoundsException e) {
 229       System.out.println( e );
 230       System.exit(1);
 231     }
 232     //    System.out.println("Finish b5");
 233   }
 234 
 235   void b6(double arropt[]) {
 236     x = t3;
 237     try {
 238       t14 = arropt[t1];
 239       arropt[t2] = t14;
 240       arropt[t1] = x;
 241     }
 242     catch (ArrayIndexOutOfBoundsException e) {
 243       System.out.println( e );
 244       System.exit(1);
 245     }
 246     //    System.out.println("Finish b6");
 247   }
 248 }