diff --git a/src/hotspot/share/opto/library_call.cpp b/src/hotspot/share/opto/library_call.cpp index e9878cc0c77..6ddb63a9a2d 100644 --- a/src/hotspot/share/opto/library_call.cpp +++ b/src/hotspot/share/opto/library_call.cpp @@ -1575,9 +1575,14 @@ bool LibraryCallKit::inline_string_toBytesU() { Node* src_start = array_element_address(value, offset, T_CHAR); Node* dst_start = basic_plus_adr(newcopy, arrayOopDesc::base_offset_in_bytes(T_BYTE)); - // Check if src array address is aligned to HeapWordSize (dst is always aligned) - const TypeInt* toffset = gvn().type(offset)->is_int(); - bool aligned = toffset->is_con() && ((toffset->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); + // Check if dst array address is aligned to HeapWordSize + bool aligned = (arrayOopDesc::base_offset_in_bytes(T_BYTE) % HeapWordSize == 0); + // If true, then check if src array address is aligned to HeapWordSize + if (aligned) { + const TypeInt* toffset = gvn().type(offset)->is_int(); + aligned = toffset->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_CHAR) + + toffset->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); + } // Figure out which arraycopy runtime method to call (disjoint, uninitialized). const char* copyfunc_name = "arraycopy"; @@ -1658,8 +1663,8 @@ bool LibraryCallKit::inline_string_getCharsU() { // Check if array addresses are aligned to HeapWordSize const TypeInt* tsrc = gvn().type(src_begin)->is_int(); const TypeInt* tdst = gvn().type(dst_begin)->is_int(); - bool aligned = tsrc->is_con() && ((tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) && - tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); + bool aligned = tsrc->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_BYTE) + tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) && + tdst->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_CHAR) + tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0); // Figure out which arraycopy runtime method to call (disjoint, uninitialized). const char* copyfunc_name = "arraycopy"; diff --git a/src/hotspot/share/opto/stringopts.cpp b/src/hotspot/share/opto/stringopts.cpp index 641634b906e..28936a04219 100644 --- a/src/hotspot/share/opto/stringopts.cpp +++ b/src/hotspot/share/opto/stringopts.cpp @@ -1473,9 +1473,14 @@ void PhaseStringOpts::arraycopy(GraphKit& kit, IdealKit& ideal, Node* src_array, Node* src_ptr = __ array_element_address(src_array, __ intcon(0), T_BYTE); Node* dst_ptr = __ array_element_address(dst_array, start, T_BYTE); - // Check if destination address is aligned to HeapWordSize - const TypeInt* tdst = __ gvn().type(start)->is_int(); - bool aligned = tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0); + // Check if src array address is aligned to HeapWordSize + bool aligned = (arrayOopDesc::base_offset_in_bytes(T_BYTE) % HeapWordSize == 0); + // If true, then check if dst array address is aligned to HeapWordSize + if (aligned) { + const TypeInt* tdst = __ gvn().type(start)->is_int(); + aligned = tdst->is_con() && ((arrayOopDesc::base_offset_in_bytes(T_BYTE) + + tdst->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0); + } // Figure out which arraycopy runtime method to call (disjoint, uninitialized). const char* copyfunc_name = "arraycopy"; address copyfunc_addr = StubRoutines::select_arraycopy_function(elembt, aligned, true, copyfunc_name, true); diff --git a/test/hotspot/jtreg/compiler/c2/irTests/stringopts/TestArrayCopySelect.java b/test/hotspot/jtreg/compiler/c2/irTests/stringopts/TestArrayCopySelect.java new file mode 100644 index 00000000000..4743f61ac39 --- /dev/null +++ b/test/hotspot/jtreg/compiler/c2/irTests/stringopts/TestArrayCopySelect.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2025, Institute of Software, Chinese Academy of Sciences. + * All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package compiler.c2.irTests.stringopts; + +import compiler.lib.ir_framework.*; + +/** + * @test + * @bug 8359270 + * @requires vm.debug == true & vm.compiler2.enabled + * @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="riscv64" | os.arch=="aarch64" + * @summary C2: alignment check should consider base offset when emitting arraycopy runtime call. + * @library /test/lib / + * @run driver compiler.c2.irTests.stringopts.TestArrayCopySelect + */ + +public class TestArrayCopySelect { + + public static final String input_strU = "\u0f21\u0f22\u0f23\u0f24\u0f25\u0f26\u0f27\u0f28"; + public static final char[] input_arrU = new char[] {'\u0f21', '\u0f22', '\u0f23', '\u0f24', + '\u0f25', '\u0f26', '\u0f27', '\u0f28'}; + + public static String output_strU; + public static char[] output_arrU; + + public static void main(String[] args) { + TestFramework.runWithFlags("-XX:-UseCompactObjectHeaders", + "-XX:-CompactStrings", + "-XX:CompileCommand=inline,java.lang.StringBuilder::toString", + "-XX:CompileCommand=inline,java.lang.StringUTF16::getChars", + "-XX:CompileCommand=inline,java.lang.StringUTF16::toBytes"); + + TestFramework.runWithFlags("-XX:+UseCompactObjectHeaders", + "-XX:-CompactStrings", + "-XX:CompileCommand=inline,java.lang.StringBuilder::toString", + "-XX:CompileCommand=inline,java.lang.StringUTF16::getChars", + "-XX:CompileCommand=inline,java.lang.StringUTF16::toBytes"); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "false"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"}) + static void testSBToStringAligned() { + // Exercise the StringBuilder.toString API + StringBuilder sb = new StringBuilder(input_strU); + output_strU = sb.append(input_strU).toString(); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "true"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", "0"}) + static void testSBToStringUnAligned() { + // Exercise the StringBuilder.toString API + StringBuilder sb = new StringBuilder(input_strU); + output_strU = sb.append(input_strU).toString(); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "false"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"}) + static void testStrUGetCharsAligned() { + // Exercise the StringUTF16.getChars API + output_arrU = input_strU.toCharArray(); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "true"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", "0"}) + static void testStrUGetCharsUnAligned() { + // Exercise the StringUTF16.getChars API + output_arrU = input_strU.toCharArray(); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "false"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", ">0"}) + static void testStrUtoBytesAligned() { + // Exercise the StringUTF16.toBytes API + output_strU = String.valueOf(input_arrU); + } + + @Test + @Warmup(10000) + @IR(applyIf = {"UseCompactObjectHeaders", "true"}, + counts = {IRNode.CALL_OF, "arrayof_jshort_disjoint_arraycopy", "0"}) + static void testStrUtoBytesUnAligned() { + // Exercise the StringUTF16.toBytes API + output_strU = String.valueOf(input_arrU); + } + +}