--- /dev/null 2017-03-07 11:44:12.271151064 +0100 +++ new/src/share/vm/gc/shared/c1BarrierSetCodeGen.cpp 2017-04-25 16:46:23.803171403 +0200 @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "c1/c1_Defs.hpp" +#include "c1/c1_LIRGenerator.hpp" +#include "gc/shared/c1BarrierSetCodeGen.hpp" +#include "utilities/macros.hpp" + +#ifndef PATCHED_ADDR +#define PATCHED_ADDR (max_jint) +#endif + +#ifdef ASSERT +#define __ lir_generator->lir(__FILE__, __LINE__)-> +#else +#define __ lir_generator->lir()-> +#endif + +LIR_Opr C1BarrierSetCodeGen::resolve_address(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIR_Opr offset, bool resolve_in_register) { + bool on_array = (decorators & C1_ACCESS_ON_ARRAY) != 0; + bool is_oop = type == T_OBJECT || type == T_ARRAY; + bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0; + LIR_Opr addr; + if (on_array) { + addr = LIR_OprFact::address(lir_generator->emit_array_address(base.result(), offset, type, is_oop)); + } else if (needs_patching) { + // we need to patch the offset in the instruction so don't allow + // generate_address to try to be smart about emitting the -1. + // Otherwise the patching code won't know how to find the + // instruction to patch. + addr = LIR_OprFact::address(new LIR_Address(base.result(), PATCHED_ADDR, type)); + } else { + addr = LIR_OprFact::address(lir_generator->generate_address(base.result(), offset, 0, 0, type)); + } + + if (resolve_in_register) { + LIR_Opr resolved_addr = lir_generator->new_pointer_register(); + __ leal(addr, resolved_addr); + resolved_addr = LIR_OprFact::address(new LIR_Address(resolved_addr, type)); + return resolved_addr; + } else { + return addr; + } +} + +void C1BarrierSetCodeGen::store_at(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIR_Opr offset, LIR_Opr value, + CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) { + LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset, false); + store_at_resolved(lir_generator, decorators, type, addr, base, offset, value, patch_info, store_emit_info); +} + +LIR_Opr C1BarrierSetCodeGen::load_at(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIR_Opr offset, + CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) { + LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset, false); + return load_at_resolved(lir_generator, decorators, type, addr, base, offset, patch_info, load_emit_info); +} + +LIR_Opr C1BarrierSetCodeGen::cas_at(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) { + base.load_item(); + offset.load_nonconstant(); + + LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset.result(), true); + return cas_at_resolved(lir_generator, decorators, type, addr, base, offset, cmp_value, new_value); +} + +LIR_Opr C1BarrierSetCodeGen::swap_at(LIRGenerator* lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIRItem& offset, LIRItem& value) { + base.load_item(); + offset.load_nonconstant(); + + LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset.result(), true); + return swap_at_resolved(lir_generator, decorators, type, addr, base, offset, value); +} + +LIR_Opr C1BarrierSetCodeGen::add_at(LIRGenerator* lir_generator, C1DecoratorSet decorators, BasicType type, + LIRItem& base, LIRItem& offset, LIRItem& value) { + base.load_item(); + offset.load_nonconstant(); + + LIR_Opr addr = resolve_address(lir_generator, decorators, type, base, offset.result(), true); + return add_at_resolved(lir_generator, decorators, type, addr, base, offset, value); +} + +void C1BarrierSetCodeGen::store_at_resolved(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIR_Opr addr, LIRItem& base, LIR_Opr offset, LIR_Opr value, + CodeEmitInfo* patch_info, CodeEmitInfo* store_emit_info) { + bool is_volatile = (((decorators & C1_MO_VOLATILE) != 0) || AlwaysAtomicAccesses) && os::is_MP(); + bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0; + bool mask_boolean = (decorators & C1_MASK_BOOLEAN) != 0; + + if (mask_boolean) { + value = lir_generator->mask_boolean(base.result(), value, store_emit_info); + } + + if (is_volatile && os::is_MP()) { + __ membar_release(); + } + + LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none; + if (is_volatile && !needs_patching) { + lir_generator->volatile_field_store(value, addr->as_address_ptr(), store_emit_info); + } else { + __ store(value, addr->as_address_ptr(), store_emit_info, patch_code); + } + + if (is_volatile && !support_IRIW_for_not_multiple_copy_atomic_cpu) { + __ membar(); + } +} + +LIR_Opr C1BarrierSetCodeGen::load_at_resolved(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIR_Opr addr, LIRItem& base, LIR_Opr offset, + CodeEmitInfo* patch_info, CodeEmitInfo* load_emit_info) { + bool is_volatile = (((decorators & C1_MO_VOLATILE) != 0) || AlwaysAtomicAccesses) && os::is_MP(); + bool needs_patching = (decorators & C1_NEEDS_PATCHING) != 0; + bool mask_boolean = (decorators & C1_MASK_BOOLEAN) != 0; + BasicType val_bt = as_BasicType(as_ValueType(type)); + + LIR_Opr result = lir_generator->new_register(val_bt); + + if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_volatile) { + __ membar(); + } + + LIR_PatchCode patch_code = needs_patching ? lir_patch_normal : lir_patch_none; + if (is_volatile && !needs_patching) { + lir_generator->volatile_field_load(addr->as_address_ptr(), result, load_emit_info); + } else { + __ load(addr->as_address_ptr(), result, load_emit_info, patch_code); + } + + if (is_volatile && os::is_MP()) { + __ membar_acquire(); + } + + /* Normalize boolean value returned by unsafe operation, i.e., value != 0 ? value = true : value false. */ + if (mask_boolean) { + LabelObj* equalZeroLabel = new LabelObj(); + __ cmp(lir_cond_equal, result, 0); + __ branch(lir_cond_equal, T_BOOLEAN, equalZeroLabel->label()); + __ move(LIR_OprFact::intConst(1), result); + __ branch_destination(equalZeroLabel->label()); + } + + return result; +} + +LIR_Opr C1BarrierSetCodeGen::cas_at_resolved(LIRGenerator *lir_generator, C1DecoratorSet decorators, BasicType type, + LIR_Opr addr, LIRItem& base, LIRItem& offset, LIRItem& cmp_value, LIRItem& new_value) { + BasicType val_bt = as_BasicType(as_ValueType(type)); + return lir_generator->cas(val_bt, addr, cmp_value, new_value); +} + +LIR_Opr C1BarrierSetCodeGen::swap_at_resolved(LIRGenerator* lir_generator, C1DecoratorSet decorators, BasicType type, + LIR_Opr addr, LIRItem& base, LIRItem& offset, LIRItem& value) { + BasicType val_bt = as_BasicType(as_ValueType(type)); + return lir_generator->swap(val_bt, addr, value); +} + +LIR_Opr C1BarrierSetCodeGen::add_at_resolved(LIRGenerator* lir_generator, C1DecoratorSet decorators, BasicType type, + LIR_Opr addr, LIRItem& base, LIRItem& offset, LIRItem& value) { + BasicType val_bt = as_BasicType(as_ValueType(type)); + return lir_generator->add(val_bt, addr, value); +} + +void C1BarrierSetCodeGen::generate_referent_check(LIRGenerator* lir_generator, LIRItem& base, LIR_Opr offset, LabelObj* cont) { + // We might be reading the value of the referent field of a + // Reference object in order to attach it back to the live + // object graph. If G1 is enabled then we need to record + // the value that is being returned in an SATB log buffer. + // + // We need to generate code similar to the following... + // + // if (offset == java_lang_ref_Reference::referent_offset) { + // if (src != NULL) { + // if (klass(src)->reference_type() != REF_NONE) { + // pre_barrier(..., value, ...); + // } + // } + // } + + bool gen_pre_barrier = true; // Assume we need to generate pre_barrier. + bool gen_offset_check = true; // Assume we need to generate the offset guard. + bool gen_source_check = true; // Assume we need to check the src object for null. + bool gen_type_check = true; // Assume we need to check the reference_type. + + if (offset->is_constant()) { + LIR_Const* constant = offset->as_constant_ptr(); + jlong off_con = (constant->type() == T_INT ? + (jlong)constant->as_jint() : + constant->as_jlong()); + + + if (off_con != (jlong) java_lang_ref_Reference::referent_offset) { + // The constant offset is something other than referent_offset. + // We can skip generating/checking the remaining guards and + // skip generation of the code stub. + gen_pre_barrier = false; + } else { + // The constant offset is the same as referent_offset - + // we do not need to generate a runtime offset check. + gen_offset_check = false; + } + } + + // We don't need to generate stub if the source object is an array + if (gen_pre_barrier && base.type()->is_array()) { + gen_pre_barrier = false; + } + + if (gen_pre_barrier) { + // We still need to continue with the checks. + if (base.is_constant()) { + ciObject* src_con = base.get_jobject_constant(); + guarantee(src_con != NULL, "no source constant"); + + if (src_con->is_null_object()) { + // The constant src object is null - We can skip + // generating the code stub. + gen_pre_barrier = false; + } else { + // Non-null constant source object. We still have to generate + // the slow stub - but we don't need to generate the runtime + // null object check. + gen_source_check = false; + } + } + } + if (gen_pre_barrier && !PatchALot) { + // Can the klass of object be statically determined to be + // a sub-class of Reference? + ciType* type = base.value()->declared_type(); + if ((type != NULL) && type->is_loaded()) { + if (type->is_subtype_of(lir_generator->compilation()->env()->Reference_klass())) { + gen_type_check = false; + } else if (type->is_klass() && + !lir_generator->compilation()->env()->Object_klass()->is_subtype_of(type->as_klass())) { + // Not Reference and not Object klass. + gen_pre_barrier = false; + } + } + } + + if (gen_pre_barrier) { + // We can have generate one runtime check here. Let's start with + // the offset check. + if (gen_offset_check) { + // if (offset != referent_offset) -> continue + // If offset is an int then we can do the comparison with the + // referent_offset constant; otherwise we need to move + // referent_offset into a temporary register and generate + // a reg-reg compare. + + LIR_Opr referent_off; + + if (offset->type() == T_INT) { + referent_off = LIR_OprFact::intConst(java_lang_ref_Reference::referent_offset); + } else { + assert(offset->type() == T_LONG, "what else?"); + referent_off = lir_generator->new_register(T_LONG); + __ move(LIR_OprFact::longConst(java_lang_ref_Reference::referent_offset), referent_off); + } + __ cmp(lir_cond_notEqual, offset, referent_off); + __ branch(lir_cond_notEqual, offset->type(), cont->label()); + } + if (gen_source_check) { + // offset is a const and equals referent offset + // if (source == null) -> continue + __ cmp(lir_cond_equal, base.result(), LIR_OprFact::oopConst(NULL)); + __ branch(lir_cond_equal, T_OBJECT, cont->label()); + } + LIR_Opr src_klass = lir_generator->new_register(T_OBJECT); + if (gen_type_check) { + // We have determined that offset == referent_offset && src != null. + // if (src->_klass->_reference_type == REF_NONE) -> continue + __ move(new LIR_Address(base.result(), oopDesc::klass_offset_in_bytes(), T_ADDRESS), src_klass); + LIR_Address* reference_type_addr = new LIR_Address(src_klass, in_bytes(InstanceKlass::reference_type_offset()), T_BYTE); + LIR_Opr reference_type = lir_generator->new_register(T_INT); + __ move(reference_type_addr, reference_type); + __ cmp(lir_cond_equal, reference_type, LIR_OprFact::intConst(REF_NONE)); + __ branch(lir_cond_equal, T_INT, cont->label()); + } + } +}