1 /*
   2  * Copyright (c) 2015, 2018, 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 #include "precompiled.hpp"
  25 #include "c1/c1_LIR.hpp"
  26 #include "c1/c1_LIRGenerator.hpp"
  27 #include "c1/c1_CodeStubs.hpp"
  28 #include "gc/z/c1/zBarrierSetC1.hpp"
  29 #include "gc/z/zBarrierSet.hpp"
  30 #include "gc/z/zBarrierSetAssembler.hpp"
  31 #include "gc/z/zThreadLocalData.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 ZLoadBarrierStubC1::ZLoadBarrierStubC1(LIRAccess& access, LIR_Opr ref, address runtime_stub) :
  35     _decorators(access.decorators()),
  36     _ref_addr(access.resolved_addr()),
  37     _ref(ref),
  38     _tmp(LIR_OprFact::illegalOpr),
  39     _runtime_stub(runtime_stub) {
  40 
  41   // Allocate tmp register if needed
  42   if (!_ref_addr->is_register()) {
  43     assert(_ref_addr->is_address(), "Must be an address");
  44     if (_ref_addr->as_address_ptr()->index()->is_valid() ||
  45         _ref_addr->as_address_ptr()->disp() != 0) {
  46       // Has index or displacement, need tmp register to load address into
  47       _tmp = access.gen()->new_pointer_register();
  48     } else {
  49       // No index or displacement, address available in base register
  50       _ref_addr = _ref_addr->as_address_ptr()->base();
  51     }
  52   }
  53 
  54   assert(_ref->is_register(), "Must be a register");
  55   assert(_ref_addr->is_register() != _tmp->is_register(), "Only one should be a register");
  56 }
  57 
  58 DecoratorSet ZLoadBarrierStubC1::decorators() const {
  59   return _decorators;
  60 }
  61 
  62 LIR_Opr ZLoadBarrierStubC1::ref() const {
  63   return _ref;
  64 }
  65 
  66 LIR_Opr ZLoadBarrierStubC1::ref_addr() const {
  67   return _ref_addr;
  68 }
  69 
  70 LIR_Opr ZLoadBarrierStubC1::tmp() const {
  71   return _tmp;
  72 }
  73 
  74 address ZLoadBarrierStubC1::runtime_stub() const {
  75   return _runtime_stub;
  76 }
  77 
  78 void ZLoadBarrierStubC1::visit(LIR_OpVisitState* visitor) {
  79   visitor->do_slow_case();
  80   visitor->do_input(_ref_addr);
  81   visitor->do_output(_ref);
  82   if (_tmp->is_valid()) {
  83     visitor->do_temp(_tmp);
  84   }
  85 }
  86 
  87 void ZLoadBarrierStubC1::emit_code(LIR_Assembler* ce) {
  88   ZBarrierSet::assembler()->generate_c1_load_barrier_stub(ce, this);
  89 }
  90 
  91 #ifndef PRODUCT
  92 void ZLoadBarrierStubC1::print_name(outputStream* out) const {
  93   out->print("ZLoadBarrierStubC1");
  94 }
  95 #endif // PRODUCT
  96 
  97 class LIR_OpZLoadBarrierTest : public LIR_Op {
  98 private:
  99   LIR_Opr _opr;
 100 
 101 public:
 102   LIR_OpZLoadBarrierTest(LIR_Opr opr) :
 103       LIR_Op(),
 104       _opr(opr) {}
 105 
 106   virtual void visit(LIR_OpVisitState* state) {
 107     state->do_input(_opr);
 108   }
 109 
 110   virtual void emit_code(LIR_Assembler* ce) {
 111     ZBarrierSet::assembler()->generate_c1_load_barrier_test(ce, _opr);
 112   }
 113 
 114   virtual void print_instr(outputStream* out) const {
 115     _opr->print(out);
 116     out->print(" ");
 117   }
 118 
 119 #ifndef PRODUCT
 120   virtual const char* name() const {
 121     return "lir_z_load_barrier_test";
 122   }
 123 #endif // PRODUCT
 124 };
 125 
 126 static bool barrier_needed(LIRAccess& access) {
 127   return ZBarrierSet::barrier_needed(access.decorators(), access.type());
 128 }
 129 
 130 ZBarrierSetC1::ZBarrierSetC1() :
 131     _load_barrier_on_oop_field_preloaded_runtime_stub(NULL),
 132     _load_barrier_on_weak_oop_field_preloaded_runtime_stub(NULL) {}
 133 
 134 address ZBarrierSetC1::load_barrier_on_oop_field_preloaded_runtime_stub(DecoratorSet decorators) const {
 135   assert((decorators & ON_PHANTOM_OOP_REF) == 0, "Unsupported decorator");
 136   //assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Unsupported decorator");
 137 
 138   if ((decorators & ON_WEAK_OOP_REF) != 0) {
 139     return _load_barrier_on_weak_oop_field_preloaded_runtime_stub;
 140   } else {
 141     return _load_barrier_on_oop_field_preloaded_runtime_stub;
 142   }
 143 }
 144 
 145 #ifdef ASSERT
 146 #define __ access.gen()->lir(__FILE__, __LINE__)->
 147 #else
 148 #define __ access.gen()->lir()->
 149 #endif
 150 
 151 void ZBarrierSetC1::load_barrier(LIRAccess& access, LIR_Opr result) const {
 152   // Fast path
 153   __ append(new LIR_OpZLoadBarrierTest(result));
 154 
 155   // Slow path
 156   const address runtime_stub = load_barrier_on_oop_field_preloaded_runtime_stub(access.decorators());
 157   CodeStub* const stub = new ZLoadBarrierStubC1(access, result, runtime_stub);
 158   __ branch(lir_cond_notEqual, T_ADDRESS, stub);
 159   __ branch_destination(stub->continuation());
 160 }
 161 
 162 LIR_Opr ZBarrierSetC1::resolve_address(LIRAccess& access, bool resolve_in_register) {
 163   // We must resolve in register when patching. This is to avoid
 164   // having a patch area in the load barrier stub, since the call
 165   // into the runtime to patch will not have the proper oop map.
 166   const bool patch_before_barrier = barrier_needed(access) && (access.decorators() & C1_NEEDS_PATCHING) != 0;
 167   return BarrierSetC1::resolve_address(access, resolve_in_register || patch_before_barrier);
 168 }
 169 
 170 #undef __
 171 
 172 void ZBarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {
 173   BarrierSetC1::load_at_resolved(access, result);
 174 
 175   if (barrier_needed(access)) {
 176     load_barrier(access, result);
 177   }
 178 }
 179 
 180 static void pre_load_barrier(LIRAccess& access) {
 181   DecoratorSet decorators = access.decorators();
 182 
 183   // Downgrade access to MO_UNORDERED
 184   decorators = (decorators & ~MO_DECORATOR_MASK) | MO_UNORDERED;
 185 
 186   // Remove ACCESS_WRITE
 187   decorators = (decorators & ~ACCESS_WRITE);
 188 
 189   // Generate synthetic load at
 190   access.gen()->access_load_at(decorators,
 191                                access.type(),
 192                                access.base().item(),
 193                                access.offset().opr(),
 194                                access.gen()->new_register(access.type()),
 195                                NULL /* patch_emit_info */,
 196                                NULL /* load_emit_info */);
 197 }
 198 
 199 LIR_Opr ZBarrierSetC1::atomic_xchg_at_resolved(LIRAccess& access, LIRItem& value) {
 200   if (barrier_needed(access)) {
 201     pre_load_barrier(access);
 202   }
 203 
 204   return BarrierSetC1::atomic_xchg_at_resolved(access, value);
 205 }
 206 
 207 LIR_Opr ZBarrierSetC1::atomic_cmpxchg_at_resolved(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value) {
 208   if (barrier_needed(access)) {
 209     pre_load_barrier(access);
 210   }
 211 
 212   return BarrierSetC1::atomic_cmpxchg_at_resolved(access, cmp_value, new_value);
 213 }
 214 
 215 class ZLoadBarrierRuntimeStubCodeGenClosure : public StubAssemblerCodeGenClosure {
 216 private:
 217   const DecoratorSet _decorators;
 218 
 219 public:
 220   ZLoadBarrierRuntimeStubCodeGenClosure(DecoratorSet decorators) :
 221       _decorators(decorators) {}
 222 
 223   virtual OopMapSet* generate_code(StubAssembler* sasm) {
 224     ZBarrierSet::assembler()->generate_c1_load_barrier_runtime_stub(sasm, _decorators);
 225     return NULL;
 226   }
 227 };
 228 
 229 static address generate_c1_runtime_stub(BufferBlob* blob, DecoratorSet decorators, const char* name) {
 230   ZLoadBarrierRuntimeStubCodeGenClosure cl(decorators);
 231   CodeBlob* const code_blob = Runtime1::generate_blob(blob, -1 /* stub_id */, name, false /* expect_oop_map*/, &cl);
 232   return code_blob->code_begin();
 233 }
 234 
 235 void ZBarrierSetC1::generate_c1_runtime_stubs(BufferBlob* blob) {
 236   _load_barrier_on_oop_field_preloaded_runtime_stub =
 237     generate_c1_runtime_stub(blob, ON_STRONG_OOP_REF, "load_barrier_on_oop_field_preloaded_runtime_stub");
 238   _load_barrier_on_weak_oop_field_preloaded_runtime_stub =
 239     generate_c1_runtime_stub(blob, ON_WEAK_OOP_REF, "load_barrier_on_weak_oop_field_preloaded_runtime_stub");
 240 }