aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: import com.oracle.java.testlibrary.Asserts; aoqi@0: import com.oracle.java.testlibrary.Platform; aoqi@0: import com.oracle.java.testlibrary.Utils; aoqi@0: import sun.hotspot.code.NMethod; aoqi@0: import sun.hotspot.cpuinfo.CPUInfo; aoqi@0: aoqi@0: import java.lang.reflect.Executable; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.util.concurrent.Callable; aoqi@0: import java.util.function.Function; aoqi@0: aoqi@0: public class BmiIntrinsicBase extends CompilerWhiteBoxTest { aoqi@0: aoqi@0: protected BmiIntrinsicBase(BmiTestCase testCase) { aoqi@0: super(testCase); aoqi@0: } aoqi@0: aoqi@0: public static void verifyTestCase(Function constructor, Method... methods) throws Exception { aoqi@0: for (Method method : methods) { aoqi@0: new BmiIntrinsicBase(constructor.apply(method)).test(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: protected void test() throws Exception { aoqi@0: BmiTestCase bmiTestCase = (BmiTestCase) testCase; aoqi@0: aoqi@0: if (!(Platform.isX86() || Platform.isX64())) { aoqi@0: System.out.println("Unsupported platform, test SKIPPED"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (!Platform.isServer()) { aoqi@0: System.out.println("Not server VM, test SKIPPED"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (!CPUInfo.hasFeature(bmiTestCase.getCpuFlag())) { aoqi@0: System.out.println("Unsupported hardware, no required CPU flag " + bmiTestCase.getCpuFlag() + " , test SKIPPED"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (!Boolean.valueOf(getVMOption(bmiTestCase.getVMFlag()))) { aoqi@0: System.out.println("VM flag " + bmiTestCase.getVMFlag() + " disabled, test SKIPPED"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: System.out.println(testCase.name()); aoqi@0: aoqi@0: switch (MODE) { aoqi@0: case "compiled mode": aoqi@0: case "mixed mode": aoqi@0: if (TIERED_COMPILATION && TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_MAX) { aoqi@0: System.out.println("TieredStopAtLevel value (" + TIERED_STOP_AT_LEVEL + ") is too low, test SKIPPED"); aoqi@0: return; aoqi@0: } aoqi@0: deoptimize(); aoqi@0: compileAtLevelAndCheck(CompilerWhiteBoxTest.COMP_LEVEL_MAX); aoqi@0: break; aoqi@0: case "interpreted mode": // test is not applicable in this mode; aoqi@0: System.err.println("Warning: This test is not applicable in mode: " + MODE); aoqi@0: break; aoqi@0: default: aoqi@0: throw new AssertionError("Test bug, unknown VM mode: " + MODE); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected void compileAtLevelAndCheck(int level) { aoqi@0: WHITE_BOX.enqueueMethodForCompilation(method, level); aoqi@0: waitBackgroundCompilation(); aoqi@0: checkCompilation(method, level); aoqi@0: checkEmittedCode(method); aoqi@0: } aoqi@0: aoqi@0: protected void checkCompilation(Executable executable, int level) { aoqi@0: if (!WHITE_BOX.isMethodCompiled(executable)) { aoqi@0: throw new AssertionError("Test bug, expected compilation (level): " + level + ", but not compiled" + WHITE_BOX.isMethodCompilable(executable, level)); aoqi@0: } aoqi@0: final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable); aoqi@0: if (compilationLevel != level) { aoqi@0: throw new AssertionError("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected void checkEmittedCode(Executable executable) { aoqi@0: final byte[] nativeCode = NMethod.get(executable, false).insts; aoqi@0: if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) { aoqi@0: throw new AssertionError(testCase.name() + "CPU instructions expected not found: " + Utils.toHexString(nativeCode)); aoqi@0: } else { aoqi@0: System.out.println("CPU instructions found, PASSED"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase { aoqi@0: private final Method method; aoqi@0: protected byte[] instrMask; aoqi@0: protected byte[] instrPattern; aoqi@0: protected boolean isLongOperation; aoqi@0: aoqi@0: public BmiTestCase(Method method) { aoqi@0: this.method = method; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String name() { aoqi@0: return method.toGenericString(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Executable getExecutable() { aoqi@0: return method; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Callable getCallable() { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean isOsr() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: protected int countCpuInstructions(byte[] nativeCode) { aoqi@0: int count = 0; aoqi@0: int patternSize = Math.min(instrMask.length, instrPattern.length); aoqi@0: boolean found; aoqi@0: Asserts.assertGreaterThan(patternSize, 0); aoqi@0: for (int i = 0, n = nativeCode.length - patternSize; i < n; i++) { aoqi@0: found = true; aoqi@0: for (int j = 0; j < patternSize; j++) { aoqi@0: if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) { aoqi@0: found = false; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: if (found) { aoqi@0: ++count; aoqi@0: i += patternSize - 1; aoqi@0: } aoqi@0: } aoqi@0: return count; aoqi@0: } aoqi@0: aoqi@0: public boolean verifyPositive(byte[] nativeCode) { aoqi@0: final int cnt = countCpuInstructions(nativeCode); aoqi@0: if (Platform.isX86()) { aoqi@0: return cnt >= (isLongOperation ? 2 : 1); aoqi@0: } else { aoqi@0: return Platform.isX64() && cnt >= 1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected String getCpuFlag() { aoqi@0: return "bmi1"; aoqi@0: } aoqi@0: aoqi@0: protected String getVMFlag() { aoqi@0: return "UseBMI1Instructions"; aoqi@0: } aoqi@0: } aoqi@0: }