Skip to content
Permalink
Browse files
8261671: X86 I2L conversion can be skipped for certain masked positiv…
…e values

Reviewed-by: kvn, neliasso, vlivanov
  • Loading branch information
mgkwill authored and Sandhya Viswanathan committed Mar 17, 2021
1 parent 5d87a21 commit 4f4ca0e705c32fff9760074d198fea2cc67328aa
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 5 deletions.
@@ -3176,6 +3176,18 @@ operand immL_32bits()
interface(CONST_INTER);
%}

// Int Immediate: 2^n-1, postive
operand immI_Pow2M1()
%{
predicate((n->get_int() > 0)
&& is_power_of_2(n->get_int() + 1));
match(ConI);

op_cost(20);
format %{ %}
interface(CONST_INTER);
%}

// Float Immediate zero
operand immF0()
%{
@@ -9153,6 +9165,21 @@ instruct andI2L_rReg_imm65535(rRegL dst, rRegI src, immI_65535 mask)
ins_pipe(ialu_reg);
%}

// Can skip int2long conversions after AND with small bitmask
instruct convI2LAndI_reg_immIbitmask(rRegL dst, rRegI src, immI_Pow2M1 mask, rRegI tmp, rFlagsReg cr)
%{
predicate(VM_Version::supports_bmi2());
ins_cost(125);
effect(TEMP tmp, KILL cr);
match(Set dst (ConvI2L (AndI src mask)));
format %{ "bzhiq $dst, $src, $mask \t# using $tmp as TEMP, int & immI_Pow2M1 -> long" %}
ins_encode %{
__ movl($tmp$$Register, exact_log2($mask$$constant + 1));
__ bzhiq($dst$$Register, $src$$Register, $tmp$$Register);
%}
ins_pipe(ialu_reg_reg);
%}

// And Register with Immediate
instruct andI_rReg_imm(rRegI dst, immI src, rFlagsReg cr)
%{
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @summary Support BMI2 instructions on x86/x64
*
* @run main/othervm -Xbatch -XX:-TieredCompilation -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure
* -XX:CompileCommand=dontinline,compiler.codegen.BMI2$BMITests::*
* compiler.codegen.BMI2
*/

package compiler.codegen;

public class BMI2 {
private final static int ITERATIONS = 30000;

// match(Set dst (ConvI2L (AndI src1 mask)))
public static void testBzhiI2L(int ix) {
long[] goldv = new long[16];
for (int i = 0; i <= 15; i++) {
goldv[i] = BMITests.bzhiI2L(ix, i);
}
for (int i2 = 0; i2 < ITERATIONS; i2++) {
for (int i = 0; i <= 15; i++) {
long v = BMITests.bzhiI2L(ix, i);
if (v != goldv[i]) {
throw new Error(returnBzhiI2LErrMessage (goldv[i], v));
}
}
}
}

private static String returnBzhiI2LErrMessage (long value, long value2) {
return "bzhi I2L with register failed, uncompiled result: " + value + " does not match compiled result: " + value2;
}

static class BMITests {

static long bzhiI2L(int src1, int src2) {

switch(src2) {
case 0:
return (long)(src1 & 0x1);
case 1:
return (long)(src1 & 0x3);
case 2:
return (long)(src1 & 0x7);
case 3:
return (long)(src1 & 0xF);
case 4:
return (long)(src1 & 0x1F);
case 5:
return (long)(src1 & 0x3F);
case 6:
return (long)(src1 & 0x7F);
case 7:
return (long)(src1 & 0xFF);
case 8:
return (long)(src1 & 0x1FF);
case 9:
return (long)(src1 & 0x3FF);
case 10:
return (long)(src1 & 0x7FF);
case 11:
return (long)(src1 & 0xFFF);
case 12:
return (long)(src1 & 0x1FFF);
case 13:
return (long)(src1 & 0x3FFF);
case 14:
return (long)(src1 & 0x7FFF);
case 15:
return (long)(src1 & 0xFFFF);
default:
return (long)(src1 & 0xFFFF);
}
}
}

public static void main(String[] args) {
testBzhiI2L(0);
testBzhiI2L(1);
}
}
@@ -218,6 +218,7 @@ public static void runTests(Expr expr, int iterations, Random rng) {
runUnaryIntMemTest(expr, iterations, rng);
runUnaryLongRegTest(expr, iterations, rng);
runUnaryLongMemTest(expr, iterations, rng);
runUnaryIntToLongRegTest(expr, iterations, rng);
runBinaryRegRegIntTest(expr, iterations, rng);
runBinaryRegMemIntTest(expr, iterations, rng);
runBinaryMemRegIntTest(expr, iterations, rng);
@@ -306,6 +307,25 @@ public static void runUnaryLongMemTest(Expr expr, int iterations,
}
}

public static void runUnaryIntToLongRegTest(Expr expr, int iterations,
Random rng) {
if (!(expr.isUnaryArgumentSupported()
&& expr.isIntToLongExprSupported())) {
return;
}

for (int value : getIntBitShifts()) {
log("UnaryIntToLongReg(0X%x) -> 0X%x",
value, expr.intToLongExpr(value));
}

for (int i = 0; i < iterations; i++) {
int value = rng.nextInt();
log("UnaryIntToLongReg(0X%x) -> 0X%x",
value, expr.intToLongExpr(value));
}
}

public static void runBinaryRegRegIntTest(Expr expr, int iterations,
Random rng) {
if (!(expr.isIntExprSupported()
@@ -61,6 +61,10 @@ public boolean isLongExprSupported() {
return false;
}

public boolean isIntToLongExprSupported() {
return false;
}

public boolean isMemExprSupported() {
return false;
}
@@ -113,6 +117,10 @@ public long longExpr(MemL a, MemL b) {
throw new UnsupportedOperationException();
}

public long intToLongExpr(int reg) {
throw new UnsupportedOperationException();
}

public static class BMIExpr extends Expr {

public boolean isMemExprSupported() {
@@ -158,6 +166,13 @@ public boolean isLongExprSupported() {
}
}

public static class BMIUnaryIntToLongExpr extends BMIUnaryExpr {
public boolean isIntToLongExprSupported() {
return true;
}
}


public static class BitCountingExpr extends Expr {
public boolean isUnaryArgumentSupported() {
return true;
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @key randomness
* @summary Verify that results of computations are the same w/
* and w/o usage of BZHI instruction
* @library /test/lib /
* @modules java.base/jdk.internal.misc
* java.management
* @build sun.hotspot.WhiteBox
* @run driver ClassFileInstaller sun.hotspot.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
* -XX:+WhiteBoxAPI compiler.intrinsics.bmi.TestBzhiI2L
*/

package compiler.intrinsics.bmi;

import sun.hotspot.cpuinfo.CPUInfo;

public class TestBzhiI2L {

public static void main(String args[]) throws Throwable {
if (!CPUInfo.hasFeature("bmi2")) {
System.out.println("INFO: CPU does not support bmi2 feature, test SKIPPED" );
return;
}

BMITestRunner.runTests(BzhiI2LExpr.class, args,
"-XX:+IgnoreUnrecognizedVMOptions",
"-XX:+UseBMI2Instructions");
BMITestRunner.runTests(BzhiI2LCommutativeExpr.class, args,
"-XX:+IgnoreUnrecognizedVMOptions",
"-XX:+UseBMI2Instructions");
}

public static class BzhiI2LExpr extends Expr.BMIUnaryIntToLongExpr {

public long intToLongExpr(int src1) {
int value = src1;
long returnValue = 0L;

for(int i = 0; i < 10000; ++i) {
returnValue =
(long)(value & 0x1) ^ (long)(value & 0x3) ^ (long)(value & 0x7) ^ (long)(value & 0xF) ^
(long)(value & 0x1F) ^ (long)(value & 0x3F) ^ (long)(value & 0x7F) ^ (long)(value & 0xFF) ^
(long)(value & 0x1FF) ^ (long)(value & 0x3FF) ^ (long)(value & 0x7FF) ^ (long)(value & 0xFFF) ^
(long)(value & 0x1FFF) ^ (long)(value & 0x3FFF) ^ (long)(value & 0x7FFF) ^ (long)(value & 0xFFFF);
}
return returnValue;
}
}

public static class BzhiI2LCommutativeExpr extends Expr.BMIUnaryIntToLongExpr {

public long intToLongExpr(int src1) {
int value = src1;
long returnValue = 0L;

for(int i = 0; i < 10000; ++i) {
returnValue =
(long)(value & 0x1) ^ (long)(value & 0x3) ^ (long)(value & 0x7) ^ (long)(value & 0xF) ^
(long)(value & 0x1F) ^ (long)(value & 0x3F) ^ (long)(value & 0x7F) ^ (long)(value & 0xFF) ^
(long)(value & 0x1FF) ^ (long)(value & 0x3FF) ^ (long)(value & 0x7FF) ^ (long)(value & 0xFFF) ^
(long)(value & 0x1FFF) ^ (long)(value & 0x3FFF) ^ (long)(value & 0x7FFF) ^ (long)(value & 0xFFFF);
}
return returnValue;
}
}
}
@@ -51,7 +51,12 @@ public static void verifyTestCase(Function<Method, BmiTestCase> constructor, Met

@Override
protected void test() throws Exception {
BmiTestCase bmiTestCase = (BmiTestCase) testCase;
BmiTestCase bmiTestCase;
if (((BmiTestCase) testCase).getTestCaseX64()) {
bmiTestCase = (BmiTestCase_x64) testCase;
} else {
bmiTestCase = (BmiTestCase) testCase;
}

if (!(Platform.isX86() || Platform.isX64())) {
System.out.println("Unsupported platform, test SKIPPED");
@@ -105,10 +110,13 @@ protected void checkCompilation(Executable executable, int level) {

protected void checkEmittedCode(Executable executable) {
final byte[] nativeCode = NMethod.get(executable, false).insts;
final byte[] matchInstrPattern = (((BmiTestCase) testCase).getTestCaseX64()) ? ((BmiTestCase_x64) testCase).getInstrPattern_x64() : ((BmiTestCase) testCase).getInstrPattern();
if (!((BmiTestCase) testCase).verifyPositive(nativeCode)) {
throw new AssertionError(testCase.name() + "CPU instructions expected not found: " + Utils.toHexString(nativeCode));
throw new AssertionError(testCase.name() + " " + "CPU instructions expected not found in nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +
Utils.toHexString(matchInstrPattern));
} else {
System.out.println("CPU instructions found, PASSED");
System.out.println("CPU instructions found, PASSED, nativeCode: " + Utils.toHexString(nativeCode) + " ---- Expected instrPattern: " +
Utils.toHexString(matchInstrPattern));
}
}

@@ -117,6 +125,8 @@ abstract static class BmiTestCase implements CompilerWhiteBoxTest.TestCase {
protected byte[] instrMask;
protected byte[] instrPattern;
protected boolean isLongOperation;
protected String cpuFlag = "bmi1";
protected String vmFlag = "UseBMI1Instructions";

public BmiTestCase(Method method) {
this.method = method;
@@ -142,6 +152,10 @@ public boolean isOsr() {
return false;
}

public byte[] getInstrPattern() {
return instrPattern;
}

protected int countCpuInstructions(byte[] nativeCode) {
return countCpuInstructions(nativeCode, instrMask, instrPattern);
}
@@ -177,11 +191,15 @@ public boolean verifyPositive(byte[] nativeCode) {
}

protected String getCpuFlag() {
return "bmi1";
return cpuFlag;
}

protected String getVMFlag() {
return "UseBMI1Instructions";
return vmFlag;
}

protected boolean getTestCaseX64() {
return false;
}
}

@@ -193,6 +211,14 @@ protected BmiTestCase_x64(Method method) {
super(method);
}

public byte[] getInstrPattern_x64() {
return instrPattern_x64;
}

protected boolean getTestCaseX64() {
return true;
}

protected int countCpuInstructions(byte[] nativeCode) {
int cnt = super.countCpuInstructions(nativeCode);
if (Platform.isX64()) { // on x64 platform the instruction we search for can be encoded in 2 different ways

0 comments on commit 4f4ca0e

Please sign in to comment.