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