1 /*
   2  * Copyright (c) 2018, 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/shared/satbMarkQueue.hpp"
  27 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  28 #include "gc/shenandoah/shenandoahHeap.hpp"
  29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  30 #include "gc/shenandoah/shenandoahRuntime.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 void ShenandoahBarrierSetC1::pre_barrier(LIRGenerator* gen, CodeEmitInfo* info, DecoratorSet decorators, LIR_Opr addr_opr, LIR_Opr pre_val) {
  51   // First we test whether marking is in progress.
  52   BasicType flag_type;
  53   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
  54   bool do_load = pre_val == LIR_OprFact::illegalOpr;
  55   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
  56     flag_type = T_INT;
  57   } else {
  58     guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1,
  59               "Assumption");
  60     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
  61     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
  62     flag_type = T_BOOLEAN;
  63   }
  64   LIR_Opr thrd = gen->getThreadPointer();
  65   LIR_Address* mark_active_flag_addr =
  66     new LIR_Address(thrd,
  67                     in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()),
  68                     flag_type);
  69   // Read the marking-in-progress flag.
  70   LIR_Opr flag_val = gen->new_register(T_INT);
  71   __ load(mark_active_flag_addr, flag_val);
  72   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
  73 
  74   LIR_PatchCode pre_val_patch_code = lir_patch_none;
  75 
  76   CodeStub* slow;
  77 
  78   if (do_load) {
  79     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
  80     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
  81 
  82     if (patch)
  83       pre_val_patch_code = lir_patch_normal;
  84 
  85     pre_val = gen->new_register(T_OBJECT);
  86 
  87     if (!addr_opr->is_address()) {
  88       assert(addr_opr->is_register(), "must be");
  89       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
  90     }
  91     slow = new ShenandoahPreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info ? new CodeEmitInfo(info) : NULL);
  92   } else {
  93     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
  94     assert(pre_val->is_register(), "must be");
  95     assert(pre_val->type() == T_OBJECT, "must be an object");
  96 
  97     slow = new ShenandoahPreBarrierStub(pre_val);
  98   }
  99 
 100   __ branch(lir_cond_notEqual, T_INT, slow);
 101   __ branch_destination(slow->continuation());
 102 }
 103 
 104 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier(LIRGenerator* gen, LIR_Opr obj) {
 105   if (ShenandoahLoadRefBarrier) {
 106     return load_reference_barrier_impl(gen, obj);
 107   } else {
 108     return obj;
 109   }
 110 }
 111 
 112 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier_impl(LIRGenerator* gen, LIR_Opr obj) {
 113   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 114 
 115   obj = ensure_in_register(gen, obj);
 116   assert(obj->is_register(), "must be a register at this point");
 117   LIR_Opr result = gen->result_register_for(obj->value_type());
 118   __ move(obj, result);
 119 
 120   LIR_Opr thrd = gen->getThreadPointer();
 121   LIR_Address* active_flag_addr =
 122     new LIR_Address(thrd,
 123                     in_bytes(ShenandoahThreadLocalData::gc_state_offset()),
 124                     T_BYTE);
 125   // Read and check the gc-state-flag.
 126   LIR_Opr flag_val = gen->new_register(T_INT);
 127   __ load(active_flag_addr, flag_val);
 128   LIR_Opr mask = LIR_OprFact::intConst(ShenandoahHeap::HAS_FORWARDED |
 129                                        ShenandoahHeap::EVACUATION |
 130                                        ShenandoahHeap::TRAVERSAL);
 131   LIR_Opr mask_reg = gen->new_register(T_INT);
 132   __ move(mask, mask_reg);
 133 
 134   if (TwoOperandLIRForm) {
 135     __ logical_and(flag_val, mask_reg, flag_val);
 136   } else {
 137     LIR_Opr masked_flag = gen->new_register(T_INT);
 138     __ logical_and(flag_val, mask_reg, masked_flag);
 139     flag_val = masked_flag;
 140   }
 141   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
 142 
 143   CodeStub* slow = new ShenandoahLoadReferenceBarrierStub(obj, result);
 144   __ branch(lir_cond_notEqual, T_INT, slow);
 145   __ branch_destination(slow->continuation());
 146 
 147   return result;
 148 }
 149 
 150 LIR_Opr ShenandoahBarrierSetC1::ensure_in_register(LIRGenerator* gen, LIR_Opr obj) {
 151   if (!obj->is_register()) {
 152     LIR_Opr obj_reg = gen->new_register(T_OBJECT);
 153     if (obj->is_constant()) {
 154       __ move(obj, obj_reg);
 155     } else {
 156       __ leal(obj, obj_reg);
 157     }
 158     obj = obj_reg;
 159   }
 160   return obj;
 161 }
 162 
 163 LIR_Opr ShenandoahBarrierSetC1::storeval_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, DecoratorSet decorators) {
 164   if (ShenandoahStoreValEnqueueBarrier) {
 165     obj = ensure_in_register(gen, obj);
 166     pre_barrier(gen, info, decorators, LIR_OprFact::illegalOpr, obj);
 167   }
 168   return obj;
 169 }
 170 
 171 void ShenandoahBarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) {
 172   if (access.is_oop()) {
 173     if (ShenandoahSATBBarrier) {
 174       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(), LIR_OprFact::illegalOpr /* pre_val */);
 175     }
 176     value = storeval_barrier(access.gen(), value, access.access_emit_info(), access.decorators());
 177   }
 178   BarrierSetC1::store_at_resolved(access, value);
 179 }
 180 
 181 void ShenandoahBarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {
 182   if (!access.is_oop()) {
 183     BarrierSetC1::load_at_resolved(access, result);
 184     return;
 185   }
 186 
 187   LIRGenerator* gen = access.gen();
 188 
 189   DecoratorSet decorators = access.decorators();
 190   if ((decorators & IN_NATIVE) != 0) {
 191     assert(access.is_oop(), "IN_NATIVE access only for oop values");
 192     BarrierSetC1::load_at_resolved(access, result);
 193     LIR_OprList* args = new LIR_OprList();
 194     args->append(result);
 195     BasicTypeList signature;
 196     signature.append(T_OBJECT);
 197     LIR_Opr call_result = gen->call_runtime(&signature, args,
 198                                             CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_native),
 199                                             objectType, NULL);
 200     __ move(call_result, result);
 201     return;
 202   }
 203 
 204   if (ShenandoahLoadRefBarrier) {
 205     LIR_Opr tmp = gen->new_register(T_OBJECT);
 206     BarrierSetC1::load_at_resolved(access, tmp);
 207     tmp = load_reference_barrier(access.gen(), tmp);
 208     __ move(tmp, result);
 209   } else {
 210     BarrierSetC1::load_at_resolved(access, result);
 211   }
 212 
 213   if (ShenandoahKeepAliveBarrier) {
 214     bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 215     bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 216     bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 217     if (is_weak || is_phantom || is_anonymous) {
 218       // Register the value in the referent field with the pre-barrier
 219       LabelObj *Lcont_anonymous;
 220       if (is_anonymous) {
 221         Lcont_anonymous = new LabelObj();
 222         generate_referent_check(access, Lcont_anonymous);
 223       }
 224       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), LIR_OprFact::illegalOpr /* addr_opr */,
 225                   result /* pre_val */);
 226       if (is_anonymous) {
 227         __ branch_destination(Lcont_anonymous->label());
 228       }
 229     }
 230   }
 231 }
 232 
 233 class C1ShenandoahPreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 234   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 235     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 236     bs->generate_c1_pre_barrier_runtime_stub(sasm);
 237     return NULL;
 238   }
 239 };
 240 
 241 class C1ShenandoahLoadReferenceBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 242   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 243     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 244     bs->generate_c1_load_reference_barrier_runtime_stub(sasm);
 245     return NULL;
 246   }
 247 };
 248 
 249 void ShenandoahBarrierSetC1::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
 250   C1ShenandoahPreBarrierCodeGenClosure pre_code_gen_cl;
 251   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 252                                                               "shenandoah_pre_barrier_slow",
 253                                                               false, &pre_code_gen_cl);
 254   if (ShenandoahLoadRefBarrier) {
 255     C1ShenandoahLoadReferenceBarrierCodeGenClosure lrb_code_gen_cl;
 256     _load_reference_barrier_rt_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 257                                                                   "shenandoah_load_reference_barrier_slow",
 258                                                                   false, &lrb_code_gen_cl);
 259   }
 260 }
 261 
 262 const char* ShenandoahBarrierSetC1::rtcall_name_for_address(address entry) {
 263   if (entry == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_native)) {
 264     return "ShenandoahRuntime::load_reference_barrier_native";
 265   }
 266   return NULL;
 267 }