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/shenandoahThreadLocalData.hpp"
  31 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  32 
  33 #ifndef PATCHED_ADDR
  34 #define PATCHED_ADDR  (max_jint)
  35 #endif
  36 
  37 #ifdef ASSERT
  38 #define __ gen->lir(__FILE__, __LINE__)->
  39 #else
  40 #define __ gen->lir()->
  41 #endif
  42 
  43 void ShenandoahPreBarrierStub::emit_code(LIR_Assembler* ce) {
  44   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  45   bs->gen_pre_barrier_stub(ce, this);
  46 }
  47 
  48 void ShenandoahLoadReferenceBarrierStub::emit_code(LIR_Assembler* ce) {
  49   ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
  50   bs->gen_load_reference_barrier_stub(ce, this);
  51 }
  52 
  53 void ShenandoahBarrierSetC1::pre_barrier(LIRGenerator* gen, CodeEmitInfo* info, DecoratorSet decorators, LIR_Opr addr_opr, LIR_Opr pre_val) {
  54   // First we test whether marking is in progress.
  55   BasicType flag_type;
  56   bool patch = (decorators & C1_NEEDS_PATCHING) != 0;
  57   bool do_load = pre_val == LIR_OprFact::illegalOpr;
  58   if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
  59     flag_type = T_INT;
  60   } else {
  61     guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1,
  62               "Assumption");
  63     // Use unsigned type T_BOOLEAN here rather than signed T_BYTE since some platforms, eg. ARM,
  64     // need to use unsigned instructions to use the large offset to load the satb_mark_queue.
  65     flag_type = T_BOOLEAN;
  66   }
  67   LIR_Opr thrd = gen->getThreadPointer();
  68   LIR_Address* mark_active_flag_addr =
  69     new LIR_Address(thrd,
  70                     in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()),
  71                     flag_type);
  72   // Read the marking-in-progress flag.
  73   LIR_Opr flag_val = gen->new_register(T_INT);
  74   __ load(mark_active_flag_addr, flag_val);
  75   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
  76 
  77   LIR_PatchCode pre_val_patch_code = lir_patch_none;
  78 
  79   CodeStub* slow;
  80 
  81   if (do_load) {
  82     assert(pre_val == LIR_OprFact::illegalOpr, "sanity");
  83     assert(addr_opr != LIR_OprFact::illegalOpr, "sanity");
  84 
  85     if (patch)
  86       pre_val_patch_code = lir_patch_normal;
  87 
  88     pre_val = gen->new_register(T_OBJECT);
  89 
  90     if (!addr_opr->is_address()) {
  91       assert(addr_opr->is_register(), "must be");
  92       addr_opr = LIR_OprFact::address(new LIR_Address(addr_opr, T_OBJECT));
  93     }
  94     slow = new ShenandoahPreBarrierStub(addr_opr, pre_val, pre_val_patch_code, info ? new CodeEmitInfo(info) : NULL);
  95   } else {
  96     assert(addr_opr == LIR_OprFact::illegalOpr, "sanity");
  97     assert(pre_val->is_register(), "must be");
  98     assert(pre_val->type() == T_OBJECT, "must be an object");
  99 
 100     slow = new ShenandoahPreBarrierStub(pre_val);
 101   }
 102 
 103   __ branch(lir_cond_notEqual, T_INT, slow);
 104   __ branch_destination(slow->continuation());
 105 }
 106 
 107 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
 108   if (ShenandoahLoadRefBarrier) {
 109     return load_reference_barrier_impl(gen, obj, info, need_null_check);
 110   } else {
 111     return obj;
 112   }
 113 }
 114 
 115 LIR_Opr ShenandoahBarrierSetC1::load_reference_barrier_impl(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, bool need_null_check) {
 116   assert(ShenandoahLoadRefBarrier, "Should be enabled");
 117 
 118   obj = ensure_in_register(gen, obj);
 119   assert(obj->is_register(), "must be a register at this point");
 120   LIR_Opr result = gen->new_register(T_OBJECT);
 121   __ move(obj, result);
 122 
 123   LIR_Opr thrd = gen->getThreadPointer();
 124   LIR_Address* active_flag_addr =
 125     new LIR_Address(thrd,
 126                     in_bytes(ShenandoahThreadLocalData::gc_state_offset()),
 127                     T_BYTE);
 128   // Read and check the gc-state-flag.
 129   LIR_Opr flag_val = gen->new_register(T_INT);
 130   __ load(active_flag_addr, flag_val);
 131   LIR_Opr mask = LIR_OprFact::intConst(ShenandoahHeap::HAS_FORWARDED |
 132                                        ShenandoahHeap::EVACUATION |
 133                                        ShenandoahHeap::TRAVERSAL);
 134   LIR_Opr mask_reg = gen->new_register(T_INT);
 135   __ move(mask, mask_reg);
 136 
 137   if (TwoOperandLIRForm) {
 138     __ logical_and(flag_val, mask_reg, flag_val);
 139   } else {
 140     LIR_Opr masked_flag = gen->new_register(T_INT);
 141     __ logical_and(flag_val, mask_reg, masked_flag);
 142     flag_val = masked_flag;
 143   }
 144   __ cmp(lir_cond_notEqual, flag_val, LIR_OprFact::intConst(0));
 145 
 146   CodeStub* slow = new ShenandoahLoadReferenceBarrierStub(obj, result, info ? new CodeEmitInfo(info) : NULL, need_null_check);
 147   __ branch(lir_cond_notEqual, T_INT, slow);
 148   __ branch_destination(slow->continuation());
 149 
 150   return result;
 151 }
 152 
 153 LIR_Opr ShenandoahBarrierSetC1::ensure_in_register(LIRGenerator* gen, LIR_Opr obj) {
 154   if (!obj->is_register()) {
 155     LIR_Opr obj_reg = gen->new_register(T_OBJECT);
 156     if (obj->is_constant()) {
 157       __ move(obj, obj_reg);
 158     } else {
 159       __ leal(obj, obj_reg);
 160     }
 161     obj = obj_reg;
 162   }
 163   return obj;
 164 }
 165 
 166 LIR_Opr ShenandoahBarrierSetC1::storeval_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, DecoratorSet decorators) {
 167   if (ShenandoahStoreValEnqueueBarrier) {
 168     obj = ensure_in_register(gen, obj);
 169     pre_barrier(gen, info, decorators, LIR_OprFact::illegalOpr, obj);
 170   }
 171   return obj;
 172 }
 173 
 174 void ShenandoahBarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) {
 175   if (access.is_oop()) {
 176     if (ShenandoahSATBBarrier) {
 177       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(), LIR_OprFact::illegalOpr /* pre_val */);
 178     }
 179     value = storeval_barrier(access.gen(), value, access.access_emit_info(), access.decorators());
 180   }
 181   BarrierSetC1::store_at_resolved(access, value);
 182 }
 183 
 184 void ShenandoahBarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {
 185   if (!access.is_oop()) {
 186     BarrierSetC1::load_at_resolved(access, result);
 187     return;
 188   }
 189 
 190   LIRGenerator *gen = access.gen();
 191 
 192   if (ShenandoahLoadRefBarrier) {
 193     LIR_Opr tmp = gen->new_register(T_OBJECT);
 194     BarrierSetC1::load_at_resolved(access, tmp);
 195     tmp = load_reference_barrier(access.gen(), tmp, access.access_emit_info(), true);
 196     __ move(tmp, result);
 197   } else {
 198     BarrierSetC1::load_at_resolved(access, result);
 199   }
 200 
 201   if (ShenandoahKeepAliveBarrier) {
 202     DecoratorSet decorators = access.decorators();
 203     bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
 204     bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
 205     bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 206     if (is_weak || is_phantom || is_anonymous) {
 207       // Register the value in the referent field with the pre-barrier
 208       LabelObj *Lcont_anonymous;
 209       if (is_anonymous) {
 210         Lcont_anonymous = new LabelObj();
 211         generate_referent_check(access, Lcont_anonymous);
 212       }
 213       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), LIR_OprFact::illegalOpr /* addr_opr */,
 214                   result /* pre_val */);
 215       if (is_anonymous) {
 216         __ branch_destination(Lcont_anonymous->label());
 217       }
 218     }
 219   }
 220 }
 221 
 222 class C1ShenandoahPreBarrierCodeGenClosure : public StubAssemblerCodeGenClosure {
 223   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 224     ShenandoahBarrierSetAssembler* bs = (ShenandoahBarrierSetAssembler*)BarrierSet::barrier_set()->barrier_set_assembler();
 225     bs->generate_c1_pre_barrier_runtime_stub(sasm);
 226     return NULL;
 227   }
 228 };
 229 
 230 void ShenandoahBarrierSetC1::generate_c1_runtime_stubs(BufferBlob* buffer_blob) {
 231   C1ShenandoahPreBarrierCodeGenClosure pre_code_gen_cl;
 232   _pre_barrier_c1_runtime_code_blob = Runtime1::generate_blob(buffer_blob, -1,
 233                                                               "shenandoah_pre_barrier_slow",
 234                                                               false, &pre_code_gen_cl);
 235 }