1 /*
   2  * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.inline.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 "ci/ciUtilities.hpp"
  38 #include "gc/shared/c1/barrierSetC1.hpp"
  39 #include "gc/shared/cardTable.hpp"
  40 #include "gc/shared/cardTableBarrierSet.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "utilities/powerOfTwo.hpp"
  44 #include "vmreg_arm.inline.hpp"
  45 
  46 #ifdef ASSERT
  47 #define __ gen()->lir(__FILE__, __LINE__)->
  48 #else
  49 #define __ gen()->lir()->
  50 #endif
  51 
  52 void LIRItem::load_byte_item() {
  53   load_item();
  54 }
  55 
  56 void LIRItem::load_nonconstant() {
  57   LIR_Opr r = value()->operand();
  58   if (_gen->can_inline_as_constant(value())) {
  59     if (!r->is_constant()) {
  60       r = LIR_OprFact::value_type(value()->type());
  61     }
  62     _result = r;
  63   } else {
  64     load_item();
  65   }
  66 }
  67 
  68 //--------------------------------------------------------------
  69 //               LIRGenerator
  70 //--------------------------------------------------------------
  71 
  72 
  73 LIR_Opr LIRGenerator::exceptionOopOpr() {
  74   return FrameMap::Exception_oop_opr;
  75 }
  76 
  77 LIR_Opr LIRGenerator::exceptionPcOpr()  {
  78   return FrameMap::Exception_pc_opr;
  79 }
  80 
  81 LIR_Opr LIRGenerator::syncLockOpr()     {
  82   return new_register(T_INT);
  83 }
  84 
  85 LIR_Opr LIRGenerator::syncTempOpr()     {
  86   return new_register(T_OBJECT);
  87 }
  88 
  89 LIR_Opr LIRGenerator::getThreadTemp()   {
  90   return LIR_OprFact::illegalOpr;
  91 }
  92 
  93 LIR_Opr LIRGenerator::atomicLockOpr() {
  94   return LIR_OprFact::illegalOpr;
  95 }
  96 
  97 LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
  98   LIR_Opr opr;
  99   switch (type->tag()) {
 100     case intTag:     opr = FrameMap::Int_result_opr;    break;
 101     case objectTag:  opr = FrameMap::Object_result_opr; break;
 102     case longTag:    opr = FrameMap::Long_result_opr;   break;
 103     case floatTag:   opr = FrameMap::Float_result_opr;  break;
 104     case doubleTag:  opr = FrameMap::Double_result_opr; break;
 105     case addressTag:
 106     default: ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
 107   }
 108   assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
 109   return opr;
 110 }
 111 
 112 
 113 LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
 114   return new_register(T_INT);
 115 }
 116 
 117 
 118 //--------- loading items into registers --------------------------------
 119 
 120 
 121 bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
 122   return false;
 123 }
 124 
 125 
 126 bool LIRGenerator::can_inline_as_constant(Value v) const {
 127   if (v->type()->as_IntConstant() != NULL) {
 128     return Assembler::is_arith_imm_in_range(v->type()->as_IntConstant()->value());
 129   } else if (v->type()->as_ObjectConstant() != NULL) {
 130     return v->type()->as_ObjectConstant()->value()->is_null_object();
 131   } else if (v->type()->as_FloatConstant() != NULL) {
 132     return v->type()->as_FloatConstant()->value() == 0.0f;
 133   } else if (v->type()->as_DoubleConstant() != NULL) {
 134     return v->type()->as_DoubleConstant()->value() == 0.0;
 135   }
 136   return false;
 137 }
 138 
 139 
 140 bool LIRGenerator::can_inline_as_constant(LIR_Const* c) const {
 141   ShouldNotCallThis(); // Not used on ARM
 142   return false;
 143 }
 144 
 145 
 146 
 147 
 148 LIR_Opr LIRGenerator::safepoint_poll_register() {
 149   return LIR_OprFact::illegalOpr;
 150 }
 151 
 152 
 153 static LIR_Opr make_constant(BasicType type, jlong c) {
 154   switch (type) {
 155     case T_ADDRESS:
 156     case T_OBJECT:  return LIR_OprFact::intptrConst(c);
 157     case T_LONG:    return LIR_OprFact::longConst(c);
 158     case T_INT:     return LIR_OprFact::intConst(c);
 159     default: ShouldNotReachHere();
 160     return LIR_OprFact::intConst(-1);
 161   }
 162 }
 163 
 164 
 165 
 166 void LIRGenerator::add_large_constant(LIR_Opr src, int c, LIR_Opr dest) {
 167   assert(c != 0, "must be");
 168   // Find first non-zero bit
 169   int shift = 0;
 170   while ((c & (3 << shift)) == 0) {
 171     shift += 2;
 172   }
 173   // Add the least significant part of the constant
 174   int mask = 0xff << shift;
 175   __ add(src, LIR_OprFact::intConst(c & mask), dest);
 176   // Add up to 3 other parts of the constant;
 177   // each of them can be represented as rotated_imm
 178   if (c & (mask << 8)) {
 179     __ add(dest, LIR_OprFact::intConst(c & (mask << 8)), dest);
 180   }
 181   if (c & (mask << 16)) {
 182     __ add(dest, LIR_OprFact::intConst(c & (mask << 16)), dest);
 183   }
 184   if (c & (mask << 24)) {
 185     __ add(dest, LIR_OprFact::intConst(c & (mask << 24)), dest);
 186   }
 187 }
 188 
 189 static LIR_Address* make_address(LIR_Opr base, LIR_Opr index, LIR_Address::Scale scale, BasicType type) {
 190   return new LIR_Address(base, index, scale, 0, type);
 191 }
 192 
 193 LIR_Address* LIRGenerator::generate_address(LIR_Opr base, LIR_Opr index,
 194                                             int shift, int disp, BasicType type) {
 195   assert(base->is_register(), "must be");
 196 
 197   if (index->is_constant()) {
 198     disp += index->as_constant_ptr()->as_jint() << shift;
 199     index = LIR_OprFact::illegalOpr;
 200   }
 201 
 202   if (base->type() == T_LONG) {
 203     LIR_Opr tmp = new_register(T_INT);
 204     __ convert(Bytecodes::_l2i, base, tmp);
 205     base = tmp;
 206   }
 207   if (index != LIR_OprFact::illegalOpr && index->type() == T_LONG) {
 208     LIR_Opr tmp = new_register(T_INT);
 209     __ convert(Bytecodes::_l2i, index, tmp);
 210     index = tmp;
 211   }
 212   // At this point base and index should be all ints and not constants
 213   assert(base->is_single_cpu() && !base->is_constant(), "base should be an non-constant int");
 214   assert(index->is_illegal() || (index->type() == T_INT && !index->is_constant()), "index should be an non-constant int");
 215 
 216   int max_disp;
 217   bool disp_is_in_range;
 218   bool embedded_shift;
 219 
 220   switch (type) {
 221     case T_BYTE:
 222     case T_SHORT:
 223     case T_CHAR:
 224       max_disp = 256;          // ldrh, ldrsb encoding has 8-bit offset
 225       embedded_shift = false;
 226       break;
 227     case T_FLOAT:
 228     case T_DOUBLE:
 229       max_disp = 1024;         // flds, fldd have 8-bit offset multiplied by 4
 230       embedded_shift = false;
 231       break;
 232     case T_LONG:
 233       max_disp = 4096;
 234       embedded_shift = false;
 235       break;
 236     default:
 237       max_disp = 4096;         // ldr, ldrb allow 12-bit offset
 238       embedded_shift = true;
 239   }
 240 
 241   disp_is_in_range = (-max_disp < disp && disp < max_disp);
 242 
 243   if (index->is_register()) {
 244     LIR_Opr tmp = new_pointer_register();
 245     if (!disp_is_in_range) {
 246       add_large_constant(base, disp, tmp);
 247       base = tmp;
 248       disp = 0;
 249     }
 250     LIR_Address* addr = make_address(base, index, (LIR_Address::Scale)shift, type);
 251     if (disp == 0 && embedded_shift) {
 252       // can use ldr/str instruction with register index
 253       return addr;
 254     } else {
 255       LIR_Opr tmp = new_pointer_register();
 256       __ add(base, LIR_OprFact::address(addr), tmp); // add with shifted/extended register
 257       return new LIR_Address(tmp, disp, type);
 258     }
 259   }
 260 
 261   // If the displacement is too large to be inlined into LDR instruction,
 262   // generate large constant with additional sequence of ADD instructions
 263   int excess_disp = disp & ~(max_disp - 1);
 264   if (excess_disp != 0) {
 265     LIR_Opr tmp = new_pointer_register();
 266     add_large_constant(base, excess_disp, tmp);
 267     base = tmp;
 268   }
 269   return new LIR_Address(base, disp & (max_disp - 1), type);
 270 }
 271 
 272 
 273 LIR_Address* LIRGenerator::emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr, BasicType type) {
 274   int base_offset = arrayOopDesc::base_offset_in_bytes(type);
 275   int elem_size = type2aelembytes(type);
 276 
 277   if (index_opr->is_constant()) {
 278     int offset = base_offset + index_opr->as_constant_ptr()->as_jint() * elem_size;
 279     return generate_address(array_opr, offset, type);
 280   } else {
 281     assert(index_opr->is_register(), "must be");
 282     int scale = exact_log2(elem_size);
 283     return generate_address(array_opr, index_opr, scale, base_offset, type);
 284   }
 285 }
 286 
 287 
 288 LIR_Opr LIRGenerator::load_immediate(jlong x, BasicType type) {
 289   assert(type == T_LONG || type == T_INT, "should be");
 290   LIR_Opr r = make_constant(type, x);
 291   bool imm_in_range = AsmOperand::is_rotated_imm((unsigned int)(x));
 292   if (!imm_in_range) {
 293     LIR_Opr tmp = new_register(type);
 294     __ move(r, tmp);
 295     return tmp;
 296   }
 297   return r;
 298 }
 299 
 300 
 301 void LIRGenerator::increment_counter(address counter, BasicType type, int step) {
 302   LIR_Opr pointer = new_pointer_register();
 303   __ move(LIR_OprFact::intptrConst(counter), pointer);
 304   LIR_Address* addr = new LIR_Address(pointer, type);
 305   increment_counter(addr, step);
 306 }
 307 
 308 
 309 void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
 310   LIR_Opr temp = new_register(addr->type());
 311   __ move(addr, temp);
 312   __ add(temp, make_constant(addr->type(), step), temp);
 313   __ move(temp, addr);
 314 }
 315 
 316 
 317 void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
 318   __ load(new LIR_Address(base, disp, T_INT), FrameMap::LR_opr, info);
 319   __ cmp(condition, FrameMap::LR_opr, c);
 320 }
 321 
 322 
 323 void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base, int disp, BasicType type, CodeEmitInfo* info) {
 324   __ load(new LIR_Address(base, disp, type), FrameMap::LR_opr, info);
 325   __ cmp(condition, reg, FrameMap::LR_opr);
 326 }
 327 
 328 
 329 bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, jint c, LIR_Opr result, LIR_Opr tmp) {
 330   assert(left != result, "should be different registers");
 331   if (is_power_of_2(c + 1)) {
 332     LIR_Address::Scale scale = (LIR_Address::Scale) log2i_exact(c + 1);
 333     LIR_Address* addr = new LIR_Address(left, left, scale, 0, T_INT);
 334     __ sub(LIR_OprFact::address(addr), left, result); // rsb with shifted register
 335     return true;
 336   } else if (is_power_of_2(c - 1)) {
 337     LIR_Address::Scale scale = (LIR_Address::Scale) log2i_exact(c - 1);
 338     LIR_Address* addr = new LIR_Address(left, left, scale, 0, T_INT);
 339     __ add(left, LIR_OprFact::address(addr), result); // add with shifted register
 340     return true;
 341   }
 342   return false;
 343 }
 344 
 345 
 346 void LIRGenerator::store_stack_parameter(LIR_Opr item, ByteSize offset_from_sp) {
 347   assert(item->type() == T_INT, "other types are not expected");
 348   __ store(item, new LIR_Address(FrameMap::SP_opr, in_bytes(offset_from_sp), item->type()));
 349 }
 350 
 351 void LIRGenerator::set_card(LIR_Opr value, LIR_Address* card_addr) {
 352   assert(CardTable::dirty_card_val() == 0,
 353     "Cannot use the register containing the card table base address directly");
 354   if((ci_card_table_address_as<intx>() & 0xff) == 0) {
 355     // If the card table base address is aligned to 256 bytes, we can use the register
 356     // that contains the card_table_base_address.
 357     __ move(value, card_addr);
 358   } else {
 359     // Otherwise we need to create a register containing that value.
 360     LIR_Opr tmp_zero = new_register(T_INT);
 361     __ move(LIR_OprFact::intConst(CardTable::dirty_card_val()), tmp_zero);
 362     __ move(tmp_zero, card_addr);
 363   }
 364 }
 365 
 366 void LIRGenerator::CardTableBarrierSet_post_barrier_helper(LIR_Opr addr, LIR_Const* card_table_base) {
 367   assert(addr->is_register(), "must be a register at this point");
 368 
 369   LIR_Opr tmp = FrameMap::LR_ptr_opr;
 370 
 371   bool load_card_table_base_const = VM_Version::supports_movw();
 372   if (load_card_table_base_const) {
 373     __ move((LIR_Opr)card_table_base, tmp);
 374   } else {
 375     __ move(new LIR_Address(FrameMap::Rthread_opr, in_bytes(JavaThread::card_table_base_offset()), T_ADDRESS), tmp);
 376   }
 377 
 378   // Use unsigned type T_BOOLEAN here rather than (signed) T_BYTE since signed load
 379   // byte instruction does not support the addressing mode we need.
 380   LIR_Address* card_addr = new LIR_Address(tmp, addr, (LIR_Address::Scale) -CardTable::card_shift(), 0, T_BOOLEAN);
 381   if (UseCondCardMark) {
 382     LIR_Opr cur_value = new_register(T_INT);
 383     __ move(card_addr, cur_value);
 384 
 385     LabelObj* L_already_dirty = new LabelObj();
 386     __ cmp(lir_cond_equal, cur_value, LIR_OprFact::intConst(CardTable::dirty_card_val()));
 387     __ branch(lir_cond_equal, L_already_dirty->label());
 388     set_card(tmp, card_addr);
 389     __ branch_destination(L_already_dirty->label());
 390   } else {
 391     set_card(tmp, card_addr);
 392   }
 393 }
 394 
 395 void LIRGenerator::array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info, ciMethod* profiled_method, int profiled_bci) {
 396   LIR_Opr tmp1 = FrameMap::R0_oop_opr;
 397   LIR_Opr tmp2 = FrameMap::R1_oop_opr;
 398   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
 399   __ store_check(value, array, tmp1, tmp2, tmp3, store_check_info, profiled_method, profiled_bci);
 400 }
 401 
 402 //----------------------------------------------------------------------
 403 //             visitor functions
 404 //----------------------------------------------------------------------
 405 
 406 void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
 407   assert(x->is_pinned(),"");
 408   LIRItem obj(x->obj(), this);
 409   obj.load_item();
 410   set_no_result(x);
 411 
 412   LIR_Opr lock = new_pointer_register();
 413 
 414   CodeEmitInfo* info_for_exception = NULL;
 415   if (x->needs_null_check()) {
 416     info_for_exception = state_for(x);
 417   }
 418 
 419   CodeEmitInfo* info = state_for(x, x->state(), true);
 420   monitor_enter(obj.result(), lock, syncTempOpr(), new_register(T_INT), new_register(T_INT),
 421                 x->monitor_no(), info_for_exception, info);
 422 }
 423 
 424 
 425 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
 426   assert(x->is_pinned(),"");
 427   LIRItem obj(x->obj(), this);
 428   obj.dont_load_item();
 429   set_no_result(x);
 430 
 431   LIR_Opr obj_temp = new_pointer_register();
 432   LIR_Opr lock     = new_pointer_register();
 433   LIR_Opr hdr      = new_pointer_register();
 434 
 435   monitor_exit(obj_temp, lock, hdr, atomicLockOpr(), x->monitor_no());
 436 }
 437 
 438 
 439 // _ineg, _lneg, _fneg, _dneg
 440 void LIRGenerator::do_NegateOp(NegateOp* x) {
 441 #ifdef __SOFTFP__
 442   address runtime_func = NULL;
 443   ValueTag tag = x->type()->tag();
 444   if (tag == floatTag) {
 445     runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fneg);
 446   } else if (tag == doubleTag) {
 447     runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dneg);
 448   }
 449   if (runtime_func != NULL) {
 450     set_result(x, call_runtime(x->x(), runtime_func, x->type(), NULL));
 451     return;
 452   }
 453 #endif // __SOFTFP__
 454   LIRItem value(x->x(), this);
 455   value.load_item();
 456   LIR_Opr reg = rlock_result(x);
 457   __ negate(value.result(), reg);
 458 }
 459 
 460 
 461 // for  _fadd, _fmul, _fsub, _fdiv, _frem
 462 //      _dadd, _dmul, _dsub, _ddiv, _drem
 463 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
 464   address runtime_func;
 465   switch (x->op()) {
 466     case Bytecodes::_frem:
 467       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
 468       break;
 469     case Bytecodes::_drem:
 470       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
 471       break;
 472 #ifdef __SOFTFP__
 473     // Call function compiled with -msoft-float.
 474 
 475       // __aeabi_XXXX_glibc: Imported code from glibc soft-fp bundle for calculation accuracy improvement. See CR 6757269.
 476 
 477     case Bytecodes::_fadd:
 478       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fadd_glibc);
 479       break;
 480     case Bytecodes::_fmul:
 481       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fmul);
 482       break;
 483     case Bytecodes::_fsub:
 484       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fsub_glibc);
 485       break;
 486     case Bytecodes::_fdiv:
 487       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fdiv);
 488       break;
 489     case Bytecodes::_dadd:
 490       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dadd_glibc);
 491       break;
 492     case Bytecodes::_dmul:
 493       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dmul);
 494       break;
 495     case Bytecodes::_dsub:
 496       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dsub_glibc);
 497       break;
 498     case Bytecodes::_ddiv:
 499       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_ddiv);
 500       break;
 501     default:
 502       ShouldNotReachHere();
 503 #else // __SOFTFP__
 504     default: {
 505       LIRItem left(x->x(), this);
 506       LIRItem right(x->y(), this);
 507       left.load_item();
 508       right.load_item();
 509       rlock_result(x);
 510       arithmetic_op_fpu(x->op(), x->operand(), left.result(), right.result());
 511       return;
 512     }
 513 #endif // __SOFTFP__
 514   }
 515 
 516   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, x->type(), NULL);
 517   set_result(x, result);
 518 }
 519 
 520 
 521 void LIRGenerator::make_div_by_zero_check(LIR_Opr right_arg, BasicType type, CodeEmitInfo* info) {
 522   assert(right_arg->is_register(), "must be");
 523   __ cmp(lir_cond_equal, right_arg, make_constant(type, 0));
 524   __ branch(lir_cond_equal, new DivByZeroStub(info));
 525 }
 526 
 527 
 528 // for  _ladd, _lmul, _lsub, _ldiv, _lrem
 529 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
 530   CodeEmitInfo* info = NULL;
 531   if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem) {
 532     info = state_for(x);
 533   }
 534 
 535   switch (x->op()) {
 536     case Bytecodes::_ldiv:
 537     case Bytecodes::_lrem: {
 538       LIRItem right(x->y(), this);
 539       right.load_item();
 540       make_div_by_zero_check(right.result(), T_LONG, info);
 541     }
 542     // Fall through
 543     case Bytecodes::_lmul: {
 544       address entry;
 545       switch (x->op()) {
 546       case Bytecodes::_lrem:
 547         entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
 548         break;
 549       case Bytecodes::_ldiv:
 550         entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
 551         break;
 552       case Bytecodes::_lmul:
 553         entry = CAST_FROM_FN_PTR(address, SharedRuntime::lmul);
 554         break;
 555       default:
 556         ShouldNotReachHere();
 557         return;
 558       }
 559       LIR_Opr result = call_runtime(x->y(), x->x(), entry, x->type(), NULL);
 560       set_result(x, result);
 561       break;
 562     }
 563     case Bytecodes::_ladd:
 564     case Bytecodes::_lsub: {
 565       LIRItem left(x->x(), this);
 566       LIRItem right(x->y(), this);
 567       left.load_item();
 568       right.load_item();
 569       rlock_result(x);
 570       arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), NULL);
 571       break;
 572     }
 573     default:
 574       ShouldNotReachHere();
 575   }
 576 }
 577 
 578 
 579 // for: _iadd, _imul, _isub, _idiv, _irem
 580 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
 581   bool is_div_rem = x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem;
 582   LIRItem left(x->x(), this);
 583   LIRItem right(x->y(), this);
 584   LIRItem* left_arg = &left;
 585   LIRItem* right_arg = &right;
 586 
 587   // Test if instr is commutative and if we should swap
 588   if (x->is_commutative() && left.is_constant()) {
 589     left_arg = &right;
 590     right_arg = &left;
 591   }
 592 
 593   if (is_div_rem) {
 594     CodeEmitInfo* info = state_for(x);
 595     if (x->op() == Bytecodes::_idiv && right_arg->is_constant() && is_power_of_2(right_arg->get_jint_constant())) {
 596       left_arg->load_item();
 597       right_arg->dont_load_item();
 598       LIR_Opr tmp = LIR_OprFact::illegalOpr;
 599       LIR_Opr result = rlock_result(x);
 600       __ idiv(left_arg->result(), right_arg->result(), result, tmp, info);
 601     } else {
 602       left_arg->load_item_force(FrameMap::R0_opr);
 603       right_arg->load_item_force(FrameMap::R2_opr);
 604       LIR_Opr tmp = FrameMap::R1_opr;
 605       LIR_Opr result = rlock_result(x);
 606       LIR_Opr out_reg;
 607       if (x->op() == Bytecodes::_irem) {
 608         out_reg = FrameMap::R0_opr;
 609         __ irem(left_arg->result(), right_arg->result(), out_reg, tmp, info);
 610       } else { // (x->op() == Bytecodes::_idiv)
 611         out_reg = FrameMap::R1_opr;
 612         __ idiv(left_arg->result(), right_arg->result(), out_reg, tmp, info);
 613       }
 614       __ move(out_reg, result);
 615     }
 616 
 617 
 618   } else {
 619     left_arg->load_item();
 620     if (x->op() == Bytecodes::_imul && right_arg->is_constant()) {
 621       jint c = right_arg->get_jint_constant();
 622       if (c > 0 && c < max_jint && (is_power_of_2(c) || is_power_of_2(c - 1) || is_power_of_2(c + 1))) {
 623         right_arg->dont_load_item();
 624       } else {
 625         right_arg->load_item();
 626       }
 627     } else {
 628       right_arg->load_nonconstant();
 629     }
 630     rlock_result(x);
 631     assert(right_arg->is_constant() || right_arg->is_register(), "wrong state of right");
 632     arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), LIR_OprFact::nullOpr);
 633   }
 634 }
 635 
 636 
 637 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
 638   ValueTag tag = x->type()->tag();
 639   assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
 640   switch (tag) {
 641     case floatTag:
 642     case doubleTag:  do_ArithmeticOp_FPU(x);  return;
 643     case longTag:    do_ArithmeticOp_Long(x); return;
 644     case intTag:     do_ArithmeticOp_Int(x);  return;
 645     default:         ShouldNotReachHere();    return;
 646   }
 647 }
 648 
 649 
 650 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
 651 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
 652   LIRItem value(x->x(), this);
 653   LIRItem count(x->y(), this);
 654 
 655   if (value.type()->is_long()) {
 656     count.set_destroys_register();
 657   }
 658 
 659   if (count.is_constant()) {
 660     assert(count.type()->as_IntConstant() != NULL, "should be");
 661     count.dont_load_item();
 662   } else {
 663     count.load_item();
 664   }
 665   value.load_item();
 666 
 667   LIR_Opr res = rlock_result(x);
 668   shift_op(x->op(), res, value.result(), count.result(), LIR_OprFact::illegalOpr);
 669 }
 670 
 671 
 672 // _iand, _land, _ior, _lor, _ixor, _lxor
 673 void LIRGenerator::do_LogicOp(LogicOp* x) {
 674   LIRItem left(x->x(), this);
 675   LIRItem right(x->y(), this);
 676 
 677   left.load_item();
 678 
 679   right.load_nonconstant();
 680 
 681   logic_op(x->op(), rlock_result(x), left.result(), right.result());
 682 }
 683 
 684 
 685 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
 686 void LIRGenerator::do_CompareOp(CompareOp* x) {
 687 #ifdef __SOFTFP__
 688   address runtime_func;
 689   switch (x->op()) {
 690     case Bytecodes::_fcmpl:
 691       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fcmpl);
 692       break;
 693     case Bytecodes::_fcmpg:
 694       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fcmpg);
 695       break;
 696     case Bytecodes::_dcmpl:
 697       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcmpl);
 698       break;
 699     case Bytecodes::_dcmpg:
 700       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcmpg);
 701       break;
 702     case Bytecodes::_lcmp: {
 703         LIRItem left(x->x(), this);
 704         LIRItem right(x->y(), this);
 705         left.load_item();
 706         right.load_nonconstant();
 707         LIR_Opr reg = rlock_result(x);
 708          __ lcmp2int(left.result(), right.result(), reg);
 709         return;
 710       }
 711     default:
 712       ShouldNotReachHere();
 713   }
 714   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, x->type(), NULL);
 715   set_result(x, result);
 716 #else // __SOFTFP__
 717   LIRItem left(x->x(), this);
 718   LIRItem right(x->y(), this);
 719   left.load_item();
 720 
 721   right.load_nonconstant();
 722 
 723   LIR_Opr reg = rlock_result(x);
 724 
 725   if (x->x()->type()->is_float_kind()) {
 726     Bytecodes::Code code = x->op();
 727     __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
 728   } else if (x->x()->type()->tag() == longTag) {
 729     __ lcmp2int(left.result(), right.result(), reg);
 730   } else {
 731     ShouldNotReachHere();
 732   }
 733 #endif // __SOFTFP__
 734 }
 735 
 736 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
 737   LIR_Opr ill = LIR_OprFact::illegalOpr;  // for convenience
 738   LIR_Opr tmp1 = LIR_OprFact::illegalOpr;
 739   LIR_Opr tmp2 = LIR_OprFact::illegalOpr;
 740   new_value.load_item();
 741   cmp_value.load_item();
 742   LIR_Opr result = new_register(T_INT);
 743   if (type == T_OBJECT || type == T_ARRAY) {
 744     __ cas_obj(addr, cmp_value.result(), new_value.result(), new_register(T_INT), new_register(T_INT), result);
 745   } else if (type == T_INT) {
 746     __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), tmp1, tmp1, result);
 747   } else if (type == T_LONG) {
 748     tmp1 = new_register(T_LONG);
 749     __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), tmp1, tmp2, result);
 750   } else {
 751     ShouldNotReachHere();
 752   }
 753   return result;
 754 }
 755 
 756 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
 757   bool is_oop = type == T_OBJECT || type == T_ARRAY;
 758   LIR_Opr result = new_register(type);
 759   value.load_item();
 760   assert(type == T_INT || is_oop || (type == T_LONG && VM_Version::supports_ldrexd()), "unexpected type");
 761   LIR_Opr tmp = (UseCompressedOops && is_oop) ? new_pointer_register() : LIR_OprFact::illegalOpr;
 762   __ xchg(addr, value.result(), result, tmp);
 763   return result;
 764 }
 765 
 766 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
 767   LIR_Opr result = new_register(type);
 768   value.load_item();
 769   assert(type == T_INT || (type == T_LONG && VM_Version::supports_ldrexd ()), "unexpected type");
 770   LIR_Opr tmp = new_register(type);
 771   __ xadd(addr, value.result(), result, tmp);
 772   return result;
 773 }
 774 
 775 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
 776   address runtime_func;
 777   switch (x->id()) {
 778     case vmIntrinsics::_dabs: {
 779 #ifdef __SOFTFP__
 780       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dabs);
 781       break;
 782 #else
 783       assert(x->number_of_arguments() == 1, "wrong type");
 784       LIRItem value(x->argument_at(0), this);
 785       value.load_item();
 786       __ abs(value.result(), rlock_result(x), LIR_OprFact::illegalOpr);
 787       return;
 788 #endif // __SOFTFP__
 789     }
 790     case vmIntrinsics::_dsqrt:
 791     case vmIntrinsics::_dsqrt_strict: {
 792 #ifdef __SOFTFP__
 793       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt);
 794       break;
 795 #else
 796       assert(x->number_of_arguments() == 1, "wrong type");
 797       LIRItem value(x->argument_at(0), this);
 798       value.load_item();
 799       __ sqrt(value.result(), rlock_result(x), LIR_OprFact::illegalOpr);
 800       return;
 801 #endif // __SOFTFP__
 802     }
 803     case vmIntrinsics::_dsin:
 804       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 805       break;
 806     case vmIntrinsics::_dcos:
 807       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 808       break;
 809     case vmIntrinsics::_dtan:
 810       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 811       break;
 812     case vmIntrinsics::_dlog:
 813       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 814       break;
 815     case vmIntrinsics::_dlog10:
 816       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 817       break;
 818     case vmIntrinsics::_dexp:
 819       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 820       break;
 821     case vmIntrinsics::_dpow:
 822       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 823       break;
 824     default:
 825       ShouldNotReachHere();
 826       return;
 827   }
 828 
 829   LIR_Opr result;
 830   if (x->number_of_arguments() == 1) {
 831     result = call_runtime(x->argument_at(0), runtime_func, x->type(), NULL);
 832   } else {
 833     assert(x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow, "unexpected intrinsic");
 834     result = call_runtime(x->argument_at(0), x->argument_at(1), runtime_func, x->type(), NULL);
 835   }
 836   set_result(x, result);
 837 }
 838 
 839 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
 840   fatal("FMA intrinsic is not implemented on this platform");
 841 }
 842 
 843 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
 844   fatal("vectorizedMismatch intrinsic is not implemented on this platform");
 845 }
 846 
 847 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
 848   CodeEmitInfo* info = state_for(x, x->state());
 849   assert(x->number_of_arguments() == 5, "wrong type");
 850   LIRItem src(x->argument_at(0), this);
 851   LIRItem src_pos(x->argument_at(1), this);
 852   LIRItem dst(x->argument_at(2), this);
 853   LIRItem dst_pos(x->argument_at(3), this);
 854   LIRItem length(x->argument_at(4), this);
 855 
 856   // We put arguments into the same registers which are used for a Java call.
 857   // Note: we used fixed registers for all arguments because all registers
 858   // are caller-saved, so register allocator treats them all as used.
 859   src.load_item_force    (FrameMap::R0_oop_opr);
 860   src_pos.load_item_force(FrameMap::R1_opr);
 861   dst.load_item_force    (FrameMap::R2_oop_opr);
 862   dst_pos.load_item_force(FrameMap::R3_opr);
 863   length.load_item_force (FrameMap::R4_opr);
 864   LIR_Opr tmp =          (FrameMap::R5_opr);
 865   set_no_result(x);
 866 
 867   int flags;
 868   ciArrayKlass* expected_type;
 869   arraycopy_helper(x, &flags, &expected_type);
 870   __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(),
 871                tmp, expected_type, flags, info);
 872 }
 873 
 874 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
 875   fatal("CRC32 intrinsic is not implemented on this platform");
 876 }
 877 
 878 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
 879   Unimplemented();
 880 }
 881 
 882 void LIRGenerator::do_Convert(Convert* x) {
 883   address runtime_func;
 884   switch (x->op()) {
 885     case Bytecodes::_l2f:
 886       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::l2f);
 887       break;
 888     case Bytecodes::_l2d:
 889       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::l2d);
 890       break;
 891     case Bytecodes::_f2l:
 892       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::f2l);
 893       break;
 894     case Bytecodes::_d2l:
 895       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::d2l);
 896       break;
 897 #ifdef __SOFTFP__
 898     case Bytecodes::_f2d:
 899       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_f2d);
 900       break;
 901     case Bytecodes::_d2f:
 902       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_d2f);
 903       break;
 904     case Bytecodes::_i2f:
 905       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_i2f);
 906       break;
 907     case Bytecodes::_i2d:
 908       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_i2d);
 909       break;
 910     case Bytecodes::_f2i:
 911       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_f2iz);
 912       break;
 913     case Bytecodes::_d2i:
 914       // This is implemented in hard float in assembler on arm but a call
 915       // on other platforms.
 916       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::d2i);
 917       break;
 918 #endif // __SOFTFP__
 919     default: {
 920       LIRItem value(x->value(), this);
 921       value.load_item();
 922       LIR_Opr reg = rlock_result(x);
 923       __ convert(x->op(), value.result(), reg, NULL);
 924       return;
 925     }
 926   }
 927 
 928   LIR_Opr result = call_runtime(x->value(), runtime_func, x->type(), NULL);
 929   set_result(x, result);
 930 }
 931 
 932 
 933 void LIRGenerator::do_NewInstance(NewInstance* x) {
 934   print_if_not_loaded(x);
 935 
 936   CodeEmitInfo* info = state_for(x, x->state());
 937   LIR_Opr reg = result_register_for(x->type());  // R0 is required by runtime call in NewInstanceStub::emit_code
 938   LIR_Opr klass_reg = FrameMap::R1_metadata_opr; // R1 is required by runtime call in NewInstanceStub::emit_code
 939   LIR_Opr tmp1 = new_register(objectType);
 940   LIR_Opr tmp2 = new_register(objectType);
 941   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
 942 
 943   new_instance(reg, x->klass(), x->is_unresolved(), tmp1, tmp2, tmp3,
 944                LIR_OprFact::illegalOpr, klass_reg, info);
 945 
 946   LIR_Opr result = rlock_result(x);
 947   __ move(reg, result);
 948 }
 949 
 950 
 951 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
 952   // Evaluate state_for() first, because it can emit code
 953   // with the same fixed registers that are used here (R1, R2)
 954   CodeEmitInfo* info = state_for(x, x->state());
 955   LIRItem length(x->length(), this);
 956 
 957   length.load_item_force(FrameMap::R2_opr);      // R2 is required by runtime call in NewTypeArrayStub::emit_code
 958   LIR_Opr len = length.result();
 959 
 960   LIR_Opr reg = result_register_for(x->type());  // R0 is required by runtime call in NewTypeArrayStub::emit_code
 961   LIR_Opr klass_reg = FrameMap::R1_metadata_opr; // R1 is required by runtime call in NewTypeArrayStub::emit_code
 962 
 963   LIR_Opr tmp1 = new_register(objectType);
 964   LIR_Opr tmp2 = new_register(objectType);
 965   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
 966   LIR_Opr tmp4 = LIR_OprFact::illegalOpr;
 967 
 968   BasicType elem_type = x->elt_type();
 969   __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
 970 
 971   CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
 972   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path);
 973 
 974   LIR_Opr result = rlock_result(x);
 975   __ move(reg, result);
 976 }
 977 
 978 
 979 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
 980   // Evaluate state_for() first, because it can emit code
 981   // with the same fixed registers that are used here (R1, R2)
 982   CodeEmitInfo* info = state_for(x, x->state());
 983   LIRItem length(x->length(), this);
 984 
 985   length.load_item_force(FrameMap::R2_opr);           // R2 is required by runtime call in NewObjectArrayStub::emit_code
 986   LIR_Opr len = length.result();
 987 
 988   CodeEmitInfo* patching_info = NULL;
 989   if (!x->klass()->is_loaded() || PatchALot) {
 990     patching_info = state_for(x, x->state_before());
 991   }
 992 
 993   LIR_Opr reg = result_register_for(x->type());       // R0 is required by runtime call in NewObjectArrayStub::emit_code
 994   LIR_Opr klass_reg = FrameMap::R1_metadata_opr;      // R1 is required by runtime call in NewObjectArrayStub::emit_code
 995 
 996   LIR_Opr tmp1 = new_register(objectType);
 997   LIR_Opr tmp2 = new_register(objectType);
 998   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
 999   LIR_Opr tmp4 = LIR_OprFact::illegalOpr;
1000 
1001   CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info);
1002   ciMetadata* obj = ciObjArrayKlass::make(x->klass());
1003   if (obj == ciEnv::unloaded_ciobjarrayklass()) {
1004     BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
1005   }
1006   klass2reg_with_patching(klass_reg, obj, patching_info);
1007   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path);
1008 
1009   LIR_Opr result = rlock_result(x);
1010   __ move(reg, result);
1011 }
1012 
1013 
1014 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1015   Values* dims = x->dims();
1016   int i = dims->length();
1017   LIRItemList* items = new LIRItemList(i, i, NULL);
1018   while (i-- > 0) {
1019     LIRItem* size = new LIRItem(dims->at(i), this);
1020     items->at_put(i, size);
1021   }
1022 
1023   // Need to get the info before, as the items may become invalid through item_free
1024   CodeEmitInfo* patching_info = NULL;
1025   if (!x->klass()->is_loaded() || PatchALot) {
1026     patching_info = state_for(x, x->state_before());
1027 
1028     // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1029     // clone all handlers (NOTE: Usually this is handled transparently
1030     // by the CodeEmitInfo cloning logic in CodeStub constructors but
1031     // is done explicitly here because a stub isn't being used).
1032     x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1033   }
1034 
1035   i = dims->length();
1036   while (i-- > 0) {
1037     LIRItem* size = items->at(i);
1038     size->load_item();
1039     LIR_Opr sz = size->result();
1040     assert(sz->type() == T_INT, "should be");
1041     store_stack_parameter(sz, in_ByteSize(i * BytesPerInt));
1042   }
1043 
1044   CodeEmitInfo* info = state_for(x, x->state());
1045   LIR_Opr klass_reg = FrameMap::R0_metadata_opr;
1046   klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1047 
1048   LIR_Opr rank = FrameMap::R2_opr;
1049   __ move(LIR_OprFact::intConst(x->rank()), rank);
1050   LIR_Opr varargs = FrameMap::SP_opr;
1051   LIR_OprList* args = new LIR_OprList(3);
1052   args->append(klass_reg);
1053   args->append(rank);
1054   args->append(varargs);
1055   LIR_Opr reg = result_register_for(x->type());
1056   __ call_runtime(Runtime1::entry_for(Runtime1::new_multi_array_id),
1057                   LIR_OprFact::illegalOpr, reg, args, info);
1058 
1059   LIR_Opr result = rlock_result(x);
1060   __ move(reg, result);
1061 }
1062 
1063 
1064 void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1065   // nothing to do for now
1066 }
1067 
1068 
1069 void LIRGenerator::do_CheckCast(CheckCast* x) {
1070   LIRItem obj(x->obj(), this);
1071   CodeEmitInfo* patching_info = NULL;
1072   if (!x->klass()->is_loaded() || (PatchALot && !x->is_incompatible_class_change_check() && !x->is_invokespecial_receiver_check())) {
1073     patching_info = state_for(x, x->state_before());
1074   }
1075 
1076   obj.load_item();
1077 
1078   CodeEmitInfo* info_for_exception =
1079     (x->needs_exception_state() ? state_for(x) :
1080                                   state_for(x, x->state_before(), true /*ignore_xhandler*/));
1081 
1082   CodeStub* stub;
1083   if (x->is_incompatible_class_change_check()) {
1084     assert(patching_info == NULL, "can't patch this");
1085     stub = new SimpleExceptionStub(Runtime1::throw_incompatible_class_change_error_id,
1086                                    LIR_OprFact::illegalOpr, info_for_exception);
1087   } else if (x->is_invokespecial_receiver_check()) {
1088     assert(patching_info == NULL, "can't patch this");
1089     stub = new DeoptimizeStub(info_for_exception,
1090                               Deoptimization::Reason_class_check,
1091                               Deoptimization::Action_none);
1092   } else {
1093     stub = new SimpleExceptionStub(Runtime1::throw_class_cast_exception_id,
1094                                    LIR_OprFact::illegalOpr, info_for_exception);
1095   }
1096 
1097   LIR_Opr out_reg = rlock_result(x);
1098   LIR_Opr tmp1 = FrameMap::R0_oop_opr;
1099   LIR_Opr tmp2 = FrameMap::R1_oop_opr;
1100   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1101 
1102   __ checkcast(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3, x->direct_compare(),
1103                info_for_exception, patching_info, stub, x->profiled_method(), x->profiled_bci());
1104 }
1105 
1106 
1107 void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1108   LIRItem obj(x->obj(), this);
1109   CodeEmitInfo* patching_info = NULL;
1110   if (!x->klass()->is_loaded() || PatchALot) {
1111     patching_info = state_for(x, x->state_before());
1112   }
1113 
1114   obj.load_item();
1115   LIR_Opr out_reg = rlock_result(x);
1116   LIR_Opr tmp1 = FrameMap::R0_oop_opr;
1117   LIR_Opr tmp2 = FrameMap::R1_oop_opr;
1118   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1119 
1120   __ instanceof(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,
1121                 x->direct_compare(), patching_info, x->profiled_method(), x->profiled_bci());
1122 }
1123 
1124 
1125 #ifdef __SOFTFP__
1126 // Turn operator if (f <op> g) into runtime call:
1127 //     call _aeabi_fcmp<op>(f, g)
1128 //     cmp(eq, 1)
1129 //     branch(eq, true path).
1130 void LIRGenerator::do_soft_float_compare(If* x) {
1131   assert(x->number_of_sux() == 2, "inconsistency");
1132   ValueTag tag = x->x()->type()->tag();
1133   If::Condition cond = x->cond();
1134   address runtime_func;
1135   // unordered comparison gets the wrong answer because aeabi functions
1136   //  return false.
1137   bool unordered_is_true = x->unordered_is_true();
1138   // reverse of condition for ne
1139   bool compare_to_zero = false;
1140   switch (lir_cond(cond)) {
1141     case lir_cond_notEqual:
1142       compare_to_zero = true;  // fall through
1143     case lir_cond_equal:
1144       runtime_func = tag == floatTag ?
1145           CAST_FROM_FN_PTR(address, __aeabi_fcmpeq):
1146           CAST_FROM_FN_PTR(address, __aeabi_dcmpeq);
1147       break;
1148     case lir_cond_less:
1149       if (unordered_is_true) {
1150         runtime_func = tag == floatTag ?
1151           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmplt):
1152           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmplt);
1153       } else {
1154         runtime_func = tag == floatTag ?
1155           CAST_FROM_FN_PTR(address, __aeabi_fcmplt):
1156           CAST_FROM_FN_PTR(address, __aeabi_dcmplt);
1157       }
1158       break;
1159     case lir_cond_lessEqual:
1160       if (unordered_is_true) {
1161         runtime_func = tag == floatTag ?
1162           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmple):
1163           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmple);
1164       } else {
1165         runtime_func = tag == floatTag ?
1166           CAST_FROM_FN_PTR(address, __aeabi_fcmple):
1167           CAST_FROM_FN_PTR(address, __aeabi_dcmple);
1168       }
1169       break;
1170     case lir_cond_greaterEqual:
1171       if (unordered_is_true) {
1172         runtime_func = tag == floatTag ?
1173           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmpge):
1174           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmpge);
1175       } else {
1176         runtime_func = tag == floatTag ?
1177           CAST_FROM_FN_PTR(address, __aeabi_fcmpge):
1178           CAST_FROM_FN_PTR(address, __aeabi_dcmpge);
1179       }
1180       break;
1181     case lir_cond_greater:
1182       if (unordered_is_true) {
1183         runtime_func = tag == floatTag ?
1184           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmpgt):
1185           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmpgt);
1186       } else {
1187         runtime_func = tag == floatTag ?
1188           CAST_FROM_FN_PTR(address, __aeabi_fcmpgt):
1189           CAST_FROM_FN_PTR(address, __aeabi_dcmpgt);
1190       }
1191       break;
1192     case lir_cond_aboveEqual:
1193     case lir_cond_belowEqual:
1194       ShouldNotReachHere();  // We're not going to get these.
1195     default:
1196       assert(lir_cond(cond) == lir_cond_always, "must be");
1197       ShouldNotReachHere();
1198   }
1199   set_no_result(x);
1200 
1201   // add safepoint before generating condition code so it can be recomputed
1202   if (x->is_safepoint()) {
1203     increment_backedge_counter(state_for(x, x->state_before()), x->profiled_bci());
1204     __ safepoint(LIR_OprFact::illegalOpr, state_for(x, x->state_before()));
1205   }
1206   // Call float compare function, returns (1,0) if true or false.
1207   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, intType, NULL);
1208   __ cmp(lir_cond_equal, result,
1209          compare_to_zero ?
1210            LIR_OprFact::intConst(0) : LIR_OprFact::intConst(1));
1211   profile_branch(x, cond);
1212   move_to_phi(x->state());
1213   __ branch(lir_cond_equal, x->tsux());
1214 }
1215 #endif // __SOFTFP__
1216 
1217 void LIRGenerator::do_If(If* x) {
1218   assert(x->number_of_sux() == 2, "inconsistency");
1219   ValueTag tag = x->x()->type()->tag();
1220 
1221 #ifdef __SOFTFP__
1222   if (tag == floatTag || tag == doubleTag) {
1223     do_soft_float_compare(x);
1224     assert(x->default_sux() == x->fsux(), "wrong destination above");
1225     __ jump(x->default_sux());
1226     return;
1227   }
1228 #endif // __SOFTFP__
1229 
1230   LIRItem xitem(x->x(), this);
1231   LIRItem yitem(x->y(), this);
1232   LIRItem* xin = &xitem;
1233   LIRItem* yin = &yitem;
1234   If::Condition cond = x->cond();
1235 
1236   if (tag == longTag) {
1237     if (cond == If::gtr || cond == If::leq) {
1238       cond = Instruction::mirror(cond);
1239       xin = &yitem;
1240       yin = &xitem;
1241     }
1242     xin->set_destroys_register();
1243   }
1244 
1245   xin->load_item();
1246   LIR_Opr left = xin->result();
1247   LIR_Opr right;
1248 
1249   if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 &&
1250       (cond == If::eql || cond == If::neq)) {
1251     // inline long zero
1252     right = LIR_OprFact::value_type(yin->value()->type());
1253   } else {
1254     yin->load_nonconstant();
1255     right = yin->result();
1256   }
1257 
1258   set_no_result(x);
1259 
1260   // add safepoint before generating condition code so it can be recomputed
1261   if (x->is_safepoint()) {
1262     increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1263         x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1264     __ safepoint(LIR_OprFact::illegalOpr, state_for(x, x->state_before()));
1265   }
1266 
1267   __ cmp(lir_cond(cond), left, right);
1268   profile_branch(x, cond);
1269   move_to_phi(x->state());
1270   if (x->x()->type()->is_float_kind()) {
1271     __ branch(lir_cond(cond), x->tsux(), x->usux());
1272   } else {
1273     __ branch(lir_cond(cond), x->tsux());
1274   }
1275   assert(x->default_sux() == x->fsux(), "wrong destination above");
1276   __ jump(x->default_sux());
1277 }
1278 
1279 
1280 LIR_Opr LIRGenerator::getThreadPointer() {
1281   return FrameMap::Rthread_opr;
1282 }
1283 
1284 void LIRGenerator::trace_block_entry(BlockBegin* block) {
1285   __ move(LIR_OprFact::intConst(block->block_id()), FrameMap::R0_opr);
1286   LIR_OprList* args = new LIR_OprList(1);
1287   args->append(FrameMap::R0_opr);
1288   address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1289   __ call_runtime_leaf(func, getThreadTemp(), LIR_OprFact::illegalOpr, args);
1290 }
1291 
1292 
1293 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1294                                         CodeEmitInfo* info) {
1295   if (value->is_double_cpu()) {
1296     assert(address->index()->is_illegal(), "should have a constant displacement");
1297     LIR_Address* store_addr = NULL;
1298     if (address->disp() != 0) {
1299       LIR_Opr tmp = new_pointer_register();
1300       add_large_constant(address->base(), address->disp(), tmp);
1301       store_addr = new LIR_Address(tmp, (intx)0, address->type());
1302     } else {
1303       // address->disp() can be 0, if the address is referenced using the unsafe intrinsic
1304       store_addr = address;
1305     }
1306     __ volatile_store_mem_reg(value, store_addr, info);
1307     return;
1308   }
1309   __ store(value, address, info, lir_patch_none);
1310 }
1311 
1312 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1313                                        CodeEmitInfo* info) {
1314   if (result->is_double_cpu()) {
1315     assert(address->index()->is_illegal(), "should have a constant displacement");
1316     LIR_Address* load_addr = NULL;
1317     if (address->disp() != 0) {
1318       LIR_Opr tmp = new_pointer_register();
1319       add_large_constant(address->base(), address->disp(), tmp);
1320       load_addr = new LIR_Address(tmp, (intx)0, address->type());
1321     } else {
1322       // address->disp() can be 0, if the address is referenced using the unsafe intrinsic
1323       load_addr = address;
1324     }
1325     __ volatile_load_mem_reg(load_addr, result, info);
1326     return;
1327   }
1328   __ load(address, result, info, lir_patch_none);
1329 }