1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2017, 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 "c1/c1_Compilation.hpp"
  28 #include "c1/c1_LIRAssembler.hpp"
  29 #include "c1/c1_MacroAssembler.hpp"
  30 #include "c1/c1_Runtime1.hpp"
  31 #include "c1/c1_ValueStack.hpp"
  32 #include "ci/ciArrayKlass.hpp"
  33 #include "ci/ciInstance.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "gc/shared/barrierSet.hpp"
  36 #include "gc/shared/cardTableBarrierSet.hpp"
  37 #include "nativeInst_s390.hpp"
  38 #include "oops/objArrayKlass.hpp"
  39 #include "runtime/frame.inline.hpp"
  40 #include "runtime/safepointMechanism.inline.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "vmreg_s390.inline.hpp"
  43 
  44 #define __ _masm->
  45 
  46 #ifndef PRODUCT
  47 #undef __
  48 #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm) : _masm)->
  49 #endif
  50 
  51 //------------------------------------------------------------
  52 
  53 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
  54   // Not used on ZARCH_64
  55   ShouldNotCallThis();
  56   return false;
  57 }
  58 
  59 LIR_Opr LIR_Assembler::receiverOpr() {
  60   return FrameMap::Z_R2_oop_opr;
  61 }
  62 
  63 LIR_Opr LIR_Assembler::osrBufferPointer() {
  64   return FrameMap::Z_R2_opr;
  65 }
  66 
  67 int LIR_Assembler::initial_frame_size_in_bytes() const {
  68   return in_bytes(frame_map()->framesize_in_bytes());
  69 }
  70 
  71 // Inline cache check: done before the frame is built.
  72 // The inline cached class is in Z_inline_cache(Z_R9).
  73 // We fetch the class of the receiver and compare it with the cached class.
  74 // If they do not match we jump to the slow case.
  75 int LIR_Assembler::check_icache() {
  76   Register receiver = receiverOpr()->as_register();
  77   int offset = __ offset();
  78   __ inline_cache_check(receiver, Z_inline_cache);
  79   return offset;
  80 }
  81 
  82 void LIR_Assembler::osr_entry() {
  83   // On-stack-replacement entry sequence (interpreter frame layout described in interpreter_sparc.cpp):
  84   //
  85   //   1. Create a new compiled activation.
  86   //   2. Initialize local variables in the compiled activation. The expression stack must be empty
  87   //      at the osr_bci; it is not initialized.
  88   //   3. Jump to the continuation address in compiled code to resume execution.
  89 
  90   // OSR entry point
  91   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
  92   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
  93   ValueStack* entry_state = osr_entry->end()->state();
  94   int number_of_locks = entry_state->locks_size();
  95 
  96   // Create a frame for the compiled activation.
  97   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
  98 
  99   // OSR buffer is
 100   //
 101   // locals[nlocals-1..0]
 102   // monitors[number_of_locks-1..0]
 103   //
 104   // Locals is a direct copy of the interpreter frame so in the osr buffer
 105   // the first slot in the local array is the last local from the interpreter
 106   // and the last slot is local[0] (receiver) from the interpreter
 107   //
 108   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 109   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 110   // in the interpreter frame (the method lock if a sync method)
 111 
 112   // Initialize monitors in the compiled activation.
 113   //   I0: pointer to osr buffer
 114   //
 115   // All other registers are dead at this point and the locals will be
 116   // copied into place by code emitted in the IR.
 117 
 118   Register OSR_buf = osrBufferPointer()->as_register();
 119   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 120     int monitor_offset = BytesPerWord * method()->max_locals() +
 121       (2 * BytesPerWord) * (number_of_locks - 1);
 122     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 123     // the OSR buffer using 2 word entries: first the lock and then
 124     // the oop.
 125     for (int i = 0; i < number_of_locks; i++) {
 126       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 127       // Verify the interpreter's monitor has a non-null object.
 128       __ asm_assert_mem8_isnot_zero(slot_offset + 1*BytesPerWord, OSR_buf, "locked object is NULL", __LINE__);
 129       // Copy the lock field into the compiled activation.
 130       __ z_lg(Z_R1_scratch, slot_offset + 0, OSR_buf);
 131       __ z_stg(Z_R1_scratch, frame_map()->address_for_monitor_lock(i));
 132       __ z_lg(Z_R1_scratch, slot_offset + 1*BytesPerWord, OSR_buf);
 133       __ z_stg(Z_R1_scratch, frame_map()->address_for_monitor_object(i));
 134     }
 135   }
 136 }
 137 
 138 // --------------------------------------------------------------------------------------------
 139 
 140 address LIR_Assembler::emit_call_c(address a) {
 141   __ align_call_far_patchable(__ pc());
 142   address call_addr = __ call_c_opt(a);
 143   if (call_addr == NULL) {
 144     bailout("const section overflow");
 145   }
 146   return call_addr;
 147 }
 148 
 149 int LIR_Assembler::emit_exception_handler() {
 150   // If the last instruction is a call (typically to do a throw which
 151   // is coming at the end after block reordering) the return address
 152   // must still point into the code area in order to avoid assertion
 153   // failures when searching for the corresponding bci. => Add a nop.
 154   // (was bug 5/14/1999 - gri)
 155   __ nop();
 156 
 157   // Generate code for exception handler.
 158   address handler_base = __ start_a_stub(exception_handler_size());
 159   if (handler_base == NULL) {
 160     // Not enough space left for the handler.
 161     bailout("exception handler overflow");
 162     return -1;
 163   }
 164 
 165   int offset = code_offset();
 166 
 167   address a = Runtime1::entry_for (Runtime1::handle_exception_from_callee_id);
 168   address call_addr = emit_call_c(a);
 169   CHECK_BAILOUT_(-1);
 170   __ should_not_reach_here();
 171   guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
 172   __ end_a_stub();
 173 
 174   return offset;
 175 }
 176 
 177 // Emit the code to remove the frame from the stack in the exception
 178 // unwind path.
 179 int LIR_Assembler::emit_unwind_handler() {
 180 #ifndef PRODUCT
 181   if (CommentedAssembly) {
 182     _masm->block_comment("Unwind handler");
 183   }
 184 #endif
 185 
 186   int offset = code_offset();
 187   Register exception_oop_callee_saved = Z_R10; // Z_R10 is callee-saved.
 188   Register Rtmp1                      = Z_R11;
 189   Register Rtmp2                      = Z_R12;
 190 
 191   // Fetch the exception from TLS and clear out exception related thread state.
 192   Address exc_oop_addr = Address(Z_thread, JavaThread::exception_oop_offset());
 193   Address exc_pc_addr  = Address(Z_thread, JavaThread::exception_pc_offset());
 194   __ z_lg(Z_EXC_OOP, exc_oop_addr);
 195   __ clear_mem(exc_oop_addr, sizeof(oop));
 196   __ clear_mem(exc_pc_addr, sizeof(intptr_t));
 197 
 198   __ bind(_unwind_handler_entry);
 199   __ verify_not_null_oop(Z_EXC_OOP);
 200   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 201     __ lgr_if_needed(exception_oop_callee_saved, Z_EXC_OOP); // Preserve the exception.
 202   }
 203 
 204   // Preform needed unlocking.
 205   MonitorExitStub* stub = NULL;
 206   if (method()->is_synchronized()) {
 207     // Runtime1::monitorexit_id expects lock address in Z_R1_scratch.
 208     LIR_Opr lock = FrameMap::as_opr(Z_R1_scratch);
 209     monitor_address(0, lock);
 210     stub = new MonitorExitStub(lock, true, 0);
 211     __ unlock_object(Rtmp1, Rtmp2, lock->as_register(), *stub->entry());
 212     __ bind(*stub->continuation());
 213   }
 214 
 215   if (compilation()->env()->dtrace_method_probes()) {
 216     ShouldNotReachHere(); // Not supported.
 217 #if 0
 218     __ mov(rdi, r15_thread);
 219     __ mov_metadata(rsi, method()->constant_encoding());
 220     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit)));
 221 #endif
 222   }
 223 
 224   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 225     __ lgr_if_needed(Z_EXC_OOP, exception_oop_callee_saved);  // Restore the exception.
 226   }
 227 
 228   // Remove the activation and dispatch to the unwind handler.
 229   __ pop_frame();
 230   __ z_lg(Z_EXC_PC, _z_abi16(return_pc), Z_SP);
 231 
 232   // Z_EXC_OOP: exception oop
 233   // Z_EXC_PC: exception pc
 234 
 235   // Dispatch to the unwind logic.
 236   __ load_const_optimized(Z_R5, Runtime1::entry_for (Runtime1::unwind_exception_id));
 237   __ z_br(Z_R5);
 238 
 239   // Emit the slow path assembly.
 240   if (stub != NULL) {
 241     stub->emit_code(this);
 242   }
 243 
 244   return offset;
 245 }
 246 
 247 int LIR_Assembler::emit_deopt_handler() {
 248   // If the last instruction is a call (typically to do a throw which
 249   // is coming at the end after block reordering) the return address
 250   // must still point into the code area in order to avoid assertion
 251   // failures when searching for the corresponding bci. => Add a nop.
 252   // (was bug 5/14/1999 - gri)
 253   __ nop();
 254 
 255   // Generate code for exception handler.
 256   address handler_base = __ start_a_stub(deopt_handler_size());
 257   if (handler_base == NULL) {
 258     // Not enough space left for the handler.
 259     bailout("deopt handler overflow");
 260     return -1;
 261   }  int offset = code_offset();
 262   // Size must be constant (see HandlerImpl::emit_deopt_handler).
 263   __ load_const(Z_R1_scratch, SharedRuntime::deopt_blob()->unpack());
 264   __ call(Z_R1_scratch);
 265   guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
 266   __ end_a_stub();
 267 
 268   return offset;
 269 }
 270 
 271 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
 272   if (o == NULL) {
 273     __ clear_reg(reg, true/*64bit*/, false/*set cc*/); // Must not kill cc set by cmove.
 274   } else {
 275     AddressLiteral a = __ allocate_oop_address(o);
 276     bool success = __ load_oop_from_toc(reg, a, reg);
 277     if (!success) {
 278       bailout("const section overflow");
 279     }
 280   }
 281 }
 282 
 283 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
 284   // Allocate a new index in table to hold the object once it's been patched.
 285   int oop_index = __ oop_recorder()->allocate_oop_index(NULL);
 286   PatchingStub* patch = new PatchingStub(_masm, patching_id(info), oop_index);
 287 
 288   AddressLiteral addrlit((intptr_t)0, oop_Relocation::spec(oop_index));
 289   assert(addrlit.rspec().type() == relocInfo::oop_type, "must be an oop reloc");
 290   // The NULL will be dynamically patched later so the sequence to
 291   // load the address literal must not be optimized.
 292   __ load_const(reg, addrlit);
 293 
 294   patching_epilog(patch, lir_patch_normal, reg, info);
 295 }
 296 
 297 void LIR_Assembler::metadata2reg(Metadata* md, Register reg) {
 298   bool success = __ set_metadata_constant(md, reg);
 299   if (!success) {
 300     bailout("const section overflow");
 301     return;
 302   }
 303 }
 304 
 305 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo *info) {
 306   // Allocate a new index in table to hold the klass once it's been patched.
 307   int index = __ oop_recorder()->allocate_metadata_index(NULL);
 308   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id, index);
 309   AddressLiteral addrlit((intptr_t)0, metadata_Relocation::spec(index));
 310   assert(addrlit.rspec().type() == relocInfo::metadata_type, "must be an metadata reloc");
 311   // The NULL will be dynamically patched later so the sequence to
 312   // load the address literal must not be optimized.
 313   __ load_const(reg, addrlit);
 314 
 315   patching_epilog(patch, lir_patch_normal, reg, info);
 316 }
 317 
 318 void LIR_Assembler::emit_op3(LIR_Op3* op) {
 319   switch (op->code()) {
 320     case lir_idiv:
 321     case lir_irem:
 322       arithmetic_idiv(op->code(),
 323                       op->in_opr1(),
 324                       op->in_opr2(),
 325                       op->in_opr3(),
 326                       op->result_opr(),
 327                       op->info());
 328       break;
 329     case lir_fmad: {
 330       const FloatRegister opr1 = op->in_opr1()->as_double_reg(),
 331                           opr2 = op->in_opr2()->as_double_reg(),
 332                           opr3 = op->in_opr3()->as_double_reg(),
 333                           res  = op->result_opr()->as_double_reg();
 334       __ z_madbr(opr3, opr1, opr2);
 335       if (res != opr3) { __ z_ldr(res, opr3); }
 336     } break;
 337     case lir_fmaf: {
 338       const FloatRegister opr1 = op->in_opr1()->as_float_reg(),
 339                           opr2 = op->in_opr2()->as_float_reg(),
 340                           opr3 = op->in_opr3()->as_float_reg(),
 341                           res  = op->result_opr()->as_float_reg();
 342       __ z_maebr(opr3, opr1, opr2);
 343       if (res != opr3) { __ z_ler(res, opr3); }
 344     } break;
 345     default: ShouldNotReachHere(); break;
 346   }
 347 }
 348 
 349 
 350 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
 351 #ifdef ASSERT
 352   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
 353   if (op->block() != NULL)  { _branch_target_blocks.append(op->block()); }
 354   if (op->ublock() != NULL) { _branch_target_blocks.append(op->ublock()); }
 355 #endif
 356 
 357   if (op->cond() == lir_cond_always) {
 358     if (op->info() != NULL) { add_debug_info_for_branch(op->info()); }
 359     __ branch_optimized(Assembler::bcondAlways, *(op->label()));
 360   } else {
 361     Assembler::branch_condition acond = Assembler::bcondZero;
 362     if (op->code() == lir_cond_float_branch) {
 363       assert(op->ublock() != NULL, "must have unordered successor");
 364       __ branch_optimized(Assembler::bcondNotOrdered, *(op->ublock()->label()));
 365     }
 366     switch (op->cond()) {
 367       case lir_cond_equal:        acond = Assembler::bcondEqual;     break;
 368       case lir_cond_notEqual:     acond = Assembler::bcondNotEqual;  break;
 369       case lir_cond_less:         acond = Assembler::bcondLow;       break;
 370       case lir_cond_lessEqual:    acond = Assembler::bcondNotHigh;   break;
 371       case lir_cond_greaterEqual: acond = Assembler::bcondNotLow;    break;
 372       case lir_cond_greater:      acond = Assembler::bcondHigh;      break;
 373       case lir_cond_belowEqual:   acond = Assembler::bcondNotHigh;   break;
 374       case lir_cond_aboveEqual:   acond = Assembler::bcondNotLow;    break;
 375       default:                         ShouldNotReachHere();
 376     }
 377     __ branch_optimized(acond,*(op->label()));
 378   }
 379 }
 380 
 381 
 382 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
 383   LIR_Opr src  = op->in_opr();
 384   LIR_Opr dest = op->result_opr();
 385 
 386   switch (op->bytecode()) {
 387     case Bytecodes::_i2l:
 388       __ move_reg_if_needed(dest->as_register_lo(), T_LONG, src->as_register(), T_INT);
 389       break;
 390 
 391     case Bytecodes::_l2i:
 392       __ move_reg_if_needed(dest->as_register(), T_INT, src->as_register_lo(), T_LONG);
 393       break;
 394 
 395     case Bytecodes::_i2b:
 396       __ move_reg_if_needed(dest->as_register(), T_BYTE, src->as_register(), T_INT);
 397       break;
 398 
 399     case Bytecodes::_i2c:
 400       __ move_reg_if_needed(dest->as_register(), T_CHAR, src->as_register(), T_INT);
 401       break;
 402 
 403     case Bytecodes::_i2s:
 404       __ move_reg_if_needed(dest->as_register(), T_SHORT, src->as_register(), T_INT);
 405       break;
 406 
 407     case Bytecodes::_f2d:
 408       assert(dest->is_double_fpu(), "check");
 409       __ move_freg_if_needed(dest->as_double_reg(), T_DOUBLE, src->as_float_reg(), T_FLOAT);
 410       break;
 411 
 412     case Bytecodes::_d2f:
 413       assert(dest->is_single_fpu(), "check");
 414       __ move_freg_if_needed(dest->as_float_reg(), T_FLOAT, src->as_double_reg(), T_DOUBLE);
 415       break;
 416 
 417     case Bytecodes::_i2f:
 418       __ z_cefbr(dest->as_float_reg(), src->as_register());
 419       break;
 420 
 421     case Bytecodes::_i2d:
 422       __ z_cdfbr(dest->as_double_reg(), src->as_register());
 423       break;
 424 
 425     case Bytecodes::_l2f:
 426       __ z_cegbr(dest->as_float_reg(), src->as_register_lo());
 427       break;
 428     case Bytecodes::_l2d:
 429       __ z_cdgbr(dest->as_double_reg(), src->as_register_lo());
 430       break;
 431 
 432     case Bytecodes::_f2i:
 433     case Bytecodes::_f2l: {
 434       Label done;
 435       FloatRegister Rsrc = src->as_float_reg();
 436       Register Rdst = (op->bytecode() == Bytecodes::_f2i ? dest->as_register() : dest->as_register_lo());
 437       __ clear_reg(Rdst, true, false);
 438       __ z_cebr(Rsrc, Rsrc);
 439       __ z_brno(done); // NaN -> 0
 440       if (op->bytecode() == Bytecodes::_f2i) {
 441         __ z_cfebr(Rdst, Rsrc, Assembler::to_zero);
 442       } else { // op->bytecode() == Bytecodes::_f2l
 443         __ z_cgebr(Rdst, Rsrc, Assembler::to_zero);
 444       }
 445       __ bind(done);
 446     }
 447     break;
 448 
 449     case Bytecodes::_d2i:
 450     case Bytecodes::_d2l: {
 451       Label done;
 452       FloatRegister Rsrc = src->as_double_reg();
 453       Register Rdst = (op->bytecode() == Bytecodes::_d2i ? dest->as_register() : dest->as_register_lo());
 454       __ clear_reg(Rdst, true, false);  // Don't set CC.
 455       __ z_cdbr(Rsrc, Rsrc);
 456       __ z_brno(done); // NaN -> 0
 457       if (op->bytecode() == Bytecodes::_d2i) {
 458         __ z_cfdbr(Rdst, Rsrc, Assembler::to_zero);
 459       } else { // Bytecodes::_d2l
 460         __ z_cgdbr(Rdst, Rsrc, Assembler::to_zero);
 461       }
 462       __ bind(done);
 463     }
 464     break;
 465 
 466     default: ShouldNotReachHere();
 467   }
 468 }
 469 
 470 void LIR_Assembler::align_call(LIR_Code code) {
 471   // End of call instruction must be 4 byte aligned.
 472   int offset = __ offset();
 473   switch (code) {
 474     case lir_icvirtual_call:
 475       offset += MacroAssembler::load_const_from_toc_size();
 476       // no break
 477     case lir_static_call:
 478     case lir_optvirtual_call:
 479     case lir_dynamic_call:
 480       offset += NativeCall::call_far_pcrelative_displacement_offset;
 481       break;
 482     case lir_virtual_call:   // currently, sparc-specific for niagara
 483     default: ShouldNotReachHere();
 484   }
 485   if ((offset & (NativeCall::call_far_pcrelative_displacement_alignment-1)) != 0) {
 486     __ nop();
 487   }
 488 }
 489 
 490 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
 491   assert((__ offset() + NativeCall::call_far_pcrelative_displacement_offset) % NativeCall::call_far_pcrelative_displacement_alignment == 0,
 492          "must be aligned (offset=%d)", __ offset());
 493   assert(rtype == relocInfo::none ||
 494          rtype == relocInfo::opt_virtual_call_type ||
 495          rtype == relocInfo::static_call_type, "unexpected rtype");
 496   // Prepend each BRASL with a nop.
 497   __ relocate(rtype);
 498   __ z_nop();
 499   __ z_brasl(Z_R14, op->addr());
 500   add_call_info(code_offset(), op->info());
 501 }
 502 
 503 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
 504   address virtual_call_oop_addr = NULL;
 505   AddressLiteral empty_ic((address) Universe::non_oop_word());
 506   virtual_call_oop_addr = __ pc();
 507   bool success = __ load_const_from_toc(Z_inline_cache, empty_ic);
 508   if (!success) {
 509     bailout("const section overflow");
 510     return;
 511   }
 512 
 513   // CALL to fixup routine. Fixup routine uses ScopeDesc info
 514   // to determine who we intended to call.
 515   __ relocate(virtual_call_Relocation::spec(virtual_call_oop_addr));
 516   call(op, relocInfo::none);
 517 }
 518 
 519 // not supported
 520 void LIR_Assembler::vtable_call(LIR_OpJavaCall* op) {
 521   ShouldNotReachHere();
 522 }
 523 
 524 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
 525   if (from_reg != to_reg) __ z_lgr(to_reg, from_reg);
 526 }
 527 
 528 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 529   assert(src->is_constant(), "should not call otherwise");
 530   assert(dest->is_stack(), "should not call otherwise");
 531   LIR_Const* c = src->as_constant_ptr();
 532 
 533   unsigned int lmem = 0;
 534   unsigned int lcon = 0;
 535   int64_t cbits = 0;
 536   Address dest_addr;
 537   switch (c->type()) {
 538     case T_INT:  // fall through
 539     case T_FLOAT:
 540       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 541       lmem = 4; lcon = 4; cbits = c->as_jint_bits();
 542       break;
 543 
 544     case T_ADDRESS:
 545       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 546       lmem = 8; lcon = 4; cbits = c->as_jint_bits();
 547       break;
 548 
 549     case T_OBJECT:
 550       dest_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 551       if (c->as_jobject() == NULL) {
 552         __ store_const(dest_addr, (int64_t)NULL_WORD, 8, 8);
 553       } else {
 554         jobject2reg(c->as_jobject(), Z_R1_scratch);
 555         __ reg2mem_opt(Z_R1_scratch, dest_addr, true);
 556       }
 557       return;
 558 
 559     case T_LONG:  // fall through
 560     case T_DOUBLE:
 561       dest_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 562       lmem = 8; lcon = 8; cbits = (int64_t)(c->as_jlong_bits());
 563       break;
 564 
 565     default:
 566       ShouldNotReachHere();
 567   }
 568 
 569   __ store_const(dest_addr, cbits, lmem, lcon);
 570 }
 571 
 572 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 573   assert(src->is_constant(), "should not call otherwise");
 574   assert(dest->is_address(), "should not call otherwise");
 575   // See special case in LIRGenerator::do_StoreIndexed.
 576   // T_BYTE: Special case for card mark store.
 577   assert(type == T_BYTE || !dest->as_address_ptr()->index()->is_valid(), "not supported");
 578   LIR_Const* c = src->as_constant_ptr();
 579   Address addr = as_Address(dest->as_address_ptr());
 580 
 581   int store_offset = -1;
 582   unsigned int lmem = 0;
 583   unsigned int lcon = 0;
 584   int64_t cbits = 0;
 585   switch (type) {
 586     case T_INT:    // fall through
 587     case T_FLOAT:
 588       lmem = 4; lcon = 4; cbits = c->as_jint_bits();
 589       break;
 590 
 591     case T_ADDRESS:
 592       lmem = 8; lcon = 4; cbits = c->as_jint_bits();
 593       break;
 594 
 595     case T_OBJECT:  // fall through
 596     case T_ARRAY:
 597       if (c->as_jobject() == NULL) {
 598         if (UseCompressedOops && !wide) {
 599           store_offset = __ store_const(addr, (int32_t)NULL_WORD, 4, 4);
 600         } else {
 601           store_offset = __ store_const(addr, (int64_t)NULL_WORD, 8, 8);
 602         }
 603       } else {
 604         jobject2reg(c->as_jobject(), Z_R1_scratch);
 605         if (UseCompressedOops && !wide) {
 606           __ encode_heap_oop(Z_R1_scratch);
 607           store_offset = __ reg2mem_opt(Z_R1_scratch, addr, false);
 608         } else {
 609           store_offset = __ reg2mem_opt(Z_R1_scratch, addr, true);
 610         }
 611       }
 612       assert(store_offset >= 0, "check");
 613       break;
 614 
 615     case T_LONG:    // fall through
 616     case T_DOUBLE:
 617       lmem = 8; lcon = 8; cbits = (int64_t)(c->as_jlong_bits());
 618       break;
 619 
 620     case T_BOOLEAN: // fall through
 621     case T_BYTE:
 622       lmem = 1; lcon = 1; cbits = (int8_t)(c->as_jint());
 623       break;
 624 
 625     case T_CHAR:    // fall through
 626     case T_SHORT:
 627       lmem = 2; lcon = 2; cbits = (int16_t)(c->as_jint());
 628       break;
 629 
 630     default:
 631       ShouldNotReachHere();
 632   };
 633 
 634   // Index register is normally not supported, but for
 635   // LIRGenerator::CardTableBarrierSet_post_barrier we make an exception.
 636   if (type == T_BYTE && dest->as_address_ptr()->index()->is_valid()) {
 637     __ load_const_optimized(Z_R0_scratch, (int8_t)(c->as_jint()));
 638     store_offset = __ offset();
 639     if (Immediate::is_uimm12(addr.disp())) {
 640       __ z_stc(Z_R0_scratch, addr);
 641     } else {
 642       __ z_stcy(Z_R0_scratch, addr);
 643     }
 644   }
 645 
 646   if (store_offset == -1) {
 647     store_offset = __ store_const(addr, cbits, lmem, lcon);
 648     assert(store_offset >= 0, "check");
 649   }
 650 
 651   if (info != NULL) {
 652     add_debug_info_for_null_check(store_offset, info);
 653   }
 654 }
 655 
 656 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
 657   assert(src->is_constant(), "should not call otherwise");
 658   assert(dest->is_register(), "should not call otherwise");
 659   LIR_Const* c = src->as_constant_ptr();
 660 
 661   switch (c->type()) {
 662     case T_INT: {
 663       assert(patch_code == lir_patch_none, "no patching handled here");
 664       __ load_const_optimized(dest->as_register(), c->as_jint());
 665       break;
 666     }
 667 
 668     case T_ADDRESS: {
 669       assert(patch_code == lir_patch_none, "no patching handled here");
 670       __ load_const_optimized(dest->as_register(), c->as_jint());
 671       break;
 672     }
 673 
 674     case T_LONG: {
 675       assert(patch_code == lir_patch_none, "no patching handled here");
 676       __ load_const_optimized(dest->as_register_lo(), (intptr_t)c->as_jlong());
 677       break;
 678     }
 679 
 680     case T_OBJECT: {
 681       if (patch_code != lir_patch_none) {
 682         jobject2reg_with_patching(dest->as_register(), info);
 683       } else {
 684         jobject2reg(c->as_jobject(), dest->as_register());
 685       }
 686       break;
 687     }
 688 
 689     case T_METADATA: {
 690       if (patch_code != lir_patch_none) {
 691         klass2reg_with_patching(dest->as_register(), info);
 692       } else {
 693         metadata2reg(c->as_metadata(), dest->as_register());
 694       }
 695       break;
 696     }
 697 
 698     case T_FLOAT: {
 699       Register toc_reg = Z_R1_scratch;
 700       __ load_toc(toc_reg);
 701       address const_addr = __ float_constant(c->as_jfloat());
 702       if (const_addr == NULL) {
 703         bailout("const section overflow");
 704         break;
 705       }
 706       int displ = const_addr - _masm->code()->consts()->start();
 707       if (dest->is_single_fpu()) {
 708         __ z_ley(dest->as_float_reg(), displ, toc_reg);
 709       } else {
 710         assert(dest->is_single_cpu(), "Must be a cpu register.");
 711         __ z_ly(dest->as_register(), displ, toc_reg);
 712       }
 713     }
 714     break;
 715 
 716     case T_DOUBLE: {
 717       Register toc_reg = Z_R1_scratch;
 718       __ load_toc(toc_reg);
 719       address const_addr = __ double_constant(c->as_jdouble());
 720       if (const_addr == NULL) {
 721         bailout("const section overflow");
 722         break;
 723       }
 724       int displ = const_addr - _masm->code()->consts()->start();
 725       if (dest->is_double_fpu()) {
 726         __ z_ldy(dest->as_double_reg(), displ, toc_reg);
 727       } else {
 728         assert(dest->is_double_cpu(), "Must be a long register.");
 729         __ z_lg(dest->as_register_lo(), displ, toc_reg);
 730       }
 731     }
 732     break;
 733 
 734     default:
 735       ShouldNotReachHere();
 736   }
 737 }
 738 
 739 Address LIR_Assembler::as_Address(LIR_Address* addr) {
 740   if (addr->base()->is_illegal()) {
 741     Unimplemented();
 742   }
 743 
 744   Register base = addr->base()->as_pointer_register();
 745 
 746   if (addr->index()->is_illegal()) {
 747     return Address(base, addr->disp());
 748   } else if (addr->index()->is_cpu_register()) {
 749     Register index = addr->index()->as_pointer_register();
 750     return Address(base, index, addr->disp());
 751   } else if (addr->index()->is_constant()) {
 752     intptr_t addr_offset = addr->index()->as_constant_ptr()->as_jint() + addr->disp();
 753     return Address(base, addr_offset);
 754   } else {
 755     ShouldNotReachHere();
 756     return Address();
 757   }
 758 }
 759 
 760 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
 761   switch (type) {
 762     case T_INT:
 763     case T_FLOAT: {
 764       Register tmp = Z_R1_scratch;
 765       Address from = frame_map()->address_for_slot(src->single_stack_ix());
 766       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
 767       __ mem2reg_opt(tmp, from, false);
 768       __ reg2mem_opt(tmp, to, false);
 769       break;
 770     }
 771     case T_ADDRESS:
 772     case T_OBJECT: {
 773       Register tmp = Z_R1_scratch;
 774       Address from = frame_map()->address_for_slot(src->single_stack_ix());
 775       Address to   = frame_map()->address_for_slot(dest->single_stack_ix());
 776       __ mem2reg_opt(tmp, from, true);
 777       __ reg2mem_opt(tmp, to, true);
 778       break;
 779     }
 780     case T_LONG:
 781     case T_DOUBLE: {
 782       Register tmp = Z_R1_scratch;
 783       Address from = frame_map()->address_for_double_slot(src->double_stack_ix());
 784       Address to   = frame_map()->address_for_double_slot(dest->double_stack_ix());
 785       __ mem2reg_opt(tmp, from, true);
 786       __ reg2mem_opt(tmp, to, true);
 787       break;
 788     }
 789 
 790     default:
 791       ShouldNotReachHere();
 792   }
 793 }
 794 
 795 // 4-byte accesses only! Don't use it to access 8 bytes!
 796 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
 797   ShouldNotCallThis();
 798   return 0; // unused
 799 }
 800 
 801 // 4-byte accesses only! Don't use it to access 8 bytes!
 802 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
 803   ShouldNotCallThis();
 804   return 0; // unused
 805 }
 806 
 807 void LIR_Assembler::mem2reg(LIR_Opr src_opr, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code,
 808                             CodeEmitInfo* info, bool wide, bool unaligned) {
 809 
 810   assert(type != T_METADATA, "load of metadata ptr not supported");
 811   LIR_Address* addr = src_opr->as_address_ptr();
 812   LIR_Opr to_reg = dest;
 813 
 814   Register src = addr->base()->as_pointer_register();
 815   Register disp_reg = Z_R0;
 816   int disp_value = addr->disp();
 817   bool needs_patching = (patch_code != lir_patch_none);
 818 
 819   if (addr->base()->type() == T_OBJECT) {
 820     __ verify_oop(src);
 821   }
 822 
 823   PatchingStub* patch = NULL;
 824   if (needs_patching) {
 825     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
 826     assert(!to_reg->is_double_cpu() ||
 827            patch_code == lir_patch_none ||
 828            patch_code == lir_patch_normal, "patching doesn't match register");
 829   }
 830 
 831   if (addr->index()->is_illegal()) {
 832     if (!Immediate::is_simm20(disp_value)) {
 833       if (needs_patching) {
 834         __ load_const(Z_R1_scratch, (intptr_t)0);
 835       } else {
 836         __ load_const_optimized(Z_R1_scratch, disp_value);
 837       }
 838       disp_reg = Z_R1_scratch;
 839       disp_value = 0;
 840     }
 841   } else {
 842     if (!Immediate::is_simm20(disp_value)) {
 843       __ load_const_optimized(Z_R1_scratch, disp_value);
 844       __ z_la(Z_R1_scratch, 0, Z_R1_scratch, addr->index()->as_register());
 845       disp_reg = Z_R1_scratch;
 846       disp_value = 0;
 847     }
 848     disp_reg = addr->index()->as_pointer_register();
 849   }
 850 
 851   // Remember the offset of the load. The patching_epilog must be done
 852   // before the call to add_debug_info, otherwise the PcDescs don't get
 853   // entered in increasing order.
 854   int offset = code_offset();
 855 
 856   assert(disp_reg != Z_R0 || Immediate::is_simm20(disp_value), "should have set this up");
 857 
 858   bool short_disp = Immediate::is_uimm12(disp_value);
 859 
 860   switch (type) {
 861     case T_BOOLEAN: // fall through
 862     case T_BYTE  :  __ z_lb(dest->as_register(),   disp_value, disp_reg, src); break;
 863     case T_CHAR  :  __ z_llgh(dest->as_register(), disp_value, disp_reg, src); break;
 864     case T_SHORT :
 865       if (short_disp) {
 866                     __ z_lh(dest->as_register(),   disp_value, disp_reg, src);
 867       } else {
 868                     __ z_lhy(dest->as_register(),  disp_value, disp_reg, src);
 869       }
 870       break;
 871     case T_INT   :
 872       if (short_disp) {
 873                     __ z_l(dest->as_register(),    disp_value, disp_reg, src);
 874       } else {
 875                     __ z_ly(dest->as_register(),   disp_value, disp_reg, src);
 876       }
 877       break;
 878     case T_ADDRESS:
 879       if (UseCompressedClassPointers && addr->disp() == oopDesc::klass_offset_in_bytes()) {
 880         __ z_llgf(dest->as_register(), disp_value, disp_reg, src);
 881         __ decode_klass_not_null(dest->as_register());
 882       } else {
 883         __ z_lg(dest->as_register(), disp_value, disp_reg, src);
 884       }
 885       break;
 886     case T_ARRAY : // fall through
 887     case T_OBJECT:
 888     {
 889       if (UseCompressedOops && !wide) {
 890         __ z_llgf(dest->as_register(), disp_value, disp_reg, src);
 891         __ oop_decoder(dest->as_register(), dest->as_register(), true);
 892       } else {
 893         __ z_lg(dest->as_register(), disp_value, disp_reg, src);
 894       }
 895       break;
 896     }
 897     case T_FLOAT:
 898       if (short_disp) {
 899                     __ z_le(dest->as_float_reg(),  disp_value, disp_reg, src);
 900       } else {
 901                     __ z_ley(dest->as_float_reg(), disp_value, disp_reg, src);
 902       }
 903       break;
 904     case T_DOUBLE:
 905       if (short_disp) {
 906                     __ z_ld(dest->as_double_reg(),  disp_value, disp_reg, src);
 907       } else {
 908                     __ z_ldy(dest->as_double_reg(), disp_value, disp_reg, src);
 909       }
 910       break;
 911     case T_LONG  :  __ z_lg(dest->as_register_lo(), disp_value, disp_reg, src); break;
 912     default      : ShouldNotReachHere();
 913   }
 914   if (type == T_ARRAY || type == T_OBJECT) {
 915     __ verify_oop(dest->as_register());
 916   }
 917 
 918   if (patch != NULL) {
 919     patching_epilog(patch, patch_code, src, info);
 920   }
 921   if (info != NULL) add_debug_info_for_null_check(offset, info);
 922 }
 923 
 924 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
 925   assert(src->is_stack(), "should not call otherwise");
 926   assert(dest->is_register(), "should not call otherwise");
 927 
 928   if (dest->is_single_cpu()) {
 929     if (type == T_ARRAY || type == T_OBJECT) {
 930       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), true);
 931       __ verify_oop(dest->as_register());
 932     } else if (type == T_METADATA) {
 933       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), true);
 934     } else {
 935       __ mem2reg_opt(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()), false);
 936     }
 937   } else if (dest->is_double_cpu()) {
 938     Address src_addr_LO = frame_map()->address_for_slot(src->double_stack_ix());
 939     __ mem2reg_opt(dest->as_register_lo(), src_addr_LO, true);
 940   } else if (dest->is_single_fpu()) {
 941     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
 942     __ mem2freg_opt(dest->as_float_reg(), src_addr, false);
 943   } else if (dest->is_double_fpu()) {
 944     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
 945     __ mem2freg_opt(dest->as_double_reg(), src_addr, true);
 946   } else {
 947     ShouldNotReachHere();
 948   }
 949 }
 950 
 951 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
 952   assert(src->is_register(), "should not call otherwise");
 953   assert(dest->is_stack(), "should not call otherwise");
 954 
 955   if (src->is_single_cpu()) {
 956     const Address dst = frame_map()->address_for_slot(dest->single_stack_ix());
 957     if (type == T_OBJECT || type == T_ARRAY) {
 958       __ verify_oop(src->as_register());
 959       __ reg2mem_opt(src->as_register(), dst, true);
 960     } else if (type == T_METADATA) {
 961       __ reg2mem_opt(src->as_register(), dst, true);
 962     } else {
 963       __ reg2mem_opt(src->as_register(), dst, false);
 964     }
 965   } else if (src->is_double_cpu()) {
 966     Address dstLO = frame_map()->address_for_slot(dest->double_stack_ix());
 967     __ reg2mem_opt(src->as_register_lo(), dstLO, true);
 968   } else if (src->is_single_fpu()) {
 969     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 970     __ freg2mem_opt(src->as_float_reg(), dst_addr, false);
 971   } else if (src->is_double_fpu()) {
 972     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 973     __ freg2mem_opt(src->as_double_reg(), dst_addr, true);
 974   } else {
 975     ShouldNotReachHere();
 976   }
 977 }
 978 
 979 void LIR_Assembler::reg2reg(LIR_Opr from_reg, LIR_Opr to_reg) {
 980   if (from_reg->is_float_kind() && to_reg->is_float_kind()) {
 981     if (from_reg->is_double_fpu()) {
 982       // double to double moves
 983       assert(to_reg->is_double_fpu(), "should match");
 984       __ z_ldr(to_reg->as_double_reg(), from_reg->as_double_reg());
 985     } else {
 986       // float to float moves
 987       assert(to_reg->is_single_fpu(), "should match");
 988       __ z_ler(to_reg->as_float_reg(), from_reg->as_float_reg());
 989     }
 990   } else if (!from_reg->is_float_kind() && !to_reg->is_float_kind()) {
 991     if (from_reg->is_double_cpu()) {
 992       __ z_lgr(to_reg->as_pointer_register(), from_reg->as_pointer_register());
 993     } else if (to_reg->is_double_cpu()) {
 994       // int to int moves
 995       __ z_lgr(to_reg->as_register_lo(), from_reg->as_register());
 996     } else {
 997       // int to int moves
 998       __ z_lgr(to_reg->as_register(), from_reg->as_register());
 999     }
1000   } else {
1001     ShouldNotReachHere();
1002   }
1003   if (to_reg->type() == T_OBJECT || to_reg->type() == T_ARRAY) {
1004     __ verify_oop(to_reg->as_register());
1005   }
1006 }
1007 
1008 void LIR_Assembler::reg2mem(LIR_Opr from, LIR_Opr dest_opr, BasicType type,
1009                             LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack,
1010                             bool wide, bool unaligned) {
1011   assert(type != T_METADATA, "store of metadata ptr not supported");
1012   LIR_Address* addr = dest_opr->as_address_ptr();
1013 
1014   Register dest = addr->base()->as_pointer_register();
1015   Register disp_reg = Z_R0;
1016   int disp_value = addr->disp();
1017   bool needs_patching = (patch_code != lir_patch_none);
1018 
1019   if (addr->base()->is_oop_register()) {
1020     __ verify_oop(dest);
1021   }
1022 
1023   PatchingStub* patch = NULL;
1024   if (needs_patching) {
1025     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1026     assert(!from->is_double_cpu() ||
1027            patch_code == lir_patch_none ||
1028            patch_code == lir_patch_normal, "patching doesn't match register");
1029   }
1030 
1031   assert(!needs_patching || (!Immediate::is_simm20(disp_value) && addr->index()->is_illegal()), "assumption");
1032   if (addr->index()->is_illegal()) {
1033     if (!Immediate::is_simm20(disp_value)) {
1034       if (needs_patching) {
1035         __ load_const(Z_R1_scratch, (intptr_t)0);
1036       } else {
1037         __ load_const_optimized(Z_R1_scratch, disp_value);
1038       }
1039       disp_reg = Z_R1_scratch;
1040       disp_value = 0;
1041     }
1042   } else {
1043     if (!Immediate::is_simm20(disp_value)) {
1044       __ load_const_optimized(Z_R1_scratch, disp_value);
1045       __ z_la(Z_R1_scratch, 0, Z_R1_scratch, addr->index()->as_register());
1046       disp_reg = Z_R1_scratch;
1047       disp_value = 0;
1048     }
1049     disp_reg = addr->index()->as_pointer_register();
1050   }
1051 
1052   assert(disp_reg != Z_R0 || Immediate::is_simm20(disp_value), "should have set this up");
1053 
1054   if (type == T_ARRAY || type == T_OBJECT) {
1055     __ verify_oop(from->as_register());
1056   }
1057 
1058   bool short_disp = Immediate::is_uimm12(disp_value);
1059 
1060   // Remember the offset of the store. The patching_epilog must be done
1061   // before the call to add_debug_info_for_null_check, otherwise the PcDescs don't get
1062   // entered in increasing order.
1063   int offset = code_offset();
1064   switch (type) {
1065     case T_BOOLEAN: // fall through
1066     case T_BYTE  :
1067       if (short_disp) {
1068                     __ z_stc(from->as_register(),  disp_value, disp_reg, dest);
1069       } else {
1070                     __ z_stcy(from->as_register(), disp_value, disp_reg, dest);
1071       }
1072       break;
1073     case T_CHAR  : // fall through
1074     case T_SHORT :
1075       if (short_disp) {
1076                     __ z_sth(from->as_register(),  disp_value, disp_reg, dest);
1077       } else {
1078                     __ z_sthy(from->as_register(), disp_value, disp_reg, dest);
1079       }
1080       break;
1081     case T_INT   :
1082       if (short_disp) {
1083                     __ z_st(from->as_register(),  disp_value, disp_reg, dest);
1084       } else {
1085                     __ z_sty(from->as_register(), disp_value, disp_reg, dest);
1086       }
1087       break;
1088     case T_LONG  :  __ z_stg(from->as_register_lo(), disp_value, disp_reg, dest); break;
1089     case T_ADDRESS: __ z_stg(from->as_register(),    disp_value, disp_reg, dest); break;
1090       break;
1091     case T_ARRAY : // fall through
1092     case T_OBJECT:
1093       {
1094         if (UseCompressedOops && !wide) {
1095           Register compressed_src = Z_R14;
1096           __ oop_encoder(compressed_src, from->as_register(), true, (disp_reg != Z_R1) ? Z_R1 : Z_R0, -1, true);
1097           offset = code_offset();
1098           if (short_disp) {
1099             __ z_st(compressed_src,  disp_value, disp_reg, dest);
1100           } else {
1101             __ z_sty(compressed_src, disp_value, disp_reg, dest);
1102           }
1103         } else {
1104           __ z_stg(from->as_register(), disp_value, disp_reg, dest);
1105         }
1106         break;
1107       }
1108     case T_FLOAT :
1109       if (short_disp) {
1110         __ z_ste(from->as_float_reg(),  disp_value, disp_reg, dest);
1111       } else {
1112         __ z_stey(from->as_float_reg(), disp_value, disp_reg, dest);
1113       }
1114       break;
1115     case T_DOUBLE:
1116       if (short_disp) {
1117         __ z_std(from->as_double_reg(),  disp_value, disp_reg, dest);
1118       } else {
1119         __ z_stdy(from->as_double_reg(), disp_value, disp_reg, dest);
1120       }
1121       break;
1122     default: ShouldNotReachHere();
1123   }
1124 
1125   if (patch != NULL) {
1126     patching_epilog(patch, patch_code, dest, info);
1127   }
1128 
1129   if (info != NULL) add_debug_info_for_null_check(offset, info);
1130 }
1131 
1132 
1133 void LIR_Assembler::return_op(LIR_Opr result) {
1134   assert(result->is_illegal() ||
1135          (result->is_single_cpu() && result->as_register() == Z_R2) ||
1136          (result->is_double_cpu() && result->as_register_lo() == Z_R2) ||
1137          (result->is_single_fpu() && result->as_float_reg() == Z_F0) ||
1138          (result->is_double_fpu() && result->as_double_reg() == Z_F0), "convention");
1139 
1140   if (SafepointMechanism::uses_thread_local_poll()) {
1141     __ z_lg(Z_R1_scratch, Address(Z_thread, Thread::polling_page_offset()));
1142   } else {
1143     AddressLiteral pp(os::get_polling_page());
1144     __ load_const_optimized(Z_R1_scratch, pp);
1145   }
1146 
1147   // Pop the frame before the safepoint code.
1148   __ pop_frame_restore_retPC(initial_frame_size_in_bytes());
1149 
1150   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
1151     __ reserved_stack_check(Z_R14);
1152   }
1153 
1154   // We need to mark the code position where the load from the safepoint
1155   // polling page was emitted as relocInfo::poll_return_type here.
1156   __ relocate(relocInfo::poll_return_type);
1157   __ load_from_polling_page(Z_R1_scratch);
1158 
1159   __ z_br(Z_R14); // Return to caller.
1160 }
1161 
1162 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
1163   const Register poll_addr = tmp->as_register_lo();
1164   if (SafepointMechanism::uses_thread_local_poll()) {
1165     __ z_lg(poll_addr, Address(Z_thread, Thread::polling_page_offset()));
1166   } else {
1167     AddressLiteral pp(os::get_polling_page());
1168     __ load_const_optimized(poll_addr, pp);
1169   }
1170   guarantee(info != NULL, "Shouldn't be NULL");
1171   add_debug_info_for_branch(info);
1172   int offset = __ offset();
1173   __ relocate(relocInfo::poll_type);
1174   __ load_from_polling_page(poll_addr);
1175   return offset;
1176 }
1177 
1178 void LIR_Assembler::emit_static_call_stub() {
1179 
1180   // Stub is fixed up when the corresponding call is converted from calling
1181   // compiled code to calling interpreted code.
1182 
1183   address call_pc = __ pc();
1184   address stub = __ start_a_stub(call_stub_size());
1185   if (stub == NULL) {
1186     bailout("static call stub overflow");
1187     return;
1188   }
1189 
1190   int start = __ offset();
1191 
1192   __ relocate(static_stub_Relocation::spec(call_pc));
1193 
1194   // See also Matcher::interpreter_method_oop_reg().
1195   AddressLiteral meta = __ allocate_metadata_address(NULL);
1196   bool success = __ load_const_from_toc(Z_method, meta);
1197 
1198   __ set_inst_mark();
1199   AddressLiteral a((address)-1);
1200   success = success && __ load_const_from_toc(Z_R1, a);
1201   if (!success) {
1202     bailout("const section overflow");
1203     return;
1204   }
1205 
1206   __ z_br(Z_R1);
1207   assert(__ offset() - start <= call_stub_size(), "stub too big");
1208   __ end_a_stub(); // Update current stubs pointer and restore insts_end.
1209 }
1210 
1211 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
1212   bool unsigned_comp = condition == lir_cond_belowEqual || condition == lir_cond_aboveEqual;
1213   if (opr1->is_single_cpu()) {
1214     Register reg1 = opr1->as_register();
1215     if (opr2->is_single_cpu()) {
1216       // cpu register - cpu register
1217       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
1218         __ z_clgr(reg1, opr2->as_register());
1219       } else {
1220         assert(opr2->type() != T_OBJECT && opr2->type() != T_ARRAY, "cmp int, oop?");
1221         if (unsigned_comp) {
1222           __ z_clr(reg1, opr2->as_register());
1223         } else {
1224           __ z_cr(reg1, opr2->as_register());
1225         }
1226       }
1227     } else if (opr2->is_stack()) {
1228       // cpu register - stack
1229       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY) {
1230         __ z_cg(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1231       } else {
1232         if (unsigned_comp) {
1233           __ z_cly(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1234         } else {
1235           __ z_cy(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
1236         }
1237       }
1238     } else if (opr2->is_constant()) {
1239       // cpu register - constant
1240       LIR_Const* c = opr2->as_constant_ptr();
1241       if (c->type() == T_INT) {
1242         if (unsigned_comp) {
1243           __ z_clfi(reg1, c->as_jint());
1244         } else {
1245           __ z_cfi(reg1, c->as_jint());
1246         }
1247       } else if (c->type() == T_OBJECT || c->type() == T_ARRAY) {
1248         // In 64bit oops are single register.
1249         jobject o = c->as_jobject();
1250         if (o == NULL) {
1251           __ z_ltgr(reg1, reg1);
1252         } else {
1253           jobject2reg(o, Z_R1_scratch);
1254           __ z_cgr(reg1, Z_R1_scratch);
1255         }
1256       } else {
1257         fatal("unexpected type: %s", basictype_to_str(c->type()));
1258       }
1259       // cpu register - address
1260     } else if (opr2->is_address()) {
1261       if (op->info() != NULL) {
1262         add_debug_info_for_null_check_here(op->info());
1263       }
1264       if (unsigned_comp) {
1265         __ z_cly(reg1, as_Address(opr2->as_address_ptr()));
1266       } else {
1267         __ z_cy(reg1, as_Address(opr2->as_address_ptr()));
1268       }
1269     } else {
1270       ShouldNotReachHere();
1271     }
1272 
1273   } else if (opr1->is_double_cpu()) {
1274     assert(!unsigned_comp, "unexpected");
1275     Register xlo = opr1->as_register_lo();
1276     Register xhi = opr1->as_register_hi();
1277     if (opr2->is_double_cpu()) {
1278       __ z_cgr(xlo, opr2->as_register_lo());
1279     } else if (opr2->is_constant()) {
1280       // cpu register - constant 0
1281       assert(opr2->as_jlong() == (jlong)0, "only handles zero");
1282       __ z_ltgr(xlo, xlo);
1283     } else {
1284       ShouldNotReachHere();
1285     }
1286 
1287   } else if (opr1->is_single_fpu()) {
1288     if (opr2->is_single_fpu()) {
1289       __ z_cebr(opr1->as_float_reg(), opr2->as_float_reg());
1290     } else {
1291       // stack slot
1292       Address addr = frame_map()->address_for_slot(opr2->single_stack_ix());
1293       if (Immediate::is_uimm12(addr.disp())) {
1294         __ z_ceb(opr1->as_float_reg(), addr);
1295       } else {
1296         __ z_ley(Z_fscratch_1, addr);
1297         __ z_cebr(opr1->as_float_reg(), Z_fscratch_1);
1298       }
1299     }
1300   } else if (opr1->is_double_fpu()) {
1301     if (opr2->is_double_fpu()) {
1302     __ z_cdbr(opr1->as_double_reg(), opr2->as_double_reg());
1303     } else {
1304       // stack slot
1305       Address addr = frame_map()->address_for_slot(opr2->double_stack_ix());
1306       if (Immediate::is_uimm12(addr.disp())) {
1307         __ z_cdb(opr1->as_double_reg(), addr);
1308       } else {
1309         __ z_ldy(Z_fscratch_1, addr);
1310         __ z_cdbr(opr1->as_double_reg(), Z_fscratch_1);
1311       }
1312     }
1313   } else {
1314     ShouldNotReachHere();
1315   }
1316 }
1317 
1318 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op) {
1319   Label    done;
1320   Register dreg = dst->as_register();
1321 
1322   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
1323     assert((left->is_single_fpu() && right->is_single_fpu()) ||
1324            (left->is_double_fpu() && right->is_double_fpu()), "unexpected operand types");
1325     bool is_single = left->is_single_fpu();
1326     bool is_unordered_less = (code == lir_ucmp_fd2i);
1327     FloatRegister lreg = is_single ? left->as_float_reg() : left->as_double_reg();
1328     FloatRegister rreg = is_single ? right->as_float_reg() : right->as_double_reg();
1329     if (is_single) {
1330       __ z_cebr(lreg, rreg);
1331     } else {
1332       __ z_cdbr(lreg, rreg);
1333     }
1334     if (VM_Version::has_LoadStoreConditional()) {
1335       Register one       = Z_R0_scratch;
1336       Register minus_one = Z_R1_scratch;
1337       __ z_lghi(minus_one, -1);
1338       __ z_lghi(one,  1);
1339       __ z_lghi(dreg, 0);
1340       __ z_locgr(dreg, one,       is_unordered_less ? Assembler::bcondHigh            : Assembler::bcondHighOrNotOrdered);
1341       __ z_locgr(dreg, minus_one, is_unordered_less ? Assembler::bcondLowOrNotOrdered : Assembler::bcondLow);
1342     } else {
1343       __ clear_reg(dreg, true, false);
1344       __ z_bre(done); // if (left == right) dst = 0
1345 
1346       // if (left > right || ((code ~= cmpg) && (left <> right)) dst := 1
1347       __ z_lhi(dreg, 1);
1348       __ z_brc(is_unordered_less ? Assembler::bcondHigh : Assembler::bcondHighOrNotOrdered, done);
1349 
1350       // if (left < right || ((code ~= cmpl) && (left <> right)) dst := -1
1351       __ z_lhi(dreg, -1);
1352     }
1353   } else {
1354     assert(code == lir_cmp_l2i, "check");
1355     if (VM_Version::has_LoadStoreConditional()) {
1356       Register one       = Z_R0_scratch;
1357       Register minus_one = Z_R1_scratch;
1358       __ z_cgr(left->as_register_lo(), right->as_register_lo());
1359       __ z_lghi(minus_one, -1);
1360       __ z_lghi(one,  1);
1361       __ z_lghi(dreg, 0);
1362       __ z_locgr(dreg, one, Assembler::bcondHigh);
1363       __ z_locgr(dreg, minus_one, Assembler::bcondLow);
1364     } else {
1365       __ z_cgr(left->as_register_lo(), right->as_register_lo());
1366       __ z_lghi(dreg,  0);     // eq value
1367       __ z_bre(done);
1368       __ z_lghi(dreg,  1);     // gt value
1369       __ z_brh(done);
1370       __ z_lghi(dreg, -1);     // lt value
1371     }
1372   }
1373   __ bind(done);
1374 }
1375 
1376 // result = condition ? opr1 : opr2
1377 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type) {
1378   Assembler::branch_condition acond = Assembler::bcondEqual, ncond = Assembler::bcondNotEqual;
1379   switch (condition) {
1380     case lir_cond_equal:        acond = Assembler::bcondEqual;    ncond = Assembler::bcondNotEqual; break;
1381     case lir_cond_notEqual:     acond = Assembler::bcondNotEqual; ncond = Assembler::bcondEqual;    break;
1382     case lir_cond_less:         acond = Assembler::bcondLow;      ncond = Assembler::bcondNotLow;   break;
1383     case lir_cond_lessEqual:    acond = Assembler::bcondNotHigh;  ncond = Assembler::bcondHigh;     break;
1384     case lir_cond_greaterEqual: acond = Assembler::bcondNotLow;   ncond = Assembler::bcondLow;      break;
1385     case lir_cond_greater:      acond = Assembler::bcondHigh;     ncond = Assembler::bcondNotHigh;  break;
1386     case lir_cond_belowEqual:   acond = Assembler::bcondNotHigh;  ncond = Assembler::bcondHigh;     break;
1387     case lir_cond_aboveEqual:   acond = Assembler::bcondNotLow;   ncond = Assembler::bcondLow;      break;
1388     default:                    ShouldNotReachHere();
1389   }
1390 
1391   if (opr1->is_cpu_register()) {
1392     reg2reg(opr1, result);
1393   } else if (opr1->is_stack()) {
1394     stack2reg(opr1, result, result->type());
1395   } else if (opr1->is_constant()) {
1396     const2reg(opr1, result, lir_patch_none, NULL);
1397   } else {
1398     ShouldNotReachHere();
1399   }
1400 
1401   if (VM_Version::has_LoadStoreConditional() && !opr2->is_constant()) {
1402     // Optimized version that does not require a branch.
1403     if (opr2->is_single_cpu()) {
1404       assert(opr2->cpu_regnr() != result->cpu_regnr(), "opr2 already overwritten by previous move");
1405       __ z_locgr(result->as_register(), opr2->as_register(), ncond);
1406     } else if (opr2->is_double_cpu()) {
1407       assert(opr2->cpu_regnrLo() != result->cpu_regnrLo() && opr2->cpu_regnrLo() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1408       assert(opr2->cpu_regnrHi() != result->cpu_regnrLo() && opr2->cpu_regnrHi() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
1409       __ z_locgr(result->as_register_lo(), opr2->as_register_lo(), ncond);
1410     } else if (opr2->is_single_stack()) {
1411       __ z_loc(result->as_register(), frame_map()->address_for_slot(opr2->single_stack_ix()), ncond);
1412     } else if (opr2->is_double_stack()) {
1413       __ z_locg(result->as_register_lo(), frame_map()->address_for_slot(opr2->double_stack_ix()), ncond);
1414     } else {
1415       ShouldNotReachHere();
1416     }
1417   } else {
1418     Label skip;
1419     __ z_brc(acond, skip);
1420     if (opr2->is_cpu_register()) {
1421       reg2reg(opr2, result);
1422     } else if (opr2->is_stack()) {
1423       stack2reg(opr2, result, result->type());
1424     } else if (opr2->is_constant()) {
1425       const2reg(opr2, result, lir_patch_none, NULL);
1426     } else {
1427       ShouldNotReachHere();
1428     }
1429     __ bind(skip);
1430   }
1431 }
1432 
1433 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest,
1434                              CodeEmitInfo* info, bool pop_fpu_stack) {
1435   assert(info == NULL, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1436 
1437   if (left->is_single_cpu()) {
1438     assert(left == dest, "left and dest must be equal");
1439     Register lreg = left->as_register();
1440 
1441     if (right->is_single_cpu()) {
1442       // cpu register - cpu register
1443       Register rreg = right->as_register();
1444       switch (code) {
1445         case lir_add: __ z_ar (lreg, rreg); break;
1446         case lir_sub: __ z_sr (lreg, rreg); break;
1447         case lir_mul: __ z_msr(lreg, rreg); break;
1448         default: ShouldNotReachHere();
1449       }
1450 
1451     } else if (right->is_stack()) {
1452       // cpu register - stack
1453       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
1454       switch (code) {
1455         case lir_add: __ z_ay(lreg, raddr); break;
1456         case lir_sub: __ z_sy(lreg, raddr); break;
1457         default: ShouldNotReachHere();
1458       }
1459 
1460     } else if (right->is_constant()) {
1461       // cpu register - constant
1462       jint c = right->as_constant_ptr()->as_jint();
1463       switch (code) {
1464         case lir_add: __ z_agfi(lreg, c);  break;
1465         case lir_sub: __ z_agfi(lreg, -c); break; // note: -min_jint == min_jint
1466         case lir_mul: __ z_msfi(lreg, c);  break;
1467         default: ShouldNotReachHere();
1468       }
1469 
1470     } else {
1471       ShouldNotReachHere();
1472     }
1473 
1474   } else if (left->is_double_cpu()) {
1475     assert(left == dest, "left and dest must be equal");
1476     Register lreg_lo = left->as_register_lo();
1477     Register lreg_hi = left->as_register_hi();
1478 
1479     if (right->is_double_cpu()) {
1480       // cpu register - cpu register
1481       Register rreg_lo = right->as_register_lo();
1482       Register rreg_hi = right->as_register_hi();
1483       assert_different_registers(lreg_lo, rreg_lo);
1484       switch (code) {
1485         case lir_add:
1486           __ z_agr(lreg_lo, rreg_lo);
1487           break;
1488         case lir_sub:
1489           __ z_sgr(lreg_lo, rreg_lo);
1490           break;
1491         case lir_mul:
1492           __ z_msgr(lreg_lo, rreg_lo);
1493           break;
1494         default:
1495           ShouldNotReachHere();
1496       }
1497 
1498     } else if (right->is_constant()) {
1499       // cpu register - constant
1500       jlong c = right->as_constant_ptr()->as_jlong_bits();
1501       switch (code) {
1502         case lir_add: __ z_agfi(lreg_lo, c); break;
1503         case lir_sub:
1504           if (c != min_jint) {
1505                       __ z_agfi(lreg_lo, -c);
1506           } else {
1507             // -min_jint cannot be represented as simm32 in z_agfi
1508             // min_jint sign extended:      0xffffffff80000000
1509             // -min_jint as 64 bit integer: 0x0000000080000000
1510             // 0x80000000 can be represented as uimm32 in z_algfi
1511             // lreg_lo := lreg_lo + -min_jint == lreg_lo + 0x80000000
1512                       __ z_algfi(lreg_lo, UCONST64(0x80000000));
1513           }
1514           break;
1515         case lir_mul: __ z_msgfi(lreg_lo, c); break;
1516         default:
1517           ShouldNotReachHere();
1518       }
1519 
1520     } else {
1521       ShouldNotReachHere();
1522     }
1523 
1524   } else if (left->is_single_fpu()) {
1525     assert(left == dest, "left and dest must be equal");
1526     FloatRegister lreg = left->as_float_reg();
1527     FloatRegister rreg = right->is_single_fpu() ? right->as_float_reg() : fnoreg;
1528     Address raddr;
1529 
1530     if (rreg == fnoreg) {
1531       assert(right->is_single_stack(), "constants should be loaded into register");
1532       raddr = frame_map()->address_for_slot(right->single_stack_ix());
1533       if (!Immediate::is_uimm12(raddr.disp())) {
1534         __ mem2freg_opt(rreg = Z_fscratch_1, raddr, false);
1535       }
1536     }
1537 
1538     if (rreg != fnoreg) {
1539       switch (code) {
1540         case lir_add: __ z_aebr(lreg, rreg);  break;
1541         case lir_sub: __ z_sebr(lreg, rreg);  break;
1542         case lir_mul_strictfp: // fall through
1543         case lir_mul: __ z_meebr(lreg, rreg); break;
1544         case lir_div_strictfp: // fall through
1545         case lir_div: __ z_debr(lreg, rreg);  break;
1546         default: ShouldNotReachHere();
1547       }
1548     } else {
1549       switch (code) {
1550         case lir_add: __ z_aeb(lreg, raddr);  break;
1551         case lir_sub: __ z_seb(lreg, raddr);  break;
1552         case lir_mul_strictfp: // fall through
1553         case lir_mul: __ z_meeb(lreg, raddr);  break;
1554         case lir_div_strictfp: // fall through
1555         case lir_div: __ z_deb(lreg, raddr);  break;
1556         default: ShouldNotReachHere();
1557       }
1558     }
1559   } else if (left->is_double_fpu()) {
1560     assert(left == dest, "left and dest must be equal");
1561     FloatRegister lreg = left->as_double_reg();
1562     FloatRegister rreg = right->is_double_fpu() ? right->as_double_reg() : fnoreg;
1563     Address raddr;
1564 
1565     if (rreg == fnoreg) {
1566       assert(right->is_double_stack(), "constants should be loaded into register");
1567       raddr = frame_map()->address_for_slot(right->double_stack_ix());
1568       if (!Immediate::is_uimm12(raddr.disp())) {
1569         __ mem2freg_opt(rreg = Z_fscratch_1, raddr, true);
1570       }
1571     }
1572 
1573     if (rreg != fnoreg) {
1574       switch (code) {
1575         case lir_add: __ z_adbr(lreg, rreg); break;
1576         case lir_sub: __ z_sdbr(lreg, rreg); break;
1577         case lir_mul_strictfp: // fall through
1578         case lir_mul: __ z_mdbr(lreg, rreg); break;
1579         case lir_div_strictfp: // fall through
1580         case lir_div: __ z_ddbr(lreg, rreg); break;
1581         default: ShouldNotReachHere();
1582       }
1583     } else {
1584       switch (code) {
1585         case lir_add: __ z_adb(lreg, raddr); break;
1586         case lir_sub: __ z_sdb(lreg, raddr); break;
1587         case lir_mul_strictfp: // fall through
1588         case lir_mul: __ z_mdb(lreg, raddr); break;
1589         case lir_div_strictfp: // fall through
1590         case lir_div: __ z_ddb(lreg, raddr); break;
1591         default: ShouldNotReachHere();
1592       }
1593     }
1594   } else if (left->is_address()) {
1595     assert(left == dest, "left and dest must be equal");
1596     assert(code == lir_add, "unsupported operation");
1597     assert(right->is_constant(), "unsupported operand");
1598     jint c = right->as_constant_ptr()->as_jint();
1599     LIR_Address* lir_addr = left->as_address_ptr();
1600     Address addr = as_Address(lir_addr);
1601     switch (lir_addr->type()) {
1602       case T_INT:
1603         __ add2mem_32(addr, c, Z_R1_scratch);
1604         break;
1605       case T_LONG:
1606         __ add2mem_64(addr, c, Z_R1_scratch);
1607         break;
1608       default:
1609         ShouldNotReachHere();
1610     }
1611   } else {
1612     ShouldNotReachHere();
1613   }
1614 }
1615 
1616 void LIR_Assembler::fpop() {
1617   // do nothing
1618 }
1619 
1620 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr thread, LIR_Opr dest, LIR_Op* op) {
1621   switch (code) {
1622     case lir_sqrt: {
1623       assert(!thread->is_valid(), "there is no need for a thread_reg for dsqrt");
1624       FloatRegister src_reg = value->as_double_reg();
1625       FloatRegister dst_reg = dest->as_double_reg();
1626       __ z_sqdbr(dst_reg, src_reg);
1627       break;
1628     }
1629     case lir_abs: {
1630       assert(!thread->is_valid(), "there is no need for a thread_reg for fabs");
1631       FloatRegister src_reg = value->as_double_reg();
1632       FloatRegister dst_reg = dest->as_double_reg();
1633       __ z_lpdbr(dst_reg, src_reg);
1634       break;
1635     }
1636     default: {
1637       ShouldNotReachHere();
1638       break;
1639     }
1640   }
1641 }
1642 
1643 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
1644   if (left->is_single_cpu()) {
1645     Register reg = left->as_register();
1646     if (right->is_constant()) {
1647       int val = right->as_constant_ptr()->as_jint();
1648       switch (code) {
1649         case lir_logic_and: __ z_nilf(reg, val); break;
1650         case lir_logic_or:  __ z_oilf(reg, val); break;
1651         case lir_logic_xor: __ z_xilf(reg, val); break;
1652         default: ShouldNotReachHere();
1653       }
1654     } else if (right->is_stack()) {
1655       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
1656       switch (code) {
1657         case lir_logic_and: __ z_ny(reg, raddr); break;
1658         case lir_logic_or:  __ z_oy(reg, raddr); break;
1659         case lir_logic_xor: __ z_xy(reg, raddr); break;
1660         default: ShouldNotReachHere();
1661       }
1662     } else {
1663       Register rright = right->as_register();
1664       switch (code) {
1665         case lir_logic_and: __ z_nr(reg, rright); break;
1666         case lir_logic_or : __ z_or(reg, rright); break;
1667         case lir_logic_xor: __ z_xr(reg, rright); break;
1668         default: ShouldNotReachHere();
1669       }
1670     }
1671     move_regs(reg, dst->as_register());
1672   } else {
1673     Register l_lo = left->as_register_lo();
1674     if (right->is_constant()) {
1675       __ load_const_optimized(Z_R1_scratch, right->as_constant_ptr()->as_jlong());
1676       switch (code) {
1677         case lir_logic_and:
1678           __ z_ngr(l_lo, Z_R1_scratch);
1679           break;
1680         case lir_logic_or:
1681           __ z_ogr(l_lo, Z_R1_scratch);
1682           break;
1683         case lir_logic_xor:
1684           __ z_xgr(l_lo, Z_R1_scratch);
1685           break;
1686         default: ShouldNotReachHere();
1687       }
1688     } else {
1689       Register r_lo;
1690       if (right->type() == T_OBJECT || right->type() == T_ARRAY) {
1691         r_lo = right->as_register();
1692       } else {
1693         r_lo = right->as_register_lo();
1694       }
1695       switch (code) {
1696         case lir_logic_and:
1697           __ z_ngr(l_lo, r_lo);
1698           break;
1699         case lir_logic_or:
1700           __ z_ogr(l_lo, r_lo);
1701           break;
1702         case lir_logic_xor:
1703           __ z_xgr(l_lo, r_lo);
1704           break;
1705         default: ShouldNotReachHere();
1706       }
1707     }
1708 
1709     Register dst_lo = dst->as_register_lo();
1710 
1711     move_regs(l_lo, dst_lo);
1712   }
1713 }
1714 
1715 // See operand selection in LIRGenerator::do_ArithmeticOp_Int().
1716 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr temp, LIR_Opr result, CodeEmitInfo* info) {
1717   if (left->is_double_cpu()) {
1718     // 64 bit integer case
1719     assert(left->is_double_cpu(), "left must be register");
1720     assert(right->is_double_cpu() || is_power_of_2_long(right->as_jlong()),
1721            "right must be register or power of 2 constant");
1722     assert(result->is_double_cpu(), "result must be register");
1723 
1724     Register lreg = left->as_register_lo();
1725     Register dreg = result->as_register_lo();
1726 
1727     if (right->is_constant()) {
1728       // Convert division by a power of two into some shifts and logical operations.
1729       Register treg1 = Z_R0_scratch;
1730       Register treg2 = Z_R1_scratch;
1731       jlong divisor = right->as_jlong();
1732       jlong log_divisor = log2_long(right->as_jlong());
1733 
1734       if (divisor == min_jlong) {
1735         // Min_jlong is special. Result is '0' except for min_jlong/min_jlong = 1.
1736         if (dreg == lreg) {
1737           NearLabel done;
1738           __ load_const_optimized(treg2, min_jlong);
1739           __ z_cgr(lreg, treg2);
1740           __ z_lghi(dreg, 0);           // Preserves condition code.
1741           __ z_brne(done);
1742           __ z_lghi(dreg, 1);           // min_jlong / min_jlong = 1
1743           __ bind(done);
1744         } else {
1745           assert_different_registers(dreg, lreg);
1746           NearLabel done;
1747           __ z_lghi(dreg, 0);
1748           __ compare64_and_branch(lreg, min_jlong, Assembler::bcondNotEqual, done);
1749           __ z_lghi(dreg, 1);
1750           __ bind(done);
1751         }
1752         return;
1753       }
1754       __ move_reg_if_needed(dreg, T_LONG, lreg, T_LONG);
1755       if (divisor == 2) {
1756         __ z_srlg(treg2, dreg, 63);     // dividend < 0 ? 1 : 0
1757       } else {
1758         __ z_srag(treg2, dreg, 63);     // dividend < 0 ? -1 : 0
1759         __ and_imm(treg2, divisor - 1, treg1, true);
1760       }
1761       if (code == lir_idiv) {
1762         __ z_agr(dreg, treg2);
1763         __ z_srag(dreg, dreg, log_divisor);
1764       } else {
1765         assert(code == lir_irem, "check");
1766         __ z_agr(treg2, dreg);
1767         __ and_imm(treg2, ~(divisor - 1), treg1, true);
1768         __ z_sgr(dreg, treg2);
1769       }
1770       return;
1771     }
1772 
1773     // Divisor is not a power of 2 constant.
1774     Register rreg = right->as_register_lo();
1775     Register treg = temp->as_register_lo();
1776     assert(right->is_double_cpu(), "right must be register");
1777     assert(lreg == Z_R11, "see ldivInOpr()");
1778     assert(rreg != lreg, "right register must not be same as left register");
1779     assert((code == lir_idiv && dreg == Z_R11 && treg == Z_R10) ||
1780            (code == lir_irem && dreg == Z_R10 && treg == Z_R11), "see ldivInOpr(), ldivOutOpr(), lremOutOpr()");
1781 
1782     Register R1 = lreg->predecessor();
1783     Register R2 = rreg;
1784     assert(code != lir_idiv || lreg==dreg, "see code below");
1785     if (code == lir_idiv) {
1786       __ z_lcgr(lreg, lreg);
1787     } else {
1788       __ clear_reg(dreg, true, false);
1789     }
1790     NearLabel done;
1791     __ compare64_and_branch(R2, -1, Assembler::bcondEqual, done);
1792     if (code == lir_idiv) {
1793       __ z_lcgr(lreg, lreg); // Revert lcgr above.
1794     }
1795     if (ImplicitDiv0Checks) {
1796       // No debug info because the idiv won't trap.
1797       // Add_debug_info_for_div0 would instantiate another DivByZeroStub,
1798       // which is unnecessary, too.
1799       add_debug_info_for_div0(__ offset(), info);
1800     }
1801     __ z_dsgr(R1, R2);
1802     __ bind(done);
1803     return;
1804   }
1805 
1806   // 32 bit integer case
1807 
1808   assert(left->is_single_cpu(), "left must be register");
1809   assert(right->is_single_cpu() || is_power_of_2(right->as_jint()), "right must be register or power of 2 constant");
1810   assert(result->is_single_cpu(), "result must be register");
1811 
1812   Register lreg = left->as_register();
1813   Register dreg = result->as_register();
1814 
1815   if (right->is_constant()) {
1816     // Convert division by a power of two into some shifts and logical operations.
1817     Register treg1 = Z_R0_scratch;
1818     Register treg2 = Z_R1_scratch;
1819     jlong divisor = right->as_jint();
1820     jlong log_divisor = log2_long(right->as_jint());
1821     __ move_reg_if_needed(dreg, T_LONG, lreg, T_INT); // sign extend
1822     if (divisor == 2) {
1823       __ z_srlg(treg2, dreg, 63);     // dividend < 0 ?  1 : 0
1824     } else {
1825       __ z_srag(treg2, dreg, 63);     // dividend < 0 ? -1 : 0
1826       __ and_imm(treg2, divisor - 1, treg1, true);
1827     }
1828     if (code == lir_idiv) {
1829       __ z_agr(dreg, treg2);
1830       __ z_srag(dreg, dreg, log_divisor);
1831     } else {
1832       assert(code == lir_irem, "check");
1833       __ z_agr(treg2, dreg);
1834       __ and_imm(treg2, ~(divisor - 1), treg1, true);
1835       __ z_sgr(dreg, treg2);
1836     }
1837     return;
1838   }
1839 
1840   // Divisor is not a power of 2 constant.
1841   Register rreg = right->as_register();
1842   Register treg = temp->as_register();
1843   assert(right->is_single_cpu(), "right must be register");
1844   assert(lreg == Z_R11, "left register must be rax,");
1845   assert(rreg != lreg, "right register must not be same as left register");
1846   assert((code == lir_idiv && dreg == Z_R11 && treg == Z_R10)
1847       || (code == lir_irem && dreg == Z_R10 && treg == Z_R11), "see divInOpr(), divOutOpr(), remOutOpr()");
1848 
1849   Register R1 = lreg->predecessor();
1850   Register R2 = rreg;
1851   __ move_reg_if_needed(lreg, T_LONG, lreg, T_INT); // sign extend
1852   if (ImplicitDiv0Checks) {
1853     // No debug info because the idiv won't trap.
1854     // Add_debug_info_for_div0 would instantiate another DivByZeroStub,
1855     // which is unnecessary, too.
1856     add_debug_info_for_div0(__ offset(), info);
1857   }
1858   __ z_dsgfr(R1, R2);
1859 }
1860 
1861 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
1862   assert(exceptionOop->as_register() == Z_EXC_OOP, "should match");
1863   assert(exceptionPC->as_register() == Z_EXC_PC, "should match");
1864 
1865   // Exception object is not added to oop map by LinearScan
1866   // (LinearScan assumes that no oops are in fixed registers).
1867   info->add_register_oop(exceptionOop);
1868 
1869   // Reuse the debug info from the safepoint poll for the throw op itself.
1870   __ get_PC(Z_EXC_PC);
1871   add_call_info(__ offset(), info); // for exception handler
1872   address stub = Runtime1::entry_for (compilation()->has_fpu_code() ? Runtime1::handle_exception_id
1873                                                                     : Runtime1::handle_exception_nofpu_id);
1874   emit_call_c(stub);
1875 }
1876 
1877 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
1878   assert(exceptionOop->as_register() == Z_EXC_OOP, "should match");
1879 
1880   __ branch_optimized(Assembler::bcondAlways, _unwind_handler_entry);
1881 }
1882 
1883 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
1884   ciArrayKlass* default_type = op->expected_type();
1885   Register src = op->src()->as_register();
1886   Register dst = op->dst()->as_register();
1887   Register src_pos = op->src_pos()->as_register();
1888   Register dst_pos = op->dst_pos()->as_register();
1889   Register length  = op->length()->as_register();
1890   Register tmp = op->tmp()->as_register();
1891 
1892   CodeStub* stub = op->stub();
1893   int flags = op->flags();
1894   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
1895   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
1896 
1897   // If we don't know anything, just go through the generic arraycopy.
1898   if (default_type == NULL) {
1899     Label done;
1900     // Save outgoing arguments in callee saved registers (C convention) in case
1901     // a call to System.arraycopy is needed.
1902     Register callee_saved_src     = Z_R10;
1903     Register callee_saved_src_pos = Z_R11;
1904     Register callee_saved_dst     = Z_R12;
1905     Register callee_saved_dst_pos = Z_R13;
1906     Register callee_saved_length  = Z_ARG5; // Z_ARG5 == Z_R6 is callee saved.
1907 
1908     __ lgr_if_needed(callee_saved_src, src);
1909     __ lgr_if_needed(callee_saved_src_pos, src_pos);
1910     __ lgr_if_needed(callee_saved_dst, dst);
1911     __ lgr_if_needed(callee_saved_dst_pos, dst_pos);
1912     __ lgr_if_needed(callee_saved_length, length);
1913 
1914     // C function requires 64 bit values.
1915     __ z_lgfr(src_pos, src_pos);
1916     __ z_lgfr(dst_pos, dst_pos);
1917     __ z_lgfr(length, length);
1918 
1919     address C_entry = CAST_FROM_FN_PTR(address, Runtime1::arraycopy);
1920 
1921     address copyfunc_addr = StubRoutines::generic_arraycopy();
1922 
1923     // Pass arguments: may push as this is not a safepoint; SP must be fix at each safepoint.
1924 
1925     // The arguments are in the corresponding registers.
1926     assert(Z_ARG1 == src,     "assumption");
1927     assert(Z_ARG2 == src_pos, "assumption");
1928     assert(Z_ARG3 == dst,     "assumption");
1929     assert(Z_ARG4 == dst_pos, "assumption");
1930     assert(Z_ARG5 == length,  "assumption");
1931     if (copyfunc_addr == NULL) { // Use C version if stub was not generated.
1932       emit_call_c(C_entry);
1933     } else {
1934 #ifndef PRODUCT
1935       if (PrintC1Statistics) {
1936         __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_generic_arraycopystub_cnt);
1937         __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
1938       }
1939 #endif
1940       emit_call_c(copyfunc_addr);
1941     }
1942     CHECK_BAILOUT();
1943 
1944     __ compare32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondEqual, *stub->continuation());
1945 
1946     if (copyfunc_addr != NULL) {
1947       __ z_lgr(tmp, Z_RET);
1948       __ z_xilf(tmp, -1);
1949     }
1950 
1951     // Restore values from callee saved registers so they are where the stub
1952     // expects them.
1953     __ lgr_if_needed(src, callee_saved_src);
1954     __ lgr_if_needed(src_pos, callee_saved_src_pos);
1955     __ lgr_if_needed(dst, callee_saved_dst);
1956     __ lgr_if_needed(dst_pos, callee_saved_dst_pos);
1957     __ lgr_if_needed(length, callee_saved_length);
1958 
1959     if (copyfunc_addr != NULL) {
1960       __ z_sr(length, tmp);
1961       __ z_ar(src_pos, tmp);
1962       __ z_ar(dst_pos, tmp);
1963     }
1964     __ branch_optimized(Assembler::bcondAlways, *stub->entry());
1965 
1966     __ bind(*stub->continuation());
1967     return;
1968   }
1969 
1970   assert(default_type != NULL && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
1971 
1972   int elem_size = type2aelembytes(basic_type);
1973   int shift_amount;
1974 
1975   switch (elem_size) {
1976     case 1 :
1977       shift_amount = 0;
1978       break;
1979     case 2 :
1980       shift_amount = 1;
1981       break;
1982     case 4 :
1983       shift_amount = 2;
1984       break;
1985     case 8 :
1986       shift_amount = 3;
1987       break;
1988     default:
1989       shift_amount = -1;
1990       ShouldNotReachHere();
1991   }
1992 
1993   Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
1994   Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
1995   Address src_klass_addr = Address(src, oopDesc::klass_offset_in_bytes());
1996   Address dst_klass_addr = Address(dst, oopDesc::klass_offset_in_bytes());
1997 
1998   // Length and pos's are all sign extended at this point on 64bit.
1999 
2000   // test for NULL
2001   if (flags & LIR_OpArrayCopy::src_null_check) {
2002     __ compareU64_and_branch(src, (intptr_t)0, Assembler::bcondZero, *stub->entry());
2003   }
2004   if (flags & LIR_OpArrayCopy::dst_null_check) {
2005     __ compareU64_and_branch(dst, (intptr_t)0, Assembler::bcondZero, *stub->entry());
2006   }
2007 
2008   // Check if negative.
2009   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2010     __ compare32_and_branch(src_pos, (intptr_t)0, Assembler::bcondLow, *stub->entry());
2011   }
2012   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2013     __ compare32_and_branch(dst_pos, (intptr_t)0, Assembler::bcondLow, *stub->entry());
2014   }
2015 
2016   // If the compiler was not able to prove that exact type of the source or the destination
2017   // of the arraycopy is an array type, check at runtime if the source or the destination is
2018   // an instance type.
2019   if (flags & LIR_OpArrayCopy::type_check) {
2020     assert(Klass::_lh_neutral_value == 0, "or replace z_lt instructions");
2021 
2022     if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2023       __ load_klass(tmp, dst);
2024       __ z_lt(tmp, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2025       __ branch_optimized(Assembler::bcondNotLow, *stub->entry());
2026     }
2027 
2028     if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2029       __ load_klass(tmp, src);
2030       __ z_lt(tmp, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2031       __ branch_optimized(Assembler::bcondNotLow, *stub->entry());
2032     }
2033   }
2034 
2035   if (flags & LIR_OpArrayCopy::src_range_check) {
2036     __ z_la(tmp, Address(src_pos, length));
2037     __ z_cl(tmp, src_length_addr);
2038     __ branch_optimized(Assembler::bcondHigh, *stub->entry());
2039   }
2040   if (flags & LIR_OpArrayCopy::dst_range_check) {
2041     __ z_la(tmp, Address(dst_pos, length));
2042     __ z_cl(tmp, dst_length_addr);
2043     __ branch_optimized(Assembler::bcondHigh, *stub->entry());
2044   }
2045 
2046   if (flags & LIR_OpArrayCopy::length_positive_check) {
2047     __ z_ltr(length, length);
2048     __ branch_optimized(Assembler::bcondNegative, *stub->entry());
2049   }
2050 
2051   // Stubs require 64 bit values.
2052   __ z_lgfr(src_pos, src_pos); // int -> long
2053   __ z_lgfr(dst_pos, dst_pos); // int -> long
2054   __ z_lgfr(length, length);   // int -> long
2055 
2056   if (flags & LIR_OpArrayCopy::type_check) {
2057     // We don't know the array types are compatible.
2058     if (basic_type != T_OBJECT) {
2059       // Simple test for basic type arrays.
2060       if (UseCompressedClassPointers) {
2061         __ z_l(tmp, src_klass_addr);
2062         __ z_c(tmp, dst_klass_addr);
2063       } else {
2064         __ z_lg(tmp, src_klass_addr);
2065         __ z_cg(tmp, dst_klass_addr);
2066       }
2067       __ branch_optimized(Assembler::bcondNotEqual, *stub->entry());
2068     } else {
2069       // For object arrays, if src is a sub class of dst then we can
2070       // safely do the copy.
2071       NearLabel cont, slow;
2072       Register src_klass = Z_R1_scratch;
2073       Register dst_klass = Z_R10;
2074 
2075       __ load_klass(src_klass, src);
2076       __ load_klass(dst_klass, dst);
2077 
2078       __ check_klass_subtype_fast_path(src_klass, dst_klass, tmp, &cont, &slow, NULL);
2079 
2080       store_parameter(src_klass, 0); // sub
2081       store_parameter(dst_klass, 1); // super
2082       emit_call_c(Runtime1::entry_for (Runtime1::slow_subtype_check_id));
2083       CHECK_BAILOUT();
2084       // Sets condition code 0 for match (2 otherwise).
2085       __ branch_optimized(Assembler::bcondEqual, cont);
2086 
2087       __ bind(slow);
2088 
2089       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2090       if (copyfunc_addr != NULL) { // use stub if available
2091         // Src is not a sub class of dst so we have to do a
2092         // per-element check.
2093 
2094         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2095         if ((flags & mask) != mask) {
2096           // Check that at least both of them object arrays.
2097           assert(flags & mask, "one of the two should be known to be an object array");
2098 
2099           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2100             __ load_klass(tmp, src);
2101           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2102             __ load_klass(tmp, dst);
2103           }
2104           Address klass_lh_addr(tmp, Klass::layout_helper_offset());
2105           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2106           __ load_const_optimized(Z_R1_scratch, objArray_lh);
2107           __ z_c(Z_R1_scratch, klass_lh_addr);
2108           __ branch_optimized(Assembler::bcondNotEqual, *stub->entry());
2109         }
2110 
2111         // Save outgoing arguments in callee saved registers (C convention) in case
2112         // a call to System.arraycopy is needed.
2113         Register callee_saved_src     = Z_R10;
2114         Register callee_saved_src_pos = Z_R11;
2115         Register callee_saved_dst     = Z_R12;
2116         Register callee_saved_dst_pos = Z_R13;
2117         Register callee_saved_length  = Z_ARG5; // Z_ARG5 == Z_R6 is callee saved.
2118 
2119         __ lgr_if_needed(callee_saved_src, src);
2120         __ lgr_if_needed(callee_saved_src_pos, src_pos);
2121         __ lgr_if_needed(callee_saved_dst, dst);
2122         __ lgr_if_needed(callee_saved_dst_pos, dst_pos);
2123         __ lgr_if_needed(callee_saved_length, length);
2124 
2125         __ z_llgfr(length, length); // Higher 32bits must be null.
2126 
2127         __ z_sllg(Z_ARG1, src_pos, shift_amount); // index -> byte offset
2128         __ z_sllg(Z_ARG2, dst_pos, shift_amount); // index -> byte offset
2129 
2130         __ z_la(Z_ARG1, Address(src, Z_ARG1, arrayOopDesc::base_offset_in_bytes(basic_type)));
2131         assert_different_registers(Z_ARG1, dst, dst_pos, length);
2132         __ z_la(Z_ARG2, Address(dst, Z_ARG2, arrayOopDesc::base_offset_in_bytes(basic_type)));
2133         assert_different_registers(Z_ARG2, dst, length);
2134 
2135         __ z_lgr(Z_ARG3, length);
2136         assert_different_registers(Z_ARG3, dst);
2137 
2138         __ load_klass(Z_ARG5, dst);
2139         __ z_lg(Z_ARG5, Address(Z_ARG5, ObjArrayKlass::element_klass_offset()));
2140         __ z_lg(Z_ARG4, Address(Z_ARG5, Klass::super_check_offset_offset()));
2141         emit_call_c(copyfunc_addr);
2142         CHECK_BAILOUT();
2143 
2144 #ifndef PRODUCT
2145         if (PrintC1Statistics) {
2146           NearLabel failed;
2147           __ compareU32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondNotEqual, failed);
2148           __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_checkcast_cnt);
2149           __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2150           __ bind(failed);
2151         }
2152 #endif
2153 
2154         __ compareU32_and_branch(Z_RET, (intptr_t)0, Assembler::bcondEqual, *stub->continuation());
2155 
2156 #ifndef PRODUCT
2157         if (PrintC1Statistics) {
2158           __ load_const_optimized(Z_R1_scratch, (address)&Runtime1::_arraycopy_checkcast_attempt_cnt);
2159           __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2160         }
2161 #endif
2162 
2163         __ z_lgr(tmp, Z_RET);
2164         __ z_xilf(tmp, -1);
2165 
2166         // Restore previously spilled arguments
2167         __ lgr_if_needed(src, callee_saved_src);
2168         __ lgr_if_needed(src_pos, callee_saved_src_pos);
2169         __ lgr_if_needed(dst, callee_saved_dst);
2170         __ lgr_if_needed(dst_pos, callee_saved_dst_pos);
2171         __ lgr_if_needed(length, callee_saved_length);
2172 
2173         __ z_sr(length, tmp);
2174         __ z_ar(src_pos, tmp);
2175         __ z_ar(dst_pos, tmp);
2176       }
2177 
2178       __ branch_optimized(Assembler::bcondAlways, *stub->entry());
2179 
2180       __ bind(cont);
2181     }
2182   }
2183 
2184 #ifdef ASSERT
2185   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2186     // Sanity check the known type with the incoming class. For the
2187     // primitive case the types must match exactly with src.klass and
2188     // dst.klass each exactly matching the default type. For the
2189     // object array case, if no type check is needed then either the
2190     // dst type is exactly the expected type and the src type is a
2191     // subtype which we can't check or src is the same array as dst
2192     // but not necessarily exactly of type default_type.
2193     NearLabel known_ok, halt;
2194     metadata2reg(default_type->constant_encoding(), tmp);
2195     if (UseCompressedClassPointers) {
2196       __ encode_klass_not_null(tmp);
2197     }
2198 
2199     if (basic_type != T_OBJECT) {
2200       if (UseCompressedClassPointers)         { __ z_c (tmp, dst_klass_addr); }
2201       else                                    { __ z_cg(tmp, dst_klass_addr); }
2202       __ branch_optimized(Assembler::bcondNotEqual, halt);
2203       if (UseCompressedClassPointers)         { __ z_c (tmp, src_klass_addr); }
2204       else                                    { __ z_cg(tmp, src_klass_addr); }
2205       __ branch_optimized(Assembler::bcondEqual, known_ok);
2206     } else {
2207       if (UseCompressedClassPointers)         { __ z_c (tmp, dst_klass_addr); }
2208       else                                    { __ z_cg(tmp, dst_klass_addr); }
2209       __ branch_optimized(Assembler::bcondEqual, known_ok);
2210       __ compareU64_and_branch(src, dst, Assembler::bcondEqual, known_ok);
2211     }
2212     __ bind(halt);
2213     __ stop("incorrect type information in arraycopy");
2214     __ bind(known_ok);
2215   }
2216 #endif
2217 
2218 #ifndef PRODUCT
2219   if (PrintC1Statistics) {
2220     __ load_const_optimized(Z_R1_scratch, Runtime1::arraycopy_count_address(basic_type));
2221     __ add2mem_32(Address(Z_R1_scratch), 1, Z_R0_scratch);
2222   }
2223 #endif
2224 
2225   __ z_sllg(tmp, src_pos, shift_amount); // index -> byte offset
2226   __ z_sllg(Z_R1_scratch, dst_pos, shift_amount); // index -> byte offset
2227 
2228   assert_different_registers(Z_ARG1, dst, dst_pos, length);
2229   __ z_la(Z_ARG1, Address(src, tmp, arrayOopDesc::base_offset_in_bytes(basic_type)));
2230   assert_different_registers(Z_ARG2, length);
2231   __ z_la(Z_ARG2, Address(dst, Z_R1_scratch, arrayOopDesc::base_offset_in_bytes(basic_type)));
2232   __ lgr_if_needed(Z_ARG3, length);
2233 
2234   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2235   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2236   const char *name;
2237   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2238   __ call_VM_leaf(entry);
2239 
2240   __ bind(*stub->continuation());
2241 }
2242 
2243 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2244   if (dest->is_single_cpu()) {
2245     if (left->type() == T_OBJECT) {
2246       switch (code) {
2247         case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2248         case lir_shr:  __ z_srag (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2249         case lir_ushr: __ z_srlg (dest->as_register(), left->as_register(), 0, count->as_register()); break;
2250         default: ShouldNotReachHere();
2251       }
2252     } else {
2253       assert(code == lir_shl || left == dest, "left and dest must be equal for 2 operand form right shifts");
2254       Register masked_count = Z_R1_scratch;
2255       __ z_lr(masked_count, count->as_register());
2256       __ z_nill(masked_count, 31);
2257       switch (code) {
2258         case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), 0, masked_count); break;
2259         case lir_shr:  __ z_sra  (dest->as_register(), 0, masked_count); break;
2260         case lir_ushr: __ z_srl  (dest->as_register(), 0, masked_count); break;
2261         default: ShouldNotReachHere();
2262       }
2263     }
2264   } else {
2265     switch (code) {
2266       case lir_shl:  __ z_sllg (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2267       case lir_shr:  __ z_srag (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2268       case lir_ushr: __ z_srlg (dest->as_register_lo(), left->as_register_lo(), 0, count->as_register()); break;
2269       default: ShouldNotReachHere();
2270     }
2271   }
2272 }
2273 
2274 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2275   if (left->type() == T_OBJECT) {
2276     count = count & 63;  // Shouldn't shift by more than sizeof(intptr_t).
2277     Register l = left->as_register();
2278     Register d = dest->as_register_lo();
2279     switch (code) {
2280       case lir_shl:  __ z_sllg (d, l, count); break;
2281       case lir_shr:  __ z_srag (d, l, count); break;
2282       case lir_ushr: __ z_srlg (d, l, count); break;
2283       default: ShouldNotReachHere();
2284     }
2285     return;
2286   }
2287   if (dest->is_single_cpu()) {
2288     assert(code == lir_shl || left == dest, "left and dest must be equal for 2 operand form right shifts");
2289     count = count & 0x1F; // Java spec
2290     switch (code) {
2291       case lir_shl:  __ z_sllg (dest->as_register(), left->as_register(), count); break;
2292       case lir_shr:  __ z_sra  (dest->as_register(), count); break;
2293       case lir_ushr: __ z_srl  (dest->as_register(), count); break;
2294       default: ShouldNotReachHere();
2295     }
2296   } else if (dest->is_double_cpu()) {
2297     count = count & 63; // Java spec
2298     Register l = left->as_pointer_register();
2299     Register d = dest->as_pointer_register();
2300     switch (code) {
2301       case lir_shl:  __ z_sllg (d, l, count); break;
2302       case lir_shr:  __ z_srag (d, l, count); break;
2303       case lir_ushr: __ z_srlg (d, l, count); break;
2304       default: ShouldNotReachHere();
2305     }
2306   } else {
2307     ShouldNotReachHere();
2308   }
2309 }
2310 
2311 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
2312   if (op->init_check()) {
2313     // Make sure klass is initialized & doesn't have finalizer.
2314     const int state_offset = in_bytes(InstanceKlass::init_state_offset());
2315     Register iklass = op->klass()->as_register();
2316     add_debug_info_for_null_check_here(op->stub()->info());
2317     if (Immediate::is_uimm12(state_offset)) {
2318       __ z_cli(state_offset, iklass, InstanceKlass::fully_initialized);
2319     } else {
2320       __ z_cliy(state_offset, iklass, InstanceKlass::fully_initialized);
2321     }
2322     __ branch_optimized(Assembler::bcondNotEqual, *op->stub()->entry()); // Use long branch, because slow_case might be far.
2323   }
2324   __ allocate_object(op->obj()->as_register(),
2325                      op->tmp1()->as_register(),
2326                      op->tmp2()->as_register(),
2327                      op->header_size(),
2328                      op->object_size(),
2329                      op->klass()->as_register(),
2330                      *op->stub()->entry());
2331   __ bind(*op->stub()->continuation());
2332   __ verify_oop(op->obj()->as_register());
2333 }
2334 
2335 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
2336   Register len = op->len()->as_register();
2337   __ move_reg_if_needed(len, T_LONG, len, T_INT); // sign extend
2338 
2339   if (UseSlowPath ||
2340       (!UseFastNewObjectArray && (op->type() == T_OBJECT || op->type() == T_ARRAY)) ||
2341       (!UseFastNewTypeArray   && (op->type() != T_OBJECT && op->type() != T_ARRAY))) {
2342     __ z_brul(*op->stub()->entry());
2343   } else {
2344     __ allocate_array(op->obj()->as_register(),
2345                       op->len()->as_register(),
2346                       op->tmp1()->as_register(),
2347                       op->tmp2()->as_register(),
2348                       arrayOopDesc::header_size(op->type()),
2349                       type2aelembytes(op->type()),
2350                       op->klass()->as_register(),
2351                       *op->stub()->entry());
2352   }
2353   __ bind(*op->stub()->continuation());
2354 }
2355 
2356 void LIR_Assembler::type_profile_helper(Register mdo, ciMethodData *md, ciProfileData *data,
2357                                         Register recv, Register tmp1, Label* update_done) {
2358   uint i;
2359   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2360     Label next_test;
2361     // See if the receiver is receiver[n].
2362     Address receiver_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)));
2363     __ z_cg(recv, receiver_addr);
2364     __ z_brne(next_test);
2365     Address data_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)));
2366     __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2367     __ branch_optimized(Assembler::bcondAlways, *update_done);
2368     __ bind(next_test);
2369   }
2370 
2371   // Didn't find receiver; find next empty slot and fill it in.
2372   for (i = 0; i < VirtualCallData::row_limit(); i++) {
2373     Label next_test;
2374     Address recv_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)));
2375     __ z_ltg(Z_R0_scratch, recv_addr);
2376     __ z_brne(next_test);
2377     __ z_stg(recv, recv_addr);
2378     __ load_const_optimized(tmp1, DataLayout::counter_increment);
2379     __ z_stg(tmp1, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)), mdo);
2380     __ branch_optimized(Assembler::bcondAlways, *update_done);
2381     __ bind(next_test);
2382   }
2383 }
2384 
2385 void LIR_Assembler::setup_md_access(ciMethod* method, int bci,
2386                                     ciMethodData*& md, ciProfileData*& data, int& mdo_offset_bias) {
2387   Unimplemented();
2388 }
2389 
2390 void LIR_Assembler::store_parameter(Register r, int param_num) {
2391   assert(param_num >= 0, "invalid num");
2392   int offset_in_bytes = param_num * BytesPerWord + FrameMap::first_available_sp_in_frame;
2393   assert(offset_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2394   __ z_stg(r, offset_in_bytes, Z_SP);
2395 }
2396 
2397 void LIR_Assembler::store_parameter(jint c, int param_num) {
2398   assert(param_num >= 0, "invalid num");
2399   int offset_in_bytes = param_num * BytesPerWord + FrameMap::first_available_sp_in_frame;
2400   assert(offset_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2401   __ store_const(Address(Z_SP, offset_in_bytes), c, Z_R1_scratch, true);
2402 }
2403 
2404 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
2405   // We always need a stub for the failure case.
2406   CodeStub* stub = op->stub();
2407   Register obj = op->object()->as_register();
2408   Register k_RInfo = op->tmp1()->as_register();
2409   Register klass_RInfo = op->tmp2()->as_register();
2410   Register dst = op->result_opr()->as_register();
2411   Register Rtmp1 = Z_R1_scratch;
2412   ciKlass* k = op->klass();
2413 
2414   assert(!op->tmp3()->is_valid(), "tmp3's not needed");
2415 
2416   // Check if it needs to be profiled.
2417   ciMethodData* md = NULL;
2418   ciProfileData* data = NULL;
2419 
2420   if (op->should_profile()) {
2421     ciMethod* method = op->profiled_method();
2422     assert(method != NULL, "Should have method");
2423     int bci = op->profiled_bci();
2424     md = method->method_data_or_null();
2425     assert(md != NULL, "Sanity");
2426     data = md->bci_to_data(bci);
2427     assert(data != NULL,                "need data for type check");
2428     assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2429   }
2430 
2431   // Temp operands do not overlap with inputs, if this is their last
2432   // use (end of range is exclusive), so a register conflict is possible.
2433   if (obj == k_RInfo) {
2434     k_RInfo = dst;
2435   } else if (obj == klass_RInfo) {
2436     klass_RInfo = dst;
2437   }
2438   assert_different_registers(obj, k_RInfo, klass_RInfo);
2439 
2440   if (op->should_profile()) {
2441     NearLabel not_null;
2442     __ compareU64_and_branch(obj, (intptr_t) 0, Assembler::bcondNotEqual, not_null);
2443     // Object is null; update MDO and exit.
2444     Register mdo = klass_RInfo;
2445     metadata2reg(md->constant_encoding(), mdo);
2446     Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
2447     int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
2448     __ or2mem_8(data_addr, header_bits);
2449     __ branch_optimized(Assembler::bcondAlways, *obj_is_null);
2450     __ bind(not_null);
2451   } else {
2452     __ compareU64_and_branch(obj, (intptr_t) 0, Assembler::bcondEqual, *obj_is_null);
2453   }
2454 
2455   NearLabel profile_cast_failure, profile_cast_success;
2456   Label *failure_target = op->should_profile() ? &profile_cast_failure : failure;
2457   Label *success_target = op->should_profile() ? &profile_cast_success : success;
2458 
2459   // Patching may screw with our temporaries on sparc,
2460   // so let's do it before loading the class.
2461   if (k->is_loaded()) {
2462     metadata2reg(k->constant_encoding(), k_RInfo);
2463   } else {
2464     klass2reg_with_patching(k_RInfo, op->info_for_patch());
2465   }
2466   assert(obj != k_RInfo, "must be different");
2467 
2468   __ verify_oop(obj);
2469 
2470   // Get object class.
2471   // Not a safepoint as obj null check happens earlier.
2472   if (op->fast_check()) {
2473     if (UseCompressedClassPointers) {
2474       __ load_klass(klass_RInfo, obj);
2475       __ compareU64_and_branch(k_RInfo, klass_RInfo, Assembler::bcondNotEqual, *failure_target);
2476     } else {
2477       __ z_cg(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
2478       __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2479     }
2480     // Successful cast, fall through to profile or jump.
2481   } else {
2482     bool need_slow_path = !k->is_loaded() ||
2483                           ((int) k->super_check_offset() == in_bytes(Klass::secondary_super_cache_offset()));
2484     intptr_t super_check_offset = k->is_loaded() ? k->super_check_offset() : -1L;
2485     __ load_klass(klass_RInfo, obj);
2486     // Perform the fast part of the checking logic.
2487     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1,
2488                                      (need_slow_path ? success_target : NULL),
2489                                      failure_target, NULL,
2490                                      RegisterOrConstant(super_check_offset));
2491     if (need_slow_path) {
2492       // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2493       address a = Runtime1::entry_for (Runtime1::slow_subtype_check_id);
2494       store_parameter(klass_RInfo, 0); // sub
2495       store_parameter(k_RInfo, 1);     // super
2496       emit_call_c(a); // Sets condition code 0 for match (2 otherwise).
2497       CHECK_BAILOUT();
2498       __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2499       // Fall through to success case.
2500     }
2501   }
2502 
2503   if (op->should_profile()) {
2504     Register mdo = klass_RInfo, recv = k_RInfo;
2505     assert_different_registers(obj, mdo, recv);
2506     __ bind(profile_cast_success);
2507     metadata2reg(md->constant_encoding(), mdo);
2508     __ load_klass(recv, obj);
2509     type_profile_helper(mdo, md, data, recv, Rtmp1, success);
2510     __ branch_optimized(Assembler::bcondAlways, *success);
2511 
2512     __ bind(profile_cast_failure);
2513     metadata2reg(md->constant_encoding(), mdo);
2514     __ add2mem_64(Address(mdo, md->byte_offset_of_slot(data, CounterData::count_offset())), -(int)DataLayout::counter_increment, Rtmp1);
2515     __ branch_optimized(Assembler::bcondAlways, *failure);
2516   } else {
2517     __ branch_optimized(Assembler::bcondAlways, *success);
2518   }
2519 }
2520 
2521 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
2522   LIR_Code code = op->code();
2523   if (code == lir_store_check) {
2524     Register value = op->object()->as_register();
2525     Register array = op->array()->as_register();
2526     Register k_RInfo = op->tmp1()->as_register();
2527     Register klass_RInfo = op->tmp2()->as_register();
2528     Register Rtmp1 = Z_R1_scratch;
2529 
2530     CodeStub* stub = op->stub();
2531 
2532     // Check if it needs to be profiled.
2533     ciMethodData* md = NULL;
2534     ciProfileData* data = NULL;
2535 
2536     assert_different_registers(value, k_RInfo, klass_RInfo);
2537 
2538     if (op->should_profile()) {
2539       ciMethod* method = op->profiled_method();
2540       assert(method != NULL, "Should have method");
2541       int bci = op->profiled_bci();
2542       md = method->method_data_or_null();
2543       assert(md != NULL, "Sanity");
2544       data = md->bci_to_data(bci);
2545       assert(data != NULL,                "need data for type check");
2546       assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
2547     }
2548     NearLabel profile_cast_success, profile_cast_failure, done;
2549     Label *success_target = op->should_profile() ? &profile_cast_success : &done;
2550     Label *failure_target = op->should_profile() ? &profile_cast_failure : stub->entry();
2551 
2552     if (op->should_profile()) {
2553       NearLabel not_null;
2554       __ compareU64_and_branch(value, (intptr_t) 0, Assembler::bcondNotEqual, not_null);
2555       // Object is null; update MDO and exit.
2556       Register mdo = klass_RInfo;
2557       metadata2reg(md->constant_encoding(), mdo);
2558       Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::header_offset()));
2559       int header_bits = DataLayout::flag_mask_to_header_mask(BitData::null_seen_byte_constant());
2560       __ or2mem_8(data_addr, header_bits);
2561       __ branch_optimized(Assembler::bcondAlways, done);
2562       __ bind(not_null);
2563     } else {
2564       __ compareU64_and_branch(value, (intptr_t) 0, Assembler::bcondEqual, done);
2565     }
2566 
2567     add_debug_info_for_null_check_here(op->info_for_exception());
2568     __ load_klass(k_RInfo, array);
2569     __ load_klass(klass_RInfo, value);
2570 
2571     // Get instance klass (it's already uncompressed).
2572     __ z_lg(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
2573     // Perform the fast part of the checking logic.
2574     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
2575     // Call out-of-line instance of __ check_klass_subtype_slow_path(...):
2576     address a = Runtime1::entry_for (Runtime1::slow_subtype_check_id);
2577     store_parameter(klass_RInfo, 0); // sub
2578     store_parameter(k_RInfo, 1);     // super
2579     emit_call_c(a); // Sets condition code 0 for match (2 otherwise).
2580     CHECK_BAILOUT();
2581     __ branch_optimized(Assembler::bcondNotEqual, *failure_target);
2582     // Fall through to success case.
2583 
2584     if (op->should_profile()) {
2585       Register mdo = klass_RInfo, recv = k_RInfo;
2586       assert_different_registers(value, mdo, recv);
2587       __ bind(profile_cast_success);
2588       metadata2reg(md->constant_encoding(), mdo);
2589       __ load_klass(recv, value);
2590       type_profile_helper(mdo, md, data, recv, Rtmp1, &done);
2591       __ branch_optimized(Assembler::bcondAlways, done);
2592 
2593       __ bind(profile_cast_failure);
2594       metadata2reg(md->constant_encoding(), mdo);
2595       __ add2mem_64(Address(mdo, md->byte_offset_of_slot(data, CounterData::count_offset())), -(int)DataLayout::counter_increment, Rtmp1);
2596       __ branch_optimized(Assembler::bcondAlways, *stub->entry());
2597     }
2598 
2599     __ bind(done);
2600   } else {
2601     if (code == lir_checkcast) {
2602       Register obj = op->object()->as_register();
2603       Register dst = op->result_opr()->as_register();
2604       NearLabel success;
2605       emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
2606       __ bind(success);
2607       __ lgr_if_needed(dst, obj);
2608     } else {
2609       if (code == lir_instanceof) {
2610         Register obj = op->object()->as_register();
2611         Register dst = op->result_opr()->as_register();
2612         NearLabel success, failure, done;
2613         emit_typecheck_helper(op, &success, &failure, &failure);
2614         __ bind(failure);
2615         __ clear_reg(dst);
2616         __ branch_optimized(Assembler::bcondAlways, done);
2617         __ bind(success);
2618         __ load_const_optimized(dst, 1);
2619         __ bind(done);
2620       } else {
2621         ShouldNotReachHere();
2622       }
2623     }
2624   }
2625 }
2626 
2627 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
2628   Register addr = op->addr()->as_pointer_register();
2629   Register t1_cmp = Z_R1_scratch;
2630   if (op->code() == lir_cas_long) {
2631     assert(VM_Version::supports_cx8(), "wrong machine");
2632     Register cmp_value_lo = op->cmp_value()->as_register_lo();
2633     Register new_value_lo = op->new_value()->as_register_lo();
2634     __ z_lgr(t1_cmp, cmp_value_lo);
2635     // Perform the compare and swap operation.
2636     __ z_csg(t1_cmp, new_value_lo, 0, addr);
2637   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj) {
2638     Register cmp_value = op->cmp_value()->as_register();
2639     Register new_value = op->new_value()->as_register();
2640     if (op->code() == lir_cas_obj) {
2641       if (UseCompressedOops) {
2642                  t1_cmp = op->tmp1()->as_register();
2643         Register t2_new = op->tmp2()->as_register();
2644         assert_different_registers(cmp_value, new_value, addr, t1_cmp, t2_new);
2645         __ oop_encoder(t1_cmp, cmp_value, true /*maybe null*/);
2646         __ oop_encoder(t2_new, new_value, true /*maybe null*/);
2647         __ z_cs(t1_cmp, t2_new, 0, addr);
2648       } else {
2649         __ z_lgr(t1_cmp, cmp_value);
2650         __ z_csg(t1_cmp, new_value, 0, addr);
2651       }
2652     } else {
2653       __ z_lr(t1_cmp, cmp_value);
2654       __ z_cs(t1_cmp, new_value, 0, addr);
2655     }
2656   } else {
2657     ShouldNotReachHere(); // new lir_cas_??
2658   }
2659 }
2660 
2661 void LIR_Assembler::set_24bit_FPU() {
2662   ShouldNotCallThis(); // x86 only
2663 }
2664 
2665 void LIR_Assembler::reset_FPU() {
2666   ShouldNotCallThis(); // x86 only
2667 }
2668 
2669 void LIR_Assembler::breakpoint() {
2670   Unimplemented();
2671   //  __ breakpoint_trap();
2672 }
2673 
2674 void LIR_Assembler::push(LIR_Opr opr) {
2675   ShouldNotCallThis(); // unused
2676 }
2677 
2678 void LIR_Assembler::pop(LIR_Opr opr) {
2679   ShouldNotCallThis(); // unused
2680 }
2681 
2682 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst_opr) {
2683   Address addr = frame_map()->address_for_monitor_lock(monitor_no);
2684   __ add2reg(dst_opr->as_register(), addr.disp(), addr.base());
2685 }
2686 
2687 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2688   Register obj = op->obj_opr()->as_register();  // May not be an oop.
2689   Register hdr = op->hdr_opr()->as_register();
2690   Register lock = op->lock_opr()->as_register();
2691   if (!UseFastLocking) {
2692     __ branch_optimized(Assembler::bcondAlways, *op->stub()->entry());
2693   } else if (op->code() == lir_lock) {
2694     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2695     // Add debug info for NullPointerException only if one is possible.
2696     if (op->info() != NULL) {
2697       add_debug_info_for_null_check_here(op->info());
2698     }
2699     __ lock_object(hdr, obj, lock, *op->stub()->entry());
2700     // done
2701   } else if (op->code() == lir_unlock) {
2702     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2703     __ unlock_object(hdr, obj, lock, *op->stub()->entry());
2704   } else {
2705     ShouldNotReachHere();
2706   }
2707   __ bind(*op->stub()->continuation());
2708 }
2709 
2710 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2711   ciMethod* method = op->profiled_method();
2712   int bci          = op->profiled_bci();
2713   ciMethod* callee = op->profiled_callee();
2714 
2715   // Update counter for all call types.
2716   ciMethodData* md = method->method_data_or_null();
2717   assert(md != NULL, "Sanity");
2718   ciProfileData* data = md->bci_to_data(bci);
2719   assert(data != NULL && data->is_CounterData(), "need CounterData for calls");
2720   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
2721   Register mdo  = op->mdo()->as_register();
2722   assert(op->tmp1()->is_double_cpu(), "tmp1 must be allocated");
2723   Register tmp1 = op->tmp1()->as_register_lo();
2724   metadata2reg(md->constant_encoding(), mdo);
2725 
2726   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
2727   // Perform additional virtual call profiling for invokevirtual and
2728   // invokeinterface bytecodes
2729   if (op->should_profile_receiver_type()) {
2730     assert(op->recv()->is_single_cpu(), "recv must be allocated");
2731     Register recv = op->recv()->as_register();
2732     assert_different_registers(mdo, tmp1, recv);
2733     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2734     ciKlass* known_klass = op->known_holder();
2735     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
2736       // We know the type that will be seen at this call site; we can
2737       // statically update the MethodData* rather than needing to do
2738       // dynamic tests on the receiver type.
2739 
2740       // NOTE: we should probably put a lock around this search to
2741       // avoid collisions by concurrent compilations.
2742       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2743       uint i;
2744       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2745         ciKlass* receiver = vc_data->receiver(i);
2746         if (known_klass->equals(receiver)) {
2747           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2748           __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2749           return;
2750         }
2751       }
2752 
2753       // Receiver type not found in profile data. Select an empty slot.
2754 
2755       // Note that this is less efficient than it should be because it
2756       // always does a write to the receiver part of the
2757       // VirtualCallData rather than just the first time.
2758       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2759         ciKlass* receiver = vc_data->receiver(i);
2760         if (receiver == NULL) {
2761           Address recv_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)));
2762           metadata2reg(known_klass->constant_encoding(), tmp1);
2763           __ z_stg(tmp1, recv_addr);
2764           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2765           __ add2mem_64(data_addr, DataLayout::counter_increment, tmp1);
2766           return;
2767         }
2768       }
2769     } else {
2770       __ load_klass(recv, recv);
2771       NearLabel update_done;
2772       type_profile_helper(mdo, md, data, recv, tmp1, &update_done);
2773       // Receiver did not match any saved receiver and there is no empty row for it.
2774       // Increment total counter to indicate polymorphic case.
2775       __ add2mem_64(counter_addr, DataLayout::counter_increment, tmp1);
2776       __ bind(update_done);
2777     }
2778   } else {
2779     // static call
2780     __ add2mem_64(counter_addr, DataLayout::counter_increment, tmp1);
2781   }
2782 }
2783 
2784 void LIR_Assembler::align_backward_branch_target() {
2785   __ align(OptoLoopAlignment);
2786 }
2787 
2788 void LIR_Assembler::emit_delay(LIR_OpDelay* op) {
2789   ShouldNotCallThis(); // There are no delay slots on ZARCH_64.
2790 }
2791 
2792 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest) {
2793   assert(left->is_register(), "can only handle registers");
2794 
2795   if (left->is_single_cpu()) {
2796     __ z_lcr(dest->as_register(), left->as_register());
2797   } else if (left->is_single_fpu()) {
2798     __ z_lcebr(dest->as_float_reg(), left->as_float_reg());
2799   } else if (left->is_double_fpu()) {
2800     __ z_lcdbr(dest->as_double_reg(), left->as_double_reg());
2801   } else {
2802     assert(left->is_double_cpu(), "Must be a long");
2803     __ z_lcgr(dest->as_register_lo(), left->as_register_lo());
2804   }
2805 }
2806 
2807 void LIR_Assembler::fxch(int i) {
2808   ShouldNotCallThis(); // x86 only
2809 }
2810 
2811 void LIR_Assembler::fld(int i) {
2812   ShouldNotCallThis(); // x86 only
2813 }
2814 
2815 void LIR_Assembler::ffree(int i) {
2816   ShouldNotCallThis(); // x86 only
2817 }
2818 
2819 void LIR_Assembler::rt_call(LIR_Opr result, address dest,
2820                             const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
2821   assert(!tmp->is_valid(), "don't need temporary");
2822   emit_call_c(dest);
2823   CHECK_BAILOUT();
2824   if (info != NULL) {
2825     add_call_info_here(info);
2826   }
2827 }
2828 
2829 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
2830   ShouldNotCallThis(); // not needed on ZARCH_64
2831 }
2832 
2833 void LIR_Assembler::membar() {
2834   __ z_fence();
2835 }
2836 
2837 void LIR_Assembler::membar_acquire() {
2838   __ z_acquire();
2839 }
2840 
2841 void LIR_Assembler::membar_release() {
2842   __ z_release();
2843 }
2844 
2845 void LIR_Assembler::membar_loadload() {
2846   __ z_acquire();
2847 }
2848 
2849 void LIR_Assembler::membar_storestore() {
2850   __ z_release();
2851 }
2852 
2853 void LIR_Assembler::membar_loadstore() {
2854   __ z_acquire();
2855 }
2856 
2857 void LIR_Assembler::membar_storeload() {
2858   __ z_fence();
2859 }
2860 
2861 void LIR_Assembler::on_spin_wait() {
2862   Unimplemented();
2863 }
2864 
2865 void LIR_Assembler::leal(LIR_Opr addr_opr, LIR_Opr dest) {
2866   LIR_Address* addr = addr_opr->as_address_ptr();
2867   assert(addr->scale() == LIR_Address::times_1, "scaling unsupported");
2868   __ load_address(dest->as_pointer_register(), as_Address(addr));
2869 }
2870 
2871 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
2872   ShouldNotCallThis(); // unused
2873 }
2874 
2875 #ifdef ASSERT
2876 // Emit run-time assertion.
2877 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
2878   Unimplemented();
2879 }
2880 #endif
2881 
2882 void LIR_Assembler::peephole(LIR_List*) {
2883   // Do nothing for now.
2884 }
2885 
2886 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
2887   assert(code == lir_xadd, "lir_xchg not supported");
2888   Address src_addr = as_Address(src->as_address_ptr());
2889   Register base = src_addr.base();
2890   intptr_t disp = src_addr.disp();
2891   if (src_addr.index()->is_valid()) {
2892     // LAA and LAAG do not support index register.
2893     __ load_address(Z_R1_scratch, src_addr);
2894     base = Z_R1_scratch;
2895     disp = 0;
2896   }
2897   if (data->type() == T_INT) {
2898     __ z_laa(dest->as_register(), data->as_register(), disp, base);
2899   } else if (data->type() == T_LONG) {
2900     assert(data->as_register_lo() == data->as_register_hi(), "should be a single register");
2901     __ z_laag(dest->as_register_lo(), data->as_register_lo(), disp, base);
2902   } else {
2903     ShouldNotReachHere();
2904   }
2905 }
2906 
2907 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2908   Register obj = op->obj()->as_register();
2909   Register tmp1 = op->tmp()->as_pointer_register();
2910   Register tmp2 = Z_R1_scratch;
2911   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2912   ciKlass* exact_klass = op->exact_klass();
2913   intptr_t current_klass = op->current_klass();
2914   bool not_null = op->not_null();
2915   bool no_conflict = op->no_conflict();
2916 
2917   Label update, next, none, null_seen, init_klass;
2918 
2919   bool do_null = !not_null;
2920   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2921   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
2922 
2923   assert(do_null || do_update, "why are we here?");
2924   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
2925 
2926   __ verify_oop(obj);
2927 
2928   if (do_null || tmp1 != obj DEBUG_ONLY(|| true)) {
2929     __ z_ltgr(tmp1, obj);
2930   }
2931   if (do_null) {
2932     __ z_brnz(update);
2933     if (!TypeEntries::was_null_seen(current_klass)) {
2934       __ z_lg(tmp1, mdo_addr);
2935       __ z_oill(tmp1, TypeEntries::null_seen);
2936       __ z_stg(tmp1, mdo_addr);
2937     }
2938     if (do_update) {
2939       __ z_bru(next);
2940     }
2941   } else {
2942     __ asm_assert_ne("unexpect null obj", __LINE__);
2943   }
2944 
2945   __ bind(update);
2946 
2947   if (do_update) {
2948 #ifdef ASSERT
2949     if (exact_klass != NULL) {
2950       __ load_klass(tmp1, tmp1);
2951       metadata2reg(exact_klass->constant_encoding(), tmp2);
2952       __ z_cgr(tmp1, tmp2);
2953       __ asm_assert_eq("exact klass and actual klass differ", __LINE__);
2954     }
2955 #endif
2956 
2957     Label do_update;
2958     __ z_lg(tmp2, mdo_addr);
2959 
2960     if (!no_conflict) {
2961       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
2962         if (exact_klass != NULL) {
2963           metadata2reg(exact_klass->constant_encoding(), tmp1);
2964         } else {
2965           __ load_klass(tmp1, tmp1);
2966         }
2967 
2968         // Klass seen before: nothing to do (regardless of unknown bit).
2969         __ z_lgr(Z_R0_scratch, tmp2);
2970         assert(Immediate::is_uimm(~TypeEntries::type_klass_mask, 16), "or change following instruction");
2971         __ z_nill(Z_R0_scratch, TypeEntries::type_klass_mask & 0xFFFF);
2972         __ compareU64_and_branch(Z_R0_scratch, tmp1, Assembler::bcondEqual, next);
2973 
2974         // Already unknown: Nothing to do anymore.
2975         __ z_tmll(tmp2, TypeEntries::type_unknown);
2976         __ z_brc(Assembler::bcondAllOne, next);
2977 
2978         if (TypeEntries::is_type_none(current_klass)) {
2979           __ z_lgr(Z_R0_scratch, tmp2);
2980           assert(Immediate::is_uimm(~TypeEntries::type_mask, 16), "or change following instruction");
2981           __ z_nill(Z_R0_scratch, TypeEntries::type_mask & 0xFFFF);
2982           __ compareU64_and_branch(Z_R0_scratch, (intptr_t)0, Assembler::bcondEqual, init_klass);
2983         }
2984       } else {
2985         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
2986                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
2987 
2988         // Already unknown: Nothing to do anymore.
2989         __ z_tmll(tmp2, TypeEntries::type_unknown);
2990         __ z_brc(Assembler::bcondAllOne, next);
2991       }
2992 
2993       // Different than before. Cannot keep accurate profile.
2994       __ z_oill(tmp2, TypeEntries::type_unknown);
2995       __ z_bru(do_update);
2996     } else {
2997       // There's a single possible klass at this profile point.
2998       assert(exact_klass != NULL, "should be");
2999       if (TypeEntries::is_type_none(current_klass)) {
3000         metadata2reg(exact_klass->constant_encoding(), tmp1);
3001         __ z_lgr(Z_R0_scratch, tmp2);
3002         assert(Immediate::is_uimm(~TypeEntries::type_klass_mask, 16), "or change following instruction");
3003         __ z_nill(Z_R0_scratch, TypeEntries::type_klass_mask & 0xFFFF);
3004         __ compareU64_and_branch(Z_R0_scratch, tmp1, Assembler::bcondEqual, next);
3005 #ifdef ASSERT
3006         {
3007           Label ok;
3008           __ z_lgr(Z_R0_scratch, tmp2);
3009           assert(Immediate::is_uimm(~TypeEntries::type_mask, 16), "or change following instruction");
3010           __ z_nill(Z_R0_scratch, TypeEntries::type_mask & 0xFFFF);
3011           __ compareU64_and_branch(Z_R0_scratch, (intptr_t)0, Assembler::bcondEqual, ok);
3012           __ stop("unexpected profiling mismatch");
3013           __ bind(ok);
3014         }
3015 #endif
3016 
3017       } else {
3018         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3019                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3020 
3021         // Already unknown: Nothing to do anymore.
3022         __ z_tmll(tmp2, TypeEntries::type_unknown);
3023         __ z_brc(Assembler::bcondAllOne, next);
3024         __ z_oill(tmp2, TypeEntries::type_unknown);
3025         __ z_bru(do_update);
3026       }
3027     }
3028 
3029     __ bind(init_klass);
3030     // Combine klass and null_seen bit (only used if (tmp & type_mask)==0).
3031     __ z_ogr(tmp2, tmp1);
3032 
3033     __ bind(do_update);
3034     __ z_stg(tmp2, mdo_addr);
3035 
3036     __ bind(next);
3037   }
3038 }
3039 
3040 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
3041   assert(op->crc()->is_single_cpu(), "crc must be register");
3042   assert(op->val()->is_single_cpu(), "byte value must be register");
3043   assert(op->result_opr()->is_single_cpu(), "result must be register");
3044   Register crc = op->crc()->as_register();
3045   Register val = op->val()->as_register();
3046   Register res = op->result_opr()->as_register();
3047 
3048   assert_different_registers(val, crc, res);
3049 
3050   __ load_const_optimized(res, StubRoutines::crc_table_addr());
3051   __ kernel_crc32_singleByteReg(crc, val, res, true);
3052   __ z_lgfr(res, crc);
3053 }
3054 
3055 #undef __