1 /*
  2  * Copyright (c) 2018, 2021, 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 != NULL) {
 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 != NULL && 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 != NULL, "");
 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 void CardTableBarrierSetC2::clone(GraphKit* kit, Node* src, Node* dst, Node* size, bool is_array) const {
129   BarrierSetC2::clone(kit, src, dst, size, is_array);
130   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
131 
132   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
133   bool card_mark = !is_array && !use_ReduceInitialCardMarks();
134   if (card_mark) {
135     assert(!is_array, "");
136     // Put in store barrier for any and all oops we are sticking
137     // into this object.  (We could avoid this if we could prove
138     // that the object type contains no oop fields at all.)
139     Node* no_particular_value = NULL;
140     Node* no_particular_field = NULL;
141     int raw_adr_idx = Compile::AliasIdxRaw;
142     post_barrier(kit, kit->control(),
143                  kit->memory(raw_adr_type),
144                  dst,
145                  no_particular_field,
146                  raw_adr_idx,
147                  no_particular_value,
148                  T_OBJECT,
149                  false);
150   }
151 }
152 
153 bool CardTableBarrierSetC2::use_ReduceInitialCardMarks() const {
154   return ReduceInitialCardMarks;
155 }
156 
157 bool CardTableBarrierSetC2::is_gc_barrier_node(Node* node) const {
158   return ModRefBarrierSetC2::is_gc_barrier_node(node) || node->Opcode() == Op_StoreCM;
159 }
160 
161 void CardTableBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
162   assert(node->Opcode() == Op_CastP2X, "ConvP2XNode required");
163   Node *shift = node->unique_out();
164   Node *addp = shift->unique_out();
165   for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
166     Node *mem = addp->last_out(j);
167     if (UseCondCardMark && mem->is_Load()) {
168       assert(mem->Opcode() == Op_LoadB, "unexpected code shape");
169       // The load is checking if the card has been written so
170       // replace it with zero to fold the test.
171       macro->replace_node(mem, macro->intcon(0));
172       continue;
173     }
174     assert(mem->is_Store(), "store required");
175     macro->replace_node(mem, mem->in(MemNode::Memory));
176   }
177 }
178 
179 bool CardTableBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
180   bool is_oop = is_reference_type(type);
181   return is_oop && (!tightly_coupled_alloc || !use_ReduceInitialCardMarks());
182 }