package org.graalvm.compiler.lir.amd64;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
import static jdk.vm.ci.code.ValueUtil.asRegister;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import org.graalvm.compiler.asm.Label;
import org.graalvm.compiler.asm.amd64.AMD64Address;
import org.graalvm.compiler.asm.amd64.AMD64Address.Scale;
import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.lir.LIRInstructionClass;
import org.graalvm.compiler.lir.Opcode;
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.amd64.AMD64.CPUFeature;
import jdk.vm.ci.amd64.AMD64Kind;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.Value;
import sun.misc.Unsafe;
@Opcode("ARRAY_EQUALS")
public final class AMD64ArrayEqualsOp extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64ArrayEqualsOp> TYPE = LIRInstructionClass.create(AMD64ArrayEqualsOp.class);
private final JavaKind kind;
private final int arrayBaseOffset;
private final int arrayIndexScale;
@Def({REG}) protected Value resultValue;
@Alive({REG}) protected Value array1Value;
@Alive({REG}) protected Value array2Value;
@Alive({REG}) protected Value lengthValue;
@Temp({REG}) protected Value temp1;
@Temp({REG}) protected Value temp2;
@Temp({REG}) protected Value temp3;
@Temp({REG}) protected Value temp4;
@Temp({REG, ILLEGAL}) protected Value vectorTemp1;
@Temp({REG, ILLEGAL}) protected Value vectorTemp2;
public AMD64ArrayEqualsOp(LIRGeneratorTool tool, JavaKind kind, Value result, Value array1, Value array2, Value length) {
super(TYPE);
this.kind = kind;
Class<?> arrayClass = Array.newInstance(kind.toJavaClass(), 0).getClass();
this.arrayBaseOffset = UNSAFE.arrayBaseOffset(arrayClass);
this.arrayIndexScale = UNSAFE.arrayIndexScale(arrayClass);
this.resultValue = result;
this.array1Value = array1;
this.array2Value = array2;
this.lengthValue = length;
this.temp1 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
this.temp2 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
this.temp3 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
this.temp4 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
if (supportsSSE41(tool.target())) {
this.vectorTemp1 = tool.newVariable(LIRKind.value(AMD64Kind.DOUBLE));
this.vectorTemp2 = tool.newVariable(LIRKind.value(AMD64Kind.DOUBLE));
} else {
this.vectorTemp1 = Value.ILLEGAL;
this.vectorTemp2 = Value.ILLEGAL;
}
}
@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
Register result = asRegister(resultValue);
Register array1 = asRegister(temp1);
Register array2 = asRegister(temp2);
Register length = asRegister(temp3);
Label trueLabel = new Label();
Label falseLabel = new Label();
Label done = new Label();
masm.leaq(array1, new AMD64Address(asRegister(array1Value), arrayBaseOffset));
masm.leaq(array2, new AMD64Address(asRegister(array2Value), arrayBaseOffset));
masm.imull(length, asRegister(lengthValue), arrayIndexScale);
masm.movl(result, length);
if (supportsAVX2(crb.target)) {
emitAVXCompare(crb, masm, result, array1, array2, length, trueLabel, falseLabel);
} else if (supportsSSE41(crb.target)) {
emitSSE41Compare(crb, masm, result, array1, array2, length, trueLabel, falseLabel);
}
emit8ByteCompare(crb, masm, result, array1, array2, length, trueLabel, falseLabel);
emitTailCompares(masm, result, array1, array2, length, trueLabel, falseLabel);
masm.bind(trueLabel);
masm.movl(result, 1);
masm.jmpb(done);
masm.bind(falseLabel);
masm.xorl(result, result);
masm.bind(done);
}
private static boolean supportsSSE41(TargetDescription target) {
AMD64 arch = (AMD64) target.arch;
return arch.getFeatures().contains(CPUFeature.SSE4_1);
}
private static final int SSE4_1_VECTOR_SIZE = 16;
private void emitSSE41Compare(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, Register array1, Register array2, Register length, Label trueLabel, Label falseLabel) {
assert supportsSSE41(crb.target);
Register vector1 = asRegister(vectorTemp1, AMD64Kind.DOUBLE);
Register vector2 = asRegister(vectorTemp2, AMD64Kind.DOUBLE);
Label loop = new Label();
Label compareTail = new Label();
masm.andl(result, SSE4_1_VECTOR_SIZE - 1);
masm.andl(length, ~(SSE4_1_VECTOR_SIZE - 1));
masm.jccb(ConditionFlag.Zero, compareTail);
masm.leaq(array1, new AMD64Address(array1, length, Scale.Times1, 0));
masm.leaq(array2, new AMD64Address(array2, length, Scale.Times1, 0));
masm.negq(length);
masm.align(crb.target.wordSize * 2);
masm.bind(loop);
masm.movdqu(vector1, new AMD64Address(array1, length, Scale.Times1, 0));
masm.movdqu(vector2, new AMD64Address(array2, length, Scale.Times1, 0));
masm.pxor(vector1, vector2);
masm.ptest(vector1, vector1);
masm.jcc(ConditionFlag.NotZero, falseLabel);
masm.addq(length, SSE4_1_VECTOR_SIZE);
masm.jcc(ConditionFlag.NotZero, loop);
masm.testl(result, result);
masm.jcc(ConditionFlag.Zero, trueLabel);
masm.movdqu(vector1, new AMD64Address(array1, result, Scale.Times1, -SSE4_1_VECTOR_SIZE));
masm.movdqu(vector2, new AMD64Address(array2, result, Scale.Times1, -SSE4_1_VECTOR_SIZE));
masm.pxor(vector1, vector2);
masm.ptest(vector1, vector1);
masm.jcc(ConditionFlag.NotZero, falseLabel);
masm.jmp(trueLabel);
masm.bind(compareTail);
masm.movl(length, result);
}
private static boolean supportsAVX2(TargetDescription target) {
AMD64 arch = (AMD64) target.arch;
return arch.getFeatures().contains(CPUFeature.AVX2);
}
private static final int AVX_VECTOR_SIZE = 32;
private void emitAVXCompare(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, Register array1, Register array2, Register length, Label trueLabel, Label falseLabel) {
assert supportsAVX2(crb.target);
Register vector1 = asRegister(vectorTemp1, AMD64Kind.DOUBLE);
Register vector2 = asRegister(vectorTemp2, AMD64Kind.DOUBLE);
Label loop = new Label();
Label compareTail = new Label();
masm.andl(result, AVX_VECTOR_SIZE - 1);
masm.andl(length, ~(AVX_VECTOR_SIZE - 1));
masm.jccb(ConditionFlag.Zero, compareTail);
masm.leaq(array1, new AMD64Address(array1, length, Scale.Times1, 0));
masm.leaq(array2, new AMD64Address(array2, length, Scale.Times1, 0));
masm.negq(length);
masm.align(crb.target.wordSize * 2);
masm.bind(loop);
masm.vmovdqu(vector1, new AMD64Address(array1, length, Scale.Times1, 0));
masm.vmovdqu(vector2, new AMD64Address(array2, length, Scale.Times1, 0));
masm.vpxor(vector1, vector1, vector2);
masm.vptest(vector1, vector1);
masm.jcc(ConditionFlag.NotZero, falseLabel);
masm.addq(length, AVX_VECTOR_SIZE);
masm.jcc(ConditionFlag.NotZero, loop);
masm.testl(result, result);
masm.jcc(ConditionFlag.Zero, trueLabel);
masm.vmovdqu(vector1, new AMD64Address(array1, result, Scale.Times1, -AVX_VECTOR_SIZE));
masm.vmovdqu(vector2, new AMD64Address(array2, result, Scale.Times1, -AVX_VECTOR_SIZE));
masm.vpxor(vector1, vector1, vector2);
masm.vptest(vector1, vector1);
masm.jcc(ConditionFlag.NotZero, falseLabel);
masm.jmp(trueLabel);
masm.bind(compareTail);
masm.movl(length, result);
}
private static final int VECTOR_SIZE = 8;
private void emit8ByteCompare(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, Register array1, Register array2, Register length, Label trueLabel, Label falseLabel) {
Label loop = new Label();
Label compareTail = new Label();
Register temp = asRegister(temp4);
masm.andl(result, VECTOR_SIZE - 1);
masm.andl(length, ~(VECTOR_SIZE - 1));
masm.jccb(ConditionFlag.Zero, compareTail);
masm.leaq(array1, new AMD64Address(array1, length, Scale.Times1, 0));
masm.leaq(array2, new AMD64Address(array2, length, Scale.Times1, 0));
masm.negq(length);
masm.align(crb.target.wordSize * 2);
masm.bind(loop);
masm.movq(temp, new AMD64Address(array1, length, Scale.Times1, 0));
masm.cmpq(temp, new AMD64Address(array2, length, Scale.Times1, 0));
masm.jccb(ConditionFlag.NotEqual, falseLabel);
masm.addq(length, VECTOR_SIZE);
masm.jccb(ConditionFlag.NotZero, loop);
masm.testl(result, result);
masm.jccb(ConditionFlag.Zero, trueLabel);
masm.movq(temp, new AMD64Address(array1, result, Scale.Times1, -VECTOR_SIZE));
masm.cmpq(temp, new AMD64Address(array2, result, Scale.Times1, -VECTOR_SIZE));
masm.jccb(ConditionFlag.NotEqual, falseLabel);
masm.jmpb(trueLabel);
masm.bind(compareTail);
masm.movl(length, result);
}
private void emitTailCompares(AMD64MacroAssembler masm, Register result, Register array1, Register array2, Register length, Label trueLabel, Label falseLabel) {
Label compare2Bytes = new Label();
Label compare1Byte = new Label();
Register temp = asRegister(temp4);
if (kind.getByteCount() <= 4) {
masm.testl(result, 4);
masm.jccb(ConditionFlag.Zero, compare2Bytes);
masm.movl(temp, new AMD64Address(array1, 0));
masm.cmpl(temp, new AMD64Address(array2, 0));
masm.jccb(ConditionFlag.NotEqual, falseLabel);
if (kind.getByteCount() <= 2) {
masm.leaq(array1, new AMD64Address(array1, 4));
masm.leaq(array2, new AMD64Address(array2, 4));
masm.bind(compare2Bytes);
masm.testl(result, 2);
masm.jccb(ConditionFlag.Zero, compare1Byte);
masm.movzwl(temp, new AMD64Address(array1, 0));
masm.movzwl(length, new AMD64Address(array2, 0));
masm.cmpl(temp, length);
masm.jccb(ConditionFlag.NotEqual, falseLabel);
if (kind.getByteCount() <= 1) {
masm.leaq(array1, new AMD64Address(array1, 2));
masm.leaq(array2, new AMD64Address(array2, 2));
masm.bind(compare1Byte);
masm.testl(result, 1);
masm.jccb(ConditionFlag.Zero, trueLabel);
masm.movzbl(temp, new AMD64Address(array1, 0));
masm.movzbl(length, new AMD64Address(array2, 0));
masm.cmpl(temp, length);
masm.jccb(ConditionFlag.NotEqual, falseLabel);
} else {
masm.bind(compare1Byte);
}
} else {
masm.bind(compare2Bytes);
}
}
}
private static final Unsafe UNSAFE = initUnsafe();
private static Unsafe initUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException se) {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(Unsafe.class);
} catch (Exception e) {
throw new RuntimeException("exception while trying to get Unsafe", e);
}
}
}
}