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