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