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 "c1/c1_LIRGenerator.hpp"
  27 #include "c1/c1_CodeStubs.hpp"
  28 #include "gc/g1/g1BarrierSet.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "gc/g1/c1G1BSCodeGen.hpp"
  31 #include "gc/g1/g1BSCodeGen.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 #ifdef ASSERT
  35 #define __ lir_generator->lir(__FILE__, __LINE__)->
  36 #else
  37 #define __ lir_generator->lir()->
  38 #endif
  39 
  40 void G1PreBarrierStub::emit_code(LIR_Assembler* ce) {
  41   G1BSCodeGen *code_gen = (G1BSCodeGen*)Universe::heap()->barrier_set()->code_gen();
  42   code_gen->gen_g1_pre_barrier_stub(ce, this);
  43 }
  44 
  45 void G1PostBarrierStub::emit_code(LIR_Assembler* ce) {
  46   G1BSCodeGen *code_gen = (G1BSCodeGen*)Universe::heap()->barrier_set()->code_gen();
  47   code_gen->gen_g1_post_barrier_stub(ce, this);
  48 }
  49 
  50 void C1G1BSCodeGen::pre_barrier(LIRGenerator *lir_generator, C1DecoratorSet decorators,
  51                                 LIR_Opr addr_opr, LIR_Opr pre_val, CodeEmitInfo* info) {
  52   // First we test whether marking is in progress.
  53   BasicType flag_type;
  54   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
  55   bool do_load = pre_val == LIR_OprFact::illegalOpr;
  56   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
  57     flag_type = T_INT;
  58   } else {
  59     guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1,
  60               "Assumption");
  61     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
  62     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
  63     flag_type = T_BOOLEAN;
  64   }
  65   LIR_Opr thrd = lir_generator->getThreadPointer();
  66   LIR_Address* mark_active_flag_addr =
  67     new LIR_Address(thrd,
  68                     in_bytes(JavaThread::satb_mark_queue_offset() +
  69                              SATBMarkQueue::byte_offset_of_active()),
  70                     flag_type);
  71   // Read the marking-in-progress flag.
  72   LIR_Opr flag_val = lir_generator->new_register(T_INT);
  73   __ load(mark_active_flag_addr, flag_val);
  74   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
  75 
  76   LIR_PatchCode pre_val_patch_code = lir_patch_none;
  77 
  78   CodeStub* slow;
  79 
  80   if (do_load) {
  81     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
  82     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
  83 
  84     if (patch)
  85       pre_val_patch_code = lir_patch_normal;
  86 
  87     pre_val = lir_generator->new_register(T_OBJECT);
  88 
  89     if (!addr_opr->is_address()) {
  90       assert(addr_opr->is_register(), "must be");
  91       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
  92     }
  93     slow = new G1PreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info);
  94   } else {
  95     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
  96     assert(pre_val->is_register(), "must be");
  97     assert(pre_val->type() == T_OBJECT, "must be an object");
  98     assert(info == NULL, "sanity");
  99 
 100     slow = new G1PreBarrierStub(pre_val);
 101   }
 102 
 103   __ branch(lir_cond_notEqual, T_INT, slow);
 104   __ branch_destination(slow->continuation());
 105 }
 106 
 107 void C1G1BSCodeGen::post_barrier(LIRGenerator *lir_generator, C1DecoratorSet decorators,
 108                                  LIR_OprDesc* addr, LIR_OprDesc* new_val) {
 109   // If the "new_val" is a constant NULL, no barrier is necessary.
 110   if (new_val->is_constant() &&
 111       new_val->as_constant_ptr()->as_jobject() == NULL) return;
 112 
 113   if (!new_val->is_register()) {
 114     LIR_Opr new_val_reg = lir_generator->new_register(T_OBJECT);
 115     if (new_val->is_constant()) {
 116       __ move(new_val, new_val_reg);
 117     } else {
 118       __ leal(new_val, new_val_reg);
 119     }
 120     new_val = new_val_reg;
 121   }
 122   assert(new_val->is_register(), "must be a register at this point");
 123 
 124   if (addr->is_address()) {
 125     LIR_Address* address = addr->as_address_ptr();
 126     LIR_Opr ptr = lir_generator->new_pointer_register();
 127     if (!address->index()->is_valid() && address->disp() == 0) {
 128       __ move(address->base(), ptr);
 129     } else {
 130       assert(address->disp() != max_jint, "lea doesn't support patched addresses!");
 131       __ leal(addr, ptr);
 132     }
 133     addr = ptr;
 134   }
 135   assert(addr->is_register(), "must be a register at this point");
 136 
 137   LIR_Opr xor_res = lir_generator->new_pointer_register();
 138   LIR_Opr xor_shift_res = lir_generator->new_pointer_register();
 139   if (TwoOperandLIRForm) {
 140     __ move(addr, xor_res);
 141     __ logical_xor(xor_res, new_val, xor_res);
 142     __ move(xor_res, xor_shift_res);
 143     __ unsigned_shift_right(xor_shift_res,
 144                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
 145                             xor_shift_res,
 146                             LIR_OprDesc::illegalOpr());
 147   } else {
 148     __ logical_xor(addr, new_val, xor_res);
 149     __ unsigned_shift_right(xor_res,
 150                             LIR_OprFact::intConst(HeapRegion::LogOfHRGrainBytes),
 151                             xor_shift_res,
 152                             LIR_OprDesc::illegalOpr());
 153   }
 154 
 155   if (!new_val->is_register()) {
 156     LIR_Opr new_val_reg = lir_generator->new_register(T_OBJECT);
 157     __ leal(new_val, new_val_reg);
 158     new_val = new_val_reg;
 159   }
 160   assert(new_val->is_register(), "must be a register at this point");
 161 
 162   __ cmp(lir_cond_notEqual, xor_shift_res, LIR_OprFact::intptrConst(NULL_WORD));
 163 
 164   CodeStub* slow = new G1PostBarrierStub(addr, new_val);
 165   __ branch(lir_cond_notEqual, LP64_ONLY(T_LONG) NOT_LP64(T_INT), slow);
 166   __ branch_destination(slow->continuation());
 167 }
 168 
 169 LIR_Opr C1G1BSCodeGen::load_at_resolved(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type,
 170                                         LIR_Opr addr, LIRItem& base, LIR_Opr offset,
 171                                         CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) {
 172   bool is_oop = type == T_OBJECT || type == T_ARRAY;
 173   bool is_weak = (decorators & C1_ACCESS_ON_WEAK) != 0;
 174   bool is_anonymous = (decorators & C1_ACCESS_ON_ANONYMOUS) != 0;
 175 
 176   LIR_Opr result = C1BarrierSetCodeGen::load_at_resolved(lir_generator, decorators, type, addr, base, offset, patch_info, load_emit_info);
 177 
 178   if (is_oop && (is_weak || is_anonymous)) {
 179     // Register the value in the referent field with the pre-barrier
 180     LabelObj *Lcont_anonymous;
 181     if (is_anonymous) {
 182       Lcont_anonymous = new LabelObj();
 183       generate_referent_check(lir_generator, base, offset, Lcont_anonymous);
 184     }
 185     pre_barrier(lir_generator, decorators, LIR_OprFact::illegalOpr /* addr_opr */, result /* pre_val */, patch_info /* info */);
 186     if (is_anonymous) {
 187       __ branch_destination(Lcont_anonymous->label());
 188     }
 189   }
 190 
 191   return result;
 192 }
 193 
 194 class C1G1PreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 195   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 196     G1BSCodeGen *code_gen = (G1BSCodeGen*)Universe::heap()->barrier_set()->code_gen();
 197     code_gen->generate_c1_pre_barrier_runtime_stub(sasm);
 198     return NULL;
 199   }
 200 };
 201 
 202 class C1G1PostBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 203   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 204     G1BSCodeGen *code_gen = (G1BSCodeGen*)Universe::heap()->barrier_set()->code_gen();
 205     code_gen->generate_c1_post_barrier_runtime_stub(sasm);
 206     return NULL;
 207   }
 208 };
 209 
 210 void C1G1BSCodeGen::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
 211   C1G1PreBarrierCodeGenClosure pre_code_gen_cl;
 212   C1G1PostBarrierCodeGenClosure post_code_gen_cl;
 213   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1, "g1_pre_barrier_slow", false, &pre_code_gen_cl);
 214   _post_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1, "g1_post_barrier_slow", false, &post_code_gen_cl);
 215 }