1 /*
   2  * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "c1/c1_Compilation.hpp"
  29 #include "c1/c1_FrameMap.hpp"
  30 #include "c1/c1_Instruction.hpp"
  31 #include "c1/c1_LIRAssembler.hpp"
  32 #include "c1/c1_LIRGenerator.hpp"
  33 #include "c1/c1_Runtime1.hpp"
  34 #include "c1/c1_ValueStack.hpp"
  35 #include "ci/ciArray.hpp"
  36 #include "ci/ciInstanceKlass.hpp"
  37 #include "ci/ciObjArrayKlass.hpp"
  38 #include "ci/ciTypeArrayKlass.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "utilities/powerOfTwo.hpp"
  42 #include "vmreg_riscv.inline.hpp"
  43 
  44 #ifdef ASSERT
  45 #define __ gen()->lir(__FILE__, __LINE__)->
  46 #else
  47 #define __ gen()->lir()->
  48 #endif
  49 
  50 // Item will be loaded into a byte register; Intel only
  51 void LIRItem::load_byte_item() {
  52   load_item();
  53 }
  54 
  55 
  56 void LIRItem::load_nonconstant() {
  57   LIR_Opr r = value()->operand();
  58   if (r->is_constant()) {
  59     _result = r;
  60   } else {
  61     load_item();
  62   }
  63 }
  64 
  65 //--------------------------------------------------------------
  66 //               LIRGenerator
  67 //--------------------------------------------------------------
  68 
  69 
  70 LIR_Opr LIRGenerator::exceptionOopOpr() { return FrameMap::r10_oop_opr; }
  71 LIR_Opr LIRGenerator::exceptionPcOpr()  { return FrameMap::r13_opr; }
  72 LIR_Opr LIRGenerator::divInOpr()        { Unimplemented(); return LIR_OprFact::illegalOpr; }
  73 LIR_Opr LIRGenerator::divOutOpr()       { Unimplemented(); return LIR_OprFact::illegalOpr; }
  74 LIR_Opr LIRGenerator::remOutOpr()       { Unimplemented(); return LIR_OprFact::illegalOpr; }
  75 LIR_Opr LIRGenerator::shiftCountOpr()   { Unimplemented(); return LIR_OprFact::illegalOpr; }
  76 LIR_Opr LIRGenerator::syncLockOpr()     { return new_register(T_INT); }
  77 LIR_Opr LIRGenerator::syncTempOpr()     { return FrameMap::r10_opr; }
  78 LIR_Opr LIRGenerator::getThreadTemp()   { return LIR_OprFact::illegalOpr; }
  79 
  80 
  81 LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
  82   LIR_Opr opr;
  83   switch (type->tag()) {
  84     case intTag:     opr = FrameMap::r10_opr;          break;
  85     case objectTag:  opr = FrameMap::r10_oop_opr;      break;
  86     case longTag:    opr = FrameMap::long10_opr;       break;
  87     case floatTag:   opr = FrameMap::fpu10_float_opr;  break;
  88     case doubleTag:  opr = FrameMap::fpu10_double_opr; break;
  89 
  90     case addressTag: // fall through
  91     default:
  92       ShouldNotReachHere();
  93       return LIR_OprFact::illegalOpr;
  94   }
  95 
  96   assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
  97   return opr;
  98 }
  99 
 100 
 101 LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
 102   LIR_Opr reg = new_register(T_INT);
 103   set_vreg_flag(reg, LIRGenerator::byte_reg);
 104   return reg;
 105 }
 106 
 107 
 108 //--------- loading items into registers --------------------------------
 109 
 110 
 111 bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
 112   if (v->type()->as_IntConstant() != nullptr) {
 113     return v->type()->as_IntConstant()->value() == 0;
 114   } else if (v->type()->as_LongConstant() != nullptr) {
 115     return v->type()->as_LongConstant()->value() == 0;
 116   } else if (v->type()->as_ObjectConstant() != nullptr) {
 117     return v->type()->as_ObjectConstant()->value()->is_null_object();
 118   } else if (v->type()->as_FloatConstant() != nullptr) {
 119     return jint_cast(v->type()->as_FloatConstant()->value()) == 0.0f;
 120   } else if (v->type()->as_DoubleConstant() != nullptr) {
 121     return jlong_cast(v->type()->as_DoubleConstant()->value()) == 0.0;
 122   }
 123   return false;
 124 }
 125 
 126 bool LIRGenerator::can_inline_as_constant(Value v) const {
 127   if (v->type()->as_IntConstant() != nullptr) {
 128     int value = v->type()->as_IntConstant()->value();
 129     // "-value" must be defined for value may be used for sub
 130     return Assembler::is_simm12(value) && Assembler::is_simm12(- value);
 131   } else if (v->type()->as_ObjectConstant() != nullptr) {
 132     return v->type()->as_ObjectConstant()->value()->is_null_object();
 133   } else if (v->type()->as_LongConstant() != nullptr) {
 134     long value = v->type()->as_LongConstant()->value();
 135     // "-value" must be defined for value may be used for sub
 136     return Assembler::is_simm12(value) && Assembler::is_simm12(- value);
 137   } else if (v->type()->as_FloatConstant() != nullptr) {
 138     return v->type()->as_FloatConstant()->value() == 0.0f;
 139   } else if (v->type()->as_DoubleConstant() != nullptr) {
 140     return v->type()->as_DoubleConstant()->value() == 0.0;
 141   }
 142   return false;
 143 }
 144 
 145 bool LIRGenerator::can_inline_as_constant(LIR_Const* c) const {
 146   if (c->as_constant() != nullptr) {
 147     long constant = 0;
 148     switch (c->type()) {
 149       case T_INT:  constant = c->as_jint();   break;
 150       case T_LONG: constant = c->as_jlong();  break;
 151       default:     return false;
 152     }
 153     // "-constant" must be defined for c may be used for sub
 154     return Assembler::is_simm12(constant) && Assembler::is_simm12(- constant);
 155   }
 156   return false;
 157 }
 158 
 159 LIR_Opr LIRGenerator::safepoint_poll_register() {
 160   return LIR_OprFact::illegalOpr;
 161 }
 162 
 163 LIR_Address* LIRGenerator::generate_address(LIR_Opr base, LIR_Opr index,
 164                                             int shift, int disp, BasicType type) {
 165   assert(base->is_register(), "must be");
 166 
 167   if (index->is_constant()) {
 168     LIR_Const *constant = index->as_constant_ptr();
 169     jlong c;
 170     if (constant->type() == T_INT) {
 171       c = (jlong(index->as_jint()) << shift) + disp;
 172     } else {
 173       assert(constant->type() == T_LONG, "should be");
 174       c = (index->as_jlong() << shift) + disp;
 175     }
 176     if ((jlong)((jint)c) == c) {
 177       return new LIR_Address(base, (jint)c, type);
 178     } else {
 179       LIR_Opr tmp = new_register(T_LONG);
 180       __ move(index, tmp);
 181       return new LIR_Address(base, tmp, type);
 182     }
 183   }
 184 
 185   return new LIR_Address(base, index, (LIR_Address::Scale)shift, disp, type);
 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   int elem_size = type2aelembytes(type);
 192   int shift = exact_log2(elem_size);
 193   return generate_address(array_opr, index_opr, shift, offset_in_bytes, type);
 194 }
 195 
 196 LIR_Opr LIRGenerator::load_immediate(jlong x, BasicType type) {
 197   LIR_Opr r;
 198   switch (type) {
 199     case T_LONG:
 200       r = LIR_OprFact::longConst(x);
 201       break;
 202     case T_INT:
 203       r = LIR_OprFact::intConst(checked_cast<jint>(x));
 204       break;
 205     default:
 206       ShouldNotReachHere();
 207   }
 208   return r;
 209 }
 210 
 211 void LIRGenerator::increment_counter(address counter, BasicType type, int step) {
 212   LIR_Opr pointer = new_pointer_register();
 213   __ move(LIR_OprFact::intptrConst(counter), pointer);
 214   LIR_Address* addr = new LIR_Address(pointer, type);
 215   increment_counter(addr, step);
 216 }
 217 
 218 void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
 219   LIR_Opr reg = new_register(addr->type());
 220   __ load(addr, reg);
 221   __ add(reg, load_immediate(step, addr->type()), reg);
 222   __ store(reg, addr);
 223 }
 224 
 225 void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
 226   LIR_Opr reg = new_register(T_INT);
 227   __ load(generate_address(base, disp, T_INT), reg, info);
 228   __ cmp(condition, reg, LIR_OprFact::intConst(c));
 229 }
 230 
 231 void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info) {
 232   LIR_Opr reg1 = new_register(T_INT);
 233   __ load(generate_address(base, disp, type), reg1, info);
 234   __ cmp(condition, reg, reg1);
 235 }
 236 
 237 bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, jint c, LIR_Opr result, LIR_Opr tmp) {
 238   if (tmp->is_valid() && c > 0 && c < max_jint) {
 239     if (is_power_of_2(c - 1)) {
 240       __ shift_left(left, exact_log2(c - 1), tmp);
 241       __ add(tmp, left, result);
 242       return true;
 243     } else if (is_power_of_2(c + 1)) {
 244       __ shift_left(left, exact_log2(c + 1), tmp);
 245       __ sub(tmp, left, result);
 246       return true;
 247     }
 248   }
 249   return false;
 250 }
 251 
 252 void LIRGenerator::store_stack_parameter (LIR_Opr item, ByteSize offset_from_sp) {
 253   BasicType type = item->type();
 254   __ store(item, new LIR_Address(FrameMap::sp_opr, in_bytes(offset_from_sp), type));
 255 }
 256 
 257 void LIRGenerator::array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info,
 258                                      ciMethod* profiled_method, int profiled_bci) {
 259     LIR_Opr tmp1 = new_register(objectType);
 260     LIR_Opr tmp2 = new_register(objectType);
 261     LIR_Opr tmp3 = new_register(objectType);
 262     __ store_check(value, array, tmp1, tmp2, tmp3, store_check_info, profiled_method, profiled_bci);
 263 }
 264 
 265 //----------------------------------------------------------------------
 266 //             visitor functions
 267 //----------------------------------------------------------------------
 268 
 269 void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
 270   assert(x->is_pinned(), "");
 271   LIRItem obj(x->obj(), this);
 272   obj.load_item();
 273 
 274   set_no_result(x);
 275 
 276   // "lock" stores the address of the monitor stack slot, so this is not an oop
 277   LIR_Opr lock = new_register(T_INT);
 278   LIR_Opr scratch = new_register(T_INT);
 279 
 280   CodeEmitInfo* info_for_exception = nullptr;
 281   if (x->needs_null_check()) {
 282     info_for_exception = state_for(x);
 283   }
 284 
 285   CodeStub* throw_ie_stub =
 286       x->maybe_inlinetype() ?
 287       new SimpleExceptionStub(StubId::c1_throw_identity_exception_id, obj.result(), state_for(x)) :
 288       nullptr;
 289 
 290   // this CodeEmitInfo must not have the xhandlers because here the
 291   // object is already locked (xhandlers expect object to be unlocked)
 292   CodeEmitInfo* info = state_for(x, x->state(), true);
 293   monitor_enter(obj.result(), lock, syncTempOpr(), scratch,
 294                 x->monitor_no(), info_for_exception, info, throw_ie_stub);
 295 }
 296 
 297 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
 298   assert(x->is_pinned(), "");
 299 
 300   LIRItem obj(x->obj(), this);
 301   obj.dont_load_item();
 302 
 303   LIR_Opr lock = new_register(T_INT);
 304   LIR_Opr obj_temp = new_register(T_INT);
 305   LIR_Opr scratch = new_register(T_INT);
 306   set_no_result(x);
 307   monitor_exit(obj_temp, lock, syncTempOpr(), scratch, x->monitor_no());
 308 }
 309 
 310 // neg
 311 void LIRGenerator::do_NegateOp(NegateOp* x) {
 312   LIRItem from(x->x(), this);
 313   from.load_item();
 314   LIR_Opr result = rlock_result(x);
 315   __ negate(from.result(), result);
 316 }
 317 
 318 // for  _fadd, _fmul, _fsub, _fdiv, _frem
 319 //      _dadd, _dmul, _dsub, _ddiv, _drem
 320 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
 321   LIRItem left(x->x(), this);
 322   LIRItem right(x->y(), this);
 323 
 324   if (x->op() == Bytecodes::_frem || x->op() == Bytecodes::_drem) {
 325 
 326     // float remainder is implemented as a direct call into the runtime
 327     BasicTypeList signature(2);
 328     if (x->op() == Bytecodes::_frem) {
 329       signature.append(T_FLOAT);
 330       signature.append(T_FLOAT);
 331     } else {
 332       signature.append(T_DOUBLE);
 333       signature.append(T_DOUBLE);
 334     }
 335     CallingConvention* cc = frame_map()->c_calling_convention(&signature);
 336 
 337     const LIR_Opr result_reg = result_register_for(x->type());
 338 
 339     left.load_item();
 340     __ move(left.result(), cc->at(0));
 341     right.load_item_force(cc->at(1));
 342 
 343     address entry;
 344     if (x->op() == Bytecodes::_frem) {
 345       entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
 346     } else {
 347       entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
 348     }
 349 
 350     LIR_Opr result = rlock_result(x);
 351     __ call_runtime_leaf(entry, getThreadTemp(), result_reg, cc->args());
 352     __ move(result_reg, result);
 353 
 354     return;
 355   }
 356 
 357   if (!left.is_register()) {
 358     left.load_item();
 359   }
 360   // Always load right hand side.
 361   right.load_item();
 362 
 363   LIR_Opr reg = rlock(x);
 364   arithmetic_op_fpu(x->op(), reg, left.result(), right.result());
 365 
 366   set_result(x, reg);
 367 }
 368 
 369 // for  _ladd, _lmul, _lsub, _ldiv, _lrem
 370 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
 371 
 372   // missing test if instr is commutative and if we should swap
 373   LIRItem left(x->x(), this);
 374   LIRItem right(x->y(), this);
 375 
 376   if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem) {
 377 
 378     left.load_item();
 379 
 380     bool need_zero_check = true;
 381     if (right.is_constant()) {
 382       jlong c = right.get_jlong_constant();
 383       // no need to do div-by-zero check if the divisor is a non-zero constant
 384       if (c != 0) { need_zero_check = false; }
 385       // do not load right if the divisor is a power-of-2 constant
 386       if (c > 0 && is_power_of_2(c)) {
 387         right.dont_load_item();
 388       } else {
 389         right.load_item();
 390       }
 391     } else {
 392       right.load_item();
 393     }
 394     if (need_zero_check) {
 395       CodeEmitInfo* info = state_for(x);
 396       __ cmp(lir_cond_equal, right.result(), LIR_OprFact::longConst(0));
 397       __ branch(lir_cond_equal, new DivByZeroStub(info));
 398     }
 399 
 400     rlock_result(x);
 401     switch (x->op()) {
 402       case Bytecodes::_lrem:
 403         __ rem(left.result(), right.result(), x->operand());
 404         break;
 405       case Bytecodes::_ldiv:
 406         __ div(left.result(), right.result(), x->operand());
 407         break;
 408       default:
 409         ShouldNotReachHere();
 410     }
 411   } else {
 412     assert(x->op() == Bytecodes::_lmul || x->op() == Bytecodes::_ladd || x->op() == Bytecodes::_lsub,
 413            "expect lmul, ladd or lsub");
 414     // add, sub, mul
 415     left.load_item();
 416     if (!right.is_register()) {
 417       if (x->op() == Bytecodes::_lmul ||
 418           !right.is_constant() ||
 419           (x->op() == Bytecodes::_ladd &&
 420           !Assembler::is_simm12(right.get_jlong_constant())) ||
 421           (x->op() == Bytecodes::_lsub &&
 422           !Assembler::is_simm12(-right.get_jlong_constant()))) {
 423             right.load_item();
 424       } else { // add, sub
 425         assert(x->op() == Bytecodes::_ladd || x->op() == Bytecodes::_lsub, "expected ladd or lsub");
 426         // don't load constants to save register
 427         right.load_nonconstant();
 428       }
 429     }
 430     rlock_result(x);
 431     arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), nullptr);
 432   }
 433 }
 434 
 435 // for: _iadd, _imul, _isub, _idiv, _irem
 436 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
 437 
 438   // Test if instr is commutative and if we should swap
 439   LIRItem left(x->x(),  this);
 440   LIRItem right(x->y(), this);
 441   LIRItem* left_arg = &left;
 442   LIRItem* right_arg = &right;
 443   if (x->is_commutative() && left.is_stack() && right.is_register()) {
 444     // swap them if left is real stack (or cached) and right is real register(not cached)
 445     left_arg = &right;
 446     right_arg = &left;
 447   }
 448   left_arg->load_item();
 449   // do not need to load right, as we can handle stack and constants
 450   if (x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem) {
 451 
 452     rlock_result(x);
 453 
 454     bool need_zero_check = true;
 455     if (right.is_constant()) {
 456       jint c = right.get_jint_constant();
 457       // no need to do div-by-zero check if the divisor is a non-zero constant
 458       if (c != 0) { need_zero_check = false; }
 459       // do not load right if the divisor is a power-of-2 constant
 460       if (c > 0 && is_power_of_2(c)) {
 461         right_arg->dont_load_item();
 462       } else {
 463         right_arg->load_item();
 464       }
 465     } else {
 466       right_arg->load_item();
 467     }
 468     if (need_zero_check) {
 469       CodeEmitInfo* info = state_for(x);
 470       __ cmp(lir_cond_equal, right_arg->result(), LIR_OprFact::longConst(0));
 471       __ branch(lir_cond_equal, new DivByZeroStub(info));
 472     }
 473 
 474     LIR_Opr ill = LIR_OprFact::illegalOpr;
 475     if (x->op() == Bytecodes::_irem) {
 476       __ irem(left_arg->result(), right_arg->result(), x->operand(), ill, nullptr);
 477     } else if (x->op() == Bytecodes::_idiv) {
 478       __ idiv(left_arg->result(), right_arg->result(), x->operand(), ill, nullptr);
 479     }
 480 
 481   } else if (x->op() == Bytecodes::_iadd || x->op() == Bytecodes::_isub) {
 482     if (right.is_constant() &&
 483         ((x->op() == Bytecodes::_iadd && !Assembler::is_simm12(right.get_jint_constant())) ||
 484          (x->op() == Bytecodes::_isub && !Assembler::is_simm12(-right.get_jint_constant())))) {
 485       right.load_nonconstant();
 486     } else {
 487       right.load_item();
 488     }
 489     rlock_result(x);
 490     arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), LIR_OprFact::illegalOpr);
 491   } else {
 492     assert (x->op() == Bytecodes::_imul, "expect imul");
 493     if (right.is_constant()) {
 494       jint c = right.get_jint_constant();
 495       if (c > 0 && c < max_jint && (is_power_of_2(c) || is_power_of_2(c - 1) || is_power_of_2(c + 1))) {
 496         right_arg->dont_load_item();
 497       } else {
 498         // Cannot use constant op.
 499         right_arg->load_item();
 500       }
 501     } else {
 502       right.load_item();
 503     }
 504     rlock_result(x);
 505     arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), new_register(T_INT));
 506   }
 507 }
 508 
 509 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
 510   // when an operand with use count 1 is the left operand, then it is
 511   // likely that no move for 2-operand-LIR-form is necessary
 512   if (x->is_commutative() && x->y()->as_Constant() == nullptr && x->x()->use_count() > x->y()->use_count()) {
 513     x->swap_operands();
 514   }
 515 
 516   ValueTag tag = x->type()->tag();
 517   assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
 518   switch (tag) {
 519     case floatTag:
 520     case doubleTag:  do_ArithmeticOp_FPU(x);  return;
 521     case longTag:    do_ArithmeticOp_Long(x); return;
 522     case intTag:     do_ArithmeticOp_Int(x);  return;
 523     default:         ShouldNotReachHere();    return;
 524   }
 525 }
 526 
 527 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
 528 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
 529   LIRItem value(x->x(), this);
 530   LIRItem count(x->y(), this);
 531 
 532   value.load_item();
 533   if (count.is_constant()) {
 534     assert(count.type()->as_IntConstant() != nullptr || count.type()->as_LongConstant() != nullptr , "should be");
 535     count.dont_load_item();
 536   } else {
 537     count.load_item();
 538   }
 539 
 540   LIR_Opr res = rlock_result(x);
 541   shift_op(x->op(), res, value.result(), count.result(), LIR_OprFact::illegalOpr);
 542 }
 543 
 544 
 545 // _iand, _land, _ior, _lor, _ixor, _lxor
 546 void LIRGenerator::do_LogicOp(LogicOp* x) {
 547 
 548   LIRItem left(x->x(),  this);
 549   LIRItem right(x->y(), this);
 550 
 551   left.load_item();
 552   rlock_result(x);
 553   ValueTag tag = right.type()->tag();
 554   if (right.is_constant() &&
 555      ((tag == longTag && Assembler::is_simm12(right.get_jlong_constant())) ||
 556       (tag == intTag && Assembler::is_simm12(right.get_jint_constant()))))  {
 557     right.dont_load_item();
 558   } else {
 559     right.load_item();
 560   }
 561 
 562   switch (x->op()) {
 563     case Bytecodes::_iand:  // fall through
 564     case Bytecodes::_land:
 565       __ logical_and(left.result(), right.result(), x->operand()); break;
 566     case Bytecodes::_ior:   // fall through
 567     case Bytecodes::_lor:
 568       __ logical_or(left.result(), right.result(), x->operand()); break;
 569     case Bytecodes::_ixor:  // fall through
 570     case Bytecodes::_lxor:
 571       __ logical_xor(left.result(), right.result(), x->operand()); break;
 572     default: Unimplemented();
 573   }
 574 }
 575 
 576 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
 577 void LIRGenerator::do_CompareOp(CompareOp* x) {
 578   LIRItem left(x->x(), this);
 579   LIRItem right(x->y(), this);
 580   ValueTag tag = x->x()->type()->tag();
 581   if (tag == longTag) {
 582     left.set_destroys_register();
 583   }
 584   left.load_item();
 585   right.load_item();
 586   LIR_Opr reg = rlock_result(x);
 587 
 588   if (x->x()->type()->is_float_kind()) {
 589     Bytecodes::Code code = x->op();
 590     __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
 591   } else if (x->x()->type()->tag() == longTag) {
 592     __ lcmp2int(left.result(), right.result(), reg);
 593   } else {
 594     Unimplemented();
 595   }
 596 }
 597 
 598 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
 599   LIR_Opr ill = LIR_OprFact::illegalOpr;  // for convenience
 600   new_value.load_item();
 601   cmp_value.load_item();
 602   LIR_Opr result = new_register(T_INT);
 603   if (is_reference_type(type)) {
 604     __ cas_obj(addr, cmp_value.result(), new_value.result(), new_register(T_INT), new_register(T_INT), result);
 605   } else if (type == T_INT) {
 606     __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
 607   } else if (type == T_LONG) {
 608     __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), ill, ill);
 609   } else {
 610     ShouldNotReachHere();
 611   }
 612   __ logical_xor(FrameMap::r5_opr, LIR_OprFact::intConst(1), result);
 613   return result;
 614 }
 615 
 616 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
 617   bool is_oop = is_reference_type(type);
 618   LIR_Opr result = new_register(type);
 619   value.load_item();
 620   assert(type == T_INT || is_oop LP64_ONLY( || type == T_LONG ), "unexpected type");
 621   LIR_Opr tmp = new_register(T_INT);
 622   __ xchg(addr, value.result(), result, tmp);
 623   return result;
 624 }
 625 
 626 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
 627   LIR_Opr result = new_register(type);
 628   value.load_item();
 629   assert(type == T_INT LP64_ONLY( || type == T_LONG ), "unexpected type");
 630   LIR_Opr tmp = new_register(T_INT);
 631   __ xadd(addr, value.result(), result, tmp);
 632   return result;
 633 }
 634 
 635 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
 636   assert(x->number_of_arguments() == 1 || (x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow),
 637          "wrong type");
 638 
 639   switch (x->id()) {
 640     case vmIntrinsics::_dexp: // fall through
 641     case vmIntrinsics::_dlog: // fall through
 642     case vmIntrinsics::_dpow: // fall through
 643     case vmIntrinsics::_dcos: // fall through
 644     case vmIntrinsics::_dsin: // fall through
 645     case vmIntrinsics::_dtan: // fall through
 646     case vmIntrinsics::_dlog10:
 647       do_LibmIntrinsic(x);
 648       break;
 649     case vmIntrinsics::_dabs: // fall through
 650     case vmIntrinsics::_dsqrt: // fall through
 651     case vmIntrinsics::_dsqrt_strict: {
 652       assert(x->number_of_arguments() == 1, "wrong type");
 653       LIRItem value(x->argument_at(0), this);
 654       value.load_item();
 655       LIR_Opr dst = rlock_result(x);
 656 
 657       switch (x->id()) {
 658         case vmIntrinsics::_dsqrt: // fall through
 659         case vmIntrinsics::_dsqrt_strict: {
 660           __ sqrt(value.result(), dst, LIR_OprFact::illegalOpr);
 661           break;
 662         }
 663         case vmIntrinsics::_dabs: {
 664           __ abs(value.result(), dst, LIR_OprFact::illegalOpr);
 665           break;
 666         }
 667         default:
 668           ShouldNotReachHere();
 669       }
 670       break;
 671     }
 672     default:
 673       ShouldNotReachHere();
 674   }
 675 }
 676 
 677 void LIRGenerator::do_LibmIntrinsic(Intrinsic* x) {
 678   LIRItem value(x->argument_at(0), this);
 679   value.set_destroys_register();
 680 
 681   LIR_Opr calc_result = rlock_result(x);
 682   LIR_Opr result_reg = result_register_for(x->type());
 683 
 684   CallingConvention* cc = nullptr;
 685 
 686   if (x->id() == vmIntrinsics::_dpow) {
 687     LIRItem value1(x->argument_at(1), this);
 688 
 689     value1.set_destroys_register();
 690 
 691     BasicTypeList signature(2);
 692     signature.append(T_DOUBLE);
 693     signature.append(T_DOUBLE);
 694     cc = frame_map()->c_calling_convention(&signature);
 695     value.load_item_force(cc->at(0));
 696     value1.load_item_force(cc->at(1));
 697   } else {
 698     BasicTypeList signature(1);
 699     signature.append(T_DOUBLE);
 700     cc = frame_map()->c_calling_convention(&signature);
 701     value.load_item_force(cc->at(0));
 702   }
 703 
 704   switch (x->id()) {
 705     case vmIntrinsics::_dexp:
 706       if (StubRoutines::dexp() != nullptr) { __ call_runtime_leaf(StubRoutines::dexp(), getThreadTemp(), result_reg, cc->args()); }
 707       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dexp), getThreadTemp(), result_reg, cc->args()); }
 708       break;
 709     case vmIntrinsics::_dlog:
 710       if (StubRoutines::dlog() != nullptr) {  __ call_runtime_leaf(StubRoutines::dlog(), getThreadTemp(), result_reg, cc->args()); }
 711       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog), getThreadTemp(), result_reg, cc->args()); }
 712       break;
 713     case vmIntrinsics::_dlog10:
 714       if (StubRoutines::dlog10() != nullptr) { __ call_runtime_leaf(StubRoutines::dlog10(), getThreadTemp(), result_reg, cc->args()); }
 715       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dlog10), getThreadTemp(), result_reg, cc->args()); }
 716       break;
 717     case vmIntrinsics::_dsin:
 718       if (StubRoutines::dsin() != nullptr) { __ call_runtime_leaf(StubRoutines::dsin(), getThreadTemp(), result_reg, cc->args()); }
 719       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dsin), getThreadTemp(), result_reg, cc->args()); }
 720       break;
 721     case vmIntrinsics::_dcos:
 722       if (StubRoutines::dcos() != nullptr) {  __ call_runtime_leaf(StubRoutines::dcos(), getThreadTemp(), result_reg, cc->args()); }
 723       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dcos), getThreadTemp(), result_reg, cc->args()); }
 724       break;
 725     case vmIntrinsics::_dtan:
 726       if (StubRoutines::dtan() != nullptr) { __ call_runtime_leaf(StubRoutines::dtan(), getThreadTemp(), result_reg, cc->args()); }
 727       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtan), getThreadTemp(), result_reg, cc->args()); }
 728       break;
 729     case vmIntrinsics::_dpow:
 730       if (StubRoutines::dpow() != nullptr) { __ call_runtime_leaf(StubRoutines::dpow(), getThreadTemp(), result_reg, cc->args()); }
 731       else { __ call_runtime_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dpow), getThreadTemp(), result_reg, cc->args()); }
 732       break;
 733     default:  ShouldNotReachHere();
 734   }
 735   __ move(result_reg, calc_result);
 736 }
 737 
 738 
 739 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
 740   assert(x->number_of_arguments() == 5, "wrong type");
 741 
 742   // Make all state_for calls early since they can emit code
 743   CodeEmitInfo* info = nullptr;
 744   if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
 745     info = state_for(x, x->state_before());
 746     info->set_force_reexecute();
 747   } else {
 748     info = state_for(x, x->state());
 749   }
 750 
 751   LIRItem src(x->argument_at(0), this);
 752   LIRItem src_pos(x->argument_at(1), this);
 753   LIRItem dst(x->argument_at(2), this);
 754   LIRItem dst_pos(x->argument_at(3), this);
 755   LIRItem length(x->argument_at(4), this);
 756 
 757   // operands for arraycopy must use fixed registers, otherwise
 758   // LinearScan will fail allocation (because arraycopy always needs a
 759   // call)
 760 
 761   // The java calling convention will give us enough registers
 762   // so that on the stub side the args will be perfect already.
 763   // On the other slow/special case side we call C and the arg
 764   // positions are not similar enough to pick one as the best.
 765   // Also because the java calling convention is a "shifted" version
 766   // of the C convention we can process the java args trivially into C
 767   // args without worry of overwriting during the xfer
 768 
 769   src.load_item_force     (FrameMap::as_oop_opr(j_rarg0));
 770   src_pos.load_item_force (FrameMap::as_opr(j_rarg1));
 771   dst.load_item_force     (FrameMap::as_oop_opr(j_rarg2));
 772   dst_pos.load_item_force (FrameMap::as_opr(j_rarg3));
 773   length.load_item_force  (FrameMap::as_opr(j_rarg4));
 774 
 775   LIR_Opr tmp = FrameMap::as_opr(j_rarg5);
 776 
 777   set_no_result(x);
 778 
 779   int flags;
 780   ciArrayKlass* expected_type = nullptr;
 781   arraycopy_helper(x, &flags, &expected_type);
 782   if (x->check_flag(Instruction::OmitChecksFlag)) {
 783     flags = (flags & LIR_OpArrayCopy::get_initial_copy_flags());
 784   }
 785 
 786   __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(), tmp,
 787                expected_type, flags, info); // does add_safepoint
 788 }
 789 
 790 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
 791   assert(UseCRC32Intrinsics, "why are we here?");
 792   // Make all state_for calls early since they can emit code
 793   LIR_Opr result = rlock_result(x);
 794   switch (x->id()) {
 795     case vmIntrinsics::_updateCRC32: {
 796       LIRItem crc(x->argument_at(0), this);
 797       LIRItem val(x->argument_at(1), this);
 798       // val is destroyed by update_crc32
 799       val.set_destroys_register();
 800       crc.load_item();
 801       val.load_item();
 802       __ update_crc32(crc.result(), val.result(), result);
 803       break;
 804     }
 805     case vmIntrinsics::_updateBytesCRC32:
 806     case vmIntrinsics::_updateByteBufferCRC32: {
 807       bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32);
 808 
 809       LIRItem crc(x->argument_at(0), this);
 810       LIRItem buf(x->argument_at(1), this);
 811       LIRItem off(x->argument_at(2), this);
 812       LIRItem len(x->argument_at(3), this);
 813       buf.load_item();
 814       off.load_nonconstant();
 815 
 816       LIR_Opr index = off.result();
 817       int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
 818       if (off.result()->is_constant()) {
 819         index = LIR_OprFact::illegalOpr;
 820         offset += off.result()->as_jint();
 821       }
 822       LIR_Opr base_op = buf.result();
 823 
 824       if (index->is_valid()) {
 825         LIR_Opr tmp = new_register(T_LONG);
 826         __ convert(Bytecodes::_i2l, index, tmp);
 827         index = tmp;
 828       }
 829 
 830       if (offset) {
 831         LIR_Opr tmp = new_pointer_register();
 832         __ add(base_op, LIR_OprFact::intConst(offset), tmp);
 833         base_op = tmp;
 834         offset = 0;
 835       }
 836 
 837       LIR_Address* a = new LIR_Address(base_op,
 838                                        index,
 839                                        offset,
 840                                        T_BYTE);
 841       BasicTypeList signature(3);
 842       signature.append(T_INT);
 843       signature.append(T_ADDRESS);
 844       signature.append(T_INT);
 845       CallingConvention* cc = frame_map()->c_calling_convention(&signature);
 846       const LIR_Opr result_reg = result_register_for(x->type());
 847 
 848       LIR_Opr addr = new_register(T_ADDRESS);
 849       __ leal(LIR_OprFact::address(a), addr);
 850 
 851       crc.load_item_force(cc->at(0));
 852       __ move(addr, cc->at(1));
 853       len.load_item_force(cc->at(2));
 854 
 855       __ call_runtime_leaf(StubRoutines::updateBytesCRC32(), getThreadTemp(), result_reg, cc->args());
 856       __ move(result_reg, result);
 857 
 858       break;
 859     }
 860     default: {
 861       ShouldNotReachHere();
 862     }
 863   }
 864 }
 865 
 866 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
 867   ShouldNotReachHere();
 868 }
 869 
 870 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
 871   assert(x->number_of_arguments() == 3, "wrong type");
 872   assert(UseFMA, "Needs FMA instructions support.");
 873   LIRItem value(x->argument_at(0), this);
 874   LIRItem value1(x->argument_at(1), this);
 875   LIRItem value2(x->argument_at(2), this);
 876 
 877   value.load_item();
 878   value1.load_item();
 879   value2.load_item();
 880 
 881   LIR_Opr calc_input = value.result();
 882   LIR_Opr calc_input1 = value1.result();
 883   LIR_Opr calc_input2 = value2.result();
 884   LIR_Opr calc_result = rlock_result(x);
 885 
 886   switch (x->id()) {
 887     case vmIntrinsics::_fmaD:   __ fmad(calc_input, calc_input1, calc_input2, calc_result); break;
 888     case vmIntrinsics::_fmaF:   __ fmaf(calc_input, calc_input1, calc_input2, calc_result); break;
 889     default:                    ShouldNotReachHere();
 890   }
 891 }
 892 
 893 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
 894   ShouldNotReachHere();
 895 }
 896 
 897 // _i2l, _i2f, _i2d, _l2i, _l2f, _l2d, _f2i, _f2l, _f2d, _d2i, _d2l, _d2f
 898 // _i2b, _i2c, _i2s
 899 void LIRGenerator::do_Convert(Convert* x) {
 900   LIRItem value(x->value(), this);
 901   value.load_item();
 902   LIR_Opr input = value.result();
 903   LIR_Opr result = rlock(x);
 904 
 905   // arguments of lir_convert
 906   LIR_Opr conv_input = input;
 907   LIR_Opr conv_result = result;
 908 
 909   __ convert(x->op(), conv_input, conv_result);
 910 
 911   assert(result->is_virtual(), "result must be virtual register");
 912   set_result(x, result);
 913 }
 914 
 915 void LIRGenerator::do_NewInstance(NewInstance* x) {
 916 #ifndef PRODUCT
 917   if (PrintNotLoaded && !x->klass()->is_loaded()) {
 918     tty->print_cr("   ###class not loaded at new bci %d", x->printable_bci());
 919   }
 920 #endif
 921   CodeEmitInfo* info = state_for(x, x->needs_state_before() ? x->state_before() : x->state());
 922   LIR_Opr reg = result_register_for(x->type());
 923   new_instance(reg, x->klass(), x->is_unresolved(),
 924                !x->is_unresolved() && x->klass()->is_inlinetype(),
 925                FrameMap::r12_oop_opr,
 926                FrameMap::r15_oop_opr,
 927                FrameMap::r14_oop_opr,
 928                LIR_OprFact::illegalOpr,
 929                FrameMap::r13_metadata_opr,
 930                info);
 931 
 932   LIR_Opr result = rlock_result(x);
 933   __ move(reg, result);
 934 }
 935 
 936 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
 937   CodeEmitInfo* info = nullptr;
 938   if (x->state_before() != nullptr && x->state_before()->force_reexecute()) {
 939     info = state_for(x, x->state_before());
 940     info->set_force_reexecute();
 941   } else {
 942     info = state_for(x, x->state());
 943   }
 944 
 945   LIRItem length(x->length(), this);
 946   length.load_item_force(FrameMap::r9_opr);
 947 
 948   LIR_Opr reg = result_register_for(x->type());
 949   LIR_Opr tmp1 = FrameMap::r12_oop_opr;
 950   LIR_Opr tmp2 = FrameMap::r14_oop_opr;
 951   LIR_Opr tmp3 = FrameMap::r15_oop_opr;
 952   LIR_Opr tmp4 = reg;
 953   LIR_Opr klass_reg = FrameMap::r13_metadata_opr;
 954   LIR_Opr len = length.result();
 955   BasicType elem_type = x->elt_type();
 956 
 957   __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
 958 
 959   CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
 960   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path, x->zero_array());
 961 
 962   LIR_Opr result = rlock_result(x);
 963   __ move(reg, result);
 964 }
 965 
 966 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
 967   LIRItem length(x->length(), this);
 968   // in case of patching (i.e., object class is not yet loaded), we need to reexecute the instruction
 969   // and therefore provide the state before the parameters have been consumed
 970   CodeEmitInfo* patching_info = nullptr;
 971   if (!x->klass()->is_loaded() || PatchALot) {
 972     patching_info =  state_for(x, x->state_before());
 973   }
 974 
 975   CodeEmitInfo* info = state_for(x, x->state());
 976 
 977   LIR_Opr reg = result_register_for(x->type());
 978   LIR_Opr tmp1 = FrameMap::r12_oop_opr;
 979   LIR_Opr tmp2 = FrameMap::r14_oop_opr;
 980   LIR_Opr tmp3 = FrameMap::r15_oop_opr;
 981   LIR_Opr tmp4 = reg;
 982   LIR_Opr klass_reg = FrameMap::r13_metadata_opr;
 983 
 984   length.load_item_force(FrameMap::r9_opr);
 985   LIR_Opr len = length.result();
 986 
 987   ciKlass* obj = ciObjArrayKlass::make(x->klass());
 988 
 989   // TODO 8265122 Implement a fast path for this
 990   bool is_flat = obj->is_loaded() && obj->is_flat_array_klass();
 991   bool is_null_free = obj->is_loaded() && obj->as_array_klass()->is_elem_null_free();
 992 
 993   CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info, is_null_free);
 994 
 995   if (obj == ciEnv::unloaded_ciobjarrayklass()) {
 996     BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
 997   }
 998   klass2reg_with_patching(klass_reg, obj, patching_info);
 999   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path, true, is_null_free || is_flat);
1000 
1001   LIR_Opr result = rlock_result(x);
1002   __ move(reg, result);
1003 }
1004 
1005 
1006 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1007   Values* dims = x->dims();
1008   int i = dims->length();
1009   LIRItemList* items = new LIRItemList(i, i, nullptr);
1010   while (i-- > 0) {
1011     LIRItem* size = new LIRItem(dims->at(i), this);
1012     items->at_put(i, size);
1013   }
1014 
1015   // Evaluate state_for early since it may emit code.
1016   CodeEmitInfo* patching_info = nullptr;
1017   if (!x->klass()->is_loaded() || PatchALot) {
1018     patching_info = state_for(x, x->state_before());
1019 
1020     // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1021     // clone all handlers (NOTE: Usually this is handled transparently
1022     // by the CodeEmitInfo cloning logic in CodeStub constructors but
1023     // is done explicitly here because a stub isn't being used).
1024     x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1025   }
1026   CodeEmitInfo* info = state_for(x, x->state());
1027 
1028   i = dims->length();
1029   while (i-- > 0) {
1030     LIRItem* size = items->at(i);
1031     size->load_item();
1032 
1033     store_stack_parameter(size->result(), in_ByteSize(i * BytesPerInt));
1034   }
1035 
1036   LIR_Opr klass_reg = FrameMap::r10_metadata_opr;
1037   klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1038 
1039   LIR_Opr rank = FrameMap::r9_opr;
1040   __ move(LIR_OprFact::intConst(x->rank()), rank);
1041   LIR_Opr varargs = FrameMap::r12_opr;
1042   __ move(FrameMap::sp_opr, varargs);
1043   LIR_OprList* args = new LIR_OprList(3);
1044   args->append(klass_reg);
1045   args->append(rank);
1046   args->append(varargs);
1047   LIR_Opr reg = result_register_for(x->type());
1048   __ call_runtime(Runtime1::entry_for(StubId::c1_new_multi_array_id),
1049                   LIR_OprFact::illegalOpr,
1050                   reg, args, info);
1051 
1052   LIR_Opr result = rlock_result(x);
1053   __ move(reg, result);
1054 }
1055 
1056 void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1057   // nothing to do for now
1058 }
1059 
1060 void LIRGenerator::do_CheckCast(CheckCast* x) {
1061   LIRItem obj(x->obj(), this);
1062 
1063   CodeEmitInfo* patching_info = nullptr;
1064   if (!x->klass()->is_loaded() ||
1065       (PatchALot && !x->is_incompatible_class_change_check() && !x->is_invokespecial_receiver_check())) {
1066     // must do this before locking the destination register as an oop register,
1067     // and before the obj is loaded (the latter is for deoptimization)
1068     patching_info = state_for(x, x->state_before());
1069   }
1070   obj.load_item();
1071 
1072   // info for exceptions
1073   CodeEmitInfo* info_for_exception =
1074       (x->needs_exception_state() ? state_for(x) :
1075                                     state_for(x, x->state_before(), true /*ignore_xhandler*/ ));
1076 
1077   CodeStub* stub = nullptr;
1078   if (x->is_incompatible_class_change_check()) {
1079     assert(patching_info == nullptr, "can't patch this");
1080     stub = new SimpleExceptionStub(StubId::c1_throw_incompatible_class_change_error_id, LIR_OprFact::illegalOpr,
1081                                    info_for_exception);
1082   } else if (x->is_invokespecial_receiver_check()) {
1083     assert(patching_info == nullptr, "can't patch this");
1084     stub = new DeoptimizeStub(info_for_exception,
1085                               Deoptimization::Reason_class_check,
1086                               Deoptimization::Action_none);
1087   } else {
1088     stub = new SimpleExceptionStub(StubId::c1_throw_class_cast_exception_id, obj.result(), info_for_exception);
1089   }
1090   LIR_Opr reg = rlock_result(x);
1091   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1092   tmp3 = new_register(objectType);
1093   __ checkcast(reg, obj.result(), x->klass(),
1094                new_register(objectType), new_register(objectType), tmp3,
1095                x->direct_compare(), info_for_exception, patching_info, stub,
1096                x->profiled_method(), x->profiled_bci(), x->is_null_free());
1097 }
1098 
1099 void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1100   LIRItem obj(x->obj(), this);
1101 
1102   // result and test object may not be in same register
1103   LIR_Opr reg = rlock_result(x);
1104   CodeEmitInfo* patching_info = nullptr;
1105   if ((!x->klass()->is_loaded() || PatchALot)) {
1106     // must do this before locking the destination register as an oop register
1107     patching_info = state_for(x, x->state_before());
1108   }
1109   obj.load_item();
1110   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1111   tmp3 = new_register(objectType);
1112   __ instanceof(reg, obj.result(), x->klass(),
1113                 new_register(objectType), new_register(objectType), tmp3,
1114                 x->direct_compare(), patching_info, x->profiled_method(), x->profiled_bci());
1115 }
1116 
1117 // Intrinsic for Class::isInstance
1118 address LIRGenerator::isInstance_entry() {
1119   return Runtime1::entry_for(StubId::c1_is_instance_of_id);
1120 }
1121 
1122 void LIRGenerator::do_If(If* x) {
1123   // If should have two successors
1124   assert(x->number_of_sux() == 2, "inconsistency");
1125   ValueTag tag = x->x()->type()->tag();
1126   bool is_safepoint = x->is_safepoint();
1127 
1128   If::Condition cond = x->cond();
1129 
1130   LIRItem xitem(x->x(), this);
1131   LIRItem yitem(x->y(), this);
1132   LIRItem* xin = &xitem;
1133   LIRItem* yin = &yitem;
1134 
1135   if (tag == longTag) {
1136     // for longs, only conditions "eql", "neq", "lss", "geq" are valid;
1137     // mirror for other conditions
1138     if (cond == If::gtr || cond == If::leq) {
1139       cond = Instruction::mirror(cond);
1140       xin = &yitem;
1141       yin = &xitem;
1142     }
1143     xin->set_destroys_register();
1144   }
1145   xin->load_item();
1146   yin->load_item();
1147 
1148   set_no_result(x);
1149 
1150   LIR_Opr left = xin->result();
1151   LIR_Opr right = yin->result();
1152 
1153   // add safepoint before generating condition code so it can be recomputed
1154   if (x->is_safepoint()) {
1155     // increment backedge counter if needed
1156     increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1157                                              x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1158     __ safepoint(LIR_OprFact::illegalOpr, state_for(x, x->state_before()));
1159   }
1160 
1161   if (x->substitutability_check()) {
1162     substitutability_check(x, *xin, *yin);
1163   } else {
1164     __ cmp(lir_cond(cond), left, right);
1165   }
1166 
1167   // Generate branch profiling. Profiling code doesn't kill flags.
1168   profile_branch(x, cond);
1169   move_to_phi(x->state());
1170   if (x->x()->type()->is_float_kind()) {
1171     __ branch(lir_cond(cond), x->tsux(), x->usux());
1172   } else {
1173     __ branch(lir_cond(cond), x->tsux());
1174   }
1175   assert(x->default_sux() == x->fsux(), "wrong destination above");
1176   __ jump(x->default_sux());
1177 }
1178 
1179 LIR_Opr LIRGenerator::getThreadPointer() {
1180    return FrameMap::as_pointer_opr(xthread);
1181 }
1182 
1183 void LIRGenerator::trace_block_entry(BlockBegin* block) { Unimplemented(); }
1184 
1185 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1186                                         CodeEmitInfo* info) {
1187   __ volatile_store_mem_reg(value, address, info);
1188 }
1189 
1190 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1191                                        CodeEmitInfo* info) {
1192   __ volatile_load_mem_reg(address, result, info);
1193   __ membar_acquire();
1194 }