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