< 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,351 @@
      __ 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);
+ 
+   // Test for null.
+   if (narrow) {
+     __ testl(obj, obj);
+   } else {
+     __ testptr(obj, obj);
+   }
+   __ jccb(Assembler::zero, *stub->continuation());
+ 
+   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);
+ 
+   ShenandoahCASBarrierSlowStubC2* const slow_stub = ShenandoahCASBarrierSlowStubC2::create(node, addr, oldval, newval, res, tmp1, tmp2, exchange);
+   ShenandoahCASBarrierMidStubC2* const mid_stub = ShenandoahCASBarrierMidStubC2::create(node, slow_stub, oldval, res, tmp1, exchange);
+ 
+   Label L_success, L_failure;
+ 
+   // Remember oldval for retry logic below. It will be overwritten by the CAS.
+   if (ShenandoahCASBarrier) {
+     __ movptr(tmp2, oldval);
+   }
+ 
+   // Step 1. Fast-path.
+   //
+   // Try to CAS with given arguments. If successful, then we are done.
+   __ lock();
+   if (UseCompressedOops) {
+     __ cmpxchgl(newval, addr);
+   } else {
+     __ cmpxchgptr(newval, addr);
+   }
+ 
+   if (!ShenandoahCASBarrier) {
+     if (!exchange) {
+       assert(res != noreg, "need result register");
+       __ setcc(Assembler::equal, res);
+     }
+     return;
+   }
+ 
+   __ jcc(Assembler::notEqual, *mid_stub->entry());
+ 
+   // Slow-stub re-enters with condition flags according to CAS, we may need to
+   // set result accordingly.
+   __ bind(*slow_stub->continuation());
+ 
+   // Step 5. If we need a boolean result out of CAS, set the flag appropriately.
+   // and promote the result. Note that we handle the flag from both the 1st and 2nd CAS.
+   // Otherwise, failure witness for CAE is in oldval on all paths, and we can return.
+ 
+   if (!exchange) {
+     assert(res != noreg, "need result register");
+     __ setcc(Assembler::equal, res);
+   }
+ 
+   // Mid-stub re-enters with result set correctly.
+   __ bind(*mid_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);
+     }
+     __ 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) {
+   __ bind(*entry());
+ 
+   if (!_cae) {
+     // Set result to false, in case that we fail the following tests.
+     // Failing those tests means legitimate failures.
+     // Otherwise, result will be set correctly after returning from
+     // the slow-path.
+     __ movl(_result, 0); // Result = false.
+   }
+   // Check if CAS result is null. If it is, then we must have a legitimate failure.
+   // This makes loading the fwdptr in the slow-path simpler.
+   if (UseCompressedOops) {
+     __ testl(_expected, _expected);
+   } else {
+     __ testptr(_expected, _expected);
+   }
+   __ jcc(Assembler::equal, *continuation());
+ 
+   // Check if GC is in progress, otherwise we must have a legitimate failure.
+   Address gc_state(r15_thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
+   __ testb(gc_state, ShenandoahHeap::HAS_FORWARDED);
+   __ jcc(Assembler::notZero, *_slow_stub->entry());
+   __ jmp(*continuation());
+ }
+ 
+ void ShenandoahCASBarrierSlowStubC2::emit_code(MacroAssembler& masm) {
+   __ bind(*entry());
+ 
+   assert(_expected == rax, "expected must be rax");
+ 
+   // Step 2. CAS has failed because the value held at addr does not
+   // match expected.  This may be a false negative because the value fetched
+   // from addr (now held in result) may be a from-space pointer to the
+   // original copy of same object referenced by to-space pointer expected.
+   //
+   // To resolve this, it suffices to find the forward pointer associated
+   // with fetched value.  If this matches expected, retry CAS with new
+   // parameters.  If this mismatches, then we have a legitimate
+   // failure, and we're done.
+ 
+   // overwrite tmp1 with from-space pointer fetched from memory
+   __ movptr(_tmp1, _expected);
+ 
+   if (UseCompressedOops) {
+     __ decode_heap_oop_not_null(_tmp1);
+   }
+ 
+   // Load/decode forwarding pointer.
+   __ movq(_tmp1, Address(_tmp1, oopDesc::mark_offset_in_bytes()));
+   // Negate the mark-word. This allows us to test lowest 2 bits easily while preserving the upper bits.
+   __ negq(_tmp1);
+   __ testq(_tmp1, markWord::lock_mask_in_place);
+   // Not forwarded, must have a legit CAS failure.
+   __ jcc(Assembler::notEqual, *continuation());
+   // Set the lowest two bits. This is equivalent to clearing the two bits after
+   // the subsequent inversion.
+   __ orq(_tmp1, markWord::marked_value);
+   // And invert back to get the forwardee.
+   __ negq(_tmp1);
+ 
+   if (UseCompressedOops) {
+     __ encode_heap_oop_not_null(_tmp1); // encode for comparison
+   }
+ 
+   // Now we have the forwarded offender in tmp1.
+   // We preserved the original expected value in tmp2 in the fast-path.
+   // Compare and if they don't match, we have legitimate failure
+   __ cmpptr(_tmp1, _tmp2);
+   __ jcc(Assembler::notEqual, *continuation());
+ 
+   // Fall through to step 3.
+ 
+   // Step 3.  We've confirmed that the value originally held in memory
+   // (now held in result) pointed to from-space version of original
+   // expected value.  Try the CAS again with the from-space expected
+   // value.  If it now succeeds, we're good.
+   //
+   // Note: expected holds encoded from-space pointer that matches to-space
+   // object residing at tmp1.
+   __ lock();
+   if (UseCompressedOops) {
+     __ cmpxchgl(_new_val, _addr);
+   } else {
+     __ cmpxchgptr(_new_val, _addr);
+   }
+ 
+   // If fetched value did not equal the new expected, this could
+   // still be a false negative because some other (GC) thread may have
+   // newly overwritten the memory value with its to-space equivalent.
+   __ jcc(Assembler::equal, *continuation());
+ 
+   // Step 4. Try to CAS again, but with the original to-space expected.
+   // This should be very rare.
+   __ movptr(_expected, _tmp2);
+   __ lock();
+   if (UseCompressedOops) {
+     __ cmpxchgl(_new_val, _addr);
+   } else {
+     __ cmpxchgptr(_new_val, _addr);
+   }
+   // At this point, there can no longer be false negatives.
+   __ 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 >