1 /*
   2  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2021 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_LIRAssembler.hpp"
  30 #include "c1/c1_MacroAssembler.hpp"
  31 #include "c1/c1_Runtime1.hpp"
  32 #include "c1/c1_ValueStack.hpp"
  33 #include "ci/ciArrayKlass.hpp"
  34 #include "ci/ciInstance.hpp"
  35 #include "gc/shared/collectedHeap.hpp"
  36 #include "memory/universe.hpp"
  37 #include "nativeInst_ppc.hpp"
  38 #include "oops/compressedOops.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "runtime/frame.inline.hpp"
  41 #include "runtime/safepointMechanism.inline.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/vm_version.hpp"
  45 #include "utilities/powerOfTwo.hpp"
  46 
  47 #define __ _masm->
  48 
  49 
  50 const ConditionRegister LIR_Assembler::BOOL_RESULT = CCR5;
  51 
  52 
  53 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
  54   Unimplemented(); return false; // Currently not used on this platform.
  55 }
  56 
  57 
  58 LIR_Opr LIR_Assembler::receiverOpr() {
  59   return FrameMap::R3_oop_opr;
  60 }
  61 
  62 
  63 LIR_Opr LIR_Assembler::osrBufferPointer() {
  64   return FrameMap::R3_opr;
  65 }
  66 
  67 
  68 // This specifies the stack pointer decrement needed to build the frame.
  69 int LIR_Assembler::initial_frame_size_in_bytes() const {
  70   return in_bytes(frame_map()->framesize_in_bytes());
  71 }
  72 
  73 
  74 // Inline cache check: the inline cached class is in inline_cache_reg;
  75 // we fetch the class of the receiver and compare it with the cached class.
  76 // If they do not match we jump to slow case.
  77 int LIR_Assembler::check_icache() {
  78   int offset = __ offset();
  79   __ inline_cache_check(R3_ARG1, R19_inline_cache_reg);
  80   return offset;
  81 }
  82 
  83 void LIR_Assembler::clinit_barrier(ciMethod* method) {
  84   assert(!method->holder()->is_not_initialized(), "initialization should have been started");
  85 
  86   Label L_skip_barrier;
  87   Register klass = R20;
  88 
  89   metadata2reg(method->holder()->constant_encoding(), klass);
  90   __ clinit_barrier(klass, R16_thread, &L_skip_barrier /*L_fast_path*/);
  91 
  92   __ load_const_optimized(klass, SharedRuntime::get_handle_wrong_method_stub(), R0);
  93   __ mtctr(klass);
  94   __ bctr();
  95 
  96   __ bind(L_skip_barrier);
  97 }
  98 
  99 void LIR_Assembler::osr_entry() {
 100   // On-stack-replacement entry sequence:
 101   //
 102   //   1. Create a new compiled activation.
 103   //   2. Initialize local variables in the compiled activation. The expression
 104   //      stack must be empty at the osr_bci; it is not initialized.
 105   //   3. Jump to the continuation address in compiled code to resume execution.
 106 
 107   // OSR entry point
 108   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
 109   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
 110   ValueStack* entry_state = osr_entry->end()->state();
 111   int number_of_locks = entry_state->locks_size();
 112 
 113   // Create a frame for the compiled activation.
 114   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
 115 
 116   // OSR buffer is
 117   //
 118   // locals[nlocals-1..0]
 119   // monitors[number_of_locks-1..0]
 120   //
 121   // Locals is a direct copy of the interpreter frame so in the osr buffer
 122   // the first slot in the local array is the last local from the interpreter
 123   // and the last slot is local[0] (receiver) from the interpreter.
 124   //
 125   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 126   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 127   // in the interpreter frame (the method lock if a sync method).
 128 
 129   // Initialize monitors in the compiled activation.
 130   //   R3: pointer to osr buffer
 131   //
 132   // All other registers are dead at this point and the locals will be
 133   // copied into place by code emitted in the IR.
 134 
 135   Register OSR_buf = osrBufferPointer()->as_register();
 136   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 137     int monitor_offset = BytesPerWord * method()->max_locals() +
 138       (2 * BytesPerWord) * (number_of_locks - 1);
 139     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 140     // the OSR buffer using 2 word entries: first the lock and then
 141     // the oop.
 142     for (int i = 0; i < number_of_locks; i++) {
 143       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 144 #ifdef ASSERT
 145       // Verify the interpreter's monitor has a non-null object.
 146       {
 147         Label L;
 148         __ ld(R0, slot_offset + 1*BytesPerWord, OSR_buf);
 149         __ cmpdi(CCR0, R0, 0);
 150         __ bne(CCR0, L);
 151         __ stop("locked object is NULL");
 152         __ bind(L);
 153       }
 154 #endif // ASSERT
 155       // Copy the lock field into the compiled activation.
 156       Address ml = frame_map()->address_for_monitor_lock(i),
 157               mo = frame_map()->address_for_monitor_object(i);
 158       assert(ml.index() == noreg && mo.index() == noreg, "sanity");
 159       __ ld(R0, slot_offset + 0, OSR_buf);
 160       __ std(R0, ml.disp(), ml.base());
 161       __ ld(R0, slot_offset + 1*BytesPerWord, OSR_buf);
 162       __ std(R0, mo.disp(), mo.base());
 163     }
 164   }
 165 }
 166 
 167 
 168 int LIR_Assembler::emit_exception_handler() {
 169   // If the last instruction is a call (typically to do a throw which
 170   // is coming at the end after block reordering) the return address
 171   // must still point into the code area in order to avoid assertion
 172   // failures when searching for the corresponding bci => add a nop
 173   // (was bug 5/14/1999 - gri).
 174   __ nop();
 175 
 176   // Generate code for the exception handler.
 177   address handler_base = __ start_a_stub(exception_handler_size());
 178 
 179   if (handler_base == NULL) {
 180     // Not enough space left for the handler.
 181     bailout("exception handler overflow");
 182     return -1;
 183   }
 184 
 185   int offset = code_offset();
 186   address entry_point = CAST_FROM_FN_PTR(address, Runtime1::entry_for(Runtime1::handle_exception_from_callee_id));
 187   //__ load_const_optimized(R0, entry_point);
 188   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(entry_point));
 189   __ mtctr(R0);
 190   __ bctr();
 191 
 192   guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
 193   __ end_a_stub();
 194 
 195   return offset;
 196 }
 197 
 198 
 199 // Emit the code to remove the frame from the stack in the exception
 200 // unwind path.
 201 int LIR_Assembler::emit_unwind_handler() {
 202   _masm->block_comment("Unwind handler");
 203 
 204   int offset = code_offset();
 205   bool preserve_exception = method()->is_synchronized() || compilation()->env()->dtrace_method_probes();
 206   const Register Rexception = R3 /*LIRGenerator::exceptionOopOpr()*/, Rexception_save = R31;
 207 
 208   // Fetch the exception from TLS and clear out exception related thread state.
 209   __ ld(Rexception, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 210   __ li(R0, 0);
 211   __ std(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
 212   __ std(R0, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
 213 
 214   __ bind(_unwind_handler_entry);
 215   __ verify_not_null_oop(Rexception);
 216   if (preserve_exception) { __ mr(Rexception_save, Rexception); }
 217 
 218   // Perform needed unlocking
 219   MonitorExitStub* stub = NULL;
 220   if (method()->is_synchronized()) {
 221     monitor_address(0, FrameMap::R4_opr);
 222     stub = new MonitorExitStub(FrameMap::R4_opr, true, 0);
 223     __ unlock_object(R5, R6, R4, *stub->entry());
 224     __ bind(*stub->continuation());
 225   }
 226 
 227   if (compilation()->env()->dtrace_method_probes()) {
 228     Unimplemented();
 229   }
 230 
 231   // Dispatch to the unwind logic.
 232   address unwind_stub = Runtime1::entry_for(Runtime1::unwind_exception_id);
 233   //__ load_const_optimized(R0, unwind_stub);
 234   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(unwind_stub));
 235   if (preserve_exception) { __ mr(Rexception, Rexception_save); }
 236   __ mtctr(R0);
 237   __ bctr();
 238 
 239   // Emit the slow path assembly.
 240   if (stub != NULL) {
 241     stub->emit_code(this);
 242   }
 243 
 244   return offset;
 245 }
 246 
 247 
 248 int LIR_Assembler::emit_deopt_handler() {
 249   // If the last instruction is a call (typically to do a throw which
 250   // is coming at the end after block reordering) the return address
 251   // must still point into the code area in order to avoid assertion
 252   // failures when searching for the corresponding bci => add a nop
 253   // (was bug 5/14/1999 - gri).
 254   __ nop();
 255 
 256   // Generate code for deopt handler.
 257   address handler_base = __ start_a_stub(deopt_handler_size());
 258 
 259   if (handler_base == NULL) {
 260     // Not enough space left for the handler.
 261     bailout("deopt handler overflow");
 262     return -1;
 263   }
 264 
 265   int offset = code_offset();
 266   __ bl64_patchable(SharedRuntime::deopt_blob()->unpack(), relocInfo::runtime_call_type);
 267 
 268   guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
 269   __ end_a_stub();
 270 
 271   return offset;
 272 }
 273 
 274 
 275 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
 276   if (o == NULL) {
 277     __ li(reg, 0);
 278   } else {
 279     AddressLiteral addrlit = __ constant_oop_address(o);
 280     __ load_const(reg, addrlit, (reg != R0) ? R0 : noreg);
 281   }
 282 }
 283 
 284 
 285 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
 286   // Allocate a new index in table to hold the object once it's been patched.
 287   int oop_index = __ oop_recorder()->allocate_oop_index(NULL);
 288   PatchingStub* patch = new PatchingStub(_masm, patching_id(info), oop_index);
 289 
 290   AddressLiteral addrlit((address)NULL, oop_Relocation::spec(oop_index));
 291   __ load_const(reg, addrlit, R0);
 292 
 293   patching_epilog(patch, lir_patch_normal, reg, info);
 294 }
 295 
 296 
 297 void LIR_Assembler::metadata2reg(Metadata* o, Register reg) {
 298   AddressLiteral md = __ constant_metadata_address(o); // Notify OOP recorder (don't need the relocation)
 299   __ load_const_optimized(reg, md.value(), (reg != R0) ? R0 : noreg);
 300 }
 301 
 302 
 303 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo *info) {
 304   // Allocate a new index in table to hold the klass once it's been patched.
 305   int index = __ oop_recorder()->allocate_metadata_index(NULL);
 306   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id, index);
 307 
 308   AddressLiteral addrlit((address)NULL, metadata_Relocation::spec(index));
 309   assert(addrlit.rspec().type() == relocInfo::metadata_type, "must be an metadata reloc");
 310   __ load_const(reg, addrlit, R0);
 311 
 312   patching_epilog(patch, lir_patch_normal, reg, info);
 313 }
 314 
 315 
 316 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr temp, LIR_Opr result, CodeEmitInfo* info) {
 317   const bool is_int = result->is_single_cpu();
 318   Register Rdividend = is_int ? left->as_register() : left->as_register_lo();
 319   Register Rdivisor  = noreg;
 320   Register Rscratch  = temp->as_register();
 321   Register Rresult   = is_int ? result->as_register() : result->as_register_lo();
 322   long divisor = -1;
 323 
 324   if (right->is_register()) {
 325     Rdivisor = is_int ? right->as_register() : right->as_register_lo();
 326   } else {
 327     divisor = is_int ? right->as_constant_ptr()->as_jint()
 328                      : right->as_constant_ptr()->as_jlong();
 329   }
 330 
 331   assert(Rdividend != Rscratch, "");
 332   assert(Rdivisor  != Rscratch, "");
 333   assert(code == lir_idiv || code == lir_irem, "Must be irem or idiv");
 334 
 335   if (Rdivisor == noreg) {
 336     if (divisor == 1) { // stupid, but can happen
 337       if (code == lir_idiv) {
 338         __ mr_if_needed(Rresult, Rdividend);
 339       } else {
 340         __ li(Rresult, 0);
 341       }
 342 
 343     } else if (is_power_of_2(divisor)) {
 344       // Convert division by a power of two into some shifts and logical operations.
 345       int log2 = log2i_exact(divisor);
 346 
 347       // Round towards 0.
 348       if (divisor == 2) {
 349         if (is_int) {
 350           __ srwi(Rscratch, Rdividend, 31);
 351         } else {
 352           __ srdi(Rscratch, Rdividend, 63);
 353         }
 354       } else {
 355         if (is_int) {
 356           __ srawi(Rscratch, Rdividend, 31);
 357         } else {
 358           __ sradi(Rscratch, Rdividend, 63);
 359         }
 360         __ clrldi(Rscratch, Rscratch, 64-log2);
 361       }
 362       __ add(Rscratch, Rdividend, Rscratch);
 363 
 364       if (code == lir_idiv) {
 365         if (is_int) {
 366           __ srawi(Rresult, Rscratch, log2);
 367         } else {
 368           __ sradi(Rresult, Rscratch, log2);
 369         }
 370       } else { // lir_irem
 371         __ clrrdi(Rscratch, Rscratch, log2);
 372         __ sub(Rresult, Rdividend, Rscratch);
 373       }
 374 
 375     } else if (divisor == -1) {
 376       if (code == lir_idiv) {
 377         __ neg(Rresult, Rdividend);
 378       } else {
 379         __ li(Rresult, 0);
 380       }
 381 
 382     } else {
 383       __ load_const_optimized(Rscratch, divisor);
 384       if (code == lir_idiv) {
 385         if (is_int) {
 386           __ divw(Rresult, Rdividend, Rscratch); // Can't divide minint/-1.
 387         } else {
 388           __ divd(Rresult, Rdividend, Rscratch); // Can't divide minint/-1.
 389         }
 390       } else {
 391         assert(Rscratch != R0, "need both");
 392         if (is_int) {
 393           __ divw(R0, Rdividend, Rscratch); // Can't divide minint/-1.
 394           __ mullw(Rscratch, R0, Rscratch);
 395         } else {
 396           __ divd(R0, Rdividend, Rscratch); // Can't divide minint/-1.
 397           __ mulld(Rscratch, R0, Rscratch);
 398         }
 399         __ sub(Rresult, Rdividend, Rscratch);
 400       }
 401 
 402     }
 403     return;
 404   }
 405 
 406   Label regular, done;
 407   if (is_int) {
 408     __ cmpwi(CCR0, Rdivisor, -1);
 409   } else {
 410     __ cmpdi(CCR0, Rdivisor, -1);
 411   }
 412   __ bne(CCR0, regular);
 413   if (code == lir_idiv) {
 414     __ neg(Rresult, Rdividend);
 415     __ b(done);
 416     __ bind(regular);
 417     if (is_int) {
 418       __ divw(Rresult, Rdividend, Rdivisor); // Can't divide minint/-1.
 419     } else {
 420       __ divd(Rresult, Rdividend, Rdivisor); // Can't divide minint/-1.
 421     }
 422   } else { // lir_irem
 423     __ li(Rresult, 0);
 424     __ b(done);
 425     __ bind(regular);
 426     if (is_int) {
 427       __ divw(Rscratch, Rdividend, Rdivisor); // Can't divide minint/-1.
 428       __ mullw(Rscratch, Rscratch, Rdivisor);
 429     } else {
 430       __ divd(Rscratch, Rdividend, Rdivisor); // Can't divide minint/-1.
 431       __ mulld(Rscratch, Rscratch, Rdivisor);
 432     }
 433     __ sub(Rresult, Rdividend, Rscratch);
 434   }
 435   __ bind(done);
 436 }
 437 
 438 
 439 void LIR_Assembler::emit_op3(LIR_Op3* op) {
 440   switch (op->code()) {
 441   case lir_idiv:
 442   case lir_irem:
 443     arithmetic_idiv(op->code(), op->in_opr1(), op->in_opr2(), op->in_opr3(),
 444                     op->result_opr(), op->info());
 445     break;
 446   case lir_fmad:
 447     __ fmadd(op->result_opr()->as_double_reg(), op->in_opr1()->as_double_reg(),
 448              op->in_opr2()->as_double_reg(), op->in_opr3()->as_double_reg());
 449     break;
 450   case lir_fmaf:
 451     __ fmadds(op->result_opr()->as_float_reg(), op->in_opr1()->as_float_reg(),
 452               op->in_opr2()->as_float_reg(), op->in_opr3()->as_float_reg());
 453     break;
 454   default: ShouldNotReachHere(); break;
 455   }
 456 }
 457 
 458 
 459 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
 460 #ifdef ASSERT
 461   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
 462   if (op->block() != NULL)  _branch_target_blocks.append(op->block());
 463   if (op->ublock() != NULL) _branch_target_blocks.append(op->ublock());
 464   assert(op->info() == NULL, "shouldn't have CodeEmitInfo");
 465 #endif
 466 
 467   Label *L = op->label();
 468   if (op->cond() == lir_cond_always) {
 469     __ b(*L);
 470   } else {
 471     Label done;
 472     bool is_unordered = false;
 473     if (op->code() == lir_cond_float_branch) {
 474       assert(op->ublock() != NULL, "must have unordered successor");
 475       is_unordered = true;
 476     } else {
 477       assert(op->code() == lir_branch, "just checking");
 478     }
 479 
 480     bool positive = false;
 481     Assembler::Condition cond = Assembler::equal;
 482     switch (op->cond()) {
 483       case lir_cond_equal:        positive = true ; cond = Assembler::equal  ; is_unordered = false; break;
 484       case lir_cond_notEqual:     positive = false; cond = Assembler::equal  ; is_unordered = false; break;
 485       case lir_cond_less:         positive = true ; cond = Assembler::less   ; break;
 486       case lir_cond_belowEqual:   assert(op->code() != lir_cond_float_branch, ""); // fallthru
 487       case lir_cond_lessEqual:    positive = false; cond = Assembler::greater; break;
 488       case lir_cond_greater:      positive = true ; cond = Assembler::greater; break;
 489       case lir_cond_aboveEqual:   assert(op->code() != lir_cond_float_branch, ""); // fallthru
 490       case lir_cond_greaterEqual: positive = false; cond = Assembler::less   ; break;
 491       default:                    ShouldNotReachHere();
 492     }
 493     int bo = positive ? Assembler::bcondCRbiIs1 : Assembler::bcondCRbiIs0;
 494     int bi = Assembler::bi0(BOOL_RESULT, cond);
 495     if (is_unordered) {
 496       if (positive) {
 497         if (op->ublock() == op->block()) {
 498           __ bc_far_optimized(Assembler::bcondCRbiIs1, __ bi0(BOOL_RESULT, Assembler::summary_overflow), *L);
 499         }
 500       } else {
 501         if (op->ublock() != op->block()) { __ bso(BOOL_RESULT, done); }
 502       }
 503     }
 504     __ bc_far_optimized(bo, bi, *L);
 505     __ bind(done);
 506   }
 507 }
 508 
 509 
 510 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
 511   Bytecodes::Code code = op->bytecode();
 512   LIR_Opr src = op->in_opr(),
 513           dst = op->result_opr();
 514 
 515   switch(code) {
 516     case Bytecodes::_i2l: {
 517       __ extsw(dst->as_register_lo(), src->as_register());
 518       break;
 519     }
 520     case Bytecodes::_l2i: {
 521       __ mr_if_needed(dst->as_register(), src->as_register_lo()); // high bits are garbage
 522       break;
 523     }
 524     case Bytecodes::_i2b: {
 525       __ extsb(dst->as_register(), src->as_register());
 526       break;
 527     }
 528     case Bytecodes::_i2c: {
 529       __ clrldi(dst->as_register(), src->as_register(), 64-16);
 530       break;
 531     }
 532     case Bytecodes::_i2s: {
 533       __ extsh(dst->as_register(), src->as_register());
 534       break;
 535     }
 536     case Bytecodes::_i2d:
 537     case Bytecodes::_l2d: {
 538       bool src_in_memory = !VM_Version::has_mtfprd();
 539       FloatRegister rdst = dst->as_double_reg();
 540       FloatRegister rsrc;
 541       if (src_in_memory) {
 542         rsrc = src->as_double_reg(); // via mem
 543       } else {
 544         // move src to dst register
 545         if (code == Bytecodes::_i2d) {
 546           __ mtfprwa(rdst, src->as_register());
 547         } else {
 548           __ mtfprd(rdst, src->as_register_lo());
 549         }
 550         rsrc = rdst;
 551       }
 552       __ fcfid(rdst, rsrc);
 553       break;
 554     }
 555     case Bytecodes::_i2f:
 556     case Bytecodes::_l2f: {
 557       bool src_in_memory = !VM_Version::has_mtfprd();
 558       FloatRegister rdst = dst->as_float_reg();
 559       FloatRegister rsrc;
 560       if (src_in_memory) {
 561         rsrc = src->as_double_reg(); // via mem
 562       } else {
 563         // move src to dst register
 564         if (code == Bytecodes::_i2f) {
 565           __ mtfprwa(rdst, src->as_register());
 566         } else {
 567           __ mtfprd(rdst, src->as_register_lo());
 568         }
 569         rsrc = rdst;
 570       }
 571       if (VM_Version::has_fcfids()) {
 572         __ fcfids(rdst, rsrc);
 573       } else {
 574         assert(code == Bytecodes::_i2f, "fcfid+frsp needs fixup code to avoid rounding incompatibility");
 575         __ fcfid(rdst, rsrc);
 576         __ frsp(rdst, rdst);
 577       }
 578       break;
 579     }
 580     case Bytecodes::_f2d: {
 581       __ fmr_if_needed(dst->as_double_reg(), src->as_float_reg());
 582       break;
 583     }
 584     case Bytecodes::_d2f: {
 585       __ frsp(dst->as_float_reg(), src->as_double_reg());
 586       break;
 587     }
 588     case Bytecodes::_d2i:
 589     case Bytecodes::_f2i: {
 590       bool dst_in_memory = !VM_Version::has_mtfprd();
 591       FloatRegister rsrc = (code == Bytecodes::_d2i) ? src->as_double_reg() : src->as_float_reg();
 592       Address       addr = dst_in_memory ? frame_map()->address_for_slot(dst->double_stack_ix()) : NULL;
 593       Label L;
 594       // Result must be 0 if value is NaN; test by comparing value to itself.
 595       __ fcmpu(CCR0, rsrc, rsrc);
 596       if (dst_in_memory) {
 597         __ li(R0, 0); // 0 in case of NAN
 598         __ std(R0, addr.disp(), addr.base());
 599       } else {
 600         __ li(dst->as_register(), 0);
 601       }
 602       __ bso(CCR0, L);
 603       __ fctiwz(rsrc, rsrc); // USE_KILL
 604       if (dst_in_memory) {
 605         __ stfd(rsrc, addr.disp(), addr.base());
 606       } else {
 607         __ mffprd(dst->as_register(), rsrc);
 608       }
 609       __ bind(L);
 610       break;
 611     }
 612     case Bytecodes::_d2l:
 613     case Bytecodes::_f2l: {
 614       bool dst_in_memory = !VM_Version::has_mtfprd();
 615       FloatRegister rsrc = (code == Bytecodes::_d2l) ? src->as_double_reg() : src->as_float_reg();
 616       Address       addr = dst_in_memory ? frame_map()->address_for_slot(dst->double_stack_ix()) : NULL;
 617       Label L;
 618       // Result must be 0 if value is NaN; test by comparing value to itself.
 619       __ fcmpu(CCR0, rsrc, rsrc);
 620       if (dst_in_memory) {
 621         __ li(R0, 0); // 0 in case of NAN
 622         __ std(R0, addr.disp(), addr.base());
 623       } else {
 624         __ li(dst->as_register_lo(), 0);
 625       }
 626       __ bso(CCR0, L);
 627       __ fctidz(rsrc, rsrc); // USE_KILL
 628       if (dst_in_memory) {
 629         __ stfd(rsrc, addr.disp(), addr.base());
 630       } else {
 631         __ mffprd(dst->as_register_lo(), rsrc);
 632       }
 633       __ bind(L);
 634       break;
 635     }
 636 
 637     default: ShouldNotReachHere();
 638   }
 639 }
 640 
 641 
 642 void LIR_Assembler::align_call(LIR_Code) {
 643   // do nothing since all instructions are word aligned on ppc
 644 }
 645 
 646 
 647 bool LIR_Assembler::emit_trampoline_stub_for_call(address target, Register Rtoc) {
 648   int start_offset = __ offset();
 649   // Put the entry point as a constant into the constant pool.
 650   const address entry_point_toc_addr   = __ address_constant(target, RelocationHolder::none);
 651   if (entry_point_toc_addr == NULL) {
 652     bailout("const section overflow");
 653     return false;
 654   }
 655   const int     entry_point_toc_offset = __ offset_to_method_toc(entry_point_toc_addr);
 656 
 657   // Emit the trampoline stub which will be related to the branch-and-link below.
 658   address stub = __ emit_trampoline_stub(entry_point_toc_offset, start_offset, Rtoc);
 659   if (!stub) {
 660     bailout("no space for trampoline stub");
 661     return false;
 662   }
 663   return true;
 664 }
 665 
 666 
 667 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
 668   assert(rtype==relocInfo::opt_virtual_call_type || rtype==relocInfo::static_call_type, "unexpected rtype");
 669 
 670   bool success = emit_trampoline_stub_for_call(op->addr());
 671   if (!success) { return; }
 672 
 673   __ relocate(rtype);
 674   // Note: At this point we do not have the address of the trampoline
 675   // stub, and the entry point might be too far away for bl, so __ pc()
 676   // serves as dummy and the bl will be patched later.
 677   __ code()->set_insts_mark();
 678   __ bl(__ pc());
 679   add_call_info(code_offset(), op->info());
 680 }
 681 
 682 
 683 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
 684   __ calculate_address_from_global_toc(R2_TOC, __ method_toc());
 685 
 686   // Virtual call relocation will point to ic load.
 687   address virtual_call_meta_addr = __ pc();
 688   // Load a clear inline cache.
 689   AddressLiteral empty_ic((address) Universe::non_oop_word());
 690   bool success = __ load_const_from_method_toc(R19_inline_cache_reg, empty_ic, R2_TOC);
 691   if (!success) {
 692     bailout("const section overflow");
 693     return;
 694   }
 695   // Call to fixup routine. Fixup routine uses ScopeDesc info
 696   // to determine who we intended to call.
 697   __ relocate(virtual_call_Relocation::spec(virtual_call_meta_addr));
 698 
 699   success = emit_trampoline_stub_for_call(op->addr(), R2_TOC);
 700   if (!success) { return; }
 701 
 702   // Note: At this point we do not have the address of the trampoline
 703   // stub, and the entry point might be too far away for bl, so __ pc()
 704   // serves as dummy and the bl will be patched later.
 705   __ bl(__ pc());
 706   add_call_info(code_offset(), op->info());
 707 }
 708 
 709 void LIR_Assembler::explicit_null_check(Register addr, CodeEmitInfo* info) {
 710   ImplicitNullCheckStub* stub = new ImplicitNullCheckStub(code_offset(), info);
 711   __ null_check(addr, stub->entry());
 712   append_code_stub(stub);
 713 }
 714 
 715 
 716 // Attention: caller must encode oop if needed
 717 int LIR_Assembler::store(LIR_Opr from_reg, Register base, int offset, BasicType type, bool wide) {
 718   int store_offset;
 719   if (!Assembler::is_simm16(offset)) {
 720     // For offsets larger than a simm16 we setup the offset.
 721     assert(wide && !from_reg->is_same_register(FrameMap::R0_opr), "large offset only supported in special case");
 722     __ load_const_optimized(R0, offset);
 723     store_offset = store(from_reg, base, R0, type, wide);
 724   } else {
 725     store_offset = code_offset();
 726     switch (type) {
 727       case T_BOOLEAN: // fall through
 728       case T_BYTE  : __ stb(from_reg->as_register(), offset, base); break;
 729       case T_CHAR  :
 730       case T_SHORT : __ sth(from_reg->as_register(), offset, base); break;
 731       case T_INT   : __ stw(from_reg->as_register(), offset, base); break;
 732       case T_LONG  : __ std(from_reg->as_register_lo(), offset, base); break;
 733       case T_ADDRESS:
 734       case T_METADATA: __ std(from_reg->as_register(), offset, base); break;
 735       case T_ARRAY : // fall through
 736       case T_OBJECT:
 737         {
 738           if (UseCompressedOops && !wide) {
 739             // Encoding done in caller
 740             __ stw(from_reg->as_register(), offset, base);
 741             __ verify_coop(from_reg->as_register(), FILE_AND_LINE);
 742           } else {
 743             __ std(from_reg->as_register(), offset, base);
 744             __ verify_oop(from_reg->as_register(), FILE_AND_LINE);
 745           }
 746           break;
 747         }
 748       case T_FLOAT : __ stfs(from_reg->as_float_reg(), offset, base); break;
 749       case T_DOUBLE: __ stfd(from_reg->as_double_reg(), offset, base); break;
 750       default      : ShouldNotReachHere();
 751     }
 752   }
 753   return store_offset;
 754 }
 755 
 756 
 757 // Attention: caller must encode oop if needed
 758 int LIR_Assembler::store(LIR_Opr from_reg, Register base, Register disp, BasicType type, bool wide) {
 759   int store_offset = code_offset();
 760   switch (type) {
 761     case T_BOOLEAN: // fall through
 762     case T_BYTE  : __ stbx(from_reg->as_register(), base, disp); break;
 763     case T_CHAR  :
 764     case T_SHORT : __ sthx(from_reg->as_register(), base, disp); break;
 765     case T_INT   : __ stwx(from_reg->as_register(), base, disp); break;
 766     case T_LONG  :
 767 #ifdef _LP64
 768       __ stdx(from_reg->as_register_lo(), base, disp);
 769 #else
 770       Unimplemented();
 771 #endif
 772       break;
 773     case T_ADDRESS:
 774       __ stdx(from_reg->as_register(), base, disp);
 775       break;
 776     case T_ARRAY : // fall through
 777     case T_OBJECT:
 778       {
 779         if (UseCompressedOops && !wide) {
 780           // Encoding done in caller.
 781           __ stwx(from_reg->as_register(), base, disp);
 782           __ verify_coop(from_reg->as_register(), FILE_AND_LINE); // kills R0
 783         } else {
 784           __ stdx(from_reg->as_register(), base, disp);
 785           __ verify_oop(from_reg->as_register(), FILE_AND_LINE); // kills R0
 786         }
 787         break;
 788       }
 789     case T_FLOAT : __ stfsx(from_reg->as_float_reg(), base, disp); break;
 790     case T_DOUBLE: __ stfdx(from_reg->as_double_reg(), base, disp); break;
 791     default      : ShouldNotReachHere();
 792   }
 793   return store_offset;
 794 }
 795 
 796 
 797 int LIR_Assembler::load(Register base, int offset, LIR_Opr to_reg, BasicType type, bool wide) {
 798   int load_offset;
 799   if (!Assembler::is_simm16(offset)) {
 800     // For offsets larger than a simm16 we setup the offset.
 801     __ load_const_optimized(R0, offset);
 802     load_offset = load(base, R0, to_reg, type, wide);
 803   } else {
 804     load_offset = code_offset();
 805     switch(type) {
 806       case T_BOOLEAN: // fall through
 807       case T_BYTE  :   __ lbz(to_reg->as_register(), offset, base);
 808                        __ extsb(to_reg->as_register(), to_reg->as_register()); break;
 809       case T_CHAR  :   __ lhz(to_reg->as_register(), offset, base); break;
 810       case T_SHORT :   __ lha(to_reg->as_register(), offset, base); break;
 811       case T_INT   :   __ lwa(to_reg->as_register(), offset, base); break;
 812       case T_LONG  :   __ ld(to_reg->as_register_lo(), offset, base); break;
 813       case T_METADATA: __ ld(to_reg->as_register(), offset, base); break;
 814       case T_ADDRESS:
 815         __ ld(to_reg->as_register(), offset, base);
 816         break;
 817       case T_ARRAY : // fall through
 818       case T_OBJECT:
 819         {
 820           if (UseCompressedOops && !wide) {
 821             __ lwz(to_reg->as_register(), offset, base);
 822             __ decode_heap_oop(to_reg->as_register());
 823           } else {
 824             __ ld(to_reg->as_register(), offset, base);
 825           }
 826           __ verify_oop(to_reg->as_register(), FILE_AND_LINE);
 827           break;
 828         }
 829       case T_FLOAT:  __ lfs(to_reg->as_float_reg(), offset, base); break;
 830       case T_DOUBLE: __ lfd(to_reg->as_double_reg(), offset, base); break;
 831       default      : ShouldNotReachHere();
 832     }
 833   }
 834   return load_offset;
 835 }
 836 
 837 
 838 int LIR_Assembler::load(Register base, Register disp, LIR_Opr to_reg, BasicType type, bool wide) {
 839   int load_offset = code_offset();
 840   switch(type) {
 841     case T_BOOLEAN: // fall through
 842     case T_BYTE  :  __ lbzx(to_reg->as_register(), base, disp);
 843                     __ extsb(to_reg->as_register(), to_reg->as_register()); break;
 844     case T_CHAR  :  __ lhzx(to_reg->as_register(), base, disp); break;
 845     case T_SHORT :  __ lhax(to_reg->as_register(), base, disp); break;
 846     case T_INT   :  __ lwax(to_reg->as_register(), base, disp); break;
 847     case T_ADDRESS: __ ldx(to_reg->as_register(), base, disp); break;
 848     case T_ARRAY : // fall through
 849     case T_OBJECT:
 850       {
 851         if (UseCompressedOops && !wide) {
 852           __ lwzx(to_reg->as_register(), base, disp);
 853           __ decode_heap_oop(to_reg->as_register());
 854         } else {
 855           __ ldx(to_reg->as_register(), base, disp);
 856         }
 857         __ verify_oop(to_reg->as_register(), FILE_AND_LINE);
 858         break;
 859       }
 860     case T_FLOAT:  __ lfsx(to_reg->as_float_reg() , base, disp); break;
 861     case T_DOUBLE: __ lfdx(to_reg->as_double_reg(), base, disp); break;
 862     case T_LONG  :
 863 #ifdef _LP64
 864       __ ldx(to_reg->as_register_lo(), base, disp);
 865 #else
 866       Unimplemented();
 867 #endif
 868       break;
 869     default      : ShouldNotReachHere();
 870   }
 871   return load_offset;
 872 }
 873 
 874 
 875 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 876   LIR_Const* c = src->as_constant_ptr();
 877   Register src_reg = R0;
 878   switch (c->type()) {
 879     case T_INT:
 880     case T_FLOAT: {
 881       int value = c->as_jint_bits();
 882       __ load_const_optimized(src_reg, value);
 883       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 884       __ stw(src_reg, addr.disp(), addr.base());
 885       break;
 886     }
 887     case T_ADDRESS: {
 888       int value = c->as_jint_bits();
 889       __ load_const_optimized(src_reg, value);
 890       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 891       __ std(src_reg, addr.disp(), addr.base());
 892       break;
 893     }
 894     case T_OBJECT: {
 895       jobject2reg(c->as_jobject(), src_reg);
 896       Address addr = frame_map()->address_for_slot(dest->single_stack_ix());
 897       __ std(src_reg, addr.disp(), addr.base());
 898       break;
 899     }
 900     case T_LONG:
 901     case T_DOUBLE: {
 902       int value = c->as_jlong_bits();
 903       __ load_const_optimized(src_reg, value);
 904       Address addr = frame_map()->address_for_double_slot(dest->double_stack_ix());
 905       __ std(src_reg, addr.disp(), addr.base());
 906       break;
 907     }
 908     default:
 909       Unimplemented();
 910   }
 911 }
 912 
 913 
 914 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 915   LIR_Const* c = src->as_constant_ptr();
 916   LIR_Address* addr = dest->as_address_ptr();
 917   Register base = addr->base()->as_pointer_register();
 918   LIR_Opr tmp = LIR_OprFact::illegalOpr;
 919   int offset = -1;
 920   // Null check for large offsets in LIRGenerator::do_StoreField.
 921   bool needs_explicit_null_check = !ImplicitNullChecks;
 922 
 923   if (info != NULL && needs_explicit_null_check) {
 924     explicit_null_check(base, info);
 925   }
 926 
 927   switch (c->type()) {
 928     case T_FLOAT: type = T_INT;
 929     case T_INT:
 930     case T_ADDRESS: {
 931       tmp = FrameMap::R0_opr;
 932       __ load_const_optimized(tmp->as_register(), c->as_jint_bits());
 933       break;
 934     }
 935     case T_DOUBLE: type = T_LONG;
 936     case T_LONG: {
 937       tmp = FrameMap::R0_long_opr;
 938       __ load_const_optimized(tmp->as_register_lo(), c->as_jlong_bits());
 939       break;
 940     }
 941     case T_OBJECT: {
 942       tmp = FrameMap::R0_opr;
 943       if (UseCompressedOops && !wide && c->as_jobject() != NULL) {
 944         AddressLiteral oop_addr = __ constant_oop_address(c->as_jobject());
 945         __ lis(R0, oop_addr.value() >> 16); // Don't care about sign extend (will use stw).
 946         __ relocate(oop_addr.rspec(), /*compressed format*/ 1);
 947         __ ori(R0, R0, oop_addr.value() & 0xffff);
 948       } else {
 949         jobject2reg(c->as_jobject(), R0);
 950       }
 951       break;
 952     }
 953     default:
 954       Unimplemented();
 955   }
 956 
 957   // Handle either reg+reg or reg+disp address.
 958   if (addr->index()->is_valid()) {
 959     assert(addr->disp() == 0, "must be zero");
 960     offset = store(tmp, base, addr->index()->as_pointer_register(), type, wide);
 961   } else {
 962     assert(Assembler::is_simm16(addr->disp()), "can't handle larger addresses");
 963     offset = store(tmp, base, addr->disp(), type, wide);
 964   }
 965 
 966   if (info != NULL) {
 967     assert(offset != -1, "offset should've been set");
 968     if (!needs_explicit_null_check) {
 969       add_debug_info_for_null_check(offset, info);
 970     }
 971   }
 972 }
 973 
 974 
 975 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
 976   LIR_Const* c = src->as_constant_ptr();
 977   LIR_Opr to_reg = dest;
 978 
 979   switch (c->type()) {
 980     case T_INT: {
 981       assert(patch_code == lir_patch_none, "no patching handled here");
 982       __ load_const_optimized(dest->as_register(), c->as_jint(), R0);
 983       break;
 984     }
 985     case T_ADDRESS: {
 986       assert(patch_code == lir_patch_none, "no patching handled here");
 987       __ load_const_optimized(dest->as_register(), c->as_jint(), R0);  // Yes, as_jint ...
 988       break;
 989     }
 990     case T_LONG: {
 991       assert(patch_code == lir_patch_none, "no patching handled here");
 992       __ load_const_optimized(dest->as_register_lo(), c->as_jlong(), R0);
 993       break;
 994     }
 995 
 996     case T_OBJECT: {
 997       if (patch_code == lir_patch_none) {
 998         jobject2reg(c->as_jobject(), to_reg->as_register());
 999       } else {
1000         jobject2reg_with_patching(to_reg->as_register(), info);
1001       }
1002       break;
1003     }
1004 
1005     case T_METADATA:
1006       {
1007         if (patch_code == lir_patch_none) {
1008           metadata2reg(c->as_metadata(), to_reg->as_register());
1009         } else {
1010           klass2reg_with_patching(to_reg->as_register(), info);
1011         }
1012       }
1013       break;
1014 
1015     case T_FLOAT:
1016       {
1017         if (to_reg->is_single_fpu()) {
1018           address const_addr = __ float_constant(c->as_jfloat());
1019           if (const_addr == NULL) {
1020             bailout("const section overflow");
1021             break;
1022           }
1023           RelocationHolder rspec = internal_word_Relocation::spec(const_addr);
1024           __ relocate(rspec);
1025           __ load_const(R0, const_addr);
1026           __ lfsx(to_reg->as_float_reg(), R0);
1027         } else {
1028           assert(to_reg->is_single_cpu(), "Must be a cpu register.");
1029           __ load_const_optimized(to_reg->as_register(), jint_cast(c->as_jfloat()), R0);
1030         }
1031       }
1032       break;
1033 
1034     case T_DOUBLE:
1035       {
1036         if (to_reg->is_double_fpu()) {
1037           address const_addr = __ double_constant(c->as_jdouble());
1038           if (const_addr == NULL) {
1039             bailout("const section overflow");
1040             break;
1041           }
1042           RelocationHolder rspec = internal_word_Relocation::spec(const_addr);
1043           __ relocate(rspec);
1044           __ load_const(R0, const_addr);
1045           __ lfdx(to_reg->as_double_reg(), R0);
1046         } else {
1047           assert(to_reg->is_double_cpu(), "Must be a long register.");
1048           __ load_const_optimized(to_reg->as_register_lo(), jlong_cast(c->as_jdouble()), R0);
1049         }
1050       }
1051       break;
1052 
1053     default:
1054       ShouldNotReachHere();
1055   }
1056 }
1057 
1058 
1059 Address LIR_Assembler::as_Address(LIR_Address* addr) {
1060   Unimplemented(); return Address();
1061 }
1062 
1063 
1064 inline RegisterOrConstant index_or_disp(LIR_Address* addr) {
1065   if (addr->index()->is_illegal()) {
1066     return (RegisterOrConstant)(addr->disp());
1067   } else {
1068     return (RegisterOrConstant)(addr->index()->as_pointer_register());
1069   }
1070 }
1071 
1072 
1073 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
1074   const Register tmp = R0;
1075   switch (type) {
1076     case T_INT:
1077     case T_FLOAT: {
1078       Address from = frame_map()->address_for_slot(src->single_stack_ix());
1079       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
1080       __ lwz(tmp, from.disp(), from.base());
1081       __ stw(tmp, to.disp(), to.base());
1082       break;
1083     }
1084     case T_ADDRESS:
1085     case T_OBJECT: {
1086       Address from = frame_map()->address_for_slot(src->single_stack_ix());
1087       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
1088       __ ld(tmp, from.disp(), from.base());
1089       __ std(tmp, to.disp(), to.base());
1090       break;
1091     }
1092     case T_LONG:
1093     case T_DOUBLE: {
1094       Address from = frame_map()->address_for_double_slot(src->double_stack_ix());
1095       Address to   = frame_map()->address_for_double_slot(dest->double_stack_ix());
1096       __ ld(tmp, from.disp(), from.base());
1097       __ std(tmp, to.disp(), to.base());
1098       break;
1099     }
1100 
1101     default:
1102       ShouldNotReachHere();
1103   }
1104 }
1105 
1106 
1107 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
1108   Unimplemented(); return Address();
1109 }
1110 
1111 
1112 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
1113   Unimplemented(); return Address();
1114 }
1115 
1116 
1117 void LIR_Assembler::mem2reg(LIR_Opr src_opr, LIR_Opr dest, BasicType type,
1118                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide) {
1119 
1120   assert(type != T_METADATA, "load of metadata ptr not supported");
1121   LIR_Address* addr = src_opr->as_address_ptr();
1122   LIR_Opr to_reg = dest;
1123 
1124   Register src = addr->base()->as_pointer_register();
1125   Register disp_reg = noreg;
1126   int disp_value = addr->disp();
1127   bool needs_patching = (patch_code != lir_patch_none);
1128   // null check for large offsets in LIRGenerator::do_LoadField
1129   bool needs_explicit_null_check = !os::zero_page_read_protected() || !ImplicitNullChecks;
1130 
1131   if (info != NULL && needs_explicit_null_check) {
1132     explicit_null_check(src, info);
1133   }
1134 
1135   if (addr->base()->type() == T_OBJECT) {
1136     __ verify_oop(src, FILE_AND_LINE);
1137   }
1138 
1139   PatchingStub* patch = NULL;
1140   if (needs_patching) {
1141     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1142     assert(!to_reg->is_double_cpu() ||
1143            patch_code == lir_patch_none ||
1144            patch_code == lir_patch_normal, "patching doesn't match register");
1145   }
1146 
1147   if (addr->index()->is_illegal()) {
1148     if (!Assembler::is_simm16(disp_value)) {
1149       if (needs_patching) {
1150         __ load_const32(R0, 0); // patchable int
1151       } else {
1152         __ load_const_optimized(R0, disp_value);
1153       }
1154       disp_reg = R0;
1155     }
1156   } else {
1157     disp_reg = addr->index()->as_pointer_register();
1158     assert(disp_value == 0, "can't handle 3 operand addresses");
1159   }
1160 
1161   // Remember the offset of the load. The patching_epilog must be done
1162   // before the call to add_debug_info, otherwise the PcDescs don't get
1163   // entered in increasing order.
1164   int offset;
1165 
1166   if (disp_reg == noreg) {
1167     assert(Assembler::is_simm16(disp_value), "should have set this up");
1168     offset = load(src, disp_value, to_reg, type, wide);
1169   } else {
1170     offset = load(src, disp_reg, to_reg, type, wide);
1171   }
1172 
1173   if (patch != NULL) {
1174     patching_epilog(patch, patch_code, src, info);
1175   }
1176   if (info != NULL && !needs_explicit_null_check) {
1177     add_debug_info_for_null_check(offset, info);
1178   }
1179 }
1180 
1181 
1182 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
1183   Address addr;
1184   if (src->is_single_word()) {
1185     addr = frame_map()->address_for_slot(src->single_stack_ix());
1186   } else if (src->is_double_word())  {
1187     addr = frame_map()->address_for_double_slot(src->double_stack_ix());
1188   }
1189 
1190   load(addr.base(), addr.disp(), dest, dest->type(), true /*wide*/);
1191 }
1192 
1193 
1194 void LIR_Assembler::reg2stack(LIR_Opr from_reg, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
1195   Address addr;
1196   if (dest->is_single_word()) {
1197     addr = frame_map()->address_for_slot(dest->single_stack_ix());
1198   } else if (dest->is_double_word())  {
1199     addr = frame_map()->address_for_slot(dest->double_stack_ix());
1200   }
1201 
1202   store(from_reg, addr.base(), addr.disp(), from_reg->type(), true /*wide*/);
1203 }
1204 
1205 
1206 void LIR_Assembler::reg2reg(LIR_Opr from_reg, LIR_Opr to_reg) {
1207   if (from_reg->is_float_kind() && to_reg->is_float_kind()) {
1208     if (from_reg->is_double_fpu()) {
1209       // double to double moves
1210       assert(to_reg->is_double_fpu(), "should match");
1211       __ fmr_if_needed(to_reg->as_double_reg(), from_reg->as_double_reg());
1212     } else {
1213       // float to float moves
1214       assert(to_reg->is_single_fpu(), "should match");
1215       __ fmr_if_needed(to_reg->as_float_reg(), from_reg->as_float_reg());
1216     }
1217   } else if (!from_reg->is_float_kind() && !to_reg->is_float_kind()) {
1218     if (from_reg->is_double_cpu()) {
1219       __ mr_if_needed(to_reg->as_pointer_register(), from_reg->as_pointer_register());
1220     } else if (to_reg->is_double_cpu()) {
1221       // int to int moves
1222       __ mr_if_needed(to_reg->as_register_lo(), from_reg->as_register());
1223     } else {
1224       // int to int moves
1225       __ mr_if_needed(to_reg->as_register(), from_reg->as_register());
1226     }
1227   } else {
1228     ShouldNotReachHere();
1229   }
1230   if (is_reference_type(to_reg->type())) {
1231     __ verify_oop(to_reg->as_register(), FILE_AND_LINE);
1232   }
1233 }
1234 
1235 
1236 void LIR_Assembler::reg2mem(LIR_Opr from_reg, LIR_Opr dest, BasicType type,
1237                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack,
1238                             bool wide) {
1239   assert(type != T_METADATA, "store of metadata ptr not supported");
1240   LIR_Address* addr = dest->as_address_ptr();
1241 
1242   Register src = addr->base()->as_pointer_register();
1243   Register disp_reg = noreg;
1244   int disp_value = addr->disp();
1245   bool needs_patching = (patch_code != lir_patch_none);
1246   bool compress_oop = (is_reference_type(type)) && UseCompressedOops && !wide &&
1247                       CompressedOops::mode() != CompressedOops::UnscaledNarrowOop;
1248   bool load_disp = addr->index()->is_illegal() && !Assembler::is_simm16(disp_value);
1249   bool use_R29 = compress_oop && load_disp; // Avoid register conflict, also do null check before killing R29.
1250   // Null check for large offsets in LIRGenerator::do_StoreField.
1251   bool needs_explicit_null_check = !ImplicitNullChecks || use_R29;
1252 
1253   if (info != NULL && needs_explicit_null_check) {
1254     explicit_null_check(src, info);
1255   }
1256 
1257   if (addr->base()->is_oop_register()) {
1258     __ verify_oop(src, FILE_AND_LINE);
1259   }
1260 
1261   PatchingStub* patch = NULL;
1262   if (needs_patching) {
1263     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1264     assert(!from_reg->is_double_cpu() ||
1265            patch_code == lir_patch_none ||
1266            patch_code == lir_patch_normal, "patching doesn't match register");
1267   }
1268 
1269   if (addr->index()->is_illegal()) {
1270     if (load_disp) {
1271       disp_reg = use_R29 ? R29_TOC : R0;
1272       if (needs_patching) {
1273         __ load_const32(disp_reg, 0); // patchable int
1274       } else {
1275         __ load_const_optimized(disp_reg, disp_value);
1276       }
1277     }
1278   } else {
1279     disp_reg = addr->index()->as_pointer_register();
1280     assert(disp_value == 0, "can't handle 3 operand addresses");
1281   }
1282 
1283   // remember the offset of the store. The patching_epilog must be done
1284   // before the call to add_debug_info_for_null_check, otherwise the PcDescs don't get
1285   // entered in increasing order.
1286   int offset;
1287 
1288   if (compress_oop) {
1289     Register co = __ encode_heap_oop(R0, from_reg->as_register());
1290     from_reg = FrameMap::as_opr(co);
1291   }
1292 
1293   if (disp_reg == noreg) {
1294     assert(Assembler::is_simm16(disp_value), "should have set this up");
1295     offset = store(from_reg, src, disp_value, type, wide);
1296   } else {
1297     offset = store(from_reg, src, disp_reg, type, wide);
1298   }
1299 
1300   if (use_R29) {
1301     __ load_const_optimized(R29_TOC, MacroAssembler::global_toc(), R0); // reinit
1302   }
1303 
1304   if (patch != NULL) {
1305     patching_epilog(patch, patch_code, src, info);
1306   }
1307 
1308   if (info != NULL && !needs_explicit_null_check) {
1309     add_debug_info_for_null_check(offset, info);
1310   }
1311 }
1312 
1313 
1314 void LIR_Assembler::return_op(LIR_Opr result, C1SafepointPollStub* code_stub) {
1315   const Register return_pc = R31;  // Must survive C-call to enable_stack_reserved_zone().
1316   const Register temp      = R12;
1317 
1318   // Pop the stack before the safepoint code.
1319   int frame_size = initial_frame_size_in_bytes();
1320   if (Assembler::is_simm(frame_size, 16)) {
1321     __ addi(R1_SP, R1_SP, frame_size);
1322   } else {
1323     __ pop_frame();
1324   }
1325 
1326   // Restore return pc relative to callers' sp.
1327   __ ld(return_pc, _abi0(lr), R1_SP);
1328   // Move return pc to LR.
1329   __ mtlr(return_pc);
1330 
1331   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
1332     __ reserved_stack_check(return_pc);
1333   }
1334 
1335   // We need to mark the code position where the load from the safepoint
1336   // polling page was emitted as relocInfo::poll_return_type here.
1337   if (!UseSIGTRAP) {
1338     code_stub->set_safepoint_offset(__ offset());
1339     __ relocate(relocInfo::poll_return_type);
1340   }
1341   __ safepoint_poll(*code_stub->entry(), temp, true /* at_return */, true /* in_nmethod */);
1342 
1343   // Return.
1344   __ blr();
1345 }
1346 
1347 
1348 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
1349   const Register poll_addr = tmp->as_register();
1350   __ ld(poll_addr, in_bytes(JavaThread::polling_page_offset()), R16_thread);
1351   if (info != NULL) {
1352     add_debug_info_for_branch(info);
1353   }
1354   int offset = __ offset();
1355   __ relocate(relocInfo::poll_type);
1356   __ load_from_polling_page(poll_addr);
1357 
1358   return offset;
1359 }
1360 
1361 
1362 void LIR_Assembler::emit_static_call_stub() {
1363   address call_pc = __ pc();
1364   address stub = __ start_a_stub(static_call_stub_size());
1365   if (stub == NULL) {
1366     bailout("static call stub overflow");
1367     return;
1368   }
1369 
1370   // For java_to_interp stubs we use R11_scratch1 as scratch register
1371   // and in call trampoline stubs we use R12_scratch2. This way we
1372   // can distinguish them (see is_NativeCallTrampolineStub_at()).
1373   const Register reg_scratch = R11_scratch1;
1374 
1375   // Create a static stub relocation which relates this stub
1376   // with the call instruction at insts_call_instruction_offset in the
1377   // instructions code-section.
1378   int start = __ offset();
1379   __ relocate(static_stub_Relocation::spec(call_pc));
1380 
1381   // Now, create the stub's code:
1382   // - load the TOC
1383   // - load the inline cache oop from the constant pool
1384   // - load the call target from the constant pool
1385   // - call
1386   __ calculate_address_from_global_toc(reg_scratch, __ method_toc());
1387   AddressLiteral ic = __ allocate_metadata_address((Metadata *)NULL);
1388   bool success = __ load_const_from_method_toc(R19_inline_cache_reg, ic, reg_scratch, /*fixed_size*/ true);
1389 
1390   if (ReoptimizeCallSequences) {
1391     __ b64_patchable((address)-1, relocInfo::none);
1392   } else {
1393     AddressLiteral a((address)-1);
1394     success = success && __ load_const_from_method_toc(reg_scratch, a, reg_scratch, /*fixed_size*/ true);
1395     __ mtctr(reg_scratch);
1396     __ bctr();
1397   }
1398   if (!success) {
1399     bailout("const section overflow");
1400     return;
1401   }
1402 
1403   assert(__ offset() - start <= static_call_stub_size(), "stub too big");
1404   __ end_a_stub();
1405 }
1406 
1407 
1408 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
1409   bool unsigned_comp = (condition == lir_cond_belowEqual || condition == lir_cond_aboveEqual);
1410   if (opr1->is_single_fpu()) {
1411     __ fcmpu(BOOL_RESULT, opr1->as_float_reg(), opr2->as_float_reg());
1412   } else if (opr1->is_double_fpu()) {
1413     __ fcmpu(BOOL_RESULT, opr1->as_double_reg(), opr2->as_double_reg());
1414   } else if (opr1->is_single_cpu()) {
1415     if (opr2->is_constant()) {
1416       switch (opr2->as_constant_ptr()->type()) {
1417         case T_INT:
1418           {
1419             jint con = opr2->as_constant_ptr()->as_jint();
1420             if (unsigned_comp) {
1421               if (Assembler::is_uimm(con, 16)) {
1422                 __ cmplwi(BOOL_RESULT, opr1->as_register(), con);
1423               } else {
1424                 __ load_const_optimized(R0, con);
1425                 __ cmplw(BOOL_RESULT, opr1->as_register(), R0);
1426               }
1427             } else {
1428               if (Assembler::is_simm(con, 16)) {
1429                 __ cmpwi(BOOL_RESULT, opr1->as_register(), con);
1430               } else {
1431                 __ load_const_optimized(R0, con);
1432                 __ cmpw(BOOL_RESULT, opr1->as_register(), R0);
1433               }
1434             }
1435           }
1436           break;
1437 
1438         case T_OBJECT:
1439           // There are only equal/notequal comparisons on objects.
1440           {
1441             assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
1442             jobject con = opr2->as_constant_ptr()->as_jobject();
1443             if (con == NULL) {
1444               __ cmpdi(BOOL_RESULT, opr1->as_register(), 0);
1445             } else {
1446               jobject2reg(con, R0);
1447               __ cmpd(BOOL_RESULT, opr1->as_register(), R0);
1448             }
1449           }
1450           break;
1451 
1452         case T_METADATA:
1453           // We only need, for now, comparison with NULL for metadata.
1454           {
1455             assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
1456             Metadata* p = opr2->as_constant_ptr()->as_metadata();
1457             if (p == NULL) {
1458               __ cmpdi(BOOL_RESULT, opr1->as_register(), 0);
1459             } else {
1460               ShouldNotReachHere();
1461             }
1462           }
1463           break;
1464 
1465         default:
1466           ShouldNotReachHere();
1467           break;
1468       }
1469     } else {
1470       assert(opr1->type() != T_ADDRESS && opr2->type() != T_ADDRESS, "currently unsupported");
1471       if (is_reference_type(opr1->type())) {
1472         // There are only equal/notequal comparisons on objects.
1473         assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "oops");
1474         __ cmpd(BOOL_RESULT, opr1->as_register(), opr2->as_register());
1475       } else {
1476         if (unsigned_comp) {
1477           __ cmplw(BOOL_RESULT, opr1->as_register(), opr2->as_register());
1478         } else {
1479           __ cmpw(BOOL_RESULT, opr1->as_register(), opr2->as_register());
1480         }
1481       }
1482     }
1483   } else if (opr1->is_double_cpu()) {
1484     if (opr2->is_constant()) {
1485       jlong con = opr2->as_constant_ptr()->as_jlong();
1486       if (unsigned_comp) {
1487         if (Assembler::is_uimm(con, 16)) {
1488           __ cmpldi(BOOL_RESULT, opr1->as_register_lo(), con);
1489         } else {
1490           __ load_const_optimized(R0, con);
1491           __ cmpld(BOOL_RESULT, opr1->as_register_lo(), R0);
1492         }
1493       } else {
1494         if (Assembler::is_simm(con, 16)) {
1495           __ cmpdi(BOOL_RESULT, opr1->as_register_lo(), con);
1496         } else {
1497           __ load_const_optimized(R0, con);
1498           __ cmpd(BOOL_RESULT, opr1->as_register_lo(), R0);
1499         }
1500       }
1501     } else if (opr2->is_register()) {
1502       if (unsigned_comp) {
1503         __ cmpld(BOOL_RESULT, opr1->as_register_lo(), opr2->as_register_lo());
1504       } else {
1505         __ cmpd(BOOL_RESULT, opr1->as_register_lo(), opr2->as_register_lo());
1506       }
1507     } else {
1508       ShouldNotReachHere();
1509     }
1510   } else {
1511     ShouldNotReachHere();
1512   }
1513 }
1514 
1515 
1516 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op){
1517   const Register Rdst = dst->as_register();
1518   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
1519     bool is_unordered_less = (code == lir_ucmp_fd2i);
1520     if (left->is_single_fpu()) {
1521       __ fcmpu(CCR0, left->as_float_reg(), right->as_float_reg());
1522     } else if (left->is_double_fpu()) {
1523       __ fcmpu(CCR0, left->as_double_reg(), right->as_double_reg());
1524     } else {
1525       ShouldNotReachHere();
1526     }
1527     __ set_cmpu3(Rdst, is_unordered_less); // is_unordered_less ? -1 : 1
1528   } else if (code == lir_cmp_l2i) {
1529     __ cmpd(CCR0, left->as_register_lo(), right->as_register_lo());
1530     __ set_cmp3(Rdst);  // set result as follows: <: -1, =: 0, >: 1
1531   } else {
1532     ShouldNotReachHere();
1533   }
1534 }
1535 
1536 
1537 inline void load_to_reg(LIR_Assembler *lasm, LIR_Opr src, LIR_Opr dst) {
1538   if (src->is_constant()) {
1539     lasm->const2reg(src, dst, lir_patch_none, NULL);
1540   } else if (src->is_register()) {
1541     lasm->reg2reg(src, dst);
1542   } else if (src->is_stack()) {
1543     lasm->stack2reg(src, dst, dst->type());
1544   } else {
1545     ShouldNotReachHere();
1546   }
1547 }
1548 
1549 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type,
1550                           LIR_Opr cmp_opr1, LIR_Opr cmp_opr2) {
1551   assert(cmp_opr1 == LIR_OprFact::illegalOpr && cmp_opr2 == LIR_OprFact::illegalOpr, "unnecessary cmp oprs on ppc");
1552 
1553   if (opr1->is_equal(opr2) || opr1->is_same_register(opr2)) {
1554     load_to_reg(this, opr1, result); // Condition doesn't matter.
1555     return;
1556   }
1557 
1558   bool positive = false;
1559   Assembler::Condition cond = Assembler::equal;
1560   switch (condition) {
1561     case lir_cond_equal:        positive = true ; cond = Assembler::equal  ; break;
1562     case lir_cond_notEqual:     positive = false; cond = Assembler::equal  ; break;
1563     case lir_cond_less:         positive = true ; cond = Assembler::less   ; break;
1564     case lir_cond_belowEqual:
1565     case lir_cond_lessEqual:    positive = false; cond = Assembler::greater; break;
1566     case lir_cond_greater:      positive = true ; cond = Assembler::greater; break;
1567     case lir_cond_aboveEqual:
1568     case lir_cond_greaterEqual: positive = false; cond = Assembler::less   ; break;
1569     default:                    ShouldNotReachHere();
1570   }
1571 
1572   // Try to use isel on >=Power7.
1573   if (VM_Version::has_isel() && result->is_cpu_register()) {
1574     bool o1_is_reg = opr1->is_cpu_register(), o2_is_reg = opr2->is_cpu_register();
1575     const Register result_reg = result->is_single_cpu() ? result->as_register() : result->as_register_lo();
1576 
1577     // We can use result_reg to load one operand if not already in register.
1578     Register first  = o1_is_reg ? (opr1->is_single_cpu() ? opr1->as_register() : opr1->as_register_lo()) : result_reg,
1579              second = o2_is_reg ? (opr2->is_single_cpu() ? opr2->as_register() : opr2->as_register_lo()) : result_reg;
1580 
1581     if (first != second) {
1582       if (!o1_is_reg) {
1583         load_to_reg(this, opr1, result);
1584       }
1585 
1586       if (!o2_is_reg) {
1587         load_to_reg(this, opr2, result);
1588       }
1589 
1590       __ isel(result_reg, BOOL_RESULT, cond, !positive, first, second);
1591       return;
1592     }
1593   } // isel
1594 
1595   load_to_reg(this, opr1, result);
1596 
1597   Label skip;
1598   int bo = positive ? Assembler::bcondCRbiIs1 : Assembler::bcondCRbiIs0;
1599   int bi = Assembler::bi0(BOOL_RESULT, cond);
1600   __ bc(bo, bi, skip);
1601 
1602   load_to_reg(this, opr2, result);
1603   __ bind(skip);
1604 }
1605 
1606 
1607 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest,
1608                              CodeEmitInfo* info, bool pop_fpu_stack) {
1609   assert(info == NULL, "unused on this code path");
1610   assert(left->is_register(), "wrong items state");
1611   assert(dest->is_register(), "wrong items state");
1612 
1613   if (right->is_register()) {
1614     if (dest->is_float_kind()) {
1615 
1616       FloatRegister lreg, rreg, res;
1617       if (right->is_single_fpu()) {
1618         lreg = left->as_float_reg();
1619         rreg = right->as_float_reg();
1620         res  = dest->as_float_reg();
1621         switch (code) {
1622           case lir_add: __ fadds(res, lreg, rreg); break;
1623           case lir_sub: __ fsubs(res, lreg, rreg); break;
1624           case lir_mul: __ fmuls(res, lreg, rreg); break;
1625           case lir_div: __ fdivs(res, lreg, rreg); break;
1626           default: ShouldNotReachHere();
1627         }
1628       } else {
1629         lreg = left->as_double_reg();
1630         rreg = right->as_double_reg();
1631         res  = dest->as_double_reg();
1632         switch (code) {
1633           case lir_add: __ fadd(res, lreg, rreg); break;
1634           case lir_sub: __ fsub(res, lreg, rreg); break;
1635           case lir_mul: __ fmul(res, lreg, rreg); break;
1636           case lir_div: __ fdiv(res, lreg, rreg); break;
1637           default: ShouldNotReachHere();
1638         }
1639       }
1640 
1641     } else if (dest->is_double_cpu()) {
1642 
1643       Register dst_lo = dest->as_register_lo();
1644       Register op1_lo = left->as_pointer_register();
1645       Register op2_lo = right->as_pointer_register();
1646 
1647       switch (code) {
1648         case lir_add: __ add(dst_lo, op1_lo, op2_lo); break;
1649         case lir_sub: __ sub(dst_lo, op1_lo, op2_lo); break;
1650         case lir_mul: __ mulld(dst_lo, op1_lo, op2_lo); break;
1651         default: ShouldNotReachHere();
1652       }
1653     } else {
1654       assert (right->is_single_cpu(), "Just Checking");
1655 
1656       Register lreg = left->as_register();
1657       Register res  = dest->as_register();
1658       Register rreg = right->as_register();
1659       switch (code) {
1660         case lir_add:  __ add  (res, lreg, rreg); break;
1661         case lir_sub:  __ sub  (res, lreg, rreg); break;
1662         case lir_mul:  __ mullw(res, lreg, rreg); break;
1663         default: ShouldNotReachHere();
1664       }
1665     }
1666   } else {
1667     assert (right->is_constant(), "must be constant");
1668 
1669     if (dest->is_single_cpu()) {
1670       Register lreg = left->as_register();
1671       Register res  = dest->as_register();
1672       int    simm16 = right->as_constant_ptr()->as_jint();
1673 
1674       switch (code) {
1675         case lir_sub:  assert(Assembler::is_simm16(-simm16), "cannot encode"); // see do_ArithmeticOp_Int
1676                        simm16 = -simm16;
1677         case lir_add:  if (res == lreg && simm16 == 0) break;
1678                        __ addi(res, lreg, simm16); break;
1679         case lir_mul:  if (res == lreg && simm16 == 1) break;
1680                        __ mulli(res, lreg, simm16); break;
1681         default: ShouldNotReachHere();
1682       }
1683     } else {
1684       Register lreg = left->as_pointer_register();
1685       Register res  = dest->as_register_lo();
1686       long con = right->as_constant_ptr()->as_jlong();
1687       assert(Assembler::is_simm16(con), "must be simm16");
1688 
1689       switch (code) {
1690         case lir_sub:  assert(Assembler::is_simm16(-con), "cannot encode");  // see do_ArithmeticOp_Long
1691                        con = -con;
1692         case lir_add:  if (res == lreg && con == 0) break;
1693                        __ addi(res, lreg, (int)con); break;
1694         case lir_mul:  if (res == lreg && con == 1) break;
1695                        __ mulli(res, lreg, (int)con); break;
1696         default: ShouldNotReachHere();
1697       }
1698     }
1699   }
1700 }
1701 
1702 
1703 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr thread, LIR_Opr dest, LIR_Op* op) {
1704   switch (code) {
1705     case lir_sqrt: {
1706       __ fsqrt(dest->as_double_reg(), value->as_double_reg());
1707       break;
1708     }
1709     case lir_abs: {
1710       __ fabs(dest->as_double_reg(), value->as_double_reg());
1711       break;
1712     }
1713     default: {
1714       ShouldNotReachHere();
1715       break;
1716     }
1717   }
1718 }
1719 
1720 
1721 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest) {
1722   if (right->is_constant()) { // see do_LogicOp
1723     long uimm;
1724     Register d, l;
1725     if (dest->is_single_cpu()) {
1726       uimm = right->as_constant_ptr()->as_jint();
1727       d = dest->as_register();
1728       l = left->as_register();
1729     } else {
1730       uimm = right->as_constant_ptr()->as_jlong();
1731       d = dest->as_register_lo();
1732       l = left->as_register_lo();
1733     }
1734     long uimms  = (unsigned long)uimm >> 16,
1735          uimmss = (unsigned long)uimm >> 32;
1736 
1737     switch (code) {
1738       case lir_logic_and:
1739         if (uimmss != 0 || (uimms != 0 && (uimm & 0xFFFF) != 0) || is_power_of_2(uimm)) {
1740           __ andi(d, l, uimm); // special cases
1741         } else if (uimms != 0) { __ andis_(d, l, uimms); }
1742         else { __ andi_(d, l, uimm); }
1743         break;
1744 
1745       case lir_logic_or:
1746         if (uimms != 0) { assert((uimm & 0xFFFF) == 0, "sanity"); __ oris(d, l, uimms); }
1747         else { __ ori(d, l, uimm); }
1748         break;
1749 
1750       case lir_logic_xor:
1751         if (uimm == -1) { __ nand(d, l, l); } // special case
1752         else if (uimms != 0) { assert((uimm & 0xFFFF) == 0, "sanity"); __ xoris(d, l, uimms); }
1753         else { __ xori(d, l, uimm); }
1754         break;
1755 
1756       default: ShouldNotReachHere();
1757     }
1758   } else {
1759     assert(right->is_register(), "right should be in register");
1760 
1761     if (dest->is_single_cpu()) {
1762       switch (code) {
1763         case lir_logic_and: __ andr(dest->as_register(), left->as_register(), right->as_register()); break;
1764         case lir_logic_or:  __ orr (dest->as_register(), left->as_register(), right->as_register()); break;
1765         case lir_logic_xor: __ xorr(dest->as_register(), left->as_register(), right->as_register()); break;
1766         default: ShouldNotReachHere();
1767       }
1768     } else {
1769       Register l = (left->is_single_cpu() && left->is_oop_register()) ? left->as_register() :
1770                                                                         left->as_register_lo();
1771       Register r = (right->is_single_cpu() && right->is_oop_register()) ? right->as_register() :
1772                                                                           right->as_register_lo();
1773 
1774       switch (code) {
1775         case lir_logic_and: __ andr(dest->as_register_lo(), l, r); break;
1776         case lir_logic_or:  __ orr (dest->as_register_lo(), l, r); break;
1777         case lir_logic_xor: __ xorr(dest->as_register_lo(), l, r); break;
1778         default: ShouldNotReachHere();
1779       }
1780     }
1781   }
1782 }
1783 
1784 
1785 int LIR_Assembler::shift_amount(BasicType t) {
1786   int elem_size = type2aelembytes(t);
1787   switch (elem_size) {
1788     case 1 : return 0;
1789     case 2 : return 1;
1790     case 4 : return 2;
1791     case 8 : return 3;
1792   }
1793   ShouldNotReachHere();
1794   return -1;
1795 }
1796 
1797 
1798 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
1799   info->add_register_oop(exceptionOop);
1800 
1801   // Reuse the debug info from the safepoint poll for the throw op itself.
1802   address pc_for_athrow = __ pc();
1803   int pc_for_athrow_offset = __ offset();
1804   //RelocationHolder rspec = internal_word_Relocation::spec(pc_for_athrow);
1805   //__ relocate(rspec);
1806   //__ load_const(exceptionPC->as_register(), pc_for_athrow, R0);
1807   __ calculate_address_from_global_toc(exceptionPC->as_register(), pc_for_athrow, true, true, /*add_relocation*/ true);
1808   add_call_info(pc_for_athrow_offset, info); // for exception handler
1809 
1810   address stub = Runtime1::entry_for(compilation()->has_fpu_code() ? Runtime1::handle_exception_id
1811                                                                    : Runtime1::handle_exception_nofpu_id);
1812   //__ load_const_optimized(R0, stub);
1813   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(stub));
1814   __ mtctr(R0);
1815   __ bctr();
1816 }
1817 
1818 
1819 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
1820   // Note: Not used with EnableDebuggingOnDemand.
1821   assert(exceptionOop->as_register() == R3, "should match");
1822   __ b(_unwind_handler_entry);
1823 }
1824 
1825 
1826 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
1827   Register src = op->src()->as_register();
1828   Register dst = op->dst()->as_register();
1829   Register src_pos = op->src_pos()->as_register();
1830   Register dst_pos = op->dst_pos()->as_register();
1831   Register length  = op->length()->as_register();
1832   Register tmp = op->tmp()->as_register();
1833   Register tmp2 = R0;
1834 
1835   int flags = op->flags();
1836   ciArrayKlass* default_type = op->expected_type();
1837   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
1838   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
1839 
1840   // Set up the arraycopy stub information.
1841   ArrayCopyStub* stub = op->stub();
1842   const int frame_resize = frame::abi_reg_args_size - sizeof(frame::jit_abi); // C calls need larger frame.
1843 
1844   // Always do stub if no type information is available. It's ok if
1845   // the known type isn't loaded since the code sanity checks
1846   // in debug mode and the type isn't required when we know the exact type
1847   // also check that the type is an array type.
1848   if (op->expected_type() == NULL) {
1849     assert(src->is_nonvolatile() && src_pos->is_nonvolatile() && dst->is_nonvolatile() && dst_pos->is_nonvolatile() &&
1850            length->is_nonvolatile(), "must preserve");
1851     address copyfunc_addr = StubRoutines::generic_arraycopy();
1852     assert(copyfunc_addr != NULL, "generic arraycopy stub required");
1853 
1854     // 3 parms are int. Convert to long.
1855     __ mr(R3_ARG1, src);
1856     __ extsw(R4_ARG2, src_pos);
1857     __ mr(R5_ARG3, dst);
1858     __ extsw(R6_ARG4, dst_pos);
1859     __ extsw(R7_ARG5, length);
1860 
1861 #ifndef PRODUCT
1862     if (PrintC1Statistics) {
1863       address counter = (address)&Runtime1::_generic_arraycopystub_cnt;
1864       int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true);
1865       __ lwz(R11_scratch1, simm16_offs, tmp);
1866       __ addi(R11_scratch1, R11_scratch1, 1);
1867       __ stw(R11_scratch1, simm16_offs, tmp);
1868     }
1869 #endif
1870     __ call_c_with_frame_resize(copyfunc_addr, /*stub does not need resized frame*/ 0);
1871 
1872     __ nand(tmp, R3_RET, R3_RET);
1873     __ subf(length, tmp, length);
1874     __ add(src_pos, tmp, src_pos);
1875     __ add(dst_pos, tmp, dst_pos);
1876 
1877     __ cmpwi(CCR0, R3_RET, 0);
1878     __ bc_far_optimized(Assembler::bcondCRbiIs1, __ bi0(CCR0, Assembler::less), *stub->entry());
1879     __ bind(*stub->continuation());
1880     return;
1881   }
1882 
1883   assert(default_type != NULL && default_type->is_array_klass(), "must be true at this point");
1884   Label cont, slow, copyfunc;
1885 
1886   bool simple_check_flag_set = flags & (LIR_OpArrayCopy::src_null_check |
1887                                         LIR_OpArrayCopy::dst_null_check |
1888                                         LIR_OpArrayCopy::src_pos_positive_check |
1889                                         LIR_OpArrayCopy::dst_pos_positive_check |
1890                                         LIR_OpArrayCopy::length_positive_check);
1891 
1892   // Use only one conditional branch for simple checks.
1893   if (simple_check_flag_set) {
1894     ConditionRegister combined_check = CCR1, tmp_check = CCR1;
1895 
1896     // Make sure src and dst are non-null.
1897     if (flags & LIR_OpArrayCopy::src_null_check) {
1898       __ cmpdi(combined_check, src, 0);
1899       tmp_check = CCR0;
1900     }
1901 
1902     if (flags & LIR_OpArrayCopy::dst_null_check) {
1903       __ cmpdi(tmp_check, dst, 0);
1904       if (tmp_check != combined_check) {
1905         __ cror(combined_check, Assembler::equal, tmp_check, Assembler::equal);
1906       }
1907       tmp_check = CCR0;
1908     }
1909 
1910     // Clear combined_check.eq if not already used.
1911     if (tmp_check == combined_check) {
1912       __ crandc(combined_check, Assembler::equal, combined_check, Assembler::equal);
1913       tmp_check = CCR0;
1914     }
1915 
1916     if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
1917       // Test src_pos register.
1918       __ cmpwi(tmp_check, src_pos, 0);
1919       __ cror(combined_check, Assembler::equal, tmp_check, Assembler::less);
1920     }
1921 
1922     if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
1923       // Test dst_pos register.
1924       __ cmpwi(tmp_check, dst_pos, 0);
1925       __ cror(combined_check, Assembler::equal, tmp_check, Assembler::less);
1926     }
1927 
1928     if (flags & LIR_OpArrayCopy::length_positive_check) {
1929       // Make sure length isn't negative.
1930       __ cmpwi(tmp_check, length, 0);
1931       __ cror(combined_check, Assembler::equal, tmp_check, Assembler::less);
1932     }
1933 
1934     __ beq(combined_check, slow);
1935   }
1936 
1937   // If the compiler was not able to prove that exact type of the source or the destination
1938   // of the arraycopy is an array type, check at runtime if the source or the destination is
1939   // an instance type.
1940   if (flags & LIR_OpArrayCopy::type_check) {
1941     if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
1942       __ load_klass(tmp, dst);
1943       __ lwz(tmp2, in_bytes(Klass::layout_helper_offset()), tmp);
1944       __ cmpwi(CCR0, tmp2, Klass::_lh_neutral_value);
1945       __ bge(CCR0, slow);
1946     }
1947 
1948     if (!(flags & LIR_OpArrayCopy::src_objarray)) {
1949       __ load_klass(tmp, src);
1950       __ lwz(tmp2, in_bytes(Klass::layout_helper_offset()), tmp);
1951       __ cmpwi(CCR0, tmp2, Klass::_lh_neutral_value);
1952       __ bge(CCR0, slow);
1953     }
1954   }
1955 
1956   // Higher 32bits must be null.
1957   __ extsw(length, length);
1958 
1959   __ extsw(src_pos, src_pos);
1960   if (flags & LIR_OpArrayCopy::src_range_check) {
1961     __ lwz(tmp2, arrayOopDesc::length_offset_in_bytes(), src);
1962     __ add(tmp, length, src_pos);
1963     __ cmpld(CCR0, tmp2, tmp);
1964     __ ble(CCR0, slow);
1965   }
1966 
1967   __ extsw(dst_pos, dst_pos);
1968   if (flags & LIR_OpArrayCopy::dst_range_check) {
1969     __ lwz(tmp2, arrayOopDesc::length_offset_in_bytes(), dst);
1970     __ add(tmp, length, dst_pos);
1971     __ cmpld(CCR0, tmp2, tmp);
1972     __ ble(CCR0, slow);
1973   }
1974 
1975   int shift = shift_amount(basic_type);
1976 
1977   if (!(flags & LIR_OpArrayCopy::type_check)) {
1978     __ b(cont);
1979   } else {
1980     // We don't know the array types are compatible.
1981     if (basic_type != T_OBJECT) {
1982       // Simple test for basic type arrays.
1983       if (UseCompressedClassPointers) {
1984         // We don't need decode because we just need to compare.
1985         __ lwz(tmp, oopDesc::klass_offset_in_bytes(), src);
1986         __ lwz(tmp2, oopDesc::klass_offset_in_bytes(), dst);
1987         __ cmpw(CCR0, tmp, tmp2);
1988       } else {
1989         __ ld(tmp, oopDesc::klass_offset_in_bytes(), src);
1990         __ ld(tmp2, oopDesc::klass_offset_in_bytes(), dst);
1991         __ cmpd(CCR0, tmp, tmp2);
1992       }
1993       __ beq(CCR0, cont);
1994     } else {
1995       // For object arrays, if src is a sub class of dst then we can
1996       // safely do the copy.
1997       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
1998 
1999       const Register sub_klass = R5, super_klass = R4; // like CheckCast/InstanceOf
2000       assert_different_registers(tmp, tmp2, sub_klass, super_klass);
2001 
2002       __ load_klass(sub_klass, src);
2003       __ load_klass(super_klass, dst);
2004 
2005       __ check_klass_subtype_fast_path(sub_klass, super_klass, tmp, tmp2,
2006                                        &cont, copyfunc_addr != NULL ? &copyfunc : &slow, NULL);
2007 
2008       address slow_stc = Runtime1::entry_for(Runtime1::slow_subtype_check_id);
2009       //__ load_const_optimized(tmp, slow_stc, tmp2);
2010       __ calculate_address_from_global_toc(tmp, slow_stc, true, true, false);
2011       __ mtctr(tmp);
2012       __ bctrl(); // sets CR0
2013       __ beq(CCR0, cont);
2014 
2015       if (copyfunc_addr != NULL) { // Use stub if available.
2016         __ bind(copyfunc);
2017         // Src is not a sub class of dst so we have to do a
2018         // per-element check.
2019         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2020         if ((flags & mask) != mask) {
2021           assert(flags & mask, "one of the two should be known to be an object array");
2022 
2023           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2024             __ load_klass(tmp, src);
2025           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2026             __ load_klass(tmp, dst);
2027           }
2028 
2029           __ lwz(tmp2, in_bytes(Klass::layout_helper_offset()), tmp);
2030 
2031           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2032           __ load_const_optimized(tmp, objArray_lh);
2033           __ cmpw(CCR0, tmp, tmp2);
2034           __ bne(CCR0, slow);
2035         }
2036 
2037         Register src_ptr = R3_ARG1;
2038         Register dst_ptr = R4_ARG2;
2039         Register len     = R5_ARG3;
2040         Register chk_off = R6_ARG4;
2041         Register super_k = R7_ARG5;
2042 
2043         __ addi(src_ptr, src, arrayOopDesc::base_offset_in_bytes(basic_type));
2044         __ addi(dst_ptr, dst, arrayOopDesc::base_offset_in_bytes(basic_type));
2045         if (shift == 0) {
2046           __ add(src_ptr, src_pos, src_ptr);
2047           __ add(dst_ptr, dst_pos, dst_ptr);
2048         } else {
2049           __ sldi(tmp, src_pos, shift);
2050           __ sldi(tmp2, dst_pos, shift);
2051           __ add(src_ptr, tmp, src_ptr);
2052           __ add(dst_ptr, tmp2, dst_ptr);
2053         }
2054 
2055         __ load_klass(tmp, dst);
2056         __ mr(len, length);
2057 
2058         int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2059         __ ld(super_k, ek_offset, tmp);
2060 
2061         int sco_offset = in_bytes(Klass::super_check_offset_offset());
2062         __ lwz(chk_off, sco_offset, super_k);
2063 
2064         __ call_c_with_frame_resize(copyfunc_addr, /*stub does not need resized frame*/ 0);
2065 
2066 #ifndef PRODUCT
2067         if (PrintC1Statistics) {
2068           Label failed;
2069           __ cmpwi(CCR0, R3_RET, 0);
2070           __ bne(CCR0, failed);
2071           address counter = (address)&Runtime1::_arraycopy_checkcast_cnt;
2072           int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true);
2073           __ lwz(R11_scratch1, simm16_offs, tmp);
2074           __ addi(R11_scratch1, R11_scratch1, 1);
2075           __ stw(R11_scratch1, simm16_offs, tmp);
2076           __ bind(failed);
2077         }
2078 #endif
2079 
2080         __ nand(tmp, R3_RET, R3_RET);
2081         __ cmpwi(CCR0, R3_RET, 0);
2082         __ beq(CCR0, *stub->continuation());
2083 
2084 #ifndef PRODUCT
2085         if (PrintC1Statistics) {
2086           address counter = (address)&Runtime1::_arraycopy_checkcast_attempt_cnt;
2087           int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true);
2088           __ lwz(R11_scratch1, simm16_offs, tmp);
2089           __ addi(R11_scratch1, R11_scratch1, 1);
2090           __ stw(R11_scratch1, simm16_offs, tmp);
2091         }
2092 #endif
2093 
2094         __ subf(length, tmp, length);
2095         __ add(src_pos, tmp, src_pos);
2096         __ add(dst_pos, tmp, dst_pos);
2097       }
2098     }
2099   }
2100   __ bind(slow);
2101   __ b(*stub->entry());
2102   __ bind(cont);
2103 
2104 #ifdef ASSERT
2105   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2106     // Sanity check the known type with the incoming class. For the
2107     // primitive case the types must match exactly with src.klass and
2108     // dst.klass each exactly matching the default type. For the
2109     // object array case, if no type check is needed then either the
2110     // dst type is exactly the expected type and the src type is a
2111     // subtype which we can't check or src is the same array as dst
2112     // but not necessarily exactly of type default_type.
2113     Label known_ok, halt;
2114     metadata2reg(op->expected_type()->constant_encoding(), tmp);
2115     if (UseCompressedClassPointers) {
2116       // Tmp holds the default type. It currently comes uncompressed after the
2117       // load of a constant, so encode it.
2118       __ encode_klass_not_null(tmp);
2119       // Load the raw value of the dst klass, since we will be comparing
2120       // uncompressed values directly.
2121       __ lwz(tmp2, oopDesc::klass_offset_in_bytes(), dst);
2122       __ cmpw(CCR0, tmp, tmp2);
2123       if (basic_type != T_OBJECT) {
2124         __ bne(CCR0, halt);
2125         // Load the raw value of the src klass.
2126         __ lwz(tmp2, oopDesc::klass_offset_in_bytes(), src);
2127         __ cmpw(CCR0, tmp, tmp2);
2128         __ beq(CCR0, known_ok);
2129       } else {
2130         __ beq(CCR0, known_ok);
2131         __ cmpw(CCR0, src, dst);
2132         __ beq(CCR0, known_ok);
2133       }
2134     } else {
2135       __ ld(tmp2, oopDesc::klass_offset_in_bytes(), dst);
2136       __ cmpd(CCR0, tmp, tmp2);
2137       if (basic_type != T_OBJECT) {
2138         __ bne(CCR0, halt);
2139         // Load the raw value of the src klass.
2140         __ ld(tmp2, oopDesc::klass_offset_in_bytes(), src);
2141         __ cmpd(CCR0, tmp, tmp2);
2142         __ beq(CCR0, known_ok);
2143       } else {
2144         __ beq(CCR0, known_ok);
2145         __ cmpd(CCR0, src, dst);
2146         __ beq(CCR0, known_ok);
2147       }
2148     }
2149     __ bind(halt);
2150     __ stop("incorrect type information in arraycopy");
2151     __ bind(known_ok);
2152   }
2153 #endif
2154 
2155 #ifndef PRODUCT
2156   if (PrintC1Statistics) {
2157     address counter = Runtime1::arraycopy_count_address(basic_type);
2158     int simm16_offs = __ load_const_optimized(tmp, counter, tmp2, true);
2159     __ lwz(R11_scratch1, simm16_offs, tmp);
2160     __ addi(R11_scratch1, R11_scratch1, 1);
2161     __ stw(R11_scratch1, simm16_offs, tmp);
2162   }
2163 #endif
2164 
2165   Register src_ptr = R3_ARG1;
2166   Register dst_ptr = R4_ARG2;
2167   Register len     = R5_ARG3;
2168 
2169   __ addi(src_ptr, src, arrayOopDesc::base_offset_in_bytes(basic_type));
2170   __ addi(dst_ptr, dst, arrayOopDesc::base_offset_in_bytes(basic_type));
2171   if (shift == 0) {
2172     __ add(src_ptr, src_pos, src_ptr);
2173     __ add(dst_ptr, dst_pos, dst_ptr);
2174   } else {
2175     __ sldi(tmp, src_pos, shift);
2176     __ sldi(tmp2, dst_pos, shift);
2177     __ add(src_ptr, tmp, src_ptr);
2178     __ add(dst_ptr, tmp2, dst_ptr);
2179   }
2180 
2181   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2182   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2183   const char *name;
2184   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2185 
2186   // Arraycopy stubs takes a length in number of elements, so don't scale it.
2187   __ mr(len, length);
2188   __ call_c_with_frame_resize(entry, /*stub does not need resized frame*/ 0);
2189 
2190   __ bind(*stub->continuation());
2191 }
2192 
2193 
2194 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2195   if (dest->is_single_cpu()) {
2196     __ rldicl(tmp->as_register(), count->as_register(), 0, 64-5);
2197 #ifdef _LP64
2198     if (left->type() == T_OBJECT) {
2199       switch (code) {
2200         case lir_shl:  __ sld(dest->as_register(), left->as_register(), tmp->as_register()); break;
2201         case lir_shr:  __ srad(dest->as_register(), left->as_register(), tmp->as_register()); break;
2202         case lir_ushr: __ srd(dest->as_register(), left->as_register(), tmp->as_register()); break;
2203         default: ShouldNotReachHere();
2204       }
2205     } else
2206 #endif
2207       switch (code) {
2208         case lir_shl:  __ slw(dest->as_register(), left->as_register(), tmp->as_register()); break;
2209         case lir_shr:  __ sraw(dest->as_register(), left->as_register(), tmp->as_register()); break;
2210         case lir_ushr: __ srw(dest->as_register(), left->as_register(), tmp->as_register()); break;
2211         default: ShouldNotReachHere();
2212       }
2213   } else {
2214     __ rldicl(tmp->as_register(), count->as_register(), 0, 64-6);
2215     switch (code) {
2216       case lir_shl:  __ sld(dest->as_register_lo(), left->as_register_lo(), tmp->as_register()); break;
2217       case lir_shr:  __ srad(dest->as_register_lo(), left->as_register_lo(), tmp->as_register()); break;
2218       case lir_ushr: __ srd(dest->as_register_lo(), left->as_register_lo(), tmp->as_register()); break;
2219       default: ShouldNotReachHere();
2220     }
2221   }
2222 }
2223 
2224 
2225 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2226 #ifdef _LP64
2227   if (left->type() == T_OBJECT) {
2228     count = count & 63;  // Shouldn't shift by more than sizeof(intptr_t).
2229     if (count == 0) { __ mr_if_needed(dest->as_register_lo(), left->as_register()); }
2230     else {
2231       switch (code) {
2232         case lir_shl:  __ sldi(dest->as_register_lo(), left->as_register(), count); break;
2233         case lir_shr:  __ sradi(dest->as_register_lo(), left->as_register(), count); break;
2234         case lir_ushr: __ srdi(dest->as_register_lo(), left->as_register(), count); break;
2235         default: ShouldNotReachHere();
2236       }
2237     }
2238     return;
2239   }
2240 #endif
2241 
2242   if (dest->is_single_cpu()) {
2243     count = count & 0x1F; // Java spec
2244     if (count == 0) { __ mr_if_needed(dest->as_register(), left->as_register()); }
2245     else {
2246       switch (code) {
2247         case lir_shl: __ slwi(dest->as_register(), left->as_register(), count); break;
2248         case lir_shr:  __ srawi(dest->as_register(), left->as_register(), count); break;
2249         case lir_ushr: __ srwi(dest->as_register(), left->as_register(), count); break;
2250         default: ShouldNotReachHere();
2251       }
2252     }
2253   } else if (dest->is_double_cpu()) {
2254     count = count & 63; // Java spec
2255     if (count == 0) { __ mr_if_needed(dest->as_pointer_register(), left->as_pointer_register()); }
2256     else {
2257       switch (code) {
2258         case lir_shl:  __ sldi(dest->as_pointer_register(), left->as_pointer_register(), count); break;
2259         case lir_shr:  __ sradi(dest->as_pointer_register(), left->as_pointer_register(), count); break;
2260         case lir_ushr: __ srdi(dest->as_pointer_register(), left->as_pointer_register(), count); break;
2261         default: ShouldNotReachHere();
2262       }
2263     }
2264   } else {
2265     ShouldNotReachHere();
2266   }
2267 }
2268 
2269 
2270 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
2271   if (op->init_check()) {
2272     if (!os::zero_page_read_protected() || !ImplicitNullChecks) {
2273       explicit_null_check(op->klass()->as_register(), op->stub()->info());
2274     } else {
2275       add_debug_info_for_null_check_here(op->stub()->info());
2276     }
2277     __ lbz(op->tmp1()->as_register(),
2278            in_bytes(InstanceKlass::init_state_offset()), op->klass()->as_register());
2279     __ cmpwi(CCR0, op->tmp1()->as_register(), InstanceKlass::fully_initialized);
2280     __ bc_far_optimized(Assembler::bcondCRbiIs0, __ bi0(CCR0, Assembler::equal), *op->stub()->entry());
2281   }
2282   __ allocate_object(op->obj()->as_register(),
2283                      op->tmp1()->as_register(),
2284                      op->tmp2()->as_register(),
2285                      op->tmp3()->as_register(),
2286                      op->header_size(),
2287                      op->object_size(),
2288                      op->klass()->as_register(),
2289                      *op->stub()->entry());
2290 
2291   __ bind(*op->stub()->continuation());
2292   __ verify_oop(op->obj()->as_register(), FILE_AND_LINE);
2293 }
2294 
2295 
2296 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
2297   LP64_ONLY( __ extsw(op->len()->as_register(), op->len()->as_register()); )
2298   if (UseSlowPath ||
2299       (!UseFastNewObjectArray && (is_reference_type(op->type()))) ||
2300       (!UseFastNewTypeArray   && (!is_reference_type(op->type())))) {
2301     __ b(*op->stub()->entry());
2302   } else {
2303     __ allocate_array(op->obj()->as_register(),
2304                       op->len()->as_register(),
2305                       op->tmp1()->as_register(),
2306                       op->tmp2()->as_register(),
2307                       op->tmp3()->as_register(),
2308                       arrayOopDesc::header_size(op->type()),
2309                       type2aelembytes(op->type()),
2310                       op->klass()->as_register(),
2311                       *op->stub()->entry());
2312   }
2313   __ bind(*op->stub()->continuation());
2314 }
2315 
2316 
2317 void LIR_Assembler::type_profile_helper(Register mdo, int mdo_offset_bias,
2318                                         ciMethodData *md, ciProfileData *data,
2319                                         Register recv, Register tmp1, Label* update_done) {
2320   uint i;
2321   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2322     Label next_test;
2323     // See if the receiver is receiver[n].
2324     __ ld(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)) - mdo_offset_bias, mdo);
2325     __ verify_klass_ptr(tmp1);
2326     __ cmpd(CCR0, recv, tmp1);
2327     __ bne(CCR0, next_test);
2328 
2329     __ ld(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2330     __ addi(tmp1, tmp1, DataLayout::counter_increment);
2331     __ std(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2332     __ b(*update_done);
2333 
2334     __ bind(next_test);
2335   }
2336 
2337   // Didn't find receiver; find next empty slot and fill it in.
2338   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2339     Label next_test;
2340     __ ld(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)) - mdo_offset_bias, mdo);
2341     __ cmpdi(CCR0, tmp1, 0);
2342     __ bne(CCR0, next_test);
2343     __ li(tmp1, DataLayout::counter_increment);
2344     __ std(recv, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)) - mdo_offset_bias, mdo);
2345     __ std(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2346     __ b(*update_done);
2347 
2348     __ bind(next_test);
2349   }
2350 }
2351 
2352 
2353 void LIR_Assembler::setup_md_access(ciMethod* method, int bci,
2354                                     ciMethodData*& md, ciProfileData*& data, int& mdo_offset_bias) {
2355   md = method->method_data_or_null();
2356   assert(md != NULL, "Sanity");
2357   data = md->bci_to_data(bci);
2358   assert(data != NULL,       "need data for checkcast");
2359   assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2360   if (!Assembler::is_simm16(md->byte_offset_of_slot(data, DataLayout::header_offset()) + data->size_in_bytes())) {
2361     // The offset is large so bias the mdo by the base of the slot so
2362     // that the ld can use simm16s to reference the slots of the data.
2363     mdo_offset_bias = md->byte_offset_of_slot(data, DataLayout::header_offset());
2364   }
2365 }
2366 
2367 
2368 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
2369   const Register obj = op->object()->as_register(); // Needs to live in this register at safepoint (patching stub).
2370   Register k_RInfo = op->tmp1()->as_register();
2371   Register klass_RInfo = op->tmp2()->as_register();
2372   Register Rtmp1 = op->tmp3()->as_register();
2373   Register dst = op->result_opr()->as_register();
2374   ciKlass* k = op->klass();
2375   bool should_profile = op->should_profile();
2376   // Attention: do_temp(opTypeCheck->_object) is not used, i.e. obj may be same as one of the temps.
2377   bool reg_conflict = false;
2378   if (obj == k_RInfo) {
2379     k_RInfo = dst;
2380     reg_conflict = true;
2381   } else if (obj == klass_RInfo) {
2382     klass_RInfo = dst;
2383     reg_conflict = true;
2384   } else if (obj == Rtmp1) {
2385     Rtmp1 = dst;
2386     reg_conflict = true;
2387   }
2388   assert_different_registers(obj, k_RInfo, klass_RInfo, Rtmp1);
2389 
2390   __ cmpdi(CCR0, obj, 0);
2391 
2392   ciMethodData* md = NULL;
2393   ciProfileData* data = NULL;
2394   int mdo_offset_bias = 0;
2395   if (should_profile) {
2396     ciMethod* method = op->profiled_method();
2397     assert(method != NULL, "Should have method");
2398     setup_md_access(method, op->profiled_bci(), md, data, mdo_offset_bias);
2399 
2400     Register mdo      = k_RInfo;
2401     Register data_val = Rtmp1;
2402     Label not_null;
2403     __ bne(CCR0, not_null);
2404     metadata2reg(md->constant_encoding(), mdo);
2405     __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2406     __ lbz(data_val, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias, mdo);
2407     __ ori(data_val, data_val, BitData::null_seen_byte_constant());
2408     __ stb(data_val, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias, mdo);
2409     __ b(*obj_is_null);
2410     __ bind(not_null);
2411   } else {
2412     __ beq(CCR0, *obj_is_null);
2413   }
2414 
2415   // get object class
2416   __ load_klass(klass_RInfo, obj);
2417 
2418   if (k->is_loaded()) {
2419     metadata2reg(k->constant_encoding(), k_RInfo);
2420   } else {
2421     klass2reg_with_patching(k_RInfo, op->info_for_patch());
2422   }
2423 
2424   Label profile_cast_failure, failure_restore_obj, profile_cast_success;
2425   Label *failure_target = should_profile ? &profile_cast_failure : failure;
2426   Label *success_target = should_profile ? &profile_cast_success : success;
2427 
2428   if (op->fast_check()) {
2429     assert_different_registers(klass_RInfo, k_RInfo);
2430     __ cmpd(CCR0, k_RInfo, klass_RInfo);
2431     if (should_profile) {
2432       __ bne(CCR0, *failure_target);
2433       // Fall through to success case.
2434     } else {
2435       __ beq(CCR0, *success);
2436       // Fall through to failure case.
2437     }
2438   } else {
2439     bool need_slow_path = true;
2440     if (k->is_loaded()) {
2441       if ((int) k->super_check_offset() != in_bytes(Klass::secondary_super_cache_offset())) {
2442         need_slow_path = false;
2443       }
2444       // Perform the fast part of the checking logic.
2445       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, R0, (need_slow_path ? success_target : NULL),
2446                                        failure_target, NULL, RegisterOrConstant(k->super_check_offset()));
2447     } else {
2448       // Perform the fast part of the checking logic.
2449       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, R0, success_target, failure_target);
2450     }
2451     if (!need_slow_path) {
2452       if (!should_profile) { __ b(*success); }
2453     } else {
2454       // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2455       address entry = Runtime1::entry_for(Runtime1::slow_subtype_check_id);
2456       // Stub needs fixed registers (tmp1-3).
2457       Register original_k_RInfo = op->tmp1()->as_register();
2458       Register original_klass_RInfo = op->tmp2()->as_register();
2459       Register original_Rtmp1 = op->tmp3()->as_register();
2460       bool keep_obj_alive = reg_conflict && (op->code() == lir_checkcast);
2461       bool keep_klass_RInfo_alive = (obj == original_klass_RInfo) && should_profile;
2462       if (keep_obj_alive && (obj != original_Rtmp1)) { __ mr(R0, obj); }
2463       __ mr_if_needed(original_k_RInfo, k_RInfo);
2464       __ mr_if_needed(original_klass_RInfo, klass_RInfo);
2465       if (keep_obj_alive) { __ mr(dst, (obj == original_Rtmp1) ? obj : R0); }
2466       //__ load_const_optimized(original_Rtmp1, entry, R0);
2467       __ calculate_address_from_global_toc(original_Rtmp1, entry, true, true, false);
2468       __ mtctr(original_Rtmp1);
2469       __ bctrl(); // sets CR0
2470       if (keep_obj_alive) {
2471         if (keep_klass_RInfo_alive) { __ mr(R0, obj); }
2472         __ mr(obj, dst);
2473       }
2474       if (should_profile) {
2475         __ bne(CCR0, *failure_target);
2476         if (keep_klass_RInfo_alive) { __ mr(klass_RInfo, keep_obj_alive ? R0 : obj); }
2477         // Fall through to success case.
2478       } else {
2479         __ beq(CCR0, *success);
2480         // Fall through to failure case.
2481       }
2482     }
2483   }
2484 
2485   if (should_profile) {
2486     Register mdo = k_RInfo, recv = klass_RInfo;
2487     assert_different_registers(mdo, recv, Rtmp1);
2488     __ bind(profile_cast_success);
2489     metadata2reg(md->constant_encoding(), mdo);
2490     __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2491     type_profile_helper(mdo, mdo_offset_bias, md, data, recv, Rtmp1, success);
2492     __ b(*success);
2493 
2494     // Cast failure case.
2495     __ bind(profile_cast_failure);
2496     metadata2reg(md->constant_encoding(), mdo);
2497     __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2498     __ ld(Rtmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2499     __ addi(Rtmp1, Rtmp1, -DataLayout::counter_increment);
2500     __ std(Rtmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2501   }
2502 
2503   __ bind(*failure);
2504 }
2505 
2506 
2507 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
2508   LIR_Code code = op->code();
2509   if (code == lir_store_check) {
2510     Register value = op->object()->as_register();
2511     Register array = op->array()->as_register();
2512     Register k_RInfo = op->tmp1()->as_register();
2513     Register klass_RInfo = op->tmp2()->as_register();
2514     Register Rtmp1 = op->tmp3()->as_register();
2515     bool should_profile = op->should_profile();
2516 
2517     __ verify_oop(value, FILE_AND_LINE);
2518     CodeStub* stub = op->stub();
2519     // Check if it needs to be profiled.
2520     ciMethodData* md = NULL;
2521     ciProfileData* data = NULL;
2522     int mdo_offset_bias = 0;
2523     if (should_profile) {
2524       ciMethod* method = op->profiled_method();
2525       assert(method != NULL, "Should have method");
2526       setup_md_access(method, op->profiled_bci(), md, data, mdo_offset_bias);
2527     }
2528     Label profile_cast_success, failure, done;
2529     Label *success_target = should_profile ? &profile_cast_success : &done;
2530 
2531     __ cmpdi(CCR0, value, 0);
2532     if (should_profile) {
2533       Label not_null;
2534       __ bne(CCR0, not_null);
2535       Register mdo      = k_RInfo;
2536       Register data_val = Rtmp1;
2537       metadata2reg(md->constant_encoding(), mdo);
2538       __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2539       __ lbz(data_val, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias, mdo);
2540       __ ori(data_val, data_val, BitData::null_seen_byte_constant());
2541       __ stb(data_val, md->byte_offset_of_slot(data, DataLayout::flags_offset()) - mdo_offset_bias, mdo);
2542       __ b(done);
2543       __ bind(not_null);
2544     } else {
2545       __ beq(CCR0, done);
2546     }
2547     if (!os::zero_page_read_protected() || !ImplicitNullChecks) {
2548       explicit_null_check(array, op->info_for_exception());
2549     } else {
2550       add_debug_info_for_null_check_here(op->info_for_exception());
2551     }
2552     __ load_klass(k_RInfo, array);
2553     __ load_klass(klass_RInfo, value);
2554 
2555     // Get instance klass.
2556     __ ld(k_RInfo, in_bytes(ObjArrayKlass::element_klass_offset()), k_RInfo);
2557     // Perform the fast part of the checking logic.
2558     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, R0, success_target, &failure, NULL);
2559 
2560     // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2561     const address slow_path = Runtime1::entry_for(Runtime1::slow_subtype_check_id);
2562     //__ load_const_optimized(R0, slow_path);
2563     __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(slow_path));
2564     __ mtctr(R0);
2565     __ bctrl(); // sets CR0
2566     if (!should_profile) {
2567       __ beq(CCR0, done);
2568       __ bind(failure);
2569     } else {
2570       __ bne(CCR0, failure);
2571       // Fall through to the success case.
2572 
2573       Register mdo  = klass_RInfo, recv = k_RInfo, tmp1 = Rtmp1;
2574       assert_different_registers(value, mdo, recv, tmp1);
2575       __ bind(profile_cast_success);
2576       metadata2reg(md->constant_encoding(), mdo);
2577       __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2578       __ load_klass(recv, value);
2579       type_profile_helper(mdo, mdo_offset_bias, md, data, recv, tmp1, &done);
2580       __ b(done);
2581 
2582       // Cast failure case.
2583       __ bind(failure);
2584       metadata2reg(md->constant_encoding(), mdo);
2585       __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2586       Address data_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias);
2587       __ ld(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2588       __ addi(tmp1, tmp1, -DataLayout::counter_increment);
2589       __ std(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2590     }
2591     __ b(*stub->entry());
2592     __ bind(done);
2593 
2594   } else if (code == lir_checkcast) {
2595     Label success, failure;
2596     emit_typecheck_helper(op, &success, /*fallthru*/&failure, &success);
2597     __ b(*op->stub()->entry());
2598     __ align(32, 12);
2599     __ bind(success);
2600     __ mr_if_needed(op->result_opr()->as_register(), op->object()->as_register());
2601   } else if (code == lir_instanceof) {
2602     Register dst = op->result_opr()->as_register();
2603     Label success, failure, done;
2604     emit_typecheck_helper(op, &success, /*fallthru*/&failure, &failure);
2605     __ li(dst, 0);
2606     __ b(done);
2607     __ align(32, 12);
2608     __ bind(success);
2609     __ li(dst, 1);
2610     __ bind(done);
2611   } else {
2612     ShouldNotReachHere();
2613   }
2614 }
2615 
2616 
2617 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
2618   Register addr = op->addr()->as_pointer_register();
2619   Register cmp_value = noreg, new_value = noreg;
2620   bool is_64bit = false;
2621 
2622   if (op->code() == lir_cas_long) {
2623     cmp_value = op->cmp_value()->as_register_lo();
2624     new_value = op->new_value()->as_register_lo();
2625     is_64bit = true;
2626   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj) {
2627     cmp_value = op->cmp_value()->as_register();
2628     new_value = op->new_value()->as_register();
2629     if (op->code() == lir_cas_obj) {
2630       if (UseCompressedOops) {
2631         Register t1 = op->tmp1()->as_register();
2632         Register t2 = op->tmp2()->as_register();
2633         cmp_value = __ encode_heap_oop(t1, cmp_value);
2634         new_value = __ encode_heap_oop(t2, new_value);
2635       } else {
2636         is_64bit = true;
2637       }
2638     }
2639   } else {
2640     Unimplemented();
2641   }
2642 
2643   if (is_64bit) {
2644     __ cmpxchgd(BOOL_RESULT, /*current_value=*/R0, cmp_value, new_value, addr,
2645                 MacroAssembler::MemBarNone,
2646                 MacroAssembler::cmpxchgx_hint_atomic_update(),
2647                 noreg, NULL, /*check without ldarx first*/true);
2648   } else {
2649     __ cmpxchgw(BOOL_RESULT, /*current_value=*/R0, cmp_value, new_value, addr,
2650                 MacroAssembler::MemBarNone,
2651                 MacroAssembler::cmpxchgx_hint_atomic_update(),
2652                 noreg, /*check without ldarx first*/true);
2653   }
2654 
2655   if (support_IRIW_for_not_multiple_copy_atomic_cpu) {
2656     __ isync();
2657   } else {
2658     __ sync();
2659   }
2660 }
2661 
2662 void LIR_Assembler::breakpoint() {
2663   __ illtrap();
2664 }
2665 
2666 
2667 void LIR_Assembler::push(LIR_Opr opr) {
2668   Unimplemented();
2669 }
2670 
2671 void LIR_Assembler::pop(LIR_Opr opr) {
2672   Unimplemented();
2673 }
2674 
2675 
2676 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst_opr) {
2677   Address mon_addr = frame_map()->address_for_monitor_lock(monitor_no);
2678   Register dst = dst_opr->as_register();
2679   Register reg = mon_addr.base();
2680   int offset = mon_addr.disp();
2681   // Compute pointer to BasicLock.
2682   __ add_const_optimized(dst, reg, offset);
2683 }
2684 
2685 
2686 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2687   Register obj = op->obj_opr()->as_register();
2688   Register hdr = op->hdr_opr()->as_register();
2689   Register lock = op->lock_opr()->as_register();
2690 
2691   // Obj may not be an oop.
2692   if (op->code() == lir_lock) {
2693     MonitorEnterStub* stub = (MonitorEnterStub*)op->stub();
2694     if (UseFastLocking) {
2695       assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2696       // Add debug info for NullPointerException only if one is possible.
2697       if (op->info() != NULL) {
2698         if (!os::zero_page_read_protected() || !ImplicitNullChecks) {
2699           explicit_null_check(obj, op->info());
2700         } else {
2701           add_debug_info_for_null_check_here(op->info());
2702         }
2703       }
2704       __ lock_object(hdr, obj, lock, op->scratch_opr()->as_register(), *op->stub()->entry());
2705     } else {
2706       // always do slow locking
2707       // note: The slow locking code could be inlined here, however if we use
2708       //       slow locking, speed doesn't matter anyway and this solution is
2709       //       simpler and requires less duplicated code - additionally, the
2710       //       slow locking code is the same in either case which simplifies
2711       //       debugging.
2712       __ b(*op->stub()->entry());
2713     }
2714   } else {
2715     assert (op->code() == lir_unlock, "Invalid code, expected lir_unlock");
2716     if (UseFastLocking) {
2717       assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2718       __ unlock_object(hdr, obj, lock, *op->stub()->entry());
2719     } else {
2720       // always do slow unlocking
2721       // note: The slow unlocking code could be inlined here, however if we use
2722       //       slow unlocking, speed doesn't matter anyway and this solution is
2723       //       simpler and requires less duplicated code - additionally, the
2724       //       slow unlocking code is the same in either case which simplifies
2725       //       debugging.
2726       __ b(*op->stub()->entry());
2727     }
2728   }
2729   __ bind(*op->stub()->continuation());
2730 }
2731 
2732 void LIR_Assembler::emit_load_klass(LIR_OpLoadKlass* op) {
2733   Register obj = op->obj()->as_pointer_register();
2734   Register result = op->result_opr()->as_pointer_register();
2735 
2736   CodeEmitInfo* info = op->info();
2737   if (info != NULL) {
2738     if (!os::zero_page_read_protected() || !ImplicitNullChecks) {
2739       explicit_null_check(obj, info);
2740     } else {
2741       add_debug_info_for_null_check_here(info);
2742     }
2743   }
2744 
2745   if (UseCompressedClassPointers) {
2746     __ lwz(result, oopDesc::klass_offset_in_bytes(), obj);
2747     __ decode_klass_not_null(result);
2748   } else {
2749     __ ld(result, oopDesc::klass_offset_in_bytes(), obj);
2750   }
2751 }
2752 
2753 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2754   ciMethod* method = op->profiled_method();
2755   int bci          = op->profiled_bci();
2756   ciMethod* callee = op->profiled_callee();
2757 
2758   // Update counter for all call types.
2759   ciMethodData* md = method->method_data_or_null();
2760   assert(md != NULL, "Sanity");
2761   ciProfileData* data = md->bci_to_data(bci);
2762   assert(data != NULL && data->is_CounterData(), "need CounterData for calls");
2763   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
2764   Register mdo = op->mdo()->as_register();
2765 #ifdef _LP64
2766   assert(op->tmp1()->is_double_cpu(), "tmp1 must be allocated");
2767   Register tmp1 = op->tmp1()->as_register_lo();
2768 #else
2769   assert(op->tmp1()->is_single_cpu(), "tmp1 must be allocated");
2770   Register tmp1 = op->tmp1()->as_register();
2771 #endif
2772   metadata2reg(md->constant_encoding(), mdo);
2773   int mdo_offset_bias = 0;
2774   if (!Assembler::is_simm16(md->byte_offset_of_slot(data, CounterData::count_offset()) +
2775                             data->size_in_bytes())) {
2776     // The offset is large so bias the mdo by the base of the slot so
2777     // that the ld can use simm16s to reference the slots of the data.
2778     mdo_offset_bias = md->byte_offset_of_slot(data, CounterData::count_offset());
2779     __ add_const_optimized(mdo, mdo, mdo_offset_bias, R0);
2780   }
2781 
2782   // Perform additional virtual call profiling for invokevirtual and
2783   // invokeinterface bytecodes
2784   if (op->should_profile_receiver_type()) {
2785     assert(op->recv()->is_single_cpu(), "recv must be allocated");
2786     Register recv = op->recv()->as_register();
2787     assert_different_registers(mdo, tmp1, recv);
2788     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2789     ciKlass* known_klass = op->known_holder();
2790     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
2791       // We know the type that will be seen at this call site; we can
2792       // statically update the MethodData* rather than needing to do
2793       // dynamic tests on the receiver type.
2794 
2795       // NOTE: we should probably put a lock around this search to
2796       // avoid collisions by concurrent compilations.
2797       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2798       uint i;
2799       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2800         ciKlass* receiver = vc_data->receiver(i);
2801         if (known_klass->equals(receiver)) {
2802           __ ld(tmp1, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2803           __ addi(tmp1, tmp1, DataLayout::counter_increment);
2804           __ std(tmp1, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2805           return;
2806         }
2807       }
2808 
2809       // Receiver type not found in profile data; select an empty slot.
2810 
2811       // Note that this is less efficient than it should be because it
2812       // always does a write to the receiver part of the
2813       // VirtualCallData rather than just the first time.
2814       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2815         ciKlass* receiver = vc_data->receiver(i);
2816         if (receiver == NULL) {
2817           metadata2reg(known_klass->constant_encoding(), tmp1);
2818           __ std(tmp1, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)) - mdo_offset_bias, mdo);
2819 
2820           __ ld(tmp1, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2821           __ addi(tmp1, tmp1, DataLayout::counter_increment);
2822           __ std(tmp1, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)) - mdo_offset_bias, mdo);
2823           return;
2824         }
2825       }
2826     } else {
2827       __ load_klass(recv, recv);
2828       Label update_done;
2829       type_profile_helper(mdo, mdo_offset_bias, md, data, recv, tmp1, &update_done);
2830       // Receiver did not match any saved receiver and there is no empty row for it.
2831       // Increment total counter to indicate polymorphic case.
2832       __ ld(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2833       __ addi(tmp1, tmp1, DataLayout::counter_increment);
2834       __ std(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2835 
2836       __ bind(update_done);
2837     }
2838   } else {
2839     // Static call
2840     __ ld(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2841     __ addi(tmp1, tmp1, DataLayout::counter_increment);
2842     __ std(tmp1, md->byte_offset_of_slot(data, CounterData::count_offset()) - mdo_offset_bias, mdo);
2843   }
2844 }
2845 
2846 
2847 void LIR_Assembler::align_backward_branch_target() {
2848   __ align(32, 12); // Insert up to 3 nops to align with 32 byte boundary.
2849 }
2850 
2851 
2852 void LIR_Assembler::emit_delay(LIR_OpDelay* op) {
2853   Unimplemented();
2854 }
2855 
2856 
2857 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
2858   // tmp must be unused
2859   assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2860   assert(left->is_register(), "can only handle registers");
2861 
2862   if (left->is_single_cpu()) {
2863     __ neg(dest->as_register(), left->as_register());
2864   } else if (left->is_single_fpu()) {
2865     __ fneg(dest->as_float_reg(), left->as_float_reg());
2866   } else if (left->is_double_fpu()) {
2867     __ fneg(dest->as_double_reg(), left->as_double_reg());
2868   } else {
2869     assert (left->is_double_cpu(), "Must be a long");
2870     __ neg(dest->as_register_lo(), left->as_register_lo());
2871   }
2872 }
2873 
2874 
2875 void LIR_Assembler::rt_call(LIR_Opr result, address dest,
2876                             const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
2877   // Stubs: Called via rt_call, but dest is a stub address (no function descriptor).
2878   if (dest == Runtime1::entry_for(Runtime1::register_finalizer_id) ||
2879       dest == Runtime1::entry_for(Runtime1::new_multi_array_id   )) {
2880     //__ load_const_optimized(R0, dest);
2881     __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(dest));
2882     __ mtctr(R0);
2883     __ bctrl();
2884     assert(info != NULL, "sanity");
2885     add_call_info_here(info);
2886     return;
2887   }
2888 
2889   __ call_c_with_frame_resize(dest, /*no resizing*/ 0);
2890   if (info != NULL) {
2891     add_call_info_here(info);
2892   }
2893 }
2894 
2895 
2896 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
2897   ShouldNotReachHere(); // Not needed on _LP64.
2898 }
2899 
2900 void LIR_Assembler::membar() {
2901   __ fence();
2902 }
2903 
2904 void LIR_Assembler::membar_acquire() {
2905   __ acquire();
2906 }
2907 
2908 void LIR_Assembler::membar_release() {
2909   __ release();
2910 }
2911 
2912 void LIR_Assembler::membar_loadload() {
2913   __ membar(Assembler::LoadLoad);
2914 }
2915 
2916 void LIR_Assembler::membar_storestore() {
2917   __ membar(Assembler::StoreStore);
2918 }
2919 
2920 void LIR_Assembler::membar_loadstore() {
2921   __ membar(Assembler::LoadStore);
2922 }
2923 
2924 void LIR_Assembler::membar_storeload() {
2925   __ membar(Assembler::StoreLoad);
2926 }
2927 
2928 void LIR_Assembler::on_spin_wait() {
2929   Unimplemented();
2930 }
2931 
2932 void LIR_Assembler::leal(LIR_Opr addr_opr, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
2933   LIR_Address* addr = addr_opr->as_address_ptr();
2934   assert(addr->scale() == LIR_Address::times_1, "no scaling on this platform");
2935 
2936   if (addr->index()->is_illegal()) {
2937     if (patch_code != lir_patch_none) {
2938       PatchingStub* patch = new PatchingStub(_masm, PatchingStub::access_field_id);
2939       __ load_const32(R0, 0); // patchable int
2940       __ add(dest->as_pointer_register(), addr->base()->as_pointer_register(), R0);
2941       patching_epilog(patch, patch_code, addr->base()->as_register(), info);
2942     } else {
2943       __ add_const_optimized(dest->as_pointer_register(), addr->base()->as_pointer_register(), addr->disp());
2944     }
2945   } else {
2946     assert(patch_code == lir_patch_none, "Patch code not supported");
2947     assert(addr->disp() == 0, "can't have both: index and disp");
2948     __ add(dest->as_pointer_register(), addr->index()->as_pointer_register(), addr->base()->as_pointer_register());
2949   }
2950 }
2951 
2952 
2953 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
2954   ShouldNotReachHere();
2955 }
2956 
2957 
2958 #ifdef ASSERT
2959 // Emit run-time assertion.
2960 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
2961   Unimplemented();
2962 }
2963 #endif
2964 
2965 
2966 void LIR_Assembler::peephole(LIR_List* lir) {
2967   // Optimize instruction pairs before emitting.
2968   LIR_OpList* inst = lir->instructions_list();
2969   for (int i = 1; i < inst->length(); i++) {
2970     LIR_Op* op = inst->at(i);
2971 
2972     // 2 register-register-moves
2973     if (op->code() == lir_move) {
2974       LIR_Opr in2  = ((LIR_Op1*)op)->in_opr(),
2975               res2 = ((LIR_Op1*)op)->result_opr();
2976       if (in2->is_register() && res2->is_register()) {
2977         LIR_Op* prev = inst->at(i - 1);
2978         if (prev && prev->code() == lir_move) {
2979           LIR_Opr in1  = ((LIR_Op1*)prev)->in_opr(),
2980                   res1 = ((LIR_Op1*)prev)->result_opr();
2981           if (in1->is_same_register(res2) && in2->is_same_register(res1)) {
2982             inst->remove_at(i);
2983           }
2984         }
2985       }
2986     }
2987 
2988   }
2989   return;
2990 }
2991 
2992 
2993 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
2994   const LIR_Address *addr = src->as_address_ptr();
2995   assert(addr->disp() == 0 && addr->index()->is_illegal(), "use leal!");
2996   const Register Rptr = addr->base()->as_pointer_register(),
2997                  Rtmp = tmp->as_register();
2998   Register Rco = noreg;
2999   if (UseCompressedOops && data->is_oop()) {
3000     Rco = __ encode_heap_oop(Rtmp, data->as_register());
3001   }
3002 
3003   Label Lretry;
3004   __ bind(Lretry);
3005 
3006   if (data->type() == T_INT) {
3007     const Register Rold = dest->as_register(),
3008                    Rsrc = data->as_register();
3009     assert_different_registers(Rptr, Rtmp, Rold, Rsrc);
3010     __ lwarx(Rold, Rptr, MacroAssembler::cmpxchgx_hint_atomic_update());
3011     if (code == lir_xadd) {
3012       __ add(Rtmp, Rsrc, Rold);
3013       __ stwcx_(Rtmp, Rptr);
3014     } else {
3015       __ stwcx_(Rsrc, Rptr);
3016     }
3017   } else if (data->is_oop()) {
3018     assert(code == lir_xchg, "xadd for oops");
3019     const Register Rold = dest->as_register();
3020     if (UseCompressedOops) {
3021       assert_different_registers(Rptr, Rold, Rco);
3022       __ lwarx(Rold, Rptr, MacroAssembler::cmpxchgx_hint_atomic_update());
3023       __ stwcx_(Rco, Rptr);
3024     } else {
3025       const Register Robj = data->as_register();
3026       assert_different_registers(Rptr, Rold, Robj);
3027       __ ldarx(Rold, Rptr, MacroAssembler::cmpxchgx_hint_atomic_update());
3028       __ stdcx_(Robj, Rptr);
3029     }
3030   } else if (data->type() == T_LONG) {
3031     const Register Rold = dest->as_register_lo(),
3032                    Rsrc = data->as_register_lo();
3033     assert_different_registers(Rptr, Rtmp, Rold, Rsrc);
3034     __ ldarx(Rold, Rptr, MacroAssembler::cmpxchgx_hint_atomic_update());
3035     if (code == lir_xadd) {
3036       __ add(Rtmp, Rsrc, Rold);
3037       __ stdcx_(Rtmp, Rptr);
3038     } else {
3039       __ stdcx_(Rsrc, Rptr);
3040     }
3041   } else {
3042     ShouldNotReachHere();
3043   }
3044 
3045   if (UseStaticBranchPredictionInCompareAndSwapPPC64) {
3046     __ bne_predict_not_taken(CCR0, Lretry);
3047   } else {
3048     __ bne(                  CCR0, Lretry);
3049   }
3050 
3051   if (UseCompressedOops && data->is_oop()) {
3052     __ decode_heap_oop(dest->as_register());
3053   }
3054 }
3055 
3056 
3057 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
3058   Register obj = op->obj()->as_register();
3059   Register tmp = op->tmp()->as_pointer_register();
3060   LIR_Address* mdo_addr = op->mdp()->as_address_ptr();
3061   ciKlass* exact_klass = op->exact_klass();
3062   intptr_t current_klass = op->current_klass();
3063   bool not_null = op->not_null();
3064   bool no_conflict = op->no_conflict();
3065 
3066   Label Lupdate, Ldo_update, Ldone;
3067 
3068   bool do_null = !not_null;
3069   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
3070   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
3071 
3072   assert(do_null || do_update, "why are we here?");
3073   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
3074 
3075   __ verify_oop(obj, FILE_AND_LINE);
3076 
3077   if (do_null) {
3078     if (!TypeEntries::was_null_seen(current_klass)) {
3079       __ cmpdi(CCR0, obj, 0);
3080       __ bne(CCR0, Lupdate);
3081       __ ld(R0, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3082       __ ori(R0, R0, TypeEntries::null_seen);
3083       if (do_update) {
3084         __ b(Ldo_update);
3085       } else {
3086         __ std(R0, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3087       }
3088     } else {
3089       if (do_update) {
3090         __ cmpdi(CCR0, obj, 0);
3091         __ beq(CCR0, Ldone);
3092       }
3093     }
3094 #ifdef ASSERT
3095   } else {
3096     __ cmpdi(CCR0, obj, 0);
3097     __ bne(CCR0, Lupdate);
3098     __ stop("unexpect null obj");
3099 #endif
3100   }
3101 
3102   __ bind(Lupdate);
3103   if (do_update) {
3104     Label Lnext;
3105     const Register klass = R29_TOC; // kill and reload
3106     bool klass_reg_used = false;
3107 #ifdef ASSERT
3108     if (exact_klass != NULL) {
3109       Label ok;
3110       klass_reg_used = true;
3111       __ load_klass(klass, obj);
3112       metadata2reg(exact_klass->constant_encoding(), R0);
3113       __ cmpd(CCR0, klass, R0);
3114       __ beq(CCR0, ok);
3115       __ stop("exact klass and actual klass differ");
3116       __ bind(ok);
3117     }
3118 #endif
3119 
3120     if (!no_conflict) {
3121       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
3122         klass_reg_used = true;
3123         if (exact_klass != NULL) {
3124           __ ld(tmp, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3125           metadata2reg(exact_klass->constant_encoding(), klass);
3126         } else {
3127           __ load_klass(klass, obj);
3128           __ ld(tmp, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register()); // may kill obj
3129         }
3130 
3131         // Like InterpreterMacroAssembler::profile_obj_type
3132         __ clrrdi(R0, tmp, exact_log2(-TypeEntries::type_klass_mask));
3133         // Basically same as andi(R0, tmp, TypeEntries::type_klass_mask);
3134         __ cmpd(CCR1, R0, klass);
3135         // Klass seen before, nothing to do (regardless of unknown bit).
3136         //beq(CCR1, do_nothing);
3137 
3138         __ andi_(R0, klass, TypeEntries::type_unknown);
3139         // Already unknown. Nothing to do anymore.
3140         //bne(CCR0, do_nothing);
3141         __ crorc(CCR0, Assembler::equal, CCR1, Assembler::equal); // cr0 eq = cr1 eq or cr0 ne
3142         __ beq(CCR0, Lnext);
3143 
3144         if (TypeEntries::is_type_none(current_klass)) {
3145           __ clrrdi_(R0, tmp, exact_log2(-TypeEntries::type_mask));
3146           __ orr(R0, klass, tmp); // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
3147           __ beq(CCR0, Ldo_update); // First time here. Set profile type.
3148         }
3149 
3150       } else {
3151         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3152                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
3153 
3154         __ ld(tmp, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3155         __ andi_(R0, tmp, TypeEntries::type_unknown);
3156         // Already unknown. Nothing to do anymore.
3157         __ bne(CCR0, Lnext);
3158       }
3159 
3160       // Different than before. Cannot keep accurate profile.
3161       __ ori(R0, tmp, TypeEntries::type_unknown);
3162     } else {
3163       // There's a single possible klass at this profile point
3164       assert(exact_klass != NULL, "should be");
3165       __ ld(tmp, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3166 
3167       if (TypeEntries::is_type_none(current_klass)) {
3168         klass_reg_used = true;
3169         metadata2reg(exact_klass->constant_encoding(), klass);
3170 
3171         __ clrrdi(R0, tmp, exact_log2(-TypeEntries::type_klass_mask));
3172         // Basically same as andi(R0, tmp, TypeEntries::type_klass_mask);
3173         __ cmpd(CCR1, R0, klass);
3174         // Klass seen before, nothing to do (regardless of unknown bit).
3175         __ beq(CCR1, Lnext);
3176 #ifdef ASSERT
3177         {
3178           Label ok;
3179           __ clrrdi_(R0, tmp, exact_log2(-TypeEntries::type_mask));
3180           __ beq(CCR0, ok); // First time here.
3181 
3182           __ stop("unexpected profiling mismatch");
3183           __ bind(ok);
3184         }
3185 #endif
3186         // First time here. Set profile type.
3187         __ orr(R0, klass, tmp); // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
3188       } else {
3189         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3190                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3191 
3192         // Already unknown. Nothing to do anymore.
3193         __ andi_(R0, tmp, TypeEntries::type_unknown);
3194         __ bne(CCR0, Lnext);
3195 
3196         // Different than before. Cannot keep accurate profile.
3197         __ ori(R0, tmp, TypeEntries::type_unknown);
3198       }
3199     }
3200 
3201     __ bind(Ldo_update);
3202     __ std(R0, index_or_disp(mdo_addr), mdo_addr->base()->as_pointer_register());
3203 
3204     __ bind(Lnext);
3205     if (klass_reg_used) { __ load_const_optimized(R29_TOC, MacroAssembler::global_toc(), R0); } // reinit
3206   }
3207   __ bind(Ldone);
3208 }
3209 
3210 
3211 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
3212   assert(op->crc()->is_single_cpu(), "crc must be register");
3213   assert(op->val()->is_single_cpu(), "byte value must be register");
3214   assert(op->result_opr()->is_single_cpu(), "result must be register");
3215   Register crc = op->crc()->as_register();
3216   Register val = op->val()->as_register();
3217   Register res = op->result_opr()->as_register();
3218 
3219   assert_different_registers(val, crc, res);
3220 
3221   __ load_const_optimized(res, StubRoutines::crc_table_addr(), R0);
3222   __ kernel_crc32_singleByteReg(crc, val, res, true);
3223   __ mr(res, crc);
3224 }
3225 
3226 #undef __