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