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 #include "precompiled.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
29 #include "gc/shenandoah/shenandoahForwarding.hpp"
30 #include "gc/shenandoah/shenandoahHeap.hpp"
31 #include "gc/shenandoah/shenandoahRuntime.hpp"
32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
33 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
34 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
35 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
36 #include "opto/arraycopynode.hpp"
37 #include "opto/escape.hpp"
38 #include "opto/graphKit.hpp"
39 #include "opto/idealKit.hpp"
40 #include "opto/macro.hpp"
41 #include "opto/movenode.hpp"
42 #include "opto/narrowptrnode.hpp"
43 #include "opto/rootnode.hpp"
44 #include "opto/runtime.hpp"
45
46 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {
47 return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());
48 }
49
50 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena)
51 : _iu_barriers(new (comp_arena) GrowableArray<ShenandoahIUBarrierNode*>(comp_arena, 8, 0, nullptr)),
52 _load_reference_barriers(new (comp_arena) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena, 8, 0, nullptr)) {
53 }
54
55 int ShenandoahBarrierSetC2State::iu_barriers_count() const {
56 return _iu_barriers->length();
57 }
58
59 ShenandoahIUBarrierNode* ShenandoahBarrierSetC2State::iu_barrier(int idx) const {
60 return _iu_barriers->at(idx);
61 }
62
63 void ShenandoahBarrierSetC2State::add_iu_barrier(ShenandoahIUBarrierNode* n) {
64 assert(!_iu_barriers->contains(n), "duplicate entry in barrier list");
65 _iu_barriers->append(n);
66 }
67
68 void ShenandoahBarrierSetC2State::remove_iu_barrier(ShenandoahIUBarrierNode* n) {
69 _iu_barriers->remove_if_existing(n);
70 }
71
72 int ShenandoahBarrierSetC2State::load_reference_barriers_count() const {
73 return _load_reference_barriers->length();
74 }
75
76 ShenandoahLoadReferenceBarrierNode* ShenandoahBarrierSetC2State::load_reference_barrier(int idx) const {
77 return _load_reference_barriers->at(idx);
78 }
79
80 void ShenandoahBarrierSetC2State::add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
81 assert(!_load_reference_barriers->contains(n), "duplicate entry in barrier list");
82 _load_reference_barriers->append(n);
83 }
84
85 void ShenandoahBarrierSetC2State::remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
86 if (_load_reference_barriers->contains(n)) {
87 _load_reference_barriers->remove(n);
88 }
89 }
90
91 Node* ShenandoahBarrierSetC2::shenandoah_iu_barrier(GraphKit* kit, Node* obj) const {
92 if (ShenandoahIUBarrier) {
93 return kit->gvn().transform(new ShenandoahIUBarrierNode(obj));
94 }
95 return obj;
96 }
97
98 #define __ kit->
99
100 bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr,
101 BasicType bt, uint adr_idx) const {
102 intptr_t offset = 0;
103 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
104 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
105
106 if (offset == Type::OffsetBot) {
107 return false; // cannot unalias unless there are precise offsets
108 }
109
110 if (alloc == nullptr) {
111 return false; // No allocation found
112 }
113
114 intptr_t size_in_bytes = type2aelembytes(bt);
115
116 Node* mem = __ memory(adr_idx); // start searching here...
117
279 } __ end_if(); // (!index)
280 } __ end_if(); // (pre_val != nullptr)
281 } __ end_if(); // (!marking)
282
283 // Final sync IdealKit and GraphKit.
284 kit->final_sync(ideal);
285
286 if (ShenandoahSATBBarrier && adr != nullptr) {
287 Node* c = kit->control();
288 Node* call = c->in(1)->in(1)->in(1)->in(0);
289 assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected");
290 call->add_req(adr);
291 }
292 }
293
294 bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) {
295 return call->is_CallLeaf() &&
296 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry);
297 }
298
299 bool ShenandoahBarrierSetC2::is_shenandoah_lrb_call(Node* call) {
300 if (!call->is_CallLeaf()) {
301 return false;
302 }
303
304 address entry_point = call->as_CallLeaf()->entry_point();
305 return (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong)) ||
306 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow)) ||
307 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak)) ||
308 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow)) ||
309 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom));
310 }
311
312 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseValues* phase, Node* n) {
313 if (n->Opcode() != Op_If) {
314 return false;
315 }
316
317 Node* bol = n->in(1);
318 assert(bol->is_Bool(), "");
319 Node* cmpx = bol->in(1);
320 if (bol->as_Bool()->_test._test == BoolTest::ne &&
321 cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) &&
322 is_shenandoah_state_load(cmpx->in(1)->in(1)) &&
323 cmpx->in(1)->in(2)->is_Con() &&
324 cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) {
325 return true;
326 }
327
328 return false;
329 }
433 // Use the pre-barrier to record the value in the referent field
434 satb_write_barrier_pre(kit, false /* do_load */,
435 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */,
436 pre_val /* pre_val */,
437 T_OBJECT);
438 if (need_mem_bar) {
439 // Add memory barrier to prevent commoning reads from this field
440 // across safepoint since GC can change its value.
441 kit->insert_mem_bar(Op_MemBarCPUOrder);
442 }
443 // Update IdealKit from graphKit.
444 __ sync_kit(kit);
445
446 } __ end_if(); // _ref_type != ref_none
447 } __ end_if(); // offset == referent_offset
448
449 // Final sync IdealKit and GraphKit.
450 kit->final_sync(ideal);
451 }
452
453 #undef __
454
455 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() {
456 const Type **fields = TypeTuple::fields(2);
457 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value
458 fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread
459 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
460
461 // create result type (range)
462 fields = TypeTuple::fields(0);
463 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
464
465 return TypeFunc::make(domain, range);
466 }
467
468 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() {
469 const Type **fields = TypeTuple::fields(1);
470 fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop
471 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
472
491
492 return TypeFunc::make(domain, range);
493 }
494
495 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
496 DecoratorSet decorators = access.decorators();
497
498 const TypePtr* adr_type = access.addr().type();
499 Node* adr = access.addr().node();
500
501 if (!access.is_oop()) {
502 return BarrierSetC2::store_at_resolved(access, val);
503 }
504
505 if (access.is_parse_access()) {
506 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
507 GraphKit* kit = parse_access.kit();
508
509 uint adr_idx = kit->C->get_alias_index(adr_type);
510 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
511 Node* value = val.node();
512 value = shenandoah_iu_barrier(kit, value);
513 val.set_node(value);
514 shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(),
515 static_cast<const TypeOopPtr*>(val.type()), nullptr /* pre_val */, access.type());
516 } else {
517 assert(access.is_opt_access(), "only for optimization passes");
518 assert(((decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0 || !ShenandoahSATBBarrier) && (decorators & C2_ARRAY_COPY) != 0, "unexpected caller of this code");
519 C2OptAccess& opt_access = static_cast<C2OptAccess&>(access);
520 PhaseGVN& gvn = opt_access.gvn();
521
522 if (ShenandoahIUBarrier) {
523 Node* enqueue = gvn.transform(new ShenandoahIUBarrierNode(val.node()));
524 val.set_node(enqueue);
525 }
526 }
527 return BarrierSetC2::store_at_resolved(access, val);
528 }
529
530 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
531 // 1: non-reference load, no additional barrier is needed
532 if (!access.is_oop()) {
533 return BarrierSetC2::load_at_resolved(access, val_type);
534 }
535
536 Node* load = BarrierSetC2::load_at_resolved(access, val_type);
537 DecoratorSet decorators = access.decorators();
538 BasicType type = access.type();
539
540 // 2: apply LRB if needed
541 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
542 load = new ShenandoahLoadReferenceBarrierNode(nullptr, load, decorators);
543 if (access.is_parse_access()) {
544 load = static_cast<C2ParseAccess &>(access).kit()->gvn().transform(load);
545 } else {
546 load = static_cast<C2OptAccess &>(access).gvn().transform(load);
547 }
578
579 if (on_weak_ref) {
580 // Use the pre-barrier to record the value in the referent field
581 satb_write_barrier_pre(kit, false /* do_load */,
582 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */,
583 load /* pre_val */, T_OBJECT);
584 // Add memory barrier to prevent commoning reads from this field
585 // across safepoint since GC can change its value.
586 kit->insert_mem_bar(Op_MemBarCPUOrder);
587 } else if (unknown) {
588 // We do not require a mem bar inside pre_barrier if need_mem_bar
589 // is set: the barriers would be emitted by us.
590 insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar);
591 }
592 }
593
594 return load;
595 }
596
597 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
598 Node* new_val, const Type* value_type) const {
599 GraphKit* kit = access.kit();
600 if (access.is_oop()) {
601 new_val = shenandoah_iu_barrier(kit, new_val);
602 shenandoah_write_barrier_pre(kit, false /* do_load */,
603 nullptr, nullptr, max_juint, nullptr, nullptr,
604 expected_val /* pre_val */, T_OBJECT);
605
606 MemNode::MemOrd mo = access.mem_node_mo();
607 Node* mem = access.memory();
608 Node* adr = access.addr().node();
609 const TypePtr* adr_type = access.addr().type();
610 Node* load_store = nullptr;
611
612 #ifdef _LP64
613 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
614 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
615 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
616 if (ShenandoahCASBarrier) {
617 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
618 } else {
619 load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
620 }
621 } else
622 #endif
623 {
624 if (ShenandoahCASBarrier) {
625 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
626 } else {
627 load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
628 }
629 }
630
631 access.set_raw_access(load_store);
632 pin_atomic_op(access);
633
634 #ifdef _LP64
635 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
636 load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
637 }
638 #endif
639 load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, load_store, access.decorators()));
640 return load_store;
641 }
642 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
643 }
644
645 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
646 Node* new_val, const Type* value_type) const {
647 GraphKit* kit = access.kit();
648 if (access.is_oop()) {
649 new_val = shenandoah_iu_barrier(kit, new_val);
650 shenandoah_write_barrier_pre(kit, false /* do_load */,
651 nullptr, nullptr, max_juint, nullptr, nullptr,
652 expected_val /* pre_val */, T_OBJECT);
653 DecoratorSet decorators = access.decorators();
654 MemNode::MemOrd mo = access.mem_node_mo();
655 Node* mem = access.memory();
656 bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0;
657 Node* load_store = nullptr;
658 Node* adr = access.addr().node();
659 #ifdef _LP64
660 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
661 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
662 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
663 if (ShenandoahCASBarrier) {
664 if (is_weak_cas) {
665 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
666 } else {
667 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
668 }
669 } else {
675 }
676 } else
677 #endif
678 {
679 if (ShenandoahCASBarrier) {
680 if (is_weak_cas) {
681 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
682 } else {
683 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
684 }
685 } else {
686 if (is_weak_cas) {
687 load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
688 } else {
689 load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
690 }
691 }
692 }
693 access.set_raw_access(load_store);
694 pin_atomic_op(access);
695 return load_store;
696 }
697 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
698 }
699
700 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const {
701 GraphKit* kit = access.kit();
702 if (access.is_oop()) {
703 val = shenandoah_iu_barrier(kit, val);
704 }
705 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);
706 if (access.is_oop()) {
707 result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, result, access.decorators()));
708 shenandoah_write_barrier_pre(kit, false /* do_load */,
709 nullptr, nullptr, max_juint, nullptr, nullptr,
710 result /* pre_val */, T_OBJECT);
711 }
712 return result;
713 }
714
715
716 bool ShenandoahBarrierSetC2::is_gc_pre_barrier_node(Node* node) const {
717 return is_shenandoah_wb_pre_call(node);
718 }
719
720 // Support for GC barriers emitted during parsing
721 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const {
722 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier || node->Opcode() == Op_ShenandoahIUBarrier) return true;
723 if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) {
724 return false;
725 }
726 CallLeafNode *call = node->as_CallLeaf();
727 if (call->_name == nullptr) {
728 return false;
729 }
730
731 return strcmp(call->_name, "shenandoah_clone_barrier") == 0 ||
732 strcmp(call->_name, "shenandoah_cas_obj") == 0 ||
733 strcmp(call->_name, "shenandoah_wb_pre") == 0;
734 }
735
736 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const {
737 if (c == nullptr) {
738 return c;
739 }
740 if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
741 return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn);
742 }
743 if (c->Opcode() == Op_ShenandoahIUBarrier) {
744 c = c->in(1);
745 }
746 return c;
747 }
748
749 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
750 return !ShenandoahBarrierC2Support::expand(C, igvn);
751 }
752
753 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const {
754 if (mode == LoopOptsShenandoahExpand) {
755 assert(UseShenandoahGC, "only for shenandoah");
756 ShenandoahBarrierC2Support::pin_and_expand(phase);
757 return true;
758 } else if (mode == LoopOptsShenandoahPostExpand) {
759 assert(UseShenandoahGC, "only for shenandoah");
760 visited.clear();
761 ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase);
762 return true;
763 }
764 return false;
765 }
766
767 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
768 bool is_oop = is_reference_type(type);
769 if (!is_oop) {
770 return false;
771 }
772 if (ShenandoahSATBBarrier && tightly_coupled_alloc) {
773 if (phase == Optimization) {
774 return false;
775 }
776 return !is_clone;
777 }
778 if (phase == Optimization) {
779 return !ShenandoahIUBarrier;
780 }
781 return true;
782 }
783
784 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) {
785 const TypeOopPtr* src_type = gvn.type(src)->is_oopptr();
786 if (src_type->isa_instptr() != nullptr) {
787 ciInstanceKlass* ik = src_type->is_instptr()->instance_klass();
788 if ((src_type->klass_is_exact() || !ik->has_subklass()) && !ik->has_injected_fields()) {
789 if (ik->has_object_fields()) {
790 return true;
791 } else {
792 if (!src_type->klass_is_exact()) {
793 Compile::current()->dependencies()->assert_leaf_type(ik);
794 }
795 }
796 } else {
797 return true;
798 }
799 } else if (src_type->isa_aryptr()) {
800 BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type();
819 Node* src = phase->basic_plus_adr(src_base, src_offset);
820 Node* dest = phase->basic_plus_adr(dest_base, dest_offset);
821
822 if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) {
823 // Check if heap is has forwarded objects. If it does, we need to call into the special
824 // routine that would fix up source references before we can continue.
825
826 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
827 Node* region = new RegionNode(PATH_LIMIT);
828 Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM);
829
830 Node* thread = phase->transform_later(new ThreadLocalNode());
831 Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
832 Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset));
833
834 uint gc_state_idx = Compile::AliasIdxRaw;
835 const TypePtr* gc_state_adr_type = nullptr; // debug-mode-only argument
836 debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));
837
838 Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered));
839 int flags = ShenandoahHeap::HAS_FORWARDED;
840 if (ShenandoahIUBarrier) {
841 flags |= ShenandoahHeap::MARKING;
842 }
843 Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(flags)));
844 Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT)));
845 Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne));
846
847 IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If();
848 Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff));
849 Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff));
850
851 // Heap is stable, no need to do anything additional
852 region->init_req(_heap_stable, stable_ctrl);
853 mem_phi->init_req(_heap_stable, mem);
854
855 // Heap is unstable, call into clone barrier stub
856 Node* call = phase->make_leaf_call(unstable_ctrl, mem,
857 ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(),
858 CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier),
859 "shenandoah_clone",
860 TypeRawPtr::BOTTOM,
861 src_base);
862 call = phase->transform_later(call);
863
872
873 const char* name = "arraycopy";
874 call = phase->make_leaf_call(ctrl, mem,
875 OptoRuntime::fast_arraycopy_Type(),
876 phase->basictype2arraycopy(T_LONG, nullptr, nullptr, true, name, true),
877 name, TypeRawPtr::BOTTOM,
878 src, dest, length
879 LP64_ONLY(COMMA phase->top()));
880 call = phase->transform_later(call);
881
882 // Hook up the whole thing into the graph
883 phase->igvn().replace_node(ac, call);
884 } else {
885 BarrierSetC2::clone_at_expansion(phase, ac);
886 }
887 }
888
889
890 // Support for macro expanded GC barriers
891 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const {
892 if (node->Opcode() == Op_ShenandoahIUBarrier) {
893 state()->add_iu_barrier((ShenandoahIUBarrierNode*) node);
894 }
895 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
896 state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
897 }
898 }
899
900 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const {
901 if (node->Opcode() == Op_ShenandoahIUBarrier) {
902 state()->remove_iu_barrier((ShenandoahIUBarrierNode*) node);
903 }
904 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
905 state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
906 }
907 }
908
909 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* n) const {
910 if (is_shenandoah_wb_pre_call(n)) {
911 shenandoah_eliminate_wb_pre(n, ¯o->igvn());
912 }
913 }
914
915 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const {
916 assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), "");
917 Node* c = call->as_Call()->proj_out(TypeFunc::Control);
918 c = c->unique_ctrl_out();
919 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
920 c = c->unique_ctrl_out();
921 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
922 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
923 assert(iff->is_If(), "expect test");
924 if (!is_shenandoah_marking_if(igvn, iff)) {
925 c = c->unique_ctrl_out();
926 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
927 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
928 assert(is_shenandoah_marking_if(igvn, iff), "expect marking test");
929 }
930 Node* cmpx = iff->in(1)->in(1);
931 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ));
932 igvn->rehash_node_delayed(call);
933 call->del_req(call->req()-1);
934 }
935
936 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const {
937 if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) {
938 igvn->add_users_to_worklist(node);
939 }
940 }
941
942 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const {
943 for (uint i = 0; i < useful.size(); i++) {
944 Node* n = useful.at(i);
945 if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) {
946 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
947 C->record_for_igvn(n->fast_out(i));
948 }
949 }
950 }
951 for (int i = state()->iu_barriers_count() - 1; i >= 0; i--) {
952 ShenandoahIUBarrierNode* n = state()->iu_barrier(i);
953 if (!useful.member(n)) {
954 state()->remove_iu_barrier(n);
955 }
956 }
957 for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) {
958 ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i);
959 if (!useful.member(n)) {
960 state()->remove_load_reference_barrier(n);
961 }
962 }
963 }
964
965 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {
966 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);
967 }
968
969 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {
970 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
971 }
972
973 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
974 // expanded later, then now is the time to do so.
975 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }
976
1170 // Pointer stores in Shenandoah barriers looks like unsafe access.
1171 // Ignore such stores to be able scalar replace non-escaping
1172 // allocations.
1173 if (adr_type->isa_rawptr() && adr->is_AddP()) {
1174 Node* base = conn_graph->get_addp_base(adr);
1175 if (base->Opcode() == Op_LoadP &&
1176 base->in(MemNode::Address)->is_AddP()) {
1177 adr = base->in(MemNode::Address);
1178 Node* tls = conn_graph->get_addp_base(adr);
1179 if (tls->Opcode() == Op_ThreadLocal) {
1180 int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
1181 const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());
1182 if (offs == buf_offset) {
1183 return true; // Pre barrier previous oop value store.
1184 }
1185 }
1186 }
1187 }
1188 return false;
1189 }
1190 case Op_ShenandoahIUBarrier:
1191 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);
1192 break;
1193 case Op_ShenandoahLoadReferenceBarrier:
1194 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), delayed_worklist);
1195 return true;
1196 default:
1197 // Nothing
1198 break;
1199 }
1200 return false;
1201 }
1202
1203 bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const {
1204 switch (opcode) {
1205 case Op_ShenandoahCompareAndExchangeP:
1206 case Op_ShenandoahCompareAndExchangeN: {
1207 Node *adr = n->in(MemNode::Address);
1208 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, nullptr);
1209 // fallthrough
1210 }
1211 case Op_ShenandoahCompareAndSwapP:
1212 case Op_ShenandoahCompareAndSwapN:
1213 case Op_ShenandoahWeakCompareAndSwapP:
1214 case Op_ShenandoahWeakCompareAndSwapN:
1215 return conn_graph->add_final_edges_unsafe_access(n, opcode);
1216 case Op_ShenandoahIUBarrier:
1217 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), nullptr);
1218 return true;
1219 case Op_ShenandoahLoadReferenceBarrier:
1220 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), nullptr);
1221 return true;
1222 default:
1223 // Nothing
1224 break;
1225 }
1226 return false;
1227 }
1228
1229 bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const {
1230 return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) ||
1231 n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN);
1232
1233 }
1234
1235 bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const {
1236 switch (opcode) {
1237 case Op_ShenandoahCompareAndExchangeP:
1238 case Op_ShenandoahCompareAndExchangeN:
|
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 "precompiled.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "gc/shared/barrierSet.hpp"
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30 #include "gc/shenandoah/shenandoahCardTable.hpp"
31 #include "gc/shenandoah/shenandoahForwarding.hpp"
32 #include "gc/shenandoah/shenandoahHeap.hpp"
33 #include "gc/shenandoah/shenandoahRuntime.hpp"
34 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
35 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
36 #include "gc/shenandoah/c2/shenandoahSupport.hpp"
37 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
38 #include "opto/arraycopynode.hpp"
39 #include "opto/escape.hpp"
40 #include "opto/graphKit.hpp"
41 #include "opto/idealKit.hpp"
42 #include "opto/macro.hpp"
43 #include "opto/movenode.hpp"
44 #include "opto/narrowptrnode.hpp"
45 #include "opto/rootnode.hpp"
46 #include "opto/runtime.hpp"
47
48 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {
49 return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());
50 }
51
52 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena)
53 : _load_reference_barriers(new (comp_arena) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena, 8, 0, nullptr)) {
54 }
55
56 int ShenandoahBarrierSetC2State::load_reference_barriers_count() const {
57 return _load_reference_barriers->length();
58 }
59
60 ShenandoahLoadReferenceBarrierNode* ShenandoahBarrierSetC2State::load_reference_barrier(int idx) const {
61 return _load_reference_barriers->at(idx);
62 }
63
64 void ShenandoahBarrierSetC2State::add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
65 assert(!_load_reference_barriers->contains(n), "duplicate entry in barrier list");
66 _load_reference_barriers->append(n);
67 }
68
69 void ShenandoahBarrierSetC2State::remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {
70 if (_load_reference_barriers->contains(n)) {
71 _load_reference_barriers->remove(n);
72 }
73 }
74
75 #define __ kit->
76
77 bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr,
78 BasicType bt, uint adr_idx) const {
79 intptr_t offset = 0;
80 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);
81 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);
82
83 if (offset == Type::OffsetBot) {
84 return false; // cannot unalias unless there are precise offsets
85 }
86
87 if (alloc == nullptr) {
88 return false; // No allocation found
89 }
90
91 intptr_t size_in_bytes = type2aelembytes(bt);
92
93 Node* mem = __ memory(adr_idx); // start searching here...
94
256 } __ end_if(); // (!index)
257 } __ end_if(); // (pre_val != nullptr)
258 } __ end_if(); // (!marking)
259
260 // Final sync IdealKit and GraphKit.
261 kit->final_sync(ideal);
262
263 if (ShenandoahSATBBarrier && adr != nullptr) {
264 Node* c = kit->control();
265 Node* call = c->in(1)->in(1)->in(1)->in(0);
266 assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected");
267 call->add_req(adr);
268 }
269 }
270
271 bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) {
272 return call->is_CallLeaf() &&
273 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry);
274 }
275
276 bool ShenandoahBarrierSetC2::is_shenandoah_clone_call(Node* call) {
277 return call->is_CallLeaf() &&
278 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier);
279 }
280
281 bool ShenandoahBarrierSetC2::is_shenandoah_lrb_call(Node* call) {
282 if (!call->is_CallLeaf()) {
283 return false;
284 }
285
286 address entry_point = call->as_CallLeaf()->entry_point();
287 return (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong)) ||
288 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow)) ||
289 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak)) ||
290 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow)) ||
291 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom)) ||
292 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom_narrow));
293 }
294
295 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseValues* phase, Node* n) {
296 if (n->Opcode() != Op_If) {
297 return false;
298 }
299
300 Node* bol = n->in(1);
301 assert(bol->is_Bool(), "");
302 Node* cmpx = bol->in(1);
303 if (bol->as_Bool()->_test._test == BoolTest::ne &&
304 cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) &&
305 is_shenandoah_state_load(cmpx->in(1)->in(1)) &&
306 cmpx->in(1)->in(2)->is_Con() &&
307 cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) {
308 return true;
309 }
310
311 return false;
312 }
416 // Use the pre-barrier to record the value in the referent field
417 satb_write_barrier_pre(kit, false /* do_load */,
418 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */,
419 pre_val /* pre_val */,
420 T_OBJECT);
421 if (need_mem_bar) {
422 // Add memory barrier to prevent commoning reads from this field
423 // across safepoint since GC can change its value.
424 kit->insert_mem_bar(Op_MemBarCPUOrder);
425 }
426 // Update IdealKit from graphKit.
427 __ sync_kit(kit);
428
429 } __ end_if(); // _ref_type != ref_none
430 } __ end_if(); // offset == referent_offset
431
432 // Final sync IdealKit and GraphKit.
433 kit->final_sync(ideal);
434 }
435
436 Node* ShenandoahBarrierSetC2::byte_map_base_node(GraphKit* kit) const {
437 BarrierSet* bs = BarrierSet::barrier_set();
438 ShenandoahBarrierSet* ctbs = barrier_set_cast<ShenandoahBarrierSet>(bs);
439 CardTable::CardValue* card_table_base = ctbs->card_table()->byte_map_base();
440 if (card_table_base != nullptr) {
441 return kit->makecon(TypeRawPtr::make((address)card_table_base));
442 } else {
443 return kit->null();
444 }
445 }
446
447 void ShenandoahBarrierSetC2::post_barrier(GraphKit* kit,
448 Node* ctl,
449 Node* oop_store,
450 Node* obj,
451 Node* adr,
452 uint adr_idx,
453 Node* val,
454 BasicType bt,
455 bool use_precise) const {
456 assert(ShenandoahCardBarrier, "Should have been checked by caller");
457
458 // No store check needed if we're storing a null.
459 if (val != nullptr && val->is_Con()) {
460 // must be either an oop or NULL
461 const Type* t = val->bottom_type();
462 if (t == TypePtr::NULL_PTR || t == Type::TOP)
463 return;
464 }
465
466 if (ReduceInitialCardMarks && obj == kit->just_allocated_object(kit->control())) {
467 // We can skip marks on a freshly-allocated object in Eden.
468 // Keep this code in sync with new_deferred_store_barrier() in runtime.cpp.
469 // That routine informs GC to take appropriate compensating steps,
470 // upon a slow-path allocation, so as to make this card-mark
471 // elision safe.
472 return;
473 }
474
475 if (!use_precise) {
476 // All card marks for a (non-array) instance are in one place:
477 adr = obj;
478 }
479 // (Else it's an array (or unknown), and we want more precise card marks.)
480 assert(adr != nullptr, "");
481
482 IdealKit ideal(kit, true);
483
484 // Convert the pointer to an int prior to doing math on it
485 Node* cast = __ CastPX(__ ctrl(), adr);
486
487 // Divide by card size
488 Node* card_offset = __ URShiftX( cast, __ ConI(CardTable::card_shift()) );
489
490 // Combine card table base and card offset
491 Node* card_adr = __ AddP(__ top(), byte_map_base_node(kit), card_offset );
492
493 // Get the alias_index for raw card-mark memory
494 int adr_type = Compile::AliasIdxRaw;
495 Node* zero = __ ConI(0); // Dirty card value
496
497 if (UseCondCardMark) {
498 // The classic GC reference write barrier is typically implemented
499 // as a store into the global card mark table. Unfortunately
500 // unconditional stores can result in false sharing and excessive
501 // coherence traffic as well as false transactional aborts.
502 // UseCondCardMark enables MP "polite" conditional card mark
503 // stores. In theory we could relax the load from ctrl() to
504 // no_ctrl, but that doesn't buy much latitude.
505 Node* card_val = __ load( __ ctrl(), card_adr, TypeInt::BYTE, T_BYTE, adr_type);
506 __ if_then(card_val, BoolTest::ne, zero);
507 }
508
509 // Smash zero into card
510 __ store(__ ctrl(), card_adr, zero, T_BYTE, adr_type, MemNode::unordered);
511
512 if (UseCondCardMark) {
513 __ end_if();
514 }
515
516 // Final sync IdealKit and GraphKit.
517 kit->final_sync(ideal);
518 }
519
520 #undef __
521
522 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() {
523 const Type **fields = TypeTuple::fields(2);
524 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value
525 fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread
526 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
527
528 // create result type (range)
529 fields = TypeTuple::fields(0);
530 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
531
532 return TypeFunc::make(domain, range);
533 }
534
535 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() {
536 const Type **fields = TypeTuple::fields(1);
537 fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop
538 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
539
558
559 return TypeFunc::make(domain, range);
560 }
561
562 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
563 DecoratorSet decorators = access.decorators();
564
565 const TypePtr* adr_type = access.addr().type();
566 Node* adr = access.addr().node();
567
568 if (!access.is_oop()) {
569 return BarrierSetC2::store_at_resolved(access, val);
570 }
571
572 if (access.is_parse_access()) {
573 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
574 GraphKit* kit = parse_access.kit();
575
576 uint adr_idx = kit->C->get_alias_index(adr_type);
577 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );
578 shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(),
579 static_cast<const TypeOopPtr*>(val.type()), nullptr /* pre_val */, access.type());
580
581 Node* result = BarrierSetC2::store_at_resolved(access, val);
582
583 if (ShenandoahCardBarrier) {
584 const bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
585 const bool is_array = (decorators & IS_ARRAY) != 0;
586 const bool use_precise = is_array || anonymous;
587 post_barrier(kit, kit->control(), access.raw_access(), access.base(),
588 adr, adr_idx, val.node(), access.type(), use_precise);
589 }
590 return result;
591 } else {
592 assert(access.is_opt_access(), "only for optimization passes");
593 assert(((decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0 || !ShenandoahSATBBarrier) && (decorators & C2_ARRAY_COPY) != 0, "unexpected caller of this code");
594 return BarrierSetC2::store_at_resolved(access, val);
595 }
596 }
597
598 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {
599 // 1: non-reference load, no additional barrier is needed
600 if (!access.is_oop()) {
601 return BarrierSetC2::load_at_resolved(access, val_type);
602 }
603
604 Node* load = BarrierSetC2::load_at_resolved(access, val_type);
605 DecoratorSet decorators = access.decorators();
606 BasicType type = access.type();
607
608 // 2: apply LRB if needed
609 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {
610 load = new ShenandoahLoadReferenceBarrierNode(nullptr, load, decorators);
611 if (access.is_parse_access()) {
612 load = static_cast<C2ParseAccess &>(access).kit()->gvn().transform(load);
613 } else {
614 load = static_cast<C2OptAccess &>(access).gvn().transform(load);
615 }
646
647 if (on_weak_ref) {
648 // Use the pre-barrier to record the value in the referent field
649 satb_write_barrier_pre(kit, false /* do_load */,
650 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */,
651 load /* pre_val */, T_OBJECT);
652 // Add memory barrier to prevent commoning reads from this field
653 // across safepoint since GC can change its value.
654 kit->insert_mem_bar(Op_MemBarCPUOrder);
655 } else if (unknown) {
656 // We do not require a mem bar inside pre_barrier if need_mem_bar
657 // is set: the barriers would be emitted by us.
658 insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar);
659 }
660 }
661
662 return load;
663 }
664
665 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
666 Node* new_val, const Type* value_type) const {
667 GraphKit* kit = access.kit();
668 if (access.is_oop()) {
669 shenandoah_write_barrier_pre(kit, false /* do_load */,
670 nullptr, nullptr, max_juint, nullptr, nullptr,
671 expected_val /* pre_val */, T_OBJECT);
672
673 MemNode::MemOrd mo = access.mem_node_mo();
674 Node* mem = access.memory();
675 Node* adr = access.addr().node();
676 const TypePtr* adr_type = access.addr().type();
677 Node* load_store = nullptr;
678
679 #ifdef _LP64
680 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
681 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
682 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
683 if (ShenandoahCASBarrier) {
684 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
685 } else {
686 load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));
687 }
688 } else
689 #endif
690 {
691 if (ShenandoahCASBarrier) {
692 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
693 } else {
694 load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));
695 }
696 }
697
698 access.set_raw_access(load_store);
699 pin_atomic_op(access);
700
701 #ifdef _LP64
702 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
703 load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
704 }
705 #endif
706 load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, load_store, access.decorators()));
707 if (ShenandoahCardBarrier) {
708 post_barrier(kit, kit->control(), access.raw_access(), access.base(),
709 access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true);
710 }
711 return load_store;
712 }
713 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
714 }
715
716 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
717 Node* new_val, const Type* value_type) const {
718 GraphKit* kit = access.kit();
719 if (access.is_oop()) {
720 shenandoah_write_barrier_pre(kit, false /* do_load */,
721 nullptr, nullptr, max_juint, nullptr, nullptr,
722 expected_val /* pre_val */, T_OBJECT);
723 DecoratorSet decorators = access.decorators();
724 MemNode::MemOrd mo = access.mem_node_mo();
725 Node* mem = access.memory();
726 bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0;
727 Node* load_store = nullptr;
728 Node* adr = access.addr().node();
729 #ifdef _LP64
730 if (adr->bottom_type()->is_ptr_to_narrowoop()) {
731 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));
732 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));
733 if (ShenandoahCASBarrier) {
734 if (is_weak_cas) {
735 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
736 } else {
737 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));
738 }
739 } else {
745 }
746 } else
747 #endif
748 {
749 if (ShenandoahCASBarrier) {
750 if (is_weak_cas) {
751 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
752 } else {
753 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
754 }
755 } else {
756 if (is_weak_cas) {
757 load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
758 } else {
759 load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));
760 }
761 }
762 }
763 access.set_raw_access(load_store);
764 pin_atomic_op(access);
765 if (ShenandoahCardBarrier) {
766 post_barrier(kit, kit->control(), access.raw_access(), access.base(),
767 access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true);
768 }
769 return load_store;
770 }
771 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
772 }
773
774 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const {
775 GraphKit* kit = access.kit();
776 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);
777 if (access.is_oop()) {
778 result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, result, access.decorators()));
779 shenandoah_write_barrier_pre(kit, false /* do_load */,
780 nullptr, nullptr, max_juint, nullptr, nullptr,
781 result /* pre_val */, T_OBJECT);
782 if (ShenandoahCardBarrier) {
783 post_barrier(kit, kit->control(), access.raw_access(), access.base(),
784 access.addr().node(), access.alias_idx(), val, T_OBJECT, true);
785 }
786 }
787 return result;
788 }
789
790
791 bool ShenandoahBarrierSetC2::is_gc_pre_barrier_node(Node* node) const {
792 return is_shenandoah_wb_pre_call(node);
793 }
794
795 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const {
796 return (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) ||
797 is_shenandoah_lrb_call(node) ||
798 is_shenandoah_wb_pre_call(node) ||
799 is_shenandoah_clone_call(node);
800 }
801
802 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const {
803 if (c == nullptr) {
804 return c;
805 }
806 if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
807 return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn);
808 }
809 return c;
810 }
811
812 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {
813 return !ShenandoahBarrierC2Support::expand(C, igvn);
814 }
815
816 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const {
817 if (mode == LoopOptsShenandoahExpand) {
818 assert(UseShenandoahGC, "only for shenandoah");
819 ShenandoahBarrierC2Support::pin_and_expand(phase);
820 return true;
821 } else if (mode == LoopOptsShenandoahPostExpand) {
822 assert(UseShenandoahGC, "only for shenandoah");
823 visited.clear();
824 ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase);
825 return true;
826 }
827 return false;
828 }
829
830 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {
831 bool is_oop = is_reference_type(type);
832 if (!is_oop) {
833 return false;
834 }
835 if (ShenandoahSATBBarrier && tightly_coupled_alloc) {
836 if (phase == Optimization) {
837 return false;
838 }
839 return !is_clone;
840 }
841 return true;
842 }
843
844 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) {
845 const TypeOopPtr* src_type = gvn.type(src)->is_oopptr();
846 if (src_type->isa_instptr() != nullptr) {
847 ciInstanceKlass* ik = src_type->is_instptr()->instance_klass();
848 if ((src_type->klass_is_exact() || !ik->has_subklass()) && !ik->has_injected_fields()) {
849 if (ik->has_object_fields()) {
850 return true;
851 } else {
852 if (!src_type->klass_is_exact()) {
853 Compile::current()->dependencies()->assert_leaf_type(ik);
854 }
855 }
856 } else {
857 return true;
858 }
859 } else if (src_type->isa_aryptr()) {
860 BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type();
879 Node* src = phase->basic_plus_adr(src_base, src_offset);
880 Node* dest = phase->basic_plus_adr(dest_base, dest_offset);
881
882 if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) {
883 // Check if heap is has forwarded objects. If it does, we need to call into the special
884 // routine that would fix up source references before we can continue.
885
886 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };
887 Node* region = new RegionNode(PATH_LIMIT);
888 Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM);
889
890 Node* thread = phase->transform_later(new ThreadLocalNode());
891 Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));
892 Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset));
893
894 uint gc_state_idx = Compile::AliasIdxRaw;
895 const TypePtr* gc_state_adr_type = nullptr; // debug-mode-only argument
896 debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));
897
898 Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered));
899 Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(ShenandoahHeap::HAS_FORWARDED)));
900 Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT)));
901 Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne));
902
903 IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If();
904 Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff));
905 Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff));
906
907 // Heap is stable, no need to do anything additional
908 region->init_req(_heap_stable, stable_ctrl);
909 mem_phi->init_req(_heap_stable, mem);
910
911 // Heap is unstable, call into clone barrier stub
912 Node* call = phase->make_leaf_call(unstable_ctrl, mem,
913 ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(),
914 CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier),
915 "shenandoah_clone",
916 TypeRawPtr::BOTTOM,
917 src_base);
918 call = phase->transform_later(call);
919
928
929 const char* name = "arraycopy";
930 call = phase->make_leaf_call(ctrl, mem,
931 OptoRuntime::fast_arraycopy_Type(),
932 phase->basictype2arraycopy(T_LONG, nullptr, nullptr, true, name, true),
933 name, TypeRawPtr::BOTTOM,
934 src, dest, length
935 LP64_ONLY(COMMA phase->top()));
936 call = phase->transform_later(call);
937
938 // Hook up the whole thing into the graph
939 phase->igvn().replace_node(ac, call);
940 } else {
941 BarrierSetC2::clone_at_expansion(phase, ac);
942 }
943 }
944
945
946 // Support for macro expanded GC barriers
947 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const {
948 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
949 state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
950 }
951 }
952
953 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const {
954 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {
955 state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);
956 }
957 }
958
959 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const {
960 if (is_shenandoah_wb_pre_call(node)) {
961 shenandoah_eliminate_wb_pre(node, ¯o->igvn());
962 }
963 if (ShenandoahCardBarrier && node->Opcode() == Op_CastP2X) {
964 Node* shift = node->unique_out();
965 Node* addp = shift->unique_out();
966 for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) {
967 Node* mem = addp->last_out(j);
968 if (UseCondCardMark && mem->is_Load()) {
969 assert(mem->Opcode() == Op_LoadB, "unexpected code shape");
970 // The load is checking if the card has been written so
971 // replace it with zero to fold the test.
972 macro->replace_node(mem, macro->intcon(0));
973 continue;
974 }
975 assert(mem->is_Store(), "store required");
976 macro->replace_node(mem, mem->in(MemNode::Memory));
977 }
978 }
979 }
980
981 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const {
982 assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), "");
983 Node* c = call->as_Call()->proj_out(TypeFunc::Control);
984 c = c->unique_ctrl_out();
985 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
986 c = c->unique_ctrl_out();
987 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
988 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
989 assert(iff->is_If(), "expect test");
990 if (!is_shenandoah_marking_if(igvn, iff)) {
991 c = c->unique_ctrl_out();
992 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");
993 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);
994 assert(is_shenandoah_marking_if(igvn, iff), "expect marking test");
995 }
996 Node* cmpx = iff->in(1)->in(1);
997 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ));
998 igvn->rehash_node_delayed(call);
999 call->del_req(call->req()-1);
1000 }
1001
1002 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const {
1003 if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) {
1004 igvn->add_users_to_worklist(node);
1005 }
1006 }
1007
1008 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const {
1009 for (uint i = 0; i < useful.size(); i++) {
1010 Node* n = useful.at(i);
1011 if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) {
1012 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1013 C->record_for_igvn(n->fast_out(i));
1014 }
1015 }
1016 }
1017
1018 for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) {
1019 ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i);
1020 if (!useful.member(n)) {
1021 state()->remove_load_reference_barrier(n);
1022 }
1023 }
1024 }
1025
1026 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {
1027 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);
1028 }
1029
1030 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {
1031 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());
1032 }
1033
1034 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be
1035 // expanded later, then now is the time to do so.
1036 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }
1037
1231 // Pointer stores in Shenandoah barriers looks like unsafe access.
1232 // Ignore such stores to be able scalar replace non-escaping
1233 // allocations.
1234 if (adr_type->isa_rawptr() && adr->is_AddP()) {
1235 Node* base = conn_graph->get_addp_base(adr);
1236 if (base->Opcode() == Op_LoadP &&
1237 base->in(MemNode::Address)->is_AddP()) {
1238 adr = base->in(MemNode::Address);
1239 Node* tls = conn_graph->get_addp_base(adr);
1240 if (tls->Opcode() == Op_ThreadLocal) {
1241 int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
1242 const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());
1243 if (offs == buf_offset) {
1244 return true; // Pre barrier previous oop value store.
1245 }
1246 }
1247 }
1248 }
1249 return false;
1250 }
1251 case Op_ShenandoahLoadReferenceBarrier:
1252 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), delayed_worklist);
1253 return true;
1254 default:
1255 // Nothing
1256 break;
1257 }
1258 return false;
1259 }
1260
1261 bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const {
1262 switch (opcode) {
1263 case Op_ShenandoahCompareAndExchangeP:
1264 case Op_ShenandoahCompareAndExchangeN: {
1265 Node *adr = n->in(MemNode::Address);
1266 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, nullptr);
1267 // fallthrough
1268 }
1269 case Op_ShenandoahCompareAndSwapP:
1270 case Op_ShenandoahCompareAndSwapN:
1271 case Op_ShenandoahWeakCompareAndSwapP:
1272 case Op_ShenandoahWeakCompareAndSwapN:
1273 return conn_graph->add_final_edges_unsafe_access(n, opcode);
1274 case Op_ShenandoahLoadReferenceBarrier:
1275 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), nullptr);
1276 return true;
1277 default:
1278 // Nothing
1279 break;
1280 }
1281 return false;
1282 }
1283
1284 bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const {
1285 return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) ||
1286 n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN);
1287
1288 }
1289
1290 bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const {
1291 switch (opcode) {
1292 case Op_ShenandoahCompareAndExchangeP:
1293 case Op_ShenandoahCompareAndExchangeN:
|