1 /*
  2  * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 28 #include "gc/shenandoah/mode/shenandoahMode.hpp"
 29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
 30 #include "gc/shenandoah/shenandoahBarrierSetAssembler.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 #include "opto/output.hpp"
 47 #endif
 48 
 49 #define __ masm->
 50 
 51 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
 52                                                        Register src, Register dst, Register count, RegSet saved_regs) {
 53   if (is_oop) {
 54     bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
 55     if ((ShenandoahSATBBarrier && !dest_uninitialized) || ShenandoahLoadRefBarrier) {
 56 
 57       Label done;
 58 
 59       // Avoid calling runtime if count == 0
 60       __ beqz(count, done);
 61 
 62       // Is GC active?
 63       Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 64       assert_different_registers(src, dst, count, t0);
 65 
 66       assert(!saved_regs.contains(t0), "Sanity: about to clobber t0");
 67 
 68       __ lbu(t0, gc_state);
 69       if (ShenandoahSATBBarrier && dest_uninitialized) {
 70         __ test_bit(t0, t0, ShenandoahHeap::HAS_FORWARDED_BITPOS);
 71         __ beqz(t0, done);
 72       } else {
 73         __ andi(t0, t0, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::MARKING);
 74         __ beqz(t0, done);
 75       }
 76 
 77       __ push_call_clobbered_registers();
 78       // If arguments are not in proper places, shuffle them.
 79       // Doing this via the stack is the most straight-forward way to avoid
 80       // accidentally smashing any register.
 81       if (c_rarg0 != src || c_rarg1 != dst || c_rarg2 != count) {
 82         __ push_reg(RegSet::of(src), sp);
 83         __ push_reg(RegSet::of(dst), sp);
 84         __ push_reg(RegSet::of(count), sp);
 85         __ pop_reg(RegSet::of(c_rarg2), sp);
 86         __ pop_reg(RegSet::of(c_rarg1), sp);
 87         __ pop_reg(RegSet::of(c_rarg0), sp);
 88       }
 89       address target = nullptr;
 90       if (UseCompressedOops) {
 91         target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_narrow_oop);
 92       } else {
 93         target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::arraycopy_barrier_oop);
 94       }
 95       __ call_VM_leaf(target, 3);
 96       __ pop_call_clobbered_registers();
 97       __ bind(done);
 98     }
 99   }
100 }
101 
102 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
103                                                        Register start, Register count, Register tmp) {
104   if (ShenandoahCardBarrier && is_oop) {
105     gen_write_ref_array_post_barrier(masm, decorators, start, count, tmp);
106   }
107 }
108 
109 void ShenandoahBarrierSetAssembler::satb_barrier(MacroAssembler* masm,
110                                                  Register obj,
111                                                  Register pre_val,
112                                                  Register thread,
113                                                  Register tmp1,
114                                                  Register tmp2) {
115   assert(ShenandoahSATBBarrier, "Should be checked by caller");
116   assert(thread == xthread, "must be");
117 
118   Label done;
119   Label runtime;
120 
121   assert_different_registers(obj, pre_val, tmp1, tmp2);
122   assert(pre_val != noreg && tmp1 != noreg && tmp2 != noreg, "expecting a register");
123 
124   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
125   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
126 
127   // Is marking active?
128   Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
129   __ lbu(t1, gc_state);
130   __ test_bit(t1, t1, ShenandoahHeap::MARKING_BITPOS);
131   __ beqz(t1, done);
132 
133   // Do we need to load the previous value?
134   if (obj != noreg) {
135     if (UseCompressedOops) {
136       __ lwu(pre_val, Address(obj, 0));
137       __ decode_heap_oop(pre_val);
138     } else {
139       __ ld(pre_val, Address(obj, 0));
140     }
141   }
142 
143   // Is the previous value null?
144   __ beqz(pre_val, done);
145 
146   // Can we store original value in the thread's buffer?
147   // Is index == 0?
148   // (The index field is typed as size_t.)
149   __ ld(tmp1, index);                  // tmp := *index_adr
150   __ beqz(tmp1, runtime);              // tmp == 0? If yes, goto runtime
151 
152   __ subi(tmp1, tmp1, wordSize);       // tmp := tmp - wordSize
153   __ sd(tmp1, index);                  // *index_adr := tmp
154   __ ld(tmp2, buffer);
155   __ add(tmp1, tmp1, tmp2);            // tmp := tmp + *buffer_adr
156 
157   // Record the previous value
158   __ sd(pre_val, Address(tmp1, 0));
159   __ j(done);
160 
161   // Slow-path call.
162   __ bind(runtime);
163   __ enter();
164   __ push_call_clobbered_registers();
165   if (c_rarg0 != pre_val) {
166     __ mv(c_rarg0, pre_val);
167   }
168   // Calling with super_call_VM_leaf with c_rarg0 bypasses interpreter checks and avoids any moves.
169   __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre), c_rarg0);
170   __ pop_call_clobbered_registers();
171   __ leave();
172 
173   __ bind(done);
174 }
175 
176 void ShenandoahBarrierSetAssembler::load_reference_barrier(MacroAssembler* masm,
177                                                            Register dst,
178                                                            Address load_addr,
179                                                            DecoratorSet decorators) {
180   assert(ShenandoahLoadRefBarrier, "Should be enabled");
181   assert(dst != t1 && load_addr.base() != t1, "need t1");
182   assert_different_registers(load_addr.base(), t0, t1);
183 
184   bool is_strong  = ShenandoahBarrierSet::is_strong_access(decorators);
185   bool is_weak    = ShenandoahBarrierSet::is_weak_access(decorators);
186   bool is_phantom = ShenandoahBarrierSet::is_phantom_access(decorators);
187   bool is_native  = ShenandoahBarrierSet::is_native_access(decorators);
188   bool is_narrow  = UseCompressedOops && !is_native;
189 
190   Label heap_stable, not_cset;
191   Address gc_state(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
192   __ lbu(t1, gc_state);
193 
194   // Check for heap stability
195   if (is_strong) {
196     __ test_bit(t1, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
197     __ beqz(t1, heap_stable);
198   } else {
199     Label lrb;
200     __ test_bit(t0, t1, ShenandoahHeap::WEAK_ROOTS_BITPOS);
201     __ bnez(t0, lrb);
202     __ test_bit(t0, t1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
203     __ beqz(t0, heap_stable);
204     __ bind(lrb);
205   }
206 
207   // use x11 for load address
208   Register result_dst = dst;
209   if (dst == x11) {
210     __ mv(t1, dst);
211     dst = t1;
212   }
213 
214   // Save x10 and x11, unless it is an output register
215   RegSet saved_regs = RegSet::of(x10, x11) - result_dst;
216   __ push_reg(saved_regs, sp);
217   __ la(x11, load_addr);
218   __ mv(x10, dst);
219 
220   // Test for in-cset
221   if (is_strong) {
222     if (AOTCodeCache::is_on_for_dump()) {
223       __ ld(t1, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
224       __ lwu(t0, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
225       __ srl(t0, x10, t0);
226     } else {
227       __ mv(t1, ShenandoahHeap::in_cset_fast_test_addr());
228       __ srli(t0, x10, ShenandoahHeapRegion::region_size_bytes_shift_jint());
229     }
230     __ add(t1, t1, t0);
231     __ lbu(t1, Address(t1));
232     __ test_bit(t0, t1, 0);
233     __ beqz(t0, not_cset);
234   }
235 
236   // Slow-path call
237   __ enter();
238   __ push_call_clobbered_registers();
239   address target = nullptr;
240   if (is_strong) {
241     if (is_narrow) {
242       target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
243     } else {
244       target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
245     }
246   } else if (is_weak) {
247     if (is_narrow) {
248       target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
249     } else {
250       target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
251     }
252   } else {
253     assert(is_phantom, "only remaining strength");
254     assert(!is_narrow, "phantom access cannot be narrow");
255     target = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
256   }
257   // Calling with super_call_VM_leaf with c_rarg0/1 bypasses interpreter checks and avoids any moves.
258   __ super_call_VM_leaf(target, c_rarg0, c_rarg1);
259   __ mv(t0, x10);
260   __ pop_call_clobbered_registers();
261   __ mv(x10, t0);
262   __ leave();
263 
264   __ bind(not_cset);
265   __ mv(result_dst, x10);
266   __ pop_reg(saved_regs, sp);
267 
268   __ bind(heap_stable);
269 }
270 
271 //
272 // Arguments:
273 //
274 // Inputs:
275 //   src:        oop location to load from, might be clobbered
276 //
277 // Output:
278 //   dst:        oop loaded from src location
279 //
280 // Kill:
281 //   x30 (tmp reg)
282 //
283 // Alias:
284 //   dst: x30 (might use x30 as temporary output register to avoid clobbering src)
285 //
286 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm,
287                                             DecoratorSet decorators,
288                                             BasicType type,
289                                             Register dst,
290                                             Address src,
291                                             Register tmp1,
292                                             Register tmp2) {
293   // 1: non-reference load, no additional barrier is needed
294   if (!is_reference_type(type)) {
295     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
296     return;
297   }
298 
299   // 2: load a reference from src location and apply LRB if needed
300   if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
301     Register result_dst = dst;
302 
303     // Preserve src location for LRB
304     RegSet saved_regs;
305     if (dst == src.base()) {
306       dst = (src.base() == x28) ? x29 : x28;
307       saved_regs = RegSet::of(dst);
308       __ push_reg(saved_regs, sp);
309     }
310     assert_different_registers(dst, src.base());
311 
312     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
313 
314     load_reference_barrier(masm, dst, src, decorators);
315 
316     if (dst != result_dst) {
317       __ mv(result_dst, dst);
318       dst = result_dst;
319     }
320 
321     if (saved_regs.bits() != 0) {
322       __ pop_reg(saved_regs, sp);
323     }
324   } else {
325     BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp2);
326   }
327 
328   // 3: apply keep-alive barrier if needed
329   if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {
330     satb_barrier(masm /* masm */,
331                  noreg /* obj */,
332                  dst /* pre_val */,
333                  xthread /* thread */,
334                  tmp1 /* tmp1 */,
335                  tmp2 /* tmp2 */);
336   }
337 }
338 
339 void ShenandoahBarrierSetAssembler::card_barrier(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2) {
340   assert(ShenandoahCardBarrier, "Should have been checked by caller");
341   assert(CardTable::dirty_card_val() == 0, "must be");
342   assert_different_registers(obj, tmp1, tmp2);
343 
344   __ srli(obj, obj, CardTable::card_shift());
345 
346   Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
347   __ ld(tmp1, curr_ct_holder_addr);
348   __ add(tmp1, obj, tmp1);
349 
350   if (UseCondCardMark) {
351     Label L_already_dirty;
352     __ lbu(tmp2, Address(tmp1));
353     __ beqz(tmp2, L_already_dirty);
354     __ sb(zr, Address(tmp1));
355     __ bind(L_already_dirty);
356   } else {
357     __ sb(zr, Address(tmp1));
358   }
359 }
360 
361 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
362                                              Address dst, Register val, Register tmp1, Register tmp2, Register tmp3) {
363   // 1: non-reference types require no barriers
364   if (!is_reference_type(type)) {
365     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2, tmp3);
366     return;
367   }
368 
369   // Flatten object address right away for simplicity: likely needed by barriers
370   if (dst.offset() == 0) {
371     if (dst.base() != tmp3) {
372       __ mv(tmp3, dst.base());
373     }
374   } else {
375     __ la(tmp3, dst);
376   }
377 
378   // 2: pre-barrier: SATB needs the previous value
379   if (ShenandoahBarrierSet::need_satb_barrier(decorators, type)) {
380     satb_barrier(masm,
381                  tmp3 /* obj */,
382                  tmp2 /* pre_val */,
383                  xthread /* thread */,
384                  tmp1 /* tmp */,
385                  t0 /* tmp2 */);
386   }
387 
388   // Store!
389   BarrierSetAssembler::store_at(masm, decorators, type, Address(tmp3, 0), val, noreg, noreg, noreg);
390 
391   // 3: post-barrier: card barrier needs store address
392   bool storing_non_null = (val != noreg);
393   if (ShenandoahBarrierSet::need_card_barrier(decorators, type) && storing_non_null) {
394     card_barrier(masm, tmp3, tmp1, tmp2);
395   }
396 }
397 
398 void ShenandoahBarrierSetAssembler::try_resolve_jobject_in_native(MacroAssembler* masm, Register jni_env,
399                                                                   Register obj, Register tmp, Label& slowpath) {
400   Label done;
401   // Resolve jobject
402   BarrierSetAssembler::try_resolve_jobject_in_native(masm, jni_env, obj, tmp, slowpath);
403 
404   // Check for null.
405   __ beqz(obj, done);
406 
407   assert(obj != t1, "need t1");
408   Address gc_state(jni_env, ShenandoahThreadLocalData::gc_state_offset() - JavaThread::jni_environment_offset());
409   __ lbu(t1, gc_state);
410 
411   // Check for heap in evacuation phase
412   __ test_bit(t0, t1, ShenandoahHeap::EVACUATION_BITPOS);
413   __ bnez(t0, slowpath);
414 
415   __ bind(done);
416 }
417 
418 void ShenandoahBarrierSetAssembler::try_peek_weak_handle_in_nmethod(MacroAssembler *masm, Register weak_handle,
419                                                                     Register obj, Register tmp, Label& slow_path) {
420   assert_different_registers(weak_handle, tmp, noreg);
421   assert_different_registers(obj, tmp, noreg);
422 
423 
424   Label done;
425 
426   // Peek weak handle using the standard implementation.
427   BarrierSetAssembler::try_peek_weak_handle_in_nmethod(masm, weak_handle, obj, tmp, slow_path);
428 
429   // Check if the reference is null, and if it is, take the fast path.
430   __ beqz(obj, done);
431 
432   Address gc_state(xthread, ShenandoahThreadLocalData::gc_state_offset());
433   __ lbu(tmp, gc_state);
434 
435   // Check if the heap is under weak-reference/roots processing, in
436   // which case we need to take the slow path.
437   __ test_bit(tmp, tmp, ShenandoahHeap::WEAK_ROOTS_BITPOS);
438   __ bnez(tmp, slow_path);
439   __ bind(done);
440 }
441 
442 void ShenandoahBarrierSetAssembler::check_oop(MacroAssembler* masm, Register obj, Register tmp1, Register tmp2, Label& L_error) {
443   // Check if the oop is in the right area of memory
444   __ mv(tmp2, (intptr_t) Universe::verify_oop_mask());
445   __ andr(tmp1, obj, tmp2);
446   __ mv(tmp2, (intptr_t) Universe::verify_oop_bits());
447 
448   // Compare tmp1 and tmp2.
449   __ bne(tmp1, tmp2, L_error);
450 
451   // This routine is sometimes called before applying GC barriers.
452   // With +COH, loading the klass may end up loading forwarding pointer instead.
453   Label L_skip;
454   if (UseCompactObjectHeaders) {
455     Address gc_state(xthread, ShenandoahThreadLocalData::gc_state_offset());
456     __ lbu(tmp1, gc_state);
457     __ test_bit(tmp1, tmp1, ShenandoahHeap::HAS_FORWARDED_BITPOS);
458     __ bnez(tmp1, L_skip);
459   }
460 
461   // Make sure klass is 'reasonable', which is not zero.
462   __ load_narrow_klass(tmp1, obj);
463   __ beqz(tmp1, L_error);
464 
465   __ bind(L_skip);
466 }
467 
468 void ShenandoahBarrierSetAssembler::gen_write_ref_array_post_barrier(MacroAssembler* masm, DecoratorSet decorators,
469                                                                      Register start, Register count, Register tmp) {
470   assert(ShenandoahCardBarrier, "Did you mean to enable ShenandoahCardBarrier?");
471 
472   Label L_loop, L_done;
473   const Register end = count;
474 
475   // Zero count? Nothing to do.
476   __ beqz(count, L_done);
477 
478   // end = start + count << LogBytesPerHeapOop
479   // last element address to make inclusive
480   __ shadd(end, count, start, tmp, LogBytesPerHeapOop);
481   __ subi(end, end, BytesPerHeapOop);
482   __ srli(start, start, CardTable::card_shift());
483   __ srli(end, end, CardTable::card_shift());
484 
485   // number of bytes to copy
486   __ sub(count, end, start);
487 
488   Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
489   __ ld(tmp, curr_ct_holder_addr);
490   __ add(start, start, tmp);
491 
492   __ bind(L_loop);
493   __ add(tmp, start, count);
494   __ sb(zr, Address(tmp));
495   __ subi(count, count, 1);
496   __ bgez(count, L_loop);
497   __ bind(L_done);
498 }
499 
500 #undef __
501 
502 #ifdef COMPILER1
503 
504 #define __ ce->masm()->
505 
506 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_stub(LIR_Assembler* ce, ShenandoahKeepaliveBarrierStub* stub) {
507   __ bind(*stub->entry());
508 
509   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
510 
511   Register obj = stub->obj()->as_register();
512 
513   if (stub->do_load()) {
514     ce->mem2reg(stub->addr(), stub->obj(), T_OBJECT, lir_patch_none, nullptr, false /* wide */);
515   }
516   __ beqz(obj, *stub->continuation(), /* is_far */ true);
517 
518   ce->store_parameter(obj, 0);
519   __ far_call(RuntimeAddress(bs->keepalive_barrier_stub()));
520   __ j(*stub->continuation());
521 }
522 
523 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_stub(LIR_Assembler* ce, ShenandoahLoadReferenceBarrierStub* stub) {
524   __ bind(*stub->entry());
525 
526   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*) BarrierSet::barrier_set()->barrier_set_c1();
527 
528   Register obj = stub->obj()->as_register();
529   Register addr = stub->addr()->as_pointer_register();
530   Register slow_result = stub->slow_result()->as_register();
531   assert_different_registers(obj, addr, slow_result);
532   assert(slow_result == x10, "C1 must know about our slow call result register");
533 
534   ce->store_parameter(obj, 0);
535   ce->store_parameter(addr, 1);
536   __ far_call(RuntimeAddress(bs->load_reference_barrier_stub(stub->decorators())));
537   if (obj != slow_result) {
538     __ mv(obj, slow_result);
539   }
540 
541   __ j(*stub->continuation());
542 }
543 
544 #undef __
545 
546 #define __ sasm->
547 
548 void ShenandoahBarrierSetAssembler::keepalive_barrier_c1_runtime_stub(StubAssembler* sasm) {
549   __ prologue("shenandoah_keepalive_barrier", false);
550   const Register tmp_obj = x10;
551   const Register tmp1 = x11;
552   const Register tmp2 = x12;
553   __ push_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
554   __ load_parameter(0, tmp_obj);
555   satb_barrier(sasm, noreg, tmp_obj, xthread, tmp1, tmp2);
556   __ pop_reg(RegSet::of(tmp1, tmp2, tmp_obj), sp);
557   __ epilogue();
558 }
559 
560 void ShenandoahBarrierSetAssembler::load_reference_barrier_c1_runtime_stub(StubAssembler* sasm, DecoratorSet decorators) {
561   __ prologue("shenandoah_load_reference_barrier", false);
562   const Register tmp_obj = x10;
563   const Register tmp_addr = x11;
564   __ push_reg(RegSet::of(tmp_addr), sp);
565   __ load_parameter(0, tmp_obj);
566   __ load_parameter(1, tmp_addr);
567   load_reference_barrier(sasm, tmp_obj, Address(tmp_addr, 0), decorators);
568   __ pop_reg(RegSet::of(tmp_addr), sp);
569   __ epilogue();
570 }
571 
572 #undef __
573 
574 #endif // COMPILER1
575 
576 #ifdef COMPILER2
577 
578 #undef __
579 #define __ masm->
580 
581 void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, Register tmp1, Register tmp2, bool is_narrow) {
582   // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
583   if (is_narrow) {
584     __ lwu(dst, src);
585   } else {
586     __ ld(dst, src);
587   }
588 
589   ShenandoahBarrierStubC2::load_post(masm, node, dst, src, tmp1, tmp2, is_narrow);
590 }
591 
592 void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm, Address dst, bool dst_narrow,
593     Register src, bool src_narrow, Register tmp1, Register tmp2, Register tmp3) {
594 
595   ShenandoahBarrierStubC2::store_pre(masm, node, dst, tmp1, tmp2, tmp3, dst_narrow);
596 
597   // Do the actual store
598   if (dst_narrow) {
599     if (!src_narrow) {
600       // Need to encode into tmp, because we cannot clobber src.
601       assert(tmp1 != noreg, "need temp register");
602       if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
603         __ encode_heap_oop(tmp1, src);
604       } else {
605         __ encode_heap_oop_not_null(tmp1, src);
606       }
607       src = tmp1;
608     }
609     __ sw(src, dst);
610   } else {
611     __ sd(src, dst);
612   }
613 
614   ShenandoahBarrierStubC2::store_post(masm, node, dst, tmp2, tmp3);
615 }
616 
617 void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm, Register res, Register addr,
618     Register oldval, Register newval, Register tmp1, Register tmp2, Register tmp3, bool exchange, bool narrow, bool is_acquire) {
619   const Assembler::Aqrl acquire = is_acquire ? Assembler::aq : Assembler::relaxed;
620   const Assembler::Aqrl release = Assembler::rl;
621   const Assembler::operand_size size = narrow ? Assembler::uint32 : Assembler::int64;
622 
623   ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr), tmp1, tmp2, tmp3, narrow);
624 
625   // CAS!
626   __ cmpxchg(addr, oldval, newval, size, acquire, release, /* result */ res, !exchange /* result_as_bool */);
627 
628   ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
629 }
630 
631 void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register preval,
632     Register newval, Register addr, Register tmp1, Register tmp2, Register tmp3, bool is_acquire) {
633   const bool is_narrow = node->bottom_type()->isa_narrowoop();
634 
635   ShenandoahBarrierStubC2::load_store_pre(masm, node, Address(addr, 0), tmp1, tmp2, tmp3, is_narrow);
636 
637   if (is_narrow) {
638     if (is_acquire) {
639       __ atomic_xchgalwu(preval, newval, addr);
640     } else {
641       __ atomic_xchgwu(preval, newval, addr);
642     }
643   } else {
644     if (is_acquire) {
645       __ atomic_xchgal(preval, newval, addr);
646     } else {
647       __ atomic_xchg(preval, newval, addr);
648     }
649   }
650 
651   ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), tmp2, tmp3);
652 }
653 
654 #undef __
655 #define __ masm.
656 
657 void ShenandoahBarrierStubC2::cardtable(MacroAssembler& masm, Address address, Register tmp1, Register tmp2) {
658   assert(CardTable::dirty_card_val() == 0, "must be");
659   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
660 
661   // tmp1 = card table base (holder)
662   Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
663   __ ld(tmp1, curr_ct_holder_addr);
664 
665   // tmp1 = effective address
666   __ la(tmp2, address);
667 
668   // tmp2 = &card_table[ addr >> CardTable::card_shift() ] ; card index
669   __ srli(tmp2, tmp2, CardTable::card_shift());
670   __ add(tmp2, tmp2, tmp1);
671 
672   if (UseCondCardMark) {
673     Label L_already_dirty;
674     __ lbu(tmp1, Address(tmp2));
675     __ beqz(tmp1, L_already_dirty);
676     __ sb(zr, Address(tmp2));
677     __ bind(L_already_dirty);
678   } else {
679     __ sb(zr, Address(tmp2));
680   }
681 }
682 
683 void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
684   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
685 
686   Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
687   __ lbu(tmp, gc_state_fast);
688   __ beqz(tmp, *continuation());
689   __ j(*entry());
690 
691   // This is were the slowpath stub will return to or the code above will
692   // jump to if the checks are false
693   __ bind(*continuation());
694 }
695 
696 void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
697   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
698   assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
699 
700   __ bind(*entry());
701 
702   // If we need to load ourselves, do it here.
703   if (_do_load) {
704     if (_narrow) {
705       __ lwu(_obj, _addr);
706     } else {
707       __ ld(_obj, _addr);
708     }
709   }
710 
711   // If the object is null, there is no point in applying barriers.
712   maybe_far_jump_if_zero(masm, _obj);
713 
714   // We need to make sure that loads done by callers survive across slow-path calls.
715   // For self-loads, we need to care about the case when both KA and LRB are enabled (rare).
716   bool needs_both_barriers = _needs_keep_alive_barrier && _needs_load_ref_barrier;
717   if (!_do_load || needs_both_barriers) {
718     preserve(_obj);
719   }
720 
721   // Go for barriers. Barriers can return straight to continuation, as long
722   // as another barrier is not needed and we can reach the fastpath.
723   if (needs_both_barriers) {
724     keepalive(masm, nullptr);
725     lrb(masm);
726   } else if (_needs_keep_alive_barrier) {
727     keepalive(masm, continuation());
728   } else if (_needs_load_ref_barrier) {
729     lrb(masm);
730   } else {
731     ShouldNotReachHere();
732   }
733 }
734 
735 void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg) {
736   Label L_short_jump;
737   __ bnez(reg, L_short_jump);
738   __ j(*continuation());
739   __ bind(L_short_jump);
740 }
741 
742 void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
743   Address index(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
744   Address buffer(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
745   Label L_through, L_slowpath;
746 
747   // If another barrier is enabled as well, do a runtime check for a specific barrier.
748   if (_needs_load_ref_barrier) {
749     assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
750     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
751     __ lbu(_tmp1, gc_state_fast);
752     __ beqz(_tmp1, L_through);
753   }
754 
755   // Fast-path: put object into buffer.
756   // If buffer is already full, go slow.
757   __ ld(_tmp1, index);
758   __ beqz(_tmp1, L_slowpath);
759   __ subi(_tmp1, _tmp1, wordSize);
760   __ sd(_tmp1, index);
761   __ ld(_tmp2, buffer);
762 
763   // Store the object in queue.
764   // If object is narrow, we need to decode it before inserting.
765   __ add(_tmp1, _tmp1, _tmp2);
766   if (_narrow) {
767     __ decode_heap_oop_not_null(_tmp2, _obj);
768     __ sd(_tmp2, Address(_tmp1));
769   } else {
770     __ sd(_obj, Address(_tmp1));
771   }
772 
773   // Fast-path exits here.
774   if (L_done != nullptr) {
775     __ j(*L_done);
776   } else {
777     __ j(L_through);
778   }
779 
780   // Slow-path: call runtime to handle.
781   __ bind(L_slowpath);
782 
783   {
784     SaveLiveRegisters slr(&masm, this);
785 
786     // Go to runtime and handle the rest there.
787     __ mv(c_rarg0, _obj);
788     __ la(ra, RuntimeAddress(keepalive_runtime_entry_addr()));
789     __ jalr(ra);
790   }
791   if (L_done != nullptr) {
792     __ j(*L_done);
793   } else {
794     __ bind(L_through);
795   }
796 }
797 
798 void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
799   Label L_slow;
800 
801   // If another barrier is enabled as well, do a runtime check for a specific barrier.
802   if (_needs_keep_alive_barrier) {
803     char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
804     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
805     __ lbu(_tmp1, gc_state_fast);
806     maybe_far_jump_if_zero(masm, _tmp1);
807   }
808 
809   // If weak references are being processed, weak/phantom loads need to go slow,
810   // regardless of their cset status.
811   if (_needs_load_ref_weak_barrier) {
812     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
813     __ lbu(_tmp1, gc_state_fast);
814     __ bnez(_tmp1, L_slow);
815   }
816 
817   // Cset-check. Fall-through to slow if in collection set.
818   if (_narrow) {
819     __ decode_heap_oop_not_null(_tmp2, _obj);
820   } else {
821     __ mv(_tmp2, _obj);
822   }
823 
824   if (AOTCodeCache::is_on_for_dump()) {
825     __ lwu(_tmp1, ExternalAddress(AOTRuntimeConstants::grain_shift_address()));
826     __ srl(_tmp2, _tmp2, _tmp1);
827     __ ld(_tmp1, ExternalAddress(AOTRuntimeConstants::cset_base_address()));
828   } else {
829     __ mv(_tmp1, ShenandoahHeap::in_cset_fast_test_addr());
830     __ srli(_tmp2, _tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
831   }
832   __ add(_tmp1, _tmp1, _tmp2);
833   __ lbu(_tmp1, Address(_tmp1, 0));
834   maybe_far_jump_if_zero(masm, _tmp1);
835 
836   // Slow path
837   __ bind(L_slow);
838 
839   // Obj is the result, need to temporarily stop preserving it.
840   bool is_obj_preserved = is_preserved(_obj);
841   if (is_obj_preserved) {
842     dont_preserve(_obj);
843   }
844   {
845     SaveLiveRegisters slr(&masm, this);
846 
847     // Shuffle in the arguments. The end result should be:
848     //   c_rarg0 <- obj
849     //   c_rarg1 <- lea(addr)
850     if (c_rarg0 == _obj) {
851       __ la(c_rarg1, _addr);
852     } else if (c_rarg1 == _obj) {
853       __ mv(_tmp1, c_rarg1);
854       __ la(c_rarg1, _addr);
855       __ mv(c_rarg0, _tmp1);
856     } else {
857       assert_different_registers(c_rarg1, _obj);
858       __ la(c_rarg1, _addr);
859       __ mv(c_rarg0, _obj);
860     }
861 
862     // Go to runtime and handle the rest there.
863     __ la(ra, RuntimeAddress(lrb_runtime_entry_addr()));
864     __ jalr(ra);
865 
866     // Save the result where needed. Narrow entries return narrowOop (32 bits)
867     // we need to zero the upper 32 bits of x10.
868     if (_narrow) {
869       __ zext(_obj, x10, 32);
870     } else {
871       __ mv(_obj, x10);
872     }
873   }
874   if (is_obj_preserved) {
875     preserve(_obj);
876   }
877 
878   __ j(*continuation());
879 }
880 
881 int ShenandoahBarrierStubC2::available_gp_registers() {
882   Unimplemented(); // Not used
883   return 0;
884 }
885 
886 bool ShenandoahBarrierStubC2::is_special_register(Register r) {
887   Unimplemented(); // Not used
888   return true;
889 }
890 
891 void ShenandoahBarrierStubC2::post_init() {
892   // Do nothing.
893 }
894 
895 #endif // COMPILER2