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