1 /*
   2  * Copyright (c) 2015, 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 package org.graalvm.compiler.core.test;
  25 
  26 import java.util.Arrays;
  27 
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.java.NewArrayNode;
  32 
  33 public class CopyOfVirtualizationTest extends GraalCompilerTest {
  34 
  35     @Override
  36     protected boolean checkMidTierGraph(StructuredGraph graph) {
  37         assertTrue(graph.getNodes().filter(node -> node instanceof NewArrayNode).count() == 0, "shouldn't require allocation in %s", graph);
  38         return super.checkMidTierGraph(graph);
  39     }
  40 
  41     public byte byteCopyOfVirtualization(int index) {
  42         byte[] array = new byte[]{1, 2, 3, 4};
  43         return Arrays.copyOf(array, array.length)[index];
  44     }
  45 
  46     public short shortCopyOfVirtualization(int index) {
  47         short[] array = new short[]{1, 2, 3, 4};
  48         return Arrays.copyOf(array, array.length)[index];
  49     }
  50 
  51     public char charCopyOfVirtualization(int index) {
  52         char[] array = new char[]{1, 2, 3, 4};
  53         return Arrays.copyOf(array, array.length)[index];
  54     }
  55 
  56     public int intCopyOfVirtualization(int index) {
  57         int[] array = new int[]{1, 2, 3, 4};
  58         return Arrays.copyOf(array, array.length)[index];
  59     }
  60 
  61     public long longCopyOfVirtualization(int index) {
  62         long[] array = new long[]{1, 2, 3, 4};
  63         return Arrays.copyOf(array, array.length)[index];
  64     }
  65 
  66     public float floatCopyOfVirtualization(int index) {
  67         float[] array = new float[]{1, 2, 3, 4};
  68         return Arrays.copyOf(array, array.length)[index];
  69     }
  70 
  71     public double doubleCopyOfVirtualization(int index) {
  72         double[] array = new double[]{1, 2, 3, 4};
  73         return Arrays.copyOf(array, array.length)[index];
  74     }
  75 
  76     public Object objectCopyOfVirtualization(int index) {
  77         Object[] array = new Object[]{1, 2, 3, 4};
  78         return Arrays.copyOf(array, array.length)[index];
  79     }
  80 
  81     // @Test
  82     public void testCopyOfVirtualization() {
  83         test("byteCopyOfVirtualization", 3);
  84         test("shortCopyOfVirtualization", 3);
  85         test("charCopyOfVirtualization", 3);
  86         test("intCopyOfVirtualization", 3);
  87         test("longCopyOfVirtualization", 3);
  88         test("floatCopyOfVirtualization", 3);
  89         test("doubleCopyOfVirtualization", 3);
  90         test("objectCopyOfVirtualization", 3);
  91     }
  92 
  93     static final byte[] byteArray = new byte[]{1, 2, 3, 4};
  94 
  95     public byte byteCopyOfVirtualizableAllocation() {
  96         return Arrays.copyOf(byteArray, byteArray.length)[3];
  97     }
  98 
  99     static final short[] shortArray = new short[]{1, 2, 3, 4};
 100 
 101     public short shortCopyOfVirtualizableAllocation() {
 102         return Arrays.copyOf(shortArray, shortArray.length)[3];
 103     }
 104 
 105     static final char[] charArray = new char[]{1, 2, 3, 4};
 106 
 107     public char charCopyOfVirtualizableAllocation() {
 108         return Arrays.copyOf(charArray, charArray.length)[3];
 109     }
 110 
 111     static final int[] intArray = new int[]{1, 2, 3, 4};
 112 
 113     public int intCopyOfVirtualizableAllocation() {
 114         return Arrays.copyOf(intArray, intArray.length)[3];
 115     }
 116 
 117     static final long[] longArray = new long[]{1, 2, 3, 4};
 118 
 119     public long longCopyOfVirtualizableAllocation() {
 120         return Arrays.copyOf(longArray, longArray.length)[3];
 121     }
 122 
 123     static final float[] floatArray = new float[]{1, 2, 3, 4};
 124 
 125     public float floatCopyOfVirtualizableAllocation() {
 126         return Arrays.copyOf(floatArray, floatArray.length)[3];
 127     }
 128 
 129     static final double[] doubleArray = new double[]{1, 2, 3, 4};
 130 
 131     public double doubleCopyOfVirtualizableAllocation() {
 132         return Arrays.copyOf(doubleArray, doubleArray.length)[3];
 133     }
 134 
 135     static final Object[] objectArray = new Object[]{1, 2, 3, 4};
 136 
 137     public Object objectCopyOfVirtualizableAllocation() {
 138         return Arrays.copyOf(objectArray, objectArray.length)[3];
 139     }
 140 
 141     @Test
 142     public void testCopyOfVirtualizableAllocation() {
 143         test("byteCopyOfVirtualizableAllocation");
 144         test("shortCopyOfVirtualizableAllocation");
 145         test("charCopyOfVirtualizableAllocation");
 146         test("intCopyOfVirtualizableAllocation");
 147         test("longCopyOfVirtualizableAllocation");
 148         test("floatCopyOfVirtualizableAllocation");
 149         test("doubleCopyOfVirtualizableAllocation");
 150         test("objectCopyOfVirtualizableAllocation");
 151     }
 152 }