1 /*
  2  * Copyright (c) 2018, 2023, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "classfile/javaClasses.inline.hpp"
 27 #include "gc/shared/barrierSet.hpp"
 28 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
 29 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
 30 #include "gc/shenandoah/shenandoahForwarding.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.hpp"
 32 #include "gc/shenandoah/shenandoahRuntime.hpp"
 33 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 34 #include "opto/arraycopynode.hpp"
 35 #include "opto/escape.hpp"
 36 #include "opto/graphKit.hpp"
 37 #include "opto/idealKit.hpp"
 38 #include "opto/macro.hpp"
 39 #include "opto/narrowptrnode.hpp"
 40 #include "opto/output.hpp"
 41 #include "opto/rootnode.hpp"
 42 #include "opto/runtime.hpp"
 43 
 44 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {
 45   return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());
 46 }
 47 
 48 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena) :
 49     BarrierSetC2State(comp_arena),
 50     _stubs(new (comp_arena) GrowableArray<ShenandoahBarrierStubC2*>(comp_arena, 8,  0, nullptr)),
 51     _stubs_start_offset(0) {
 52 }
 53 
 54 #define __ kit->
 55 
 56 static bool satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr,
 57                                         BasicType bt, uint adr_idx) {
 58   intptr_t offset = 0;
 59   Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
 60   AllocateNode* alloc = AllocateNode::Ideal_allocation(base);
 61 
 62   if (offset == Type::OffsetBot) {
 63     return false; // cannot unalias unless there are precise offsets
 64   }
 65 
 66   if (alloc == nullptr) {
 67     return false; // No allocation found
 68   }
 69 
 70   intptr_t size_in_bytes = type2aelembytes(bt);
 71 
 72   Node* mem = __ memory(adr_idx); // start searching here...
 73 
 74   for (int cnt = 0; cnt < 50; cnt++) {
 75 
 76     if (mem->is_Store()) {
 77 
 78       Node* st_adr = mem->in(MemNode::Address);
 79       intptr_t st_offset = 0;
 80       Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
 81 
 82       if (st_base == nullptr) {
 83         break; // inscrutable pointer
 84       }
 85 
 86       // Break we have found a store with same base and offset as ours so break
 87       if (st_base == base && st_offset == offset) {
 88         break;
 89       }
 90 
 91       if (st_offset != offset && st_offset != Type::OffsetBot) {
 92         const int MAX_STORE = BytesPerLong;
 93         if (st_offset >= offset + size_in_bytes ||
 94             st_offset <= offset - MAX_STORE ||
 95             st_offset <= offset - mem->as_Store()->memory_size()) {
 96           // Success:  The offsets are provably independent.
 97           // (You may ask, why not just test st_offset != offset and be done?
 98           // The answer is that stores of different sizes can co-exist
 99           // in the same sequence of RawMem effects.  We sometimes initialize
100           // a whole 'tile' of array elements with a single jint or jlong.)
101           mem = mem->in(MemNode::Memory);
102           continue; // advance through independent store memory
103         }
104       }
105 
106       if (st_base != base
107           && MemNode::detect_ptr_independence(base, alloc, st_base,
108                                               AllocateNode::Ideal_allocation(st_base),
109                                               phase)) {
110         // Success:  The bases are provably independent.
111         mem = mem->in(MemNode::Memory);
112         continue; // advance through independent store memory
113       }
114     } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
115 
116       InitializeNode* st_init = mem->in(0)->as_Initialize();
117       AllocateNode* st_alloc = st_init->allocation();
118 
119       // Make sure that we are looking at the same allocation site.
120       // The alloc variable is guaranteed to not be null here from earlier check.
121       if (alloc == st_alloc) {
122         // Check that the initialization is storing null so that no previous store
123         // has been moved up and directly write a reference
124         Node* captured_store = st_init->find_captured_store(offset,
125                                                             type2aelembytes(T_OBJECT),
126                                                             phase);
127         if (captured_store == nullptr || captured_store == st_init->zero_memory()) {
128           return true;
129         }
130       }
131     }
132 
133     // Unless there is an explicit 'continue', we must bail out here,
134     // because 'mem' is an inscrutable memory state (e.g., a call).
135     break;
136   }
137 
138   return false;
139 }
140 
141 static bool shenandoah_can_remove_post_barrier(GraphKit* kit, PhaseValues* phase, Node* store_ctrl, Node* adr) {
142   intptr_t      offset = 0;
143   Node*         base   = AddPNode::Ideal_base_and_offset(adr, phase, offset);
144   AllocateNode* alloc  = AllocateNode::Ideal_allocation(base);
145 
146   if (offset == Type::OffsetBot) {
147     return false; // Cannot unalias unless there are precise offsets.
148   }
149   if (alloc == nullptr) {
150     return false; // No allocation found.
151   }
152 
153   Node* mem = store_ctrl;   // Start search from Store node.
154   if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
155     InitializeNode* st_init = mem->in(0)->as_Initialize();
156     AllocateNode*  st_alloc = st_init->allocation();
157     // Make sure we are looking at the same allocation
158     if (alloc == st_alloc) {
159       return true;
160     }
161   }
162 
163   return false;
164 }
165 
166 static uint8_t get_store_barrier(C2Access& access) {
167   if (!access.is_parse_access()) {
168     // Only support for eliding barriers at parse time for now.
169     return (ShenandoahSATBBarrier ? ShenandoahBarrierSATB : 0) | (ShenandoahCardBarrier ? ShenandoahBarrierCardMark : 0);
170   }
171   GraphKit* kit = (static_cast<C2ParseAccess&>(access)).kit();
172   Node* ctl = kit->control();
173   Node* adr = access.addr().node();
174   uint adr_idx = kit->C->get_alias_index(access.addr().type());
175   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory");
176 
177   bool can_remove_pre_barrier = satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, access.type(), adr_idx);
178 
179   // We can skip marks on a freshly-allocated object in Eden. Keep this code in
180   // sync with CardTableBarrierSet::on_slowpath_allocation_exit. That routine
181   // informs GC to take appropriate compensating steps, upon a slow-path
182   // allocation, so as to make this card-mark elision safe.
183   // The post-barrier can also be removed if null is written. This case is
184   // handled by ShenandoahBarrierSetC2::expand_barriers, which runs at the end of C2's
185   // platform-independent optimizations to exploit stronger type information.
186   bool can_remove_post_barrier = ReduceInitialCardMarks &&
187     ((access.base() == kit->just_allocated_object(ctl)) ||
188      shenandoah_can_remove_post_barrier(kit, &kit->gvn(), ctl, adr));
189 
190   int barriers = 0;
191   if (!can_remove_pre_barrier) {
192     barriers |= (ShenandoahSATBBarrier ? ShenandoahBarrierSATB : 0);
193   } else {
194     barriers |= ShenandoahBarrierElided;
195   }
196 
197   if (!can_remove_post_barrier) {
198     barriers |= (ShenandoahCardBarrier ? ShenandoahBarrierCardMark : 0);
199   } else {
200     barriers |= ShenandoahBarrierElided;
201   }
202 
203   return barriers;
204 }
205 
206 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
207   DecoratorSet decorators = access.decorators();
208   bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
209   bool in_heap = (decorators & IN_HEAP) != 0;
210   bool tightly_coupled_alloc = (decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0;
211   bool needs_pre_barrier = access.is_oop() && (in_heap || anonymous);
212   // Pre-barriers are unnecessary for tightly-coupled initialization stores.
213   bool can_be_elided = needs_pre_barrier && tightly_coupled_alloc && ReduceInitialCardMarks;
214   bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
215   if (needs_pre_barrier) {
216     if (can_be_elided) {
217       access.set_barrier_data(access.barrier_data() & ~ShenandoahBarrierSATB);
218       access.set_barrier_data(access.barrier_data() | ShenandoahBarrierElided);
219     } else {
220       access.set_barrier_data(get_store_barrier(access));
221     }
222   }
223   if (no_keepalive) {
224     // No keep-alive means no need for the pre-barrier.
225     access.set_barrier_data(access.barrier_data() & ~ShenandoahBarrierSATB);
226   }
227   return BarrierSetC2::store_at_resolved(access, val);
228 }
229 
230 static void set_barrier_data(C2Access& access, bool rmw) {
231   if (!access.is_oop()) {
232     return;
233   }
234 
235   if (access.decorators() & C2_TIGHTLY_COUPLED_ALLOC) {
236     access.set_barrier_data(ShenandoahBarrierElided);
237     return;
238   }
239 
240   uint8_t barrier_data = 0;
241 
242   if (ShenandoahLoadRefBarrier) {
243     if (access.decorators() & ON_PHANTOM_OOP_REF) {
244       barrier_data |= ShenandoahBarrierPhantom;
245     } else if (access.decorators() & ON_WEAK_OOP_REF) {
246       barrier_data |= ShenandoahBarrierWeak;
247     } else {
248       barrier_data |= ShenandoahBarrierStrong;
249     }
250   }
251 
252   if (rmw) {
253     if (ShenandoahSATBBarrier) {
254       barrier_data |= ShenandoahBarrierSATB;
255     }
256     if (ShenandoahCardBarrier) {
257       barrier_data |= ShenandoahCardBarrier;
258     }
259   }
260 
261   if (access.decorators() & IN_NATIVE) {
262     barrier_data |= ShenandoahBarrierNative;
263   }
264 
265   access.set_barrier_data(barrier_data);
266 }
267 
268 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
269   // 1: non-reference load, no additional barrier is needed
270   if (!access.is_oop()) {
271     return BarrierSetC2::load_at_resolved(access, val_type);
272   }
273 
274   // 2. Set barrier data for LRB.
275   set_barrier_data(access, /* rmw = */ false);
276 
277   // 3. If we are reading the value of the referent field of a Reference object, we
278   // need to record the referent in an SATB log buffer using the pre-barrier
279   // mechanism.
280   DecoratorSet decorators = access.decorators();
281   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
282   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
283   bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
284   bool needs_read_barrier = ((on_weak || on_phantom) && !no_keepalive);
285   if (needs_read_barrier) {
286     uint8_t barriers = access.barrier_data() | (ShenandoahSATBBarrier ? ShenandoahBarrierSATB : 0);
287     access.set_barrier_data(barriers);
288   }
289 
290   return BarrierSetC2::load_at_resolved(access, val_type);
291 }
292 
293 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
294                                                              Node* new_val, const Type* value_type) const {
295   set_barrier_data(access, /* rmw = */ true);
296   return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
297 }
298 
299 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
300                                                               Node* new_val, const Type* value_type) const {
301   set_barrier_data(access, /* rmw = */ true);
302   return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
303 }
304 
305 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const {
306   set_barrier_data(access, /* rmw = */ true);
307   return BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);
308 }
309 
310 void ShenandoahBarrierSetC2::refine_store(const Node* n) {
311   MemNode* store = n->as_Store();
312   const Node* newval = n->in(MemNode::ValueIn);
313   assert(newval != nullptr, "");
314   const Type* newval_bottom = newval->bottom_type();
315   TypePtr::PTR newval_type = newval_bottom->make_ptr()->ptr();
316   uint8_t barrier_data = store->barrier_data();
317   if (!newval_bottom->isa_oopptr() &&
318       !newval_bottom->isa_narrowoop() &&
319       newval_type != TypePtr::Null) {
320     // newval is neither an OOP nor null, so there is no barrier to refine.
321     assert(barrier_data == 0, "non-OOP stores should have no barrier data");
322     return;
323   }
324   if (barrier_data == 0) {
325     // No barrier to refine.
326     return;
327   }
328   if (newval_type == TypePtr::Null) {
329     barrier_data &= ~ShenandoahBarrierNotNull;
330     // Simply elide post-barrier if writing null.
331     barrier_data &= ~ShenandoahBarrierCardMark;
332   } else if (newval_type == TypePtr::NotNull) {
333     barrier_data |= ShenandoahBarrierNotNull;
334   }
335   store->set_barrier_data(barrier_data);
336 }
337 
338 void ShenandoahBarrierSetC2::final_refinement(Compile* C) const {
339   ResourceMark rm;
340   VectorSet visited;
341   Node_List worklist;
342   worklist.push(C->root());
343   while (worklist.size() > 0) {
344     Node* n = worklist.pop();
345     if (visited.test_set(n->_idx)) {
346       continue;
347     }
348 
349     // If there are no real barrier flags on the node, strip away additional fluff.
350     // Matcher does not care about this, and we would like to avoid invoking "barrier_data() != 0"
351     // rules when the only flags are the irrelevant fluff.
352     if (n->is_LoadStore()) {
353       LoadStoreNode* load_store = n->as_LoadStore();
354       uint8_t barrier_data = load_store->barrier_data();
355       if ((barrier_data & ShenandoahBarriersReal) == 0) {
356         load_store->set_barrier_data(0);
357       }
358     } else if (n->is_Mem()) {
359       MemNode* mem = n->as_Mem();
360       uint8_t barrier_data = mem->barrier_data();
361       if ((barrier_data & ShenandoahBarriersReal) == 0) {
362         mem->set_barrier_data(0);
363       }
364     }
365 
366     for (uint j = 0; j < n->req(); j++) {
367       Node* in = n->in(j);
368       if (in != nullptr) {
369         worklist.push(in);
370       }
371     }
372   }
373 }
374 
375 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
376   ResourceMark rm;
377   VectorSet visited;
378   Node_List worklist;
379   worklist.push(C->root());
380   while (worklist.size() > 0) {
381     Node* n = worklist.pop();
382     if (visited.test_set(n->_idx)) {
383       continue;
384     }
385     switch(n->Opcode()) {
386       case Op_StoreP:
387       case Op_StoreN: {
388         refine_store(n);
389         break;
390       }
391     }
392 
393     for (uint j = 0; j < n->req(); j++) {
394       Node* in = n->in(j);
395       if (in != nullptr) {
396         worklist.push(in);
397       }
398     }
399   }
400   return false;
401 }
402 
403 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
404   bool is_oop = is_reference_type(type);
405   if (!is_oop) {
406     return false;
407   }
408   if (ShenandoahSATBBarrier && tightly_coupled_alloc) {
409     if (phase == Optimization) {
410       return false;
411     }
412     return !is_clone;
413   }
414   return true;
415 }
416 
417 bool ShenandoahBarrierSetC2::clone_needs_barrier(const TypeOopPtr* src_type, bool& is_oop_array) {
418   if (!ShenandoahCloneBarrier) {
419     return false;
420   }
421 
422   if (src_type->isa_instptr() != nullptr) {
423     // Instance: need barrier only if there is a possibility of having an oop anywhere in it.
424     ciInstanceKlass* ik = src_type->is_instptr()->instance_klass();
425     if ((src_type->klass_is_exact() || !ik->has_subklass()) &&
426         !ik->has_injected_fields() && !ik->has_object_fields()) {
427       if (!src_type->klass_is_exact()) {
428         // Class is *currently* the leaf in the hierarchy.
429         // Record the dependency so that we deopt if this does not hold in future.
430         Compile::current()->dependencies()->assert_leaf_type(ik);
431       }
432       return false;
433     }
434   } else if (src_type->isa_aryptr() != nullptr) {
435     // Array: need barrier only if array is oop-bearing.
436     BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type();
437     if (is_reference_type(src_elem, true)) {
438       is_oop_array = true;
439     } else {
440       return false;
441     }
442   }
443 
444   // Assume the worst.
445   return true;
446 }
447 
448 void ShenandoahBarrierSetC2::clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const {
449   const TypeOopPtr* src_type = kit->gvn().type(src_base)->is_oopptr();
450 
451   bool is_oop_array = false;
452   if (!clone_needs_barrier(src_type, is_oop_array)) {
453     // No barrier is needed? Just do what common BarrierSetC2 wants with it.
454     BarrierSetC2::clone(kit, src_base, dst_base, size, is_array);
455     return;
456   }
457 
458   if (ShenandoahCloneRuntime || !is_array || !is_oop_array) {
459     // Looks like an instance? Prepare the instance clone. This would either
460     // be exploded into individual accesses or be left as runtime call.
461     // Common BarrierSetC2 prepares everything for both cases.
462     BarrierSetC2::clone(kit, src_base, dst_base, size, is_array);
463     return;
464   }
465 
466   // We are cloning the oop array. Prepare to call the normal arraycopy stub
467   // after the expansion. Normal stub takes the number of actual type-sized
468   // elements to copy after the base, compute the count here.
469   Node* offset = kit->MakeConX(arrayOopDesc::base_offset_in_bytes(UseCompressedOops ? T_NARROWOOP : T_OBJECT));
470   size = kit->gvn().transform(new SubXNode(size, offset));
471   size = kit->gvn().transform(new URShiftXNode(size, kit->intcon(LogBytesPerHeapOop)));
472   ArrayCopyNode* ac = ArrayCopyNode::make(kit, false, src_base, offset, dst_base, offset, size, true, false);
473   ac->set_clone_array();
474   Node* n = kit->gvn().transform(ac);
475   if (n == ac) {
476     ac->set_adr_type(TypeRawPtr::BOTTOM);
477     kit->set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), TypeRawPtr::BOTTOM);
478   } else {
479     kit->set_all_memory(n);
480   }
481 }
482 
483 void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {
484   Node* const ctrl        = ac->in(TypeFunc::Control);
485   Node* const mem         = ac->in(TypeFunc::Memory);
486   Node* const src         = ac->in(ArrayCopyNode::Src);
487   Node* const src_offset  = ac->in(ArrayCopyNode::SrcPos);
488   Node* const dest        = ac->in(ArrayCopyNode::Dest);
489   Node* const dest_offset = ac->in(ArrayCopyNode::DestPos);
490   Node* length            = ac->in(ArrayCopyNode::Length);
491 
492   const TypeOopPtr* src_type = phase->igvn().type(src)->is_oopptr();
493 
494   bool is_oop_array = false;
495   if (!clone_needs_barrier(src_type, is_oop_array)) {
496     // No barrier is needed? Expand to normal HeapWord-sized arraycopy.
497     BarrierSetC2::clone_at_expansion(phase, ac);
498     return;
499   }
500 
501   if (ShenandoahCloneRuntime || !ac->is_clone_array() || !is_oop_array) {
502     // Still looks like an instance? Likely a large instance or reflective
503     // clone with unknown length. Go to runtime and handle it there.
504     clone_in_runtime(phase, ac, CAST_FROM_FN_PTR(address, ShenandoahRuntime::clone_addr()), "ShenandoahRuntime::clone");
505     return;
506   }
507 
508   // We are cloning the oop array. Call into normal oop array copy stubs.
509   // Those stubs would call BarrierSetAssembler to handle GC barriers.
510 
511   // This is the full clone, so offsets should equal each other and be at array base.
512   assert(src_offset == dest_offset, "should be equal");
513   const jlong offset = src_offset->get_long();
514   const TypeAryPtr* const ary_ptr = src->get_ptr_type()->isa_aryptr();
515   BasicType bt = ary_ptr->elem()->array_element_basic_type();
516   assert(offset == arrayOopDesc::base_offset_in_bytes(bt), "should match");
517 
518   const char*   copyfunc_name = "arraycopy";
519   const address copyfunc_addr = phase->basictype2arraycopy(T_OBJECT, nullptr, nullptr, true, copyfunc_name, true);
520 
521   Node* const call = phase->make_leaf_call(ctrl, mem,
522       OptoRuntime::fast_arraycopy_Type(),
523       copyfunc_addr, copyfunc_name,
524       TypeRawPtr::BOTTOM,
525       phase->basic_plus_adr(src, src_offset),
526       phase->basic_plus_adr(dest, dest_offset),
527       length,
528       phase->top()
529   );
530   phase->transform_later(call);
531 
532   phase->igvn().replace_node(ac, call);
533 }
534 
535 // Support for macro expanded GC barriers
536 void ShenandoahBarrierSetC2::eliminate_gc_barrier_data(Node* node) const {
537   if (node->is_LoadStore()) {
538     LoadStoreNode* loadstore = node->as_LoadStore();
539     loadstore->set_barrier_data(0);
540   } else if (node->is_Mem()) {
541     MemNode* mem = node->as_Mem();
542     mem->set_barrier_data(0);
543   }
544 }
545 
546 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
547   eliminate_gc_barrier_data(node);
548 }
549 
550 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {
551   return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);
552 }
553 
554 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {
555   return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
556 }
557 
558 void ShenandoahBarrierSetC2::print_barrier_data(outputStream* os, uint8_t data) {
559   os->print(" Node barriers: ");
560   if ((data & ShenandoahBarrierStrong) != 0) {
561     data &= ~ShenandoahBarrierStrong;
562     os->print("strong ");
563   }
564 
565   if ((data & ShenandoahBarrierWeak) != 0) {
566     data &= ~ShenandoahBarrierWeak;
567     os->print("weak ");
568   }
569 
570   if ((data & ShenandoahBarrierPhantom) != 0) {
571     data &= ~ShenandoahBarrierPhantom;
572     os->print("phantom ");
573   }
574 
575   if ((data & ShenandoahBarrierNative) != 0) {
576     data &= ~ShenandoahBarrierNative;
577     os->print("native ");
578   }
579 
580   if ((data & ShenandoahBarrierElided) != 0) {
581     data &= ~ShenandoahBarrierElided;
582     os->print("elided ");
583   }
584 
585   if ((data & ShenandoahBarrierSATB) != 0) {
586     data &= ~ShenandoahBarrierSATB;
587     os->print("satb ");
588   }
589 
590   if ((data & ShenandoahBarrierCardMark) != 0) {
591     data &= ~ShenandoahBarrierCardMark;
592     os->print("cardmark ");
593   }
594 
595   if ((data & ShenandoahBarrierNotNull) != 0) {
596     data &= ~ShenandoahBarrierNotNull;
597     os->print("not-null ");
598   }
599   os->cr();
600 
601   if (data > 0) {
602     fatal("Unknown bit!");
603   }
604 
605   os->print_cr(" GC configuration: %sLRB %sSATB %sCAS %sClone %sCard",
606     (ShenandoahLoadRefBarrier ? "+" : "-"),
607     (ShenandoahSATBBarrier    ? "+" : "-"),
608     (ShenandoahCASBarrier     ? "+" : "-"),
609     (ShenandoahCloneBarrier   ? "+" : "-"),
610     (ShenandoahCardBarrier    ? "+" : "-")
611   );
612 }
613 
614 #ifdef ASSERT
615 void ShenandoahBarrierSetC2::verify_gc_barrier_assert(bool cond, const char* msg, uint8_t bd, Node* n) {
616   if (!cond) {
617     stringStream ss;
618     ss.print_cr("%s", msg);
619     ss.print_cr("-----------------");
620     print_barrier_data(&ss, bd);
621     ss.print_cr("-----------------");
622     n->dump_bfs(1, nullptr, "", &ss);
623     report_vm_error(__FILE__, __LINE__, ss.as_string());
624   }
625 }
626 
627 void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const {
628   if (!ShenandoahVerifyOptoBarriers) {
629     return;
630   }
631 
632   // Final refinement might have removed the remaining auxiliary flags, making some accesses completely blank.
633   bool accept_blank = (phase == BeforeCodeGen);
634   bool expect_load_barriers       = !accept_blank && ShenandoahLoadRefBarrier;
635   bool expect_store_barriers      = !accept_blank && (ShenandoahSATBBarrier || ShenandoahCardBarrier);
636   bool expect_load_store_barriers = !accept_blank && ShenandoahCASBarrier;
637 
638   Unique_Node_List wq;
639   Node_Stack phis(0);
640   VectorSet visited;
641 
642   wq.push(compile->root());
643   for (uint next = 0; next < wq.size(); next++) {
644     Node *n = wq.at(next);
645     int opc = n->Opcode();
646 
647     if (opc == Op_LoadP || opc == Op_LoadN) {
648       uint8_t bd = n->as_Load()->barrier_data();
649 
650       const TypePtr* adr_type = n->as_Load()->adr_type();
651       if (adr_type->isa_oopptr() || adr_type->isa_narrowoop()) {
652         verify_gc_barrier_assert(!expect_load_barriers || (bd != 0), "Oop load should have barrier data", bd, n);
653 
654         bool is_weak = ((bd & (ShenandoahBarrierWeak | ShenandoahBarrierPhantom)) != 0);
655         bool is_referent = adr_type->isa_instptr() &&
656             adr_type->is_instptr()->instance_klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) &&
657             adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset();
658 
659         verify_gc_barrier_assert(!is_weak || is_referent, "Weak load only for Reference.referent", bd, n);
660       } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
661         // Some LoadP-s are used for T_ADDRESS loads from raw pointers. These are not oops.
662         // Some LoadP-s are used to load class data.
663         // TODO: Verify their barrier data.
664       } else {
665         verify_gc_barrier_assert(false, "Unclassified access type", bd, n);
666       }
667     } else if (opc == Op_StoreP || opc == Op_StoreN) {
668       uint8_t bd = n->as_Store()->barrier_data();
669       const TypePtr* adr_type = n->as_Store()->adr_type();
670       if (adr_type->isa_oopptr() || adr_type->isa_narrowoop()) {
671         // Reference.clear stores null
672         bool is_referent = adr_type->isa_instptr() &&
673              adr_type->is_instptr()->instance_klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) &&
674              adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset();
675 
676         const TypePtr* val_type = n->as_Store()->in(MemNode::Memory)->adr_type();
677         if (!is_referent && (val_type->isa_oopptr() || val_type->isa_narrowoop())) {
678           verify_gc_barrier_assert(!expect_store_barriers || (bd != 0), "Oop store should have barrier data", bd, n);
679         }
680       } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
681         // Similar to LoadP-s, some of these accesses are raw, and some are handling oops.
682         // TODO: Verify their barrier data.
683       } else {
684         verify_gc_barrier_assert(false, "Unclassified access type", bd, n);
685       }
686     } else if (opc == Op_WeakCompareAndSwapP || opc == Op_WeakCompareAndSwapN ||
687                opc == Op_CompareAndExchangeP || opc == Op_CompareAndExchangeN ||
688                opc == Op_CompareAndSwapP     || opc == Op_CompareAndSwapN ||
689                opc == Op_GetAndSetP          || opc == Op_GetAndSetN) {
690       uint8_t bd = n->as_LoadStore()->barrier_data();
691       verify_gc_barrier_assert(!expect_load_store_barriers || (bd != 0), "Oop load-store should have barrier data", bd, n);
692     } else if (n->is_Mem()) {
693       uint8_t bd = MemNode::barrier_data(n); // FIXME: LOL HotSpot, why not n->as_Mem()? LoadStore is both is_Mem() and not as_Mem().
694       verify_gc_barrier_assert(bd == 0, "Other mem nodes should have no barrier data", bd, n);
695     }
696 
697     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
698       Node* m = n->fast_out(i);
699       wq.push(m);
700     }
701   }
702 }
703 #endif
704 
705 static ShenandoahBarrierSetC2State* barrier_set_state() {
706   return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
707 }
708 
709 int ShenandoahBarrierSetC2::estimate_stub_size() const {
710   Compile* const C = Compile::current();
711   BufferBlob* const blob = C->output()->scratch_buffer_blob();
712   GrowableArray<ShenandoahBarrierStubC2*>* const stubs = barrier_set_state()->stubs();
713   int size = 0;
714 
715   for (int i = 0; i < stubs->length(); i++) {
716     CodeBuffer cb(blob->content_begin(), checked_cast<CodeBuffer::csize_t>((address)C->output()->scratch_locs_memory() - blob->content_begin()));
717     MacroAssembler masm(&cb);
718     stubs->at(i)->emit_code(masm);
719     size += cb.insts_size();
720   }
721 
722   return size;
723 }
724 
725 void ShenandoahBarrierSetC2::emit_stubs(CodeBuffer& cb) const {
726   MacroAssembler masm(&cb);
727   GrowableArray<ShenandoahBarrierStubC2*>* const stubs = barrier_set_state()->stubs();
728   barrier_set_state()->set_stubs_start_offset(masm.offset());
729 
730   for (int i = 0; i < stubs->length(); i++) {
731     // Make sure there is enough space in the code buffer
732     if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::MAX_inst_size) && cb.blob() == nullptr) {
733       ciEnv::current()->record_failure("CodeCache is full");
734       return;
735     }
736 
737     stubs->at(i)->emit_code(masm);
738   }
739 
740   masm.flush();
741 
742 }
743 
744 void ShenandoahBarrierStubC2::register_stub() {
745   if (!Compile::current()->output()->in_scratch_emit_size()) {
746     barrier_set_state()->stubs()->append(this);
747   }
748 }
749 
750 ShenandoahLoadBarrierStubC2* ShenandoahLoadBarrierStubC2::create(const MachNode* node, Register dst, Address src, bool narrow, Register tmp) {
751   auto* stub = new (Compile::current()->comp_arena()) ShenandoahLoadBarrierStubC2(node, dst, src, narrow, tmp);
752   stub->register_stub();
753   return stub;
754 }
755 
756 ShenandoahStoreBarrierStubC2* ShenandoahStoreBarrierStubC2::create(const MachNode* node, Address dst, bool dst_narrow, Register src, bool src_narrow, Register tmp) {
757   auto* stub = new (Compile::current()->comp_arena()) ShenandoahStoreBarrierStubC2(node, dst, dst_narrow, src, src_narrow, tmp);
758   stub->register_stub();
759   return stub;
760 }
761 
762 ShenandoahLoadRefBarrierStubC2* ShenandoahLoadRefBarrierStubC2::create(const MachNode* node, Register obj, Register addr, Register tmp1, Register tmp2, Register tmp3, bool narrow) {
763   auto* stub = new (Compile::current()->comp_arena()) ShenandoahLoadRefBarrierStubC2(node, obj, addr, tmp1, tmp2, tmp3, narrow);
764   stub->register_stub();
765   return stub;
766 }
767 
768 ShenandoahSATBBarrierStubC2* ShenandoahSATBBarrierStubC2::create(const MachNode* node, Register addr, Register preval, Register tmp, bool encoded_preval) {
769   auto* stub = new (Compile::current()->comp_arena()) ShenandoahSATBBarrierStubC2(node, addr, preval, tmp, encoded_preval);
770   stub->register_stub();
771   return stub;
772 }
773 
774 ShenandoahCASBarrierSlowStubC2* ShenandoahCASBarrierSlowStubC2::create(const MachNode* node, Register addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae, bool acquire, bool release, bool weak) {
775   auto* stub = new (Compile::current()->comp_arena()) ShenandoahCASBarrierSlowStubC2(node, addr, Address(), expected, new_val, result, tmp1, tmp2, cae, acquire, release, weak);
776   stub->register_stub();
777   return stub;
778 }
779 
780 ShenandoahCASBarrierSlowStubC2* ShenandoahCASBarrierSlowStubC2::create(const MachNode* node, Address addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae) {
781   auto* stub = new (Compile::current()->comp_arena()) ShenandoahCASBarrierSlowStubC2(node, noreg, addr, expected, new_val, result, tmp1, tmp2, cae, false, false, false);
782   stub->register_stub();
783   return stub;
784 }
785 
786 bool ShenandoahBarrierSetC2State::needs_liveness_data(const MachNode* mach) const {
787   return ShenandoahSATBBarrierStubC2::needs_barrier(mach) ||
788          ShenandoahLoadRefBarrierStubC2::needs_barrier(mach);
789 }
790 
791 bool ShenandoahBarrierSetC2State::needs_livein_data() const {
792   return true;
793 }