1 /*
   2  * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2019 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "c1/c1_Compilation.hpp"
  29 #include "c1/c1_FrameMap.hpp"
  30 #include "c1/c1_Instruction.hpp"
  31 #include "c1/c1_LIRAssembler.hpp"
  32 #include "c1/c1_LIRGenerator.hpp"
  33 #include "c1/c1_Runtime1.hpp"
  34 #include "c1/c1_ValueStack.hpp"
  35 #include "ci/ciArray.hpp"
  36 #include "ci/ciObjArrayKlass.hpp"
  37 #include "ci/ciTypeArrayKlass.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/vm_version.hpp"
  41 #include "utilities/powerOfTwo.hpp"
  42 #include "vmreg_ppc.inline.hpp"
  43 
  44 #ifdef ASSERT
  45 #define __ gen()->lir(__FILE__, __LINE__)->
  46 #else
  47 #define __ gen()->lir()->
  48 #endif
  49 
  50 void LIRItem::load_byte_item() {
  51   // Byte loads use same registers as other loads.
  52   load_item();
  53 }
  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 //--------------------------------------------------------------
  70 //               LIRGenerator
  71 //--------------------------------------------------------------
  72 
  73 LIR_Opr LIRGenerator::exceptionOopOpr()              { return FrameMap::R3_oop_opr; }
  74 LIR_Opr LIRGenerator::exceptionPcOpr()               { return FrameMap::R4_opr; }
  75 LIR_Opr LIRGenerator::syncLockOpr()                  { return FrameMap::R5_opr; }     // Need temp effect for MonitorEnterStub.
  76 LIR_Opr LIRGenerator::syncTempOpr()                  { return FrameMap::R4_oop_opr; } // Need temp effect for MonitorEnterStub.
  77 LIR_Opr LIRGenerator::getThreadTemp()                { return LIR_OprFact::illegalOpr; } // not needed
  78 
  79 LIR_Opr LIRGenerator::result_register_for(ValueType* type, bool callee) {
  80   LIR_Opr opr;
  81   switch (type->tag()) {
  82   case intTag:     opr = FrameMap::R3_opr;         break;
  83   case objectTag:  opr = FrameMap::R3_oop_opr;     break;
  84   case longTag:    opr = FrameMap::R3_long_opr;    break;
  85   case floatTag:   opr = FrameMap::F1_opr;         break;
  86   case doubleTag:  opr = FrameMap::F1_double_opr;  break;
  87 
  88   case addressTag:
  89   default: ShouldNotReachHere(); return LIR_OprFact::illegalOpr;
  90   }
  91 
  92   assert(opr->type_field() == as_OprType(as_BasicType(type)), "type mismatch");
  93   return opr;
  94 }
  95 
  96 LIR_Opr LIRGenerator::rlock_callee_saved(BasicType type) {
  97   ShouldNotReachHere();
  98   return LIR_OprFact::illegalOpr;
  99 }
 100 
 101 
 102 LIR_Opr LIRGenerator::rlock_byte(BasicType type) {
 103   return new_register(T_INT);
 104 }
 105 
 106 
 107 //--------- loading items into registers --------------------------------
 108 
 109 // PPC cannot inline all constants.
 110 bool LIRGenerator::can_store_as_constant(Value v, BasicType type) const {
 111   if (v->type()->as_IntConstant() != NULL) {
 112     return Assembler::is_simm16(v->type()->as_IntConstant()->value());
 113   } else if (v->type()->as_LongConstant() != NULL) {
 114     return Assembler::is_simm16(v->type()->as_LongConstant()->value());
 115   } else if (v->type()->as_ObjectConstant() != NULL) {
 116     return v->type()->as_ObjectConstant()->value()->is_null_object();
 117   } else {
 118     return false;
 119   }
 120 }
 121 
 122 
 123 // Only simm16 constants can be inlined.
 124 bool LIRGenerator::can_inline_as_constant(Value i) const {
 125   return can_store_as_constant(i, as_BasicType(i->type()));
 126 }
 127 
 128 
 129 bool LIRGenerator::can_inline_as_constant(LIR_Const* c) const {
 130   if (c->type() == T_INT) {
 131     return Assembler::is_simm16(c->as_jint());
 132   }
 133   if (c->type() == T_LONG) {
 134     return Assembler::is_simm16(c->as_jlong());
 135   }
 136   if (c->type() == T_OBJECT) {
 137     return c->as_jobject() == NULL;
 138   }
 139   return false;
 140 }
 141 
 142 
 143 LIR_Opr LIRGenerator::safepoint_poll_register() {
 144   return new_register(T_INT);
 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     LIR_Const *constant = index->as_constant_ptr();
 156     if (constant->type() == T_LONG) {
 157       large_disp += constant->as_jlong() << shift;
 158     } else {
 159       large_disp += (intx)(constant->as_jint()) << shift;
 160     }
 161     index = LIR_OprFact::illegalOpr;
 162   }
 163 
 164   if (index->is_register()) {
 165     // Apply the shift and accumulate the displacement.
 166     if (shift > 0) {
 167       LIR_Opr tmp = new_pointer_register();
 168       __ shift_left(index, shift, tmp);
 169       index = tmp;
 170     }
 171     if (large_disp != 0) {
 172       LIR_Opr tmp = new_pointer_register();
 173       if (Assembler::is_simm16(large_disp)) {
 174         __ add(index, LIR_OprFact::intptrConst(large_disp), tmp);
 175         index = tmp;
 176       } else {
 177         __ move(LIR_OprFact::intptrConst(large_disp), tmp);
 178         __ add(tmp, index, tmp);
 179         index = tmp;
 180       }
 181       large_disp = 0;
 182     }
 183   } else if (!Assembler::is_simm16(large_disp)) {
 184     // Index is illegal so replace it with the displacement loaded into a register.
 185     index = new_pointer_register();
 186     __ move(LIR_OprFact::intptrConst(large_disp), index);
 187     large_disp = 0;
 188   }
 189 
 190   // At this point we either have base + index or base + displacement.
 191   if (large_disp == 0) {
 192     return new LIR_Address(base, index, type);
 193   } else {
 194     assert(Assembler::is_simm16(large_disp), "must be");
 195     return new LIR_Address(base, large_disp, type);
 196   }
 197 }
 198 
 199 
 200 LIR_Address* LIRGenerator::emit_array_address(LIR_Opr array_opr, LIR_Opr index_opr,
 201                                               BasicType type) {
 202   int elem_size = type2aelembytes(type);
 203   int shift = exact_log2(elem_size);
 204 
 205   LIR_Opr base_opr;
 206   intx offset = arrayOopDesc::base_offset_in_bytes(type);
 207 
 208   if (index_opr->is_constant()) {
 209     intx i = index_opr->as_constant_ptr()->as_jint();
 210     intx array_offset = i * elem_size;
 211     if (Assembler::is_simm16(array_offset + offset)) {
 212       base_opr = array_opr;
 213       offset = array_offset + offset;
 214     } else {
 215       base_opr = new_pointer_register();
 216       if (Assembler::is_simm16(array_offset)) {
 217         __ add(array_opr, LIR_OprFact::intptrConst(array_offset), base_opr);
 218       } else {
 219         __ move(LIR_OprFact::intptrConst(array_offset), base_opr);
 220         __ add(base_opr, array_opr, base_opr);
 221       }
 222     }
 223   } else {
 224 #ifdef _LP64
 225     if (index_opr->type() == T_INT) {
 226       LIR_Opr tmp = new_register(T_LONG);
 227       __ convert(Bytecodes::_i2l, index_opr, tmp);
 228       index_opr = tmp;
 229     }
 230 #endif
 231 
 232     base_opr = new_pointer_register();
 233     assert (index_opr->is_register(), "Must be register");
 234     if (shift > 0) {
 235       __ shift_left(index_opr, shift, base_opr);
 236       __ add(base_opr, array_opr, base_opr);
 237     } else {
 238       __ add(index_opr, array_opr, base_opr);
 239     }
 240   }
 241   return new LIR_Address(base_opr, offset, type);
 242 }
 243 
 244 
 245 LIR_Opr LIRGenerator::load_immediate(int x, BasicType type) {
 246   LIR_Opr r;
 247   if (type == T_LONG) {
 248     r = LIR_OprFact::longConst(x);
 249   } else if (type == T_INT) {
 250     r = LIR_OprFact::intConst(x);
 251   } else {
 252     ShouldNotReachHere();
 253   }
 254   if (!Assembler::is_simm16(x)) {
 255     LIR_Opr tmp = new_register(type);
 256     __ move(r, tmp);
 257     return tmp;
 258   }
 259   return r;
 260 }
 261 
 262 
 263 void LIRGenerator::increment_counter(address counter, BasicType type, int step) {
 264   LIR_Opr pointer = new_pointer_register();
 265   __ move(LIR_OprFact::intptrConst(counter), pointer);
 266   LIR_Address* addr = new LIR_Address(pointer, type);
 267   increment_counter(addr, step);
 268 }
 269 
 270 
 271 void LIRGenerator::increment_counter(LIR_Address* addr, int step) {
 272   LIR_Opr temp = new_register(addr->type());
 273   __ move(addr, temp);
 274   __ add(temp, load_immediate(step, addr->type()), temp);
 275   __ move(temp, addr);
 276 }
 277 
 278 
 279 void LIRGenerator::cmp_mem_int(LIR_Condition condition, LIR_Opr base, int disp, int c, CodeEmitInfo* info) {
 280   LIR_Opr tmp = FrameMap::R0_opr;
 281   __ load(new LIR_Address(base, disp, T_INT), tmp, info);
 282   __ cmp(condition, tmp, c);
 283 }
 284 
 285 
 286 void LIRGenerator::cmp_reg_mem(LIR_Condition condition, LIR_Opr reg, LIR_Opr base,
 287                                int disp, BasicType type, CodeEmitInfo* info) {
 288   LIR_Opr tmp = FrameMap::R0_opr;
 289   __ load(new LIR_Address(base, disp, type), tmp, info);
 290   __ cmp(condition, reg, tmp);
 291 }
 292 
 293 
 294 bool LIRGenerator::strength_reduce_multiply(LIR_Opr left, jint c, LIR_Opr result, LIR_Opr tmp) {
 295   assert(left != result, "should be different registers");
 296   if (is_power_of_2(c + 1)) {
 297     __ shift_left(left, log2i_exact(c + 1), result);
 298     __ sub(result, left, result);
 299     return true;
 300   } else if (is_power_of_2(c - 1)) {
 301     __ shift_left(left, log2i_exact(c - 1), result);
 302     __ add(result, left, result);
 303     return true;
 304   }
 305   return false;
 306 }
 307 
 308 
 309 void LIRGenerator::store_stack_parameter(LIR_Opr item, ByteSize offset_from_sp) {
 310   BasicType t = item->type();
 311   LIR_Opr sp_opr = FrameMap::SP_opr;
 312   __ move(item, new LIR_Address(sp_opr, in_bytes(offset_from_sp), t));
 313 }
 314 
 315 
 316 //----------------------------------------------------------------------
 317 //             visitor functions
 318 //----------------------------------------------------------------------
 319 
 320 void LIRGenerator::array_store_check(LIR_Opr value, LIR_Opr array, CodeEmitInfo* store_check_info, ciMethod* profiled_method, int profiled_bci) {
 321   // Following registers are used by slow_subtype_check:
 322   LIR_Opr tmp1 = FrameMap::R4_opr; // super_klass
 323   LIR_Opr tmp2 = FrameMap::R5_opr; // sub_klass
 324   LIR_Opr tmp3 = FrameMap::R6_opr; // temp
 325   __ store_check(value, array, tmp1, tmp2, tmp3, store_check_info, profiled_method, profiled_bci);
 326 }
 327 
 328 
 329 void LIRGenerator::do_MonitorEnter(MonitorEnter* x) {
 330   assert(x->is_pinned(),"");
 331   LIRItem obj(x->obj(), this);
 332   obj.load_item();
 333 
 334   set_no_result(x);
 335 
 336   // We use R4+R5 in order to get a temp effect. These regs are used in slow path (MonitorEnterStub).
 337   LIR_Opr lock    = FrameMap::R5_opr;
 338   LIR_Opr scratch = FrameMap::R4_opr;
 339   LIR_Opr hdr     = FrameMap::R6_opr;
 340 
 341   CodeEmitInfo* info_for_exception = NULL;
 342   if (x->needs_null_check()) {
 343     info_for_exception = state_for(x);
 344   }
 345 
 346   // This CodeEmitInfo must not have the xhandlers because here the
 347   // object is already locked (xhandlers expects object to be unlocked).
 348   CodeEmitInfo* info = state_for(x, x->state(), true);
 349   monitor_enter(obj.result(), lock, hdr, scratch, x->monitor_no(), info_for_exception, info);
 350 }
 351 
 352 
 353 void LIRGenerator::do_MonitorExit(MonitorExit* x) {
 354   assert(x->is_pinned(),"");
 355   LIRItem obj(x->obj(), this);
 356   obj.dont_load_item();
 357 
 358   set_no_result(x);
 359   LIR_Opr lock     = FrameMap::R5_opr;
 360   LIR_Opr hdr      = FrameMap::R4_opr; // Used for slow path (MonitorExitStub).
 361   LIR_Opr obj_temp = FrameMap::R6_opr;
 362   monitor_exit(obj_temp, lock, hdr, LIR_OprFact::illegalOpr, x->monitor_no());
 363 }
 364 
 365 
 366 // _ineg, _lneg, _fneg, _dneg
 367 void LIRGenerator::do_NegateOp(NegateOp* x) {
 368   LIRItem value(x->x(), this);
 369   value.load_item();
 370   LIR_Opr reg = rlock_result(x);
 371   __ negate(value.result(), reg);
 372 }
 373 
 374 
 375 // for  _fadd, _fmul, _fsub, _fdiv, _frem
 376 //      _dadd, _dmul, _dsub, _ddiv, _drem
 377 void LIRGenerator::do_ArithmeticOp_FPU(ArithmeticOp* x) {
 378   switch (x->op()) {
 379   case Bytecodes::_fadd:
 380   case Bytecodes::_fmul:
 381   case Bytecodes::_fsub:
 382   case Bytecodes::_fdiv:
 383   case Bytecodes::_dadd:
 384   case Bytecodes::_dmul:
 385   case Bytecodes::_dsub:
 386   case Bytecodes::_ddiv: {
 387     LIRItem left(x->x(), this);
 388     LIRItem right(x->y(), this);
 389     left.load_item();
 390     right.load_item();
 391     rlock_result(x);
 392     arithmetic_op_fpu(x->op(), x->operand(), left.result(), right.result());
 393   }
 394   break;
 395 
 396   case Bytecodes::_frem:
 397   case Bytecodes::_drem: {
 398     address entry = NULL;
 399     switch (x->op()) {
 400     case Bytecodes::_frem:
 401       entry = CAST_FROM_FN_PTR(address, SharedRuntime::frem);
 402       break;
 403     case Bytecodes::_drem:
 404       entry = CAST_FROM_FN_PTR(address, SharedRuntime::drem);
 405       break;
 406     default:
 407       ShouldNotReachHere();
 408     }
 409     LIR_Opr result = call_runtime(x->x(), x->y(), entry, x->type(), NULL);
 410     set_result(x, result);
 411   }
 412   break;
 413 
 414   default: ShouldNotReachHere();
 415   }
 416 }
 417 
 418 
 419 // for  _ladd, _lmul, _lsub, _ldiv, _lrem
 420 void LIRGenerator::do_ArithmeticOp_Long(ArithmeticOp* x) {
 421   bool is_div_rem = x->op() == Bytecodes::_ldiv || x->op() == Bytecodes::_lrem;
 422 
 423   LIRItem right(x->y(), this);
 424   // Missing test if instr is commutative and if we should swap.
 425   if (right.value()->type()->as_LongConstant() &&
 426       (x->op() == Bytecodes::_lsub && right.value()->type()->as_LongConstant()->value() == ((-1)<<15)) ) {
 427     // Sub is implemented by addi and can't support min_simm16 as constant..
 428     right.load_item();
 429   } else {
 430     right.load_nonconstant();
 431   }
 432   assert(right.is_constant() || right.is_register(), "wrong state of right");
 433 
 434   if (is_div_rem) {
 435     LIR_Opr divisor = right.result();
 436     if (divisor->is_register()) {
 437       CodeEmitInfo* null_check_info = state_for(x);
 438       __ cmp(lir_cond_equal, divisor, LIR_OprFact::longConst(0));
 439       __ branch(lir_cond_equal, new DivByZeroStub(null_check_info));
 440     } else {
 441       jlong const_divisor = divisor->as_constant_ptr()->as_jlong();
 442       if (const_divisor == 0) {
 443         CodeEmitInfo* null_check_info = state_for(x);
 444         __ jump(new DivByZeroStub(null_check_info));
 445         rlock_result(x);
 446         __ move(LIR_OprFact::longConst(0), x->operand()); // dummy
 447         return;
 448       }
 449       if (x->op() == Bytecodes::_lrem && !is_power_of_2(const_divisor) && const_divisor != -1) {
 450         // Remainder computation would need additional tmp != R0.
 451         right.load_item();
 452       }
 453     }
 454   }
 455 
 456   LIRItem left(x->x(), this);
 457   left.load_item();
 458   rlock_result(x);
 459   if (is_div_rem) {
 460     CodeEmitInfo* info = NULL; // Null check already done above.
 461     LIR_Opr tmp = FrameMap::R0_opr;
 462     if (x->op() == Bytecodes::_lrem) {
 463       __ irem(left.result(), right.result(), x->operand(), tmp, info);
 464     } else if (x->op() == Bytecodes::_ldiv) {
 465       __ idiv(left.result(), right.result(), x->operand(), tmp, info);
 466     }
 467   } else {
 468     arithmetic_op_long(x->op(), x->operand(), left.result(), right.result(), NULL);
 469   }
 470 }
 471 
 472 
 473 // for: _iadd, _imul, _isub, _idiv, _irem
 474 void LIRGenerator::do_ArithmeticOp_Int(ArithmeticOp* x) {
 475   bool is_div_rem = x->op() == Bytecodes::_idiv || x->op() == Bytecodes::_irem;
 476 
 477   LIRItem right(x->y(), this);
 478   // Missing test if instr is commutative and if we should swap.
 479   if (right.value()->type()->as_IntConstant() &&
 480       (x->op() == Bytecodes::_isub && right.value()->type()->as_IntConstant()->value() == ((-1)<<15)) ) {
 481     // Sub is implemented by addi and can't support min_simm16 as constant.
 482     right.load_item();
 483   } else {
 484     right.load_nonconstant();
 485   }
 486   assert(right.is_constant() || right.is_register(), "wrong state of right");
 487 
 488   if (is_div_rem) {
 489     LIR_Opr divisor = right.result();
 490     if (divisor->is_register()) {
 491       CodeEmitInfo* null_check_info = state_for(x);
 492       __ cmp(lir_cond_equal, divisor, LIR_OprFact::intConst(0));
 493       __ branch(lir_cond_equal, new DivByZeroStub(null_check_info));
 494     } else {
 495       jint const_divisor = divisor->as_constant_ptr()->as_jint();
 496       if (const_divisor == 0) {
 497         CodeEmitInfo* null_check_info = state_for(x);
 498         __ jump(new DivByZeroStub(null_check_info));
 499         rlock_result(x);
 500         __ move(LIR_OprFact::intConst(0), x->operand()); // dummy
 501         return;
 502       }
 503       if (x->op() == Bytecodes::_irem && !is_power_of_2(const_divisor) && const_divisor != -1) {
 504         // Remainder computation would need additional tmp != R0.
 505         right.load_item();
 506       }
 507     }
 508   }
 509 
 510   LIRItem left(x->x(), this);
 511   left.load_item();
 512   rlock_result(x);
 513   if (is_div_rem) {
 514     CodeEmitInfo* info = NULL; // Null check already done above.
 515     LIR_Opr tmp = FrameMap::R0_opr;
 516     if (x->op() == Bytecodes::_irem) {
 517       __ irem(left.result(), right.result(), x->operand(), tmp, info);
 518     } else if (x->op() == Bytecodes::_idiv) {
 519       __ idiv(left.result(), right.result(), x->operand(), tmp, info);
 520     }
 521   } else {
 522     arithmetic_op_int(x->op(), x->operand(), left.result(), right.result(), FrameMap::R0_opr);
 523   }
 524 }
 525 
 526 
 527 void LIRGenerator::do_ArithmeticOp(ArithmeticOp* x) {
 528   ValueTag tag = x->type()->tag();
 529   assert(x->x()->type()->tag() == tag && x->y()->type()->tag() == tag, "wrong parameters");
 530   switch (tag) {
 531     case floatTag:
 532     case doubleTag: do_ArithmeticOp_FPU(x);  return;
 533     case longTag:   do_ArithmeticOp_Long(x); return;
 534     case intTag:    do_ArithmeticOp_Int(x);  return;
 535     default: ShouldNotReachHere();
 536   }
 537 }
 538 
 539 
 540 // _ishl, _lshl, _ishr, _lshr, _iushr, _lushr
 541 void LIRGenerator::do_ShiftOp(ShiftOp* x) {
 542   LIRItem value(x->x(), this);
 543   LIRItem count(x->y(), this);
 544   value.load_item();
 545   LIR_Opr reg = rlock_result(x);
 546   LIR_Opr mcount;
 547   if (count.result()->is_register()) {
 548     mcount = FrameMap::R0_opr;
 549   } else {
 550     mcount = LIR_OprFact::illegalOpr;
 551   }
 552   shift_op(x->op(), reg, value.result(), count.result(), mcount);
 553 }
 554 
 555 
 556 inline bool can_handle_logic_op_as_uimm(ValueType *type, Bytecodes::Code bc) {
 557   jlong int_or_long_const;
 558   if (type->as_IntConstant()) {
 559     int_or_long_const = type->as_IntConstant()->value();
 560   } else if (type->as_LongConstant()) {
 561     int_or_long_const = type->as_LongConstant()->value();
 562   } else if (type->as_ObjectConstant()) {
 563     return type->as_ObjectConstant()->value()->is_null_object();
 564   } else {
 565     return false;
 566   }
 567 
 568   if (Assembler::is_uimm(int_or_long_const, 16)) return true;
 569   if ((int_or_long_const & 0xFFFF) == 0 &&
 570       Assembler::is_uimm((jlong)((julong)int_or_long_const >> 16), 16)) return true;
 571 
 572   // see Assembler::andi
 573   if (bc == Bytecodes::_iand &&
 574       (is_power_of_2(int_or_long_const+1) ||
 575        is_power_of_2(int_or_long_const) ||
 576        is_power_of_2(-int_or_long_const))) return true;
 577   if (bc == Bytecodes::_land &&
 578       (is_power_of_2(int_or_long_const+1) ||
 579        (Assembler::is_uimm(int_or_long_const, 32) && is_power_of_2(int_or_long_const)) ||
 580        (int_or_long_const != min_jlong && is_power_of_2(-int_or_long_const)))) return true;
 581 
 582   // special case: xor -1
 583   if ((bc == Bytecodes::_ixor || bc == Bytecodes::_lxor) &&
 584       int_or_long_const == -1) return true;
 585   return false;
 586 }
 587 
 588 
 589 // _iand, _land, _ior, _lor, _ixor, _lxor
 590 void LIRGenerator::do_LogicOp(LogicOp* x) {
 591   LIRItem left(x->x(), this);
 592   LIRItem right(x->y(), this);
 593 
 594   left.load_item();
 595 
 596   Value rval = right.value();
 597   LIR_Opr r = rval->operand();
 598   ValueType *type = rval->type();
 599   // Logic instructions use unsigned immediate values.
 600   if (can_handle_logic_op_as_uimm(type, x->op())) {
 601     if (!r->is_constant()) {
 602       r = LIR_OprFact::value_type(type);
 603       rval->set_operand(r);
 604     }
 605     right.set_result(r);
 606   } else {
 607     right.load_item();
 608   }
 609 
 610   LIR_Opr reg = rlock_result(x);
 611 
 612   logic_op(x->op(), reg, left.result(), right.result());
 613 }
 614 
 615 
 616 // _lcmp, _fcmpl, _fcmpg, _dcmpl, _dcmpg
 617 void LIRGenerator::do_CompareOp(CompareOp* x) {
 618   LIRItem left(x->x(), this);
 619   LIRItem right(x->y(), this);
 620   left.load_item();
 621   right.load_item();
 622   LIR_Opr reg = rlock_result(x);
 623   if (x->x()->type()->is_float_kind()) {
 624     Bytecodes::Code code = x->op();
 625     __ fcmp2int(left.result(), right.result(), reg, (code == Bytecodes::_fcmpl || code == Bytecodes::_dcmpl));
 626   } else if (x->x()->type()->tag() == longTag) {
 627     __ lcmp2int(left.result(), right.result(), reg);
 628   } else {
 629     Unimplemented();
 630   }
 631 }
 632 
 633 
 634 LIR_Opr LIRGenerator::atomic_cmpxchg(BasicType type, LIR_Opr addr, LIRItem& cmp_value, LIRItem& new_value) {
 635   LIR_Opr result = new_register(T_INT);
 636   LIR_Opr t1 = LIR_OprFact::illegalOpr;
 637   LIR_Opr t2 = LIR_OprFact::illegalOpr;
 638   cmp_value.load_item();
 639   new_value.load_item();
 640 
 641   // Volatile load may be followed by Unsafe CAS.
 642   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 643     __ membar();
 644   } else {
 645     __ membar_release();
 646   }
 647 
 648   if (is_reference_type(type)) {
 649     if (UseCompressedOops) {
 650       t1 = new_register(T_OBJECT);
 651       t2 = new_register(T_OBJECT);
 652     }
 653     __ cas_obj(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), t1, t2);
 654   } else if (type == T_INT) {
 655     __ cas_int(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), t1, t2);
 656   } else if (type == T_LONG) {
 657     __ cas_long(addr->as_address_ptr()->base(), cmp_value.result(), new_value.result(), t1, t2);
 658   } else {
 659     Unimplemented();
 660   }
 661   __ cmove(lir_cond_equal, LIR_OprFact::intConst(1), LIR_OprFact::intConst(0),
 662            result, type);
 663   return result;
 664 }
 665 
 666 
 667 LIR_Opr LIRGenerator::atomic_xchg(BasicType type, LIR_Opr addr, LIRItem& value) {
 668   LIR_Opr result = new_register(type);
 669   LIR_Opr tmp = FrameMap::R0_opr;
 670 
 671   value.load_item();
 672 
 673   // Volatile load may be followed by Unsafe CAS.
 674   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 675     __ membar();
 676   } else {
 677     __ membar_release();
 678   }
 679 
 680   __ xchg(addr, value.result(), result, tmp);
 681 
 682   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 683     __ membar_acquire();
 684   } else {
 685     __ membar();
 686   }
 687   return result;
 688 }
 689 
 690 
 691 LIR_Opr LIRGenerator::atomic_add(BasicType type, LIR_Opr addr, LIRItem& value) {
 692   LIR_Opr result = new_register(type);
 693   LIR_Opr tmp = FrameMap::R0_opr;
 694 
 695   value.load_item();
 696 
 697   // Volatile load may be followed by Unsafe CAS.
 698   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 699     __ membar(); // To be safe. Unsafe semantics are unclear.
 700   } else {
 701     __ membar_release();
 702   }
 703 
 704   __ xadd(addr, value.result(), result, tmp);
 705 
 706   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
 707     __ membar_acquire();
 708   } else {
 709     __ membar();
 710   }
 711   return result;
 712 }
 713 
 714 
 715 void LIRGenerator::do_MathIntrinsic(Intrinsic* x) {
 716   switch (x->id()) {
 717     case vmIntrinsics::_dabs: {
 718       assert(x->number_of_arguments() == 1, "wrong type");
 719       LIRItem value(x->argument_at(0), this);
 720       value.load_item();
 721       LIR_Opr dst = rlock_result(x);
 722       __ abs(value.result(), dst, LIR_OprFact::illegalOpr);
 723       break;
 724     }
 725     case vmIntrinsics::_dsqrt:
 726     case vmIntrinsics::_dsqrt_strict: {
 727       if (VM_Version::has_fsqrt()) {
 728         assert(x->number_of_arguments() == 1, "wrong type");
 729         LIRItem value(x->argument_at(0), this);
 730         value.load_item();
 731         LIR_Opr dst = rlock_result(x);
 732         __ sqrt(value.result(), dst, LIR_OprFact::illegalOpr);
 733         break;
 734       } // else fallthru
 735     }
 736     case vmIntrinsics::_dsin:   // fall through
 737     case vmIntrinsics::_dcos:   // fall through
 738     case vmIntrinsics::_dtan:   // fall through
 739     case vmIntrinsics::_dlog:   // fall through
 740     case vmIntrinsics::_dlog10: // fall through
 741     case vmIntrinsics::_dexp: {
 742       assert(x->number_of_arguments() == 1, "wrong type");
 743 
 744       address runtime_entry = NULL;
 745       switch (x->id()) {
 746         case vmIntrinsics::_dsqrt:
 747         case vmIntrinsics::_dsqrt_strict:
 748           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dsqrt);
 749           break;
 750         case vmIntrinsics::_dsin:
 751           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 752           break;
 753         case vmIntrinsics::_dcos:
 754           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 755           break;
 756         case vmIntrinsics::_dtan:
 757           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 758           break;
 759         case vmIntrinsics::_dlog:
 760           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 761           break;
 762         case vmIntrinsics::_dlog10:
 763           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 764           break;
 765         case vmIntrinsics::_dexp:
 766           runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 767           break;
 768         default:
 769           ShouldNotReachHere();
 770       }
 771 
 772       LIR_Opr result = call_runtime(x->argument_at(0), runtime_entry, x->type(), NULL);
 773       set_result(x, result);
 774       break;
 775     }
 776     case vmIntrinsics::_dpow: {
 777       assert(x->number_of_arguments() == 2, "wrong type");
 778       address runtime_entry = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 779       LIR_Opr result = call_runtime(x->argument_at(0), x->argument_at(1), runtime_entry, x->type(), NULL);
 780       set_result(x, result);
 781       break;
 782     }
 783     default:
 784       break;
 785   }
 786 }
 787 
 788 
 789 void LIRGenerator::do_ArrayCopy(Intrinsic* x) {
 790   assert(x->number_of_arguments() == 5, "wrong type");
 791 
 792   // Make all state_for calls early since they can emit code.
 793   CodeEmitInfo* info = state_for(x, x->state());
 794 
 795   LIRItem src     (x->argument_at(0), this);
 796   LIRItem src_pos (x->argument_at(1), this);
 797   LIRItem dst     (x->argument_at(2), this);
 798   LIRItem dst_pos (x->argument_at(3), this);
 799   LIRItem length  (x->argument_at(4), this);
 800 
 801   // Load all values in callee_save_registers (C calling convention),
 802   // as this makes the parameter passing to the fast case simpler.
 803   src.load_item_force     (FrameMap::R14_oop_opr);
 804   src_pos.load_item_force (FrameMap::R15_opr);
 805   dst.load_item_force     (FrameMap::R17_oop_opr);
 806   dst_pos.load_item_force (FrameMap::R18_opr);
 807   length.load_item_force  (FrameMap::R19_opr);
 808   LIR_Opr tmp =            FrameMap::R20_opr;
 809 
 810   int flags;
 811   ciArrayKlass* expected_type;
 812   arraycopy_helper(x, &flags, &expected_type);
 813 
 814   __ arraycopy(src.result(), src_pos.result(), dst.result(), dst_pos.result(),
 815                length.result(), tmp,
 816                expected_type, flags, info);
 817   set_no_result(x);
 818 }
 819 
 820 
 821 // _i2l, _i2f, _i2d, _l2i, _l2f, _l2d, _f2i, _f2l, _f2d, _d2i, _d2l, _d2f
 822 // _i2b, _i2c, _i2s
 823 void LIRGenerator::do_Convert(Convert* x) {
 824   if (!VM_Version::has_mtfprd()) {
 825     switch (x->op()) {
 826 
 827       // int -> float: force spill
 828       case Bytecodes::_l2f: {
 829         if (!VM_Version::has_fcfids()) { // fcfids is >= Power7 only
 830           // fcfid+frsp needs fixup code to avoid rounding incompatibility.
 831           address entry = CAST_FROM_FN_PTR(address, SharedRuntime::l2f);
 832           LIR_Opr result = call_runtime(x->value(), entry, x->type(), NULL);
 833           set_result(x, result);
 834           return;
 835         } // else fallthru
 836       }
 837       case Bytecodes::_l2d: {
 838         LIRItem value(x->value(), this);
 839         LIR_Opr reg = rlock_result(x);
 840         value.load_item();
 841         LIR_Opr tmp = force_to_spill(value.result(), T_DOUBLE);
 842         __ convert(x->op(), tmp, reg);
 843         return;
 844       }
 845       case Bytecodes::_i2f:
 846       case Bytecodes::_i2d: {
 847         LIRItem value(x->value(), this);
 848         LIR_Opr reg = rlock_result(x);
 849         value.load_item();
 850         // Convert i2l first.
 851         LIR_Opr tmp1 = new_register(T_LONG);
 852         __ convert(Bytecodes::_i2l, value.result(), tmp1);
 853         LIR_Opr tmp2 = force_to_spill(tmp1, T_DOUBLE);
 854         __ convert(x->op(), tmp2, reg);
 855         return;
 856       }
 857 
 858       // float -> int: result will be stored
 859       case Bytecodes::_f2l:
 860       case Bytecodes::_d2l: {
 861         LIRItem value(x->value(), this);
 862         LIR_Opr reg = rlock_result(x);
 863         value.set_destroys_register(); // USE_KILL
 864         value.load_item();
 865         set_vreg_flag(reg, must_start_in_memory);
 866         __ convert(x->op(), value.result(), reg);
 867         return;
 868       }
 869       case Bytecodes::_f2i:
 870       case Bytecodes::_d2i: {
 871         LIRItem value(x->value(), this);
 872         LIR_Opr reg = rlock_result(x);
 873         value.set_destroys_register(); // USE_KILL
 874         value.load_item();
 875         // Convert l2i afterwards.
 876         LIR_Opr tmp1 = new_register(T_LONG);
 877         set_vreg_flag(tmp1, must_start_in_memory);
 878         __ convert(x->op(), value.result(), tmp1);
 879         __ convert(Bytecodes::_l2i, tmp1, reg);
 880         return;
 881       }
 882 
 883       // Within same category: just register conversions.
 884       case Bytecodes::_i2b:
 885       case Bytecodes::_i2c:
 886       case Bytecodes::_i2s:
 887       case Bytecodes::_i2l:
 888       case Bytecodes::_l2i:
 889       case Bytecodes::_f2d:
 890       case Bytecodes::_d2f:
 891         break;
 892 
 893       default: ShouldNotReachHere();
 894     }
 895   }
 896 
 897   // Register conversion.
 898   LIRItem value(x->value(), this);
 899   LIR_Opr reg = rlock_result(x);
 900   value.load_item();
 901   switch (x->op()) {
 902     case Bytecodes::_f2l:
 903     case Bytecodes::_d2l:
 904     case Bytecodes::_f2i:
 905     case Bytecodes::_d2i: value.set_destroys_register(); break; // USE_KILL
 906     default: break;
 907   }
 908   __ convert(x->op(), value.result(), reg);
 909 }
 910 
 911 
 912 void LIRGenerator::do_NewInstance(NewInstance* x) {
 913   // This instruction can be deoptimized in the slow path.
 914   const LIR_Opr reg = result_register_for(x->type());
 915 #ifndef PRODUCT
 916   if (PrintNotLoaded && !x->klass()->is_loaded()) {
 917     tty->print_cr("   ###class not loaded at new bci %d", x->printable_bci());
 918   }
 919 #endif
 920   CodeEmitInfo* info = state_for(x, x->state());
 921   LIR_Opr klass_reg = FrameMap::R4_metadata_opr; // Used by slow path (NewInstanceStub).
 922   LIR_Opr tmp1 = FrameMap::R5_oop_opr;
 923   LIR_Opr tmp2 = FrameMap::R6_oop_opr;
 924   LIR_Opr tmp3 = FrameMap::R7_oop_opr;
 925   LIR_Opr tmp4 = FrameMap::R8_oop_opr;
 926   new_instance(reg, x->klass(), x->is_unresolved(), tmp1, tmp2, tmp3, tmp4, klass_reg, info);
 927 
 928   // Must prevent reordering of stores for object initialization
 929   // with stores that publish the new object.
 930   __ membar_storestore();
 931   LIR_Opr result = rlock_result(x);
 932   __ move(reg, result);
 933 }
 934 
 935 
 936 void LIRGenerator::do_NewTypeArray(NewTypeArray* x) {
 937   // Evaluate state_for early since it may emit code.
 938   CodeEmitInfo* info = state_for(x, x->state());
 939 
 940   LIRItem length(x->length(), this);
 941   length.load_item();
 942 
 943   LIR_Opr reg = result_register_for(x->type());
 944   LIR_Opr klass_reg = FrameMap::R4_metadata_opr; // Used by slow path (NewTypeArrayStub).
 945   // We use R5 in order to get a temp effect. This reg is used in slow path (NewTypeArrayStub).
 946   LIR_Opr tmp1 = FrameMap::R5_oop_opr;
 947   LIR_Opr tmp2 = FrameMap::R6_oop_opr;
 948   LIR_Opr tmp3 = FrameMap::R7_oop_opr;
 949   LIR_Opr tmp4 = FrameMap::R8_oop_opr;
 950   LIR_Opr len = length.result();
 951   BasicType elem_type = x->elt_type();
 952 
 953   __ metadata2reg(ciTypeArrayKlass::make(elem_type)->constant_encoding(), klass_reg);
 954 
 955   CodeStub* slow_path = new NewTypeArrayStub(klass_reg, len, reg, info);
 956   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, elem_type, klass_reg, slow_path);
 957 
 958   // Must prevent reordering of stores for object initialization
 959   // with stores that publish the new object.
 960   __ membar_storestore();
 961   LIR_Opr result = rlock_result(x);
 962   __ move(reg, result);
 963 }
 964 
 965 
 966 void LIRGenerator::do_NewObjectArray(NewObjectArray* x) {
 967   // Evaluate state_for early since it may emit code.
 968   CodeEmitInfo* info = state_for(x, x->state());
 969   // In case of patching (i.e., object class is not yet loaded),
 970   // we need to reexecute the instruction and therefore provide
 971   // the state before the parameters have been consumed.
 972   CodeEmitInfo* patching_info = NULL;
 973   if (!x->klass()->is_loaded() || PatchALot) {
 974     patching_info = state_for(x, x->state_before());
 975   }
 976 
 977   LIRItem length(x->length(), this);
 978   length.load_item();
 979 
 980   const LIR_Opr reg = result_register_for(x->type());
 981   LIR_Opr klass_reg = FrameMap::R4_metadata_opr; // Used by slow path (NewObjectArrayStub).
 982   // We use R5 in order to get a temp effect. This reg is used in slow path (NewObjectArrayStub).
 983   LIR_Opr tmp1 = FrameMap::R5_oop_opr;
 984   LIR_Opr tmp2 = FrameMap::R6_oop_opr;
 985   LIR_Opr tmp3 = FrameMap::R7_oop_opr;
 986   LIR_Opr tmp4 = FrameMap::R8_oop_opr;
 987   LIR_Opr len = length.result();
 988 
 989   CodeStub* slow_path = new NewObjectArrayStub(klass_reg, len, reg, info);
 990   ciMetadata* obj = ciObjArrayKlass::make(x->klass());
 991   if (obj == ciEnv::unloaded_ciobjarrayklass()) {
 992     BAILOUT("encountered unloaded_ciobjarrayklass due to out of memory error");
 993   }
 994   klass2reg_with_patching(klass_reg, obj, patching_info);
 995   __ allocate_array(reg, len, tmp1, tmp2, tmp3, tmp4, T_OBJECT, klass_reg, slow_path);
 996 
 997   // Must prevent reordering of stores for object initialization
 998   // with stores that publish the new object.
 999   __ membar_storestore();
1000   LIR_Opr result = rlock_result(x);
1001   __ move(reg, result);
1002 }
1003 
1004 
1005 void LIRGenerator::do_NewMultiArray(NewMultiArray* x) {
1006   Values* dims = x->dims();
1007   int i = dims->length();
1008   LIRItemList* items = new LIRItemList(i, i, NULL);
1009   while (i-- > 0) {
1010     LIRItem* size = new LIRItem(dims->at(i), this);
1011     items->at_put(i, size);
1012   }
1013 
1014   // Evaluate state_for early since it may emit code.
1015   CodeEmitInfo* patching_info = NULL;
1016   if (!x->klass()->is_loaded() || PatchALot) {
1017     patching_info = state_for(x, x->state_before());
1018 
1019     // Cannot re-use same xhandlers for multiple CodeEmitInfos, so
1020     // clone all handlers (NOTE: Usually this is handled transparently
1021     // by the CodeEmitInfo cloning logic in CodeStub constructors but
1022     // is done explicitly here because a stub isn't being used).
1023     x->set_exception_handlers(new XHandlers(x->exception_handlers()));
1024   }
1025   CodeEmitInfo* info = state_for(x, x->state());
1026 
1027   i = dims->length();
1028   while (i-- > 0) {
1029     LIRItem* size = items->at(i);
1030     size->load_nonconstant();
1031     // FrameMap::_reserved_argument_area_size includes the dimensions
1032     // varargs, because it's initialized to hir()->max_stack() when the
1033     // FrameMap is created.
1034     store_stack_parameter(size->result(), in_ByteSize(i*sizeof(jint) + FrameMap::first_available_sp_in_frame));
1035   }
1036 
1037   const LIR_Opr klass_reg = FrameMap::R4_metadata_opr; // Used by slow path.
1038   klass2reg_with_patching(klass_reg, x->klass(), patching_info);
1039 
1040   LIR_Opr rank = FrameMap::R5_opr; // Used by slow path.
1041   __ move(LIR_OprFact::intConst(x->rank()), rank);
1042 
1043   LIR_Opr varargs = FrameMap::as_pointer_opr(R6); // Used by slow path.
1044   __ leal(LIR_OprFact::address(new LIR_Address(FrameMap::SP_opr, FrameMap::first_available_sp_in_frame, T_INT)),
1045           varargs);
1046 
1047   // Note: This instruction can be deoptimized in the slow path.
1048   LIR_OprList* args = new LIR_OprList(3);
1049   args->append(klass_reg);
1050   args->append(rank);
1051   args->append(varargs);
1052   const LIR_Opr reg = result_register_for(x->type());
1053   __ call_runtime(Runtime1::entry_for(Runtime1::new_multi_array_id),
1054                   LIR_OprFact::illegalOpr,
1055                   reg, args, info);
1056 
1057   // Must prevent reordering of stores for object initialization
1058   // with stores that publish the new object.
1059   __ membar_storestore();
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     // Must do this before locking the destination register as
1075     // an oop register, and before the obj is loaded (so x->obj()->item()
1076     // is valid for creating a debug info location).
1077     patching_info = state_for(x, x->state_before());
1078   }
1079   obj.load_item();
1080   LIR_Opr out_reg = rlock_result(x);
1081   CodeStub* stub;
1082   CodeEmitInfo* info_for_exception =
1083       (x->needs_exception_state() ? state_for(x) :
1084                                     state_for(x, x->state_before(), true /*ignore_xhandler*/));
1085 
1086   if (x->is_incompatible_class_change_check()) {
1087     assert(patching_info == NULL, "can't patch this");
1088     stub = new SimpleExceptionStub(Runtime1::throw_incompatible_class_change_error_id,
1089                                    LIR_OprFact::illegalOpr, info_for_exception);
1090   } else if (x->is_invokespecial_receiver_check()) {
1091     assert(patching_info == NULL, "can't patch this");
1092     stub = new DeoptimizeStub(info_for_exception,
1093                               Deoptimization::Reason_class_check,
1094                               Deoptimization::Action_none);
1095   } else {
1096     stub = new SimpleExceptionStub(Runtime1::throw_class_cast_exception_id, obj.result(), info_for_exception);
1097   }
1098   // Following registers are used by slow_subtype_check:
1099   LIR_Opr tmp1 = FrameMap::R4_oop_opr; // super_klass
1100   LIR_Opr tmp2 = FrameMap::R5_oop_opr; // sub_klass
1101   LIR_Opr tmp3 = FrameMap::R6_oop_opr; // temp
1102   __ checkcast(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,
1103                x->direct_compare(), info_for_exception, patching_info, stub,
1104                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   // Ensure the result register is not the input register because the
1115   // result is initialized before the patching safepoint.
1116   obj.load_item();
1117   LIR_Opr out_reg = rlock_result(x);
1118   // Following registers are used by slow_subtype_check:
1119   LIR_Opr tmp1 = FrameMap::R4_oop_opr; // super_klass
1120   LIR_Opr tmp2 = FrameMap::R5_oop_opr; // sub_klass
1121   LIR_Opr tmp3 = FrameMap::R6_oop_opr; // temp
1122   __ instanceof(out_reg, obj.result(), x->klass(), tmp1, tmp2, tmp3,
1123                 x->direct_compare(), patching_info,
1124                 x->profiled_method(), x->profiled_bci());
1125 }
1126 
1127 
1128 void LIRGenerator::do_If(If* x) {
1129   assert(x->number_of_sux() == 2, "inconsistency");
1130   ValueTag tag = x->x()->type()->tag();
1131   LIRItem xitem(x->x(), this);
1132   LIRItem yitem(x->y(), this);
1133   LIRItem* xin = &xitem;
1134   LIRItem* yin = &yitem;
1135   If::Condition cond = x->cond();
1136 
1137   LIR_Opr left = LIR_OprFact::illegalOpr;
1138   LIR_Opr right = LIR_OprFact::illegalOpr;
1139 
1140   xin->load_item();
1141   left = xin->result();
1142 
1143   if (yin->result()->is_constant() && yin->result()->type() == T_INT &&
1144       Assembler::is_simm16(yin->result()->as_constant_ptr()->as_jint())) {
1145     // Inline int constants which are small enough to be immediate operands.
1146     right = LIR_OprFact::value_type(yin->value()->type());
1147   } else if (tag == longTag && yin->is_constant() && yin->get_jlong_constant() == 0 &&
1148              (cond == If::eql || cond == If::neq)) {
1149     // Inline long zero.
1150     right = LIR_OprFact::value_type(yin->value()->type());
1151   } else if (tag == objectTag && yin->is_constant() && (yin->get_jobject_constant()->is_null_object())) {
1152     right = LIR_OprFact::value_type(yin->value()->type());
1153   } else {
1154     yin->load_item();
1155     right = yin->result();
1156   }
1157   set_no_result(x);
1158 
1159   // Add safepoint before generating condition code so it can be recomputed.
1160   if (x->is_safepoint()) {
1161     // Increment backedge counter if needed.
1162     increment_backedge_counter_conditionally(lir_cond(cond), left, right, state_for(x, x->state_before()),
1163         x->tsux()->bci(), x->fsux()->bci(), x->profiled_bci());
1164     __ safepoint(safepoint_poll_register(), state_for(x, x->state_before()));
1165   }
1166 
1167   __ cmp(lir_cond(cond), left, right);
1168   // Generate branch profiling. Profiling code doesn't kill flags.
1169   profile_branch(x, cond);
1170   move_to_phi(x->state());
1171   if (x->x()->type()->is_float_kind()) {
1172     __ branch(lir_cond(cond), x->tsux(), x->usux());
1173   } else {
1174     __ branch(lir_cond(cond), x->tsux());
1175   }
1176   assert(x->default_sux() == x->fsux(), "wrong destination above");
1177   __ jump(x->default_sux());
1178 }
1179 
1180 
1181 LIR_Opr LIRGenerator::getThreadPointer() {
1182   return FrameMap::as_pointer_opr(R16_thread);
1183 }
1184 
1185 
1186 void LIRGenerator::trace_block_entry(BlockBegin* block) {
1187   LIR_Opr arg1 = FrameMap::R3_opr; // ARG1
1188   __ move(LIR_OprFact::intConst(block->block_id()), arg1);
1189   LIR_OprList* args = new LIR_OprList(1);
1190   args->append(arg1);
1191   address func = CAST_FROM_FN_PTR(address, Runtime1::trace_block_entry);
1192   __ call_runtime_leaf(func, LIR_OprFact::illegalOpr, LIR_OprFact::illegalOpr, args);
1193 }
1194 
1195 
1196 void LIRGenerator::volatile_field_store(LIR_Opr value, LIR_Address* address,
1197                                         CodeEmitInfo* info) {
1198 #ifdef _LP64
1199   __ store(value, address, info);
1200 #else
1201   Unimplemented();
1202 //  __ volatile_store_mem_reg(value, address, info);
1203 #endif
1204 }
1205 
1206 void LIRGenerator::volatile_field_load(LIR_Address* address, LIR_Opr result,
1207                                        CodeEmitInfo* info) {
1208 #ifdef _LP64
1209   __ load(address, result, info);
1210 #else
1211   Unimplemented();
1212 //  __ volatile_load_mem_reg(address, result, info);
1213 #endif
1214 }
1215 
1216 
1217 void LIRGenerator::do_update_CRC32(Intrinsic* x) {
1218   assert(UseCRC32Intrinsics, "or should not be here");
1219   LIR_Opr result = rlock_result(x);
1220 
1221   switch (x->id()) {
1222     case vmIntrinsics::_updateCRC32: {
1223       LIRItem crc(x->argument_at(0), this);
1224       LIRItem val(x->argument_at(1), this);
1225       // Registers destroyed by update_crc32.
1226       crc.set_destroys_register();
1227       val.set_destroys_register();
1228       crc.load_item();
1229       val.load_item();
1230       __ update_crc32(crc.result(), val.result(), result);
1231       break;
1232     }
1233     case vmIntrinsics::_updateBytesCRC32:
1234     case vmIntrinsics::_updateByteBufferCRC32: {
1235       bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32);
1236 
1237       LIRItem crc(x->argument_at(0), this);
1238       LIRItem buf(x->argument_at(1), this);
1239       LIRItem off(x->argument_at(2), this);
1240       LIRItem len(x->argument_at(3), this);
1241       buf.load_item();
1242       off.load_nonconstant();
1243 
1244       LIR_Opr index = off.result();
1245       int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
1246       if (off.result()->is_constant()) {
1247         index = LIR_OprFact::illegalOpr;
1248         offset += off.result()->as_jint();
1249       }
1250       LIR_Opr base_op = buf.result();
1251       LIR_Address* a = NULL;
1252 
1253       if (index->is_valid()) {
1254         LIR_Opr tmp = new_register(T_LONG);
1255         __ convert(Bytecodes::_i2l, index, tmp);
1256         index = tmp;
1257         __ add(index, LIR_OprFact::intptrConst(offset), index);
1258         a = new LIR_Address(base_op, index, T_BYTE);
1259       } else {
1260         a = new LIR_Address(base_op, offset, T_BYTE);
1261       }
1262 
1263       BasicTypeList signature(3);
1264       signature.append(T_INT);
1265       signature.append(T_ADDRESS);
1266       signature.append(T_INT);
1267       CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1268       const LIR_Opr result_reg = result_register_for(x->type());
1269 
1270       LIR_Opr arg1 = cc->at(0),
1271               arg2 = cc->at(1),
1272               arg3 = cc->at(2);
1273 
1274       crc.load_item_force(arg1); // We skip int->long conversion here, because CRC32 stub doesn't care about high bits.
1275       __ leal(LIR_OprFact::address(a), arg2);
1276       len.load_item_force(arg3); // We skip int->long conversion here, , because CRC32 stub expects int.
1277 
1278       __ call_runtime_leaf(StubRoutines::updateBytesCRC32(), LIR_OprFact::illegalOpr, result_reg, cc->args());
1279       __ move(result_reg, result);
1280       break;
1281     }
1282     default: {
1283       ShouldNotReachHere();
1284     }
1285   }
1286 }
1287 
1288 void LIRGenerator::do_update_CRC32C(Intrinsic* x) {
1289   assert(UseCRC32CIntrinsics, "or should not be here");
1290   LIR_Opr result = rlock_result(x);
1291 
1292   switch (x->id()) {
1293     case vmIntrinsics::_updateBytesCRC32C:
1294     case vmIntrinsics::_updateDirectByteBufferCRC32C: {
1295       bool is_updateBytes = (x->id() == vmIntrinsics::_updateBytesCRC32C);
1296 
1297       LIRItem crc(x->argument_at(0), this);
1298       LIRItem buf(x->argument_at(1), this);
1299       LIRItem off(x->argument_at(2), this);
1300       LIRItem end(x->argument_at(3), this);
1301       buf.load_item();
1302       off.load_nonconstant();
1303       end.load_nonconstant();
1304 
1305       // len = end - off
1306       LIR_Opr len  = end.result();
1307       LIR_Opr tmpA = new_register(T_INT);
1308       LIR_Opr tmpB = new_register(T_INT);
1309       __ move(end.result(), tmpA);
1310       __ move(off.result(), tmpB);
1311       __ sub(tmpA, tmpB, tmpA);
1312       len = tmpA;
1313 
1314       LIR_Opr index = off.result();
1315       int offset = is_updateBytes ? arrayOopDesc::base_offset_in_bytes(T_BYTE) : 0;
1316       if (off.result()->is_constant()) {
1317         index = LIR_OprFact::illegalOpr;
1318         offset += off.result()->as_jint();
1319       }
1320       LIR_Opr base_op = buf.result();
1321       LIR_Address* a = NULL;
1322 
1323       if (index->is_valid()) {
1324         LIR_Opr tmp = new_register(T_LONG);
1325         __ convert(Bytecodes::_i2l, index, tmp);
1326         index = tmp;
1327         __ add(index, LIR_OprFact::intptrConst(offset), index);
1328         a = new LIR_Address(base_op, index, T_BYTE);
1329       } else {
1330         a = new LIR_Address(base_op, offset, T_BYTE);
1331       }
1332 
1333       BasicTypeList signature(3);
1334       signature.append(T_INT);
1335       signature.append(T_ADDRESS);
1336       signature.append(T_INT);
1337       CallingConvention* cc = frame_map()->c_calling_convention(&signature);
1338       const LIR_Opr result_reg = result_register_for(x->type());
1339 
1340       LIR_Opr arg1 = cc->at(0),
1341               arg2 = cc->at(1),
1342               arg3 = cc->at(2);
1343 
1344       crc.load_item_force(arg1); // We skip int->long conversion here, because CRC32C stub doesn't care about high bits.
1345       __ leal(LIR_OprFact::address(a), arg2);
1346       __ move(len, cc->at(2));   // We skip int->long conversion here, because CRC32C stub expects int.
1347 
1348       __ call_runtime_leaf(StubRoutines::updateBytesCRC32C(), LIR_OprFact::illegalOpr, result_reg, cc->args());
1349       __ move(result_reg, result);
1350       break;
1351     }
1352     default: {
1353       ShouldNotReachHere();
1354     }
1355   }
1356 }
1357 
1358 void LIRGenerator::do_FmaIntrinsic(Intrinsic* x) {
1359   assert(x->number_of_arguments() == 3, "wrong type");
1360   assert(UseFMA, "Needs FMA instructions support.");
1361   LIRItem value(x->argument_at(0), this);
1362   LIRItem value1(x->argument_at(1), this);
1363   LIRItem value2(x->argument_at(2), this);
1364 
1365   value.load_item();
1366   value1.load_item();
1367   value2.load_item();
1368 
1369   LIR_Opr calc_input = value.result();
1370   LIR_Opr calc_input1 = value1.result();
1371   LIR_Opr calc_input2 = value2.result();
1372   LIR_Opr calc_result = rlock_result(x);
1373 
1374   switch (x->id()) {
1375   case vmIntrinsics::_fmaD: __ fmad(calc_input, calc_input1, calc_input2, calc_result); break;
1376   case vmIntrinsics::_fmaF: __ fmaf(calc_input, calc_input1, calc_input2, calc_result); break;
1377   default:                  ShouldNotReachHere();
1378   }
1379 }
1380 
1381 void LIRGenerator::do_vectorizedMismatch(Intrinsic* x) {
1382   fatal("vectorizedMismatch intrinsic is not implemented on this platform");
1383 }




--- EOF ---