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