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/subcommon04.
  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.subcommon04.subcommon04
  34  */
  35 
  36 package vm.compiler.jbe.subcommon.subcommon04;
  37 
  38 /* Tests the Global Common Sub-expression Elimination optimization, including duplicate
  39    calls to math function.
  40  */
  41 
  42 public class subcommon04 {
  43     int k, m, n;
  44     double a, b, c, d;
  45     double arr[] = new double[100];
  46     double arr_opt[] = new double[100];
  47     double arr1[][] = new double[10][10];
  48     double arr1_opt[][] = new double[10][10];
  49 
  50     public static void main(String args[]) {
  51         subcommon04 sce = new subcommon04();
  52         sce.init();
  53         sce.un_optimized();
  54         sce.hand_optimized();
  55         sce.mat();
  56 
  57         if (sce.eCheck()) {
  58             System.out.println("Test subcommon04 Passed.");
  59         } else {
  60             throw new Error("Test subcommon04 Failed.");
  61         }
  62     }
  63 
  64     void init() {
  65         for(int i = 0; i < arr.length; i++) {
  66             arr[i] = 17E-2;
  67             arr_opt[i] = 17E-2;
  68         }
  69         for(m = 0; m < arr1[0].length; m++) {
  70             arr1[0][m] = arr[m];
  71             arr1_opt[0][m] = arr_opt[m];
  72         }
  73     }
  74 
  75     void mat() {
  76         for(k = 0; k < arr1[0].length; k++) {
  77             n = k * arr1[0].length;
  78             for(m = 0; m < arr1[0].length; m++) {
  79                 arr[n+m] = Math.exp(arr[m]);
  80                 arr_opt[n+m] = Math.exp(arr_opt[m]);
  81                 arr1[k][m] = (arr[m] * 1/Math.PI);
  82                 arr1_opt[k][m] = (arr_opt[m] * 1/Math.PI);
  83             }
  84         }
  85     }
  86 
  87     void un_optimized() {
  88         c = 1.123456789;
  89         d = 1.010101012;
  90         b = 1E-8;
  91         // example 1
  92         if (c == d) {
  93             a = d * c * (b * 10.0);
  94         }
  95         else {
  96             a = d / c * (b * 10.0);
  97         }
  98 
  99         // example 2
 100         if ((a * c) > 9.0) {
 101             b = a * c / 10.0;
 102             c = 1.0;
 103         }
 104         else {
 105             b = a * c;
 106             c = 0.1;
 107         }
 108 
 109         // example 3
 110         int n = 9;
 111         for (k = 0; k < arr.length; k++) {
 112             n = n - 1;
 113             if (n < 0) n = 9;
 114             if (arr1[0][n] == arr[k]) break;
 115         }
 116         if (arr1[0][n] == b) c += 1.0;
 117         arr[2] = a;
 118         arr[3] = arr1[0][n];
 119         arr[4] = c;
 120         arr[5] = d;
 121         arr[8] = a / c;
 122         arr[9] = c - a;
 123 
 124         // example 4
 125         b = d * c;
 126         d = d * c;
 127         arr[6] = b;
 128         arr[7] = d;
 129     }
 130 
 131     void hand_optimized() {
 132         c = 1.123456789;
 133         d = 1.010101012;
 134         b = 1E-8;
 135         // example 1
 136         if (c == d) {
 137             a = d * c;
 138         }
 139         else {
 140             a = d / c;
 141         }
 142         a *= (b * 10.0);
 143 
 144         // example 2
 145         b = a * c;
 146         if (b > 9.0) {
 147             b /= 10.0;
 148             c = 1.0;
 149         }
 150         else {
 151             c = 0.1;
 152         }
 153 
 154         // example 3
 155         double t1 = arr1_opt[0][n];
 156         n = 9;
 157         for (k = 0; k < arr_opt.length; k++) {
 158             n--;
 159             if (n < 0) n = 9;
 160             if(t1 == arr_opt[k]) break;
 161         }
 162         if (t1 == b) c++;
 163         arr_opt[2] = a;
 164         arr_opt[3] = t1;
 165         arr_opt[4] = c;
 166         arr_opt[5] = d;
 167         arr_opt[8] = a / c;
 168         arr_opt[9] = c - a;
 169 
 170         // example 4
 171         b = d * c;
 172         d = b;
 173         arr_opt[6] = b;
 174         arr_opt[7] = d;
 175     }
 176 
 177     boolean eCheck() {
 178         boolean st = true;
 179 
 180         for (int i = 0; i < arr.length; i++) {
 181             if (arr[i] != arr_opt[i]) {
 182                 System.out.println(">>Bad output: arr["+i+"]="+arr[i]+"; arr_opt["+i+"]="+arr_opt[i]);
 183                 st = false;
 184             }
 185         }
 186 
 187         for (int i = 0; i < arr1.length; i++)
 188             for (int j = 0; j < arr1[i].length; j++) {
 189                 if (arr1[i][j] != arr1_opt[i][j]) {
 190                     System.out.println(">>Bad output: arr["+i+"]["+j+"]="+arr1[i][j]+"; arr_opt["+i+"]["+j+"]="+arr1_opt[i][j]);
 191                     st = false;
 192                 }
 193             }
 194         return st;
 195     }
 196 }