1 /*
  2  * Copyright (c) 2023, 2024, 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 8313667
 27  * @summary Test that GenZ uses correct array copy stub for flat value class arrays when expanding ArrayCopyNode.
 28  * @requires vm.gc.ZSinglegen
 29  * @library /test/lib
 30  * @enablePreview
 31  * @modules java.base/jdk.internal.value
 32  *          java.base/jdk.internal.vm.annotation
 33  * @run main/othervm -Xbatch -XX:+UseZGC -XX:-ZGenerational
 34  *                   -XX:CompileCommand=exclude,compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC::check
 35  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC::test*
 36  *                   compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC
 37  */
 38 
 39 package compiler.valhalla.inlinetypes;
 40 
 41 import jdk.test.lib.Asserts;
 42 import jdk.test.lib.Utils;
 43 
 44 import jdk.internal.value.ValueClass;
 45 import jdk.internal.vm.annotation.ImplicitlyConstructible;
 46 import jdk.internal.vm.annotation.LooselyConsistentValue;
 47 import jdk.internal.vm.annotation.NullRestricted;
 48 
 49 public class TestWrongFlatArrayCopyStubWithZGC {
 50 
 51     public static void main(String[] args) {
 52         ValueWithLong[] arrWithLong = (ValueWithLong[])ValueClass.newNullRestrictedArray(ValueWithLong.class, 3);
 53         arrWithLong[0] = new ValueWithLong(0x408BE000000fffffL);
 54         arrWithLong[1] = new ValueWithLong(0x408BE0000000000L);
 55         long randomValue = Utils.getRandomInstance().nextLong();
 56         arrWithLong[2] = new ValueWithLong(randomValue);
 57 
 58         for (int i = 0; i < 10000; i++) {
 59             ValueWithLong[] result = testLong(arrWithLong);
 60             check(result[0].l, 0x408BE000000fffffL);
 61             check(result[1].l, 0x408BE0000000000L);
 62             check(result[2].l, randomValue);
 63         }
 64 
 65         ValueWithOop[] arrWithOop = (ValueWithOop[])ValueClass.newNullRestrictedArray(ValueWithOop.class, 2);
 66         arrWithOop[0] = new ValueWithOop();
 67         arrWithOop[1] = new ValueWithOop();
 68 
 69         for (int i = 0; i < 10000; i++) {
 70             testOop(arrWithOop);
 71         }
 72     }
 73 
 74     static void check(long result, long expected) {
 75         Asserts.assertEQ(result, expected);
 76     }
 77 
 78     static ValueWithLong[] testLong(ValueWithLong[] arr) {
 79         return arr.clone();
 80     }
 81 
 82     static ValueWithOop[] testOop(ValueWithOop[] arr) {
 83         return arr.clone();
 84     }
 85 }
 86 
 87 @ImplicitlyConstructible
 88 @LooselyConsistentValue
 89 value class ValueWithLong {
 90     long l;
 91 
 92     public ValueWithLong(long l) {
 93         this.l = l;
 94     }
 95 }
 96 
 97 @ImplicitlyConstructible
 98 @LooselyConsistentValue
 99 value class ValueWithOop {
100     Object v;
101 
102     public ValueWithOop() {
103         this.v = new Object();
104     }
105 }
106