2
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28
* @requires !vm.graal.enabled
29
* @modules java.base/jdk.internal.misc
30
* @library /test/lib /
32
* @build jdk.test.whitebox.WhiteBox
33
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
34
* @run main/othervm -Xbootclasspath/a:.
35
* -XX:+UnlockDiagnosticVMOptions
37
* -XX:DisableIntrinsic=_putCharVolatile,_putInt
38
* -XX:DisableIntrinsic=_putIntVolatile
39
* -XX:CompileCommand=option,jdk.internal.misc.Unsafe::putChar,ccstrlist,DisableIntrinsic,_getCharVolatile,_getInt
40
* -XX:CompileCommand=option,jdk.internal.misc.Unsafe::putCharVolatile,ccstrlist,DisableIntrinsic,_getIntVolatile
41
* compiler.intrinsics.IntrinsicDisabledTest
42
* @run main/othervm -Xbootclasspath/a:.
43
* -XX:+UnlockDiagnosticVMOptions
45
* -XX:ControlIntrinsic=-_putCharVolatile,-_putInt
46
* -XX:ControlIntrinsic=-_putIntVolatile
47
* -XX:CompileCommand=ControlIntrinsic,jdk.internal.misc.Unsafe::putChar,-_getCharVolatile,-_getInt
48
* -XX:CompileCommand=ControlIntrinsic,jdk.internal.misc.Unsafe::putCharVolatile,-_getIntVolatile
49
* compiler.intrinsics.IntrinsicDisabledTest
50
* @run main/othervm -Xbootclasspath/a:.
51
* -XX:+UnlockDiagnosticVMOptions
53
* -XX:ControlIntrinsic=+_putIntVolatile,+_putCharVolatile,+_putInt
54
* -XX:DisableIntrinsic=_putCharVolatile,_putInt
55
* -XX:DisableIntrinsic=_putIntVolatile
56
* -XX:CompileCommand=ControlIntrinsic,jdk.internal.misc.Unsafe::putChar,+_getCharVolatile,+_getInt
57
* -XX:CompileCommand=ControlIntrinsic,jdk.internal.misc.Unsafe::putCharVolatile,+_getIntVolatile
58
* -XX:CompileCommand=DisableIntrinsic,jdk.internal.misc.Unsafe::putChar,_getCharVolatile,_getInt
59
* -XX:CompileCommand=DisableIntrinsic,jdk.internal.misc.Unsafe::putCharVolatile,_getIntVolatile
60
* compiler.intrinsics.IntrinsicDisabledTest
63
package compiler.intrinsics;
65
import jdk.test.lib.Platform;
66
import jdk.test.whitebox.WhiteBox;
67
import compiler.whitebox.CompilerWhiteBoxTest;
69
import java.lang.reflect.Executable;
70
import java.util.Objects;
72
public class IntrinsicDisabledTest {
74
private static final WhiteBox wb = WhiteBox.getWhiteBox();
76
/* Determine if tiered compilation is enabled. */
77
private static final boolean TIERED_COMPILATION = wb.getBooleanVMFlag("TieredCompilation");
79
private static final int TIERED_STOP_AT_LEVEL = wb.getIntxVMFlag("TieredStopAtLevel").intValue();
81
/* This test uses several methods from jdk.internal.misc.Unsafe. The method
82
* getMethod() returns a different Executable for each different
83
* combination of its input parameters. There are eight possible
84
* combinations, getMethod can return an Executable representing
85
* the following methods: putChar, putCharVolatile, getChar,
86
* getCharVolatile, putInt, putIntVolatile, getInt,
87
* getIntVolatile. These methods were selected because they can
88
* be intrinsified by both the C1 and the C2 compiler.
90
static Executable getMethod(boolean isChar, boolean isPut, boolean isVolatile) throws RuntimeException {
92
String methodTypeName = isChar ? "Char" : "Int";
95
Class aClass = Class.forName("jdk.internal.misc.Unsafe");
97
aMethod = aClass.getDeclaredMethod("put" + methodTypeName + (isVolatile ? "Volatile" : ""),
100
isChar ? char.class : int.class);
102
aMethod = aClass.getDeclaredMethod("get" + methodTypeName + (isVolatile ? "Volatile" : ""),
106
} catch (NoSuchMethodException e) {
107
throw new RuntimeException("Test bug, method is unavailable. " + e);
108
} catch (ClassNotFoundException e) {
109
throw new RuntimeException("Test bug, class is unavailable. " + e);
115
public static void test(int compLevel) {
117
Executable putChar = getMethod(true, /*isPut =*/ true, /*isVolatile = */ false);
118
Executable getChar = getMethod(true, /*isPut =*/ false, /*isVolatile = */ false);
119
Executable putCharVolatile = getMethod(true, /*isPut =*/ true, /*isVolatile = */ true);
120
Executable getCharVolatile = getMethod(true, /*isPut =*/ false, /*isVolatile = */ true);
121
Executable putInt = getMethod(false, /*isPut =*/ true, /*isVolatile = */ false);
122
Executable getInt = getMethod(false, /*isPut =*/ false, /*isVolatile = */ false);
123
Executable putIntVolatile = getMethod(false, /*isPut =*/ true, /*isVolatile = */ true);
124
Executable getIntVolatile = getMethod(false, /*isPut =*/ false, /*isVolatile = */ true);
126
/* Test if globally disabling intrinsics works. */
127
if (!wb.isIntrinsicAvailable(putChar, compLevel)) {
128
throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
129
"] is not available globally although it should be.");
132
if (wb.isIntrinsicAvailable(putCharVolatile, compLevel)) {
133
throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
134
"] is available globally although it should not be.");
137
if (wb.isIntrinsicAvailable(putInt, compLevel)) {
138
throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
139
"] is available globally although it should not be.");
142
if (wb.isIntrinsicAvailable(putIntVolatile, compLevel)) {
143
throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
144
"] is available globally although it should not be.");
147
/* Test if disabling intrinsics on a per-method level
148
* works. The method for which intrinsics are
149
* disabled (the compilation context) is putChar. */
150
if (!wb.isIntrinsicAvailable(getChar, putChar, compLevel)) {
151
throw new RuntimeException("Intrinsic for [" + getChar.toGenericString() +
152
"] is not available for intrinsification in [" +
153
putChar.toGenericString() + "] although it should be.");
156
if (wb.isIntrinsicAvailable(getCharVolatile, putChar, compLevel)) {
157
throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
158
"] is available for intrinsification in [" +
159
putChar.toGenericString() + "] although it should not be.");
162
if (wb.isIntrinsicAvailable(getInt, putChar, compLevel)) {
163
throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
164
"] is available for intrinsification in [" +
165
putChar.toGenericString() + "] although it should not be.");
168
if (wb.isIntrinsicAvailable(getIntVolatile, putCharVolatile, compLevel)) {
169
throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
170
"] is available for intrinsification in [" +
171
putCharVolatile.toGenericString() + "] although it should not be.");
174
/* Test if disabling intrinsics on a per-method level
175
* leaves those intrinsics enabled globally. */
176
if (!wb.isIntrinsicAvailable(getCharVolatile, compLevel)) {
177
throw new RuntimeException("Intrinsic for [" + getCharVolatile.toGenericString() +
178
"] is not available globally although it should be.");
181
if (!wb.isIntrinsicAvailable(getInt, compLevel)) {
182
throw new RuntimeException("Intrinsic for [" + getInt.toGenericString() +
183
"] is not available globally although it should be.");
187
if (!wb.isIntrinsicAvailable(getIntVolatile, compLevel)) {
188
throw new RuntimeException("Intrinsic for [" + getIntVolatile.toGenericString() +
189
"] is not available globally although it should be.");
192
/* Test if disabling an intrinsic globally disables it on a
193
* per-method level as well. */
194
if (!wb.isIntrinsicAvailable(putChar, getChar, compLevel)) {
195
throw new RuntimeException("Intrinsic for [" + putChar.toGenericString() +
196
"] is not available for intrinsification in [" +
197
getChar.toGenericString() + "] although it should be.");
200
if (wb.isIntrinsicAvailable(putCharVolatile, getChar, compLevel)) {
201
throw new RuntimeException("Intrinsic for [" + putCharVolatile.toGenericString() +
202
"] is available for intrinsification in [" +
203
getChar.toGenericString() + "] although it should not be.");
206
if (wb.isIntrinsicAvailable(putInt, getChar, compLevel)) {
207
throw new RuntimeException("Intrinsic for [" + putInt.toGenericString() +
208
"] is available for intrinsification in [" +
209
getChar.toGenericString() + "] although it should not be.");
212
if (wb.isIntrinsicAvailable(putIntVolatile, getChar, compLevel)) {
213
throw new RuntimeException("Intrinsic for [" + putIntVolatile.toGenericString() +
214
"] is available for intrinsification in [" +
215
getChar.toGenericString() + "] although it should not be.");
219
public static void main(String args[]) {
220
if (Platform.isServer() && !Platform.isEmulatedClient() &&
221
(TIERED_STOP_AT_LEVEL == CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION)) {
222
if (TIERED_COMPILATION) {
223
test(CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
225
test(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION);
227
test(CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);