AArch64ArrayEqualsOp.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2013, 2015, 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 */
23package org.graalvm.compiler.lir.aarch64;
24
25import static jdk.vm.ci.aarch64.AArch64.zr;
26import static jdk.vm.ci.code.ValueUtil.asRegister;
27import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
28
29import java.lang.reflect.Array;
30import java.lang.reflect.Field;
31
32import org.graalvm.compiler.asm.Label;
33import org.graalvm.compiler.asm.aarch64.AArch64Address;
34import org.graalvm.compiler.asm.aarch64.AArch64Assembler.ConditionFlag;
35import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
36import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler.ScratchRegister;
37import org.graalvm.compiler.core.common.LIRKind;
38import org.graalvm.compiler.lir.LIRInstructionClass;
39import org.graalvm.compiler.lir.Opcode;
40import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
41import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
42
43import jdk.vm.ci.code.Register;
44import jdk.vm.ci.meta.JavaKind;
45import jdk.vm.ci.meta.Value;
46import sun.misc.Unsafe;
47
48/**
49 * Emits code which compares two arrays of the same length. If the CPU supports any vector
50 * instructions specialized code is emitted to leverage these instructions.
51 */
52@Opcode("ARRAY_EQUALS")
53public final class AArch64ArrayEqualsOp extends AArch64LIRInstruction {
54    public static final LIRInstructionClass<AArch64ArrayEqualsOp> TYPE = LIRInstructionClass.create(AArch64ArrayEqualsOp.class);
55
56    private final JavaKind kind;
57    private final int arrayBaseOffset;
58    private final int arrayIndexScale;
59
60    @Def({REG}) protected Value resultValue;
61    @Alive({REG}) protected Value array1Value;
62    @Alive({REG}) protected Value array2Value;
63    @Alive({REG}) protected Value lengthValue;
64    @Temp({REG}) protected Value temp1;
65    @Temp({REG}) protected Value temp2;
66    @Temp({REG}) protected Value temp3;
67    @Temp({REG}) protected Value temp4;
68
69    public AArch64ArrayEqualsOp(LIRGeneratorTool tool, JavaKind kind, Value result, Value array1, Value array2, Value length) {
70        super(TYPE);
71        this.kind = kind;
72
73        Class<?> arrayClass = Array.newInstance(kind.toJavaClass(), 0).getClass();
74        this.arrayBaseOffset = UNSAFE.arrayBaseOffset(arrayClass);
75        this.arrayIndexScale = UNSAFE.arrayIndexScale(arrayClass);
76
77        this.resultValue = result;
78        this.array1Value = array1;
79        this.array2Value = array2;
80        this.lengthValue = length;
81
82        // Allocate some temporaries.
83        this.temp1 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
84        this.temp2 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind()));
85        this.temp3 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
86        this.temp4 = tool.newVariable(LIRKind.value(tool.target().arch.getWordKind()));
87    }
88
89    @Override
90    public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
91        Register result = asRegister(resultValue);
92        Register array1 = asRegister(temp1);
93        Register array2 = asRegister(temp2);
94        Register length = asRegister(temp3);
95
96        Label breakLabel = new Label();
97
98        try (ScratchRegister sc1 = masm.getScratchRegister()) {
99            Register rscratch1 = sc1.getRegister();
100            // Load array base addresses.
101            masm.lea(array1, AArch64Address.createUnscaledImmediateAddress(asRegister(array1Value), arrayBaseOffset));
102            masm.lea(array2, AArch64Address.createUnscaledImmediateAddress(asRegister(array2Value), arrayBaseOffset));
103
104            // Get array length in bytes.
105            masm.mov(rscratch1, arrayIndexScale);
106            masm.smaddl(length, asRegister(lengthValue), rscratch1, zr);
107            masm.mov(64, result, length); // copy
108
109            emit8ByteCompare(crb, masm, result, array1, array2, length, breakLabel, rscratch1);
110            emitTailCompares(masm, result, array1, array2, breakLabel, rscratch1);
111
112            // Return: rscratch1 is non-zero iff the arrays differ
113            masm.bind(breakLabel);
114            masm.cmp(64, rscratch1, zr);
115            masm.cset(result, ConditionFlag.EQ);
116        }
117    }
118
119    /**
120     * Vector size used in {@link #emit8ByteCompare}.
121     */
122    private static final int VECTOR_SIZE = 8;
123
124    /**
125     * Emits code that uses 8-byte vector compares.
126     *
127     */
128    private void emit8ByteCompare(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register result, Register array1, Register array2, Register length, Label breakLabel,
129                    Register rscratch1) {
130        Label loop = new Label();
131        Label compareTail = new Label();
132
133        Register temp = asRegister(temp4);
134
135        masm.and(64, result, result, VECTOR_SIZE - 1); // tail count (in bytes)
136        masm.ands(64, length, length, ~(VECTOR_SIZE - 1));  // vector count (in bytes)
137        masm.branchConditionally(ConditionFlag.EQ, compareTail);
138
139        masm.lea(array1, AArch64Address.createRegisterOffsetAddress(array1, length, false));
140        masm.lea(array2, AArch64Address.createRegisterOffsetAddress(array2, length, false));
141        masm.sub(64, length, zr, length);
142
143        // Align the main loop
144        masm.align(crb.target.wordSize * 2);
145        masm.bind(loop);
146        masm.ldr(64, temp, AArch64Address.createRegisterOffsetAddress(array1, length, false));
147        masm.ldr(64, rscratch1, AArch64Address.createRegisterOffsetAddress(array2, length, false));
148        masm.eor(64, rscratch1, temp, rscratch1);
149        masm.cbnz(64, rscratch1, breakLabel);
150        masm.add(64, length, length, VECTOR_SIZE);
151        masm.cbnz(64, length, loop);
152
153        masm.cbz(64, result, breakLabel);
154
155        /*
156         * Compare the remaining bytes with an unaligned memory load aligned to the end of the
157         * array.
158         */
159        masm.lea(array1, AArch64Address.createUnscaledImmediateAddress(array1, -VECTOR_SIZE));
160        masm.lea(array2, AArch64Address.createUnscaledImmediateAddress(array2, -VECTOR_SIZE));
161        masm.ldr(64, temp, AArch64Address.createRegisterOffsetAddress(array1, result, false));
162        masm.ldr(64, rscratch1, AArch64Address.createRegisterOffsetAddress(array2, result, false));
163        masm.eor(64, rscratch1, temp, rscratch1);
164        masm.jmp(breakLabel);
165
166        masm.bind(compareTail);
167    }
168
169    /**
170     * Emits code to compare the remaining 1 to 4 bytes.
171     *
172     */
173    private void emitTailCompares(AArch64MacroAssembler masm, Register result, Register array1, Register array2, Label breakLabel, Register rscratch1) {
174        Label compare2Bytes = new Label();
175        Label compare1Byte = new Label();
176        Label end = new Label();
177
178        Register temp = asRegister(temp4);
179
180        if (kind.getByteCount() <= 4) {
181            // Compare trailing 4 bytes, if any.
182            masm.ands(32, zr, result, 4);
183            masm.branchConditionally(ConditionFlag.EQ, compare2Bytes);
184            masm.ldr(32, temp, AArch64Address.createPostIndexedImmediateAddress(array1, 4));
185            masm.ldr(32, rscratch1, AArch64Address.createPostIndexedImmediateAddress(array2, 4));
186            masm.eor(32, rscratch1, temp, rscratch1);
187            masm.cbnz(32, rscratch1, breakLabel);
188
189            if (kind.getByteCount() <= 2) {
190                // Compare trailing 2 bytes, if any.
191                masm.bind(compare2Bytes);
192                masm.ands(32, zr, result, 2);
193                masm.branchConditionally(ConditionFlag.EQ, compare1Byte);
194                masm.ldr(16, temp, AArch64Address.createPostIndexedImmediateAddress(array1, 2));
195                masm.ldr(16, rscratch1, AArch64Address.createPostIndexedImmediateAddress(array2, 2));
196                masm.eor(32, rscratch1, temp, rscratch1);
197                masm.cbnz(32, rscratch1, breakLabel);
198
199                // The one-byte tail compare is only required for boolean and byte arrays.
200                if (kind.getByteCount() <= 1) {
201                    // Compare trailing byte, if any.
202                    masm.bind(compare1Byte);
203                    masm.ands(32, zr, result, 1);
204                    masm.branchConditionally(ConditionFlag.EQ, end);
205                    masm.ldr(8, temp, AArch64Address.createBaseRegisterOnlyAddress(array1));
206                    masm.ldr(8, rscratch1, AArch64Address.createBaseRegisterOnlyAddress(array2));
207                    masm.eor(32, rscratch1, temp, rscratch1);
208                    masm.cbnz(32, rscratch1, breakLabel);
209                } else {
210                    masm.bind(compare1Byte);
211                }
212            } else {
213                masm.bind(compare2Bytes);
214            }
215        }
216        masm.bind(end);
217        masm.mov(64, rscratch1, zr);
218    }
219
220    private static final Unsafe UNSAFE = initUnsafe();
221
222    private static Unsafe initUnsafe() {
223        try {
224            return Unsafe.getUnsafe();
225        } catch (SecurityException se) {
226            try {
227                Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
228                theUnsafe.setAccessible(true);
229                return (Unsafe) theUnsafe.get(Unsafe.class);
230            } catch (Exception e) {
231                throw new RuntimeException("exception while trying to get Unsafe", e);
232            }
233        }
234    }
235}
236