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