1 /*
  2  * Copyright (c) 2018, 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 "gc/shared/c1/cardTableBarrierSetC1.hpp"
 27 #include "gc/shared/cardTableBarrierSet.hpp"
 28 #include "utilities/macros.hpp"
 29 
 30 #ifdef ASSERT
 31 #define __ lir_generator->lir(__FILE__, __LINE__)->
 32 #else
 33 #define __ lir_generator->lir()->
 34 #endif
 35 
 36 void CardTableBarrierSetC1::post_barrier(LIRGenerator *lir_generator, DecoratorSet decorators,
 37                                          LIR_OprDesc* addr, LIR_OprDesc* new_val) {
 38   bool in_heap = (decorators & IN_HEAP) != 0;
 39   if (!in_heap) {
 40     return;
 41   }
 42 
 43   BarrierSet* bs = BarrierSet::barrier_set();
 44   CardTableBarrierSet* ctbs = barrier_set_cast<CardTableBarrierSet>(bs);
 45   CardTable* ct = ctbs->card_table();
 46   assert(sizeof(*(ct->byte_map_base())) == sizeof(jbyte), "adjust this code");
 47   LIR_Const* card_table_base = new LIR_Const(ct->byte_map_base());
 48   if (addr->is_address()) {
 49     LIR_Address* address = addr->as_address_ptr();
 50     // ptr cannot be an object because we use this barrier for array card marks
 51     // and addr can point in the middle of an array.
 52     LIR_Opr ptr = lir_generator->new_pointer_register();
 53     if (!address->index()->is_valid() && address->disp() == 0) {
 54       __ move(address->base(), ptr);
 55     } else {
 56       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
 57       __ leal(addr, ptr);
 58     }
 59     addr = ptr;
 60   }
 61   assert(addr->is_register(), "must be a register at this point");
 62 
 63 #ifdef CARDTABLEBARRIERSET_POST_BARRIER_HELPER
 64   CardTableBarrierSet_post_barrier_helper(addr, card_table_base);
 65 #else
 66   LIR_Opr tmp = lir_generator->new_pointer_register();
 67   if (TwoOperandLIRForm) {
 68     __ move(addr, tmp);
 69     __ unsigned_shift_right(tmp, CardTable::card_shift, tmp);
 70   } else {
 71     __ unsigned_shift_right(addr, CardTable::card_shift, tmp);
 72   }
 73 
 74   LIR_Address* card_addr;
 75   if (lir_generator->can_inline_as_constant(card_table_base)) {
 76     card_addr = new LIR_Address(tmp, card_table_base->as_jint(), T_BYTE);
 77   } else {
 78     card_addr = new LIR_Address(tmp, lir_generator->load_constant(card_table_base), T_BYTE);
 79   }
 80 
 81   LIR_Opr dirty = LIR_OprFact::intConst(CardTable::dirty_card_val());
 82   if (UseCondCardMark) {
 83     LIR_Opr cur_value = lir_generator->new_register(T_INT);
 84     if (ct->scanned_concurrently()) {
 85       __ membar_storeload();
 86     }
 87     __ move(card_addr, cur_value);
 88 
 89     LabelObj* L_already_dirty = new LabelObj();
 90     __ cmp(lir_cond_equal, cur_value, dirty);
 91     __ branch(lir_cond_equal, T_BYTE, L_already_dirty->label());
 92     __ move(dirty, card_addr);
 93     __ branch_destination(L_already_dirty->label());
 94   } else {
 95     if (ct->scanned_concurrently()) {
 96       __ membar_storestore();
 97     }
 98     __ move(dirty, card_addr);
 99   }
100 #endif
101 }