--- /dev/null 2014-10-17 05:25:30.601014041 +0200 +++ new/test/compiler/arraycopy/TestArrayCopyAsLoadsStores.java 2015-01-14 10:19:27.089523752 +0100 @@ -0,0 +1,560 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. 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. + */ + +/* + * @test + * @bug 6912521 + * @summary small array copy as loads/stores + * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:CompileCommand=dontinline,TestArrayCopyAsLoadsStores::m* -XX:TypeProfileLevel=200 TestArrayCopyAsLoadsStores + * @run main/othervm -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:CompileCommand=dontinline,TestArrayCopyAsLoadsStores::m* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressArrayCopyMacroNode -XX:TypeProfileLevel=200 TestArrayCopyAsLoadsStores + * + */ + +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.util.*; + +public class TestArrayCopyAsLoadsStores { + + public enum ArraySrc { + SMALL, + LARGE, + ZERO + } + + public enum ArrayDst { + NONE, + NEW, + SRC + } + + static class A { + } + + static class B extends A { + } + + static final A[] small_object_src = new A[5]; + static final A[] large_object_src = new A[10]; + static final A[] zero_object_src = new A[0]; + static final int[] small_int_src = new int[5]; + static final int[] large_int_src = new int[10]; + static final int[] zero_int_src = new int[0]; + static Object src; + + @Retention(RetentionPolicy.RUNTIME) + @interface Args { + ArraySrc src(); + ArrayDst dst() default ArrayDst.NONE; + int[] extra_args() default {}; + } + + // array clone should be compiled as loads/stores + @Args(src=ArraySrc.SMALL) + static A[] m1() throws CloneNotSupportedException { + return (A[])small_object_src.clone(); + } + + @Args(src=ArraySrc.SMALL) + static int[] m2() throws CloneNotSupportedException { + return (int[])small_int_src.clone(); + } + + // new array allocation should be optimized out + @Args(src=ArraySrc.SMALL) + static int m3() throws CloneNotSupportedException { + int[] array_clone = (int[])small_int_src.clone(); + return array_clone[0] + array_clone[1] + array_clone[2] + + array_clone[3] + array_clone[4]; + } + + // should not be compiled as loads/stores + @Args(src=ArraySrc.LARGE) + static int[] m4() throws CloneNotSupportedException { + return (int[])large_int_src.clone(); + } + + // check that array of length 0 is handled correctly + @Args(src=ArraySrc.ZERO) + static int[] m5() throws CloneNotSupportedException { + return (int[])zero_int_src.clone(); + } + + // array copy should be compiled as loads/stores + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW) + static void m6(int[] src, int[] dest) { + System.arraycopy(src, 0, dest, 0, 5); + } + + // array copy should not be compiled as loads/stores + @Args(src=ArraySrc.LARGE, dst=ArrayDst.NEW) + static void m7(int[] src, int[] dest) { + System.arraycopy(src, 0, dest, 0, 10); + } + + // array copy should be compiled as loads/stores + @Args(src=ArraySrc.SMALL) + static A[] m8(A[] src) { + src[0] = src[0]; // force null check + A[] dest = new A[5]; + System.arraycopy(src, 0, dest, 0, 5); + return dest; + } + + // array copy should not be compiled as loads/stores: we would + // need to emit GC barriers + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW) + static void m9(A[] src, A[] dest) { + System.arraycopy(src, 0, dest, 0, 5); + } + + // overlapping array regions: copy backward + @Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC) + static void m10(int[] src, int[] dest) { + System.arraycopy(src, 0, dest, 1, 4); + } + + static boolean m10_check(int[] src, int[] dest) { + boolean failure = false; + for (int i = 0; i < 5; i++) { + int j = Math.max(i - 1, 0); + if (dest[i] != src[j]) { + System.out.println("Test m10 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]); + failure = true; + } + } + return failure; + } + + // overlapping array regions: copy forward + @Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC) + static void m11(int[] src, int[] dest) { + System.arraycopy(src, 1, dest, 0, 4); + } + + static boolean m11_check(int[] src, int[] dest) { + boolean failure = false; + for (int i = 0; i < 5; i++) { + int j = Math.min(i + 1, 4); + if (dest[i] != src[j]) { + System.out.println("Test m11 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]); + failure = true; + } + } + return failure; + } + + // overlapping array region with unknown src/dest offsets: compiled must include both forward and backward copies + @Args(src=ArraySrc.SMALL, dst=ArrayDst.SRC, extra_args={0,1}) + static void m12(int[] src, int[] dest, int srcPos, int destPos) { + System.arraycopy(src, srcPos, dest, destPos, 4); + } + + static boolean m12_check(int[] src, int[] dest) { + boolean failure = false; + for (int i = 0; i < 5; i++) { + int j = Math.max(i - 1, 0); + if (dest[i] != src[j]) { + System.out.println("Test m10 failed for " + i + " src[" + j +"]=" + src[j] + ", dest[" + i + "]=" + dest[i]); + failure = true; + } + } + return failure; + } + + // Array allocation and copy should optimize out + @Args(src=ArraySrc.SMALL) + static int m13(int[] src) { + int[] dest = new int[5]; + System.arraycopy(src, 0, dest, 0, 5); + return dest[0] + dest[1] + dest[2] + dest[3] + dest[4]; + } + + // Check that copy of length 0 is handled correctly + @Args(src=ArraySrc.ZERO, dst=ArrayDst.NEW) + static void m14(int[] src, int[] dest) { + System.arraycopy(src, 0, dest, 0, 0); + } + + // copyOf should compile to loads/stores + @Args(src=ArraySrc.SMALL) + static A[] m15() { + return Arrays.copyOf(small_object_src, 5, A[].class); + } + + static Object[] helper16(int i) { + Object[] arr = null; + if ((i%2) == 0) { + arr = small_object_src; + } else { + arr = new Object[5]; + } + return arr; + } + + // CopyOf may need subtype check + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0}) + static A[] m16(A[] unused_src, int i) { + Object[] arr = helper16(i); + return Arrays.copyOf(arr, 5, A[].class); + } + + static Object[] helper17_1(int i) { + Object[] arr = null; + if ((i%2) == 0) { + arr = small_object_src; + } else { + arr = new Object[5]; + } + return arr; + } + + static A[] helper17_2(Object[] arr) { + return Arrays.copyOf(arr, 5, A[].class); + } + + // CopyOf may leverage type speculation + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0}) + static A[] m17(A[] unused_src, int i) { + Object[] arr = helper17_1(i); + return helper17_2(arr); + } + + static Object[] helper18_1(int i) { + Object[] arr = null; + if ((i%2) == 0) { + arr = small_object_src; + } else { + arr = new Object[5]; + } + return arr; + } + + static Object[] helper18_2(Object[] arr) { + return Arrays.copyOf(arr, 5, Object[].class); + } + + // CopyOf should not attempt to use type speculation if it's not needed + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0}) + static Object[] m18(A[] unused_src, int i) { + Object[] arr = helper18_1(i); + return helper18_2(arr); + } + + static Object[] helper19(int i) { + Object[] arr = null; + if ((i%2) == 0) { + arr = small_object_src; + } else { + arr = new Object[5]; + } + return arr; + } + + // CopyOf may need subtype check. Test is run to make type check + // fail and cause deoptimization. Next compilation should not + // compile as loads/stores because failed first compilation. + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NONE, extra_args={0}) + static A[] m19(A[] unused_src, int i) { + Object[] arr = helper19(i); + return Arrays.copyOf(arr, 5, A[].class); + } + + // copyOf for large array should not compile to loads/stores + @Args(src=ArraySrc.LARGE) + static A[] m20() { + return Arrays.copyOf(large_object_src, 10, A[].class); + } + + // check zero length copyOf is handled correctly + @Args(src=ArraySrc.ZERO) + static A[] m21() { + return Arrays.copyOf(zero_object_src, 0, A[].class); + } + + // Run with srcPos=0 for a 1st compile, then with incorrect value + // of srcPos to cause deoptimization, then with srcPos=0 for a 2nd + // compile. The 2nd compile shouldn't turn arraycopy into + // loads/stores because input arguments are no longer known to be + // valid. + @Args(src=ArraySrc.SMALL, dst=ArrayDst.NEW, extra_args={0}) + static void m22(int[] src, int[] dest, int srcPos) { + System.arraycopy(src, srcPos, dest, 0, 5); + } + + // copyOf should compile to loads/stores + @Args(src=ArraySrc.SMALL) + static A[] m23() { + return Arrays.copyOfRange(small_object_src, 1, 4, A[].class); + } + + static boolean m23_check(A[] src, A[] dest) { + boolean failure = false; + for (int i = 0; i < 3; i++) { + if (src[i+1] != dest[i]) { + System.out.println("Test m23 failed for " + i + " src[" + (i+1) +"]=" + dest[i] + ", dest[" + i + "]=" + dest[i]); + failure = true; + } + } + return failure; + } + + final HashMap tests = new HashMap<>(); + { + for (Method m : this.getClass().getDeclaredMethods()) { + if (m.getName().matches("m[0-9]+(_check)?")) { + assert(Modifier.isStatic(m.getModifiers())) : m; + tests.put(m.getName(), m); + } + } + } + + boolean success = true; + + void doTest(String name) throws Exception { + Method m = tests.get(name); + Method m_check = tests.get(name + "_check"); + Class[] paramTypes = m.getParameterTypes(); + Object[] params = new Object[paramTypes.length]; + Class retType = m.getReturnType(); + boolean isIntArray = (retType.isPrimitive() && !retType.equals(Void.TYPE)) || + (retType.equals(Void.TYPE) && paramTypes[0].getComponentType().isPrimitive()) || + (retType.isArray() && retType.getComponentType().isPrimitive()); + + Args args = m.getAnnotation(Args.class); + + Object src = null; + switch(args.src()) { + case SMALL: { + if (isIntArray) { + src = small_int_src; + } else { + src = small_object_src; + } + break; + } + case LARGE: { + if (isIntArray) { + src = large_int_src; + } else { + src = large_object_src; + } + break; + } + case ZERO: { + assert isIntArray; + if (isIntArray) { + src = zero_int_src; + } else { + src = zero_object_src; + } + break; + } + } + + for (int i = 0; i < 20000; i++) { + boolean failure = false; + + int p = 0; + + if (params.length > 0) { + if (isIntArray) { + params[0] = ((int[])src).clone(); + } else { + params[0] = ((A[])src).clone(); + } + p++; + } + + if (params.length > 1) { + switch(args.dst()) { + case NEW: { + if (isIntArray) { + params[1] = new int[((int[])params[0]).length]; + } else { + params[1] = new A[((A[])params[0]).length]; + } + p++; + break; + } + case SRC: { + params[1] = params[0]; + p++; + break; + } + case NONE: break; + } + } + + for (int j = 0; j < args.extra_args().length; j++) { + params[p+j] = args.extra_args()[j]; + } + + Object res = m.invoke(null, params); + + if (retType.isPrimitive() && !retType.equals(Void.TYPE)) { + int s = (int)res; + int sum = 0; + int[] int_res = (int[])src; + for (int j = 0; j < int_res.length; j++) { + sum += int_res[j]; + } + failure = (s != sum); + if (failure) { + System.out.println("Test " + name + " failed: result = " + s + " != " + sum); + } + } else { + Object dest = null; + if (!retType.equals(Void.TYPE)) { + dest = res; + } else { + dest = params[1]; + } + + if (m_check != null) { + failure = (boolean)m_check.invoke(null, new Object[] { src, dest }); + } else { + if (isIntArray) { + int[] int_res = (int[])src; + int[] int_dest = (int[])dest; + for (int j = 0; j < int_res.length; j++) { + if (int_res[j] != int_dest[j]) { + System.out.println("Test " + name + " failed for " + j + " src[" + j +"]=" + int_res[j] + ", dest[" + j + "]=" + int_dest[j]); + failure = true; + } + } + } else { + Object[] object_res = (Object[])src; + Object[] object_dest = (Object[])dest; + for (int j = 0; j < object_res.length; j++) { + if (object_res[j] != object_dest[j]) { + System.out.println("Test " + name + " failed for " + j + " src[" + j +"]=" + object_res[j] + ", dest[" + j + "]=" + object_dest[j]); + failure = true; + } + } + } + } + } + + if (failure) { + success = false; + break; + } + } + } + + public static void main(String[] args) throws Exception { + for (int i = 0; i < small_object_src.length; i++) { + small_object_src[i] = new A(); + } + + for (int i = 0; i < small_int_src.length; i++) { + small_int_src[i] = i; + } + + for (int i = 0; i < large_int_src.length; i++) { + large_int_src[i] = i; + } + + TestArrayCopyAsLoadsStores test = new TestArrayCopyAsLoadsStores(); + + test.doTest("m1"); + test.doTest("m2"); + test.doTest("m3"); + test.doTest("m4"); + test.doTest("m5"); + test.doTest("m6"); + test.doTest("m7"); + test.doTest("m8"); + test.doTest("m9"); + test.doTest("m10"); + test.doTest("m11"); + test.doTest("m12"); + test.doTest("m13"); + test.doTest("m14"); + test.doTest("m15"); + + // make both branches of the If appear taken + for (int i = 0; i < 20000; i++) { + helper16(i); + } + + test.doTest("m16"); + + // load class B so type check in m17 would not be simple comparison + B b = new B(); + // make both branches of the If appear taken + for (int i = 0; i < 20000; i++) { + helper17_1(i); + } + + test.doTest("m17"); + + // make both branches of the If appear taken + for (int i = 0; i < 20000; i++) { + helper18_1(i); + } + test.doTest("m18"); + + // make both branches of the If appear taken + for (int i = 0; i < 20000; i++) { + helper19(i); + } + + // Compile + for (int i = 0; i < 20000; i++) { + m19(null, 0); + } + + // force deopt + for (int i = 0; i < 10; i++) { + m19(null, 1); + } + + test.doTest("m19"); + + test.doTest("m20"); + test.doTest("m21"); + + // Compile + int[] dst = new int[small_int_src.length]; + for (int i = 0; i < 20000; i++) { + m22(small_int_src, dst, 0); + } + + // force deopt + for (int i = 0; i < 10; i++) { + try { + m22(small_int_src, dst, 5); + } catch(ArrayIndexOutOfBoundsException aioobe) {} + } + + test.doTest("m22"); + + test.doTest("m23"); + + if (!test.success) { + throw new RuntimeException("some tests failed"); + } + } +} --- old/src/share/vm/opto/macroArrayCopy.cpp 2015-01-14 10:19:27.314783128 +0100 +++ new/src/share/vm/opto/macroArrayCopy.cpp 2015-01-14 10:19:27.137212198 +0100 @@ -519,7 +519,7 @@ // Test S[] against D[], not S against D, because (probably) // the secondary supertype cache is less busy for S[] than S. // This usually only matters when D is an interface. - Node* not_subtype_ctrl = ac->is_arraycopy_validated() ? top() : + Node* not_subtype_ctrl = (ac->is_arraycopy_validated() || ac->is_copyof_validated() || ac->is_copyofrange_validated()) ? top() : Phase::gen_subtype_check(src_klass, dest_klass, ctrl, mem, &_igvn); // Plug failing path into checked_oop_disjoint_arraycopy if (not_subtype_ctrl != top()) { --- old/src/share/vm/opto/callnode.hpp 2015-01-14 10:19:27.383548334 +0100 +++ new/src/share/vm/opto/callnode.hpp 2015-01-14 10:19:27.190761563 +0100 @@ -1120,10 +1120,27 @@ ArrayCopyNode(Compile* C, bool alloc_tightly_coupled); + intptr_t get_length_if_constant(PhaseGVN *phase) const; int get_count(PhaseGVN *phase) const; static const TypePtr* get_address_type(PhaseGVN *phase, Node* n); Node* try_clone_instance(PhaseGVN *phase, bool can_reshape, int count); + Node* conv_I2X_offset(PhaseGVN *phase, Node* offset, const TypeAryPtr* ary_t); + bool prepare_array_copy(PhaseGVN *phase, bool can_reshape, + Node*& adr_src, Node*& base_src, Node*& adr_dest, Node*& base_dest, + BasicType& copy_type, const Type*& value_type, bool& disjoint_bases); + void array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, + bool disjoint_bases, Node*& forward_ctl, Node*& backward_ctl); + Node* array_copy_forward(PhaseGVN *phase, bool can_reshape, Node* ctl, + Node* start_mem_src, Node* start_mem_dest, + const TypePtr* atp_src, const TypePtr* atp_dest, + Node* adr_src, Node* base_src, Node* adr_dest, Node* base_dest, + BasicType copy_type, const Type* value_type, int count); + Node* array_copy_backward(PhaseGVN *phase, bool can_reshape, Node* ctl, + Node *start_mem_src, Node* start_mem_dest, + const TypePtr* atp_src, const TypePtr* atp_dest, + Node* adr_src, Node* base_src, Node* adr_dest, Node* base_dest, + BasicType copy_type, const Type* value_type, int count); bool finish_transform(PhaseGVN *phase, bool can_reshape, Node* ctl, Node *mem); @@ -1157,13 +1174,15 @@ bool is_clonebasic() const { assert(_kind != None, "should bet set"); return _kind == CloneBasic; } bool is_cloneoop() const { assert(_kind != None, "should bet set"); return _kind == CloneOop; } bool is_copyof() const { assert(_kind != None, "should bet set"); return _kind == CopyOf; } + bool is_copyof_validated() const { assert(_kind != None, "should bet set"); return _kind == CopyOf && _arguments_validated; } bool is_copyofrange() const { assert(_kind != None, "should bet set"); return _kind == CopyOfRange; } + bool is_copyofrange_validated() const { assert(_kind != None, "should bet set"); return _kind == CopyOfRange && _arguments_validated; } void set_arraycopy(bool validated) { assert(_kind == None, "shouldn't bet set yet"); _kind = ArrayCopy; _arguments_validated = validated; } void set_clonebasic() { assert(_kind == None, "shouldn't bet set yet"); _kind = CloneBasic; } void set_cloneoop() { assert(_kind == None, "shouldn't bet set yet"); _kind = CloneOop; } - void set_copyof() { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOf; _arguments_validated = false; } - void set_copyofrange() { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOfRange; _arguments_validated = false; } + void set_copyof(bool validated) { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOf; _arguments_validated = validated; } + void set_copyofrange(bool validated) { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOfRange; _arguments_validated = validated; } virtual int Opcode() const; virtual uint size_of() const; // Size is bigger --- old/src/share/vm/opto/callnode.cpp 2015-01-14 10:19:27.399708264 +0100 +++ new/src/share/vm/opto/callnode.cpp 2015-01-14 10:19:27.214244981 +0100 @@ -1875,28 +1875,73 @@ } #endif +intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const { + // check that length is constant + Node* length = in(ArrayCopyNode::Length); + const Type* length_type = phase->type(length); + + if (length_type == Type::TOP) { + return -1; + } + + intptr_t length_const = -1; + if (is_clonebasic()) { + const TypeX* length_int_type = length_type->is_intptr_t(); + if (length_int_type->is_con()) { + length_const = length_int_type->get_con(); + } + } else if (is_arraycopy() || is_copyof() || is_copyofrange()) { + const TypeInt* length_int_type = length_type->is_int(); + if (length_int_type->is_con()) { + length_const = length_int_type->get_con(); + } + } else { + fatal("unexpected array copy type"); + } + + return length_const; +} + int ArrayCopyNode::get_count(PhaseGVN *phase) const { Node* src = in(ArrayCopyNode::Src); const Type* src_type = phase->type(src); - assert(is_clonebasic(), "unexpected arraycopy type"); - if (src_type->isa_instptr()) { - const TypeInstPtr* inst_src = src_type->is_instptr(); - ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); - // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected - // fields into account. They are rare anyway so easier to simply - // skip instances with injected fields. - if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) { + if (is_clonebasic()) { + if (src_type->isa_instptr()) { + const TypeInstPtr* inst_src = src_type->is_instptr(); + ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); + // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected + // fields into account. They are rare anyway so easier to simply + // skip instances with injected fields. + if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) { + return -1; + } + int nb_fields = ik->nof_nonstatic_fields(); + return nb_fields; + } else { + const TypeAryPtr* ary_src = src_type->isa_aryptr(); + assert (ary_src != NULL, "not an array or instance?"); + // clone passes a length as a rounded number of longs. If we're + // cloning an array we'll do it element by element. If the + // length input to ArrayCopyNode is constant, length of input + // array must be too. + + assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con(), "inconsistent"); + + if (ary_src->size()->is_con()) { + return ary_src->size()->get_con(); + } return -1; } - int nb_fields = ik->nof_nonstatic_fields(); - return nb_fields; - } - return -1; + } + + return get_length_if_constant(phase); } Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) { - assert(is_clonebasic(), "unexpected arraycopy type"); + if (!is_clonebasic()) { + return NULL; + } Node* src = in(ArrayCopyNode::Src); Node* dest = in(ArrayCopyNode::Dest); @@ -1913,7 +1958,11 @@ MergeMemNode* mem = MergeMemNode::make(in_mem); - const TypeInstPtr* inst_src = src_type->is_instptr(); + const TypeInstPtr* inst_src = src_type->isa_instptr(); + + if (inst_src == NULL) { + return NULL; + } if (!inst_src->klass_is_exact()) { ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); @@ -1959,38 +2008,288 @@ return mem; } -bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape, - Node* ctl, Node *mem) { - if (can_reshape) { - PhaseIterGVN* igvn = phase->is_IterGVN(); - assert(is_clonebasic(), "unexpected arraycopy type"); - Node* out_mem = proj_out(TypeFunc::Memory); +Node* ArrayCopyNode::conv_I2X_offset(PhaseGVN *phase, Node* offset, const TypeAryPtr* ary_t) { +#ifdef _LP64 + // see comment in GraphKit::array_element_address + int index_max = ary_t->size()->_hi - 1; + const TypeLong* lidxtype = TypeLong::make(CONST64(0), index_max, Type::WidenMax); + return phase->transform(new ConvI2LNode(offset, lidxtype)); +#else + return offset; +#endif +} + +bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape, + Node*& adr_src, + Node*& base_src, + Node*& adr_dest, + Node*& base_dest, + BasicType& copy_type, + const Type*& value_type, + bool& disjoint_bases) { + Node* src = in(ArrayCopyNode::Src); + Node* dest = in(ArrayCopyNode::Dest); + const Type* src_type = phase->type(src); + const TypeAryPtr* ary_src = src_type->isa_aryptr(); + + if (is_arraycopy() || is_copyofrange() || is_copyof()) { + const Type* dest_type = phase->type(dest); + const TypeAryPtr* ary_dest = dest_type->isa_aryptr(); + Node* src_offset = in(ArrayCopyNode::SrcPos); + Node* dest_offset = in(ArrayCopyNode::DestPos); + + // newly allocated object is guaranteed to not overlap with source object + disjoint_bases = is_alloc_tightly_coupled(); + + if (ary_src == NULL || ary_src->klass() == NULL || + ary_dest == NULL || ary_dest->klass() == NULL) { + // We don't know if arguments are arrays + return false; + } + + BasicType src_elem = ary_src->klass()->as_array_klass()->element_type()->basic_type(); + BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type(); + if (src_elem == T_ARRAY) src_elem = T_OBJECT; + if (dest_elem == T_ARRAY) dest_elem = T_OBJECT; - if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() || - out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) { - assert(!GraphKit::use_ReduceInitialCardMarks(), "can only happen with card marking"); + if (src_elem != dest_elem || dest_elem == T_VOID) { + // We don't know if arguments are arrays of the same type return false; } - igvn->replace_node(out_mem->raw_out(0), mem); + if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() || !GraphKit::use_ReduceInitialCardMarks())) { + // It's an object array copy but we can't emit the card marking + // that is needed + return false; + } + + value_type = ary_src->elem(); - Node* out_ctl = proj_out(TypeFunc::Control); - igvn->replace_node(out_ctl, ctl); + base_src = src; + base_dest = dest; + + uint shift = exact_log2(type2aelembytes(dest_elem)); + uint header = arrayOopDesc::base_offset_in_bytes(dest_elem); + + adr_src = src; + adr_dest = dest; + + src_offset = conv_I2X_offset(phase, src_offset, ary_src); + dest_offset = conv_I2X_offset(phase, dest_offset, ary_src); + + Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift))); + Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift))); + + adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale)); + adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale)); + + adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header)); + adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header)); + + adr_src = phase->transform(adr_src); + adr_dest = phase->transform(adr_dest); + + copy_type = dest_elem; + } else { + assert (is_clonebasic(), "should be"); + + disjoint_bases = true; + assert(src->is_AddP(), "should be base + off"); + assert(dest->is_AddP(), "should be base + off"); + adr_src = src; + base_src = src->in(AddPNode::Base); + adr_dest = dest; + base_dest = dest->in(AddPNode::Base); + + assert(phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con() == phase->type(dest->in(AddPNode::Offset))->is_intptr_t()->get_con(), "same start offset?"); + BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type(); + if (elem == T_ARRAY) elem = T_OBJECT; + + int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con(); + assert(diff >= 0, "clone should not start after 1st array element"); + if (diff > 0) { + adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff))); + adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff))); + } + + copy_type = elem; + value_type = ary_src->elem(); } return true; } +const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) { + const Type* at = phase->type(n); + assert(at != Type::TOP, "unexpected type"); + const TypePtr* atp = at->isa_ptr(); + // adjust atp to be the correct array element address type + atp = atp->add_offset(Type::OffsetBot); + return atp; +} -Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) { +void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, Node*& forward_ctl, Node*& backward_ctl) { + Node* ctl = in(TypeFunc::Control); + if (!disjoint_bases) { + Node* src_offset = in(ArrayCopyNode::SrcPos); + Node* dest_offset = in(ArrayCopyNode::DestPos); + assert(src_offset != NULL && dest_offset != NULL, "should be"); + Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset)); + Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt)); + IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN); + + phase->transform(iff); + + forward_ctl = phase->transform(new IfFalseNode(iff)); + backward_ctl = phase->transform(new IfTrueNode(iff)); + } else { + forward_ctl = ctl; + } +} + +Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase, + bool can_reshape, + Node* forward_ctl, + Node* start_mem_src, + Node* start_mem_dest, + const TypePtr* atp_src, + const TypePtr* atp_dest, + Node* adr_src, + Node* base_src, + Node* adr_dest, + Node* base_dest, + BasicType copy_type, + const Type* value_type, + int count) { + Node* mem = phase->C->top(); + if (!forward_ctl->is_top()) { + // copy forward + mem = start_mem_dest; + + if (count > 0) { + Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered); + v = phase->transform(v); + mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered); + mem = phase->transform(mem); + for (int i = 1; i < count; i++) { + Node* off = phase->MakeConX(type2aelembytes(copy_type) * i); + Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off)); + Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off)); + v = LoadNode::make(*phase, forward_ctl, mem, next_src, atp_src, value_type, copy_type, MemNode::unordered); + v = phase->transform(v); + mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered); + mem = phase->transform(mem); + } + } else if(can_reshape) { + PhaseIterGVN* igvn = phase->is_IterGVN(); + igvn->_worklist.push(adr_src); + igvn->_worklist.push(adr_dest); + } + } + return mem; +} +Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase, + bool can_reshape, + Node* backward_ctl, + Node* start_mem_src, + Node* start_mem_dest, + const TypePtr* atp_src, + const TypePtr* atp_dest, + Node* adr_src, + Node* base_src, + Node* adr_dest, + Node* base_dest, + BasicType copy_type, + const Type* value_type, + int count) { + Node* mem = phase->C->top(); + if (!backward_ctl->is_top()) { + // copy backward + mem = start_mem_dest; + + if (count > 0) { + for (int i = count-1; i >= 1; i--) { + Node* off = phase->MakeConX(type2aelembytes(copy_type) * i); + Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off)); + Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off)); + Node* v = LoadNode::make(*phase, backward_ctl, mem, next_src, atp_src, value_type, copy_type, MemNode::unordered); + v = phase->transform(v); + mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered); + mem = phase->transform(mem); + } + Node* v = LoadNode::make(*phase, backward_ctl, mem, adr_src, atp_src, value_type, copy_type, MemNode::unordered); + v = phase->transform(v); + mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered); + mem = phase->transform(mem); + } else if(can_reshape) { + PhaseIterGVN* igvn = phase->is_IterGVN(); + igvn->_worklist.push(adr_src); + igvn->_worklist.push(adr_dest); + } + } + return mem; +} + +bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape, + Node* ctl, Node *mem) { + if (can_reshape) { + PhaseIterGVN* igvn = phase->is_IterGVN(); + igvn->set_delay_transform(false); + if (is_clonebasic()) { + Node* out_mem = proj_out(TypeFunc::Memory); + + if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() || + out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) { + assert(!GraphKit::use_ReduceInitialCardMarks(), "can only happen with card marking"); + return false; + } + + igvn->replace_node(out_mem->raw_out(0), mem); + + Node* out_ctl = proj_out(TypeFunc::Control); + igvn->replace_node(out_ctl, ctl); + } else { + // replace fallthrough projections of the ArrayCopyNode by the + // new memory, control and the input IO. + CallProjections callprojs; + extract_projections(&callprojs, true); + + igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O)); + igvn->replace_node(callprojs.fallthrough_memproj, mem); + igvn->replace_node(callprojs.fallthrough_catchproj, ctl); + + // The ArrayCopyNode is not disconnected. It still has the + // projections for the exception case. Replace current + // ArrayCopyNode with a dummy new one with a top() control so + // that this part of the graph stays consistent but is + // eventually removed. + + set_req(0, phase->C->top()); + remove_dead_region(phase, can_reshape); + } + } else { + if (in(TypeFunc::Control) != ctl) { + // we can't return new memory and control from Ideal at parse time + assert(!is_clonebasic(), "added control for clone?"); + return NULL; + } + } + return true; +} + + +Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) { if (StressArrayCopyMacroNode && !can_reshape) return NULL; // See if it's a small array copy and we can inline it as // loads/stores // Here we can only do: + // - arraycopy if all arguments were validated before and we don't + // need card marking // - clone for which we don't need to do card marking - if (!is_clonebasic()) { + if (!is_clonebasic() && !is_arraycopy_validated() && + !is_copyofrange_validated() && !is_copyof_validated()) { return NULL; } @@ -2005,5 +2304,92 @@ } Node* mem = try_clone_instance(phase, can_reshape, count); + if (mem != NULL) { + return mem; + } + + Node* adr_src = NULL; + Node* base_src = NULL; + Node* adr_dest = NULL; + Node* base_dest = NULL; + BasicType copy_type = T_ILLEGAL; + const Type* value_type = NULL; + bool disjoint_bases = false; + + if (!prepare_array_copy(phase, can_reshape, + adr_src, base_src, adr_dest, base_dest, + copy_type, value_type, disjoint_bases)) { + return NULL; + } + + Node* src = in(ArrayCopyNode::Src); + Node* dest = in(ArrayCopyNode::Dest); + const TypePtr* atp_src = get_address_type(phase, src); + const TypePtr* atp_dest = get_address_type(phase, dest); + uint alias_idx_src = phase->C->get_alias_index(atp_src); + uint alias_idx_dest = phase->C->get_alias_index(atp_dest); + + Node *in_mem = in(TypeFunc::Memory); + Node *start_mem_src = in_mem; + Node *start_mem_dest = in_mem; + if (in_mem->is_MergeMem()) { + start_mem_src = in_mem->as_MergeMem()->memory_at(alias_idx_src); + start_mem_dest = in_mem->as_MergeMem()->memory_at(alias_idx_dest); + } + + + if (can_reshape) { + assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms"); + phase->is_IterGVN()->set_delay_transform(true); + } + + Node* backward_ctl = phase->C->top(); + Node* forward_ctl = phase->C->top(); + array_copy_test_overlap(phase, can_reshape, disjoint_bases, forward_ctl, backward_ctl); + + Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl, + start_mem_src, start_mem_dest, + atp_src, atp_dest, + adr_src, base_src, adr_dest, base_dest, + copy_type, value_type, count); + + Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl, + start_mem_src, start_mem_dest, + atp_src, atp_dest, + adr_src, base_src, adr_dest, base_dest, + copy_type, value_type, count); + + Node* ctl = NULL; + if (!forward_ctl->is_top() && !backward_ctl->is_top()) { + ctl = new RegionNode(3); + mem = new PhiNode(ctl, Type::MEMORY, atp_dest); + ctl->init_req(1, forward_ctl); + mem->init_req(1, forward_mem); + ctl->init_req(2, backward_ctl); + mem->init_req(2, backward_mem); + ctl = phase->transform(ctl); + mem = phase->transform(mem); + } else if (!forward_ctl->is_top()) { + ctl = forward_ctl; + mem = forward_mem; + } else { + assert(!backward_ctl->is_top(), "no copy?"); + ctl = backward_ctl; + mem = backward_mem; + } + + if (can_reshape) { + assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms"); + phase->is_IterGVN()->set_delay_transform(false); + } + + MergeMemNode* out_mem = MergeMemNode::make(in_mem); + out_mem->set_memory_at(alias_idx_dest, mem); + mem = out_mem; + + if (!finish_transform(phase, can_reshape, ctl, mem)) { + return NULL; + } + return mem; } --- old/src/share/vm/opto/library_call.cpp 2015-01-14 10:19:27.407217609 +0100 +++ new/src/share/vm/opto/library_call.cpp 2015-01-14 10:19:27.165629666 +0100 @@ -3949,18 +3949,50 @@ // Extreme case: Arrays.copyOf((Integer[])x, 10, String[].class). // This will fail a store-check if x contains any non-nulls. - Node* alloc = tightly_coupled_allocation(newcopy, NULL); + if (_gvn.type(klass_node)->singleton()) { + ciKlass* subk = _gvn.type(load_object_klass(original))->is_klassptr()->klass(); + ciKlass* superk = _gvn.type(klass_node)->is_klassptr()->klass(); - ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, alloc != NULL, + int test = C->static_subtype_check(superk, subk); + if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) { + const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr(); + if (t_original->speculative_type() != NULL) { + original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true); + } + } + } + + bool validated = false; + // Reason_class_check rather than Reason_intrinsic because we + // want to intrinsify even if this traps. + if (!too_many_traps(Deoptimization::Reason_class_check)) { + Node* not_subtype_ctrl = gen_subtype_check(load_object_klass(original), + klass_node); + + if (not_subtype_ctrl != top()) { + PreserveJVMState pjvms(this); + set_control(not_subtype_ctrl); + uncommon_trap(Deoptimization::Reason_class_check, + Deoptimization::Action_make_not_entrant); + assert(stopped(), "Should be stopped"); + } + validated = true; + } + + ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, load_object_klass(original), klass_node); if (!is_copyOfRange) { - ac->set_copyof(); + ac->set_copyof(validated); } else { - ac->set_copyofrange(); + ac->set_copyofrange(validated); } Node* n = _gvn.transform(ac); - assert(n == ac, "cannot disappear"); - ac->connect_outputs(this); + if (n == ac) { + ac->connect_outputs(this); + } else { + assert(validated, "shouldn't transform if all arguments not validated"); + set_all_memory(n); + } } } // original reexecute is set back here