1 /*
2 * Copyright (c) 2018, 2023, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "classfile/javaClasses.inline.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
29 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
30 #include "gc/shenandoah/shenandoahForwarding.hpp"
31 #include "gc/shenandoah/shenandoahHeap.hpp"
32 #include "gc/shenandoah/shenandoahRuntime.hpp"
33 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
34 #include "opto/arraycopynode.hpp"
35 #include "opto/escape.hpp"
36 #include "opto/graphKit.hpp"
37 #include "opto/idealKit.hpp"
38 #include "opto/macro.hpp"
39 #include "opto/narrowptrnode.hpp"
40 #include "opto/output.hpp"
41 #include "opto/rootnode.hpp"
42 #include "opto/runtime.hpp"
43
44 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {
45 return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());
46 }
47
48 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena) :
49 BarrierSetC2State(comp_arena),
50 _stubs(new (comp_arena) GrowableArray<ShenandoahBarrierStubC2*>(comp_arena, 8, 0, nullptr)),
51 _stubs_start_offset(0) {
52 }
53
54 #define __ kit->
55
56 static bool satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr,
57 BasicType bt, uint adr_idx) {
58 intptr_t offset = 0;
59 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
60 AllocateNode* alloc = AllocateNode::Ideal_allocation(base);
61
62 if (offset == Type::OffsetBot) {
63 return false; // cannot unalias unless there are precise offsets
64 }
65
66 if (alloc == nullptr) {
67 return false; // No allocation found
68 }
69
70 intptr_t size_in_bytes = type2aelembytes(bt);
71
72 Node* mem = __ memory(adr_idx); // start searching here...
73
74 for (int cnt = 0; cnt < 50; cnt++) {
75
76 if (mem->is_Store()) {
77
78 Node* st_adr = mem->in(MemNode::Address);
79 intptr_t st_offset = 0;
80 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);
81
82 if (st_base == nullptr) {
83 break; // inscrutable pointer
84 }
85
86 // Break we have found a store with same base and offset as ours so break
87 if (st_base == base && st_offset == offset) {
88 break;
89 }
90
91 if (st_offset != offset && st_offset != Type::OffsetBot) {
92 const int MAX_STORE = BytesPerLong;
93 if (st_offset >= offset + size_in_bytes ||
94 st_offset <= offset - MAX_STORE ||
95 st_offset <= offset - mem->as_Store()->memory_size()) {
96 // Success: The offsets are provably independent.
97 // (You may ask, why not just test st_offset != offset and be done?
98 // The answer is that stores of different sizes can co-exist
99 // in the same sequence of RawMem effects. We sometimes initialize
100 // a whole 'tile' of array elements with a single jint or jlong.)
101 mem = mem->in(MemNode::Memory);
102 continue; // advance through independent store memory
103 }
104 }
105
106 if (st_base != base
107 && MemNode::detect_ptr_independence(base, alloc, st_base,
108 AllocateNode::Ideal_allocation(st_base),
109 phase)) {
110 // Success: The bases are provably independent.
111 mem = mem->in(MemNode::Memory);
112 continue; // advance through independent store memory
113 }
114 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
115
116 InitializeNode* st_init = mem->in(0)->as_Initialize();
117 AllocateNode* st_alloc = st_init->allocation();
118
119 // Make sure that we are looking at the same allocation site.
120 // The alloc variable is guaranteed to not be null here from earlier check.
121 if (alloc == st_alloc) {
122 // Check that the initialization is storing null so that no previous store
123 // has been moved up and directly write a reference
124 Node* captured_store = st_init->find_captured_store(offset,
125 type2aelembytes(T_OBJECT),
126 phase);
127 if (captured_store == nullptr || captured_store == st_init->zero_memory()) {
128 return true;
129 }
130 }
131 }
132
133 // Unless there is an explicit 'continue', we must bail out here,
134 // because 'mem' is an inscrutable memory state (e.g., a call).
135 break;
136 }
137
138 return false;
139 }
140
141 static bool shenandoah_can_remove_post_barrier(GraphKit* kit, PhaseValues* phase, Node* store_ctrl, Node* adr) {
142 intptr_t offset = 0;
143 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
144 AllocateNode* alloc = AllocateNode::Ideal_allocation(base);
145
146 if (offset == Type::OffsetBot) {
147 return false; // Cannot unalias unless there are precise offsets.
148 }
149 if (alloc == nullptr) {
150 return false; // No allocation found.
151 }
152
153 Node* mem = store_ctrl; // Start search from Store node.
154 if (mem->is_Proj() && mem->in(0)->is_Initialize()) {
155 InitializeNode* st_init = mem->in(0)->as_Initialize();
156 AllocateNode* st_alloc = st_init->allocation();
157 // Make sure we are looking at the same allocation
158 if (alloc == st_alloc) {
159 return true;
160 }
161 }
162
163 return false;
164 }
165
166 bool ShenandoahBarrierSetC2::is_shenandoah_clone_call(Node* call) {
167 return call->is_CallLeaf() &&
168 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::clone_barrier);
169 }
170
171 const TypeFunc* ShenandoahBarrierSetC2::clone_barrier_Type() {
172 const Type **fields = TypeTuple::fields(1);
173 fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop
174 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
175
176 // create result type (range)
177 fields = TypeTuple::fields(0);
178 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
179
180 return TypeFunc::make(domain, range);
181 }
182
183 static uint8_t get_store_barrier(C2Access& access) {
184 if (!access.is_parse_access()) {
185 // Only support for eliding barriers at parse time for now.
186 return ShenandoahBarrierSATB | ShenandoahBarrierCardMark;
187 }
188 GraphKit* kit = (static_cast<C2ParseAccess&>(access)).kit();
189 Node* ctl = kit->control();
190 Node* adr = access.addr().node();
191 uint adr_idx = kit->C->get_alias_index(access.addr().type());
192 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory");
193
194 bool can_remove_pre_barrier = satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, access.type(), adr_idx);
195
196 // We can skip marks on a freshly-allocated object in Eden. Keep this code in
197 // sync with CardTableBarrierSet::on_slowpath_allocation_exit. That routine
198 // informs GC to take appropriate compensating steps, upon a slow-path
199 // allocation, so as to make this card-mark elision safe.
200 // The post-barrier can also be removed if null is written. This case is
201 // handled by ShenandoahBarrierSetC2::expand_barriers, which runs at the end of C2's
202 // platform-independent optimizations to exploit stronger type information.
203 bool can_remove_post_barrier = ReduceInitialCardMarks &&
204 ((access.base() == kit->just_allocated_object(ctl)) ||
205 shenandoah_can_remove_post_barrier(kit, &kit->gvn(), ctl, adr));
206
207 int barriers = 0;
208 if (!can_remove_pre_barrier) {
209 barriers |= ShenandoahBarrierSATB;
210 } else {
211 barriers |= ShenandoahBarrierElided;
212 }
213
214 if (!can_remove_post_barrier) {
215 barriers |= ShenandoahBarrierCardMark;
216 } else {
217 barriers |= ShenandoahBarrierElided;
218 }
219
220 return barriers;
221 }
222
223 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
224 DecoratorSet decorators = access.decorators();
225 bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
226 bool in_heap = (decorators & IN_HEAP) != 0;
227 bool tightly_coupled_alloc = (decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0;
228 bool needs_pre_barrier = access.is_oop() && (in_heap || anonymous);
229 // Pre-barriers are unnecessary for tightly-coupled initialization stores.
230 bool can_be_elided = needs_pre_barrier && tightly_coupled_alloc && ReduceInitialCardMarks;
231 bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
232 if (needs_pre_barrier) {
233 if (can_be_elided) {
234 access.set_barrier_data(access.barrier_data() & ~ShenandoahBarrierSATB);
235 access.set_barrier_data(access.barrier_data() | ShenandoahBarrierElided);
236 } else {
237 access.set_barrier_data(get_store_barrier(access));
238 }
239 }
240 if (no_keepalive) {
241 // No keep-alive means no need for the pre-barrier.
242 access.set_barrier_data(access.barrier_data() & ~ShenandoahBarrierSATB);
243 }
244 return BarrierSetC2::store_at_resolved(access, val);
245 }
246
247 static void set_barrier_data(C2Access& access) {
248 if (!access.is_oop()) {
249 return;
250 }
251
252 if (access.decorators() & C2_TIGHTLY_COUPLED_ALLOC) {
253 access.set_barrier_data(ShenandoahBarrierElided);
254 return;
255 }
256
257 uint8_t barrier_data = 0;
258
259 if (access.decorators() & ON_PHANTOM_OOP_REF) {
260 barrier_data |= ShenandoahBarrierPhantom;
261 } else if (access.decorators() & ON_WEAK_OOP_REF) {
262 barrier_data |= ShenandoahBarrierWeak;
263 } else {
264 barrier_data |= ShenandoahBarrierStrong;
265 }
266
267 if (access.decorators() & IN_NATIVE) {
268 barrier_data |= ShenandoahBarrierNative;
269 }
270
271 access.set_barrier_data(barrier_data);
272 }
273
274 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
275 // 1: non-reference load, no additional barrier is needed
276 if (!access.is_oop()) {
277 return BarrierSetC2::load_at_resolved(access, val_type);
278 }
279
280 // 2. Set barrier data for LRB.
281 set_barrier_data(access);
282
283 // 3. If we are reading the value of the referent field of a Reference object, we
284 // need to record the referent in an SATB log buffer using the pre-barrier
285 // mechanism.
286 DecoratorSet decorators = access.decorators();
287 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0;
288 bool on_phantom = (decorators & ON_PHANTOM_OOP_REF) != 0;
289 bool no_keepalive = (decorators & AS_NO_KEEPALIVE) != 0;
290 // If we are reading the value of the referent field of a Reference object, we
291 // need to record the referent in an SATB log buffer using the pre-barrier
292 // mechanism. Also we need to add a memory barrier to prevent commoning reads
293 // from this field across safepoints, since GC can change its value.
294 uint8_t barriers = access.barrier_data();
295 bool need_read_barrier = ((on_weak || on_phantom) && !no_keepalive);
296 if (access.is_oop() && need_read_barrier) {
297 barriers |= ShenandoahBarrierSATB;
298 }
299 access.set_barrier_data(barriers);
300
301 return BarrierSetC2::load_at_resolved(access, val_type);
302 }
303
304 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
305 Node* new_val, const Type* value_type) const {
306 if (ShenandoahCASBarrier) {
307 set_barrier_data(access);
308 }
309
310 if (access.is_oop()) {
311 access.set_barrier_data(access.barrier_data() | ShenandoahBarrierSATB | ShenandoahBarrierCardMark);
312 }
313 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
314 }
315
316 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
317 Node* new_val, const Type* value_type) const {
318 if (ShenandoahCASBarrier) {
319 set_barrier_data(access);
320 }
321 GraphKit* kit = access.kit();
322 if (access.is_oop()) {
323 access.set_barrier_data(access.barrier_data() | ShenandoahBarrierSATB | ShenandoahBarrierCardMark);
324 }
325 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
326 }
327
328 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const {
329 if (access.is_oop()) {
330 access.set_barrier_data(ShenandoahBarrierStrong | ShenandoahBarrierSATB | ShenandoahBarrierCardMark);
331 }
332 return BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);
333 }
334
335
336 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const {
337 return is_shenandoah_clone_call(node);
338 }
339
340 static void refine_barrier_by_new_val_type(const Node* n) {
341 if (n->Opcode() != Op_StoreP && n->Opcode() != Op_StoreN) {
342 return;
343 }
344 MemNode* store = n->as_Mem();
345 const Node* newval = n->in(MemNode::ValueIn);
346 assert(newval != nullptr, "");
347 const Type* newval_bottom = newval->bottom_type();
348 TypePtr::PTR newval_type = newval_bottom->make_ptr()->ptr();
349 uint8_t barrier_data = store->barrier_data();
350 if (!newval_bottom->isa_oopptr() &&
351 !newval_bottom->isa_narrowoop() &&
352 newval_type != TypePtr::Null) {
353 // newval is neither an OOP nor null, so there is no barrier to refine.
354 assert(barrier_data == 0, "non-OOP stores should have no barrier data");
355 return;
356 }
357 if (barrier_data == 0) {
358 // No barrier to refine.
359 return;
360 }
361 if (newval_type == TypePtr::Null) {
362 // Simply elide post-barrier if writing null.
363 barrier_data &= ~ShenandoahBarrierCardMark;
364 barrier_data &= ~ShenandoahBarrierCardMarkNotNull;
365 } else if ((barrier_data & ShenandoahBarrierCardMark) != 0 &&
366 newval_type == TypePtr::NotNull) {
367 // If the post-barrier has not been elided yet (e.g. due to newval being
368 // freshly allocated), mark it as not-null (simplifies barrier tests and
369 // compressed OOPs logic).
370 barrier_data |= ShenandoahBarrierCardMarkNotNull;
371 }
372 store->set_barrier_data(barrier_data);
373 }
374
375 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
376 ResourceMark rm;
377 VectorSet visited;
378 Node_List worklist;
379 worklist.push(C->root());
380 while (worklist.size() > 0) {
381 Node* n = worklist.pop();
382 if (visited.test_set(n->_idx)) {
383 continue;
384 }
385 refine_barrier_by_new_val_type(n);
386 for (uint j = 0; j < n->req(); j++) {
387 Node* in = n->in(j);
388 if (in != nullptr) {
389 worklist.push(in);
390 }
391 }
392 }
393 return false;
394 }
395
396 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
397 bool is_oop = is_reference_type(type);
398 if (!is_oop) {
399 return false;
400 }
401 if (ShenandoahSATBBarrier && tightly_coupled_alloc) {
402 if (phase == Optimization) {
403 return false;
404 }
405 return !is_clone;
406 }
407 return true;
408 }
409
410 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) {
411 const TypeOopPtr* src_type = gvn.type(src)->is_oopptr();
412 if (src_type->isa_instptr() != nullptr) {
413 ciInstanceKlass* ik = src_type->is_instptr()->instance_klass();
414 if ((src_type->klass_is_exact() || !ik->has_subklass()) && !ik->has_injected_fields()) {
415 if (ik->has_object_fields()) {
416 return true;
417 } else {
418 if (!src_type->klass_is_exact()) {
419 Compile::current()->dependencies()->assert_leaf_type(ik);
420 }
421 }
422 } else {
423 return true;
424 }
425 } else if (src_type->isa_aryptr()) {
426 BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type();
427 if (is_reference_type(src_elem, true)) {
428 return true;
429 }
430 } else {
431 return true;
432 }
433 return false;
434 }
435
436 void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {
437 Node* ctrl = ac->in(TypeFunc::Control);
438 Node* mem = ac->in(TypeFunc::Memory);
439 Node* src_base = ac->in(ArrayCopyNode::Src);
440 Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
441 Node* dest_base = ac->in(ArrayCopyNode::Dest);
442 Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
443 Node* length = ac->in(ArrayCopyNode::Length);
444
445 Node* src = phase->basic_plus_adr(src_base, src_offset);
446 Node* dest = phase->basic_plus_adr(dest_base, dest_offset);
447
448 if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) {
449 // Check if heap is has forwarded objects. If it does, we need to call into the special
450 // routine that would fix up source references before we can continue.
451
452 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
453 Node* region = new RegionNode(PATH_LIMIT);
454 Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM);
455
456 Node* thread = phase->transform_later(new ThreadLocalNode());
457 Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
458 Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset));
459
460 uint gc_state_idx = Compile::AliasIdxRaw;
461 const TypePtr* gc_state_adr_type = nullptr; // debug-mode-only argument
462 DEBUG_ONLY(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));
463
464 Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered));
465 Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(ShenandoahHeap::HAS_FORWARDED)));
466 Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT)));
467 Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne));
468
469 IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If();
470 Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff));
471 Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff));
472
473 // Heap is stable, no need to do anything additional
474 region->init_req(_heap_stable, stable_ctrl);
475 mem_phi->init_req(_heap_stable, mem);
476
477 // Heap is unstable, call into clone barrier stub
478 Node* call = phase->make_leaf_call(unstable_ctrl, mem,
479 ShenandoahBarrierSetC2::clone_barrier_Type(),
480 CAST_FROM_FN_PTR(address, ShenandoahRuntime::clone_barrier),
481 "shenandoah_clone",
482 TypeRawPtr::BOTTOM,
483 src_base);
484 call = phase->transform_later(call);
485
486 ctrl = phase->transform_later(new ProjNode(call, TypeFunc::Control));
487 mem = phase->transform_later(new ProjNode(call, TypeFunc::Memory));
488 region->init_req(_heap_unstable, ctrl);
489 mem_phi->init_req(_heap_unstable, mem);
490
491 // Wire up the actual arraycopy stub now
492 ctrl = phase->transform_later(region);
493 mem = phase->transform_later(mem_phi);
494
495 const char* name = "arraycopy";
496 call = phase->make_leaf_call(ctrl, mem,
497 OptoRuntime::fast_arraycopy_Type(),
498 phase->basictype2arraycopy(T_LONG, nullptr, nullptr, true, name, true),
499 name, TypeRawPtr::BOTTOM,
500 src, dest, length
501 LP64_ONLY(COMMA phase->top()));
502 call = phase->transform_later(call);
503
504 // Hook up the whole thing into the graph
505 phase->igvn().replace_node(ac, call);
506 } else {
507 BarrierSetC2::clone_at_expansion(phase, ac);
508 }
509 }
510
511
512 // Support for macro expanded GC barriers
513 void ShenandoahBarrierSetC2::eliminate_gc_barrier_data(Node* node) const {
514 if (node->is_LoadStore()) {
515 LoadStoreNode* loadstore = node->as_LoadStore();
516 loadstore->set_barrier_data(0);
517 } else if (node->is_Mem()) {
518 MemNode* mem = node->as_Mem();
519 mem->set_barrier_data(0);
520 }
521 }
522
523 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
524 eliminate_gc_barrier_data(node);
525 }
526
527 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {
528 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);
529 }
530
531 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {
532 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
533 }
534
535 #ifdef ASSERT
536 void ShenandoahBarrierSetC2::report_verify_failure(bool failed, const char* msg, Node* n) {
537 if (failed) {
538 tty->print_cr("----------------------------------- IDX %d -----------------------------------", n->_idx);
539 n->dump(3);
540 tty->print_cr("---------------------------------------------------------------------------------");
541 fatal("%s", msg);
542 }
543 }
544
545 void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const {
546 if (!ShenandoahVerifyOptoBarriers) {
547 return;
548 }
549
550 Unique_Node_List wq;
551 Node_Stack phis(0);
552 VectorSet visited;
553
554 wq.push(compile->root());
555 for (uint next = 0; next < wq.size(); next++) {
556 Node *n = wq.at(next);
557 int opc = n->Opcode();
558
559 if (opc == Op_LoadP || opc == Op_LoadN) {
560 const TypePtr* adr_type = n->as_Load()->adr_type();
561
562 if (!adr_type->isa_oopptr()) {
563 continue;
564 } else if (adr_type->isa_instptr() &&
565 adr_type->is_instptr()->instance_klass()->is_subtype_of(Compile::current()->env()->Reference_klass()) &&
566 adr_type->is_instptr()->offset() == java_lang_ref_Reference::referent_offset()) {
567 continue;
568 } else {
569 report_verify_failure(n->as_Load()->barrier_data() == 0, "Load should have barriers.", n);
570 }
571 } else if (opc == Op_StoreP || opc == Op_StoreN) {
572 const TypePtr* adr_type = n->as_Store()->adr_type();
573 if (adr_type->isa_oopptr() && n->in(MemNode::ValueIn)->bottom_type()->make_oopptr()) {
574 const TypePtr* adr_type = n->as_Store()->in(MemNode::Memory)->adr_type();
575 if (adr_type->isa_oopptr()) {
576 report_verify_failure(n->as_Store()->barrier_data() == 0, "Store should have barrier data.", n);
577 }
578 }
579 } else if (n->is_LoadStore()) {
580 report_verify_failure(n->bottom_type()->make_oopptr() && n->as_LoadStore()->barrier_data() == 0, "LoadStore should have barrier data.", n);
581 }
582
583 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
584 Node* m = n->fast_out(i);
585 wq.push(m);
586 }
587 }
588 }
589 #endif
590
591 static ShenandoahBarrierSetC2State* barrier_set_state() {
592 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
593 }
594
595 int ShenandoahBarrierSetC2::estimate_stub_size() const {
596 Compile* const C = Compile::current();
597 BufferBlob* const blob = C->output()->scratch_buffer_blob();
598 GrowableArray<ShenandoahBarrierStubC2*>* const stubs = barrier_set_state()->stubs();
599 int size = 0;
600
601 for (int i = 0; i < stubs->length(); i++) {
602 CodeBuffer cb(blob->content_begin(), checked_cast<CodeBuffer::csize_t>((address)C->output()->scratch_locs_memory() - blob->content_begin()));
603 MacroAssembler masm(&cb);
604 stubs->at(i)->emit_code(masm);
605 size += cb.insts_size();
606 }
607
608 return size;
609 }
610
611 void ShenandoahBarrierSetC2::emit_stubs(CodeBuffer& cb) const {
612 MacroAssembler masm(&cb);
613 GrowableArray<ShenandoahBarrierStubC2*>* const stubs = barrier_set_state()->stubs();
614 barrier_set_state()->set_stubs_start_offset(masm.offset());
615
616 for (int i = 0; i < stubs->length(); i++) {
617 // Make sure there is enough space in the code buffer
618 if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::MAX_inst_size) && cb.blob() == nullptr) {
619 ciEnv::current()->record_failure("CodeCache is full");
620 return;
621 }
622
623 stubs->at(i)->emit_code(masm);
624 }
625
626 masm.flush();
627
628 }
629
630 void ShenandoahBarrierStubC2::register_stub() {
631 if (!Compile::current()->output()->in_scratch_emit_size()) {
632 barrier_set_state()->stubs()->append(this);
633 }
634 }
635
636 ShenandoahLoadRefBarrierStubC2* ShenandoahLoadRefBarrierStubC2::create(const MachNode* node, Register obj, Register addr, Register tmp1, Register tmp2, Register tmp3, bool narrow) {
637 auto* stub = new (Compile::current()->comp_arena()) ShenandoahLoadRefBarrierStubC2(node, obj, addr, tmp1, tmp2, tmp3, narrow);
638 stub->register_stub();
639 return stub;
640 }
641
642 ShenandoahSATBBarrierStubC2* ShenandoahSATBBarrierStubC2::create(const MachNode* node, Register addr, Register preval, Register tmp) {
643 auto* stub = new (Compile::current()->comp_arena()) ShenandoahSATBBarrierStubC2(node, addr, preval, tmp);
644 stub->register_stub();
645 return stub;
646 }
647
648 ShenandoahCASBarrierSlowStubC2* ShenandoahCASBarrierSlowStubC2::create(const MachNode* node, Register addr, Register expected, Register new_val, Register result, Register tmp, bool cae, bool acquire, bool release, bool weak) {
649 auto* stub = new (Compile::current()->comp_arena()) ShenandoahCASBarrierSlowStubC2(node, addr, Address(), expected, new_val, result, tmp, noreg, cae, acquire, release, weak);
650 stub->register_stub();
651 return stub;
652 }
653
654 ShenandoahCASBarrierSlowStubC2* ShenandoahCASBarrierSlowStubC2::create(const MachNode* node, Address addr, Register expected, Register new_val, Register result, Register tmp1, Register tmp2, bool cae) {
655 auto* stub = new (Compile::current()->comp_arena()) ShenandoahCASBarrierSlowStubC2(node, noreg, addr, expected, new_val, result, tmp1, tmp2, cae, false, false, false);
656 stub->register_stub();
657 return stub;
658 }
659
660 ShenandoahCASBarrierMidStubC2* ShenandoahCASBarrierMidStubC2::create(const MachNode* node, ShenandoahCASBarrierSlowStubC2* slow_stub, Register expected, Register result, Register tmp, bool cae) {
661 auto* stub = new (Compile::current()->comp_arena()) ShenandoahCASBarrierMidStubC2(node, slow_stub, expected, result, tmp, cae);
662 stub->register_stub();
663 return stub;
664 }
665
666 bool ShenandoahBarrierSetC2State::needs_liveness_data(const MachNode* mach) const {
667 //assert(mach->barrier_data() != 0, "what else?");
668 // return mach->barrier_data() != 0;
669 //return (mach->barrier_data() & ShenandoahSATBBarrier) != 0;
670 return ShenandoahSATBBarrierStubC2::needs_barrier(mach) || ShenandoahLoadRefBarrierStubC2::needs_barrier(mach);
671 }
672
673 bool ShenandoahBarrierSetC2State::needs_livein_data() const {
674 return true;
675 }