1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.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 "ci/ciValueKlass.hpp"
  36 #include "gc/shared/barrierSet.hpp"
  37 #include "gc/shared/cardTableBarrierSet.hpp"
  38 #include "gc/shared/collectedHeap.hpp"
  39 #include "nativeInst_x86.hpp"
  40 #include "oops/objArrayKlass.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/safepointMechanism.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "vmreg_x86.inline.hpp"
  45 
  46 
  47 // These masks are used to provide 128-bit aligned bitmasks to the XMM
  48 // instructions, to allow sign-masking or sign-bit flipping.  They allow
  49 // fast versions of NegF/NegD and AbsF/AbsD.
  50 
  51 // Note: 'double' and 'long long' have 32-bits alignment on x86.
  52 static jlong* double_quadword(jlong *adr, jlong lo, jlong hi) {
  53   // Use the expression (adr)&(~0xF) to provide 128-bits aligned address
  54   // of 128-bits operands for SSE instructions.
  55   jlong *operand = (jlong*)(((intptr_t)adr) & ((intptr_t)(~0xF)));
  56   // Store the value to a 128-bits operand.
  57   operand[0] = lo;
  58   operand[1] = hi;
  59   return operand;
  60 }
  61 
  62 // Buffer for 128-bits masks used by SSE instructions.
  63 static jlong fp_signmask_pool[(4+1)*2]; // 4*128bits(data) + 128bits(alignment)
  64 
  65 // Static initialization during VM startup.
  66 static jlong *float_signmask_pool  = double_quadword(&fp_signmask_pool[1*2],         CONST64(0x7FFFFFFF7FFFFFFF),         CONST64(0x7FFFFFFF7FFFFFFF));
  67 static jlong *double_signmask_pool = double_quadword(&fp_signmask_pool[2*2],         CONST64(0x7FFFFFFFFFFFFFFF),         CONST64(0x7FFFFFFFFFFFFFFF));
  68 static jlong *float_signflip_pool  = double_quadword(&fp_signmask_pool[3*2], (jlong)UCONST64(0x8000000080000000), (jlong)UCONST64(0x8000000080000000));
  69 static jlong *double_signflip_pool = double_quadword(&fp_signmask_pool[4*2], (jlong)UCONST64(0x8000000000000000), (jlong)UCONST64(0x8000000000000000));
  70 
  71 
  72 NEEDS_CLEANUP // remove this definitions ?
  73 const Register IC_Klass    = rax;   // where the IC klass is cached
  74 const Register SYNC_header = rax;   // synchronization header
  75 const Register SHIFT_count = rcx;   // where count for shift operations must be
  76 
  77 #define __ _masm->
  78 
  79 
  80 static void select_different_registers(Register preserve,
  81                                        Register extra,
  82                                        Register &tmp1,
  83                                        Register &tmp2) {
  84   if (tmp1 == preserve) {
  85     assert_different_registers(tmp1, tmp2, extra);
  86     tmp1 = extra;
  87   } else if (tmp2 == preserve) {
  88     assert_different_registers(tmp1, tmp2, extra);
  89     tmp2 = extra;
  90   }
  91   assert_different_registers(preserve, tmp1, tmp2);
  92 }
  93 
  94 
  95 
  96 static void select_different_registers(Register preserve,
  97                                        Register extra,
  98                                        Register &tmp1,
  99                                        Register &tmp2,
 100                                        Register &tmp3) {
 101   if (tmp1 == preserve) {
 102     assert_different_registers(tmp1, tmp2, tmp3, extra);
 103     tmp1 = extra;
 104   } else if (tmp2 == preserve) {
 105     assert_different_registers(tmp1, tmp2, tmp3, extra);
 106     tmp2 = extra;
 107   } else if (tmp3 == preserve) {
 108     assert_different_registers(tmp1, tmp2, tmp3, extra);
 109     tmp3 = extra;
 110   }
 111   assert_different_registers(preserve, tmp1, tmp2, tmp3);
 112 }
 113 
 114 
 115 
 116 bool LIR_Assembler::is_small_constant(LIR_Opr opr) {
 117   if (opr->is_constant()) {
 118     LIR_Const* constant = opr->as_constant_ptr();
 119     switch (constant->type()) {
 120       case T_INT: {
 121         return true;
 122       }
 123 
 124       default:
 125         return false;
 126     }
 127   }
 128   return false;
 129 }
 130 
 131 
 132 LIR_Opr LIR_Assembler::receiverOpr() {
 133   return FrameMap::receiver_opr;
 134 }
 135 
 136 LIR_Opr LIR_Assembler::osrBufferPointer() {
 137   return FrameMap::as_pointer_opr(receiverOpr()->as_register());
 138 }
 139 
 140 //--------------fpu register translations-----------------------
 141 
 142 
 143 address LIR_Assembler::float_constant(float f) {
 144   address const_addr = __ float_constant(f);
 145   if (const_addr == NULL) {
 146     bailout("const section overflow");
 147     return __ code()->consts()->start();
 148   } else {
 149     return const_addr;
 150   }
 151 }
 152 
 153 
 154 address LIR_Assembler::double_constant(double d) {
 155   address const_addr = __ double_constant(d);
 156   if (const_addr == NULL) {
 157     bailout("const section overflow");
 158     return __ code()->consts()->start();
 159   } else {
 160     return const_addr;
 161   }
 162 }
 163 
 164 
 165 void LIR_Assembler::set_24bit_FPU() {
 166   __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_24()));
 167 }
 168 
 169 void LIR_Assembler::reset_FPU() {
 170   __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
 171 }
 172 
 173 void LIR_Assembler::fpop() {
 174   __ fpop();
 175 }
 176 
 177 void LIR_Assembler::fxch(int i) {
 178   __ fxch(i);
 179 }
 180 
 181 void LIR_Assembler::fld(int i) {
 182   __ fld_s(i);
 183 }
 184 
 185 void LIR_Assembler::ffree(int i) {
 186   __ ffree(i);
 187 }
 188 
 189 void LIR_Assembler::breakpoint() {
 190   __ int3();
 191 }
 192 
 193 void LIR_Assembler::push(LIR_Opr opr) {
 194   if (opr->is_single_cpu()) {
 195     __ push_reg(opr->as_register());
 196   } else if (opr->is_double_cpu()) {
 197     NOT_LP64(__ push_reg(opr->as_register_hi()));
 198     __ push_reg(opr->as_register_lo());
 199   } else if (opr->is_stack()) {
 200     __ push_addr(frame_map()->address_for_slot(opr->single_stack_ix()));
 201   } else if (opr->is_constant()) {
 202     LIR_Const* const_opr = opr->as_constant_ptr();
 203     if (const_opr->type() == T_OBJECT || const_opr->type() == T_VALUETYPE) {
 204       __ push_oop(const_opr->as_jobject());
 205     } else if (const_opr->type() == T_INT) {
 206       __ push_jint(const_opr->as_jint());
 207     } else {
 208       ShouldNotReachHere();
 209     }
 210 
 211   } else {
 212     ShouldNotReachHere();
 213   }
 214 }
 215 
 216 void LIR_Assembler::pop(LIR_Opr opr) {
 217   if (opr->is_single_cpu()) {
 218     __ pop_reg(opr->as_register());
 219   } else {
 220     ShouldNotReachHere();
 221   }
 222 }
 223 
 224 bool LIR_Assembler::is_literal_address(LIR_Address* addr) {
 225   return addr->base()->is_illegal() && addr->index()->is_illegal();
 226 }
 227 
 228 //-------------------------------------------
 229 
 230 Address LIR_Assembler::as_Address(LIR_Address* addr) {
 231   return as_Address(addr, rscratch1);
 232 }
 233 
 234 Address LIR_Assembler::as_Address(LIR_Address* addr, Register tmp) {
 235   if (addr->base()->is_illegal()) {
 236     assert(addr->index()->is_illegal(), "must be illegal too");
 237     AddressLiteral laddr((address)addr->disp(), relocInfo::none);
 238     if (! __ reachable(laddr)) {
 239       __ movptr(tmp, laddr.addr());
 240       Address res(tmp, 0);
 241       return res;
 242     } else {
 243       return __ as_Address(laddr);
 244     }
 245   }
 246 
 247   Register base = addr->base()->as_pointer_register();
 248 
 249   if (addr->index()->is_illegal()) {
 250     return Address( base, addr->disp());
 251   } else if (addr->index()->is_cpu_register()) {
 252     Register index = addr->index()->as_pointer_register();
 253     return Address(base, index, (Address::ScaleFactor) addr->scale(), addr->disp());
 254   } else if (addr->index()->is_constant()) {
 255     intptr_t addr_offset = (addr->index()->as_constant_ptr()->as_jint() << addr->scale()) + addr->disp();
 256     assert(Assembler::is_simm32(addr_offset), "must be");
 257 
 258     return Address(base, addr_offset);
 259   } else {
 260     Unimplemented();
 261     return Address();
 262   }
 263 }
 264 
 265 
 266 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
 267   Address base = as_Address(addr);
 268   return Address(base._base, base._index, base._scale, base._disp + BytesPerWord);
 269 }
 270 
 271 
 272 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
 273   return as_Address(addr);
 274 }
 275 
 276 
 277 void LIR_Assembler::osr_entry() {
 278   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
 279   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
 280   ValueStack* entry_state = osr_entry->state();
 281   int number_of_locks = entry_state->locks_size();
 282 
 283   // we jump here if osr happens with the interpreter
 284   // state set up to continue at the beginning of the
 285   // loop that triggered osr - in particular, we have
 286   // the following registers setup:
 287   //
 288   // rcx: osr buffer
 289   //
 290 
 291   // build frame
 292   ciMethod* m = compilation()->method();
 293   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes(),
 294                  needs_stack_repair(), NULL);
 295 
 296   // OSR buffer is
 297   //
 298   // locals[nlocals-1..0]
 299   // monitors[0..number_of_locks]
 300   //
 301   // locals is a direct copy of the interpreter frame so in the osr buffer
 302   // so first slot in the local array is the last local from the interpreter
 303   // and last slot is local[0] (receiver) from the interpreter
 304   //
 305   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 306   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 307   // in the interpreter frame (the method lock if a sync method)
 308 
 309   // Initialize monitors in the compiled activation.
 310   //   rcx: pointer to osr buffer
 311   //
 312   // All other registers are dead at this point and the locals will be
 313   // copied into place by code emitted in the IR.
 314 
 315   Register OSR_buf = osrBufferPointer()->as_pointer_register();
 316   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 317     int monitor_offset = BytesPerWord * method()->max_locals() +
 318       (BasicObjectLock::size() * BytesPerWord) * (number_of_locks - 1);
 319     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 320     // the OSR buffer using 2 word entries: first the lock and then
 321     // the oop.
 322     for (int i = 0; i < number_of_locks; i++) {
 323       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 324 #ifdef ASSERT
 325       // verify the interpreter's monitor has a non-null object
 326       {
 327         Label L;
 328         __ cmpptr(Address(OSR_buf, slot_offset + 1*BytesPerWord), (int32_t)NULL_WORD);
 329         __ jcc(Assembler::notZero, L);
 330         __ stop("locked object is NULL");
 331         __ bind(L);
 332       }
 333 #endif
 334       __ movptr(rbx, Address(OSR_buf, slot_offset + 0));
 335       __ movptr(frame_map()->address_for_monitor_lock(i), rbx);
 336       __ movptr(rbx, Address(OSR_buf, slot_offset + 1*BytesPerWord));
 337       __ movptr(frame_map()->address_for_monitor_object(i), rbx);
 338     }
 339   }
 340 }
 341 
 342 
 343 // inline cache check; done before the frame is built.
 344 int LIR_Assembler::check_icache() {
 345   Register receiver = FrameMap::receiver_opr->as_register();
 346   Register ic_klass = IC_Klass;
 347   const int ic_cmp_size = LP64_ONLY(10) NOT_LP64(9);
 348   const bool do_post_padding = VerifyOops || UseCompressedClassPointers;
 349   if (!do_post_padding) {
 350     // insert some nops so that the verified entry point is aligned on CodeEntryAlignment
 351     __ align(CodeEntryAlignment, __ offset() + ic_cmp_size);
 352   }
 353   int offset = __ offset();
 354   __ inline_cache_check(receiver, IC_Klass);
 355   assert(__ offset() % CodeEntryAlignment == 0 || do_post_padding, "alignment must be correct");
 356   if (do_post_padding) {
 357     // force alignment after the cache check.
 358     // It's been verified to be aligned if !VerifyOops
 359     __ align(CodeEntryAlignment);
 360   }
 361   return offset;
 362 }
 363 
 364 
 365 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo* info) {
 366   jobject o = NULL;
 367   PatchingStub* patch = new PatchingStub(_masm, patching_id(info));
 368   __ movoop(reg, o);
 369   patching_epilog(patch, lir_patch_normal, reg, info);
 370 }
 371 
 372 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo* info) {
 373   Metadata* o = NULL;
 374   PatchingStub* patch = new PatchingStub(_masm, PatchingStub::load_klass_id);
 375   __ mov_metadata(reg, o);
 376   patching_epilog(patch, lir_patch_normal, reg, info);
 377 }
 378 
 379 // This specifies the rsp decrement needed to build the frame
 380 int LIR_Assembler::initial_frame_size_in_bytes() const {
 381   // if rounding, must let FrameMap know!
 382 
 383   // The frame_map records size in slots (32bit word)
 384 
 385   // subtract two words to account for return address and link
 386   return (frame_map()->framesize() - (2*VMRegImpl::slots_per_word))  * VMRegImpl::stack_slot_size;
 387 }
 388 
 389 
 390 int LIR_Assembler::emit_exception_handler() {
 391   // if the last instruction is a call (typically to do a throw which
 392   // is coming at the end after block reordering) the return address
 393   // must still point into the code area in order to avoid assertion
 394   // failures when searching for the corresponding bci => add a nop
 395   // (was bug 5/14/1999 - gri)
 396   __ nop();
 397 
 398   // generate code for exception handler
 399   address handler_base = __ start_a_stub(exception_handler_size());
 400   if (handler_base == NULL) {
 401     // not enough space left for the handler
 402     bailout("exception handler overflow");
 403     return -1;
 404   }
 405 
 406   int offset = code_offset();
 407 
 408   // the exception oop and pc are in rax, and rdx
 409   // no other registers need to be preserved, so invalidate them
 410   __ invalidate_registers(false, true, true, false, true, true);
 411 
 412   // check that there is really an exception
 413   __ verify_not_null_oop(rax);
 414 
 415   // search an exception handler (rax: exception oop, rdx: throwing pc)
 416   __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::handle_exception_from_callee_id)));
 417   __ should_not_reach_here();
 418   guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
 419   __ end_a_stub();
 420 
 421   return offset;
 422 }
 423 
 424 
 425 // Emit the code to remove the frame from the stack in the exception
 426 // unwind path.
 427 int LIR_Assembler::emit_unwind_handler() {
 428 #ifndef PRODUCT
 429   if (CommentedAssembly) {
 430     _masm->block_comment("Unwind handler");
 431   }
 432 #endif
 433 
 434   int offset = code_offset();
 435 
 436   // Fetch the exception from TLS and clear out exception related thread state
 437   Register thread = NOT_LP64(rsi) LP64_ONLY(r15_thread);
 438   NOT_LP64(__ get_thread(rsi));
 439   __ movptr(rax, Address(thread, JavaThread::exception_oop_offset()));
 440   __ movptr(Address(thread, JavaThread::exception_oop_offset()), (intptr_t)NULL_WORD);
 441   __ movptr(Address(thread, JavaThread::exception_pc_offset()), (intptr_t)NULL_WORD);
 442 
 443   __ bind(_unwind_handler_entry);
 444   __ verify_not_null_oop(rax);
 445   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 446     __ mov(rbx, rax);  // Preserve the exception (rbx is always callee-saved)
 447   }
 448 
 449   // Preform needed unlocking
 450   MonitorExitStub* stub = NULL;
 451   if (method()->is_synchronized()) {
 452     monitor_address(0, FrameMap::rax_opr);
 453     stub = new MonitorExitStub(FrameMap::rax_opr, true, 0);
 454     __ unlock_object(rdi, rsi, rax, *stub->entry());
 455     __ bind(*stub->continuation());
 456   }
 457 
 458   if (compilation()->env()->dtrace_method_probes()) {
 459 #ifdef _LP64
 460     __ mov(rdi, r15_thread);
 461     __ mov_metadata(rsi, method()->constant_encoding());
 462 #else
 463     __ get_thread(rax);
 464     __ movptr(Address(rsp, 0), rax);
 465     __ mov_metadata(Address(rsp, sizeof(void*)), method()->constant_encoding());
 466 #endif
 467     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit)));
 468   }
 469 
 470   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 471     __ mov(rax, rbx);  // Restore the exception
 472   }
 473 
 474   // remove the activation and dispatch to the unwind handler
 475   __ remove_frame(initial_frame_size_in_bytes(), needs_stack_repair());
 476   __ jump(RuntimeAddress(Runtime1::entry_for(Runtime1::unwind_exception_id)));
 477 
 478   // Emit the slow path assembly
 479   if (stub != NULL) {
 480     stub->emit_code(this);
 481   }
 482 
 483   return offset;
 484 }
 485 
 486 
 487 int LIR_Assembler::emit_deopt_handler() {
 488   // if the last instruction is a call (typically to do a throw which
 489   // is coming at the end after block reordering) the return address
 490   // must still point into the code area in order to avoid assertion
 491   // failures when searching for the corresponding bci => add a nop
 492   // (was bug 5/14/1999 - gri)
 493   __ nop();
 494 
 495   // generate code for exception handler
 496   address handler_base = __ start_a_stub(deopt_handler_size());
 497   if (handler_base == NULL) {
 498     // not enough space left for the handler
 499     bailout("deopt handler overflow");
 500     return -1;
 501   }
 502 
 503   int offset = code_offset();
 504   InternalAddress here(__ pc());
 505 
 506   __ pushptr(here.addr());
 507   __ jump(RuntimeAddress(SharedRuntime::deopt_blob()->unpack()));
 508   guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
 509   __ end_a_stub();
 510 
 511   return offset;
 512 }
 513 
 514 
 515 void LIR_Assembler::return_op(LIR_Opr result) {
 516   assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == rax, "word returns are in rax,");
 517   if (!result->is_illegal() && result->is_float_kind() && !result->is_xmm_register()) {
 518     assert(result->fpu() == 0, "result must already be on TOS");
 519   }
 520 
 521   ciMethod* method = compilation()->method();
 522   if (ValueTypeReturnedAsFields && method->signature()->returns_never_null()) {
 523     ciType* return_type = method->return_type();
 524     if (return_type->is_valuetype()) {
 525       ciValueKlass* vk = return_type->as_value_klass();
 526       if (vk->can_be_returned_as_fields()) {
 527 #ifndef _LP64
 528         Unimplemented();
 529 #else
 530         address unpack_handler = vk->unpack_handler();
 531         assert(unpack_handler != NULL, "must be");
 532         __ call(RuntimeAddress(unpack_handler));
 533         // At this point, rax points to the value object (for interpreter or C1 caller).
 534         // The fields of the object are copied into registers (for C2 caller).
 535 #endif
 536       }
 537     }
 538   }
 539 
 540   // Pop the stack before the safepoint code
 541   __ remove_frame(initial_frame_size_in_bytes(), needs_stack_repair());
 542 
 543   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
 544     __ reserved_stack_check();
 545   }
 546 
 547   bool result_is_oop = result->is_valid() ? result->is_oop() : false;
 548 
 549   // Note: we do not need to round double result; float result has the right precision
 550   // the poll sets the condition code, but no data registers
 551 
 552   if (SafepointMechanism::uses_thread_local_poll()) {
 553 #ifdef _LP64
 554     const Register poll_addr = rscratch1;
 555     __ movptr(poll_addr, Address(r15_thread, Thread::polling_page_offset()));
 556 #else
 557     const Register poll_addr = rbx;
 558     assert(FrameMap::is_caller_save_register(poll_addr), "will overwrite");
 559     __ get_thread(poll_addr);
 560     __ movptr(poll_addr, Address(poll_addr, Thread::polling_page_offset()));
 561 #endif
 562     __ relocate(relocInfo::poll_return_type);
 563     __ testl(rax, Address(poll_addr, 0));
 564   } else {
 565     AddressLiteral polling_page(os::get_polling_page(), relocInfo::poll_return_type);
 566 
 567     if (Assembler::is_polling_page_far()) {
 568       __ lea(rscratch1, polling_page);
 569       __ relocate(relocInfo::poll_return_type);
 570       __ testl(rax, Address(rscratch1, 0));
 571     } else {
 572       __ testl(rax, polling_page);
 573     }
 574   }
 575   __ ret(0);
 576 }
 577 
 578 
 579 void LIR_Assembler::store_value_type_fields_to_buf(ciValueKlass* vk) {
 580   __ store_value_type_fields_to_buf(vk);
 581 }
 582 
 583 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
 584   guarantee(info != NULL, "Shouldn't be NULL");
 585   int offset = __ offset();
 586   if (SafepointMechanism::uses_thread_local_poll()) {
 587 #ifdef _LP64
 588     const Register poll_addr = rscratch1;
 589     __ movptr(poll_addr, Address(r15_thread, Thread::polling_page_offset()));
 590 #else
 591     assert(tmp->is_cpu_register(), "needed");
 592     const Register poll_addr = tmp->as_register();
 593     __ get_thread(poll_addr);
 594     __ movptr(poll_addr, Address(poll_addr, in_bytes(Thread::polling_page_offset())));
 595 #endif
 596     add_debug_info_for_branch(info);
 597     __ relocate(relocInfo::poll_type);
 598     address pre_pc = __ pc();
 599     __ testl(rax, Address(poll_addr, 0));
 600     address post_pc = __ pc();
 601     guarantee(pointer_delta(post_pc, pre_pc, 1) == 2 LP64_ONLY(+1), "must be exact length");
 602   } else {
 603     AddressLiteral polling_page(os::get_polling_page(), relocInfo::poll_type);
 604     if (Assembler::is_polling_page_far()) {
 605       __ lea(rscratch1, polling_page);
 606       offset = __ offset();
 607       add_debug_info_for_branch(info);
 608       __ relocate(relocInfo::poll_type);
 609       __ testl(rax, Address(rscratch1, 0));
 610     } else {
 611       add_debug_info_for_branch(info);
 612       __ testl(rax, polling_page);
 613     }
 614   }
 615   return offset;
 616 }
 617 
 618 
 619 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
 620   if (from_reg != to_reg) __ mov(to_reg, from_reg);
 621 }
 622 
 623 void LIR_Assembler::swap_reg(Register a, Register b) {
 624   __ xchgptr(a, b);
 625 }
 626 
 627 
 628 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
 629   assert(src->is_constant(), "should not call otherwise");
 630   assert(dest->is_register(), "should not call otherwise");
 631   LIR_Const* c = src->as_constant_ptr();
 632 
 633   switch (c->type()) {
 634     case T_INT: {
 635       assert(patch_code == lir_patch_none, "no patching handled here");
 636       __ movl(dest->as_register(), c->as_jint());
 637       break;
 638     }
 639 
 640     case T_ADDRESS: {
 641       assert(patch_code == lir_patch_none, "no patching handled here");
 642       __ movptr(dest->as_register(), c->as_jint());
 643       break;
 644     }
 645 
 646     case T_LONG: {
 647       assert(patch_code == lir_patch_none, "no patching handled here");
 648 #ifdef _LP64
 649       __ movptr(dest->as_register_lo(), (intptr_t)c->as_jlong());
 650 #else
 651       __ movptr(dest->as_register_lo(), c->as_jint_lo());
 652       __ movptr(dest->as_register_hi(), c->as_jint_hi());
 653 #endif // _LP64
 654       break;
 655     }
 656 
 657     case T_VALUETYPE: // Fall through
 658     case T_OBJECT: {
 659       if (patch_code != lir_patch_none) {
 660         jobject2reg_with_patching(dest->as_register(), info);
 661       } else {
 662         __ movoop(dest->as_register(), c->as_jobject());
 663       }
 664       break;
 665     }
 666 
 667     case T_METADATA: {
 668       if (patch_code != lir_patch_none) {
 669         klass2reg_with_patching(dest->as_register(), info);
 670       } else {
 671         __ mov_metadata(dest->as_register(), c->as_metadata());
 672       }
 673       break;
 674     }
 675 
 676     case T_FLOAT: {
 677       if (dest->is_single_xmm()) {
 678         if (LP64_ONLY(UseAVX <= 2 &&) c->is_zero_float()) {
 679           __ xorps(dest->as_xmm_float_reg(), dest->as_xmm_float_reg());
 680         } else {
 681           __ movflt(dest->as_xmm_float_reg(),
 682                    InternalAddress(float_constant(c->as_jfloat())));
 683         }
 684       } else {
 685         assert(dest->is_single_fpu(), "must be");
 686         assert(dest->fpu_regnr() == 0, "dest must be TOS");
 687         if (c->is_zero_float()) {
 688           __ fldz();
 689         } else if (c->is_one_float()) {
 690           __ fld1();
 691         } else {
 692           __ fld_s (InternalAddress(float_constant(c->as_jfloat())));
 693         }
 694       }
 695       break;
 696     }
 697 
 698     case T_DOUBLE: {
 699       if (dest->is_double_xmm()) {
 700         if (LP64_ONLY(UseAVX <= 2 &&) c->is_zero_double()) {
 701           __ xorpd(dest->as_xmm_double_reg(), dest->as_xmm_double_reg());
 702         } else {
 703           __ movdbl(dest->as_xmm_double_reg(),
 704                     InternalAddress(double_constant(c->as_jdouble())));
 705         }
 706       } else {
 707         assert(dest->is_double_fpu(), "must be");
 708         assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
 709         if (c->is_zero_double()) {
 710           __ fldz();
 711         } else if (c->is_one_double()) {
 712           __ fld1();
 713         } else {
 714           __ fld_d (InternalAddress(double_constant(c->as_jdouble())));
 715         }
 716       }
 717       break;
 718     }
 719 
 720     default:
 721       ShouldNotReachHere();
 722   }
 723 }
 724 
 725 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 726   assert(src->is_constant(), "should not call otherwise");
 727   assert(dest->is_stack(), "should not call otherwise");
 728   LIR_Const* c = src->as_constant_ptr();
 729 
 730   switch (c->type()) {
 731     case T_INT:  // fall through
 732     case T_FLOAT:
 733       __ movl(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jint_bits());
 734       break;
 735 
 736     case T_ADDRESS:
 737       __ movptr(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jint_bits());
 738       break;
 739 
 740     case T_VALUETYPE: // Fall through
 741     case T_OBJECT:
 742       __ movoop(frame_map()->address_for_slot(dest->single_stack_ix()), c->as_jobject());
 743       break;
 744 
 745     case T_LONG:  // fall through
 746     case T_DOUBLE:
 747 #ifdef _LP64
 748       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 749                                             lo_word_offset_in_bytes), (intptr_t)c->as_jlong_bits());
 750 #else
 751       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 752                                               lo_word_offset_in_bytes), c->as_jint_lo_bits());
 753       __ movptr(frame_map()->address_for_slot(dest->double_stack_ix(),
 754                                               hi_word_offset_in_bytes), c->as_jint_hi_bits());
 755 #endif // _LP64
 756       break;
 757 
 758     default:
 759       ShouldNotReachHere();
 760   }
 761 }
 762 
 763 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 764   assert(src->is_constant(), "should not call otherwise");
 765   assert(dest->is_address(), "should not call otherwise");
 766   LIR_Const* c = src->as_constant_ptr();
 767   LIR_Address* addr = dest->as_address_ptr();
 768 
 769   int null_check_here = code_offset();
 770   switch (type) {
 771     case T_INT:    // fall through
 772     case T_FLOAT:
 773       __ movl(as_Address(addr), c->as_jint_bits());
 774       break;
 775 
 776     case T_ADDRESS:
 777       __ movptr(as_Address(addr), c->as_jint_bits());
 778       break;
 779 
 780     case T_VALUETYPE: // fall through
 781     case T_OBJECT:  // fall through
 782     case T_ARRAY:
 783       if (c->as_jobject() == NULL) {
 784         if (UseCompressedOops && !wide) {
 785           __ movl(as_Address(addr), (int32_t)NULL_WORD);
 786         } else {
 787 #ifdef _LP64
 788           __ xorptr(rscratch1, rscratch1);
 789           null_check_here = code_offset();
 790           __ movptr(as_Address(addr), rscratch1);
 791 #else
 792           __ movptr(as_Address(addr), NULL_WORD);
 793 #endif
 794         }
 795       } else {
 796         if (is_literal_address(addr)) {
 797           ShouldNotReachHere();
 798           __ movoop(as_Address(addr, noreg), c->as_jobject());
 799         } else {
 800 #ifdef _LP64
 801           __ movoop(rscratch1, c->as_jobject());
 802           if (UseCompressedOops && !wide) {
 803             __ encode_heap_oop(rscratch1);
 804             null_check_here = code_offset();
 805             __ movl(as_Address_lo(addr), rscratch1);
 806           } else {
 807             null_check_here = code_offset();
 808             __ movptr(as_Address_lo(addr), rscratch1);
 809           }
 810 #else
 811           __ movoop(as_Address(addr), c->as_jobject());
 812 #endif
 813         }
 814       }
 815       break;
 816 
 817     case T_LONG:    // fall through
 818     case T_DOUBLE:
 819 #ifdef _LP64
 820       if (is_literal_address(addr)) {
 821         ShouldNotReachHere();
 822         __ movptr(as_Address(addr, r15_thread), (intptr_t)c->as_jlong_bits());
 823       } else {
 824         __ movptr(r10, (intptr_t)c->as_jlong_bits());
 825         null_check_here = code_offset();
 826         __ movptr(as_Address_lo(addr), r10);
 827       }
 828 #else
 829       // Always reachable in 32bit so this doesn't produce useless move literal
 830       __ movptr(as_Address_hi(addr), c->as_jint_hi_bits());
 831       __ movptr(as_Address_lo(addr), c->as_jint_lo_bits());
 832 #endif // _LP64
 833       break;
 834 
 835     case T_BOOLEAN: // fall through
 836     case T_BYTE:
 837       __ movb(as_Address(addr), c->as_jint() & 0xFF);
 838       break;
 839 
 840     case T_CHAR:    // fall through
 841     case T_SHORT:
 842       __ movw(as_Address(addr), c->as_jint() & 0xFFFF);
 843       break;
 844 
 845     default:
 846       ShouldNotReachHere();
 847   };
 848 
 849   if (info != NULL) {
 850     add_debug_info_for_null_check(null_check_here, info);
 851   }
 852 }
 853 
 854 
 855 void LIR_Assembler::reg2reg(LIR_Opr src, LIR_Opr dest) {
 856   assert(src->is_register(), "should not call otherwise");
 857   assert(dest->is_register(), "should not call otherwise");
 858 
 859   // move between cpu-registers
 860   if (dest->is_single_cpu()) {
 861 #ifdef _LP64
 862     if (src->type() == T_LONG) {
 863       // Can do LONG -> OBJECT
 864       move_regs(src->as_register_lo(), dest->as_register());
 865       return;
 866     }
 867 #endif
 868     assert(src->is_single_cpu(), "must match");
 869     if (src->type() == T_OBJECT || src->type() == T_VALUETYPE) {
 870       __ verify_oop(src->as_register());
 871     }
 872     move_regs(src->as_register(), dest->as_register());
 873 
 874   } else if (dest->is_double_cpu()) {
 875 #ifdef _LP64
 876     if (src->type() == T_OBJECT || src->type() == T_ARRAY || src->type() == T_VALUETYPE) {
 877       // Surprising to me but we can see move of a long to t_object
 878       __ verify_oop(src->as_register());
 879       move_regs(src->as_register(), dest->as_register_lo());
 880       return;
 881     }
 882 #endif
 883     assert(src->is_double_cpu(), "must match");
 884     Register f_lo = src->as_register_lo();
 885     Register f_hi = src->as_register_hi();
 886     Register t_lo = dest->as_register_lo();
 887     Register t_hi = dest->as_register_hi();
 888 #ifdef _LP64
 889     assert(f_hi == f_lo, "must be same");
 890     assert(t_hi == t_lo, "must be same");
 891     move_regs(f_lo, t_lo);
 892 #else
 893     assert(f_lo != f_hi && t_lo != t_hi, "invalid register allocation");
 894 
 895 
 896     if (f_lo == t_hi && f_hi == t_lo) {
 897       swap_reg(f_lo, f_hi);
 898     } else if (f_hi == t_lo) {
 899       assert(f_lo != t_hi, "overwriting register");
 900       move_regs(f_hi, t_hi);
 901       move_regs(f_lo, t_lo);
 902     } else {
 903       assert(f_hi != t_lo, "overwriting register");
 904       move_regs(f_lo, t_lo);
 905       move_regs(f_hi, t_hi);
 906     }
 907 #endif // LP64
 908 
 909     // special moves from fpu-register to xmm-register
 910     // necessary for method results
 911   } else if (src->is_single_xmm() && !dest->is_single_xmm()) {
 912     __ movflt(Address(rsp, 0), src->as_xmm_float_reg());
 913     __ fld_s(Address(rsp, 0));
 914   } else if (src->is_double_xmm() && !dest->is_double_xmm()) {
 915     __ movdbl(Address(rsp, 0), src->as_xmm_double_reg());
 916     __ fld_d(Address(rsp, 0));
 917   } else if (dest->is_single_xmm() && !src->is_single_xmm()) {
 918     __ fstp_s(Address(rsp, 0));
 919     __ movflt(dest->as_xmm_float_reg(), Address(rsp, 0));
 920   } else if (dest->is_double_xmm() && !src->is_double_xmm()) {
 921     __ fstp_d(Address(rsp, 0));
 922     __ movdbl(dest->as_xmm_double_reg(), Address(rsp, 0));
 923 
 924     // move between xmm-registers
 925   } else if (dest->is_single_xmm()) {
 926     assert(src->is_single_xmm(), "must match");
 927     __ movflt(dest->as_xmm_float_reg(), src->as_xmm_float_reg());
 928   } else if (dest->is_double_xmm()) {
 929     assert(src->is_double_xmm(), "must match");
 930     __ movdbl(dest->as_xmm_double_reg(), src->as_xmm_double_reg());
 931 
 932     // move between fpu-registers (no instruction necessary because of fpu-stack)
 933   } else if (dest->is_single_fpu() || dest->is_double_fpu()) {
 934     assert(src->is_single_fpu() || src->is_double_fpu(), "must match");
 935     assert(src->fpu() == dest->fpu(), "currently should be nothing to do");
 936   } else {
 937     ShouldNotReachHere();
 938   }
 939 }
 940 
 941 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
 942   assert(src->is_register(), "should not call otherwise");
 943   assert(dest->is_stack(), "should not call otherwise");
 944 
 945   if (src->is_single_cpu()) {
 946     Address dst = frame_map()->address_for_slot(dest->single_stack_ix());
 947     if (type == T_OBJECT || type == T_ARRAY || type == T_VALUETYPE) {
 948       __ verify_oop(src->as_register());
 949       __ movptr (dst, src->as_register());
 950     } else if (type == T_METADATA) {
 951       __ movptr (dst, src->as_register());
 952     } else {
 953       __ movl (dst, src->as_register());
 954     }
 955 
 956   } else if (src->is_double_cpu()) {
 957     Address dstLO = frame_map()->address_for_slot(dest->double_stack_ix(), lo_word_offset_in_bytes);
 958     Address dstHI = frame_map()->address_for_slot(dest->double_stack_ix(), hi_word_offset_in_bytes);
 959     __ movptr (dstLO, src->as_register_lo());
 960     NOT_LP64(__ movptr (dstHI, src->as_register_hi()));
 961 
 962   } else if (src->is_single_xmm()) {
 963     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 964     __ movflt(dst_addr, src->as_xmm_float_reg());
 965 
 966   } else if (src->is_double_xmm()) {
 967     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 968     __ movdbl(dst_addr, src->as_xmm_double_reg());
 969 
 970   } else if (src->is_single_fpu()) {
 971     assert(src->fpu_regnr() == 0, "argument must be on TOS");
 972     Address dst_addr = frame_map()->address_for_slot(dest->single_stack_ix());
 973     if (pop_fpu_stack)     __ fstp_s (dst_addr);
 974     else                   __ fst_s  (dst_addr);
 975 
 976   } else if (src->is_double_fpu()) {
 977     assert(src->fpu_regnrLo() == 0, "argument must be on TOS");
 978     Address dst_addr = frame_map()->address_for_slot(dest->double_stack_ix());
 979     if (pop_fpu_stack)     __ fstp_d (dst_addr);
 980     else                   __ fst_d  (dst_addr);
 981 
 982   } else {
 983     ShouldNotReachHere();
 984   }
 985 }
 986 
 987 
 988 void LIR_Assembler::reg2mem(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack, bool wide, bool /* unaligned */) {
 989   LIR_Address* to_addr = dest->as_address_ptr();
 990   PatchingStub* patch = NULL;
 991   Register compressed_src = rscratch1;
 992 
 993   if (type == T_ARRAY || type == T_OBJECT || type == T_VALUETYPE) {
 994     __ verify_oop(src->as_register());
 995 #ifdef _LP64
 996     if (UseCompressedOops && !wide) {
 997       __ movptr(compressed_src, src->as_register());
 998       __ encode_heap_oop(compressed_src);
 999       if (patch_code != lir_patch_none) {
1000         info->oop_map()->set_narrowoop(compressed_src->as_VMReg());
1001       }
1002     }
1003 #endif
1004   }
1005 
1006   if (patch_code != lir_patch_none) {
1007     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1008     Address toa = as_Address(to_addr);
1009     assert(toa.disp() != 0, "must have");
1010   }
1011 
1012   int null_check_here = code_offset();
1013   switch (type) {
1014     case T_FLOAT: {
1015       if (src->is_single_xmm()) {
1016         __ movflt(as_Address(to_addr), src->as_xmm_float_reg());
1017       } else {
1018         assert(src->is_single_fpu(), "must be");
1019         assert(src->fpu_regnr() == 0, "argument must be on TOS");
1020         if (pop_fpu_stack)      __ fstp_s(as_Address(to_addr));
1021         else                    __ fst_s (as_Address(to_addr));
1022       }
1023       break;
1024     }
1025 
1026     case T_DOUBLE: {
1027       if (src->is_double_xmm()) {
1028         __ movdbl(as_Address(to_addr), src->as_xmm_double_reg());
1029       } else {
1030         assert(src->is_double_fpu(), "must be");
1031         assert(src->fpu_regnrLo() == 0, "argument must be on TOS");
1032         if (pop_fpu_stack)      __ fstp_d(as_Address(to_addr));
1033         else                    __ fst_d (as_Address(to_addr));
1034       }
1035       break;
1036     }
1037 
1038     case T_VALUETYPE: // fall through
1039     case T_ARRAY:   // fall through
1040     case T_OBJECT:  // fall through
1041       if (UseCompressedOops && !wide) {
1042         __ movl(as_Address(to_addr), compressed_src);
1043       } else {
1044         __ movptr(as_Address(to_addr), src->as_register());
1045       }
1046       break;
1047     case T_METADATA:
1048       // We get here to store a method pointer to the stack to pass to
1049       // a dtrace runtime call. This can't work on 64 bit with
1050       // compressed klass ptrs: T_METADATA can be a compressed klass
1051       // ptr or a 64 bit method pointer.
1052       LP64_ONLY(ShouldNotReachHere());
1053       __ movptr(as_Address(to_addr), src->as_register());
1054       break;
1055     case T_ADDRESS:
1056       __ movptr(as_Address(to_addr), src->as_register());
1057       break;
1058     case T_INT:
1059       __ movl(as_Address(to_addr), src->as_register());
1060       break;
1061 
1062     case T_LONG: {
1063       Register from_lo = src->as_register_lo();
1064       Register from_hi = src->as_register_hi();
1065 #ifdef _LP64
1066       __ movptr(as_Address_lo(to_addr), from_lo);
1067 #else
1068       Register base = to_addr->base()->as_register();
1069       Register index = noreg;
1070       if (to_addr->index()->is_register()) {
1071         index = to_addr->index()->as_register();
1072       }
1073       if (base == from_lo || index == from_lo) {
1074         assert(base != from_hi, "can't be");
1075         assert(index == noreg || (index != base && index != from_hi), "can't handle this");
1076         __ movl(as_Address_hi(to_addr), from_hi);
1077         if (patch != NULL) {
1078           patching_epilog(patch, lir_patch_high, base, info);
1079           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1080           patch_code = lir_patch_low;
1081         }
1082         __ movl(as_Address_lo(to_addr), from_lo);
1083       } else {
1084         assert(index == noreg || (index != base && index != from_lo), "can't handle this");
1085         __ movl(as_Address_lo(to_addr), from_lo);
1086         if (patch != NULL) {
1087           patching_epilog(patch, lir_patch_low, base, info);
1088           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1089           patch_code = lir_patch_high;
1090         }
1091         __ movl(as_Address_hi(to_addr), from_hi);
1092       }
1093 #endif // _LP64
1094       break;
1095     }
1096 
1097     case T_BYTE:    // fall through
1098     case T_BOOLEAN: {
1099       Register src_reg = src->as_register();
1100       Address dst_addr = as_Address(to_addr);
1101       assert(VM_Version::is_P6() || src_reg->has_byte_register(), "must use byte registers if not P6");
1102       __ movb(dst_addr, src_reg);
1103       break;
1104     }
1105 
1106     case T_CHAR:    // fall through
1107     case T_SHORT:
1108       __ movw(as_Address(to_addr), src->as_register());
1109       break;
1110 
1111     default:
1112       ShouldNotReachHere();
1113   }
1114   if (info != NULL) {
1115     add_debug_info_for_null_check(null_check_here, info);
1116   }
1117 
1118   if (patch_code != lir_patch_none) {
1119     patching_epilog(patch, patch_code, to_addr->base()->as_register(), info);
1120   }
1121 }
1122 
1123 
1124 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
1125   assert(src->is_stack(), "should not call otherwise");
1126   assert(dest->is_register(), "should not call otherwise");
1127 
1128   if (dest->is_single_cpu()) {
1129     if (type == T_ARRAY || type == T_OBJECT || type == T_VALUETYPE) {
1130       __ movptr(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1131       __ verify_oop(dest->as_register());
1132     } else if (type == T_METADATA) {
1133       __ movptr(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1134     } else {
1135       __ movl(dest->as_register(), frame_map()->address_for_slot(src->single_stack_ix()));
1136     }
1137 
1138   } else if (dest->is_double_cpu()) {
1139     Address src_addr_LO = frame_map()->address_for_slot(src->double_stack_ix(), lo_word_offset_in_bytes);
1140     Address src_addr_HI = frame_map()->address_for_slot(src->double_stack_ix(), hi_word_offset_in_bytes);
1141     __ movptr(dest->as_register_lo(), src_addr_LO);
1142     NOT_LP64(__ movptr(dest->as_register_hi(), src_addr_HI));
1143 
1144   } else if (dest->is_single_xmm()) {
1145     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
1146     __ movflt(dest->as_xmm_float_reg(), src_addr);
1147 
1148   } else if (dest->is_double_xmm()) {
1149     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
1150     __ movdbl(dest->as_xmm_double_reg(), src_addr);
1151 
1152   } else if (dest->is_single_fpu()) {
1153     assert(dest->fpu_regnr() == 0, "dest must be TOS");
1154     Address src_addr = frame_map()->address_for_slot(src->single_stack_ix());
1155     __ fld_s(src_addr);
1156 
1157   } else if (dest->is_double_fpu()) {
1158     assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
1159     Address src_addr = frame_map()->address_for_slot(src->double_stack_ix());
1160     __ fld_d(src_addr);
1161 
1162   } else {
1163     ShouldNotReachHere();
1164   }
1165 }
1166 
1167 
1168 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
1169   if (src->is_single_stack()) {
1170     if (type == T_OBJECT || type == T_ARRAY || type == T_VALUETYPE) {
1171       __ pushptr(frame_map()->address_for_slot(src ->single_stack_ix()));
1172       __ popptr (frame_map()->address_for_slot(dest->single_stack_ix()));
1173     } else {
1174 #ifndef _LP64
1175       __ pushl(frame_map()->address_for_slot(src ->single_stack_ix()));
1176       __ popl (frame_map()->address_for_slot(dest->single_stack_ix()));
1177 #else
1178       //no pushl on 64bits
1179       __ movl(rscratch1, frame_map()->address_for_slot(src ->single_stack_ix()));
1180       __ movl(frame_map()->address_for_slot(dest->single_stack_ix()), rscratch1);
1181 #endif
1182     }
1183 
1184   } else if (src->is_double_stack()) {
1185 #ifdef _LP64
1186     __ pushptr(frame_map()->address_for_slot(src ->double_stack_ix()));
1187     __ popptr (frame_map()->address_for_slot(dest->double_stack_ix()));
1188 #else
1189     __ pushl(frame_map()->address_for_slot(src ->double_stack_ix(), 0));
1190     // push and pop the part at src + wordSize, adding wordSize for the previous push
1191     __ pushl(frame_map()->address_for_slot(src ->double_stack_ix(), 2 * wordSize));
1192     __ popl (frame_map()->address_for_slot(dest->double_stack_ix(), 2 * wordSize));
1193     __ popl (frame_map()->address_for_slot(dest->double_stack_ix(), 0));
1194 #endif // _LP64
1195 
1196   } else {
1197     ShouldNotReachHere();
1198   }
1199 }
1200 
1201 
1202 void LIR_Assembler::mem2reg(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide, bool /* unaligned */) {
1203   assert(src->is_address(), "should not call otherwise");
1204   assert(dest->is_register(), "should not call otherwise");
1205 
1206   LIR_Address* addr = src->as_address_ptr();
1207   Address from_addr = as_Address(addr);
1208 
1209   if (addr->base()->type() == T_OBJECT || addr->base()->type() == T_VALUETYPE) {
1210     __ verify_oop(addr->base()->as_pointer_register());
1211   }
1212 
1213   switch (type) {
1214     case T_BOOLEAN: // fall through
1215     case T_BYTE:    // fall through
1216     case T_CHAR:    // fall through
1217     case T_SHORT:
1218       if (!VM_Version::is_P6() && !from_addr.uses(dest->as_register())) {
1219         // on pre P6 processors we may get partial register stalls
1220         // so blow away the value of to_rinfo before loading a
1221         // partial word into it.  Do it here so that it precedes
1222         // the potential patch point below.
1223         __ xorptr(dest->as_register(), dest->as_register());
1224       }
1225       break;
1226    default:
1227      break;
1228   }
1229 
1230   PatchingStub* patch = NULL;
1231   if (patch_code != lir_patch_none) {
1232     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1233     assert(from_addr.disp() != 0, "must have");
1234   }
1235   if (info != NULL) {
1236     add_debug_info_for_null_check_here(info);
1237   }
1238 
1239   switch (type) {
1240     case T_FLOAT: {
1241       if (dest->is_single_xmm()) {
1242         __ movflt(dest->as_xmm_float_reg(), from_addr);
1243       } else {
1244         assert(dest->is_single_fpu(), "must be");
1245         assert(dest->fpu_regnr() == 0, "dest must be TOS");
1246         __ fld_s(from_addr);
1247       }
1248       break;
1249     }
1250 
1251     case T_DOUBLE: {
1252       if (dest->is_double_xmm()) {
1253         __ movdbl(dest->as_xmm_double_reg(), from_addr);
1254       } else {
1255         assert(dest->is_double_fpu(), "must be");
1256         assert(dest->fpu_regnrLo() == 0, "dest must be TOS");
1257         __ fld_d(from_addr);
1258       }
1259       break;
1260     }
1261 
1262     case T_VALUETYPE: // fall through
1263     case T_OBJECT:  // fall through
1264     case T_ARRAY:   // fall through
1265       if (UseCompressedOops && !wide) {
1266         __ movl(dest->as_register(), from_addr);
1267       } else {
1268         __ movptr(dest->as_register(), from_addr);
1269       }
1270       break;
1271 
1272     case T_ADDRESS:
1273       if (UseCompressedClassPointers && addr->disp() == oopDesc::klass_offset_in_bytes()) {
1274         __ movl(dest->as_register(), from_addr);
1275       } else {
1276         __ movptr(dest->as_register(), from_addr);
1277       }
1278       break;
1279     case T_INT:
1280       __ movl(dest->as_register(), from_addr);
1281       break;
1282 
1283     case T_LONG: {
1284       Register to_lo = dest->as_register_lo();
1285       Register to_hi = dest->as_register_hi();
1286 #ifdef _LP64
1287       __ movptr(to_lo, as_Address_lo(addr));
1288 #else
1289       Register base = addr->base()->as_register();
1290       Register index = noreg;
1291       if (addr->index()->is_register()) {
1292         index = addr->index()->as_register();
1293       }
1294       if ((base == to_lo && index == to_hi) ||
1295           (base == to_hi && index == to_lo)) {
1296         // addresses with 2 registers are only formed as a result of
1297         // array access so this code will never have to deal with
1298         // patches or null checks.
1299         assert(info == NULL && patch == NULL, "must be");
1300         __ lea(to_hi, as_Address(addr));
1301         __ movl(to_lo, Address(to_hi, 0));
1302         __ movl(to_hi, Address(to_hi, BytesPerWord));
1303       } else if (base == to_lo || index == to_lo) {
1304         assert(base != to_hi, "can't be");
1305         assert(index == noreg || (index != base && index != to_hi), "can't handle this");
1306         __ movl(to_hi, as_Address_hi(addr));
1307         if (patch != NULL) {
1308           patching_epilog(patch, lir_patch_high, base, info);
1309           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1310           patch_code = lir_patch_low;
1311         }
1312         __ movl(to_lo, as_Address_lo(addr));
1313       } else {
1314         assert(index == noreg || (index != base && index != to_lo), "can't handle this");
1315         __ movl(to_lo, as_Address_lo(addr));
1316         if (patch != NULL) {
1317           patching_epilog(patch, lir_patch_low, base, info);
1318           patch = new PatchingStub(_masm, PatchingStub::access_field_id);
1319           patch_code = lir_patch_high;
1320         }
1321         __ movl(to_hi, as_Address_hi(addr));
1322       }
1323 #endif // _LP64
1324       break;
1325     }
1326 
1327     case T_BOOLEAN: // fall through
1328     case T_BYTE: {
1329       Register dest_reg = dest->as_register();
1330       assert(VM_Version::is_P6() || dest_reg->has_byte_register(), "must use byte registers if not P6");
1331       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1332         __ movsbl(dest_reg, from_addr);
1333       } else {
1334         __ movb(dest_reg, from_addr);
1335         __ shll(dest_reg, 24);
1336         __ sarl(dest_reg, 24);
1337       }
1338       break;
1339     }
1340 
1341     case T_CHAR: {
1342       Register dest_reg = dest->as_register();
1343       assert(VM_Version::is_P6() || dest_reg->has_byte_register(), "must use byte registers if not P6");
1344       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1345         __ movzwl(dest_reg, from_addr);
1346       } else {
1347         __ movw(dest_reg, from_addr);
1348       }
1349       break;
1350     }
1351 
1352     case T_SHORT: {
1353       Register dest_reg = dest->as_register();
1354       if (VM_Version::is_P6() || from_addr.uses(dest_reg)) {
1355         __ movswl(dest_reg, from_addr);
1356       } else {
1357         __ movw(dest_reg, from_addr);
1358         __ shll(dest_reg, 16);
1359         __ sarl(dest_reg, 16);
1360       }
1361       break;
1362     }
1363 
1364     default:
1365       ShouldNotReachHere();
1366   }
1367 
1368   if (patch != NULL) {
1369     patching_epilog(patch, patch_code, addr->base()->as_register(), info);
1370   }
1371 
1372   if (type == T_ARRAY || type == T_OBJECT || type == T_VALUETYPE) {
1373 #ifdef _LP64
1374     if (UseCompressedOops && !wide) {
1375       __ decode_heap_oop(dest->as_register());
1376     }
1377 #endif
1378 
1379     // Load barrier has not yet been applied, so ZGC can't verify the oop here
1380     if (!UseZGC) {
1381       __ verify_oop(dest->as_register());
1382     }
1383   } else if (type == T_ADDRESS && addr->disp() == oopDesc::klass_offset_in_bytes()) {
1384 #ifdef _LP64
1385     if (UseCompressedClassPointers) {
1386       __ decode_klass_not_null(dest->as_register());
1387     }
1388 #endif
1389   }
1390 }
1391 
1392 
1393 NEEDS_CLEANUP; // This could be static?
1394 Address::ScaleFactor LIR_Assembler::array_element_size(BasicType type) const {
1395   int elem_size = type2aelembytes(type);
1396   switch (elem_size) {
1397     case 1: return Address::times_1;
1398     case 2: return Address::times_2;
1399     case 4: return Address::times_4;
1400     case 8: return Address::times_8;
1401   }
1402   ShouldNotReachHere();
1403   return Address::no_scale;
1404 }
1405 
1406 
1407 void LIR_Assembler::emit_op3(LIR_Op3* op) {
1408   switch (op->code()) {
1409     case lir_idiv:
1410     case lir_irem:
1411       arithmetic_idiv(op->code(),
1412                       op->in_opr1(),
1413                       op->in_opr2(),
1414                       op->in_opr3(),
1415                       op->result_opr(),
1416                       op->info());
1417       break;
1418     case lir_fmad:
1419       __ fmad(op->result_opr()->as_xmm_double_reg(),
1420               op->in_opr1()->as_xmm_double_reg(),
1421               op->in_opr2()->as_xmm_double_reg(),
1422               op->in_opr3()->as_xmm_double_reg());
1423       break;
1424     case lir_fmaf:
1425       __ fmaf(op->result_opr()->as_xmm_float_reg(),
1426               op->in_opr1()->as_xmm_float_reg(),
1427               op->in_opr2()->as_xmm_float_reg(),
1428               op->in_opr3()->as_xmm_float_reg());
1429       break;
1430     default:      ShouldNotReachHere(); break;
1431   }
1432 }
1433 
1434 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
1435 #ifdef ASSERT
1436   assert(op->block() == NULL || op->block()->label() == op->label(), "wrong label");
1437   if (op->block() != NULL)  _branch_target_blocks.append(op->block());
1438   if (op->ublock() != NULL) _branch_target_blocks.append(op->ublock());
1439 #endif
1440 
1441   if (op->cond() == lir_cond_always) {
1442     if (op->info() != NULL) add_debug_info_for_branch(op->info());
1443     __ jmp (*(op->label()));
1444   } else {
1445     Assembler::Condition acond = Assembler::zero;
1446     if (op->code() == lir_cond_float_branch) {
1447       assert(op->ublock() != NULL, "must have unordered successor");
1448       __ jcc(Assembler::parity, *(op->ublock()->label()));
1449       switch(op->cond()) {
1450         case lir_cond_equal:        acond = Assembler::equal;      break;
1451         case lir_cond_notEqual:     acond = Assembler::notEqual;   break;
1452         case lir_cond_less:         acond = Assembler::below;      break;
1453         case lir_cond_lessEqual:    acond = Assembler::belowEqual; break;
1454         case lir_cond_greaterEqual: acond = Assembler::aboveEqual; break;
1455         case lir_cond_greater:      acond = Assembler::above;      break;
1456         default:                         ShouldNotReachHere();
1457       }
1458     } else {
1459       switch (op->cond()) {
1460         case lir_cond_equal:        acond = Assembler::equal;       break;
1461         case lir_cond_notEqual:     acond = Assembler::notEqual;    break;
1462         case lir_cond_less:         acond = Assembler::less;        break;
1463         case lir_cond_lessEqual:    acond = Assembler::lessEqual;   break;
1464         case lir_cond_greaterEqual: acond = Assembler::greaterEqual;break;
1465         case lir_cond_greater:      acond = Assembler::greater;     break;
1466         case lir_cond_belowEqual:   acond = Assembler::belowEqual;  break;
1467         case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;  break;
1468         default:                         ShouldNotReachHere();
1469       }
1470     }
1471     __ jcc(acond,*(op->label()));
1472   }
1473 }
1474 
1475 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
1476   LIR_Opr src  = op->in_opr();
1477   LIR_Opr dest = op->result_opr();
1478 
1479   switch (op->bytecode()) {
1480     case Bytecodes::_i2l:
1481 #ifdef _LP64
1482       __ movl2ptr(dest->as_register_lo(), src->as_register());
1483 #else
1484       move_regs(src->as_register(), dest->as_register_lo());
1485       move_regs(src->as_register(), dest->as_register_hi());
1486       __ sarl(dest->as_register_hi(), 31);
1487 #endif // LP64
1488       break;
1489 
1490     case Bytecodes::_l2i:
1491 #ifdef _LP64
1492       __ movl(dest->as_register(), src->as_register_lo());
1493 #else
1494       move_regs(src->as_register_lo(), dest->as_register());
1495 #endif
1496       break;
1497 
1498     case Bytecodes::_i2b:
1499       move_regs(src->as_register(), dest->as_register());
1500       __ sign_extend_byte(dest->as_register());
1501       break;
1502 
1503     case Bytecodes::_i2c:
1504       move_regs(src->as_register(), dest->as_register());
1505       __ andl(dest->as_register(), 0xFFFF);
1506       break;
1507 
1508     case Bytecodes::_i2s:
1509       move_regs(src->as_register(), dest->as_register());
1510       __ sign_extend_short(dest->as_register());
1511       break;
1512 
1513 
1514     case Bytecodes::_f2d:
1515     case Bytecodes::_d2f:
1516       if (dest->is_single_xmm()) {
1517         __ cvtsd2ss(dest->as_xmm_float_reg(), src->as_xmm_double_reg());
1518       } else if (dest->is_double_xmm()) {
1519         __ cvtss2sd(dest->as_xmm_double_reg(), src->as_xmm_float_reg());
1520       } else {
1521         assert(src->fpu() == dest->fpu(), "register must be equal");
1522         // do nothing (float result is rounded later through spilling)
1523       }
1524       break;
1525 
1526     case Bytecodes::_i2f:
1527     case Bytecodes::_i2d:
1528       if (dest->is_single_xmm()) {
1529         __ cvtsi2ssl(dest->as_xmm_float_reg(), src->as_register());
1530       } else if (dest->is_double_xmm()) {
1531         __ cvtsi2sdl(dest->as_xmm_double_reg(), src->as_register());
1532       } else {
1533         assert(dest->fpu() == 0, "result must be on TOS");
1534         __ movl(Address(rsp, 0), src->as_register());
1535         __ fild_s(Address(rsp, 0));
1536       }
1537       break;
1538 
1539     case Bytecodes::_f2i:
1540     case Bytecodes::_d2i:
1541       if (src->is_single_xmm()) {
1542         __ cvttss2sil(dest->as_register(), src->as_xmm_float_reg());
1543       } else if (src->is_double_xmm()) {
1544         __ cvttsd2sil(dest->as_register(), src->as_xmm_double_reg());
1545       } else {
1546         assert(src->fpu() == 0, "input must be on TOS");
1547         __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_trunc()));
1548         __ fist_s(Address(rsp, 0));
1549         __ movl(dest->as_register(), Address(rsp, 0));
1550         __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
1551       }
1552 
1553       // IA32 conversion instructions do not match JLS for overflow, underflow and NaN -> fixup in stub
1554       assert(op->stub() != NULL, "stub required");
1555       __ cmpl(dest->as_register(), 0x80000000);
1556       __ jcc(Assembler::equal, *op->stub()->entry());
1557       __ bind(*op->stub()->continuation());
1558       break;
1559 
1560     case Bytecodes::_l2f:
1561     case Bytecodes::_l2d:
1562       assert(!dest->is_xmm_register(), "result in xmm register not supported (no SSE instruction present)");
1563       assert(dest->fpu() == 0, "result must be on TOS");
1564 
1565       __ movptr(Address(rsp, 0),            src->as_register_lo());
1566       NOT_LP64(__ movl(Address(rsp, BytesPerWord), src->as_register_hi()));
1567       __ fild_d(Address(rsp, 0));
1568       // float result is rounded later through spilling
1569       break;
1570 
1571     case Bytecodes::_f2l:
1572     case Bytecodes::_d2l:
1573       assert(!src->is_xmm_register(), "input in xmm register not supported (no SSE instruction present)");
1574       assert(src->fpu() == 0, "input must be on TOS");
1575       assert(dest == FrameMap::long0_opr, "runtime stub places result in these registers");
1576 
1577       // instruction sequence too long to inline it here
1578       {
1579         __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::fpu2long_stub_id)));
1580       }
1581       break;
1582 
1583     default: ShouldNotReachHere();
1584   }
1585 }
1586 
1587 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
1588   if (op->init_check()) {
1589     add_debug_info_for_null_check_here(op->stub()->info());
1590     __ cmpb(Address(op->klass()->as_register(),
1591                     InstanceKlass::init_state_offset()),
1592                     InstanceKlass::fully_initialized);
1593     __ jcc(Assembler::notEqual, *op->stub()->entry());
1594   }
1595   __ allocate_object(op->obj()->as_register(),
1596                      op->tmp1()->as_register(),
1597                      op->tmp2()->as_register(),
1598                      op->header_size(),
1599                      op->object_size(),
1600                      op->klass()->as_register(),
1601                      *op->stub()->entry());
1602   __ bind(*op->stub()->continuation());
1603 }
1604 
1605 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
1606   Register len =  op->len()->as_register();
1607   LP64_ONLY( __ movslq(len, len); )
1608 
1609   if (UseSlowPath || op->type() == T_VALUETYPE ||
1610       (!UseFastNewObjectArray && (op->type() == T_OBJECT || op->type() == T_ARRAY)) ||
1611       (!UseFastNewTypeArray   && (op->type() != T_OBJECT && op->type() != T_ARRAY))) {
1612     __ jmp(*op->stub()->entry());
1613   } else {
1614     Register tmp1 = op->tmp1()->as_register();
1615     Register tmp2 = op->tmp2()->as_register();
1616     Register tmp3 = op->tmp3()->as_register();
1617     if (len == tmp1) {
1618       tmp1 = tmp3;
1619     } else if (len == tmp2) {
1620       tmp2 = tmp3;
1621     } else if (len == tmp3) {
1622       // everything is ok
1623     } else {
1624       __ mov(tmp3, len);
1625     }
1626     __ allocate_array(op->obj()->as_register(),
1627                       len,
1628                       tmp1,
1629                       tmp2,
1630                       arrayOopDesc::header_size(op->type()),
1631                       array_element_size(op->type()),
1632                       op->klass()->as_register(),
1633                       *op->stub()->entry());
1634   }
1635   __ bind(*op->stub()->continuation());
1636 }
1637 
1638 void LIR_Assembler::type_profile_helper(Register mdo,
1639                                         ciMethodData *md, ciProfileData *data,
1640                                         Register recv, Label* update_done) {
1641   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1642     Label next_test;
1643     // See if the receiver is receiver[n].
1644     __ cmpptr(recv, Address(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i))));
1645     __ jccb(Assembler::notEqual, next_test);
1646     Address data_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i)));
1647     __ addptr(data_addr, DataLayout::counter_increment);
1648     __ jmp(*update_done);
1649     __ bind(next_test);
1650   }
1651 
1652   // Didn't find receiver; find next empty slot and fill it in
1653   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1654     Label next_test;
1655     Address recv_addr(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_offset(i)));
1656     __ cmpptr(recv_addr, (intptr_t)NULL_WORD);
1657     __ jccb(Assembler::notEqual, next_test);
1658     __ movptr(recv_addr, recv);
1659     __ movptr(Address(mdo, md->byte_offset_of_slot(data, ReceiverTypeData::receiver_count_offset(i))), DataLayout::counter_increment);
1660     __ jmp(*update_done);
1661     __ bind(next_test);
1662   }
1663 }
1664 
1665 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
1666   // we always need a stub for the failure case.
1667   CodeStub* stub = op->stub();
1668   Register obj = op->object()->as_register();
1669   Register k_RInfo = op->tmp1()->as_register();
1670   Register klass_RInfo = op->tmp2()->as_register();
1671   Register dst = op->result_opr()->as_register();
1672   ciKlass* k = op->klass();
1673   Register Rtmp1 = noreg;
1674 
1675   // check if it needs to be profiled
1676   ciMethodData* md = NULL;
1677   ciProfileData* data = NULL;
1678 
1679   if (op->should_profile()) {
1680     ciMethod* method = op->profiled_method();
1681     assert(method != NULL, "Should have method");
1682     int bci = op->profiled_bci();
1683     md = method->method_data_or_null();
1684     assert(md != NULL, "Sanity");
1685     data = md->bci_to_data(bci);
1686     assert(data != NULL,                "need data for type check");
1687     assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1688   }
1689   Label profile_cast_success, profile_cast_failure;
1690   Label *success_target = op->should_profile() ? &profile_cast_success : success;
1691   Label *failure_target = op->should_profile() ? &profile_cast_failure : failure;
1692 
1693   if (obj == k_RInfo) {
1694     k_RInfo = dst;
1695   } else if (obj == klass_RInfo) {
1696     klass_RInfo = dst;
1697   }
1698   if (k->is_loaded() && !UseCompressedClassPointers) {
1699     select_different_registers(obj, dst, k_RInfo, klass_RInfo);
1700   } else {
1701     Rtmp1 = op->tmp3()->as_register();
1702     select_different_registers(obj, dst, k_RInfo, klass_RInfo, Rtmp1);
1703   }
1704 
1705   assert_different_registers(obj, k_RInfo, klass_RInfo);
1706 
1707   if (op->need_null_check()) {
1708     __ cmpptr(obj, (int32_t)NULL_WORD);
1709     if (op->should_profile()) {
1710       Label not_null;
1711       __ jccb(Assembler::notEqual, not_null);
1712       // Object is null; update MDO and exit
1713       Register mdo  = klass_RInfo;
1714       __ mov_metadata(mdo, md->constant_encoding());
1715       Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::flags_offset()));
1716       int header_bits = BitData::null_seen_byte_constant();
1717       __ orb(data_addr, header_bits);
1718       __ jmp(*obj_is_null);
1719       __ bind(not_null);
1720     } else {
1721       __ jcc(Assembler::equal, *obj_is_null);
1722     }
1723   }
1724 
1725   if (!k->is_loaded()) {
1726     klass2reg_with_patching(k_RInfo, op->info_for_patch());
1727   } else {
1728 #ifdef _LP64
1729     __ mov_metadata(k_RInfo, k->constant_encoding());
1730 #endif // _LP64
1731   }
1732   __ verify_oop(obj);
1733 
1734   if (op->fast_check()) {
1735     // get object class
1736     // not a safepoint as obj null check happens earlier
1737 #ifdef _LP64
1738     if (UseCompressedClassPointers) {
1739       __ load_klass(Rtmp1, obj);
1740       __ cmpptr(k_RInfo, Rtmp1);
1741     } else {
1742       __ cmpptr(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
1743     }
1744 #else
1745     if (k->is_loaded()) {
1746       __ cmpklass(Address(obj, oopDesc::klass_offset_in_bytes()), k->constant_encoding());
1747     } else {
1748       __ cmpptr(k_RInfo, Address(obj, oopDesc::klass_offset_in_bytes()));
1749     }
1750 #endif
1751     __ jcc(Assembler::notEqual, *failure_target);
1752     // successful cast, fall through to profile or jump
1753   } else {
1754     // get object class
1755     // not a safepoint as obj null check happens earlier
1756     __ load_klass(klass_RInfo, obj);
1757     if (k->is_loaded()) {
1758       // See if we get an immediate positive hit
1759 #ifdef _LP64
1760       __ cmpptr(k_RInfo, Address(klass_RInfo, k->super_check_offset()));
1761 #else
1762       __ cmpklass(Address(klass_RInfo, k->super_check_offset()), k->constant_encoding());
1763 #endif // _LP64
1764       if ((juint)in_bytes(Klass::secondary_super_cache_offset()) != k->super_check_offset()) {
1765         __ jcc(Assembler::notEqual, *failure_target);
1766         // successful cast, fall through to profile or jump
1767       } else {
1768         // See if we get an immediate positive hit
1769         __ jcc(Assembler::equal, *success_target);
1770         // check for self
1771 #ifdef _LP64
1772         __ cmpptr(klass_RInfo, k_RInfo);
1773 #else
1774         __ cmpklass(klass_RInfo, k->constant_encoding());
1775 #endif // _LP64
1776         __ jcc(Assembler::equal, *success_target);
1777 
1778         __ push(klass_RInfo);
1779 #ifdef _LP64
1780         __ push(k_RInfo);
1781 #else
1782         __ pushklass(k->constant_encoding());
1783 #endif // _LP64
1784         __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1785         __ pop(klass_RInfo);
1786         __ pop(klass_RInfo);
1787         // result is a boolean
1788         __ cmpl(klass_RInfo, 0);
1789         __ jcc(Assembler::equal, *failure_target);
1790         // successful cast, fall through to profile or jump
1791       }
1792     } else {
1793       // perform the fast part of the checking logic
1794       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
1795       // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1796       __ push(klass_RInfo);
1797       __ push(k_RInfo);
1798       __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1799       __ pop(klass_RInfo);
1800       __ pop(k_RInfo);
1801       // result is a boolean
1802       __ cmpl(k_RInfo, 0);
1803       __ jcc(Assembler::equal, *failure_target);
1804       // successful cast, fall through to profile or jump
1805     }
1806   }
1807   if (op->should_profile()) {
1808     Register mdo  = klass_RInfo, recv = k_RInfo;
1809     __ bind(profile_cast_success);
1810     __ mov_metadata(mdo, md->constant_encoding());
1811     __ load_klass(recv, obj);
1812     type_profile_helper(mdo, md, data, recv, success);
1813     __ jmp(*success);
1814 
1815     __ bind(profile_cast_failure);
1816     __ mov_metadata(mdo, md->constant_encoding());
1817     Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1818     __ subptr(counter_addr, DataLayout::counter_increment);
1819     __ jmp(*failure);
1820   }
1821   __ jmp(*success);
1822 }
1823 
1824 
1825 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
1826   LIR_Code code = op->code();
1827   if (code == lir_store_check) {
1828     Register value = op->object()->as_register();
1829     Register array = op->array()->as_register();
1830     Register k_RInfo = op->tmp1()->as_register();
1831     Register klass_RInfo = op->tmp2()->as_register();
1832     Register Rtmp1 = op->tmp3()->as_register();
1833 
1834     CodeStub* stub = op->stub();
1835 
1836     // check if it needs to be profiled
1837     ciMethodData* md = NULL;
1838     ciProfileData* data = NULL;
1839 
1840     if (op->should_profile()) {
1841       ciMethod* method = op->profiled_method();
1842       assert(method != NULL, "Should have method");
1843       int bci = op->profiled_bci();
1844       md = method->method_data_or_null();
1845       assert(md != NULL, "Sanity");
1846       data = md->bci_to_data(bci);
1847       assert(data != NULL,                "need data for type check");
1848       assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1849     }
1850     Label profile_cast_success, profile_cast_failure, done;
1851     Label *success_target = op->should_profile() ? &profile_cast_success : &done;
1852     Label *failure_target = op->should_profile() ? &profile_cast_failure : stub->entry();
1853 
1854     __ cmpptr(value, (int32_t)NULL_WORD);
1855     if (op->should_profile()) {
1856       Label not_null;
1857       __ jccb(Assembler::notEqual, not_null);
1858       // Object is null; update MDO and exit
1859       Register mdo  = klass_RInfo;
1860       __ mov_metadata(mdo, md->constant_encoding());
1861       Address data_addr(mdo, md->byte_offset_of_slot(data, DataLayout::flags_offset()));
1862       int header_bits = BitData::null_seen_byte_constant();
1863       __ orb(data_addr, header_bits);
1864       __ jmp(done);
1865       __ bind(not_null);
1866     } else {
1867       __ jcc(Assembler::equal, done);
1868     }
1869 
1870     add_debug_info_for_null_check_here(op->info_for_exception());
1871     __ load_klass(k_RInfo, array);
1872     __ load_klass(klass_RInfo, value);
1873 
1874     // get instance klass (it's already uncompressed)
1875     __ movptr(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
1876     // perform the fast part of the checking logic
1877     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, NULL);
1878     // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1879     __ push(klass_RInfo);
1880     __ push(k_RInfo);
1881     __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
1882     __ pop(klass_RInfo);
1883     __ pop(k_RInfo);
1884     // result is a boolean
1885     __ cmpl(k_RInfo, 0);
1886     __ jcc(Assembler::equal, *failure_target);
1887     // fall through to the success case
1888 
1889     if (op->should_profile()) {
1890       Register mdo  = klass_RInfo, recv = k_RInfo;
1891       __ bind(profile_cast_success);
1892       __ mov_metadata(mdo, md->constant_encoding());
1893       __ load_klass(recv, value);
1894       type_profile_helper(mdo, md, data, recv, &done);
1895       __ jmpb(done);
1896 
1897       __ bind(profile_cast_failure);
1898       __ mov_metadata(mdo, md->constant_encoding());
1899       Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1900       __ subptr(counter_addr, DataLayout::counter_increment);
1901       __ jmp(*stub->entry());
1902     }
1903 
1904     __ bind(done);
1905   } else
1906     if (code == lir_checkcast) {
1907       Register obj = op->object()->as_register();
1908       Register dst = op->result_opr()->as_register();
1909       Label success;
1910       emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
1911       __ bind(success);
1912       if (dst != obj) {
1913         __ mov(dst, obj);
1914       }
1915     } else
1916       if (code == lir_instanceof) {
1917         Register obj = op->object()->as_register();
1918         Register dst = op->result_opr()->as_register();
1919         Label success, failure, done;
1920         emit_typecheck_helper(op, &success, &failure, &failure);
1921         __ bind(failure);
1922         __ xorptr(dst, dst);
1923         __ jmpb(done);
1924         __ bind(success);
1925         __ movptr(dst, 1);
1926         __ bind(done);
1927       } else {
1928         ShouldNotReachHere();
1929       }
1930 
1931 }
1932 
1933 void LIR_Assembler::emit_opFlattenedStoreCheck(LIR_OpFlattenedStoreCheck* op) {
1934   Klass* k = (Klass*)(op->element_klass()->constant_encoding());
1935   assert(k->is_klass(), "must be a loaded klass");
1936   add_debug_info_for_null_check_here(op->info_for_exception());
1937 
1938 #ifdef _LP64
1939   if (UseCompressedClassPointers) {
1940     __ movl(op->tmp1()->as_register(), Address(op->object()->as_register(), oopDesc::klass_offset_in_bytes()));
1941     __ cmp_narrow_klass(op->tmp1()->as_register(), k);
1942   } else {
1943     __ movq(op->tmp1()->as_register(), Address(op->object()->as_register(), oopDesc::klass_offset_in_bytes()));
1944     __ cmpq(op->tmp1()->as_register(), op->tmp2()->as_register());
1945   }
1946 #else
1947   Unimplemented(); // FIXME
1948 #endif
1949 
1950   __ jcc(Assembler::notEqual, *op->stub()->entry());
1951   __ bind(*op->stub()->continuation());
1952 }
1953 
1954 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
1955   if (LP64_ONLY(false &&) op->code() == lir_cas_long && VM_Version::supports_cx8()) {
1956     assert(op->cmp_value()->as_register_lo() == rax, "wrong register");
1957     assert(op->cmp_value()->as_register_hi() == rdx, "wrong register");
1958     assert(op->new_value()->as_register_lo() == rbx, "wrong register");
1959     assert(op->new_value()->as_register_hi() == rcx, "wrong register");
1960     Register addr = op->addr()->as_register();
1961     __ lock();
1962     NOT_LP64(__ cmpxchg8(Address(addr, 0)));
1963 
1964   } else if (op->code() == lir_cas_int || op->code() == lir_cas_obj ) {
1965     NOT_LP64(assert(op->addr()->is_single_cpu(), "must be single");)
1966     Register addr = (op->addr()->is_single_cpu() ? op->addr()->as_register() : op->addr()->as_register_lo());
1967     Register newval = op->new_value()->as_register();
1968     Register cmpval = op->cmp_value()->as_register();
1969     assert(cmpval == rax, "wrong register");
1970     assert(newval != NULL, "new val must be register");
1971     assert(cmpval != newval, "cmp and new values must be in different registers");
1972     assert(cmpval != addr, "cmp and addr must be in different registers");
1973     assert(newval != addr, "new value and addr must be in different registers");
1974 
1975     if ( op->code() == lir_cas_obj) {
1976 #ifdef _LP64
1977       if (UseCompressedOops) {
1978         __ encode_heap_oop(cmpval);
1979         __ mov(rscratch1, newval);
1980         __ encode_heap_oop(rscratch1);
1981         __ lock();
1982         // cmpval (rax) is implicitly used by this instruction
1983         __ cmpxchgl(rscratch1, Address(addr, 0));
1984       } else
1985 #endif
1986       {
1987         __ lock();
1988         __ cmpxchgptr(newval, Address(addr, 0));
1989       }
1990     } else {
1991       assert(op->code() == lir_cas_int, "lir_cas_int expected");
1992       __ lock();
1993       __ cmpxchgl(newval, Address(addr, 0));
1994     }
1995 #ifdef _LP64
1996   } else if (op->code() == lir_cas_long) {
1997     Register addr = (op->addr()->is_single_cpu() ? op->addr()->as_register() : op->addr()->as_register_lo());
1998     Register newval = op->new_value()->as_register_lo();
1999     Register cmpval = op->cmp_value()->as_register_lo();
2000     assert(cmpval == rax, "wrong register");
2001     assert(newval != NULL, "new val must be register");
2002     assert(cmpval != newval, "cmp and new values must be in different registers");
2003     assert(cmpval != addr, "cmp and addr must be in different registers");
2004     assert(newval != addr, "new value and addr must be in different registers");
2005     __ lock();
2006     __ cmpxchgq(newval, Address(addr, 0));
2007 #endif // _LP64
2008   } else {
2009     Unimplemented();
2010   }
2011 }
2012 
2013 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type) {
2014   Assembler::Condition acond, ncond;
2015   switch (condition) {
2016     case lir_cond_equal:        acond = Assembler::equal;        ncond = Assembler::notEqual;     break;
2017     case lir_cond_notEqual:     acond = Assembler::notEqual;     ncond = Assembler::equal;        break;
2018     case lir_cond_less:         acond = Assembler::less;         ncond = Assembler::greaterEqual; break;
2019     case lir_cond_lessEqual:    acond = Assembler::lessEqual;    ncond = Assembler::greater;      break;
2020     case lir_cond_greaterEqual: acond = Assembler::greaterEqual; ncond = Assembler::less;         break;
2021     case lir_cond_greater:      acond = Assembler::greater;      ncond = Assembler::lessEqual;    break;
2022     case lir_cond_belowEqual:   acond = Assembler::belowEqual;   ncond = Assembler::above;        break;
2023     case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;   ncond = Assembler::below;        break;
2024     default:                    acond = Assembler::equal;        ncond = Assembler::notEqual;
2025                                 ShouldNotReachHere();
2026   }
2027 
2028   if (opr1->is_cpu_register()) {
2029     reg2reg(opr1, result);
2030   } else if (opr1->is_stack()) {
2031     stack2reg(opr1, result, result->type());
2032   } else if (opr1->is_constant()) {
2033     const2reg(opr1, result, lir_patch_none, NULL);
2034   } else {
2035     ShouldNotReachHere();
2036   }
2037 
2038   if (VM_Version::supports_cmov() && !opr2->is_constant()) {
2039     // optimized version that does not require a branch
2040     if (opr2->is_single_cpu()) {
2041       assert(opr2->cpu_regnr() != result->cpu_regnr(), "opr2 already overwritten by previous move");
2042       __ cmov(ncond, result->as_register(), opr2->as_register());
2043     } else if (opr2->is_double_cpu()) {
2044       assert(opr2->cpu_regnrLo() != result->cpu_regnrLo() && opr2->cpu_regnrLo() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
2045       assert(opr2->cpu_regnrHi() != result->cpu_regnrLo() && opr2->cpu_regnrHi() != result->cpu_regnrHi(), "opr2 already overwritten by previous move");
2046       __ cmovptr(ncond, result->as_register_lo(), opr2->as_register_lo());
2047       NOT_LP64(__ cmovptr(ncond, result->as_register_hi(), opr2->as_register_hi());)
2048     } else if (opr2->is_single_stack()) {
2049       __ cmovl(ncond, result->as_register(), frame_map()->address_for_slot(opr2->single_stack_ix()));
2050     } else if (opr2->is_double_stack()) {
2051       __ cmovptr(ncond, result->as_register_lo(), frame_map()->address_for_slot(opr2->double_stack_ix(), lo_word_offset_in_bytes));
2052       NOT_LP64(__ cmovptr(ncond, result->as_register_hi(), frame_map()->address_for_slot(opr2->double_stack_ix(), hi_word_offset_in_bytes));)
2053     } else {
2054       ShouldNotReachHere();
2055     }
2056 
2057   } else {
2058     Label skip;
2059     __ jcc (acond, skip);
2060     if (opr2->is_cpu_register()) {
2061       reg2reg(opr2, result);
2062     } else if (opr2->is_stack()) {
2063       stack2reg(opr2, result, result->type());
2064     } else if (opr2->is_constant()) {
2065       const2reg(opr2, result, lir_patch_none, NULL);
2066     } else {
2067       ShouldNotReachHere();
2068     }
2069     __ bind(skip);
2070   }
2071 }
2072 
2073 
2074 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info, bool pop_fpu_stack) {
2075   assert(info == NULL, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
2076 
2077   if (left->is_single_cpu()) {
2078     assert(left == dest, "left and dest must be equal");
2079     Register lreg = left->as_register();
2080 
2081     if (right->is_single_cpu()) {
2082       // cpu register - cpu register
2083       Register rreg = right->as_register();
2084       switch (code) {
2085         case lir_add: __ addl (lreg, rreg); break;
2086         case lir_sub: __ subl (lreg, rreg); break;
2087         case lir_mul: __ imull(lreg, rreg); break;
2088         default:      ShouldNotReachHere();
2089       }
2090 
2091     } else if (right->is_stack()) {
2092       // cpu register - stack
2093       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
2094       switch (code) {
2095         case lir_add: __ addl(lreg, raddr); break;
2096         case lir_sub: __ subl(lreg, raddr); break;
2097         default:      ShouldNotReachHere();
2098       }
2099 
2100     } else if (right->is_constant()) {
2101       // cpu register - constant
2102       jint c = right->as_constant_ptr()->as_jint();
2103       switch (code) {
2104         case lir_add: {
2105           __ incrementl(lreg, c);
2106           break;
2107         }
2108         case lir_sub: {
2109           __ decrementl(lreg, c);
2110           break;
2111         }
2112         default: ShouldNotReachHere();
2113       }
2114 
2115     } else {
2116       ShouldNotReachHere();
2117     }
2118 
2119   } else if (left->is_double_cpu()) {
2120     assert(left == dest, "left and dest must be equal");
2121     Register lreg_lo = left->as_register_lo();
2122     Register lreg_hi = left->as_register_hi();
2123 
2124     if (right->is_double_cpu()) {
2125       // cpu register - cpu register
2126       Register rreg_lo = right->as_register_lo();
2127       Register rreg_hi = right->as_register_hi();
2128       NOT_LP64(assert_different_registers(lreg_lo, lreg_hi, rreg_lo, rreg_hi));
2129       LP64_ONLY(assert_different_registers(lreg_lo, rreg_lo));
2130       switch (code) {
2131         case lir_add:
2132           __ addptr(lreg_lo, rreg_lo);
2133           NOT_LP64(__ adcl(lreg_hi, rreg_hi));
2134           break;
2135         case lir_sub:
2136           __ subptr(lreg_lo, rreg_lo);
2137           NOT_LP64(__ sbbl(lreg_hi, rreg_hi));
2138           break;
2139         case lir_mul:
2140 #ifdef _LP64
2141           __ imulq(lreg_lo, rreg_lo);
2142 #else
2143           assert(lreg_lo == rax && lreg_hi == rdx, "must be");
2144           __ imull(lreg_hi, rreg_lo);
2145           __ imull(rreg_hi, lreg_lo);
2146           __ addl (rreg_hi, lreg_hi);
2147           __ mull (rreg_lo);
2148           __ addl (lreg_hi, rreg_hi);
2149 #endif // _LP64
2150           break;
2151         default:
2152           ShouldNotReachHere();
2153       }
2154 
2155     } else if (right->is_constant()) {
2156       // cpu register - constant
2157 #ifdef _LP64
2158       jlong c = right->as_constant_ptr()->as_jlong_bits();
2159       __ movptr(r10, (intptr_t) c);
2160       switch (code) {
2161         case lir_add:
2162           __ addptr(lreg_lo, r10);
2163           break;
2164         case lir_sub:
2165           __ subptr(lreg_lo, r10);
2166           break;
2167         default:
2168           ShouldNotReachHere();
2169       }
2170 #else
2171       jint c_lo = right->as_constant_ptr()->as_jint_lo();
2172       jint c_hi = right->as_constant_ptr()->as_jint_hi();
2173       switch (code) {
2174         case lir_add:
2175           __ addptr(lreg_lo, c_lo);
2176           __ adcl(lreg_hi, c_hi);
2177           break;
2178         case lir_sub:
2179           __ subptr(lreg_lo, c_lo);
2180           __ sbbl(lreg_hi, c_hi);
2181           break;
2182         default:
2183           ShouldNotReachHere();
2184       }
2185 #endif // _LP64
2186 
2187     } else {
2188       ShouldNotReachHere();
2189     }
2190 
2191   } else if (left->is_single_xmm()) {
2192     assert(left == dest, "left and dest must be equal");
2193     XMMRegister lreg = left->as_xmm_float_reg();
2194 
2195     if (right->is_single_xmm()) {
2196       XMMRegister rreg = right->as_xmm_float_reg();
2197       switch (code) {
2198         case lir_add: __ addss(lreg, rreg);  break;
2199         case lir_sub: __ subss(lreg, rreg);  break;
2200         case lir_mul_strictfp: // fall through
2201         case lir_mul: __ mulss(lreg, rreg);  break;
2202         case lir_div_strictfp: // fall through
2203         case lir_div: __ divss(lreg, rreg);  break;
2204         default: ShouldNotReachHere();
2205       }
2206     } else {
2207       Address raddr;
2208       if (right->is_single_stack()) {
2209         raddr = frame_map()->address_for_slot(right->single_stack_ix());
2210       } else if (right->is_constant()) {
2211         // hack for now
2212         raddr = __ as_Address(InternalAddress(float_constant(right->as_jfloat())));
2213       } else {
2214         ShouldNotReachHere();
2215       }
2216       switch (code) {
2217         case lir_add: __ addss(lreg, raddr);  break;
2218         case lir_sub: __ subss(lreg, raddr);  break;
2219         case lir_mul_strictfp: // fall through
2220         case lir_mul: __ mulss(lreg, raddr);  break;
2221         case lir_div_strictfp: // fall through
2222         case lir_div: __ divss(lreg, raddr);  break;
2223         default: ShouldNotReachHere();
2224       }
2225     }
2226 
2227   } else if (left->is_double_xmm()) {
2228     assert(left == dest, "left and dest must be equal");
2229 
2230     XMMRegister lreg = left->as_xmm_double_reg();
2231     if (right->is_double_xmm()) {
2232       XMMRegister rreg = right->as_xmm_double_reg();
2233       switch (code) {
2234         case lir_add: __ addsd(lreg, rreg);  break;
2235         case lir_sub: __ subsd(lreg, rreg);  break;
2236         case lir_mul_strictfp: // fall through
2237         case lir_mul: __ mulsd(lreg, rreg);  break;
2238         case lir_div_strictfp: // fall through
2239         case lir_div: __ divsd(lreg, rreg);  break;
2240         default: ShouldNotReachHere();
2241       }
2242     } else {
2243       Address raddr;
2244       if (right->is_double_stack()) {
2245         raddr = frame_map()->address_for_slot(right->double_stack_ix());
2246       } else if (right->is_constant()) {
2247         // hack for now
2248         raddr = __ as_Address(InternalAddress(double_constant(right->as_jdouble())));
2249       } else {
2250         ShouldNotReachHere();
2251       }
2252       switch (code) {
2253         case lir_add: __ addsd(lreg, raddr);  break;
2254         case lir_sub: __ subsd(lreg, raddr);  break;
2255         case lir_mul_strictfp: // fall through
2256         case lir_mul: __ mulsd(lreg, raddr);  break;
2257         case lir_div_strictfp: // fall through
2258         case lir_div: __ divsd(lreg, raddr);  break;
2259         default: ShouldNotReachHere();
2260       }
2261     }
2262 
2263   } else if (left->is_single_fpu()) {
2264     assert(dest->is_single_fpu(),  "fpu stack allocation required");
2265 
2266     if (right->is_single_fpu()) {
2267       arith_fpu_implementation(code, left->fpu_regnr(), right->fpu_regnr(), dest->fpu_regnr(), pop_fpu_stack);
2268 
2269     } else {
2270       assert(left->fpu_regnr() == 0, "left must be on TOS");
2271       assert(dest->fpu_regnr() == 0, "dest must be on TOS");
2272 
2273       Address raddr;
2274       if (right->is_single_stack()) {
2275         raddr = frame_map()->address_for_slot(right->single_stack_ix());
2276       } else if (right->is_constant()) {
2277         address const_addr = float_constant(right->as_jfloat());
2278         assert(const_addr != NULL, "incorrect float/double constant maintainance");
2279         // hack for now
2280         raddr = __ as_Address(InternalAddress(const_addr));
2281       } else {
2282         ShouldNotReachHere();
2283       }
2284 
2285       switch (code) {
2286         case lir_add: __ fadd_s(raddr); break;
2287         case lir_sub: __ fsub_s(raddr); break;
2288         case lir_mul_strictfp: // fall through
2289         case lir_mul: __ fmul_s(raddr); break;
2290         case lir_div_strictfp: // fall through
2291         case lir_div: __ fdiv_s(raddr); break;
2292         default:      ShouldNotReachHere();
2293       }
2294     }
2295 
2296   } else if (left->is_double_fpu()) {
2297     assert(dest->is_double_fpu(),  "fpu stack allocation required");
2298 
2299     if (code == lir_mul_strictfp || code == lir_div_strictfp) {
2300       // Double values require special handling for strictfp mul/div on x86
2301       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias1()));
2302       __ fmulp(left->fpu_regnrLo() + 1);
2303     }
2304 
2305     if (right->is_double_fpu()) {
2306       arith_fpu_implementation(code, left->fpu_regnrLo(), right->fpu_regnrLo(), dest->fpu_regnrLo(), pop_fpu_stack);
2307 
2308     } else {
2309       assert(left->fpu_regnrLo() == 0, "left must be on TOS");
2310       assert(dest->fpu_regnrLo() == 0, "dest must be on TOS");
2311 
2312       Address raddr;
2313       if (right->is_double_stack()) {
2314         raddr = frame_map()->address_for_slot(right->double_stack_ix());
2315       } else if (right->is_constant()) {
2316         // hack for now
2317         raddr = __ as_Address(InternalAddress(double_constant(right->as_jdouble())));
2318       } else {
2319         ShouldNotReachHere();
2320       }
2321 
2322       switch (code) {
2323         case lir_add: __ fadd_d(raddr); break;
2324         case lir_sub: __ fsub_d(raddr); break;
2325         case lir_mul_strictfp: // fall through
2326         case lir_mul: __ fmul_d(raddr); break;
2327         case lir_div_strictfp: // fall through
2328         case lir_div: __ fdiv_d(raddr); break;
2329         default: ShouldNotReachHere();
2330       }
2331     }
2332 
2333     if (code == lir_mul_strictfp || code == lir_div_strictfp) {
2334       // Double values require special handling for strictfp mul/div on x86
2335       __ fld_x(ExternalAddress(StubRoutines::addr_fpu_subnormal_bias2()));
2336       __ fmulp(dest->fpu_regnrLo() + 1);
2337     }
2338 
2339   } else if (left->is_single_stack() || left->is_address()) {
2340     assert(left == dest, "left and dest must be equal");
2341 
2342     Address laddr;
2343     if (left->is_single_stack()) {
2344       laddr = frame_map()->address_for_slot(left->single_stack_ix());
2345     } else if (left->is_address()) {
2346       laddr = as_Address(left->as_address_ptr());
2347     } else {
2348       ShouldNotReachHere();
2349     }
2350 
2351     if (right->is_single_cpu()) {
2352       Register rreg = right->as_register();
2353       switch (code) {
2354         case lir_add: __ addl(laddr, rreg); break;
2355         case lir_sub: __ subl(laddr, rreg); break;
2356         default:      ShouldNotReachHere();
2357       }
2358     } else if (right->is_constant()) {
2359       jint c = right->as_constant_ptr()->as_jint();
2360       switch (code) {
2361         case lir_add: {
2362           __ incrementl(laddr, c);
2363           break;
2364         }
2365         case lir_sub: {
2366           __ decrementl(laddr, c);
2367           break;
2368         }
2369         default: ShouldNotReachHere();
2370       }
2371     } else {
2372       ShouldNotReachHere();
2373     }
2374 
2375   } else {
2376     ShouldNotReachHere();
2377   }
2378 }
2379 
2380 void LIR_Assembler::arith_fpu_implementation(LIR_Code code, int left_index, int right_index, int dest_index, bool pop_fpu_stack) {
2381   assert(pop_fpu_stack  || (left_index     == dest_index || right_index     == dest_index), "invalid LIR");
2382   assert(!pop_fpu_stack || (left_index - 1 == dest_index || right_index - 1 == dest_index), "invalid LIR");
2383   assert(left_index == 0 || right_index == 0, "either must be on top of stack");
2384 
2385   bool left_is_tos = (left_index == 0);
2386   bool dest_is_tos = (dest_index == 0);
2387   int non_tos_index = (left_is_tos ? right_index : left_index);
2388 
2389   switch (code) {
2390     case lir_add:
2391       if (pop_fpu_stack)       __ faddp(non_tos_index);
2392       else if (dest_is_tos)    __ fadd (non_tos_index);
2393       else                     __ fadda(non_tos_index);
2394       break;
2395 
2396     case lir_sub:
2397       if (left_is_tos) {
2398         if (pop_fpu_stack)     __ fsubrp(non_tos_index);
2399         else if (dest_is_tos)  __ fsub  (non_tos_index);
2400         else                   __ fsubra(non_tos_index);
2401       } else {
2402         if (pop_fpu_stack)     __ fsubp (non_tos_index);
2403         else if (dest_is_tos)  __ fsubr (non_tos_index);
2404         else                   __ fsuba (non_tos_index);
2405       }
2406       break;
2407 
2408     case lir_mul_strictfp: // fall through
2409     case lir_mul:
2410       if (pop_fpu_stack)       __ fmulp(non_tos_index);
2411       else if (dest_is_tos)    __ fmul (non_tos_index);
2412       else                     __ fmula(non_tos_index);
2413       break;
2414 
2415     case lir_div_strictfp: // fall through
2416     case lir_div:
2417       if (left_is_tos) {
2418         if (pop_fpu_stack)     __ fdivrp(non_tos_index);
2419         else if (dest_is_tos)  __ fdiv  (non_tos_index);
2420         else                   __ fdivra(non_tos_index);
2421       } else {
2422         if (pop_fpu_stack)     __ fdivp (non_tos_index);
2423         else if (dest_is_tos)  __ fdivr (non_tos_index);
2424         else                   __ fdiva (non_tos_index);
2425       }
2426       break;
2427 
2428     case lir_rem:
2429       assert(left_is_tos && dest_is_tos && right_index == 1, "must be guaranteed by FPU stack allocation");
2430       __ fremr(noreg);
2431       break;
2432 
2433     default:
2434       ShouldNotReachHere();
2435   }
2436 }
2437 
2438 
2439 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr tmp, LIR_Opr dest, LIR_Op* op) {
2440   if (value->is_double_xmm()) {
2441     switch(code) {
2442       case lir_abs :
2443         {
2444 #ifdef _LP64
2445           if (UseAVX > 2 && !VM_Version::supports_avx512vl()) {
2446             assert(tmp->is_valid(), "need temporary");
2447             __ vpandn(dest->as_xmm_double_reg(), tmp->as_xmm_double_reg(), value->as_xmm_double_reg(), 2);
2448           } else
2449 #endif
2450           {
2451             if (dest->as_xmm_double_reg() != value->as_xmm_double_reg()) {
2452               __ movdbl(dest->as_xmm_double_reg(), value->as_xmm_double_reg());
2453             }
2454             assert(!tmp->is_valid(), "do not need temporary");
2455             __ andpd(dest->as_xmm_double_reg(),
2456                      ExternalAddress((address)double_signmask_pool));
2457           }
2458         }
2459         break;
2460 
2461       case lir_sqrt: __ sqrtsd(dest->as_xmm_double_reg(), value->as_xmm_double_reg()); break;
2462       // all other intrinsics are not available in the SSE instruction set, so FPU is used
2463       default      : ShouldNotReachHere();
2464     }
2465 
2466   } else if (value->is_double_fpu()) {
2467     assert(value->fpu_regnrLo() == 0 && dest->fpu_regnrLo() == 0, "both must be on TOS");
2468     switch(code) {
2469       case lir_abs   : __ fabs() ; break;
2470       case lir_sqrt  : __ fsqrt(); break;
2471       default      : ShouldNotReachHere();
2472     }
2473   } else {
2474     Unimplemented();
2475   }
2476 }
2477 
2478 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
2479   // assert(left->destroys_register(), "check");
2480   if (left->is_single_cpu()) {
2481     Register reg = left->as_register();
2482     if (right->is_constant()) {
2483       int val = right->as_constant_ptr()->as_jint();
2484       switch (code) {
2485         case lir_logic_and: __ andl (reg, val); break;
2486         case lir_logic_or:  __ orl  (reg, val); break;
2487         case lir_logic_xor: __ xorl (reg, val); break;
2488         default: ShouldNotReachHere();
2489       }
2490     } else if (right->is_stack()) {
2491       // added support for stack operands
2492       Address raddr = frame_map()->address_for_slot(right->single_stack_ix());
2493       switch (code) {
2494         case lir_logic_and: __ andl (reg, raddr); break;
2495         case lir_logic_or:  __ orl  (reg, raddr); break;
2496         case lir_logic_xor: __ xorl (reg, raddr); break;
2497         default: ShouldNotReachHere();
2498       }
2499     } else {
2500       Register rright = right->as_register();
2501       switch (code) {
2502         case lir_logic_and: __ andptr (reg, rright); break;
2503         case lir_logic_or : __ orptr  (reg, rright); break;
2504         case lir_logic_xor: __ xorptr (reg, rright); break;
2505         default: ShouldNotReachHere();
2506       }
2507     }
2508     move_regs(reg, dst->as_register());
2509   } else {
2510     Register l_lo = left->as_register_lo();
2511     Register l_hi = left->as_register_hi();
2512     if (right->is_constant()) {
2513 #ifdef _LP64
2514       __ mov64(rscratch1, right->as_constant_ptr()->as_jlong());
2515       switch (code) {
2516         case lir_logic_and:
2517           __ andq(l_lo, rscratch1);
2518           break;
2519         case lir_logic_or:
2520           __ orq(l_lo, rscratch1);
2521           break;
2522         case lir_logic_xor:
2523           __ xorq(l_lo, rscratch1);
2524           break;
2525         default: ShouldNotReachHere();
2526       }
2527 #else
2528       int r_lo = right->as_constant_ptr()->as_jint_lo();
2529       int r_hi = right->as_constant_ptr()->as_jint_hi();
2530       switch (code) {
2531         case lir_logic_and:
2532           __ andl(l_lo, r_lo);
2533           __ andl(l_hi, r_hi);
2534           break;
2535         case lir_logic_or:
2536           __ orl(l_lo, r_lo);
2537           __ orl(l_hi, r_hi);
2538           break;
2539         case lir_logic_xor:
2540           __ xorl(l_lo, r_lo);
2541           __ xorl(l_hi, r_hi);
2542           break;
2543         default: ShouldNotReachHere();
2544       }
2545 #endif // _LP64
2546     } else {
2547 #ifdef _LP64
2548       Register r_lo;
2549       if (right->type() == T_OBJECT || right->type() == T_ARRAY || right->type() == T_VALUETYPE) {
2550         r_lo = right->as_register();
2551       } else {
2552         r_lo = right->as_register_lo();
2553       }
2554 #else
2555       Register r_lo = right->as_register_lo();
2556       Register r_hi = right->as_register_hi();
2557       assert(l_lo != r_hi, "overwriting registers");
2558 #endif
2559       switch (code) {
2560         case lir_logic_and:
2561           __ andptr(l_lo, r_lo);
2562           NOT_LP64(__ andptr(l_hi, r_hi);)
2563           break;
2564         case lir_logic_or:
2565           __ orptr(l_lo, r_lo);
2566           NOT_LP64(__ orptr(l_hi, r_hi);)
2567           break;
2568         case lir_logic_xor:
2569           __ xorptr(l_lo, r_lo);
2570           NOT_LP64(__ xorptr(l_hi, r_hi);)
2571           break;
2572         default: ShouldNotReachHere();
2573       }
2574     }
2575 
2576     Register dst_lo = dst->as_register_lo();
2577     Register dst_hi = dst->as_register_hi();
2578 
2579 #ifdef _LP64
2580     move_regs(l_lo, dst_lo);
2581 #else
2582     if (dst_lo == l_hi) {
2583       assert(dst_hi != l_lo, "overwriting registers");
2584       move_regs(l_hi, dst_hi);
2585       move_regs(l_lo, dst_lo);
2586     } else {
2587       assert(dst_lo != l_hi, "overwriting registers");
2588       move_regs(l_lo, dst_lo);
2589       move_regs(l_hi, dst_hi);
2590     }
2591 #endif // _LP64
2592   }
2593 }
2594 
2595 
2596 // we assume that rax, and rdx can be overwritten
2597 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr temp, LIR_Opr result, CodeEmitInfo* info) {
2598 
2599   assert(left->is_single_cpu(),   "left must be register");
2600   assert(right->is_single_cpu() || right->is_constant(),  "right must be register or constant");
2601   assert(result->is_single_cpu(), "result must be register");
2602 
2603   //  assert(left->destroys_register(), "check");
2604   //  assert(right->destroys_register(), "check");
2605 
2606   Register lreg = left->as_register();
2607   Register dreg = result->as_register();
2608 
2609   if (right->is_constant()) {
2610     jint divisor = right->as_constant_ptr()->as_jint();
2611     assert(divisor > 0 && is_power_of_2(divisor), "must be");
2612     if (code == lir_idiv) {
2613       assert(lreg == rax, "must be rax,");
2614       assert(temp->as_register() == rdx, "tmp register must be rdx");
2615       __ cdql(); // sign extend into rdx:rax
2616       if (divisor == 2) {
2617         __ subl(lreg, rdx);
2618       } else {
2619         __ andl(rdx, divisor - 1);
2620         __ addl(lreg, rdx);
2621       }
2622       __ sarl(lreg, log2_jint(divisor));
2623       move_regs(lreg, dreg);
2624     } else if (code == lir_irem) {
2625       Label done;
2626       __ mov(dreg, lreg);
2627       __ andl(dreg, 0x80000000 | (divisor - 1));
2628       __ jcc(Assembler::positive, done);
2629       __ decrement(dreg);
2630       __ orl(dreg, ~(divisor - 1));
2631       __ increment(dreg);
2632       __ bind(done);
2633     } else {
2634       ShouldNotReachHere();
2635     }
2636   } else {
2637     Register rreg = right->as_register();
2638     assert(lreg == rax, "left register must be rax,");
2639     assert(rreg != rdx, "right register must not be rdx");
2640     assert(temp->as_register() == rdx, "tmp register must be rdx");
2641 
2642     move_regs(lreg, rax);
2643 
2644     int idivl_offset = __ corrected_idivl(rreg);
2645     if (ImplicitDiv0Checks) {
2646       add_debug_info_for_div0(idivl_offset, info);
2647     }
2648     if (code == lir_irem) {
2649       move_regs(rdx, dreg); // result is in rdx
2650     } else {
2651       move_regs(rax, dreg);
2652     }
2653   }
2654 }
2655 
2656 
2657 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
2658   if (opr1->is_single_cpu()) {
2659     Register reg1 = opr1->as_register();
2660     if (opr2->is_single_cpu()) {
2661       // cpu register - cpu register
2662       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY || opr1->type() == T_VALUETYPE) {
2663         __ cmpoop(reg1, opr2->as_register());
2664       } else {
2665         assert(opr2->type() != T_OBJECT && opr2->type() != T_ARRAY && opr2->type() != T_VALUETYPE, "cmp int, oop?");
2666         __ cmpl(reg1, opr2->as_register());
2667       }
2668     } else if (opr2->is_stack()) {
2669       // cpu register - stack
2670       if (opr1->type() == T_OBJECT || opr1->type() == T_ARRAY || opr1->type() == T_VALUETYPE) {
2671         __ cmpoop(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2672       } else {
2673         __ cmpl(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2674       }
2675     } else if (opr2->is_constant()) {
2676       // cpu register - constant
2677       LIR_Const* c = opr2->as_constant_ptr();
2678       if (c->type() == T_INT) {
2679         __ cmpl(reg1, c->as_jint());
2680       } else if (c->type() == T_OBJECT || c->type() == T_ARRAY || c->type() == T_VALUETYPE) {
2681         // In 64bit oops are single register
2682         jobject o = c->as_jobject();
2683         if (o == NULL) {
2684           __ cmpptr(reg1, (int32_t)NULL_WORD);
2685         } else {
2686           __ cmpoop(reg1, o);
2687         }
2688       } else {
2689         fatal("unexpected type: %s", basictype_to_str(c->type()));
2690       }
2691       // cpu register - address
2692     } else if (opr2->is_address()) {
2693       if (op->info() != NULL) {
2694         add_debug_info_for_null_check_here(op->info());
2695       }
2696       __ cmpl(reg1, as_Address(opr2->as_address_ptr()));
2697     } else {
2698       ShouldNotReachHere();
2699     }
2700 
2701   } else if(opr1->is_double_cpu()) {
2702     Register xlo = opr1->as_register_lo();
2703     Register xhi = opr1->as_register_hi();
2704     if (opr2->is_double_cpu()) {
2705 #ifdef _LP64
2706       __ cmpptr(xlo, opr2->as_register_lo());
2707 #else
2708       // cpu register - cpu register
2709       Register ylo = opr2->as_register_lo();
2710       Register yhi = opr2->as_register_hi();
2711       __ subl(xlo, ylo);
2712       __ sbbl(xhi, yhi);
2713       if (condition == lir_cond_equal || condition == lir_cond_notEqual) {
2714         __ orl(xhi, xlo);
2715       }
2716 #endif // _LP64
2717     } else if (opr2->is_constant()) {
2718       // cpu register - constant 0
2719       assert(opr2->as_jlong() == (jlong)0, "only handles zero");
2720 #ifdef _LP64
2721       __ cmpptr(xlo, (int32_t)opr2->as_jlong());
2722 #else
2723       assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "only handles equals case");
2724       __ orl(xhi, xlo);
2725 #endif // _LP64
2726     } else {
2727       ShouldNotReachHere();
2728     }
2729 
2730   } else if (opr1->is_single_xmm()) {
2731     XMMRegister reg1 = opr1->as_xmm_float_reg();
2732     if (opr2->is_single_xmm()) {
2733       // xmm register - xmm register
2734       __ ucomiss(reg1, opr2->as_xmm_float_reg());
2735     } else if (opr2->is_stack()) {
2736       // xmm register - stack
2737       __ ucomiss(reg1, frame_map()->address_for_slot(opr2->single_stack_ix()));
2738     } else if (opr2->is_constant()) {
2739       // xmm register - constant
2740       __ ucomiss(reg1, InternalAddress(float_constant(opr2->as_jfloat())));
2741     } else if (opr2->is_address()) {
2742       // xmm register - address
2743       if (op->info() != NULL) {
2744         add_debug_info_for_null_check_here(op->info());
2745       }
2746       __ ucomiss(reg1, as_Address(opr2->as_address_ptr()));
2747     } else {
2748       ShouldNotReachHere();
2749     }
2750 
2751   } else if (opr1->is_double_xmm()) {
2752     XMMRegister reg1 = opr1->as_xmm_double_reg();
2753     if (opr2->is_double_xmm()) {
2754       // xmm register - xmm register
2755       __ ucomisd(reg1, opr2->as_xmm_double_reg());
2756     } else if (opr2->is_stack()) {
2757       // xmm register - stack
2758       __ ucomisd(reg1, frame_map()->address_for_slot(opr2->double_stack_ix()));
2759     } else if (opr2->is_constant()) {
2760       // xmm register - constant
2761       __ ucomisd(reg1, InternalAddress(double_constant(opr2->as_jdouble())));
2762     } else if (opr2->is_address()) {
2763       // xmm register - address
2764       if (op->info() != NULL) {
2765         add_debug_info_for_null_check_here(op->info());
2766       }
2767       __ ucomisd(reg1, as_Address(opr2->pointer()->as_address()));
2768     } else {
2769       ShouldNotReachHere();
2770     }
2771 
2772   } else if(opr1->is_single_fpu() || opr1->is_double_fpu()) {
2773     assert(opr1->is_fpu_register() && opr1->fpu() == 0, "currently left-hand side must be on TOS (relax this restriction)");
2774     assert(opr2->is_fpu_register(), "both must be registers");
2775     __ fcmp(noreg, opr2->fpu(), op->fpu_pop_count() > 0, op->fpu_pop_count() > 1);
2776 
2777   } else if (opr1->is_address() && opr2->is_constant()) {
2778     LIR_Const* c = opr2->as_constant_ptr();
2779 #ifdef _LP64
2780     if (c->type() == T_OBJECT || c->type() == T_ARRAY || c->type() == T_VALUETYPE) {
2781       assert(condition == lir_cond_equal || condition == lir_cond_notEqual, "need to reverse");
2782       __ movoop(rscratch1, c->as_jobject());
2783     }
2784 #endif // LP64
2785     if (op->info() != NULL) {
2786       add_debug_info_for_null_check_here(op->info());
2787     }
2788     // special case: address - constant
2789     LIR_Address* addr = opr1->as_address_ptr();
2790     if (c->type() == T_INT) {
2791       __ cmpl(as_Address(addr), c->as_jint());
2792     } else if (c->type() == T_OBJECT || c->type() == T_ARRAY || c->type() == T_VALUETYPE) {
2793 #ifdef _LP64
2794       // %%% Make this explode if addr isn't reachable until we figure out a
2795       // better strategy by giving noreg as the temp for as_Address
2796       __ cmpoop(rscratch1, as_Address(addr, noreg));
2797 #else
2798       __ cmpoop(as_Address(addr), c->as_jobject());
2799 #endif // _LP64
2800     } else {
2801       ShouldNotReachHere();
2802     }
2803 
2804   } else {
2805     ShouldNotReachHere();
2806   }
2807 }
2808 
2809 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op) {
2810   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
2811     if (left->is_single_xmm()) {
2812       assert(right->is_single_xmm(), "must match");
2813       __ cmpss2int(left->as_xmm_float_reg(), right->as_xmm_float_reg(), dst->as_register(), code == lir_ucmp_fd2i);
2814     } else if (left->is_double_xmm()) {
2815       assert(right->is_double_xmm(), "must match");
2816       __ cmpsd2int(left->as_xmm_double_reg(), right->as_xmm_double_reg(), dst->as_register(), code == lir_ucmp_fd2i);
2817 
2818     } else {
2819       assert(left->is_single_fpu() || left->is_double_fpu(), "must be");
2820       assert(right->is_single_fpu() || right->is_double_fpu(), "must match");
2821 
2822       assert(left->fpu() == 0, "left must be on TOS");
2823       __ fcmp2int(dst->as_register(), code == lir_ucmp_fd2i, right->fpu(),
2824                   op->fpu_pop_count() > 0, op->fpu_pop_count() > 1);
2825     }
2826   } else {
2827     assert(code == lir_cmp_l2i, "check");
2828 #ifdef _LP64
2829     Label done;
2830     Register dest = dst->as_register();
2831     __ cmpptr(left->as_register_lo(), right->as_register_lo());
2832     __ movl(dest, -1);
2833     __ jccb(Assembler::less, done);
2834     __ set_byte_if_not_zero(dest);
2835     __ movzbl(dest, dest);
2836     __ bind(done);
2837 #else
2838     __ lcmp2int(left->as_register_hi(),
2839                 left->as_register_lo(),
2840                 right->as_register_hi(),
2841                 right->as_register_lo());
2842     move_regs(left->as_register_hi(), dst->as_register());
2843 #endif // _LP64
2844   }
2845 }
2846 
2847 
2848 void LIR_Assembler::align_call(LIR_Code code) {
2849   // make sure that the displacement word of the call ends up word aligned
2850   int offset = __ offset();
2851   switch (code) {
2852   case lir_static_call:
2853   case lir_optvirtual_call:
2854   case lir_dynamic_call:
2855     offset += NativeCall::displacement_offset;
2856     break;
2857   case lir_icvirtual_call:
2858     offset += NativeCall::displacement_offset + NativeMovConstReg::instruction_size;
2859     break;
2860   case lir_virtual_call:  // currently, sparc-specific for niagara
2861   default: ShouldNotReachHere();
2862   }
2863   __ align(BytesPerWord, offset);
2864 }
2865 
2866 
2867 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
2868   assert((__ offset() + NativeCall::displacement_offset) % BytesPerWord == 0,
2869          "must be aligned");
2870   __ call(AddressLiteral(op->addr(), rtype));
2871   add_call_info(code_offset(), op->info());
2872 }
2873 
2874 
2875 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
2876   __ ic_call(op->addr());
2877   add_call_info(code_offset(), op->info());
2878   assert((__ offset() - NativeCall::instruction_size + NativeCall::displacement_offset) % BytesPerWord == 0,
2879          "must be aligned");
2880 }
2881 
2882 
2883 /* Currently, vtable-dispatch is only enabled for sparc platforms */
2884 void LIR_Assembler::vtable_call(LIR_OpJavaCall* op) {
2885   ShouldNotReachHere();
2886 }
2887 
2888 
2889 void LIR_Assembler::emit_static_call_stub() {
2890   address call_pc = __ pc();
2891   address stub = __ start_a_stub(call_stub_size());
2892   if (stub == NULL) {
2893     bailout("static call stub overflow");
2894     return;
2895   }
2896 
2897   int start = __ offset();
2898 
2899   // make sure that the displacement word of the call ends up word aligned
2900   __ align(BytesPerWord, __ offset() + NativeMovConstReg::instruction_size + NativeCall::displacement_offset);
2901   __ relocate(static_stub_Relocation::spec(call_pc, false /* is_aot */));
2902   __ mov_metadata(rbx, (Metadata*)NULL);
2903   // must be set to -1 at code generation time
2904   assert(((__ offset() + 1) % BytesPerWord) == 0, "must be aligned");
2905   // On 64bit this will die since it will take a movq & jmp, must be only a jmp
2906   __ jump(RuntimeAddress(__ pc()));
2907 
2908   if (UseAOT) {
2909     // Trampoline to aot code
2910     __ relocate(static_stub_Relocation::spec(call_pc, true /* is_aot */));
2911 #ifdef _LP64
2912     __ mov64(rax, CONST64(0));  // address is zapped till fixup time.
2913 #else
2914     __ movl(rax, 0xdeadffff);  // address is zapped till fixup time.
2915 #endif
2916     __ jmp(rax);
2917   }
2918   assert(__ offset() - start <= call_stub_size(), "stub too big");
2919   __ end_a_stub();
2920 }
2921 
2922 
2923 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
2924   assert(exceptionOop->as_register() == rax, "must match");
2925   assert(exceptionPC->as_register() == rdx, "must match");
2926 
2927   // exception object is not added to oop map by LinearScan
2928   // (LinearScan assumes that no oops are in fixed registers)
2929   info->add_register_oop(exceptionOop);
2930   Runtime1::StubID unwind_id;
2931 
2932   // get current pc information
2933   // pc is only needed if the method has an exception handler, the unwind code does not need it.
2934   int pc_for_athrow_offset = __ offset();
2935   InternalAddress pc_for_athrow(__ pc());
2936   __ lea(exceptionPC->as_register(), pc_for_athrow);
2937   add_call_info(pc_for_athrow_offset, info); // for exception handler
2938 
2939   __ verify_not_null_oop(rax);
2940   // search an exception handler (rax: exception oop, rdx: throwing pc)
2941   if (compilation()->has_fpu_code()) {
2942     unwind_id = Runtime1::handle_exception_id;
2943   } else {
2944     unwind_id = Runtime1::handle_exception_nofpu_id;
2945   }
2946   __ call(RuntimeAddress(Runtime1::entry_for(unwind_id)));
2947 
2948   // enough room for two byte trap
2949   __ nop();
2950 }
2951 
2952 
2953 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
2954   assert(exceptionOop->as_register() == rax, "must match");
2955 
2956   __ jmp(_unwind_handler_entry);
2957 }
2958 
2959 
2960 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2961 
2962   // optimized version for linear scan:
2963   // * count must be already in ECX (guaranteed by LinearScan)
2964   // * left and dest must be equal
2965   // * tmp must be unused
2966   assert(count->as_register() == SHIFT_count, "count must be in ECX");
2967   assert(left == dest, "left and dest must be equal");
2968   assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2969 
2970   if (left->is_single_cpu()) {
2971     Register value = left->as_register();
2972     assert(value != SHIFT_count, "left cannot be ECX");
2973 
2974     switch (code) {
2975       case lir_shl:  __ shll(value); break;
2976       case lir_shr:  __ sarl(value); break;
2977       case lir_ushr: __ shrl(value); break;
2978       default: ShouldNotReachHere();
2979     }
2980   } else if (left->is_double_cpu()) {
2981     Register lo = left->as_register_lo();
2982     Register hi = left->as_register_hi();
2983     assert(lo != SHIFT_count && hi != SHIFT_count, "left cannot be ECX");
2984 #ifdef _LP64
2985     switch (code) {
2986       case lir_shl:  __ shlptr(lo);        break;
2987       case lir_shr:  __ sarptr(lo);        break;
2988       case lir_ushr: __ shrptr(lo);        break;
2989       default: ShouldNotReachHere();
2990     }
2991 #else
2992 
2993     switch (code) {
2994       case lir_shl:  __ lshl(hi, lo);        break;
2995       case lir_shr:  __ lshr(hi, lo, true);  break;
2996       case lir_ushr: __ lshr(hi, lo, false); break;
2997       default: ShouldNotReachHere();
2998     }
2999 #endif // LP64
3000   } else {
3001     ShouldNotReachHere();
3002   }
3003 }
3004 
3005 
3006 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
3007   if (dest->is_single_cpu()) {
3008     // first move left into dest so that left is not destroyed by the shift
3009     Register value = dest->as_register();
3010     count = count & 0x1F; // Java spec
3011 
3012     move_regs(left->as_register(), value);
3013     switch (code) {
3014       case lir_shl:  __ shll(value, count); break;
3015       case lir_shr:  __ sarl(value, count); break;
3016       case lir_ushr: __ shrl(value, count); break;
3017       default: ShouldNotReachHere();
3018     }
3019   } else if (dest->is_double_cpu()) {
3020 #ifndef _LP64
3021     Unimplemented();
3022 #else
3023     // first move left into dest so that left is not destroyed by the shift
3024     Register value = dest->as_register_lo();
3025     count = count & 0x1F; // Java spec
3026 
3027     move_regs(left->as_register_lo(), value);
3028     switch (code) {
3029       case lir_shl:  __ shlptr(value, count); break;
3030       case lir_shr:  __ sarptr(value, count); break;
3031       case lir_ushr: __ shrptr(value, count); break;
3032       default: ShouldNotReachHere();
3033     }
3034 #endif // _LP64
3035   } else {
3036     ShouldNotReachHere();
3037   }
3038 }
3039 
3040 
3041 void LIR_Assembler::store_parameter(Register r, int offset_from_rsp_in_words) {
3042   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
3043   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
3044   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
3045   __ movptr (Address(rsp, offset_from_rsp_in_bytes), r);
3046 }
3047 
3048 
3049 void LIR_Assembler::store_parameter(jint c,     int offset_from_rsp_in_words) {
3050   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
3051   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
3052   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
3053   __ movptr (Address(rsp, offset_from_rsp_in_bytes), c);
3054 }
3055 
3056 
3057 void LIR_Assembler::store_parameter(jobject o,  int offset_from_rsp_in_words) {
3058   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
3059   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
3060   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
3061   __ movoop (Address(rsp, offset_from_rsp_in_bytes), o);
3062 }
3063 
3064 
3065 void LIR_Assembler::store_parameter(Metadata* m,  int offset_from_rsp_in_words) {
3066   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
3067   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
3068   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
3069   __ mov_metadata(Address(rsp, offset_from_rsp_in_bytes), m);
3070 }
3071 
3072 
3073 void LIR_Assembler::arraycopy_flat_check(Register obj, Register tmp, CodeStub* slow_path) {
3074   Address klass_addr = Address(obj, oopDesc::klass_offset_in_bytes());
3075   if (UseCompressedClassPointers) {
3076     __ movl(tmp, klass_addr);
3077     LP64_ONLY(__ decode_klass_not_null(tmp));
3078   } else {
3079     __ movptr(tmp, klass_addr);
3080   }
3081   __ movl(tmp, Address(tmp, Klass::layout_helper_offset()));
3082   __ sarl(tmp, Klass::_lh_array_tag_shift);
3083   __ cmpl(tmp, Klass::_lh_array_tag_vt_value);
3084   __ jcc(Assembler::equal, *slow_path->entry());
3085 }
3086 
3087 
3088 // This code replaces a call to arraycopy; no exception may
3089 // be thrown in this code, they must be thrown in the System.arraycopy
3090 // activation frame; we could save some checks if this would not be the case
3091 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
3092   ciArrayKlass* default_type = op->expected_type();
3093   Register src = op->src()->as_register();
3094   Register dst = op->dst()->as_register();
3095   Register src_pos = op->src_pos()->as_register();
3096   Register dst_pos = op->dst_pos()->as_register();
3097   Register length  = op->length()->as_register();
3098   Register tmp = op->tmp()->as_register();
3099 
3100   __ resolve(ACCESS_READ, src);
3101   __ resolve(ACCESS_WRITE, dst);
3102 
3103   CodeStub* stub = op->stub();
3104   int flags = op->flags();
3105   BasicType basic_type = default_type != NULL ? default_type->element_type()->basic_type() : T_ILLEGAL;
3106   if (basic_type == T_ARRAY) basic_type = T_OBJECT;
3107 
3108   if (flags & LIR_OpArrayCopy::always_slow_path) {
3109     __ jmp(*stub->entry());
3110     __ bind(*stub->continuation());
3111     return;
3112   }
3113 
3114   if (flags & LIR_OpArrayCopy::src_flat_check) {
3115     arraycopy_flat_check(src, tmp, stub);
3116   }
3117 
3118   if (flags & LIR_OpArrayCopy::dst_flat_check) {
3119     arraycopy_flat_check(dst, tmp, stub);
3120   }
3121 
3122   if (basic_type == T_VALUETYPE) {
3123     assert(flags & (LIR_OpArrayCopy::always_slow_path |
3124                     LIR_OpArrayCopy::src_flat_check |
3125                     LIR_OpArrayCopy::dst_flat_check), "must have checked");
3126     // If either src or dst is (or maybe) a flattened array, one of the 3 checks
3127     // above would have caught it, and taken the slow path. So when we come here,
3128     // the array must be a (non-flat) object array.
3129     basic_type = T_OBJECT;
3130   }
3131 
3132   // if we don't know anything, just go through the generic arraycopy
3133   if (default_type == NULL) {
3134     // save outgoing arguments on stack in case call to System.arraycopy is needed
3135     // HACK ALERT. This code used to push the parameters in a hardwired fashion
3136     // for interpreter calling conventions. Now we have to do it in new style conventions.
3137     // For the moment until C1 gets the new register allocator I just force all the
3138     // args to the right place (except the register args) and then on the back side
3139     // reload the register args properly if we go slow path. Yuck
3140 
3141     // These are proper for the calling convention
3142     store_parameter(length, 2);
3143     store_parameter(dst_pos, 1);
3144     store_parameter(dst, 0);
3145 
3146     // these are just temporary placements until we need to reload
3147     store_parameter(src_pos, 3);
3148     store_parameter(src, 4);
3149     NOT_LP64(assert(src == rcx && src_pos == rdx, "mismatch in calling convention");)
3150 
3151     address copyfunc_addr = StubRoutines::generic_arraycopy();
3152     assert(copyfunc_addr != NULL, "generic arraycopy stub required");
3153 
3154     // pass arguments: may push as this is not a safepoint; SP must be fix at each safepoint
3155 #ifdef _LP64
3156     // The arguments are in java calling convention so we can trivially shift them to C
3157     // convention
3158     assert_different_registers(c_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4);
3159     __ mov(c_rarg0, j_rarg0);
3160     assert_different_registers(c_rarg1, j_rarg2, j_rarg3, j_rarg4);
3161     __ mov(c_rarg1, j_rarg1);
3162     assert_different_registers(c_rarg2, j_rarg3, j_rarg4);
3163     __ mov(c_rarg2, j_rarg2);
3164     assert_different_registers(c_rarg3, j_rarg4);
3165     __ mov(c_rarg3, j_rarg3);
3166 #ifdef _WIN64
3167     // Allocate abi space for args but be sure to keep stack aligned
3168     __ subptr(rsp, 6*wordSize);
3169     store_parameter(j_rarg4, 4);
3170 #ifndef PRODUCT
3171     if (PrintC1Statistics) {
3172       __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3173     }
3174 #endif
3175     __ call(RuntimeAddress(copyfunc_addr));
3176     __ addptr(rsp, 6*wordSize);
3177 #else
3178     __ mov(c_rarg4, j_rarg4);
3179 #ifndef PRODUCT
3180     if (PrintC1Statistics) {
3181       __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3182     }
3183 #endif
3184     __ call(RuntimeAddress(copyfunc_addr));
3185 #endif // _WIN64
3186 #else
3187     __ push(length);
3188     __ push(dst_pos);
3189     __ push(dst);
3190     __ push(src_pos);
3191     __ push(src);
3192 
3193 #ifndef PRODUCT
3194     if (PrintC1Statistics) {
3195       __ incrementl(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
3196     }
3197 #endif
3198     __ call_VM_leaf(copyfunc_addr, 5); // removes pushed parameter from the stack
3199 
3200 #endif // _LP64
3201 
3202     __ cmpl(rax, 0);
3203     __ jcc(Assembler::equal, *stub->continuation());
3204 
3205     __ mov(tmp, rax);
3206     __ xorl(tmp, -1);
3207 
3208     // Reload values from the stack so they are where the stub
3209     // expects them.
3210     __ movptr   (dst,     Address(rsp, 0*BytesPerWord));
3211     __ movptr   (dst_pos, Address(rsp, 1*BytesPerWord));
3212     __ movptr   (length,  Address(rsp, 2*BytesPerWord));
3213     __ movptr   (src_pos, Address(rsp, 3*BytesPerWord));
3214     __ movptr   (src,     Address(rsp, 4*BytesPerWord));
3215 
3216     __ subl(length, tmp);
3217     __ addl(src_pos, tmp);
3218     __ addl(dst_pos, tmp);
3219     __ jmp(*stub->entry());
3220 
3221     __ bind(*stub->continuation());
3222     return;
3223   }
3224 
3225   assert(default_type != NULL && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
3226 
3227   int elem_size = type2aelembytes(basic_type);
3228   Address::ScaleFactor scale;
3229 
3230   switch (elem_size) {
3231     case 1 :
3232       scale = Address::times_1;
3233       break;
3234     case 2 :
3235       scale = Address::times_2;
3236       break;
3237     case 4 :
3238       scale = Address::times_4;
3239       break;
3240     case 8 :
3241       scale = Address::times_8;
3242       break;
3243     default:
3244       scale = Address::no_scale;
3245       ShouldNotReachHere();
3246   }
3247 
3248   Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
3249   Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
3250   Address src_klass_addr = Address(src, oopDesc::klass_offset_in_bytes());
3251   Address dst_klass_addr = Address(dst, oopDesc::klass_offset_in_bytes());
3252 
3253   // length and pos's are all sign extended at this point on 64bit
3254 
3255   // test for NULL
3256   if (flags & LIR_OpArrayCopy::src_null_check) {
3257     __ testptr(src, src);
3258     __ jcc(Assembler::zero, *stub->entry());
3259   }
3260   if (flags & LIR_OpArrayCopy::dst_null_check) {
3261     __ testptr(dst, dst);
3262     __ jcc(Assembler::zero, *stub->entry());
3263   }
3264 
3265   // If the compiler was not able to prove that exact type of the source or the destination
3266   // of the arraycopy is an array type, check at runtime if the source or the destination is
3267   // an instance type.
3268   if (flags & LIR_OpArrayCopy::type_check) {
3269     if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
3270       __ load_klass(tmp, dst);
3271       __ cmpl(Address(tmp, in_bytes(Klass::layout_helper_offset())), Klass::_lh_neutral_value);
3272       __ jcc(Assembler::greaterEqual, *stub->entry());
3273     }
3274 
3275     if (!(flags & LIR_OpArrayCopy::src_objarray)) {
3276       __ load_klass(tmp, src);
3277       __ cmpl(Address(tmp, in_bytes(Klass::layout_helper_offset())), Klass::_lh_neutral_value);
3278       __ jcc(Assembler::greaterEqual, *stub->entry());
3279     }
3280   }
3281 
3282   // check if negative
3283   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
3284     __ testl(src_pos, src_pos);
3285     __ jcc(Assembler::less, *stub->entry());
3286   }
3287   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
3288     __ testl(dst_pos, dst_pos);
3289     __ jcc(Assembler::less, *stub->entry());
3290   }
3291 
3292   if (flags & LIR_OpArrayCopy::src_range_check) {
3293     __ lea(tmp, Address(src_pos, length, Address::times_1, 0));
3294     __ cmpl(tmp, src_length_addr);
3295     __ jcc(Assembler::above, *stub->entry());
3296   }
3297   if (flags & LIR_OpArrayCopy::dst_range_check) {
3298     __ lea(tmp, Address(dst_pos, length, Address::times_1, 0));
3299     __ cmpl(tmp, dst_length_addr);
3300     __ jcc(Assembler::above, *stub->entry());
3301   }
3302 
3303   if (flags & LIR_OpArrayCopy::length_positive_check) {
3304     __ testl(length, length);
3305     __ jcc(Assembler::less, *stub->entry());
3306   }
3307 
3308 #ifdef _LP64
3309   __ movl2ptr(src_pos, src_pos); //higher 32bits must be null
3310   __ movl2ptr(dst_pos, dst_pos); //higher 32bits must be null
3311 #endif
3312 
3313   if (flags & LIR_OpArrayCopy::type_check) {
3314     // We don't know the array types are compatible
3315     if (basic_type != T_OBJECT) {
3316       // Simple test for basic type arrays
3317       if (UseCompressedClassPointers) {
3318         __ movl(tmp, src_klass_addr);
3319         __ cmpl(tmp, dst_klass_addr);
3320       } else {
3321         __ movptr(tmp, src_klass_addr);
3322         __ cmpptr(tmp, dst_klass_addr);
3323       }
3324       __ jcc(Assembler::notEqual, *stub->entry());
3325     } else {
3326       // For object arrays, if src is a sub class of dst then we can
3327       // safely do the copy.
3328       Label cont, slow;
3329 
3330       __ push(src);
3331       __ push(dst);
3332 
3333       __ load_klass(src, src);
3334       __ load_klass(dst, dst);
3335 
3336       __ check_klass_subtype_fast_path(src, dst, tmp, &cont, &slow, NULL);
3337 
3338       __ push(src);
3339       __ push(dst);
3340       __ call(RuntimeAddress(Runtime1::entry_for(Runtime1::slow_subtype_check_id)));
3341       __ pop(dst);
3342       __ pop(src);
3343 
3344       __ cmpl(src, 0);
3345       __ jcc(Assembler::notEqual, cont);
3346 
3347       __ bind(slow);
3348       __ pop(dst);
3349       __ pop(src);
3350 
3351       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
3352       if (copyfunc_addr != NULL) { // use stub if available
3353         // src is not a sub class of dst so we have to do a
3354         // per-element check.
3355 
3356         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
3357         if ((flags & mask) != mask) {
3358           // Check that at least both of them object arrays.
3359           assert(flags & mask, "one of the two should be known to be an object array");
3360 
3361           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
3362             __ load_klass(tmp, src);
3363           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
3364             __ load_klass(tmp, dst);
3365           }
3366           int lh_offset = in_bytes(Klass::layout_helper_offset());
3367           Address klass_lh_addr(tmp, lh_offset);
3368           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
3369           __ cmpl(klass_lh_addr, objArray_lh);
3370           __ jcc(Assembler::notEqual, *stub->entry());
3371         }
3372 
3373        // Spill because stubs can use any register they like and it's
3374        // easier to restore just those that we care about.
3375        store_parameter(dst, 0);
3376        store_parameter(dst_pos, 1);
3377        store_parameter(length, 2);
3378        store_parameter(src_pos, 3);
3379        store_parameter(src, 4);
3380 
3381 #ifndef _LP64
3382         __ movptr(tmp, dst_klass_addr);
3383         __ movptr(tmp, Address(tmp, ObjArrayKlass::element_klass_offset()));
3384         __ push(tmp);
3385         __ movl(tmp, Address(tmp, Klass::super_check_offset_offset()));
3386         __ push(tmp);
3387         __ push(length);
3388         __ lea(tmp, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3389         __ push(tmp);
3390         __ lea(tmp, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3391         __ push(tmp);
3392 
3393         __ call_VM_leaf(copyfunc_addr, 5);
3394 #else
3395         __ movl2ptr(length, length); //higher 32bits must be null
3396 
3397         __ lea(c_rarg0, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3398         assert_different_registers(c_rarg0, dst, dst_pos, length);
3399         __ lea(c_rarg1, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3400         assert_different_registers(c_rarg1, dst, length);
3401 
3402         __ mov(c_rarg2, length);
3403         assert_different_registers(c_rarg2, dst);
3404 
3405 #ifdef _WIN64
3406         // Allocate abi space for args but be sure to keep stack aligned
3407         __ subptr(rsp, 6*wordSize);
3408         __ load_klass(c_rarg3, dst);
3409         __ movptr(c_rarg3, Address(c_rarg3, ObjArrayKlass::element_klass_offset()));
3410         store_parameter(c_rarg3, 4);
3411         __ movl(c_rarg3, Address(c_rarg3, Klass::super_check_offset_offset()));
3412         __ call(RuntimeAddress(copyfunc_addr));
3413         __ addptr(rsp, 6*wordSize);
3414 #else
3415         __ load_klass(c_rarg4, dst);
3416         __ movptr(c_rarg4, Address(c_rarg4, ObjArrayKlass::element_klass_offset()));
3417         __ movl(c_rarg3, Address(c_rarg4, Klass::super_check_offset_offset()));
3418         __ call(RuntimeAddress(copyfunc_addr));
3419 #endif
3420 
3421 #endif
3422 
3423 #ifndef PRODUCT
3424         if (PrintC1Statistics) {
3425           Label failed;
3426           __ testl(rax, rax);
3427           __ jcc(Assembler::notZero, failed);
3428           __ incrementl(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_cnt));
3429           __ bind(failed);
3430         }
3431 #endif
3432 
3433         __ testl(rax, rax);
3434         __ jcc(Assembler::zero, *stub->continuation());
3435 
3436 #ifndef PRODUCT
3437         if (PrintC1Statistics) {
3438           __ incrementl(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_attempt_cnt));
3439         }
3440 #endif
3441 
3442         __ mov(tmp, rax);
3443 
3444         __ xorl(tmp, -1);
3445 
3446         // Restore previously spilled arguments
3447         __ movptr   (dst,     Address(rsp, 0*BytesPerWord));
3448         __ movptr   (dst_pos, Address(rsp, 1*BytesPerWord));
3449         __ movptr   (length,  Address(rsp, 2*BytesPerWord));
3450         __ movptr   (src_pos, Address(rsp, 3*BytesPerWord));
3451         __ movptr   (src,     Address(rsp, 4*BytesPerWord));
3452 
3453 
3454         __ subl(length, tmp);
3455         __ addl(src_pos, tmp);
3456         __ addl(dst_pos, tmp);
3457       }
3458 
3459       __ jmp(*stub->entry());
3460 
3461       __ bind(cont);
3462       __ pop(dst);
3463       __ pop(src);
3464     }
3465   }
3466 
3467 #ifdef ASSERT
3468   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
3469     // Sanity check the known type with the incoming class.  For the
3470     // primitive case the types must match exactly with src.klass and
3471     // dst.klass each exactly matching the default type.  For the
3472     // object array case, if no type check is needed then either the
3473     // dst type is exactly the expected type and the src type is a
3474     // subtype which we can't check or src is the same array as dst
3475     // but not necessarily exactly of type default_type.
3476     Label known_ok, halt;
3477     __ mov_metadata(tmp, default_type->constant_encoding());
3478 #ifdef _LP64
3479     if (UseCompressedClassPointers) {
3480       __ encode_klass_not_null(tmp);
3481     }
3482 #endif
3483 
3484     if (basic_type != T_OBJECT) {
3485 
3486       if (UseCompressedClassPointers)          __ cmpl(tmp, dst_klass_addr);
3487       else                   __ cmpptr(tmp, dst_klass_addr);
3488       __ jcc(Assembler::notEqual, halt);
3489       if (UseCompressedClassPointers)          __ cmpl(tmp, src_klass_addr);
3490       else                   __ cmpptr(tmp, src_klass_addr);
3491       __ jcc(Assembler::equal, known_ok);
3492     } else {
3493       if (UseCompressedClassPointers)          __ cmpl(tmp, dst_klass_addr);
3494       else                   __ cmpptr(tmp, dst_klass_addr);
3495       __ jcc(Assembler::equal, known_ok);
3496       __ cmpptr(src, dst);
3497       __ jcc(Assembler::equal, known_ok);
3498     }
3499     __ bind(halt);
3500     __ stop("incorrect type information in arraycopy");
3501     __ bind(known_ok);
3502   }
3503 #endif
3504 
3505 #ifndef PRODUCT
3506   if (PrintC1Statistics) {
3507     __ incrementl(ExternalAddress(Runtime1::arraycopy_count_address(basic_type)));
3508   }
3509 #endif
3510 
3511 #ifdef _LP64
3512   assert_different_registers(c_rarg0, dst, dst_pos, length);
3513   __ lea(c_rarg0, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3514   assert_different_registers(c_rarg1, length);
3515   __ lea(c_rarg1, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3516   __ mov(c_rarg2, length);
3517 
3518 #else
3519   __ lea(tmp, Address(src, src_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3520   store_parameter(tmp, 0);
3521   __ lea(tmp, Address(dst, dst_pos, scale, arrayOopDesc::base_offset_in_bytes(basic_type)));
3522   store_parameter(tmp, 1);
3523   store_parameter(length, 2);
3524 #endif // _LP64
3525 
3526   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
3527   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
3528   const char *name;
3529   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
3530   __ call_VM_leaf(entry, 0);
3531 
3532   __ bind(*stub->continuation());
3533 }
3534 
3535 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
3536   assert(op->crc()->is_single_cpu(),  "crc must be register");
3537   assert(op->val()->is_single_cpu(),  "byte value must be register");
3538   assert(op->result_opr()->is_single_cpu(), "result must be register");
3539   Register crc = op->crc()->as_register();
3540   Register val = op->val()->as_register();
3541   Register res = op->result_opr()->as_register();
3542 
3543   assert_different_registers(val, crc, res);
3544 
3545   __ lea(res, ExternalAddress(StubRoutines::crc_table_addr()));
3546   __ notl(crc); // ~crc
3547   __ update_byte_crc32(crc, val, res);
3548   __ notl(crc); // ~crc
3549   __ mov(res, crc);
3550 }
3551 
3552 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
3553   Register obj = op->obj_opr()->as_register();  // may not be an oop
3554   Register hdr = op->hdr_opr()->as_register();
3555   Register lock = op->lock_opr()->as_register();
3556   if (!UseFastLocking) {
3557     __ jmp(*op->stub()->entry());
3558   } else if (op->code() == lir_lock) {
3559     Register scratch = noreg;
3560     if (UseBiasedLocking) {
3561       scratch = op->scratch_opr()->as_register();
3562     }
3563     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
3564     __ resolve(ACCESS_READ | ACCESS_WRITE, obj);
3565     // add debug info for NullPointerException only if one is possible
3566     int null_check_offset = __ lock_object(hdr, obj, lock, scratch, *op->stub()->entry());
3567     if (op->info() != NULL) {
3568       add_debug_info_for_null_check(null_check_offset, op->info());
3569     }
3570     // done
3571   } else if (op->code() == lir_unlock) {
3572     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
3573     __ unlock_object(hdr, obj, lock, *op->stub()->entry());
3574   } else {
3575     Unimplemented();
3576   }
3577   __ bind(*op->stub()->continuation());
3578 }
3579 
3580 
3581 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
3582   ciMethod* method = op->profiled_method();
3583   int bci          = op->profiled_bci();
3584   ciMethod* callee = op->profiled_callee();
3585 
3586   // Update counter for all call types
3587   ciMethodData* md = method->method_data_or_null();
3588   assert(md != NULL, "Sanity");
3589   ciProfileData* data = md->bci_to_data(bci);
3590   assert(data != NULL && data->is_CounterData(), "need CounterData for calls");
3591   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
3592   Register mdo  = op->mdo()->as_register();
3593   __ mov_metadata(mdo, md->constant_encoding());
3594   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
3595   // Perform additional virtual call profiling for invokevirtual and
3596   // invokeinterface bytecodes
3597   if (op->should_profile_receiver_type()) {
3598     assert(op->recv()->is_single_cpu(), "recv must be allocated");
3599     Register recv = op->recv()->as_register();
3600     assert_different_registers(mdo, recv);
3601     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
3602     ciKlass* known_klass = op->known_holder();
3603     if (C1OptimizeVirtualCallProfiling && known_klass != NULL) {
3604       // We know the type that will be seen at this call site; we can
3605       // statically update the MethodData* rather than needing to do
3606       // dynamic tests on the receiver type
3607 
3608       // NOTE: we should probably put a lock around this search to
3609       // avoid collisions by concurrent compilations
3610       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
3611       uint i;
3612       for (i = 0; i < VirtualCallData::row_limit(); i++) {
3613         ciKlass* receiver = vc_data->receiver(i);
3614         if (known_klass->equals(receiver)) {
3615           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
3616           __ addptr(data_addr, DataLayout::counter_increment);
3617           return;
3618         }
3619       }
3620 
3621       // Receiver type not found in profile data; select an empty slot
3622 
3623       // Note that this is less efficient than it should be because it
3624       // always does a write to the receiver part of the
3625       // VirtualCallData rather than just the first time
3626       for (i = 0; i < VirtualCallData::row_limit(); i++) {
3627         ciKlass* receiver = vc_data->receiver(i);
3628         if (receiver == NULL) {
3629           Address recv_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)));
3630           __ mov_metadata(recv_addr, known_klass->constant_encoding());
3631           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
3632           __ addptr(data_addr, DataLayout::counter_increment);
3633           return;
3634         }
3635       }
3636     } else {
3637       __ load_klass(recv, recv);
3638       Label update_done;
3639       type_profile_helper(mdo, md, data, recv, &update_done);
3640       // Receiver did not match any saved receiver and there is no empty row for it.
3641       // Increment total counter to indicate polymorphic case.
3642       __ addptr(counter_addr, DataLayout::counter_increment);
3643 
3644       __ bind(update_done);
3645     }
3646   } else {
3647     // Static call
3648     __ addptr(counter_addr, DataLayout::counter_increment);
3649   }
3650 }
3651 
3652 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
3653   Register obj = op->obj()->as_register();
3654   Register tmp = op->tmp()->as_pointer_register();
3655   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
3656   ciKlass* exact_klass = op->exact_klass();
3657   intptr_t current_klass = op->current_klass();
3658   bool not_null = op->not_null();
3659   bool no_conflict = op->no_conflict();
3660 
3661   Label update, next, none;
3662 
3663   bool do_null = !not_null;
3664   bool exact_klass_set = exact_klass != NULL && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
3665   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
3666 
3667   assert(do_null || do_update, "why are we here?");
3668   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
3669 
3670   __ verify_oop(obj);
3671 
3672   if (tmp != obj) {
3673     __ mov(tmp, obj);
3674   }
3675   if (do_null) {
3676     __ testptr(tmp, tmp);
3677     __ jccb(Assembler::notZero, update);
3678     if (!TypeEntries::was_null_seen(current_klass)) {
3679       __ orptr(mdo_addr, TypeEntries::null_seen);
3680     }
3681     if (do_update) {
3682 #ifndef ASSERT
3683       __ jmpb(next);
3684     }
3685 #else
3686       __ jmp(next);
3687     }
3688   } else {
3689     __ testptr(tmp, tmp);
3690     __ jcc(Assembler::notZero, update);
3691     __ stop("unexpect null obj");
3692 #endif
3693   }
3694 
3695   __ bind(update);
3696 
3697   if (do_update) {
3698 #ifdef ASSERT
3699     if (exact_klass != NULL) {
3700       Label ok;
3701       __ load_klass(tmp, tmp);
3702       __ push(tmp);
3703       __ mov_metadata(tmp, exact_klass->constant_encoding());
3704       __ cmpptr(tmp, Address(rsp, 0));
3705       __ jcc(Assembler::equal, ok);
3706       __ stop("exact klass and actual klass differ");
3707       __ bind(ok);
3708       __ pop(tmp);
3709     }
3710 #endif
3711     if (!no_conflict) {
3712       if (exact_klass == NULL || TypeEntries::is_type_none(current_klass)) {
3713         if (exact_klass != NULL) {
3714           __ mov_metadata(tmp, exact_klass->constant_encoding());
3715         } else {
3716           __ load_klass(tmp, tmp);
3717         }
3718 
3719         __ xorptr(tmp, mdo_addr);
3720         __ testptr(tmp, TypeEntries::type_klass_mask);
3721         // klass seen before, nothing to do. The unknown bit may have been
3722         // set already but no need to check.
3723         __ jccb(Assembler::zero, next);
3724 
3725         __ testptr(tmp, TypeEntries::type_unknown);
3726         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3727 
3728         if (TypeEntries::is_type_none(current_klass)) {
3729           __ cmpptr(mdo_addr, 0);
3730           __ jccb(Assembler::equal, none);
3731           __ cmpptr(mdo_addr, TypeEntries::null_seen);
3732           __ jccb(Assembler::equal, none);
3733           // There is a chance that the checks above (re-reading profiling
3734           // data from memory) fail if another thread has just set the
3735           // profiling to this obj's klass
3736           __ xorptr(tmp, mdo_addr);
3737           __ testptr(tmp, TypeEntries::type_klass_mask);
3738           __ jccb(Assembler::zero, next);
3739         }
3740       } else {
3741         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3742                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
3743 
3744         __ movptr(tmp, mdo_addr);
3745         __ testptr(tmp, TypeEntries::type_unknown);
3746         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3747       }
3748 
3749       // different than before. Cannot keep accurate profile.
3750       __ orptr(mdo_addr, TypeEntries::type_unknown);
3751 
3752       if (TypeEntries::is_type_none(current_klass)) {
3753         __ jmpb(next);
3754 
3755         __ bind(none);
3756         // first time here. Set profile type.
3757         __ movptr(mdo_addr, tmp);
3758       }
3759     } else {
3760       // There's a single possible klass at this profile point
3761       assert(exact_klass != NULL, "should be");
3762       if (TypeEntries::is_type_none(current_klass)) {
3763         __ mov_metadata(tmp, exact_klass->constant_encoding());
3764         __ xorptr(tmp, mdo_addr);
3765         __ testptr(tmp, TypeEntries::type_klass_mask);
3766 #ifdef ASSERT
3767         __ jcc(Assembler::zero, next);
3768 
3769         {
3770           Label ok;
3771           __ push(tmp);
3772           __ cmpptr(mdo_addr, 0);
3773           __ jcc(Assembler::equal, ok);
3774           __ cmpptr(mdo_addr, TypeEntries::null_seen);
3775           __ jcc(Assembler::equal, ok);
3776           // may have been set by another thread
3777           __ mov_metadata(tmp, exact_klass->constant_encoding());
3778           __ xorptr(tmp, mdo_addr);
3779           __ testptr(tmp, TypeEntries::type_mask);
3780           __ jcc(Assembler::zero, ok);
3781 
3782           __ stop("unexpected profiling mismatch");
3783           __ bind(ok);
3784           __ pop(tmp);
3785         }
3786 #else
3787         __ jccb(Assembler::zero, next);
3788 #endif
3789         // first time here. Set profile type.
3790         __ movptr(mdo_addr, tmp);
3791       } else {
3792         assert(ciTypeEntries::valid_ciklass(current_klass) != NULL &&
3793                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
3794 
3795         __ movptr(tmp, mdo_addr);
3796         __ testptr(tmp, TypeEntries::type_unknown);
3797         __ jccb(Assembler::notZero, next); // already unknown. Nothing to do anymore.
3798 
3799         __ orptr(mdo_addr, TypeEntries::type_unknown);
3800       }
3801     }
3802 
3803     __ bind(next);
3804   }
3805 }
3806 
3807 void LIR_Assembler::emit_delay(LIR_OpDelay*) {
3808   Unimplemented();
3809 }
3810 
3811 
3812 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst) {
3813   __ lea(dst->as_register(), frame_map()->address_for_monitor_lock(monitor_no));
3814 }
3815 
3816 
3817 void LIR_Assembler::align_backward_branch_target() {
3818   __ align(BytesPerWord);
3819 }
3820 
3821 
3822 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
3823   if (left->is_single_cpu()) {
3824     __ negl(left->as_register());
3825     move_regs(left->as_register(), dest->as_register());
3826 
3827   } else if (left->is_double_cpu()) {
3828     Register lo = left->as_register_lo();
3829 #ifdef _LP64
3830     Register dst = dest->as_register_lo();
3831     __ movptr(dst, lo);
3832     __ negptr(dst);
3833 #else
3834     Register hi = left->as_register_hi();
3835     __ lneg(hi, lo);
3836     if (dest->as_register_lo() == hi) {
3837       assert(dest->as_register_hi() != lo, "destroying register");
3838       move_regs(hi, dest->as_register_hi());
3839       move_regs(lo, dest->as_register_lo());
3840     } else {
3841       move_regs(lo, dest->as_register_lo());
3842       move_regs(hi, dest->as_register_hi());
3843     }
3844 #endif // _LP64
3845 
3846   } else if (dest->is_single_xmm()) {
3847 #ifdef _LP64
3848     if (UseAVX > 2 && !VM_Version::supports_avx512vl()) {
3849       assert(tmp->is_valid(), "need temporary");
3850       assert_different_registers(left->as_xmm_float_reg(), tmp->as_xmm_float_reg());
3851       __ vpxor(dest->as_xmm_float_reg(), tmp->as_xmm_float_reg(), left->as_xmm_float_reg(), 2);
3852     }
3853     else
3854 #endif
3855     {
3856       assert(!tmp->is_valid(), "do not need temporary");
3857       if (left->as_xmm_float_reg() != dest->as_xmm_float_reg()) {
3858         __ movflt(dest->as_xmm_float_reg(), left->as_xmm_float_reg());
3859       }
3860       __ xorps(dest->as_xmm_float_reg(),
3861                ExternalAddress((address)float_signflip_pool));
3862     }
3863   } else if (dest->is_double_xmm()) {
3864 #ifdef _LP64
3865     if (UseAVX > 2 && !VM_Version::supports_avx512vl()) {
3866       assert(tmp->is_valid(), "need temporary");
3867       assert_different_registers(left->as_xmm_double_reg(), tmp->as_xmm_double_reg());
3868       __ vpxor(dest->as_xmm_double_reg(), tmp->as_xmm_double_reg(), left->as_xmm_double_reg(), 2);
3869     }
3870     else
3871 #endif
3872     {
3873       assert(!tmp->is_valid(), "do not need temporary");
3874       if (left->as_xmm_double_reg() != dest->as_xmm_double_reg()) {
3875         __ movdbl(dest->as_xmm_double_reg(), left->as_xmm_double_reg());
3876       }
3877       __ xorpd(dest->as_xmm_double_reg(),
3878                ExternalAddress((address)double_signflip_pool));
3879     }
3880   } else if (left->is_single_fpu() || left->is_double_fpu()) {
3881     assert(left->fpu() == 0, "arg must be on TOS");
3882     assert(dest->fpu() == 0, "dest must be TOS");
3883     __ fchs();
3884 
3885   } else {
3886     ShouldNotReachHere();
3887   }
3888 }
3889 
3890 
3891 void LIR_Assembler::leal(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
3892   assert(src->is_address(), "must be an address");
3893   assert(dest->is_register(), "must be a register");
3894 
3895   PatchingStub* patch = NULL;
3896   if (patch_code != lir_patch_none) {
3897     patch = new PatchingStub(_masm, PatchingStub::access_field_id);
3898   }
3899 
3900   Register reg = dest->as_pointer_register();
3901   LIR_Address* addr = src->as_address_ptr();
3902   __ lea(reg, as_Address(addr));
3903 
3904   if (patch != NULL) {
3905     patching_epilog(patch, patch_code, addr->base()->as_register(), info);
3906   }
3907 }
3908 
3909 
3910 
3911 void LIR_Assembler::rt_call(LIR_Opr result, address dest, const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
3912   assert(!tmp->is_valid(), "don't need temporary");
3913   __ call(RuntimeAddress(dest));
3914   if (info != NULL) {
3915     add_call_info_here(info);
3916   }
3917 }
3918 
3919 
3920 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
3921   assert(type == T_LONG, "only for volatile long fields");
3922 
3923   if (info != NULL) {
3924     add_debug_info_for_null_check_here(info);
3925   }
3926 
3927   if (src->is_double_xmm()) {
3928     if (dest->is_double_cpu()) {
3929 #ifdef _LP64
3930       __ movdq(dest->as_register_lo(), src->as_xmm_double_reg());
3931 #else
3932       __ movdl(dest->as_register_lo(), src->as_xmm_double_reg());
3933       __ psrlq(src->as_xmm_double_reg(), 32);
3934       __ movdl(dest->as_register_hi(), src->as_xmm_double_reg());
3935 #endif // _LP64
3936     } else if (dest->is_double_stack()) {
3937       __ movdbl(frame_map()->address_for_slot(dest->double_stack_ix()), src->as_xmm_double_reg());
3938     } else if (dest->is_address()) {
3939       __ movdbl(as_Address(dest->as_address_ptr()), src->as_xmm_double_reg());
3940     } else {
3941       ShouldNotReachHere();
3942     }
3943 
3944   } else if (dest->is_double_xmm()) {
3945     if (src->is_double_stack()) {
3946       __ movdbl(dest->as_xmm_double_reg(), frame_map()->address_for_slot(src->double_stack_ix()));
3947     } else if (src->is_address()) {
3948       __ movdbl(dest->as_xmm_double_reg(), as_Address(src->as_address_ptr()));
3949     } else {
3950       ShouldNotReachHere();
3951     }
3952 
3953   } else if (src->is_double_fpu()) {
3954     assert(src->fpu_regnrLo() == 0, "must be TOS");
3955     if (dest->is_double_stack()) {
3956       __ fistp_d(frame_map()->address_for_slot(dest->double_stack_ix()));
3957     } else if (dest->is_address()) {
3958       __ fistp_d(as_Address(dest->as_address_ptr()));
3959     } else {
3960       ShouldNotReachHere();
3961     }
3962 
3963   } else if (dest->is_double_fpu()) {
3964     assert(dest->fpu_regnrLo() == 0, "must be TOS");
3965     if (src->is_double_stack()) {
3966       __ fild_d(frame_map()->address_for_slot(src->double_stack_ix()));
3967     } else if (src->is_address()) {
3968       __ fild_d(as_Address(src->as_address_ptr()));
3969     } else {
3970       ShouldNotReachHere();
3971     }
3972   } else {
3973     ShouldNotReachHere();
3974   }
3975 }
3976 
3977 #ifdef ASSERT
3978 // emit run-time assertion
3979 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
3980   assert(op->code() == lir_assert, "must be");
3981 
3982   if (op->in_opr1()->is_valid()) {
3983     assert(op->in_opr2()->is_valid(), "both operands must be valid");
3984     comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
3985   } else {
3986     assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
3987     assert(op->condition() == lir_cond_always, "no other conditions allowed");
3988   }
3989 
3990   Label ok;
3991   if (op->condition() != lir_cond_always) {
3992     Assembler::Condition acond = Assembler::zero;
3993     switch (op->condition()) {
3994       case lir_cond_equal:        acond = Assembler::equal;       break;
3995       case lir_cond_notEqual:     acond = Assembler::notEqual;    break;
3996       case lir_cond_less:         acond = Assembler::less;        break;
3997       case lir_cond_lessEqual:    acond = Assembler::lessEqual;   break;
3998       case lir_cond_greaterEqual: acond = Assembler::greaterEqual;break;
3999       case lir_cond_greater:      acond = Assembler::greater;     break;
4000       case lir_cond_belowEqual:   acond = Assembler::belowEqual;  break;
4001       case lir_cond_aboveEqual:   acond = Assembler::aboveEqual;  break;
4002       default:                    ShouldNotReachHere();
4003     }
4004     __ jcc(acond, ok);
4005   }
4006   if (op->halt()) {
4007     const char* str = __ code_string(op->msg());
4008     __ stop(str);
4009   } else {
4010     breakpoint();
4011   }
4012   __ bind(ok);
4013 }
4014 #endif
4015 
4016 void LIR_Assembler::membar() {
4017   // QQQ sparc TSO uses this,
4018   __ membar( Assembler::Membar_mask_bits(Assembler::StoreLoad));
4019 }
4020 
4021 void LIR_Assembler::membar_acquire() {
4022   // No x86 machines currently require load fences
4023 }
4024 
4025 void LIR_Assembler::membar_release() {
4026   // No x86 machines currently require store fences
4027 }
4028 
4029 void LIR_Assembler::membar_loadload() {
4030   // no-op
4031   //__ membar(Assembler::Membar_mask_bits(Assembler::loadload));
4032 }
4033 
4034 void LIR_Assembler::membar_storestore() {
4035   // no-op
4036   //__ membar(Assembler::Membar_mask_bits(Assembler::storestore));
4037 }
4038 
4039 void LIR_Assembler::membar_loadstore() {
4040   // no-op
4041   //__ membar(Assembler::Membar_mask_bits(Assembler::loadstore));
4042 }
4043 
4044 void LIR_Assembler::membar_storeload() {
4045   __ membar(Assembler::Membar_mask_bits(Assembler::StoreLoad));
4046 }
4047 
4048 void LIR_Assembler::on_spin_wait() {
4049   __ pause ();
4050 }
4051 
4052 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
4053   assert(result_reg->is_register(), "check");
4054 #ifdef _LP64
4055   // __ get_thread(result_reg->as_register_lo());
4056   __ mov(result_reg->as_register(), r15_thread);
4057 #else
4058   __ get_thread(result_reg->as_register());
4059 #endif // _LP64
4060 }
4061 
4062 
4063 void LIR_Assembler::peephole(LIR_List*) {
4064   // do nothing for now
4065 }
4066 
4067 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp) {
4068   assert(data == dest, "xchg/xadd uses only 2 operands");
4069 
4070   if (data->type() == T_INT) {
4071     if (code == lir_xadd) {
4072       __ lock();
4073       __ xaddl(as_Address(src->as_address_ptr()), data->as_register());
4074     } else {
4075       __ xchgl(data->as_register(), as_Address(src->as_address_ptr()));
4076     }
4077   } else if (data->is_oop()) {
4078     assert (code == lir_xchg, "xadd for oops");
4079     Register obj = data->as_register();
4080 #ifdef _LP64
4081     if (UseCompressedOops) {
4082       __ encode_heap_oop(obj);
4083       __ xchgl(obj, as_Address(src->as_address_ptr()));
4084       __ decode_heap_oop(obj);
4085     } else {
4086       __ xchgptr(obj, as_Address(src->as_address_ptr()));
4087     }
4088 #else
4089     __ xchgl(obj, as_Address(src->as_address_ptr()));
4090 #endif
4091   } else if (data->type() == T_LONG) {
4092 #ifdef _LP64
4093     assert(data->as_register_lo() == data->as_register_hi(), "should be a single register");
4094     if (code == lir_xadd) {
4095       __ lock();
4096       __ xaddq(as_Address(src->as_address_ptr()), data->as_register_lo());
4097     } else {
4098       __ xchgq(data->as_register_lo(), as_Address(src->as_address_ptr()));
4099     }
4100 #else
4101     ShouldNotReachHere();
4102 #endif
4103   } else {
4104     ShouldNotReachHere();
4105   }
4106 }
4107 
4108 #undef __