1 /*
   2  * Copyright (c) 2008, 2021, 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(int 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(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   LIR_Opr hdr  = new_pointer_register();
 414 
 415   CodeEmitInfo* info_for_exception = NULL;
 416   if (x->needs_null_check()) {
 417     info_for_exception = state_for(x);
 418   }
 419 
 420   CodeEmitInfo* info = state_for(x, x->state(), true);
 421   monitor_enter(obj.result(), lock, hdr, LIR_OprFact::illegalOpr,
 422                 x->monitor_no(), info_for_exception, info);
 423 }
 424 
 425 
 426 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
 427   assert(x->is_pinned(),"");
 428   LIRItem obj(x->obj(), this);
 429   obj.dont_load_item();
 430   set_no_result(x);
 431 
 432   LIR_Opr obj_temp = new_pointer_register();
 433   LIR_Opr lock     = new_pointer_register();
 434   LIR_Opr hdr      = new_pointer_register();
 435 
 436   monitor_exit(obj_temp, lock, hdr, atomicLockOpr(), x->monitor_no());
 437 }
 438 
 439 
 440 // _ineg, _lneg, _fneg, _dneg
 441 void LIRGenerator::do_NegateOp(NegateOp* x) {
 442 #ifdef __SOFTFP__
 443   address runtime_func = NULL;
 444   ValueTag tag = x->type()->tag();
 445   if (tag == floatTag) {
 446     runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fneg);
 447   } else if (tag == doubleTag) {
 448     runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dneg);
 449   }
 450   if (runtime_func != NULL) {
 451     set_result(x, call_runtime(x->x(), runtime_func, x->type(), NULL));
 452     return;
 453   }
 454 #endif // __SOFTFP__
 455   LIRItem value(x->x(), this);
 456   value.load_item();
 457   LIR_Opr reg = rlock_result(x);
 458   __ negate(value.result(), reg);
 459 }
 460 
 461 
 462 // for  _fadd, _fmul, _fsub, _fdiv, _frem
 463 //      _dadd, _dmul, _dsub, _ddiv, _drem
 464 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
 465   address runtime_func;
 466   switch (x->op()) {
 467     case Bytecodes::_frem:
 468       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
 469       break;
 470     case Bytecodes::_drem:
 471       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
 472       break;
 473 #ifdef __SOFTFP__
 474     // Call function compiled with -msoft-float.
 475 
 476       // __aeabi_XXXX_glibc: Imported code from glibc soft-fp bundle for calculation accuracy improvement. See CR 6757269.
 477 
 478     case Bytecodes::_fadd:
 479       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fadd_glibc);
 480       break;
 481     case Bytecodes::_fmul:
 482       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fmul);
 483       break;
 484     case Bytecodes::_fsub:
 485       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fsub_glibc);
 486       break;
 487     case Bytecodes::_fdiv:
 488       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_fdiv);
 489       break;
 490     case Bytecodes::_dadd:
 491       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dadd_glibc);
 492       break;
 493     case Bytecodes::_dmul:
 494       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dmul);
 495       break;
 496     case Bytecodes::_dsub:
 497       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_dsub_glibc);
 498       break;
 499     case Bytecodes::_ddiv:
 500       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_ddiv);
 501       break;
 502     default:
 503       ShouldNotReachHere();
 504 #else // __SOFTFP__
 505     default: {
 506       LIRItem left(x->x(), this);
 507       LIRItem right(x->y(), this);
 508       left.load_item();
 509       right.load_item();
 510       rlock_result(x);
 511       arithmetic_op_fpu(x->op(), x->operand(), left.result(), right.result());
 512       return;
 513     }
 514 #endif // __SOFTFP__
 515   }
 516 
 517   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, x->type(), NULL);
 518   set_result(x, result);
 519 }
 520 
 521 
 522 void LIRGenerator::make_div_by_zero_check(LIR_Opr right_arg, BasicType type, CodeEmitInfo* info) {
 523   assert(right_arg->is_register(), "must be");
 524   __ cmp(lir_cond_equal, right_arg, make_constant(type, 0));
 525   __ branch(lir_cond_equal, new DivByZeroStub(info));
 526 }
 527 
 528 
 529 // for  _ladd, _lmul, _lsub, _ldiv, _lrem
 530 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
 531   CodeEmitInfo* info = NULL;
 532   if (x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem) {
 533     info = state_for(x);
 534   }
 535 
 536   switch (x->op()) {
 537     case Bytecodes::_ldiv:
 538     case Bytecodes::_lrem: {
 539       LIRItem right(x->y(), this);
 540       right.load_item();
 541       make_div_by_zero_check(right.result(), T_LONG, info);
 542     }
 543     // Fall through
 544     case Bytecodes::_lmul: {
 545       address entry;
 546       switch (x->op()) {
 547       case Bytecodes::_lrem:
 548         entry = CAST_FROM_FN_PTR(address, SharedRuntime::lrem);
 549         break;
 550       case Bytecodes::_ldiv:
 551         entry = CAST_FROM_FN_PTR(address, SharedRuntime::ldiv);
 552         break;
 553       case Bytecodes::_lmul:
 554         entry = CAST_FROM_FN_PTR(address, SharedRuntime::lmul);
 555         break;
 556       default:
 557         ShouldNotReachHere();
 558         return;
 559       }
 560       LIR_Opr result = call_runtime(x->y(), x->x(), entry, x->type(), NULL);
 561       set_result(x, result);
 562       break;
 563     }
 564     case Bytecodes::_ladd:
 565     case Bytecodes::_lsub: {
 566       LIRItem left(x->x(), this);
 567       LIRItem right(x->y(), this);
 568       left.load_item();
 569       right.load_item();
 570       rlock_result(x);
 571       arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), NULL);
 572       break;
 573     }
 574     default:
 575       ShouldNotReachHere();
 576   }
 577 }
 578 
 579 
 580 // for: _iadd, _imul, _isub, _idiv, _irem
 581 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
 582   bool is_div_rem = x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem;
 583   LIRItem left(x->x(), this);
 584   LIRItem right(x->y(), this);
 585   LIRItem* left_arg = &left;
 586   LIRItem* right_arg = &right;
 587 
 588   // Test if instr is commutative and if we should swap
 589   if (x->is_commutative() && left.is_constant()) {
 590     left_arg = &right;
 591     right_arg = &left;
 592   }
 593 
 594   if (is_div_rem) {
 595     CodeEmitInfo* info = state_for(x);
 596     if (x->op() == Bytecodes::_idiv && right_arg->is_constant() && is_power_of_2(right_arg->get_jint_constant())) {
 597       left_arg->load_item();
 598       right_arg->dont_load_item();
 599       LIR_Opr tmp = LIR_OprFact::illegalOpr;
 600       LIR_Opr result = rlock_result(x);
 601       __ idiv(left_arg->result(), right_arg->result(), result, tmp, info);
 602     } else {
 603       left_arg->load_item_force(FrameMap::R0_opr);
 604       right_arg->load_item_force(FrameMap::R2_opr);
 605       LIR_Opr tmp = FrameMap::R1_opr;
 606       LIR_Opr result = rlock_result(x);
 607       LIR_Opr out_reg;
 608       if (x->op() == Bytecodes::_irem) {
 609         out_reg = FrameMap::R0_opr;
 610         __ irem(left_arg->result(), right_arg->result(), out_reg, tmp, info);
 611       } else { // (x->op() == Bytecodes::_idiv)
 612         out_reg = FrameMap::R1_opr;
 613         __ idiv(left_arg->result(), right_arg->result(), out_reg, tmp, info);
 614       }
 615       __ move(out_reg, result);
 616     }
 617 
 618 
 619   } else {
 620     left_arg->load_item();
 621     if (x->op() == Bytecodes::_imul && right_arg->is_constant()) {
 622       jint c = right_arg->get_jint_constant();
 623       if (c > 0 && c < max_jint && (is_power_of_2(c) || is_power_of_2(c - 1) || is_power_of_2(c + 1))) {
 624         right_arg->dont_load_item();
 625       } else {
 626         right_arg->load_item();
 627       }
 628     } else {
 629       right_arg->load_nonconstant();
 630     }
 631     rlock_result(x);
 632     assert(right_arg->is_constant() || right_arg->is_register(), "wrong state of right");
 633     arithmetic_op_int(x->op(), x->operand(), left_arg->result(), right_arg->result(), LIR_OprFact::nullOpr);
 634   }
 635 }
 636 
 637 
 638 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
 639   ValueTag tag = x->type()->tag();
 640   assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
 641   switch (tag) {
 642     case floatTag:
 643     case doubleTag:  do_ArithmeticOp_FPU(x);  return;
 644     case longTag:    do_ArithmeticOp_Long(x); return;
 645     case intTag:     do_ArithmeticOp_Int(x);  return;
 646     default:         ShouldNotReachHere();    return;
 647   }
 648 }
 649 
 650 
 651 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
 652 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
 653   LIRItem value(x->x(), this);
 654   LIRItem count(x->y(), this);
 655 
 656   if (value.type()->is_long()) {
 657     count.set_destroys_register();
 658   }
 659 
 660   if (count.is_constant()) {
 661     assert(count.type()->as_IntConstant() != NULL, "should be");
 662     count.dont_load_item();
 663   } else {
 664     count.load_item();
 665   }
 666   value.load_item();
 667 
 668   LIR_Opr res = rlock_result(x);
 669   shift_op(x->op(), res, value.result(), count.result(), LIR_OprFact::illegalOpr);
 670 }
 671 
 672 
 673 // _iand, _land, _ior, _lor, _ixor, _lxor
 674 void LIRGenerator::do_LogicOp(LogicOp* x) {
 675   LIRItem left(x->x(), this);
 676   LIRItem right(x->y(), this);
 677 
 678   left.load_item();
 679 
 680   right.load_nonconstant();
 681 
 682   logic_op(x->op(), rlock_result(x), left.result(), right.result());
 683 }
 684 
 685 
 686 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
 687 void LIRGenerator::do_CompareOp(CompareOp* x) {
 688 #ifdef __SOFTFP__
 689   address runtime_func;
 690   switch (x->op()) {
 691     case Bytecodes::_fcmpl:
 692       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fcmpl);
 693       break;
 694     case Bytecodes::_fcmpg:
 695       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::fcmpg);
 696       break;
 697     case Bytecodes::_dcmpl:
 698       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcmpl);
 699       break;
 700     case Bytecodes::_dcmpg:
 701       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcmpg);
 702       break;
 703     case Bytecodes::_lcmp: {
 704         LIRItem left(x->x(), this);
 705         LIRItem right(x->y(), this);
 706         left.load_item();
 707         right.load_nonconstant();
 708         LIR_Opr reg = rlock_result(x);
 709          __ lcmp2int(left.result(), right.result(), reg);
 710         return;
 711       }
 712     default:
 713       ShouldNotReachHere();
 714   }
 715   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, x->type(), NULL);
 716   set_result(x, result);
 717 #else // __SOFTFP__
 718   LIRItem left(x->x(), this);
 719   LIRItem right(x->y(), this);
 720   left.load_item();
 721 
 722   right.load_nonconstant();
 723 
 724   LIR_Opr reg = rlock_result(x);
 725 
 726   if (x->x()->type()->is_float_kind()) {
 727     Bytecodes::Code code = x->op();
 728     __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
 729   } else if (x->x()->type()->tag() == longTag) {
 730     __ lcmp2int(left.result(), right.result(), reg);
 731   } else {
 732     ShouldNotReachHere();
 733   }
 734 #endif // __SOFTFP__
 735 }
 736 
 737 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
 738   LIR_Opr ill = LIR_OprFact::illegalOpr;  // for convenience
 739   LIR_Opr tmp1 = LIR_OprFact::illegalOpr;
 740   LIR_Opr tmp2 = LIR_OprFact::illegalOpr;
 741   new_value.load_item();
 742   cmp_value.load_item();
 743   LIR_Opr result = new_register(T_INT);
 744   if (type == T_OBJECT || type == T_ARRAY) {
 745     __ cas_obj(addr, cmp_value.result(), new_value.result(), new_register(T_INT), new_register(T_INT), result);
 746   } else if (type == T_INT) {
 747     __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), tmp1, tmp1, result);
 748   } else if (type == T_LONG) {
 749     tmp1 = new_register(T_LONG);
 750     __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), tmp1, tmp2, result);
 751   } else {
 752     ShouldNotReachHere();
 753   }
 754   return result;
 755 }
 756 
 757 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
 758   bool is_oop = type == T_OBJECT || type == T_ARRAY;
 759   LIR_Opr result = new_register(type);
 760   value.load_item();
 761   assert(type == T_INT || is_oop || (type == T_LONG && VM_Version::supports_ldrexd()), "unexpected type");
 762   LIR_Opr tmp = (UseCompressedOops && is_oop) ? new_pointer_register() : LIR_OprFact::illegalOpr;
 763   __ xchg(addr, value.result(), result, tmp);
 764   return result;
 765 }
 766 
 767 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
 768   LIR_Opr result = new_register(type);
 769   value.load_item();
 770   assert(type == T_INT || (type == T_LONG && VM_Version::supports_ldrexd ()), "unexpected type");
 771   LIR_Opr tmp = new_register(type);
 772   __ xadd(addr, value.result(), result, tmp);
 773   return result;
 774 }
 775 
 776 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
 777   address runtime_func;
 778   switch (x->id()) {
 779     case vmIntrinsics::_dabs: {
 780 #ifdef __SOFTFP__
 781       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dabs);
 782       break;
 783 #else
 784       assert(x->number_of_arguments() == 1, "wrong type");
 785       LIRItem value(x->argument_at(0), this);
 786       value.load_item();
 787       __ abs(value.result(), rlock_result(x), LIR_OprFact::illegalOpr);
 788       return;
 789 #endif // __SOFTFP__
 790     }
 791     case vmIntrinsics::_dsqrt:
 792     case vmIntrinsics::_dsqrt_strict: {
 793 #ifdef __SOFTFP__
 794       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt);
 795       break;
 796 #else
 797       assert(x->number_of_arguments() == 1, "wrong type");
 798       LIRItem value(x->argument_at(0), this);
 799       value.load_item();
 800       __ sqrt(value.result(), rlock_result(x), LIR_OprFact::illegalOpr);
 801       return;
 802 #endif // __SOFTFP__
 803     }
 804     case vmIntrinsics::_dsin:
 805       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 806       break;
 807     case vmIntrinsics::_dcos:
 808       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 809       break;
 810     case vmIntrinsics::_dtan:
 811       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 812       break;
 813     case vmIntrinsics::_dlog:
 814       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 815       break;
 816     case vmIntrinsics::_dlog10:
 817       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 818       break;
 819     case vmIntrinsics::_dexp:
 820       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 821       break;
 822     case vmIntrinsics::_dpow:
 823       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 824       break;
 825     default:
 826       ShouldNotReachHere();
 827       return;
 828   }
 829 
 830   LIR_Opr result;
 831   if (x->number_of_arguments() == 1) {
 832     result = call_runtime(x->argument_at(0), runtime_func, x->type(), NULL);
 833   } else {
 834     assert(x->number_of_arguments() == 2 && x->id() == vmIntrinsics::_dpow, "unexpected intrinsic");
 835     result = call_runtime(x->argument_at(0), x->argument_at(1), runtime_func, x->type(), NULL);
 836   }
 837   set_result(x, result);
 838 }
 839 
 840 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
 841   fatal("FMA intrinsic is not implemented on this platform");
 842 }
 843 
 844 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
 845   fatal("vectorizedMismatch intrinsic is not implemented on this platform");
 846 }
 847 
 848 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
 849   CodeEmitInfo* info = state_for(x, x->state());
 850   assert(x->number_of_arguments() == 5, "wrong type");
 851   LIRItem src(x->argument_at(0), this);
 852   LIRItem src_pos(x->argument_at(1), this);
 853   LIRItem dst(x->argument_at(2), this);
 854   LIRItem dst_pos(x->argument_at(3), this);
 855   LIRItem length(x->argument_at(4), this);
 856 
 857   // We put arguments into the same registers which are used for a Java call.
 858   // Note: we used fixed registers for all arguments because all registers
 859   // are caller-saved, so register allocator treats them all as used.
 860   src.load_item_force    (FrameMap::R0_oop_opr);
 861   src_pos.load_item_force(FrameMap::R1_opr);
 862   dst.load_item_force    (FrameMap::R2_oop_opr);
 863   dst_pos.load_item_force(FrameMap::R3_opr);
 864   length.load_item_force (FrameMap::R4_opr);
 865   LIR_Opr tmp =          (FrameMap::R5_opr);
 866   set_no_result(x);
 867 
 868   int flags;
 869   ciArrayKlass* expected_type;
 870   arraycopy_helper(x, &flags, &expected_type);
 871   __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(), length.result(),
 872                tmp, expected_type, flags, info);
 873 }
 874 
 875 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
 876   fatal("CRC32 intrinsic is not implemented on this platform");
 877 }
 878 
 879 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
 880   Unimplemented();
 881 }
 882 
 883 void LIRGenerator::do_Convert(Convert* x) {
 884   address runtime_func;
 885   switch (x->op()) {
 886     case Bytecodes::_l2f:
 887       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::l2f);
 888       break;
 889     case Bytecodes::_l2d:
 890       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::l2d);
 891       break;
 892     case Bytecodes::_f2l:
 893       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::f2l);
 894       break;
 895     case Bytecodes::_d2l:
 896       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::d2l);
 897       break;
 898 #ifdef __SOFTFP__
 899     case Bytecodes::_f2d:
 900       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_f2d);
 901       break;
 902     case Bytecodes::_d2f:
 903       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_d2f);
 904       break;
 905     case Bytecodes::_i2f:
 906       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_i2f);
 907       break;
 908     case Bytecodes::_i2d:
 909       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_i2d);
 910       break;
 911     case Bytecodes::_f2i:
 912       runtime_func = CAST_FROM_FN_PTR(address, __aeabi_f2iz);
 913       break;
 914     case Bytecodes::_d2i:
 915       // This is implemented in hard float in assembler on arm but a call
 916       // on other platforms.
 917       runtime_func = CAST_FROM_FN_PTR(address, SharedRuntime::d2i);
 918       break;
 919 #endif // __SOFTFP__
 920     default: {
 921       LIRItem value(x->value(), this);
 922       value.load_item();
 923       LIR_Opr reg = rlock_result(x);
 924       __ convert(x->op(), value.result(), reg, NULL);
 925       return;
 926     }
 927   }
 928 
 929   LIR_Opr result = call_runtime(x->value(), runtime_func, x->type(), NULL);
 930   set_result(x, result);
 931 }
 932 
 933 
 934 void LIRGenerator::do_NewInstance(NewInstance* x) {
 935   print_if_not_loaded(x);
 936 
 937   CodeEmitInfo* info = state_for(x, x->state());
 938   LIR_Opr reg = result_register_for(x->type());  // R0 is required by runtime call in NewInstanceStub::emit_code
 939   LIR_Opr klass_reg = FrameMap::R1_metadata_opr; // R1 is required by runtime call in NewInstanceStub::emit_code
 940   LIR_Opr tmp1 = new_register(objectType);
 941   LIR_Opr tmp2 = new_register(objectType);
 942   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
 943 
 944   new_instance(reg, x->klass(), x->is_unresolved(), tmp1, tmp2, tmp3,
 945                LIR_OprFact::illegalOpr, klass_reg, info);
 946 
 947   LIR_Opr result = rlock_result(x);
 948   __ move(reg, result);
 949 }
 950 
 951 
 952 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
 953   // Evaluate state_for() first, because it can emit code
 954   // with the same fixed registers that are used here (R1, R2)
 955   CodeEmitInfo* info = state_for(x, x->state());
 956   LIRItem length(x->length(), this);
 957 
 958   length.load_item_force(FrameMap::R2_opr);      // R2 is required by runtime call in NewTypeArrayStub::emit_code
 959   LIR_Opr len = length.result();
 960 
 961   LIR_Opr reg = result_register_for(x->type());  // R0 is required by runtime call in NewTypeArrayStub::emit_code
 962   LIR_Opr klass_reg = FrameMap::R1_metadata_opr; // R1 is required by runtime call in NewTypeArrayStub::emit_code
 963 
 964   LIR_Opr tmp1 = new_register(objectType);
 965   LIR_Opr tmp2 = new_register(objectType);
 966   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
 967   LIR_Opr tmp4 = LIR_OprFact::illegalOpr;
 968 
 969   BasicType elem_type = x->elt_type();
 970   __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
 971 
 972   CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
 973   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path);
 974 
 975   LIR_Opr result = rlock_result(x);
 976   __ move(reg, result);
 977 }
 978 
 979 
 980 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
 981   // Evaluate state_for() first, because it can emit code
 982   // with the same fixed registers that are used here (R1, R2)
 983   CodeEmitInfo* info = state_for(x, x->state());
 984   LIRItem length(x->length(), this);
 985 
 986   length.load_item_force(FrameMap::R2_opr);           // R2 is required by runtime call in NewObjectArrayStub::emit_code
 987   LIR_Opr len = length.result();
 988 
 989   CodeEmitInfo* patching_info = NULL;
 990   if (!x->klass()->is_loaded() || PatchALot) {
 991     patching_info = state_for(x, x->state_before());
 992   }
 993 
 994   LIR_Opr reg = result_register_for(x->type());       // R0 is required by runtime call in NewObjectArrayStub::emit_code
 995   LIR_Opr klass_reg = FrameMap::R1_metadata_opr;      // R1 is required by runtime call in NewObjectArrayStub::emit_code
 996 
 997   LIR_Opr tmp1 = new_register(objectType);
 998   LIR_Opr tmp2 = new_register(objectType);
 999   LIR_Opr tmp3 = FrameMap::LR_oop_opr;
1000   LIR_Opr tmp4 = LIR_OprFact::illegalOpr;
1001 
1002   CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info);
1003   ciMetadata* obj = ciObjArrayKlass::make(x->klass());
1004   if (obj == ciEnv::unloaded_ciobjarrayklass()) {
1005     BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
1006   }
1007   klass2reg_with_patching(klass_reg, obj, patching_info);
1008   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path);
1009 
1010   LIR_Opr result = rlock_result(x);
1011   __ move(reg, result);
1012 }
1013 
1014 
1015 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1016   Values* dims = x->dims();
1017   int i = dims->length();
1018   LIRItemList* items = new LIRItemList(i, i, NULL);
1019   while (i-- > 0) {
1020     LIRItem* size = new LIRItem(dims->at(i), this);
1021     items->at_put(i, size);
1022   }
1023 
1024   // Need to get the info before, as the items may become invalid through item_free
1025   CodeEmitInfo* patching_info = NULL;
1026   if (!x->klass()->is_loaded() || PatchALot) {
1027     patching_info = state_for(x, x->state_before());
1028 
1029     // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1030     // clone all handlers (NOTE: Usually this is handled transparently
1031     // by the CodeEmitInfo cloning logic in CodeStub constructors but
1032     // is done explicitly here because a stub isn't being used).
1033     x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1034   }
1035 
1036   i = dims->length();
1037   while (i-- > 0) {
1038     LIRItem* size = items->at(i);
1039     size->load_item();
1040     LIR_Opr sz = size->result();
1041     assert(sz->type() == T_INT, "should be");
1042     store_stack_parameter(sz, in_ByteSize(i * BytesPerInt));
1043   }
1044 
1045   CodeEmitInfo* info = state_for(x, x->state());
1046   LIR_Opr klass_reg = FrameMap::R0_metadata_opr;
1047   klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1048 
1049   LIR_Opr rank = FrameMap::R2_opr;
1050   __ move(LIR_OprFact::intConst(x->rank()), rank);
1051   LIR_Opr varargs = FrameMap::SP_opr;
1052   LIR_OprList* args = new LIR_OprList(3);
1053   args->append(klass_reg);
1054   args->append(rank);
1055   args->append(varargs);
1056   LIR_Opr reg = result_register_for(x->type());
1057   __ call_runtime(Runtime1::entry_for(Runtime1::new_multi_array_id),
1058                   LIR_OprFact::illegalOpr, reg, args, info);
1059 
1060   LIR_Opr result = rlock_result(x);
1061   __ move(reg, result);
1062 }
1063 
1064 
1065 void LIRGenerator::do_BlockBegin(BlockBegin* x) {
1066   // nothing to do for now
1067 }
1068 
1069 
1070 void LIRGenerator::do_CheckCast(CheckCast* x) {
1071   LIRItem obj(x->obj(), this);
1072   CodeEmitInfo* patching_info = NULL;
1073   if (!x->klass()->is_loaded() || (PatchALot && !x->is_incompatible_class_change_check() && !x->is_invokespecial_receiver_check())) {
1074     patching_info = state_for(x, x->state_before());
1075   }
1076 
1077   obj.load_item();
1078 
1079   CodeEmitInfo* info_for_exception =
1080     (x->needs_exception_state() ? state_for(x) :
1081                                   state_for(x, x->state_before(), true /*ignore_xhandler*/));
1082 
1083   CodeStub* stub;
1084   if (x->is_incompatible_class_change_check()) {
1085     assert(patching_info == NULL, "can't patch this");
1086     stub = new SimpleExceptionStub(Runtime1::throw_incompatible_class_change_error_id,
1087                                    LIR_OprFact::illegalOpr, info_for_exception);
1088   } else if (x->is_invokespecial_receiver_check()) {
1089     assert(patching_info == NULL, "can't patch this");
1090     stub = new DeoptimizeStub(info_for_exception,
1091                               Deoptimization::Reason_class_check,
1092                               Deoptimization::Action_none);
1093   } else {
1094     stub = new SimpleExceptionStub(Runtime1::throw_class_cast_exception_id,
1095                                    LIR_OprFact::illegalOpr, info_for_exception);
1096   }
1097 
1098   LIR_Opr out_reg = rlock_result(x);
1099   LIR_Opr tmp1 = FrameMap::R0_oop_opr;
1100   LIR_Opr tmp2 = FrameMap::R1_oop_opr;
1101   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1102 
1103   __ checkcast(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3, x->direct_compare(),
1104                info_for_exception, patching_info, stub, x->profiled_method(), x->profiled_bci());
1105 }
1106 
1107 
1108 void LIRGenerator::do_InstanceOf(InstanceOf* x) {
1109   LIRItem obj(x->obj(), this);
1110   CodeEmitInfo* patching_info = NULL;
1111   if (!x->klass()->is_loaded() || PatchALot) {
1112     patching_info = state_for(x, x->state_before());
1113   }
1114 
1115   obj.load_item();
1116   LIR_Opr out_reg = rlock_result(x);
1117   LIR_Opr tmp1 = FrameMap::R0_oop_opr;
1118   LIR_Opr tmp2 = FrameMap::R1_oop_opr;
1119   LIR_Opr tmp3 = LIR_OprFact::illegalOpr;
1120 
1121   __ instanceof(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,
1122                 x->direct_compare(), patching_info, x->profiled_method(), x->profiled_bci());
1123 }
1124 
1125 
1126 #ifdef __SOFTFP__
1127 // Turn operator if (f <op> g) into runtime call:
1128 //     call _aeabi_fcmp<op>(f, g)
1129 //     cmp(eq, 1)
1130 //     branch(eq, true path).
1131 void LIRGenerator::do_soft_float_compare(If* x) {
1132   assert(x->number_of_sux() == 2, "inconsistency");
1133   ValueTag tag = x->x()->type()->tag();
1134   If::Condition cond = x->cond();
1135   address runtime_func;
1136   // unordered comparison gets the wrong answer because aeabi functions
1137   //  return false.
1138   bool unordered_is_true = x->unordered_is_true();
1139   // reverse of condition for ne
1140   bool compare_to_zero = false;
1141   switch (lir_cond(cond)) {
1142     case lir_cond_notEqual:
1143       compare_to_zero = true;  // fall through
1144     case lir_cond_equal:
1145       runtime_func = tag == floatTag ?
1146           CAST_FROM_FN_PTR(address, __aeabi_fcmpeq):
1147           CAST_FROM_FN_PTR(address, __aeabi_dcmpeq);
1148       break;
1149     case lir_cond_less:
1150       if (unordered_is_true) {
1151         runtime_func = tag == floatTag ?
1152           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmplt):
1153           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmplt);
1154       } else {
1155         runtime_func = tag == floatTag ?
1156           CAST_FROM_FN_PTR(address, __aeabi_fcmplt):
1157           CAST_FROM_FN_PTR(address, __aeabi_dcmplt);
1158       }
1159       break;
1160     case lir_cond_lessEqual:
1161       if (unordered_is_true) {
1162         runtime_func = tag == floatTag ?
1163           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmple):
1164           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmple);
1165       } else {
1166         runtime_func = tag == floatTag ?
1167           CAST_FROM_FN_PTR(address, __aeabi_fcmple):
1168           CAST_FROM_FN_PTR(address, __aeabi_dcmple);
1169       }
1170       break;
1171     case lir_cond_greaterEqual:
1172       if (unordered_is_true) {
1173         runtime_func = tag == floatTag ?
1174           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmpge):
1175           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmpge);
1176       } else {
1177         runtime_func = tag == floatTag ?
1178           CAST_FROM_FN_PTR(address, __aeabi_fcmpge):
1179           CAST_FROM_FN_PTR(address, __aeabi_dcmpge);
1180       }
1181       break;
1182     case lir_cond_greater:
1183       if (unordered_is_true) {
1184         runtime_func = tag == floatTag ?
1185           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_fcmpgt):
1186           CAST_FROM_FN_PTR(address, SharedRuntime::unordered_dcmpgt);
1187       } else {
1188         runtime_func = tag == floatTag ?
1189           CAST_FROM_FN_PTR(address, __aeabi_fcmpgt):
1190           CAST_FROM_FN_PTR(address, __aeabi_dcmpgt);
1191       }
1192       break;
1193     case lir_cond_aboveEqual:
1194     case lir_cond_belowEqual:
1195       ShouldNotReachHere();  // We're not going to get these.
1196     default:
1197       assert(lir_cond(cond) == lir_cond_always, "must be");
1198       ShouldNotReachHere();
1199   }
1200   set_no_result(x);
1201 
1202   // add safepoint before generating condition code so it can be recomputed
1203   if (x->is_safepoint()) {
1204     increment_backedge_counter(state_for(x, x->state_before()), x->profiled_bci());
1205     __ safepoint(LIR_OprFact::illegalOpr, state_for(x, x->state_before()));
1206   }
1207   // Call float compare function, returns (1,0) if true or false.
1208   LIR_Opr result = call_runtime(x->x(), x->y(), runtime_func, intType, NULL);
1209   __ cmp(lir_cond_equal, result,
1210          compare_to_zero ?
1211            LIR_OprFact::intConst(0) : LIR_OprFact::intConst(1));
1212   profile_branch(x, cond);
1213   move_to_phi(x->state());
1214   __ branch(lir_cond_equal, x->tsux());
1215 }
1216 #endif // __SOFTFP__
1217 
1218 void LIRGenerator::do_If(If* x) {
1219   assert(x->number_of_sux() == 2, "inconsistency");
1220   ValueTag tag = x->x()->type()->tag();
1221 
1222 #ifdef __SOFTFP__
1223   if (tag == floatTag || tag == doubleTag) {
1224     do_soft_float_compare(x);
1225     assert(x->default_sux() == x->fsux(), "wrong destination above");
1226     __ jump(x->default_sux());
1227     return;
1228   }
1229 #endif // __SOFTFP__
1230 
1231   LIRItem xitem(x->x(), this);
1232   LIRItem yitem(x->y(), this);
1233   LIRItem* xin = &xitem;
1234   LIRItem* yin = &yitem;
1235   If::Condition cond = x->cond();
1236 
1237   if (tag == longTag) {
1238     if (cond == If::gtr || cond == If::leq) {
1239       cond = Instruction::mirror(cond);
1240       xin = &yitem;
1241       yin = &xitem;
1242     }
1243     xin->set_destroys_register();
1244   }
1245 
1246   xin->load_item();
1247   LIR_Opr left = xin->result();
1248   LIR_Opr right;
1249 
1250   if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 &&
1251       (cond == If::eql || cond == If::neq)) {
1252     // inline long zero
1253     right = LIR_OprFact::value_type(yin->value()->type());
1254   } else {
1255     yin->load_nonconstant();
1256     right = yin->result();
1257   }
1258 
1259   set_no_result(x);
1260 
1261   // add safepoint before generating condition code so it can be recomputed
1262   if (x->is_safepoint()) {
1263     increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1264         x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1265     __ safepoint(LIR_OprFact::illegalOpr, state_for(x, x->state_before()));
1266   }
1267 
1268   __ cmp(lir_cond(cond), left, right);
1269   profile_branch(x, cond);
1270   move_to_phi(x->state());
1271   if (x->x()->type()->is_float_kind()) {
1272     __ branch(lir_cond(cond), x->tsux(), x->usux());
1273   } else {
1274     __ branch(lir_cond(cond), x->tsux());
1275   }
1276   assert(x->default_sux() == x->fsux(), "wrong destination above");
1277   __ jump(x->default_sux());
1278 }
1279 
1280 
1281 LIR_Opr LIRGenerator::getThreadPointer() {
1282   return FrameMap::Rthread_opr;
1283 }
1284 
1285 void LIRGenerator::trace_block_entry(BlockBegin* block) {
1286   __ move(LIR_OprFact::intConst(block->block_id()), FrameMap::R0_opr);
1287   LIR_OprList* args = new LIR_OprList(1);
1288   args->append(FrameMap::R0_opr);
1289   address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1290   __ call_runtime_leaf(func, getThreadTemp(), LIR_OprFact::illegalOpr, args);
1291 }
1292 
1293 
1294 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1295                                         CodeEmitInfo* info) {
1296   if (value->is_double_cpu()) {
1297     assert(address->index()->is_illegal(), "should have a constant displacement");
1298     LIR_Address* store_addr = NULL;
1299     if (address->disp() != 0) {
1300       LIR_Opr tmp = new_pointer_register();
1301       add_large_constant(address->base(), address->disp(), tmp);
1302       store_addr = new LIR_Address(tmp, (intx)0, address->type());
1303     } else {
1304       // address->disp() can be 0, if the address is referenced using the unsafe intrinsic
1305       store_addr = address;
1306     }
1307     __ volatile_store_mem_reg(value, store_addr, info);
1308     return;
1309   }
1310   __ store(value, address, info, lir_patch_none);
1311 }
1312 
1313 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1314                                        CodeEmitInfo* info) {
1315   if (result->is_double_cpu()) {
1316     assert(address->index()->is_illegal(), "should have a constant displacement");
1317     LIR_Address* load_addr = NULL;
1318     if (address->disp() != 0) {
1319       LIR_Opr tmp = new_pointer_register();
1320       add_large_constant(address->base(), address->disp(), tmp);
1321       load_addr = new LIR_Address(tmp, (intx)0, address->type());
1322     } else {
1323       // address->disp() can be 0, if the address is referenced using the unsafe intrinsic
1324       load_addr = address;
1325     }
1326     __ volatile_load_mem_reg(load_addr, result, info);
1327     return;
1328   }
1329   __ load(address, result, info, lir_patch_none);
1330 }




--- EOF ---