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