IntrinsicDisabledTest.java revision 11098:927d84d0b391
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 * @test
26 * @bug 8138651
27 * @modules java.base/jdk.internal.misc
28 * @library /testlibrary /test/lib
29 * @build IntrinsicDisabledTest
30 * @run main ClassFileInstaller sun.hotspot.WhiteBox
31 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
32 * @run main/othervm -Xbootclasspath/a:.
33 *                   -XX:+UnlockDiagnosticVMOptions
34 *                   -XX:+WhiteBoxAPI
35 *                   -XX:DisableIntrinsic=_putCharVolatile,_putInt
36 *                   -XX:DisableIntrinsic=_putIntVolatile
37 *                   -XX:CompileCommand=option,jdk.internal.misc.Unsafe::putChar,ccstrlist,DisableIntrinsic,_getCharVolatile,_getInt
38 *                   -XX:CompileCommand=option,jdk.internal.misc.Unsafe::putCharVolatile,ccstrlist,DisableIntrinsic,_getIntVolatile
39 *                   IntrinsicDisabledTest
40 */
41
42import java.lang.reflect.Executable;
43import java.util.Objects;
44
45import sun.hotspot.WhiteBox;
46
47import jdk.test.lib.Platform;
48
49public class IntrinsicDisabledTest {
50
51    private static final WhiteBox wb = WhiteBox.getWhiteBox();
52
53    /* Compilation level corresponding to C1. */
54    private static final int COMP_LEVEL_SIMPLE = 1;
55
56    /* Compilation level corresponding to C2. */
57    private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
58
59    /* Determine if tiered compilation is enabled. */
60    private static boolean isTieredCompilationEnabled() {
61        return Boolean.valueOf(Objects.toString(wb.getVMFlag("TieredCompilation")));
62    }
63
64    /* This test uses several methods from jdk.internal.misc.Unsafe. The method
65     * getMethod() returns a different Executable for each different
66     * combination of its input parameters. There are eight possible
67     * combinations, getMethod can return an Executable representing
68     * the following methods: putChar, putCharVolatile, getChar,
69     * getCharVolatile, putInt, putIntVolatile, getInt,
70     * getIntVolatile. These methods were selected because they can
71     * be intrinsified by both the C1 and the C2 compiler.
72     */
73    static Executable getMethod(boolean isChar, boolean isPut, boolean isVolatile) throws RuntimeException {
74        Executable aMethod;
75        String methodTypeName = isChar ? "Char" : "Int";
76
77        try {
78            Class aClass = Class.forName("jdk.internal.misc.Unsafe");
79            if (isPut) {
80                aMethod = aClass.getDeclaredMethod("put" + methodTypeName + (isVolatile ? "Volatile" : ""),
81                                                   Object.class,
82                                                   long.class,
83                                                   isChar ? char.class : int.class);
84            } else {
85                aMethod = aClass.getDeclaredMethod("get" + methodTypeName + (isVolatile ? "Volatile" : ""),
86                                                   Object.class,
87                                                   long.class);
88            }
89        } catch (NoSuchMethodException e) {
90            throw new RuntimeException("Test bug, method is unavailable. " + e);
91        } catch (ClassNotFoundException e) {
92            throw new RuntimeException("Test bug, class is unavailable. " + e);
93        }
94
95        return aMethod;
96    }
97
98    public static void test(int compLevel) {
99
100        Executable putChar = getMethod(true, /*isPut =*/ true, /*isVolatile = */ false);
101        Executable getChar = getMethod(true, /*isPut =*/ false, /*isVolatile = */ false);
102        Executable putCharVolatile = getMethod(true, /*isPut =*/ true, /*isVolatile = */ true);
103        Executable getCharVolatile = getMethod(true, /*isPut =*/ false, /*isVolatile = */ true);
104        Executable putInt = getMethod(false, /*isPut =*/ true, /*isVolatile = */ false);
105        Executable getInt = getMethod(false, /*isPut =*/ false, /*isVolatile = */ false);
106        Executable putIntVolatile = getMethod(false, /*isPut =*/ true, /*isVolatile = */ true);
107        Executable getIntVolatile = getMethod(false, /*isPut =*/ false, /*isVolatile = */ true);
108
109        /* Test if globally disabling intrinsics works. */
110        if (!wb.isIntrinsicAvailable(putChar, compLevel)) {
111            throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
112                                       "] is not available globally although it should be.");
113        }
114
115        if (wb.isIntrinsicAvailable(putCharVolatile, compLevel)) {
116            throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
117                                       "] is available globally although it should not be.");
118        }
119
120        if (wb.isIntrinsicAvailable(putInt, compLevel)) {
121            throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
122                                       "] is available globally although it should not be.");
123        }
124
125        if (wb.isIntrinsicAvailable(putIntVolatile, compLevel)) {
126            throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
127                                       "] is available globally although it should not be.");
128        }
129
130        /* Test if disabling intrinsics on a per-method level
131         * works. The method for which intrinsics are
132         * disabled (the compilation context) is putChar. */
133        if (!wb.isIntrinsicAvailable(getChar, putChar, compLevel)) {
134            throw new RuntimeException("Intrinsic for [" + getChar.toGenericString() +
135                                       "] is not available for intrinsification in [" +
136                                       putChar.toGenericString() + "] although it should be.");
137        }
138
139        if (wb.isIntrinsicAvailable(getCharVolatile, putChar, compLevel)) {
140            throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
141                                       "] is available for intrinsification in [" +
142                                       putChar.toGenericString() + "] although it should not be.");
143        }
144
145        if (wb.isIntrinsicAvailable(getInt, putChar, compLevel)) {
146            throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
147                                       "] is available for intrinsification in [" +
148                                       putChar.toGenericString() + "] although it should not be.");
149        }
150
151        if (wb.isIntrinsicAvailable(getIntVolatile, putCharVolatile, compLevel)) {
152            throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
153                                       "] is available for intrinsification in [" +
154                                       putCharVolatile.toGenericString() + "] although it should not be.");
155        }
156
157        /* Test if disabling intrinsics on a per-method level
158         * leaves those intrinsics enabled globally. */
159        if (!wb.isIntrinsicAvailable(getCharVolatile, compLevel)) {
160            throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
161                                       "] is not available globally although it should be.");
162        }
163
164        if (!wb.isIntrinsicAvailable(getInt, compLevel)) {
165            throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
166                                       "] is not available globally although it should be.");
167        }
168
169
170        if (!wb.isIntrinsicAvailable(getIntVolatile, compLevel)) {
171            throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
172                                       "] is not available globally although it should be.");
173        }
174
175        /* Test if disabling an intrinsic globally disables it on a
176         * per-method level as well. */
177        if (!wb.isIntrinsicAvailable(putChar, getChar, compLevel)) {
178            throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
179                                       "] is not available for intrinsification in [" +
180                                       getChar.toGenericString() + "] although it should be.");
181        }
182
183        if (wb.isIntrinsicAvailable(putCharVolatile, getChar, compLevel)) {
184            throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
185                                       "] is available for intrinsification in [" +
186                                       getChar.toGenericString() + "] although it should not be.");
187        }
188
189        if (wb.isIntrinsicAvailable(putInt, getChar, compLevel)) {
190            throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
191                                       "] is available for intrinsification in [" +
192                                       getChar.toGenericString() + "] although it should not be.");
193        }
194
195        if (wb.isIntrinsicAvailable(putIntVolatile, getChar, compLevel)) {
196            throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
197                                       "] is available for intrinsification in [" +
198                                       getChar.toGenericString() + "] although it should not be.");
199        }
200    }
201
202    public static void main(String args[]) {
203        if (Platform.isServer()) {
204            if (isTieredCompilationEnabled()) {
205                test(COMP_LEVEL_SIMPLE);
206            }
207            test(COMP_LEVEL_FULL_OPTIMIZATION);
208        } else {
209            test(COMP_LEVEL_SIMPLE);
210        }
211    }
212}
213