package org.graalvm.compiler.lir.amd64;
import static jdk.vm.ci.amd64.AMD64.k1;
import static jdk.vm.ci.amd64.AMD64.k2;
import static jdk.vm.ci.amd64.AMD64.rdi;
import static jdk.vm.ci.amd64.AMD64.rdx;
import static jdk.vm.ci.amd64.AMD64.rsi;
import static jdk.vm.ci.code.ValueUtil.asRegister;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
import jdk.vm.ci.amd64.AMD64;
import org.graalvm.compiler.asm.Label;
import org.graalvm.compiler.asm.amd64.AMD64Address;
import org.graalvm.compiler.asm.amd64.AMD64Assembler;
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.AMD64Kind;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.meta.Value;
@Opcode("AMD64_STRING_INFLATE")
public final class AMD64StringLatin1InflateOp extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64StringLatin1InflateOp> TYPE = LIRInstructionClass.create(AMD64StringLatin1InflateOp.class);
@Alive({REG}) private Value rsrc;
@Alive({REG}) private Value rdst;
@Alive({REG}) private Value rlen;
@Temp({REG}) private Value vtmp1;
@Temp({REG}) private Value rtmp2;
public AMD64StringLatin1InflateOp(LIRGeneratorTool tool, Value src, Value dst, Value len) {
super(TYPE);
assert asRegister(src).equals(rsi);
assert asRegister(dst).equals(rdi);
assert asRegister(len).equals(rdx);
rsrc = src;
rdst = dst;
rlen = len;
vtmp1 = tool.newVariable(LIRKind.value(AMD64Kind.V512_BYTE));
rtmp2 = tool.newVariable(LIRKind.value(AMD64Kind.DWORD));
}
@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
Register src = asRegister(rsrc);
Register dst = asRegister(rdst);
Register len = asRegister(rlen);
Register tmp1 = asRegister(vtmp1);
Register tmp2 = asRegister(rtmp2);
byteArrayInflate(masm, src, dst, len, tmp1, tmp2);
}
private static void byteArrayInflate(AMD64MacroAssembler masm, Register src, Register dst, Register len, Register vtmp, Register tmp) {
assert vtmp.getRegisterCategory().equals(AMD64.XMM);
Label labelDone = new Label();
Label labelBelowThreshold = new Label();
assert src.number != dst.number && src.number != len.number && src.number != tmp.number;
assert dst.number != len.number && dst.number != tmp.number;
assert len.number != tmp.number;
if (masm.supports(AMD64.CPUFeature.AVX512BW) &&
masm.supports(AMD64.CPUFeature.AVX512VL) &&
masm.supports(AMD64.CPUFeature.BMI2)) {
masm.testl(len, -16);
masm.jcc(AMD64Assembler.ConditionFlag.Zero, labelBelowThreshold);
Label labelAvx512Tail = new Label();
masm.movl(tmp, len);
masm.andl(tmp, -32);
masm.jccb(AMD64Assembler.ConditionFlag.Zero, labelAvx512Tail);
masm.andl(len, 32 - 1);
masm.leaq(src, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.leaq(dst, new AMD64Address(dst, tmp, AMD64Address.Scale.Times2));
masm.negq(tmp);
Label labelAvx512Loop = new Label();
masm.bind(labelAvx512Loop);
masm.evpmovzxbw(vtmp, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.evmovdqu16(new AMD64Address(dst, tmp, AMD64Address.Scale.Times2), vtmp);
masm.addq(tmp, 32);
masm.jcc(AMD64Assembler.ConditionFlag.NotZero, labelAvx512Loop);
masm.bind(labelAvx512Tail);
masm.testl(len, len);
masm.jcc(AMD64Assembler.ConditionFlag.Zero, labelDone);
masm.kmovq(k2, k1);
masm.movl(tmp, -1);
masm.shlxl(tmp, tmp, len);
masm.notl(tmp);
masm.kmovd(k1, tmp);
masm.evpmovzxbw(vtmp, k1, new AMD64Address(src));
masm.evmovdqu16(new AMD64Address(dst), k1, vtmp);
masm.kmovq(k1, k2);
masm.jmp(labelDone);
}
if (masm.supports(AMD64.CPUFeature.SSE4_1)) {
Label labelSSETail = new Label();
if (masm.supports(AMD64.CPUFeature.AVX2)) {
Label labelAvx2Tail = new Label();
masm.movl(tmp, len);
masm.andl(tmp, -16);
masm.jccb(AMD64Assembler.ConditionFlag.Zero, labelAvx2Tail);
masm.andl(len, 16 - 1);
masm.leaq(src, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.leaq(dst, new AMD64Address(dst, tmp, AMD64Address.Scale.Times2));
masm.negq(tmp);
Label labelAvx2Loop = new Label();
masm.bind(labelAvx2Loop);
masm.vpmovzxbw(vtmp, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.vmovdqu(new AMD64Address(dst, tmp, AMD64Address.Scale.Times2), vtmp);
masm.addq(tmp, 16);
masm.jcc(AMD64Assembler.ConditionFlag.NotZero, labelAvx2Loop);
masm.bind(labelBelowThreshold);
masm.bind(labelAvx2Tail);
masm.movl(tmp, len);
masm.andl(tmp, -8);
masm.jccb(AMD64Assembler.ConditionFlag.Zero, labelSSETail);
masm.andl(len, 8 - 1);
masm.pmovzxbw(vtmp, new AMD64Address(src));
masm.movdqu(new AMD64Address(dst), vtmp);
masm.addq(src, 8);
masm.addq(dst, 16);
} else {
masm.movl(tmp, len);
masm.andl(tmp, -8);
masm.jccb(AMD64Assembler.ConditionFlag.Zero, labelSSETail);
masm.andl(len, 8 - 1);
masm.leaq(src, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.leaq(dst, new AMD64Address(dst, tmp, AMD64Address.Scale.Times2));
masm.negq(tmp);
Label labelSSECopy8Loop = new Label();
masm.bind(labelSSECopy8Loop);
masm.pmovzxbw(vtmp, new AMD64Address(src, tmp, AMD64Address.Scale.Times1));
masm.movdqu(new AMD64Address(dst, tmp, AMD64Address.Scale.Times2), vtmp);
masm.addq(tmp, 8);
masm.jcc(AMD64Assembler.ConditionFlag.NotZero, labelSSECopy8Loop);
}
Label labelCopyChars = new Label();
masm.bind(labelSSETail);
masm.cmpl(len, 4);
masm.jccb(AMD64Assembler.ConditionFlag.Less, labelCopyChars);
masm.movdl(vtmp, new AMD64Address(src));
masm.pmovzxbw(vtmp, vtmp);
masm.movq(new AMD64Address(dst), vtmp);
masm.subq(len, 4);
masm.addq(src, 4);
masm.addq(dst, 8);
masm.bind(labelCopyChars);
}
masm.testl(len, len);
masm.jccb(AMD64Assembler.ConditionFlag.Zero, labelDone);
masm.leaq(src, new AMD64Address(src, len, AMD64Address.Scale.Times1));
masm.leaq(dst, new AMD64Address(dst, len, AMD64Address.Scale.Times2));
masm.negq(len);
Label labelCopyCharsLoop = new Label();
masm.bind(labelCopyCharsLoop);
masm.movzbl(tmp, new AMD64Address(src, len, AMD64Address.Scale.Times1));
masm.movw(new AMD64Address(dst, len, AMD64Address.Scale.Times2), tmp);
masm.incrementq(len, 1);
masm.jcc(AMD64Assembler.ConditionFlag.NotZero, labelCopyCharsLoop);
masm.bind(labelDone);
}
}