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_C2_SHENANDOAHBARRIERSETC2_HPP
 26 #define SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP
 27 
 28 #include "gc/shared/c2/barrierSetC2.hpp"
 29 
 30 static const uint8_t ShenandoahBitStrong    = 1 << 0; // Barrier: LRB, strong
 31 static const uint8_t ShenandoahBitWeak      = 1 << 1; // Barrier: LRB, weak
 32 static const uint8_t ShenandoahBitPhantom   = 1 << 2; // Barrier: LRB, phantom
 33 static const uint8_t ShenandoahBitKeepAlive = 1 << 3; // Barrier: KeepAlive (SATB for stores, KA for loads)
 34 static const uint8_t ShenandoahBitCardMark  = 1 << 4; // Barrier: CM
 35 static const uint8_t ShenandoahBitNotNull   = 1 << 5; // Metadata: src/dst is definitely not null
 36 static const uint8_t ShenandoahBitNative    = 1 << 6; // Metadata: access is in native, not in heap
 37 static const uint8_t ShenandoahBitElided    = 1 << 7; // Metadata: some part of the barrier is elided
 38 
 39 // Barrier data that implies real barriers, not additional metadata.
 40 static const uint8_t ShenandoahBitsReal = ShenandoahBitStrong | ShenandoahBitWeak | ShenandoahBitPhantom |
 41                                           ShenandoahBitKeepAlive |
 42                                           ShenandoahBitCardMark;
 43 
 44 class MachNode;
 45 class ShenandoahBarrierStubC2;
 46 
 47 class ShenandoahBarrierSetC2State : public BarrierSetC2State {
 48   GrowableArray<ShenandoahBarrierStubC2*>* _stubs;
 49   int _trampoline_stubs_count;
 50   int _stubs_start_offset;
 51   int _stubs_current_total_size;
 52 
 53 public:
 54   explicit ShenandoahBarrierSetC2State(Arena* comp_arena);
 55 
 56   bool needs_liveness_data(const MachNode* mach) const override;
 57   bool needs_livein_data() const override;
 58 
 59   GrowableArray<ShenandoahBarrierStubC2*>* stubs() {
 60     return _stubs;
 61   }
 62 
 63   void inc_trampoline_stubs_count() {
 64     assert(_trampoline_stubs_count != INT_MAX, "Overflow");
 65     ++_trampoline_stubs_count;
 66   }
 67 
 68   int trampoline_stubs_count() {
 69     return _trampoline_stubs_count;
 70   }
 71 
 72   void set_stubs_start_offset(int offset) {
 73     _stubs_start_offset = offset;
 74   }
 75 
 76   int stubs_start_offset() {
 77     return _stubs_start_offset;
 78   }
 79 
 80   int inc_stubs_current_total_size(int size) {
 81     _stubs_current_total_size += size;
 82     return _stubs_current_total_size;
 83   }
 84 
 85   int stubs_current_total_size() {
 86     return _stubs_current_total_size;
 87   }
 88 };
 89 
 90 class ShenandoahBarrierSetC2 : public BarrierSetC2 {
 91 
 92   static bool clone_needs_barrier(const TypeOopPtr* src_type, bool& is_oop_array);
 93 
 94   static bool can_remove_load_barrier(Node* node);
 95 
 96   static uint8_t refine_load(Node* node, uint8_t bd);
 97   static uint8_t refine_store(Node* node, uint8_t bd);
 98 
 99   static bool is_Load(int opcode);
100   static bool is_Store(int opcode);
101   static bool is_LoadStore(int opcode);
102 
103 protected:
104   virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
105   virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
106   virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
107                                                Node* new_val, const Type* val_type) const;
108   virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
109                                                 Node* new_val, const Type* value_type) const;
110   virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
111 
112 public:
113   // This is the entry-point for the backend to perform accesses through the Access API.
114   virtual void clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const;
115   virtual void clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const;
116 
117   // These are general helper methods used by C2
118   virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone,
119       bool is_clone_instance, ArrayCopyPhase phase) const;
120 
121   // Support for macro expanded GC barriers
122   virtual void eliminate_gc_barrier(PhaseIterGVN* macro, Node* node) const;
123   virtual void eliminate_gc_barrier_data(Node* node) const;
124 
125   // Allow barrier sets to have shared state that is preserved across a compilation unit.
126   // This could for example comprise macro nodes to be expanded during macro expansion.
127   virtual void* create_barrier_state(Arena* comp_arena) const;
128 
129 #ifdef ASSERT
130   virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const;
131   static void verify_gc_barrier_assert(bool cond, const char* msg, uint8_t bd, Node* n);
132 #endif
133 
134   virtual int estimate_stub_size() const;
135   virtual void emit_stubs(CodeBuffer& cb) const;
136   virtual void late_barrier_analysis() const {
137     compute_liveness_at_stubs();
138     analyze_dominating_barriers();
139   }
140 
141   virtual void elide_dominated_barrier(MachNode* mach, MachNode* dominator) const;
142   virtual void analyze_dominating_barriers() const;
143   virtual void final_refinement(Compile* C) const;
144 
145   virtual uint estimated_barrier_size(const Node* node) const;
146 
147   static void print_barrier_data(outputStream* os, uint8_t data);
148 };
149 
150 class ShenandoahBarrierStubC2 : public BarrierStubC2 {
151   Register _obj;
152   Address const _addr;
153   Register const _tmp1;
154   Register const _tmp2;
155   const bool _do_load;
156   const bool _narrow;
157   const bool _needs_load_ref_barrier;
158   const bool _needs_load_ref_weak_barrier;
159   const bool _needs_keep_alive_barrier;
160   bool _needs_far_jump;
161 
162   static void register_stub(ShenandoahBarrierStubC2* stub);
163 
164   int available_gp_registers();
165   bool is_live_register(Register reg);
166   bool is_special_register(Register reg);
167   Register select_temp_register(bool& selected_live, Register skip_reg1 = noreg, Register skip_reg2 = noreg);
168 
169   void maybe_far_jump_if_zero(MacroAssembler& masm, Register reg);
170 
171   void patchable_jump(MacroAssembler& masm, char gc_state, bool jump_when_state, Register tmp1, Register tmp2, Label* L_target);
172 
173   void patchable_jump_if_gc_state(MacroAssembler& masm, const char gc_state, Register tmp1, Register tmp2, Label* L_target) {
174     patchable_jump(masm, gc_state, true, tmp1, tmp2, L_target);
175   }
176   void patchable_jump_if_not_gc_state(MacroAssembler& masm, const char gc_state, Register tmp1, Register tmp2, Label* L_target) {
177     patchable_jump(masm, gc_state, false, tmp1, tmp2, L_target);
178   }
179 
180   void enter_if_gc_state(MacroAssembler& masm, const char test_state, Register tmp1, Register tmp2);
181 
182   void keepalive(MacroAssembler& masm, Label* L_done);
183   void lrb(MacroAssembler& masm);
184 
185   static void cardtable(MacroAssembler& masm, Address addr, Register tmp1, Register tmp2);
186 
187   address keepalive_runtime_entry_addr();
188   address lrb_runtime_entry_addr();
189 
190   static ShenandoahBarrierStubC2* create(const MachNode* node, Register obj, Address addr, Register tmp1, Register tmp2, bool narrow, bool do_load);
191   void post_init();
192 
193   ShenandoahBarrierStubC2(const MachNode* node, Register obj, Address addr, Register tmp1, Register tmp2, bool narrow, bool do_load) :
194     BarrierStubC2(node),
195     _obj(obj),
196     _addr(addr),
197     _tmp1(tmp1),
198     _tmp2(tmp2),
199     _do_load(do_load),
200     _narrow(narrow),
201     _needs_load_ref_barrier(needs_load_ref_barrier(node)),
202     _needs_load_ref_weak_barrier(needs_load_ref_barrier_weak(node)),
203     _needs_keep_alive_barrier(needs_keep_alive_barrier(node)),
204     _needs_far_jump() {
205     assert(!_narrow || is_heap_access(node), "Only heap accesses can be narrow");
206     if (_tmp1 != noreg && _tmp2 != noreg) {
207       assert_different_registers(_tmp1, _tmp2, _obj, _addr.base(), _addr.index());
208     } else {
209       assert(_tmp1 == _tmp2, "should both be noreg");
210       assert_different_registers(_obj, _addr.base(), _addr.index());
211     }
212     post_init();
213   }
214 
215   static bool is_heap_access(const MachNode* node) {
216     return (node->barrier_data() & ShenandoahBitNative) == 0;
217   }
218   static bool needs_load_ref_barrier(const MachNode* node) {
219     return (node->barrier_data() & (ShenandoahBitStrong | ShenandoahBitWeak | ShenandoahBitPhantom)) != 0;
220   }
221   static bool needs_load_ref_barrier_weak(const MachNode* node) {
222     return (node->barrier_data() & (ShenandoahBitWeak | ShenandoahBitPhantom)) != 0;
223   }
224   static bool needs_keep_alive_barrier(const MachNode* node) {
225     return (node->barrier_data() & ShenandoahBitKeepAlive) != 0;
226   }
227   static bool needs_card_barrier(const MachNode* node) {
228     return (node->barrier_data() & ShenandoahBitCardMark) != 0;
229   }
230 
231 public:
232   static bool needs_slow_barrier(const MachNode* node) {
233     return needs_load_ref_barrier(node) || needs_keep_alive_barrier(node);
234   }
235 
236   static void       load_post(MacroAssembler* masm, const MachNode* node, Register obj, Address addr,  Register tmp1, Register tmp2, bool narrow);
237   static void       store_pre(MacroAssembler* masm, const MachNode* node, Address addr, Register tmp1, Register tmp2, Register tmp3, bool narrow);
238   static void      store_post(MacroAssembler* masm, const MachNode* node, Address addr, Register tmp1, Register tmp2);
239   static void  load_store_pre(MacroAssembler* masm, const MachNode* node, Address addr, Register tmp1, Register tmp2, Register tmp3, bool narrow);
240   static void load_store_post(MacroAssembler* masm, const MachNode* node, Address addr, Register tmp1, Register tmp2);
241 
242   void emit_code(MacroAssembler& masm);
243 };
244 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP