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 "utilities/growableArray.hpp"
31
32 static const uint8_t ShenandoahBarrierStrong = 1 << 0;
33 static const uint8_t ShenandoahBarrierWeak = 1 << 1;
34 static const uint8_t ShenandoahBarrierPhantom = 1 << 2;
35 static const uint8_t ShenandoahBarrierNative = 1 << 3;
36 static const uint8_t ShenandoahBarrierElided = 1 << 4;
37 static const uint8_t ShenandoahBarrierSATB = 1 << 5;
38 static const uint8_t ShenandoahBarrierCardMark = 1 << 6;
39 static const uint8_t ShenandoahBarrierNotNull = 1 << 7;
40
41 // Barrier data that implies real barriers, not additional metadata.
42 static const uint8_t ShenandoahBarriersReal =
43 ShenandoahBarrierStrong | // LRB strong
44 ShenandoahBarrierWeak | // LRB weak
45 ShenandoahBarrierPhantom | // LRB phantom
46 ShenandoahBarrierSATB | // SATB
47 ShenandoahBarrierCardMark; // Card Mark
48
49 class ShenandoahBarrierStubC2;
50
51 class ShenandoahBarrierSetC2State : public BarrierSetC2State {
52 GrowableArray<ShenandoahBarrierStubC2*>* _stubs;
53 int _stubs_start_offset;
54
55 public:
56 explicit ShenandoahBarrierSetC2State(Arena* comp_arena);
57
58 bool needs_liveness_data(const MachNode* mach) const override;
59 bool needs_livein_data() const override;
60
61 GrowableArray<ShenandoahBarrierStubC2*>* stubs() {
62 return _stubs;
63 }
64
65 void set_stubs_start_offset(int offset) {
66 _stubs_start_offset = offset;
67 }
68
69 int stubs_start_offset() {
70 return _stubs_start_offset;
71 }};
72
73 class ShenandoahBarrierSetC2 : public BarrierSetC2 {
74
75 static bool clone_needs_barrier(const TypeOopPtr* src_type, bool& is_oop_array);
76
77 static void refine_store(const Node* node);
78
79 protected:
80 virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
81 virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
82 virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
83 Node* new_val, const Type* val_type) const;
84 virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
85 Node* new_val, const Type* value_type) const;
86 virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
87
88 public:
89 static ShenandoahBarrierSetC2* bsc2();
90
91 ShenandoahBarrierSetC2State* state() const;
92
93 // This is the entry-point for the backend to perform accesses through the Access API.
94 virtual void clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const;
95 virtual void clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const;
96
97 // These are general helper methods used by C2
98 virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const;
99
100 // Support for GC barriers emitted during parsing
101 virtual bool expand_barriers(Compile* C, PhaseIterGVN& igvn) const;
102 virtual void final_refinement(Compile* C) const;
103
104 // Support for macro expanded GC barriers
105 virtual void eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const;
106 virtual void eliminate_gc_barrier_data(Node* node) const;
107
108 // Allow barrier sets to have shared state that is preserved across a compilation unit.
109 // This could for example comprise macro nodes to be expanded during macro expansion.
110 virtual void* create_barrier_state(Arena* comp_arena) const;
111
112 #ifdef ASSERT
113 virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const;
114 static void verify_gc_barrier_assert(bool cond, const char* msg, uint8_t bd, Node* n);
115 #endif
116
117 int estimate_stub_size() const /* override */;
118 void emit_stubs(CodeBuffer& cb) const /* override */;
119 void late_barrier_analysis() const /* override*/ {
120 compute_liveness_at_stubs();
121 }
122
123 static void print_barrier_data(outputStream* os, uint8_t data);
124 };
125
126 class ShenandoahBarrierStubC2 : public BarrierStubC2 {
127 protected:
128 explicit ShenandoahBarrierStubC2(const MachNode* node) : BarrierStubC2(node) {}
129 void register_stub();
130 public:
131 virtual void emit_code(MacroAssembler& masm) = 0;
132 };
133
134 class ShenandoahLoadBarrierStubC2 : public ShenandoahBarrierStubC2 {
135 Register const _dst;
136 Address const _src;
137 Register const _tmp;
138 const bool _narrow;
139 const bool _needs_load_ref_barrier;
140 const bool _needs_satb_barrier;
141
142 ShenandoahLoadBarrierStubC2(const MachNode* node, Register dst, Address src, bool narrow, Register tmp) :
143 ShenandoahBarrierStubC2(node), _dst(dst), _src(src), _tmp(tmp), _narrow(narrow),
144 _needs_load_ref_barrier(needs_load_ref_barrier(node)), _needs_satb_barrier(needs_satb_barrier(node)) {}
145
146 public:
147 static bool needs_barrier(const MachNode* node) {
148 return needs_load_ref_barrier(node) || needs_satb_barrier(node);
149 }
150 static bool needs_satb_barrier(const MachNode* node) {
151 return (node->barrier_data() & ShenandoahBarrierSATB) != 0;
152 }
153 static bool needs_load_ref_barrier(const MachNode* node) {
154 return (node->barrier_data() & (ShenandoahBarrierStrong | ShenandoahBarrierWeak | ShenandoahBarrierPhantom)) != 0;
155 }
156 static bool needs_load_ref_barrier_weak(const MachNode* node) {
157 return (node->barrier_data() & (ShenandoahBarrierWeak | ShenandoahBarrierPhantom)) != 0;
158 }
159 static bool src_not_null(const MachNode* node) {
160 return (node->barrier_data() & ShenandoahBarrierNotNull) != 0;
161 }
162
163 static ShenandoahLoadBarrierStubC2* create(const MachNode* node, Register dst, Address src, bool narrow, Register tmp);
164
165 void emit_code(MacroAssembler& masm) override;
166 };
167
168
169 class ShenandoahStoreBarrierStubC2 : public ShenandoahBarrierStubC2 {
170 Address const _dst;
171 Register const _src;
172 Register const _tmp;
173 const bool _dst_narrow;
174 const bool _src_narrow;
175
176 ShenandoahStoreBarrierStubC2(const MachNode* node, Address dst, bool dst_narrow, Register src, bool src_narrow, Register tmp) :
177 ShenandoahBarrierStubC2(node), _dst(dst), _src(src), _tmp(tmp), _dst_narrow(dst_narrow), _src_narrow(src_narrow) {}
178
179 public:
180 static bool needs_barrier(const MachNode* node) {
181 return needs_card_barrier(node) || needs_satb_barrier(node);
182 }
183 static bool needs_satb_barrier(const MachNode* node) {
184 return (node->barrier_data() & ShenandoahBarrierSATB) != 0;
185 }
186 static bool needs_card_barrier(const MachNode* node) {
187 return (node->barrier_data() & ShenandoahBarrierCardMark) != 0;
188 }
189 static bool src_not_null(const MachNode* node) {
190 return (node->barrier_data() & ShenandoahBarrierNotNull) != 0;
191 }
192
193 static ShenandoahStoreBarrierStubC2* create(const MachNode* node, Address dst, bool dst_narrow, Register src, bool src_narrow, Register tmp);
194
195 void emit_code(MacroAssembler& masm) override;
196 };
197
198
199 class ShenandoahLoadRefBarrierStubC2 : public ShenandoahBarrierStubC2 {
200 Register _obj;
201 Register _addr;
202 Register _tmp1;
203 Register _tmp2;
204 Register _tmp3;
205 bool _narrow;
206 ShenandoahLoadRefBarrierStubC2(const MachNode* node, Register obj, Register addr, Register tmp1, Register tmp2, Register tmp3, bool narrow) :
207 ShenandoahBarrierStubC2(node), _obj(obj), _addr(addr), _tmp1(tmp1), _tmp2(tmp2), _tmp3(tmp3), _narrow(narrow) {}
208 public:
209 static bool needs_barrier(const MachNode* node) {
210 return (node->barrier_data() & (ShenandoahBarrierStrong | ShenandoahBarrierWeak | ShenandoahBarrierPhantom | ShenandoahBarrierNative)) != 0;
211 }
212 static ShenandoahLoadRefBarrierStubC2* create(const MachNode* node, Register obj, Register addr, Register tmp1, Register tmp2, Register tmp3, bool narrow);
213 void emit_code(MacroAssembler& masm) override;
214 };
215
216 class ShenandoahSATBBarrierStubC2 : public ShenandoahBarrierStubC2 {
217 Register _addr;
218 Register _preval;
219 Register _tmp;
220 bool _encoded_preval;
221 ShenandoahSATBBarrierStubC2(const MachNode* node, Register addr, Register preval, Register tmp, bool encoded_preval) :
222 ShenandoahBarrierStubC2(node), _addr(addr), _preval(preval), _tmp(tmp), _encoded_preval(encoded_preval) {}
223
224 public:
225 static bool needs_barrier(const MachNode* node) {
226 return (node->barrier_data() & ShenandoahBarrierSATB) != 0;
227 }
228 static ShenandoahSATBBarrierStubC2* create(const MachNode* node, Register addr, Register preval, Register tmp, bool encoded_preval);
229
230 void emit_code(MacroAssembler& masm) override;
231 };
232
233 class ShenandoahCASBarrierSlowStubC2 : public ShenandoahBarrierStubC2 {
234 Register const _addr_reg;
235 Address const _addr;
236 Register const _expected;
237 Register const _new_val;
238 Register const _result;
239 Register const _tmp1;
240 Register const _tmp2;
241 bool const _cae;
242 bool const _acquire;
243 bool const _release;
244 bool const _weak;
245
246 explicit ShenandoahCASBarrierSlowStubC2(const MachNode* node, Register addr_reg, Address addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae, bool acquire, bool release, bool weak) :
247 ShenandoahBarrierStubC2(node),
248 _addr_reg(addr_reg), _addr(addr), _expected(expected), _new_val(new_val), _result(result), _tmp1(tmp1), _tmp2(tmp2), _cae(cae), _acquire(acquire), _release(release), _weak(weak) {}
249
250 public:
251 static ShenandoahCASBarrierSlowStubC2* create(const MachNode* node, Register addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae, bool acquire, bool release, bool weak);
252 static ShenandoahCASBarrierSlowStubC2* create(const MachNode* node, Address addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae);
253 void emit_code(MacroAssembler& masm) override;
254 };
255
256 #endif // SHARE_GC_SHENANDOAH_C2_SHENANDOAHBARRIERSETC2_HPP