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 "ci/ciUtilities.hpp"
 26 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
 27 #include "gc/shared/cardTable.hpp"
 28 #include "gc/shared/cardTableBarrierSet.hpp"
 29 #include "gc/shared/gc_globals.hpp"
 30 #include "opto/arraycopynode.hpp"
 31 #include "opto/graphKit.hpp"
 32 #include "opto/idealKit.hpp"
 33 #include "opto/macro.hpp"
 34 #include "utilities/macros.hpp"
 35 
 36 #define __ ideal.
 37 
 38 Node* CardTableBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
 39   DecoratorSet decorators = access.decorators();
 40 
 41   Node* adr = access.addr().node();
 42 
 43   bool is_array = (decorators & IS_ARRAY) != 0;
 44   bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 45   bool in_heap = (decorators & IN_HEAP) != 0;
 46   bool use_precise = is_array || anonymous;
 47   bool tightly_coupled_alloc = (decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0;
 48 
 49   if (!access.is_oop() || tightly_coupled_alloc || (!in_heap && !anonymous)) {
 50     return BarrierSetC2::store_at_resolved(access, val);
 51   }
 52 
 53   assert(access.is_parse_access(), "entry not supported at optimization time");
 54   C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
 55 
 56   Node* store = BarrierSetC2::store_at_resolved(access, val);
 57   post_barrier(parse_access.kit(), access.base(), adr, val.node(), use_precise);
 58 
 59   return store;
 60 }
 61 
 62 Node* CardTableBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
 63                                                             Node* new_val, const Type* value_type) const {
 64   if (!access.is_oop()) {
 65     return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
 66   }
 67 
 68   Node* result = BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
 69 
 70   post_barrier(access.kit(), access.base(), access.addr().node(), new_val, true);
 71 
 72   return result;
 73 }
 74 
 75 Node* CardTableBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
 76                                                              Node* new_val, const Type* value_type) const {
 77   GraphKit* kit = access.kit();
 78 
 79   if (!access.is_oop()) {
 80     return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
 81   }
 82 
 83   Node* load_store = BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
 84 
 85   // Emit the post barrier only when the actual store happened. This makes sense
 86   // to check only for LS_cmp_* that can fail to set the value.
 87   // LS_cmp_exchange does not produce any branches by default, so there is no
 88   // boolean result to piggyback on. TODO: When we merge CompareAndSwap with
 89   // CompareAndExchange and move branches here, it would make sense to conditionalize
 90   // post_barriers for LS_cmp_exchange as well.
 91   //
 92   // CAS success path is marked more likely since we anticipate this is a performance
 93   // critical path, while CAS failure path can use the penalty for going through unlikely
 94   // path as backoff. Which is still better than doing a store barrier there.
 95   IdealKit ideal(kit);
 96   ideal.if_then(load_store, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
 97     kit->sync_kit(ideal);
 98     post_barrier(kit, access.base(), access.addr().node(), new_val, true);
 99     ideal.sync_kit(kit);
100   } ideal.end_if();
101   kit->final_sync(ideal);
102 
103   return load_store;
104 }
105 
106 Node* CardTableBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
107   Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, new_val, value_type);
108   if (!access.is_oop()) {
109     return result;
110   }
111 
112   post_barrier(access.kit(), access.base(), access.addr().node(), new_val, true);
113 
114   return result;
115 }
116 
117 Node* CardTableBarrierSetC2::byte_map_base_node(GraphKit* kit) const {
118   // Get base of card map
119   CardTable::CardValue* card_table_base = ci_card_table_address();
120    if (card_table_base != nullptr) {
121      return kit->makecon(TypeRawPtr::make((address)card_table_base));
122    } else {
123      return kit->null();
124    }
125 }
126 
127 // vanilla post barrier
128 // Insert a write-barrier store.  This is to let generational GC work; we have
129 // to flag all oop-stores before the next GC point.
130 void CardTableBarrierSetC2::post_barrier(GraphKit* kit,
131                                          Node* obj,
132                                          Node* adr,
133                                          Node* val,
134                                          bool use_precise) const {
135   // No store check needed if we're storing a null.
136   if (val != nullptr && val->is_Con()) {
137     const Type* t = val->bottom_type();
138     if (t == TypePtr::NULL_PTR || t == Type::TOP) {
139       return;
140     }
141   }
142 
143   if (use_ReduceInitialCardMarks()
144       && obj == kit->just_allocated_object(kit->control())) {
145     // We can skip marks on a freshly-allocated object in Eden.
146     // Keep this code in sync with CardTableBarrierSet::on_slowpath_allocation_exit.
147     // That routine informs GC to take appropriate compensating steps,
148     // upon a slow-path allocation, so as to make this card-mark
149     // elision safe.
150     return;
151   }
152 
153   if (!use_precise) {
154     // All card marks for a (non-array) instance are in one place:
155     adr = obj;
156   } else {
157     // Else it's an array (or unknown), and we want more precise card marks.
158   }
159 
160   assert(adr != nullptr, "");
161 
162   IdealKit ideal(kit, true);
163 
164   // Convert the pointer to an int prior to doing math on it
165   Node* cast = __ CastPX(__ ctrl(), adr);
166 
167   // Divide by card size
168   Node* card_offset = __ URShiftX(cast, __ ConI(CardTable::card_shift()));
169 
170   // Combine card table base and card offset
171   Node* card_adr = __ AddP(__ top(), byte_map_base_node(kit), card_offset);
172 
173   // Get the alias_index for raw card-mark memory
174   int adr_type = Compile::AliasIdxRaw;
175 
176   // Dirty card value to store
177   Node* dirty = __ ConI(CardTable::dirty_card_val());
178 
179   if (UseCondCardMark) {
180     // The classic GC reference write barrier is typically implemented
181     // as a store into the global card mark table.  Unfortunately
182     // unconditional stores can result in false sharing and excessive
183     // coherence traffic as well as false transactional aborts.
184     // UseCondCardMark enables MP "polite" conditional card mark
185     // stores.  In theory we could relax the load from ctrl() to
186     // no_ctrl, but that doesn't buy much latitude.
187     Node* card_val = __ load( __ ctrl(), card_adr, TypeInt::BYTE, T_BYTE, adr_type);
188     __ if_then(card_val, BoolTest::ne, dirty);
189   }
190 
191   // Smash dirty value into card
192   __ store(__ ctrl(), card_adr, dirty, T_BYTE, adr_type, MemNode::unordered);
193 
194   if (UseCondCardMark) {
195     __ end_if();
196   }
197 
198   // Final sync IdealKit and GraphKit.
199   kit->final_sync(ideal);
200 }
201 
202 bool CardTableBarrierSetC2::use_ReduceInitialCardMarks() {
203   return ReduceInitialCardMarks;
204 }
205 
206 void CardTableBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
207   assert(node->Opcode() == Op_CastP2X, "ConvP2XNode required");
208   Node *shift = node->unique_out();
209   Node *addp = shift->unique_out();
210   for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
211     Node *mem = addp->last_out(j);
212     if (UseCondCardMark && mem->is_Load()) {
213       assert(mem->Opcode() == Op_LoadB, "unexpected code shape");
214       // The load is checking if the card has been written so
215       // replace it with zero to fold the test.
216       macro->replace_node(mem, macro->intcon(0));
217       continue;
218     }
219     assert(mem->is_Store(), "store required");
220     macro->replace_node(mem, mem->in(MemNode::Memory));
221   }
222 }
223 
224 bool CardTableBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
225   bool is_oop = is_reference_type(type);
226   return is_oop && (!tightly_coupled_alloc || !use_ReduceInitialCardMarks());
227 }