Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bmi/verifycode/BmiIntrinsicBase.java
41161 views
1
/*
2
* Copyright (c) 2014, 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
package compiler.intrinsics.bmi.verifycode;
25
26
import compiler.whitebox.CompilerWhiteBoxTest;
27
import jdk.test.lib.Asserts;
28
import jdk.test.lib.Platform;
29
import jdk.test.lib.Utils;
30
import sun.hotspot.code.NMethod;
31
import sun.hotspot.cpuinfo.CPUInfo;
32
33
import java.lang.invoke.MethodHandle;
34
import java.lang.invoke.MethodType;
35
import java.lang.reflect.Executable;
36
import java.lang.reflect.Method;
37
import java.util.concurrent.Callable;
38
import java.util.function.Function;
39
40
public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
41
42
protected BmiIntrinsicBase(BmiTestCase testCase) {
43
super(testCase);
44
}
45
46
public static void verifyTestCase(Function<Method, BmiTestCase> constructor, Method... methods) throws Exception {
47
for (Method method : methods) {
48
new BmiIntrinsicBase(constructor.apply(method)).test();
49
}
50
}
51
52
@Override
53
protected void test() throws Exception {
54
BmiTestCase bmiTestCase;
55
if (((BmiTestCase) testCase).getTestCaseX64()) {
56
bmiTestCase = (BmiTestCase_x64) testCase;
57
} else {
58
bmiTestCase = (BmiTestCase) testCase;
59
}
60
61
if (!(Platform.isX86() || Platform.isX64())) {
62
System.out.println("Unsupported platform, test SKIPPED");
63
return;
64
}
65
66
if (!Platform.isServer()) {
67
throw new Error("TESTBUG: Not server VM");
68
}
69
70
if (Platform.isInt()) {
71
throw new Error("TESTBUG: test can not be run in interpreter");
72
}
73
74
if (!CPUInfo.hasFeature(bmiTestCase.getCpuFlag())) {
75
System.out.println("Unsupported hardware, no required CPU flag " + bmiTestCase.getCpuFlag() + " , test SKIPPED");
76
return;
77
}
78
79
if (!Boolean.valueOf(getVMOption(bmiTestCase.getVMFlag()))) {
80
System.out.println("VM flag " + bmiTestCase.getVMFlag() + " disabled, test SKIPPED");
81
return;
82
}
83
84
System.out.println(testCase.name());
85
86
if (TIERED_COMPILATION && TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_MAX || Platform.isEmulatedClient()) {
87
System.out.println("TieredStopAtLevel value (" + TIERED_STOP_AT_LEVEL + ") is too low, test SKIPPED");
88
return;
89
}
90
deoptimize();
91
compileAtLevelAndCheck(CompilerWhiteBoxTest.COMP_LEVEL_MAX);
92
}
93
94
protected void compileAtLevelAndCheck(int level) {
95
WHITE_BOX.enqueueMethodForCompilation(method, level);
96
waitBackgroundCompilation();
97
checkCompilation(method, level);
98
checkEmittedCode(method);
99
}
100
101
protected void checkCompilation(Executable executable, int level) {
102
if (!WHITE_BOX.isMethodCompiled(executable)) {
103
throw new AssertionError("Test bug, expected compilation (level): " + level + ", but not compiled" + WHITE_BOX.isMethodCompilable(executable, level));
104
}
105
final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable);
106
if (compilationLevel != level) {
107
throw new AssertionError("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel);
108
}
109
}
110
111
protected void checkEmittedCode(Executable executable) {
112
final byte[] nativeCode = NMethod.get(executable, false).insts;
113
final byte[] matchInstrPattern = (((BmiTestCase) testCase).getTestCaseX64() && Platform.isX64()) ? ((BmiTestCase_x64) testCase).getInstrPattern_x64() : ((BmiTestCase) testCase).getInstrPattern();
114
if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) {
115
throw new AssertionError(testCase.name() + " " + "CPU instructions expected not found in nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +
116
Utils.toHexString(matchInstrPattern));
117
} else {
118
System.out.println("CPU instructions found, PASSED, nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +
119
Utils.toHexString(matchInstrPattern));
120
}
121
}
122
123
abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase {
124
private final Method method;
125
protected byte[] instrMask;
126
protected byte[] instrPattern;
127
protected boolean isLongOperation;
128
protected String cpuFlag = "bmi1";
129
protected String vmFlag = "UseBMI1Instructions";
130
131
public BmiTestCase(Method method) {
132
this.method = method;
133
}
134
135
@Override
136
public String name() {
137
return method.toGenericString();
138
}
139
140
@Override
141
public Executable getExecutable() {
142
return method;
143
}
144
145
@Override
146
public Callable<Integer> getCallable() {
147
return null;
148
}
149
150
@Override
151
public boolean isOsr() {
152
return false;
153
}
154
155
public byte[] getInstrPattern() {
156
return instrPattern;
157
}
158
159
protected int countCpuInstructions(byte[] nativeCode) {
160
return countCpuInstructions(nativeCode, instrMask, instrPattern);
161
}
162
163
public static int countCpuInstructions(byte[] nativeCode, byte[] instrMask, byte[] instrPattern) {
164
int count = 0;
165
int patternSize = Math.min(instrMask.length, instrPattern.length);
166
boolean found;
167
Asserts.assertGreaterThan(patternSize, 0);
168
for (int i = 0, n = nativeCode.length - patternSize; i < n; i++) {
169
found = true;
170
for (int j = 0; j < patternSize; j++) {
171
if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) {
172
found = false;
173
break;
174
}
175
}
176
if (found) {
177
++count;
178
i += patternSize - 1;
179
}
180
}
181
return count;
182
}
183
184
public boolean verifyPositive(byte[] nativeCode) {
185
final int cnt = countCpuInstructions(nativeCode);
186
if (Platform.isX86()) {
187
return cnt >= (isLongOperation ? 2 : 1);
188
} else {
189
return Platform.isX64() && cnt >= 1;
190
}
191
}
192
193
protected String getCpuFlag() {
194
return cpuFlag;
195
}
196
197
protected String getVMFlag() {
198
return vmFlag;
199
}
200
201
protected boolean getTestCaseX64() {
202
return false;
203
}
204
}
205
206
abstract static class BmiTestCase_x64 extends BmiTestCase {
207
protected byte[] instrMask_x64;
208
protected byte[] instrPattern_x64;
209
210
protected BmiTestCase_x64(Method method) {
211
super(method);
212
}
213
214
public byte[] getInstrPattern_x64() {
215
return instrPattern_x64;
216
}
217
218
protected boolean getTestCaseX64() {
219
return true;
220
}
221
222
protected int countCpuInstructions(byte[] nativeCode) {
223
int cnt = super.countCpuInstructions(nativeCode);
224
if (Platform.isX64()) { // on x64 platform the instruction we search for can be encoded in 2 different ways
225
cnt += countCpuInstructions(nativeCode, instrMask_x64, instrPattern_x64);
226
}
227
return cnt;
228
}
229
}
230
}
231
232