Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/jsr292/NonInlinedCall/InvokeTest.java
41153 views
1
/*
2
* Copyright (c) 2015, 2019, 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 8072008
27
* @library /test/lib / ../patches
28
* @modules java.base/jdk.internal.misc
29
* java.base/jdk.internal.vm.annotation
30
*
31
* @build java.base/java.lang.invoke.MethodHandleHelper
32
* sun.hotspot.WhiteBox
33
* @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions
34
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
35
* -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1
36
* compiler.jsr292.NonInlinedCall.InvokeTest
37
*/
38
39
package compiler.jsr292.NonInlinedCall;
40
41
import jdk.internal.vm.annotation.DontInline;
42
import sun.hotspot.WhiteBox;
43
44
import java.lang.invoke.MethodHandle;
45
import java.lang.invoke.MethodHandleHelper;
46
import java.lang.invoke.MethodHandles;
47
import java.lang.invoke.MethodType;
48
49
import static jdk.test.lib.Asserts.assertEquals;
50
51
public class InvokeTest {
52
static MethodHandles.Lookup LOOKUP = MethodHandleHelper.IMPL_LOOKUP;
53
54
static final MethodHandle virtualMH; // invokevirtual T.f1
55
static final MethodHandle staticMH; // invokestatic T.f2
56
static final MethodHandle intfMH; // invokeinterface I.f3
57
static final MethodHandle defaultMH; // invokevirtual T.f3
58
static final MethodHandle specialMH; // invokespecial T.f4 T
59
static final MethodHandle privateMH; // invokespecial I.f4 T
60
61
static final MethodHandle intrinsicMH; // invokevirtual Object.hashCode
62
63
static final WhiteBox WB = WhiteBox.getWhiteBox();
64
65
static volatile boolean doDeopt = false;
66
67
static {
68
try {
69
MethodType mtype = MethodType.methodType(Class.class);
70
71
virtualMH = LOOKUP.findVirtual(T.class, "f1", mtype);
72
staticMH = LOOKUP.findStatic (T.class, "f2", mtype);
73
intfMH = LOOKUP.findVirtual(I.class, "f3", mtype);
74
defaultMH = LOOKUP.findVirtual(T.class, "f3", mtype);
75
specialMH = LOOKUP.findSpecial(T.class, "f4", mtype, T.class);
76
privateMH = LOOKUP.findSpecial(I.class, "f4", mtype, I.class);
77
intrinsicMH = LOOKUP.findVirtual(Object.class, "hashCode", MethodType.methodType(int.class));
78
} catch (Exception e) {
79
throw new Error(e);
80
}
81
}
82
83
static class T implements I {
84
@DontInline public Class<?> f1() { if (doDeopt) WB.deoptimizeAll(); return T.class; }
85
@DontInline public static Class<?> f2() { if (doDeopt) WB.deoptimizeAll(); return T.class; }
86
@DontInline private Class<?> f4() { if (doDeopt) WB.deoptimizeAll(); return T.class; }
87
}
88
89
static class P1 extends T {
90
@DontInline public Class<?> f1() { if (doDeopt) WB.deoptimizeAll(); return P1.class; }
91
@DontInline public Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return P1.class; }
92
}
93
94
static class P2 extends T {
95
@DontInline public Class<?> f1() { if (doDeopt) WB.deoptimizeAll(); return P2.class; }
96
@DontInline public Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return P2.class; }
97
}
98
99
interface I {
100
@DontInline default Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return I.class; }
101
@DontInline private Class<?> f4() { if (doDeopt) WB.deoptimizeAll(); return I.class; }
102
}
103
104
interface J1 extends I {
105
@DontInline default Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return J1.class; }
106
}
107
108
interface J2 extends I {
109
@DontInline default Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return J2.class; }
110
}
111
112
interface J3 extends I {
113
@DontInline default Class<?> f3() { if (doDeopt) WB.deoptimizeAll(); return J3.class; }
114
}
115
116
static class Q1 extends T implements J1 {}
117
static class Q2 extends T implements J2 {}
118
static class Q3 extends T implements J3 {}
119
120
static class H {
121
public int hashCode() { return 0; }
122
}
123
124
@DontInline
125
static void linkToVirtual(T recv, Class<?> expected) {
126
try {
127
Class<?> cls = (Class<?>)virtualMH.invokeExact(recv);
128
assertEquals(cls, expected);
129
} catch (Throwable e) {
130
throw new Error(e);
131
}
132
}
133
134
@DontInline
135
static void linkToVirtualDefault(T recv, Class<?> expected) {
136
try {
137
Class<?> cls = (Class<?>)defaultMH.invokeExact(recv);
138
assertEquals(cls, expected);
139
} catch (Throwable e) {
140
throw new Error(e);
141
}
142
}
143
144
@DontInline
145
static void linkToVirtualIntrinsic(Object recv, int expected) {
146
try {
147
int v = (int)intrinsicMH.invokeExact(recv);
148
assertEquals(v, expected);
149
} catch (Throwable e) {
150
throw new Error(e);
151
}
152
}
153
154
@DontInline
155
static void linkToInterface(I recv, Class<?> expected) {
156
try {
157
Class<?> cls = (Class<?>)intfMH.invokeExact(recv);
158
assertEquals(cls, expected);
159
} catch (Throwable e) {
160
throw new Error(e);
161
}
162
}
163
164
@DontInline
165
static void linkToStatic() {
166
try {
167
Class<?> cls = (Class<?>)staticMH.invokeExact();
168
assertEquals(cls, T.class);
169
} catch (Throwable e) {
170
throw new Error(e);
171
}
172
}
173
174
@DontInline
175
static void linkToSpecial(T recv, Class<?> expected) {
176
try {
177
Class<?> cls = (Class<?>)specialMH.invokeExact(recv);
178
assertEquals(cls, expected);
179
} catch (Throwable e) {
180
throw new Error(e);
181
}
182
}
183
184
@DontInline
185
static void linkToSpecialIntf(I recv, Class<?> expected) {
186
try {
187
Class<?> cls = (Class<?>)privateMH.invokeExact(recv);
188
assertEquals(cls, expected);
189
} catch (Throwable e) {
190
throw new Error(e);
191
}
192
}
193
194
static void run(Runnable r) {
195
for (int i = 0; i < 20_000; i++) {
196
r.run();
197
}
198
199
doDeopt = true;
200
r.run();
201
doDeopt = false;
202
203
WB.clearInlineCaches();
204
205
for (int i = 0; i < 20_000; i++) {
206
r.run();
207
}
208
209
doDeopt = true;
210
r.run();
211
doDeopt = false;
212
}
213
214
static void testVirtual() {
215
System.out.println("linkToVirtual");
216
217
// Monomorphic case (optimized virtual call)
218
run(() -> linkToVirtual(new T(), T.class));
219
run(() -> linkToVirtualDefault(new T(), I.class));
220
221
run(() -> linkToVirtualIntrinsic(new H(), 0));
222
223
// Megamorphic case (optimized virtual call)
224
run(() -> {
225
linkToVirtual(new T() {}, T.class);
226
linkToVirtual(new T() {}, T.class);
227
linkToVirtual(new T() {}, T.class);
228
});
229
230
run(() -> {
231
linkToVirtualDefault(new T(){}, I.class);
232
linkToVirtualDefault(new T(){}, I.class);
233
linkToVirtualDefault(new T(){}, I.class);
234
});
235
236
// Megamorphic case (virtual call), multiple implementations
237
run(() -> {
238
linkToVirtual(new T(), T.class);
239
linkToVirtual(new P1(), P1.class);
240
linkToVirtual(new P2(), P2.class);
241
});
242
243
run(() -> {
244
linkToVirtualDefault(new Q1(), J1.class);
245
linkToVirtualDefault(new Q2(), J2.class);
246
linkToVirtualDefault(new Q3(), J3.class);
247
});
248
}
249
250
static void testInterface() {
251
System.out.println("linkToInterface");
252
253
// Monomorphic case (optimized virtual call), concrete target method
254
run(() -> linkToInterface(new P1(), P1.class));
255
256
// Monomorphic case (optimized virtual call), default target method
257
run(() -> linkToInterface(new T(), I.class));
258
259
// Megamorphic case (virtual call)
260
run(() -> {
261
linkToInterface(new T(), I.class);
262
linkToInterface(new P1(), P1.class);
263
linkToInterface(new P2(), P2.class);
264
});
265
}
266
267
static void testSpecial() {
268
System.out.println("linkToSpecial");
269
// Monomorphic case (optimized virtual call)
270
run(() -> linkToSpecial(new T(), T.class));
271
run(() -> linkToSpecialIntf(new T(), I.class));
272
}
273
274
static void testStatic() {
275
System.out.println("linkToStatic");
276
// static call
277
run(() -> linkToStatic());
278
}
279
280
public static void main(String[] args) {
281
testVirtual();
282
testInterface();
283
testSpecial();
284
testStatic();
285
}
286
}
287
288