8261671: X86 I2L conversion can be skipped for certain masked positive values

Reviewed-by: kvn, neliasso, vlivanov
This commit is contained in:
Marcus G K Williams 2021-03-17 16:19:06 +00:00 committed by Sandhya Viswanathan
parent 5d87a21991
commit 4f4ca0e705
7 changed files with 370 additions and 5 deletions

View File

@ -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)
%{

View File

@ -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);
}
}

View File

@ -218,6 +218,7 @@ public class BMITestRunner {
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 class BMITestRunner {
}
}
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()

View File

@ -61,6 +61,10 @@ public abstract class Expr {
return false;
}
public boolean isIntToLongExprSupported() {
return false;
}
public boolean isMemExprSupported() {
return false;
}
@ -113,6 +117,10 @@ public abstract class Expr {
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 abstract class Expr {
}
}
public static class BMIUnaryIntToLongExpr extends BMIUnaryExpr {
public boolean isIntToLongExprSupported() {
return true;
}
}
public static class BitCountingExpr extends Expr {
public boolean isUnaryArgumentSupported() {
return true;

View File

@ -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;
}
}
}

View File

@ -51,7 +51,12 @@ public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
@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 @@ public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
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 @@ public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
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 class BmiIntrinsicBase extends CompilerWhiteBoxTest {
return false;
}
public byte[] getInstrPattern() {
return instrPattern;
}
protected int countCpuInstructions(byte[] nativeCode) {
return countCpuInstructions(nativeCode, instrMask, instrPattern);
}
@ -177,11 +191,15 @@ public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
}
protected String getCpuFlag() {
return "bmi1";
return cpuFlag;
}
protected String getVMFlag() {
return "UseBMI1Instructions";
return vmFlag;
}
protected boolean getTestCaseX64() {
return false;
}
}
@ -193,6 +211,14 @@ public class BmiIntrinsicBase extends CompilerWhiteBoxTest {
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

View File

@ -0,0 +1,81 @@
/*
* 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
* @requires vm.flavor == "server" & !vm.emulatedClient & !vm.graal.enabled
* @library /test/lib /
* @modules java.base/jdk.internal.misc
* java.management
*
* @build sun.hotspot.WhiteBox
* @run driver ClassFileInstaller sun.hotspot.WhiteBox
* @run main/bootclasspath/othervm -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+AbortVMOnCompilationFailure
* -XX:+IgnoreUnrecognizedVMOptions -XX:+UseBMI2Instructions
* compiler.intrinsics.bmi.verifycode.BzhiTestI2L
*/
package compiler.intrinsics.bmi.verifycode;
import compiler.intrinsics.bmi.TestBzhiI2L;
import java.lang.reflect.Method;
public class BzhiTestI2L extends BmiIntrinsicBase.BmiTestCase_x64 {
protected BzhiTestI2L(Method method) {
super(method);
cpuFlag = "bmi2";
vmFlag = "UseBMI2Instructions";
isLongOperation = true;
// from intel manual VEX.LZ.0F38.W1 F5 /r
instrMask = new byte[]{
(byte) 0xFF,
(byte) 0x1F,
(byte) 0x80,
(byte) 0xFF};
instrPattern = new byte[]{
(byte) 0xC4, // prefix for 3-byte VEX instruction
(byte) 0x02, // 00010 implied 0F 38 leading opcode bytes
(byte) 0x80,
(byte) 0xF5};
instrMask_x64 = new byte[]{
(byte) 0xFF,
(byte) 0x7F,
(byte) 0xFF,
(byte) 0xFF};
instrPattern_x64 = new byte[]{
(byte) 0xC4, // prefix for 3-byte VEX instruction
(byte) 0x62, // 00010 implied 0F 38 leading opcode bytes
(byte) 0xA8,
(byte) 0xF5};
}
public static void main(String[] args) throws Exception {
BmiIntrinsicBase.verifyTestCase(BzhiTestI2L::new, TestBzhiI2L.BzhiI2LExpr.class.getDeclaredMethods());
BmiIntrinsicBase.verifyTestCase(BzhiTestI2L::new, TestBzhiI2L.BzhiI2LCommutativeExpr.class.getDeclaredMethods());
}
}