Path: blob/master/test/hotspot/jtreg/compiler/arraycopy/TestCloneAccess.java
64474 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 824879126* @summary Test cloning with more than 8 (=ArrayCopyLoadStoreMaxElem) where loads are wrongly replaced by zero.27* @requires vm.compiler2.enabled | vm.graal.enabled28*29* @run main/othervm -XX:-ReduceBulkZeroing30* -XX:CompileCommand=dontinline,compiler.arraycopy.TestCloneAccess::*31* compiler.arraycopy.TestCloneAccess32* @run main/othervm -XX:-ReduceBulkZeroing -XX:-ReduceInitialCardMarks33* -XX:CompileCommand=dontinline,compiler.arraycopy.TestCloneAccess::*34* compiler.arraycopy.TestCloneAccess35*/36package compiler.arraycopy;3738public class TestCloneAccess {39static int test(E src) throws CloneNotSupportedException {40// ArrayCopyNode for this clone is not inlined since there are more than 8 (=ArrayCopyLoadStoreMaxElem) fields41src.i1 = 3;42E dest = (E)src.clone();43dontInline(dest.i1, dest.i2);4445// Both loads are wrongly optimized and replaced by a constant zero. LoadNode::Value() tries to find out if a load46// is done from a freshly-allocated object. If that is the case, the load can be replaced by the default value zero.47// However, in this case, the Allocation node belongs to an ArrayCopyNode which is responsible for initializing 'dest'.48// If -XX:-ReduceBulkZeroing is set, the InitializationNode of the allocation does not bail out of this optimization49// which results in a replacement of both loads by zero. This is addressed by this fix. If -XX:+ReduceBulkZeroing is50// set, then we already bail out and perform the load correctly.51return dest.i1 + dest.i2;52}5354public static void main(String[] args) throws Exception {55E e = new E();56e.i2 = 4;57int res = 0;58for (int i = 0; i < 20000; i++) {59res = test(e);60if (res != 7 || e.i1 != 3 || e.i2 != 4) {61throw new RuntimeException("Wrong result! Expected: res = 7, e.i1 = 3, e.i2 = 4 "62+ "but got: res = " + res + ", e.i1 = " + e.i1 + ", e.i2 = " + e.i2);63}64}65}6667// Dont inline this method68public static void dontInline(int i1, int i2) {69}70}7172class E implements Cloneable {73/*74* Need more than 8 (=ArrayCopyLoadStoreMaxElem) fields75*/76int i1;77int i2;78int i3;79int i4;80int i5;81int i6;82int i7;83int i8;84int i9;8586E() {87i1 = 0;88i2 = 1;89i3 = 2;90i4 = 3;91i5 = 4;92i6 = 5;93i7 = 6;94i8 = 7;95i9 = 8;96}9798public Object clone() throws CloneNotSupportedException {99return super.clone();100}101}102103104105