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