1 /*
   2  * Copyright (c) 2005, 2022, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "c1/c1_Compilation.hpp"
  27 #include "c1/c1_FrameMap.hpp"
  28 #include "c1/c1_Instruction.hpp"
  29 #include "c1/c1_LIRAssembler.hpp"
  30 #include "c1/c1_LIRGenerator.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "c1/c1_ValueStack.hpp"
  33 #include "ci/ciArray.hpp"
  34 #include "ci/ciObjArrayKlass.hpp"
  35 #include "ci/ciTypeArrayKlass.hpp"
  36 #include "gc/shared/c1/barrierSetC1.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "utilities/powerOfTwo.hpp"
  40 #include "vmreg_x86.inline.hpp"
  41 
  42 #ifdef ASSERT
  43 #define __ gen()->lir(__FILE__, __LINE__)->
  44 #else
  45 #define __ gen()->lir()->
  46 #endif
  47 
  48 // Item will be loaded into a byte register; Intel only
  49 void LIRItem::load_byte_item() {
  50   load_item();
  51   LIR_Opr res = result();
  52 
  53   if (!res->is_virtual() || !_gen->is_vreg_flag_set(res, LIRGenerator::byte_reg)) {
  54     // make sure that it is a byte register
  55     assert(!value()->type()->is_float() && !value()->type()->is_double(),
  56            "can't load floats in byte register");
  57     LIR_Opr reg = _gen->rlock_byte(T_BYTE);
  58     __ move(res, reg);
  59 
  60     _result = reg;
  61   }
  62 }
  63 
  64 
  65 void LIRItem::load_nonconstant() {
  66   LIR_Opr r = value()->operand();
  67   if (r->is_constant()) {
  68     _result = r;
  69   } else {
  70     load_item();
  71   }
  72 }
  73 
  74 //--------------------------------------------------------------
  75 //               LIRGenerator
  76 //--------------------------------------------------------------
  77 
  78 
  79 LIR_Opr LIRGenerator::exceptionOopOpr() { return FrameMap::rax_oop_opr; }
  80 LIR_Opr LIRGenerator::exceptionPcOpr()  { return FrameMap::rdx_opr; }
  81 LIR_Opr LIRGenerator::divInOpr()        { return FrameMap::rax_opr; }
  82 LIR_Opr LIRGenerator::divOutOpr()       { return FrameMap::rax_opr; }
  83 LIR_Opr LIRGenerator::remOutOpr()       { return FrameMap::rdx_opr; }
  84 LIR_Opr LIRGenerator::shiftCountOpr()   { return FrameMap::rcx_opr; }
  85 LIR_Opr LIRGenerator::syncLockOpr()     { return new_register(T_INT); }
  86 LIR_Opr LIRGenerator::syncTempOpr()     { return FrameMap::rax_opr; }
  87 LIR_Opr LIRGenerator::getThreadTemp()   { return LIR_OprFact::illegalOpr; }
  88 
  89 
  90 LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
  91   LIR_Opr opr;
  92   switch (type->tag()) {
  93     case intTag:     opr = FrameMap::rax_opr;          break;
  94     case objectTag:  opr = FrameMap::rax_oop_opr;      break;
  95     case longTag:    opr = FrameMap::long0_opr;        break;
  96 #ifdef _LP64
  97     case floatTag:   opr = FrameMap::xmm0_float_opr;   break;
  98     case doubleTag:  opr = FrameMap::xmm0_double_opr;  break;
  99 #else
 100     case floatTag:   opr = UseSSE >= 1 ? FrameMap::xmm0_float_opr  : FrameMap::fpu0_float_opr;  break;
 101     case doubleTag:  opr = UseSSE >= 2 ? FrameMap::xmm0_double_opr : FrameMap::fpu0_double_opr;  break;
 102 #endif // _LP64
 103     case addressTag:
 104     default: ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
 105   }
 106 
 107   assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
 108   return opr;
 109 }
 110 
 111 
 112 LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
 113   LIR_Opr reg = new_register(T_INT);
 114   set_vreg_flag(reg, LIRGenerator::byte_reg);
 115   return reg;
 116 }
 117 
 118 
 119 //--------- loading items into registers --------------------------------
 120 
 121 
 122 // i486 instructions can inline constants
 123 bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
 124   if (type == T_SHORT || type == T_CHAR) {
 125     return false;
 126   }
 127   Constant* c = v->as_Constant();
 128   if (c && c->state_before() == NULL) {
 129     // constants of any type can be stored directly, except for
 130     // unloaded object constants.
 131     return true;
 132   }
 133   return false;
 134 }
 135 
 136 
 137 bool LIRGenerator::can_inline_as_constant(Value v) const {
 138   if (v->type()->tag() == longTag) return false;
 139   return v->type()->tag() != objectTag ||
 140     (v->type()->is_constant() && v->type()->as_ObjectType()->constant_value()->is_null_object());
 141 }
 142 
 143 
 144 bool LIRGenerator::can_inline_as_constant(LIR_Const* c) const {
 145   if (c->type() == T_LONG) return false;
 146   return c->type() != T_OBJECT || c->as_jobject() == NULL;
 147 }
 148 
 149 
 150 LIR_Opr LIRGenerator::safepoint_poll_register() {
 151   NOT_LP64( return new_register(T_ADDRESS); )
 152   return LIR_OprFact::illegalOpr;
 153 }
 154 
 155 
 156 LIR_Address* LIRGenerator::generate_address(LIR_Opr base, LIR_Opr index,
 157                                             int shift, int disp, BasicType type) {
 158   assert(base->is_register(), "must be");
 159   if (index->is_constant()) {
 160     LIR_Const *constant = index->as_constant_ptr();
 161 #ifdef _LP64
 162     jlong c;
 163     if (constant->type() == T_INT) {
 164       c = (jlong(index->as_jint()) << shift) + disp;
 165     } else {
 166       assert(constant->type() == T_LONG, "should be");
 167       c = (index->as_jlong() << shift) + disp;
 168     }
 169     if ((jlong)((jint)c) == c) {
 170       return new LIR_Address(base, (jint)c, type);
 171     } else {
 172       LIR_Opr tmp = new_register(T_LONG);
 173       __ move(index, tmp);
 174       return new LIR_Address(base, tmp, type);
 175     }
 176 #else
 177     return new LIR_Address(base,
 178                            ((intx)(constant->as_jint()) << shift) + disp,
 179                            type);
 180 #endif
 181   } else {
 182     return new LIR_Address(base, index, (LIR_Address::Scale)shift, disp, type);
 183   }
 184 }
 185 
 186 
 187 LIR_Address* LIRGenerator::emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr,
 188                                               BasicType type) {
 189   int offset_in_bytes = arrayOopDesc::base_offset_in_bytes(type);
 190 
 191   LIR_Address* addr;
 192   if (index_opr->is_constant()) {
 193     int elem_size = type2aelembytes(type);
 194 #ifdef _LP64
 195     jint index = index_opr->as_jint();
 196     jlong disp = offset_in_bytes + (jlong)(index) * elem_size;
 197     if (disp > max_jint) {
 198       // Displacement overflow. Cannot directly use instruction with 32-bit displacement for 64-bit addresses.
 199       // Convert array index to long to do array offset computation with 64-bit values.
 200       index_opr = new_register(T_LONG);
 201       __ move(LIR_OprFact::longConst(index), index_opr);
 202       addr = new LIR_Address(array_opr, index_opr, LIR_Address::scale(type), offset_in_bytes, type);
 203     } else {
 204       addr = new LIR_Address(array_opr, (intx)disp, type);
 205     }
 206 #else
 207     // A displacement overflow can also occur for x86 but that is not a problem due to the 32-bit address range!
 208     // Let's assume an array 'a' and an access with displacement 'disp'. When disp overflows, then "a + disp" will
 209     // always be negative (i.e. underflows the 32-bit address range):
 210     // Let N = 2^32: a + signed_overflow(disp) = a + disp - N.
 211     // "a + disp" is always smaller than N. If an index was chosen which would point to an address beyond N, then
 212     // range checks would catch that and throw an exception. Thus, a + disp < 0 holds which means that it always
 213     // underflows the 32-bit address range:
 214     // unsigned_underflow(a + signed_overflow(disp)) = unsigned_underflow(a + disp - N)
 215     //                                              = (a + disp - N) + N = a + disp
 216     // This shows that we still end up at the correct address with a displacement overflow due to the 32-bit address
 217     // range limitation. This overflow only needs to be handled if addresses can be larger as on 64-bit platforms.
 218     addr = new LIR_Address(array_opr, offset_in_bytes + (intx)(index_opr->as_jint()) * elem_size, type);
 219 #endif // _LP64
 220   } else {
 221 #ifdef _LP64
 222     if (index_opr->type() == T_INT) {
 223       LIR_Opr tmp = new_register(T_LONG);
 224       __ convert(Bytecodes::_i2l, index_opr, tmp);
 225       index_opr = tmp;
 226     }
 227 #endif // _LP64
 228     addr =  new LIR_Address(array_opr,
 229                             index_opr,
 230                             LIR_Address::scale(type),
 231                             offset_in_bytes, type);
 232   }
 233   return addr;
 234 }
 235 
 236 
 237 LIR_Opr LIRGenerator::load_immediate(jlong x, BasicType type) {
 238   LIR_Opr r;
 239   if (type == T_LONG) {
 240     r = LIR_OprFact::longConst(x);
 241   } else if (type == T_INT) {
 242     r = LIR_OprFact::intConst(checked_cast<jint>(x));
 243   } else {
 244     ShouldNotReachHere();
 245   }
 246   return r;
 247 }
 248 
 249 void LIRGenerator::increment_counter(address counter, BasicType type, int step) {
 250   LIR_Opr pointer = new_pointer_register();
 251   __ move(LIR_OprFact::intptrConst(counter), pointer);
 252   LIR_Address* addr = new LIR_Address(pointer, type);
 253   increment_counter(addr, step);
 254 }
 255 
 256 
 257 void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
 258   __ add((LIR_Opr)addr, LIR_OprFact::intConst(step), (LIR_Opr)addr);
 259 }
 260 
 261 void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
 262   __ cmp_mem_int(condition, base, disp, c, info);
 263 }
 264 
 265 
 266 void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info) {
 267   __ cmp_reg_mem(condition, reg, new LIR_Address(base, disp, type), info);
 268 }
 269 
 270 
 271 bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, jint c, LIR_Opr result, LIR_Opr tmp) {
 272   if (tmp->is_valid() && c > 0 && c < max_jint) {
 273     if (is_power_of_2(c + 1)) {
 274       __ move(left, tmp);
 275       __ shift_left(left, log2i_exact(c + 1), left);
 276       __ sub(left, tmp, result);
 277       return true;
 278     } else if (is_power_of_2(c - 1)) {
 279       __ move(left, tmp);
 280       __ shift_left(left, log2i_exact(c - 1), left);
 281       __ add(left, tmp, result);
 282       return true;
 283     }
 284   }
 285   return false;
 286 }
 287 
 288 
 289 void LIRGenerator::store_stack_parameter (LIR_Opr item, ByteSize offset_from_sp) {
 290   BasicType type = item->type();
 291   __ store(item, new LIR_Address(FrameMap::rsp_opr, in_bytes(offset_from_sp), type));
 292 }
 293 
 294 void LIRGenerator::array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info, ciMethod* profiled_method, int profiled_bci) {
 295   LIR_Opr tmp1 = new_register(objectType);
 296   LIR_Opr tmp2 = new_register(objectType);
 297   LIR_Opr tmp3 = new_register(objectType);
 298   __ store_check(value, array, tmp1, tmp2, tmp3, store_check_info, profiled_method, profiled_bci);
 299 }
 300 
 301 //----------------------------------------------------------------------
 302 //             visitor functions
 303 //----------------------------------------------------------------------
 304 
 305 void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
 306   assert(x->is_pinned(),"");
 307   LIRItem obj(x->obj(), this);
 308   obj.load_item();
 309 
 310   set_no_result(x);
 311 
 312   // "lock" stores the address of the monitor stack slot, so this is not an oop
 313   LIR_Opr lock = new_register(T_INT);
 314 
 315   CodeEmitInfo* info_for_exception = NULL;
 316   if (x->needs_null_check()) {
 317     info_for_exception = state_for(x);
 318   }
 319   // this CodeEmitInfo must not have the xhandlers because here the
 320   // object is already locked (xhandlers expect object to be unlocked)
 321   CodeEmitInfo* info = state_for(x, x->state(), true);
 322   LIR_Opr tmp = UseFastLocking ? new_register(T_INT) : LIR_OprFact::illegalOpr;
 323   monitor_enter(obj.result(), lock, syncTempOpr(), tmp,
 324                         x->monitor_no(), info_for_exception, info);
 325 }
 326 
 327 
 328 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
 329   assert(x->is_pinned(),"");
 330 
 331   LIRItem obj(x->obj(), this);
 332   obj.dont_load_item();
 333 
 334   LIR_Opr lock = new_register(T_INT);
 335   LIR_Opr obj_temp = new_register(T_INT);
 336   set_no_result(x);
 337   monitor_exit(obj_temp, lock, syncTempOpr(), LIR_OprFact::illegalOpr, x->monitor_no());
 338 }
 339 
 340 // _ineg, _lneg, _fneg, _dneg
 341 void LIRGenerator::do_NegateOp(NegateOp* x) {
 342   LIRItem value(x->x(), this);
 343   value.set_destroys_register();
 344   value.load_item();
 345   LIR_Opr reg = rlock(x);
 346 
 347   LIR_Opr tmp = LIR_OprFact::illegalOpr;
 348 #ifdef _LP64
 349   if (UseAVX > 2 && !VM_Version::supports_avx512vl()) {
 350     if (x->type()->tag() == doubleTag) {
 351       tmp = new_register(T_DOUBLE);
 352       __ move(LIR_OprFact::doubleConst(-0.0), tmp);
 353     }
 354     else if (x->type()->tag() == floatTag) {
 355       tmp = new_register(T_FLOAT);
 356       __ move(LIR_OprFact::floatConst(-0.0), tmp);
 357     }
 358   }
 359 #endif
 360   __ negate(value.result(), reg, tmp);
 361 
 362   set_result(x, round_item(reg));
 363 }
 364 
 365 // for  _fadd, _fmul, _fsub, _fdiv, _frem
 366 //      _dadd, _dmul, _dsub, _ddiv, _drem
 367 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
 368   LIRItem left(x->x(),  this);
 369   LIRItem right(x->y(), this);
 370   LIRItem* left_arg  = &left;
 371   LIRItem* right_arg = &right;
 372   assert(!left.is_stack() || !right.is_stack(), "can't both be memory operands");
 373   bool must_load_both = (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem);
 374   if (left.is_register() || x->x()->type()->is_constant() || must_load_both) {
 375     left.load_item();
 376   } else {
 377     left.dont_load_item();
 378   }
 379 
 380 #ifndef _LP64
 381   // do not load right operand if it is a constant.  only 0 and 1 are
 382   // loaded because there are special instructions for loading them
 383   // without memory access (not needed for SSE2 instructions)
 384   bool must_load_right = false;
 385   if (right.is_constant()) {
 386     LIR_Const* c = right.result()->as_constant_ptr();
 387     assert(c != NULL, "invalid constant");
 388     assert(c->type() == T_FLOAT || c->type() == T_DOUBLE, "invalid type");
 389 
 390     if (c->type() == T_FLOAT) {
 391       must_load_right = UseSSE < 1 && (c->is_one_float() || c->is_zero_float());
 392     } else {
 393       must_load_right = UseSSE < 2 && (c->is_one_double() || c->is_zero_double());
 394     }
 395   }
 396 #endif // !LP64
 397 
 398   if (must_load_both) {
 399     // frem and drem destroy also right operand, so move it to a new register
 400     right.set_destroys_register();
 401     right.load_item();
 402   } else if (right.is_register()) {
 403     right.load_item();
 404 #ifndef _LP64
 405   } else if (must_load_right) {
 406     right.load_item();
 407 #endif // !LP64
 408   } else {
 409     right.dont_load_item();
 410   }
 411   LIR_Opr reg = rlock(x);
 412   LIR_Opr tmp = LIR_OprFact::illegalOpr;
 413   if (x->op() == Bytecodes::_dmul || x->op() == Bytecodes::_ddiv) {
 414     tmp = new_register(T_DOUBLE);
 415   }
 416 
 417 #ifdef _LP64
 418   if (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem) {
 419     // frem and drem are implemented as a direct call into the runtime.
 420     LIRItem left(x->x(), this);
 421     LIRItem right(x->y(), this);
 422 
 423     BasicType bt = as_BasicType(x->type());
 424     BasicTypeList signature(2);
 425     signature.append(bt);
 426     signature.append(bt);
 427     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
 428 
 429     const LIR_Opr result_reg = result_register_for(x->type());
 430     left.load_item_force(cc->at(0));
 431     right.load_item_force(cc->at(1));
 432 
 433     address entry = NULL;
 434     switch (x->op()) {
 435       case Bytecodes::_frem:
 436         entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
 437         break;
 438       case Bytecodes::_drem:
 439         entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
 440         break;
 441       default:
 442         ShouldNotReachHere();
 443     }
 444 
 445     LIR_Opr result = rlock_result(x);
 446     __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
 447     __ move(result_reg, result);
 448   } else {
 449     arithmetic_op_fpu(x->op(), reg, left.result(), right.result(), tmp);
 450     set_result(x, round_item(reg));
 451   }
 452 #else
 453   if ((UseSSE >= 1 && x->op() == Bytecodes::_frem) || (UseSSE >= 2 && x->op() == Bytecodes::_drem)) {
 454     // special handling for frem and drem: no SSE instruction, so must use FPU with temporary fpu stack slots
 455     LIR_Opr fpu0, fpu1;
 456     if (x->op() == Bytecodes::_frem) {
 457       fpu0 = LIR_OprFact::single_fpu(0);
 458       fpu1 = LIR_OprFact::single_fpu(1);
 459     } else {
 460       fpu0 = LIR_OprFact::double_fpu(0);
 461       fpu1 = LIR_OprFact::double_fpu(1);
 462     }
 463     __ move(right.result(), fpu1); // order of left and right operand is important!
 464     __ move(left.result(), fpu0);
 465     __ rem (fpu0, fpu1, fpu0);
 466     __ move(fpu0, reg);
 467 
 468   } else {
 469     arithmetic_op_fpu(x->op(), reg, left.result(), right.result(), tmp);
 470   }
 471   set_result(x, round_item(reg));
 472 #endif // _LP64
 473 }
 474 
 475 
 476 // for  _ladd, _lmul, _lsub, _ldiv, _lrem
 477 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
 478   if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem ) {
 479     // long division is implemented as a direct call into the runtime
 480     LIRItem left(x->x(), this);
 481     LIRItem right(x->y(), this);
 482 
 483     // the check for division by zero destroys the right operand
 484     right.set_destroys_register();
 485 
 486     BasicTypeList signature(2);
 487     signature.append(T_LONG);
 488     signature.append(T_LONG);
 489     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
 490 
 491     // check for division by zero (destroys registers of right operand!)
 492     CodeEmitInfo* info = state_for(x);
 493 
 494     const LIR_Opr result_reg = result_register_for(x->type());
 495     left.load_item_force(cc->at(1));
 496     right.load_item();
 497 
 498     __ move(right.result(), cc->at(0));
 499 
 500     __ cmp(lir_cond_equal, right.result(), LIR_OprFact::longConst(0));
 501     __ branch(lir_cond_equal, new DivByZeroStub(info));
 502 
 503     address entry = NULL;
 504     switch (x->op()) {
 505     case Bytecodes::_lrem:
 506       entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
 507       break; // check if dividend is 0 is done elsewhere
 508     case Bytecodes::_ldiv:
 509       entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
 510       break; // check if dividend is 0 is done elsewhere
 511     default:
 512       ShouldNotReachHere();
 513     }
 514 
 515     LIR_Opr result = rlock_result(x);
 516     __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
 517     __ move(result_reg, result);
 518   } else if (x->op() == Bytecodes::_lmul) {
 519     // missing test if instr is commutative and if we should swap
 520     LIRItem left(x->x(), this);
 521     LIRItem right(x->y(), this);
 522 
 523     // right register is destroyed by the long mul, so it must be
 524     // copied to a new register.
 525     right.set_destroys_register();
 526 
 527     left.load_item();
 528     right.load_item();
 529 
 530     LIR_Opr reg = FrameMap::long0_opr;
 531     arithmetic_op_long(x->op(), reg, left.result(), right.result(), NULL);
 532     LIR_Opr result = rlock_result(x);
 533     __ move(reg, result);
 534   } else {
 535     // missing test if instr is commutative and if we should swap
 536     LIRItem left(x->x(), this);
 537     LIRItem right(x->y(), this);
 538 
 539     left.load_item();
 540     // don't load constants to save register
 541     right.load_nonconstant();
 542     rlock_result(x);
 543     arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), NULL);
 544   }
 545 }
 546 
 547 
 548 
 549 // for: _iadd, _imul, _isub, _idiv, _irem
 550 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
 551   if (x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem) {
 552     // The requirements for division and modulo
 553     // input : rax,: dividend                         min_int
 554     //         reg: divisor   (may not be rax,/rdx)   -1
 555     //
 556     // output: rax,: quotient  (= rax, idiv reg)       min_int
 557     //         rdx: remainder (= rax, irem reg)       0
 558 
 559     // rax, and rdx will be destroyed
 560 
 561     // Note: does this invalidate the spec ???
 562     LIRItem right(x->y(), this);
 563     LIRItem left(x->x() , this);   // visit left second, so that the is_register test is valid
 564 
 565     // call state_for before load_item_force because state_for may
 566     // force the evaluation of other instructions that are needed for
 567     // correct debug info.  Otherwise the live range of the fix
 568     // register might be too long.
 569     CodeEmitInfo* info = state_for(x);
 570 
 571     left.load_item_force(divInOpr());
 572 
 573     right.load_item();
 574 
 575     LIR_Opr result = rlock_result(x);
 576     LIR_Opr result_reg;
 577     if (x->op() == Bytecodes::_idiv) {
 578       result_reg = divOutOpr();
 579     } else {
 580       result_reg = remOutOpr();
 581     }
 582 
 583     if (!ImplicitDiv0Checks) {
 584       __ cmp(lir_cond_equal, right.result(), LIR_OprFact::intConst(0));
 585       __ branch(lir_cond_equal, new DivByZeroStub(info));
 586       // Idiv/irem cannot trap (passing info would generate an assertion).
 587       info = NULL;
 588     }
 589     LIR_Opr tmp = FrameMap::rdx_opr; // idiv and irem use rdx in their implementation
 590     if (x->op() == Bytecodes::_irem) {
 591       __ irem(left.result(), right.result(), result_reg, tmp, info);
 592     } else if (x->op() == Bytecodes::_idiv) {
 593       __ idiv(left.result(), right.result(), result_reg, tmp, info);
 594     } else {
 595       ShouldNotReachHere();
 596     }
 597 
 598     __ move(result_reg, result);
 599   } else {
 600     // missing test if instr is commutative and if we should swap
 601     LIRItem left(x->x(),  this);
 602     LIRItem right(x->y(), this);
 603     LIRItem* left_arg = &left;
 604     LIRItem* right_arg = &right;
 605     if (x->is_commutative() && left.is_stack() && right.is_register()) {
 606       // swap them if left is real stack (or cached) and right is real register(not cached)
 607       left_arg = &right;
 608       right_arg = &left;
 609     }
 610 
 611     left_arg->load_item();
 612 
 613     // do not need to load right, as we can handle stack and constants
 614     if (x->op() == Bytecodes::_imul ) {
 615       // check if we can use shift instead
 616       bool use_constant = false;
 617       bool use_tmp = false;
 618       if (right_arg->is_constant()) {
 619         jint iconst = right_arg->get_jint_constant();
 620         if (iconst > 0 && iconst < max_jint) {
 621           if (is_power_of_2(iconst)) {
 622             use_constant = true;
 623           } else if (is_power_of_2(iconst - 1) || is_power_of_2(iconst + 1)) {
 624             use_constant = true;
 625             use_tmp = true;
 626           }
 627         }
 628       }
 629       if (use_constant) {
 630         right_arg->dont_load_item();
 631       } else {
 632         right_arg->load_item();
 633       }
 634       LIR_Opr tmp = LIR_OprFact::illegalOpr;
 635       if (use_tmp) {
 636         tmp = new_register(T_INT);
 637       }
 638       rlock_result(x);
 639 
 640       arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
 641     } else {
 642       right_arg->dont_load_item();
 643       rlock_result(x);
 644       LIR_Opr tmp = LIR_OprFact::illegalOpr;
 645       arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), tmp);
 646     }
 647   }
 648 }
 649 
 650 
 651 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
 652   // when an operand with use count 1 is the left operand, then it is
 653   // likely that no move for 2-operand-LIR-form is necessary
 654   if (x->is_commutative() && x->y()->as_Constant() == NULL && x->x()->use_count() > x->y()->use_count()) {
 655     x->swap_operands();
 656   }
 657 
 658   ValueTag tag = x->type()->tag();
 659   assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
 660   switch (tag) {
 661     case floatTag:
 662     case doubleTag:  do_ArithmeticOp_FPU(x);  return;
 663     case longTag:    do_ArithmeticOp_Long(x); return;
 664     case intTag:     do_ArithmeticOp_Int(x);  return;
 665     default:         ShouldNotReachHere();    return;
 666   }
 667 }
 668 
 669 
 670 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
 671 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
 672   // count must always be in rcx
 673   LIRItem value(x->x(), this);
 674   LIRItem count(x->y(), this);
 675 
 676   ValueTag elemType = x->type()->tag();
 677   bool must_load_count = !count.is_constant() || elemType == longTag;
 678   if (must_load_count) {
 679     // count for long must be in register
 680     count.load_item_force(shiftCountOpr());
 681   } else {
 682     count.dont_load_item();
 683   }
 684   value.load_item();
 685   LIR_Opr reg = rlock_result(x);
 686 
 687   shift_op(x->op(), reg, value.result(), count.result(), LIR_OprFact::illegalOpr);
 688 }
 689 
 690 
 691 // _iand, _land, _ior, _lor, _ixor, _lxor
 692 void LIRGenerator::do_LogicOp(LogicOp* x) {
 693   // when an operand with use count 1 is the left operand, then it is
 694   // likely that no move for 2-operand-LIR-form is necessary
 695   if (x->is_commutative() && x->y()->as_Constant() == NULL && x->x()->use_count() > x->y()->use_count()) {
 696     x->swap_operands();
 697   }
 698 
 699   LIRItem left(x->x(), this);
 700   LIRItem right(x->y(), this);
 701 
 702   left.load_item();
 703   right.load_nonconstant();
 704   LIR_Opr reg = rlock_result(x);
 705 
 706   logic_op(x->op(), reg, left.result(), right.result());
 707 }
 708 
 709 
 710 
 711 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
 712 void LIRGenerator::do_CompareOp(CompareOp* x) {
 713   LIRItem left(x->x(), this);
 714   LIRItem right(x->y(), this);
 715   ValueTag tag = x->x()->type()->tag();
 716   if (tag == longTag) {
 717     left.set_destroys_register();
 718   }
 719   left.load_item();
 720   right.load_item();
 721   LIR_Opr reg = rlock_result(x);
 722 
 723   if (x->x()->type()->is_float_kind()) {
 724     Bytecodes::Code code = x->op();
 725     __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
 726   } else if (x->x()->type()->tag() == longTag) {
 727     __ lcmp2int(left.result(), right.result(), reg);
 728   } else {
 729     Unimplemented();
 730   }
 731 }
 732 
 733 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
 734   LIR_Opr ill = LIR_OprFact::illegalOpr;  // for convenience
 735   if (is_reference_type(type)) {
 736     cmp_value.load_item_force(FrameMap::rax_oop_opr);
 737     new_value.load_item();
 738     __ cas_obj(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
 739   } else if (type == T_INT) {
 740     cmp_value.load_item_force(FrameMap::rax_opr);
 741     new_value.load_item();
 742     __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
 743   } else if (type == T_LONG) {
 744     cmp_value.load_item_force(FrameMap::long0_opr);
 745     new_value.load_item_force(FrameMap::long1_opr);
 746     __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
 747   } else {
 748     Unimplemented();
 749   }
 750   LIR_Opr result = new_register(T_INT);
 751   __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0),
 752            result, T_INT);
 753   return result;
 754 }
 755 
 756 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
 757   bool is_oop = is_reference_type(type);
 758   LIR_Opr result = new_register(type);
 759   value.load_item();
 760   // Because we want a 2-arg form of xchg and xadd
 761   __ move(value.result(), result);
 762   assert(type == T_INT || is_oop LP64_ONLY( || type == T_LONG ), "unexpected type");
 763   __ xchg(addr, result, result, LIR_OprFact::illegalOpr);
 764   return result;
 765 }
 766 
 767 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
 768   LIR_Opr result = new_register(type);
 769   value.load_item();
 770   // Because we want a 2-arg form of xchg and xadd
 771   __ move(value.result(), result);
 772   assert(type == T_INT LP64_ONLY( || type == T_LONG ), "unexpected type");
 773   __ xadd(addr, result, result, LIR_OprFact::illegalOpr);
 774   return result;
 775 }
 776 
 777 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
 778   assert(x->number_of_arguments() == 3, "wrong type");
 779   assert(UseFMA, "Needs FMA instructions support.");
 780   LIRItem value(x->argument_at(0), this);
 781   LIRItem value1(x->argument_at(1), this);
 782   LIRItem value2(x->argument_at(2), this);
 783 
 784   value2.set_destroys_register();
 785 
 786   value.load_item();
 787   value1.load_item();
 788   value2.load_item();
 789 
 790   LIR_Opr calc_input = value.result();
 791   LIR_Opr calc_input1 = value1.result();
 792   LIR_Opr calc_input2 = value2.result();
 793   LIR_Opr calc_result = rlock_result(x);
 794 
 795   switch (x->id()) {
 796   case vmIntrinsics::_fmaD:   __ fmad(calc_input, calc_input1, calc_input2, calc_result); break;
 797   case vmIntrinsics::_fmaF:   __ fmaf(calc_input, calc_input1, calc_input2, calc_result); break;
 798   default:                    ShouldNotReachHere();
 799   }
 800 
 801 }
 802 
 803 
 804 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
 805   assert(x->number_of_arguments() == 1 || (x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow), "wrong type");
 806 
 807   if (x->id() == vmIntrinsics::_dexp || x->id() == vmIntrinsics::_dlog ||
 808       x->id() == vmIntrinsics::_dpow || x->id() == vmIntrinsics::_dcos ||
 809       x->id() == vmIntrinsics::_dsin || x->id() == vmIntrinsics::_dtan ||
 810       x->id() == vmIntrinsics::_dlog10) {
 811     do_LibmIntrinsic(x);
 812     return;
 813   }
 814 
 815   LIRItem value(x->argument_at(0), this);
 816 
 817   bool use_fpu = false;
 818 #ifndef _LP64
 819   if (UseSSE < 2) {
 820     value.set_destroys_register();
 821   }
 822 #endif // !LP64
 823   value.load_item();
 824 
 825   LIR_Opr calc_input = value.result();
 826   LIR_Opr calc_result = rlock_result(x);
 827 
 828   LIR_Opr tmp = LIR_OprFact::illegalOpr;
 829 #ifdef _LP64
 830   if (UseAVX > 2 && (!VM_Version::supports_avx512vl()) &&
 831       (x->id() == vmIntrinsics::_dabs)) {
 832     tmp = new_register(T_DOUBLE);
 833     __ move(LIR_OprFact::doubleConst(-0.0), tmp);
 834   }
 835 #endif
 836 
 837   switch(x->id()) {
 838     case vmIntrinsics::_dabs:
 839       __ abs(calc_input, calc_result, tmp);
 840       break;
 841     case vmIntrinsics::_dsqrt:
 842     case vmIntrinsics::_dsqrt_strict:
 843       __ sqrt(calc_input, calc_result, LIR_OprFact::illegalOpr);
 844       break;
 845     default:
 846       ShouldNotReachHere();
 847   }
 848 
 849   if (use_fpu) {
 850     __ move(calc_result, x->operand());
 851   }
 852 }
 853 
 854 void LIRGenerator::do_LibmIntrinsic(Intrinsic* x) {
 855   LIRItem value(x->argument_at(0), this);
 856   value.set_destroys_register();
 857 
 858   LIR_Opr calc_result = rlock_result(x);
 859   LIR_Opr result_reg = result_register_for(x->type());
 860 
 861   CallingConvention* cc = NULL;
 862 
 863   if (x->id() == vmIntrinsics::_dpow) {
 864     LIRItem value1(x->argument_at(1), this);
 865 
 866     value1.set_destroys_register();
 867 
 868     BasicTypeList signature(2);
 869     signature.append(T_DOUBLE);
 870     signature.append(T_DOUBLE);
 871     cc = frame_map()->c_calling_convention(&signature);
 872     value.load_item_force(cc->at(0));
 873     value1.load_item_force(cc->at(1));
 874   } else {
 875     BasicTypeList signature(1);
 876     signature.append(T_DOUBLE);
 877     cc = frame_map()->c_calling_convention(&signature);
 878     value.load_item_force(cc->at(0));
 879   }
 880 
 881 #ifndef _LP64
 882   LIR_Opr tmp = FrameMap::fpu0_double_opr;
 883   result_reg = tmp;
 884   switch(x->id()) {
 885     case vmIntrinsics::_dexp:
 886       if (StubRoutines::dexp() != NULL) {
 887         __ call_runtime_leaf(StubRoutines::dexp(), getThreadTemp(), result_reg, cc->args());
 888       } else {
 889         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dexp), getThreadTemp(), result_reg, cc->args());
 890       }
 891       break;
 892     case vmIntrinsics::_dlog:
 893       if (StubRoutines::dlog() != NULL) {
 894         __ call_runtime_leaf(StubRoutines::dlog(), getThreadTemp(), result_reg, cc->args());
 895       } else {
 896         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog), getThreadTemp(), result_reg, cc->args());
 897       }
 898       break;
 899     case vmIntrinsics::_dlog10:
 900       if (StubRoutines::dlog10() != NULL) {
 901        __ call_runtime_leaf(StubRoutines::dlog10(), getThreadTemp(), result_reg, cc->args());
 902       } else {
 903         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog10), getThreadTemp(), result_reg, cc->args());
 904       }
 905       break;
 906     case vmIntrinsics::_dpow:
 907       if (StubRoutines::dpow() != NULL) {
 908         __ call_runtime_leaf(StubRoutines::dpow(), getThreadTemp(), result_reg, cc->args());
 909       } else {
 910         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), getThreadTemp(), result_reg, cc->args());
 911       }
 912       break;
 913     case vmIntrinsics::_dsin:
 914       if (VM_Version::supports_sse2() && StubRoutines::dsin() != NULL) {
 915         __ call_runtime_leaf(StubRoutines::dsin(), getThreadTemp(), result_reg, cc->args());
 916       } else {
 917         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dsin), getThreadTemp(), result_reg, cc->args());
 918       }
 919       break;
 920     case vmIntrinsics::_dcos:
 921       if (VM_Version::supports_sse2() && StubRoutines::dcos() != NULL) {
 922         __ call_runtime_leaf(StubRoutines::dcos(), getThreadTemp(), result_reg, cc->args());
 923       } else {
 924         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dcos), getThreadTemp(), result_reg, cc->args());
 925       }
 926       break;
 927     case vmIntrinsics::_dtan:
 928       if (StubRoutines::dtan() != NULL) {
 929         __ call_runtime_leaf(StubRoutines::dtan(), getThreadTemp(), result_reg, cc->args());
 930       } else {
 931         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtan), getThreadTemp(), result_reg, cc->args());
 932       }
 933       break;
 934     default:  ShouldNotReachHere();
 935   }
 936 #else
 937   switch (x->id()) {
 938     case vmIntrinsics::_dexp:
 939       if (StubRoutines::dexp() != NULL) {
 940         __ call_runtime_leaf(StubRoutines::dexp(), getThreadTemp(), result_reg, cc->args());
 941       } else {
 942         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dexp), getThreadTemp(), result_reg, cc->args());
 943       }
 944       break;
 945     case vmIntrinsics::_dlog:
 946       if (StubRoutines::dlog() != NULL) {
 947       __ call_runtime_leaf(StubRoutines::dlog(), getThreadTemp(), result_reg, cc->args());
 948       } else {
 949         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog), getThreadTemp(), result_reg, cc->args());
 950       }
 951       break;
 952     case vmIntrinsics::_dlog10:
 953       if (StubRoutines::dlog10() != NULL) {
 954       __ call_runtime_leaf(StubRoutines::dlog10(), getThreadTemp(), result_reg, cc->args());
 955       } else {
 956         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog10), getThreadTemp(), result_reg, cc->args());
 957       }
 958       break;
 959     case vmIntrinsics::_dpow:
 960        if (StubRoutines::dpow() != NULL) {
 961       __ call_runtime_leaf(StubRoutines::dpow(), getThreadTemp(), result_reg, cc->args());
 962       } else {
 963         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), getThreadTemp(), result_reg, cc->args());
 964       }
 965       break;
 966     case vmIntrinsics::_dsin:
 967       if (StubRoutines::dsin() != NULL) {
 968         __ call_runtime_leaf(StubRoutines::dsin(), getThreadTemp(), result_reg, cc->args());
 969       } else {
 970         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dsin), getThreadTemp(), result_reg, cc->args());
 971       }
 972       break;
 973     case vmIntrinsics::_dcos:
 974       if (StubRoutines::dcos() != NULL) {
 975         __ call_runtime_leaf(StubRoutines::dcos(), getThreadTemp(), result_reg, cc->args());
 976       } else {
 977         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dcos), getThreadTemp(), result_reg, cc->args());
 978       }
 979       break;
 980     case vmIntrinsics::_dtan:
 981        if (StubRoutines::dtan() != NULL) {
 982       __ call_runtime_leaf(StubRoutines::dtan(), getThreadTemp(), result_reg, cc->args());
 983       } else {
 984         __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtan), getThreadTemp(), result_reg, cc->args());
 985       }
 986       break;
 987     default:  ShouldNotReachHere();
 988   }
 989 #endif // _LP64
 990   __ move(result_reg, calc_result);
 991 }
 992 
 993 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
 994   assert(x->number_of_arguments() == 5, "wrong type");
 995 
 996   // Make all state_for calls early since they can emit code
 997   CodeEmitInfo* info = state_for(x, x->state());
 998 
 999   LIRItem src(x->argument_at(0), this);
1000   LIRItem src_pos(x->argument_at(1), this);
1001   LIRItem dst(x->argument_at(2), this);
1002   LIRItem dst_pos(x->argument_at(3), this);
1003   LIRItem length(x->argument_at(4), this);
1004 
1005   // operands for arraycopy must use fixed registers, otherwise
1006   // LinearScan will fail allocation (because arraycopy always needs a
1007   // call)
1008 
1009 #ifndef _LP64
1010   src.load_item_force     (FrameMap::rcx_oop_opr);
1011   src_pos.load_item_force (FrameMap::rdx_opr);
1012   dst.load_item_force     (FrameMap::rax_oop_opr);
1013   dst_pos.load_item_force (FrameMap::rbx_opr);
1014   length.load_item_force  (FrameMap::rdi_opr);
1015   LIR_Opr tmp =           (FrameMap::rsi_opr);
1016 #else
1017 
1018   // The java calling convention will give us enough registers
1019   // so that on the stub side the args will be perfect already.
1020   // On the other slow/special case side we call C and the arg
1021   // positions are not similar enough to pick one as the best.
1022   // Also because the java calling convention is a "shifted" version
1023   // of the C convention we can process the java args trivially into C
1024   // args without worry of overwriting during the xfer
1025 
1026   src.load_item_force     (FrameMap::as_oop_opr(j_rarg0));
1027   src_pos.load_item_force (FrameMap::as_opr(j_rarg1));
1028   dst.load_item_force     (FrameMap::as_oop_opr(j_rarg2));
1029   dst_pos.load_item_force (FrameMap::as_opr(j_rarg3));
1030   length.load_item_force  (FrameMap::as_opr(j_rarg4));
1031 
1032   LIR_Opr tmp =           FrameMap::as_opr(j_rarg5);
1033 #endif // LP64
1034 
1035   set_no_result(x);
1036 
1037   int flags;
1038   ciArrayKlass* expected_type;
1039   arraycopy_helper(x, &flags, &expected_type);
1040 
1041   __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(), tmp, expected_type, flags, info); // does add_safepoint
1042 }
1043 
1044 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
1045   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
1046   // Make all state_for calls early since they can emit code
1047   LIR_Opr result = rlock_result(x);
1048   int flags = 0;
1049   switch (x->id()) {
1050     case vmIntrinsics::_updateCRC32: {
1051       LIRItem crc(x->argument_at(0), this);
1052       LIRItem val(x->argument_at(1), this);
1053       // val is destroyed by update_crc32
1054       val.set_destroys_register();
1055       crc.load_item();
1056       val.load_item();
1057       __ update_crc32(crc.result(), val.result(), result);
1058       break;
1059     }
1060     case vmIntrinsics::_updateBytesCRC32:
1061     case vmIntrinsics::_updateByteBufferCRC32: {
1062       bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32);
1063 
1064       LIRItem crc(x->argument_at(0), this);
1065       LIRItem buf(x->argument_at(1), this);
1066       LIRItem off(x->argument_at(2), this);
1067       LIRItem len(x->argument_at(3), this);
1068       buf.load_item();
1069       off.load_nonconstant();
1070 
1071       LIR_Opr index = off.result();
1072       int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
1073       if(off.result()->is_constant()) {
1074         index = LIR_OprFact::illegalOpr;
1075        offset += off.result()->as_jint();
1076       }
1077       LIR_Opr base_op = buf.result();
1078 
1079 #ifndef _LP64
1080       if (!is_updateBytes) { // long b raw address
1081          base_op = new_register(T_INT);
1082          __ convert(Bytecodes::_l2i, buf.result(), base_op);
1083       }
1084 #else
1085       if (index->is_valid()) {
1086         LIR_Opr tmp = new_register(T_LONG);
1087         __ convert(Bytecodes::_i2l, index, tmp);
1088         index = tmp;
1089       }
1090 #endif
1091 
1092       LIR_Address* a = new LIR_Address(base_op,
1093                                        index,
1094                                        offset,
1095                                        T_BYTE);
1096       BasicTypeList signature(3);
1097       signature.append(T_INT);
1098       signature.append(T_ADDRESS);
1099       signature.append(T_INT);
1100       CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1101       const LIR_Opr result_reg = result_register_for(x->type());
1102 
1103       LIR_Opr addr = new_pointer_register();
1104       __ leal(LIR_OprFact::address(a), addr);
1105 
1106       crc.load_item_force(cc->at(0));
1107       __ move(addr, cc->at(1));
1108       len.load_item_force(cc->at(2));
1109 
1110       __ call_runtime_leaf(StubRoutines::updateBytesCRC32(), getThreadTemp(), result_reg, cc->args());
1111       __ move(result_reg, result);
1112 
1113       break;
1114     }
1115     default: {
1116       ShouldNotReachHere();
1117     }
1118   }
1119 }
1120 
1121 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
1122   Unimplemented();
1123 }
1124 
1125 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
1126   assert(UseVectorizedMismatchIntrinsic, "need AVX instruction support");
1127 
1128   // Make all state_for calls early since they can emit code
1129   LIR_Opr result = rlock_result(x);
1130 
1131   LIRItem a(x->argument_at(0), this); // Object
1132   LIRItem aOffset(x->argument_at(1), this); // long
1133   LIRItem b(x->argument_at(2), this); // Object
1134   LIRItem bOffset(x->argument_at(3), this); // long
1135   LIRItem length(x->argument_at(4), this); // int
1136   LIRItem log2ArrayIndexScale(x->argument_at(5), this); // int
1137 
1138   a.load_item();
1139   aOffset.load_nonconstant();
1140   b.load_item();
1141   bOffset.load_nonconstant();
1142 
1143   long constant_aOffset = 0;
1144   LIR_Opr result_aOffset = aOffset.result();
1145   if (result_aOffset->is_constant()) {
1146     constant_aOffset = result_aOffset->as_jlong();
1147     result_aOffset = LIR_OprFact::illegalOpr;
1148   }
1149   LIR_Opr result_a = a.result();
1150 
1151   long constant_bOffset = 0;
1152   LIR_Opr result_bOffset = bOffset.result();
1153   if (result_bOffset->is_constant()) {
1154     constant_bOffset = result_bOffset->as_jlong();
1155     result_bOffset = LIR_OprFact::illegalOpr;
1156   }
1157   LIR_Opr result_b = b.result();
1158 
1159 #ifndef _LP64
1160   result_a = new_register(T_INT);
1161   __ convert(Bytecodes::_l2i, a.result(), result_a);
1162   result_b = new_register(T_INT);
1163   __ convert(Bytecodes::_l2i, b.result(), result_b);
1164 #endif
1165 
1166 
1167   LIR_Address* addr_a = new LIR_Address(result_a,
1168                                         result_aOffset,
1169                                         constant_aOffset,
1170                                         T_BYTE);
1171 
1172   LIR_Address* addr_b = new LIR_Address(result_b,
1173                                         result_bOffset,
1174                                         constant_bOffset,
1175                                         T_BYTE);
1176 
1177   BasicTypeList signature(4);
1178   signature.append(T_ADDRESS);
1179   signature.append(T_ADDRESS);
1180   signature.append(T_INT);
1181   signature.append(T_INT);
1182   CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1183   const LIR_Opr result_reg = result_register_for(x->type());
1184 
1185   LIR_Opr ptr_addr_a = new_pointer_register();
1186   __ leal(LIR_OprFact::address(addr_a), ptr_addr_a);
1187 
1188   LIR_Opr ptr_addr_b = new_pointer_register();
1189   __ leal(LIR_OprFact::address(addr_b), ptr_addr_b);
1190 
1191   __ move(ptr_addr_a, cc->at(0));
1192   __ move(ptr_addr_b, cc->at(1));
1193   length.load_item_force(cc->at(2));
1194   log2ArrayIndexScale.load_item_force(cc->at(3));
1195 
1196   __ call_runtime_leaf(StubRoutines::vectorizedMismatch(), getThreadTemp(), result_reg, cc->args());
1197   __ move(result_reg, result);
1198 }
1199 
1200 // _i2l, _i2f, _i2d, _l2i, _l2f, _l2d, _f2i, _f2l, _f2d, _d2i, _d2l, _d2f
1201 // _i2b, _i2c, _i2s
1202 LIR_Opr fixed_register_for(BasicType type) {
1203   switch (type) {
1204     case T_FLOAT:  return FrameMap::fpu0_float_opr;
1205     case T_DOUBLE: return FrameMap::fpu0_double_opr;
1206     case T_INT:    return FrameMap::rax_opr;
1207     case T_LONG:   return FrameMap::long0_opr;
1208     default:       ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
1209   }
1210 }
1211 
1212 void LIRGenerator::do_Convert(Convert* x) {
1213 #ifdef _LP64
1214   LIRItem value(x->value(), this);
1215   value.load_item();
1216   LIR_Opr input = value.result();
1217   LIR_Opr result = rlock(x);
1218   __ convert(x->op(), input, result);
1219   assert(result->is_virtual(), "result must be virtual register");
1220   set_result(x, result);
1221 #else
1222   // flags that vary for the different operations and different SSE-settings
1223   bool fixed_input = false, fixed_result = false, round_result = false, needs_stub = false;
1224 
1225   switch (x->op()) {
1226     case Bytecodes::_i2l: // fall through
1227     case Bytecodes::_l2i: // fall through
1228     case Bytecodes::_i2b: // fall through
1229     case Bytecodes::_i2c: // fall through
1230     case Bytecodes::_i2s: fixed_input = false;       fixed_result = false;       round_result = false;      needs_stub = false; break;
1231 
1232     case Bytecodes::_f2d: fixed_input = UseSSE == 1; fixed_result = false;       round_result = false;      needs_stub = false; break;
1233     case Bytecodes::_d2f: fixed_input = false;       fixed_result = UseSSE == 1; round_result = UseSSE < 1; needs_stub = false; break;
1234     case Bytecodes::_i2f: fixed_input = false;       fixed_result = false;       round_result = UseSSE < 1; needs_stub = false; break;
1235     case Bytecodes::_i2d: fixed_input = false;       fixed_result = false;       round_result = false;      needs_stub = false; break;
1236     case Bytecodes::_f2i: fixed_input = false;       fixed_result = false;       round_result = false;      needs_stub = true;  break;
1237     case Bytecodes::_d2i: fixed_input = false;       fixed_result = false;       round_result = false;      needs_stub = true;  break;
1238     case Bytecodes::_l2f: fixed_input = false;       fixed_result = UseSSE >= 1; round_result = UseSSE < 1; needs_stub = false; break;
1239     case Bytecodes::_l2d: fixed_input = false;       fixed_result = UseSSE >= 2; round_result = UseSSE < 2; needs_stub = false; break;
1240     case Bytecodes::_f2l: fixed_input = true;        fixed_result = true;        round_result = false;      needs_stub = false; break;
1241     case Bytecodes::_d2l: fixed_input = true;        fixed_result = true;        round_result = false;      needs_stub = false; break;
1242     default: ShouldNotReachHere();
1243   }
1244 
1245   LIRItem value(x->value(), this);
1246   value.load_item();
1247   LIR_Opr input = value.result();
1248   LIR_Opr result = rlock(x);
1249 
1250   // arguments of lir_convert
1251   LIR_Opr conv_input = input;
1252   LIR_Opr conv_result = result;
1253   ConversionStub* stub = NULL;
1254 
1255   if (fixed_input) {
1256     conv_input = fixed_register_for(input->type());
1257     __ move(input, conv_input);
1258   }
1259 
1260   assert(fixed_result == false || round_result == false, "cannot set both");
1261   if (fixed_result) {
1262     conv_result = fixed_register_for(result->type());
1263   } else if (round_result) {
1264     result = new_register(result->type());
1265     set_vreg_flag(result, must_start_in_memory);
1266   }
1267 
1268   if (needs_stub) {
1269     stub = new ConversionStub(x->op(), conv_input, conv_result);
1270   }
1271 
1272   __ convert(x->op(), conv_input, conv_result, stub);
1273 
1274   if (result != conv_result) {
1275     __ move(conv_result, result);
1276   }
1277 
1278   assert(result->is_virtual(), "result must be virtual register");
1279   set_result(x, result);
1280 #endif // _LP64
1281 }
1282 
1283 
1284 void LIRGenerator::do_NewInstance(NewInstance* x) {
1285   print_if_not_loaded(x);
1286 
1287   CodeEmitInfo* info = state_for(x, x->state());
1288   LIR_Opr reg = result_register_for(x->type());
1289   new_instance(reg, x->klass(), x->is_unresolved(),
1290                        FrameMap::rcx_oop_opr,
1291                        FrameMap::rdi_oop_opr,
1292                        FrameMap::rsi_oop_opr,
1293                        LIR_OprFact::illegalOpr,
1294                        FrameMap::rdx_metadata_opr, info);
1295   LIR_Opr result = rlock_result(x);
1296   __ move(reg, result);
1297 }
1298 
1299 
1300 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
1301   CodeEmitInfo* info = state_for(x, x->state());
1302 
1303   LIRItem length(x->length(), this);
1304   length.load_item_force(FrameMap::rbx_opr);
1305 
1306   LIR_Opr reg = result_register_for(x->type());
1307   LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1308   LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1309   LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1310   LIR_Opr tmp4 = reg;
1311   LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1312   LIR_Opr len = length.result();
1313   BasicType elem_type = x->elt_type();
1314 
1315   __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
1316 
1317   CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
1318   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path);
1319 
1320   LIR_Opr result = rlock_result(x);
1321   __ move(reg, result);
1322 }
1323 
1324 
1325 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
1326   LIRItem length(x->length(), this);
1327   // in case of patching (i.e., object class is not yet loaded), we need to reexecute the instruction
1328   // and therefore provide the state before the parameters have been consumed
1329   CodeEmitInfo* patching_info = NULL;
1330   if (!x->klass()->is_loaded() || PatchALot) {
1331     patching_info =  state_for(x, x->state_before());
1332   }
1333 
1334   CodeEmitInfo* info = state_for(x, x->state());
1335 
1336   const LIR_Opr reg = result_register_for(x->type());
1337   LIR_Opr tmp1 = FrameMap::rcx_oop_opr;
1338   LIR_Opr tmp2 = FrameMap::rsi_oop_opr;
1339   LIR_Opr tmp3 = FrameMap::rdi_oop_opr;
1340   LIR_Opr tmp4 = reg;
1341   LIR_Opr klass_reg = FrameMap::rdx_metadata_opr;
1342 
1343   length.load_item_force(FrameMap::rbx_opr);
1344   LIR_Opr len = length.result();
1345 
1346   CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info);
1347   ciKlass* obj = (ciKlass*) ciObjArrayKlass::make(x->klass());
1348   if (obj == ciEnv::unloaded_ciobjarrayklass()) {
1349     BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
1350   }
1351   klass2reg_with_patching(klass_reg, obj, patching_info);
1352   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path);
1353 
1354   LIR_Opr result = rlock_result(x);
1355   __ move(reg, result);
1356 }
1357 
1358 
1359 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1360   Values* dims = x->dims();
1361   int i = dims->length();
1362   LIRItemList* items = new LIRItemList(i, i, NULL);
1363   while (i-- > 0) {
1364     LIRItem* size = new LIRItem(dims->at(i), this);
1365     items->at_put(i, size);
1366   }
1367 
1368   // Evaluate state_for early since it may emit code.
1369   CodeEmitInfo* patching_info = NULL;
1370   if (!x->klass()->is_loaded() || PatchALot) {
1371     patching_info = state_for(x, x->state_before());
1372 
1373     // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1374     // clone all handlers (NOTE: Usually this is handled transparently
1375     // by the CodeEmitInfo cloning logic in CodeStub constructors but
1376     // is done explicitly here because a stub isn't being used).
1377     x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1378   }
1379   CodeEmitInfo* info = state_for(x, x->state());
1380 
1381   i = dims->length();
1382   while (i-- > 0) {
1383     LIRItem* size = items->at(i);
1384     size->load_nonconstant();
1385 
1386     store_stack_parameter(size->result(), in_ByteSize(i*4));
1387   }
1388 
1389   LIR_Opr klass_reg = FrameMap::rax_metadata_opr;
1390   klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1391 
1392   LIR_Opr rank = FrameMap::rbx_opr;
1393   __ move(LIR_OprFact::intConst(x->rank()), rank);
1394   LIR_Opr varargs = FrameMap::rcx_opr;
1395   __ move(FrameMap::rsp_opr, varargs);
1396   LIR_OprList* args = new LIR_OprList(3);
1397   args->append(klass_reg);
1398   args->append(rank);
1399   args->append(varargs);
1400   LIR_Opr reg = result_register_for(x->type());
1401   __ call_runtime(Runtime1::entry_for(Runtime1::new_multi_array_id),
1402                   LIR_OprFact::illegalOpr,
1403                   reg, args, info);
1404 
1405   LIR_Opr result = rlock_result(x);
1406   __ move(reg, result);
1407 }
1408 
1409 
1410 void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1411   // nothing to do for now
1412 }
1413 
1414 
1415 void LIRGenerator::do_CheckCast(CheckCast* x) {
1416   LIRItem obj(x->obj(), this);
1417 
1418   CodeEmitInfo* patching_info = NULL;
1419   if (!x->klass()->is_loaded() || (PatchALot && !x->is_incompatible_class_change_check() && !x->is_invokespecial_receiver_check())) {
1420     // must do this before locking the destination register as an oop register,
1421     // and before the obj is loaded (the latter is for deoptimization)
1422     patching_info = state_for(x, x->state_before());
1423   }
1424   obj.load_item();
1425 
1426   // info for exceptions
1427   CodeEmitInfo* info_for_exception =
1428       (x->needs_exception_state() ? state_for(x) :
1429                                     state_for(x, x->state_before(), true /*ignore_xhandler*/));
1430 
1431   CodeStub* stub;
1432   if (x->is_incompatible_class_change_check()) {
1433     assert(patching_info == NULL, "can't patch this");
1434     stub = new SimpleExceptionStub(Runtime1::throw_incompatible_class_change_error_id, LIR_OprFact::illegalOpr, info_for_exception);
1435   } else if (x->is_invokespecial_receiver_check()) {
1436     assert(patching_info == NULL, "can't patch this");
1437     stub = new DeoptimizeStub(info_for_exception, Deoptimization::Reason_class_check, Deoptimization::Action_none);
1438   } else {
1439     stub = new SimpleExceptionStub(Runtime1::throw_class_cast_exception_id, obj.result(), info_for_exception);
1440   }
1441   LIR_Opr reg = rlock_result(x);
1442   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1443   if (!x->klass()->is_loaded() || UseCompressedClassPointers) {
1444     tmp3 = new_register(objectType);
1445   }
1446   __ checkcast(reg, obj.result(), x->klass(),
1447                new_register(objectType), new_register(objectType), tmp3,
1448                x->direct_compare(), info_for_exception, patching_info, stub,
1449                x->profiled_method(), x->profiled_bci());
1450 }
1451 
1452 
1453 void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1454   LIRItem obj(x->obj(), this);
1455 
1456   // result and test object may not be in same register
1457   LIR_Opr reg = rlock_result(x);
1458   CodeEmitInfo* patching_info = NULL;
1459   if ((!x->klass()->is_loaded() || PatchALot)) {
1460     // must do this before locking the destination register as an oop register
1461     patching_info = state_for(x, x->state_before());
1462   }
1463   obj.load_item();
1464   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1465   if (!x->klass()->is_loaded() || UseCompressedClassPointers) {
1466     tmp3 = new_register(objectType);
1467   }
1468   __ instanceof(reg, obj.result(), x->klass(),
1469                 new_register(objectType), new_register(objectType), tmp3,
1470                 x->direct_compare(), patching_info, x->profiled_method(), x->profiled_bci());
1471 }
1472 
1473 
1474 void LIRGenerator::do_If(If* x) {
1475   assert(x->number_of_sux() == 2, "inconsistency");
1476   ValueTag tag = x->x()->type()->tag();
1477   bool is_safepoint = x->is_safepoint();
1478 
1479   If::Condition cond = x->cond();
1480 
1481   LIRItem xitem(x->x(), this);
1482   LIRItem yitem(x->y(), this);
1483   LIRItem* xin = &xitem;
1484   LIRItem* yin = &yitem;
1485 
1486   if (tag == longTag) {
1487     // for longs, only conditions "eql", "neq", "lss", "geq" are valid;
1488     // mirror for other conditions
1489     if (cond == If::gtr || cond == If::leq) {
1490       cond = Instruction::mirror(cond);
1491       xin = &yitem;
1492       yin = &xitem;
1493     }
1494     xin->set_destroys_register();
1495   }
1496   xin->load_item();
1497   if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 && (cond == If::eql || cond == If::neq)) {
1498     // inline long zero
1499     yin->dont_load_item();
1500   } else if (tag == longTag || tag == floatTag || tag == doubleTag) {
1501     // longs cannot handle constants at right side
1502     yin->load_item();
1503   } else {
1504     yin->dont_load_item();
1505   }
1506 
1507   LIR_Opr left = xin->result();
1508   LIR_Opr right = yin->result();
1509 
1510   set_no_result(x);
1511 
1512   // add safepoint before generating condition code so it can be recomputed
1513   if (x->is_safepoint()) {
1514     // increment backedge counter if needed
1515     increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1516         x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1517     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
1518   }
1519 
1520   __ cmp(lir_cond(cond), left, right);
1521   // Generate branch profiling. Profiling code doesn't kill flags.
1522   profile_branch(x, cond);
1523   move_to_phi(x->state());
1524   if (x->x()->type()->is_float_kind()) {
1525     __ branch(lir_cond(cond), x->tsux(), x->usux());
1526   } else {
1527     __ branch(lir_cond(cond), x->tsux());
1528   }
1529   assert(x->default_sux() == x->fsux(), "wrong destination above");
1530   __ jump(x->default_sux());
1531 }
1532 
1533 
1534 LIR_Opr LIRGenerator::getThreadPointer() {
1535 #ifdef _LP64
1536   return FrameMap::as_pointer_opr(r15_thread);
1537 #else
1538   LIR_Opr result = new_register(T_INT);
1539   __ get_thread(result);
1540   return result;
1541 #endif //
1542 }
1543 
1544 void LIRGenerator::trace_block_entry(BlockBegin* block) {
1545   store_stack_parameter(LIR_OprFact::intConst(block->block_id()), in_ByteSize(0));
1546   LIR_OprList* args = new LIR_OprList();
1547   address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1548   __ call_runtime_leaf(func, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, args);
1549 }
1550 
1551 
1552 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1553                                         CodeEmitInfo* info) {
1554   if (address->type() == T_LONG) {
1555     address = new LIR_Address(address->base(),
1556                               address->index(), address->scale(),
1557                               address->disp(), T_DOUBLE);
1558     // Transfer the value atomically by using FP moves.  This means
1559     // the value has to be moved between CPU and FPU registers.  It
1560     // always has to be moved through spill slot since there's no
1561     // quick way to pack the value into an SSE register.
1562     LIR_Opr temp_double = new_register(T_DOUBLE);
1563     LIR_Opr spill = new_register(T_LONG);
1564     set_vreg_flag(spill, must_start_in_memory);
1565     __ move(value, spill);
1566     __ volatile_move(spill, temp_double, T_LONG);
1567     __ volatile_move(temp_double, LIR_OprFact::address(address), T_LONG, info);
1568   } else {
1569     __ store(value, address, info);
1570   }
1571 }
1572 
1573 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1574                                        CodeEmitInfo* info) {
1575   if (address->type() == T_LONG) {
1576     address = new LIR_Address(address->base(),
1577                               address->index(), address->scale(),
1578                               address->disp(), T_DOUBLE);
1579     // Transfer the value atomically by using FP moves.  This means
1580     // the value has to be moved between CPU and FPU registers.  In
1581     // SSE0 and SSE1 mode it has to be moved through spill slot but in
1582     // SSE2+ mode it can be moved directly.
1583     LIR_Opr temp_double = new_register(T_DOUBLE);
1584     __ volatile_move(LIR_OprFact::address(address), temp_double, T_LONG, info);
1585     __ volatile_move(temp_double, result, T_LONG);
1586 #ifndef _LP64
1587     if (UseSSE < 2) {
1588       // no spill slot needed in SSE2 mode because xmm->cpu register move is possible
1589       set_vreg_flag(result, must_start_in_memory);
1590     }
1591 #endif // !LP64
1592   } else {
1593     __ load(address, result, info);
1594   }
1595 }