1 /*
 2  * Copyright (c) 2025, Institute of Software, Chinese Academy of Sciences.
 3  * All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 package compiler.c2.irTests.stringopts;
26 
27 import compiler.lib.ir_framework.*;
28 
29 /**
30  * @test
31  * @bug 8359270
32  * @requires vm.debug == true & vm.compiler2.enabled
33  * @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="riscv64" | os.arch=="aarch64"
34  * @summary C2: alignment check should consider base offset when emitting arraycopy runtime call.
35  * @library /test/lib /
36  * @run driver compiler.c2.irTests.stringopts.TestArrayCopySelect
37  */
38 
39 public class TestArrayCopySelect {
40 
41     public static final String input_strU = "\u0f21\u0f22\u0f23\u0f24\u0f25\u0f26\u0f27\u0f28";
42     public static final char[] input_arrU = new char[] {'\u0f21', '\u0f22', '\u0f23', '\u0f24',
43                                                         '\u0f25', '\u0f26', '\u0f27', '\u0f28'};
44 
45     public static String output_strU;
46     public static char[] output_arrU;
47 
48     public static void main(String[] args) {
49         TestFramework.runWithFlags("-XX:-UseCompactObjectHeaders",
50                                    "-XX:-CompactStrings",
51                                    "-XX:CompileCommand=inline,java.lang.StringBuilder::toString",
52                                    "-XX:CompileCommand=inline,java.lang.StringUTF16::getChars",
53                                    "-XX:CompileCommand=inline,java.lang.StringUTF16::toBytes");
54 
55         TestFramework.runWithFlags("-XX:+UseCompactObjectHeaders",
56                                    "-XX:-CompactStrings",
57                                    "-XX:CompileCommand=inline,java.lang.StringBuilder::toString",
58                                    "-XX:CompileCommand=inline,java.lang.StringUTF16::getChars",
59                                    "-XX:CompileCommand=inline,java.lang.StringUTF16::toBytes");
60     }
61 
62     @Test
63     @Warmup(10000)
64     @IR(counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"})
65     static void testSBToStringAligned() {
66         // Exercise the StringBuilder.toString API
67         StringBuilder sb = new StringBuilder(input_strU);
68         output_strU = sb.append(input_strU).toString();
69     }
70 
71     @Test
72     @Warmup(10000)
73     @IR(counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"})
74     static void testStrUGetCharsAligned() {
75         // Exercise the StringUTF16.getChars API
76         output_arrU = input_strU.toCharArray();
77     }
78 
79     @Test
80     @Warmup(10000)
81     @IR(counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"})
82     static void testStrUtoBytesAligned() {
83         // Exercise the StringUTF16.toBytes API
84         output_strU = String.valueOf(input_arrU);
85     }
86 
87 }