1 /* 2 * Copyright (c) 2009, 2016, 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 26 package org.graalvm.compiler.core.amd64; 27 28 import static jdk.vm.ci.code.ValueUtil.asRegister; 29 import static jdk.vm.ci.code.ValueUtil.isAllocatableValue; 30 import static jdk.vm.ci.code.ValueUtil.isRegister; 31 import static org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic.CMP; 32 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.DWORD; 33 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.PD; 34 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.PS; 35 import static org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize.QWORD; 36 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 37 import static org.graalvm.compiler.lir.LIRValueUtil.asConstant; 38 import static org.graalvm.compiler.lir.LIRValueUtil.asConstantValue; 39 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant; 40 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue; 41 import static org.graalvm.compiler.lir.LIRValueUtil.isIntConstant; 42 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant; 43 44 import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64BinaryArithmetic; 45 import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64MIOp; 46 import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64RMOp; 47 import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag; 48 import org.graalvm.compiler.asm.amd64.AMD64BaseAssembler.OperandSize; 49 import org.graalvm.compiler.asm.amd64.AMD64Assembler.SSEOp; 50 import org.graalvm.compiler.core.common.LIRKind; 51 import org.graalvm.compiler.core.common.NumUtil; 52 import org.graalvm.compiler.core.common.calc.Condition; 53 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage; 54 import org.graalvm.compiler.core.common.spi.LIRKindTool; 55 import org.graalvm.compiler.debug.GraalError; 56 import org.graalvm.compiler.lir.ConstantValue; 57 import org.graalvm.compiler.lir.LIRFrameState; 58 import org.graalvm.compiler.lir.LIRInstruction; 59 import org.graalvm.compiler.lir.LIRValueUtil; 60 import org.graalvm.compiler.lir.LabelRef; 61 import org.graalvm.compiler.lir.StandardOp.JumpOp; 62 import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp; 63 import org.graalvm.compiler.lir.SwitchStrategy; 64 import org.graalvm.compiler.lir.Variable; 65 import org.graalvm.compiler.lir.amd64.AMD64AddressValue; 66 import org.graalvm.compiler.lir.amd64.AMD64ArithmeticLIRGeneratorTool; 67 import org.graalvm.compiler.lir.amd64.AMD64ArrayCompareToOp; 68 import org.graalvm.compiler.lir.amd64.AMD64ArrayEqualsOp; 69 import org.graalvm.compiler.lir.amd64.AMD64ArrayIndexOfOp; 70 import org.graalvm.compiler.lir.amd64.AMD64Binary; 71 import org.graalvm.compiler.lir.amd64.AMD64BinaryConsumer; 72 import org.graalvm.compiler.lir.amd64.AMD64ByteSwapOp; 73 import org.graalvm.compiler.lir.amd64.AMD64Call; 74 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow; 75 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.BranchOp; 76 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.CondMoveOp; 77 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.CondSetOp; 78 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatBranchOp; 79 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondMoveOp; 80 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.FloatCondSetOp; 81 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.ReturnOp; 82 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.StrategySwitchOp; 83 import org.graalvm.compiler.lir.amd64.AMD64ControlFlow.TableSwitchOp; 84 import org.graalvm.compiler.lir.amd64.AMD64LFenceOp; 85 import org.graalvm.compiler.lir.amd64.AMD64Move; 86 import org.graalvm.compiler.lir.amd64.AMD64Move.CompareAndSwapOp; 87 import org.graalvm.compiler.lir.amd64.AMD64Move.MembarOp; 88 import org.graalvm.compiler.lir.amd64.AMD64Move.StackLeaOp; 89 import org.graalvm.compiler.lir.amd64.AMD64PauseOp; 90 import org.graalvm.compiler.lir.amd64.AMD64StringLatin1InflateOp; 91 import org.graalvm.compiler.lir.amd64.AMD64StringUTF16CompressOp; 92 import org.graalvm.compiler.lir.amd64.AMD64ZapRegistersOp; 93 import org.graalvm.compiler.lir.amd64.AMD64ZapStackOp; 94 import org.graalvm.compiler.lir.gen.LIRGenerationResult; 95 import org.graalvm.compiler.lir.gen.LIRGenerator; 96 import org.graalvm.compiler.phases.util.Providers; 97 98 import jdk.vm.ci.amd64.AMD64; 99 import jdk.vm.ci.amd64.AMD64Kind; 100 import jdk.vm.ci.code.CallingConvention; 101 import jdk.vm.ci.code.Register; 102 import jdk.vm.ci.code.RegisterValue; 103 import jdk.vm.ci.code.StackSlot; 104 import jdk.vm.ci.meta.AllocatableValue; 105 import jdk.vm.ci.meta.JavaConstant; 106 import jdk.vm.ci.meta.JavaKind; 107 import jdk.vm.ci.meta.PlatformKind; 108 import jdk.vm.ci.meta.VMConstant; 109 import jdk.vm.ci.meta.Value; 110 import jdk.vm.ci.meta.ValueKind; 111 112 /** 113 * This class implements the AMD64 specific portion of the LIR generator. 114 */ 115 public abstract class AMD64LIRGenerator extends LIRGenerator { 116 117 public AMD64LIRGenerator(LIRKindTool lirKindTool, AMD64ArithmeticLIRGenerator arithmeticLIRGen, MoveFactory moveFactory, Providers providers, LIRGenerationResult lirGenRes) { 118 super(lirKindTool, arithmeticLIRGen, moveFactory, providers, lirGenRes); 119 } 120 121 /** 122 * Checks whether the supplied constant can be used without loading it into a register for store 123 * operations, i.e., on the right hand side of a memory access. 124 * 125 * @param c The constant to check. 126 * @return True if the constant can be used directly, false if the constant needs to be in a 127 * register. 128 */ 129 protected static final boolean canStoreConstant(JavaConstant c) { 130 // there is no immediate move of 64-bit constants on Intel 131 switch (c.getJavaKind()) { 132 case Long: 133 return NumUtil.isInt(c.asLong()); 134 case Double: 135 return false; 136 case Object: 137 return c.isNull(); 138 default: 139 return true; 140 } 141 } 142 143 @Override 144 protected JavaConstant zapValueForKind(PlatformKind kind) { 145 long dead = 0xDEADDEADDEADDEADL; 146 switch ((AMD64Kind) kind) { 147 case BYTE: 148 return JavaConstant.forByte((byte) dead); 149 case WORD: 150 return JavaConstant.forShort((short) dead); 151 case DWORD: 152 return JavaConstant.forInt((int) dead); 153 case QWORD: 154 return JavaConstant.forLong(dead); 155 case SINGLE: 156 return JavaConstant.forFloat(Float.intBitsToFloat((int) dead)); 157 default: 158 // we don't support vector types, so just zap with double for all of them 159 return JavaConstant.forDouble(Double.longBitsToDouble(dead)); 160 } 161 } 162 163 public AMD64AddressValue asAddressValue(Value address) { 164 if (address instanceof AMD64AddressValue) { 165 return (AMD64AddressValue) address; 166 } else { 167 if (address instanceof JavaConstant) { 168 long displacement = ((JavaConstant) address).asLong(); 169 if (NumUtil.isInt(displacement)) { 170 return new AMD64AddressValue(address.getValueKind(), Value.ILLEGAL, (int) displacement); 171 } 172 } 173 return new AMD64AddressValue(address.getValueKind(), asAllocatable(address), 0); 174 } 175 } 176 177 @Override 178 public Variable emitAddress(AllocatableValue stackslot) { 179 Variable result = newVariable(LIRKind.value(target().arch.getWordKind())); 180 append(new StackLeaOp(result, stackslot)); 181 return result; 182 } 183 184 /** 185 * The AMD64 backend only uses DWORD and QWORD values in registers because of a performance 186 * penalty when accessing WORD or BYTE registers. This function converts small integer kinds to 187 * DWORD. 188 */ 189 @Override 190 public <K extends ValueKind<K>> K toRegisterKind(K kind) { 191 switch ((AMD64Kind) kind.getPlatformKind()) { 192 case BYTE: 193 case WORD: 194 return kind.changeType(AMD64Kind.DWORD); 195 default: 196 return kind; 197 } 198 } 199 200 private AllocatableValue asAllocatable(Value value, ValueKind<?> kind) { 201 if (value.getValueKind().equals(kind)) { 202 return asAllocatable(value); 203 } else if (isRegister(value)) { 204 return asRegister(value).asValue(kind); 205 } else if (isConstantValue(value)) { 206 return emitLoadConstant(kind, asConstant(value)); 207 } else { 208 Variable variable = newVariable(kind); 209 emitMove(variable, value); 210 return variable; 211 } 212 } 213 214 private Value emitCompareAndSwap(boolean isLogic, LIRKind accessKind, Value address, Value expectedValue, Value newValue, Value trueValue, Value falseValue) { 215 ValueKind<?> kind = newValue.getValueKind(); 216 assert kind.equals(expectedValue.getValueKind()); 217 218 AMD64AddressValue addressValue = asAddressValue(address); 219 LIRKind integralAccessKind = accessKind; 220 Value reinterpretedExpectedValue = expectedValue; 221 Value reinterpretedNewValue = newValue; 222 boolean isXmm = ((AMD64Kind) accessKind.getPlatformKind()).isXMM(); 223 if (isXmm) { 224 if (accessKind.getPlatformKind().equals(AMD64Kind.SINGLE)) { 225 integralAccessKind = LIRKind.fromJavaKind(target().arch, JavaKind.Int); 226 } else { 227 integralAccessKind = LIRKind.fromJavaKind(target().arch, JavaKind.Long); 228 } 229 reinterpretedExpectedValue = arithmeticLIRGen.emitReinterpret(integralAccessKind, expectedValue); 230 reinterpretedNewValue = arithmeticLIRGen.emitReinterpret(integralAccessKind, newValue); 231 } 232 AMD64Kind memKind = (AMD64Kind) integralAccessKind.getPlatformKind(); 233 RegisterValue aRes = AMD64.rax.asValue(integralAccessKind); 234 AllocatableValue allocatableNewValue = asAllocatable(reinterpretedNewValue, integralAccessKind); 235 emitMove(aRes, reinterpretedExpectedValue); 236 append(new CompareAndSwapOp(memKind, aRes, addressValue, aRes, allocatableNewValue)); 237 238 if (isLogic) { 239 assert trueValue.getValueKind().equals(falseValue.getValueKind()); 240 Variable result = newVariable(trueValue.getValueKind()); 241 append(new CondMoveOp(result, Condition.EQ, asAllocatable(trueValue), falseValue)); 242 return result; 243 } else { 244 if (isXmm) { 245 return arithmeticLIRGen.emitReinterpret(accessKind, aRes); 246 } else { 247 Variable result = newVariable(kind); 248 emitMove(result, aRes); 249 return result; 250 } 251 } 252 } 253 254 @Override 255 public Variable emitLogicCompareAndSwap(LIRKind accessKind, Value address, Value expectedValue, Value newValue, Value trueValue, Value falseValue) { 256 return (Variable) emitCompareAndSwap(true, accessKind, address, expectedValue, newValue, trueValue, falseValue); 257 } 258 259 @Override 260 public Value emitValueCompareAndSwap(LIRKind accessKind, Value address, Value expectedValue, Value newValue) { 261 return emitCompareAndSwap(false, accessKind, address, expectedValue, newValue, null, null); 262 } 263 264 public void emitCompareAndSwapBranch(ValueKind<?> kind, AMD64AddressValue address, Value expectedValue, Value newValue, Condition condition, LabelRef trueLabel, LabelRef falseLabel, 265 double trueLabelProbability) { 266 assert kind.getPlatformKind().getSizeInBytes() <= expectedValue.getValueKind().getPlatformKind().getSizeInBytes(); 267 assert kind.getPlatformKind().getSizeInBytes() <= newValue.getValueKind().getPlatformKind().getSizeInBytes(); 268 assert condition == Condition.EQ || condition == Condition.NE; 269 AMD64Kind memKind = (AMD64Kind) kind.getPlatformKind(); 270 RegisterValue raxValue = AMD64.rax.asValue(kind); 271 emitMove(raxValue, expectedValue); 272 append(new CompareAndSwapOp(memKind, raxValue, address, raxValue, asAllocatable(newValue))); 273 append(new BranchOp(condition, trueLabel, falseLabel, trueLabelProbability)); 274 } 275 276 @Override 277 public Value emitAtomicReadAndAdd(Value address, ValueKind<?> kind, Value delta) { 278 Variable result = newVariable(kind); 279 AMD64AddressValue addressValue = asAddressValue(address); 280 append(new AMD64Move.AtomicReadAndAddOp((AMD64Kind) kind.getPlatformKind(), result, addressValue, asAllocatable(delta))); 281 return result; 282 } 283 284 @Override 285 public Value emitAtomicReadAndWrite(Value address, ValueKind<?> kind, Value newValue) { 286 Variable result = newVariable(kind); 287 AMD64AddressValue addressValue = asAddressValue(address); 288 append(new AMD64Move.AtomicReadAndWriteOp((AMD64Kind) kind.getPlatformKind(), result, addressValue, asAllocatable(newValue))); 289 return result; 290 } 291 292 @Override 293 public void emitNullCheck(Value address, LIRFrameState state) { 294 append(new AMD64Move.NullCheckOp(asAddressValue(address), state)); 295 } 296 297 @Override 298 public void emitJump(LabelRef label) { 299 assert label != null; 300 append(new JumpOp(label)); 301 } 302 303 @Override 304 public void emitCompareBranch(PlatformKind cmpKind, Value left, Value right, Condition cond, boolean unorderedIsTrue, LabelRef trueLabel, LabelRef falseLabel, double trueLabelProbability) { 305 Condition finalCondition = emitCompare(cmpKind, left, right, cond); 306 if (cmpKind == AMD64Kind.SINGLE || cmpKind == AMD64Kind.DOUBLE) { 307 append(new FloatBranchOp(finalCondition, unorderedIsTrue, trueLabel, falseLabel, trueLabelProbability)); 308 } else { 309 append(new BranchOp(finalCondition, trueLabel, falseLabel, trueLabelProbability)); 310 } 311 } 312 313 public void emitCompareBranchMemory(AMD64Kind cmpKind, Value left, AMD64AddressValue right, LIRFrameState state, Condition cond, boolean unorderedIsTrue, LabelRef trueLabel, LabelRef falseLabel, 314 double trueLabelProbability) { 315 boolean mirrored = emitCompareMemory(cmpKind, left, right, state); 316 Condition finalCondition = mirrored ? cond.mirror() : cond; 317 if (cmpKind.isXMM()) { 318 append(new FloatBranchOp(finalCondition, unorderedIsTrue, trueLabel, falseLabel, trueLabelProbability)); 319 } else { 320 append(new BranchOp(finalCondition, trueLabel, falseLabel, trueLabelProbability)); 321 } 322 } 323 324 @Override 325 public void emitOverflowCheckBranch(LabelRef overflow, LabelRef noOverflow, LIRKind cmpLIRKind, double overflowProbability) { 326 append(new BranchOp(ConditionFlag.Overflow, overflow, noOverflow, overflowProbability)); 327 } 328 329 @Override 330 public void emitIntegerTestBranch(Value left, Value right, LabelRef trueDestination, LabelRef falseDestination, double trueDestinationProbability) { 331 emitIntegerTest(left, right); 332 append(new BranchOp(Condition.EQ, trueDestination, falseDestination, trueDestinationProbability)); 333 } 334 335 @Override 336 public Variable emitConditionalMove(PlatformKind cmpKind, Value left, Value right, Condition cond, boolean unorderedIsTrue, Value trueValue, Value falseValue) { 337 boolean isFloatComparison = cmpKind == AMD64Kind.SINGLE || cmpKind == AMD64Kind.DOUBLE; 338 339 Condition finalCondition = cond; 340 Value finalTrueValue = trueValue; 341 Value finalFalseValue = falseValue; 342 if (isFloatComparison) { 343 // eliminate the parity check in case of a float comparison 344 Value finalLeft = left; 345 Value finalRight = right; 346 if (unorderedIsTrue != AMD64ControlFlow.trueOnUnordered(finalCondition)) { 347 if (unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.mirror())) { 348 finalCondition = finalCondition.mirror(); 349 finalLeft = right; 350 finalRight = left; 351 } else if (finalCondition != Condition.EQ && finalCondition != Condition.NE) { 352 // negating EQ and NE does not make any sense as we would need to negate 353 // unorderedIsTrue as well (otherwise, we would no longer fulfill the Java 354 // NaN semantics) 355 assert unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.negate()); 356 finalCondition = finalCondition.negate(); 357 finalTrueValue = falseValue; 358 finalFalseValue = trueValue; 359 } 360 } 361 emitRawCompare(cmpKind, finalLeft, finalRight); 362 } else { 363 finalCondition = emitCompare(cmpKind, left, right, cond); 364 } 365 366 boolean isParityCheckNecessary = isFloatComparison && unorderedIsTrue != AMD64ControlFlow.trueOnUnordered(finalCondition); 367 Variable result = newVariable(finalTrueValue.getValueKind()); 368 if (!isParityCheckNecessary && isIntConstant(finalTrueValue, 1) && isIntConstant(finalFalseValue, 0)) { 369 if (isFloatComparison) { 370 append(new FloatCondSetOp(result, finalCondition)); 371 } else { 372 append(new CondSetOp(result, finalCondition)); 373 } 374 } else if (!isParityCheckNecessary && isIntConstant(finalTrueValue, 0) && isIntConstant(finalFalseValue, 1)) { 375 if (isFloatComparison) { 376 if (unorderedIsTrue == AMD64ControlFlow.trueOnUnordered(finalCondition.negate())) { 377 append(new FloatCondSetOp(result, finalCondition.negate())); 378 } else { 379 append(new FloatCondSetOp(result, finalCondition)); 380 Variable negatedResult = newVariable(result.getValueKind()); 381 append(new AMD64Binary.ConstOp(AMD64BinaryArithmetic.XOR, OperandSize.get(result.getPlatformKind()), negatedResult, result, 1)); 382 result = negatedResult; 383 } 384 } else { 385 append(new CondSetOp(result, finalCondition.negate())); 386 } 387 } else if (isFloatComparison) { 388 append(new FloatCondMoveOp(result, finalCondition, unorderedIsTrue, load(finalTrueValue), load(finalFalseValue))); 389 } else { 390 append(new CondMoveOp(result, finalCondition, load(finalTrueValue), loadNonConst(finalFalseValue))); 391 } 392 return result; 393 } 394 395 @Override 396 public Variable emitIntegerTestMove(Value left, Value right, Value trueValue, Value falseValue) { 397 emitIntegerTest(left, right); 398 Variable result = newVariable(trueValue.getValueKind()); 399 append(new CondMoveOp(result, Condition.EQ, load(trueValue), loadNonConst(falseValue))); 400 return result; 401 } 402 403 private void emitIntegerTest(Value a, Value b) { 404 assert ((AMD64Kind) a.getPlatformKind()).isInteger(); 405 OperandSize size = a.getPlatformKind() == AMD64Kind.QWORD ? QWORD : DWORD; 406 if (isJavaConstant(b) && NumUtil.is32bit(asJavaConstant(b).asLong())) { 407 append(new AMD64BinaryConsumer.ConstOp(AMD64MIOp.TEST, size, asAllocatable(a), (int) asJavaConstant(b).asLong())); 408 } else if (isJavaConstant(a) && NumUtil.is32bit(asJavaConstant(a).asLong())) { 409 append(new AMD64BinaryConsumer.ConstOp(AMD64MIOp.TEST, size, asAllocatable(b), (int) asJavaConstant(a).asLong())); 410 } else if (isAllocatableValue(b)) { 411 append(new AMD64BinaryConsumer.Op(AMD64RMOp.TEST, size, asAllocatable(b), asAllocatable(a))); 412 } else { 413 append(new AMD64BinaryConsumer.Op(AMD64RMOp.TEST, size, asAllocatable(a), asAllocatable(b))); 414 } 415 } 416 417 /** 418 * This method emits the compare against memory instruction, and may reorder the operands. It 419 * returns true if it did so. 420 * 421 * @param b the right operand of the comparison 422 * @return true if the left and right operands were switched, false otherwise 423 */ 424 private boolean emitCompareMemory(AMD64Kind cmpKind, Value a, AMD64AddressValue b, LIRFrameState state) { 425 OperandSize size; 426 switch (cmpKind) { 427 case BYTE: 428 size = OperandSize.BYTE; 429 break; 430 case WORD: 431 size = OperandSize.WORD; 432 break; 433 case DWORD: 434 size = OperandSize.DWORD; 435 break; 436 case QWORD: 437 size = OperandSize.QWORD; 438 break; 439 case SINGLE: 440 append(new AMD64BinaryConsumer.MemoryRMOp(SSEOp.UCOMIS, PS, asAllocatable(a), b, state)); 441 return false; 442 case DOUBLE: 443 append(new AMD64BinaryConsumer.MemoryRMOp(SSEOp.UCOMIS, PD, asAllocatable(a), b, state)); 444 return false; 445 default: 446 throw GraalError.shouldNotReachHere("unexpected kind: " + cmpKind); 447 } 448 449 if (isConstantValue(a)) { 450 return emitCompareMemoryConOp(size, asConstantValue(a), b, state); 451 } else { 452 return emitCompareRegMemoryOp(size, asAllocatable(a), b, state); 453 } 454 } 455 456 protected boolean emitCompareMemoryConOp(OperandSize size, ConstantValue a, AMD64AddressValue b, LIRFrameState state) { 457 if (JavaConstant.isNull(a.getConstant())) { 458 append(new AMD64BinaryConsumer.MemoryConstOp(CMP, size, b, 0, state)); 459 return true; 460 } else if (a.getConstant() instanceof VMConstant && size == DWORD) { 461 VMConstant vc = (VMConstant) a.getConstant(); 462 append(new AMD64BinaryConsumer.MemoryVMConstOp(CMP.getMIOpcode(size, false), b, vc, state)); 463 return true; 464 } else { 465 long value = a.getJavaConstant().asLong(); 466 if (NumUtil.is32bit(value)) { 467 append(new AMD64BinaryConsumer.MemoryConstOp(CMP, size, b, (int) value, state)); 468 return true; 469 } else { 470 return emitCompareRegMemoryOp(size, asAllocatable(a), b, state); 471 } 472 } 473 } 474 475 private boolean emitCompareRegMemoryOp(OperandSize size, AllocatableValue a, AMD64AddressValue b, LIRFrameState state) { 476 AMD64RMOp op = CMP.getRMOpcode(size); 477 append(new AMD64BinaryConsumer.MemoryRMOp(op, size, a, b, state)); 478 return false; 479 } 480 481 /** 482 * This method emits the compare instruction, and may reorder the operands. It returns true if 483 * it did so. 484 * 485 * @param a the left operand of the comparison 486 * @param b the right operand of the comparison 487 * @param cond the condition of the comparison 488 * @return true if the left and right operands were switched, false otherwise 489 */ 490 private Condition emitCompare(PlatformKind cmpKind, Value a, Value b, Condition cond) { 491 if (LIRValueUtil.isVariable(b)) { 492 emitRawCompare(cmpKind, b, a); 493 return cond.mirror(); 494 } else { 495 emitRawCompare(cmpKind, a, b); 496 return cond; 497 } 498 } 499 500 private void emitRawCompare(PlatformKind cmpKind, Value left, Value right) { 501 ((AMD64ArithmeticLIRGeneratorTool) arithmeticLIRGen).emitCompareOp((AMD64Kind) cmpKind, load(left), loadNonConst(right)); 502 } 503 504 @Override 505 public void emitMembar(int barriers) { 506 int necessaryBarriers = target().arch.requiredBarriers(barriers); 507 if (target().isMP && necessaryBarriers != 0) { 508 append(new MembarOp(necessaryBarriers)); 509 } 510 } 511 512 public abstract void emitCCall(long address, CallingConvention nativeCallingConvention, Value[] args, int numberOfFloatingPointArguments); 513 514 @Override 515 protected void emitForeignCallOp(ForeignCallLinkage linkage, Value result, Value[] arguments, Value[] temps, LIRFrameState info) { 516 long maxOffset = linkage.getMaxCallTargetOffset(); 517 if (maxOffset != (int) maxOffset && !GeneratePIC.getValue(getResult().getLIR().getOptions())) { 518 append(new AMD64Call.DirectFarForeignCallOp(linkage, result, arguments, temps, info)); 519 } else { 520 append(new AMD64Call.DirectNearForeignCallOp(linkage, result, arguments, temps, info)); 521 } 522 } 523 524 @Override 525 public Variable emitByteSwap(Value input) { 526 Variable result = newVariable(LIRKind.combine(input)); 527 append(new AMD64ByteSwapOp(result, input)); 528 return result; 529 } 530 531 @Override 532 public Variable emitArrayCompareTo(JavaKind kind1, JavaKind kind2, Value array1, Value array2, Value length1, Value length2) { 533 LIRKind resultKind = LIRKind.value(AMD64Kind.DWORD); 534 RegisterValue raxRes = AMD64.rax.asValue(resultKind); 535 RegisterValue cnt1 = AMD64.rcx.asValue(length1.getValueKind()); 536 RegisterValue cnt2 = AMD64.rdx.asValue(length2.getValueKind()); 537 emitMove(cnt1, length1); 538 emitMove(cnt2, length2); 539 append(new AMD64ArrayCompareToOp(this, kind1, kind2, raxRes, array1, array2, cnt1, cnt2)); 540 Variable result = newVariable(resultKind); 541 emitMove(result, raxRes); 542 return result; 543 } 544 545 @Override 546 public Variable emitArrayEquals(JavaKind kind, Value array1, Value array2, Value length, int constantLength, boolean directPointers) { 547 Variable result = newVariable(LIRKind.value(AMD64Kind.DWORD)); 548 append(new AMD64ArrayEqualsOp(this, kind, result, array1, array2, asAllocatable(length), constantLength, directPointers, getMaxVectorSize())); 549 return result; 550 } 551 552 /** 553 * Return a conservative estimate of the page size for use by the String.indexOf intrinsic. 554 */ 555 protected int getVMPageSize() { 556 return 4096; 557 } 558 559 /** 560 * Return the maximum size of vector registers used in SSE/AVX instructions. 561 */ 562 protected int getMaxVectorSize() { 563 // default for "unlimited" 564 return -1; 565 } 566 567 @Override 568 public Variable emitArrayIndexOf(JavaKind kind, boolean findTwoConsecutive, Value arrayPointer, Value arrayLength, Value... searchValues) { 569 Variable result = newVariable(LIRKind.value(AMD64Kind.QWORD)); 570 Value[] allocatableSearchValues = new Value[searchValues.length]; 571 for (int i = 0; i < searchValues.length; i++) { 572 allocatableSearchValues[i] = asAllocatable(searchValues[i]); 573 } 574 append(new AMD64ArrayIndexOfOp(kind, findTwoConsecutive, getVMPageSize(), getMaxVectorSize(), this, result, asAllocatable(arrayPointer), asAllocatable(arrayLength), allocatableSearchValues)); 575 return result; 576 } 577 578 @Override 579 public void emitStringLatin1Inflate(Value src, Value dst, Value len) { 580 RegisterValue rsrc = AMD64.rsi.asValue(src.getValueKind()); 581 RegisterValue rdst = AMD64.rdi.asValue(dst.getValueKind()); 582 RegisterValue rlen = AMD64.rdx.asValue(len.getValueKind()); 583 584 emitMove(rsrc, src); 585 emitMove(rdst, dst); 586 emitMove(rlen, len); 587 588 append(new AMD64StringLatin1InflateOp(this, rsrc, rdst, rlen)); 589 } 590 591 @Override 592 public Variable emitStringUTF16Compress(Value src, Value dst, Value len) { 593 RegisterValue rsrc = AMD64.rsi.asValue(src.getValueKind()); 594 RegisterValue rdst = AMD64.rdi.asValue(dst.getValueKind()); 595 RegisterValue rlen = AMD64.rdx.asValue(len.getValueKind()); 596 597 emitMove(rsrc, src); 598 emitMove(rdst, dst); 599 emitMove(rlen, len); 600 601 LIRKind reskind = LIRKind.value(AMD64Kind.DWORD); 602 RegisterValue rres = AMD64.rax.asValue(reskind); 603 604 append(new AMD64StringUTF16CompressOp(this, rres, rsrc, rdst, rlen)); 605 606 Variable res = newVariable(reskind); 607 emitMove(res, rres); 608 return res; 609 } 610 611 @Override 612 public void emitReturn(JavaKind kind, Value input) { 613 AllocatableValue operand = Value.ILLEGAL; 614 if (input != null) { 615 operand = resultOperandFor(kind, input.getValueKind()); 616 emitMove(operand, input); 617 } 618 append(new ReturnOp(operand)); 619 } 620 621 protected StrategySwitchOp createStrategySwitchOp(SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, Variable key, AllocatableValue temp) { 622 return new StrategySwitchOp(strategy, keyTargets, defaultTarget, key, temp); 623 } 624 625 @Override 626 public void emitStrategySwitch(SwitchStrategy strategy, Variable key, LabelRef[] keyTargets, LabelRef defaultTarget) { 627 // a temp is needed for loading object constants 628 boolean needsTemp = !LIRKind.isValue(key); 629 append(createStrategySwitchOp(strategy, keyTargets, defaultTarget, key, needsTemp ? newVariable(key.getValueKind()) : Value.ILLEGAL)); 630 } 631 632 @Override 633 protected void emitTableSwitch(int lowKey, LabelRef defaultTarget, LabelRef[] targets, Value key) { 634 append(new TableSwitchOp(lowKey, defaultTarget, targets, key, newVariable(LIRKind.value(target().arch.getWordKind())), newVariable(key.getValueKind()))); 635 } 636 637 @Override 638 public void emitPause() { 639 append(new AMD64PauseOp()); 640 } 641 642 @Override 643 public SaveRegistersOp createZapRegisters(Register[] zappedRegisters, JavaConstant[] zapValues) { 644 return new AMD64ZapRegistersOp(zappedRegisters, zapValues); 645 } 646 647 @Override 648 public LIRInstruction createZapArgumentSpace(StackSlot[] zappedStack, JavaConstant[] zapValues) { 649 return new AMD64ZapStackOp(zappedStack, zapValues); 650 } 651 652 @Override 653 public void emitSpeculationFence() { 654 append(new AMD64LFenceOp()); 655 } 656 }