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