1 /* 2 * Copyright (c) 2013, 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 com.oracle.graal.lir.hsail; 24 25 import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*; 26 27 import com.oracle.graal.api.meta.*; 28 import com.oracle.graal.asm.hsail.*; 29 import com.oracle.graal.graph.*; 30 import com.oracle.graal.lir.*; 31 import com.oracle.graal.lir.asm.*; 32 import com.oracle.graal.nodes.calc.*; 33 34 /** 35 * Implementation of control flow instructions. 36 */ 37 public class HSAILControlFlow { 38 39 public static class ReturnOp extends HSAILLIRInstruction { 40 41 @Use({REG, ILLEGAL}) protected Value x; 42 43 public ReturnOp(Value x) { 44 this.x = x; 45 } 46 47 @Override 48 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 49 if (tasm.frameContext != null) { 50 tasm.frameContext.leave(tasm); 51 } 52 masm.exit(); 53 } 54 } 55 56 public static class CompareBranchOp extends HSAILLIRInstruction implements StandardOp.BranchOp { 57 58 @Opcode protected final HSAILCompare opcode; 59 @Use({REG, CONST}) protected Value x; 60 @Use({REG, CONST}) protected Value y; 61 @Def({REG}) protected Value z; 62 protected Condition condition; 63 protected LabelRef destination; 64 protected boolean unordered = false; 65 @Def({REG}) protected Value result; 66 67 public CompareBranchOp(HSAILCompare opcode, Condition condition, Value x, Value y, Value z, Value result, LabelRef destination) { 68 this.condition = condition; 69 this.opcode = opcode; 70 this.x = x; 71 this.y = y; 72 this.z = z; 73 this.result = result; 74 this.destination = destination; 75 } 76 77 @Override 78 public LabelRef destination() { 79 return destination; 80 } 81 82 @Override 83 public void negate(LabelRef newDestination) { 84 destination = newDestination; 85 condition = condition.negate(); 86 } 87 88 @Override 89 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 90 HSAILCompare.emit(tasm, masm, condition, x, y, z, unordered); 91 masm.cbr(masm.nameOf(destination.label())); 92 } 93 } 94 95 public static class FloatCompareBranchOp extends CompareBranchOp { 96 97 public FloatCompareBranchOp(HSAILCompare opcode, Condition condition, Value x, Value y, Value z, Value result, LabelRef destination, boolean unordered) { 98 super(opcode, condition, x, y, z, result, destination); 99 this.unordered = unordered; 100 } 101 102 @Override 103 public void negate(LabelRef newDestination) { 104 destination = newDestination; 105 condition = condition.negate(); 106 unordered = !unordered; 107 } 108 109 @Override 110 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 111 HSAILCompare.emit(tasm, masm, condition, x, y, z, unordered); 112 masm.cbr(masm.nameOf(destination.label())); 113 } 114 } 115 116 public static class DoubleCompareBranchOp extends CompareBranchOp { 117 118 public DoubleCompareBranchOp(HSAILCompare opcode, Condition condition, Value x, Value y, Value z, Value result, LabelRef destination, boolean unordered) { 119 super(opcode, condition, x, y, z, result, destination); 120 this.unordered = unordered; 121 } 122 123 @Override 124 public void negate(LabelRef newDestination) { 125 destination = newDestination; 126 condition = condition.negate(); 127 unordered = !unordered; 128 } 129 130 @Override 131 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 132 HSAILCompare.emit(tasm, masm, condition, x, y, z, unordered); 133 masm.cbr(masm.nameOf(destination.label())); 134 } 135 } 136 137 public static class BranchOp extends HSAILLIRInstruction implements StandardOp.BranchOp { 138 139 protected Condition condition; 140 protected LabelRef destination; 141 @Def({REG}) protected Value result; 142 143 public BranchOp(Condition condition, Value result, LabelRef destination) { 144 this.condition = condition; 145 this.destination = destination; 146 this.result = result; 147 } 148 149 @Override 150 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 151 masm.cbr(masm.nameOf(destination.label())); 152 } 153 154 @Override 155 public LabelRef destination() { 156 return destination; 157 } 158 159 @Override 160 public void negate(LabelRef newDestination) { 161 destination = newDestination; 162 condition = condition.negate(); 163 } 164 } 165 166 public static class FloatBranchOp extends BranchOp { 167 168 public FloatBranchOp(Condition condition, Value result, LabelRef destination) { 169 super(condition, result, destination); 170 } 171 172 @Override 173 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 174 masm.cbr(masm.nameOf(destination.label())); 175 } 176 } 177 178 public static class CondMoveOp extends HSAILLIRInstruction { 179 180 @Opcode protected final HSAILCompare opcode; 181 @Def({REG, HINT}) protected Value result; 182 @Alive({REG}) protected Value trueValue; 183 @Use({REG, STACK, CONST}) protected Value falseValue; 184 @Use({REG, STACK, CONST}) protected Value left; 185 @Use({REG, STACK, CONST}) protected Value right; 186 protected final Condition condition; 187 188 public CondMoveOp(HSAILCompare opcode, Variable left, Variable right, Variable result, Condition condition, Variable trueValue, Value falseValue) { 189 this.opcode = opcode; 190 this.result = result; 191 this.left = left; 192 this.right = right; 193 this.condition = condition; 194 this.trueValue = trueValue; 195 this.falseValue = falseValue; 196 } 197 198 @Override 199 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 200 HSAILCompare.emit(tasm, masm, condition, left, right, right, false); 201 cmove(tasm, masm, result, false, trueValue, falseValue); 202 } 203 } 204 205 public static class FloatCondMoveOp extends CondMoveOp { 206 207 private final boolean unorderedIsTrue; 208 209 public FloatCondMoveOp(HSAILCompare opcode, Variable left, Variable right, Variable result, Condition condition, boolean unorderedIsTrue, Variable trueValue, Value falseValue) { 210 super(opcode, left, right, result, condition, trueValue, falseValue); 211 this.unorderedIsTrue = unorderedIsTrue; 212 } 213 214 @Override 215 public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) { 216 HSAILCompare.emit(tasm, masm, condition, left, right, right, unorderedIsTrue); 217 cmove(tasm, masm, result, false, trueValue, falseValue); 218 } 219 } 220 221 @SuppressWarnings("unused") 222 private static void cmove(TargetMethodAssembler tasm, HSAILAssembler masm, Value result, boolean unorderedIsTrue, Value trueValue, Value falseValue) { 223 // Check that we don't overwrite an input operand before it is used. 224 assert (result.getKind() == trueValue.getKind() && result.getKind() == falseValue.getKind()); 225 int width; 226 switch (result.getKind()) { 227 /** 228 * We don't need to pass the cond to the assembler. We will always use $c0 as the control 229 * register. 230 */ 231 case Float: 232 case Int: 233 width = 32; 234 break; 235 case Double: 236 case Long: 237 width = 64; 238 break; 239 default: 240 throw GraalInternalError.shouldNotReachHere(); 241 } 242 masm.cmovCommon(result, trueValue, falseValue, width); 243 } 244 }