1 /*
2 * Copyright (c) 2018, 2025, Oracle and/or its affiliates. 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_SHARED_C2_BARRIERSETC2_HPP
26 #define SHARE_GC_SHARED_C2_BARRIERSETC2_HPP
27
28 #include "memory/allocation.hpp"
29 #include "oops/accessDecorators.hpp"
30 #include "opto/loopnode.hpp"
31 #include "opto/machnode.hpp"
32 #include "opto/matcher.hpp"
33 #include "opto/memnode.hpp"
34 #include "utilities/globalDefinitions.hpp"
35
36 // This means the access is mismatched. This means the value of an access
37 // is not equivalent to the value pointed to by the address.
38 const DecoratorSet C2_MISMATCHED = DECORATOR_LAST << 1;
39 // The access may not be aligned to its natural size.
40 const DecoratorSet C2_UNALIGNED = DECORATOR_LAST << 2;
41 // The atomic cmpxchg is weak, meaning that spurious false negatives are allowed,
42 // but never false positives.
43 const DecoratorSet C2_WEAK_CMPXCHG = DECORATOR_LAST << 3;
44 // This denotes that a load has control dependency.
45 const DecoratorSet C2_CONTROL_DEPENDENT_LOAD = DECORATOR_LAST << 4;
46 // This denotes that a load that must be pinned, but may float above safepoints.
47 const DecoratorSet C2_UNKNOWN_CONTROL_LOAD = DECORATOR_LAST << 5;
48 // This denotes that the access is produced from the sun.misc.Unsafe intrinsics.
49 const DecoratorSet C2_UNSAFE_ACCESS = DECORATOR_LAST << 6;
50 // This denotes that the access mutates state.
51 const DecoratorSet C2_WRITE_ACCESS = DECORATOR_LAST << 7;
52 // This denotes that the access reads state.
53 const DecoratorSet C2_READ_ACCESS = DECORATOR_LAST << 8;
54 // A nearby allocation?
55 const DecoratorSet C2_TIGHTLY_COUPLED_ALLOC = DECORATOR_LAST << 9;
56 // Loads and stores from an arraycopy being optimized
57 const DecoratorSet C2_ARRAY_COPY = DECORATOR_LAST << 10;
58 // Loads from immutable memory
59 const DecoratorSet C2_IMMUTABLE_MEMORY = DECORATOR_LAST << 11;
60
61 class Compile;
62 class ConnectionGraph;
63 class GraphKit;
64 class IdealKit;
65 class Node;
66 class PhaseGVN;
67 class PhaseIdealLoop;
68 class PhaseMacroExpand;
69 class Type;
70 class TypePtr;
71 class Unique_Node_List;
72
73 // This class wraps a node and a type.
74 class C2AccessValue: public StackObj {
75 protected:
76 Node* _node;
77 const Type* _type;
78
79 public:
80 C2AccessValue(Node* node, const Type* type) :
81 _node(node),
82 _type(type) {}
83
84 Node* node() const { return _node; }
85 const Type* type() const { return _type; }
86
87 void set_node(Node* node) { _node = node; }
88 };
89
90 // This class wraps a node and a pointer type.
91 class C2AccessValuePtr: public C2AccessValue {
92
93 public:
94 C2AccessValuePtr(Node* node, const TypePtr* type) :
95 C2AccessValue(node, type) {}
96
97 const TypePtr* type() const { return _type->is_ptr(); }
98 };
99
100 // This class wraps a bunch of context parameters that are passed around in the
101 // BarrierSetC2 backend hierarchy, for loads and stores, to reduce boiler plate.
102 class C2Access: public StackObj {
103 protected:
104 DecoratorSet _decorators;
105 Node* _base;
106 C2AccessValuePtr& _addr;
107 Node* _raw_access;
108 BasicType _type;
109 uint8_t _barrier_data;
110
111 void fixup_decorators();
112
113 public:
114 C2Access(DecoratorSet decorators,
115 BasicType type, Node* base, C2AccessValuePtr& addr) :
116 _decorators(decorators),
117 _base(base),
118 _addr(addr),
119 _raw_access(nullptr),
120 _type(type),
121 _barrier_data(0)
122 {}
123
124 DecoratorSet decorators() const { return _decorators; }
125 Node* base() const { return _base; }
126 C2AccessValuePtr& addr() const { return _addr; }
127 BasicType type() const { return _type; }
128 bool is_oop() const { return is_reference_type(_type); }
129 bool is_raw() const { return (_decorators & AS_RAW) != 0; }
130 Node* raw_access() const { return _raw_access; }
131
132 uint8_t barrier_data() const { return _barrier_data; }
133 void set_barrier_data(uint8_t data) { _barrier_data = data; }
134
135 void set_raw_access(Node* raw_access) { _raw_access = raw_access; }
136 virtual void set_memory() {} // no-op for normal accesses, but not for atomic accesses.
137
138 MemNode::MemOrd mem_node_mo() const;
139 bool needs_cpu_membar() const;
140
141 virtual PhaseGVN& gvn() const = 0;
142 virtual bool is_parse_access() const { return false; }
143 virtual bool is_opt_access() const { return false; }
144 };
145
146 // C2Access for parse time calls to the BarrierSetC2 backend.
147 class C2ParseAccess: public C2Access {
148 protected:
149 GraphKit* _kit;
150 Node* _ctl;
151 const InlineTypeNode* _vt; // For flat, atomic accesses that might require GC barriers on oop fields
152
153 void* barrier_set_state() const;
154
155 public:
156 C2ParseAccess(GraphKit* kit, DecoratorSet decorators,
157 BasicType type, Node* base, C2AccessValuePtr& addr,
158 Node* ctl = nullptr, const InlineTypeNode* vt = nullptr) :
159 C2Access(decorators, type, base, addr),
160 _kit(kit),
161 _ctl(ctl),
162 _vt (vt) {
163 fixup_decorators();
164 }
165
166 GraphKit* kit() const { return _kit; }
167 Node* control() const;
168 const InlineTypeNode* vt() const { return _vt; }
169
170 virtual PhaseGVN& gvn() const;
171 virtual bool is_parse_access() const { return true; }
172 };
173
174 // This class wraps a bunch of context parameters that are passed around in the
175 // BarrierSetC2 backend hierarchy, for atomic accesses, to reduce boiler plate.
176 class C2AtomicParseAccess: public C2ParseAccess {
177 Node* _memory;
178 uint _alias_idx;
179
180 public:
181 C2AtomicParseAccess(GraphKit* kit, DecoratorSet decorators, BasicType type,
182 Node* base, C2AccessValuePtr& addr, uint alias_idx) :
183 C2ParseAccess(kit, decorators, type, base, addr),
184 _memory(nullptr),
185 _alias_idx(alias_idx) {}
186
187 // Set the memory node based on the current memory slice.
188 virtual void set_memory();
189
190 Node* memory() const { return _memory; }
191 uint alias_idx() const { return _alias_idx; }
192 };
193
194 // C2Access for optimization time calls to the BarrierSetC2 backend.
195 class C2OptAccess: public C2Access {
196 PhaseGVN& _gvn;
197 MergeMemNode* _mem;
198 Node* _ctl;
199
200 public:
201 C2OptAccess(PhaseGVN& gvn, Node* ctl, MergeMemNode* mem, DecoratorSet decorators,
202 BasicType type, Node* base, C2AccessValuePtr& addr) :
203 C2Access(decorators, type, base, addr),
204 _gvn(gvn), _mem(mem), _ctl(ctl) {
205 fixup_decorators();
206 }
207
208 MergeMemNode* mem() const { return _mem; }
209 Node* ctl() const { return _ctl; }
210
211 virtual PhaseGVN& gvn() const { return _gvn; }
212 virtual bool is_opt_access() const { return true; }
213 };
214
215 class BarrierSetC2State : public ArenaObj {
216 protected:
217 Node_Array _live;
218
219 public:
220 BarrierSetC2State(Arena* arena) : _live(arena) {}
221
222 RegMask* live(const Node* node) {
223 if (!node->is_Mach() || !needs_liveness_data(node->as_Mach())) {
224 // Don't need liveness for non-MachNodes or if the GC doesn't request it
225 return nullptr;
226 }
227 RegMask* live = (RegMask*)_live[node->_idx];
228 if (live == nullptr) {
229 live = new (Compile::current()->comp_arena()->AmallocWords(sizeof(RegMask))) RegMask();
230 _live.map(node->_idx, (Node*)live);
231 }
232
233 return live;
234 }
235
236 virtual bool needs_liveness_data(const MachNode* mach) const = 0;
237 virtual bool needs_livein_data() const = 0;
238 };
239
240 // This class represents the slow path in a C2 barrier. It is defined by a
241 // memory access, an entry point, and a continuation point (typically the end of
242 // the barrier). It provides a set of registers whose value is live across the
243 // barrier, and hence must be preserved across runtime calls from the stub.
244 class BarrierStubC2 : public ArenaObj {
245 protected:
246 const MachNode* _node;
247 Label _entry;
248 Label _continuation;
249 RegMask _preserve;
250
251 // Registers that are live-in/live-out of the entire memory access
252 // implementation (possibly including multiple barriers). Whether live-in or
253 // live-out registers are returned depends on
254 // BarrierSetC2State::needs_livein_data().
255 RegMask& live() const;
256
257 public:
258 BarrierStubC2(const MachNode* node);
259
260 // Entry point to the stub.
261 Label* entry();
262 // Return point from the stub (typically end of barrier).
263 Label* continuation();
264 // High-level, GC-specific barrier flags.
265 uint8_t barrier_data() const;
266
267 // Preserve the value in reg across runtime calls in this barrier.
268 void preserve(Register reg);
269 // Do not preserve the value in reg across runtime calls in this barrier.
270 void dont_preserve(Register reg);
271 // Set of registers whose value needs to be preserved across runtime calls in this barrier.
272 const RegMask& preserve_set() const;
273 };
274
275 // This is the top-level class for the backend of the Access API in C2.
276 // The top-level class is responsible for performing raw accesses. The
277 // various GC barrier sets inherit from the BarrierSetC2 class to sprinkle
278 // barriers into the accesses.
279 class BarrierSetC2: public CHeapObj<mtGC> {
280 protected:
281 virtual void resolve_address(C2Access& access) const;
282 virtual Node* store_at_resolved(C2Access& access, C2AccessValue& val) const;
283 virtual Node* load_at_resolved(C2Access& access, const Type* val_type) const;
284
285 virtual Node* atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
286 Node* new_val, const Type* val_type) const;
287 virtual Node* atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
288 Node* new_val, const Type* value_type) const;
289 virtual Node* atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
290 virtual Node* atomic_add_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const;
291 void pin_atomic_op(C2AtomicParseAccess& access) const;
292 void clone_in_runtime(PhaseMacroExpand* phase, ArrayCopyNode* ac,
293 address call_addr, const char* call_name) const;
294
295 public:
296 // This is the entry-point for the backend to perform accesses through the Access API.
297 virtual Node* store_at(C2Access& access, C2AccessValue& val) const;
298 virtual Node* load_at(C2Access& access, const Type* val_type) const;
299
300 virtual Node* atomic_cmpxchg_val_at(C2AtomicParseAccess& access, Node* expected_val,
301 Node* new_val, const Type* val_type) const;
302 virtual Node* atomic_cmpxchg_bool_at(C2AtomicParseAccess& access, Node* expected_val,
303 Node* new_val, const Type* val_type) const;
304 virtual Node* atomic_xchg_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const;
305 virtual Node* atomic_add_at(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const;
306
307 virtual void clone(GraphKit* kit, Node* src_base, Node* dst_base, Node* size, bool is_array) const;
308
309 virtual Node* obj_allocate(PhaseMacroExpand* macro, Node* mem, Node* toobig_false, Node* size_in_bytes,
310 Node*& i_o, Node*& needgc_ctrl,
311 Node*& fast_oop_ctrl, Node*& fast_oop_rawmem,
312 intx prefetch_lines) const;
313
314 virtual Node* ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const { return nullptr; }
315
316 // These are general helper methods used by C2
317 enum ArrayCopyPhase {
318 Parsing,
319 Optimization,
320 Expansion
321 };
322
323 virtual bool array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const { return false; }
324 virtual void clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const;
325
326 // Support for GC barriers emitted during parsing
327 virtual bool has_load_barrier_nodes() const { return false; }
328 virtual bool is_gc_pre_barrier_node(Node* node) const { return false; }
329 virtual bool is_gc_barrier_node(Node* node) const { return false; }
330 virtual Node* step_over_gc_barrier(Node* c) const { return c; }
331
332 // Support for macro expanded GC barriers
333 virtual void register_potential_barrier_node(Node* node) const { }
334 virtual void unregister_potential_barrier_node(Node* node) const { }
335 virtual void eliminate_gc_barrier(PhaseIterGVN* igvn, Node* node) const { }
336 virtual void eliminate_gc_barrier_data(Node* node) const { }
337 virtual void enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const {}
338 virtual void eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const {}
339
340 // Allow barrier sets to have shared state that is preserved across a compilation unit.
341 // This could for example comprise macro nodes to be expanded during macro expansion.
342 virtual void* create_barrier_state(Arena* comp_arena) const { return nullptr; }
343 // If the BarrierSetC2 state has barrier nodes in its compilation
344 // unit state to be expanded later, then now is the time to do so.
345 virtual bool expand_barriers(Compile* C, PhaseIterGVN& igvn) const { return false; }
346 virtual bool optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const { return false; }
347 virtual bool strip_mined_loops_expanded(LoopOptsMode mode) const { return false; }
348 virtual bool is_gc_specific_loop_opts_pass(LoopOptsMode mode) const { return false; }
349 // Estimated size of the node barrier in number of C2 Ideal nodes.
350 // This is used to guide heuristics in C2, e.g. whether to unroll a loop.
351 virtual uint estimated_barrier_size(const Node* node) const { return 0; }
352 // Whether the given store can be used to initialize a newly allocated object.
353 virtual bool can_initialize_object(const StoreNode* store) const { return true; }
354
355 enum CompilePhase {
356 BeforeOptimize,
357 BeforeMacroExpand,
358 BeforeCodeGen
359 };
360
361 #ifdef ASSERT
362 virtual void verify_gc_barriers(Compile* compile, CompilePhase phase) const {}
363 #endif
364
365 virtual bool final_graph_reshaping(Compile* compile, Node* n, uint opcode, Unique_Node_List& dead_nodes) const { return false; }
366
367 virtual bool escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const { return false; }
368 virtual bool escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const { return false; }
369 virtual bool escape_has_out_with_unsafe_object(Node* n) const { return false; }
370
371 virtual bool matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const { return false; };
372 virtual bool matcher_is_store_load_barrier(Node* x, uint xop) const { return false; }
373
374 // Whether the given phi node joins OOPs from fast and slow allocation paths.
375 static bool is_allocation(const Node* node);
376 // Elide GC barriers from a Mach node according to elide_dominated_barriers().
377 virtual void elide_dominated_barrier(MachNode* mach) const { }
378 // Elide GC barriers from instructions in 'accesses' if they are dominated by
379 // instructions in 'access_dominators' (according to elide_mach_barrier()) and
380 // there is no safepoint poll in between.
381 void elide_dominated_barriers(Node_List& accesses, Node_List& access_dominators) const;
382 virtual void late_barrier_analysis() const { }
383 virtual void compute_liveness_at_stubs() const;
384 virtual int estimate_stub_size() const { return 0; }
385 virtual void emit_stubs(CodeBuffer& cb) const { }
386
387 static int arraycopy_payload_base_offset(bool is_array);
388
389 #ifndef PRODUCT
390 virtual void dump_barrier_data(const MachNode* mach, outputStream* st) const {
391 st->print("%x", mach->barrier_data());
392 };
393 #endif
394 };
395
396 #endif // SHARE_GC_SHARED_C2_BARRIERSETC2_HPP