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