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 "code/aotCodeCache.hpp" 27 #include "gc/shared/c2/cardTableBarrierSetC2.hpp" 28 #include "gc/shared/cardTable.hpp" 29 #include "gc/shared/cardTableBarrierSet.hpp" 30 #include "gc/shared/gc_globals.hpp" 31 #include "opto/arraycopynode.hpp" 32 #include "opto/graphKit.hpp" 33 #include "opto/idealKit.hpp" 34 #include "opto/macro.hpp" 35 #include "utilities/macros.hpp" 36 37 #define __ ideal. 38 39 Node* CardTableBarrierSetC2::byte_map_base_node(IdealKit* kit) const { 40 // Get base of card map 41 #if INCLUDE_CDS 42 if (AOTCodeCache::is_on_for_dump()) { 43 // load the card table address from the AOT Runtime Constants area 44 Node* byte_map_base_adr = kit->makecon(TypeRawPtr::make(AOTRuntimeConstants::card_table_address())); 45 return kit->load_aot_const(byte_map_base_adr, TypeRawPtr::NOTNULL); 46 } 47 #endif 48 CardTable::CardValue* card_table_base = ci_card_table_address(); 49 if (card_table_base != nullptr) { 50 return kit->makecon(TypeRawPtr::make((address)card_table_base)); 51 } else { 52 return kit->makecon(Type::get_zero_type(T_ADDRESS)); 53 } 54 } 55 56 // vanilla post barrier 57 // Insert a write-barrier store. This is to let generational GC work; we have 58 // to flag all oop-stores before the next GC point. 59 void CardTableBarrierSetC2::post_barrier(GraphKit* kit, 60 Node* obj, 61 Node* adr, 62 Node* val, 63 bool use_precise) const { 64 // No store check needed if we're storing a null. 65 if (val != nullptr && val->is_Con()) { 66 const Type* t = val->bottom_type(); 67 if (t == TypePtr::NULL_PTR || t == Type::TOP) { 68 return; 69 } 70 } 71 72 if (use_ReduceInitialCardMarks() 73 && obj == kit->just_allocated_object(kit->control())) { 74 // We can skip marks on a freshly-allocated object in Eden. 75 // Keep this code in sync with CardTableBarrierSet::on_slowpath_allocation_exit. 76 // That routine informs GC to take appropriate compensating steps, 77 // upon a slow-path allocation, so as to make this card-mark 78 // elision safe. 79 return; 80 } 81 82 if (!use_precise) { 83 // All card marks for a (non-array) instance are in one place: 84 adr = obj; 85 } else { 86 // Else it's an array (or unknown), and we want more precise card marks. 87 } 88 89 assert(adr != nullptr, ""); 90 91 IdealKit ideal(kit, true); 92 93 // Convert the pointer to an int prior to doing math on it 94 Node* cast = __ CastPX(__ ctrl(), adr); 95 96 // Divide by card size 97 Node* card_offset = __ URShiftX(cast, __ ConI(CardTable::card_shift())); 98 99 // Combine card table base and card offset 100 Node* card_adr = __ AddP(__ top(), byte_map_base_node(&ideal), card_offset); 101 102 // Get the alias_index for raw card-mark memory 103 int adr_type = Compile::AliasIdxRaw; 104 105 // Dirty card value to store 106 Node* dirty = __ ConI(CardTable::dirty_card_val()); 107 108 if (UseCondCardMark) { 109 // The classic GC reference write barrier is typically implemented 110 // as a store into the global card mark table. Unfortunately 111 // unconditional stores can result in false sharing and excessive 112 // coherence traffic as well as false transactional aborts. 113 // UseCondCardMark enables MP "polite" conditional card mark 114 // stores. In theory we could relax the load from ctrl() to 115 // no_ctrl, but that doesn't buy much latitude. 116 Node* card_val = __ load( __ ctrl(), card_adr, TypeInt::BYTE, T_BYTE, adr_type); 117 __ if_then(card_val, BoolTest::ne, dirty); 118 } 119 120 // Smash dirty value into card 121 __ store(__ ctrl(), card_adr, dirty, T_BYTE, adr_type, MemNode::unordered); 122 123 if (UseCondCardMark) { 124 __ end_if(); 125 } 126 127 // Final sync IdealKit and GraphKit. 128 kit->final_sync(ideal); 129 } 130 131 bool CardTableBarrierSetC2::use_ReduceInitialCardMarks() { 132 return ReduceInitialCardMarks; 133 } 134 135 void CardTableBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { 136 assert(node->Opcode() == Op_CastP2X, "ConvP2XNode required"); 137 Node *shift = node->unique_out(); 138 Node *addp = shift->unique_out(); 139 for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) { 140 Node *mem = addp->last_out(j); 141 if (UseCondCardMark && mem->is_Load()) { 142 assert(mem->Opcode() == Op_LoadB, "unexpected code shape"); 143 // The load is checking if the card has been written so 144 // replace it with zero to fold the test. 145 macro->replace_node(mem, macro->intcon(0)); 146 continue; 147 } 148 assert(mem->is_Store(), "store required"); 149 macro->replace_node(mem, mem->in(MemNode::Memory)); 150 } 151 } 152 153 bool CardTableBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const { 154 bool is_oop = is_reference_type(type); 155 return is_oop && (!tightly_coupled_alloc || !use_ReduceInitialCardMarks()); 156 }