1 /*
   2  * Copyright (c) 2021, 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  * @bug 8262739
  27  * @summary Test correct insertion of anti-dependencies after String inflation.
  28  * @run main/othervm -Xbatch
  29  *                   compiler.controldependency.TestAntiDependencyAfterStringInflation
  30  */
  31 
  32 package compiler.controldependency;
  33 
  34 public class TestAntiDependencyAfterStringInflation {
  35 
  36     static String reverseString(String str) {
  37         int size = str.length();
  38         char[] buffer = new char[size];
  39         reverse(str, buffer, size);
  40         return new String(buffer, 0, size);
  41     }
  42 
  43     static void reverse(String str, char[] buffer, int size) {
  44         // Inflate String.value byte[] to char[]
  45         str.getChars(0, size, buffer, 0);
  46         // Reverse String by copying buffer elements. This will fail
  47         // if C2 does not insert anti-dependencies between loads/stores.
  48         int half = size / 2;
  49         for (int l = 0, r = size - 1; l < half; l++, r--) {
  50             char tmp = buffer[l];
  51             buffer[l] = buffer[r];
  52             buffer[r] = tmp;
  53         }
  54     }
  55 
  56     public static void main(String[] args) throws Exception {
  57         for (int i = 0; i < 50_000; i++) {
  58             String res = reverseString("0123456789");
  59             if (!res.equals("9876543210")) {
  60                 throw new RuntimeException("Unexpected result: " + res);
  61             }
  62         }
  63     }
  64 }