< prev index next >

src/hotspot/cpu/x86/gc/shenandoah/shenandoahBarrierSetAssembler_x86.cpp

Print this page
@@ -39,10 +39,13 @@
  #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"
+ #endif
  
  #define __ masm->
  
  static void save_machine_state(MacroAssembler* masm, bool handle_gpr, bool handle_fp) {
    if (handle_gpr) {

@@ -670,11 +673,15 @@
    //
    // Before reaching to resolve sequence, see if we can avoid the whole shebang
    // with filters.
  
    // Filter: when offending in-memory value is null, the failure is definitely legitimate
-   __ testptr(oldval, oldval);
+   if (UseCompressedOops) {
+     __ testl(oldval, oldval);
+   } else {
+     __ testptr(oldval, oldval);
+   }
    __ jcc(Assembler::zero, L_failure);
  
    // Filter: when heap is stable, the failure is definitely legitimate
    const Register thread = r15_thread;
    Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));

@@ -772,10 +779,332 @@
      __ movptr(res, 1);
      __ bind(exit);
    }
  }
  
+ #ifdef COMPILER2
+ void ShenandoahBarrierSetAssembler::load_ref_barrier_c2(const MachNode* node, MacroAssembler* masm, Register obj, Register addr, Register tmp1, Register tmp2, Register tmp3, bool narrow) {
+   if (!ShenandoahLoadRefBarrierStubC2::needs_barrier(node)) {
+     return;
+   }
+   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
+ 
+   ShenandoahLoadRefBarrierStubC2* const stub = ShenandoahLoadRefBarrierStubC2::create(node, obj, addr, tmp1, tmp2, tmp3, narrow);
+   stub->dont_preserve(obj);    // set at the end, no need to save
+   if (tmp1 != noreg) {
+     stub->dont_preserve(tmp1); // temp, no need to save
+   }
+   if (tmp2 != noreg) {
+     stub->dont_preserve(tmp2); // temp, no need to save
+   }
+   if (tmp3 != noreg) {
+     stub->dont_preserve(tmp3); // temp, no need to save
+   }
+ 
+   Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
+   int flags = ShenandoahHeap::HAS_FORWARDED;
+   bool is_strong = (node->barrier_data() & ShenandoahBarrierStrong) != 0;
+   if (!is_strong) {
+     flags |= ShenandoahHeap::WEAK_ROOTS;
+   }
+   __ testb(gc_state, flags);
+   __ jcc(Assembler::notZero, *stub->entry());
+   __ bind(*stub->continuation());
+ }
+ 
+ void ShenandoahBarrierSetAssembler::satb_barrier_c2(const MachNode* node, MacroAssembler* masm,
+                                                     Register addr, Register preval, Register tmp) {
+   if (!ShenandoahSATBBarrierStubC2::needs_barrier(node)) {
+     return;
+   }
+   ShenandoahSATBBarrierStubC2* const stub = ShenandoahSATBBarrierStubC2::create(node, addr, preval, tmp);
+   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
+   Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
+   __ testb(gc_state, ShenandoahHeap::MARKING);
+   __ jcc(Assembler::notZero, *stub->entry());
+   __ bind(*stub->continuation());
+ }
+ 
+ void ShenandoahBarrierSetAssembler::card_barrier_c2(const MachNode* node, MacroAssembler* masm,
+                                                     Register addr, Register addr_tmp, Register tmp) {
+   if (!ShenandoahCardBarrier ||
+       (node->barrier_data() & (ShenandoahBarrierCardMark | ShenandoahBarrierCardMarkNotNull)) == 0) {
+     return;
+   }
+   Assembler::InlineSkippedInstructionsCounter skip_counter(masm);
+   if (addr != noreg) {
+     __ mov(addr_tmp, addr);
+   }
+   __ shrptr(addr_tmp, CardTable::card_shift());
+ 
+   Address curr_ct_holder_addr(r15_thread, in_bytes(ShenandoahThreadLocalData::card_table_offset()));
+   __ movptr(tmp, curr_ct_holder_addr);
+   Address card_addr(tmp, addr_tmp, Address::times_1);
+ 
+   int dirty = CardTable::dirty_card_val();
+   if (UseCondCardMark) {
+     Label L_already_dirty;
+     __ cmpb(card_addr, dirty);
+     __ jccb(Assembler::equal, L_already_dirty);
+     __ movb(card_addr, dirty);
+     __ bind(L_already_dirty);
+   } else {
+     __ movb(card_addr, dirty);
+   }
+ }
+ 
+ void ShenandoahBarrierSetAssembler::cmpxchg_oop_c2(const MachNode* node, MacroAssembler* masm,
+                                                 Register res, Address addr, Register oldval, Register newval, Register tmp1, Register tmp2,
+                                                 bool exchange) {
+   assert(oldval == rax, "must be in rax for implicit use in cmpxchg");
+   assert_different_registers(oldval, tmp1, tmp2);
+   assert_different_registers(newval, tmp1, tmp2);
+ 
+   // Remember oldval for retry logic in slow path. We need to do it here,
+   // because it will be overwritten by the fast-path CAS.
+   if (ShenandoahCASBarrier) {
+     __ movptr(tmp2, oldval);
+   }
+ 
+   // Fast-path: Try to CAS optimistically. If successful, then we are done.
+   __ lock();
+   if (UseCompressedOops) {
+     __ cmpxchgl(newval, addr);
+   } else {
+     __ cmpxchgptr(newval, addr);
+   }
+ 
+   // If we need a boolean result out of CAS, set the flag appropriately and promote the result.
+   // This would be the final result if we do not go slow.
+   if (!exchange) {
+     assert(res != noreg, "need result register");
+     __ setcc(Assembler::equal, res);
+   } else {
+     assert(res == noreg, "no result expected");
+   }
+ 
+   if (ShenandoahCASBarrier) {
+     ShenandoahCASBarrierSlowStubC2* const slow_stub =
+       ShenandoahCASBarrierSlowStubC2::create(node, addr, oldval, newval, res, tmp1, tmp2, exchange);
+     if (res != noreg) {
+       slow_stub->dont_preserve(res);  // set at the end, no need to save
+     }
+     slow_stub->dont_preserve(oldval); // saved explicitly
+     slow_stub->dont_preserve(tmp1);   // temp, no need to save
+     slow_stub->dont_preserve(tmp2);   // temp, no need to save
+ 
+     // On success, we do not need any additional handling.
+     __ jccb(Assembler::equal, *slow_stub->continuation());
+ 
+     // If GC is in progress, it is likely we need additional handling for false negatives.
+     Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
+     __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
+     __ jcc(Assembler::notZero, *slow_stub->entry());
+ 
+     // Slow stub re-enters with result set correctly.
+     __ bind(*slow_stub->continuation());
+   }
+ }
+ 
+ #undef __
+ #define __ masm.
+ 
+ void ShenandoahLoadRefBarrierStubC2::emit_code(MacroAssembler& masm) {
+   Assembler::InlineSkippedInstructionsCounter skip_counter(&masm);
+   __ bind(*entry());
+ 
+   Register obj = _obj;
+   if (_narrow) {
+     __ movl(_tmp1, _obj);
+     __ decode_heap_oop(_tmp1);
+     obj = _tmp1;
+   }
+ 
+   // Weak/phantom loads always need to go to runtime.
+   if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
+     __ movptr(_tmp2, obj);
+     __ shrptr(_tmp2, ShenandoahHeapRegion::region_size_bytes_shift_jint());
+     __ movptr(_tmp3, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
+     __ movbool(_tmp2, Address(_tmp2, _tmp3, Address::times_1));
+     __ testbool(_tmp2);
+     __ jcc(Assembler::zero, *continuation());
+   }
+ 
+   {
+     SaveLiveRegisters save_registers(&masm, this);
+     if (c_rarg0 != obj) {
+       if (c_rarg0 == _addr) {
+         __ movptr(_tmp2, _addr);
+         _addr = _tmp2;
+       }
+       __ movptr(c_rarg0, obj);
+     }
+     if (c_rarg1 != _addr) {
+       __ movptr(c_rarg1, _addr);
+     }
+ 
+     address entry;
+     if (_narrow) {
+       if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow);
+       } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow);
+       } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow);
+       }
+     } else {
+       if ((_node->barrier_data() & ShenandoahBarrierStrong) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong);
+       } else if ((_node->barrier_data() & ShenandoahBarrierWeak) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak);
+       } else if ((_node->barrier_data() & ShenandoahBarrierPhantom) != 0) {
+         entry = CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom);
+       }
+     }
+     __ call(RuntimeAddress(entry), rax);
+     assert(!save_registers.contains(_obj), "must not save result register");
+     __ movptr(_obj, rax);
+   }
+   if (_narrow) {
+     __ encode_heap_oop(_obj);
+   }
+ 
+   __ jmp(*continuation());
+ }
+ 
+ void ShenandoahSATBBarrierStubC2::emit_code(MacroAssembler& masm) {
+   __ bind(*entry());
+   Address index(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
+   Address buffer(r15_thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
+ 
+   Label runtime;
+ 
+   // Do we need to load the previous value?
+   if (_addr != noreg) {
+     __ load_heap_oop(_preval, Address(_addr, 0), noreg, AS_RAW);
+   }
+   // Is the previous value null?
+   __ cmpptr(_preval, NULL_WORD);
+   __ jcc(Assembler::equal, *continuation());
+ 
+   // Can we store a value in the given thread's buffer?
+   // (The index field is typed as size_t.)
+   __ movptr(_tmp, index);
+   __ testptr(_tmp, _tmp);
+   __ jccb(Assembler::zero, runtime);
+   // The buffer is not full, store value into it.
+   __ subptr(_tmp, wordSize);
+   __ movptr(index, _tmp);
+   __ addptr(_tmp, buffer);
+   __ movptr(Address(_tmp, 0), _preval);
+ 
+   __ jmp(*continuation());
+ 
+   __ bind(runtime);
+   {
+     SaveLiveRegisters save_registers(&masm, this);
+     if (c_rarg0 != _preval) {
+       __ mov(c_rarg0, _preval);
+     }
+     // rax is a caller-saved, non-argument-passing register, so it does not
+     // interfere with c_rarg0 or c_rarg1. If it contained any live value before
+     // entering this stub, it is saved at this point, and restored after the
+     // call. If it did not contain any live value, it is free to be used. In
+     // either case, it is safe to use it here as a call scratch register.
+     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_pre_c2)), rax);
+   }
+   __ jmp(*continuation());
+ }
+ 
+ void ShenandoahCASBarrierMidStubC2::emit_code(MacroAssembler& masm) {
+   // x86_64 does not implement this.
+   ShouldNotReachHere();
+ }
+ 
+ void ShenandoahCASBarrierSlowStubC2::emit_code(MacroAssembler& masm) {
+   __ bind(*entry());
+ 
+   // CAS has failed because the value held at addr does not match expected.
+   // This may be a false negative because the version in memory might be
+   // the from-space version of the same object we currently hold to-space
+   // reference for.
+   //
+   // To resolve this, we need to pass the location through the LRB fixup,
+   // this will make sure that the location has only to-space pointers.
+   // To avoid calling into runtime often, we cset-check the object first.
+   // We can inline most of the work here, but there is little point,
+   // as CAS failures over cset locations must be rare. This fast-slow split
+   // matches what we do for normal LRB.
+ 
+   assert(_expected == rax, "expected must be rax");
+ 
+   // Non-strong references should always go to runtime. We do not expect
+   // CASes over non-strong locations.
+   assert((_node->barrier_data() & ShenandoahBarrierStrong) != 0, "Only strong references for CASes");
+ 
+   Label L_final;
+ 
+   // Fast-path stashed original oldval to tmp2 for us. We need to save it
+   // for the final retry. This frees up tmp2 for cset check below.
+   __ push(_tmp2);
+ 
+   // (Compressed) failure witness is in _expected.
+   // Unpack it and check if it is in collection set.
+   __ movptr(_tmp1, _expected);
+   if (UseCompressedOops) {
+     __ decode_heap_oop(_tmp1);
+   }
+   __ shrptr(_tmp1, ShenandoahHeapRegion::region_size_bytes_shift_jint());
+   __ movptr(_tmp2, (intptr_t) ShenandoahHeap::in_cset_fast_test_addr());
+   __ movbool(_tmp1, Address(_tmp1, _tmp2, Address::times_1));
+   __ testbool(_tmp1);
+   __ jcc(Assembler::zero, L_final);
+ 
+   {
+     SaveLiveRegisters save_registers(&masm, this);
+     // Load up failure witness again.
+     if (c_rarg0 != _expected) {
+       __ movptr(c_rarg0, _expected);
+     }
+     if (UseCompressedOops) {
+       __ decode_heap_oop(c_rarg0);
+     }
+     __ lea(c_rarg1, _addr);
+ 
+     if (UseCompressedOops) {
+       __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow), 2);
+     } else {
+       __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong), 2);
+     }
+     // We have called LRB to fix up the heap location. We do not care about its result,
+     // as we will just try to CAS the location again.
+   }
+ 
+   __ bind(L_final);
+ 
+   // Try to CAS again with the original expected value.
+   // At this point, there can no longer be false negatives.
+   __ pop(_expected);
+   __ lock();
+   if (UseCompressedOops) {
+     __ cmpxchgl(_new_val, _addr);
+   } else {
+     __ cmpxchgptr(_new_val, _addr);
+   }
+   if (!_cae) {
+     assert(_result != noreg, "need result register");
+     __ setcc(Assembler::equal, _result);
+   } else {
+     assert(_result == noreg, "no result expected");
+   }
+   __ jmp(*continuation());
+ }
+ 
+ #undef __
+ #define __ masm->
+ #endif
+ 
  #ifdef PRODUCT
  #define BLOCK_COMMENT(str) /* nothing */
  #else
  #define BLOCK_COMMENT(str) __ block_comment(str)
  #endif
< prev index next >