1 /*
   2  * Copyright (c) 2013, 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.core.aarch64;
  26 
  27 import static jdk.vm.ci.aarch64.AArch64.sp;
  28 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
  29 import static org.graalvm.compiler.lir.LIRValueUtil.isIntConstant;
  30 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
  31 
  32 import java.util.function.Function;
  33 
  34 import org.graalvm.compiler.asm.aarch64.AArch64Address.AddressingMode;
  35 import org.graalvm.compiler.asm.aarch64.AArch64Assembler.ConditionFlag;
  36 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  37 import org.graalvm.compiler.core.common.LIRKind;
  38 import org.graalvm.compiler.core.common.calc.Condition;
  39 import org.graalvm.compiler.core.common.spi.LIRKindTool;
  40 import org.graalvm.compiler.debug.GraalError;
  41 import org.graalvm.compiler.lir.LIRFrameState;
  42 import org.graalvm.compiler.lir.LIRValueUtil;
  43 import org.graalvm.compiler.lir.LabelRef;
  44 import org.graalvm.compiler.lir.StandardOp;
  45 import org.graalvm.compiler.lir.SwitchStrategy;
  46 import org.graalvm.compiler.lir.Variable;
  47 import org.graalvm.compiler.lir.aarch64.AArch64AddressValue;
  48 import org.graalvm.compiler.lir.aarch64.AArch64ArithmeticOp;
  49 import org.graalvm.compiler.lir.aarch64.AArch64ArrayCompareToOp;
  50 import org.graalvm.compiler.lir.aarch64.AArch64ArrayEqualsOp;
  51 import org.graalvm.compiler.lir.aarch64.AArch64ByteSwapOp;
  52 import org.graalvm.compiler.lir.aarch64.AArch64Compare;
  53 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow;
  54 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.BranchOp;
  55 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.CompareBranchZeroOp;
  56 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.CondMoveOp;
  57 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.CondSetOp;
  58 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.StrategySwitchOp;
  59 import org.graalvm.compiler.lir.aarch64.AArch64ControlFlow.TableSwitchOp;
  60 import org.graalvm.compiler.lir.aarch64.AArch64LIRFlagsVersioned;
  61 import org.graalvm.compiler.lir.aarch64.AArch64Move;
  62 import org.graalvm.compiler.lir.aarch64.AArch64AtomicMove.AtomicReadAndAddOp;
  63 import org.graalvm.compiler.lir.aarch64.AArch64AtomicMove.AtomicReadAndAddLSEOp;
  64 import org.graalvm.compiler.lir.aarch64.AArch64AtomicMove.CompareAndSwapOp;
  65 import org.graalvm.compiler.lir.aarch64.AArch64AtomicMove.AtomicReadAndWriteOp;
  66 import org.graalvm.compiler.lir.aarch64.AArch64Move.MembarOp;
  67 import org.graalvm.compiler.lir.aarch64.AArch64PauseOp;
  68 import org.graalvm.compiler.lir.aarch64.AArch64SpeculativeBarrier;
  69 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  70 import org.graalvm.compiler.lir.gen.LIRGenerator;
  71 import org.graalvm.compiler.phases.util.Providers;
  72 
  73 import jdk.vm.ci.aarch64.AArch64;
  74 import jdk.vm.ci.aarch64.AArch64Kind;
  75 import jdk.vm.ci.code.CallingConvention;
  76 import jdk.vm.ci.code.RegisterValue;
  77 import jdk.vm.ci.meta.AllocatableValue;
  78 import jdk.vm.ci.meta.JavaConstant;
  79 import jdk.vm.ci.meta.JavaKind;
  80 import jdk.vm.ci.meta.PlatformKind;
  81 import jdk.vm.ci.meta.PrimitiveConstant;
  82 import jdk.vm.ci.meta.Value;
  83 import jdk.vm.ci.meta.ValueKind;
  84 
  85 public abstract class AArch64LIRGenerator extends LIRGenerator {
  86 
  87     public AArch64LIRGenerator(LIRKindTool lirKindTool, AArch64ArithmeticLIRGenerator arithmeticLIRGen, MoveFactory moveFactory, Providers providers, LIRGenerationResult lirGenRes) {
  88         super(lirKindTool, arithmeticLIRGen, moveFactory, providers, lirGenRes);
  89     }
  90 
  91     /**
  92      * Checks whether the supplied constant can be used without loading it into a register for store
  93      * operations, i.e., on the right hand side of a memory access.
  94      *
  95      * @param c The constant to check.
  96      * @return True if the constant can be used directly, false if the constant needs to be in a
  97      *         register.
  98      */
  99     protected static final boolean canStoreConstant(JavaConstant c) {
 100         // Our own code never calls this since we can't make a definite statement about whether or
 101         // not we can inline a constant without knowing what kind of operation we execute. Let's be
 102         // optimistic here and fix up mistakes later.
 103         return true;
 104     }
 105 
 106     /**
 107      * If val denotes the stackpointer, move it to another location. This is necessary since most
 108      * ops cannot handle the stackpointer as input or output.
 109      */
 110     public AllocatableValue moveSp(AllocatableValue val) {
 111         if (val instanceof RegisterValue && ((RegisterValue) val).getRegister().equals(sp)) {
 112             assert val.getPlatformKind() == AArch64Kind.QWORD : "Stackpointer must be long";
 113             return emitMove(val);
 114         }
 115         return val;
 116     }
 117 
 118     /**
 119      * AArch64 cannot use anything smaller than a word in any instruction other than load and store.
 120      */
 121     @Override
 122     public <K extends ValueKind<K>> K toRegisterKind(K kind) {
 123         switch ((AArch64Kind) kind.getPlatformKind()) {
 124             case BYTE:
 125             case WORD:
 126                 return kind.changeType(AArch64Kind.DWORD);
 127             default:
 128                 return kind;
 129         }
 130     }
 131 
 132     @Override
 133     public void emitNullCheck(Value address, LIRFrameState state) {
 134         append(new AArch64Move.NullCheckOp(asAddressValue(address), state));
 135     }
 136 
 137     @Override
 138     public Variable emitAddress(AllocatableValue stackslot) {
 139         Variable result = newVariable(LIRKind.value(target().arch.getWordKind()));
 140         append(new AArch64Move.StackLoadAddressOp(result, stackslot));
 141         return result;
 142     }
 143 
 144     public AArch64AddressValue asAddressValue(Value address) {
 145         if (address instanceof AArch64AddressValue) {
 146             return (AArch64AddressValue) address;
 147         } else {
 148             return new AArch64AddressValue(address.getValueKind(), asAllocatable(address), Value.ILLEGAL, 0, 1, AddressingMode.BASE_REGISTER_ONLY);
 149         }
 150     }
 151 
 152     @Override
 153     public Variable emitLogicCompareAndSwap(LIRKind accessKind, Value address, Value expectedValue, Value newValue, Value trueValue, Value falseValue) {
 154         Variable prevValue = newVariable(expectedValue.getValueKind());
 155         Variable scratch = newVariable(LIRKind.value(AArch64Kind.DWORD));
 156         append(new CompareAndSwapOp(prevValue, loadReg(expectedValue), loadReg(newValue), asAllocatable(address), scratch));
 157         assert trueValue.getValueKind().equals(falseValue.getValueKind());
 158         Variable result = newVariable(trueValue.getValueKind());
 159         append(new CondMoveOp(result, ConditionFlag.EQ, asAllocatable(trueValue), asAllocatable(falseValue)));
 160         return result;
 161     }
 162 
 163     @Override
 164     public Variable emitValueCompareAndSwap(LIRKind accessKind, Value address, Value expectedValue, Value newValue) {
 165         Variable result = newVariable(newValue.getValueKind());
 166         Variable scratch = newVariable(LIRKind.value(AArch64Kind.WORD));
 167         append(new CompareAndSwapOp(result, loadReg(expectedValue), loadReg(newValue), asAllocatable(address), scratch));
 168         return result;
 169     }
 170 
 171     @Override
 172     public Value emitAtomicReadAndWrite(Value address, ValueKind<?> kind, Value newValue) {
 173         Variable result = newVariable(kind);
 174         Variable scratch = newVariable(kind);
 175         append(new AtomicReadAndWriteOp((AArch64Kind) kind.getPlatformKind(), asAllocatable(result), asAllocatable(address), asAllocatable(newValue), asAllocatable(scratch)));
 176         return result;
 177     }
 178 
 179     @Override
 180     public Value emitAtomicReadAndAdd(Value address, ValueKind<?> kind, Value delta) {
 181         Variable result = newVariable(kind);
 182         if (AArch64LIRFlagsVersioned.useLSE(target().arch)) {
 183             append(new AtomicReadAndAddLSEOp((AArch64Kind) kind.getPlatformKind(), asAllocatable(result), asAllocatable(address), asAllocatable(delta)));
 184         } else {
 185             append(new AtomicReadAndAddOp((AArch64Kind) kind.getPlatformKind(), asAllocatable(result), asAllocatable(address), delta));
 186         }
 187         return result;
 188     }
 189 
 190     @Override
 191     public void emitMembar(int barriers) {
 192         int necessaryBarriers = target().arch.requiredBarriers(barriers);
 193         if (target().isMP && necessaryBarriers != 0) {
 194             append(new MembarOp(necessaryBarriers));
 195         }
 196     }
 197 
 198     @Override
 199     public void emitJump(LabelRef label) {
 200         assert label != null;
 201         append(new StandardOp.JumpOp(label));
 202     }
 203 
 204     @Override
 205     public void emitOverflowCheckBranch(LabelRef overflow, LabelRef noOverflow, LIRKind cmpKind, double overflowProbability) {
 206         append(new AArch64ControlFlow.BranchOp(ConditionFlag.VS, overflow, noOverflow, overflowProbability));
 207     }
 208 
 209     /**
 210      * Branches to label if (left & right) == 0. If negated is true branchse on non-zero instead.
 211      *
 212      * @param left Integer kind. Non null.
 213      * @param right Integer kind. Non null.
 214      * @param trueDestination destination if left & right == 0. Non null.
 215      * @param falseDestination destination if left & right != 0. Non null
 216      * @param trueSuccessorProbability hoistoric probability that comparison is true
 217      */
 218     @Override
 219     public void emitIntegerTestBranch(Value left, Value right, LabelRef trueDestination, LabelRef falseDestination, double trueSuccessorProbability) {
 220         assert ((AArch64Kind) left.getPlatformKind()).isInteger() && left.getPlatformKind() == right.getPlatformKind();
 221         ((AArch64ArithmeticLIRGenerator) getArithmetic()).emitBinary(LIRKind.combine(left, right), AArch64ArithmeticOp.ANDS, true, left, right);
 222         append(new AArch64ControlFlow.BranchOp(ConditionFlag.EQ, trueDestination, falseDestination, trueSuccessorProbability));
 223     }
 224 
 225     /**
 226      * Conditionally move trueValue into new variable if cond + unorderedIsTrue is true, else
 227      * falseValue.
 228      *
 229      * @param left Arbitrary value. Has to have same type as right. Non null.
 230      * @param right Arbitrary value. Has to have same type as left. Non null.
 231      * @param cond condition that decides whether to move trueValue or falseValue into result. Non
 232      *            null.
 233      * @param unorderedIsTrue defines whether floating-point comparisons consider unordered true or
 234      *            not. Ignored for integer comparisons.
 235      * @param trueValue arbitrary value same type as falseValue. Non null.
 236      * @param falseValue arbitrary value same type as trueValue. Non null.
 237      * @return value containing trueValue if cond + unorderedIsTrue is true, else falseValue. Non
 238      *         null.
 239      */
 240     @Override
 241     public Variable emitConditionalMove(PlatformKind cmpKind, Value left, final Value right, Condition cond, boolean unorderedIsTrue, Value trueValue, Value falseValue) {
 242         AArch64ArithmeticLIRGenerator arithLir = ((AArch64ArithmeticLIRGenerator) arithmeticLIRGen);
 243         Value actualRight = right;
 244         if (isJavaConstant(actualRight) && arithLir.mustReplaceNullWithNullRegister((asJavaConstant(actualRight)))) {
 245             actualRight = arithLir.getNullRegisterValue();
 246         }
 247         boolean mirrored = emitCompare(cmpKind, left, actualRight, cond, unorderedIsTrue);
 248         Condition finalCondition = mirrored ? cond.mirror() : cond;
 249         boolean finalUnorderedIsTrue = mirrored ? !unorderedIsTrue : unorderedIsTrue;
 250         ConditionFlag cmpCondition = toConditionFlag(((AArch64Kind) cmpKind).isInteger(), finalCondition, finalUnorderedIsTrue);
 251         Variable result = newVariable(trueValue.getValueKind());
 252 
 253         if (isIntConstant(trueValue, 1) && isIntConstant(falseValue, 0)) {
 254             append(new CondSetOp(result, cmpCondition));
 255         } else if (isIntConstant(trueValue, 0) && isIntConstant(falseValue, 1)) {
 256             append(new CondSetOp(result, cmpCondition.negate()));
 257         } else {
 258             append(new CondMoveOp(result, cmpCondition, loadReg(trueValue), loadReg(falseValue)));
 259         }
 260         return result;
 261     }
 262 
 263     @Override
 264     public void emitCompareBranch(PlatformKind cmpKind, Value left, final Value right, Condition cond, boolean unorderedIsTrue, LabelRef trueDestination, LabelRef falseDestination,
 265                     double trueDestinationProbability) {
 266         Value actualRight = right;
 267         if (cond == Condition.EQ) {
 268             // emit cbz instruction for IsNullNode.
 269             assert !LIRValueUtil.isNullConstant(left) : "emitNullCheckBranch()'s null input should be in right.";
 270             AArch64ArithmeticLIRGenerator arithLir = ((AArch64ArithmeticLIRGenerator) arithmeticLIRGen);
 271             if (LIRValueUtil.isNullConstant(actualRight)) {
 272                 JavaConstant rightConstant = asJavaConstant(actualRight);
 273                 if (arithLir.mustReplaceNullWithNullRegister(rightConstant)) {
 274                     actualRight = arithLir.getNullRegisterValue();
 275                 } else {
 276                     append(new CompareBranchZeroOp(asAllocatable(left), trueDestination, falseDestination,
 277                                     trueDestinationProbability));
 278                     return;
 279                 }
 280             }
 281 
 282             // emit cbz instruction for IntegerEquals when any of the inputs is zero.
 283             AArch64Kind kind = (AArch64Kind) cmpKind;
 284             if (kind.isInteger()) {
 285                 if (isIntConstant(left, 0)) {
 286                     append(new CompareBranchZeroOp(asAllocatable(actualRight), trueDestination, falseDestination, trueDestinationProbability));
 287                     return;
 288                 } else if (isIntConstant(actualRight, 0)) {
 289                     append(new CompareBranchZeroOp(asAllocatable(left), trueDestination, falseDestination, trueDestinationProbability));
 290                     return;
 291                 }
 292             }
 293         }
 294 
 295         boolean mirrored = emitCompare(cmpKind, left, actualRight, cond, unorderedIsTrue);
 296         Condition finalCondition = mirrored ? cond.mirror() : cond;
 297         boolean finalUnorderedIsTrue = mirrored ? !unorderedIsTrue : unorderedIsTrue;
 298         ConditionFlag cmpCondition = toConditionFlag(((AArch64Kind) cmpKind).isInteger(), finalCondition, finalUnorderedIsTrue);
 299         append(new BranchOp(cmpCondition, trueDestination, falseDestination, trueDestinationProbability));
 300     }
 301 
 302     private static ConditionFlag toConditionFlag(boolean isInt, Condition cond, boolean unorderedIsTrue) {
 303         return isInt ? toIntConditionFlag(cond) : toFloatConditionFlag(cond, unorderedIsTrue);
 304     }
 305 
 306     /**
 307      * Takes a Condition and unorderedIsTrue flag and returns the correct Aarch64 specific
 308      * ConditionFlag. Note: This is only correct if the emitCompare code for floats has correctly
 309      * handled the case of 'EQ && unorderedIsTrue', respectively 'NE && !unorderedIsTrue'!
 310      */
 311     private static ConditionFlag toFloatConditionFlag(Condition cond, boolean unorderedIsTrue) {
 312         switch (cond) {
 313             case LT:
 314                 return unorderedIsTrue ? ConditionFlag.LT : ConditionFlag.LO;
 315             case LE:
 316                 return unorderedIsTrue ? ConditionFlag.LE : ConditionFlag.LS;
 317             case GE:
 318                 return unorderedIsTrue ? ConditionFlag.PL : ConditionFlag.GE;
 319             case GT:
 320                 return unorderedIsTrue ? ConditionFlag.HI : ConditionFlag.GT;
 321             case EQ:
 322                 return ConditionFlag.EQ;
 323             case NE:
 324                 return ConditionFlag.NE;
 325             default:
 326                 throw GraalError.shouldNotReachHere();
 327         }
 328     }
 329 
 330     /**
 331      * Takes a Condition and returns the correct Aarch64 specific ConditionFlag.
 332      */
 333     private static ConditionFlag toIntConditionFlag(Condition cond) {
 334         switch (cond) {
 335             case EQ:
 336                 return ConditionFlag.EQ;
 337             case NE:
 338                 return ConditionFlag.NE;
 339             case LT:
 340                 return ConditionFlag.LT;
 341             case LE:
 342                 return ConditionFlag.LE;
 343             case GT:
 344                 return ConditionFlag.GT;
 345             case GE:
 346                 return ConditionFlag.GE;
 347             case AE:
 348                 return ConditionFlag.HS;
 349             case BE:
 350                 return ConditionFlag.LS;
 351             case AT:
 352                 return ConditionFlag.HI;
 353             case BT:
 354                 return ConditionFlag.LO;
 355             default:
 356                 throw GraalError.shouldNotReachHere();
 357         }
 358     }
 359 
 360     /**
 361      * This method emits the compare instruction, and may reorder the operands. It returns true if
 362      * it did so.
 363      *
 364      * @param a the left operand of the comparison. Has to have same type as b. Non null.
 365      * @param b the right operand of the comparison. Has to have same type as a. Non null.
 366      * @return true if mirrored (i.e. "b cmp a" instead of "a cmp b" was done).
 367      */
 368     protected boolean emitCompare(PlatformKind cmpKind, Value a, Value b, Condition condition, boolean unorderedIsTrue) {
 369         Value left;
 370         Value right;
 371         boolean mirrored;
 372         AArch64Kind kind = (AArch64Kind) cmpKind;
 373         if (kind.isInteger()) {
 374             Value aExt = a;
 375             Value bExt = b;
 376 
 377             int compareBytes = cmpKind.getSizeInBytes();
 378             // AArch64 compares 32 or 64 bits: sign extend a and b as required.
 379             if (compareBytes < a.getPlatformKind().getSizeInBytes()) {
 380                 aExt = arithmeticLIRGen.emitSignExtend(a, compareBytes * 8, 64);
 381             }
 382             if (compareBytes < b.getPlatformKind().getSizeInBytes()) {
 383                 bExt = arithmeticLIRGen.emitSignExtend(b, compareBytes * 8, 64);
 384             }
 385 
 386             if (LIRValueUtil.isVariable(bExt)) {
 387                 left = load(bExt);
 388                 right = loadNonConst(aExt);
 389                 mirrored = true;
 390             } else {
 391                 left = load(aExt);
 392                 right = loadNonConst(bExt);
 393                 mirrored = false;
 394             }
 395             append(new AArch64Compare.CompareOp(left, loadNonCompareConst(right)));
 396         } else if (kind.isSIMD()) {
 397             if (AArch64Compare.FloatCompareOp.isFloatCmpConstant(a, condition, unorderedIsTrue)) {
 398                 left = load(b);
 399                 right = a;
 400                 mirrored = true;
 401             } else if (AArch64Compare.FloatCompareOp.isFloatCmpConstant(b, condition, unorderedIsTrue)) {
 402                 left = load(a);
 403                 right = b;
 404                 mirrored = false;
 405             } else {
 406                 left = load(a);
 407                 right = loadReg(b);
 408                 mirrored = false;
 409             }
 410             append(new AArch64Compare.FloatCompareOp(left, asAllocatable(right), condition, unorderedIsTrue));
 411         } else {
 412             throw GraalError.shouldNotReachHere();
 413         }
 414         return mirrored;
 415     }
 416 
 417     /**
 418      * If value is a constant that cannot be used directly with a gpCompare instruction load it into
 419      * a register and return the register, otherwise return constant value unchanged.
 420      */
 421     protected Value loadNonCompareConst(Value value) {
 422         if (!isCompareConstant(value)) {
 423             return loadReg(value);
 424         }
 425         return value;
 426     }
 427 
 428     /**
 429      * Checks whether value can be used directly with a gpCompare instruction. This is <b>not</b>
 430      * the same as {@link AArch64ArithmeticLIRGenerator#isArithmeticConstant(JavaConstant)}, because
 431      * 0.0 is a valid compare constant for floats, while there are no arithmetic constants for
 432      * floats.
 433      *
 434      * @param value any type. Non null.
 435      * @return true if value can be used directly in comparison instruction, false otherwise.
 436      */
 437     public boolean isCompareConstant(Value value) {
 438         if (isJavaConstant(value)) {
 439             JavaConstant constant = asJavaConstant(value);
 440             if (constant instanceof PrimitiveConstant) {
 441                 final long longValue = constant.asLong();
 442                 long maskedValue;
 443                 switch (constant.getJavaKind()) {
 444                     case Boolean:
 445                     case Byte:
 446                         maskedValue = longValue & 0xFF;
 447                         break;
 448                     case Char:
 449                     case Short:
 450                         maskedValue = longValue & 0xFFFF;
 451                         break;
 452                     case Int:
 453                         maskedValue = longValue & 0xFFFF_FFFF;
 454                         break;
 455                     case Long:
 456                         maskedValue = longValue;
 457                         break;
 458                     default:
 459                         throw GraalError.shouldNotReachHere();
 460                 }
 461                 return AArch64MacroAssembler.isArithmeticImmediate(maskedValue);
 462             } else {
 463                 return constant.isDefaultForKind();
 464             }
 465         }
 466         return false;
 467     }
 468 
 469     /**
 470      * Moves trueValue into result if (left & right) == 0, else falseValue.
 471      *
 472      * @param left Integer kind. Non null.
 473      * @param right Integer kind. Non null.
 474      * @param trueValue Integer kind. Non null.
 475      * @param falseValue Integer kind. Non null.
 476      * @return virtual register containing trueValue if (left & right) == 0, else falseValue.
 477      */
 478     @Override
 479     public Variable emitIntegerTestMove(Value left, Value right, Value trueValue, Value falseValue) {
 480         assert ((AArch64Kind) left.getPlatformKind()).isInteger() && ((AArch64Kind) right.getPlatformKind()).isInteger();
 481         assert ((AArch64Kind) trueValue.getPlatformKind()).isInteger() && ((AArch64Kind) falseValue.getPlatformKind()).isInteger();
 482         ((AArch64ArithmeticLIRGenerator) getArithmetic()).emitBinary(left.getValueKind(), AArch64ArithmeticOp.ANDS, true, left, right);
 483         Variable result = newVariable(trueValue.getValueKind());
 484 
 485         if (isIntConstant(trueValue, 1) && isIntConstant(falseValue, 0)) {
 486             append(new CondSetOp(result, ConditionFlag.EQ));
 487         } else if (isIntConstant(trueValue, 0) && isIntConstant(falseValue, 1)) {
 488             append(new CondSetOp(result, ConditionFlag.NE));
 489         } else {
 490             append(new CondMoveOp(result, ConditionFlag.EQ, load(trueValue), load(falseValue)));
 491         }
 492         return result;
 493     }
 494 
 495     @Override
 496     public void emitStrategySwitch(SwitchStrategy strategy, Variable key, LabelRef[] keyTargets, LabelRef defaultTarget) {
 497         append(createStrategySwitchOp(strategy, keyTargets, defaultTarget, key, newVariable(key.getValueKind()), AArch64LIRGenerator::toIntConditionFlag));
 498     }
 499 
 500     protected StrategySwitchOp createStrategySwitchOp(SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, Variable key, AllocatableValue scratchValue,
 501                     Function<Condition, ConditionFlag> converter) {
 502         return new StrategySwitchOp(strategy, keyTargets, defaultTarget, key, scratchValue, converter);
 503     }
 504 
 505     @Override
 506     protected void emitTableSwitch(int lowKey, LabelRef defaultTarget, LabelRef[] targets, Value key) {
 507         append(new TableSwitchOp(lowKey, defaultTarget, targets, key, newVariable(LIRKind.value(target().arch.getWordKind())), newVariable(key.getValueKind())));
 508     }
 509 
 510     @Override
 511     public Variable emitByteSwap(Value input) {
 512         Variable result = newVariable(LIRKind.combine(input));
 513         append(new AArch64ByteSwapOp(result, input));
 514         return result;
 515     }
 516 
 517     @Override
 518     public Variable emitArrayCompareTo(JavaKind kind1, JavaKind kind2, Value array1, Value array2, Value length1, Value length2) {
 519         LIRKind resultKind = LIRKind.value(AArch64Kind.DWORD);
 520         // DMS TODO: check calling conversion and registers used
 521         RegisterValue res = AArch64.r0.asValue(resultKind);
 522         RegisterValue cnt1 = AArch64.r1.asValue(length1.getValueKind());
 523         RegisterValue cnt2 = AArch64.r2.asValue(length2.getValueKind());
 524         emitMove(cnt1, length1);
 525         emitMove(cnt2, length2);
 526         append(new AArch64ArrayCompareToOp(this, kind1, kind2, res, array1, array2, cnt1, cnt2));
 527         Variable result = newVariable(resultKind);
 528         emitMove(result, res);
 529         return result;
 530     }
 531 
 532     @Override
 533     public Variable emitArrayEquals(JavaKind kind, Value array1, Value array2, Value length, int constantLength, boolean directPointers) {
 534         Variable result = newVariable(LIRKind.value(AArch64Kind.DWORD));
 535         append(new AArch64ArrayEqualsOp(this, kind, result, array1, array2, asAllocatable(length), directPointers));
 536         return result;
 537     }
 538 
 539     @Override
 540     protected JavaConstant zapValueForKind(PlatformKind kind) {
 541         long dead = 0xDEADDEADDEADDEADL;
 542         switch ((AArch64Kind) kind) {
 543             case BYTE:
 544                 return JavaConstant.forByte((byte) dead);
 545             case WORD:
 546                 return JavaConstant.forShort((short) dead);
 547             case DWORD:
 548                 return JavaConstant.forInt((int) dead);
 549             case QWORD:
 550                 return JavaConstant.forLong(dead);
 551             case SINGLE:
 552                 return JavaConstant.forFloat(Float.intBitsToFloat((int) dead));
 553             case DOUBLE:
 554                 return JavaConstant.forDouble(Double.longBitsToDouble(dead));
 555             default:
 556                 throw GraalError.shouldNotReachHere();
 557         }
 558     }
 559 
 560     /**
 561      * Loads value into virtual register. Contrary to {@link #load(Value)} this handles
 562      * RegisterValues (i.e. values corresponding to fixed physical registers) correctly, by not
 563      * creating an unnecessary move into a virtual register.
 564      *
 565      * This avoids generating the following code: mov x0, x19 # x19 is fixed thread register ldr x0,
 566      * [x0] instead of: ldr x0, [x19].
 567      */
 568     protected AllocatableValue loadReg(Value val) {
 569         if (!(val instanceof Variable || val instanceof RegisterValue)) {
 570             return emitMove(val);
 571         }
 572         return (AllocatableValue) val;
 573     }
 574 
 575     @Override
 576     public void emitPause() {
 577         append(new AArch64PauseOp());
 578     }
 579 
 580     public abstract void emitCCall(long address, CallingConvention nativeCallingConvention, Value[] args);
 581 
 582     @Override
 583     public void emitSpeculationFence() {
 584         append(new AArch64SpeculativeBarrier());
 585     }
 586 }