1 /*
   2  * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "c1/c1_IR.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  30 #include "gc/shenandoah/shenandoahSATBMarkQueue.hpp"
  31 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
  32 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  33 
  34 #ifdef ASSERT
  35 #define __ gen->lir(__FILE__, __LINE__)->
  36 #else
  37 #define __ gen->lir()->
  38 #endif
  39 
  40 void ShenandoahPreBarrierStub::emit_code(LIR_Assembler* ce) {
  41   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  42   bs->gen_pre_barrier_stub(ce, this);
  43 }
  44 
  45 void ShenandoahLoadReferenceBarrierStub::emit_code(LIR_Assembler* ce) {
  46   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  47   bs->gen_load_reference_barrier_stub(ce, this);
  48 }
  49 
  50 ShenandoahBarrierSetC1::ShenandoahBarrierSetC1() :
  51   _pre_barrier_c1_runtime_code_blob(NULL),
  52   _load_reference_barrier_rt_code_blob(NULL) {}
  53 
  54 void ShenandoahBarrierSetC1::pre_barrier(LIRGenerator* gen, CodeEmitInfo* info, DecoratorSet decorators, LIR_Opr addr_opr, LIR_Opr pre_val) {
  55   // First we test whether marking is in progress.
  56   BasicType flag_type;
  57   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
  58   bool do_load = pre_val == LIR_OprFact::illegalOpr;
  59   if (in_bytes(ShenandoahSATBMarkQueue::byte_width_of_active()) == 4) {
  60     flag_type = T_INT;
  61   } else {
  62     guarantee(in_bytes(ShenandoahSATBMarkQueue::byte_width_of_active()) == 1,
  63               "Assumption");
  64     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
  65     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
  66     flag_type = T_BOOLEAN;
  67   }
  68   LIR_Opr thrd = gen->getThreadPointer();
  69   LIR_Address* mark_active_flag_addr =
  70     new LIR_Address(thrd,
  71                     in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()),
  72                     flag_type);
  73   // Read the marking-in-progress flag.
  74   LIR_Opr flag_val = gen->new_register(T_INT);
  75   __ load(mark_active_flag_addr, flag_val);
  76   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
  77 
  78   LIR_PatchCode pre_val_patch_code = lir_patch_none;
  79 
  80   CodeStub* slow;
  81 
  82   if (do_load) {
  83     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
  84     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
  85 
  86     if (patch)
  87       pre_val_patch_code = lir_patch_normal;
  88 
  89     pre_val = gen->new_register(T_OBJECT);
  90 
  91     if (!addr_opr->is_address()) {
  92       assert(addr_opr->is_register(), "must be");
  93       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
  94     }
  95     slow = new ShenandoahPreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info ? new CodeEmitInfo(info) : NULL);
  96   } else {
  97     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
  98     assert(pre_val->is_register(), "must be");
  99     assert(pre_val->type() == T_OBJECT, "must be an object");
 100 
 101     slow = new ShenandoahPreBarrierStub(pre_val);
 102   }
 103 
 104   __ branch(lir_cond_notEqual, T_INT, slow);
 105   __ branch_destination(slow->continuation());
 106 }
 107 
 108 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier(LIRGenerator* gen, LIR_Opr obj, LIR_Opr addr) {
 109   if (ShenandoahLoadRefBarrier) {
 110     return load_reference_barrier_impl(gen, obj, addr);
 111   } else {
 112     return obj;
 113   }
 114 }
 115 
 116 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier_impl(LIRGenerator* gen, LIR_Opr obj, LIR_Opr addr) {
 117   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 118 
 119   obj = ensure_in_register(gen, obj, T_OBJECT);
 120   assert(obj->is_register(), "must be a register at this point");
 121   addr = ensure_in_register(gen, addr, T_ADDRESS);
 122   assert(addr->is_register(), "must be a register at this point");
 123   LIR_Opr result = gen->result_register_for(obj->value_type());
 124   __ move(obj, result);
 125   LIR_Opr tmp1 = gen->new_register(T_ADDRESS);
 126   LIR_Opr tmp2 = gen->new_register(T_ADDRESS);
 127 
 128   LIR_Opr thrd = gen->getThreadPointer();
 129   LIR_Address* active_flag_addr =
 130     new LIR_Address(thrd,
 131                     in_bytes(ShenandoahThreadLocalData::gc_state_offset()),
 132                     T_BYTE);
 133   // Read and check the gc-state-flag.
 134   LIR_Opr flag_val = gen->new_register(T_INT);
 135   __ load(active_flag_addr, flag_val);
 136   LIR_Opr mask = LIR_OprFact::intConst(ShenandoahHeap::HAS_FORWARDED);
 137   LIR_Opr mask_reg = gen->new_register(T_INT);
 138   __ move(mask, mask_reg);
 139 
 140   if (TwoOperandLIRForm) {
 141     __ logical_and(flag_val, mask_reg, flag_val);
 142   } else {
 143     LIR_Opr masked_flag = gen->new_register(T_INT);
 144     __ logical_and(flag_val, mask_reg, masked_flag);
 145     flag_val = masked_flag;
 146   }
 147   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
 148 
 149   CodeStub* slow = new ShenandoahLoadReferenceBarrierStub(obj, addr, result, tmp1, tmp2);
 150   __ branch(lir_cond_notEqual, T_INT, slow);
 151   __ branch_destination(slow->continuation());
 152 
 153   return result;
 154 }
 155 
 156 LIR_Opr ShenandoahBarrierSetC1::ensure_in_register(LIRGenerator* gen, LIR_Opr obj, BasicType type) {
 157   if (!obj->is_register()) {
 158     LIR_Opr obj_reg;
 159     if (obj->is_constant()) {
 160       obj_reg = gen->new_register(type);
 161       __ move(obj, obj_reg);
 162     } else {
 163       obj_reg = gen->new_pointer_register();
 164       __ leal(obj, obj_reg);
 165     }
 166     obj = obj_reg;
 167   }
 168   return obj;
 169 }
 170 
 171 LIR_Opr ShenandoahBarrierSetC1::storeval_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, DecoratorSet decorators) {
 172   if (ShenandoahStoreValEnqueueBarrier) {
 173     obj = ensure_in_register(gen, obj, T_OBJECT);
 174     pre_barrier(gen, info, decorators, LIR_OprFact::illegalOpr, obj);
 175   }
 176   return obj;
 177 }
 178 
 179 void ShenandoahBarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) {
 180   if (access.is_oop()) {
 181     if (ShenandoahSATBBarrier) {
 182       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(), LIR_OprFact::illegalOpr /* pre_val */);
 183     }
 184     value = storeval_barrier(access.gen(), value, access.access_emit_info(), access.decorators());
 185   }
 186   BarrierSetC1::store_at_resolved(access, value);
 187 }
 188 
 189 LIR_Opr ShenandoahBarrierSetC1::resolve_address(LIRAccess& access, bool resolve_in_register) {
 190   // We must resolve in register when patching. This is to avoid
 191   // having a patch area in the load barrier stub, since the call
 192   // into the runtime to patch will not have the proper oop map.
 193   const bool patch_before_barrier = access.is_oop() && (access.decorators() & C1_NEEDS_PATCHING) != 0;
 194   return BarrierSetC1::resolve_address(access, resolve_in_register || patch_before_barrier);
 195 }
 196 
 197 void ShenandoahBarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {
 198   // 1: non-reference load, no additional barrier is needed
 199   if (!access.is_oop()) {
 200     BarrierSetC1::load_at_resolved(access, result);
 201     return;
 202   }
 203 
 204   LIRGenerator* gen = access.gen();
 205   DecoratorSet decorators = access.decorators();
 206   BasicType type = access.type();
 207 
 208   // 2: load a reference from src location and apply LRB if ShenandoahLoadRefBarrier is set
 209   if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
 210     LIR_Opr tmp = gen->new_register(T_OBJECT);
 211     BarrierSetC1::load_at_resolved(access, tmp);
 212     tmp = load_reference_barrier(gen, tmp, access.resolved_addr());
 213     __ move(tmp, result);
 214   } else {
 215     BarrierSetC1::load_at_resolved(access, result);
 216   }
 217 
 218   // 3: apply keep-alive barrier if ShenandoahSATBBarrier is set
 219   if (ShenandoahSATBBarrier) {
 220     DecoratorSet decorators = access.decorators();
 221     bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 222     bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 223     bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 224     if (is_weak || is_phantom || is_anonymous) {
 225       // Register the value in the referent field with the pre-barrier
 226       LabelObj *Lcont_anonymous;
 227       if (is_anonymous) {
 228         Lcont_anonymous = new LabelObj();
 229         generate_referent_check(access, Lcont_anonymous);
 230       }
 231       pre_barrier(gen, access.access_emit_info(), decorators, LIR_OprFact::illegalOpr /* addr_opr */,
 232                   result /* pre_val */);
 233       if (is_anonymous) {
 234         __ branch_destination(Lcont_anonymous->label());
 235       }
 236     }
 237  }
 238 }
 239 
 240 class C1ShenandoahPreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 241   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 242     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 243     bs->generate_c1_pre_barrier_runtime_stub(sasm);
 244     return NULL;
 245   }
 246 };
 247 
 248 class C1ShenandoahLoadReferenceBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 249   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 250     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 251     bs->generate_c1_load_reference_barrier_runtime_stub(sasm);
 252     return NULL;
 253   }
 254 };
 255 
 256 void ShenandoahBarrierSetC1::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
 257   C1ShenandoahPreBarrierCodeGenClosure pre_code_gen_cl;
 258   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 259                                                               "shenandoah_pre_barrier_slow",
 260                                                               false, &pre_code_gen_cl);
 261   if (ShenandoahLoadRefBarrier) {
 262     C1ShenandoahLoadReferenceBarrierCodeGenClosure lrb_code_gen_cl;
 263     _load_reference_barrier_rt_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 264                                                                   "shenandoah_load_reference_barrier_slow",
 265                                                                   false, &lrb_code_gen_cl);
 266   }
 267 }