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/constprop/constprop01.
  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.constprop.constprop01.constprop01
  34  */
  35 
  36 package vm.compiler.jbe.constprop.constprop01;
  37 
  38 // constprop01.java
  39 
  40 /* Tests the Constant Propagation optimization, including calls to math functions
  41    with constant arguments.
  42  */
  43 
  44 class Starter {
  45     int global;
  46     int gl_unopt;
  47     int gl_opt;
  48     int a_one;
  49     int zero;
  50 
  51     Starter() {
  52         global = 1;
  53         gl_unopt = 1;
  54         gl_opt = 1;
  55         a_one = 1;
  56         zero = global - a_one; // really a zero, but compiler doesn't know
  57     }
  58 
  59     void increment_unopt() {
  60         gl_unopt += a_one;
  61     }
  62 
  63     void increment_opt() {
  64         gl_opt += a_one;
  65     }
  66 }
  67 
  68 
  69 public class constprop01 {
  70     static int err = 0;
  71     static final int is = 16386;
  72     double a, b, c, d, x, z;
  73     double a_opt, b_opt, c_opt, d_opt, x_opt, z_opt;
  74     static Starter st = new Starter();
  75 
  76     public static void main(String args[]) {
  77         constprop01 cpr = new constprop01();
  78 
  79         cpr.un_optimized();
  80         cpr.hand_optimized();
  81         if (st.gl_unopt != st.gl_opt) {
  82             System.out.println(">>Bad output: gl_unopt is not equal to gl_optimized. gl_unopt = "+st.gl_unopt+"; gl_optimized = "+st.gl_opt);
  83             err = 1;
  84         }
  85         if (cpr.a != cpr.a_opt) {
  86             System.out.println(">>Bad output: a_unoptimized="+cpr.a+"; a_optimized="+cpr.a_opt);
  87             err = 1;
  88         }
  89         if (cpr.b != cpr.b_opt) {
  90             System.out.println(">>Bad output: b_unoptimized="+cpr.b+"; b_optimized="+cpr.b_opt);
  91             err = 1;
  92         }
  93         if (cpr.c != cpr.c_opt) {
  94             System.out.println(">>Bad output: c_unoptimized="+cpr.c+"; c_optimized="+cpr.c_opt);
  95             err = 1;
  96         }
  97         if (cpr.d != cpr.d_opt) {
  98             System.out.println(">>Bad output: d_unoptimized="+cpr.d+"; d_optimized="+cpr.d_opt);
  99             err = 1;
 100         }
 101         if (cpr.x != cpr.x_opt) {
 102             System.out.println(">>Bad output: x_unoptimized="+cpr.x+"; x_optimized="+cpr.x_opt);
 103             err = 1;
 104         }
 105         if (cpr.z != cpr.z_opt) {
 106             System.out.println(">>Bad output: z_unoptimized="+cpr.z+"; z_optimized="+cpr.z_opt);
 107             err = 1;
 108         }
 109 
 110         if (err == 0) {
 111             System.out.println("Test constprop01 Passed.");
 112         } else {
 113             throw new Error("Test constprop01 Failed.");
 114         }
 115     }
 116 
 117     void un_optimized() {
 118         int k, m;
 119 
 120         a = st.a_one;
 121         c = st.a_one;
 122         d = st.a_one;
 123         x = st.a_one;
 124 
 125         // example 1
 126         for(k = 0; k < is; k++) {
 127             st.increment_unopt();
 128         }
 129         System.out.println("gl_unopt = "+st.gl_unopt+" after incrementing it 16386 times.");
 130         st.gl_unopt -= 16386;
 131         System.out.println("gl_unopt = "+st.gl_unopt+" after subtracting 16386.");
 132 
 133         // example 2
 134         m = 32769;
 135         b = c * m;
 136         System.out.println("c = "+c+"; m = 2; b = "+b);
 137 
 138         // example 3
 139         a = Math.E / (a * d * Math.pow(1/Math.PI, 15));
 140         c = 2 * Math.E * d;
 141         d = 2 * Math.E * a;
 142         System.out.println("e = "+Math.E+"; d = "+d+"; a = "+a);
 143 
 144         // example 4
 145         System.out.println("x = "+x);
 146         x += m;
 147         System.out.println("x = "+x+"; m = "+m);
 148 
 149         // example 5
 150         z = Math.pow(1/Math.PI, 15) + st.zero/Math.PI;
 151         System.out.println("z = "+z);
 152     }
 153 
 154 
 155     void hand_optimized() {
 156         int k, m;
 157 
 158         a_opt = st.a_one;
 159         c_opt = st.a_one;
 160         d_opt = st.a_one;
 161         x_opt = st.a_one;
 162 
 163         // example 1
 164         for(k = 0; k < 16386; k++) {
 165             st.increment_opt();
 166         }
 167         System.out.println("gl_opt = "+st.gl_opt+" after incrementing it 16386 times.");
 168         st.gl_opt -= 16386;
 169         System.out.println("gl_opt = "+st.gl_opt+" after subtracting 16386.");
 170 
 171         // example 2
 172         m = 32769;
 173         b_opt = c_opt * 32769;
 174         System.out.println("c_opt = "+c_opt+"; b_opt = "+b_opt);
 175 
 176         // example 3
 177         double e = 2.7182818284590452354;
 178         a_opt = 2.7182818284590452354 / (a_opt * d_opt * Math.pow(0.3183098861837907, 15));
 179         c_opt = 2 * 2.7182818284590452354 * d_opt;
 180         d_opt = 2 * 2.7182818284590452354 * a_opt;
 181         System.out.println("e = "+2.7182818284590452354+"; d_opt = "+d_opt+"; a_opt = "+a_opt);
 182 
 183         // example 4
 184         System.out.println("x_opt = "+x_opt);
 185         x_opt += 32769;
 186         System.out.println("x_opt = "+x_opt+"; m = "+m);
 187 
 188         // example 5
 189         z_opt = 3.4894092627910365E-8 + 0/3.14159265358979323846;
 190         System.out.println("z_opt = "+z_opt);
 191     }
 192 }