src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/ArrayNewInstanceTest.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
child 50858 2d3e99a72541
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     1
/*
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     4
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     8
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    14
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    18
 *
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    22
 */
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    23
package org.graalvm.compiler.hotspot.test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    24
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    25
import java.lang.reflect.Array;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    26
import java.util.ArrayList;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    27
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    28
import org.graalvm.compiler.api.directives.GraalDirectives;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    29
import org.graalvm.compiler.core.test.GraalCompilerTest;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    30
import org.junit.Test;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    31
import org.junit.runner.RunWith;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    32
import org.junit.runners.Parameterized;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    33
import org.junit.runners.Parameterized.Parameters;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    34
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    35
@RunWith(Parameterized.class)
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    36
public class ArrayNewInstanceTest extends GraalCompilerTest {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    37
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    38
    @Parameters(name = "{index}: class {0} length {1}")
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    39
    public static Iterable<Object[]> data() {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    40
        ArrayList<Object[]> parameters = new ArrayList<>();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    41
        Class<?>[] classesToTest = new Class<?>[]{
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    42
                        byte.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    43
                        boolean.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    44
                        short.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    45
                        char.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    46
                        int.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    47
                        long.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    48
                        Void.class,
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    49
                        ArrayNewInstanceTest.class
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    50
        };
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    51
        for (Class<?> clazz : classesToTest) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    52
            // Negative sizes always deopt
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    53
            parameters.add(new Object[]{clazz, -1, true});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    54
            parameters.add(new Object[]{clazz, 0, false});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    55
            parameters.add(new Object[]{clazz, 42, false});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    56
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    57
        // The void type always throws an exception where graal deopts
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    58
        parameters.add(new Object[]{void.class, -1, true});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    59
        parameters.add(new Object[]{void.class, 0, true});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    60
        parameters.add(new Object[]{void.class, 42, true});
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    61
        return parameters;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    62
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    63
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    64
    private final Class<?> type;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    65
    private final int length;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    66
    private final boolean shouldDeopt;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    67
    private final DeoptimizationBox box = new DeoptimizationBox();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    68
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    69
    public ArrayNewInstanceTest(Class<?> type, int length, boolean shouldDeopt) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    70
        super();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    71
        this.type = type;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    72
        this.length = length;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    73
        this.shouldDeopt = shouldDeopt;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    74
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    75
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    76
    public static Object newArray(Class<?> klass, int length, DeoptimizationBox box) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    77
        Object result = Array.newInstance(klass, length);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    78
        box.inCompiledCode = GraalDirectives.inCompiledCode();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    79
        return result;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    80
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    81
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    82
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    83
    public void testNewArray() {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    84
        test("newArray", type, length, box);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    85
        assertTrue(box.inCompiledCode != shouldDeopt);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    86
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    87
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    88
    public static Object newArrayInLoop(Class<?> klass, int length, int iterations, DeoptimizationBox box) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    89
        Object o = null;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    90
        for (int i = 0; i < iterations; i++) {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    91
            o = Array.newInstance(klass, length);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    92
        }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    93
        box.inCompiledCode = GraalDirectives.inCompiledCode();
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    94
        return o;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    95
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    96
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    97
    @Test
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    98
    public void testNewArrayInLoop() {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
    99
        test("newArrayInLoop", type, length, 2, box);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   100
        assertTrue(box.inCompiledCode != shouldDeopt);
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   101
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   102
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   103
    private static class DeoptimizationBox {
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   104
        volatile boolean inCompiledCode = false;
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   105
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   106
47f19ff9903c 8194819: Update Graal
iveresov
parents:
diff changeset
   107
}