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