001/*
002 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023
024package com.oracle.graal.compiler.test;
025
026import java.util.*;
027
028import org.junit.*;
029
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.java.*;
032
033public class CopyOfVirtualizationTest extends GraalCompilerTest {
034
035    @Override
036    protected boolean checkMidTierGraph(StructuredGraph graph) {
037        assertTrue(graph.getNodes().filter(node -> node instanceof NewArrayNode).count() == 0, "shouldn't require allocation in %s", graph);
038        return super.checkMidTierGraph(graph);
039    }
040
041    public byte byteCopyOfVirtualization(int index) {
042        byte[] array = new byte[]{1, 2, 3, 4};
043        return Arrays.copyOf(array, array.length)[index];
044    }
045
046    public short shortCopyOfVirtualization(int index) {
047        short[] array = new short[]{1, 2, 3, 4};
048        return Arrays.copyOf(array, array.length)[index];
049    }
050
051    public char charCopyOfVirtualization(int index) {
052        char[] array = new char[]{1, 2, 3, 4};
053        return Arrays.copyOf(array, array.length)[index];
054    }
055
056    public int intCopyOfVirtualization(int index) {
057        int[] array = new int[]{1, 2, 3, 4};
058        return Arrays.copyOf(array, array.length)[index];
059    }
060
061    public long longCopyOfVirtualization(int index) {
062        long[] array = new long[]{1, 2, 3, 4};
063        return Arrays.copyOf(array, array.length)[index];
064    }
065
066    public float floatCopyOfVirtualization(int index) {
067        float[] array = new float[]{1, 2, 3, 4};
068        return Arrays.copyOf(array, array.length)[index];
069    }
070
071    public double doubleCopyOfVirtualization(int index) {
072        double[] array = new double[]{1, 2, 3, 4};
073        return Arrays.copyOf(array, array.length)[index];
074    }
075
076    public Object objectCopyOfVirtualization(int index) {
077        Object[] array = new Object[]{1, 2, 3, 4};
078        return Arrays.copyOf(array, array.length)[index];
079    }
080
081    // @Test
082    public void testCopyOfVirtualization() {
083        test("byteCopyOfVirtualization", 3);
084        test("shortCopyOfVirtualization", 3);
085        test("charCopyOfVirtualization", 3);
086        test("intCopyOfVirtualization", 3);
087        test("longCopyOfVirtualization", 3);
088        test("floatCopyOfVirtualization", 3);
089        test("doubleCopyOfVirtualization", 3);
090        test("objectCopyOfVirtualization", 3);
091    }
092
093    static final byte[] byteArray = new byte[]{1, 2, 3, 4};
094
095    public byte byteCopyOfVirtualizableAllocation() {
096        return Arrays.copyOf(byteArray, byteArray.length)[3];
097    }
098
099    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}