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