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 "code/aotCodeCache.hpp"
 26 #include "gc/shared/c1/cardTableBarrierSetC1.hpp"
 27 #include "gc/shared/cardTable.hpp"
 28 #include "gc/shared/cardTableBarrierSet.hpp"
 29 #include "gc/shared/gc_globals.hpp"
 30 #include "utilities/macros.hpp"
 31 
 32 #ifdef ASSERT
 33 #define __ gen->lir(__FILE__, __LINE__)->
 34 #else
 35 #define __ gen->lir()->
 36 #endif
 37 
 38 void CardTableBarrierSetC1::post_barrier(LIRAccess& access, LIR_Opr addr, LIR_Opr new_val) {
 39   DecoratorSet decorators = access.decorators();
 40   LIRGenerator* gen = access.gen();
 41   bool in_heap = (decorators & IN_HEAP) != 0;
 42   if (!in_heap) {
 43     return;
 44   }
 45 
 46   BarrierSet* bs = BarrierSet::barrier_set();
 47   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
 48   CardTable* ct = ctbs->card_table();
 49   LIR_Const* card_table_base = new LIR_Const(ct->byte_map_base());
 50   SHENANDOAHGC_ONLY(assert(!UseShenandoahGC, "Shenandoah byte_map_base is not constant.");)
 51 
 52   if (addr->is_address()) {
 53     LIR_Address* address = addr->as_address_ptr();
 54     // ptr cannot be an object because we use this barrier for array card marks
 55     // and addr can point in the middle of an array.
 56     LIR_Opr ptr = gen->new_pointer_register();
 57     if (!address->index()->is_valid() && address->disp() == 0) {
 58       __ move(address->base(), ptr);
 59     } else {
 60       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
 61       __ leal(addr, ptr);
 62     }
 63     addr = ptr;
 64   }
 65   assert(addr->is_register(), "must be a register at this point");
 66 
 67 #ifdef CARDTABLEBARRIERSET_POST_BARRIER_HELPER
 68   assert(!AOTCodeCache::is_on(), "this path is not implemented");
 69   gen->CardTableBarrierSet_post_barrier_helper(addr, card_table_base);
 70 #else
 71   LIR_Opr tmp = gen->new_pointer_register();
 72   if (two_operand_lir_form) {
 73     LIR_Opr addr_opr = LIR_OprFact::address(new LIR_Address(addr, addr->type()));
 74     __ leal(addr_opr, tmp);
 75     __ unsigned_shift_right(tmp, CardTable::card_shift(), tmp);
 76   } else {
 77     __ unsigned_shift_right(addr, CardTable::card_shift(), tmp);
 78   }
 79 
 80   LIR_Address* card_addr;
 81 #if INCLUDE_CDS
 82   if (AOTCodeCache::is_on_for_dump()) {
 83     // load the card table address from the AOT Runtime Constants area
 84     LIR_Opr byte_map_base_adr = LIR_OprFact::intptrConst(AOTRuntimeConstants::card_table_address());
 85     LIR_Opr byte_map_base_reg = gen->new_pointer_register();
 86     __ move(byte_map_base_adr, byte_map_base_reg);
 87     LIR_Address* byte_map_base_indirect = new LIR_Address(byte_map_base_reg, 0, T_LONG);
 88     __ move(byte_map_base_indirect, byte_map_base_reg);
 89     card_addr = new LIR_Address(tmp, byte_map_base_reg, T_BYTE);
 90   } else
 91 #endif
 92   if (gen->can_inline_as_constant(card_table_base)) {
 93     card_addr = new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE);
 94   } else {
 95     card_addr = new LIR_Address(tmp, gen->load_constant(card_table_base), T_BYTE);
 96   }
 97 
 98   LIR_Opr dirty = LIR_OprFact::intConst(CardTable::dirty_card_val());
 99   if (UseCondCardMark) {
100     LIR_Opr cur_value = gen->new_register(T_INT);
101     __ move(card_addr, cur_value);
102 
103     LabelObj* L_already_dirty = new LabelObj();
104     __ cmp(lir_cond_equal, cur_value, dirty);
105     __ branch(lir_cond_equal, L_already_dirty->label());
106     __ move(dirty, card_addr);
107     __ branch_destination(L_already_dirty->label());
108   } else {
109     __ move(dirty, card_addr);
110   }
111 #endif
112 }