1 /*
   2  * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "asm/macroAssembler.inline.hpp"
  27 #include "asm/assembler.hpp"
  28 #include "c1/c1_CodeStubs.hpp"
  29 #include "c1/c1_Compilation.hpp"
  30 #include "c1/c1_LIRAssembler.hpp"
  31 #include "c1/c1_MacroAssembler.hpp"
  32 #include "c1/c1_Runtime1.hpp"
  33 #include "c1/c1_ValueStack.hpp"
  34 #include "ci/ciArrayKlass.hpp"
  35 #include "ci/ciInstance.hpp"
  36 #include "code/compiledIC.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "gc/shared/gc_globals.hpp"
  39 #include "nativeInst_aarch64.hpp"
  40 #include "oops/objArrayKlass.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "utilities/powerOfTwo.hpp"
  45 #include "vmreg_aarch64.inline.hpp"
  46 
  47 
  48 #ifndef PRODUCT
  49 #define COMMENT(x)   do { __ block_comment(x); } while (0)
  50 #else
  51 #define COMMENT(x)
  52 #endif
  53 
  54 NEEDS_CLEANUP // remove this definitions ?
  55 const Register SYNC_header = r0;   // synchronization header
  56 const Register SHIFT_count = r0;   // where count for shift operations must be
  57 
  58 #define __ _masm->
  59 
  60 
  61 static void select_different_registers(Register preserve,
  62                                        Register extra,
  63                                        Register &tmp1,
  64                                        Register &tmp2) {
  65   if (tmp1 == preserve) {
  66     assert_different_registers(tmp1, tmp2, extra);
  67     tmp1 = extra;
  68   } else if (tmp2 == preserve) {
  69     assert_different_registers(tmp1, tmp2, extra);
  70     tmp2 = extra;
  71   }
  72   assert_different_registers(preserve, tmp1, tmp2);
  73 }
  74 
  75 
  76 
  77 static void select_different_registers(Register preserve,
  78                                        Register extra,
  79                                        Register &tmp1,
  80                                        Register &tmp2,
  81                                        Register &tmp3) {
  82   if (tmp1 == preserve) {
  83     assert_different_registers(tmp1, tmp2, tmp3, extra);
  84     tmp1 = extra;
  85   } else if (tmp2 == preserve) {
  86     assert_different_registers(tmp1, tmp2, tmp3, extra);
  87     tmp2 = extra;
  88   } else if (tmp3 == preserve) {
  89     assert_different_registers(tmp1, tmp2, tmp3, extra);
  90     tmp3 = extra;
  91   }
  92   assert_different_registers(preserve, tmp1, tmp2, tmp3);
  93 }
  94 
  95 
  96 bool LIR_Assembler::is_small_constant(LIR_Opr opr) { Unimplemented(); return false; }
  97 
  98 
  99 LIR_Opr LIR_Assembler::receiverOpr() {
 100   return FrameMap::receiver_opr;
 101 }
 102 
 103 LIR_Opr LIR_Assembler::osrBufferPointer() {
 104   return FrameMap::as_pointer_opr(receiverOpr()->as_register());
 105 }
 106 
 107 //--------------fpu register translations-----------------------
 108 
 109 
 110 address LIR_Assembler::float_constant(float f) {
 111   address const_addr = __ float_constant(f);
 112   if (const_addr == nullptr) {
 113     bailout("const section overflow");
 114     return __ code()->consts()->start();
 115   } else {
 116     return const_addr;
 117   }
 118 }
 119 
 120 
 121 address LIR_Assembler::double_constant(double d) {
 122   address const_addr = __ double_constant(d);
 123   if (const_addr == nullptr) {
 124     bailout("const section overflow");
 125     return __ code()->consts()->start();
 126   } else {
 127     return const_addr;
 128   }
 129 }
 130 
 131 address LIR_Assembler::int_constant(jlong n) {
 132   address const_addr = __ long_constant(n);
 133   if (const_addr == nullptr) {
 134     bailout("const section overflow");
 135     return __ code()->consts()->start();
 136   } else {
 137     return const_addr;
 138   }
 139 }
 140 
 141 void LIR_Assembler::breakpoint() { Unimplemented(); }
 142 
 143 void LIR_Assembler::push(LIR_Opr opr) { Unimplemented(); }
 144 
 145 void LIR_Assembler::pop(LIR_Opr opr) { Unimplemented(); }
 146 
 147 bool LIR_Assembler::is_literal_address(LIR_Address* addr) { Unimplemented(); return false; }
 148 //-------------------------------------------
 149 
 150 static Register as_reg(LIR_Opr op) {
 151   return op->is_double_cpu() ? op->as_register_lo() : op->as_register();
 152 }
 153 
 154 static jlong as_long(LIR_Opr data) {
 155   jlong result;
 156   switch (data->type()) {
 157   case T_INT:
 158     result = (data->as_jint());
 159     break;
 160   case T_LONG:
 161     result = (data->as_jlong());
 162     break;
 163   default:
 164     ShouldNotReachHere();
 165     result = 0;  // unreachable
 166   }
 167   return result;
 168 }
 169 
 170 Address LIR_Assembler::as_Address(LIR_Address* addr, Register tmp) {
 171   Register base = addr->base()->as_pointer_register();
 172   LIR_Opr opr = addr->index();
 173   if (opr->is_cpu_register()) {
 174     Register index;
 175     if (opr->is_single_cpu())
 176       index = opr->as_register();
 177     else
 178       index = opr->as_register_lo();
 179     assert(addr->disp() == 0, "must be");
 180     switch(opr->type()) {
 181       case T_INT:
 182         return Address(base, index, Address::sxtw(addr->scale()));
 183       case T_LONG:
 184         return Address(base, index, Address::lsl(addr->scale()));
 185       default:
 186         ShouldNotReachHere();
 187       }
 188   } else {
 189     assert(addr->scale() == 0,
 190            "expected for immediate operand, was: %d", addr->scale());
 191     ptrdiff_t offset = ptrdiff_t(addr->disp());
 192     // NOTE: Does not handle any 16 byte vector access.
 193     const uint type_size = type2aelembytes(addr->type(), true);
 194     return __ legitimize_address(Address(base, offset), type_size, tmp);
 195   }
 196   return Address();
 197 }
 198 
 199 Address LIR_Assembler::as_Address_hi(LIR_Address* addr) {
 200   ShouldNotReachHere();
 201   return Address();
 202 }
 203 
 204 Address LIR_Assembler::as_Address(LIR_Address* addr) {
 205   return as_Address(addr, rscratch1);
 206 }
 207 
 208 Address LIR_Assembler::as_Address_lo(LIR_Address* addr) {
 209   return as_Address(addr, rscratch1);  // Ouch
 210   // FIXME: This needs to be much more clever.  See x86.
 211 }
 212 
 213 // Ensure a valid Address (base + offset) to a stack-slot. If stack access is
 214 // not encodable as a base + (immediate) offset, generate an explicit address
 215 // calculation to hold the address in a temporary register.
 216 Address LIR_Assembler::stack_slot_address(int index, uint size, Register tmp, int adjust) {
 217   precond(size == 4 || size == 8);
 218   Address addr = frame_map()->address_for_slot(index, adjust);
 219   precond(addr.getMode() == Address::base_plus_offset);
 220   precond(addr.base() == sp);
 221   precond(addr.offset() > 0);
 222   uint mask = size - 1;
 223   assert((addr.offset() & mask) == 0, "scaled offsets only");
 224   return __ legitimize_address(addr, size, tmp);
 225 }
 226 
 227 void LIR_Assembler::osr_entry() {
 228   offsets()->set_value(CodeOffsets::OSR_Entry, code_offset());
 229   BlockBegin* osr_entry = compilation()->hir()->osr_entry();
 230   ValueStack* entry_state = osr_entry->state();
 231   int number_of_locks = entry_state->locks_size();
 232 
 233   // we jump here if osr happens with the interpreter
 234   // state set up to continue at the beginning of the
 235   // loop that triggered osr - in particular, we have
 236   // the following registers setup:
 237   //
 238   // r2: osr buffer
 239   //
 240 
 241   // build frame
 242   ciMethod* m = compilation()->method();
 243   __ build_frame(initial_frame_size_in_bytes(), bang_size_in_bytes());
 244 
 245   // OSR buffer is
 246   //
 247   // locals[nlocals-1..0]
 248   // monitors[0..number_of_locks]
 249   //
 250   // locals is a direct copy of the interpreter frame so in the osr buffer
 251   // so first slot in the local array is the last local from the interpreter
 252   // and last slot is local[0] (receiver) from the interpreter
 253   //
 254   // Similarly with locks. The first lock slot in the osr buffer is the nth lock
 255   // from the interpreter frame, the nth lock slot in the osr buffer is 0th lock
 256   // in the interpreter frame (the method lock if a sync method)
 257 
 258   // Initialize monitors in the compiled activation.
 259   //   r2: pointer to osr buffer
 260   //
 261   // All other registers are dead at this point and the locals will be
 262   // copied into place by code emitted in the IR.
 263 
 264   Register OSR_buf = osrBufferPointer()->as_pointer_register();
 265   { assert(frame::interpreter_frame_monitor_size() == BasicObjectLock::size(), "adjust code below");
 266     int monitor_offset = BytesPerWord * method()->max_locals() +
 267       (2 * BytesPerWord) * (number_of_locks - 1);
 268     // SharedRuntime::OSR_migration_begin() packs BasicObjectLocks in
 269     // the OSR buffer using 2 word entries: first the lock and then
 270     // the oop.
 271     for (int i = 0; i < number_of_locks; i++) {
 272       int slot_offset = monitor_offset - ((i * 2) * BytesPerWord);
 273 #ifdef ASSERT
 274       // verify the interpreter's monitor has a non-null object
 275       {
 276         Label L;
 277         __ ldr(rscratch1, __ form_address(rscratch1, OSR_buf, slot_offset + 1*BytesPerWord, 0));
 278         __ cbnz(rscratch1, L);
 279         __ stop("locked object is null");
 280         __ bind(L);
 281       }
 282 #endif
 283       __ ldr(r19, __ form_address(rscratch1, OSR_buf, slot_offset, 0));
 284       __ ldr(r20, __ form_address(rscratch1, OSR_buf, slot_offset + BytesPerWord, 0));
 285       __ str(r19, frame_map()->address_for_monitor_lock(i));
 286       __ str(r20, frame_map()->address_for_monitor_object(i));
 287     }
 288   }
 289 }
 290 
 291 
 292 // inline cache check; done before the frame is built.
 293 int LIR_Assembler::check_icache() {
 294   return __ ic_check(CodeEntryAlignment);
 295 }
 296 
 297 void LIR_Assembler::clinit_barrier(ciMethod* method) {
 298   assert(VM_Version::supports_fast_class_init_checks(), "sanity");
 299   assert(!method->holder()->is_not_initialized(), "initialization should have been started");
 300 
 301   Label L_skip_barrier;
 302 
 303   __ mov_metadata(rscratch2, method->holder()->constant_encoding());
 304   __ clinit_barrier(rscratch2, rscratch1, &L_skip_barrier /*L_fast_path*/);
 305   __ far_jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub()));
 306   __ bind(L_skip_barrier);
 307 }
 308 
 309 void LIR_Assembler::jobject2reg(jobject o, Register reg) {
 310   if (o == nullptr) {
 311     __ mov(reg, zr);
 312   } else {
 313     __ movoop(reg, o);
 314   }
 315 }
 316 
 317 void LIR_Assembler::deoptimize_trap(CodeEmitInfo *info) {
 318   address target = nullptr;
 319   relocInfo::relocType reloc_type = relocInfo::none;
 320 
 321   switch (patching_id(info)) {
 322   case PatchingStub::access_field_id:
 323     target = Runtime1::entry_for(C1StubId::access_field_patching_id);
 324     reloc_type = relocInfo::section_word_type;
 325     break;
 326   case PatchingStub::load_klass_id:
 327     target = Runtime1::entry_for(C1StubId::load_klass_patching_id);
 328     reloc_type = relocInfo::metadata_type;
 329     break;
 330   case PatchingStub::load_mirror_id:
 331     target = Runtime1::entry_for(C1StubId::load_mirror_patching_id);
 332     reloc_type = relocInfo::oop_type;
 333     break;
 334   case PatchingStub::load_appendix_id:
 335     target = Runtime1::entry_for(C1StubId::load_appendix_patching_id);
 336     reloc_type = relocInfo::oop_type;
 337     break;
 338   default: ShouldNotReachHere();
 339   }
 340 
 341   __ far_call(RuntimeAddress(target));
 342   add_call_info_here(info);
 343 }
 344 
 345 void LIR_Assembler::jobject2reg_with_patching(Register reg, CodeEmitInfo *info) {
 346   deoptimize_trap(info);
 347 }
 348 
 349 
 350 // This specifies the rsp decrement needed to build the frame
 351 int LIR_Assembler::initial_frame_size_in_bytes() const {
 352   // if rounding, must let FrameMap know!
 353 
 354   return in_bytes(frame_map()->framesize_in_bytes());
 355 }
 356 
 357 
 358 int LIR_Assembler::emit_exception_handler() {
 359   // generate code for exception handler
 360   address handler_base = __ start_a_stub(exception_handler_size());
 361   if (handler_base == nullptr) {
 362     // not enough space left for the handler
 363     bailout("exception handler overflow");
 364     return -1;
 365   }
 366 
 367   int offset = code_offset();
 368 
 369   // the exception oop and pc are in r0, and r3
 370   // no other registers need to be preserved, so invalidate them
 371   __ invalidate_registers(false, true, true, false, true, true);
 372 
 373   // check that there is really an exception
 374   __ verify_not_null_oop(r0);
 375 
 376   // search an exception handler (r0: exception oop, r3: throwing pc)
 377   __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::handle_exception_from_callee_id)));
 378   __ should_not_reach_here();
 379   guarantee(code_offset() - offset <= exception_handler_size(), "overflow");
 380   __ end_a_stub();
 381 
 382   return offset;
 383 }
 384 
 385 
 386 // Emit the code to remove the frame from the stack in the exception
 387 // unwind path.
 388 int LIR_Assembler::emit_unwind_handler() {
 389 #ifndef PRODUCT
 390   if (CommentedAssembly) {
 391     _masm->block_comment("Unwind handler");
 392   }
 393 #endif
 394 
 395   int offset = code_offset();
 396 
 397   // Fetch the exception from TLS and clear out exception related thread state
 398   __ ldr(r0, Address(rthread, JavaThread::exception_oop_offset()));
 399   __ str(zr, Address(rthread, JavaThread::exception_oop_offset()));
 400   __ str(zr, Address(rthread, JavaThread::exception_pc_offset()));
 401 
 402   __ bind(_unwind_handler_entry);
 403   __ verify_not_null_oop(r0);
 404   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 405     __ mov(r19, r0);  // Preserve the exception
 406   }
 407 
 408   // Perform needed unlocking
 409   MonitorExitStub* stub = nullptr;
 410   if (method()->is_synchronized()) {
 411     monitor_address(0, FrameMap::r0_opr);
 412     stub = new MonitorExitStub(FrameMap::r0_opr, true, 0);
 413     if (LockingMode == LM_MONITOR) {
 414       __ b(*stub->entry());
 415     } else {
 416       __ unlock_object(r5, r4, r0, r6, *stub->entry());
 417     }
 418     __ bind(*stub->continuation());
 419   }
 420 
 421   if (compilation()->env()->dtrace_method_probes()) {
 422     __ mov(c_rarg0, rthread);
 423     __ mov_metadata(c_rarg1, method()->constant_encoding());
 424     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit), c_rarg0, c_rarg1);
 425   }
 426 
 427   if (method()->is_synchronized() || compilation()->env()->dtrace_method_probes()) {
 428     __ mov(r0, r19);  // Restore the exception
 429   }
 430 
 431   // remove the activation and dispatch to the unwind handler
 432   __ block_comment("remove_frame and dispatch to the unwind handler");
 433   __ remove_frame(initial_frame_size_in_bytes());
 434   __ far_jump(RuntimeAddress(Runtime1::entry_for(C1StubId::unwind_exception_id)));
 435 
 436   // Emit the slow path assembly
 437   if (stub != nullptr) {
 438     stub->emit_code(this);
 439   }
 440 
 441   return offset;
 442 }
 443 
 444 
 445 int LIR_Assembler::emit_deopt_handler() {
 446   // generate code for exception handler
 447   address handler_base = __ start_a_stub(deopt_handler_size());
 448   if (handler_base == nullptr) {
 449     // not enough space left for the handler
 450     bailout("deopt handler overflow");
 451     return -1;
 452   }
 453 
 454   int offset = code_offset();
 455 
 456   __ adr(lr, pc());
 457   __ far_jump(RuntimeAddress(SharedRuntime::deopt_blob()->unpack()));
 458   guarantee(code_offset() - offset <= deopt_handler_size(), "overflow");
 459   __ end_a_stub();
 460 
 461   return offset;
 462 }
 463 
 464 void LIR_Assembler::add_debug_info_for_branch(address adr, CodeEmitInfo* info) {
 465   _masm->code_section()->relocate(adr, relocInfo::poll_type);
 466   int pc_offset = code_offset();
 467   flush_debug_info(pc_offset);
 468   info->record_debug_info(compilation()->debug_info_recorder(), pc_offset);
 469   if (info->exception_handlers() != nullptr) {
 470     compilation()->add_exception_handlers_for_pco(pc_offset, info->exception_handlers());
 471   }
 472 }
 473 
 474 void LIR_Assembler::return_op(LIR_Opr result, C1SafepointPollStub* code_stub) {
 475   assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == r0, "word returns are in r0,");
 476 
 477   // Pop the stack before the safepoint code
 478   __ remove_frame(initial_frame_size_in_bytes());
 479 
 480   if (StackReservedPages > 0 && compilation()->has_reserved_stack_access()) {
 481     __ reserved_stack_check();
 482   }
 483 
 484   code_stub->set_safepoint_offset(__ offset());
 485   __ relocate(relocInfo::poll_return_type);
 486   __ safepoint_poll(*code_stub->entry(), true /* at_return */, false /* acquire */, true /* in_nmethod */);
 487   __ ret(lr);
 488 }
 489 
 490 int LIR_Assembler::safepoint_poll(LIR_Opr tmp, CodeEmitInfo* info) {
 491   guarantee(info != nullptr, "Shouldn't be null");
 492   __ get_polling_page(rscratch1, relocInfo::poll_type);
 493   add_debug_info_for_branch(info);  // This isn't just debug info:
 494                                     // it's the oop map
 495   __ read_polling_page(rscratch1, relocInfo::poll_type);
 496   return __ offset();
 497 }
 498 
 499 
 500 void LIR_Assembler::move_regs(Register from_reg, Register to_reg) {
 501   if (from_reg == r31_sp)
 502     from_reg = sp;
 503   if (to_reg == r31_sp)
 504     to_reg = sp;
 505   __ mov(to_reg, from_reg);
 506 }
 507 
 508 void LIR_Assembler::swap_reg(Register a, Register b) { Unimplemented(); }
 509 
 510 
 511 void LIR_Assembler::const2reg(LIR_Opr src, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
 512   assert(src->is_constant(), "should not call otherwise");
 513   assert(dest->is_register(), "should not call otherwise");
 514   LIR_Const* c = src->as_constant_ptr();
 515 
 516   switch (c->type()) {
 517     case T_INT: {
 518       assert(patch_code == lir_patch_none, "no patching handled here");
 519       __ movw(dest->as_register(), c->as_jint());
 520       break;
 521     }
 522 
 523     case T_ADDRESS: {
 524       assert(patch_code == lir_patch_none, "no patching handled here");
 525       __ mov(dest->as_register(), c->as_jint());
 526       break;
 527     }
 528 
 529     case T_LONG: {
 530       assert(patch_code == lir_patch_none, "no patching handled here");
 531       __ mov(dest->as_register_lo(), (intptr_t)c->as_jlong());
 532       break;
 533     }
 534 
 535     case T_OBJECT: {
 536         if (patch_code == lir_patch_none) {
 537           jobject2reg(c->as_jobject(), dest->as_register());
 538         } else {
 539           jobject2reg_with_patching(dest->as_register(), info);
 540         }
 541       break;
 542     }
 543 
 544     case T_METADATA: {
 545       if (patch_code != lir_patch_none) {
 546         klass2reg_with_patching(dest->as_register(), info);
 547       } else {
 548         __ mov_metadata(dest->as_register(), c->as_metadata());
 549       }
 550       break;
 551     }
 552 
 553     case T_FLOAT: {
 554       if (__ operand_valid_for_float_immediate(c->as_jfloat())) {
 555         __ fmovs(dest->as_float_reg(), (c->as_jfloat()));
 556       } else {
 557         __ adr(rscratch1, InternalAddress(float_constant(c->as_jfloat())));
 558         __ ldrs(dest->as_float_reg(), Address(rscratch1));
 559       }
 560       break;
 561     }
 562 
 563     case T_DOUBLE: {
 564       if (__ operand_valid_for_float_immediate(c->as_jdouble())) {
 565         __ fmovd(dest->as_double_reg(), (c->as_jdouble()));
 566       } else {
 567         __ adr(rscratch1, InternalAddress(double_constant(c->as_jdouble())));
 568         __ ldrd(dest->as_double_reg(), Address(rscratch1));
 569       }
 570       break;
 571     }
 572 
 573     default:
 574       ShouldNotReachHere();
 575   }
 576 }
 577 
 578 void LIR_Assembler::const2stack(LIR_Opr src, LIR_Opr dest) {
 579   LIR_Const* c = src->as_constant_ptr();
 580   switch (c->type()) {
 581   case T_OBJECT:
 582     {
 583       if (! c->as_jobject())
 584         __ str(zr, frame_map()->address_for_slot(dest->single_stack_ix()));
 585       else {
 586         const2reg(src, FrameMap::rscratch1_opr, lir_patch_none, nullptr);
 587         reg2stack(FrameMap::rscratch1_opr, dest, c->type(), false);
 588       }
 589     }
 590     break;
 591   case T_ADDRESS:
 592     {
 593       const2reg(src, FrameMap::rscratch1_opr, lir_patch_none, nullptr);
 594       reg2stack(FrameMap::rscratch1_opr, dest, c->type(), false);
 595     }
 596   case T_INT:
 597   case T_FLOAT:
 598     {
 599       Register reg = zr;
 600       if (c->as_jint_bits() == 0)
 601         __ strw(zr, frame_map()->address_for_slot(dest->single_stack_ix()));
 602       else {
 603         __ movw(rscratch1, c->as_jint_bits());
 604         __ strw(rscratch1, frame_map()->address_for_slot(dest->single_stack_ix()));
 605       }
 606     }
 607     break;
 608   case T_LONG:
 609   case T_DOUBLE:
 610     {
 611       Register reg = zr;
 612       if (c->as_jlong_bits() == 0)
 613         __ str(zr, frame_map()->address_for_slot(dest->double_stack_ix(),
 614                                                  lo_word_offset_in_bytes));
 615       else {
 616         __ mov(rscratch1, (intptr_t)c->as_jlong_bits());
 617         __ str(rscratch1, frame_map()->address_for_slot(dest->double_stack_ix(),
 618                                                         lo_word_offset_in_bytes));
 619       }
 620     }
 621     break;
 622   default:
 623     ShouldNotReachHere();
 624   }
 625 }
 626 
 627 void LIR_Assembler::const2mem(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info, bool wide) {
 628   assert(src->is_constant(), "should not call otherwise");
 629   LIR_Const* c = src->as_constant_ptr();
 630   LIR_Address* to_addr = dest->as_address_ptr();
 631 
 632   void (Assembler::* insn)(Register Rt, const Address &adr);
 633 
 634   switch (type) {
 635   case T_ADDRESS:
 636     assert(c->as_jint() == 0, "should be");
 637     insn = &Assembler::str;
 638     break;
 639   case T_LONG:
 640     assert(c->as_jlong() == 0, "should be");
 641     insn = &Assembler::str;
 642     break;
 643   case T_INT:
 644     assert(c->as_jint() == 0, "should be");
 645     insn = &Assembler::strw;
 646     break;
 647   case T_OBJECT:
 648   case T_ARRAY:
 649     assert(c->as_jobject() == nullptr, "should be");
 650     if (UseCompressedOops && !wide) {
 651       insn = &Assembler::strw;
 652     } else {
 653       insn = &Assembler::str;
 654     }
 655     break;
 656   case T_CHAR:
 657   case T_SHORT:
 658     assert(c->as_jint() == 0, "should be");
 659     insn = &Assembler::strh;
 660     break;
 661   case T_BOOLEAN:
 662   case T_BYTE:
 663     assert(c->as_jint() == 0, "should be");
 664     insn = &Assembler::strb;
 665     break;
 666   default:
 667     ShouldNotReachHere();
 668     insn = &Assembler::str;  // unreachable
 669   }
 670 
 671   if (info) add_debug_info_for_null_check_here(info);
 672   (_masm->*insn)(zr, as_Address(to_addr, rscratch1));
 673 }
 674 
 675 void LIR_Assembler::reg2reg(LIR_Opr src, LIR_Opr dest) {
 676   assert(src->is_register(), "should not call otherwise");
 677   assert(dest->is_register(), "should not call otherwise");
 678 
 679   // move between cpu-registers
 680   if (dest->is_single_cpu()) {
 681     if (src->type() == T_LONG) {
 682       // Can do LONG -> OBJECT
 683       move_regs(src->as_register_lo(), dest->as_register());
 684       return;
 685     }
 686     assert(src->is_single_cpu(), "must match");
 687     if (src->type() == T_OBJECT) {
 688       __ verify_oop(src->as_register());
 689     }
 690     move_regs(src->as_register(), dest->as_register());
 691 
 692   } else if (dest->is_double_cpu()) {
 693     if (is_reference_type(src->type())) {
 694       // Surprising to me but we can see move of a long to t_object
 695       __ verify_oop(src->as_register());
 696       move_regs(src->as_register(), dest->as_register_lo());
 697       return;
 698     }
 699     assert(src->is_double_cpu(), "must match");
 700     Register f_lo = src->as_register_lo();
 701     Register f_hi = src->as_register_hi();
 702     Register t_lo = dest->as_register_lo();
 703     Register t_hi = dest->as_register_hi();
 704     assert(f_hi == f_lo, "must be same");
 705     assert(t_hi == t_lo, "must be same");
 706     move_regs(f_lo, t_lo);
 707 
 708   } else if (dest->is_single_fpu()) {
 709     __ fmovs(dest->as_float_reg(), src->as_float_reg());
 710 
 711   } else if (dest->is_double_fpu()) {
 712     __ fmovd(dest->as_double_reg(), src->as_double_reg());
 713 
 714   } else {
 715     ShouldNotReachHere();
 716   }
 717 }
 718 
 719 void LIR_Assembler::reg2stack(LIR_Opr src, LIR_Opr dest, BasicType type, bool pop_fpu_stack) {
 720   precond(src->is_register() && dest->is_stack());
 721 
 722   uint const c_sz32 = sizeof(uint32_t);
 723   uint const c_sz64 = sizeof(uint64_t);
 724 
 725   if (src->is_single_cpu()) {
 726     int index = dest->single_stack_ix();
 727     if (is_reference_type(type)) {
 728       __ str(src->as_register(), stack_slot_address(index, c_sz64, rscratch1));
 729       __ verify_oop(src->as_register());
 730     } else if (type == T_METADATA || type == T_DOUBLE || type == T_ADDRESS) {
 731       __ str(src->as_register(), stack_slot_address(index, c_sz64, rscratch1));
 732     } else {
 733       __ strw(src->as_register(), stack_slot_address(index, c_sz32, rscratch1));
 734     }
 735 
 736   } else if (src->is_double_cpu()) {
 737     int index = dest->double_stack_ix();
 738     Address dest_addr_LO = stack_slot_address(index, c_sz64, rscratch1, lo_word_offset_in_bytes);
 739     __ str(src->as_register_lo(), dest_addr_LO);
 740 
 741   } else if (src->is_single_fpu()) {
 742     int index = dest->single_stack_ix();
 743     __ strs(src->as_float_reg(), stack_slot_address(index, c_sz32, rscratch1));
 744 
 745   } else if (src->is_double_fpu()) {
 746     int index = dest->double_stack_ix();
 747     __ strd(src->as_double_reg(), stack_slot_address(index, c_sz64, rscratch1));
 748 
 749   } else {
 750     ShouldNotReachHere();
 751   }
 752 }
 753 
 754 
 755 void LIR_Assembler::reg2mem(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool pop_fpu_stack, bool wide) {
 756   LIR_Address* to_addr = dest->as_address_ptr();
 757   PatchingStub* patch = nullptr;
 758   Register compressed_src = rscratch1;
 759 
 760   if (patch_code != lir_patch_none) {
 761     deoptimize_trap(info);
 762     return;
 763   }
 764 
 765   if (is_reference_type(type)) {
 766     __ verify_oop(src->as_register());
 767 
 768     if (UseCompressedOops && !wide) {
 769       __ encode_heap_oop(compressed_src, src->as_register());
 770     } else {
 771       compressed_src = src->as_register();
 772     }
 773   }
 774 
 775   int null_check_here = code_offset();
 776   switch (type) {
 777     case T_FLOAT: {
 778       __ strs(src->as_float_reg(), as_Address(to_addr));
 779       break;
 780     }
 781 
 782     case T_DOUBLE: {
 783       __ strd(src->as_double_reg(), as_Address(to_addr));
 784       break;
 785     }
 786 
 787     case T_ARRAY:   // fall through
 788     case T_OBJECT:  // fall through
 789       if (UseCompressedOops && !wide) {
 790         __ strw(compressed_src, as_Address(to_addr, rscratch2));
 791       } else {
 792          __ str(compressed_src, as_Address(to_addr));
 793       }
 794       break;
 795     case T_METADATA:
 796       // We get here to store a method pointer to the stack to pass to
 797       // a dtrace runtime call. This can't work on 64 bit with
 798       // compressed klass ptrs: T_METADATA can be a compressed klass
 799       // ptr or a 64 bit method pointer.
 800       ShouldNotReachHere();
 801       __ str(src->as_register(), as_Address(to_addr));
 802       break;
 803     case T_ADDRESS:
 804       __ str(src->as_register(), as_Address(to_addr));
 805       break;
 806     case T_INT:
 807       __ strw(src->as_register(), as_Address(to_addr));
 808       break;
 809 
 810     case T_LONG: {
 811       __ str(src->as_register_lo(), as_Address_lo(to_addr));
 812       break;
 813     }
 814 
 815     case T_BYTE:    // fall through
 816     case T_BOOLEAN: {
 817       __ strb(src->as_register(), as_Address(to_addr));
 818       break;
 819     }
 820 
 821     case T_CHAR:    // fall through
 822     case T_SHORT:
 823       __ strh(src->as_register(), as_Address(to_addr));
 824       break;
 825 
 826     default:
 827       ShouldNotReachHere();
 828   }
 829   if (info != nullptr) {
 830     add_debug_info_for_null_check(null_check_here, info);
 831   }
 832 }
 833 
 834 
 835 void LIR_Assembler::stack2reg(LIR_Opr src, LIR_Opr dest, BasicType type) {
 836   precond(src->is_stack() && dest->is_register());
 837 
 838   uint const c_sz32 = sizeof(uint32_t);
 839   uint const c_sz64 = sizeof(uint64_t);
 840 
 841   if (dest->is_single_cpu()) {
 842     int index = src->single_stack_ix();
 843     if (is_reference_type(type)) {
 844       __ ldr(dest->as_register(), stack_slot_address(index, c_sz64, rscratch1));
 845       __ verify_oop(dest->as_register());
 846     } else if (type == T_METADATA || type == T_ADDRESS) {
 847       __ ldr(dest->as_register(), stack_slot_address(index, c_sz64, rscratch1));
 848     } else {
 849       __ ldrw(dest->as_register(), stack_slot_address(index, c_sz32, rscratch1));
 850     }
 851 
 852   } else if (dest->is_double_cpu()) {
 853     int index = src->double_stack_ix();
 854     Address src_addr_LO = stack_slot_address(index, c_sz64, rscratch1, lo_word_offset_in_bytes);
 855     __ ldr(dest->as_register_lo(), src_addr_LO);
 856 
 857   } else if (dest->is_single_fpu()) {
 858     int index = src->single_stack_ix();
 859     __ ldrs(dest->as_float_reg(), stack_slot_address(index, c_sz32, rscratch1));
 860 
 861   } else if (dest->is_double_fpu()) {
 862     int index = src->double_stack_ix();
 863     __ ldrd(dest->as_double_reg(), stack_slot_address(index, c_sz64, rscratch1));
 864 
 865   } else {
 866     ShouldNotReachHere();
 867   }
 868 }
 869 
 870 
 871 void LIR_Assembler::klass2reg_with_patching(Register reg, CodeEmitInfo* info) {
 872   address target = nullptr;
 873   relocInfo::relocType reloc_type = relocInfo::none;
 874 
 875   switch (patching_id(info)) {
 876   case PatchingStub::access_field_id:
 877     target = Runtime1::entry_for(C1StubId::access_field_patching_id);
 878     reloc_type = relocInfo::section_word_type;
 879     break;
 880   case PatchingStub::load_klass_id:
 881     target = Runtime1::entry_for(C1StubId::load_klass_patching_id);
 882     reloc_type = relocInfo::metadata_type;
 883     break;
 884   case PatchingStub::load_mirror_id:
 885     target = Runtime1::entry_for(C1StubId::load_mirror_patching_id);
 886     reloc_type = relocInfo::oop_type;
 887     break;
 888   case PatchingStub::load_appendix_id:
 889     target = Runtime1::entry_for(C1StubId::load_appendix_patching_id);
 890     reloc_type = relocInfo::oop_type;
 891     break;
 892   default: ShouldNotReachHere();
 893   }
 894 
 895   __ far_call(RuntimeAddress(target));
 896   add_call_info_here(info);
 897 }
 898 
 899 void LIR_Assembler::stack2stack(LIR_Opr src, LIR_Opr dest, BasicType type) {
 900 
 901   LIR_Opr temp;
 902   if (type == T_LONG || type == T_DOUBLE)
 903     temp = FrameMap::rscratch1_long_opr;
 904   else
 905     temp = FrameMap::rscratch1_opr;
 906 
 907   stack2reg(src, temp, src->type());
 908   reg2stack(temp, dest, dest->type(), false);
 909 }
 910 
 911 
 912 void LIR_Assembler::mem2reg(LIR_Opr src, LIR_Opr dest, BasicType type, LIR_PatchCode patch_code, CodeEmitInfo* info, bool wide) {
 913   LIR_Address* addr = src->as_address_ptr();
 914   LIR_Address* from_addr = src->as_address_ptr();
 915 
 916   if (addr->base()->type() == T_OBJECT) {
 917     __ verify_oop(addr->base()->as_pointer_register());
 918   }
 919 
 920   if (patch_code != lir_patch_none) {
 921     deoptimize_trap(info);
 922     return;
 923   }
 924 
 925   if (info != nullptr) {
 926     add_debug_info_for_null_check_here(info);
 927   }
 928   int null_check_here = code_offset();
 929   switch (type) {
 930     case T_FLOAT: {
 931       __ ldrs(dest->as_float_reg(), as_Address(from_addr));
 932       break;
 933     }
 934 
 935     case T_DOUBLE: {
 936       __ ldrd(dest->as_double_reg(), as_Address(from_addr));
 937       break;
 938     }
 939 
 940     case T_ARRAY:   // fall through
 941     case T_OBJECT:  // fall through
 942       if (UseCompressedOops && !wide) {
 943         __ ldrw(dest->as_register(), as_Address(from_addr));
 944       } else {
 945         __ ldr(dest->as_register(), as_Address(from_addr));
 946       }
 947       break;
 948     case T_METADATA:
 949       // We get here to store a method pointer to the stack to pass to
 950       // a dtrace runtime call. This can't work on 64 bit with
 951       // compressed klass ptrs: T_METADATA can be a compressed klass
 952       // ptr or a 64 bit method pointer.
 953       ShouldNotReachHere();
 954       __ ldr(dest->as_register(), as_Address(from_addr));
 955       break;
 956     case T_ADDRESS:
 957       __ ldr(dest->as_register(), as_Address(from_addr));
 958       break;
 959     case T_INT:
 960       __ ldrw(dest->as_register(), as_Address(from_addr));
 961       break;
 962 
 963     case T_LONG: {
 964       __ ldr(dest->as_register_lo(), as_Address_lo(from_addr));
 965       break;
 966     }
 967 
 968     case T_BYTE:
 969       __ ldrsb(dest->as_register(), as_Address(from_addr));
 970       break;
 971     case T_BOOLEAN: {
 972       __ ldrb(dest->as_register(), as_Address(from_addr));
 973       break;
 974     }
 975 
 976     case T_CHAR:
 977       __ ldrh(dest->as_register(), as_Address(from_addr));
 978       break;
 979     case T_SHORT:
 980       __ ldrsh(dest->as_register(), as_Address(from_addr));
 981       break;
 982 
 983     default:
 984       ShouldNotReachHere();
 985   }
 986 
 987   if (is_reference_type(type)) {
 988     if (UseCompressedOops && !wide) {
 989       __ decode_heap_oop(dest->as_register());
 990     }
 991 
 992     __ verify_oop(dest->as_register());
 993   }
 994 }
 995 
 996 
 997 int LIR_Assembler::array_element_size(BasicType type) const {
 998   int elem_size = type2aelembytes(type);
 999   return exact_log2(elem_size);
1000 }
1001 
1002 
1003 void LIR_Assembler::emit_op3(LIR_Op3* op) {
1004   switch (op->code()) {
1005   case lir_idiv:
1006   case lir_irem:
1007     arithmetic_idiv(op->code(),
1008                     op->in_opr1(),
1009                     op->in_opr2(),
1010                     op->in_opr3(),
1011                     op->result_opr(),
1012                     op->info());
1013     break;
1014   case lir_fmad:
1015     __ fmaddd(op->result_opr()->as_double_reg(),
1016               op->in_opr1()->as_double_reg(),
1017               op->in_opr2()->as_double_reg(),
1018               op->in_opr3()->as_double_reg());
1019     break;
1020   case lir_fmaf:
1021     __ fmadds(op->result_opr()->as_float_reg(),
1022               op->in_opr1()->as_float_reg(),
1023               op->in_opr2()->as_float_reg(),
1024               op->in_opr3()->as_float_reg());
1025     break;
1026   default:      ShouldNotReachHere(); break;
1027   }
1028 }
1029 
1030 void LIR_Assembler::emit_opBranch(LIR_OpBranch* op) {
1031 #ifdef ASSERT
1032   assert(op->block() == nullptr || op->block()->label() == op->label(), "wrong label");
1033   if (op->block() != nullptr)  _branch_target_blocks.append(op->block());
1034   if (op->ublock() != nullptr) _branch_target_blocks.append(op->ublock());
1035 #endif
1036 
1037   if (op->cond() == lir_cond_always) {
1038     if (op->info() != nullptr) add_debug_info_for_branch(op->info());
1039     __ b(*(op->label()));
1040   } else {
1041     Assembler::Condition acond;
1042     if (op->code() == lir_cond_float_branch) {
1043       bool is_unordered = (op->ublock() == op->block());
1044       // Assembler::EQ does not permit unordered branches, so we add
1045       // another branch here.  Likewise, Assembler::NE does not permit
1046       // ordered branches.
1047       if ((is_unordered && op->cond() == lir_cond_equal)
1048           || (!is_unordered && op->cond() == lir_cond_notEqual))
1049         __ br(Assembler::VS, *(op->ublock()->label()));
1050       switch(op->cond()) {
1051       case lir_cond_equal:        acond = Assembler::EQ; break;
1052       case lir_cond_notEqual:     acond = Assembler::NE; break;
1053       case lir_cond_less:         acond = (is_unordered ? Assembler::LT : Assembler::LO); break;
1054       case lir_cond_lessEqual:    acond = (is_unordered ? Assembler::LE : Assembler::LS); break;
1055       case lir_cond_greaterEqual: acond = (is_unordered ? Assembler::HS : Assembler::GE); break;
1056       case lir_cond_greater:      acond = (is_unordered ? Assembler::HI : Assembler::GT); break;
1057       default:                    ShouldNotReachHere();
1058         acond = Assembler::EQ;  // unreachable
1059       }
1060     } else {
1061       switch (op->cond()) {
1062         case lir_cond_equal:        acond = Assembler::EQ; break;
1063         case lir_cond_notEqual:     acond = Assembler::NE; break;
1064         case lir_cond_less:         acond = Assembler::LT; break;
1065         case lir_cond_lessEqual:    acond = Assembler::LE; break;
1066         case lir_cond_greaterEqual: acond = Assembler::GE; break;
1067         case lir_cond_greater:      acond = Assembler::GT; break;
1068         case lir_cond_belowEqual:   acond = Assembler::LS; break;
1069         case lir_cond_aboveEqual:   acond = Assembler::HS; break;
1070         default:                    ShouldNotReachHere();
1071           acond = Assembler::EQ;  // unreachable
1072       }
1073     }
1074     __ br(acond,*(op->label()));
1075   }
1076 }
1077 
1078 
1079 
1080 void LIR_Assembler::emit_opConvert(LIR_OpConvert* op) {
1081   LIR_Opr src  = op->in_opr();
1082   LIR_Opr dest = op->result_opr();
1083 
1084   switch (op->bytecode()) {
1085     case Bytecodes::_i2f:
1086       {
1087         __ scvtfws(dest->as_float_reg(), src->as_register());
1088         break;
1089       }
1090     case Bytecodes::_i2d:
1091       {
1092         __ scvtfwd(dest->as_double_reg(), src->as_register());
1093         break;
1094       }
1095     case Bytecodes::_l2d:
1096       {
1097         __ scvtfd(dest->as_double_reg(), src->as_register_lo());
1098         break;
1099       }
1100     case Bytecodes::_l2f:
1101       {
1102         __ scvtfs(dest->as_float_reg(), src->as_register_lo());
1103         break;
1104       }
1105     case Bytecodes::_f2d:
1106       {
1107         __ fcvts(dest->as_double_reg(), src->as_float_reg());
1108         break;
1109       }
1110     case Bytecodes::_d2f:
1111       {
1112         __ fcvtd(dest->as_float_reg(), src->as_double_reg());
1113         break;
1114       }
1115     case Bytecodes::_i2c:
1116       {
1117         __ ubfx(dest->as_register(), src->as_register(), 0, 16);
1118         break;
1119       }
1120     case Bytecodes::_i2l:
1121       {
1122         __ sxtw(dest->as_register_lo(), src->as_register());
1123         break;
1124       }
1125     case Bytecodes::_i2s:
1126       {
1127         __ sxth(dest->as_register(), src->as_register());
1128         break;
1129       }
1130     case Bytecodes::_i2b:
1131       {
1132         __ sxtb(dest->as_register(), src->as_register());
1133         break;
1134       }
1135     case Bytecodes::_l2i:
1136       {
1137         _masm->block_comment("FIXME: This could be a no-op");
1138         __ uxtw(dest->as_register(), src->as_register_lo());
1139         break;
1140       }
1141     case Bytecodes::_d2l:
1142       {
1143         __ fcvtzd(dest->as_register_lo(), src->as_double_reg());
1144         break;
1145       }
1146     case Bytecodes::_f2i:
1147       {
1148         __ fcvtzsw(dest->as_register(), src->as_float_reg());
1149         break;
1150       }
1151     case Bytecodes::_f2l:
1152       {
1153         __ fcvtzs(dest->as_register_lo(), src->as_float_reg());
1154         break;
1155       }
1156     case Bytecodes::_d2i:
1157       {
1158         __ fcvtzdw(dest->as_register(), src->as_double_reg());
1159         break;
1160       }
1161     default: ShouldNotReachHere();
1162   }
1163 }
1164 
1165 void LIR_Assembler::emit_alloc_obj(LIR_OpAllocObj* op) {
1166   if (op->init_check()) {
1167     __ lea(rscratch1, Address(op->klass()->as_register(), InstanceKlass::init_state_offset()));
1168     __ ldarb(rscratch1, rscratch1);
1169     __ cmpw(rscratch1, InstanceKlass::fully_initialized);
1170     add_debug_info_for_null_check_here(op->stub()->info());
1171     __ br(Assembler::NE, *op->stub()->entry());
1172   }
1173   __ allocate_object(op->obj()->as_register(),
1174                      op->tmp1()->as_register(),
1175                      op->tmp2()->as_register(),
1176                      op->header_size(),
1177                      op->object_size(),
1178                      op->klass()->as_register(),
1179                      *op->stub()->entry());
1180   __ bind(*op->stub()->continuation());
1181 }
1182 
1183 void LIR_Assembler::emit_alloc_array(LIR_OpAllocArray* op) {
1184   Register len =  op->len()->as_register();
1185   __ uxtw(len, len);
1186 
1187   if (UseSlowPath ||
1188       (!UseFastNewObjectArray && is_reference_type(op->type())) ||
1189       (!UseFastNewTypeArray   && !is_reference_type(op->type()))) {
1190     __ b(*op->stub()->entry());
1191   } else {
1192     Register tmp1 = op->tmp1()->as_register();
1193     Register tmp2 = op->tmp2()->as_register();
1194     Register tmp3 = op->tmp3()->as_register();
1195     if (len == tmp1) {
1196       tmp1 = tmp3;
1197     } else if (len == tmp2) {
1198       tmp2 = tmp3;
1199     } else if (len == tmp3) {
1200       // everything is ok
1201     } else {
1202       __ mov(tmp3, len);
1203     }
1204     __ allocate_array(op->obj()->as_register(),
1205                       len,
1206                       tmp1,
1207                       tmp2,
1208                       arrayOopDesc::base_offset_in_bytes(op->type()),
1209                       array_element_size(op->type()),
1210                       op->klass()->as_register(),
1211                       *op->stub()->entry(),
1212                       op->zero_array());
1213   }
1214   __ bind(*op->stub()->continuation());
1215 }
1216 
1217 void LIR_Assembler::type_profile_helper(Register mdo,
1218                                         ciMethodData *md, ciProfileData *data,
1219                                         Register recv, Label* update_done) {
1220 
1221   // Given a profile data offset, generate an Address which points to
1222   // the corresponding slot in mdo->data().
1223   // Clobbers rscratch2.
1224   auto slot_at = [=](ByteSize offset) -> Address {
1225     return __ form_address(rscratch2, mdo,
1226                            md->byte_offset_of_slot(data, offset),
1227                            LogBytesPerWord);
1228   };
1229 
1230   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1231     Label next_test;
1232     // See if the receiver is receiver[n].
1233     __ ldr(rscratch1, slot_at(ReceiverTypeData::receiver_offset(i)));
1234     __ cmp(recv, rscratch1);
1235     __ br(Assembler::NE, next_test);
1236     __ addptr(slot_at(ReceiverTypeData::receiver_count_offset(i)),
1237               DataLayout::counter_increment);
1238     __ b(*update_done);
1239     __ bind(next_test);
1240   }
1241 
1242   // Didn't find receiver; find next empty slot and fill it in
1243   for (uint i = 0; i < ReceiverTypeData::row_limit(); i++) {
1244     Label next_test;
1245     Address recv_addr(slot_at(ReceiverTypeData::receiver_offset(i)));
1246     __ ldr(rscratch1, recv_addr);
1247     __ cbnz(rscratch1, next_test);
1248     __ str(recv, recv_addr);
1249     __ mov(rscratch1, DataLayout::counter_increment);
1250     __ str(rscratch1, slot_at(ReceiverTypeData::receiver_count_offset(i)));
1251     __ b(*update_done);
1252     __ bind(next_test);
1253   }
1254 }
1255 
1256 void LIR_Assembler::emit_typecheck_helper(LIR_OpTypeCheck *op, Label* success, Label* failure, Label* obj_is_null) {
1257   // we always need a stub for the failure case.
1258   CodeStub* stub = op->stub();
1259   Register obj = op->object()->as_register();
1260   Register k_RInfo = op->tmp1()->as_register();
1261   Register klass_RInfo = op->tmp2()->as_register();
1262   Register dst = op->result_opr()->as_register();
1263   ciKlass* k = op->klass();
1264   Register Rtmp1 = noreg;
1265 
1266   // check if it needs to be profiled
1267   ciMethodData* md;
1268   ciProfileData* data;
1269 
1270   const bool should_profile = op->should_profile();
1271 
1272   if (should_profile) {
1273     ciMethod* method = op->profiled_method();
1274     assert(method != nullptr, "Should have method");
1275     int bci = op->profiled_bci();
1276     md = method->method_data_or_null();
1277     assert(md != nullptr, "Sanity");
1278     data = md->bci_to_data(bci);
1279     assert(data != nullptr,                "need data for type check");
1280     assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1281   }
1282   Label* success_target = success;
1283   Label* failure_target = failure;
1284 
1285   if (obj == k_RInfo) {
1286     k_RInfo = dst;
1287   } else if (obj == klass_RInfo) {
1288     klass_RInfo = dst;
1289   }
1290   if (k->is_loaded() && !UseCompressedClassPointers) {
1291     select_different_registers(obj, dst, k_RInfo, klass_RInfo);
1292   } else {
1293     Rtmp1 = op->tmp3()->as_register();
1294     select_different_registers(obj, dst, k_RInfo, klass_RInfo, Rtmp1);
1295   }
1296 
1297   assert_different_registers(obj, k_RInfo, klass_RInfo);
1298 
1299   if (should_profile) {
1300     Register mdo  = klass_RInfo;
1301     __ mov_metadata(mdo, md->constant_encoding());
1302     Label not_null;
1303     __ cbnz(obj, not_null);
1304     // Object is null; update MDO and exit
1305     Address data_addr
1306       = __ form_address(rscratch2, mdo,
1307                         md->byte_offset_of_slot(data, DataLayout::flags_offset()),
1308                         0);
1309     __ ldrb(rscratch1, data_addr);
1310     __ orr(rscratch1, rscratch1, BitData::null_seen_byte_constant());
1311     __ strb(rscratch1, data_addr);
1312     __ b(*obj_is_null);
1313     __ bind(not_null);
1314 
1315     Label update_done;
1316     Register recv = k_RInfo;
1317     __ load_klass(recv, obj);
1318     type_profile_helper(mdo, md, data, recv, &update_done);
1319     Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1320     __ addptr(counter_addr, DataLayout::counter_increment);
1321 
1322     __ bind(update_done);
1323   } else {
1324     __ cbz(obj, *obj_is_null);
1325   }
1326 
1327   if (!k->is_loaded()) {
1328     klass2reg_with_patching(k_RInfo, op->info_for_patch());
1329   } else {
1330     __ mov_metadata(k_RInfo, k->constant_encoding());
1331   }
1332   __ verify_oop(obj);
1333 
1334   if (op->fast_check()) {
1335     // get object class
1336     // not a safepoint as obj null check happens earlier
1337     __ load_klass(rscratch1, obj);
1338     __ cmp( rscratch1, k_RInfo);
1339 
1340     __ br(Assembler::NE, *failure_target);
1341     // successful cast, fall through to profile or jump
1342   } else {
1343     // get object class
1344     // not a safepoint as obj null check happens earlier
1345     __ load_klass(klass_RInfo, obj);
1346     if (k->is_loaded()) {
1347       // See if we get an immediate positive hit
1348       __ ldr(rscratch1, Address(klass_RInfo, int64_t(k->super_check_offset())));
1349       __ cmp(k_RInfo, rscratch1);
1350       if ((juint)in_bytes(Klass::secondary_super_cache_offset()) != k->super_check_offset()) {
1351         __ br(Assembler::NE, *failure_target);
1352         // successful cast, fall through to profile or jump
1353       } else {
1354         // See if we get an immediate positive hit
1355         __ br(Assembler::EQ, *success_target);
1356         // check for self
1357         __ cmp(klass_RInfo, k_RInfo);
1358         __ br(Assembler::EQ, *success_target);
1359 
1360         __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1361         __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::slow_subtype_check_id)));
1362         __ ldr(klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1363         // result is a boolean
1364         __ cbzw(klass_RInfo, *failure_target);
1365         // successful cast, fall through to profile or jump
1366       }
1367     } else {
1368       // perform the fast part of the checking logic
1369       __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1370       // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1371       __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1372       __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::slow_subtype_check_id)));
1373       __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1374       // result is a boolean
1375       __ cbz(k_RInfo, *failure_target);
1376       // successful cast, fall through to profile or jump
1377     }
1378   }
1379   __ b(*success);
1380 }
1381 
1382 
1383 void LIR_Assembler::emit_opTypeCheck(LIR_OpTypeCheck* op) {
1384   const bool should_profile = op->should_profile();
1385 
1386   LIR_Code code = op->code();
1387   if (code == lir_store_check) {
1388     Register value = op->object()->as_register();
1389     Register array = op->array()->as_register();
1390     Register k_RInfo = op->tmp1()->as_register();
1391     Register klass_RInfo = op->tmp2()->as_register();
1392     Register Rtmp1 = op->tmp3()->as_register();
1393 
1394     CodeStub* stub = op->stub();
1395 
1396     // check if it needs to be profiled
1397     ciMethodData* md;
1398     ciProfileData* data;
1399 
1400     if (should_profile) {
1401       ciMethod* method = op->profiled_method();
1402       assert(method != nullptr, "Should have method");
1403       int bci = op->profiled_bci();
1404       md = method->method_data_or_null();
1405       assert(md != nullptr, "Sanity");
1406       data = md->bci_to_data(bci);
1407       assert(data != nullptr,                "need data for type check");
1408       assert(data->is_ReceiverTypeData(), "need ReceiverTypeData for type check");
1409     }
1410     Label done;
1411     Label* success_target = &done;
1412     Label* failure_target = stub->entry();
1413 
1414     if (should_profile) {
1415       Label not_null;
1416       Register mdo  = klass_RInfo;
1417       __ mov_metadata(mdo, md->constant_encoding());
1418       __ cbnz(value, not_null);
1419       // Object is null; update MDO and exit
1420       Address data_addr
1421         = __ form_address(rscratch2, mdo,
1422                           md->byte_offset_of_slot(data, DataLayout::flags_offset()), 0);
1423       __ ldrb(rscratch1, data_addr);
1424       __ orr(rscratch1, rscratch1, BitData::null_seen_byte_constant());
1425       __ strb(rscratch1, data_addr);
1426       __ b(done);
1427       __ bind(not_null);
1428 
1429       Label update_done;
1430       Register recv = k_RInfo;
1431       __ load_klass(recv, value);
1432       type_profile_helper(mdo, md, data, recv, &update_done);
1433       Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
1434       __ addptr(counter_addr, DataLayout::counter_increment);
1435       __ bind(update_done);
1436     } else {
1437       __ cbz(value, done);
1438     }
1439 
1440     add_debug_info_for_null_check_here(op->info_for_exception());
1441     __ load_klass(k_RInfo, array);
1442     __ load_klass(klass_RInfo, value);
1443 
1444     // get instance klass (it's already uncompressed)
1445     __ ldr(k_RInfo, Address(k_RInfo, ObjArrayKlass::element_klass_offset()));
1446     // perform the fast part of the checking logic
1447     __ check_klass_subtype_fast_path(klass_RInfo, k_RInfo, Rtmp1, success_target, failure_target, nullptr);
1448     // call out-of-line instance of __ check_klass_subtype_slow_path(...):
1449     __ stp(klass_RInfo, k_RInfo, Address(__ pre(sp, -2 * wordSize)));
1450     __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::slow_subtype_check_id)));
1451     __ ldp(k_RInfo, klass_RInfo, Address(__ post(sp, 2 * wordSize)));
1452     // result is a boolean
1453     __ cbzw(k_RInfo, *failure_target);
1454     // fall through to the success case
1455 
1456     __ bind(done);
1457   } else if (code == lir_checkcast) {
1458     Register obj = op->object()->as_register();
1459     Register dst = op->result_opr()->as_register();
1460     Label success;
1461     emit_typecheck_helper(op, &success, op->stub()->entry(), &success);
1462     __ bind(success);
1463     if (dst != obj) {
1464       __ mov(dst, obj);
1465     }
1466   } else if (code == lir_instanceof) {
1467     Register obj = op->object()->as_register();
1468     Register dst = op->result_opr()->as_register();
1469     Label success, failure, done;
1470     emit_typecheck_helper(op, &success, &failure, &failure);
1471     __ bind(failure);
1472     __ mov(dst, zr);
1473     __ b(done);
1474     __ bind(success);
1475     __ mov(dst, 1);
1476     __ bind(done);
1477   } else {
1478     ShouldNotReachHere();
1479   }
1480 }
1481 
1482 void LIR_Assembler::casw(Register addr, Register newval, Register cmpval) {
1483   __ cmpxchg(addr, cmpval, newval, Assembler::word, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1484   __ cset(rscratch1, Assembler::NE);
1485   __ membar(__ AnyAny);
1486 }
1487 
1488 void LIR_Assembler::casl(Register addr, Register newval, Register cmpval) {
1489   __ cmpxchg(addr, cmpval, newval, Assembler::xword, /* acquire*/ true, /* release*/ true, /* weak*/ false, rscratch1);
1490   __ cset(rscratch1, Assembler::NE);
1491   __ membar(__ AnyAny);
1492 }
1493 
1494 
1495 void LIR_Assembler::emit_compare_and_swap(LIR_OpCompareAndSwap* op) {
1496   Register addr;
1497   if (op->addr()->is_register()) {
1498     addr = as_reg(op->addr());
1499   } else {
1500     assert(op->addr()->is_address(), "what else?");
1501     LIR_Address* addr_ptr = op->addr()->as_address_ptr();
1502     assert(addr_ptr->disp() == 0, "need 0 disp");
1503     assert(addr_ptr->index() == LIR_Opr::illegalOpr(), "need 0 index");
1504     addr = as_reg(addr_ptr->base());
1505   }
1506   Register newval = as_reg(op->new_value());
1507   Register cmpval = as_reg(op->cmp_value());
1508 
1509   if (op->code() == lir_cas_obj) {
1510     if (UseCompressedOops) {
1511       Register t1 = op->tmp1()->as_register();
1512       assert(op->tmp1()->is_valid(), "must be");
1513       __ encode_heap_oop(t1, cmpval);
1514       cmpval = t1;
1515       __ encode_heap_oop(rscratch2, newval);
1516       newval = rscratch2;
1517       casw(addr, newval, cmpval);
1518     } else {
1519       casl(addr, newval, cmpval);
1520     }
1521   } else if (op->code() == lir_cas_int) {
1522     casw(addr, newval, cmpval);
1523   } else {
1524     casl(addr, newval, cmpval);
1525   }
1526 }
1527 
1528 
1529 void LIR_Assembler::cmove(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Opr result, BasicType type,
1530                           LIR_Opr cmp_opr1, LIR_Opr cmp_opr2) {
1531   assert(cmp_opr1 == LIR_OprFact::illegalOpr && cmp_opr2 == LIR_OprFact::illegalOpr, "unnecessary cmp oprs on aarch64");
1532 
1533   Assembler::Condition acond, ncond;
1534   switch (condition) {
1535   case lir_cond_equal:        acond = Assembler::EQ; ncond = Assembler::NE; break;
1536   case lir_cond_notEqual:     acond = Assembler::NE; ncond = Assembler::EQ; break;
1537   case lir_cond_less:         acond = Assembler::LT; ncond = Assembler::GE; break;
1538   case lir_cond_lessEqual:    acond = Assembler::LE; ncond = Assembler::GT; break;
1539   case lir_cond_greaterEqual: acond = Assembler::GE; ncond = Assembler::LT; break;
1540   case lir_cond_greater:      acond = Assembler::GT; ncond = Assembler::LE; break;
1541   case lir_cond_belowEqual:
1542   case lir_cond_aboveEqual:
1543   default:                    ShouldNotReachHere();
1544     acond = Assembler::EQ; ncond = Assembler::NE;  // unreachable
1545   }
1546 
1547   assert(result->is_single_cpu() || result->is_double_cpu(),
1548          "expect single register for result");
1549   if (opr1->is_constant() && opr2->is_constant()
1550       && opr1->type() == T_INT && opr2->type() == T_INT) {
1551     jint val1 = opr1->as_jint();
1552     jint val2 = opr2->as_jint();
1553     if (val1 == 0 && val2 == 1) {
1554       __ cset(result->as_register(), ncond);
1555       return;
1556     } else if (val1 == 1 && val2 == 0) {
1557       __ cset(result->as_register(), acond);
1558       return;
1559     }
1560   }
1561 
1562   if (opr1->is_constant() && opr2->is_constant()
1563       && opr1->type() == T_LONG && opr2->type() == T_LONG) {
1564     jlong val1 = opr1->as_jlong();
1565     jlong val2 = opr2->as_jlong();
1566     if (val1 == 0 && val2 == 1) {
1567       __ cset(result->as_register_lo(), ncond);
1568       return;
1569     } else if (val1 == 1 && val2 == 0) {
1570       __ cset(result->as_register_lo(), acond);
1571       return;
1572     }
1573   }
1574 
1575   if (opr1->is_stack()) {
1576     stack2reg(opr1, FrameMap::rscratch1_opr, result->type());
1577     opr1 = FrameMap::rscratch1_opr;
1578   } else if (opr1->is_constant()) {
1579     LIR_Opr tmp
1580       = opr1->type() == T_LONG ? FrameMap::rscratch1_long_opr : FrameMap::rscratch1_opr;
1581     const2reg(opr1, tmp, lir_patch_none, nullptr);
1582     opr1 = tmp;
1583   }
1584 
1585   if (opr2->is_stack()) {
1586     stack2reg(opr2, FrameMap::rscratch2_opr, result->type());
1587     opr2 = FrameMap::rscratch2_opr;
1588   } else if (opr2->is_constant()) {
1589     LIR_Opr tmp
1590       = opr2->type() == T_LONG ? FrameMap::rscratch2_long_opr : FrameMap::rscratch2_opr;
1591     const2reg(opr2, tmp, lir_patch_none, nullptr);
1592     opr2 = tmp;
1593   }
1594 
1595   if (result->type() == T_LONG)
1596     __ csel(result->as_register_lo(), opr1->as_register_lo(), opr2->as_register_lo(), acond);
1597   else
1598     __ csel(result->as_register(), opr1->as_register(), opr2->as_register(), acond);
1599 }
1600 
1601 void LIR_Assembler::arith_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dest, CodeEmitInfo* info, bool pop_fpu_stack) {
1602   assert(info == nullptr, "should never be used, idiv/irem and ldiv/lrem not handled by this method");
1603 
1604   if (left->is_single_cpu()) {
1605     Register lreg = left->as_register();
1606     Register dreg = as_reg(dest);
1607 
1608     if (right->is_single_cpu()) {
1609       // cpu register - cpu register
1610 
1611       assert(left->type() == T_INT && right->type() == T_INT && dest->type() == T_INT,
1612              "should be");
1613       Register rreg = right->as_register();
1614       switch (code) {
1615       case lir_add: __ addw (dest->as_register(), lreg, rreg); break;
1616       case lir_sub: __ subw (dest->as_register(), lreg, rreg); break;
1617       case lir_mul: __ mulw (dest->as_register(), lreg, rreg); break;
1618       default:      ShouldNotReachHere();
1619       }
1620 
1621     } else if (right->is_double_cpu()) {
1622       Register rreg = right->as_register_lo();
1623       // single_cpu + double_cpu: can happen with obj+long
1624       assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1625       switch (code) {
1626       case lir_add: __ add(dreg, lreg, rreg); break;
1627       case lir_sub: __ sub(dreg, lreg, rreg); break;
1628       default: ShouldNotReachHere();
1629       }
1630     } else if (right->is_constant()) {
1631       // cpu register - constant
1632       jlong c;
1633 
1634       // FIXME.  This is fugly: we really need to factor all this logic.
1635       switch(right->type()) {
1636       case T_LONG:
1637         c = right->as_constant_ptr()->as_jlong();
1638         break;
1639       case T_INT:
1640       case T_ADDRESS:
1641         c = right->as_constant_ptr()->as_jint();
1642         break;
1643       default:
1644         ShouldNotReachHere();
1645         c = 0;  // unreachable
1646         break;
1647       }
1648 
1649       assert(code == lir_add || code == lir_sub, "mismatched arithmetic op");
1650       if (c == 0 && dreg == lreg) {
1651         COMMENT("effective nop elided");
1652         return;
1653       }
1654       switch(left->type()) {
1655       case T_INT:
1656         switch (code) {
1657         case lir_add: __ addw(dreg, lreg, c); break;
1658         case lir_sub: __ subw(dreg, lreg, c); break;
1659         default: ShouldNotReachHere();
1660         }
1661         break;
1662       case T_OBJECT:
1663       case T_ADDRESS:
1664         switch (code) {
1665         case lir_add: __ add(dreg, lreg, c); break;
1666         case lir_sub: __ sub(dreg, lreg, c); break;
1667         default: ShouldNotReachHere();
1668         }
1669         break;
1670       default:
1671         ShouldNotReachHere();
1672       }
1673     } else {
1674       ShouldNotReachHere();
1675     }
1676 
1677   } else if (left->is_double_cpu()) {
1678     Register lreg_lo = left->as_register_lo();
1679 
1680     if (right->is_double_cpu()) {
1681       // cpu register - cpu register
1682       Register rreg_lo = right->as_register_lo();
1683       switch (code) {
1684       case lir_add: __ add (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1685       case lir_sub: __ sub (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1686       case lir_mul: __ mul (dest->as_register_lo(), lreg_lo, rreg_lo); break;
1687       case lir_div: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, false, rscratch1); break;
1688       case lir_rem: __ corrected_idivq(dest->as_register_lo(), lreg_lo, rreg_lo, true, rscratch1); break;
1689       default:
1690         ShouldNotReachHere();
1691       }
1692 
1693     } else if (right->is_constant()) {
1694       jlong c = right->as_constant_ptr()->as_jlong();
1695       Register dreg = as_reg(dest);
1696       switch (code) {
1697         case lir_add:
1698         case lir_sub:
1699           if (c == 0 && dreg == lreg_lo) {
1700             COMMENT("effective nop elided");
1701             return;
1702           }
1703           code == lir_add ? __ add(dreg, lreg_lo, c) : __ sub(dreg, lreg_lo, c);
1704           break;
1705         case lir_div:
1706           assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1707           if (c == 1) {
1708             // move lreg_lo to dreg if divisor is 1
1709             __ mov(dreg, lreg_lo);
1710           } else {
1711             unsigned int shift = log2i_exact(c);
1712             // use rscratch1 as intermediate result register
1713             __ asr(rscratch1, lreg_lo, 63);
1714             __ add(rscratch1, lreg_lo, rscratch1, Assembler::LSR, 64 - shift);
1715             __ asr(dreg, rscratch1, shift);
1716           }
1717           break;
1718         case lir_rem:
1719           assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1720           if (c == 1) {
1721             // move 0 to dreg if divisor is 1
1722             __ mov(dreg, zr);
1723           } else {
1724             // use rscratch1 as intermediate result register
1725             __ negs(rscratch1, lreg_lo);
1726             __ andr(dreg, lreg_lo, c - 1);
1727             __ andr(rscratch1, rscratch1, c - 1);
1728             __ csneg(dreg, dreg, rscratch1, Assembler::MI);
1729           }
1730           break;
1731         default:
1732           ShouldNotReachHere();
1733       }
1734     } else {
1735       ShouldNotReachHere();
1736     }
1737   } else if (left->is_single_fpu()) {
1738     assert(right->is_single_fpu(), "right hand side of float arithmetics needs to be float register");
1739     switch (code) {
1740     case lir_add: __ fadds (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1741     case lir_sub: __ fsubs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1742     case lir_mul: __ fmuls (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1743     case lir_div: __ fdivs (dest->as_float_reg(), left->as_float_reg(), right->as_float_reg()); break;
1744     default:
1745       ShouldNotReachHere();
1746     }
1747   } else if (left->is_double_fpu()) {
1748     if (right->is_double_fpu()) {
1749       // fpu register - fpu register
1750       switch (code) {
1751       case lir_add: __ faddd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1752       case lir_sub: __ fsubd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1753       case lir_mul: __ fmuld (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1754       case lir_div: __ fdivd (dest->as_double_reg(), left->as_double_reg(), right->as_double_reg()); break;
1755       default:
1756         ShouldNotReachHere();
1757       }
1758     } else {
1759       if (right->is_constant()) {
1760         ShouldNotReachHere();
1761       }
1762       ShouldNotReachHere();
1763     }
1764   } else if (left->is_single_stack() || left->is_address()) {
1765     assert(left == dest, "left and dest must be equal");
1766     ShouldNotReachHere();
1767   } else {
1768     ShouldNotReachHere();
1769   }
1770 }
1771 
1772 void LIR_Assembler::arith_fpu_implementation(LIR_Code code, int left_index, int right_index, int dest_index, bool pop_fpu_stack) { Unimplemented(); }
1773 
1774 
1775 void LIR_Assembler::intrinsic_op(LIR_Code code, LIR_Opr value, LIR_Opr tmp, LIR_Opr dest, LIR_Op* op) {
1776   switch(code) {
1777   case lir_abs : __ fabsd(dest->as_double_reg(), value->as_double_reg()); break;
1778   case lir_sqrt: __ fsqrtd(dest->as_double_reg(), value->as_double_reg()); break;
1779   case lir_f2hf: __ flt_to_flt16(dest->as_register(), value->as_float_reg(), tmp->as_float_reg()); break;
1780   case lir_hf2f: __ flt16_to_flt(dest->as_float_reg(), value->as_register(), tmp->as_float_reg()); break;
1781   default      : ShouldNotReachHere();
1782   }
1783 }
1784 
1785 void LIR_Assembler::logic_op(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst) {
1786 
1787   assert(left->is_single_cpu() || left->is_double_cpu(), "expect single or double register");
1788   Register Rleft = left->is_single_cpu() ? left->as_register() :
1789                                            left->as_register_lo();
1790    if (dst->is_single_cpu()) {
1791      Register Rdst = dst->as_register();
1792      if (right->is_constant()) {
1793        switch (code) {
1794          case lir_logic_and: __ andw (Rdst, Rleft, right->as_jint()); break;
1795          case lir_logic_or:  __ orrw (Rdst, Rleft, right->as_jint()); break;
1796          case lir_logic_xor: __ eorw (Rdst, Rleft, right->as_jint()); break;
1797          default: ShouldNotReachHere(); break;
1798        }
1799      } else {
1800        Register Rright = right->is_single_cpu() ? right->as_register() :
1801                                                   right->as_register_lo();
1802        switch (code) {
1803          case lir_logic_and: __ andw (Rdst, Rleft, Rright); break;
1804          case lir_logic_or:  __ orrw (Rdst, Rleft, Rright); break;
1805          case lir_logic_xor: __ eorw (Rdst, Rleft, Rright); break;
1806          default: ShouldNotReachHere(); break;
1807        }
1808      }
1809    } else {
1810      Register Rdst = dst->as_register_lo();
1811      if (right->is_constant()) {
1812        switch (code) {
1813          case lir_logic_and: __ andr (Rdst, Rleft, right->as_jlong()); break;
1814          case lir_logic_or:  __ orr (Rdst, Rleft, right->as_jlong()); break;
1815          case lir_logic_xor: __ eor (Rdst, Rleft, right->as_jlong()); break;
1816          default: ShouldNotReachHere(); break;
1817        }
1818      } else {
1819        Register Rright = right->is_single_cpu() ? right->as_register() :
1820                                                   right->as_register_lo();
1821        switch (code) {
1822          case lir_logic_and: __ andr (Rdst, Rleft, Rright); break;
1823          case lir_logic_or:  __ orr (Rdst, Rleft, Rright); break;
1824          case lir_logic_xor: __ eor (Rdst, Rleft, Rright); break;
1825          default: ShouldNotReachHere(); break;
1826        }
1827      }
1828    }
1829 }
1830 
1831 
1832 
1833 void LIR_Assembler::arithmetic_idiv(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr illegal, LIR_Opr result, CodeEmitInfo* info) {
1834 
1835   // opcode check
1836   assert((code == lir_idiv) || (code == lir_irem), "opcode must be idiv or irem");
1837   bool is_irem = (code == lir_irem);
1838 
1839   // operand check
1840   assert(left->is_single_cpu(),   "left must be register");
1841   assert(right->is_single_cpu() || right->is_constant(),  "right must be register or constant");
1842   assert(result->is_single_cpu(), "result must be register");
1843   Register lreg = left->as_register();
1844   Register dreg = result->as_register();
1845 
1846   // power-of-2 constant check and codegen
1847   if (right->is_constant()) {
1848     int c = right->as_constant_ptr()->as_jint();
1849     assert(c > 0 && is_power_of_2(c), "divisor must be power-of-2 constant");
1850     if (is_irem) {
1851       if (c == 1) {
1852         // move 0 to dreg if divisor is 1
1853         __ movw(dreg, zr);
1854       } else {
1855         // use rscratch1 as intermediate result register
1856         __ negsw(rscratch1, lreg);
1857         __ andw(dreg, lreg, c - 1);
1858         __ andw(rscratch1, rscratch1, c - 1);
1859         __ csnegw(dreg, dreg, rscratch1, Assembler::MI);
1860       }
1861     } else {
1862       if (c == 1) {
1863         // move lreg to dreg if divisor is 1
1864         __ movw(dreg, lreg);
1865       } else {
1866         unsigned int shift = exact_log2(c);
1867         // use rscratch1 as intermediate result register
1868         __ asrw(rscratch1, lreg, 31);
1869         __ addw(rscratch1, lreg, rscratch1, Assembler::LSR, 32 - shift);
1870         __ asrw(dreg, rscratch1, shift);
1871       }
1872     }
1873   } else {
1874     Register rreg = right->as_register();
1875     __ corrected_idivl(dreg, lreg, rreg, is_irem, rscratch1);
1876   }
1877 }
1878 
1879 
1880 void LIR_Assembler::comp_op(LIR_Condition condition, LIR_Opr opr1, LIR_Opr opr2, LIR_Op2* op) {
1881   if (opr1->is_constant() && opr2->is_single_cpu()) {
1882     // tableswitch
1883     Register reg = as_reg(opr2);
1884     struct tableswitch &table = switches[opr1->as_constant_ptr()->as_jint()];
1885     __ tableswitch(reg, table._first_key, table._last_key, table._branches, table._after);
1886   } else if (opr1->is_single_cpu() || opr1->is_double_cpu()) {
1887     Register reg1 = as_reg(opr1);
1888     if (opr2->is_single_cpu()) {
1889       // cpu register - cpu register
1890       Register reg2 = opr2->as_register();
1891       if (is_reference_type(opr1->type())) {
1892         __ cmpoop(reg1, reg2);
1893       } else {
1894         assert(!is_reference_type(opr2->type()), "cmp int, oop?");
1895         __ cmpw(reg1, reg2);
1896       }
1897       return;
1898     }
1899     if (opr2->is_double_cpu()) {
1900       // cpu register - cpu register
1901       Register reg2 = opr2->as_register_lo();
1902       __ cmp(reg1, reg2);
1903       return;
1904     }
1905 
1906     if (opr2->is_constant()) {
1907       bool is_32bit = false; // width of register operand
1908       jlong imm;
1909 
1910       switch(opr2->type()) {
1911       case T_INT:
1912         imm = opr2->as_constant_ptr()->as_jint();
1913         is_32bit = true;
1914         break;
1915       case T_LONG:
1916         imm = opr2->as_constant_ptr()->as_jlong();
1917         break;
1918       case T_ADDRESS:
1919         imm = opr2->as_constant_ptr()->as_jint();
1920         break;
1921       case T_METADATA:
1922         imm = (intptr_t)(opr2->as_constant_ptr()->as_metadata());
1923         break;
1924       case T_OBJECT:
1925       case T_ARRAY:
1926         jobject2reg(opr2->as_constant_ptr()->as_jobject(), rscratch1);
1927         __ cmpoop(reg1, rscratch1);
1928         return;
1929       default:
1930         ShouldNotReachHere();
1931         imm = 0;  // unreachable
1932         break;
1933       }
1934 
1935       if (Assembler::operand_valid_for_add_sub_immediate(imm)) {
1936         if (is_32bit)
1937           __ cmpw(reg1, imm);
1938         else
1939           __ subs(zr, reg1, imm);
1940         return;
1941       } else {
1942         __ mov(rscratch1, imm);
1943         if (is_32bit)
1944           __ cmpw(reg1, rscratch1);
1945         else
1946           __ cmp(reg1, rscratch1);
1947         return;
1948       }
1949     } else
1950       ShouldNotReachHere();
1951   } else if (opr1->is_single_fpu()) {
1952     FloatRegister reg1 = opr1->as_float_reg();
1953     assert(opr2->is_single_fpu(), "expect single float register");
1954     FloatRegister reg2 = opr2->as_float_reg();
1955     __ fcmps(reg1, reg2);
1956   } else if (opr1->is_double_fpu()) {
1957     FloatRegister reg1 = opr1->as_double_reg();
1958     assert(opr2->is_double_fpu(), "expect double float register");
1959     FloatRegister reg2 = opr2->as_double_reg();
1960     __ fcmpd(reg1, reg2);
1961   } else {
1962     ShouldNotReachHere();
1963   }
1964 }
1965 
1966 void LIR_Assembler::comp_fl2i(LIR_Code code, LIR_Opr left, LIR_Opr right, LIR_Opr dst, LIR_Op2* op){
1967   if (code == lir_cmp_fd2i || code == lir_ucmp_fd2i) {
1968     bool is_unordered_less = (code == lir_ucmp_fd2i);
1969     if (left->is_single_fpu()) {
1970       __ float_cmp(true, is_unordered_less ? -1 : 1, left->as_float_reg(), right->as_float_reg(), dst->as_register());
1971     } else if (left->is_double_fpu()) {
1972       __ float_cmp(false, is_unordered_less ? -1 : 1, left->as_double_reg(), right->as_double_reg(), dst->as_register());
1973     } else {
1974       ShouldNotReachHere();
1975     }
1976   } else if (code == lir_cmp_l2i) {
1977     Label done;
1978     __ cmp(left->as_register_lo(), right->as_register_lo());
1979     __ mov(dst->as_register(), (uint64_t)-1L);
1980     __ br(Assembler::LT, done);
1981     __ csinc(dst->as_register(), zr, zr, Assembler::EQ);
1982     __ bind(done);
1983   } else {
1984     ShouldNotReachHere();
1985   }
1986 }
1987 
1988 
1989 void LIR_Assembler::align_call(LIR_Code code) {  }
1990 
1991 
1992 void LIR_Assembler::call(LIR_OpJavaCall* op, relocInfo::relocType rtype) {
1993   address call = __ trampoline_call(Address(op->addr(), rtype));
1994   if (call == nullptr) {
1995     bailout("trampoline stub overflow");
1996     return;
1997   }
1998   add_call_info(code_offset(), op->info());
1999   __ post_call_nop();
2000 }
2001 
2002 
2003 void LIR_Assembler::ic_call(LIR_OpJavaCall* op) {
2004   address call = __ ic_call(op->addr());
2005   if (call == nullptr) {
2006     bailout("trampoline stub overflow");
2007     return;
2008   }
2009   add_call_info(code_offset(), op->info());
2010   __ post_call_nop();
2011 }
2012 
2013 void LIR_Assembler::emit_static_call_stub() {
2014   address call_pc = __ pc();
2015   address stub = __ start_a_stub(call_stub_size());
2016   if (stub == nullptr) {
2017     bailout("static call stub overflow");
2018     return;
2019   }
2020 
2021   int start = __ offset();
2022 
2023   __ relocate(static_stub_Relocation::spec(call_pc));
2024   __ emit_static_call_stub();
2025 
2026   assert(__ offset() - start + CompiledDirectCall::to_trampoline_stub_size()
2027         <= call_stub_size(), "stub too big");
2028   __ end_a_stub();
2029 }
2030 
2031 
2032 void LIR_Assembler::throw_op(LIR_Opr exceptionPC, LIR_Opr exceptionOop, CodeEmitInfo* info) {
2033   assert(exceptionOop->as_register() == r0, "must match");
2034   assert(exceptionPC->as_register() == r3, "must match");
2035 
2036   // exception object is not added to oop map by LinearScan
2037   // (LinearScan assumes that no oops are in fixed registers)
2038   info->add_register_oop(exceptionOop);
2039   C1StubId unwind_id;
2040 
2041   // get current pc information
2042   // pc is only needed if the method has an exception handler, the unwind code does not need it.
2043   if (compilation()->debug_info_recorder()->last_pc_offset() == __ offset()) {
2044     // As no instructions have been generated yet for this LIR node it's
2045     // possible that an oop map already exists for the current offset.
2046     // In that case insert an dummy NOP here to ensure all oop map PCs
2047     // are unique. See JDK-8237483.
2048     __ nop();
2049   }
2050   int pc_for_athrow_offset = __ offset();
2051   InternalAddress pc_for_athrow(__ pc());
2052   __ adr(exceptionPC->as_register(), pc_for_athrow);
2053   add_call_info(pc_for_athrow_offset, info); // for exception handler
2054 
2055   __ verify_not_null_oop(r0);
2056   // search an exception handler (r0: exception oop, r3: throwing pc)
2057   if (compilation()->has_fpu_code()) {
2058     unwind_id = C1StubId::handle_exception_id;
2059   } else {
2060     unwind_id = C1StubId::handle_exception_nofpu_id;
2061   }
2062   __ far_call(RuntimeAddress(Runtime1::entry_for(unwind_id)));
2063 
2064   // FIXME: enough room for two byte trap   ????
2065   __ nop();
2066 }
2067 
2068 
2069 void LIR_Assembler::unwind_op(LIR_Opr exceptionOop) {
2070   assert(exceptionOop->as_register() == r0, "must match");
2071 
2072   __ b(_unwind_handler_entry);
2073 }
2074 
2075 
2076 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, LIR_Opr count, LIR_Opr dest, LIR_Opr tmp) {
2077   Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2078   Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2079 
2080   switch (left->type()) {
2081     case T_INT: {
2082       switch (code) {
2083       case lir_shl:  __ lslvw (dreg, lreg, count->as_register()); break;
2084       case lir_shr:  __ asrvw (dreg, lreg, count->as_register()); break;
2085       case lir_ushr: __ lsrvw (dreg, lreg, count->as_register()); break;
2086       default:
2087         ShouldNotReachHere();
2088         break;
2089       }
2090       break;
2091     case T_LONG:
2092     case T_ADDRESS:
2093     case T_OBJECT:
2094       switch (code) {
2095       case lir_shl:  __ lslv (dreg, lreg, count->as_register()); break;
2096       case lir_shr:  __ asrv (dreg, lreg, count->as_register()); break;
2097       case lir_ushr: __ lsrv (dreg, lreg, count->as_register()); break;
2098       default:
2099         ShouldNotReachHere();
2100         break;
2101       }
2102       break;
2103     default:
2104       ShouldNotReachHere();
2105       break;
2106     }
2107   }
2108 }
2109 
2110 
2111 void LIR_Assembler::shift_op(LIR_Code code, LIR_Opr left, jint count, LIR_Opr dest) {
2112   Register dreg = dest->is_single_cpu() ? dest->as_register() : dest->as_register_lo();
2113   Register lreg = left->is_single_cpu() ? left->as_register() : left->as_register_lo();
2114 
2115   switch (left->type()) {
2116     case T_INT: {
2117       switch (code) {
2118       case lir_shl:  __ lslw (dreg, lreg, count); break;
2119       case lir_shr:  __ asrw (dreg, lreg, count); break;
2120       case lir_ushr: __ lsrw (dreg, lreg, count); break;
2121       default:
2122         ShouldNotReachHere();
2123         break;
2124       }
2125       break;
2126     case T_LONG:
2127     case T_ADDRESS:
2128     case T_OBJECT:
2129       switch (code) {
2130       case lir_shl:  __ lsl (dreg, lreg, count); break;
2131       case lir_shr:  __ asr (dreg, lreg, count); break;
2132       case lir_ushr: __ lsr (dreg, lreg, count); break;
2133       default:
2134         ShouldNotReachHere();
2135         break;
2136       }
2137       break;
2138     default:
2139       ShouldNotReachHere();
2140       break;
2141     }
2142   }
2143 }
2144 
2145 
2146 void LIR_Assembler::store_parameter(Register r, int offset_from_rsp_in_words) {
2147   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2148   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2149   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2150   __ str (r, Address(sp, offset_from_rsp_in_bytes));
2151 }
2152 
2153 
2154 void LIR_Assembler::store_parameter(jint c,     int offset_from_rsp_in_words) {
2155   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2156   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2157   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2158   __ mov (rscratch1, c);
2159   __ str (rscratch1, Address(sp, offset_from_rsp_in_bytes));
2160 }
2161 
2162 
2163 void LIR_Assembler::store_parameter(jobject o,  int offset_from_rsp_in_words) {
2164   ShouldNotReachHere();
2165   assert(offset_from_rsp_in_words >= 0, "invalid offset from rsp");
2166   int offset_from_rsp_in_bytes = offset_from_rsp_in_words * BytesPerWord;
2167   assert(offset_from_rsp_in_bytes < frame_map()->reserved_argument_area_size(), "invalid offset");
2168   __ lea(rscratch1, __ constant_oop_address(o));
2169   __ str(rscratch1, Address(sp, offset_from_rsp_in_bytes));
2170 }
2171 
2172 
2173 // This code replaces a call to arraycopy; no exception may
2174 // be thrown in this code, they must be thrown in the System.arraycopy
2175 // activation frame; we could save some checks if this would not be the case
2176 void LIR_Assembler::emit_arraycopy(LIR_OpArrayCopy* op) {
2177   ciArrayKlass* default_type = op->expected_type();
2178   Register src = op->src()->as_register();
2179   Register dst = op->dst()->as_register();
2180   Register src_pos = op->src_pos()->as_register();
2181   Register dst_pos = op->dst_pos()->as_register();
2182   Register length  = op->length()->as_register();
2183   Register tmp = op->tmp()->as_register();
2184 
2185   CodeStub* stub = op->stub();
2186   int flags = op->flags();
2187   BasicType basic_type = default_type != nullptr ? default_type->element_type()->basic_type() : T_ILLEGAL;
2188   if (is_reference_type(basic_type)) basic_type = T_OBJECT;
2189 
2190   // if we don't know anything, just go through the generic arraycopy
2191   if (default_type == nullptr // || basic_type == T_OBJECT
2192       ) {
2193     Label done;
2194     assert(src == r1 && src_pos == r2, "mismatch in calling convention");
2195 
2196     // Save the arguments in case the generic arraycopy fails and we
2197     // have to fall back to the JNI stub
2198     __ stp(dst,     dst_pos, Address(sp, 0*BytesPerWord));
2199     __ stp(length,  src_pos, Address(sp, 2*BytesPerWord));
2200     __ str(src,              Address(sp, 4*BytesPerWord));
2201 
2202     address copyfunc_addr = StubRoutines::generic_arraycopy();
2203     assert(copyfunc_addr != nullptr, "generic arraycopy stub required");
2204 
2205     // The arguments are in java calling convention so we shift them
2206     // to C convention
2207     assert_different_registers(c_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4);
2208     __ mov(c_rarg0, j_rarg0);
2209     assert_different_registers(c_rarg1, j_rarg2, j_rarg3, j_rarg4);
2210     __ mov(c_rarg1, j_rarg1);
2211     assert_different_registers(c_rarg2, j_rarg3, j_rarg4);
2212     __ mov(c_rarg2, j_rarg2);
2213     assert_different_registers(c_rarg3, j_rarg4);
2214     __ mov(c_rarg3, j_rarg3);
2215     __ mov(c_rarg4, j_rarg4);
2216 #ifndef PRODUCT
2217     if (PrintC1Statistics) {
2218       __ incrementw(ExternalAddress((address)&Runtime1::_generic_arraycopystub_cnt));
2219     }
2220 #endif
2221     __ far_call(RuntimeAddress(copyfunc_addr));
2222 
2223     __ cbz(r0, *stub->continuation());
2224 
2225     // Reload values from the stack so they are where the stub
2226     // expects them.
2227     __ ldp(dst,     dst_pos, Address(sp, 0*BytesPerWord));
2228     __ ldp(length,  src_pos, Address(sp, 2*BytesPerWord));
2229     __ ldr(src,              Address(sp, 4*BytesPerWord));
2230 
2231     // r0 is -1^K where K == partial copied count
2232     __ eonw(rscratch1, r0, zr);
2233     // adjust length down and src/end pos up by partial copied count
2234     __ subw(length, length, rscratch1);
2235     __ addw(src_pos, src_pos, rscratch1);
2236     __ addw(dst_pos, dst_pos, rscratch1);
2237     __ b(*stub->entry());
2238 
2239     __ bind(*stub->continuation());
2240     return;
2241   }
2242 
2243   assert(default_type != nullptr && default_type->is_array_klass() && default_type->is_loaded(), "must be true at this point");
2244 
2245   int elem_size = type2aelembytes(basic_type);
2246   int scale = exact_log2(elem_size);
2247 
2248   Address src_length_addr = Address(src, arrayOopDesc::length_offset_in_bytes());
2249   Address dst_length_addr = Address(dst, arrayOopDesc::length_offset_in_bytes());
2250 
2251   // test for null
2252   if (flags & LIR_OpArrayCopy::src_null_check) {
2253     __ cbz(src, *stub->entry());
2254   }
2255   if (flags & LIR_OpArrayCopy::dst_null_check) {
2256     __ cbz(dst, *stub->entry());
2257   }
2258 
2259   // If the compiler was not able to prove that exact type of the source or the destination
2260   // of the arraycopy is an array type, check at runtime if the source or the destination is
2261   // an instance type.
2262   if (flags & LIR_OpArrayCopy::type_check) {
2263     if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::dst_objarray)) {
2264       __ load_klass(tmp, dst);
2265       __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2266       __ cmpw(rscratch1, Klass::_lh_neutral_value);
2267       __ br(Assembler::GE, *stub->entry());
2268     }
2269 
2270     if (!(flags & LIR_OpArrayCopy::LIR_OpArrayCopy::src_objarray)) {
2271       __ load_klass(tmp, src);
2272       __ ldrw(rscratch1, Address(tmp, in_bytes(Klass::layout_helper_offset())));
2273       __ cmpw(rscratch1, Klass::_lh_neutral_value);
2274       __ br(Assembler::GE, *stub->entry());
2275     }
2276   }
2277 
2278   // check if negative
2279   if (flags & LIR_OpArrayCopy::src_pos_positive_check) {
2280     __ cmpw(src_pos, 0);
2281     __ br(Assembler::LT, *stub->entry());
2282   }
2283   if (flags & LIR_OpArrayCopy::dst_pos_positive_check) {
2284     __ cmpw(dst_pos, 0);
2285     __ br(Assembler::LT, *stub->entry());
2286   }
2287 
2288   if (flags & LIR_OpArrayCopy::length_positive_check) {
2289     __ cmpw(length, 0);
2290     __ br(Assembler::LT, *stub->entry());
2291   }
2292 
2293   if (flags & LIR_OpArrayCopy::src_range_check) {
2294     __ addw(tmp, src_pos, length);
2295     __ ldrw(rscratch1, src_length_addr);
2296     __ cmpw(tmp, rscratch1);
2297     __ br(Assembler::HI, *stub->entry());
2298   }
2299   if (flags & LIR_OpArrayCopy::dst_range_check) {
2300     __ addw(tmp, dst_pos, length);
2301     __ ldrw(rscratch1, dst_length_addr);
2302     __ cmpw(tmp, rscratch1);
2303     __ br(Assembler::HI, *stub->entry());
2304   }
2305 
2306   if (flags & LIR_OpArrayCopy::type_check) {
2307     // We don't know the array types are compatible
2308     if (basic_type != T_OBJECT) {
2309       // Simple test for basic type arrays
2310       __ cmp_klasses_from_objects(src, dst, tmp, rscratch1);
2311       __ br(Assembler::NE, *stub->entry());
2312     } else {
2313       // For object arrays, if src is a sub class of dst then we can
2314       // safely do the copy.
2315       Label cont, slow;
2316 
2317 #define PUSH(r1, r2)                                    \
2318       stp(r1, r2, __ pre(sp, -2 * wordSize));
2319 
2320 #define POP(r1, r2)                                     \
2321       ldp(r1, r2, __ post(sp, 2 * wordSize));
2322 
2323       __ PUSH(src, dst);
2324 
2325       __ load_klass(src, src);
2326       __ load_klass(dst, dst);
2327 
2328       __ check_klass_subtype_fast_path(src, dst, tmp, &cont, &slow, nullptr);
2329 
2330       __ PUSH(src, dst);
2331       __ far_call(RuntimeAddress(Runtime1::entry_for(C1StubId::slow_subtype_check_id)));
2332       __ POP(src, dst);
2333 
2334       __ cbnz(src, cont);
2335 
2336       __ bind(slow);
2337       __ POP(src, dst);
2338 
2339       address copyfunc_addr = StubRoutines::checkcast_arraycopy();
2340       if (copyfunc_addr != nullptr) { // use stub if available
2341         // src is not a sub class of dst so we have to do a
2342         // per-element check.
2343 
2344         int mask = LIR_OpArrayCopy::src_objarray|LIR_OpArrayCopy::dst_objarray;
2345         if ((flags & mask) != mask) {
2346           // Check that at least both of them object arrays.
2347           assert(flags & mask, "one of the two should be known to be an object array");
2348 
2349           if (!(flags & LIR_OpArrayCopy::src_objarray)) {
2350             __ load_klass(tmp, src);
2351           } else if (!(flags & LIR_OpArrayCopy::dst_objarray)) {
2352             __ load_klass(tmp, dst);
2353           }
2354           int lh_offset = in_bytes(Klass::layout_helper_offset());
2355           Address klass_lh_addr(tmp, lh_offset);
2356           jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2357           __ ldrw(rscratch1, klass_lh_addr);
2358           __ mov(rscratch2, objArray_lh);
2359           __ eorw(rscratch1, rscratch1, rscratch2);
2360           __ cbnzw(rscratch1, *stub->entry());
2361         }
2362 
2363        // Spill because stubs can use any register they like and it's
2364        // easier to restore just those that we care about.
2365         __ stp(dst,     dst_pos, Address(sp, 0*BytesPerWord));
2366         __ stp(length,  src_pos, Address(sp, 2*BytesPerWord));
2367         __ str(src,              Address(sp, 4*BytesPerWord));
2368 
2369         __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2370         __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2371         assert_different_registers(c_rarg0, dst, dst_pos, length);
2372         __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2373         __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2374         assert_different_registers(c_rarg1, dst, length);
2375         __ uxtw(c_rarg2, length);
2376         assert_different_registers(c_rarg2, dst);
2377 
2378         __ load_klass(c_rarg4, dst);
2379         __ ldr(c_rarg4, Address(c_rarg4, ObjArrayKlass::element_klass_offset()));
2380         __ ldrw(c_rarg3, Address(c_rarg4, Klass::super_check_offset_offset()));
2381         __ far_call(RuntimeAddress(copyfunc_addr));
2382 
2383 #ifndef PRODUCT
2384         if (PrintC1Statistics) {
2385           Label failed;
2386           __ cbnz(r0, failed);
2387           __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_cnt));
2388           __ bind(failed);
2389         }
2390 #endif
2391 
2392         __ cbz(r0, *stub->continuation());
2393 
2394 #ifndef PRODUCT
2395         if (PrintC1Statistics) {
2396           __ incrementw(ExternalAddress((address)&Runtime1::_arraycopy_checkcast_attempt_cnt));
2397         }
2398 #endif
2399         assert_different_registers(dst, dst_pos, length, src_pos, src, r0, rscratch1);
2400 
2401         // Restore previously spilled arguments
2402         __ ldp(dst,     dst_pos, Address(sp, 0*BytesPerWord));
2403         __ ldp(length,  src_pos, Address(sp, 2*BytesPerWord));
2404         __ ldr(src,              Address(sp, 4*BytesPerWord));
2405 
2406         // return value is -1^K where K is partial copied count
2407         __ eonw(rscratch1, r0, zr);
2408         // adjust length down and src/end pos up by partial copied count
2409         __ subw(length, length, rscratch1);
2410         __ addw(src_pos, src_pos, rscratch1);
2411         __ addw(dst_pos, dst_pos, rscratch1);
2412       }
2413 
2414       __ b(*stub->entry());
2415 
2416       __ bind(cont);
2417       __ POP(src, dst);
2418     }
2419   }
2420 
2421 #ifdef ASSERT
2422   if (basic_type != T_OBJECT || !(flags & LIR_OpArrayCopy::type_check)) {
2423     // Sanity check the known type with the incoming class.  For the
2424     // primitive case the types must match exactly with src.klass and
2425     // dst.klass each exactly matching the default type.  For the
2426     // object array case, if no type check is needed then either the
2427     // dst type is exactly the expected type and the src type is a
2428     // subtype which we can't check or src is the same array as dst
2429     // but not necessarily exactly of type default_type.
2430     Label known_ok, halt;
2431     __ mov_metadata(tmp, default_type->constant_encoding());
2432 
2433     if (basic_type != T_OBJECT) {
2434       __ cmp_klass(dst, tmp, rscratch1);
2435       __ br(Assembler::NE, halt);
2436       __ cmp_klass(src, tmp, rscratch1);
2437       __ br(Assembler::EQ, known_ok);
2438     } else {
2439       __ cmp_klass(dst, tmp, rscratch1);
2440       __ br(Assembler::EQ, known_ok);
2441       __ cmp(src, dst);
2442       __ br(Assembler::EQ, known_ok);
2443     }
2444     __ bind(halt);
2445     __ stop("incorrect type information in arraycopy");
2446     __ bind(known_ok);
2447   }
2448 #endif
2449 
2450 #ifndef PRODUCT
2451   if (PrintC1Statistics) {
2452     __ incrementw(ExternalAddress(Runtime1::arraycopy_count_address(basic_type)));
2453   }
2454 #endif
2455 
2456   __ lea(c_rarg0, Address(src, src_pos, Address::uxtw(scale)));
2457   __ add(c_rarg0, c_rarg0, arrayOopDesc::base_offset_in_bytes(basic_type));
2458   assert_different_registers(c_rarg0, dst, dst_pos, length);
2459   __ lea(c_rarg1, Address(dst, dst_pos, Address::uxtw(scale)));
2460   __ add(c_rarg1, c_rarg1, arrayOopDesc::base_offset_in_bytes(basic_type));
2461   assert_different_registers(c_rarg1, dst, length);
2462   __ uxtw(c_rarg2, length);
2463   assert_different_registers(c_rarg2, dst);
2464 
2465   bool disjoint = (flags & LIR_OpArrayCopy::overlapping) == 0;
2466   bool aligned = (flags & LIR_OpArrayCopy::unaligned) == 0;
2467   const char *name;
2468   address entry = StubRoutines::select_arraycopy_function(basic_type, aligned, disjoint, name, false);
2469 
2470  CodeBlob *cb = CodeCache::find_blob(entry);
2471  if (cb) {
2472    __ far_call(RuntimeAddress(entry));
2473  } else {
2474    __ call_VM_leaf(entry, 3);
2475  }
2476 
2477   if (stub != nullptr) {
2478     __ bind(*stub->continuation());
2479   }
2480 }
2481 
2482 
2483 
2484 
2485 void LIR_Assembler::emit_lock(LIR_OpLock* op) {
2486   Register obj = op->obj_opr()->as_register();  // may not be an oop
2487   Register hdr = op->hdr_opr()->as_register();
2488   Register lock = op->lock_opr()->as_register();
2489   Register temp = op->scratch_opr()->as_register();
2490   if (LockingMode == LM_MONITOR) {
2491     if (op->info() != nullptr) {
2492       add_debug_info_for_null_check_here(op->info());
2493       __ null_check(obj, -1);
2494     }
2495     __ b(*op->stub()->entry());
2496   } else if (op->code() == lir_lock) {
2497     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2498     // add debug info for NullPointerException only if one is possible
2499     int null_check_offset = __ lock_object(hdr, obj, lock, temp, *op->stub()->entry());
2500     if (op->info() != nullptr) {
2501       add_debug_info_for_null_check(null_check_offset, op->info());
2502     }
2503     // done
2504   } else if (op->code() == lir_unlock) {
2505     assert(BasicLock::displaced_header_offset_in_bytes() == 0, "lock_reg must point to the displaced header");
2506     __ unlock_object(hdr, obj, lock, temp, *op->stub()->entry());
2507   } else {
2508     Unimplemented();
2509   }
2510   __ bind(*op->stub()->continuation());
2511 }
2512 
2513 void LIR_Assembler::emit_load_klass(LIR_OpLoadKlass* op) {
2514   Register obj = op->obj()->as_pointer_register();
2515   Register result = op->result_opr()->as_pointer_register();
2516 
2517   CodeEmitInfo* info = op->info();
2518   if (info != nullptr) {
2519     add_debug_info_for_null_check_here(info);
2520   }
2521 
2522   __ load_klass(result, obj);
2523 }
2524 
2525 void LIR_Assembler::emit_profile_call(LIR_OpProfileCall* op) {
2526   ciMethod* method = op->profiled_method();
2527   int bci          = op->profiled_bci();
2528   ciMethod* callee = op->profiled_callee();
2529 
2530   // Update counter for all call types
2531   ciMethodData* md = method->method_data_or_null();
2532   assert(md != nullptr, "Sanity");
2533   ciProfileData* data = md->bci_to_data(bci);
2534   assert(data != nullptr && data->is_CounterData(), "need CounterData for calls");
2535   assert(op->mdo()->is_single_cpu(),  "mdo must be allocated");
2536   Register mdo  = op->mdo()->as_register();
2537   __ mov_metadata(mdo, md->constant_encoding());
2538   Address counter_addr(mdo, md->byte_offset_of_slot(data, CounterData::count_offset()));
2539   // Perform additional virtual call profiling for invokevirtual and
2540   // invokeinterface bytecodes
2541   if (op->should_profile_receiver_type()) {
2542     assert(op->recv()->is_single_cpu(), "recv must be allocated");
2543     Register recv = op->recv()->as_register();
2544     assert_different_registers(mdo, recv);
2545     assert(data->is_VirtualCallData(), "need VirtualCallData for virtual calls");
2546     ciKlass* known_klass = op->known_holder();
2547     if (C1OptimizeVirtualCallProfiling && known_klass != nullptr) {
2548       // We know the type that will be seen at this call site; we can
2549       // statically update the MethodData* rather than needing to do
2550       // dynamic tests on the receiver type
2551 
2552       // NOTE: we should probably put a lock around this search to
2553       // avoid collisions by concurrent compilations
2554       ciVirtualCallData* vc_data = (ciVirtualCallData*) data;
2555       uint i;
2556       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2557         ciKlass* receiver = vc_data->receiver(i);
2558         if (known_klass->equals(receiver)) {
2559           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2560           __ addptr(data_addr, DataLayout::counter_increment);
2561           return;
2562         }
2563       }
2564 
2565       // Receiver type not found in profile data; select an empty slot
2566 
2567       // Note that this is less efficient than it should be because it
2568       // always does a write to the receiver part of the
2569       // VirtualCallData rather than just the first time
2570       for (i = 0; i < VirtualCallData::row_limit(); i++) {
2571         ciKlass* receiver = vc_data->receiver(i);
2572         if (receiver == nullptr) {
2573           __ mov_metadata(rscratch1, known_klass->constant_encoding());
2574           Address recv_addr =
2575             __ form_address(rscratch2, mdo,
2576                             md->byte_offset_of_slot(data, VirtualCallData::receiver_offset(i)),
2577                             LogBytesPerWord);
2578           __ str(rscratch1, recv_addr);
2579           Address data_addr(mdo, md->byte_offset_of_slot(data, VirtualCallData::receiver_count_offset(i)));
2580           __ addptr(data_addr, DataLayout::counter_increment);
2581           return;
2582         }
2583       }
2584     } else {
2585       __ load_klass(recv, recv);
2586       Label update_done;
2587       type_profile_helper(mdo, md, data, recv, &update_done);
2588       // Receiver did not match any saved receiver and there is no empty row for it.
2589       // Increment total counter to indicate polymorphic case.
2590       __ addptr(counter_addr, DataLayout::counter_increment);
2591 
2592       __ bind(update_done);
2593     }
2594   } else {
2595     // Static call
2596     __ addptr(counter_addr, DataLayout::counter_increment);
2597   }
2598 }
2599 
2600 
2601 void LIR_Assembler::emit_delay(LIR_OpDelay*) {
2602   Unimplemented();
2603 }
2604 
2605 
2606 void LIR_Assembler::monitor_address(int monitor_no, LIR_Opr dst) {
2607   __ lea(dst->as_register(), frame_map()->address_for_monitor_lock(monitor_no));
2608 }
2609 
2610 void LIR_Assembler::emit_updatecrc32(LIR_OpUpdateCRC32* op) {
2611   assert(op->crc()->is_single_cpu(),  "crc must be register");
2612   assert(op->val()->is_single_cpu(),  "byte value must be register");
2613   assert(op->result_opr()->is_single_cpu(), "result must be register");
2614   Register crc = op->crc()->as_register();
2615   Register val = op->val()->as_register();
2616   Register res = op->result_opr()->as_register();
2617 
2618   assert_different_registers(val, crc, res);
2619   uint64_t offset;
2620   __ adrp(res, ExternalAddress(StubRoutines::crc_table_addr()), offset);
2621   __ add(res, res, offset);
2622 
2623   __ mvnw(crc, crc); // ~crc
2624   __ update_byte_crc32(crc, val, res);
2625   __ mvnw(res, crc); // ~crc
2626 }
2627 
2628 void LIR_Assembler::emit_profile_type(LIR_OpProfileType* op) {
2629   COMMENT("emit_profile_type {");
2630   Register obj = op->obj()->as_register();
2631   Register tmp = op->tmp()->as_pointer_register();
2632   Address mdo_addr = as_Address(op->mdp()->as_address_ptr());
2633   ciKlass* exact_klass = op->exact_klass();
2634   intptr_t current_klass = op->current_klass();
2635   bool not_null = op->not_null();
2636   bool no_conflict = op->no_conflict();
2637 
2638   Label update, next, none;
2639 
2640   bool do_null = !not_null;
2641   bool exact_klass_set = exact_klass != nullptr && ciTypeEntries::valid_ciklass(current_klass) == exact_klass;
2642   bool do_update = !TypeEntries::is_type_unknown(current_klass) && !exact_klass_set;
2643 
2644   assert(do_null || do_update, "why are we here?");
2645   assert(!TypeEntries::was_null_seen(current_klass) || do_update, "why are we here?");
2646   assert(mdo_addr.base() != rscratch1, "wrong register");
2647 
2648   __ verify_oop(obj);
2649 
2650   if (tmp != obj) {
2651     assert_different_registers(obj, tmp, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2652     __ mov(tmp, obj);
2653   } else {
2654     assert_different_registers(obj, rscratch1, rscratch2, mdo_addr.base(), mdo_addr.index());
2655   }
2656   if (do_null) {
2657     __ cbnz(tmp, update);
2658     if (!TypeEntries::was_null_seen(current_klass)) {
2659       __ ldr(rscratch2, mdo_addr);
2660       __ orr(rscratch2, rscratch2, TypeEntries::null_seen);
2661       __ str(rscratch2, mdo_addr);
2662     }
2663     if (do_update) {
2664 #ifndef ASSERT
2665       __ b(next);
2666     }
2667 #else
2668       __ b(next);
2669     }
2670   } else {
2671     __ cbnz(tmp, update);
2672     __ stop("unexpected null obj");
2673 #endif
2674   }
2675 
2676   __ bind(update);
2677 
2678   if (do_update) {
2679 #ifdef ASSERT
2680     if (exact_klass != nullptr) {
2681       Label ok;
2682       __ load_klass(tmp, tmp);
2683       __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2684       __ eor(rscratch1, tmp, rscratch1);
2685       __ cbz(rscratch1, ok);
2686       __ stop("exact klass and actual klass differ");
2687       __ bind(ok);
2688     }
2689 #endif
2690     if (!no_conflict) {
2691       if (exact_klass == nullptr || TypeEntries::is_type_none(current_klass)) {
2692         if (exact_klass != nullptr) {
2693           __ mov_metadata(tmp, exact_klass->constant_encoding());
2694         } else {
2695           __ load_klass(tmp, tmp);
2696         }
2697 
2698         __ ldr(rscratch2, mdo_addr);
2699         __ eor(tmp, tmp, rscratch2);
2700         __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2701         // klass seen before, nothing to do. The unknown bit may have been
2702         // set already but no need to check.
2703         __ cbz(rscratch1, next);
2704 
2705         __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2706 
2707         if (TypeEntries::is_type_none(current_klass)) {
2708           __ cbz(rscratch2, none);
2709           __ cmp(rscratch2, (u1)TypeEntries::null_seen);
2710           __ br(Assembler::EQ, none);
2711           // There is a chance that the checks above
2712           // fail if another thread has just set the
2713           // profiling to this obj's klass
2714           __ dmb(Assembler::ISHLD);
2715           __ eor(tmp, tmp, rscratch2); // get back original value before XOR
2716           __ ldr(rscratch2, mdo_addr);
2717           __ eor(tmp, tmp, rscratch2);
2718           __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2719           __ cbz(rscratch1, next);
2720         }
2721       } else {
2722         assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2723                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "conflict only");
2724 
2725         __ ldr(tmp, mdo_addr);
2726         __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2727       }
2728 
2729       // different than before. Cannot keep accurate profile.
2730       __ ldr(rscratch2, mdo_addr);
2731       __ orr(rscratch2, rscratch2, TypeEntries::type_unknown);
2732       __ str(rscratch2, mdo_addr);
2733 
2734       if (TypeEntries::is_type_none(current_klass)) {
2735         __ b(next);
2736 
2737         __ bind(none);
2738         // first time here. Set profile type.
2739         __ str(tmp, mdo_addr);
2740 #ifdef ASSERT
2741         __ andr(tmp, tmp, TypeEntries::type_mask);
2742         __ verify_klass_ptr(tmp);
2743 #endif
2744       }
2745     } else {
2746       // There's a single possible klass at this profile point
2747       assert(exact_klass != nullptr, "should be");
2748       if (TypeEntries::is_type_none(current_klass)) {
2749         __ mov_metadata(tmp, exact_klass->constant_encoding());
2750         __ ldr(rscratch2, mdo_addr);
2751         __ eor(tmp, tmp, rscratch2);
2752         __ andr(rscratch1, tmp, TypeEntries::type_klass_mask);
2753         __ cbz(rscratch1, next);
2754 #ifdef ASSERT
2755         {
2756           Label ok;
2757           __ ldr(rscratch1, mdo_addr);
2758           __ cbz(rscratch1, ok);
2759           __ cmp(rscratch1, (u1)TypeEntries::null_seen);
2760           __ br(Assembler::EQ, ok);
2761           // may have been set by another thread
2762           __ dmb(Assembler::ISHLD);
2763           __ mov_metadata(rscratch1, exact_klass->constant_encoding());
2764           __ ldr(rscratch2, mdo_addr);
2765           __ eor(rscratch2, rscratch1, rscratch2);
2766           __ andr(rscratch2, rscratch2, TypeEntries::type_mask);
2767           __ cbz(rscratch2, ok);
2768 
2769           __ stop("unexpected profiling mismatch");
2770           __ bind(ok);
2771         }
2772 #endif
2773         // first time here. Set profile type.
2774         __ str(tmp, mdo_addr);
2775 #ifdef ASSERT
2776         __ andr(tmp, tmp, TypeEntries::type_mask);
2777         __ verify_klass_ptr(tmp);
2778 #endif
2779       } else {
2780         assert(ciTypeEntries::valid_ciklass(current_klass) != nullptr &&
2781                ciTypeEntries::valid_ciklass(current_klass) != exact_klass, "inconsistent");
2782 
2783         __ ldr(tmp, mdo_addr);
2784         __ tbnz(tmp, exact_log2(TypeEntries::type_unknown), next); // already unknown. Nothing to do anymore.
2785 
2786         __ orr(tmp, tmp, TypeEntries::type_unknown);
2787         __ str(tmp, mdo_addr);
2788         // FIXME: Write barrier needed here?
2789       }
2790     }
2791 
2792     __ bind(next);
2793   }
2794   COMMENT("} emit_profile_type");
2795 }
2796 
2797 
2798 void LIR_Assembler::align_backward_branch_target() {
2799 }
2800 
2801 
2802 void LIR_Assembler::negate(LIR_Opr left, LIR_Opr dest, LIR_Opr tmp) {
2803   // tmp must be unused
2804   assert(tmp->is_illegal(), "wasting a register if tmp is allocated");
2805 
2806   if (left->is_single_cpu()) {
2807     assert(dest->is_single_cpu(), "expect single result reg");
2808     __ negw(dest->as_register(), left->as_register());
2809   } else if (left->is_double_cpu()) {
2810     assert(dest->is_double_cpu(), "expect double result reg");
2811     __ neg(dest->as_register_lo(), left->as_register_lo());
2812   } else if (left->is_single_fpu()) {
2813     assert(dest->is_single_fpu(), "expect single float result reg");
2814     __ fnegs(dest->as_float_reg(), left->as_float_reg());
2815   } else {
2816     assert(left->is_double_fpu(), "expect double float operand reg");
2817     assert(dest->is_double_fpu(), "expect double float result reg");
2818     __ fnegd(dest->as_double_reg(), left->as_double_reg());
2819   }
2820 }
2821 
2822 
2823 void LIR_Assembler::leal(LIR_Opr addr, LIR_Opr dest, LIR_PatchCode patch_code, CodeEmitInfo* info) {
2824   if (patch_code != lir_patch_none) {
2825     deoptimize_trap(info);
2826     return;
2827   }
2828 
2829   __ lea(dest->as_register_lo(), as_Address(addr->as_address_ptr()));
2830 }
2831 
2832 
2833 void LIR_Assembler::rt_call(LIR_Opr result, address dest, const LIR_OprList* args, LIR_Opr tmp, CodeEmitInfo* info) {
2834   assert(!tmp->is_valid(), "don't need temporary");
2835 
2836   CodeBlob *cb = CodeCache::find_blob(dest);
2837   if (cb) {
2838     __ far_call(RuntimeAddress(dest));
2839   } else {
2840     __ mov(rscratch1, RuntimeAddress(dest));
2841     __ blr(rscratch1);
2842   }
2843 
2844   if (info != nullptr) {
2845     add_call_info_here(info);
2846   }
2847   __ post_call_nop();
2848 }
2849 
2850 void LIR_Assembler::volatile_move_op(LIR_Opr src, LIR_Opr dest, BasicType type, CodeEmitInfo* info) {
2851   if (dest->is_address() || src->is_address()) {
2852     move_op(src, dest, type, lir_patch_none, info,
2853             /*pop_fpu_stack*/false, /*wide*/false);
2854   } else {
2855     ShouldNotReachHere();
2856   }
2857 }
2858 
2859 #ifdef ASSERT
2860 // emit run-time assertion
2861 void LIR_Assembler::emit_assert(LIR_OpAssert* op) {
2862   assert(op->code() == lir_assert, "must be");
2863 
2864   if (op->in_opr1()->is_valid()) {
2865     assert(op->in_opr2()->is_valid(), "both operands must be valid");
2866     comp_op(op->condition(), op->in_opr1(), op->in_opr2(), op);
2867   } else {
2868     assert(op->in_opr2()->is_illegal(), "both operands must be illegal");
2869     assert(op->condition() == lir_cond_always, "no other conditions allowed");
2870   }
2871 
2872   Label ok;
2873   if (op->condition() != lir_cond_always) {
2874     Assembler::Condition acond = Assembler::AL;
2875     switch (op->condition()) {
2876       case lir_cond_equal:        acond = Assembler::EQ;  break;
2877       case lir_cond_notEqual:     acond = Assembler::NE;  break;
2878       case lir_cond_less:         acond = Assembler::LT;  break;
2879       case lir_cond_lessEqual:    acond = Assembler::LE;  break;
2880       case lir_cond_greaterEqual: acond = Assembler::GE;  break;
2881       case lir_cond_greater:      acond = Assembler::GT;  break;
2882       case lir_cond_belowEqual:   acond = Assembler::LS;  break;
2883       case lir_cond_aboveEqual:   acond = Assembler::HS;  break;
2884       default:                    ShouldNotReachHere();
2885     }
2886     __ br(acond, ok);
2887   }
2888   if (op->halt()) {
2889     const char* str = __ code_string(op->msg());
2890     __ stop(str);
2891   } else {
2892     breakpoint();
2893   }
2894   __ bind(ok);
2895 }
2896 #endif
2897 
2898 #ifndef PRODUCT
2899 #define COMMENT(x)   do { __ block_comment(x); } while (0)
2900 #else
2901 #define COMMENT(x)
2902 #endif
2903 
2904 void LIR_Assembler::membar() {
2905   COMMENT("membar");
2906   __ membar(MacroAssembler::AnyAny);
2907 }
2908 
2909 void LIR_Assembler::membar_acquire() {
2910   __ membar(Assembler::LoadLoad|Assembler::LoadStore);
2911 }
2912 
2913 void LIR_Assembler::membar_release() {
2914   __ membar(Assembler::LoadStore|Assembler::StoreStore);
2915 }
2916 
2917 void LIR_Assembler::membar_loadload() {
2918   __ membar(Assembler::LoadLoad);
2919 }
2920 
2921 void LIR_Assembler::membar_storestore() {
2922   __ membar(MacroAssembler::StoreStore);
2923 }
2924 
2925 void LIR_Assembler::membar_loadstore() { __ membar(MacroAssembler::LoadStore); }
2926 
2927 void LIR_Assembler::membar_storeload() { __ membar(MacroAssembler::StoreLoad); }
2928 
2929 void LIR_Assembler::on_spin_wait() {
2930   __ spin_wait();
2931 }
2932 
2933 void LIR_Assembler::get_thread(LIR_Opr result_reg) {
2934   __ mov(result_reg->as_register(), rthread);
2935 }
2936 
2937 
2938 void LIR_Assembler::peephole(LIR_List *lir) {
2939 #if 0
2940   if (tableswitch_count >= max_tableswitches)
2941     return;
2942 
2943   /*
2944     This finite-state automaton recognizes sequences of compare-and-
2945     branch instructions.  We will turn them into a tableswitch.  You
2946     could argue that C1 really shouldn't be doing this sort of
2947     optimization, but without it the code is really horrible.
2948   */
2949 
2950   enum { start_s, cmp1_s, beq_s, cmp_s } state;
2951   int first_key, last_key = -2147483648;
2952   int next_key = 0;
2953   int start_insn = -1;
2954   int last_insn = -1;
2955   Register reg = noreg;
2956   LIR_Opr reg_opr;
2957   state = start_s;
2958 
2959   LIR_OpList* inst = lir->instructions_list();
2960   for (int i = 0; i < inst->length(); i++) {
2961     LIR_Op* op = inst->at(i);
2962     switch (state) {
2963     case start_s:
2964       first_key = -1;
2965       start_insn = i;
2966       switch (op->code()) {
2967       case lir_cmp:
2968         LIR_Opr opr1 = op->as_Op2()->in_opr1();
2969         LIR_Opr opr2 = op->as_Op2()->in_opr2();
2970         if (opr1->is_cpu_register() && opr1->is_single_cpu()
2971             && opr2->is_constant()
2972             && opr2->type() == T_INT) {
2973           reg_opr = opr1;
2974           reg = opr1->as_register();
2975           first_key = opr2->as_constant_ptr()->as_jint();
2976           next_key = first_key + 1;
2977           state = cmp_s;
2978           goto next_state;
2979         }
2980         break;
2981       }
2982       break;
2983     case cmp_s:
2984       switch (op->code()) {
2985       case lir_branch:
2986         if (op->as_OpBranch()->cond() == lir_cond_equal) {
2987           state = beq_s;
2988           last_insn = i;
2989           goto next_state;
2990         }
2991       }
2992       state = start_s;
2993       break;
2994     case beq_s:
2995       switch (op->code()) {
2996       case lir_cmp: {
2997         LIR_Opr opr1 = op->as_Op2()->in_opr1();
2998         LIR_Opr opr2 = op->as_Op2()->in_opr2();
2999         if (opr1->is_cpu_register() && opr1->is_single_cpu()
3000             && opr1->as_register() == reg
3001             && opr2->is_constant()
3002             && opr2->type() == T_INT
3003             && opr2->as_constant_ptr()->as_jint() == next_key) {
3004           last_key = next_key;
3005           next_key++;
3006           state = cmp_s;
3007           goto next_state;
3008         }
3009       }
3010       }
3011       last_key = next_key;
3012       state = start_s;
3013       break;
3014     default:
3015       assert(false, "impossible state");
3016     }
3017     if (state == start_s) {
3018       if (first_key < last_key - 5L && reg != noreg) {
3019         {
3020           // printf("found run register %d starting at insn %d low value %d high value %d\n",
3021           //        reg->encoding(),
3022           //        start_insn, first_key, last_key);
3023           //   for (int i = 0; i < inst->length(); i++) {
3024           //     inst->at(i)->print();
3025           //     tty->print("\n");
3026           //   }
3027           //   tty->print("\n");
3028         }
3029 
3030         struct tableswitch *sw = &switches[tableswitch_count];
3031         sw->_insn_index = start_insn, sw->_first_key = first_key,
3032           sw->_last_key = last_key, sw->_reg = reg;
3033         inst->insert_before(last_insn + 1, new LIR_OpLabel(&sw->_after));
3034         {
3035           // Insert the new table of branches
3036           int offset = last_insn;
3037           for (int n = first_key; n < last_key; n++) {
3038             inst->insert_before
3039               (last_insn + 1,
3040                new LIR_OpBranch(lir_cond_always, T_ILLEGAL,
3041                                 inst->at(offset)->as_OpBranch()->label()));
3042             offset -= 2, i++;
3043           }
3044         }
3045         // Delete all the old compare-and-branch instructions
3046         for (int n = first_key; n < last_key; n++) {
3047           inst->remove_at(start_insn);
3048           inst->remove_at(start_insn);
3049         }
3050         // Insert the tableswitch instruction
3051         inst->insert_before(start_insn,
3052                             new LIR_Op2(lir_cmp, lir_cond_always,
3053                                         LIR_OprFact::intConst(tableswitch_count),
3054                                         reg_opr));
3055         inst->insert_before(start_insn + 1, new LIR_OpLabel(&sw->_branches));
3056         tableswitch_count++;
3057       }
3058       reg = noreg;
3059       last_key = -2147483648;
3060     }
3061   next_state:
3062     ;
3063   }
3064 #endif
3065 }
3066 
3067 void LIR_Assembler::atomic_op(LIR_Code code, LIR_Opr src, LIR_Opr data, LIR_Opr dest, LIR_Opr tmp_op) {
3068   Address addr = as_Address(src->as_address_ptr());
3069   BasicType type = src->type();
3070   bool is_oop = is_reference_type(type);
3071 
3072   void (MacroAssembler::* add)(Register prev, RegisterOrConstant incr, Register addr);
3073   void (MacroAssembler::* xchg)(Register prev, Register newv, Register addr);
3074 
3075   switch(type) {
3076   case T_INT:
3077     xchg = &MacroAssembler::atomic_xchgalw;
3078     add = &MacroAssembler::atomic_addalw;
3079     break;
3080   case T_LONG:
3081     xchg = &MacroAssembler::atomic_xchgal;
3082     add = &MacroAssembler::atomic_addal;
3083     break;
3084   case T_OBJECT:
3085   case T_ARRAY:
3086     if (UseCompressedOops) {
3087       xchg = &MacroAssembler::atomic_xchgalw;
3088       add = &MacroAssembler::atomic_addalw;
3089     } else {
3090       xchg = &MacroAssembler::atomic_xchgal;
3091       add = &MacroAssembler::atomic_addal;
3092     }
3093     break;
3094   default:
3095     ShouldNotReachHere();
3096     xchg = &MacroAssembler::atomic_xchgal;
3097     add = &MacroAssembler::atomic_addal; // unreachable
3098   }
3099 
3100   switch (code) {
3101   case lir_xadd:
3102     {
3103       RegisterOrConstant inc;
3104       Register tmp = as_reg(tmp_op);
3105       Register dst = as_reg(dest);
3106       if (data->is_constant()) {
3107         inc = RegisterOrConstant(as_long(data));
3108         assert_different_registers(dst, addr.base(), tmp,
3109                                    rscratch1, rscratch2);
3110       } else {
3111         inc = RegisterOrConstant(as_reg(data));
3112         assert_different_registers(inc.as_register(), dst, addr.base(), tmp,
3113                                    rscratch1, rscratch2);
3114       }
3115       __ lea(tmp, addr);
3116       (_masm->*add)(dst, inc, tmp);
3117       break;
3118     }
3119   case lir_xchg:
3120     {
3121       Register tmp = tmp_op->as_register();
3122       Register obj = as_reg(data);
3123       Register dst = as_reg(dest);
3124       if (is_oop && UseCompressedOops) {
3125         __ encode_heap_oop(rscratch2, obj);
3126         obj = rscratch2;
3127       }
3128       assert_different_registers(obj, addr.base(), tmp, rscratch1);
3129       assert_different_registers(dst, addr.base(), tmp, rscratch1);
3130       __ lea(tmp, addr);
3131       (_masm->*xchg)(dst, obj, tmp);
3132       if (is_oop && UseCompressedOops) {
3133         __ decode_heap_oop(dst);
3134       }
3135     }
3136     break;
3137   default:
3138     ShouldNotReachHere();
3139   }
3140   __ membar(__ AnyAny);
3141 }
3142 
3143 #undef __