Path: blob/master/src/hotspot/share/gc/shared/c1/barrierSetC1.cpp
40974 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_Defs.hpp"26#include "c1/c1_LIRGenerator.hpp"27#include "classfile/javaClasses.hpp"28#include "gc/shared/c1/barrierSetC1.hpp"29#include "utilities/macros.hpp"3031#ifndef PATCHED_ADDR32#define PATCHED_ADDR (max_jint)33#endif3435#ifdef ASSERT36#define __ gen->lir(__FILE__, __LINE__)->37#else38#define __ gen->lir()->39#endif4041LIR_Opr BarrierSetC1::resolve_address(LIRAccess& access, bool resolve_in_register) {42DecoratorSet decorators = access.decorators();43bool is_array = (decorators & IS_ARRAY) != 0;44bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0;4546LIRItem& base = access.base().item();47LIR_Opr offset = access.offset().opr();48LIRGenerator *gen = access.gen();4950LIR_Opr addr_opr;51if (is_array) {52addr_opr = LIR_OprFact::address(gen->emit_array_address(base.result(), offset, access.type()));53} else if (needs_patching) {54// we need to patch the offset in the instruction so don't allow55// generate_address to try to be smart about emitting the -1.56// Otherwise the patching code won't know how to find the57// instruction to patch.58addr_opr = LIR_OprFact::address(new LIR_Address(base.result(), PATCHED_ADDR, access.type()));59} else {60addr_opr = LIR_OprFact::address(gen->generate_address(base.result(), offset, 0, 0, access.type()));61}6263if (resolve_in_register) {64LIR_Opr resolved_addr = gen->new_pointer_register();65if (needs_patching) {66__ leal(addr_opr, resolved_addr, lir_patch_normal, access.patch_emit_info());67access.clear_decorators(C1_NEEDS_PATCHING);68} else {69__ leal(addr_opr, resolved_addr);70}71return LIR_OprFact::address(new LIR_Address(resolved_addr, access.type()));72} else {73return addr_opr;74}75}7677void BarrierSetC1::store_at(LIRAccess& access, LIR_Opr value) {78DecoratorSet decorators = access.decorators();79bool in_heap = (decorators & IN_HEAP) != 0;80assert(in_heap, "not supported yet");8182LIR_Opr resolved = resolve_address(access, false);83access.set_resolved_addr(resolved);84store_at_resolved(access, value);85}8687void BarrierSetC1::load_at(LIRAccess& access, LIR_Opr result) {88DecoratorSet decorators = access.decorators();89bool in_heap = (decorators & IN_HEAP) != 0;90assert(in_heap, "not supported yet");9192LIR_Opr resolved = resolve_address(access, false);93access.set_resolved_addr(resolved);94load_at_resolved(access, result);95}9697void BarrierSetC1::load(LIRAccess& access, LIR_Opr result) {98DecoratorSet decorators = access.decorators();99bool in_heap = (decorators & IN_HEAP) != 0;100assert(!in_heap, "consider using load_at");101load_at_resolved(access, result);102}103104LIR_Opr BarrierSetC1::atomic_cmpxchg_at(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value) {105DecoratorSet decorators = access.decorators();106bool in_heap = (decorators & IN_HEAP) != 0;107assert(in_heap, "not supported yet");108109access.load_address();110111LIR_Opr resolved = resolve_address(access, true);112access.set_resolved_addr(resolved);113return atomic_cmpxchg_at_resolved(access, cmp_value, new_value);114}115116LIR_Opr BarrierSetC1::atomic_xchg_at(LIRAccess& access, LIRItem& value) {117DecoratorSet decorators = access.decorators();118bool in_heap = (decorators & IN_HEAP) != 0;119assert(in_heap, "not supported yet");120121access.load_address();122123LIR_Opr resolved = resolve_address(access, true);124access.set_resolved_addr(resolved);125return atomic_xchg_at_resolved(access, value);126}127128LIR_Opr BarrierSetC1::atomic_add_at(LIRAccess& access, LIRItem& value) {129DecoratorSet decorators = access.decorators();130bool in_heap = (decorators & IN_HEAP) != 0;131assert(in_heap, "not supported yet");132133access.load_address();134135LIR_Opr resolved = resolve_address(access, true);136access.set_resolved_addr(resolved);137return atomic_add_at_resolved(access, value);138}139140void BarrierSetC1::store_at_resolved(LIRAccess& access, LIR_Opr value) {141DecoratorSet decorators = access.decorators();142bool is_volatile = (((decorators & MO_SEQ_CST) != 0) || AlwaysAtomicAccesses);143bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0;144bool mask_boolean = (decorators & C1_MASK_BOOLEAN) != 0;145LIRGenerator* gen = access.gen();146147if (mask_boolean) {148value = gen->mask_boolean(access.base().opr(), value, access.access_emit_info());149}150151if (is_volatile) {152__ membar_release();153}154155LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;156if (is_volatile && !needs_patching) {157gen->volatile_field_store(value, access.resolved_addr()->as_address_ptr(), access.access_emit_info());158} else {159__ store(value, access.resolved_addr()->as_address_ptr(), access.access_emit_info(), patch_code);160}161162if (is_volatile && !support_IRIW_for_not_multiple_copy_atomic_cpu) {163__ membar();164}165}166167void BarrierSetC1::load_at_resolved(LIRAccess& access, LIR_Opr result) {168LIRGenerator *gen = access.gen();169DecoratorSet decorators = access.decorators();170bool is_volatile = (((decorators & MO_SEQ_CST) != 0) || AlwaysAtomicAccesses);171bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0;172bool mask_boolean = (decorators & C1_MASK_BOOLEAN) != 0;173bool in_native = (decorators & IN_NATIVE) != 0;174175if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_volatile) {176__ membar();177}178179LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none;180if (in_native) {181__ move_wide(access.resolved_addr()->as_address_ptr(), result);182} else if (is_volatile && !needs_patching) {183gen->volatile_field_load(access.resolved_addr()->as_address_ptr(), result, access.access_emit_info());184} else {185__ load(access.resolved_addr()->as_address_ptr(), result, access.access_emit_info(), patch_code);186}187188if (is_volatile) {189__ membar_acquire();190}191192/* Normalize boolean value returned by unsafe operation, i.e., value != 0 ? value = true : value false. */193if (mask_boolean) {194LabelObj* equalZeroLabel = new LabelObj();195__ cmp(lir_cond_equal, result, 0);196__ branch(lir_cond_equal, equalZeroLabel->label());197__ move(LIR_OprFact::intConst(1), result);198__ branch_destination(equalZeroLabel->label());199}200}201202LIR_Opr BarrierSetC1::atomic_cmpxchg_at_resolved(LIRAccess& access, LIRItem& cmp_value, LIRItem& new_value) {203LIRGenerator *gen = access.gen();204return gen->atomic_cmpxchg(access.type(), access.resolved_addr(), cmp_value, new_value);205}206207LIR_Opr BarrierSetC1::atomic_xchg_at_resolved(LIRAccess& access, LIRItem& value) {208LIRGenerator *gen = access.gen();209return gen->atomic_xchg(access.type(), access.resolved_addr(), value);210}211212LIR_Opr BarrierSetC1::atomic_add_at_resolved(LIRAccess& access, LIRItem& value) {213LIRGenerator *gen = access.gen();214return gen->atomic_add(access.type(), access.resolved_addr(), value);215}216217void BarrierSetC1::generate_referent_check(LIRAccess& access, LabelObj* cont) {218// We might be reading the value of the referent field of a219// Reference object in order to attach it back to the live220// object graph. If G1 is enabled then we need to record221// the value that is being returned in an SATB log buffer.222//223// We need to generate code similar to the following...224//225// if (offset == java_lang_ref_Reference::referent_offset()) {226// if (src != NULL) {227// if (klass(src)->reference_type() != REF_NONE) {228// pre_barrier(..., value, ...);229// }230// }231// }232233bool gen_pre_barrier = true; // Assume we need to generate pre_barrier.234bool gen_offset_check = true; // Assume we need to generate the offset guard.235bool gen_source_check = true; // Assume we need to check the src object for null.236bool gen_type_check = true; // Assume we need to check the reference_type.237238LIRGenerator *gen = access.gen();239240LIRItem& base = access.base().item();241LIR_Opr offset = access.offset().opr();242243if (offset->is_constant()) {244LIR_Const* constant = offset->as_constant_ptr();245jlong off_con = (constant->type() == T_INT ?246(jlong)constant->as_jint() :247constant->as_jlong());248249250if (off_con != (jlong) java_lang_ref_Reference::referent_offset()) {251// The constant offset is something other than referent_offset.252// We can skip generating/checking the remaining guards and253// skip generation of the code stub.254gen_pre_barrier = false;255} else {256// The constant offset is the same as referent_offset -257// we do not need to generate a runtime offset check.258gen_offset_check = false;259}260}261262// We don't need to generate stub if the source object is an array263if (gen_pre_barrier && base.type()->is_array()) {264gen_pre_barrier = false;265}266267if (gen_pre_barrier) {268// We still need to continue with the checks.269if (base.is_constant()) {270ciObject* src_con = base.get_jobject_constant();271guarantee(src_con != NULL, "no source constant");272273if (src_con->is_null_object()) {274// The constant src object is null - We can skip275// generating the code stub.276gen_pre_barrier = false;277} else {278// Non-null constant source object. We still have to generate279// the slow stub - but we don't need to generate the runtime280// null object check.281gen_source_check = false;282}283}284}285if (gen_pre_barrier && !PatchALot) {286// Can the klass of object be statically determined to be287// a sub-class of Reference?288ciType* type = base.value()->declared_type();289if ((type != NULL) && type->is_loaded()) {290if (type->is_subtype_of(gen->compilation()->env()->Reference_klass())) {291gen_type_check = false;292} else if (type->is_klass() &&293!gen->compilation()->env()->Object_klass()->is_subtype_of(type->as_klass())) {294// Not Reference and not Object klass.295gen_pre_barrier = false;296}297}298}299300if (gen_pre_barrier) {301// We can have generate one runtime check here. Let's start with302// the offset check.303// Allocate temp register to base and load it here, otherwise304// control flow below may confuse register allocator.305LIR_Opr base_reg = gen->new_register(T_OBJECT);306__ move(base.result(), base_reg);307if (gen_offset_check) {308// if (offset != referent_offset) -> continue309// If offset is an int then we can do the comparison with the310// referent_offset constant; otherwise we need to move311// referent_offset into a temporary register and generate312// a reg-reg compare.313314LIR_Opr referent_off;315316if (offset->type() == T_INT) {317referent_off = LIR_OprFact::intConst(java_lang_ref_Reference::referent_offset());318} else {319assert(offset->type() == T_LONG, "what else?");320referent_off = gen->new_register(T_LONG);321__ move(LIR_OprFact::longConst(java_lang_ref_Reference::referent_offset()), referent_off);322}323__ cmp(lir_cond_notEqual, offset, referent_off);324__ branch(lir_cond_notEqual, cont->label());325}326if (gen_source_check) {327// offset is a const and equals referent offset328// if (source == null) -> continue329__ cmp(lir_cond_equal, base_reg, LIR_OprFact::oopConst(NULL));330__ branch(lir_cond_equal, cont->label());331}332LIR_Opr src_klass = gen->new_register(T_METADATA);333if (gen_type_check) {334// We have determined that offset == referent_offset && src != null.335// if (src->_klass->_reference_type == REF_NONE) -> continue336__ move(new LIR_Address(base_reg, oopDesc::klass_offset_in_bytes(), T_ADDRESS), src_klass);337LIR_Address* reference_type_addr = new LIR_Address(src_klass, in_bytes(InstanceKlass::reference_type_offset()), T_BYTE);338LIR_Opr reference_type = gen->new_register(T_INT);339__ move(reference_type_addr, reference_type);340__ cmp(lir_cond_equal, reference_type, LIR_OprFact::intConst(REF_NONE));341__ branch(lir_cond_equal, cont->label());342}343}344}345346347