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 #ifndef SHARE_GC_SHENANDOAH_C1_SHENANDOAHBARRIERSETC1_HPP
  25 #define SHARE_GC_SHENANDOAH_C1_SHENANDOAHBARRIERSETC1_HPP
  26 
  27 #include "c1/c1_CodeStubs.hpp"
  28 #include "gc/shared/c1/barrierSetC1.hpp"
  29 
  30 class ShenandoahPreBarrierStub: public CodeStub {
  31   friend class ShenandoahBarrierSetC1;
  32 private:
  33   bool _do_load;
  34   LIR_Opr _addr;
  35   LIR_Opr _pre_val;
  36   LIR_PatchCode _patch_code;
  37   CodeEmitInfo* _info;
  38 
  39 public:
  40   // Version that _does_ generate a load of the previous value from addr.
  41   // addr (the address of the field to be read) must be a LIR_Address
  42   // pre_val (a temporary register) must be a register;
  43   ShenandoahPreBarrierStub(LIR_Opr addr, LIR_Opr pre_val, LIR_PatchCode patch_code, CodeEmitInfo* info) :
  44     _do_load(true), _addr(addr), _pre_val(pre_val),
  45     _patch_code(patch_code), _info(info)
  46   {
  47     assert(_pre_val->is_register(), "should be temporary register");
  48     assert(_addr->is_address(), "should be the address of the field");
  49   }
  50 
  51   // Version that _does not_ generate load of the previous value; the
  52   // previous value is assumed to have already been loaded into pre_val.
  53   ShenandoahPreBarrierStub(LIR_Opr pre_val) :
  54     _do_load(false), _addr(LIR_OprFact::illegalOpr), _pre_val(pre_val),
  55     _patch_code(lir_patch_none), _info(NULL)
  56   {
  57     assert(_pre_val->is_register(), "should be a register");
  58   }
  59 
  60   LIR_Opr addr() const { return _addr; }
  61   LIR_Opr pre_val() const { return _pre_val; }
  62   LIR_PatchCode patch_code() const { return _patch_code; }
  63   CodeEmitInfo* info() const { return _info; }
  64   bool do_load() const { return _do_load; }
  65 
  66   virtual void emit_code(LIR_Assembler* e);
  67   virtual void visit(LIR_OpVisitState* visitor) {
  68     if (_do_load) {
  69       // don't pass in the code emit info since it's processed in the fast
  70       // path
  71       if (_info != NULL)
  72         visitor->do_slow_case(_info);
  73       else
  74         visitor->do_slow_case();
  75 
  76       visitor->do_input(_addr);
  77       visitor->do_temp(_pre_val);
  78     } else {
  79       visitor->do_slow_case();
  80       visitor->do_input(_pre_val);
  81     }
  82   }
  83 #ifndef PRODUCT
  84   virtual void print_name(outputStream* out) const { out->print("ShenandoahPreBarrierStub"); }
  85 #endif // PRODUCT
  86 };
  87 
  88 class ShenandoahLoadReferenceBarrierStub: public CodeStub {
  89   friend class ShenandoahBarrierSetC1;
  90 private:
  91   LIR_Opr _obj;
  92   LIR_Opr _result;
  93   LIR_Opr _tmp1;
  94   LIR_Opr _tmp2;
  95 
  96 public:
  97   ShenandoahLoadReferenceBarrierStub(LIR_Opr obj, LIR_Opr result, LIR_Opr tmp1, LIR_Opr tmp2) :
  98     _obj(obj), _result(result), _tmp1(tmp1), _tmp2(tmp2)
  99   {
 100     assert(_obj->is_register(), "should be register");
 101     assert(_result->is_register(), "should be register");
 102     assert(_tmp1->is_register(), "should be register");
 103     assert(_tmp2->is_register(), "should be register");
 104   }
 105 
 106   LIR_Opr obj() const { return _obj; }
 107   LIR_Opr result() const { return _result; }
 108   LIR_Opr tmp1() const { return _tmp1; }
 109   LIR_Opr tmp2() const { return _tmp2; }
 110 
 111   virtual void emit_code(LIR_Assembler* e);
 112   virtual void visit(LIR_OpVisitState* visitor) {
 113     visitor->do_slow_case();
 114     visitor->do_input(_obj);
 115     visitor->do_temp(_result);
 116     visitor->do_temp(_tmp1);
 117     visitor->do_temp(_tmp2);
 118   }
 119 #ifndef PRODUCT
 120   virtual void print_name(outputStream* out) const { out->print("ShenandoahLoadReferenceBarrierStub"); }
 121 #endif // PRODUCT
 122 };
 123 
 124 class LIR_OpShenandoahCompareAndSwap : public LIR_Op {
 125  friend class LIR_OpVisitState;
 126 
 127 private:
 128   LIR_Opr _addr;
 129   LIR_Opr _cmp_value;
 130   LIR_Opr _new_value;
 131   LIR_Opr _tmp1;
 132   LIR_Opr _tmp2;
 133 
 134 public:
 135   LIR_OpShenandoahCompareAndSwap(LIR_Opr addr, LIR_Opr cmp_value, LIR_Opr new_value,
 136                                  LIR_Opr t1, LIR_Opr t2, LIR_Opr result)
 137     : LIR_Op(lir_none, result, NULL)  // no info
 138     , _addr(addr)
 139     , _cmp_value(cmp_value)
 140     , _new_value(new_value)
 141     , _tmp1(t1)
 142     , _tmp2(t2)                                  { }
 143 
 144   LIR_Opr addr()        const                    { return _addr;  }
 145   LIR_Opr cmp_value()   const                    { return _cmp_value; }
 146   LIR_Opr new_value()   const                    { return _new_value; }
 147   LIR_Opr tmp1()        const                    { return _tmp1;      }
 148   LIR_Opr tmp2()        const                    { return _tmp2;      }
 149 
 150   virtual void visit(LIR_OpVisitState* state) {
 151       assert(_addr->is_valid(),      "used");
 152       assert(_cmp_value->is_valid(), "used");
 153       assert(_new_value->is_valid(), "used");
 154       if (_info)                    state->do_info(_info);
 155                                     state->do_input(_addr);
 156                                     state->do_temp(_addr);
 157                                     state->do_input(_cmp_value);
 158                                     state->do_temp(_cmp_value);
 159                                     state->do_input(_new_value);
 160                                     state->do_temp(_new_value);
 161       if (_tmp1->is_valid())        state->do_temp(_tmp1);
 162       if (_tmp2->is_valid())        state->do_temp(_tmp2);
 163       if (_result->is_valid())      state->do_output(_result);
 164   }
 165 
 166   virtual void emit_code(LIR_Assembler* masm);
 167 
 168   virtual void print_instr(outputStream* out) const {
 169     addr()->print(out);      out->print(" ");
 170     cmp_value()->print(out); out->print(" ");
 171     new_value()->print(out); out->print(" ");
 172     tmp1()->print(out);      out->print(" ");
 173     tmp2()->print(out);      out->print(" ");
 174   }
 175 #ifndef PRODUCT
 176   virtual const char* name() const {
 177     return "shenandoah_cas_obj";
 178   }
 179 #endif // PRODUCT
 180 };
 181 
 182 class ShenandoahBarrierSetC1 : public BarrierSetC1 {
 183 private:
 184   CodeBlob* _pre_barrier_c1_runtime_code_blob;
 185   CodeBlob* _load_reference_barrier_rt_code_blob;
 186 
 187   void pre_barrier(LIRGenerator* gen, CodeEmitInfo* info, DecoratorSet decorators, LIR_Opr addr_opr, LIR_Opr pre_val);
 188 
 189   LIR_Opr load_reference_barrier(LIRGenerator* gen, LIR_Opr obj);
 190   LIR_Opr storeval_barrier(LIRGenerator* gen, LIR_Opr obj, CodeEmitInfo* info, DecoratorSet decorators);
 191 
 192   LIR_Opr load_reference_barrier_impl(LIRGenerator* gen, LIR_Opr obj);
 193 
 194   LIR_Opr ensure_in_register(LIRGenerator* gen, LIR_Opr obj);
 195 
 196 public:
 197   ShenandoahBarrierSetC1();
 198 
 199   CodeBlob* pre_barrier_c1_runtime_code_blob() {
 200     assert(_pre_barrier_c1_runtime_code_blob != NULL, "");
 201     return _pre_barrier_c1_runtime_code_blob;
 202   }
 203 
 204   CodeBlob* load_reference_barrier_rt_code_blob() {
 205     assert(_load_reference_barrier_rt_code_blob != NULL, "");
 206     return _load_reference_barrier_rt_code_blob;
 207   }
 208 
 209 protected:
 210 
 211   virtual void store_at_resolved(LIRAccess& access, LIR_Opr value);
 212   virtual void load_at_resolved(LIRAccess& access, LIR_Opr result);
 213 
 214   virtual LIR_Opr atomic_cmpxchg_at_resolved(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value);
 215 
 216   virtual LIR_Opr atomic_xchg_at_resolved(LIRAccess& access, LIRItem& value);
 217 
 218 public:
 219 
 220   virtual void generate_c1_runtime_stubs(BufferBlob* buffer_blob);
 221   virtual const char* rtcall_name_for_address(address entry);
 222 };
 223 
 224 #endif // SHARE_GC_SHENANDOAH_C1_SHENANDOAHBARRIERSETC1_HPP