1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   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/brooksPointer.hpp"
  28 #include "gc/shenandoah/shenandoahBaseBarrierSetAssembler.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "gc/shenandoah/shenandoahHeapRegion.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 ShenandoahBarrierSetC1::store_at(LIRAccess& access, LIR_Opr value) {
  41   access.set_base(write_barrier(access.gen(), access.base().item().result(), access.access_emit_info(), access.needs_null_check()));
  42   LIR_Opr resolved = resolve_address(access, false);
  43   access.set_resolved_addr(resolved);
  44   if (access.is_oop()) {
  45     if (ShenandoahSATBBarrier) {
  46       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(), LIR_OprFact::illegalOpr /* pre_val */);
  47     }
  48     value = storeval_barrier(access.gen(), value, access.access_emit_info(), access.decorators());
  49   }
  50   BarrierSetC1::store_at_resolved(access, value);
  51 }
  52 
  53 void ShenandoahBarrierSetC1::load_at(LIRAccess& access, LIR_Opr result) {
  54   LIRItem& base_item = access.base().item();
  55   access.set_base(read_barrier(access.gen(), access.base().item().result(), access.access_emit_info(), access.needs_null_check()));
  56   LIR_Opr resolved = resolve_address(access, false);
  57   access.set_resolved_addr(resolved);
  58   BarrierSetC1::load_at_resolved(access, result);
  59   access.set_base(base_item);
  60 
  61   if (ShenandoahKeepAliveBarrier) {
  62     DecoratorSet decorators = access.decorators();
  63     bool is_weak = (decorators & ON_WEAK_OOP_REF) != 0;
  64     bool is_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
  65     bool is_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
  66     LIRGenerator *gen = access.gen();
  67     if (access.is_oop() && (is_weak || is_phantom || is_anonymous)) {
  68       // Register the value in the referent field with the pre-barrier
  69       LabelObj *Lcont_anonymous;
  70       if (is_anonymous) {
  71         Lcont_anonymous = new LabelObj();
  72         generate_referent_check(access, Lcont_anonymous);
  73       }
  74       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), LIR_OprFact::illegalOpr /* addr_opr */,
  75                   result /* pre_val */);
  76       if (is_anonymous) {
  77         __ branch_destination(Lcont_anonymous->label());
  78       }
  79     }
  80   }
  81 }
  82 
  83 LIR_Opr ShenandoahBarrierSetC1::atomic_cmpxchg_at(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value) {
  84   access.load_address();
  85   access.set_base(write_barrier(access.gen(), access.base().item().result(), access.access_emit_info(), access.needs_null_check()));
  86   LIR_Opr resolved = resolve_address(access, true);
  87   access.set_resolved_addr(resolved);
  88   if (access.is_oop()) {
  89     if (ShenandoahSATBBarrier) {
  90       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(),
  91                   LIR_OprFact::illegalOpr /* pre_val */);
  92     }
  93   }
  94   return atomic_cmpxchg_at_resolved(access, cmp_value, new_value);
  95 }
  96 
  97 LIR_Opr ShenandoahBarrierSetC1::atomic_xchg_at(LIRAccess& access, LIRItem& value) {
  98   access.set_base(write_barrier(access.gen(), access.base().item().result(), access.access_emit_info(), access.needs_null_check()));
  99   LIR_Opr resolved = resolve_address(access, true);
 100   access.set_resolved_addr(resolved);
 101   if (access.is_oop()) {
 102     if (ShenandoahSATBBarrier) {
 103       pre_barrier(access.gen(), access.access_emit_info(), access.decorators(), access.resolved_addr(),
 104                   LIR_OprFact::illegalOpr /* pre_val */);
 105     }
 106   }
 107   return BarrierSetC1::atomic_xchg_at_resolved(access, value);
 108 }
 109 
 110 LIR_Opr ShenandoahBarrierSetC1::atomic_add_at(LIRAccess& access, LIRItem& value) {
 111   access.load_address();
 112   access.set_base(write_barrier(access.gen(), access.base().item().result(), access.access_emit_info(), access.needs_null_check()));
 113   LIR_Opr resolved = resolve_address(access, true);
 114   access.set_resolved_addr(resolved);
 115   return BarrierSetC1::atomic_add_at_resolved(access, value);
 116 }
 117 
 118 LIR_Opr ShenandoahBarrierSetC1::resolve(LIRGenerator* gen, DecoratorSet decorators, LIR_Opr obj) {
 119   bool is_write = decorators & ACCESS_WRITE;
 120   if (is_write) {
 121     return write_barrier(gen, obj, NULL, (decorators & IS_NOT_NULL) == 0);
 122   } else {
 123     return read_barrier(gen, obj, NULL, (decorators & IS_NOT_NULL) == 0);
 124   }
 125 }