1 /*
   2  * Copyright (c) 2018, 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 
  25 package org.graalvm.compiler.hotspot.amd64.test;
  26 
  27 import static org.junit.Assume.assumeTrue;
  28 
  29 import org.graalvm.compiler.api.test.Graal;
  30 import org.graalvm.compiler.code.CompilationResult;
  31 import org.graalvm.compiler.core.test.GraalCompilerTest;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.runtime.RuntimeProvider;
  34 import org.junit.Assert;
  35 import org.junit.Before;
  36 
  37 import jdk.vm.ci.amd64.AMD64;
  38 import jdk.vm.ci.code.Architecture;
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 
  41 public abstract class BmiCompilerTest extends GraalCompilerTest {
  42 
  43     @Before
  44     public void checkAMD64() {
  45         Architecture arch = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget().arch;
  46         assumeTrue("skipping AMD64 specific test", arch instanceof AMD64);
  47         assumeTrue("skipping BMI1 specific test", ((AMD64) arch).getFeatures().contains(AMD64.CPUFeature.BMI1));
  48     }
  49 
  50     public boolean verifyPositive(String methodName, byte[] instrMask, byte[] instrPattern) {
  51         ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
  52         StructuredGraph graph = parseForCompile(method);
  53 
  54         CompilationResult c = compile(method, graph);
  55         byte[] targetCode = c.getTargetCode();
  56 
  57         return countCpuInstructions(targetCode, instrMask, instrPattern) >= 1;
  58     }
  59 
  60     public static int countCpuInstructions(byte[] nativeCode, byte[] instrMask, byte[] instrPattern) {
  61         int count = 0;
  62         int patternSize = Math.min(instrMask.length, instrPattern.length);
  63         boolean found;
  64         Assert.assertTrue(patternSize > 0);
  65         for (int i = 0, n = nativeCode.length - patternSize; i < n;) {
  66             found = true;
  67             for (int j = 0; j < patternSize; j++) {
  68                 if ((nativeCode[i + j] & instrMask[j]) != instrPattern[j]) {
  69                     found = false;
  70                     break;
  71                 }
  72             }
  73             if (found) {
  74                 ++count;
  75                 i += patternSize - 1;
  76             }
  77             i++;
  78         }
  79         return count;
  80     }
  81 }