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 #include "gc/shared/gc_globals.hpp"
30 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
31 #include "gc/shenandoah/shenandoahRuntime.hpp"
32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
33 #include "opto/machnode.hpp"
34 #include "utilities/growableArray.hpp"
35
36 static const uint8_t ShenandoahBitStrong = 1 << 0; // Barrier: LRB, strong
37 static const uint8_t ShenandoahBitWeak = 1 << 1; // Barrier: LRB, weak
38 static const uint8_t ShenandoahBitPhantom = 1 << 2; // Barrier: LRB, phantom
39 static const uint8_t ShenandoahBitKeepAlive = 1 << 3; // Barrier: KeepAlive (SATB for stores, KA for loads)
40 static const uint8_t ShenandoahBitCardMark = 1 << 4; // Barrier: CM
41 static const uint8_t ShenandoahBitNotNull = 1 << 5; // Metadata: src/dst is not null
42 static const uint8_t ShenandoahBitNative = 1 << 6; // Metadata: access is in native, not in heap
43 static const uint8_t ShenandoahBitElided = 1 << 7; // Metadata: barrier is elided
44
45 // Barrier data that implies real barriers, not additional metadata.
46 static const uint8_t ShenandoahBitsReal = ShenandoahBitStrong | ShenandoahBitWeak | ShenandoahBitPhantom |
47 ShenandoahBitKeepAlive |
48 ShenandoahBitCardMark;
49
50 class ShenandoahBarrierStubC2;
51
52 class ShenandoahBarrierSetC2State : public BarrierSetC2State {
53 GrowableArray<ShenandoahBarrierStubC2*>* _stubs;
54 int _trampoline_stubs_count;
55 int _stubs_start_offset;
56 int _save_slots_offset;
57
58 public:
59 explicit ShenandoahBarrierSetC2State(Arena* comp_arena);
60
61 bool needs_liveness_data(const MachNode* mach) const override;
62 bool needs_livein_data() const override;
63
64 GrowableArray<ShenandoahBarrierStubC2*>* stubs() {
65 return _stubs;
66 }
67
68 void inc_trampoline_stubs_count() {
69 assert(_trampoline_stubs_count != INT_MAX, "Overflow");
70 ++_trampoline_stubs_count;
71 }
72
73 int trampoline_stubs_count() {
74 return _trampoline_stubs_count;
75 }
76
77 void set_stubs_start_offset(int offset) {
78 _stubs_start_offset = offset;
79 }
80
81 int stubs_start_offset() {
82 return _stubs_start_offset;
83 }
84
85 void set_save_slots_stack_offset(int offset) {
86 _save_slots_offset = offset;
87 }
88
89 int save_slots_stack_offset() {
90 assert(_save_slots_offset >= 0, "should be set");
91 return _save_slots_offset;
92 }
93 };
94
95 class ShenandoahBarrierSetC2 : public BarrierSetC2 {
96
97 static bool clone_needs_barrier(const TypeOopPtr* src_type, bool& is_oop_array);
98
99 static bool can_remove_load_barrier(Node* node);
100
101 static void refine_load(Node* node);
102 static void refine_store(const Node* node);
103
104 static const TypeFunc* _write_barrier_pre_Type;
105 static const TypeFunc* _clone_barrier_Type;
106 static const TypeFunc* _load_reference_barrier_Type;
107 static void make_write_barrier_pre_Type();
108 static void make_clone_barrier_Type();
109 static void make_load_reference_barrier_Type();
110
111 protected:
112 virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
113 virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
114 virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
115 Node* new_val, const Type* val_type) const;
116 virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
117 Node* new_val, const Type* value_type) const;
118 virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
119
120 public:
121 static ShenandoahBarrierSetC2* bsc2();
122
123 ShenandoahBarrierSetC2State* state() const;
124
125 // This is the entry-point for the backend to perform accesses through the Access API.
126 virtual void clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const;
127 virtual void clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const;
128
129 // These are general helper methods used by C2
130 virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone,
131 bool is_clone_instance, ArrayCopyPhase phase) const;
132
133 // Support for GC barriers emitted during parsing
134 virtual bool expand_barriers(Compile* C, PhaseIterGVN& igvn) const;
135
136 // Support for macro expanded GC barriers
137 virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const;
138 virtual void eliminate_gc_barrier_data(Node* node) const;
139
140 // Allow barrier sets to have shared state that is preserved across a compilation unit.
141 // This could for example comprise macro nodes to be expanded during macro expansion.
142 virtual void* create_barrier_state(Arena* comp_arena) const;
143
144 #ifdef ASSERT
145 virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const;
146 static void verify_gc_barrier_assert(bool cond, const char* msg, uint8_t bd, Node* n);
147 #endif
148
149 int reserved_slots() const { return ShenandoahReservedStackSlots; }
150 int estimate_stub_size() const /* override */;
151 void emit_stubs(CodeBuffer& cb) const /* override */;
152 void late_barrier_analysis() const /* override*/ {
153 compute_liveness_at_stubs();
154 analyze_dominating_barriers();
155 }
156
157 void elide_dominated_barrier(MachNode* mach) const;
158 void analyze_dominating_barriers() const;
159 void strip_extra_data(const Node* node) const;
160 void strip_extra_data(Node_List& accesses) const;
161
162 virtual uint estimated_barrier_size(const Node* node) const;
163
164 static void print_barrier_data(outputStream* os, uint8_t data);
165 };
166
167 class ShenandoahBarrierStubC2 : public BarrierStubC2 {
168 Register _obj;
169 Address const _addr;
170 const bool _do_load;
171 const bool _narrow;
172 const bool _maybe_null;
173 const bool _needs_load_ref_barrier;
174 const bool _needs_load_ref_weak_barrier;
175 const bool _needs_keep_alive_barrier;
176 const int _fastpath_branch_offset;
177 bool _use_trampoline;
178 Label _trampoline_entry;
179 bool _do_emit_actual;
180 int _save_slots_idx;
181
182 GrowableArray<Register> _live_gp;
183 bool _has_live_vector_registers;
184
185 static void register_stub(ShenandoahBarrierStubC2* stub);
186 static void inc_trampoline_stubs_count();
187 static int trampoline_stubs_count();
188 static int stubs_start_offset();
189 static int save_slots_stack_offset();
190
191 // Manage save slots on stack. We cannot move SP freely when in statically-sized
192 // C2 frame. These methods emulate the stack where a stub can save registers temporarily
193 // without moving SP.
194 void push_save_register(MacroAssembler& masm, Register reg);
195 void pop_save_register(MacroAssembler& masm, Register reg);
196 bool push_save_register_if_live(MacroAssembler& masm, Register reg);
197 int push_save_slot();
198 int pop_save_slot();
199
200 bool has_save_space_for_live_gp_registers(bool skip_crarg0, bool skip_crarg1, bool skip_rax);
201 bool has_live_vector_registers();
202 bool is_live(Register reg);
203 Register select_temp_register(bool& selected_live, Address addr, Register reg1);
204
205 void load_and_decode(MacroAssembler& masm, Label& target_if_null);
206 void reencode_if_needed(MacroAssembler& masm);
207
208 void keepalive(MacroAssembler& masm, Register obj, Register tmp, Label* L_done = nullptr);
209 void lrb(MacroAssembler& masm, Register obj, Address addr, Register tmp, Label* L_done = nullptr);
210
211 void save_live_gp_regs(MacroAssembler& masm, bool skip_crarg0, bool skip_crarg1, bool skip_rax);
212 void restore_live_gp_regs(MacroAssembler& masm, bool skip_crarg0, bool skip_crarg1, bool skip_rax);
213
214 enum SaveMode {
215 Nothing,
216 GP,
217 All
218 };
219
220 address keepalive_runtime_entry_addr(SaveMode mode = SaveMode::All);
221 address lrb_runtime_entry_addr(SaveMode mode = SaveMode::All);
222
223 void post_init(int offset);
224
225 public:
226 ShenandoahBarrierStubC2(const MachNode* node, Register obj, Address addr, bool narrow, bool do_load, int offset) :
227 BarrierStubC2(node),
228 _obj(obj),
229 _addr(addr),
230 _do_load(do_load),
231 _narrow(narrow),
232 _maybe_null(maybe_null(node)),
233 _needs_load_ref_barrier(needs_load_ref_barrier(node)),
234 _needs_load_ref_weak_barrier(needs_load_ref_barrier_weak(node)),
235 _needs_keep_alive_barrier(needs_keep_alive_barrier(node)),
236 _fastpath_branch_offset(offset),
237 _use_trampoline(),
238 _trampoline_entry(),
239 _do_emit_actual(),
240 _save_slots_idx(0),
241 _has_live_vector_registers() {
242 assert(!_narrow || is_heap_access(node), "Only heap accesses can be narrow");
243 post_init(offset);
244 }
245
246 static bool is_heap_access(const MachNode* node) {
247 return (node->barrier_data() & ShenandoahBitNative) == 0;
248 }
249 static bool needs_slow_barrier(const MachNode* node) {
250 return needs_load_ref_barrier(node) || needs_keep_alive_barrier(node);
251 }
252 static bool needs_load_ref_barrier(const MachNode* node) {
253 return (node->barrier_data() & (ShenandoahBitStrong | ShenandoahBitWeak | ShenandoahBitPhantom)) != 0;
254 }
255 static bool needs_load_ref_barrier_weak(const MachNode* node) {
256 return (node->barrier_data() & (ShenandoahBitWeak | ShenandoahBitPhantom)) != 0;
257 }
258 static bool needs_keep_alive_barrier(const MachNode* node) {
259 return (node->barrier_data() & ShenandoahBitKeepAlive) != 0;
260 }
261 static bool needs_card_barrier(const MachNode* node) {
262 return (node->barrier_data() & ShenandoahBitCardMark) != 0;
263 }
264 static bool maybe_null(const MachNode* node) {
265 return (node->barrier_data() & ShenandoahBitNotNull) == 0;
266 }
267
268 static ShenandoahBarrierStubC2* create(const MachNode* node, Register obj, Address addr, bool narrow, bool do_load, int offset = 0);
269 void emit_code(MacroAssembler& masm);
270
271 void enter_if_gc_state(MacroAssembler& masm, const char test_state);
272 };
273 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP