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