< prev index next >

src/hotspot/cpu/riscv/gc/shenandoah/shenandoahBarrierSetAssembler_riscv.cpp

Print this page
*** 40,10 ***
--- 40,16 ---
  #ifdef COMPILER1
  #include "c1/c1_LIRAssembler.hpp"
  #include "c1/c1_MacroAssembler.hpp"
  #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  #endif
+ #ifdef COMPILER2
+ #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
+ #include "opto/output.hpp"
+ #include "utilities/population_count.hpp"
+ #include "utilities/powerOfTwo.hpp"
+ #endif
  
  #define __ masm->
  
  void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
                                                         Register src, Register dst, Register count, RegSet saved_regs) {

*** 766,5 ***
--- 772,329 ---
  }
  
  #undef __
  
  #endif // COMPILER1
+ 
+ #ifdef COMPILER2
+ 
+ #undef __
+ #define __ masm.
+ 
+ int ShenandoahBarrierStubC2::available_gp_registers() {
+   return Register::number_of_registers;
+ }
+ 
+ bool ShenandoahBarrierStubC2::is_special_register(Register r) {
+   return r == fp || r == sp ||
+          r == xheapbase || r == xthread ||
+          r == t0 || r == t1 || r == zr;
+ }
+ 
+ void ShenandoahBarrierStubC2::enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp) {
+   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
+ 
+   Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(test_state)));
+   __ lbu(tmp, gc_state_fast);
+   __ beqz(tmp, *continuation());
+   __ j(*entry());
+ 
+   // This is were the slowpath stub will return to or the code above will
+   // jump to if the checks are false
+   __ bind(*continuation());
+ }
+ 
+ #undef __
+ #define __ masm->
+ 
+ void ShenandoahBarrierSetAssembler::compare_and_set_c2(const MachNode* node, MacroAssembler* masm, Register res, Register addr,
+     Register oldval, Register newval, Register tmp, bool exchange, bool narrow, bool is_acquire) {
+   const Assembler::Aqrl acquire = is_acquire ? Assembler::aq : Assembler::relaxed;
+   const Assembler::Aqrl release = Assembler::rl;
+ 
+   ShenandoahBarrierStubC2::load_store_pre(masm, node, tmp, Address(addr), t0, t1, narrow);
+ 
+   // Existing RISCV cmpxchg_oop already handles Shenandoah forwarded-value retry logic.
+   // FIXME: Why? Pre-barrier already obviates the need for retry. This is an awkward dependency on SBSA. Emit the plain cmpxchg.
+   // It returns:
+   //   - boolean 0/1 for CAS (!exchange)
+   //   - loaded/current value for CAE (exchange)
+   ShenandoahBarrierSet::assembler()->cmpxchg_oop(masm, addr, oldval, newval, acquire, release, exchange /* is_cae */, res);
+ 
+   ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), t0, t1);
+ }
+ 
+ void ShenandoahBarrierSetAssembler::get_and_set_c2(const MachNode* node, MacroAssembler* masm, Register preval,
+     Register newval, Register addr, Register tmp, bool is_acquire) {
+   const bool is_narrow = node->bottom_type()->isa_narrowoop();
+ 
+   ShenandoahBarrierStubC2::load_store_pre(masm, node, tmp, Address(addr, 0), t0, t1, is_narrow);
+ 
+   if (is_narrow) {
+     if (is_acquire) {
+       __ atomic_xchgalwu(preval, newval, addr);
+     } else {
+       __ atomic_xchgwu(preval, newval, addr);
+     }
+   } else {
+     if (is_acquire) {
+       __ atomic_xchgal(preval, newval, addr);
+     } else {
+       __ atomic_xchg(preval, newval, addr);
+     }
+   }
+ 
+   ShenandoahBarrierStubC2::load_store_post(masm, node, Address(addr, 0), t0, t1);
+ }
+ 
+ void ShenandoahBarrierSetAssembler::store_c2(const MachNode* node, MacroAssembler* masm, Address dst, bool dst_narrow,
+     Register src, bool src_narrow, Register tmp) {
+ 
+   ShenandoahBarrierStubC2::store_pre(masm, node, tmp, dst, t0, t1, dst_narrow);
+ 
+   // Do the actual store
+   if (dst_narrow) {
+     if (!src_narrow) {
+       // Need to encode into tmp, because we cannot clobber src.
+       assert(tmp != noreg, "need temp register");
+       if ((node->barrier_data() & ShenandoahBitNotNull) == 0) {
+         __ encode_heap_oop(tmp, src);
+       } else {
+         __ encode_heap_oop_not_null(tmp, src);
+       }
+       src = tmp;
+     }
+     __ sw(src, dst);
+   } else {
+     __ sd(src, dst);
+   }
+ 
+   ShenandoahBarrierStubC2::store_post(masm, node, dst, t0, t1);
+ }
+ 
+ void ShenandoahBarrierSetAssembler::load_c2(const MachNode* node, MacroAssembler* masm, Register dst, Address src, bool is_narrow) {
+   // Do the actual load. This load is the candidate for implicit null check, and MUST come first.
+   if (is_narrow) {
+     __ lwu(dst, src);
+   } else {
+     __ ld(dst, src);
+   }
+ 
+   ShenandoahBarrierStubC2::load_post(masm, node, dst, src, t0, t1, is_narrow);
+ }
+ 
+ void ShenandoahBarrierStubC2::store_post(MacroAssembler* masm, const MachNode* node, Address address, Register tmp1, Register tmp2) {
+   if (!needs_card_barrier(node)) {
+     return;
+   }
+ 
+   assert(CardTable::dirty_card_val() == 0, "must be");
+   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
+ 
+   // tmp1 = card table base (holder)
+   Address curr_ct_holder_addr(xthread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
+   __ ld(tmp1, curr_ct_holder_addr);
+ 
+   // tmp1 = effective address
+   __ la(tmp2, address);
+ 
+   // tmp2 = &card_table[ addr >> CardTable::card_shift() ] ; card index
+   __ srli(tmp2, tmp2, CardTable::card_shift());
+   __ add(tmp2, tmp2, tmp1);
+ 
+   if (UseCondCardMark) {
+     Label L_already_dirty;
+     __ lbu(tmp1, Address(tmp2));
+     __ beqz(tmp1, L_already_dirty);
+     __ sb(zr, Address(tmp2));
+     __ bind(L_already_dirty);
+   } else {
+     __ sb(zr, Address(tmp2));
+   }
+ }
+ 
+ void ShenandoahBarrierStubC2::load_store_post(MacroAssembler* masm, const MachNode* node, Address address, Register tmp1, Register tmp2) {
+   store_post(masm, node, address, tmp1, tmp2);
+ }
+ 
+ #undef __
+ #define __ masm.
+ 
+ void ShenandoahBarrierStubC2::post_init() {
+   // Do nothing.
+ }
+ 
+ void ShenandoahBarrierStubC2::emit_code(MacroAssembler& masm) {
+   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
+   assert(_needs_keep_alive_barrier || _needs_load_ref_barrier, "Why are you here?");
+ 
+   __ bind(*entry());
+ 
+   // If we need to load ourselves, do it here.
+   if (_do_load) {
+     if (_narrow) {
+       __ lwu(_obj, _addr);
+     } else {
+       __ ld(_obj, _addr);
+     }
+   }
+ 
+   // If the object is null, there is no point in applying barriers.
+   maybe_far_jump_if_zero(masm, _obj, continuation());
+ 
+   // Go for barriers. Barriers can return straight to continuation, as long
+   // as another barrier is not needed and we can reach the fastpath.
+   if (_needs_keep_alive_barrier && _needs_load_ref_barrier) {
+     keepalive(masm, nullptr);
+     lrb(masm);
+   } else if (_needs_keep_alive_barrier) {
+     keepalive(masm, continuation());
+   } else if (_needs_load_ref_barrier) {
+     lrb(masm);
+   } else {
+     ShouldNotReachHere();
+   }
+ }
+ 
+ void ShenandoahBarrierStubC2::maybe_far_jump_if_zero(MacroAssembler& masm, Register reg, Label* L_done) {
+   Label L_short_jump;
+   __ bnez(reg, L_short_jump);
+   __ j(*L_done);
+   __ bind(L_short_jump);
+ }
+ 
+ void ShenandoahBarrierStubC2::keepalive(MacroAssembler& masm, Label* L_done) {
+   Address index(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
+   Address buffer(xthread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
+   Label L_through, L_slowpath;
+ 
+   // If another barrier is enabled as well, do a runtime check for a specific barrier.
+   if (_needs_load_ref_barrier) {
+     assert(L_done == nullptr, "L_done is always null when _needs_load_ref_barrier is true");
+     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::MARKING)));
+     __ lbu(_tmp1, gc_state_fast);
+     __ beqz(_tmp1, L_through);
+   }
+ 
+   // Fast-path: put object into buffer.
+   // If buffer is already full, go slow.
+   __ ld(_tmp1, index);
+   __ beqz(_tmp1, L_slowpath);
+   __ subi(_tmp1, _tmp1, wordSize);
+   __ sd(_tmp1, index);
+   __ ld(_tmp2, buffer);
+ 
+   // If object is narrow, we need to unpack it before inserting into buffer.
+   __ add(_tmp1, _tmp1, _tmp2);
+   if (_narrow) {
+     __ decode_heap_oop_not_null(_tmp2, _obj);
+     __ sd(_tmp2, Address(_tmp1));
+   } else {
+     __ sd(_obj, Address(_tmp1));
+   }
+ 
+   // Fast-path exits here.
+   if (L_done != nullptr) {
+     __ j(*L_done);
+   } else {
+     __ j(L_through);
+   }
+ 
+   // Slow-path: call runtime to handle.
+   __ bind(L_slowpath);
+ 
+   // If this stub also supports LRB then we need to preserve _obj to use it there.
+   if (_needs_load_ref_barrier) {
+     preserve(_obj);
+   } else {
+     dont_preserve(_obj);
+   }
+ 
+   {
+     SaveLiveRegisters slr(&masm, this);
+ 
+     // Go to runtime and handle the rest there.
+     __ mv(c_rarg0, _obj);
+     __ rt_call(keepalive_runtime_entry_addr());
+   }
+ 
+   if (L_done != nullptr) {
+     __ j(*L_done);
+   } else {
+     __ bind(L_through);
+   }
+ }
+ 
+ void ShenandoahBarrierStubC2::lrb(MacroAssembler& masm) {
+   Label L_slow;
+ 
+   // If another barrier is enabled as well, do a runtime check for a specific barrier.
+   if (_needs_keep_alive_barrier) {
+     char state_to_check = ShenandoahHeap::HAS_FORWARDED | (_needs_load_ref_weak_barrier ? ShenandoahHeap::WEAK_ROOTS : 0);
+     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(state_to_check)));
+     __ lbu(_tmp1, gc_state_fast);
+     maybe_far_jump_if_zero(masm, _tmp1, continuation());
+   }
+ 
+   // If weak references are being processed, weak/phantom loads need to go slow,
+   // regardless of their cset status.
+   if (_needs_load_ref_weak_barrier) {
+     Address gc_state_fast(xthread, in_bytes(ShenandoahThreadLocalData::gc_state_fast_array_offset(ShenandoahHeap::WEAK_ROOTS)));
+     __ lbu(_tmp1, gc_state_fast);
+     __ bnez(_tmp1, L_slow);
+   }
+ 
+   // Cset-check. Fall-through to slow if in collection set.
+   if (_narrow) {
+     __ decode_heap_oop_not_null(_tmp2, _obj);
+   } else {
+     __ mv(_tmp2, _obj);
+   }
+ 
+   __ mv(_tmp1, ShenandoahHeap::in_cset_fast_test_addr());
+   __ srli(_tmp2, _tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
+   __ add(_tmp1, _tmp1, _tmp2);
+   __ lbu(_tmp1, Address(_tmp1, 0));
+   maybe_far_jump_if_zero(masm, _tmp1, continuation());
+ 
+   // Slow path
+   __ bind(L_slow);
+ 
+   // Obj is the result, need to temporarily stop preserving it.
+   dont_preserve(_obj);
+   {
+     SaveLiveRegisters slr(&masm, this);
+ 
+     // Shuffle in the arguments. The end result should be:
+     //   c_rarg0 <- obj
+     //   c_rarg1 <- lea(addr)
+     if (c_rarg0 == _obj) {
+       __ la(c_rarg1, _addr);
+     } else if (c_rarg1 == _obj) {
+       // Set up arguments in reverse, and then flip them
+       __ la(c_rarg0, _addr);
+       // flip them
+       __ mv(t0, c_rarg0);
+       __ mv(c_rarg0, c_rarg1);
+       __ mv(c_rarg1, t0);
+     } else {
+       assert_different_registers(c_rarg1, _obj);
+       __ la(c_rarg1, _addr);
+       __ mv(c_rarg0, _obj);
+     }
+ 
+     // Go to runtime and handle the rest there.
+     __ rt_call(lrb_runtime_entry_addr());
+ 
+     // Save the result where needed.
+     if (_narrow) {
+       __ zext_w(_obj, x10);
+     } else {
+       __ mv(_obj, x10);
+     }
+   }
+   preserve(_obj);
+ 
+   __ j(*continuation());
+ }
+ 
+ #endif // COMPILER2
< prev index next >