Fri, 11 Apr 2014 00:34:51 +0400
8038953: Add sanity tests for BMI1 and LZCNT instructions
Reviewed-by: kvn, iignatyev
Contributed-by: anton.ivanov@oracle.com
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | import com.oracle.java.testlibrary.Asserts; |
aoqi@0 | 26 | import com.oracle.java.testlibrary.Platform; |
aoqi@0 | 27 | import com.oracle.java.testlibrary.Utils; |
aoqi@0 | 28 | import sun.hotspot.code.NMethod; |
aoqi@0 | 29 | import sun.hotspot.cpuinfo.CPUInfo; |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.lang.reflect.Executable; |
aoqi@0 | 32 | import java.lang.reflect.Method; |
aoqi@0 | 33 | import java.util.concurrent.Callable; |
aoqi@0 | 34 | import java.util.function.Function; |
aoqi@0 | 35 | |
aoqi@0 | 36 | public class BmiIntrinsicBase extends CompilerWhiteBoxTest { |
aoqi@0 | 37 | |
aoqi@0 | 38 | protected BmiIntrinsicBase(BmiTestCase testCase) { |
aoqi@0 | 39 | super(testCase); |
aoqi@0 | 40 | } |
aoqi@0 | 41 | |
aoqi@0 | 42 | public static void verifyTestCase(Function<Method, BmiTestCase> constructor, Method... methods) throws Exception { |
aoqi@0 | 43 | for (Method method : methods) { |
aoqi@0 | 44 | new BmiIntrinsicBase(constructor.apply(method)).test(); |
aoqi@0 | 45 | } |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | @Override |
aoqi@0 | 49 | protected void test() throws Exception { |
aoqi@0 | 50 | BmiTestCase bmiTestCase = (BmiTestCase) testCase; |
aoqi@0 | 51 | |
aoqi@0 | 52 | if (!(Platform.isX86() || Platform.isX64())) { |
aoqi@0 | 53 | System.out.println("Unsupported platform, test SKIPPED"); |
aoqi@0 | 54 | return; |
aoqi@0 | 55 | } |
aoqi@0 | 56 | |
aoqi@0 | 57 | if (!Platform.isServer()) { |
aoqi@0 | 58 | System.out.println("Not server VM, test SKIPPED"); |
aoqi@0 | 59 | return; |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | if (!CPUInfo.hasFeature(bmiTestCase.getCpuFlag())) { |
aoqi@0 | 63 | System.out.println("Unsupported hardware, no required CPU flag " + bmiTestCase.getCpuFlag() + " , test SKIPPED"); |
aoqi@0 | 64 | return; |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | if (!Boolean.valueOf(getVMOption(bmiTestCase.getVMFlag()))) { |
aoqi@0 | 68 | System.out.println("VM flag " + bmiTestCase.getVMFlag() + " disabled, test SKIPPED"); |
aoqi@0 | 69 | return; |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | System.out.println(testCase.name()); |
aoqi@0 | 73 | |
aoqi@0 | 74 | switch (MODE) { |
aoqi@0 | 75 | case "compiled mode": |
aoqi@0 | 76 | case "mixed mode": |
aoqi@0 | 77 | if (TIERED_COMPILATION && TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_MAX) { |
aoqi@0 | 78 | System.out.println("TieredStopAtLevel value (" + TIERED_STOP_AT_LEVEL + ") is too low, test SKIPPED"); |
aoqi@0 | 79 | return; |
aoqi@0 | 80 | } |
aoqi@0 | 81 | deoptimize(); |
aoqi@0 | 82 | compileAtLevelAndCheck(CompilerWhiteBoxTest.COMP_LEVEL_MAX); |
aoqi@0 | 83 | break; |
aoqi@0 | 84 | case "interpreted mode": // test is not applicable in this mode; |
aoqi@0 | 85 | System.err.println("Warning: This test is not applicable in mode: " + MODE); |
aoqi@0 | 86 | break; |
aoqi@0 | 87 | default: |
aoqi@0 | 88 | throw new AssertionError("Test bug, unknown VM mode: " + MODE); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | protected void compileAtLevelAndCheck(int level) { |
aoqi@0 | 93 | WHITE_BOX.enqueueMethodForCompilation(method, level); |
aoqi@0 | 94 | waitBackgroundCompilation(); |
aoqi@0 | 95 | checkCompilation(method, level); |
aoqi@0 | 96 | checkEmittedCode(method); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | protected void checkCompilation(Executable executable, int level) { |
aoqi@0 | 100 | if (!WHITE_BOX.isMethodCompiled(executable)) { |
aoqi@0 | 101 | throw new AssertionError("Test bug, expected compilation (level): " + level + ", but not compiled" + WHITE_BOX.isMethodCompilable(executable, level)); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable); |
aoqi@0 | 104 | if (compilationLevel != level) { |
aoqi@0 | 105 | throw new AssertionError("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | protected void checkEmittedCode(Executable executable) { |
aoqi@0 | 110 | final byte[] nativeCode = NMethod.get(executable, false).insts; |
aoqi@0 | 111 | if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) { |
aoqi@0 | 112 | throw new AssertionError(testCase.name() + "CPU instructions expected not found: " + Utils.toHexString(nativeCode)); |
aoqi@0 | 113 | } else { |
aoqi@0 | 114 | System.out.println("CPU instructions found, PASSED"); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase { |
aoqi@0 | 119 | private final Method method; |
aoqi@0 | 120 | protected byte[] instrMask; |
aoqi@0 | 121 | protected byte[] instrPattern; |
aoqi@0 | 122 | protected boolean isLongOperation; |
aoqi@0 | 123 | |
aoqi@0 | 124 | public BmiTestCase(Method method) { |
aoqi@0 | 125 | this.method = method; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | @Override |
aoqi@0 | 129 | public String name() { |
aoqi@0 | 130 | return method.toGenericString(); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | @Override |
aoqi@0 | 134 | public Executable getExecutable() { |
aoqi@0 | 135 | return method; |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | @Override |
aoqi@0 | 139 | public Callable<Integer> getCallable() { |
aoqi@0 | 140 | return null; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | @Override |
aoqi@0 | 144 | public boolean isOsr() { |
aoqi@0 | 145 | return false; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | protected int countCpuInstructions(byte[] nativeCode) { |
aoqi@0 | 149 | int count = 0; |
aoqi@0 | 150 | int patternSize = Math.min(instrMask.length, instrPattern.length); |
aoqi@0 | 151 | boolean found; |
aoqi@0 | 152 | Asserts.assertGreaterThan(patternSize, 0); |
aoqi@0 | 153 | for (int i = 0, n = nativeCode.length - patternSize; i < n; i++) { |
aoqi@0 | 154 | found = true; |
aoqi@0 | 155 | for (int j = 0; j < patternSize; j++) { |
aoqi@0 | 156 | if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) { |
aoqi@0 | 157 | found = false; |
aoqi@0 | 158 | break; |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | if (found) { |
aoqi@0 | 162 | ++count; |
aoqi@0 | 163 | i += patternSize - 1; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } |
aoqi@0 | 166 | return count; |
aoqi@0 | 167 | } |
aoqi@0 | 168 | |
aoqi@0 | 169 | public boolean verifyPositive(byte[] nativeCode) { |
aoqi@0 | 170 | final int cnt = countCpuInstructions(nativeCode); |
aoqi@0 | 171 | if (Platform.isX86()) { |
aoqi@0 | 172 | return cnt >= (isLongOperation ? 2 : 1); |
aoqi@0 | 173 | } else { |
aoqi@0 | 174 | return Platform.isX64() && cnt >= 1; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | protected String getCpuFlag() { |
aoqi@0 | 179 | return "bmi1"; |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | protected String getVMFlag() { |
aoqi@0 | 183 | return "UseBMI1Instructions"; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } |