1 /* 2 * Copyright (c) 2018, 2023, Red Hat, Inc. 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 "precompiled.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "gc/shared/barrierSet.hpp" 28 #include "gc/shenandoah/shenandoahBarrierSet.hpp" 29 #include "gc/shenandoah/shenandoahForwarding.hpp" 30 #include "gc/shenandoah/shenandoahHeap.hpp" 31 #include "gc/shenandoah/shenandoahRuntime.hpp" 32 #include "gc/shenandoah/shenandoahThreadLocalData.hpp" 33 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp" 34 #include "gc/shenandoah/c2/shenandoahSupport.hpp" 35 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp" 36 #include "opto/arraycopynode.hpp" 37 #include "opto/escape.hpp" 38 #include "opto/graphKit.hpp" 39 #include "opto/idealKit.hpp" 40 #include "opto/macro.hpp" 41 #include "opto/movenode.hpp" 42 #include "opto/narrowptrnode.hpp" 43 #include "opto/rootnode.hpp" 44 #include "opto/runtime.hpp" 45 46 ShenandoahBarrierSetC2* ShenandoahBarrierSetC2::bsc2() { 47 return reinterpret_cast<ShenandoahBarrierSetC2*>(BarrierSet::barrier_set()->barrier_set_c2()); 48 } 49 50 ShenandoahBarrierSetC2State::ShenandoahBarrierSetC2State(Arena* comp_arena) 51 : _load_reference_barriers(new (comp_arena) GrowableArray<ShenandoahLoadReferenceBarrierNode*>(comp_arena, 8, 0, nullptr)) { 52 } 53 54 int ShenandoahBarrierSetC2State::load_reference_barriers_count() const { 55 return _load_reference_barriers->length(); 56 } 57 58 ShenandoahLoadReferenceBarrierNode* ShenandoahBarrierSetC2State::load_reference_barrier(int idx) const { 59 return _load_reference_barriers->at(idx); 60 } 61 62 void ShenandoahBarrierSetC2State::add_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) { 63 assert(!_load_reference_barriers->contains(n), "duplicate entry in barrier list"); 64 _load_reference_barriers->append(n); 65 } 66 67 void ShenandoahBarrierSetC2State::remove_load_reference_barrier(ShenandoahLoadReferenceBarrierNode * n) { 68 if (_load_reference_barriers->contains(n)) { 69 _load_reference_barriers->remove(n); 70 } 71 } 72 73 #define __ kit-> 74 75 bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseValues* phase, Node* adr, 76 BasicType bt, uint adr_idx) const { 77 intptr_t offset = 0; 78 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 79 AllocateNode* alloc = AllocateNode::Ideal_allocation(base); 80 81 if (offset == Type::OffsetBot) { 82 return false; // cannot unalias unless there are precise offsets 83 } 84 85 if (alloc == nullptr) { 86 return false; // No allocation found 87 } 88 89 intptr_t size_in_bytes = type2aelembytes(bt); 90 91 Node* mem = __ memory(adr_idx); // start searching here... 92 93 for (int cnt = 0; cnt < 50; cnt++) { 94 95 if (mem->is_Store()) { 96 97 Node* st_adr = mem->in(MemNode::Address); 98 intptr_t st_offset = 0; 99 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset); 100 101 if (st_base == nullptr) { 102 break; // inscrutable pointer 103 } 104 105 // Break we have found a store with same base and offset as ours so break 106 if (st_base == base && st_offset == offset) { 107 break; 108 } 109 110 if (st_offset != offset && st_offset != Type::OffsetBot) { 111 const int MAX_STORE = BytesPerLong; 112 if (st_offset >= offset + size_in_bytes || 113 st_offset <= offset - MAX_STORE || 114 st_offset <= offset - mem->as_Store()->memory_size()) { 115 // Success: The offsets are provably independent. 116 // (You may ask, why not just test st_offset != offset and be done? 117 // The answer is that stores of different sizes can co-exist 118 // in the same sequence of RawMem effects. We sometimes initialize 119 // a whole 'tile' of array elements with a single jint or jlong.) 120 mem = mem->in(MemNode::Memory); 121 continue; // advance through independent store memory 122 } 123 } 124 125 if (st_base != base 126 && MemNode::detect_ptr_independence(base, alloc, st_base, 127 AllocateNode::Ideal_allocation(st_base), 128 phase)) { 129 // Success: The bases are provably independent. 130 mem = mem->in(MemNode::Memory); 131 continue; // advance through independent store memory 132 } 133 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) { 134 135 InitializeNode* st_init = mem->in(0)->as_Initialize(); 136 AllocateNode* st_alloc = st_init->allocation(); 137 138 // Make sure that we are looking at the same allocation site. 139 // The alloc variable is guaranteed to not be null here from earlier check. 140 if (alloc == st_alloc) { 141 // Check that the initialization is storing null so that no previous store 142 // has been moved up and directly write a reference 143 Node* captured_store = st_init->find_captured_store(offset, 144 type2aelembytes(T_OBJECT), 145 phase); 146 if (captured_store == nullptr || captured_store == st_init->zero_memory()) { 147 return true; 148 } 149 } 150 } 151 152 // Unless there is an explicit 'continue', we must bail out here, 153 // because 'mem' is an inscrutable memory state (e.g., a call). 154 break; 155 } 156 157 return false; 158 } 159 160 #undef __ 161 #define __ ideal. 162 163 void ShenandoahBarrierSetC2::satb_write_barrier_pre(GraphKit* kit, 164 bool do_load, 165 Node* obj, 166 Node* adr, 167 uint alias_idx, 168 Node* val, 169 const TypeOopPtr* val_type, 170 Node* pre_val, 171 BasicType bt) const { 172 // Some sanity checks 173 // Note: val is unused in this routine. 174 175 if (do_load) { 176 // We need to generate the load of the previous value 177 assert(adr != nullptr, "where are loading from?"); 178 assert(pre_val == nullptr, "loaded already?"); 179 assert(val_type != nullptr, "need a type"); 180 181 if (ReduceInitialCardMarks 182 && satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, bt, alias_idx)) { 183 return; 184 } 185 186 } else { 187 // In this case both val_type and alias_idx are unused. 188 assert(pre_val != nullptr, "must be loaded already"); 189 // Nothing to be done if pre_val is null. 190 if (pre_val->bottom_type() == TypePtr::NULL_PTR) return; 191 assert(pre_val->bottom_type()->basic_type() == T_OBJECT, "or we shouldn't be here"); 192 } 193 assert(bt == T_OBJECT, "or we shouldn't be here"); 194 195 IdealKit ideal(kit, true); 196 197 Node* tls = __ thread(); // ThreadLocalStorage 198 199 Node* no_base = __ top(); 200 Node* zero = __ ConI(0); 201 Node* zeroX = __ ConX(0); 202 203 float likely = PROB_LIKELY(0.999); 204 float unlikely = PROB_UNLIKELY(0.999); 205 206 // Offsets into the thread 207 const int index_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()); 208 const int buffer_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 209 210 // Now the actual pointers into the thread 211 Node* buffer_adr = __ AddP(no_base, tls, __ ConX(buffer_offset)); 212 Node* index_adr = __ AddP(no_base, tls, __ ConX(index_offset)); 213 214 // Now some of the values 215 Node* marking; 216 Node* gc_state = __ AddP(no_base, tls, __ ConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()))); 217 Node* ld = __ load(__ ctrl(), gc_state, TypeInt::BYTE, T_BYTE, Compile::AliasIdxRaw); 218 marking = __ AndI(ld, __ ConI(ShenandoahHeap::MARKING)); 219 assert(ShenandoahBarrierC2Support::is_gc_state_load(ld), "Should match the shape"); 220 221 // if (!marking) 222 __ if_then(marking, BoolTest::ne, zero, unlikely); { 223 BasicType index_bt = TypeX_X->basic_type(); 224 assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading Shenandoah SATBMarkQueue::_index with wrong size."); 225 Node* index = __ load(__ ctrl(), index_adr, TypeX_X, index_bt, Compile::AliasIdxRaw); 226 227 if (do_load) { 228 // load original value 229 // alias_idx correct?? 230 pre_val = __ load(__ ctrl(), adr, val_type, bt, alias_idx); 231 } 232 233 // if (pre_val != nullptr) 234 __ if_then(pre_val, BoolTest::ne, kit->null()); { 235 Node* buffer = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw); 236 237 // is the queue for this thread full? 238 __ if_then(index, BoolTest::ne, zeroX, likely); { 239 240 // decrement the index 241 Node* next_index = kit->gvn().transform(new SubXNode(index, __ ConX(sizeof(intptr_t)))); 242 243 // Now get the buffer location we will log the previous value into and store it 244 Node *log_addr = __ AddP(no_base, buffer, next_index); 245 __ store(__ ctrl(), log_addr, pre_val, T_OBJECT, Compile::AliasIdxRaw, MemNode::unordered); 246 // update the index 247 __ store(__ ctrl(), index_adr, next_index, index_bt, Compile::AliasIdxRaw, MemNode::unordered); 248 249 } __ else_(); { 250 251 // logging buffer is full, call the runtime 252 const TypeFunc *tf = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type(); 253 __ make_leaf_call(tf, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", pre_val, tls); 254 } __ end_if(); // (!index) 255 } __ end_if(); // (pre_val != nullptr) 256 } __ end_if(); // (!marking) 257 258 // Final sync IdealKit and GraphKit. 259 kit->final_sync(ideal); 260 261 if (ShenandoahSATBBarrier && adr != nullptr) { 262 Node* c = kit->control(); 263 Node* call = c->in(1)->in(1)->in(1)->in(0); 264 assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected"); 265 call->add_req(adr); 266 } 267 } 268 269 bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) { 270 return call->is_CallLeaf() && 271 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry); 272 } 273 274 bool ShenandoahBarrierSetC2::is_shenandoah_lrb_call(Node* call) { 275 if (!call->is_CallLeaf()) { 276 return false; 277 } 278 279 address entry_point = call->as_CallLeaf()->entry_point(); 280 return (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong)) || 281 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow)) || 282 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak)) || 283 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow)) || 284 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom)); 285 } 286 287 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseValues* phase, Node* n) { 288 if (n->Opcode() != Op_If) { 289 return false; 290 } 291 292 Node* bol = n->in(1); 293 assert(bol->is_Bool(), ""); 294 Node* cmpx = bol->in(1); 295 if (bol->as_Bool()->_test._test == BoolTest::ne && 296 cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) && 297 is_shenandoah_state_load(cmpx->in(1)->in(1)) && 298 cmpx->in(1)->in(2)->is_Con() && 299 cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) { 300 return true; 301 } 302 303 return false; 304 } 305 306 bool ShenandoahBarrierSetC2::is_shenandoah_state_load(Node* n) { 307 if (!n->is_Load()) return false; 308 const int state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset()); 309 return n->in(2)->is_AddP() && n->in(2)->in(2)->Opcode() == Op_ThreadLocal 310 && n->in(2)->in(3)->is_Con() 311 && n->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == state_offset; 312 } 313 314 void ShenandoahBarrierSetC2::shenandoah_write_barrier_pre(GraphKit* kit, 315 bool do_load, 316 Node* obj, 317 Node* adr, 318 uint alias_idx, 319 Node* val, 320 const TypeOopPtr* val_type, 321 Node* pre_val, 322 BasicType bt) const { 323 if (ShenandoahSATBBarrier) { 324 IdealKit ideal(kit); 325 kit->sync_kit(ideal); 326 327 satb_write_barrier_pre(kit, do_load, obj, adr, alias_idx, val, val_type, pre_val, bt); 328 329 ideal.sync_kit(kit); 330 kit->final_sync(ideal); 331 } 332 } 333 334 // Helper that guards and inserts a pre-barrier. 335 void ShenandoahBarrierSetC2::insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset, 336 Node* pre_val, bool need_mem_bar) const { 337 // We could be accessing the referent field of a reference object. If so, when Shenandoah 338 // is enabled, we need to log the value in the referent field in an SATB buffer. 339 // This routine performs some compile time filters and generates suitable 340 // runtime filters that guard the pre-barrier code. 341 // Also add memory barrier for non volatile load from the referent field 342 // to prevent commoning of loads across safepoint. 343 344 // Some compile time checks. 345 346 // If offset is a constant, is it java_lang_ref_Reference::_reference_offset? 347 const TypeX* otype = offset->find_intptr_t_type(); 348 if (otype != nullptr && otype->is_con() && 349 otype->get_con() != java_lang_ref_Reference::referent_offset()) { 350 // Constant offset but not the reference_offset so just return 351 return; 352 } 353 354 // We only need to generate the runtime guards for instances. 355 const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr(); 356 if (btype != nullptr) { 357 if (btype->isa_aryptr()) { 358 // Array type so nothing to do 359 return; 360 } 361 362 const TypeInstPtr* itype = btype->isa_instptr(); 363 if (itype != nullptr) { 364 // Can the klass of base_oop be statically determined to be 365 // _not_ a sub-class of Reference and _not_ Object? 366 ciKlass* klass = itype->instance_klass(); 367 if (klass->is_loaded() && 368 !klass->is_subtype_of(kit->env()->Reference_klass()) && 369 !kit->env()->Object_klass()->is_subtype_of(klass)) { 370 return; 371 } 372 } 373 } 374 375 // The compile time filters did not reject base_oop/offset so 376 // we need to generate the following runtime filters 377 // 378 // if (offset == java_lang_ref_Reference::_reference_offset) { 379 // if (instance_of(base, java.lang.ref.Reference)) { 380 // pre_barrier(_, pre_val, ...); 381 // } 382 // } 383 384 float likely = PROB_LIKELY( 0.999); 385 float unlikely = PROB_UNLIKELY(0.999); 386 387 IdealKit ideal(kit); 388 389 Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset()); 390 391 __ if_then(offset, BoolTest::eq, referent_off, unlikely); { 392 // Update graphKit memory and control from IdealKit. 393 kit->sync_kit(ideal); 394 395 Node* ref_klass_con = kit->makecon(TypeKlassPtr::make(kit->env()->Reference_klass())); 396 Node* is_instof = kit->gen_instanceof(base_oop, ref_klass_con); 397 398 // Update IdealKit memory and control from graphKit. 399 __ sync_kit(kit); 400 401 Node* one = __ ConI(1); 402 // is_instof == 0 if base_oop == nullptr 403 __ if_then(is_instof, BoolTest::eq, one, unlikely); { 404 405 // Update graphKit from IdeakKit. 406 kit->sync_kit(ideal); 407 408 // Use the pre-barrier to record the value in the referent field 409 satb_write_barrier_pre(kit, false /* do_load */, 410 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */, 411 pre_val /* pre_val */, 412 T_OBJECT); 413 if (need_mem_bar) { 414 // Add memory barrier to prevent commoning reads from this field 415 // across safepoint since GC can change its value. 416 kit->insert_mem_bar(Op_MemBarCPUOrder); 417 } 418 // Update IdealKit from graphKit. 419 __ sync_kit(kit); 420 421 } __ end_if(); // _ref_type != ref_none 422 } __ end_if(); // offset == referent_offset 423 424 // Final sync IdealKit and GraphKit. 425 kit->final_sync(ideal); 426 } 427 428 #undef __ 429 430 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() { 431 const Type **fields = TypeTuple::fields(2); 432 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value 433 fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread 434 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields); 435 436 // create result type (range) 437 fields = TypeTuple::fields(0); 438 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 439 440 return TypeFunc::make(domain, range); 441 } 442 443 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() { 444 const Type **fields = TypeTuple::fields(1); 445 fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop 446 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); 447 448 // create result type (range) 449 fields = TypeTuple::fields(0); 450 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 451 452 return TypeFunc::make(domain, range); 453 } 454 455 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_load_reference_barrier_Type() { 456 const Type **fields = TypeTuple::fields(2); 457 fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; // original field value 458 fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // original load address 459 460 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields); 461 462 // create result type (range) 463 fields = TypeTuple::fields(1); 464 fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; 465 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields); 466 467 return TypeFunc::make(domain, range); 468 } 469 470 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const { 471 DecoratorSet decorators = access.decorators(); 472 473 const TypePtr* adr_type = access.addr().type(); 474 Node* adr = access.addr().node(); 475 476 if (!access.is_oop()) { 477 return BarrierSetC2::store_at_resolved(access, val); 478 } 479 480 if (access.is_parse_access()) { 481 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 482 GraphKit* kit = parse_access.kit(); 483 484 uint adr_idx = kit->C->get_alias_index(adr_type); 485 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" ); 486 shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(), 487 static_cast<const TypeOopPtr*>(val.type()), nullptr /* pre_val */, access.type()); 488 } 489 return BarrierSetC2::store_at_resolved(access, val); 490 } 491 492 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const { 493 // 1: non-reference load, no additional barrier is needed 494 if (!access.is_oop()) { 495 return BarrierSetC2::load_at_resolved(access, val_type); 496 } 497 498 Node* load = BarrierSetC2::load_at_resolved(access, val_type); 499 DecoratorSet decorators = access.decorators(); 500 BasicType type = access.type(); 501 502 // 2: apply LRB if needed 503 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) { 504 load = new ShenandoahLoadReferenceBarrierNode(nullptr, load, decorators); 505 if (access.is_parse_access()) { 506 load = static_cast<C2ParseAccess &>(access).kit()->gvn().transform(load); 507 } else { 508 load = static_cast<C2OptAccess &>(access).gvn().transform(load); 509 } 510 } 511 512 // 3: apply keep-alive barrier for java.lang.ref.Reference if needed 513 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) { 514 Node* top = Compile::current()->top(); 515 Node* adr = access.addr().node(); 516 Node* offset = adr->is_AddP() ? adr->in(AddPNode::Offset) : top; 517 Node* obj = access.base(); 518 519 bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0; 520 bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0; 521 bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0; 522 523 // If we are reading the value of the referent field of a Reference 524 // object (either by using Unsafe directly or through reflection) 525 // then, if SATB is enabled, we need to record the referent in an 526 // SATB log buffer using the pre-barrier mechanism. 527 // Also we need to add memory barrier to prevent commoning reads 528 // from this field across safepoint since GC can change its value. 529 if (!on_weak_ref || (unknown && (offset == top || obj == top)) || !keep_alive) { 530 return load; 531 } 532 533 assert(access.is_parse_access(), "entry not supported at optimization time"); 534 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 535 GraphKit* kit = parse_access.kit(); 536 bool mismatched = (decorators & C2_MISMATCHED) != 0; 537 bool is_unordered = (decorators & MO_UNORDERED) != 0; 538 bool in_native = (decorators & IN_NATIVE) != 0; 539 bool need_cpu_mem_bar = !is_unordered || mismatched || in_native; 540 541 if (on_weak_ref) { 542 // Use the pre-barrier to record the value in the referent field 543 satb_write_barrier_pre(kit, false /* do_load */, 544 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */, 545 load /* pre_val */, T_OBJECT); 546 // Add memory barrier to prevent commoning reads from this field 547 // across safepoint since GC can change its value. 548 kit->insert_mem_bar(Op_MemBarCPUOrder); 549 } else if (unknown) { 550 // We do not require a mem bar inside pre_barrier if need_mem_bar 551 // is set: the barriers would be emitted by us. 552 insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar); 553 } 554 } 555 556 return load; 557 } 558 559 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 560 Node* new_val, const Type* value_type) const { 561 GraphKit* kit = access.kit(); 562 if (access.is_oop()) { 563 shenandoah_write_barrier_pre(kit, false /* do_load */, 564 nullptr, nullptr, max_juint, nullptr, nullptr, 565 expected_val /* pre_val */, T_OBJECT); 566 567 MemNode::MemOrd mo = access.mem_node_mo(); 568 Node* mem = access.memory(); 569 Node* adr = access.addr().node(); 570 const TypePtr* adr_type = access.addr().type(); 571 Node* load_store = nullptr; 572 573 #ifdef _LP64 574 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 575 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 576 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 577 if (ShenandoahCASBarrier) { 578 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo)); 579 } else { 580 load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo)); 581 } 582 } else 583 #endif 584 { 585 if (ShenandoahCASBarrier) { 586 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo)); 587 } else { 588 load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo)); 589 } 590 } 591 592 access.set_raw_access(load_store); 593 pin_atomic_op(access); 594 595 #ifdef _LP64 596 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 597 load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type())); 598 } 599 #endif 600 load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, load_store, access.decorators())); 601 return load_store; 602 } 603 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type); 604 } 605 606 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 607 Node* new_val, const Type* value_type) const { 608 GraphKit* kit = access.kit(); 609 if (access.is_oop()) { 610 shenandoah_write_barrier_pre(kit, false /* do_load */, 611 nullptr, nullptr, max_juint, nullptr, nullptr, 612 expected_val /* pre_val */, T_OBJECT); 613 DecoratorSet decorators = access.decorators(); 614 MemNode::MemOrd mo = access.mem_node_mo(); 615 Node* mem = access.memory(); 616 bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0; 617 Node* load_store = nullptr; 618 Node* adr = access.addr().node(); 619 #ifdef _LP64 620 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 621 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 622 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 623 if (ShenandoahCASBarrier) { 624 if (is_weak_cas) { 625 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 626 } else { 627 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 628 } 629 } else { 630 if (is_weak_cas) { 631 load_store = kit->gvn().transform(new WeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 632 } else { 633 load_store = kit->gvn().transform(new CompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 634 } 635 } 636 } else 637 #endif 638 { 639 if (ShenandoahCASBarrier) { 640 if (is_weak_cas) { 641 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 642 } else { 643 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 644 } 645 } else { 646 if (is_weak_cas) { 647 load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 648 } else { 649 load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 650 } 651 } 652 } 653 access.set_raw_access(load_store); 654 pin_atomic_op(access); 655 return load_store; 656 } 657 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type); 658 } 659 660 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const { 661 GraphKit* kit = access.kit(); 662 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type); 663 if (access.is_oop()) { 664 result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, result, access.decorators())); 665 shenandoah_write_barrier_pre(kit, false /* do_load */, 666 nullptr, nullptr, max_juint, nullptr, nullptr, 667 result /* pre_val */, T_OBJECT); 668 } 669 return result; 670 } 671 672 673 bool ShenandoahBarrierSetC2::is_gc_pre_barrier_node(Node* node) const { 674 return is_shenandoah_wb_pre_call(node); 675 } 676 677 // Support for GC barriers emitted during parsing 678 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const { 679 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) return true; 680 if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) { 681 return false; 682 } 683 CallLeafNode *call = node->as_CallLeaf(); 684 if (call->_name == nullptr) { 685 return false; 686 } 687 688 return strcmp(call->_name, "shenandoah_clone_barrier") == 0 || 689 strcmp(call->_name, "shenandoah_cas_obj") == 0 || 690 strcmp(call->_name, "shenandoah_wb_pre") == 0; 691 } 692 693 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const { 694 if (c == nullptr) { 695 return c; 696 } 697 if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 698 return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 699 } 700 return c; 701 } 702 703 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const { 704 return !ShenandoahBarrierC2Support::expand(C, igvn); 705 } 706 707 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const { 708 if (mode == LoopOptsShenandoahExpand) { 709 assert(UseShenandoahGC, "only for shenandoah"); 710 ShenandoahBarrierC2Support::pin_and_expand(phase); 711 return true; 712 } else if (mode == LoopOptsShenandoahPostExpand) { 713 assert(UseShenandoahGC, "only for shenandoah"); 714 visited.clear(); 715 ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase); 716 return true; 717 } 718 return false; 719 } 720 721 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const { 722 bool is_oop = is_reference_type(type); 723 if (!is_oop) { 724 return false; 725 } 726 if (ShenandoahSATBBarrier && tightly_coupled_alloc) { 727 if (phase == Optimization) { 728 return false; 729 } 730 return !is_clone; 731 } 732 return true; 733 } 734 735 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) { 736 const TypeOopPtr* src_type = gvn.type(src)->is_oopptr(); 737 if (src_type->isa_instptr() != nullptr) { 738 ciInstanceKlass* ik = src_type->is_instptr()->instance_klass(); 739 if ((src_type->klass_is_exact() || !ik->has_subklass()) && !ik->has_injected_fields()) { 740 if (ik->has_object_fields()) { 741 return true; 742 } else { 743 if (!src_type->klass_is_exact()) { 744 Compile::current()->dependencies()->assert_leaf_type(ik); 745 } 746 } 747 } else { 748 return true; 749 } 750 } else if (src_type->isa_aryptr()) { 751 BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type(); 752 if (is_reference_type(src_elem, true)) { 753 return true; 754 } 755 } else { 756 return true; 757 } 758 return false; 759 } 760 761 void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const { 762 Node* ctrl = ac->in(TypeFunc::Control); 763 Node* mem = ac->in(TypeFunc::Memory); 764 Node* src_base = ac->in(ArrayCopyNode::Src); 765 Node* src_offset = ac->in(ArrayCopyNode::SrcPos); 766 Node* dest_base = ac->in(ArrayCopyNode::Dest); 767 Node* dest_offset = ac->in(ArrayCopyNode::DestPos); 768 Node* length = ac->in(ArrayCopyNode::Length); 769 770 Node* src = phase->basic_plus_adr(src_base, src_offset); 771 Node* dest = phase->basic_plus_adr(dest_base, dest_offset); 772 773 if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) { 774 // Check if heap is has forwarded objects. If it does, we need to call into the special 775 // routine that would fix up source references before we can continue. 776 777 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT }; 778 Node* region = new RegionNode(PATH_LIMIT); 779 Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM); 780 781 Node* thread = phase->transform_later(new ThreadLocalNode()); 782 Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 783 Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset)); 784 785 uint gc_state_idx = Compile::AliasIdxRaw; 786 const TypePtr* gc_state_adr_type = nullptr; // debug-mode-only argument 787 debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx)); 788 789 Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered)); 790 Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(ShenandoahHeap::HAS_FORWARDED))); 791 Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT))); 792 Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne)); 793 794 IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If(); 795 Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff)); 796 Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff)); 797 798 // Heap is stable, no need to do anything additional 799 region->init_req(_heap_stable, stable_ctrl); 800 mem_phi->init_req(_heap_stable, mem); 801 802 // Heap is unstable, call into clone barrier stub 803 Node* call = phase->make_leaf_call(unstable_ctrl, mem, 804 ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(), 805 CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier), 806 "shenandoah_clone", 807 TypeRawPtr::BOTTOM, 808 src_base); 809 call = phase->transform_later(call); 810 811 ctrl = phase->transform_later(new ProjNode(call, TypeFunc::Control)); 812 mem = phase->transform_later(new ProjNode(call, TypeFunc::Memory)); 813 region->init_req(_heap_unstable, ctrl); 814 mem_phi->init_req(_heap_unstable, mem); 815 816 // Wire up the actual arraycopy stub now 817 ctrl = phase->transform_later(region); 818 mem = phase->transform_later(mem_phi); 819 820 const char* name = "arraycopy"; 821 call = phase->make_leaf_call(ctrl, mem, 822 OptoRuntime::fast_arraycopy_Type(), 823 phase->basictype2arraycopy(T_LONG, nullptr, nullptr, true, name, true), 824 name, TypeRawPtr::BOTTOM, 825 src, dest, length 826 LP64_ONLY(COMMA phase->top())); 827 call = phase->transform_later(call); 828 829 // Hook up the whole thing into the graph 830 phase->igvn().replace_node(ac, call); 831 } else { 832 BarrierSetC2::clone_at_expansion(phase, ac); 833 } 834 } 835 836 837 // Support for macro expanded GC barriers 838 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const { 839 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 840 state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node); 841 } 842 } 843 844 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const { 845 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 846 state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node); 847 } 848 } 849 850 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* n) const { 851 if (is_shenandoah_wb_pre_call(n)) { 852 shenandoah_eliminate_wb_pre(n, ¯o->igvn()); 853 } 854 } 855 856 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const { 857 assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), ""); 858 Node* c = call->as_Call()->proj_out(TypeFunc::Control); 859 c = c->unique_ctrl_out(); 860 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 861 c = c->unique_ctrl_out(); 862 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 863 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 864 assert(iff->is_If(), "expect test"); 865 if (!is_shenandoah_marking_if(igvn, iff)) { 866 c = c->unique_ctrl_out(); 867 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 868 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 869 assert(is_shenandoah_marking_if(igvn, iff), "expect marking test"); 870 } 871 Node* cmpx = iff->in(1)->in(1); 872 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ)); 873 igvn->rehash_node_delayed(call); 874 call->del_req(call->req()-1); 875 } 876 877 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const { 878 if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) { 879 igvn->add_users_to_worklist(node); 880 } 881 } 882 883 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const { 884 for (uint i = 0; i < useful.size(); i++) { 885 Node* n = useful.at(i); 886 if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) { 887 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 888 C->record_for_igvn(n->fast_out(i)); 889 } 890 } 891 } 892 893 for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) { 894 ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i); 895 if (!useful.member(n)) { 896 state()->remove_load_reference_barrier(n); 897 } 898 } 899 } 900 901 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const { 902 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena); 903 } 904 905 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const { 906 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state()); 907 } 908 909 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be 910 // expanded later, then now is the time to do so. 911 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; } 912 913 #ifdef ASSERT 914 void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const { 915 if (ShenandoahVerifyOptoBarriers && phase == BarrierSetC2::BeforeMacroExpand) { 916 ShenandoahBarrierC2Support::verify(Compile::current()->root()); 917 } else if (phase == BarrierSetC2::BeforeCodeGen) { 918 // Verify Shenandoah pre-barriers 919 const int gc_state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset()); 920 921 Unique_Node_List visited; 922 Node_List worklist; 923 // We're going to walk control flow backwards starting from the Root 924 worklist.push(compile->root()); 925 while (worklist.size() > 0) { 926 Node *x = worklist.pop(); 927 if (x == nullptr || x == compile->top()) { 928 continue; 929 } 930 931 if (visited.member(x)) { 932 continue; 933 } else { 934 visited.push(x); 935 } 936 937 if (x->is_Region()) { 938 for (uint i = 1; i < x->req(); i++) { 939 worklist.push(x->in(i)); 940 } 941 } else { 942 worklist.push(x->in(0)); 943 // We are looking for the pattern: 944 // /->ThreadLocal 945 // If->Bool->CmpI->LoadB->AddP->ConL(marking_offset) 946 // \->ConI(0) 947 // We want to verify that the If and the LoadB have the same control 948 // See GraphKit::g1_write_barrier_pre() 949 if (x->is_If()) { 950 IfNode *iff = x->as_If(); 951 if (iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp()) { 952 CmpNode *cmp = iff->in(1)->in(1)->as_Cmp(); 953 if (cmp->Opcode() == Op_CmpI && cmp->in(2)->is_Con() && cmp->in(2)->bottom_type()->is_int()->get_con() == 0 954 && cmp->in(1)->is_Load()) { 955 LoadNode *load = cmp->in(1)->as_Load(); 956 if (load->Opcode() == Op_LoadB && load->in(2)->is_AddP() && load->in(2)->in(2)->Opcode() == Op_ThreadLocal 957 && load->in(2)->in(3)->is_Con() 958 && load->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == gc_state_offset) { 959 960 Node *if_ctrl = iff->in(0); 961 Node *load_ctrl = load->in(0); 962 963 if (if_ctrl != load_ctrl) { 964 // Skip possible CProj->NeverBranch in infinite loops 965 if ((if_ctrl->is_Proj() && if_ctrl->Opcode() == Op_CProj) 966 && if_ctrl->in(0)->is_NeverBranch()) { 967 if_ctrl = if_ctrl->in(0)->in(0); 968 } 969 } 970 assert(load_ctrl != nullptr && if_ctrl == load_ctrl, "controls must match"); 971 } 972 } 973 } 974 } 975 } 976 } 977 } 978 } 979 #endif 980 981 Node* ShenandoahBarrierSetC2::ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const { 982 if (is_shenandoah_wb_pre_call(n)) { 983 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 984 if (n->req() > cnt) { 985 Node* addp = n->in(cnt); 986 if (has_only_shenandoah_wb_pre_uses(addp)) { 987 n->del_req(cnt); 988 if (can_reshape) { 989 phase->is_IterGVN()->_worklist.push(addp); 990 } 991 return n; 992 } 993 } 994 } 995 if (n->Opcode() == Op_CmpP) { 996 Node* in1 = n->in(1); 997 Node* in2 = n->in(2); 998 999 // If one input is null, then step over the strong LRB barriers on the other input 1000 if (in1->bottom_type() == TypePtr::NULL_PTR && 1001 !((in2->Opcode() == Op_ShenandoahLoadReferenceBarrier) && 1002 !ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in2)->decorators()))) { 1003 in2 = step_over_gc_barrier(in2); 1004 } 1005 if (in2->bottom_type() == TypePtr::NULL_PTR && 1006 !((in1->Opcode() == Op_ShenandoahLoadReferenceBarrier) && 1007 !ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in1)->decorators()))) { 1008 in1 = step_over_gc_barrier(in1); 1009 } 1010 1011 if (in1 != n->in(1)) { 1012 n->set_req_X(1, in1, phase); 1013 assert(in2 == n->in(2), "only one change"); 1014 return n; 1015 } 1016 if (in2 != n->in(2)) { 1017 n->set_req_X(2, in2, phase); 1018 return n; 1019 } 1020 } else if (can_reshape && 1021 n->Opcode() == Op_If && 1022 ShenandoahBarrierC2Support::is_heap_stable_test(n) && 1023 n->in(0) != nullptr && 1024 n->outcnt() == 2) { 1025 Node* dom = n->in(0); 1026 Node* prev_dom = n; 1027 int op = n->Opcode(); 1028 int dist = 16; 1029 // Search up the dominator tree for another heap stable test 1030 while (dom->Opcode() != op || // Not same opcode? 1031 !ShenandoahBarrierC2Support::is_heap_stable_test(dom) || // Not same input 1? 1032 prev_dom->in(0) != dom) { // One path of test does not dominate? 1033 if (dist < 0) return nullptr; 1034 1035 dist--; 1036 prev_dom = dom; 1037 dom = IfNode::up_one_dom(dom); 1038 if (!dom) return nullptr; 1039 } 1040 1041 // Check that we did not follow a loop back to ourselves 1042 if (n == dom) { 1043 return nullptr; 1044 } 1045 1046 return n->as_If()->dominated_by(prev_dom, phase->is_IterGVN(), false); 1047 } 1048 1049 return nullptr; 1050 } 1051 1052 bool ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(Node* n) { 1053 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1054 Node* u = n->fast_out(i); 1055 if (!is_shenandoah_wb_pre_call(u)) { 1056 return false; 1057 } 1058 } 1059 return n->outcnt() > 0; 1060 } 1061 1062 bool ShenandoahBarrierSetC2::final_graph_reshaping(Compile* compile, Node* n, uint opcode, Unique_Node_List& dead_nodes) const { 1063 switch (opcode) { 1064 case Op_CallLeaf: 1065 case Op_CallLeafNoFP: { 1066 assert (n->is_Call(), ""); 1067 CallNode *call = n->as_Call(); 1068 if (ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(call)) { 1069 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 1070 if (call->req() > cnt) { 1071 assert(call->req() == cnt + 1, "only one extra input"); 1072 Node *addp = call->in(cnt); 1073 assert(!ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(addp), "useless address computation?"); 1074 call->del_req(cnt); 1075 } 1076 } 1077 return false; 1078 } 1079 case Op_ShenandoahCompareAndSwapP: 1080 case Op_ShenandoahCompareAndSwapN: 1081 case Op_ShenandoahWeakCompareAndSwapN: 1082 case Op_ShenandoahWeakCompareAndSwapP: 1083 case Op_ShenandoahCompareAndExchangeP: 1084 case Op_ShenandoahCompareAndExchangeN: 1085 return true; 1086 case Op_ShenandoahLoadReferenceBarrier: 1087 assert(false, "should have been expanded already"); 1088 return true; 1089 default: 1090 return false; 1091 } 1092 } 1093 1094 bool ShenandoahBarrierSetC2::escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const { 1095 switch (opcode) { 1096 case Op_ShenandoahCompareAndExchangeP: 1097 case Op_ShenandoahCompareAndExchangeN: 1098 conn_graph->add_objload_to_connection_graph(n, delayed_worklist); 1099 // fallthrough 1100 case Op_ShenandoahWeakCompareAndSwapP: 1101 case Op_ShenandoahWeakCompareAndSwapN: 1102 case Op_ShenandoahCompareAndSwapP: 1103 case Op_ShenandoahCompareAndSwapN: 1104 conn_graph->add_to_congraph_unsafe_access(n, opcode, delayed_worklist); 1105 return true; 1106 case Op_StoreP: { 1107 Node* adr = n->in(MemNode::Address); 1108 const Type* adr_type = gvn->type(adr); 1109 // Pointer stores in Shenandoah barriers looks like unsafe access. 1110 // Ignore such stores to be able scalar replace non-escaping 1111 // allocations. 1112 if (adr_type->isa_rawptr() && adr->is_AddP()) { 1113 Node* base = conn_graph->get_addp_base(adr); 1114 if (base->Opcode() == Op_LoadP && 1115 base->in(MemNode::Address)->is_AddP()) { 1116 adr = base->in(MemNode::Address); 1117 Node* tls = conn_graph->get_addp_base(adr); 1118 if (tls->Opcode() == Op_ThreadLocal) { 1119 int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot); 1120 const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 1121 if (offs == buf_offset) { 1122 return true; // Pre barrier previous oop value store. 1123 } 1124 } 1125 } 1126 } 1127 return false; 1128 } 1129 case Op_ShenandoahLoadReferenceBarrier: 1130 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), delayed_worklist); 1131 return true; 1132 default: 1133 // Nothing 1134 break; 1135 } 1136 return false; 1137 } 1138 1139 bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const { 1140 switch (opcode) { 1141 case Op_ShenandoahCompareAndExchangeP: 1142 case Op_ShenandoahCompareAndExchangeN: { 1143 Node *adr = n->in(MemNode::Address); 1144 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, nullptr); 1145 // fallthrough 1146 } 1147 case Op_ShenandoahCompareAndSwapP: 1148 case Op_ShenandoahCompareAndSwapN: 1149 case Op_ShenandoahWeakCompareAndSwapP: 1150 case Op_ShenandoahWeakCompareAndSwapN: 1151 return conn_graph->add_final_edges_unsafe_access(n, opcode); 1152 case Op_ShenandoahLoadReferenceBarrier: 1153 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), nullptr); 1154 return true; 1155 default: 1156 // Nothing 1157 break; 1158 } 1159 return false; 1160 } 1161 1162 bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const { 1163 return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) || 1164 n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN); 1165 1166 } 1167 1168 bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const { 1169 switch (opcode) { 1170 case Op_ShenandoahCompareAndExchangeP: 1171 case Op_ShenandoahCompareAndExchangeN: 1172 case Op_ShenandoahWeakCompareAndSwapP: 1173 case Op_ShenandoahWeakCompareAndSwapN: 1174 case Op_ShenandoahCompareAndSwapP: 1175 case Op_ShenandoahCompareAndSwapN: { // Convert trinary to binary-tree 1176 Node* newval = n->in(MemNode::ValueIn); 1177 Node* oldval = n->in(LoadStoreConditionalNode::ExpectedIn); 1178 Node* pair = new BinaryNode(oldval, newval); 1179 n->set_req(MemNode::ValueIn,pair); 1180 n->del_req(LoadStoreConditionalNode::ExpectedIn); 1181 return true; 1182 } 1183 default: 1184 break; 1185 } 1186 return false; 1187 } 1188 1189 bool ShenandoahBarrierSetC2::matcher_is_store_load_barrier(Node* x, uint xop) const { 1190 return xop == Op_ShenandoahCompareAndExchangeP || 1191 xop == Op_ShenandoahCompareAndExchangeN || 1192 xop == Op_ShenandoahWeakCompareAndSwapP || 1193 xop == Op_ShenandoahWeakCompareAndSwapN || 1194 xop == Op_ShenandoahCompareAndSwapN || 1195 xop == Op_ShenandoahCompareAndSwapP; 1196 }