1 /*
  2  * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "classfile/javaClasses.hpp"
 26 #if INCLUDE_CDS
 27 #include "code/SCCache.hpp"
 28 #endif
 29 #include "code/vmreg.inline.hpp"
 30 #include "gc/g1/c2/g1BarrierSetC2.hpp"
 31 #include "gc/g1/g1BarrierSet.hpp"
 32 #include "gc/g1/g1BarrierSetAssembler.hpp"
 33 #include "gc/g1/g1BarrierSetRuntime.hpp"
 34 #include "gc/g1/g1CardTable.hpp"
 35 #include "gc/g1/g1ThreadLocalData.hpp"
 36 #include "gc/g1/g1HeapRegion.hpp"
 37 #include "opto/arraycopynode.hpp"
 38 #include "opto/block.hpp"
 39 #include "opto/compile.hpp"
 40 #include "opto/escape.hpp"
 41 #include "opto/graphKit.hpp"
 42 #include "opto/idealKit.hpp"
 43 #include "opto/machnode.hpp"
 44 #include "opto/macro.hpp"
 45 #include "opto/memnode.hpp"
 46 #include "opto/node.hpp"
 47 #include "opto/output.hpp"
 48 #include "opto/regalloc.hpp"
 49 #include "opto/rootnode.hpp"
 50 #include "opto/runtime.hpp"
 51 #include "opto/type.hpp"
 52 #include "utilities/growableArray.hpp"
 53 #include "utilities/macros.hpp"
 54 
 55 /*
 56  * Determine if the G1 pre-barrier can be removed. The pre-barrier is
 57  * required by SATB to make sure all objects live at the start of the
 58  * marking are kept alive, all reference updates need to any previous
 59  * reference stored before writing.
 60  *
 61  * If the previous value is null there is no need to save the old value.
 62  * References that are null are filtered during runtime by the barrier
 63  * code to avoid unnecessary queuing.
 64  *
 65  * However in the case of newly allocated objects it might be possible to
 66  * prove that the reference about to be overwritten is null during compile
 67  * time and avoid adding the barrier code completely.
 68  *
 69  * The compiler needs to determine that the object in which a field is about
 70  * to be written is newly allocated, and that no prior store to the same field
 71  * has happened since the allocation.
 72  */
 73 bool G1BarrierSetC2::g1_can_remove_pre_barrier(GraphKit* kit,
 74                                                PhaseValues* phase,
 75                                                Node* adr,
 76                                                BasicType bt,
 77                                                uint adr_idx) const {
 78   intptr_t offset = 0;
 79   Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
 80   AllocateNode* alloc = AllocateNode::Ideal_allocation(base);
 81 
 82   if (offset == Type::OffsetBot) {
 83     return false; // Cannot unalias unless there are precise offsets.
 84   }
 85   if (alloc == nullptr) {
 86     return false; // No allocation found.
 87   }
 88 
 89   intptr_t size_in_bytes = type2aelembytes(bt);
 90   Node* mem = kit->memory(adr_idx); // Start searching here.
 91 
 92   for (int cnt = 0; cnt < 50; cnt++) {
 93     if (mem->is_Store()) {
 94       Node* st_adr = mem->in(MemNode::Address);
 95       intptr_t st_offset = 0;
 96       Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
 97 
 98       if (st_base == nullptr) {
 99         break; // Inscrutable pointer.
100       }
101       if (st_base == base && st_offset == offset) {
102         // We have found a store with same base and offset as ours.
103         break;
104       }
105       if (st_offset != offset && st_offset != Type::OffsetBot) {
106         const int MAX_STORE = BytesPerLong;
107         if (st_offset >= offset + size_in_bytes ||
108             st_offset <= offset - MAX_STORE ||
109             st_offset <= offset - mem->as_Store()->memory_size()) {
110           // Success:  The offsets are provably independent.
111           // (You may ask, why not just test st_offset != offset and be done?
112           // The answer is that stores of different sizes can co-exist
113           // in the same sequence of RawMem effects.  We sometimes initialize
114           // a whole 'tile' of array elements with a single jint or jlong.)
115           mem = mem->in(MemNode::Memory);
116           continue; // Advance through independent store memory.
117         }
118       }
119       if (st_base != base
120           && MemNode::detect_ptr_independence(base, alloc, st_base,
121                                               AllocateNode::Ideal_allocation(st_base),
122                                               phase)) {
123         // Success: the bases are provably independent.
124         mem = mem->in(MemNode::Memory);
125         continue; // Advance through independent store memory.
126       }
127     } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
128       InitializeNode* st_init = mem->in(0)->as_Initialize();
129       AllocateNode* st_alloc = st_init->allocation();
130 
131       // Make sure that we are looking at the same allocation site.
132       // The alloc variable is guaranteed to not be null here from earlier check.
133       if (alloc == st_alloc) {
134         // Check that the initialization is storing null so that no previous store
135         // has been moved up and directly write a reference.
136         Node* captured_store = st_init->find_captured_store(offset,
137                                                             type2aelembytes(T_OBJECT),
138                                                             phase);
139         if (captured_store == nullptr || captured_store == st_init->zero_memory()) {
140           return true;
141         }
142       }
143     }
144     // Unless there is an explicit 'continue', we must bail out here,
145     // because 'mem' is an inscrutable memory state (e.g., a call).
146     break;
147   }
148   return false;
149 }
150 
151 /*
152  * G1, similar to any GC with a Young Generation, requires a way to keep track
153  * of references from Old Generation to Young Generation to make sure all live
154  * objects are found. G1 also requires to keep track of object references
155  * between different regions to enable evacuation of old regions, which is done
156  * as part of mixed collections. References are tracked in remembered sets,
157  * which are continuously updated as references are written to with the help of
158  * the post-barrier.
159  *
160  * To reduce the number of updates to the remembered set, the post-barrier
161  * filters out updates to fields in objects located in the Young Generation, the
162  * same region as the reference, when null is being written, or if the card is
163  * already marked as dirty by an earlier write.
164  *
165  * Under certain circumstances it is possible to avoid generating the
166  * post-barrier completely, if it is possible during compile time to prove the
167  * object is newly allocated and that no safepoint exists between the allocation
168  * and the store. This can be seen as a compile-time version of the
169  * above-mentioned Young Generation filter.
170  *
171  * In the case of a slow allocation, the allocation code must handle the barrier
172  * as part of the allocation if the allocated object is not located in the
173  * nursery; this would happen for humongous objects.
174  */
175 bool G1BarrierSetC2::g1_can_remove_post_barrier(GraphKit* kit,
176                                                 PhaseValues* phase, Node* store_ctrl,
177                                                 Node* adr) const {
178   intptr_t      offset = 0;
179   Node*         base   = AddPNode::Ideal_base_and_offset(adr, phase, offset);
180   AllocateNode* alloc  = AllocateNode::Ideal_allocation(base);
181 
182   if (offset == Type::OffsetBot) {
183     return false; // Cannot unalias unless there are precise offsets.
184   }
185   if (alloc == nullptr) {
186     return false; // No allocation found.
187   }
188 
189   Node* mem = store_ctrl;   // Start search from Store node.
190   if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
191     InitializeNode* st_init = mem->in(0)->as_Initialize();
192     AllocateNode*  st_alloc = st_init->allocation();
193     // Make sure we are looking at the same allocation
194     if (alloc == st_alloc) {
195       return true;
196     }
197   }
198 
199   return false;
200 }
201 
202 Node* G1BarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
203   DecoratorSet decorators = access.decorators();
204   bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
205   bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
206   bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
207   // If we are reading the value of the referent field of a Reference object, we
208   // need to record the referent in an SATB log buffer using the pre-barrier
209   // mechanism. Also we need to add a memory barrier to prevent commoning reads
210   // from this field across safepoints, since GC can change its value.
211   bool need_read_barrier = ((on_weak || on_phantom) && !no_keepalive);
212   if (access.is_oop() && need_read_barrier) {
213     access.set_barrier_data(G1C2BarrierPre);
214   }
215   return CardTableBarrierSetC2::load_at_resolved(access, val_type);
216 }
217 
218 void G1BarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
219   eliminate_gc_barrier_data(node);
220 }
221 
222 void G1BarrierSetC2::eliminate_gc_barrier_data(Node* node) const {
223   if (node->is_LoadStore()) {
224     LoadStoreNode* loadstore = node->as_LoadStore();
225     loadstore->set_barrier_data(0);
226   } else if (node->is_Mem()) {
227     MemNode* mem = node->as_Mem();
228     mem->set_barrier_data(0);
229   }
230 }
231 
232 static void refine_barrier_by_new_val_type(const Node* n) {
233   if (n->Opcode() != Op_StoreP &&
234       n->Opcode() != Op_StoreN) {
235     return;
236   }
237   MemNode* store = n->as_Mem();
238   const Node* newval = n->in(MemNode::ValueIn);
239   assert(newval != nullptr, "");
240   const Type* newval_bottom = newval->bottom_type();
241   TypePtr::PTR newval_type = newval_bottom->make_ptr()->ptr();
242   uint8_t barrier_data = store->barrier_data();
243   if (!newval_bottom->isa_oopptr() &&
244       !newval_bottom->isa_narrowoop() &&
245       newval_type != TypePtr::Null) {
246     // newval is neither an OOP nor null, so there is no barrier to refine.
247     assert(barrier_data == 0, "non-OOP stores should have no barrier data");
248     return;
249   }
250   if (barrier_data == 0) {
251     // No barrier to refine.
252     return;
253   }
254   if (newval_type == TypePtr::Null) {
255     // Simply elide post-barrier if writing null.
256     barrier_data &= ~G1C2BarrierPost;
257     barrier_data &= ~G1C2BarrierPostNotNull;
258   } else if (((barrier_data & G1C2BarrierPost) != 0) &&
259              newval_type == TypePtr::NotNull) {
260     // If the post-barrier has not been elided yet (e.g. due to newval being
261     // freshly allocated), mark it as not-null (simplifies barrier tests and
262     // compressed OOPs logic).
263     barrier_data |= G1C2BarrierPostNotNull;
264   }
265   store->set_barrier_data(barrier_data);
266   return;
267 }
268 
269 // Refine (not really expand) G1 barriers by looking at the new value type
270 // (whether it is necessarily null or necessarily non-null).
271 bool G1BarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
272   ResourceMark rm;
273   VectorSet visited;
274   Node_List worklist;
275   worklist.push(C->root());
276   while (worklist.size() > 0) {
277     Node* n = worklist.pop();
278     if (visited.test_set(n->_idx)) {
279       continue;
280     }
281     refine_barrier_by_new_val_type(n);
282     for (uint j = 0; j < n->req(); j++) {
283       Node* in = n->in(j);
284       if (in != nullptr) {
285         worklist.push(in);
286       }
287     }
288   }
289   return false;
290 }
291 
292 uint G1BarrierSetC2::estimated_barrier_size(const Node* node) const {
293   // These Ideal node counts are extracted from the pre-matching Ideal graph
294   // generated when compiling the following method with early barrier expansion:
295   //   static void write(MyObject obj1, Object o) {
296   //     obj1.o1 = o;
297   //   }
298   uint8_t barrier_data = MemNode::barrier_data(node);
299   uint nodes = 0;
300   if ((barrier_data & G1C2BarrierPre) != 0) {
301     nodes += 50;
302   }
303   if ((barrier_data & G1C2BarrierPost) != 0) {
304     nodes += 60;
305   }
306   return nodes;
307 }
308 
309 bool G1BarrierSetC2::can_initialize_object(const StoreNode* store) const {
310   assert(store->Opcode() == Op_StoreP || store->Opcode() == Op_StoreN, "OOP store expected");
311   // It is OK to move the store across the object initialization boundary only
312   // if it does not have any barrier, or if it has barriers that can be safely
313   // elided (because of the compensation steps taken on the allocation slow path
314   // when ReduceInitialCardMarks is enabled).
315   return (MemNode::barrier_data(store) == 0) || use_ReduceInitialCardMarks();
316 }
317 
318 void G1BarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {
319   if (ac->is_clone_inst() && !use_ReduceInitialCardMarks()) {
320     clone_in_runtime(phase, ac, G1BarrierSetRuntime::clone_addr(), "G1BarrierSetRuntime::clone");
321     return;
322   }
323   BarrierSetC2::clone_at_expansion(phase, ac);
324 }
325 
326 Node* G1BarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
327   DecoratorSet decorators = access.decorators();
328   bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
329   bool in_heap = (decorators & IN_HEAP) != 0;
330   bool tightly_coupled_alloc = (decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0;
331   bool need_store_barrier = !(tightly_coupled_alloc && use_ReduceInitialCardMarks()) && (in_heap || anonymous);
332   bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
333   if (access.is_oop() && need_store_barrier) {
334     access.set_barrier_data(get_store_barrier(access));
335     if (tightly_coupled_alloc) {
336       assert(!use_ReduceInitialCardMarks(),
337              "post-barriers are only needed for tightly-coupled initialization stores when ReduceInitialCardMarks is disabled");
338       // Pre-barriers are unnecessary for tightly-coupled initialization stores.
339       access.set_barrier_data(access.barrier_data() & ~G1C2BarrierPre);
340     }
341   }
342   if (no_keepalive) {
343     // No keep-alive means no need for the pre-barrier.
344     access.set_barrier_data(access.barrier_data() & ~G1C2BarrierPre);
345   }
346   return BarrierSetC2::store_at_resolved(access, val);
347 }
348 
349 Node* G1BarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
350                                                      Node* new_val, const Type* value_type) const {
351   GraphKit* kit = access.kit();
352   if (!access.is_oop()) {
353     return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
354   }
355   access.set_barrier_data(G1C2BarrierPre | G1C2BarrierPost);
356   return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
357 }
358 
359 Node* G1BarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
360                                                       Node* new_val, const Type* value_type) const {
361   GraphKit* kit = access.kit();
362   if (!access.is_oop()) {
363     return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
364   }
365   access.set_barrier_data(G1C2BarrierPre | G1C2BarrierPost);
366   return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
367 }
368 
369 Node* G1BarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
370   GraphKit* kit = access.kit();
371   if (!access.is_oop()) {
372     return BarrierSetC2::atomic_xchg_at_resolved(access, new_val, value_type);
373   }
374   access.set_barrier_data(G1C2BarrierPre | G1C2BarrierPost);
375   return BarrierSetC2::atomic_xchg_at_resolved(access, new_val, value_type);
376 }
377 
378 class G1BarrierSetC2State : public BarrierSetC2State {
379 private:
380   GrowableArray<G1BarrierStubC2*>* _stubs;
381 
382 public:
383   G1BarrierSetC2State(Arena* arena)
384     : BarrierSetC2State(arena),
385       _stubs(new (arena) GrowableArray<G1BarrierStubC2*>(arena, 8,  0, nullptr)) {}
386 
387   GrowableArray<G1BarrierStubC2*>* stubs() {
388     return _stubs;
389   }
390 
391   bool needs_liveness_data(const MachNode* mach) const {
392     return G1PreBarrierStubC2::needs_barrier(mach) ||
393            G1PostBarrierStubC2::needs_barrier(mach);
394   }
395 
396   bool needs_livein_data() const {
397     return false;
398   }
399 };
400 
401 static G1BarrierSetC2State* barrier_set_state() {
402   return reinterpret_cast<G1BarrierSetC2State*>(Compile::current()->barrier_set_state());
403 }
404 
405 G1BarrierStubC2::G1BarrierStubC2(const MachNode* node) : BarrierStubC2(node) {}
406 
407 G1PreBarrierStubC2::G1PreBarrierStubC2(const MachNode* node) : G1BarrierStubC2(node) {}
408 
409 bool G1PreBarrierStubC2::needs_barrier(const MachNode* node) {
410   return (node->barrier_data() & G1C2BarrierPre) != 0;
411 }
412 
413 G1PreBarrierStubC2* G1PreBarrierStubC2::create(const MachNode* node) {
414   G1PreBarrierStubC2* const stub = new (Compile::current()->comp_arena()) G1PreBarrierStubC2(node);
415   if (!Compile::current()->output()->in_scratch_emit_size()) {
416     barrier_set_state()->stubs()->append(stub);
417   }
418   return stub;
419 }
420 
421 void G1PreBarrierStubC2::initialize_registers(Register obj, Register pre_val, Register thread, Register tmp1, Register tmp2) {
422   _obj = obj;
423   _pre_val = pre_val;
424   _thread = thread;
425   _tmp1 = tmp1;
426   _tmp2 = tmp2;
427 }
428 
429 Register G1PreBarrierStubC2::obj() const {
430   return _obj;
431 }
432 
433 Register G1PreBarrierStubC2::pre_val() const {
434   return _pre_val;
435 }
436 
437 Register G1PreBarrierStubC2::thread() const {
438   return _thread;
439 }
440 
441 Register G1PreBarrierStubC2::tmp1() const {
442   return _tmp1;
443 }
444 
445 Register G1PreBarrierStubC2::tmp2() const {
446   return _tmp2;
447 }
448 
449 void G1PreBarrierStubC2::emit_code(MacroAssembler& masm) {
450   G1BarrierSetAssembler* bs = static_cast<G1BarrierSetAssembler*>(BarrierSet::barrier_set()->barrier_set_assembler());
451   bs->generate_c2_pre_barrier_stub(&masm, this);
452 }
453 
454 G1PostBarrierStubC2::G1PostBarrierStubC2(const MachNode* node) : G1BarrierStubC2(node) {}
455 
456 bool G1PostBarrierStubC2::needs_barrier(const MachNode* node) {
457   return (node->barrier_data() & G1C2BarrierPost) != 0;
458 }
459 
460 G1PostBarrierStubC2* G1PostBarrierStubC2::create(const MachNode* node) {
461   G1PostBarrierStubC2* const stub = new (Compile::current()->comp_arena()) G1PostBarrierStubC2(node);
462   if (!Compile::current()->output()->in_scratch_emit_size()) {
463     barrier_set_state()->stubs()->append(stub);
464   }
465   return stub;
466 }
467 
468 void G1PostBarrierStubC2::initialize_registers(Register thread, Register tmp1, Register tmp2, Register tmp3) {
469   _thread = thread;
470   _tmp1 = tmp1;
471   _tmp2 = tmp2;
472   _tmp3 = tmp3;
473 }
474 
475 Register G1PostBarrierStubC2::thread() const {
476   return _thread;
477 }
478 
479 Register G1PostBarrierStubC2::tmp1() const {
480   return _tmp1;
481 }
482 
483 Register G1PostBarrierStubC2::tmp2() const {
484   return _tmp2;
485 }
486 
487 Register G1PostBarrierStubC2::tmp3() const {
488   return _tmp3;
489 }
490 
491 void G1PostBarrierStubC2::emit_code(MacroAssembler& masm) {
492   G1BarrierSetAssembler* bs = static_cast<G1BarrierSetAssembler*>(BarrierSet::barrier_set()->barrier_set_assembler());
493   bs->generate_c2_post_barrier_stub(&masm, this);
494 }
495 
496 void* G1BarrierSetC2::create_barrier_state(Arena* comp_arena) const {
497   return new (comp_arena) G1BarrierSetC2State(comp_arena);
498 }
499 
500 int G1BarrierSetC2::get_store_barrier(C2Access& access) const {
501   if (!access.is_parse_access()) {
502     // Only support for eliding barriers at parse time for now.
503     return G1C2BarrierPre | G1C2BarrierPost;
504   }
505   GraphKit* kit = (static_cast<C2ParseAccess&>(access)).kit();
506   Node* ctl = kit->control();
507   Node* adr = access.addr().node();
508   uint adr_idx = kit->C->get_alias_index(access.addr().type());
509   assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory");
510 
511   bool can_remove_pre_barrier = g1_can_remove_pre_barrier(kit, &kit->gvn(), adr, access.type(), adr_idx);
512 
513   // We can skip marks on a freshly-allocated object in Eden. Keep this code in
514   // sync with CardTableBarrierSet::on_slowpath_allocation_exit. That routine
515   // informs GC to take appropriate compensating steps, upon a slow-path
516   // allocation, so as to make this card-mark elision safe.
517   // The post-barrier can also be removed if null is written. This case is
518   // handled by G1BarrierSetC2::expand_barriers, which runs at the end of C2's
519   // platform-independent optimizations to exploit stronger type information.
520   bool can_remove_post_barrier = use_ReduceInitialCardMarks() &&
521     ((access.base() == kit->just_allocated_object(ctl)) ||
522      g1_can_remove_post_barrier(kit, &kit->gvn(), ctl, adr));
523 
524   int barriers = 0;
525   if (!can_remove_pre_barrier) {
526     barriers |= G1C2BarrierPre;
527   }
528   if (!can_remove_post_barrier) {
529     barriers |= G1C2BarrierPost;
530   }
531 
532   return barriers;
533 }
534 
535 void G1BarrierSetC2::elide_dominated_barrier(MachNode* mach) const {
536   uint8_t barrier_data = mach->barrier_data();
537   barrier_data &= ~G1C2BarrierPre;
538   if (CardTableBarrierSetC2::use_ReduceInitialCardMarks()) {
539     barrier_data &= ~G1C2BarrierPost;
540     barrier_data &= ~G1C2BarrierPostNotNull;
541   }
542   mach->set_barrier_data(barrier_data);
543 }
544 
545 void G1BarrierSetC2::analyze_dominating_barriers() const {
546   ResourceMark rm;
547   PhaseCFG* const cfg = Compile::current()->cfg();
548 
549   // Find allocations and memory accesses (stores and atomic operations), and
550   // track them in lists.
551   Node_List accesses;
552   Node_List allocations;
553   for (uint i = 0; i < cfg->number_of_blocks(); ++i) {
554     const Block* const block = cfg->get_block(i);
555     for (uint j = 0; j < block->number_of_nodes(); ++j) {
556       Node* const node = block->get_node(j);
557       if (node->is_Phi()) {
558         if (BarrierSetC2::is_allocation(node)) {
559           allocations.push(node);
560         }
561         continue;
562       } else if (!node->is_Mach()) {
563         continue;
564       }
565 
566       MachNode* const mach = node->as_Mach();
567       switch (mach->ideal_Opcode()) {
568       case Op_StoreP:
569       case Op_StoreN:
570       case Op_CompareAndExchangeP:
571       case Op_CompareAndSwapP:
572       case Op_GetAndSetP:
573       case Op_CompareAndExchangeN:
574       case Op_CompareAndSwapN:
575       case Op_GetAndSetN:
576         if (mach->barrier_data() != 0) {
577           accesses.push(mach);
578         }
579         break;
580       default:
581         break;
582       }
583     }
584   }
585 
586   // Find dominating allocations for each memory access (store or atomic
587   // operation) and elide barriers if there is no safepoint poll in between.
588   elide_dominated_barriers(accesses, allocations);
589 }
590 
591 void G1BarrierSetC2::late_barrier_analysis() const {
592   compute_liveness_at_stubs();
593   analyze_dominating_barriers();
594 }
595 
596 void G1BarrierSetC2::emit_stubs(CodeBuffer& cb) const {
597   MacroAssembler masm(&cb);
598   GrowableArray<G1BarrierStubC2*>* const stubs = barrier_set_state()->stubs();
599   for (int i = 0; i < stubs->length(); i++) {
600     // Make sure there is enough space in the code buffer
601     if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::max_inst_gcstub_size()) && cb.blob() == nullptr) {
602       ciEnv::current()->record_failure("CodeCache is full");
603       return;
604     }
605     stubs->at(i)->emit_code(masm);
606   }
607   masm.flush();
608 }
609 
610 #ifndef PRODUCT
611 void G1BarrierSetC2::dump_barrier_data(const MachNode* mach, outputStream* st) const {
612   if ((mach->barrier_data() & G1C2BarrierPre) != 0) {
613     st->print("pre ");
614   }
615   if ((mach->barrier_data() & G1C2BarrierPost) != 0) {
616     st->print("post ");
617   }
618   if ((mach->barrier_data() & G1C2BarrierPostNotNull) != 0) {
619     st->print("notnull ");
620   }
621 }
622 #endif // !PRODUCT