1 /*
   2  * Copyright (c) 2018, 2022, Red Hat, Inc. All rights reserved.
   3  * Copyright Amazon.com Inc. or its affiliates. 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 "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  27 #include "gc/shenandoah/mode/shenandoahMode.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  30 #include "gc/shenandoah/shenandoahForwarding.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  32 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  33 #include "gc/shenandoah/shenandoahRuntime.hpp"
  34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  35 #include "interpreter/interp_masm.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "runtime/javaThread.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #ifdef COMPILER1
  40 #include "c1/c1_LIRAssembler.hpp"
  41 #include "c1/c1_MacroAssembler.hpp"
  42 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  43 #endif
  44 #ifdef COMPILER2
  45 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  46 #endif
  47 
  48 #define __ masm->
  49 
  50 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  51                                                        Register src, Register dst, Register count, RegSet saved_regs) {
  52   if (is_oop) {
  53     bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
  54     if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
  55 
  56       Label done;
  57 
  58       // Avoid calling runtime if count == 0
  59       __ cbz(count, done);
  60 
  61       // Is GC active?
  62       Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
  63       __ ldrb(rscratch1, gc_state);
  64       if (ShenandoahSATBBarrier && dest_uninitialized) {
  65         __ tbz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, done);
  66       } else {
  67         __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
  68         __ tst(rscratch1, rscratch2);
  69         __ br(Assembler::EQ, done);
  70       }
  71 
  72       __ push(saved_regs, sp);
  73       if (UseCompressedOops) {
  74         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop), src, dst, count);
  75       } else {
  76         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop), src, dst, count);
  77       }
  78       __ pop(saved_regs, sp);
  79       __ bind(done);
  80     }
  81   }
  82 }
  83 
  84 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  85                                                        Register start, Register count, Register tmp) {
  86   if (ShenandoahCardBarrier && is_oop) {
  87     gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp);
  88   }
  89 }
  90 
  91 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
  92                                                                  Register obj,
  93                                                                  Register pre_val,
  94                                                                  Register thread,
  95                                                                  Register tmp,
  96                                                                  bool tosca_live,
  97                                                                  bool expand_call) {
  98   if (ShenandoahSATBBarrier) {
  99     satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, rscratch1, tosca_live, expand_call);
 100   }
 101 }
 102 
 103 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
 104                                                            Register obj,
 105                                                            Register pre_val,
 106                                                            Register thread,
 107                                                            Register tmp1,
 108                                                            Register tmp2,
 109                                                            bool tosca_live,
 110                                                            bool expand_call) {
 111   // If expand_call is true then we expand the call_VM_leaf macro
 112   // directly to skip generating the check by
 113   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 114 
 115   assert(thread == rthread, "must be");
 116 
 117   Label done;
 118   Label runtime;
 119 
 120   assert_different_registers(obj, pre_val, tmp1, tmp2);
 121   assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register");
 122 
 123   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 124   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 125 
 126   // Is marking active?
 127   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 128   __ ldrb(tmp1, gc_state);
 129   __ tbz(tmp1, ShenandoahHeap::MARKING_BITPOS, done);
 130 
 131   // Do we need to load the previous value?
 132   if (obj != noreg) {
 133     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
 134   }
 135 
 136   // Is the previous value null?
 137   __ cbz(pre_val, done);
 138 
 139   // Can we store original value in the thread's buffer?
 140   // Is index == 0?
 141   // (The index field is typed as size_t.)
 142 
 143   __ ldr(tmp1, index);                      // tmp := *index_adr
 144   __ cbz(tmp1, runtime);                    // tmp == 0?
 145                                         // If yes, goto runtime
 146 
 147   __ sub(tmp1, tmp1, wordSize);             // tmp := tmp - wordSize
 148   __ str(tmp1, index);                      // *index_adr := tmp
 149   __ ldr(tmp2, buffer);
 150   __ add(tmp1, tmp1, tmp2);                 // tmp := tmp + *buffer_adr
 151 
 152   // Record the previous value
 153   __ str(pre_val, Address(tmp1, 0));
 154   __ b(done);
 155 
 156   __ bind(runtime);
 157   // save the live input values
 158   RegSet saved = RegSet::of(pre_val);
 159   if (tosca_live) saved += RegSet::of(r0);
 160   if (obj != noreg) saved += RegSet::of(obj);
 161 
 162   __ push(saved, sp);
 163 
 164   // Calling the runtime using the regular call_VM_leaf mechanism generates
 165   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 166   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == nullptr.
 167   //
 168   // If we care generating the pre-barrier without a frame (e.g. in the
 169   // intrinsified Reference.get() routine) then rfp might be pointing to
 170   // the caller frame and so this check will most likely fail at runtime.
 171   //
 172   // Expanding the call directly bypasses the generation of the check.
 173   // So when we do not have have a full interpreter frame on the stack
 174   // expand_call should be passed true.
 175 
 176   if (expand_call) {
 177     assert(pre_val != c_rarg1, "smashed arg");
 178     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
 179   } else {
 180     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
 181   }
 182 
 183   __ pop(saved, sp);
 184 
 185   __ bind(done);
 186 }
 187 
 188 void ShenandoahBarrierSetAssembler::resolve_forward_pointer(MacroAssembler* masm, Register dst, Register tmp) {
 189   assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
 190   Label is_null;
 191   __ cbz(dst, is_null);
 192   resolve_forward_pointer_not_null(masm, dst, tmp);
 193   __ bind(is_null);
 194 }
 195 
 196 // IMPORTANT: This must preserve all registers, even rscratch1 and rscratch2, except those explicitly
 197 // passed in.
 198 void ShenandoahBarrierSetAssembler::resolve_forward_pointer_not_null(MacroAssembler* masm, Register dst, Register tmp) {
 199   assert(ShenandoahLoadRefBarrier || ShenandoahCASBarrier, "Should be enabled");
 200   // The below loads the mark word, checks if the lowest two bits are
 201   // set, and if so, clear the lowest two bits and copy the result
 202   // to dst. Otherwise it leaves dst alone.
 203   // Implementing this is surprisingly awkward. I do it here by:
 204   // - Inverting the mark word
 205   // - Test lowest two bits == 0
 206   // - If so, set the lowest two bits
 207   // - Invert the result back, and copy to dst
 208 
 209   bool borrow_reg = (tmp == noreg);
 210   if (borrow_reg) {
 211     // No free registers available. Make one useful.
 212     tmp = rscratch1;
 213     if (tmp == dst) {
 214       tmp = rscratch2;
 215     }
 216     __ push(RegSet::of(tmp), sp);
 217   }
 218 
 219   assert_different_registers(tmp, dst);
 220 
 221   Label done;
 222   __ ldr(tmp, Address(dst, oopDesc::mark_offset_in_bytes()));
 223   __ eon(tmp, tmp, zr);
 224   __ ands(zr, tmp, markWord::lock_mask_in_place);
 225   __ br(Assembler::NE, done);
 226   __ orr(tmp, tmp, markWord::marked_value);
 227   __ eon(dst, tmp, zr);
 228   __ bind(done);
 229 
 230   if (borrow_reg) {
 231     __ pop(RegSet::of(tmp), sp);
 232   }
 233 }
 234 
 235 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm, Register dst, Address load_addr, DecoratorSet decorators) {
 236   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 237   assert(dst != rscratch2, "need rscratch2");
 238   assert_different_registers(load_addr.base(), load_addr.index(), rscratch1, rscratch2);
 239 
 240   bool is_strong  = ShenandoahBarrierSet::is_strong_access(decorators);
 241   bool is_weak    = ShenandoahBarrierSet::is_weak_access(decorators);
 242   bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
 243   bool is_native  = ShenandoahBarrierSet::is_native_access(decorators);
 244   bool is_narrow  = UseCompressedOops && !is_native;
 245 
 246   Label heap_stable, not_cset;
 247   __ enter(/*strip_ret_addr*/true);
 248   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 249   __ ldrb(rscratch2, gc_state);
 250 
 251   // Check for heap stability
 252   if (is_strong) {
 253     __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, heap_stable);
 254   } else {
 255     Label lrb;
 256     __ tbnz(rscratch2, ShenandoahHeap::WEAK_ROOTS_BITPOS, lrb);
 257     __ tbz(rscratch2, ShenandoahHeap::HAS_FORWARDED_BITPOS, heap_stable);
 258     __ bind(lrb);
 259   }
 260 
 261   // use r1 for load address
 262   Register result_dst = dst;
 263   if (dst == r1) {
 264     __ mov(rscratch1, dst);
 265     dst = rscratch1;
 266   }
 267 
 268   // Save r0 and r1, unless it is an output register
 269   RegSet to_save = RegSet::of(r0, r1) - result_dst;
 270   __ push(to_save, sp);
 271   __ lea(r1, load_addr);
 272   __ mov(r0, dst);
 273 
 274   // Test for in-cset
 275   if (is_strong) {
 276     __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
 277     __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 278     __ ldrb(rscratch2, Address(rscratch2, rscratch1));
 279     __ tbz(rscratch2, 0, not_cset);
 280   }
 281 
 282   __ push_call_clobbered_registers();
 283   if (is_strong) {
 284     if (is_narrow) {
 285       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
 286     } else {
 287       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
 288     }
 289   } else if (is_weak) {
 290     if (is_narrow) {
 291       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
 292     } else {
 293       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
 294     }
 295   } else {
 296     assert(is_phantom, "only remaining strength");
 297     assert(!is_narrow, "phantom access cannot be narrow");
 298     // AOT saved adapters need relocation for this call.
 299     __ lea(lr, RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom)));
 300   }
 301   __ blr(lr);
 302   __ mov(rscratch1, r0);
 303   __ pop_call_clobbered_registers();
 304   __ mov(r0, rscratch1);
 305 
 306   __ bind(not_cset);
 307 
 308   __ mov(result_dst, r0);
 309   __ pop(to_save, sp);
 310 
 311   __ bind(heap_stable);
 312   __ leave();
 313 }
 314 
 315 //
 316 // Arguments:
 317 //
 318 // Inputs:
 319 //   src:        oop location to load from, might be clobbered
 320 //
 321 // Output:
 322 //   dst:        oop loaded from src location
 323 //
 324 // Kill:
 325 //   rscratch1 (scratch reg)
 326 //
 327 // Alias:
 328 //   dst: rscratch1 (might use rscratch1 as temporary output register to avoid clobbering src)
 329 //
 330 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 331                                             Register dst, Address src, Register tmp1, Register tmp2) {
 332   // 1: non-reference load, no additional barrier is needed
 333   if (!is_reference_type(type)) {
 334     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
 335     return;
 336   }
 337 
 338   // 2: load a reference from src location and apply LRB if needed
 339   if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
 340     Register result_dst = dst;
 341 
 342     // Preserve src location for LRB
 343     if (dst == src.base() || dst == src.index()) {
 344       dst = rscratch1;
 345     }
 346     assert_different_registers(dst, src.base(), src.index());
 347 
 348     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
 349 
 350     load_reference_barrier(masm, dst, src, decorators);
 351 
 352     if (dst != result_dst) {
 353       __ mov(result_dst, dst);
 354       dst = result_dst;
 355     }
 356   } else {
 357     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
 358   }
 359 
 360   // 3: apply keep-alive barrier if needed
 361   if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
 362     __ enter(/*strip_ret_addr*/true);
 363     __ push_call_clobbered_registers();
 364     satb_write_barrier_pre(masm /* masm */,
 365                            noreg /* obj */,
 366                            dst /* pre_val */,
 367                            rthread /* thread */,
 368                            tmp1 /* tmp1 */,
 369                            tmp2 /* tmp2 */,
 370                            true /* tosca_live */,
 371                            true /* expand_call */);
 372     __ pop_call_clobbered_registers();
 373     __ leave();
 374   }
 375 }
 376 
 377 void ShenandoahBarrierSetAssembler::store_check(MacroAssembler* masm, Register obj) {
 378   assert(ShenandoahCardBarrier, "Should have been checked by caller");
 379 
 380   __ lsr(obj, obj, CardTable::card_shift());
 381 
 382   assert(CardTable::dirty_card_val() == 0, "must be");
 383 
 384   Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
 385   __ ldr(rscratch1, curr_ct_holder_addr);
 386 
 387   if (UseCondCardMark) {
 388     Label L_already_dirty;
 389     __ ldrb(rscratch2, Address(obj, rscratch1));
 390     __ cbz(rscratch2, L_already_dirty);
 391     __ strb(zr, Address(obj, rscratch1));
 392     __ bind(L_already_dirty);
 393   } else {
 394     __ strb(zr, Address(obj, rscratch1));
 395   }
 396 }
 397 
 398 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 399                                              Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
 400   bool on_oop = is_reference_type(type);
 401   if (!on_oop) {
 402     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
 403     return;
 404   }
 405 
 406   // flatten object address if needed
 407   if (dst.index() == noreg && dst.offset() == 0) {
 408     if (dst.base() != tmp3) {
 409       __ mov(tmp3, dst.base());
 410     }
 411   } else {
 412     __ lea(tmp3, dst);
 413   }
 414 
 415   shenandoah_write_barrier_pre(masm,
 416                                tmp3 /* obj */,
 417                                tmp2 /* pre_val */,
 418                                rthread /* thread */,
 419                                tmp1  /* tmp */,
 420                                val != noreg /* tosca_live */,
 421                                false /* expand_call */);
 422 
 423   BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
 424 
 425   bool in_heap = (decorators & IN_HEAP) != 0;
 426   bool needs_post_barrier = (val != noreg) && in_heap && ShenandoahCardBarrier;
 427   if (needs_post_barrier) {
 428     store_check(masm, tmp3);
 429   }
 430 }
 431 
 432 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
 433                                                                   Register obj, Register tmp, Label& slowpath) {
 434   Label done;
 435   // Resolve jobject
 436   BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
 437 
 438   // Check for null.
 439   __ cbz(obj, done);
 440 
 441   assert(obj != rscratch2, "need rscratch2");
 442   Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
 443   __ lea(rscratch2, gc_state);
 444   __ ldrb(rscratch2, Address(rscratch2));
 445 
 446   // Check for heap in evacuation phase
 447   __ tbnz(rscratch2, ShenandoahHeap::EVACUATION_BITPOS, slowpath);
 448 
 449   __ bind(done);
 450 }
 451 
 452 // Special Shenandoah CAS implementation that handles false negatives due
 453 // to concurrent evacuation.  The service is more complex than a
 454 // traditional CAS operation because the CAS operation is intended to
 455 // succeed if the reference at addr exactly matches expected or if the
 456 // reference at addr holds a pointer to a from-space object that has
 457 // been relocated to the location named by expected.  There are two
 458 // races that must be addressed:
 459 //  a) A parallel thread may mutate the contents of addr so that it points
 460 //     to a different object.  In this case, the CAS operation should fail.
 461 //  b) A parallel thread may heal the contents of addr, replacing a
 462 //     from-space pointer held in addr with the to-space pointer
 463 //     representing the new location of the object.
 464 // Upon entry to cmpxchg_oop, it is assured that new_val equals null
 465 // or it refers to an object that is not being evacuated out of
 466 // from-space, or it refers to the to-space version of an object that
 467 // is being evacuated out of from-space.
 468 //
 469 // By default the value held in the result register following execution
 470 // of the generated code sequence is 0 to indicate failure of CAS,
 471 // non-zero to indicate success. If is_cae, the result is the value most
 472 // recently fetched from addr rather than a boolean success indicator.
 473 //
 474 // Clobbers rscratch1, rscratch2
 475 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm,
 476                                                 Register addr,
 477                                                 Register expected,
 478                                                 Register new_val,
 479                                                 bool acquire, bool release,
 480                                                 bool is_cae,
 481                                                 Register result) {
 482   Register tmp1 = rscratch1;
 483   Register tmp2 = rscratch2;
 484   bool is_narrow = UseCompressedOops;
 485   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 486 
 487   assert_different_registers(addr, expected, tmp1, tmp2);
 488   assert_different_registers(addr, new_val,  tmp1, tmp2);
 489 
 490   Label step4, done;
 491 
 492   // There are two ways to reach this label.  Initial entry into the
 493   // cmpxchg_oop code expansion starts at step1 (which is equivalent
 494   // to label step4).  Additionally, in the rare case that four steps
 495   // are required to perform the requested operation, the fourth step
 496   // is the same as the first.  On a second pass through step 1,
 497   // control may flow through step 2 on its way to failure.  It will
 498   // not flow from step 2 to step 3 since we are assured that the
 499   // memory at addr no longer holds a from-space pointer.
 500   //
 501   // The comments that immediately follow the step4 label apply only
 502   // to the case in which control reaches this label by branch from
 503   // step 3.
 504 
 505   __ bind (step4);
 506 
 507   // Step 4. CAS has failed because the value most recently fetched
 508   // from addr is no longer the from-space pointer held in tmp2.  If a
 509   // different thread replaced the in-memory value with its equivalent
 510   // to-space pointer, then CAS may still be able to succeed.  The
 511   // value held in the expected register has not changed.
 512   //
 513   // It is extremely rare we reach this point.  For this reason, the
 514   // implementation opts for smaller rather than potentially faster
 515   // code.  Ultimately, smaller code for this rare case most likely
 516   // delivers higher overall throughput by enabling improved icache
 517   // performance.
 518 
 519   // Step 1. Fast-path.
 520   //
 521   // Try to CAS with given arguments.  If successful, then we are done.
 522   //
 523   // No label required for step 1.
 524 
 525   __ cmpxchg(addr, expected, new_val, size, acquire, release, false, tmp2);
 526   // EQ flag set iff success.  tmp2 holds value fetched.
 527 
 528   // If expected equals null but tmp2 does not equal null, the
 529   // following branches to done to report failure of CAS.  If both
 530   // expected and tmp2 equal null, the following branches to done to
 531   // report success of CAS.  There's no need for a special test of
 532   // expected equal to null.
 533 
 534   __ br(Assembler::EQ, done);
 535   // if CAS failed, fall through to step 2
 536 
 537   // Step 2. CAS has failed because the value held at addr does not
 538   // match expected.  This may be a false negative because the value fetched
 539   // from addr (now held in tmp2) may be a from-space pointer to the
 540   // original copy of same object referenced by to-space pointer expected.
 541   //
 542   // To resolve this, it suffices to find the forward pointer associated
 543   // with fetched value.  If this matches expected, retry CAS with new
 544   // parameters.  If this mismatches, then we have a legitimate
 545   // failure, and we're done.
 546   //
 547   // No need for step2 label.
 548 
 549   // overwrite tmp1 with from-space pointer fetched from memory
 550   __ mov(tmp1, tmp2);
 551 
 552   if (is_narrow) {
 553     // Decode tmp1 in order to resolve its forward pointer
 554     __ decode_heap_oop(tmp1, tmp1);
 555   }
 556   resolve_forward_pointer(masm, tmp1);
 557   // Encode tmp1 to compare against expected.
 558   __ encode_heap_oop(tmp1, tmp1);
 559 
 560   // Does forwarded value of fetched from-space pointer match original
 561   // value of expected?  If tmp1 holds null, this comparison will fail
 562   // because we know from step1 that expected is not null.  There is
 563   // no need for a separate test for tmp1 (the value originally held
 564   // in memory) equal to null.
 565   __ cmp(tmp1, expected);
 566 
 567   // If not, then the failure was legitimate and we're done.
 568   // Branching to done with NE condition denotes failure.
 569   __ br(Assembler::NE, done);
 570 
 571   // Fall through to step 3.  No need for step3 label.
 572 
 573   // Step 3.  We've confirmed that the value originally held in memory
 574   // (now held in tmp2) pointed to from-space version of original
 575   // expected value.  Try the CAS again with the from-space expected
 576   // value.  If it now succeeds, we're good.
 577   //
 578   // Note: tmp2 holds encoded from-space pointer that matches to-space
 579   // object residing at expected.  tmp2 is the new "expected".
 580 
 581   // Note that macro implementation of __cmpxchg cannot use same register
 582   // tmp2 for result and expected since it overwrites result before it
 583   // compares result with expected.
 584   __ cmpxchg(addr, tmp2, new_val, size, acquire, release, false, noreg);
 585   // EQ flag set iff success.  tmp2 holds value fetched, tmp1 (rscratch1) clobbered.
 586 
 587   // If fetched value did not equal the new expected, this could
 588   // still be a false negative because some other thread may have
 589   // newly overwritten the memory value with its to-space equivalent.
 590   __ br(Assembler::NE, step4);
 591 
 592   if (is_cae) {
 593     // We're falling through to done to indicate success.  Success
 594     // with is_cae is denoted by returning the value of expected as
 595     // result.
 596     __ mov(tmp2, expected);
 597   }
 598 
 599   __ bind(done);
 600   // At entry to done, the Z (EQ) flag is on iff if the CAS
 601   // operation was successful.  Additionally, if is_cae, tmp2 holds
 602   // the value most recently fetched from addr. In this case, success
 603   // is denoted by tmp2 matching expected.
 604 
 605   if (is_cae) {
 606     __ mov(result, tmp2);
 607   } else {
 608     __ cset(result, Assembler::EQ);
 609   }
 610 }
 611 
 612 #ifdef COMPILER2
 613 void ShenandoahBarrierSetAssembler::load_ref_barrier_c2(const MachNode* node, MacroAssembler* masm, Register obj, Register addr, Register tmp, bool narrow, bool maybe_null) {
 614   if (!ShenandoahLoadRefBarrierStubC2::needs_barrier(node)) {
 615     return;
 616   }
 617   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
 618   ShenandoahLoadRefBarrierStubC2* const stub = ShenandoahLoadRefBarrierStubC2::create(node, obj, addr, tmp, noreg, noreg, narrow);
 619 
 620   // Don't preserve the obj across the runtime call, we override it from the return value anyway.
 621   stub->dont_preserve(obj);
 622 
 623   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 624   __ ldrb(rscratch1, gc_state);
 625 
 626   // Check if GC marking is in progress or we are handling a weak reference, otherwise we don't have to do anything.
 627   bool is_strong = (node->barrier_data() & ShenandoahBarrierStrong) != 0;
 628   if (is_strong) {
 629     __ tbnz(rscratch1, ShenandoahHeap::HAS_FORWARDED_BITPOS, *stub->entry());
 630   } else {
 631     static_assert(ShenandoahHeap::HAS_FORWARDED_BITPOS == 0, "Relied on in LRB check below.");
 632     __ orr(tmp, rscratch1, rscratch1, Assembler::LSR, ShenandoahHeap::WEAK_ROOTS_BITPOS);
 633     __ tbnz(tmp, ShenandoahHeap::HAS_FORWARDED_BITPOS, *stub->entry());
 634   }
 635 
 636   __ bind(*stub->continuation());
 637 }
 638 
 639 void ShenandoahBarrierSetAssembler::load_ref_barrier_c3(const MachNode* node, MacroAssembler* masm, Register obj, Register addr, Register tmp, bool narrow, bool maybe_null, Register gc_state) {
 640   if (!ShenandoahLoadRefBarrierStubC2::needs_barrier(node)) {
 641     return;
 642   }
 643   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
 644   ShenandoahLoadRefBarrierStubC2* const stub = ShenandoahLoadRefBarrierStubC2::create(node, obj, addr, tmp, noreg, noreg, narrow);
 645 
 646   // Don't preserve the obj across the runtime call, we override it from the return value anyway.
 647   stub->dont_preserve(obj);
 648 
 649   // Check if GC marking is in progress or we are handling a weak reference, otherwise we don't have to do anything.
 650   bool is_strong = (node->barrier_data() & ShenandoahBarrierStrong) != 0;
 651   if (is_strong) {
 652     __ tbnz(gc_state, ShenandoahHeap::HAS_FORWARDED_BITPOS, *stub->entry());
 653   } else {
 654     static_assert(ShenandoahHeap::HAS_FORWARDED_BITPOS == 0, "Relied on in LRB check below.");
 655     __ orr(tmp, gc_state, gc_state, Assembler::LSR, ShenandoahHeap::WEAK_ROOTS_BITPOS);
 656     __ tbnz(tmp, ShenandoahHeap::HAS_FORWARDED_BITPOS, *stub->entry());
 657   }
 658 
 659   __ bind(*stub->continuation());
 660 }
 661 
 662 void ShenandoahBarrierSetAssembler::satb_barrier_c3(const MachNode* node, MacroAssembler* masm, Register addr, Register pre_val, Register gc_state) {
 663   assert_different_registers(addr, pre_val);
 664   if (!ShenandoahSATBBarrierStubC2::needs_barrier(node)) {
 665     return;
 666   }
 667 
 668   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
 669   ShenandoahSATBBarrierStubC2* const stub = ShenandoahSATBBarrierStubC2::create(node, addr, pre_val);
 670 
 671   // Check if GC marking is in progress, otherwise we don't have to do anything.
 672   __ tstw(gc_state, ShenandoahHeap::MARKING);
 673   __ br(Assembler::NE, *stub->entry());
 674   __ bind(*stub->continuation());
 675 }
 676 
 677 void ShenandoahBarrierSetAssembler::satb_barrier_c2(const MachNode* node, MacroAssembler* masm, Register addr, Register pre_val) {
 678   assert_different_registers(addr, pre_val);
 679   if (!ShenandoahSATBBarrierStubC2::needs_barrier(node)) {
 680     return;
 681   }
 682   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
 683   ShenandoahSATBBarrierStubC2* const stub = ShenandoahSATBBarrierStubC2::create(node, addr, pre_val);
 684 
 685   // Check if GC marking is in progress, otherwise we don't have to do anything.
 686   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 687   __ ldrb(rscratch1, gc_state);
 688   __ tstw(rscratch1, ShenandoahHeap::MARKING);
 689   __ br(Assembler::NE, *stub->entry());
 690   __ bind(*stub->continuation());
 691 }
 692 
 693 void ShenandoahBarrierSetAssembler::card_barrier_c2(const MachNode* node, MacroAssembler* masm, Register addr, Register tmp) {
 694   if (!ShenandoahCardBarrier ||
 695       (node->barrier_data() & (ShenandoahBarrierCardMark | ShenandoahBarrierCardMarkNotNull)) == 0) {
 696     return;
 697   }
 698 
 699   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
 700   __ lsr(tmp, addr, CardTable::card_shift());
 701 
 702   assert(CardTable::dirty_card_val() == 0, "must be");
 703 
 704   Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
 705   __ ldr(rscratch1, curr_ct_holder_addr);
 706 
 707   if (UseCondCardMark) {
 708     Label L_already_dirty;
 709     __ ldrb(rscratch2, Address(tmp, rscratch1));
 710     __ cbz(rscratch2, L_already_dirty);
 711     __ strb(zr, Address(tmp, rscratch1));
 712     __ bind(L_already_dirty);
 713   } else {
 714     __ strb(zr, Address(tmp, rscratch1));
 715   }
 716 }
 717 
 718 void ShenandoahBarrierSetAssembler::cmpxchg_oop_c2(const MachNode* node,
 719                                                    MacroAssembler* masm,
 720                                                    Register addr,
 721                                                    Register expected,
 722                                                    Register new_val,
 723                                                    Register result,
 724                                                    bool acquire, bool release, bool weak,
 725                                                    bool is_cae) {
 726   Register tmp = rscratch2;
 727   Assembler::operand_size size = UseCompressedOops ? Assembler::word : Assembler::xword;
 728 
 729   assert_different_registers(addr, expected, result, tmp);
 730   assert_different_registers(addr, new_val,  result, tmp);
 731 
 732   ShenandoahCASBarrierSlowStubC2* const slow_stub = ShenandoahCASBarrierSlowStubC2::create(node, addr, expected, new_val, result, tmp, is_cae, acquire, release, weak);
 733   ShenandoahCASBarrierMidStubC2* const mid_stub = ShenandoahCASBarrierMidStubC2::create(node, slow_stub, expected, result, tmp, is_cae);
 734 
 735   // Step 1. Fast-path.
 736   //
 737   // Try to CAS with given arguments.  If successful, then we are done.
 738   __ cmpxchg(addr, expected, new_val, size, acquire, release, weak, result);
 739   // EQ flag set iff success. result holds value fetched.
 740 
 741   __ br(Assembler::NE, *mid_stub->entry());
 742 
 743   // Slow-stub re-enters with condition flags according to CAS, we may need to
 744   // set result accordingly.
 745   __ bind(*slow_stub->continuation());
 746   if (!is_cae) {
 747     __ cset(result, Assembler::EQ);
 748   }
 749 
 750   // Mid-stub re-enters with result set correctly.
 751   __ bind(*mid_stub->continuation());
 752 }
 753 
 754 #undef __
 755 #define __ masm.
 756 
 757 void ShenandoahLoadRefBarrierStubC2::emit_code(MacroAssembler& masm) {
 758   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
 759   __ bind(*entry());
 760   Register obj = _obj;
 761   if (_narrow) {
 762     __ decode_heap_oop(_tmp1, _obj);
 763     obj = _tmp1;
 764   }
 765   // Weak/phantom loads always need to go to runtime.
 766   if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
 767     // Check for object in cset.
 768     __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
 769     __ lsr(rscratch1, obj, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 770     __ ldrb(rscratch2, Address(rscratch2, rscratch1));
 771     __ cbz(rscratch2, *continuation());
 772   }
 773   {
 774     SaveLiveRegisters save_registers(&masm, this);
 775     if (c_rarg0 != obj) {
 776       if (c_rarg0 == _addr) {
 777         __ mov(rscratch1, _addr);
 778         _addr = rscratch1;
 779       }
 780       __ mov(c_rarg0, obj);
 781     }
 782     __ mov(c_rarg1, _addr);
 783 
 784     if (_narrow) {
 785       if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
 786         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
 787       } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
 788         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
 789       } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
 790         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow));
 791       }
 792     } else {
 793       if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
 794         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
 795       } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
 796         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
 797       } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
 798         __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
 799       }
 800     }
 801     __ blr(rscratch1);
 802     __ mov(_obj, r0);
 803   }
 804   if (_narrow) {
 805     __ encode_heap_oop(_obj);
 806   }
 807   __ b(*continuation());
 808 }
 809 
 810 void ShenandoahSATBBarrierStubC2::emit_code(MacroAssembler& masm) {
 811   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
 812   __ bind(*entry());
 813   // Do we need to load the previous value?
 814   if (_addr != noreg) {
 815     __ load_heap_oop(_preval, Address(_addr, 0), noreg, noreg, AS_RAW);
 816   }
 817   // Is the previous value null?
 818   // __ cbz(_preval, *continuation());
 819 
 820   Address index(rthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 821   Address buffer(rthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 822   Label runtime;
 823   __ ldr(rscratch1, index);
 824   // If buffer is full, call into runtime.
 825   __ cbz(rscratch1, runtime);
 826 
 827   // The buffer is not full, store value into it.
 828   __ sub(rscratch1, rscratch1, wordSize);
 829   __ str(rscratch1, index);
 830   __ ldr(rscratch2, buffer);
 831   __ str(_preval, Address(rscratch2, rscratch1));
 832   __ b(*continuation());
 833 
 834   // Runtime call
 835   __ bind(runtime);
 836   {
 837     SaveLiveRegisters save_registers(&masm, this);
 838     if (c_rarg0 != _preval) {
 839       __ mov(c_rarg0, _preval);
 840     }
 841     __ mov(rscratch1, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre_c2));
 842     __ blr(rscratch1);
 843   }
 844   __ b(*continuation());
 845 }
 846 
 847 void ShenandoahCASBarrierMidStubC2::emit_code(MacroAssembler& masm) {
 848   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
 849   __ bind(*entry());
 850 
 851   // Check if CAS result is null. If it is, then we must have a legitimate failure.
 852   // This makes loading the fwdptr in the slow-path simpler.
 853   __ tst(_result, _result);
 854   // In case of !CAE, this has the correct value for legitimate failure (0/false)
 855   // in result register.
 856   __ br(Assembler::EQ, *continuation());
 857 
 858   // Check if GC is in progress, otherwise we must have a legitimate failure.
 859   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 860   __ ldrb(_tmp, gc_state);
 861   __ tstw(_tmp, ShenandoahHeap::HAS_FORWARDED);
 862   __ br(Assembler::NE, *_slow_stub->entry());
 863 
 864   if (!_cae) {
 865     __ mov(_result, 0); // result = false
 866   }
 867   __ b(*continuation());
 868 }
 869 
 870 void ShenandoahCASBarrierSlowStubC2::emit_code(MacroAssembler& masm) {
 871   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
 872   __ bind(*entry());
 873   Assembler::operand_size size = UseCompressedOops ? Assembler::word : Assembler::xword;
 874 
 875   // Step 2. CAS has failed because the value held at addr does not
 876   // match expected.  This may be a false negative because the value fetched
 877   // from addr (now held in result) may be a from-space pointer to the
 878   // original copy of same object referenced by to-space pointer expected.
 879   //
 880   // To resolve this, it suffices to find the forward pointer associated
 881   // with fetched value.  If this matches expected, retry CAS with new
 882   // parameters.  If this mismatches, then we have a legitimate
 883   // failure, and we're done.
 884 
 885   // overwrite tmp with from-space pointer fetched from memory
 886   __ mov(_tmp1, _result);
 887 
 888   if (UseCompressedOops) {
 889     // Decode tmp in order to resolve its forward pointer
 890     __ decode_heap_oop_not_null(_tmp1, _tmp1);
 891   }
 892 
 893   // Load/decode forwarding pointer.
 894   __ ldr(_tmp1, Address(_tmp1, oopDesc::mark_offset_in_bytes()));
 895   // Negate the mark-word. This allows us to test lowest 2 bits easily while preserving the upper bits.
 896   __ eon(_tmp1, _tmp1, zr);
 897   __ ands(zr, _tmp1, markWord::lock_mask_in_place);
 898   // Not forwarded, must have a legit CAS failure.
 899   __ br(Assembler::NE, *continuation());
 900   // Set the lowest two bits. This is equivalent to clearing the two bits after
 901   // the subsequent inversion.
 902   __ orr(_tmp1, _tmp1, markWord::marked_value);
 903   // And invert back to get the forwardee.
 904   __ eon(_tmp1, _tmp1, zr);
 905 
 906   if (UseCompressedOops) {
 907     // Encode tmp to compare against expected.
 908     __ encode_heap_oop_not_null(_tmp1, _tmp1);
 909   }
 910 
 911   // Does forwarded value of fetched from-space pointer match original
 912   // value of expected?  If result holds null, this comparison will fail
 913   // because we know from step1 that expected is not null.  There is
 914   // no need for a separate test for result (the value originally held
 915   // in memory) equal to null.
 916   __ cmp(_tmp1, _expected);
 917 
 918   // If not, then the failure was legitimate and we're done.
 919   // Branching to continuation with NE condition denotes failure.
 920   __ br(Assembler::NE, *continuation());
 921 
 922   // Fall through to step 3.
 923 
 924   // Step 3.  We've confirmed that the value originally held in memory
 925   // (now held in result) pointed to from-space version of original
 926   // expected value.  Try the CAS again with the from-space expected
 927   // value.  If it now succeeds, we're good.
 928   //
 929   // Note: result holds encoded from-space pointer that matches to-space
 930   // object residing at expected. result is the new "expected".
 931 
 932   // Note that macro implementation of __cmpxchg cannot use same register
 933   // tmp2 for result and expected since it overwrites result before it
 934   // compares result with expected.
 935   __ mov(_tmp1, _result);
 936   __ cmpxchg(_addr_reg, _tmp1, _new_val, size, _acquire, _release, _weak, _result);
 937   // EQ flag set iff success. result holds value fetched, rscratch1 clobbered.
 938 
 939   // If fetched value did not equal the new expected, this could
 940   // still be a false negative because some other thread may have
 941   // newly overwritten the memory value with its to-space equivalent.
 942   __ br(Assembler::EQ, *continuation());
 943 
 944   // Step 4. Retry CAS with original to-space expected.
 945   __ cmpxchg(_addr_reg, _expected, _new_val, size, _acquire, _release, _weak, _result);
 946 
 947   __ b(*continuation());
 948 }
 949 #undef __
 950 #define __ masm->
 951 #endif // COMPILER2
 952 
 953 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
 954                                                                      Register start, Register count, Register scratch) {
 955   assert(ShenandoahCardBarrier, "Should have been checked by caller");
 956 
 957   Label L_loop, L_done;
 958   const Register end = count;
 959 
 960   // Zero count? Nothing to do.
 961   __ cbz(count, L_done);
 962 
 963   // end = start + count << LogBytesPerHeapOop
 964   // last element address to make inclusive
 965   __ lea(end, Address(start, count, Address::lsl(LogBytesPerHeapOop)));
 966   __ sub(end, end, BytesPerHeapOop);
 967   __ lsr(start, start, CardTable::card_shift());
 968   __ lsr(end, end, CardTable::card_shift());
 969 
 970   // number of bytes to copy
 971   __ sub(count, end, start);
 972 
 973   Address curr_ct_holder_addr(rthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
 974   __ ldr(scratch, curr_ct_holder_addr);
 975   __ add(start, start, scratch);
 976   __ bind(L_loop);
 977   __ strb(zr, Address(start, count));
 978   __ subs(count, count, 1);
 979   __ br(Assembler::GE, L_loop);
 980   __ bind(L_done);
 981 }
 982 
 983 #undef __
 984 
 985 #ifdef COMPILER1
 986 
 987 #define __ ce->masm()->
 988 
 989 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 990   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 991   // At this point we know that marking is in progress.
 992   // If do_load() is true then we have to emit the
 993   // load of the previous value; otherwise it has already
 994   // been loaded into _pre_val.
 995 
 996   __ bind(*stub->entry());
 997 
 998   assert(stub->pre_val()->is_register(), "Precondition.");
 999 
1000   Register pre_val_reg = stub->pre_val()->as_register();
1001 
1002   if (stub->do_load()) {
1003     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/);
1004   }
1005   __ cbz(pre_val_reg, *stub->continuation());
1006   ce->store_parameter(stub->pre_val()->as_register(), 0);
1007   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
1008   __ b(*stub->continuation());
1009 }
1010 
1011 void ShenandoahBarrierSetAssembler::gen_load_reference_barrier_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
1012   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
1013   __ bind(*stub->entry());
1014 
1015   DecoratorSet decorators = stub->decorators();
1016   bool is_strong  = ShenandoahBarrierSet::is_strong_access(decorators);
1017   bool is_weak    = ShenandoahBarrierSet::is_weak_access(decorators);
1018   bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
1019   bool is_native  = ShenandoahBarrierSet::is_native_access(decorators);
1020 
1021   Register obj = stub->obj()->as_register();
1022   Register res = stub->result()->as_register();
1023   Register addr = stub->addr()->as_pointer_register();
1024   Register tmp1 = stub->tmp1()->as_register();
1025   Register tmp2 = stub->tmp2()->as_register();
1026 
1027   assert(res == r0, "result must arrive in r0");
1028 
1029   if (res != obj) {
1030     __ mov(res, obj);
1031   }
1032 
1033   if (is_strong) {
1034     // Check for object in cset.
1035     __ mov(tmp2, ShenandoahHeap::in_cset_fast_test_addr());
1036     __ lsr(tmp1, res, ShenandoahHeapRegion::region_size_bytes_shift_jint());
1037     __ ldrb(tmp2, Address(tmp2, tmp1));
1038     __ cbz(tmp2, *stub->continuation());
1039   }
1040 
1041   ce->store_parameter(res, 0);
1042   ce->store_parameter(addr, 1);
1043   if (is_strong) {
1044     if (is_native) {
1045       __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_native_rt_code_blob()->code_begin()));
1046     } else {
1047       __ far_call(RuntimeAddress(bs->load_reference_barrier_strong_rt_code_blob()->code_begin()));
1048     }
1049   } else if (is_weak) {
1050     __ far_call(RuntimeAddress(bs->load_reference_barrier_weak_rt_code_blob()->code_begin()));
1051   } else {
1052     assert(is_phantom, "only remaining strength");
1053     __ far_call(RuntimeAddress(bs->load_reference_barrier_phantom_rt_code_blob()->code_begin()));
1054   }
1055 
1056   __ b(*stub->continuation());
1057 }
1058 
1059 #undef __
1060 
1061 #define __ sasm->
1062 
1063 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
1064   __ prologue("shenandoah_pre_barrier", false);
1065 
1066   // arg0 : previous value of memory
1067 
1068   BarrierSet* bs = BarrierSet::barrier_set();
1069 
1070   const Register pre_val = r0;
1071   const Register thread = rthread;
1072   const Register tmp = rscratch1;
1073 
1074   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
1075   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
1076 
1077   Label done;
1078   Label runtime;
1079 
1080   // Is marking still active?
1081   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
1082   __ ldrb(tmp, gc_state);
1083   __ tbz(tmp, ShenandoahHeap::MARKING_BITPOS, done);
1084 
1085   // Can we store original value in the thread's buffer?
1086   __ ldr(tmp, queue_index);
1087   __ cbz(tmp, runtime);
1088 
1089   __ sub(tmp, tmp, wordSize);
1090   __ str(tmp, queue_index);
1091   __ ldr(rscratch2, buffer);
1092   __ add(tmp, tmp, rscratch2);
1093   __ load_parameter(0, rscratch2);
1094   __ str(rscratch2, Address(tmp, 0));
1095   __ b(done);
1096 
1097   __ bind(runtime);
1098   __ push_call_clobbered_registers();
1099   __ load_parameter(0, pre_val);
1100   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), pre_val);
1101   __ pop_call_clobbered_registers();
1102   __ bind(done);
1103 
1104   __ epilogue();
1105 }
1106 
1107 void ShenandoahBarrierSetAssembler::generate_c1_load_reference_barrier_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
1108   __ prologue("shenandoah_load_reference_barrier", false);
1109   // arg0 : object to be resolved
1110 
1111   __ push_call_clobbered_registers();
1112   __ load_parameter(0, r0);
1113   __ load_parameter(1, r1);
1114 
1115   bool is_strong  = ShenandoahBarrierSet::is_strong_access(decorators);
1116   bool is_weak    = ShenandoahBarrierSet::is_weak_access(decorators);
1117   bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
1118   bool is_native  = ShenandoahBarrierSet::is_native_access(decorators);
1119   if (is_strong) {
1120     if (is_native) {
1121       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
1122     } else {
1123       if (UseCompressedOops) {
1124         __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow));
1125       } else {
1126         __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong));
1127       }
1128     }
1129   } else if (is_weak) {
1130     assert(!is_native, "weak must not be called off-heap");
1131     if (UseCompressedOops) {
1132       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow));
1133     } else {
1134       __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak));
1135     }
1136   } else {
1137     assert(is_phantom, "only remaining strength");
1138     assert(is_native, "phantom must only be called off-heap");
1139     __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
1140   }
1141   __ blr(lr);
1142   __ mov(rscratch1, r0);
1143   __ pop_call_clobbered_registers();
1144   __ mov(r0, rscratch1);
1145 
1146   __ epilogue();
1147 }
1148 
1149 #undef __
1150 
1151 #endif // COMPILER1