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); 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), 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_lrb_call(Node* call) { 277 if (!call->is_CallLeaf()) { 278 return false; 279 } 280 281 address entry_point = call->as_CallLeaf()->entry_point(); 282 return (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong)) || 283 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_strong_narrow)) || 284 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak)) || 285 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_weak_narrow)) || 286 (entry_point == CAST_FROM_FN_PTR(address, ShenandoahRuntime::load_reference_barrier_phantom)); 287 } 288 289 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseValues* phase, Node* n) { 290 if (n->Opcode() != Op_If) { 291 return false; 292 } 293 294 Node* bol = n->in(1); 295 assert(bol->is_Bool(), ""); 296 Node* cmpx = bol->in(1); 297 if (bol->as_Bool()->_test._test == BoolTest::ne && 298 cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) && 299 is_shenandoah_state_load(cmpx->in(1)->in(1)) && 300 cmpx->in(1)->in(2)->is_Con() && 301 cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) { 302 return true; 303 } 304 305 return false; 306 } 307 308 bool ShenandoahBarrierSetC2::is_shenandoah_state_load(Node* n) { 309 if (!n->is_Load()) return false; 310 const int state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset()); 311 return n->in(2)->is_AddP() && n->in(2)->in(2)->Opcode() == Op_ThreadLocal 312 && n->in(2)->in(3)->is_Con() 313 && n->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == state_offset; 314 } 315 316 void ShenandoahBarrierSetC2::shenandoah_write_barrier_pre(GraphKit* kit, 317 bool do_load, 318 Node* obj, 319 Node* adr, 320 uint alias_idx, 321 Node* val, 322 const TypeOopPtr* val_type, 323 Node* pre_val, 324 BasicType bt) const { 325 if (ShenandoahSATBBarrier) { 326 IdealKit ideal(kit); 327 kit->sync_kit(ideal); 328 329 satb_write_barrier_pre(kit, do_load, obj, adr, alias_idx, val, val_type, pre_val, bt); 330 331 ideal.sync_kit(kit); 332 kit->final_sync(ideal); 333 } 334 } 335 336 // Helper that guards and inserts a pre-barrier. 337 void ShenandoahBarrierSetC2::insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset, 338 Node* pre_val, bool need_mem_bar) const { 339 // We could be accessing the referent field of a reference object. If so, when Shenandoah 340 // is enabled, we need to log the value in the referent field in an SATB buffer. 341 // This routine performs some compile time filters and generates suitable 342 // runtime filters that guard the pre-barrier code. 343 // Also add memory barrier for non volatile load from the referent field 344 // to prevent commoning of loads across safepoint. 345 346 // Some compile time checks. 347 348 // If offset is a constant, is it java_lang_ref_Reference::_reference_offset? 349 const TypeX* otype = offset->find_intptr_t_type(); 350 if (otype != nullptr && otype->is_con() && 351 otype->get_con() != java_lang_ref_Reference::referent_offset()) { 352 // Constant offset but not the reference_offset so just return 353 return; 354 } 355 356 // We only need to generate the runtime guards for instances. 357 const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr(); 358 if (btype != nullptr) { 359 if (btype->isa_aryptr()) { 360 // Array type so nothing to do 361 return; 362 } 363 364 const TypeInstPtr* itype = btype->isa_instptr(); 365 if (itype != nullptr) { 366 // Can the klass of base_oop be statically determined to be 367 // _not_ a sub-class of Reference and _not_ Object? 368 ciKlass* klass = itype->instance_klass(); 369 if (klass->is_loaded() && 370 !klass->is_subtype_of(kit->env()->Reference_klass()) && 371 !kit->env()->Object_klass()->is_subtype_of(klass)) { 372 return; 373 } 374 } 375 } 376 377 // The compile time filters did not reject base_oop/offset so 378 // we need to generate the following runtime filters 379 // 380 // if (offset == java_lang_ref_Reference::_reference_offset) { 381 // if (instance_of(base, java.lang.ref.Reference)) { 382 // pre_barrier(_, pre_val, ...); 383 // } 384 // } 385 386 float likely = PROB_LIKELY( 0.999); 387 float unlikely = PROB_UNLIKELY(0.999); 388 389 IdealKit ideal(kit); 390 391 Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset()); 392 393 __ if_then(offset, BoolTest::eq, referent_off, unlikely); { 394 // Update graphKit memory and control from IdealKit. 395 kit->sync_kit(ideal); 396 397 Node* ref_klass_con = kit->makecon(TypeKlassPtr::make(kit->env()->Reference_klass())); 398 Node* is_instof = kit->gen_instanceof(base_oop, ref_klass_con); 399 400 // Update IdealKit memory and control from graphKit. 401 __ sync_kit(kit); 402 403 Node* one = __ ConI(1); 404 // is_instof == 0 if base_oop == nullptr 405 __ if_then(is_instof, BoolTest::eq, one, unlikely); { 406 407 // Update graphKit from IdeakKit. 408 kit->sync_kit(ideal); 409 410 // Use the pre-barrier to record the value in the referent field 411 satb_write_barrier_pre(kit, false /* do_load */, 412 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */, 413 pre_val /* pre_val */, 414 T_OBJECT); 415 if (need_mem_bar) { 416 // Add memory barrier to prevent commoning reads from this field 417 // across safepoint since GC can change its value. 418 kit->insert_mem_bar(Op_MemBarCPUOrder); 419 } 420 // Update IdealKit from graphKit. 421 __ sync_kit(kit); 422 423 } __ end_if(); // _ref_type != ref_none 424 } __ end_if(); // offset == referent_offset 425 426 // Final sync IdealKit and GraphKit. 427 kit->final_sync(ideal); 428 } 429 430 Node* ShenandoahBarrierSetC2::byte_map_base_node(GraphKit* kit) const { 431 BarrierSet* bs = BarrierSet::barrier_set(); 432 ShenandoahBarrierSet* ctbs = barrier_set_cast<ShenandoahBarrierSet>(bs); 433 CardTable::CardValue* card_table_base = ctbs->card_table()->byte_map_base(); 434 if (card_table_base != nullptr) { 435 return kit->makecon(TypeRawPtr::make((address)card_table_base)); 436 } else { 437 return kit->null(); 438 } 439 } 440 441 void ShenandoahBarrierSetC2::post_barrier(GraphKit* kit, 442 Node* ctl, 443 Node* oop_store, 444 Node* obj, 445 Node* adr, 446 uint adr_idx, 447 Node* val, 448 BasicType bt, 449 bool use_precise) const { 450 assert(ShenandoahCardBarrier, "Should have been checked by caller"); 451 452 // No store check needed if we're storing a null. 453 if (val != nullptr && val->is_Con()) { 454 // must be either an oop or NULL 455 const Type* t = val->bottom_type(); 456 if (t == TypePtr::NULL_PTR || t == Type::TOP) 457 return; 458 } 459 460 if (ReduceInitialCardMarks && obj == kit->just_allocated_object(kit->control())) { 461 // We can skip marks on a freshly-allocated object in Eden. 462 // Keep this code in sync with new_deferred_store_barrier() in runtime.cpp. 463 // That routine informs GC to take appropriate compensating steps, 464 // upon a slow-path allocation, so as to make this card-mark 465 // elision safe. 466 return; 467 } 468 469 if (!use_precise) { 470 // All card marks for a (non-array) instance are in one place: 471 adr = obj; 472 } 473 // (Else it's an array (or unknown), and we want more precise card marks.) 474 assert(adr != nullptr, ""); 475 476 IdealKit ideal(kit, true); 477 478 // Convert the pointer to an int prior to doing math on it 479 Node* cast = __ CastPX(__ ctrl(), adr); 480 481 // Divide by card size 482 Node* card_offset = __ URShiftX( cast, __ ConI(CardTable::card_shift()) ); 483 484 // Combine card table base and card offset 485 Node* card_adr = __ AddP(__ top(), byte_map_base_node(kit), card_offset ); 486 487 // Get the alias_index for raw card-mark memory 488 int adr_type = Compile::AliasIdxRaw; 489 Node* zero = __ ConI(0); // Dirty card value 490 491 if (UseCondCardMark) { 492 // The classic GC reference write barrier is typically implemented 493 // as a store into the global card mark table. Unfortunately 494 // unconditional stores can result in false sharing and excessive 495 // coherence traffic as well as false transactional aborts. 496 // UseCondCardMark enables MP "polite" conditional card mark 497 // stores. In theory we could relax the load from ctrl() to 498 // no_ctrl, but that doesn't buy much latitude. 499 Node* card_val = __ load( __ ctrl(), card_adr, TypeInt::BYTE, T_BYTE, adr_type); 500 __ if_then(card_val, BoolTest::ne, zero); 501 } 502 503 // Smash zero into card 504 __ store(__ ctrl(), card_adr, zero, T_BYTE, adr_type, MemNode::unordered); 505 506 if (UseCondCardMark) { 507 __ end_if(); 508 } 509 510 // Final sync IdealKit and GraphKit. 511 kit->final_sync(ideal); 512 } 513 514 #undef __ 515 516 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() { 517 const Type **fields = TypeTuple::fields(2); 518 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value 519 fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread 520 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields); 521 522 // create result type (range) 523 fields = TypeTuple::fields(0); 524 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 525 526 return TypeFunc::make(domain, range); 527 } 528 529 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() { 530 const Type **fields = TypeTuple::fields(1); 531 fields[TypeFunc::Parms+0] = TypeOopPtr::NOTNULL; // src oop 532 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); 533 534 // create result type (range) 535 fields = TypeTuple::fields(0); 536 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 537 538 return TypeFunc::make(domain, range); 539 } 540 541 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_load_reference_barrier_Type() { 542 const Type **fields = TypeTuple::fields(2); 543 fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; // original field value 544 fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM; // original load address 545 546 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields); 547 548 // create result type (range) 549 fields = TypeTuple::fields(1); 550 fields[TypeFunc::Parms+0] = TypeOopPtr::BOTTOM; 551 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields); 552 553 return TypeFunc::make(domain, range); 554 } 555 556 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const { 557 DecoratorSet decorators = access.decorators(); 558 559 const TypePtr* adr_type = access.addr().type(); 560 Node* adr = access.addr().node(); 561 562 if (!access.is_oop()) { 563 return BarrierSetC2::store_at_resolved(access, val); 564 } 565 566 if (access.is_parse_access()) { 567 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 568 GraphKit* kit = parse_access.kit(); 569 570 uint adr_idx = kit->C->get_alias_index(adr_type); 571 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" ); 572 shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(), 573 static_cast<const TypeOopPtr*>(val.type()), nullptr /* pre_val */, access.type()); 574 575 Node* result = BarrierSetC2::store_at_resolved(access, val); 576 577 if (ShenandoahCardBarrier) { 578 const bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0; 579 const bool is_array = (decorators & IS_ARRAY) != 0; 580 const bool use_precise = is_array || anonymous; 581 post_barrier(kit, kit->control(), access.raw_access(), access.base(), 582 adr, adr_idx, val.node(), access.type(), use_precise); 583 } 584 return result; 585 } else { 586 assert(access.is_opt_access(), "only for optimization passes"); 587 assert(((decorators & C2_TIGHTLY_COUPLED_ALLOC) != 0 || !ShenandoahSATBBarrier) && (decorators & C2_ARRAY_COPY) != 0, "unexpected caller of this code"); 588 return BarrierSetC2::store_at_resolved(access, val); 589 } 590 } 591 592 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const { 593 // 1: non-reference load, no additional barrier is needed 594 if (!access.is_oop()) { 595 return BarrierSetC2::load_at_resolved(access, val_type); 596 } 597 598 Node* load = BarrierSetC2::load_at_resolved(access, val_type); 599 DecoratorSet decorators = access.decorators(); 600 BasicType type = access.type(); 601 602 // 2: apply LRB if needed 603 if (ShenandoahBarrierSet::need_load_reference_barrier(decorators, type)) { 604 load = new ShenandoahLoadReferenceBarrierNode(nullptr, load, decorators); 605 if (access.is_parse_access()) { 606 load = static_cast<C2ParseAccess &>(access).kit()->gvn().transform(load); 607 } else { 608 load = static_cast<C2OptAccess &>(access).gvn().transform(load); 609 } 610 } 611 612 // 3: apply keep-alive barrier for java.lang.ref.Reference if needed 613 if (ShenandoahBarrierSet::need_keep_alive_barrier(decorators, type)) { 614 Node* top = Compile::current()->top(); 615 Node* adr = access.addr().node(); 616 Node* offset = adr->is_AddP() ? adr->in(AddPNode::Offset) : top; 617 Node* obj = access.base(); 618 619 bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0; 620 bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0; 621 bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0; 622 623 // If we are reading the value of the referent field of a Reference 624 // object (either by using Unsafe directly or through reflection) 625 // then, if SATB is enabled, we need to record the referent in an 626 // SATB log buffer using the pre-barrier mechanism. 627 // Also we need to add memory barrier to prevent commoning reads 628 // from this field across safepoint since GC can change its value. 629 if (!on_weak_ref || (unknown && (offset == top || obj == top)) || !keep_alive) { 630 return load; 631 } 632 633 assert(access.is_parse_access(), "entry not supported at optimization time"); 634 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 635 GraphKit* kit = parse_access.kit(); 636 bool mismatched = (decorators & C2_MISMATCHED) != 0; 637 bool is_unordered = (decorators & MO_UNORDERED) != 0; 638 bool in_native = (decorators & IN_NATIVE) != 0; 639 bool need_cpu_mem_bar = !is_unordered || mismatched || in_native; 640 641 if (on_weak_ref) { 642 // Use the pre-barrier to record the value in the referent field 643 satb_write_barrier_pre(kit, false /* do_load */, 644 nullptr /* obj */, nullptr /* adr */, max_juint /* alias_idx */, nullptr /* val */, nullptr /* val_type */, 645 load /* pre_val */, T_OBJECT); 646 // Add memory barrier to prevent commoning reads from this field 647 // across safepoint since GC can change its value. 648 kit->insert_mem_bar(Op_MemBarCPUOrder); 649 } else if (unknown) { 650 // We do not require a mem bar inside pre_barrier if need_mem_bar 651 // is set: the barriers would be emitted by us. 652 insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar); 653 } 654 } 655 656 return load; 657 } 658 659 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 660 Node* new_val, const Type* value_type) const { 661 GraphKit* kit = access.kit(); 662 if (access.is_oop()) { 663 shenandoah_write_barrier_pre(kit, false /* do_load */, 664 nullptr, nullptr, max_juint, nullptr, nullptr, 665 expected_val /* pre_val */, T_OBJECT); 666 667 MemNode::MemOrd mo = access.mem_node_mo(); 668 Node* mem = access.memory(); 669 Node* adr = access.addr().node(); 670 const TypePtr* adr_type = access.addr().type(); 671 Node* load_store = nullptr; 672 673 #ifdef _LP64 674 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 675 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 676 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 677 if (ShenandoahCASBarrier) { 678 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo)); 679 } else { 680 load_store = kit->gvn().transform(new CompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo)); 681 } 682 } else 683 #endif 684 { 685 if (ShenandoahCASBarrier) { 686 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo)); 687 } else { 688 load_store = kit->gvn().transform(new CompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo)); 689 } 690 } 691 692 access.set_raw_access(load_store); 693 pin_atomic_op(access); 694 695 #ifdef _LP64 696 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 697 load_store = kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type())); 698 } 699 #endif 700 load_store = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, load_store, access.decorators())); 701 if (ShenandoahCardBarrier) { 702 post_barrier(kit, kit->control(), access.raw_access(), access.base(), 703 access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true); 704 } 705 return load_store; 706 } 707 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type); 708 } 709 710 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 711 Node* new_val, const Type* value_type) const { 712 GraphKit* kit = access.kit(); 713 if (access.is_oop()) { 714 shenandoah_write_barrier_pre(kit, false /* do_load */, 715 nullptr, nullptr, max_juint, nullptr, nullptr, 716 expected_val /* pre_val */, T_OBJECT); 717 DecoratorSet decorators = access.decorators(); 718 MemNode::MemOrd mo = access.mem_node_mo(); 719 Node* mem = access.memory(); 720 bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0; 721 Node* load_store = nullptr; 722 Node* adr = access.addr().node(); 723 #ifdef _LP64 724 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 725 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 726 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 727 if (ShenandoahCASBarrier) { 728 if (is_weak_cas) { 729 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 730 } else { 731 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 732 } 733 } else { 734 if (is_weak_cas) { 735 load_store = kit->gvn().transform(new WeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 736 } else { 737 load_store = kit->gvn().transform(new CompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 738 } 739 } 740 } else 741 #endif 742 { 743 if (ShenandoahCASBarrier) { 744 if (is_weak_cas) { 745 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 746 } else { 747 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 748 } 749 } else { 750 if (is_weak_cas) { 751 load_store = kit->gvn().transform(new WeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 752 } else { 753 load_store = kit->gvn().transform(new CompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 754 } 755 } 756 } 757 access.set_raw_access(load_store); 758 pin_atomic_op(access); 759 if (ShenandoahCardBarrier) { 760 post_barrier(kit, kit->control(), access.raw_access(), access.base(), 761 access.addr().node(), access.alias_idx(), new_val, T_OBJECT, true); 762 } 763 return load_store; 764 } 765 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type); 766 } 767 768 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const { 769 GraphKit* kit = access.kit(); 770 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type); 771 if (access.is_oop()) { 772 result = kit->gvn().transform(new ShenandoahLoadReferenceBarrierNode(nullptr, result, access.decorators())); 773 shenandoah_write_barrier_pre(kit, false /* do_load */, 774 nullptr, nullptr, max_juint, nullptr, nullptr, 775 result /* pre_val */, T_OBJECT); 776 if (ShenandoahCardBarrier) { 777 post_barrier(kit, kit->control(), access.raw_access(), access.base(), 778 access.addr().node(), access.alias_idx(), val, T_OBJECT, true); 779 } 780 } 781 return result; 782 } 783 784 785 bool ShenandoahBarrierSetC2::is_gc_pre_barrier_node(Node* node) const { 786 return is_shenandoah_wb_pre_call(node); 787 } 788 789 // Support for GC barriers emitted during parsing 790 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const { 791 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) return true; 792 if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) { 793 return false; 794 } 795 CallLeafNode *call = node->as_CallLeaf(); 796 if (call->_name == nullptr) { 797 return false; 798 } 799 800 return strcmp(call->_name, "shenandoah_clone_barrier") == 0 || 801 strcmp(call->_name, "shenandoah_cas_obj") == 0 || 802 strcmp(call->_name, "shenandoah_wb_pre") == 0; 803 } 804 805 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const { 806 if (c == nullptr) { 807 return c; 808 } 809 if (c->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 810 return c->in(ShenandoahLoadReferenceBarrierNode::ValueIn); 811 } 812 return c; 813 } 814 815 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const { 816 return !ShenandoahBarrierC2Support::expand(C, igvn); 817 } 818 819 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const { 820 if (mode == LoopOptsShenandoahExpand) { 821 assert(UseShenandoahGC, "only for shenandoah"); 822 ShenandoahBarrierC2Support::pin_and_expand(phase); 823 return true; 824 } else if (mode == LoopOptsShenandoahPostExpand) { 825 assert(UseShenandoahGC, "only for shenandoah"); 826 visited.clear(); 827 ShenandoahBarrierC2Support::optimize_after_expansion(visited, nstack, worklist, phase); 828 return true; 829 } 830 return false; 831 } 832 833 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, bool is_clone_instance, ArrayCopyPhase phase) const { 834 bool is_oop = is_reference_type(type); 835 if (!is_oop) { 836 return false; 837 } 838 if (ShenandoahSATBBarrier && tightly_coupled_alloc) { 839 if (phase == Optimization) { 840 return false; 841 } 842 return !is_clone; 843 } 844 return true; 845 } 846 847 bool ShenandoahBarrierSetC2::clone_needs_barrier(Node* src, PhaseGVN& gvn) { 848 const TypeOopPtr* src_type = gvn.type(src)->is_oopptr(); 849 if (src_type->isa_instptr() != nullptr) { 850 ciInstanceKlass* ik = src_type->is_instptr()->instance_klass(); 851 if ((src_type->klass_is_exact() || !ik->has_subklass()) && !ik->has_injected_fields()) { 852 if (ik->has_object_fields()) { 853 return true; 854 } else { 855 if (!src_type->klass_is_exact()) { 856 Compile::current()->dependencies()->assert_leaf_type(ik); 857 } 858 } 859 } else { 860 return true; 861 } 862 } else if (src_type->isa_aryptr()) { 863 BasicType src_elem = src_type->isa_aryptr()->elem()->array_element_basic_type(); 864 if (is_reference_type(src_elem, true)) { 865 return true; 866 } 867 } else { 868 return true; 869 } 870 return false; 871 } 872 873 void ShenandoahBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const { 874 Node* ctrl = ac->in(TypeFunc::Control); 875 Node* mem = ac->in(TypeFunc::Memory); 876 Node* src_base = ac->in(ArrayCopyNode::Src); 877 Node* src_offset = ac->in(ArrayCopyNode::SrcPos); 878 Node* dest_base = ac->in(ArrayCopyNode::Dest); 879 Node* dest_offset = ac->in(ArrayCopyNode::DestPos); 880 Node* length = ac->in(ArrayCopyNode::Length); 881 882 Node* src = phase->basic_plus_adr(src_base, src_offset); 883 Node* dest = phase->basic_plus_adr(dest_base, dest_offset); 884 885 if (ShenandoahCloneBarrier && clone_needs_barrier(src, phase->igvn())) { 886 // Check if heap is has forwarded objects. If it does, we need to call into the special 887 // routine that would fix up source references before we can continue. 888 889 enum { _heap_stable = 1, _heap_unstable, PATH_LIMIT }; 890 Node* region = new RegionNode(PATH_LIMIT); 891 Node* mem_phi = new PhiNode(region, Type::MEMORY, TypeRawPtr::BOTTOM); 892 893 Node* thread = phase->transform_later(new ThreadLocalNode()); 894 Node* offset = phase->igvn().MakeConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset())); 895 Node* gc_state_addr = phase->transform_later(new AddPNode(phase->C->top(), thread, offset)); 896 897 uint gc_state_idx = Compile::AliasIdxRaw; 898 const TypePtr* gc_state_adr_type = nullptr; // debug-mode-only argument 899 debug_only(gc_state_adr_type = phase->C->get_adr_type(gc_state_idx)); 900 901 Node* gc_state = phase->transform_later(new LoadBNode(ctrl, mem, gc_state_addr, gc_state_adr_type, TypeInt::BYTE, MemNode::unordered)); 902 Node* stable_and = phase->transform_later(new AndINode(gc_state, phase->igvn().intcon(ShenandoahHeap::HAS_FORWARDED))); 903 Node* stable_cmp = phase->transform_later(new CmpINode(stable_and, phase->igvn().zerocon(T_INT))); 904 Node* stable_test = phase->transform_later(new BoolNode(stable_cmp, BoolTest::ne)); 905 906 IfNode* stable_iff = phase->transform_later(new IfNode(ctrl, stable_test, PROB_UNLIKELY(0.999), COUNT_UNKNOWN))->as_If(); 907 Node* stable_ctrl = phase->transform_later(new IfFalseNode(stable_iff)); 908 Node* unstable_ctrl = phase->transform_later(new IfTrueNode(stable_iff)); 909 910 // Heap is stable, no need to do anything additional 911 region->init_req(_heap_stable, stable_ctrl); 912 mem_phi->init_req(_heap_stable, mem); 913 914 // Heap is unstable, call into clone barrier stub 915 Node* call = phase->make_leaf_call(unstable_ctrl, mem, 916 ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(), 917 CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier), 918 "shenandoah_clone", 919 TypeRawPtr::BOTTOM, 920 src_base); 921 call = phase->transform_later(call); 922 923 ctrl = phase->transform_later(new ProjNode(call, TypeFunc::Control)); 924 mem = phase->transform_later(new ProjNode(call, TypeFunc::Memory)); 925 region->init_req(_heap_unstable, ctrl); 926 mem_phi->init_req(_heap_unstable, mem); 927 928 // Wire up the actual arraycopy stub now 929 ctrl = phase->transform_later(region); 930 mem = phase->transform_later(mem_phi); 931 932 const char* name = "arraycopy"; 933 call = phase->make_leaf_call(ctrl, mem, 934 OptoRuntime::fast_arraycopy_Type(), 935 phase->basictype2arraycopy(T_LONG, nullptr, nullptr, true, name, true), 936 name, TypeRawPtr::BOTTOM, 937 src, dest, length 938 LP64_ONLY(COMMA phase->top())); 939 call = phase->transform_later(call); 940 941 // Hook up the whole thing into the graph 942 phase->igvn().replace_node(ac, call); 943 } else { 944 BarrierSetC2::clone_at_expansion(phase, ac); 945 } 946 } 947 948 949 // Support for macro expanded GC barriers 950 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const { 951 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 952 state()->add_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node); 953 } 954 } 955 956 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const { 957 if (node->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 958 state()->remove_load_reference_barrier((ShenandoahLoadReferenceBarrierNode*) node); 959 } 960 } 961 962 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { 963 if (is_shenandoah_wb_pre_call(node)) { 964 shenandoah_eliminate_wb_pre(node, ¯o->igvn()); 965 } 966 if (ShenandoahCardBarrier && node->Opcode() == Op_CastP2X) { 967 Node* shift = node->unique_out(); 968 Node* addp = shift->unique_out(); 969 for (DUIterator_Last jmin, j = addp->last_outs(jmin); j >= jmin; --j) { 970 Node* mem = addp->last_out(j); 971 if (UseCondCardMark && mem->is_Load()) { 972 assert(mem->Opcode() == Op_LoadB, "unexpected code shape"); 973 // The load is checking if the card has been written so 974 // replace it with zero to fold the test. 975 macro->replace_node(mem, macro->intcon(0)); 976 continue; 977 } 978 assert(mem->is_Store(), "store required"); 979 macro->replace_node(mem, mem->in(MemNode::Memory)); 980 } 981 } 982 } 983 984 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const { 985 assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), ""); 986 Node* c = call->as_Call()->proj_out(TypeFunc::Control); 987 c = c->unique_ctrl_out(); 988 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 989 c = c->unique_ctrl_out(); 990 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 991 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 992 assert(iff->is_If(), "expect test"); 993 if (!is_shenandoah_marking_if(igvn, iff)) { 994 c = c->unique_ctrl_out(); 995 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 996 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 997 assert(is_shenandoah_marking_if(igvn, iff), "expect marking test"); 998 } 999 Node* cmpx = iff->in(1)->in(1); 1000 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ)); 1001 igvn->rehash_node_delayed(call); 1002 call->del_req(call->req()-1); 1003 } 1004 1005 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const { 1006 if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) { 1007 igvn->add_users_to_worklist(node); 1008 } 1009 } 1010 1011 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const { 1012 for (uint i = 0; i < useful.size(); i++) { 1013 Node* n = useful.at(i); 1014 if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) { 1015 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1016 C->record_for_igvn(n->fast_out(i)); 1017 } 1018 } 1019 } 1020 1021 for (int i = state()->load_reference_barriers_count() - 1; i >= 0; i--) { 1022 ShenandoahLoadReferenceBarrierNode* n = state()->load_reference_barrier(i); 1023 if (!useful.member(n)) { 1024 state()->remove_load_reference_barrier(n); 1025 } 1026 } 1027 } 1028 1029 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const { 1030 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena); 1031 } 1032 1033 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const { 1034 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state()); 1035 } 1036 1037 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be 1038 // expanded later, then now is the time to do so. 1039 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; } 1040 1041 #ifdef ASSERT 1042 void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const { 1043 if (ShenandoahVerifyOptoBarriers && phase == BarrierSetC2::BeforeMacroExpand) { 1044 ShenandoahBarrierC2Support::verify(Compile::current()->root()); 1045 } else if (phase == BarrierSetC2::BeforeCodeGen) { 1046 // Verify Shenandoah pre-barriers 1047 const int gc_state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset()); 1048 1049 Unique_Node_List visited; 1050 Node_List worklist; 1051 // We're going to walk control flow backwards starting from the Root 1052 worklist.push(compile->root()); 1053 while (worklist.size() > 0) { 1054 Node *x = worklist.pop(); 1055 if (x == nullptr || x == compile->top()) { 1056 continue; 1057 } 1058 1059 if (visited.member(x)) { 1060 continue; 1061 } else { 1062 visited.push(x); 1063 } 1064 1065 if (x->is_Region()) { 1066 for (uint i = 1; i < x->req(); i++) { 1067 worklist.push(x->in(i)); 1068 } 1069 } else { 1070 worklist.push(x->in(0)); 1071 // We are looking for the pattern: 1072 // /->ThreadLocal 1073 // If->Bool->CmpI->LoadB->AddP->ConL(marking_offset) 1074 // \->ConI(0) 1075 // We want to verify that the If and the LoadB have the same control 1076 // See GraphKit::g1_write_barrier_pre() 1077 if (x->is_If()) { 1078 IfNode *iff = x->as_If(); 1079 if (iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp()) { 1080 CmpNode *cmp = iff->in(1)->in(1)->as_Cmp(); 1081 if (cmp->Opcode() == Op_CmpI && cmp->in(2)->is_Con() && cmp->in(2)->bottom_type()->is_int()->get_con() == 0 1082 && cmp->in(1)->is_Load()) { 1083 LoadNode *load = cmp->in(1)->as_Load(); 1084 if (load->Opcode() == Op_LoadB && load->in(2)->is_AddP() && load->in(2)->in(2)->Opcode() == Op_ThreadLocal 1085 && load->in(2)->in(3)->is_Con() 1086 && load->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == gc_state_offset) { 1087 1088 Node *if_ctrl = iff->in(0); 1089 Node *load_ctrl = load->in(0); 1090 1091 if (if_ctrl != load_ctrl) { 1092 // Skip possible CProj->NeverBranch in infinite loops 1093 if ((if_ctrl->is_Proj() && if_ctrl->Opcode() == Op_CProj) 1094 && if_ctrl->in(0)->is_NeverBranch()) { 1095 if_ctrl = if_ctrl->in(0)->in(0); 1096 } 1097 } 1098 assert(load_ctrl != nullptr && if_ctrl == load_ctrl, "controls must match"); 1099 } 1100 } 1101 } 1102 } 1103 } 1104 } 1105 } 1106 } 1107 #endif 1108 1109 Node* ShenandoahBarrierSetC2::ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const { 1110 if (is_shenandoah_wb_pre_call(n)) { 1111 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 1112 if (n->req() > cnt) { 1113 Node* addp = n->in(cnt); 1114 if (has_only_shenandoah_wb_pre_uses(addp)) { 1115 n->del_req(cnt); 1116 if (can_reshape) { 1117 phase->is_IterGVN()->_worklist.push(addp); 1118 } 1119 return n; 1120 } 1121 } 1122 } 1123 if (n->Opcode() == Op_CmpP) { 1124 Node* in1 = n->in(1); 1125 Node* in2 = n->in(2); 1126 1127 // If one input is null, then step over the strong LRB barriers on the other input 1128 if (in1->bottom_type() == TypePtr::NULL_PTR && 1129 !((in2->Opcode() == Op_ShenandoahLoadReferenceBarrier) && 1130 !ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in2)->decorators()))) { 1131 in2 = step_over_gc_barrier(in2); 1132 } 1133 if (in2->bottom_type() == TypePtr::NULL_PTR && 1134 !((in1->Opcode() == Op_ShenandoahLoadReferenceBarrier) && 1135 !ShenandoahBarrierSet::is_strong_access(((ShenandoahLoadReferenceBarrierNode*)in1)->decorators()))) { 1136 in1 = step_over_gc_barrier(in1); 1137 } 1138 1139 if (in1 != n->in(1)) { 1140 n->set_req_X(1, in1, phase); 1141 assert(in2 == n->in(2), "only one change"); 1142 return n; 1143 } 1144 if (in2 != n->in(2)) { 1145 n->set_req_X(2, in2, phase); 1146 return n; 1147 } 1148 } else if (can_reshape && 1149 n->Opcode() == Op_If && 1150 ShenandoahBarrierC2Support::is_heap_stable_test(n) && 1151 n->in(0) != nullptr && 1152 n->outcnt() == 2) { 1153 Node* dom = n->in(0); 1154 Node* prev_dom = n; 1155 int op = n->Opcode(); 1156 int dist = 16; 1157 // Search up the dominator tree for another heap stable test 1158 while (dom->Opcode() != op || // Not same opcode? 1159 !ShenandoahBarrierC2Support::is_heap_stable_test(dom) || // Not same input 1? 1160 prev_dom->in(0) != dom) { // One path of test does not dominate? 1161 if (dist < 0) return nullptr; 1162 1163 dist--; 1164 prev_dom = dom; 1165 dom = IfNode::up_one_dom(dom); 1166 if (!dom) return nullptr; 1167 } 1168 1169 // Check that we did not follow a loop back to ourselves 1170 if (n == dom) { 1171 return nullptr; 1172 } 1173 1174 return n->as_If()->dominated_by(prev_dom, phase->is_IterGVN(), false); 1175 } 1176 1177 return nullptr; 1178 } 1179 1180 bool ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(Node* n) { 1181 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1182 Node* u = n->fast_out(i); 1183 if (!is_shenandoah_wb_pre_call(u)) { 1184 return false; 1185 } 1186 } 1187 return n->outcnt() > 0; 1188 } 1189 1190 bool ShenandoahBarrierSetC2::final_graph_reshaping(Compile* compile, Node* n, uint opcode, Unique_Node_List& dead_nodes) const { 1191 switch (opcode) { 1192 case Op_CallLeaf: 1193 case Op_CallLeafNoFP: { 1194 assert (n->is_Call(), ""); 1195 CallNode *call = n->as_Call(); 1196 if (ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(call)) { 1197 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 1198 if (call->req() > cnt) { 1199 assert(call->req() == cnt + 1, "only one extra input"); 1200 Node *addp = call->in(cnt); 1201 assert(!ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(addp), "useless address computation?"); 1202 call->del_req(cnt); 1203 } 1204 } 1205 return false; 1206 } 1207 case Op_ShenandoahCompareAndSwapP: 1208 case Op_ShenandoahCompareAndSwapN: 1209 case Op_ShenandoahWeakCompareAndSwapN: 1210 case Op_ShenandoahWeakCompareAndSwapP: 1211 case Op_ShenandoahCompareAndExchangeP: 1212 case Op_ShenandoahCompareAndExchangeN: 1213 return true; 1214 case Op_ShenandoahLoadReferenceBarrier: 1215 assert(false, "should have been expanded already"); 1216 return true; 1217 default: 1218 return false; 1219 } 1220 } 1221 1222 bool ShenandoahBarrierSetC2::escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const { 1223 switch (opcode) { 1224 case Op_ShenandoahCompareAndExchangeP: 1225 case Op_ShenandoahCompareAndExchangeN: 1226 conn_graph->add_objload_to_connection_graph(n, delayed_worklist); 1227 // fallthrough 1228 case Op_ShenandoahWeakCompareAndSwapP: 1229 case Op_ShenandoahWeakCompareAndSwapN: 1230 case Op_ShenandoahCompareAndSwapP: 1231 case Op_ShenandoahCompareAndSwapN: 1232 conn_graph->add_to_congraph_unsafe_access(n, opcode, delayed_worklist); 1233 return true; 1234 case Op_StoreP: { 1235 Node* adr = n->in(MemNode::Address); 1236 const Type* adr_type = gvn->type(adr); 1237 // Pointer stores in Shenandoah barriers looks like unsafe access. 1238 // Ignore such stores to be able scalar replace non-escaping 1239 // allocations. 1240 if (adr_type->isa_rawptr() && adr->is_AddP()) { 1241 Node* base = conn_graph->get_addp_base(adr); 1242 if (base->Opcode() == Op_LoadP && 1243 base->in(MemNode::Address)->is_AddP()) { 1244 adr = base->in(MemNode::Address); 1245 Node* tls = conn_graph->get_addp_base(adr); 1246 if (tls->Opcode() == Op_ThreadLocal) { 1247 int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot); 1248 const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 1249 if (offs == buf_offset) { 1250 return true; // Pre barrier previous oop value store. 1251 } 1252 } 1253 } 1254 } 1255 return false; 1256 } 1257 case Op_ShenandoahLoadReferenceBarrier: 1258 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), delayed_worklist); 1259 return true; 1260 default: 1261 // Nothing 1262 break; 1263 } 1264 return false; 1265 } 1266 1267 bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const { 1268 switch (opcode) { 1269 case Op_ShenandoahCompareAndExchangeP: 1270 case Op_ShenandoahCompareAndExchangeN: { 1271 Node *adr = n->in(MemNode::Address); 1272 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, nullptr); 1273 // fallthrough 1274 } 1275 case Op_ShenandoahCompareAndSwapP: 1276 case Op_ShenandoahCompareAndSwapN: 1277 case Op_ShenandoahWeakCompareAndSwapP: 1278 case Op_ShenandoahWeakCompareAndSwapN: 1279 return conn_graph->add_final_edges_unsafe_access(n, opcode); 1280 case Op_ShenandoahLoadReferenceBarrier: 1281 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahLoadReferenceBarrierNode::ValueIn), nullptr); 1282 return true; 1283 default: 1284 // Nothing 1285 break; 1286 } 1287 return false; 1288 } 1289 1290 bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const { 1291 return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) || 1292 n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN); 1293 1294 } 1295 1296 bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const { 1297 switch (opcode) { 1298 case Op_ShenandoahCompareAndExchangeP: 1299 case Op_ShenandoahCompareAndExchangeN: 1300 case Op_ShenandoahWeakCompareAndSwapP: 1301 case Op_ShenandoahWeakCompareAndSwapN: 1302 case Op_ShenandoahCompareAndSwapP: 1303 case Op_ShenandoahCompareAndSwapN: { // Convert trinary to binary-tree 1304 Node* newval = n->in(MemNode::ValueIn); 1305 Node* oldval = n->in(LoadStoreConditionalNode::ExpectedIn); 1306 Node* pair = new BinaryNode(oldval, newval); 1307 n->set_req(MemNode::ValueIn,pair); 1308 n->del_req(LoadStoreConditionalNode::ExpectedIn); 1309 return true; 1310 } 1311 default: 1312 break; 1313 } 1314 return false; 1315 } 1316 1317 bool ShenandoahBarrierSetC2::matcher_is_store_load_barrier(Node* x, uint xop) const { 1318 return xop == Op_ShenandoahCompareAndExchangeP || 1319 xop == Op_ShenandoahCompareAndExchangeN || 1320 xop == Op_ShenandoahWeakCompareAndSwapP || 1321 xop == Op_ShenandoahWeakCompareAndSwapN || 1322 xop == Op_ShenandoahCompareAndSwapN || 1323 xop == Op_ShenandoahCompareAndSwapP; 1324 }