test/compiler/intrinsics/bmi/verifycode/BmiIntrinsicBase.java

changeset 6611
72558bacada3
parent 0
f90c822e73f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/intrinsics/bmi/verifycode/BmiIntrinsicBase.java	Fri Apr 11 00:34:51 2014 +0400
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +import com.oracle.java.testlibrary.Asserts;
    1.29 +import com.oracle.java.testlibrary.Platform;
    1.30 +import com.oracle.java.testlibrary.Utils;
    1.31 +import sun.hotspot.code.NMethod;
    1.32 +import sun.hotspot.cpuinfo.CPUInfo;
    1.33 +
    1.34 +import java.lang.reflect.Executable;
    1.35 +import java.lang.reflect.Method;
    1.36 +import java.util.concurrent.Callable;
    1.37 +import java.util.function.Function;
    1.38 +
    1.39 +public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
    1.40 +
    1.41 +    protected BmiIntrinsicBase(BmiTestCase testCase) {
    1.42 +        super(testCase);
    1.43 +    }
    1.44 +
    1.45 +    public static void verifyTestCase(Function<Method, BmiTestCase> constructor, Method... methods) throws Exception {
    1.46 +        for (Method method : methods) {
    1.47 +            new BmiIntrinsicBase(constructor.apply(method)).test();
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    @Override
    1.52 +    protected void test() throws Exception {
    1.53 +        BmiTestCase bmiTestCase = (BmiTestCase) testCase;
    1.54 +
    1.55 +        if (!(Platform.isX86() || Platform.isX64())) {
    1.56 +            System.out.println("Unsupported platform, test SKIPPED");
    1.57 +            return;
    1.58 +        }
    1.59 +
    1.60 +        if (!Platform.isServer()) {
    1.61 +            System.out.println("Not server VM, test SKIPPED");
    1.62 +            return;
    1.63 +        }
    1.64 +
    1.65 +        if (!CPUInfo.hasFeature(bmiTestCase.getCpuFlag())) {
    1.66 +            System.out.println("Unsupported hardware, no required CPU flag " + bmiTestCase.getCpuFlag() + " , test SKIPPED");
    1.67 +            return;
    1.68 +        }
    1.69 +
    1.70 +        if (!Boolean.valueOf(getVMOption(bmiTestCase.getVMFlag()))) {
    1.71 +            System.out.println("VM flag " + bmiTestCase.getVMFlag() + " disabled, test SKIPPED");
    1.72 +            return;
    1.73 +        }
    1.74 +
    1.75 +        System.out.println(testCase.name());
    1.76 +
    1.77 +        switch (MODE) {
    1.78 +            case "compiled mode":
    1.79 +            case "mixed mode":
    1.80 +                if (TIERED_COMPILATION && TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_MAX) {
    1.81 +                    System.out.println("TieredStopAtLevel value (" + TIERED_STOP_AT_LEVEL + ") is too low, test SKIPPED");
    1.82 +                    return;
    1.83 +                }
    1.84 +                deoptimize();
    1.85 +                compileAtLevelAndCheck(CompilerWhiteBoxTest.COMP_LEVEL_MAX);
    1.86 +                break;
    1.87 +            case "interpreted mode": // test is not applicable in this mode;
    1.88 +                System.err.println("Warning: This test is not applicable in mode: " + MODE);
    1.89 +                break;
    1.90 +            default:
    1.91 +                throw new AssertionError("Test bug, unknown VM mode: " + MODE);
    1.92 +        }
    1.93 +    }
    1.94 +
    1.95 +    protected void compileAtLevelAndCheck(int level) {
    1.96 +        WHITE_BOX.enqueueMethodForCompilation(method, level);
    1.97 +        waitBackgroundCompilation();
    1.98 +        checkCompilation(method, level);
    1.99 +        checkEmittedCode(method);
   1.100 +    }
   1.101 +
   1.102 +    protected void checkCompilation(Executable executable, int level) {
   1.103 +        if (!WHITE_BOX.isMethodCompiled(executable)) {
   1.104 +            throw new AssertionError("Test bug, expected compilation (level): " + level + ", but not compiled" + WHITE_BOX.isMethodCompilable(executable, level));
   1.105 +        }
   1.106 +        final int compilationLevel = WHITE_BOX.getMethodCompilationLevel(executable);
   1.107 +        if (compilationLevel != level) {
   1.108 +            throw new AssertionError("Test bug, expected compilation (level): " + level + ", but level: " + compilationLevel);
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    protected void checkEmittedCode(Executable executable) {
   1.113 +        final byte[] nativeCode = NMethod.get(executable, false).insts;
   1.114 +        if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) {
   1.115 +            throw new AssertionError(testCase.name() + "CPU instructions expected not found: " + Utils.toHexString(nativeCode));
   1.116 +        } else {
   1.117 +            System.out.println("CPU instructions found, PASSED");
   1.118 +        }
   1.119 +    }
   1.120 +
   1.121 +    abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase {
   1.122 +        private final Method method;
   1.123 +        protected byte[] instrMask;
   1.124 +        protected byte[] instrPattern;
   1.125 +        protected boolean isLongOperation;
   1.126 +
   1.127 +        public BmiTestCase(Method method) {
   1.128 +            this.method = method;
   1.129 +        }
   1.130 +
   1.131 +        @Override
   1.132 +        public String name() {
   1.133 +            return method.toGenericString();
   1.134 +        }
   1.135 +
   1.136 +        @Override
   1.137 +        public Executable getExecutable() {
   1.138 +            return method;
   1.139 +        }
   1.140 +
   1.141 +        @Override
   1.142 +        public Callable<Integer> getCallable() {
   1.143 +            return null;
   1.144 +        }
   1.145 +
   1.146 +        @Override
   1.147 +        public boolean isOsr() {
   1.148 +            return false;
   1.149 +        }
   1.150 +
   1.151 +        protected int countCpuInstructions(byte[] nativeCode) {
   1.152 +            int count = 0;
   1.153 +            int patternSize = Math.min(instrMask.length, instrPattern.length);
   1.154 +            boolean found;
   1.155 +            Asserts.assertGreaterThan(patternSize, 0);
   1.156 +            for (int i = 0, n = nativeCode.length - patternSize; i < n; i++) {
   1.157 +                found = true;
   1.158 +                for (int j = 0; j < patternSize; j++) {
   1.159 +                    if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) {
   1.160 +                        found = false;
   1.161 +                        break;
   1.162 +                    }
   1.163 +                }
   1.164 +                if (found) {
   1.165 +                    ++count;
   1.166 +                    i += patternSize - 1;
   1.167 +                }
   1.168 +            }
   1.169 +            return count;
   1.170 +        }
   1.171 +
   1.172 +        public boolean verifyPositive(byte[] nativeCode) {
   1.173 +            final int cnt = countCpuInstructions(nativeCode);
   1.174 +            if (Platform.isX86()) {
   1.175 +                return cnt >= (isLongOperation ? 2 : 1);
   1.176 +            } else {
   1.177 +                return Platform.isX64() && cnt >= 1;
   1.178 +            }
   1.179 +        }
   1.180 +
   1.181 +        protected String getCpuFlag() {
   1.182 +            return "bmi1";
   1.183 +        }
   1.184 +
   1.185 +        protected String getVMFlag() {
   1.186 +            return "UseBMI1Instructions";
   1.187 +        }
   1.188 +    }
   1.189 +}

mercurial