1 /*
   2  * Copyright (c) 2013, 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 package org.graalvm.compiler.lir.sparc;
  24 
  25 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BPCC;
  26 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CBCOND;
  27 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.FBPCC;
  28 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.INSTRUCTION_SIZE;
  29 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm10;
  30 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm11;
  31 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm13;
  32 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm5;
  33 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.ANNUL;
  34 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.NOT_ANNUL;
  35 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_NOT_TAKEN;
  36 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_TAKEN;
  37 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Fcc0;
  38 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Icc;
  39 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.CC.Xcc;
  40 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Always;
  41 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Equal;
  42 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Equal;
  43 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Greater;
  44 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_GreaterOrEqual;
  45 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_Less;
  46 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_LessOrEqual;
  47 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedGreaterOrEqual;
  48 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrEqual;
  49 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrGreater;
  50 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrLess;
  51 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.F_UnorderedOrLessOrEqual;
  52 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Greater;
  53 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterEqual;
  54 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterEqualUnsigned;
  55 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.GreaterUnsigned;
  56 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.Less;
  57 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessEqual;
  58 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessEqualUnsigned;
  59 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.LessUnsigned;
  60 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag.NotEqual;
  61 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Op3s.Subcc;
  62 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
  63 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
  64 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
  65 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  66 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
  67 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
  68 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
  69 import static org.graalvm.compiler.lir.sparc.SPARCMove.const2reg;
  70 import static org.graalvm.compiler.lir.sparc.SPARCOP3Op.emitOp3;
  71 import static jdk.vm.ci.code.ValueUtil.asRegister;
  72 import static jdk.vm.ci.sparc.SPARC.CPU;
  73 import static jdk.vm.ci.sparc.SPARC.g0;
  74 import static jdk.vm.ci.sparc.SPARCKind.WORD;
  75 import static jdk.vm.ci.sparc.SPARCKind.XWORD;
  76 
  77 import java.util.ArrayList;
  78 import java.util.EnumSet;
  79 import java.util.HashMap;
  80 import java.util.List;
  81 import java.util.Map;
  82 
  83 import org.graalvm.compiler.asm.Assembler;
  84 import org.graalvm.compiler.asm.Assembler.LabelHint;
  85 import org.graalvm.compiler.asm.Label;
  86 import org.graalvm.compiler.asm.NumUtil;
  87 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  88 import org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict;
  89 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CC;
  90 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CMOV;
  91 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag;
  92 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  93 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
  94 import org.graalvm.compiler.core.common.calc.Condition;
  95 import org.graalvm.compiler.debug.GraalError;
  96 import org.graalvm.compiler.lir.LIRInstructionClass;
  97 import org.graalvm.compiler.lir.LabelRef;
  98 import org.graalvm.compiler.lir.Opcode;
  99 import org.graalvm.compiler.lir.StandardOp;
 100 import org.graalvm.compiler.lir.SwitchStrategy;
 101 import org.graalvm.compiler.lir.SwitchStrategy.BaseSwitchClosure;
 102 import org.graalvm.compiler.lir.Variable;
 103 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
 104 
 105 import jdk.vm.ci.code.Register;
 106 import jdk.vm.ci.meta.AllocatableValue;
 107 import jdk.vm.ci.meta.Constant;
 108 import jdk.vm.ci.meta.JavaConstant;
 109 import jdk.vm.ci.meta.PlatformKind;
 110 import jdk.vm.ci.meta.Value;
 111 import jdk.vm.ci.sparc.SPARC.CPUFeature;
 112 import jdk.vm.ci.sparc.SPARCKind;
 113 
 114 public class SPARCControlFlow {
 115     // This describes the maximum offset between the first emitted (load constant in to scratch,
 116     // if does not fit into simm5 of cbcond) instruction and the final branch instruction
 117     private static final int maximumSelfOffsetInstructions = 2;
 118 
 119     public static final class ReturnOp extends SPARCBlockEndOp {
 120         public static final LIRInstructionClass<ReturnOp> TYPE = LIRInstructionClass.create(ReturnOp.class);
 121         public static final SizeEstimate SIZE = SizeEstimate.create(2);
 122 
 123         @Use({REG, ILLEGAL}) protected Value x;
 124 
 125         public ReturnOp(Value x) {
 126             super(TYPE, SIZE);
 127             this.x = x;
 128         }
 129 
 130         @Override
 131         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 132             emitCodeHelper(crb, masm);
 133         }
 134 
 135         public static void emitCodeHelper(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 136             masm.ret();
 137             // On SPARC we always leave the frame (in the delay slot).
 138             crb.frameContext.leave(crb);
 139         }
 140     }
 141 
 142     public static final class CompareBranchOp extends SPARCBlockEndOp implements SPARCDelayedControlTransfer {
 143         public static final LIRInstructionClass<CompareBranchOp> TYPE = LIRInstructionClass.create(CompareBranchOp.class);
 144         public static final SizeEstimate SIZE = SizeEstimate.create(3);
 145         static final EnumSet<SPARCKind> SUPPORTED_KINDS = EnumSet.of(XWORD, WORD);
 146 
 147         @Use({REG}) protected Value x;
 148         @Use({REG, CONST}) protected Value y;
 149         private ConditionFlag conditionFlag;
 150         protected final LabelRef trueDestination;
 151         protected LabelHint trueDestinationHint;
 152         protected final LabelRef falseDestination;
 153         protected LabelHint falseDestinationHint;
 154         protected final SPARCKind kind;
 155         protected final boolean unorderedIsTrue;
 156         private boolean emitted = false;
 157         private int delaySlotPosition = -1;
 158         private double trueDestinationProbability;
 159 
 160         public CompareBranchOp(Value x, Value y, Condition condition, LabelRef trueDestination, LabelRef falseDestination, SPARCKind kind, boolean unorderedIsTrue, double trueDestinationProbability) {
 161             super(TYPE, SIZE);
 162             this.x = x;
 163             this.y = y;
 164             this.trueDestination = trueDestination;
 165             this.falseDestination = falseDestination;
 166             this.kind = kind;
 167             this.unorderedIsTrue = unorderedIsTrue;
 168             this.trueDestinationProbability = trueDestinationProbability;
 169             conditionFlag = fromCondition(kind.isInteger(), condition, unorderedIsTrue);
 170         }
 171 
 172         @Override
 173         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 174             if (emitted) { // Only if delayed control transfer is used we must check this
 175                 assert masm.position() - delaySlotPosition == 4 : "Only one instruction can be stuffed into the delay slot";
 176             }
 177             if (!emitted) {
 178                 requestHints(masm);
 179                 int targetPosition = getTargetPosition(masm);
 180                 if (canUseShortBranch(crb, masm, targetPosition)) {
 181                     emitted = emitShortCompareBranch(crb, masm);
 182                 }
 183                 if (!emitted) { // No short compare/branch was used, so we go into fallback
 184                     emitted = emitLongCompareBranch(crb, masm, true);
 185                     emitted = true;
 186                 }
 187             }
 188             assert emitted;
 189         }
 190 
 191         private boolean emitLongCompareBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm, boolean withDelayedNop) {
 192             emitOp3(masm, Subcc, x, y);
 193             return emitBranch(crb, masm, kind, conditionFlag, trueDestination, falseDestination, withDelayedNop, trueDestinationProbability);
 194         }
 195 
 196         private static int getTargetPosition(Assembler asm) {
 197             return asm.position() + maximumSelfOffsetInstructions * asm.target.wordSize;
 198         }
 199 
 200         @Override
 201         public void emitControlTransfer(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 202             requestHints(masm);
 203             // When we use short branches, no delay slot is available
 204             int targetPosition = getTargetPosition(masm);
 205             if (!canUseShortBranch(crb, masm, targetPosition)) {
 206                 emitted = emitLongCompareBranch(crb, masm, false);
 207                 if (emitted) {
 208                     delaySlotPosition = masm.position();
 209                 }
 210             }
 211         }
 212 
 213         private void requestHints(SPARCMacroAssembler masm) {
 214             if (trueDestinationHint == null) {
 215                 this.trueDestinationHint = masm.requestLabelHint(trueDestination.label());
 216             }
 217             if (falseDestinationHint == null) {
 218                 this.falseDestinationHint = masm.requestLabelHint(falseDestination.label());
 219             }
 220         }
 221 
 222         /**
 223          * Tries to use the emit the compare/branch instruction.
 224          * <p>
 225          * CBcond has follwing limitations
 226          * <ul>
 227          * <li>Immediate field is only 5 bit and is on the right
 228          * <li>Jump offset is maximum of -+512 instruction
 229          *
 230          * <p>
 231          * We get from outside
 232          * <ul>
 233          * <li>at least one of trueDestination falseDestination is within reach of +-512
 234          * instructions
 235          * <li>two registers OR one register and a constant which fits simm13
 236          *
 237          * <p>
 238          * We do:
 239          * <ul>
 240          * <li>find out which target needs to be branched conditionally
 241          * <li>find out if fall-through is possible, if not, a unconditional branch is needed after
 242          * cbcond (needJump=true)
 243          * <li>if no fall through: we need to put the closer jump into the cbcond branch and the
 244          * farther into the jmp (unconditional branch)
 245          * <li>if constant on the left side, mirror to be on the right
 246          * <li>if constant on right does not fit into simm5, put it into a scratch register
 247          *
 248          * @param crb
 249          * @param masm
 250          * @return true if the branch could be emitted
 251          */
 252         private boolean emitShortCompareBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 253             ConditionFlag actualConditionFlag = conditionFlag;
 254             Label actualTrueTarget = trueDestination.label();
 255             Label actualFalseTarget = falseDestination.label();
 256             Label tmpTarget;
 257             boolean needJump;
 258             if (crb.isSuccessorEdge(trueDestination)) {
 259                 actualConditionFlag = conditionFlag.negate();
 260                 tmpTarget = actualTrueTarget;
 261                 actualTrueTarget = actualFalseTarget;
 262                 actualFalseTarget = tmpTarget;
 263                 needJump = false;
 264             } else {
 265                 needJump = !crb.isSuccessorEdge(falseDestination);
 266                 int targetPosition = getTargetPosition(masm);
 267                 if (needJump && !isShortBranch(masm, targetPosition, trueDestinationHint, actualTrueTarget)) {
 268                     // we have to jump in either way, so we must put the shorter
 269                     // branch into the actualTarget as only one of the two jump targets
 270                     // is guaranteed to be simm10
 271                     actualConditionFlag = actualConditionFlag.negate();
 272                     tmpTarget = actualTrueTarget;
 273                     actualTrueTarget = actualFalseTarget;
 274                     actualFalseTarget = tmpTarget;
 275                 }
 276             }
 277             emitCBCond(masm, x, y, actualTrueTarget, actualConditionFlag);
 278             if (needJump) {
 279                 masm.jmp(actualFalseTarget);
 280                 masm.nop();
 281             }
 282             return true;
 283         }
 284 
 285         private void emitCBCond(SPARCMacroAssembler masm, Value actualX, Value actualY, Label actualTrueTarget, ConditionFlag cFlag) {
 286             PlatformKind xKind = actualX.getPlatformKind();
 287             boolean isLong = kind == SPARCKind.XWORD;
 288             if (isJavaConstant(actualY)) {
 289                 JavaConstant c = asJavaConstant(actualY);
 290                 long constantY = c.isNull() ? 0 : c.asLong();
 291                 assert NumUtil.isInt(constantY);
 292                 CBCOND.emit(masm, cFlag, isLong, asRegister(actualX, xKind), (int) constantY, actualTrueTarget);
 293             } else {
 294                 CBCOND.emit(masm, cFlag, isLong, asRegister(actualX, xKind), asRegister(actualY, xKind), actualTrueTarget);
 295             }
 296         }
 297 
 298         private boolean canUseShortBranch(CompilationResultBuilder crb, SPARCAssembler asm, int position) {
 299             if (!asm.hasFeature(CPUFeature.CBCOND)) {
 300                 return false;
 301             }
 302             if (!((SPARCKind) x.getPlatformKind()).isInteger()) {
 303                 return false;
 304             }
 305             // Do not use short branch, if the y value is a constant and does not fit into simm5 but
 306             // fits into simm13; this means the code with CBcond would be longer as the code without
 307             // CBcond.
 308             if (isJavaConstant(y) && !isSimm5(asJavaConstant(y)) && isSimm13(asJavaConstant(y))) {
 309                 return false;
 310             }
 311             boolean hasShortJumpTarget = false;
 312             if (!crb.isSuccessorEdge(trueDestination)) {
 313                 hasShortJumpTarget |= isShortBranch(asm, position, trueDestinationHint, trueDestination.label());
 314             }
 315             if (!crb.isSuccessorEdge(falseDestination)) {
 316                 hasShortJumpTarget |= isShortBranch(asm, position, falseDestinationHint, falseDestination.label());
 317             }
 318             return hasShortJumpTarget;
 319         }
 320 
 321         @Override
 322         public void resetState() {
 323             emitted = false;
 324             delaySlotPosition = -1;
 325         }
 326 
 327         @Override
 328         public void verify() {
 329             super.verify();
 330             assert SUPPORTED_KINDS.contains(kind) : kind;
 331             assert !isConstantValue(x);
 332             assert x.getPlatformKind().equals(kind) && (isConstantValue(y) || y.getPlatformKind().equals(kind)) : x + " " + y;
 333         }
 334     }
 335 
 336     public static boolean isShortBranch(SPARCAssembler asm, int position, LabelHint hint, Label label) {
 337         int disp = 0;
 338         boolean dispValid = true;
 339         if (label.isBound()) {
 340             disp = label.position() - position;
 341         } else if (hint != null && hint.isValid()) {
 342             disp = hint.getTarget() - hint.getPosition();
 343         } else {
 344             dispValid = false;
 345         }
 346         if (dispValid) {
 347             if (disp < 0) {
 348                 disp -= maximumSelfOffsetInstructions * asm.target.wordSize;
 349             } else {
 350                 disp += maximumSelfOffsetInstructions * asm.target.wordSize;
 351             }
 352             return isSimm10(disp >> 2);
 353         } else if (hint == null) {
 354             asm.requestLabelHint(label);
 355         }
 356         return false;
 357     }
 358 
 359     public static final class BranchOp extends SPARCBlockEndOp implements StandardOp.BranchOp {
 360         public static final LIRInstructionClass<BranchOp> TYPE = LIRInstructionClass.create(BranchOp.class);
 361         public static final SizeEstimate SIZE = SizeEstimate.create(2);
 362         protected final ConditionFlag conditionFlag;
 363         protected final LabelRef trueDestination;
 364         protected final LabelRef falseDestination;
 365         protected final SPARCKind kind;
 366         protected final double trueDestinationProbability;
 367 
 368         public BranchOp(ConditionFlag conditionFlag, LabelRef trueDestination, LabelRef falseDestination, SPARCKind kind, double trueDestinationProbability) {
 369             super(TYPE, SIZE);
 370             this.trueDestination = trueDestination;
 371             this.falseDestination = falseDestination;
 372             this.kind = kind;
 373             this.conditionFlag = conditionFlag;
 374             this.trueDestinationProbability = trueDestinationProbability;
 375         }
 376 
 377         @Override
 378         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 379             emitBranch(crb, masm, kind, conditionFlag, trueDestination, falseDestination, true, trueDestinationProbability);
 380         }
 381     }
 382 
 383     private static boolean emitBranch(CompilationResultBuilder crb, SPARCMacroAssembler masm, SPARCKind kind, ConditionFlag conditionFlag, LabelRef trueDestination, LabelRef falseDestination,
 384                     boolean withDelayedNop, double trueDestinationProbability) {
 385         Label actualTarget;
 386         ConditionFlag actualConditionFlag;
 387         boolean needJump;
 388         BranchPredict predictTaken;
 389         if (falseDestination != null && crb.isSuccessorEdge(trueDestination)) {
 390             actualConditionFlag = conditionFlag != null ? conditionFlag.negate() : null;
 391             actualTarget = falseDestination.label();
 392             needJump = false;
 393             predictTaken = trueDestinationProbability < .5d ? PREDICT_TAKEN : PREDICT_NOT_TAKEN;
 394         } else {
 395             actualConditionFlag = conditionFlag;
 396             actualTarget = trueDestination.label();
 397             needJump = falseDestination != null && !crb.isSuccessorEdge(falseDestination);
 398             predictTaken = trueDestinationProbability > .5d ? PREDICT_TAKEN : PREDICT_NOT_TAKEN;
 399         }
 400         if (!withDelayedNop && needJump) {
 401             // We cannot make use of the delay slot when we jump in true-case and false-case
 402             return false;
 403         }
 404         if (kind.isFloat()) {
 405             FBPCC.emit(masm, Fcc0, actualConditionFlag, NOT_ANNUL, predictTaken, actualTarget);
 406         } else {
 407             assert kind.isInteger();
 408             CC cc = kind.equals(WORD) ? Icc : Xcc;
 409             BPCC.emit(masm, cc, actualConditionFlag, NOT_ANNUL, predictTaken, actualTarget);
 410         }
 411         if (withDelayedNop) {
 412             masm.nop();  // delay slot
 413         }
 414         if (needJump) {
 415             masm.jmp(falseDestination.label());
 416         }
 417         return true;
 418     }
 419 
 420     public static class StrategySwitchOp extends SPARCBlockEndOp {
 421         public static final LIRInstructionClass<StrategySwitchOp> TYPE = LIRInstructionClass.create(StrategySwitchOp.class);
 422         protected Constant[] keyConstants;
 423         private final LabelRef[] keyTargets;
 424         private LabelRef defaultTarget;
 425         @Alive({REG}) protected Value key;
 426         @Alive({REG, ILLEGAL}) protected Value constantTableBase;
 427         @Temp({REG}) protected Value scratch;
 428         protected final SwitchStrategy strategy;
 429         private final Map<Label, LabelHint> labelHints;
 430         private final List<Label> conditionalLabels = new ArrayList<>();
 431 
 432         public StrategySwitchOp(Value constantTableBase, SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, Value key, Value scratch) {
 433             this(TYPE, constantTableBase, strategy, keyTargets, defaultTarget, key, scratch);
 434         }
 435 
 436         protected StrategySwitchOp(LIRInstructionClass<? extends StrategySwitchOp> c, Value constantTableBase, SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, Value key,
 437                         Value scratch) {
 438             super(c);
 439             this.strategy = strategy;
 440             this.keyConstants = strategy.getKeyConstants();
 441             this.keyTargets = keyTargets;
 442             this.defaultTarget = defaultTarget;
 443             this.constantTableBase = constantTableBase;
 444             this.key = key;
 445             this.scratch = scratch;
 446             this.labelHints = new HashMap<>();
 447             assert keyConstants.length == keyTargets.length;
 448             assert keyConstants.length == strategy.keyProbabilities.length;
 449         }
 450 
 451         @Override
 452         public void emitCode(final CompilationResultBuilder crb, final SPARCMacroAssembler masm) {
 453             final Register keyRegister = asRegister(key);
 454             final Register constantBaseRegister = AllocatableValue.ILLEGAL.equals(constantTableBase) ? g0 : asRegister(constantTableBase);
 455             strategy.run(new SwitchClosure(keyRegister, constantBaseRegister, crb, masm));
 456         }
 457 
 458         public class SwitchClosure extends BaseSwitchClosure {
 459             private int conditionalLabelPointer = 0;
 460 
 461             protected final Register keyRegister;
 462             protected final Register constantBaseRegister;
 463             protected final CompilationResultBuilder crb;
 464             protected final SPARCMacroAssembler masm;
 465 
 466             protected SwitchClosure(Register keyRegister, Register constantBaseRegister, CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 467                 super(crb, masm, keyTargets, defaultTarget);
 468                 this.keyRegister = keyRegister;
 469                 this.constantBaseRegister = constantBaseRegister;
 470                 this.crb = crb;
 471                 this.masm = masm;
 472             }
 473 
 474             /**
 475              * This method caches the generated labels over two assembly passes to get information
 476              * about branch lengths.
 477              */
 478             @Override
 479             public Label conditionalJump(int index, Condition condition) {
 480                 Label label;
 481                 if (conditionalLabelPointer <= conditionalLabels.size()) {
 482                     label = new Label();
 483                     conditionalLabels.add(label);
 484                     conditionalLabelPointer = conditionalLabels.size();
 485                 } else {
 486                     // TODO: (sa) We rely here on the order how the labels are generated during
 487                     // code generation; if the order is not stable ower two assembly passes, the
 488                     // result can be wrong
 489                     label = conditionalLabels.get(conditionalLabelPointer++);
 490                 }
 491                 conditionalJump(index, condition, label);
 492                 return label;
 493             }
 494 
 495             @Override
 496             protected void conditionalJump(int index, Condition condition, Label target) {
 497                 JavaConstant constant = (JavaConstant) keyConstants[index];
 498                 CC conditionCode;
 499                 Long bits = constant.asLong();
 500                 switch (constant.getJavaKind()) {
 501                     case Char:
 502                     case Byte:
 503                     case Short:
 504                     case Int:
 505                         conditionCode = CC.Icc;
 506                         break;
 507                     case Long:
 508                         conditionCode = CC.Xcc;
 509                         break;
 510                     default:
 511                         throw new GraalError("switch only supported for int, long and object");
 512                 }
 513                 ConditionFlag conditionFlag = fromCondition(keyRegister.getRegisterCategory().equals(CPU), condition, false);
 514                 LabelHint hint = requestHint(masm, target);
 515                 boolean isShortConstant = isSimm5(constant);
 516                 int cbCondPosition = masm.position();
 517                 if (!isShortConstant) { // Load constant takes one instruction
 518                     cbCondPosition += INSTRUCTION_SIZE;
 519                 }
 520                 boolean canUseShortBranch = masm.hasFeature(CPUFeature.CBCOND) && isShortBranch(masm, cbCondPosition, hint, target);
 521                 if (bits != null && canUseShortBranch) {
 522                     if (isShortConstant) {
 523                         CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, (int) (long) bits, target);
 524                     } else {
 525                         Register scratchRegister = asRegister(scratch);
 526                         const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY);
 527                         CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, scratchRegister, target);
 528                     }
 529                 } else {
 530                     if (bits != null && isSimm13(constant)) {
 531                         masm.cmp(keyRegister, (int) (long) bits); // Cast is safe
 532                     } else {
 533                         Register scratchRegister = asRegister(scratch);
 534                         const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY);
 535                         masm.cmp(keyRegister, scratchRegister);
 536                     }
 537                     BPCC.emit(masm, conditionCode, conditionFlag, ANNUL, PREDICT_TAKEN, target);
 538                     masm.nop();  // delay slot
 539                 }
 540             }
 541         }
 542 
 543         protected LabelHint requestHint(SPARCMacroAssembler masm, Label label) {
 544             LabelHint hint = labelHints.get(label);
 545             if (hint == null) {
 546                 hint = masm.requestLabelHint(label);
 547                 labelHints.put(label, hint);
 548             }
 549             return hint;
 550         }
 551 
 552         protected int estimateEmbeddedSize(Constant c) {
 553             JavaConstant v = (JavaConstant) c;
 554             if (!SPARCAssembler.isSimm13(v)) {
 555                 return v.getJavaKind().getByteCount();
 556             } else {
 557                 return 0;
 558             }
 559         }
 560 
 561         @Override
 562         public SizeEstimate estimateSize() {
 563             int constantBytes = 0;
 564             for (Constant c : keyConstants) {
 565                 constantBytes += estimateEmbeddedSize(c);
 566             }
 567             return new SizeEstimate(4 * keyTargets.length, constantBytes);
 568         }
 569     }
 570 
 571     public static final class TableSwitchOp extends SPARCBlockEndOp {
 572         public static final LIRInstructionClass<TableSwitchOp> TYPE = LIRInstructionClass.create(TableSwitchOp.class);
 573 
 574         private final int lowKey;
 575         private final LabelRef defaultTarget;
 576         private final LabelRef[] targets;
 577         @Alive protected Value index;
 578         @Temp protected Value scratch;
 579 
 580         public TableSwitchOp(final int lowKey, final LabelRef defaultTarget, final LabelRef[] targets, Variable index, Variable scratch) {
 581             super(TYPE);
 582             this.lowKey = lowKey;
 583             this.defaultTarget = defaultTarget;
 584             this.targets = targets;
 585             this.index = index;
 586             this.scratch = scratch;
 587         }
 588 
 589         @Override
 590         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 591             Register value = asRegister(index, SPARCKind.WORD);
 592             Register scratchReg = asRegister(scratch, SPARCKind.XWORD);
 593 
 594             // Compare index against jump table bounds
 595             int highKey = lowKey + targets.length - 1;
 596 
 597             // subtract the low value from the switch value
 598             if (isSimm13(lowKey)) {
 599                 masm.sub(value, lowKey, scratchReg);
 600             } else {
 601                 try (ScratchRegister sc = masm.getScratchRegister()) {
 602                     Register scratch2 = sc.getRegister();
 603                     masm.setx(lowKey, scratch2, false);
 604                     masm.sub(value, scratch2, scratchReg);
 605                 }
 606             }
 607             int upperLimit = highKey - lowKey;
 608             try (ScratchRegister sc = masm.getScratchRegister()) {
 609                 Register scratch2 = sc.getRegister();
 610                 if (isSimm13(upperLimit)) {
 611                     masm.cmp(scratchReg, upperLimit);
 612                 } else {
 613                     masm.setx(upperLimit, scratch2, false);
 614                     masm.cmp(scratchReg, upperLimit);
 615                 }
 616 
 617                 // Jump to default target if index is not within the jump table
 618                 if (defaultTarget != null) {
 619                     BPCC.emit(masm, Icc, GreaterUnsigned, NOT_ANNUL, PREDICT_TAKEN, defaultTarget.label());
 620                     masm.nop();  // delay slot
 621                 }
 622 
 623                 // Load jump table entry into scratch and jump to it
 624                 masm.sll(scratchReg, 3, scratchReg); // Multiply by 8
 625                 // Zero the left bits sll with shcnt>0 does not mask upper 32 bits
 626                 masm.srl(scratchReg, 0, scratchReg);
 627                 masm.rdpc(scratch2);
 628 
 629                 // The jump table follows four instructions after rdpc
 630                 masm.add(scratchReg, 4 * 4, scratchReg);
 631                 masm.jmpl(scratch2, scratchReg, g0);
 632             }
 633             masm.nop();
 634 
 635             // Emit jump table entries
 636             for (LabelRef target : targets) {
 637                 BPCC.emit(masm, Xcc, Always, NOT_ANNUL, PREDICT_TAKEN, target.label());
 638                 masm.nop(); // delay slot
 639             }
 640         }
 641 
 642         @Override
 643         public SizeEstimate estimateSize() {
 644             return SizeEstimate.create(17 + targets.length * 2);
 645         }
 646     }
 647 
 648     @Opcode("CMOVE")
 649     public static final class CondMoveOp extends SPARCLIRInstruction {
 650         public static final LIRInstructionClass<CondMoveOp> TYPE = LIRInstructionClass.create(CondMoveOp.class);
 651 
 652         @Def({REG, HINT}) protected Value result;
 653         @Use({REG, CONST}) protected Value trueValue;
 654         @Use({REG, CONST}) protected Value falseValue;
 655 
 656         private final ConditionFlag condition;
 657         private final CC cc;
 658         private final CMOV cmove;
 659 
 660         public CondMoveOp(CMOV cmove, CC cc, ConditionFlag condition, Value trueValue, Value falseValue, Value result) {
 661             super(TYPE);
 662             this.result = result;
 663             this.condition = condition;
 664             this.trueValue = trueValue;
 665             this.falseValue = falseValue;
 666             this.cc = cc;
 667             this.cmove = cmove;
 668         }
 669 
 670         @Override
 671         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 672             if (result.equals(trueValue)) { // We have the true value in place, do he opposite
 673                 cmove(masm, condition.negate(), falseValue);
 674             } else if (result.equals(falseValue)) {
 675                 cmove(masm, condition, trueValue);
 676             } else { // We have to move one of the input values to the result
 677                 ConditionFlag actualCondition = condition;
 678                 Value actualTrueValue = trueValue;
 679                 Value actualFalseValue = falseValue;
 680                 if (isJavaConstant(falseValue) && isSimm11(asJavaConstant(falseValue))) {
 681                     actualCondition = condition.negate();
 682                     actualTrueValue = falseValue;
 683                     actualFalseValue = trueValue;
 684                 }
 685                 SPARCMove.move(crb, masm, result, actualFalseValue, SPARCDelayedControlTransfer.DUMMY);
 686                 cmove(masm, actualCondition, actualTrueValue);
 687             }
 688         }
 689 
 690         private void cmove(SPARCMacroAssembler masm, ConditionFlag localCondition, Value value) {
 691             if (isConstantValue(value)) {
 692                 cmove.emit(masm, localCondition, cc, asImmediate(asJavaConstant(value)), asRegister(result));
 693             } else {
 694                 cmove.emit(masm, localCondition, cc, asRegister(value), asRegister(result));
 695             }
 696         }
 697 
 698         @Override
 699         public SizeEstimate estimateSize() {
 700             int constantSize = 0;
 701             if (isJavaConstant(trueValue) && !SPARCAssembler.isSimm13(asJavaConstant(trueValue))) {
 702                 constantSize += trueValue.getPlatformKind().getSizeInBytes();
 703             }
 704             if (isJavaConstant(falseValue) && !SPARCAssembler.isSimm13(asJavaConstant(falseValue))) {
 705                 constantSize += trueValue.getPlatformKind().getSizeInBytes();
 706             }
 707             return SizeEstimate.create(3, constantSize);
 708         }
 709     }
 710 
 711     public static ConditionFlag fromCondition(boolean integer, Condition cond, boolean unorderedIsTrue) {
 712         if (integer) {
 713             switch (cond) {
 714                 case EQ:
 715                     return Equal;
 716                 case NE:
 717                     return NotEqual;
 718                 case BT:
 719                     return LessUnsigned;
 720                 case LT:
 721                     return Less;
 722                 case BE:
 723                     return LessEqualUnsigned;
 724                 case LE:
 725                     return LessEqual;
 726                 case AE:
 727                     return GreaterEqualUnsigned;
 728                 case GE:
 729                     return GreaterEqual;
 730                 case AT:
 731                     return GreaterUnsigned;
 732                 case GT:
 733                     return Greater;
 734             }
 735             throw GraalError.shouldNotReachHere("Unimplemented for: " + cond);
 736         } else {
 737             switch (cond) {
 738                 case EQ:
 739                     return unorderedIsTrue ? F_UnorderedOrEqual : F_Equal;
 740                 case NE:
 741                     return ConditionFlag.F_NotEqual;
 742                 case LT:
 743                     return unorderedIsTrue ? F_UnorderedOrLess : F_Less;
 744                 case LE:
 745                     return unorderedIsTrue ? F_UnorderedOrLessOrEqual : F_LessOrEqual;
 746                 case GE:
 747                     return unorderedIsTrue ? F_UnorderedGreaterOrEqual : F_GreaterOrEqual;
 748                 case GT:
 749                     return unorderedIsTrue ? F_UnorderedOrGreater : F_Greater;
 750             }
 751             throw GraalError.shouldNotReachHere("Unkown condition: " + cond);
 752         }
 753     }
 754 }