1 /*
   2  * Copyright (c) 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 package compiler.runtime;
  25 
  26 /*
  27  * @test
  28  * @summary testing deoptimization on safepoint with floating point values on stack
  29  * @bug 8202710
  30  * @run main/othervm -XX:+DeoptimizeALot
  31  *                   -XX:+UnlockDiagnosticVMOptions -XX:+IgnoreUnrecognizedVMOptions
  32  *                   compiler.runtime.TestFloatsOnStackDeopt
  33  */
  34 
  35 public class TestFloatsOnStackDeopt {
  36 
  37     private static final int ARRLEN = 97;
  38     private static final int ITERS1 = 100;
  39     private static final int ITERS2 = 40000;
  40     private static final float VALUE = 15.f;
  41     public static String dummyString = "long long string";
  42 
  43     static void run_loop_with_safepoint(float[] a0, float b) {
  44         // Non-counted loop with safepoint.
  45         for (long l = 0; l < ITERS2; l++) {
  46             // Counted and vectorized loop.
  47             for (int i = 0; i < a0.length; i += 1) {
  48                 a0[i] += b;
  49             }
  50         }
  51     }
  52 
  53     static int test() {
  54         // thread provokes frequent GC - together with +DeoptimizeALot and safepoint it forces executed function deoptimization
  55         Thread th = new Thread() {
  56             public void run() {
  57                 while(true) {
  58                     synchronized(this) { try { wait(1); } catch (Exception ex) {} }
  59                     dummyString += dummyString;
  60                     if (dummyString.length() > 1024*1024) { dummyString = "long long string"; }
  61                 }
  62             }
  63         };
  64         th.start();
  65 
  66         int errn = 0;
  67         for (int j = 0; j < ITERS1; j++) {
  68             float[] x0 = new float[ARRLEN];
  69             run_loop_with_safepoint(x0, VALUE);
  70             for (int i = 0; i < ARRLEN; i++) {
  71                 if (x0[i] != VALUE * ITERS2) {
  72                     System.err.println("(" + j + "): " + "x0[" + i + "] = " + x0[i] + " != " + VALUE * ITERS2);
  73                     errn++;
  74                 }
  75                 x0[i] = 0.f; // Reset
  76             }
  77             if (errn > 0) break;
  78         }
  79 
  80         th.stop();
  81         return errn;
  82     }
  83 
  84     public static void main(String args[]) {
  85         int errn = test();
  86         System.err.println((errn > 0) ? "FAILED" : "PASSED");
  87     }
  88 }
  89