1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/brooksPointer.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  27 #include "gc/shenandoah/shenandoahConnectionMatrix.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  30 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  31 #include "gc/shenandoah/shenandoahRuntime.hpp"
  32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interp_masm.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "runtime/thread.hpp"
  37 #ifdef COMPILER1
  38 #include "c1/c1_LIRAssembler.hpp"
  39 #include "c1/c1_MacroAssembler.hpp"
  40 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  41 #endif
  42 
  43 #define __ masm->
  44 
  45 address ShenandoahBarrierSetAssembler::_shenandoah_wb = NULL;
  46 address ShenandoahBarrierSetAssembler::_shenandoah_wb_C = NULL;
  47 
  48 void ShenandoahBarrierSetAssembler::arraycopy_prologue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  49                                                        Register addr, Register count, RegSet saved_regs) {
  50   if (is_oop) {
  51     bool dest_uninitialized = (decorators & IS_DEST_UNINITIALIZED) != 0;
  52     if (!dest_uninitialized && !ShenandoahHeap::heap()->heuristics()->can_do_traversal_gc()) {
  53       __ push(saved_regs, sp);
  54       if (count == c_rarg0) {
  55         if (addr == c_rarg1) {
  56           // exactly backwards!!
  57           __ mov(rscratch1, c_rarg0);
  58           __ mov(c_rarg0, c_rarg1);
  59           __ mov(c_rarg1, rscratch1);
  60         } else {
  61           __ mov(c_rarg1, count);
  62           __ mov(c_rarg0, addr);
  63         }
  64       } else {
  65         __ mov(c_rarg0, addr);
  66         __ mov(c_rarg1, count);
  67       }
  68       if (UseCompressedOops) {
  69         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_narrow_oop_entry), 2);
  70       } else {
  71         __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_pre_oop_entry), 2);
  72       }
  73       __ pop(saved_regs, sp);
  74     }
  75   }
  76 }
  77 
  78 void ShenandoahBarrierSetAssembler::arraycopy_epilogue(MacroAssembler* masm, DecoratorSet decorators, bool is_oop,
  79                                                        Register start, Register end, Register scratch, RegSet saved_regs) {
  80   if (is_oop) {
  81     __ push(saved_regs, sp);
  82     // must compute element count unless barrier set interface is changed (other platforms supply count)
  83     assert_different_registers(start, end, scratch);
  84     __ lea(scratch, Address(end, BytesPerHeapOop));
  85     __ sub(scratch, scratch, start);               // subtract start to get #bytes
  86     __ lsr(scratch, scratch, LogBytesPerHeapOop);  // convert to element count
  87     __ mov(c_rarg0, start);
  88     __ mov(c_rarg1, scratch);
  89     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_array_post_entry), 2);
  90     __ pop(saved_regs, sp);
  91   }
  92 }
  93 
  94 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_pre(MacroAssembler* masm,
  95                                                                  Register obj,
  96                                                                  Register pre_val,
  97                                                                  Register thread,
  98                                                                  Register tmp,
  99                                                                  bool tosca_live,
 100                                                                  bool expand_call) {
 101   if (ShenandoahSATBBarrier) {
 102     satb_write_barrier_pre(masm, obj, pre_val, thread, tmp, tosca_live, expand_call);
 103   }
 104 }
 105 
 106 void ShenandoahBarrierSetAssembler::satb_write_barrier_pre(MacroAssembler* masm,
 107                                                            Register obj,
 108                                                            Register pre_val,
 109                                                            Register thread,
 110                                                            Register tmp,
 111                                                            bool tosca_live,
 112                                                            bool expand_call) {
 113   // If expand_call is true then we expand the call_VM_leaf macro
 114   // directly to skip generating the check by
 115   // InterpreterMacroAssembler::call_VM_leaf_base that checks _last_sp.
 116 
 117   assert(thread == rthread, "must be");
 118 
 119   Label done;
 120   Label runtime;
 121 
 122   assert_different_registers(obj, pre_val, tmp, rscratch1);
 123   assert(pre_val != noreg &&  tmp != noreg, "expecting a register");
 124 
 125   Address in_progress(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()));
 126   Address index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 127   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 128 
 129   // Is marking active?
 130   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 131     __ ldrw(tmp, in_progress);
 132   } else {
 133     assert(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 134     __ ldrb(tmp, in_progress);
 135   }
 136   __ cbzw(tmp, done);
 137 
 138   // Do we need to load the previous value?
 139   if (obj != noreg) {
 140     __ load_heap_oop(pre_val, Address(obj, 0), noreg, noreg, AS_RAW);
 141   }
 142 
 143   // Is the previous value null?
 144   __ cbz(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 
 150   __ ldr(tmp, index);                      // tmp := *index_adr
 151   __ cbz(tmp, runtime);                    // tmp == 0?
 152                                         // If yes, goto runtime
 153 
 154   __ sub(tmp, tmp, wordSize);              // tmp := tmp - wordSize
 155   __ str(tmp, index);                      // *index_adr := tmp
 156   __ ldr(rscratch1, buffer);
 157   __ add(tmp, tmp, rscratch1);             // tmp := tmp + *buffer_adr
 158 
 159   // Record the previous value
 160   __ str(pre_val, Address(tmp, 0));
 161   __ b(done);
 162 
 163   __ bind(runtime);
 164   // save the live input values
 165   RegSet saved = RegSet::of(pre_val);
 166   if (tosca_live) saved += RegSet::of(r0);
 167   if (obj != noreg) saved += RegSet::of(obj);
 168 
 169   __ push(saved, sp);
 170 
 171   // Calling the runtime using the regular call_VM_leaf mechanism generates
 172   // code (generated by InterpreterMacroAssember::call_VM_leaf_base)
 173   // that checks that the *(rfp+frame::interpreter_frame_last_sp) == NULL.
 174   //
 175   // If we care generating the pre-barrier without a frame (e.g. in the
 176   // intrinsified Reference.get() routine) then ebp might be pointing to
 177   // the caller frame and so this check will most likely fail at runtime.
 178   //
 179   // Expanding the call directly bypasses the generation of the check.
 180   // So when we do not have have a full interpreter frame on the stack
 181   // expand_call should be passed true.
 182 
 183   if (expand_call) {
 184     assert(pre_val != c_rarg1, "smashed arg");
 185     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 186   } else {
 187     __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 188   }
 189 
 190   __ pop(saved, sp);
 191 
 192   __ bind(done);
 193 }
 194 
 195 void ShenandoahBarrierSetAssembler::shenandoah_write_barrier_post(MacroAssembler* masm,
 196                                                                   Register store_addr,
 197                                                                   Register new_val,
 198                                                                   Register thread,
 199                                                                   Register tmp,
 200                                                                   Register tmp2) {
 201   assert(thread == rthread, "must be");
 202   assert(UseShenandoahGC, "expect Shenandoah GC");
 203 
 204   if (! UseShenandoahMatrix) {
 205     // No need for that barrier if not using matrix.
 206     return;
 207   }
 208 
 209   assert_different_registers(store_addr, new_val, thread, tmp, tmp2, rscratch1);
 210 
 211   Label done;
 212   __ cbz(new_val, done);
 213 
 214   ShenandoahConnectionMatrix* matrix = ShenandoahHeap::heap()->connection_matrix();
 215 
 216   // Compute to-region index
 217   __ lsr(tmp, new_val, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 218 
 219   // Compute from-region index
 220   __ lsr(tmp2, store_addr, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 221 
 222   // Compute matrix index
 223   __ mov(rscratch1, matrix->stride_jint());
 224   // Address is _matrix[to * stride + from]
 225   __ madd(tmp, tmp, rscratch1, tmp2);
 226   __ mov(rscratch1, matrix->magic_offset());
 227   Address loc(tmp, rscratch1);
 228 
 229   __ ldrb(tmp2, loc);
 230   __ cbnz(tmp2, done);
 231   __ mov(tmp2, 1);
 232   __ strb(tmp2, loc);
 233   __ bind(done);
 234 }
 235 
 236 void ShenandoahBarrierSetAssembler::read_barrier(MacroAssembler* masm, Register dst) {
 237   if (ShenandoahReadBarrier) {
 238     read_barrier_impl(masm, dst);
 239   }
 240 }
 241 
 242 void ShenandoahBarrierSetAssembler::read_barrier_impl(MacroAssembler* masm, Register dst) {
 243   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled");
 244   Label is_null;
 245   __ cbz(dst, is_null);
 246   read_barrier_not_null_impl(masm, dst);
 247   __ bind(is_null);
 248 }
 249 
 250 void ShenandoahBarrierSetAssembler::read_barrier_not_null(MacroAssembler* masm, Register dst) {
 251   if (ShenandoahReadBarrier) {
 252     read_barrier_not_null_impl(masm, dst);
 253   }
 254 }
 255 
 256 
 257 void ShenandoahBarrierSetAssembler::read_barrier_not_null_impl(MacroAssembler* masm, Register dst) {
 258   assert(UseShenandoahGC && (ShenandoahReadBarrier || ShenandoahStoreValReadBarrier), "should be enabled");
 259   __ ldr(dst, Address(dst, BrooksPointer::byte_offset()));
 260 }
 261 
 262 void ShenandoahBarrierSetAssembler::write_barrier(MacroAssembler* masm, Register dst) {
 263   if (ShenandoahWriteBarrier) {
 264     write_barrier_impl(masm, dst);
 265   }
 266 }
 267 
 268 void ShenandoahBarrierSetAssembler::write_barrier_impl(MacroAssembler* masm, Register dst) {
 269   assert(UseShenandoahGC && (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier), "Should be enabled");
 270   assert(dst != rscratch1, "need rscratch1");
 271   assert(dst != rscratch2, "need rscratch2");
 272 
 273   Label done;
 274 
 275   Address gc_state(rthread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 276   __ ldrb(rscratch1, gc_state);
 277 
 278   // Check for heap stability
 279   __ mov(rscratch2, ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 280   __ tst(rscratch1, rscratch2);
 281   __ br(Assembler::EQ, done);
 282 
 283   // Heap is unstable, need to perform the read-barrier even if WB is inactive
 284   if (ShenandoahWriteBarrierRB) {
 285     __ ldr(dst, Address(dst, BrooksPointer::byte_offset()));
 286   }
 287 
 288   // Check for evacuation-in-progress and jump to WB slow-path if needed
 289   __ mov(rscratch2, ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 290   __ tst(rscratch1, rscratch2);
 291   __ br(Assembler::EQ, done);
 292 
 293   RegSet to_save = RegSet::of(r0);
 294   if (dst != r0) {
 295     __ push(to_save, sp);
 296     __ mov(r0, dst);
 297   }
 298 
 299   __ far_call(RuntimeAddress(CAST_FROM_FN_PTR(address, ShenandoahBarrierSetAssembler::shenandoah_wb())));
 300 
 301   if (dst != r0) {
 302     __ mov(dst, r0);
 303     __ pop(to_save, sp);
 304   }
 305 
 306   __ bind(done);
 307 }
 308 
 309 void ShenandoahBarrierSetAssembler::storeval_barrier(MacroAssembler* masm, Register dst, Register tmp) {
 310   if (ShenandoahStoreValEnqueueBarrier) {
 311     Label is_null;
 312     __ cbz(dst, is_null);
 313     write_barrier_impl(masm, dst);
 314     __ bind(is_null);
 315     // Save possibly live regs.
 316     RegSet live_regs = RegSet::range(r0, r4) - dst;
 317     __ push(live_regs, sp);
 318     __ strd(v0, __ pre(sp, 2 * -wordSize));
 319 
 320     satb_write_barrier_pre(masm, noreg, dst, rthread, tmp, true, false);
 321 
 322     // Restore possibly live regs.
 323     __ ldrd(v0, __ post(sp, 2 * wordSize));
 324     __ pop(live_regs, sp);
 325   }
 326   if (ShenandoahStoreValReadBarrier) {
 327     read_barrier_impl(masm, dst);
 328   }
 329 }
 330 
 331 void ShenandoahBarrierSetAssembler::load_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 332                                             Register dst, Address src, Register tmp1, Register tmp_thread) {
 333   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 334   bool in_heap = (decorators & IN_HEAP) != 0;
 335   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 336   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 337   bool on_reference = on_weak || on_phantom;
 338 
 339   if (in_heap) {
 340     read_barrier_not_null(masm, src.base());
 341   }
 342 
 343   BarrierSetAssembler::load_at(masm, decorators, type, dst, src, tmp1, tmp_thread);
 344   if (ShenandoahKeepAliveBarrier && on_oop && on_reference) {
 345     __ enter();
 346     satb_write_barrier_pre(masm /* masm */,
 347                            noreg /* obj */,
 348                            dst /* pre_val */,
 349                            rthread /* thread */,
 350                            tmp1 /* tmp */,
 351                            true /* tosca_live */,
 352                            true /* expand_call */);
 353     __ leave();
 354   }
 355 }
 356 
 357 void ShenandoahBarrierSetAssembler::store_at(MacroAssembler* masm, DecoratorSet decorators, BasicType type,
 358                                              Address dst, Register val, Register tmp1, Register tmp2) {
 359   bool on_oop = type == T_OBJECT || type == T_ARRAY;
 360   bool in_heap = (decorators & IN_HEAP) != 0;
 361   if (in_heap) {
 362     write_barrier(masm, dst.base());
 363   }
 364   if (!on_oop) {
 365     BarrierSetAssembler::store_at(masm, decorators, type, dst, val, tmp1, tmp2);
 366     return;
 367   }
 368 
 369   // flatten object address if needed
 370   if (dst.index() == noreg && dst.offset() == 0) {
 371     if (dst.base() != r3) {
 372       __ mov(r3, dst.base());
 373     }
 374   } else {
 375     __ lea(r3, dst);
 376   }
 377 
 378   shenandoah_write_barrier_pre(masm,
 379                                r3 /* obj */,
 380                                tmp2 /* pre_val */,
 381                                rthread /* thread */,
 382                                tmp1  /* tmp */,
 383                                val != noreg /* tosca_live */,
 384                                false /* expand_call */);
 385 
 386   if (val == noreg) {
 387     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), noreg, noreg, noreg);
 388   } else {
 389     storeval_barrier(masm, val, tmp1);
 390     // G1 barrier needs uncompressed oop for region cross check.
 391     Register new_val = val;
 392     if (UseCompressedOops) {
 393       new_val = rscratch2;
 394       __ mov(new_val, val);
 395     }
 396     BarrierSetAssembler::store_at(masm, decorators, type, Address(r3, 0), val, noreg, noreg);
 397     shenandoah_write_barrier_post(masm,
 398                                   r3 /* store_adr */,
 399                                   new_val /* new_val */,
 400                                   rthread /* thread */,
 401                                   tmp1 /* tmp */,
 402                                   tmp2 /* tmp2 */);
 403   }
 404 
 405 }
 406 
 407 void ShenandoahBarrierSetAssembler::obj_equals(MacroAssembler* masm, Register op1, Register op2) {
 408   __ cmp(op1, op2);
 409   if (ShenandoahAcmpBarrier) {
 410     Label done;
 411     __ br(Assembler::EQ, done);
 412     // The object may have been evacuated, but we won't see it without a
 413     // membar here.
 414     __ membar(Assembler::LoadStore| Assembler::LoadLoad);
 415     read_barrier(masm, op1);
 416     read_barrier(masm, op2);
 417     __ cmp(op1, op2);
 418     __ bind(done);
 419   }
 420 }
 421 
 422 void ShenandoahBarrierSetAssembler::tlab_allocate(MacroAssembler* masm, Register obj,
 423                                                   Register var_size_in_bytes,
 424                                                   int con_size_in_bytes,
 425                                                   Register t1,
 426                                                   Register t2,
 427                                                   Label& slow_case) {
 428 
 429   assert_different_registers(obj, t2);
 430   assert_different_registers(obj, var_size_in_bytes);
 431   Register end = t2;
 432 
 433   int oop_extra_words = Universe::heap()->oop_extra_words();
 434 
 435   __ ldr(obj, Address(rthread, JavaThread::tlab_top_offset()));
 436   if (var_size_in_bytes == noreg) {
 437     __ lea(end, Address(obj, (int) (con_size_in_bytes + BrooksPointer::byte_size())));
 438   } else {
 439     __ add(var_size_in_bytes, var_size_in_bytes, BrooksPointer::byte_size());
 440     __ lea(end, Address(obj, var_size_in_bytes));
 441   }
 442   __ ldr(rscratch1, Address(rthread, JavaThread::tlab_end_offset()));
 443   __ cmp(end, rscratch1);
 444   __ br(Assembler::HI, slow_case);
 445 
 446   // update the tlab top pointer
 447   __ str(end, Address(rthread, JavaThread::tlab_top_offset()));
 448 
 449   __ add(obj, obj, BrooksPointer::byte_size());
 450   __ str(obj, Address(obj, BrooksPointer::byte_offset()));
 451 
 452   // recover var_size_in_bytes if necessary
 453   if (var_size_in_bytes == end) {
 454     __ sub(var_size_in_bytes, var_size_in_bytes, obj);
 455   }
 456 }
 457 
 458 void ShenandoahBarrierSetAssembler::resolve_for_read(MacroAssembler* masm, DecoratorSet decorators, Register obj) {
 459   bool oop_not_null = (decorators & IS_NOT_NULL) != 0;
 460   if (oop_not_null) {
 461     read_barrier_not_null(masm, obj);
 462   } else {
 463     read_barrier(masm, obj);
 464   }
 465 }
 466 
 467 void ShenandoahBarrierSetAssembler::resolve_for_write(MacroAssembler* masm, DecoratorSet decorators, Register obj) {
 468   bool oop_not_null = (decorators & IS_NOT_NULL) != 0;
 469   if (oop_not_null) {
 470     write_barrier(masm, obj);
 471   } else {
 472     Label is_null;
 473     __ cbz(obj, is_null);
 474     write_barrier(masm, obj);
 475     __ bind(is_null);
 476   }
 477 }
 478 
 479 void ShenandoahBarrierSetAssembler::cmpxchg_oop(MacroAssembler* masm, Register addr, Register expected, Register new_val,
 480                                                 bool acquire, bool release, bool weak, bool encode,
 481                                                 Register tmp1, Register tmp2, Register tmp3,
 482                                                 Register result) {
 483 
 484   if (encode) {
 485     storeval_barrier(masm, new_val, tmp3);
 486   }
 487 
 488   if (UseCompressedOops) {
 489     if (encode) {
 490       __ encode_heap_oop(tmp1, expected);
 491       expected = tmp1;
 492       __ encode_heap_oop(tmp2, new_val);
 493       new_val = tmp2;
 494     }
 495   }
 496   bool is_cae = (result != noreg);
 497   bool is_narrow = UseCompressedOops;
 498   Assembler::operand_size size = is_narrow ? Assembler::word : Assembler::xword;
 499   if (! is_cae) result = rscratch1;
 500 
 501   assert_different_registers(addr, expected, new_val, result, tmp3);
 502 
 503   Label retry, done, fail;
 504 
 505   // CAS, using LL/SC pair.
 506   __ bind(retry);
 507   __ load_exclusive(result, addr, size, acquire);
 508   if (is_narrow) {
 509     __ cmpw(result, expected);
 510   } else {
 511     __ cmp(result, expected);
 512   }
 513   __ br(Assembler::NE, fail);
 514   __ store_exclusive(tmp3, new_val, addr, size, release);
 515   if (weak) {
 516     __ cmpw(tmp3, 0u); // If the store fails, return NE to our caller
 517   } else {
 518     __ cbnzw(tmp3, retry);
 519   }
 520   __ b(done);
 521 
 522  __  bind(fail);
 523   // Check if rb(expected)==rb(result)
 524   // Shuffle registers so that we have memory value ready for next expected.
 525   __ mov(tmp3, expected);
 526   __ mov(expected, result);
 527   if (is_narrow) {
 528     __ decode_heap_oop(result, result);
 529     __ decode_heap_oop(tmp3, tmp3);
 530   }
 531   __ resolve_for_read(0, result);
 532   __ resolve_for_read(0, tmp3);
 533   __ cmp(result, tmp3);
 534   // Retry with expected now being the value we just loaded from addr.
 535   __ br(Assembler::EQ, retry);
 536   if (is_narrow && is_cae) {
 537     // For cmp-and-exchange and narrow oops, we need to restore
 538     // the compressed old-value. We moved it to 'expected' a few lines up.
 539     __ mov(result, expected);
 540   }
 541   __ bind(done);
 542 
 543 }
 544 
 545 #ifdef COMPILER1
 546 
 547 #undef __
 548 #define __ ce->masm()->
 549 
 550 void ShenandoahBarrierSetAssembler::gen_pre_barrier_stub(LIR_Assembler* ce, ShenandoahPreBarrierStub* stub) {
 551   ShenandoahBarrierSetC1* bs = (ShenandoahBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
 552   // At this point we know that marking is in progress.
 553   // If do_load() is true then we have to emit the
 554   // load of the previous value; otherwise it has already
 555   // been loaded into _pre_val.
 556 
 557   __ bind(*stub->entry());
 558 
 559   assert(stub->pre_val()->is_register(), "Precondition.");
 560 
 561   Register pre_val_reg = stub->pre_val()->as_register();
 562 
 563   if (stub->do_load()) {
 564     ce->mem2reg(stub->addr(), stub->pre_val(), T_OBJECT, stub->patch_code(), stub->info(), false /*wide*/, false /*unaligned*/);
 565   }
 566   __ cbz(pre_val_reg, *stub->continuation());
 567   ce->store_parameter(stub->pre_val()->as_register(), 0);
 568   __ far_call(RuntimeAddress(bs->pre_barrier_c1_runtime_code_blob()->code_begin()));
 569   __ b(*stub->continuation());
 570 }
 571 
 572 void ShenandoahBarrierSetAssembler::gen_write_barrier_stub(LIR_Assembler* ce, ShenandoahWriteBarrierStub* stub) {
 573 
 574   Register obj = stub->obj()->as_register();
 575   Register res = stub->result()->as_register();
 576 
 577   Label done;
 578 
 579   __ bind(*stub->entry());
 580 
 581   if (res != obj) {
 582     __ mov(res, obj);
 583   }
 584   // Check for null.
 585   if (stub->needs_null_check()) {
 586     __ cbz(res, done);
 587   }
 588 
 589   write_barrier(ce->masm(), res);
 590 
 591   __ bind(done);
 592   __ b(*stub->continuation());
 593 }
 594 
 595 #undef __
 596 
 597 #define __ sasm->
 598 
 599 void ShenandoahBarrierSetAssembler::generate_c1_pre_barrier_runtime_stub(StubAssembler* sasm) {
 600   __ prologue("shenandoah_pre_barrier", false);
 601 
 602   // arg0 : previous value of memory
 603 
 604   BarrierSet* bs = BarrierSet::barrier_set();
 605 
 606   const Register pre_val = r0;
 607   const Register thread = rthread;
 608   const Register tmp = rscratch1;
 609 
 610   Address queue_index(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()));
 611   Address buffer(thread, in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()));
 612 
 613   Label done;
 614   Label runtime;
 615 
 616   // Is marking still active?
 617   Address gc_state(thread, in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
 618   __ ldrb(tmp, gc_state);
 619   __ mov(rscratch2, ShenandoahHeap::MARKING | ShenandoahHeap::TRAVERSAL);
 620   __ tst(tmp, rscratch2);
 621   __ br(Assembler::EQ, done);
 622 
 623   // Can we store original value in the thread's buffer?
 624   __ ldr(tmp, queue_index);
 625   __ cbz(tmp, runtime);
 626 
 627   __ sub(tmp, tmp, wordSize);
 628   __ str(tmp, queue_index);
 629   __ ldr(rscratch2, buffer);
 630   __ add(tmp, tmp, rscratch2);
 631   __ load_parameter(0, rscratch2);
 632   __ str(rscratch2, Address(tmp, 0));
 633   __ b(done);
 634 
 635   __ bind(runtime);
 636   __ push_call_clobbered_registers();
 637   __ load_parameter(0, pre_val);
 638   __ call_VM_leaf(CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), pre_val, thread);
 639   __ pop_call_clobbered_registers();
 640   __ bind(done);
 641 
 642   __ epilogue();
 643 }
 644 
 645 #undef __
 646 
 647 #endif // COMPILER1
 648 
 649 address ShenandoahBarrierSetAssembler::shenandoah_wb() {
 650   assert(_shenandoah_wb != NULL, "need write barrier stub");
 651   return _shenandoah_wb;
 652 }
 653 
 654 address ShenandoahBarrierSetAssembler::shenandoah_wb_C() {
 655   assert(_shenandoah_wb_C != NULL, "need write barrier stub");
 656   return _shenandoah_wb_C;
 657 }
 658 
 659 #define __ cgen->assembler()->
 660 
 661 // Shenandoah write barrier.
 662 //
 663 // Input:
 664 //   r0: OOP to evacuate.  Not null.
 665 //
 666 // Output:
 667 //   r0: Pointer to evacuated OOP.
 668 //
 669 // Trash rscratch1, rscratch2.  Preserve everything else.
 670 address ShenandoahBarrierSetAssembler::generate_shenandoah_wb(StubCodeGenerator* cgen, bool c_abi, bool do_cset_test) {
 671 
 672   __ align(6);
 673   StubCodeMark mark(cgen, "StubRoutines", "shenandoah_wb");
 674   address start = __ pc();
 675 
 676   if (do_cset_test) {
 677     Label work;
 678     __ mov(rscratch2, ShenandoahHeap::in_cset_fast_test_addr());
 679     __ lsr(rscratch1, r0, ShenandoahHeapRegion::region_size_bytes_shift_jint());
 680     __ ldrb(rscratch2, Address(rscratch2, rscratch1));
 681     __ tbnz(rscratch2, 0, work);
 682     __ ret(lr);
 683     __ bind(work);
 684   }
 685 
 686   Register obj = r0;
 687 
 688   __ enter(); // required for proper stackwalking of RuntimeStub frame
 689 
 690   if (!c_abi) {
 691     __ push_call_clobbered_registers();
 692   } else {
 693     __ push_call_clobbered_fp_registers();
 694   }
 695 
 696   __ mov(lr, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_JRT));
 697   __ blrt(lr, 1, 0, MacroAssembler::ret_type_integral);
 698   if (!c_abi) {
 699     __ mov(rscratch1, obj);
 700     __ pop_call_clobbered_registers();
 701     __ mov(obj, rscratch1);
 702   } else {
 703     __ pop_call_clobbered_fp_registers();
 704   }
 705 
 706   __ leave(); // required for proper stackwalking of RuntimeStub frame
 707   __ ret(lr);
 708 
 709   return start;
 710 }
 711 
 712 #undef __
 713 
 714 void ShenandoahBarrierSetAssembler::barrier_stubs_init() {
 715   if (ShenandoahWriteBarrier || ShenandoahStoreValEnqueueBarrier) {
 716     int stub_code_size = 2048;
 717     ResourceMark rm;
 718     BufferBlob* bb = BufferBlob::create("shenandoah_barrier_stubs", stub_code_size);
 719     CodeBuffer buf(bb);
 720     StubCodeGenerator cgen(&buf);
 721     _shenandoah_wb = generate_shenandoah_wb(&cgen, false, true);
 722     _shenandoah_wb_C = generate_shenandoah_wb(&cgen, true, !ShenandoahWriteBarrierCsetTestInIR);
 723   }
 724 }