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 #include "gc/shared/c2/modRefBarrierSetC2.hpp"
26 #include "opto/arraycopynode.hpp"
27 #include "opto/graphKit.hpp"
28 #include "opto/idealKit.hpp"
29
30 Node* ModRefBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {
31 DecoratorSet decorators = access.decorators();
32
33 Node* adr = access.addr().node();
34
35 bool is_array = (decorators & IS_ARRAY) != 0;
36 bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;
37 bool in_heap = (decorators & IN_HEAP) != 0;
38 bool use_precise = is_array || anonymous;
39 bool tightly_coupled_alloc = (decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0;
40
41 if (!access.is_oop() || tightly_coupled_alloc || (!in_heap && !anonymous)) {
42 return BarrierSetC2::store_at_resolved(access, val);
43 }
44
45 assert(access.is_parse_access(), "entry not supported at optimization time");
46 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);
47
48 Node* store = BarrierSetC2::store_at_resolved(access, val);
49 post_barrier(parse_access.kit(), access.base(), adr, val.node(), use_precise);
50
51 return store;
52 }
53
54 Node* ModRefBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
55 Node* new_val, const Type* value_type) const {
56 if (!access.is_oop()) {
57 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
58 }
59
60 Node* result = BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);
61
62 post_barrier(access.kit(), access.base(), access.addr().node(), new_val, true);
63
64 return result;
65 }
66
67 Node* ModRefBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,
68 Node* new_val, const Type* value_type) const {
69 GraphKit* kit = access.kit();
70
71 if (!access.is_oop()) {
72 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
73 }
74
75 Node* load_store = BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);
76
77 // Emit the post barrier only when the actual store happened. This makes sense
78 // to check only for LS_cmp_* that can fail to set the value.
79 // LS_cmp_exchange does not produce any branches by default, so there is no
80 // boolean result to piggyback on. TODO: When we merge CompareAndSwap with
81 // CompareAndExchange and move branches here, it would make sense to conditionalize
82 // post_barriers for LS_cmp_exchange as well.
83 //
84 // CAS success path is marked more likely since we anticipate this is a performance
85 // critical path, while CAS failure path can use the penalty for going through unlikely
86 // path as backoff. Which is still better than doing a store barrier there.
87 IdealKit ideal(kit);
88 ideal.if_then(load_store, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
89 kit->sync_kit(ideal);
90 post_barrier(kit, access.base(), access.addr().node(), new_val, true);
91 ideal.sync_kit(kit);
92 } ideal.end_if();
93 kit->final_sync(ideal);
94
95 return load_store;
96 }
97
98 Node* ModRefBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* value_type) const {
99 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, new_val, value_type);
100 if (!access.is_oop()) {
101 return result;
102 }
103
104 post_barrier(access.kit(), access.base(), access.addr().node(), new_val, true);
105
106 return result;
107 }