1 /* 2 * Copyright (c) 2013, 2017, 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.List; 80 import org.graalvm.compiler.asm.Assembler; 81 import org.graalvm.compiler.asm.Assembler.LabelHint; 82 import org.graalvm.compiler.asm.Label; 83 import org.graalvm.compiler.core.common.NumUtil; 84 import org.graalvm.compiler.asm.sparc.SPARCAssembler; 85 import org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict; 86 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CC; 87 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CMOV; 88 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag; 89 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler; 90 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister; 91 import org.graalvm.compiler.core.common.calc.Condition; 92 import org.graalvm.compiler.debug.GraalError; 93 import org.graalvm.compiler.lir.LIRInstructionClass; 94 import org.graalvm.compiler.lir.LabelRef; 95 import org.graalvm.compiler.lir.Opcode; 96 import org.graalvm.compiler.lir.StandardOp; 97 import org.graalvm.compiler.lir.SwitchStrategy; 98 import org.graalvm.compiler.lir.SwitchStrategy.BaseSwitchClosure; 99 import org.graalvm.compiler.lir.Variable; 100 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 101 import org.graalvm.util.Equivalence; 102 import org.graalvm.util.EconomicMap; 103 104 import jdk.vm.ci.code.Register; 105 import jdk.vm.ci.meta.AllocatableValue; 106 import jdk.vm.ci.meta.Constant; 107 import jdk.vm.ci.meta.JavaConstant; 108 import jdk.vm.ci.meta.PlatformKind; 109 import jdk.vm.ci.meta.Value; 110 import jdk.vm.ci.sparc.SPARC.CPUFeature; 111 import jdk.vm.ci.sparc.SPARCKind; 112 113 public class SPARCControlFlow { 114 // This describes the maximum offset between the first emitted (load constant in to scratch, 115 // if does not fit into simm5 of cbcond) instruction and the final branch instruction 116 private static final int maximumSelfOffsetInstructions = 2; 117 118 public static final class ReturnOp extends SPARCBlockEndOp { 119 public static final LIRInstructionClass<ReturnOp> TYPE = LIRInstructionClass.create(ReturnOp.class); 120 public static final SizeEstimate SIZE = SizeEstimate.create(2); 121 122 @Use({REG, ILLEGAL}) protected Value x; 123 124 public ReturnOp(Value x) { 125 super(TYPE, SIZE); 126 this.x = x; 127 } 128 129 @Override 130 public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) { 131 emitCodeHelper(crb, masm); 132 } 133 134 public static void emitCodeHelper(CompilationResultBuilder crb, SPARCMacroAssembler masm) { 135 masm.ret(); 136 // On SPARC we always leave the frame (in the delay slot). 137 crb.frameContext.leave(crb); 138 } 139 } 140 141 public static final class CompareBranchOp extends SPARCBlockEndOp implements SPARCDelayedControlTransfer { 142 public static final LIRInstructionClass<CompareBranchOp> TYPE = LIRInstructionClass.create(CompareBranchOp.class); 143 public static final SizeEstimate SIZE = SizeEstimate.create(3); 144 static final EnumSet<SPARCKind> SUPPORTED_KINDS = EnumSet.of(XWORD, WORD); 145 146 @Use({REG}) protected AllocatableValue x; 147 @Use({REG, CONST}) protected Value y; 148 private ConditionFlag conditionFlag; 149 protected final LabelRef trueDestination; 150 protected LabelHint trueDestinationHint; 151 protected final LabelRef falseDestination; 152 protected LabelHint falseDestinationHint; 153 protected final SPARCKind kind; 154 protected final boolean unorderedIsTrue; 155 private boolean emitted = false; 156 private int delaySlotPosition = -1; 157 private double trueDestinationProbability; 158 159 public CompareBranchOp(AllocatableValue x, Value y, Condition condition, LabelRef trueDestination, LabelRef falseDestination, SPARCKind kind, boolean unorderedIsTrue, 160 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 EconomicMap<Label, LabelHint> labelHints; 430 private final List<Label> conditionalLabels = new ArrayList<>(); 431 432 public StrategySwitchOp(Value constantTableBase, SwitchStrategy strategy, LabelRef[] keyTargets, LabelRef defaultTarget, AllocatableValue key, Variable 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, 437 AllocatableValue key, 438 Variable scratch) { 439 super(c); 440 this.strategy = strategy; 441 this.keyConstants = strategy.getKeyConstants(); 442 this.keyTargets = keyTargets; 443 this.defaultTarget = defaultTarget; 444 this.constantTableBase = constantTableBase; 445 this.key = key; 446 this.scratch = scratch; 447 this.labelHints = EconomicMap.create(Equivalence.IDENTITY_WITH_SYSTEM_HASHCODE); 448 assert keyConstants.length == keyTargets.length; 449 assert keyConstants.length == strategy.keyProbabilities.length; 450 } 451 452 @Override 453 public void emitCode(final CompilationResultBuilder crb, final SPARCMacroAssembler masm) { 454 final Register keyRegister = asRegister(key); 455 final Register constantBaseRegister = AllocatableValue.ILLEGAL.equals(constantTableBase) ? g0 : asRegister(constantTableBase); 456 strategy.run(new SwitchClosure(keyRegister, constantBaseRegister, crb, masm)); 457 } 458 459 public class SwitchClosure extends BaseSwitchClosure { 460 private int conditionalLabelPointer = 0; 461 462 protected final Register keyRegister; 463 protected final Register constantBaseRegister; 464 protected final CompilationResultBuilder crb; 465 protected final SPARCMacroAssembler masm; 466 467 protected SwitchClosure(Register keyRegister, Register constantBaseRegister, CompilationResultBuilder crb, SPARCMacroAssembler masm) { 468 super(crb, masm, keyTargets, defaultTarget); 469 this.keyRegister = keyRegister; 470 this.constantBaseRegister = constantBaseRegister; 471 this.crb = crb; 472 this.masm = masm; 473 } 474 475 /** 476 * This method caches the generated labels over two assembly passes to get information 477 * about branch lengths. 478 */ 479 @Override 480 public Label conditionalJump(int index, Condition condition) { 481 Label label; 482 if (conditionalLabelPointer <= conditionalLabels.size()) { 483 label = new Label(); 484 conditionalLabels.add(label); 485 conditionalLabelPointer = conditionalLabels.size(); 486 } else { 487 // TODO: (sa) We rely here on the order how the labels are generated during 488 // code generation; if the order is not stable ower two assembly passes, the 489 // result can be wrong 490 label = conditionalLabels.get(conditionalLabelPointer++); 491 } 492 conditionalJump(index, condition, label); 493 return label; 494 } 495 496 @Override 497 protected void conditionalJump(int index, Condition condition, Label target) { 498 JavaConstant constant = (JavaConstant) keyConstants[index]; 499 CC conditionCode; 500 Long bits = constant.asLong(); 501 switch (constant.getJavaKind()) { 502 case Char: 503 case Byte: 504 case Short: 505 case Int: 506 conditionCode = CC.Icc; 507 break; 508 case Long: 509 conditionCode = CC.Xcc; 510 break; 511 default: 512 throw new GraalError("switch only supported for int, long and object"); 513 } 514 ConditionFlag conditionFlag = fromCondition(keyRegister.getRegisterCategory().equals(CPU), condition, false); 515 LabelHint hint = requestHint(masm, target); 516 boolean isShortConstant = isSimm5(constant); 517 int cbCondPosition = masm.position(); 518 if (!isShortConstant) { // Load constant takes one instruction 519 cbCondPosition += INSTRUCTION_SIZE; 520 } 521 boolean canUseShortBranch = masm.hasFeature(CPUFeature.CBCOND) && isShortBranch(masm, cbCondPosition, hint, target); 522 if (bits != null && canUseShortBranch) { 523 if (isShortConstant) { 524 CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, (int) (long) bits, target); 525 } else { 526 Register scratchRegister = asRegister(scratch); 527 const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY); 528 CBCOND.emit(masm, conditionFlag, conditionCode == Xcc, keyRegister, scratchRegister, target); 529 } 530 } else { 531 if (bits != null && isSimm13(constant)) { 532 masm.cmp(keyRegister, (int) (long) bits); // Cast is safe 533 } else { 534 Register scratchRegister = asRegister(scratch); 535 const2reg(crb, masm, scratch, constantBaseRegister, (JavaConstant) keyConstants[index], SPARCDelayedControlTransfer.DUMMY); 536 masm.cmp(keyRegister, scratchRegister); 537 } 538 BPCC.emit(masm, conditionCode, conditionFlag, ANNUL, PREDICT_TAKEN, target); 539 masm.nop(); // delay slot 540 } 541 } 542 } 543 544 protected LabelHint requestHint(SPARCMacroAssembler masm, Label label) { 545 LabelHint hint = labelHints.get(label); 546 if (hint == null) { 547 hint = masm.requestLabelHint(label); 548 labelHints.put(label, hint); 549 } 550 return hint; 551 } 552 553 protected int estimateEmbeddedSize(Constant c) { 554 JavaConstant v = (JavaConstant) c; 555 if (!SPARCAssembler.isSimm13(v)) { 556 return v.getJavaKind().getByteCount(); 557 } else { 558 return 0; 559 } 560 } 561 562 @Override 563 public SizeEstimate estimateSize() { 564 int constantBytes = 0; 565 for (Constant c : keyConstants) { 566 constantBytes += estimateEmbeddedSize(c); 567 } 568 return new SizeEstimate(4 * keyTargets.length, constantBytes); 569 } 570 } 571 572 public static final class TableSwitchOp extends SPARCBlockEndOp { 573 public static final LIRInstructionClass<TableSwitchOp> TYPE = LIRInstructionClass.create(TableSwitchOp.class); 574 575 private final int lowKey; 576 private final LabelRef defaultTarget; 577 private final LabelRef[] targets; 578 @Alive protected Value index; 579 @Temp protected Value scratch; 580 581 public TableSwitchOp(final int lowKey, final LabelRef defaultTarget, final LabelRef[] targets, Variable index, Variable scratch) { 582 super(TYPE); 583 this.lowKey = lowKey; 584 this.defaultTarget = defaultTarget; 585 this.targets = targets; 586 this.index = index; 587 this.scratch = scratch; 588 } 589 590 @Override 591 public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) { 592 Register value = asRegister(index, SPARCKind.WORD); 593 Register scratchReg = asRegister(scratch, SPARCKind.XWORD); 594 595 // Compare index against jump table bounds 596 int highKey = lowKey + targets.length - 1; 597 598 // subtract the low value from the switch value 599 if (isSimm13(lowKey)) { 600 masm.sub(value, lowKey, scratchReg); 601 } else { 602 try (ScratchRegister sc = masm.getScratchRegister()) { 603 Register scratch2 = sc.getRegister(); 604 masm.setx(lowKey, scratch2, false); 605 masm.sub(value, scratch2, scratchReg); 606 } 607 } 608 int upperLimit = highKey - lowKey; 609 try (ScratchRegister sc = masm.getScratchRegister()) { 610 Register scratch2 = sc.getRegister(); 611 if (isSimm13(upperLimit)) { 612 masm.cmp(scratchReg, upperLimit); 613 } else { 614 masm.setx(upperLimit, scratch2, false); 615 masm.cmp(scratchReg, upperLimit); 616 } 617 618 // Jump to default target if index is not within the jump table 619 if (defaultTarget != null) { 620 BPCC.emit(masm, Icc, GreaterUnsigned, NOT_ANNUL, PREDICT_TAKEN, defaultTarget.label()); 621 masm.nop(); // delay slot 622 } 623 624 // Load jump table entry into scratch and jump to it 625 masm.sll(scratchReg, 3, scratchReg); // Multiply by 8 626 // Zero the left bits sll with shcnt>0 does not mask upper 32 bits 627 masm.srl(scratchReg, 0, scratchReg); 628 masm.rdpc(scratch2); 629 630 // The jump table follows four instructions after rdpc 631 masm.add(scratchReg, 4 * 4, scratchReg); 632 masm.jmpl(scratch2, scratchReg, g0); 633 } 634 masm.nop(); 635 636 // Emit jump table entries 637 for (LabelRef target : targets) { 638 BPCC.emit(masm, Xcc, Always, NOT_ANNUL, PREDICT_TAKEN, target.label()); 639 masm.nop(); // delay slot 640 } 641 } 642 643 @Override 644 public SizeEstimate estimateSize() { 645 return SizeEstimate.create(17 + targets.length * 2); 646 } 647 } 648 649 @Opcode("CMOVE") 650 public static final class CondMoveOp extends SPARCLIRInstruction { 651 public static final LIRInstructionClass<CondMoveOp> TYPE = LIRInstructionClass.create(CondMoveOp.class); 652 653 @Def({REG, HINT}) protected Value result; 654 @Use({REG, CONST}) protected Value trueValue; 655 @Use({REG, CONST}) protected Value falseValue; 656 657 private final ConditionFlag condition; 658 private final CC cc; 659 private final CMOV cmove; 660 661 public CondMoveOp(CMOV cmove, CC cc, ConditionFlag condition, Value trueValue, Value falseValue, Value result) { 662 super(TYPE); 663 this.result = result; 664 this.condition = condition; 665 this.trueValue = trueValue; 666 this.falseValue = falseValue; 667 this.cc = cc; 668 this.cmove = cmove; 669 } 670 671 @Override 672 public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) { 673 if (result.equals(trueValue)) { // We have the true value in place, do he opposite 674 cmove(masm, condition.negate(), falseValue); 675 } else if (result.equals(falseValue)) { 676 cmove(masm, condition, trueValue); 677 } else { // We have to move one of the input values to the result 678 ConditionFlag actualCondition = condition; 679 Value actualTrueValue = trueValue; 680 Value actualFalseValue = falseValue; 681 if (isJavaConstant(falseValue) && isSimm11(asJavaConstant(falseValue))) { 682 actualCondition = condition.negate(); 683 actualTrueValue = falseValue; 684 actualFalseValue = trueValue; 685 } 686 SPARCMove.move(crb, masm, result, actualFalseValue, SPARCDelayedControlTransfer.DUMMY); 687 cmove(masm, actualCondition, actualTrueValue); 688 } 689 } 690 691 private void cmove(SPARCMacroAssembler masm, ConditionFlag localCondition, Value value) { 692 if (isConstantValue(value)) { 693 cmove.emit(masm, localCondition, cc, asImmediate(asJavaConstant(value)), asRegister(result)); 694 } else { 695 cmove.emit(masm, localCondition, cc, asRegister(value), asRegister(result)); 696 } 697 } 698 699 @Override 700 public SizeEstimate estimateSize() { 701 int constantSize = 0; 702 if (isJavaConstant(trueValue) && !SPARCAssembler.isSimm13(asJavaConstant(trueValue))) { 703 constantSize += trueValue.getPlatformKind().getSizeInBytes(); 704 } 705 if (isJavaConstant(falseValue) && !SPARCAssembler.isSimm13(asJavaConstant(falseValue))) { 706 constantSize += trueValue.getPlatformKind().getSizeInBytes(); 707 } 708 return SizeEstimate.create(3, constantSize); 709 } 710 } 711 712 public static ConditionFlag fromCondition(boolean integer, Condition cond, boolean unorderedIsTrue) { 713 if (integer) { 714 switch (cond) { 715 case EQ: 716 return Equal; 717 case NE: 718 return NotEqual; 719 case BT: 720 return LessUnsigned; 721 case LT: 722 return Less; 723 case BE: 724 return LessEqualUnsigned; 725 case LE: 726 return LessEqual; 727 case AE: 728 return GreaterEqualUnsigned; 729 case GE: 730 return GreaterEqual; 731 case AT: 732 return GreaterUnsigned; 733 case GT: 734 return Greater; 735 } 736 throw GraalError.shouldNotReachHere("Unimplemented for: " + cond); 737 } else { 738 switch (cond) { 739 case EQ: 740 return unorderedIsTrue ? F_UnorderedOrEqual : F_Equal; 741 case NE: 742 return ConditionFlag.F_NotEqual; 743 case LT: 744 return unorderedIsTrue ? F_UnorderedOrLess : F_Less; 745 case LE: 746 return unorderedIsTrue ? F_UnorderedOrLessOrEqual : F_LessOrEqual; 747 case GE: 748 return unorderedIsTrue ? F_UnorderedGreaterOrEqual : F_GreaterOrEqual; 749 case GT: 750 return unorderedIsTrue ? F_UnorderedOrGreater : F_Greater; 751 } 752 throw GraalError.shouldNotReachHere("Unkown condition: " + cond); 753 } 754 } 755 }