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