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