Path: blob/master/src/hotspot/share/gc/shenandoah/c2/shenandoahBarrierSetC2.cpp
40976 views
/*1* Copyright (c) 2018, 2021, Red Hat, Inc. 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 "classfile/javaClasses.hpp"26#include "gc/shared/barrierSet.hpp"27#include "gc/shenandoah/shenandoahBarrierSet.hpp"28#include "gc/shenandoah/shenandoahForwarding.hpp"29#include "gc/shenandoah/shenandoahHeap.hpp"30#include "gc/shenandoah/shenandoahRuntime.hpp"31#include "gc/shenandoah/shenandoahThreadLocalData.hpp"32#include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"33#include "gc/shenandoah/c2/shenandoahSupport.hpp"34#include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"35#include "opto/arraycopynode.hpp"36#include "opto/escape.hpp"37#include "opto/graphKit.hpp"38#include "opto/idealKit.hpp"39#include "opto/macro.hpp"40#include "opto/movenode.hpp"41#include "opto/narrowptrnode.hpp"42#include "opto/rootnode.hpp"43#include "opto/runtime.hpp"4445ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() {46return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2());47}4849ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena)50: _iu_barriers(new (comp_arena) GrowableArray<ShenandoahIUBarrierNode*>(comp_arena, 8, 0, NULL)),51_load_reference_barriers(new (comp_arena) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena, 8, 0, NULL)) {52}5354int ShenandoahBarrierSetC2State::iu_barriers_count() const {55return _iu_barriers->length();56}5758ShenandoahIUBarrierNode* ShenandoahBarrierSetC2State::iu_barrier(int idx) const {59return _iu_barriers->at(idx);60}6162void ShenandoahBarrierSetC2State::add_iu_barrier(ShenandoahIUBarrierNode* n) {63assert(!_iu_barriers->contains(n), "duplicate entry in barrier list");64_iu_barriers->append(n);65}6667void ShenandoahBarrierSetC2State::remove_iu_barrier(ShenandoahIUBarrierNode* n) {68_iu_barriers->remove_if_existing(n);69}7071int ShenandoahBarrierSetC2State::load_reference_barriers_count() const {72return _load_reference_barriers->length();73}7475ShenandoahLoadReferenceBarrierNode* ShenandoahBarrierSetC2State::load_reference_barrier(int idx) const {76return _load_reference_barriers->at(idx);77}7879void ShenandoahBarrierSetC2State::add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {80assert(!_load_reference_barriers->contains(n), "duplicate entry in barrier list");81_load_reference_barriers->append(n);82}8384void ShenandoahBarrierSetC2State::remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) {85if (_load_reference_barriers->contains(n)) {86_load_reference_barriers->remove(n);87}88}8990Node* ShenandoahBarrierSetC2::shenandoah_iu_barrier(GraphKit* kit, Node* obj) const {91if (ShenandoahIUBarrier) {92return kit->gvn().transform(new ShenandoahIUBarrierNode(obj));93}94return obj;95}9697#define __ kit->9899bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseTransform* phase, Node* adr,100BasicType bt, uint adr_idx) const {101intptr_t offset = 0;102Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset);103AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase);104105if (offset == Type::OffsetBot) {106return false; // cannot unalias unless there are precise offsets107}108109if (alloc == NULL) {110return false; // No allocation found111}112113intptr_t size_in_bytes = type2aelembytes(bt);114115Node* mem = __ memory(adr_idx); // start searching here...116117for (int cnt = 0; cnt < 50; cnt++) {118119if (mem->is_Store()) {120121Node* st_adr = mem->in(MemNode::Address);122intptr_t st_offset = 0;123Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset);124125if (st_base == NULL) {126break; // inscrutable pointer127}128129// Break we have found a store with same base and offset as ours so break130if (st_base == base && st_offset == offset) {131break;132}133134if (st_offset != offset && st_offset != Type::OffsetBot) {135const int MAX_STORE = BytesPerLong;136if (st_offset >= offset + size_in_bytes ||137st_offset <= offset - MAX_STORE ||138st_offset <= offset - mem->as_Store()->memory_size()) {139// Success: The offsets are provably independent.140// (You may ask, why not just test st_offset != offset and be done?141// The answer is that stores of different sizes can co-exist142// in the same sequence of RawMem effects. We sometimes initialize143// a whole 'tile' of array elements with a single jint or jlong.)144mem = mem->in(MemNode::Memory);145continue; // advance through independent store memory146}147}148149if (st_base != base150&& MemNode::detect_ptr_independence(base, alloc, st_base,151AllocateNode::Ideal_allocation(st_base, phase),152phase)) {153// Success: The bases are provably independent.154mem = mem->in(MemNode::Memory);155continue; // advance through independent store memory156}157} else if (mem->is_Proj() && mem->in(0)->is_Initialize()) {158159InitializeNode* st_init = mem->in(0)->as_Initialize();160AllocateNode* st_alloc = st_init->allocation();161162// Make sure that we are looking at the same allocation site.163// The alloc variable is guaranteed to not be null here from earlier check.164if (alloc == st_alloc) {165// Check that the initialization is storing NULL so that no previous store166// has been moved up and directly write a reference167Node* captured_store = st_init->find_captured_store(offset,168type2aelembytes(T_OBJECT),169phase);170if (captured_store == NULL || captured_store == st_init->zero_memory()) {171return true;172}173}174}175176// Unless there is an explicit 'continue', we must bail out here,177// because 'mem' is an inscrutable memory state (e.g., a call).178break;179}180181return false;182}183184#undef __185#define __ ideal.186187void ShenandoahBarrierSetC2::satb_write_barrier_pre(GraphKit* kit,188bool do_load,189Node* obj,190Node* adr,191uint alias_idx,192Node* val,193const TypeOopPtr* val_type,194Node* pre_val,195BasicType bt) const {196// Some sanity checks197// Note: val is unused in this routine.198199if (do_load) {200// We need to generate the load of the previous value201assert(obj != NULL, "must have a base");202assert(adr != NULL, "where are loading from?");203assert(pre_val == NULL, "loaded already?");204assert(val_type != NULL, "need a type");205206if (ReduceInitialCardMarks207&& satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, bt, alias_idx)) {208return;209}210211} else {212// In this case both val_type and alias_idx are unused.213assert(pre_val != NULL, "must be loaded already");214// Nothing to be done if pre_val is null.215if (pre_val->bottom_type() == TypePtr::NULL_PTR) return;216assert(pre_val->bottom_type()->basic_type() == T_OBJECT, "or we shouldn't be here");217}218assert(bt == T_OBJECT, "or we shouldn't be here");219220IdealKit ideal(kit, true);221222Node* tls = __ thread(); // ThreadLocalStorage223224Node* no_base = __ top();225Node* zero = __ ConI(0);226Node* zeroX = __ ConX(0);227228float likely = PROB_LIKELY(0.999);229float unlikely = PROB_UNLIKELY(0.999);230231// Offsets into the thread232const int index_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset());233const int buffer_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());234235// Now the actual pointers into the thread236Node* buffer_adr = __ AddP(no_base, tls, __ ConX(buffer_offset));237Node* index_adr = __ AddP(no_base, tls, __ ConX(index_offset));238239// Now some of the values240Node* marking;241Node* gc_state = __ AddP(no_base, tls, __ ConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset())));242Node* ld = __ load(__ ctrl(), gc_state, TypeInt::BYTE, T_BYTE, Compile::AliasIdxRaw);243marking = __ AndI(ld, __ ConI(ShenandoahHeap::MARKING));244assert(ShenandoahBarrierC2Support::is_gc_state_load(ld), "Should match the shape");245246// if (!marking)247__ if_then(marking, BoolTest::ne, zero, unlikely); {248BasicType index_bt = TypeX_X->basic_type();249assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading Shenandoah SATBMarkQueue::_index with wrong size.");250Node* index = __ load(__ ctrl(), index_adr, TypeX_X, index_bt, Compile::AliasIdxRaw);251252if (do_load) {253// load original value254// alias_idx correct??255pre_val = __ load(__ ctrl(), adr, val_type, bt, alias_idx);256}257258// if (pre_val != NULL)259__ if_then(pre_val, BoolTest::ne, kit->null()); {260Node* buffer = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw);261262// is the queue for this thread full?263__ if_then(index, BoolTest::ne, zeroX, likely); {264265// decrement the index266Node* next_index = kit->gvn().transform(new SubXNode(index, __ ConX(sizeof(intptr_t))));267268// Now get the buffer location we will log the previous value into and store it269Node *log_addr = __ AddP(no_base, buffer, next_index);270__ store(__ ctrl(), log_addr, pre_val, T_OBJECT, Compile::AliasIdxRaw, MemNode::unordered);271// update the index272__ store(__ ctrl(), index_adr, next_index, index_bt, Compile::AliasIdxRaw, MemNode::unordered);273274} __ else_(); {275276// logging buffer is full, call the runtime277const TypeFunc *tf = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type();278__ make_leaf_call(tf, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", pre_val, tls);279} __ end_if(); // (!index)280} __ end_if(); // (pre_val != NULL)281} __ end_if(); // (!marking)282283// Final sync IdealKit and GraphKit.284kit->final_sync(ideal);285286if (ShenandoahSATBBarrier && adr != NULL) {287Node* c = kit->control();288Node* call = c->in(1)->in(1)->in(1)->in(0);289assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected");290call->add_req(adr);291}292}293294bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) {295return call->is_CallLeaf() &&296call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry);297}298299bool ShenandoahBarrierSetC2::is_shenandoah_lrb_call(Node* call) {300if (!call->is_CallLeaf()) {301return false;302}303304address entry_point = call->as_CallLeaf()->entry_point();305return (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}311312bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseTransform *phase, Node* n) {313if (n->Opcode() != Op_If) {314return false;315}316317Node* bol = n->in(1);318assert(bol->is_Bool(), "");319Node* cmpx = bol->in(1);320if (bol->as_Bool()->_test._test == BoolTest::ne &&321cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) &&322is_shenandoah_state_load(cmpx->in(1)->in(1)) &&323cmpx->in(1)->in(2)->is_Con() &&324cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) {325return true;326}327328return false;329}330331bool ShenandoahBarrierSetC2::is_shenandoah_state_load(Node* n) {332if (!n->is_Load()) return false;333const int state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset());334return n->in(2)->is_AddP() && n->in(2)->in(2)->Opcode() == Op_ThreadLocal335&& n->in(2)->in(3)->is_Con()336&& n->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == state_offset;337}338339void ShenandoahBarrierSetC2::shenandoah_write_barrier_pre(GraphKit* kit,340bool do_load,341Node* obj,342Node* adr,343uint alias_idx,344Node* val,345const TypeOopPtr* val_type,346Node* pre_val,347BasicType bt) const {348if (ShenandoahSATBBarrier) {349IdealKit ideal(kit);350kit->sync_kit(ideal);351352satb_write_barrier_pre(kit, do_load, obj, adr, alias_idx, val, val_type, pre_val, bt);353354ideal.sync_kit(kit);355kit->final_sync(ideal);356}357}358359// Helper that guards and inserts a pre-barrier.360void ShenandoahBarrierSetC2::insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset,361Node* pre_val, bool need_mem_bar) const {362// We could be accessing the referent field of a reference object. If so, when Shenandoah363// is enabled, we need to log the value in the referent field in an SATB buffer.364// This routine performs some compile time filters and generates suitable365// runtime filters that guard the pre-barrier code.366// Also add memory barrier for non volatile load from the referent field367// to prevent commoning of loads across safepoint.368369// Some compile time checks.370371// If offset is a constant, is it java_lang_ref_Reference::_reference_offset?372const TypeX* otype = offset->find_intptr_t_type();373if (otype != NULL && otype->is_con() &&374otype->get_con() != java_lang_ref_Reference::referent_offset()) {375// Constant offset but not the reference_offset so just return376return;377}378379// We only need to generate the runtime guards for instances.380const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();381if (btype != NULL) {382if (btype->isa_aryptr()) {383// Array type so nothing to do384return;385}386387const TypeInstPtr* itype = btype->isa_instptr();388if (itype != NULL) {389// Can the klass of base_oop be statically determined to be390// _not_ a sub-class of Reference and _not_ Object?391ciKlass* klass = itype->klass();392if ( klass->is_loaded() &&393!klass->is_subtype_of(kit->env()->Reference_klass()) &&394!kit->env()->Object_klass()->is_subtype_of(klass)) {395return;396}397}398}399400// The compile time filters did not reject base_oop/offset so401// we need to generate the following runtime filters402//403// if (offset == java_lang_ref_Reference::_reference_offset) {404// if (instance_of(base, java.lang.ref.Reference)) {405// pre_barrier(_, pre_val, ...);406// }407// }408409float likely = PROB_LIKELY( 0.999);410float unlikely = PROB_UNLIKELY(0.999);411412IdealKit ideal(kit);413414Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset());415416__ if_then(offset, BoolTest::eq, referent_off, unlikely); {417// Update graphKit memory and control from IdealKit.418kit->sync_kit(ideal);419420Node* ref_klass_con = kit->makecon(TypeKlassPtr::make(kit->env()->Reference_klass()));421Node* is_instof = kit->gen_instanceof(base_oop, ref_klass_con);422423// Update IdealKit memory and control from graphKit.424__ sync_kit(kit);425426Node* one = __ ConI(1);427// is_instof == 0 if base_oop == NULL428__ if_then(is_instof, BoolTest::eq, one, unlikely); {429430// Update graphKit from IdeakKit.431kit->sync_kit(ideal);432433// Use the pre-barrier to record the value in the referent field434satb_write_barrier_pre(kit, false /* do_load */,435NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,436pre_val /* pre_val */,437T_OBJECT);438if (need_mem_bar) {439// Add memory barrier to prevent commoning reads from this field440// across safepoint since GC can change its value.441kit->insert_mem_bar(Op_MemBarCPUOrder);442}443// Update IdealKit from graphKit.444__ sync_kit(kit);445446} __ end_if(); // _ref_type != ref_none447} __ end_if(); // offset == referent_offset448449// Final sync IdealKit and GraphKit.450kit->final_sync(ideal);451}452453#undef __454455const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() {456const Type **fields = TypeTuple::fields(2);457fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value458fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread459const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);460461// create result type (range)462fields = TypeTuple::fields(0);463const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);464465return TypeFunc::make(domain, range);466}467468const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() {469const Type **fields = TypeTuple::fields(1);470fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop471const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);472473// create result type (range)474fields = TypeTuple::fields(0);475const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);476477return TypeFunc::make(domain, range);478}479480const TypeFunc* ShenandoahBarrierSetC2::shenandoah_load_reference_barrier_Type() {481const Type **fields = TypeTuple::fields(2);482fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; // original field value483fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // original load address484485const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);486487// create result type (range)488fields = TypeTuple::fields(1);489fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM;490const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);491492return TypeFunc::make(domain, range);493}494495Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const {496DecoratorSet decorators = access.decorators();497498const TypePtr* adr_type = access.addr().type();499Node* adr = access.addr().node();500501bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0;502bool on_heap = (decorators & IN_HEAP) != 0;503504if (!access.is_oop() || (!on_heap && !anonymous)) {505return BarrierSetC2::store_at_resolved(access, val);506}507508if (access.is_parse_access()) {509C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);510GraphKit* kit = parse_access.kit();511512uint adr_idx = kit->C->get_alias_index(adr_type);513assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" );514Node* value = val.node();515value = shenandoah_iu_barrier(kit, value);516val.set_node(value);517shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(),518static_cast<const TypeOopPtr*>(val.type()), NULL /* pre_val */, access.type());519} else {520assert(access.is_opt_access(), "only for optimization passes");521assert(((decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0 || !ShenandoahSATBBarrier) && (decorators & C2_ARRAY_COPY) != 0, "unexpected caller of this code");522C2OptAccess& opt_access = static_cast<C2OptAccess&>(access);523PhaseGVN& gvn = opt_access.gvn();524525if (ShenandoahIUBarrier) {526Node* enqueue = gvn.transform(new ShenandoahIUBarrierNode(val.node()));527val.set_node(enqueue);528}529}530return BarrierSetC2::store_at_resolved(access, val);531}532533Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const {534// 1: non-reference load, no additional barrier is needed535if (!access.is_oop()) {536return BarrierSetC2::load_at_resolved(access, val_type);;537}538539Node* load = BarrierSetC2::load_at_resolved(access, val_type);540DecoratorSet decorators = access.decorators();541BasicType type = access.type();542543// 2: apply LRB if needed544if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) {545load = new ShenandoahLoadReferenceBarrierNode(NULL, load, decorators);546if (access.is_parse_access()) {547load = static_cast<C2ParseAccess &>(access).kit()->gvn().transform(load);548} else {549load = static_cast<C2OptAccess &>(access).gvn().transform(load);550}551}552553// 3: apply keep-alive barrier for java.lang.ref.Reference if needed554if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) {555Node* top = Compile::current()->top();556Node* adr = access.addr().node();557Node* offset = adr->is_AddP() ? adr->in(AddPNode::Offset) : top;558Node* obj = access.base();559560bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;561bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;562bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;563564// If we are reading the value of the referent field of a Reference565// object (either by using Unsafe directly or through reflection)566// then, if SATB is enabled, we need to record the referent in an567// SATB log buffer using the pre-barrier mechanism.568// Also we need to add memory barrier to prevent commoning reads569// from this field across safepoint since GC can change its value.570if (!on_weak_ref || (unknown && (offset == top || obj == top)) || !keep_alive) {571return load;572}573574assert(access.is_parse_access(), "entry not supported at optimization time");575C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access);576GraphKit* kit = parse_access.kit();577bool mismatched = (decorators & C2_MISMATCHED) != 0;578bool is_unordered = (decorators & MO_UNORDERED) != 0;579bool in_native = (decorators & IN_NATIVE) != 0;580bool need_cpu_mem_bar = !is_unordered || mismatched || in_native;581582if (on_weak_ref) {583// Use the pre-barrier to record the value in the referent field584satb_write_barrier_pre(kit, false /* do_load */,585NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,586load /* pre_val */, T_OBJECT);587// Add memory barrier to prevent commoning reads from this field588// across safepoint since GC can change its value.589kit->insert_mem_bar(Op_MemBarCPUOrder);590} else if (unknown) {591// We do not require a mem bar inside pre_barrier if need_mem_bar592// is set: the barriers would be emitted by us.593insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar);594}595}596597return load;598}599600Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val,601Node* new_val, const Type* value_type) const {602GraphKit* kit = access.kit();603if (access.is_oop()) {604new_val = shenandoah_iu_barrier(kit, new_val);605shenandoah_write_barrier_pre(kit, false /* do_load */,606NULL, NULL, max_juint, NULL, NULL,607expected_val /* pre_val */, T_OBJECT);608609MemNode::MemOrd mo = access.mem_node_mo();610Node* mem = access.memory();611Node* adr = access.addr().node();612const TypePtr* adr_type = access.addr().type();613Node* load_store = NULL;614615#ifdef _LP64616if (adr->bottom_type()->is_ptr_to_narrowoop()) {617Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));618Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));619if (ShenandoahCASBarrier) {620load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));621} else {622load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo));623}624} else625#endif626{627if (ShenandoahCASBarrier) {628load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));629} else {630load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo));631}632}633634access.set_raw_access(load_store);635pin_atomic_op(access);636637#ifdef _LP64638if (adr->bottom_type()->is_ptr_to_narrowoop()) {639load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type()));640}641#endif642load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(NULL, load_store, access.decorators()));643return load_store;644}645return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type);646}647648Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val,649Node* new_val, const Type* value_type) const {650GraphKit* kit = access.kit();651if (access.is_oop()) {652new_val = shenandoah_iu_barrier(kit, new_val);653shenandoah_write_barrier_pre(kit, false /* do_load */,654NULL, NULL, max_juint, NULL, NULL,655expected_val /* pre_val */, T_OBJECT);656DecoratorSet decorators = access.decorators();657MemNode::MemOrd mo = access.mem_node_mo();658Node* mem = access.memory();659bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0;660Node* load_store = NULL;661Node* adr = access.addr().node();662#ifdef _LP64663if (adr->bottom_type()->is_ptr_to_narrowoop()) {664Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop()));665Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop()));666if (ShenandoahCASBarrier) {667if (is_weak_cas) {668load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));669} else {670load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));671}672} else {673if (is_weak_cas) {674load_store = kit->gvn().transform(new WeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));675} else {676load_store = kit->gvn().transform(new CompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo));677}678}679} else680#endif681{682if (ShenandoahCASBarrier) {683if (is_weak_cas) {684load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));685} else {686load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));687}688} else {689if (is_weak_cas) {690load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));691} else {692load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo));693}694}695}696access.set_raw_access(load_store);697pin_atomic_op(access);698return load_store;699}700return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type);701}702703Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const {704GraphKit* kit = access.kit();705if (access.is_oop()) {706val = shenandoah_iu_barrier(kit, val);707}708Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type);709if (access.is_oop()) {710result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(NULL, result, access.decorators()));711shenandoah_write_barrier_pre(kit, false /* do_load */,712NULL, NULL, max_juint, NULL, NULL,713result /* pre_val */, T_OBJECT);714}715return result;716}717718// Support for GC barriers emitted during parsing719bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const {720if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) return true;721if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) {722return false;723}724CallLeafNode *call = node->as_CallLeaf();725if (call->_name == NULL) {726return false;727}728729return strcmp(call->_name, "shenandoah_clone_barrier") == 0 ||730strcmp(call->_name, "shenandoah_cas_obj") == 0 ||731strcmp(call->_name, "shenandoah_wb_pre") == 0;732}733734Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const {735if (c == NULL) {736return c;737}738if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) {739return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn);740}741if (c->Opcode() == Op_ShenandoahIUBarrier) {742c = c->in(1);743}744return c;745}746747bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const {748return !ShenandoahBarrierC2Support::expand(C, igvn);749}750751bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const {752if (mode == LoopOptsShenandoahExpand) {753assert(UseShenandoahGC, "only for shenandoah");754ShenandoahBarrierC2Support::pin_and_expand(phase);755return true;756} else if (mode == LoopOptsShenandoahPostExpand) {757assert(UseShenandoahGC, "only for shenandoah");758visited.clear();759ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase);760return true;761}762return false;763}764765bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const {766bool is_oop = is_reference_type(type);767if (!is_oop) {768return false;769}770if (ShenandoahSATBBarrier && tightly_coupled_alloc) {771if (phase == Optimization) {772return false;773}774return !is_clone;775}776if (phase == Optimization) {777return !ShenandoahIUBarrier;778}779return true;780}781782bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) {783const TypeOopPtr* src_type = gvn.type(src)->is_oopptr();784if (src_type->isa_instptr() != NULL) {785ciInstanceKlass* ik = src_type->klass()->as_instance_klass();786if ((src_type->klass_is_exact() || (!ik->is_interface() && !ik->has_subklass())) && !ik->has_injected_fields()) {787if (ik->has_object_fields()) {788return true;789} else {790if (!src_type->klass_is_exact()) {791Compile::current()->dependencies()->assert_leaf_type(ik);792}793}794} else {795return true;796}797} else if (src_type->isa_aryptr()) {798BasicType src_elem = src_type->klass()->as_array_klass()->element_type()->basic_type();799if (is_reference_type(src_elem)) {800return true;801}802} else {803return true;804}805return false;806}807808void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const {809Node* ctrl = ac->in(TypeFunc::Control);810Node* mem = ac->in(TypeFunc::Memory);811Node* src_base = ac->in(ArrayCopyNode::Src);812Node* src_offset = ac->in(ArrayCopyNode::SrcPos);813Node* dest_base = ac->in(ArrayCopyNode::Dest);814Node* dest_offset = ac->in(ArrayCopyNode::DestPos);815Node* length = ac->in(ArrayCopyNode::Length);816817Node* src = phase->basic_plus_adr(src_base, src_offset);818Node* dest = phase->basic_plus_adr(dest_base, dest_offset);819820if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) {821// Check if heap is has forwarded objects. If it does, we need to call into the special822// routine that would fix up source references before we can continue.823824enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT };825Node* region = new RegionNode(PATH_LIMIT);826Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM);827828Node* thread = phase->transform_later(new ThreadLocalNode());829Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()));830Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset));831832uint gc_state_idx = Compile::AliasIdxRaw;833const TypePtr* gc_state_adr_type = NULL; // debug-mode-only argument834debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx));835836Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered));837int flags = ShenandoahHeap::HAS_FORWARDED;838if (ShenandoahIUBarrier) {839flags |= ShenandoahHeap::MARKING;840}841Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(flags)));842Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT)));843Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne));844845IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If();846Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff));847Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff));848849// Heap is stable, no need to do anything additional850region->init_req(_heap_stable, stable_ctrl);851mem_phi->init_req(_heap_stable, mem);852853// Heap is unstable, call into clone barrier stub854Node* call = phase->make_leaf_call(unstable_ctrl, mem,855ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(),856CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier),857"shenandoah_clone",858TypeRawPtr::BOTTOM,859src_base);860call = phase->transform_later(call);861862ctrl = phase->transform_later(new ProjNode(call, TypeFunc::Control));863mem = phase->transform_later(new ProjNode(call, TypeFunc::Memory));864region->init_req(_heap_unstable, ctrl);865mem_phi->init_req(_heap_unstable, mem);866867// Wire up the actual arraycopy stub now868ctrl = phase->transform_later(region);869mem = phase->transform_later(mem_phi);870871const char* name = "arraycopy";872call = phase->make_leaf_call(ctrl, mem,873OptoRuntime::fast_arraycopy_Type(),874phase->basictype2arraycopy(T_LONG, NULL, NULL, true, name, true),875name, TypeRawPtr::BOTTOM,876src, dest, length877LP64_ONLY(COMMA phase->top()));878call = phase->transform_later(call);879880// Hook up the whole thing into the graph881phase->igvn().replace_node(ac, call);882} else {883BarrierSetC2::clone_at_expansion(phase, ac);884}885}886887888// Support for macro expanded GC barriers889void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const {890if (node->Opcode() == Op_ShenandoahIUBarrier) {891state()->add_iu_barrier((ShenandoahIUBarrierNode*) node);892}893if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {894state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);895}896}897898void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const {899if (node->Opcode() == Op_ShenandoahIUBarrier) {900state()->remove_iu_barrier((ShenandoahIUBarrierNode*) node);901}902if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) {903state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node);904}905}906907void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* n) const {908if (is_shenandoah_wb_pre_call(n)) {909shenandoah_eliminate_wb_pre(n, ¯o->igvn());910}911}912913void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const {914assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), "");915Node* c = call->as_Call()->proj_out(TypeFunc::Control);916c = c->unique_ctrl_out();917assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");918c = c->unique_ctrl_out();919assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");920Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);921assert(iff->is_If(), "expect test");922if (!is_shenandoah_marking_if(igvn, iff)) {923c = c->unique_ctrl_out();924assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?");925iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0);926assert(is_shenandoah_marking_if(igvn, iff), "expect marking test");927}928Node* cmpx = iff->in(1)->in(1);929igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ));930igvn->rehash_node_delayed(call);931call->del_req(call->req()-1);932}933934void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const {935if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) {936igvn->add_users_to_worklist(node);937}938}939940void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const {941for (uint i = 0; i < useful.size(); i++) {942Node* n = useful.at(i);943if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) {944for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {945C->record_for_igvn(n->fast_out(i));946}947}948}949for (int i = state()->iu_barriers_count() - 1; i >= 0; i--) {950ShenandoahIUBarrierNode* n = state()->iu_barrier(i);951if (!useful.member(n)) {952state()->remove_iu_barrier(n);953}954}955for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) {956ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i);957if (!useful.member(n)) {958state()->remove_load_reference_barrier(n);959}960}961}962963void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const {964return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena);965}966967ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const {968return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state());969}970971// If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be972// expanded later, then now is the time to do so.973bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; }974975#ifdef ASSERT976void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const {977if (ShenandoahVerifyOptoBarriers && phase == BarrierSetC2::BeforeMacroExpand) {978ShenandoahBarrierC2Support::verify(Compile::current()->root());979} else if (phase == BarrierSetC2::BeforeCodeGen) {980// Verify Shenandoah pre-barriers981const int marking_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset());982983Unique_Node_List visited;984Node_List worklist;985// We're going to walk control flow backwards starting from the Root986worklist.push(compile->root());987while (worklist.size() > 0) {988Node *x = worklist.pop();989if (x == NULL || x == compile->top()) continue;990if (visited.member(x)) {991continue;992} else {993visited.push(x);994}995996if (x->is_Region()) {997for (uint i = 1; i < x->req(); i++) {998worklist.push(x->in(i));999}1000} else {1001worklist.push(x->in(0));1002// We are looking for the pattern:1003// /->ThreadLocal1004// If->Bool->CmpI->LoadB->AddP->ConL(marking_offset)1005// \->ConI(0)1006// We want to verify that the If and the LoadB have the same control1007// See GraphKit::g1_write_barrier_pre()1008if (x->is_If()) {1009IfNode *iff = x->as_If();1010if (iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp()) {1011CmpNode *cmp = iff->in(1)->in(1)->as_Cmp();1012if (cmp->Opcode() == Op_CmpI && cmp->in(2)->is_Con() && cmp->in(2)->bottom_type()->is_int()->get_con() == 01013&& cmp->in(1)->is_Load()) {1014LoadNode *load = cmp->in(1)->as_Load();1015if (load->Opcode() == Op_LoadB && load->in(2)->is_AddP() && load->in(2)->in(2)->Opcode() == Op_ThreadLocal1016&& load->in(2)->in(3)->is_Con()1017&& load->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == marking_offset) {10181019Node *if_ctrl = iff->in(0);1020Node *load_ctrl = load->in(0);10211022if (if_ctrl != load_ctrl) {1023// Skip possible CProj->NeverBranch in infinite loops1024if ((if_ctrl->is_Proj() && if_ctrl->Opcode() == Op_CProj)1025&& (if_ctrl->in(0)->is_MultiBranch() && if_ctrl->in(0)->Opcode() == Op_NeverBranch)) {1026if_ctrl = if_ctrl->in(0)->in(0);1027}1028}1029assert(load_ctrl != NULL && if_ctrl == load_ctrl, "controls must match");1030}1031}1032}1033}1034}1035}1036}1037}1038#endif10391040Node* ShenandoahBarrierSetC2::ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const {1041if (is_shenandoah_wb_pre_call(n)) {1042uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt();1043if (n->req() > cnt) {1044Node* addp = n->in(cnt);1045if (has_only_shenandoah_wb_pre_uses(addp)) {1046n->del_req(cnt);1047if (can_reshape) {1048phase->is_IterGVN()->_worklist.push(addp);1049}1050return n;1051}1052}1053}1054if (n->Opcode() == Op_CmpP) {1055Node* in1 = n->in(1);1056Node* in2 = n->in(2);10571058// If one input is NULL, then step over the strong LRB barriers on the other input1059if (in1->bottom_type() == TypePtr::NULL_PTR &&1060!((in2->Opcode() == Op_ShenandoahLoadReferenceBarrier) &&1061!ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in2)->decorators()))) {1062in2 = step_over_gc_barrier(in2);1063}1064if (in2->bottom_type() == TypePtr::NULL_PTR &&1065!((in1->Opcode() == Op_ShenandoahLoadReferenceBarrier) &&1066!ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in1)->decorators()))) {1067in1 = step_over_gc_barrier(in1);1068}10691070PhaseIterGVN* igvn = phase->is_IterGVN();1071if (in1 != n->in(1)) {1072if (igvn != NULL) {1073n->set_req_X(1, in1, igvn);1074} else {1075n->set_req(1, in1);1076}1077assert(in2 == n->in(2), "only one change");1078return n;1079}1080if (in2 != n->in(2)) {1081if (igvn != NULL) {1082n->set_req_X(2, in2, igvn);1083} else {1084n->set_req(2, in2);1085}1086return n;1087}1088} else if (can_reshape &&1089n->Opcode() == Op_If &&1090ShenandoahBarrierC2Support::is_heap_stable_test(n) &&1091n->in(0) != NULL) {1092Node* dom = n->in(0);1093Node* prev_dom = n;1094int op = n->Opcode();1095int dist = 16;1096// Search up the dominator tree for another heap stable test1097while (dom->Opcode() != op || // Not same opcode?1098!ShenandoahBarrierC2Support::is_heap_stable_test(dom) || // Not same input 1?1099prev_dom->in(0) != dom) { // One path of test does not dominate?1100if (dist < 0) return NULL;11011102dist--;1103prev_dom = dom;1104dom = IfNode::up_one_dom(dom);1105if (!dom) return NULL;1106}11071108// Check that we did not follow a loop back to ourselves1109if (n == dom) {1110return NULL;1111}11121113return n->as_If()->dominated_by(prev_dom, phase->is_IterGVN());1114}11151116return NULL;1117}11181119bool ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(Node* n) {1120for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {1121Node* u = n->fast_out(i);1122if (!is_shenandoah_wb_pre_call(u)) {1123return false;1124}1125}1126return n->outcnt() > 0;1127}11281129bool ShenandoahBarrierSetC2::final_graph_reshaping(Compile* compile, Node* n, uint opcode) const {1130switch (opcode) {1131case Op_CallLeaf:1132case Op_CallLeafNoFP: {1133assert (n->is_Call(), "");1134CallNode *call = n->as_Call();1135if (ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(call)) {1136uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt();1137if (call->req() > cnt) {1138assert(call->req() == cnt + 1, "only one extra input");1139Node *addp = call->in(cnt);1140assert(!ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(addp), "useless address computation?");1141call->del_req(cnt);1142}1143}1144return false;1145}1146case Op_ShenandoahCompareAndSwapP:1147case Op_ShenandoahCompareAndSwapN:1148case Op_ShenandoahWeakCompareAndSwapN:1149case Op_ShenandoahWeakCompareAndSwapP:1150case Op_ShenandoahCompareAndExchangeP:1151case Op_ShenandoahCompareAndExchangeN:1152return true;1153case Op_ShenandoahLoadReferenceBarrier:1154assert(false, "should have been expanded already");1155return true;1156default:1157return false;1158}1159}11601161bool ShenandoahBarrierSetC2::escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const {1162switch (opcode) {1163case Op_ShenandoahCompareAndExchangeP:1164case Op_ShenandoahCompareAndExchangeN:1165conn_graph->add_objload_to_connection_graph(n, delayed_worklist);1166// fallthrough1167case Op_ShenandoahWeakCompareAndSwapP:1168case Op_ShenandoahWeakCompareAndSwapN:1169case Op_ShenandoahCompareAndSwapP:1170case Op_ShenandoahCompareAndSwapN:1171conn_graph->add_to_congraph_unsafe_access(n, opcode, delayed_worklist);1172return true;1173case Op_StoreP: {1174Node* adr = n->in(MemNode::Address);1175const Type* adr_type = gvn->type(adr);1176// Pointer stores in Shenandoah barriers looks like unsafe access.1177// Ignore such stores to be able scalar replace non-escaping1178// allocations.1179if (adr_type->isa_rawptr() && adr->is_AddP()) {1180Node* base = conn_graph->get_addp_base(adr);1181if (base->Opcode() == Op_LoadP &&1182base->in(MemNode::Address)->is_AddP()) {1183adr = base->in(MemNode::Address);1184Node* tls = conn_graph->get_addp_base(adr);1185if (tls->Opcode() == Op_ThreadLocal) {1186int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);1187const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset());1188if (offs == buf_offset) {1189return true; // Pre barrier previous oop value store.1190}1191}1192}1193}1194return false;1195}1196case Op_ShenandoahIUBarrier:1197conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist);1198break;1199case Op_ShenandoahLoadReferenceBarrier:1200conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), delayed_worklist);1201return true;1202default:1203// Nothing1204break;1205}1206return false;1207}12081209bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const {1210switch (opcode) {1211case Op_ShenandoahCompareAndExchangeP:1212case Op_ShenandoahCompareAndExchangeN: {1213Node *adr = n->in(MemNode::Address);1214conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);1215// fallthrough1216}1217case Op_ShenandoahCompareAndSwapP:1218case Op_ShenandoahCompareAndSwapN:1219case Op_ShenandoahWeakCompareAndSwapP:1220case Op_ShenandoahWeakCompareAndSwapN:1221return conn_graph->add_final_edges_unsafe_access(n, opcode);1222case Op_ShenandoahIUBarrier:1223conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL);1224return true;1225case Op_ShenandoahLoadReferenceBarrier:1226conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), NULL);1227return true;1228default:1229// Nothing1230break;1231}1232return false;1233}12341235bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const {1236return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) ||1237n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN);12381239}12401241bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const {1242switch (opcode) {1243case Op_ShenandoahCompareAndExchangeP:1244case Op_ShenandoahCompareAndExchangeN:1245case Op_ShenandoahWeakCompareAndSwapP:1246case Op_ShenandoahWeakCompareAndSwapN:1247case Op_ShenandoahCompareAndSwapP:1248case Op_ShenandoahCompareAndSwapN: { // Convert trinary to binary-tree1249Node* newval = n->in(MemNode::ValueIn);1250Node* oldval = n->in(LoadStoreConditionalNode::ExpectedIn);1251Node* pair = new BinaryNode(oldval, newval);1252n->set_req(MemNode::ValueIn,pair);1253n->del_req(LoadStoreConditionalNode::ExpectedIn);1254return true;1255}1256default:1257break;1258}1259return false;1260}12611262bool ShenandoahBarrierSetC2::matcher_is_store_load_barrier(Node* x, uint xop) const {1263return xop == Op_ShenandoahCompareAndExchangeP ||1264xop == Op_ShenandoahCompareAndExchangeN ||1265xop == Op_ShenandoahWeakCompareAndSwapP ||1266xop == Op_ShenandoahWeakCompareAndSwapN ||1267xop == Op_ShenandoahCompareAndSwapN ||1268xop == Op_ShenandoahCompareAndSwapP;1269}127012711272