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