1 /* 2 * Copyright (c) 2013, 2015, 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.aarch64; 24 25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST; 26 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG; 27 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant; 28 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant; 29 import static jdk.vm.ci.code.ValueUtil.asRegister; 30 import static jdk.vm.ci.code.ValueUtil.isRegister; 31 32 import org.graalvm.compiler.asm.NumUtil; 33 import org.graalvm.compiler.asm.aarch64.AArch64Assembler; 34 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler; 35 import org.graalvm.compiler.core.common.calc.Condition; 36 import org.graalvm.compiler.debug.GraalError; 37 import org.graalvm.compiler.lir.LIRInstructionClass; 38 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 39 40 import jdk.vm.ci.aarch64.AArch64Kind; 41 import jdk.vm.ci.meta.JavaConstant; 42 import jdk.vm.ci.meta.Value; 43 44 public class AArch64Compare { 45 46 public static class CompareOp extends AArch64LIRInstruction { 47 public static final LIRInstructionClass<CompareOp> TYPE = LIRInstructionClass.create(CompareOp.class); 48 49 @Use protected Value x; 50 @Use({REG, CONST}) protected Value y; 51 52 public CompareOp(Value x, Value y) { 53 super(TYPE); 54 assert ((AArch64Kind) x.getPlatformKind()).isInteger() && ((AArch64Kind) y.getPlatformKind()).isInteger(); 55 assert x.getPlatformKind() == y.getPlatformKind(); 56 this.x = x; 57 this.y = y; 58 } 59 60 @Override 61 public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) { 62 gpCompare(masm, x, y); 63 } 64 } 65 66 /** 67 * Compares integer values x and y. 68 * 69 * @param x integer value to compare. May not be null. 70 * @param y integer value to compare. May not be null. 71 */ 72 public static void gpCompare(AArch64MacroAssembler masm, Value x, Value y) { 73 final int size = x.getPlatformKind().getSizeInBytes() * Byte.SIZE; 74 if (isRegister(y)) { 75 masm.cmp(size, asRegister(x), asRegister(y)); 76 } else { 77 JavaConstant constant = asJavaConstant(y); 78 if (constant.isDefaultForKind()) { 79 masm.cmp(size, asRegister(x), 0); 80 } else { 81 final long longValue = constant.asLong(); 82 assert NumUtil.isInt(longValue); 83 int maskedValue; 84 switch (constant.getJavaKind()) { 85 case Boolean: 86 case Byte: 87 maskedValue = (int) (longValue & 0xFF); 88 break; 89 case Char: 90 case Short: 91 maskedValue = (int) (longValue & 0xFFFF); 92 break; 93 case Int: 94 case Long: 95 maskedValue = (int) longValue; 96 break; 97 default: 98 throw GraalError.shouldNotReachHere(); 99 } 100 masm.cmp(size, asRegister(x), maskedValue); 101 } 102 } 103 } 104 105 public static class FloatCompareOp extends AArch64LIRInstruction { 106 public static final LIRInstructionClass<FloatCompareOp> TYPE = LIRInstructionClass.create(FloatCompareOp.class); 107 108 @Use protected Value x; 109 @Use({REG, CONST}) protected Value y; 110 private final Condition condition; 111 private final boolean unorderedIsTrue; 112 113 public FloatCompareOp(Value x, Value y, Condition condition, boolean unorderedIsTrue) { 114 super(TYPE); 115 assert !isJavaConstant(y) || isFloatCmpConstant(y, condition, unorderedIsTrue); 116 this.x = x; 117 this.y = y; 118 this.condition = condition; 119 this.unorderedIsTrue = unorderedIsTrue; 120 } 121 122 /** 123 * Checks if val can be used as a constant for the gpCompare operation or not. 124 */ 125 public static boolean isFloatCmpConstant(Value val, Condition condition, boolean unorderedIsTrue) { 126 // If the condition is "EQ || unordered" or "NE && unordered" we have to use 2 registers 127 // in any case. 128 if (!(condition == Condition.EQ && unorderedIsTrue || condition == Condition.NE && !unorderedIsTrue)) { 129 return false; 130 } 131 return isJavaConstant(val) && asJavaConstant(val).isDefaultForKind(); 132 } 133 134 @Override 135 public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) { 136 assert isRegister(x); 137 int size = x.getPlatformKind().getSizeInBytes() * Byte.SIZE; 138 if (isRegister(y)) { 139 masm.fcmp(size, asRegister(x), asRegister(y)); 140 // There is no condition code for "EQ || unordered" nor one for "NE && unordered", 141 // so we have to fix them up ourselves. 142 // In both cases we combine the asked for condition into the EQ, respectively NE 143 // condition, i.e. 144 // if EQ && unoreredIsTrue, then the EQ flag will be set if the two values gpCompare 145 // unequal but are 146 // unordered. 147 if (condition == Condition.EQ && unorderedIsTrue) { 148 // if f1 ordered f2: 149 // result = f1 == f2 150 // else: 151 // result = EQUAL 152 int nzcv = 0b0100; // EQUAL -> Z = 1 153 masm.fccmp(size, asRegister(x), asRegister(y), nzcv, AArch64Assembler.ConditionFlag.VC); 154 } else if (condition == Condition.NE && !unorderedIsTrue) { 155 // if f1 ordered f2: 156 // result = f1 != f2 157 // else: 158 // result = !NE == EQUAL 159 int nzcv = 0b0100; // EQUAL -> Z = 1 160 masm.fccmp(size, asRegister(x), asRegister(y), nzcv, AArch64Assembler.ConditionFlag.VC); 161 } 162 } else { 163 // cmp against +0.0 164 masm.fcmpZero(size, asRegister(x)); 165 } 166 } 167 168 @Override 169 public void verify() { 170 assert x.getPlatformKind().equals(y.getPlatformKind()) : "a: " + x + " b: " + y; 171 } 172 } 173 174 }