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