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     static volatile boolean pleaseStop = false;
  43 
  44     static void run_loop_with_safepoint(float[] a0, float b) {
  45         // Non-counted loop with safepoint.
  46         for (long l = 0; l < ITERS2; l++) {
  47             // Counted and vectorized loop.
  48             for (int i = 0; i < a0.length; i += 1) {
  49                 a0[i] += b;
  50             }
  51         }
  52     }
  53 
  54     static int test() {
  55         // thread provokes frequent GC - together with +DeoptimizeALot and safepoint it forces executed function deoptimization
  56         Thread th = new Thread() {
  57             public void run() {
  58                 while (!pleaseStop) {
  59                     synchronized(this) { try { wait(1); } catch (Exception ex) {} }
  60                     dummyString = new StringBuffer(dummyString).append(dummyString).toString();
  61                     if (dummyString.length() > 1024*1024) { dummyString = "long long string"; }
  62                 }
  63             }
  64         };
  65         th.start();
  66 
  67         int errn = 0;
  68         for (int j = 0; j < ITERS1; j++) {
  69             float[] x0 = new float[ARRLEN];
  70             run_loop_with_safepoint(x0, VALUE);
  71             for (int i = 0; i < ARRLEN; i++) {
  72                 if (x0[i] != VALUE * ITERS2) {
  73                     System.err.println("(" + j + "): " + "x0[" + i + "] = " + x0[i] + " != " + VALUE * ITERS2);
  74                     errn++;
  75                 }
  76                 x0[i] = 0.f; // Reset
  77             }
  78             if (errn > 0) break;
  79         }
  80 
  81         pleaseStop = true;
  82         try {
  83             th.join();
  84         } catch (InterruptedException e) {
  85             throw new Error("InterruptedException in main thread ", e);
  86         }
  87         return errn;
  88     }
  89 
  90     public static void main(String args[]) {
  91         int errn = test();
  92         System.err.println((errn > 0) ? "FAILED" : "PASSED");
  93     }
  94 }
  95