graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILArithmetic.java

Print this page




  27 
  28 import com.oracle.graal.api.meta.*;
  29 import com.oracle.graal.asm.hsail.*;
  30 import com.oracle.graal.graph.*;
  31 import com.oracle.graal.lir.*;
  32 import com.oracle.graal.lir.asm.*;
  33 
  34 // @formatter:off
  35 /**
  36  * Defines arithmetic instruction nodes.
  37  */
  38 public enum HSAILArithmetic {
  39     IADD, OADD, ISUB, FSUB, DSUB, IMUL, FMUL,
  40     DMUL, IDIV, FDIV, DDIV, IUADD, IUSUB,
  41     IUMUL, IUDIV, LADD, DADD, FADD, LSUB,
  42     LMUL, LDIV, LUADD, LUSUB, LUMUL,
  43     LUDIV, IMAX, LMAX, IUMAX, LUMAX,
  44     IMIN, LMIN, IUMIN, LUMIN, IREM,
  45     LREM, FREM, DREM, IUREM, LUREM,
  46     ICARRY, LCARRY, IUCARRY, LUCARRY,
  47     IAND, INEG, IUSHR, I2B, I2S, I2L,
  48     F2D, F2I, F2L, D2F, I2F, I2D, D2I,
  49     L2F, D2L, MOV_F2I, MOV_D2L, L2D, MOV_I2F,
  50     MOV_L2D, ISHL, SQRT, UNDEF, CALL, L2I;

  51 
  52     public static class Op1Stack extends HSAILLIRInstruction {
  53         @Opcode private final HSAILArithmetic opcode;
  54         @Def({REG, HINT}) protected Value result;
  55         @Use({REG, STACK, CONST}) protected Value x;
  56 
  57         public Op1Stack(HSAILArithmetic opcode, Value result, Value x) {
  58             this.opcode = opcode;
  59             this.result = result;
  60             this.x = x;
  61         }
  62 
  63         @Override
  64         public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) {
  65             emit(tasm, masm, opcode, result, x, null);
  66         }
  67     }
  68 
  69     public static class Op2Stack extends HSAILLIRInstruction {
  70         @Opcode private final HSAILArithmetic opcode;


 228                 case I2L:
 229                 case L2I:
 230                 case F2D:
 231                 case D2F: masm.emitConvert(dst, src); break;
 232                 case SQRT: masm.emitArg1("sqrt", dst, src); break;
 233                 case UNDEF: masm.undefined("undefined node"); break;
 234                 case CALL: masm.undefined("undefined node CALL"); break;
 235                 default:
 236                     throw GraalInternalError.shouldNotReachHere();
 237             }
 238         }  else {
 239             throw GraalInternalError.shouldNotReachHere();
 240         }
 241         if (info != null) {
 242             assert exceptionOffset != -1;
 243             tasm.recordImplicitException(exceptionOffset, info);
 244         }
 245     }
 246 
 247     public static void emit(TargetMethodAssembler tasm, HSAILAssembler masm, HSAILArithmetic opcode, Value dst, Value src1, Value src2, LIRFrameState info) {
















 248         int exceptionOffset = -1;
 249         switch (opcode) {
 250             case IADD:
 251             case LADD:
 252             case DADD:
 253             case FADD:
 254             case OADD:
 255                 masm.emit("add", dst, src1, src2); break;
 256             case ISUB:
 257             case LSUB:
 258             case DSUB:
 259             case FSUB:
 260                 masm.emit("sub", dst, src1, src2); break;
 261             case IMUL:
 262             case LMUL:
 263             case FMUL:
 264             case DMUL:
 265             case LUMUL:
 266                 masm.emit("mul", dst, src1, src2); break;
 267             case IDIV:
 268             case LDIV:
 269             case FDIV:
 270             case DDIV:
 271                 masm.emit("div", dst, src1, src2); break;
 272             case IMAX:
 273             case LMAX:
 274                 masm.emit("max", dst, src1, src2); break;
 275             case IMIN:
 276             case LMIN:
 277                 masm.emit("min", dst, src1, src2); break;
 278             case ISHL:

 279                 masm.emit("shl", dst, src1, src2); break;






 280             case IREM:
 281                 masm.emit("rem", dst, src1, src2); break;
 282             default:    throw GraalInternalError.shouldNotReachHere();
 283         }
 284         if (info != null) {
 285             assert exceptionOffset != -1;
 286             tasm.recordImplicitException(exceptionOffset, info);
 287         }
 288     }
 289 
 290     private static void verifyKind(HSAILArithmetic opcode, Value result, Value x, Value y) {
 291         assert (opcode.name().startsWith("I") && result.getKind() == Kind.Int && x.getKind().getStackKind() == Kind.Int && y.getKind().getStackKind() == Kind.Int)
 292         || (opcode.name().startsWith("L") && result.getKind() == Kind.Long && x.getKind() == Kind.Long && y.getKind() == Kind.Long)
 293         || (opcode.name().startsWith("LU") && result.getKind() == Kind.Long && x.getKind() == Kind.Long && y.getKind() == Kind.Int)
 294         || (opcode.name().startsWith("F") && result.getKind() == Kind.Float && x.getKind() == Kind.Float && y.getKind() == Kind.Float)
 295         || (opcode.name().startsWith("D") && result.getKind() == Kind.Double && x.getKind() == Kind.Double && y.getKind() == Kind.Double)
 296         || (opcode.name().startsWith("O") && result.getKind() == Kind.Object && x.getKind() == Kind.Object && (y.getKind() == Kind.Int || y.getKind() == Kind.Long)
 297                         );
 298     }
 299 }


  27 
  28 import com.oracle.graal.api.meta.*;
  29 import com.oracle.graal.asm.hsail.*;
  30 import com.oracle.graal.graph.*;
  31 import com.oracle.graal.lir.*;
  32 import com.oracle.graal.lir.asm.*;
  33 
  34 // @formatter:off
  35 /**
  36  * Defines arithmetic instruction nodes.
  37  */
  38 public enum HSAILArithmetic {
  39     IADD, OADD, ISUB, FSUB, DSUB, IMUL, FMUL,
  40     DMUL, IDIV, FDIV, DDIV, IUADD, IUSUB,
  41     IUMUL, IUDIV, LADD, DADD, FADD, LSUB,
  42     LMUL, LDIV, LUADD, LUSUB, LUMUL,
  43     LUDIV, IMAX, LMAX, IUMAX, LUMAX,
  44     IMIN, LMIN, IUMIN, LUMIN, IREM,
  45     LREM, FREM, DREM, IUREM, LUREM,
  46     ICARRY, LCARRY, IUCARRY, LUCARRY,
  47     IAND, LAND, INEG, I2B, I2S, I2L,
  48     F2D, F2I, F2L, D2F, I2F, I2D, D2I,
  49     L2F, D2L, MOV_F2I, MOV_D2L, L2D, MOV_I2F,
  50     MOV_L2D, ISHL, LSHL, ISHR, LSHR, IUSHR, LUSHR,
  51     SQRT, UNDEF, CALL, L2I;
  52 
  53     public static class Op1Stack extends HSAILLIRInstruction {
  54         @Opcode private final HSAILArithmetic opcode;
  55         @Def({REG, HINT}) protected Value result;
  56         @Use({REG, STACK, CONST}) protected Value x;
  57 
  58         public Op1Stack(HSAILArithmetic opcode, Value result, Value x) {
  59             this.opcode = opcode;
  60             this.result = result;
  61             this.x = x;
  62         }
  63 
  64         @Override
  65         public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) {
  66             emit(tasm, masm, opcode, result, x, null);
  67         }
  68     }
  69 
  70     public static class Op2Stack extends HSAILLIRInstruction {
  71         @Opcode private final HSAILArithmetic opcode;


 229                 case I2L:
 230                 case L2I:
 231                 case F2D:
 232                 case D2F: masm.emitConvert(dst, src); break;
 233                 case SQRT: masm.emitArg1("sqrt", dst, src); break;
 234                 case UNDEF: masm.undefined("undefined node"); break;
 235                 case CALL: masm.undefined("undefined node CALL"); break;
 236                 default:
 237                     throw GraalInternalError.shouldNotReachHere();
 238             }
 239         }  else {
 240             throw GraalInternalError.shouldNotReachHere();
 241         }
 242         if (info != null) {
 243             assert exceptionOffset != -1;
 244             tasm.recordImplicitException(exceptionOffset, info);
 245         }
 246     }
 247 
 248     public static void emit(TargetMethodAssembler tasm, HSAILAssembler masm, HSAILArithmetic opcode, Value dst, Value src1, Value src2, LIRFrameState info) {
 249         /**
 250          * First check if one of src1 or src2 is an AddressValue.  If it is,
 251          * convert the address to a register using an lda instruction.  We can
 252          * just reuse the eventual dst register for this.
 253          */
 254         if (src1 instanceof HSAILAddressValue) {
 255             assert (!(src2 instanceof HSAILAddressValue));
 256             masm.emitLda(dst, ((HSAILAddressValue) src1).toAddress());
 257             emit(tasm, masm, opcode, dst, dst, src2, info);
 258             return;
 259         } else if (src2 instanceof HSAILAddressValue) {
 260             assert (!(src1 instanceof HSAILAddressValue));
 261             masm.emitLda(dst, ((HSAILAddressValue) src2).toAddress());
 262             emit(tasm, masm, opcode, dst, src1, dst, info);
 263             return;
 264         }
 265         int exceptionOffset = -1;
 266         switch (opcode) {
 267             case IADD:
 268             case LADD:
 269             case DADD:
 270             case FADD:
 271             case OADD:
 272                 masm.emit("add", dst, src1, src2); break;
 273             case ISUB:
 274             case LSUB:
 275             case DSUB:
 276             case FSUB:
 277                 masm.emit("sub", dst, src1, src2); break;
 278             case IMUL:
 279             case LMUL:
 280             case FMUL:
 281             case DMUL:
 282             case LUMUL:
 283                 masm.emit("mul", dst, src1, src2); break;
 284             case IDIV:
 285             case LDIV:
 286             case FDIV:
 287             case DDIV:
 288                 masm.emit("div", dst, src1, src2); break;
 289             case IMAX:
 290             case LMAX:
 291                 masm.emit("max", dst, src1, src2); break;
 292             case IMIN:
 293             case LMIN:
 294                 masm.emit("min", dst, src1, src2); break;
 295             case ISHL:
 296             case LSHL:
 297                 masm.emit("shl", dst, src1, src2); break;
 298             case ISHR:
 299             case LSHR:
 300                 masm.emit("shr", dst, src1, src2); break;
 301             case IUSHR:
 302             case LUSHR:
 303                 masm.emitForceUnsigned("shr", dst, src1, src2); break;
 304             case IREM:
 305                 masm.emit("rem", dst, src1, src2); break;
 306             default:    throw GraalInternalError.shouldNotReachHere();
 307         }
 308         if (info != null) {
 309             assert exceptionOffset != -1;
 310             tasm.recordImplicitException(exceptionOffset, info);
 311         }
 312     }
 313 
 314     private static void verifyKind(HSAILArithmetic opcode, Value result, Value x, Value y) {
 315         assert (opcode.name().startsWith("I") && result.getKind() == Kind.Int && x.getKind().getStackKind() == Kind.Int && y.getKind().getStackKind() == Kind.Int)
 316         || (opcode.name().startsWith("L") && result.getKind() == Kind.Long && x.getKind() == Kind.Long && y.getKind() == Kind.Long)
 317         || (opcode.name().startsWith("LU") && result.getKind() == Kind.Long && x.getKind() == Kind.Long && y.getKind() == Kind.Int)
 318         || (opcode.name().startsWith("F") && result.getKind() == Kind.Float && x.getKind() == Kind.Float && y.getKind() == Kind.Float)
 319         || (opcode.name().startsWith("D") && result.getKind() == Kind.Double && x.getKind() == Kind.Double && y.getKind() == Kind.Double)
 320         || (opcode.name().startsWith("O") && result.getKind() == Kind.Object && x.getKind() == Kind.Object && (y.getKind() == Kind.Int || y.getKind() == Kind.Long)
 321                         );
 322     }
 323 }