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 "ci/ciInlineKlass.hpp" 26 #include "gc/shared/c1/modRefBarrierSetC1.hpp" 27 #include "utilities/macros.hpp" 28 29 #ifdef ASSERT 30 #define __ gen->lir(__FILE__, __LINE__)-> 31 #else 32 #define __ gen->lir()-> 33 #endif 34 35 void ModRefBarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) { 36 DecoratorSet decorators = access.decorators(); 37 bool is_array = (decorators & IS_ARRAY) != 0; 38 bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0; 39 40 // Is this a flat, atomic access that might require gc barriers on oop fields? 41 ciInlineKlass* vk = access.vk(); 42 if (vk != nullptr && vk->has_object_fields()) { 43 // Add pre-barriers for oop fields 44 for (int i = 0; i < vk->nof_nonstatic_fields(); i++) { 45 ciField* field = vk->nonstatic_field_at(i); 46 if (!field->type()->is_primitive_type()) { 47 int off = access.offset().opr().as_jint() + field->offset_in_bytes() - vk->payload_offset(); 48 LIRAccess inner_access(access.gen(), decorators, access.base(), LIR_OprFact::intConst(off), field->type()->basic_type(), access.patch_emit_info(), access.access_emit_info()); 49 pre_barrier(inner_access, resolve_address(inner_access, false), 50 LIR_OprFact::illegalOpr /* pre_val */, inner_access.patch_emit_info()); 51 } 52 } 53 } 54 55 if (access.is_oop()) { 56 pre_barrier(access, access.resolved_addr(), 57 LIR_OprFact::illegalOpr /* pre_val */, access.patch_emit_info()); 58 } 59 60 BarrierSetC1::store_at_resolved(access, value); 61 62 if (access.is_oop()) { 63 bool precise = is_array || on_anonymous; 64 LIR_Opr post_addr = precise ? access.resolved_addr() : access.base().opr(); 65 post_barrier(access, post_addr, value); 66 } 67 68 if (vk != nullptr && vk->has_object_fields()) { 69 // Add post-barriers for oop fields 70 for (int i = 0; i < vk->nof_nonstatic_fields(); i++) { 71 ciField* field = vk->nonstatic_field_at(i); 72 if (!field->type()->is_primitive_type()) { 73 int inner_off = field->offset_in_bytes() - vk->payload_offset(); 74 int off = access.offset().opr().as_jint() + inner_off; 75 LIRAccess inner_access(access.gen(), decorators, access.base(), LIR_OprFact::intConst(off), field->type()->basic_type(), access.patch_emit_info(), access.access_emit_info()); 76 77 // Shift long value to extract the narrow oop field value and zero-extend 78 LIR_Opr field_val = access.gen()->new_register(T_LONG); 79 access.gen()->lir()->unsigned_shift_right(value, 80 LIR_OprFact::intConst(inner_off << LogBitsPerByte), 81 field_val, LIR_Opr::illegalOpr()); 82 LIR_Opr mask = access.gen()->load_immediate((julong) max_juint, T_LONG); 83 access.gen()->lir()->logical_and(field_val, mask, field_val); 84 LIR_Opr oop_val = access.gen()->new_register(T_OBJECT); 85 access.gen()->lir()->move(field_val, oop_val); 86 87 assert(!is_array && !on_anonymous, "not suppported"); 88 post_barrier(inner_access, access.base().opr(), oop_val); 89 } 90 } 91 } 92 } 93 94 LIR_Opr ModRefBarrierSetC1::atomic_cmpxchg_at_resolved(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value) { 95 if (access.is_oop()) { 96 pre_barrier(access, access.resolved_addr(), 97 LIR_OprFact::illegalOpr /* pre_val */, nullptr); 98 } 99 100 LIR_Opr result = BarrierSetC1::atomic_cmpxchg_at_resolved(access, cmp_value, new_value); 101 102 if (access.is_oop()) { 103 post_barrier(access, access.resolved_addr(), new_value.result()); 104 } 105 106 return result; 107 } 108 109 LIR_Opr ModRefBarrierSetC1::atomic_xchg_at_resolved(LIRAccess& access, LIRItem& value) { 110 if (access.is_oop()) { 111 pre_barrier(access, access.resolved_addr(), 112 LIR_OprFact::illegalOpr /* pre_val */, nullptr); 113 } 114 115 LIR_Opr result = BarrierSetC1::atomic_xchg_at_resolved(access, value); 116 117 if (access.is_oop()) { 118 post_barrier(access, access.resolved_addr(), value.result()); 119 } 120 121 return result; 122 } 123 124 // This overrides the default to resolve the address into a register, 125 // assuming it will be used by a write barrier anyway. 126 LIR_Opr ModRefBarrierSetC1::resolve_address(LIRAccess& access, bool resolve_in_register) { 127 DecoratorSet decorators = access.decorators(); 128 bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0; 129 bool is_write = (decorators & ACCESS_WRITE) != 0; 130 bool is_array = (decorators & IS_ARRAY) != 0; 131 bool on_anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0; 132 bool precise = is_array || on_anonymous; 133 resolve_in_register |= !needs_patching && is_write && access.is_oop() && precise; 134 return BarrierSetC1::resolve_address(access, resolve_in_register); 135 }