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