1 /* 2 * Copyright (c) 2018, 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 */ 23 24 25 package org.graalvm.compiler.lir.aarch64; 26 27 import static jdk.vm.ci.aarch64.AArch64.zr; 28 import static jdk.vm.ci.code.ValueUtil.asRegister; 29 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG; 30 31 import org.graalvm.compiler.asm.Label; 32 import org.graalvm.compiler.asm.aarch64.AArch64Address; 33 import org.graalvm.compiler.asm.aarch64.AArch64Assembler.ConditionFlag; 34 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler; 35 import org.graalvm.compiler.core.common.LIRKind; 36 import org.graalvm.compiler.lir.LIRInstructionClass; 37 import org.graalvm.compiler.lir.Opcode; 38 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 39 import org.graalvm.compiler.lir.gen.LIRGeneratorTool; 40 41 import jdk.vm.ci.code.Register; 42 import jdk.vm.ci.meta.JavaKind; 43 import jdk.vm.ci.meta.Value; 44 45 /** 46 * Emits code which compares two arrays lexicographically. If the CPU supports any vector 47 * instructions specialized code is emitted to leverage these instructions. 48 */ 49 @Opcode("ARRAY_COMPARE_TO") 50 public final class AArch64ArrayCompareToOp extends AArch64LIRInstruction { 51 public static final LIRInstructionClass<AArch64ArrayCompareToOp> TYPE = LIRInstructionClass.create(AArch64ArrayCompareToOp.class); 52 53 private final JavaKind kind1; 54 private final JavaKind kind2; 55 56 private final int array1BaseOffset; 57 private final int array2BaseOffset; 58 59 @Def({REG}) protected Value resultValue; 60 61 @Alive({REG}) protected Value array1Value; 62 @Alive({REG}) protected Value array2Value; 63 @Use({REG}) protected Value length1Value; 64 @Use({REG}) protected Value length2Value; 65 @Temp({REG}) protected Value length1ValueTemp; 66 @Temp({REG}) protected Value length2ValueTemp; 67 68 @Temp({REG}) protected Value temp1; 69 @Temp({REG}) protected Value temp2; 70 @Temp({REG}) protected Value temp3; 71 @Temp({REG}) protected Value temp4; 72 @Temp({REG}) protected Value temp5; 73 @Temp({REG}) protected Value temp6; 74 75 public AArch64ArrayCompareToOp(LIRGeneratorTool tool, JavaKind kind1, JavaKind kind2, Value result, Value array1, Value array2, Value length1, Value length2) { 76 super(TYPE); 77 this.kind1 = kind1; 78 this.kind2 = kind2; 79 80 // Both offsets should be the same but better be safe than sorry. 81 this.array1BaseOffset = tool.getProviders().getMetaAccess().getArrayBaseOffset(kind1); 82 this.array2BaseOffset = tool.getProviders().getMetaAccess().getArrayBaseOffset(kind2); 83 84 this.resultValue = result; 85 86 this.array1Value = array1; 87 this.array2Value = array2; 88 89 /* 90 * The length values are inputs but are also killed like temporaries so need both Use and 91 * Temp annotations, which will only work with fixed registers. 92 */ 93 94 this.length1Value = length1; 95 this.length2Value = length2; 96 this.length1ValueTemp = length1; 97 this.length2ValueTemp = length2; 98 99 // Allocate some temporaries. 100 this.temp1 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 101 this.temp2 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 102 this.temp3 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 103 this.temp4 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 104 this.temp5 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 105 this.temp6 = tool.newVariable(LIRKind.unknownReference(tool.target().arch.getWordKind())); 106 } 107 108 @Override 109 protected void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) { 110 111 Register result = asRegister(resultValue); 112 Register length1 = asRegister(length1Value); 113 Register length2 = asRegister(length2Value); 114 115 Register array1 = asRegister(temp1); 116 Register array2 = asRegister(temp2); 117 Register length = asRegister(temp3); 118 Register temp = asRegister(temp4); 119 Register tailCount = asRegister(temp5); 120 Register vecCount = asRegister(temp6); 121 122 // Checkstyle: stop 123 final Label BREAK_LABEL = new Label(); 124 final Label STRING_DIFFER_LABEL = new Label(); 125 final Label LENGTH_DIFFER_LABEL = new Label(); 126 final Label MAIN_LOOP_LABEL = new Label(); 127 final Label COMPARE_SHORT_LABEL = new Label(); 128 // Checkstyle: resume 129 130 // Checkstyle: stop 131 int CHAR_SIZE_BYTES = 1; 132 int VECTOR_SIZE_BYTES = 8; 133 int VECTOR_COUNT_BYTES = 8; 134 // Checkstyle: resume 135 136 // Byte is expanded to short if we compare strings with different encoding 137 if (kind1 != kind2 || kind1 == JavaKind.Char) { 138 CHAR_SIZE_BYTES = 2; 139 } 140 141 if (kind1 != kind2) { 142 VECTOR_COUNT_BYTES = 4; 143 } 144 145 // Load array base addresses. 146 masm.lea(array1, AArch64Address.createUnscaledImmediateAddress(asRegister(array1Value), array1BaseOffset)); 147 masm.lea(array2, AArch64Address.createUnscaledImmediateAddress(asRegister(array2Value), array2BaseOffset)); 148 149 // Calculate minimal length in chars for different kind case 150 // Conditions could be squashed but lets keep it readable 151 if (kind1 != kind2) { 152 masm.lshr(64, length2, length2, 1); 153 } 154 155 if (kind1 == kind2 && kind1 == JavaKind.Char) { 156 masm.lshr(64, length1, length1, 1); 157 masm.lshr(64, length2, length2, 1); 158 } 159 160 masm.cmp(64, length1, length2); 161 masm.cmov(64, length, length1, length2, ConditionFlag.LT); 162 163 // One of strings is empty 164 masm.cbz(64, length, LENGTH_DIFFER_LABEL); 165 166 // Go back to bytes if necessary 167 if (kind1 != kind2 || kind1 == JavaKind.Char) { 168 masm.shl(64, length, length, 1); 169 } 170 171 masm.mov(64, vecCount, zr); 172 masm.and(64, tailCount, length, VECTOR_SIZE_BYTES - 1); // tail count (in bytes) 173 masm.ands(64, length, length, ~(VECTOR_SIZE_BYTES - 1)); // vector count (in bytes) 174 175 // Length of string is less than VECTOR_SIZE, go to simple compare 176 masm.branchConditionally(ConditionFlag.EQ, COMPARE_SHORT_LABEL); 177 178 // MAIN_LOOP - read strings by 8 byte. 179 masm.bind(MAIN_LOOP_LABEL); 180 if (kind1 != kind2) { 181 // Load 32 bits ad unpack it to entire 64bit register 182 masm.ldr(32, result, AArch64Address.createRegisterOffsetAddress(array1, vecCount, false)); 183 masm.ubfm(64, temp, result, 0, 7); 184 masm.lshr(64, result, result, 8); 185 masm.bfm(64, temp, result, 48, 7); 186 masm.lshr(64, result, result, 8); 187 masm.bfm(64, temp, result, 32, 7); 188 masm.lshr(64, result, result, 8); 189 masm.bfm(64, temp, result, 16, 7); 190 // Unpacked value placed in temp now 191 192 masm.shl(64, result, vecCount, 1); 193 masm.ldr(64, result, AArch64Address.createRegisterOffsetAddress(array2, result, false)); 194 } else { 195 masm.ldr(64, temp, AArch64Address.createRegisterOffsetAddress(array1, vecCount, false)); 196 masm.ldr(64, result, AArch64Address.createRegisterOffsetAddress(array2, vecCount, false)); 197 } 198 masm.eor(64, result, temp, result); 199 masm.cbnz(64, result, STRING_DIFFER_LABEL); 200 masm.add(64, vecCount, vecCount, VECTOR_COUNT_BYTES); 201 masm.cmp(64, vecCount, length); 202 masm.branchConditionally(ConditionFlag.LT, MAIN_LOOP_LABEL); 203 // End of MAIN_LOOP 204 205 // Strings are equal and no TAIL go to END 206 masm.cbz(64, tailCount, LENGTH_DIFFER_LABEL); 207 208 // Compaire tail of long string ... 209 masm.lea(array1, AArch64Address.createRegisterOffsetAddress(array1, length, false)); 210 masm.lea(array2, AArch64Address.createRegisterOffsetAddress(array2, length, false)); 211 212 // ... or string less than vector length 213 masm.bind(COMPARE_SHORT_LABEL); 214 for (int i = 0; i < VECTOR_COUNT_BYTES; i += CHAR_SIZE_BYTES) { 215 if (kind1 != kind2) { 216 masm.ldr(8, temp, AArch64Address.createUnscaledImmediateAddress(array1, i / 2)); 217 } else { 218 masm.ldr(8 * CHAR_SIZE_BYTES, temp, AArch64Address.createUnscaledImmediateAddress(array1, i)); 219 } 220 221 masm.ldr(8 * CHAR_SIZE_BYTES, result, AArch64Address.createUnscaledImmediateAddress(array2, i)); 222 223 if (kind1 != kind2 && kind1 == JavaKind.Char) { 224 // Weird swap of substraction order 225 masm.subs(64, result, result, temp); 226 } else { 227 masm.subs(64, result, temp, result); 228 } 229 230 masm.branchConditionally(ConditionFlag.NE, BREAK_LABEL); 231 masm.subs(64, tailCount, tailCount, CHAR_SIZE_BYTES); 232 masm.branchConditionally(ConditionFlag.EQ, LENGTH_DIFFER_LABEL); 233 } 234 235 // STRING_DIFFER extract exact value of a difference 236 masm.bind(STRING_DIFFER_LABEL); 237 masm.rbit(64, tailCount, result); 238 masm.clz(64, vecCount, tailCount); 239 masm.and(64, vecCount, vecCount, ~((8 * CHAR_SIZE_BYTES) - 1)); // Round to byte or short 240 241 masm.eor(64, result, temp, result); 242 masm.ashr(64, result, result, vecCount); 243 masm.ashr(64, temp, temp, vecCount); 244 245 masm.and(64, result, result, 0xFFFF >>> (16 - (8 * CHAR_SIZE_BYTES))); // 0xFF or 0xFFFF 246 masm.and(64, temp, temp, 0xFFFF >>> (16 - (8 * CHAR_SIZE_BYTES))); 247 248 masm.sub(64, result, temp, result); 249 masm.branchConditionally(ConditionFlag.AL, BREAK_LABEL); 250 // End of STRING_DIFFER 251 252 // Strings are equials up to length, 253 // return length difference in chars 254 masm.bind(LENGTH_DIFFER_LABEL); 255 if (kind1 != kind2 && kind1 == JavaKind.Char) { 256 // Weird swap of substraction order 257 masm.sub(64, result, length2, length1); 258 } else { 259 masm.sub(64, result, length1, length2); 260 } 261 262 // We are done 263 masm.bind(BREAK_LABEL); 264 } 265 266 } // class