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