Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/bmi/verifycode/BmiIntrinsicBase.java
41161 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.intrinsics.bmi.verifycode;2425import compiler.whitebox.CompilerWhiteBoxTest;26import jdk.test.lib.Asserts;27import jdk.test.lib.Platform;28import jdk.test.lib.Utils;29import sun.hotspot.code.NMethod;30import sun.hotspot.cpuinfo.CPUInfo;3132import java.lang.invoke.MethodHandle;33import java.lang.invoke.MethodType;34import java.lang.reflect.Executable;35import java.lang.reflect.Method;36import java.util.concurrent.Callable;37import java.util.function.Function;3839public class BmiIntrinsicBase extends CompilerWhiteBoxTest {4041protected BmiIntrinsicBase(BmiTestCase testCase) {42super(testCase);43}4445public static void verifyTestCase(Function<Method, BmiTestCase> constructor, Method... methods) throws Exception {46for (Method method : methods) {47new BmiIntrinsicBase(constructor.apply(method)).test();48}49}5051@Override52protected void test() throws Exception {53BmiTestCase bmiTestCase;54if (((BmiTestCase) testCase).getTestCaseX64()) {55bmiTestCase = (BmiTestCase_x64) testCase;56} else {57bmiTestCase = (BmiTestCase) testCase;58}5960if (!(Platform.isX86() || Platform.isX64())) {61System.out.println("Unsupported platform, test SKIPPED");62return;63}6465if (!Platform.isServer()) {66throw new Error("TESTBUG: Not server VM");67}6869if (Platform.isInt()) {70throw new Error("TESTBUG: test can not be run in interpreter");71}7273if (!CPUInfo.hasFeature(bmiTestCase.getCpuFlag())) {74System.out.println("Unsupported hardware, no required CPU flag " + bmiTestCase.getCpuFlag() + " , test SKIPPED");75return;76}7778if (!Boolean.valueOf(getVMOption(bmiTestCase.getVMFlag()))) {79System.out.println("VM flag " + bmiTestCase.getVMFlag() + " disabled, test SKIPPED");80return;81}8283System.out.println(testCase.name());8485if (TIERED_COMPILATION && TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_MAX || Platform.isEmulatedClient()) {86System.out.println("TieredStopAtLevel value (" + TIERED_STOP_AT_LEVEL + ") is too low, test SKIPPED");87return;88}89deoptimize();90compileAtLevelAndCheck(CompilerWhiteBoxTest.COMP_LEVEL_MAX);91}9293protected void compileAtLevelAndCheck(int level) {94WHITE_BOX.enqueueMethodForCompilation(method, level);95waitBackgroundCompilation();96checkCompilation(method, level);97checkEmittedCode(method);98}99100protected void checkCompilation(Executable executable, int level) {101if (!WHITE_BOX.isMethodCompiled(executable)) {102throw new AssertionError("Test bug, expected compilation (level): " + level + ", but not compiled" + WHITE_BOX.isMethodCompilable(executable, level));103}104final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable);105if (compilationLevel != level) {106throw new AssertionError("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel);107}108}109110protected void checkEmittedCode(Executable executable) {111final byte[] nativeCode = NMethod.get(executable, false).insts;112final byte[] matchInstrPattern = (((BmiTestCase) testCase).getTestCaseX64() && Platform.isX64()) ? ((BmiTestCase_x64) testCase).getInstrPattern_x64() : ((BmiTestCase) testCase).getInstrPattern();113if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) {114throw new AssertionError(testCase.name() + " " + "CPU instructions expected not found in nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +115Utils.toHexString(matchInstrPattern));116} else {117System.out.println("CPU instructions found, PASSED, nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +118Utils.toHexString(matchInstrPattern));119}120}121122abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase {123private final Method method;124protected byte[] instrMask;125protected byte[] instrPattern;126protected boolean isLongOperation;127protected String cpuFlag = "bmi1";128protected String vmFlag = "UseBMI1Instructions";129130public BmiTestCase(Method method) {131this.method = method;132}133134@Override135public String name() {136return method.toGenericString();137}138139@Override140public Executable getExecutable() {141return method;142}143144@Override145public Callable<Integer> getCallable() {146return null;147}148149@Override150public boolean isOsr() {151return false;152}153154public byte[] getInstrPattern() {155return instrPattern;156}157158protected int countCpuInstructions(byte[] nativeCode) {159return countCpuInstructions(nativeCode, instrMask, instrPattern);160}161162public static int countCpuInstructions(byte[] nativeCode, byte[] instrMask, byte[] instrPattern) {163int count = 0;164int patternSize = Math.min(instrMask.length, instrPattern.length);165boolean found;166Asserts.assertGreaterThan(patternSize, 0);167for (int i = 0, n = nativeCode.length - patternSize; i < n; i++) {168found = true;169for (int j = 0; j < patternSize; j++) {170if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) {171found = false;172break;173}174}175if (found) {176++count;177i += patternSize - 1;178}179}180return count;181}182183public boolean verifyPositive(byte[] nativeCode) {184final int cnt = countCpuInstructions(nativeCode);185if (Platform.isX86()) {186return cnt >= (isLongOperation ? 2 : 1);187} else {188return Platform.isX64() && cnt >= 1;189}190}191192protected String getCpuFlag() {193return cpuFlag;194}195196protected String getVMFlag() {197return vmFlag;198}199200protected boolean getTestCaseX64() {201return false;202}203}204205abstract static class BmiTestCase_x64 extends BmiTestCase {206protected byte[] instrMask_x64;207protected byte[] instrPattern_x64;208209protected BmiTestCase_x64(Method method) {210super(method);211}212213public byte[] getInstrPattern_x64() {214return instrPattern_x64;215}216217protected boolean getTestCaseX64() {218return true;219}220221protected int countCpuInstructions(byte[] nativeCode) {222int cnt = super.countCpuInstructions(nativeCode);223if (Platform.isX64()) { // on x64 platform the instruction we search for can be encoded in 2 different ways224cnt += countCpuInstructions(nativeCode, instrMask_x64, instrPattern_x64);225}226return cnt;227}228}229}230231232