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 : _shenandoah_barriers(new (comp_arena) GrowableArray<ShenandoahWriteBarrierNode*>(comp_arena, 8, 0, NULL)) { 47 } 48 49 int ShenandoahBarrierSetC2State::shenandoah_barriers_count() const { 50 return _shenandoah_barriers->length(); 51 } 52 53 ShenandoahWriteBarrierNode* ShenandoahBarrierSetC2State::shenandoah_barrier(int idx) const { 54 return _shenandoah_barriers->at(idx); 55 } 56 57 void ShenandoahBarrierSetC2State::add_shenandoah_barrier(ShenandoahWriteBarrierNode * n) { 58 assert(!_shenandoah_barriers->contains(n), "duplicate entry in barrier list"); 59 _shenandoah_barriers->append(n); 60 } 61 62 void ShenandoahBarrierSetC2State::remove_shenandoah_barrier(ShenandoahWriteBarrierNode * n) { 63 if (_shenandoah_barriers->contains(n)) { 64 _shenandoah_barriers->remove(n); 65 } 66 } 67 68 #define __ kit-> 69 70 Node* ShenandoahBarrierSetC2::shenandoah_read_barrier(GraphKit* kit, Node* obj) const { 71 if (ShenandoahReadBarrier) { 72 obj = shenandoah_read_barrier_impl(kit, obj, false, true, true); 73 } 74 return obj; 75 } 76 77 Node* ShenandoahBarrierSetC2::shenandoah_storeval_barrier(GraphKit* kit, Node* obj) const { 78 if (ShenandoahStoreValEnqueueBarrier) { 79 obj = shenandoah_write_barrier(kit, obj); 80 obj = shenandoah_enqueue_barrier(kit, obj); 81 } 82 if (ShenandoahStoreValReadBarrier) { 83 obj = shenandoah_read_barrier_impl(kit, obj, true, false, false); 84 } 85 return obj; 86 } 87 88 Node* ShenandoahBarrierSetC2::shenandoah_read_barrier_impl(GraphKit* kit, Node* obj, bool use_ctrl, bool use_mem, bool allow_fromspace) const { 89 const Type* obj_type = obj->bottom_type(); 90 if (obj_type->higher_equal(TypePtr::NULL_PTR)) { 91 return obj; 92 } 93 const TypePtr* adr_type = ShenandoahBarrierNode::brooks_pointer_type(obj_type); 94 Node* mem = use_mem ? __ memory(adr_type) : __ immutable_memory(); 95 96 if (! ShenandoahBarrierNode::needs_barrier(&__ gvn(), NULL, obj, mem, allow_fromspace)) { 97 // We know it is null, no barrier needed. 98 return obj; 99 } 100 101 if (obj_type->meet(TypePtr::NULL_PTR) == obj_type->remove_speculative()) { 102 103 // We don't know if it's null or not. Need null-check. 104 enum { _not_null_path = 1, _null_path, PATH_LIMIT }; 105 RegionNode* region = new RegionNode(PATH_LIMIT); 106 Node* phi = new PhiNode(region, obj_type); 107 Node* null_ctrl = __ top(); 108 Node* not_null_obj = __ null_check_oop(obj, &null_ctrl); 109 110 region->init_req(_null_path, null_ctrl); 111 phi ->init_req(_null_path, __ zerocon(T_OBJECT)); 112 113 Node* ctrl = use_ctrl ? __ control() : NULL; 114 ShenandoahReadBarrierNode* rb = new ShenandoahReadBarrierNode(ctrl, mem, not_null_obj, allow_fromspace); 115 Node* n = __ gvn().transform(rb); 116 117 region->init_req(_not_null_path, __ control()); 118 phi ->init_req(_not_null_path, n); 119 120 __ set_control(__ gvn().transform(region)); 121 __ record_for_igvn(region); 122 return __ gvn().transform(phi); 123 124 } else { 125 // We know it is not null. Simple barrier is sufficient. 126 Node* ctrl = use_ctrl ? __ control() : NULL; 127 ShenandoahReadBarrierNode* rb = new ShenandoahReadBarrierNode(ctrl, mem, obj, allow_fromspace); 128 Node* n = __ gvn().transform(rb); 129 __ record_for_igvn(n); 130 return n; 131 } 132 } 133 134 Node* ShenandoahBarrierSetC2::shenandoah_write_barrier_helper(GraphKit* kit, Node* obj, const TypePtr* adr_type) const { 135 ShenandoahWriteBarrierNode* wb = new ShenandoahWriteBarrierNode(kit->C, kit->control(), kit->memory(adr_type), obj); 136 Node* n = __ gvn().transform(wb); 137 if (n == wb) { // New barrier needs memory projection. 138 Node* proj = __ gvn().transform(new ShenandoahWBMemProjNode(n)); 139 __ set_memory(proj, adr_type); 140 } 141 return n; 142 } 143 144 Node* ShenandoahBarrierSetC2::shenandoah_write_barrier(GraphKit* kit, Node* obj) const { 145 if (ShenandoahWriteBarrier) { 146 obj = shenandoah_write_barrier_impl(kit, obj); 147 } 148 return obj; 149 } 150 151 Node* ShenandoahBarrierSetC2::shenandoah_write_barrier_impl(GraphKit* kit, Node* obj) const { 152 if (! ShenandoahBarrierNode::needs_barrier(&__ gvn(), NULL, obj, NULL, true)) { 153 return obj; 154 } 155 const Type* obj_type = obj->bottom_type(); 156 const TypePtr* adr_type = ShenandoahBarrierNode::brooks_pointer_type(obj_type); 157 Node* n = shenandoah_write_barrier_helper(kit, obj, adr_type); 158 __ record_for_igvn(n); 159 return n; 160 } 161 162 bool ShenandoahBarrierSetC2::satb_can_remove_pre_barrier(GraphKit* kit, PhaseTransform* phase, Node* adr, 163 BasicType bt, uint adr_idx) const { 164 intptr_t offset = 0; 165 Node* base = AddPNode::Ideal_base_and_offset(adr, phase, offset); 166 AllocateNode* alloc = AllocateNode::Ideal_allocation(base, phase); 167 168 if (offset == Type::OffsetBot) { 169 return false; // cannot unalias unless there are precise offsets 170 } 171 172 if (alloc == NULL) { 173 return false; // No allocation found 174 } 175 176 intptr_t size_in_bytes = type2aelembytes(bt); 177 178 Node* mem = __ memory(adr_idx); // start searching here... 179 180 for (int cnt = 0; cnt < 50; cnt++) { 181 182 if (mem->is_Store()) { 183 184 Node* st_adr = mem->in(MemNode::Address); 185 intptr_t st_offset = 0; 186 Node* st_base = AddPNode::Ideal_base_and_offset(st_adr, phase, st_offset); 187 188 if (st_base == NULL) { 189 break; // inscrutable pointer 190 } 191 192 // Break we have found a store with same base and offset as ours so break 193 if (st_base == base && st_offset == offset) { 194 break; 195 } 196 197 if (st_offset != offset && st_offset != Type::OffsetBot) { 198 const int MAX_STORE = BytesPerLong; 199 if (st_offset >= offset + size_in_bytes || 200 st_offset <= offset - MAX_STORE || 201 st_offset <= offset - mem->as_Store()->memory_size()) { 202 // Success: The offsets are provably independent. 203 // (You may ask, why not just test st_offset != offset and be done? 204 // The answer is that stores of different sizes can co-exist 205 // in the same sequence of RawMem effects. We sometimes initialize 206 // a whole 'tile' of array elements with a single jint or jlong.) 207 mem = mem->in(MemNode::Memory); 208 continue; // advance through independent store memory 209 } 210 } 211 212 if (st_base != base 213 && MemNode::detect_ptr_independence(base, alloc, st_base, 214 AllocateNode::Ideal_allocation(st_base, phase), 215 phase)) { 216 // Success: The bases are provably independent. 217 mem = mem->in(MemNode::Memory); 218 continue; // advance through independent store memory 219 } 220 } else if (mem->is_Proj() && mem->in(0)->is_Initialize()) { 221 222 InitializeNode* st_init = mem->in(0)->as_Initialize(); 223 AllocateNode* st_alloc = st_init->allocation(); 224 225 // Make sure that we are looking at the same allocation site. 226 // The alloc variable is guaranteed to not be null here from earlier check. 227 if (alloc == st_alloc) { 228 // Check that the initialization is storing NULL so that no previous store 229 // has been moved up and directly write a reference 230 Node* captured_store = st_init->find_captured_store(offset, 231 type2aelembytes(T_OBJECT), 232 phase); 233 if (captured_store == NULL || captured_store == st_init->zero_memory()) { 234 return true; 235 } 236 } 237 } 238 239 // Unless there is an explicit 'continue', we must bail out here, 240 // because 'mem' is an inscrutable memory state (e.g., a call). 241 break; 242 } 243 244 return false; 245 } 246 247 #undef __ 248 #define __ ideal. 249 250 void ShenandoahBarrierSetC2::satb_write_barrier_pre(GraphKit* kit, 251 bool do_load, 252 Node* obj, 253 Node* adr, 254 uint alias_idx, 255 Node* val, 256 const TypeOopPtr* val_type, 257 Node* pre_val, 258 BasicType bt) const { 259 // Some sanity checks 260 // Note: val is unused in this routine. 261 262 if (do_load) { 263 // We need to generate the load of the previous value 264 assert(obj != NULL, "must have a base"); 265 assert(adr != NULL, "where are loading from?"); 266 assert(pre_val == NULL, "loaded already?"); 267 assert(val_type != NULL, "need a type"); 268 269 if (ReduceInitialCardMarks 270 && satb_can_remove_pre_barrier(kit, &kit->gvn(), adr, bt, alias_idx)) { 271 return; 272 } 273 274 } else { 275 // In this case both val_type and alias_idx are unused. 276 assert(pre_val != NULL, "must be loaded already"); 277 // Nothing to be done if pre_val is null. 278 if (pre_val->bottom_type() == TypePtr::NULL_PTR) return; 279 assert(pre_val->bottom_type()->basic_type() == T_OBJECT, "or we shouldn't be here"); 280 } 281 assert(bt == T_OBJECT, "or we shouldn't be here"); 282 283 IdealKit ideal(kit, true); 284 285 Node* tls = __ thread(); // ThreadLocalStorage 286 287 Node* no_base = __ top(); 288 Node* zero = __ ConI(0); 289 Node* zeroX = __ ConX(0); 290 291 float likely = PROB_LIKELY(0.999); 292 float unlikely = PROB_UNLIKELY(0.999); 293 294 // Offsets into the thread 295 const int index_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_index_offset()); 296 const int buffer_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 297 298 // Now the actual pointers into the thread 299 Node* buffer_adr = __ AddP(no_base, tls, __ ConX(buffer_offset)); 300 Node* index_adr = __ AddP(no_base, tls, __ ConX(index_offset)); 301 302 // Now some of the values 303 Node* marking; 304 Node* gc_state = __ AddP(no_base, tls, __ ConX(in_bytes(ShenandoahThreadLocalData::gc_state_offset()))); 305 Node* ld = __ load(__ ctrl(), gc_state, TypeInt::BYTE, T_BYTE, Compile::AliasIdxRaw); 306 marking = __ AndI(ld, __ ConI(ShenandoahHeap::MARKING)); 307 assert(ShenandoahWriteBarrierNode::is_gc_state_load(ld), "Should match the shape"); 308 309 // if (!marking) 310 __ if_then(marking, BoolTest::ne, zero, unlikely); { 311 BasicType index_bt = TypeX_X->basic_type(); 312 assert(sizeof(size_t) == type2aelembytes(index_bt), "Loading G1 SATBMarkQueue::_index with wrong size."); 313 Node* index = __ load(__ ctrl(), index_adr, TypeX_X, index_bt, Compile::AliasIdxRaw); 314 315 if (do_load) { 316 // load original value 317 // alias_idx correct?? 318 pre_val = __ load(__ ctrl(), adr, val_type, bt, alias_idx); 319 } 320 321 // if (pre_val != NULL) 322 __ if_then(pre_val, BoolTest::ne, kit->null()); { 323 Node* buffer = __ load(__ ctrl(), buffer_adr, TypeRawPtr::NOTNULL, T_ADDRESS, Compile::AliasIdxRaw); 324 325 // is the queue for this thread full? 326 __ if_then(index, BoolTest::ne, zeroX, likely); { 327 328 // decrement the index 329 Node* next_index = kit->gvn().transform(new SubXNode(index, __ ConX(sizeof(intptr_t)))); 330 331 // Now get the buffer location we will log the previous value into and store it 332 Node *log_addr = __ AddP(no_base, buffer, next_index); 333 __ store(__ ctrl(), log_addr, pre_val, T_OBJECT, Compile::AliasIdxRaw, MemNode::unordered); 334 // update the index 335 __ store(__ ctrl(), index_adr, next_index, index_bt, Compile::AliasIdxRaw, MemNode::unordered); 336 337 } __ else_(); { 338 339 // logging buffer is full, call the runtime 340 const TypeFunc *tf = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type(); 341 __ make_leaf_call(tf, CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry), "shenandoah_wb_pre", pre_val, tls); 342 } __ end_if(); // (!index) 343 } __ end_if(); // (pre_val != NULL) 344 } __ end_if(); // (!marking) 345 346 // Final sync IdealKit and GraphKit. 347 kit->final_sync(ideal); 348 349 if (ShenandoahSATBBarrier && adr != NULL) { 350 Node* c = kit->control(); 351 Node* call = c->in(1)->in(1)->in(1)->in(0); 352 assert(is_shenandoah_wb_pre_call(call), "shenandoah_wb_pre call expected"); 353 call->add_req(adr); 354 } 355 } 356 357 bool ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(Node* call) { 358 return call->is_CallLeaf() && 359 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_ref_field_pre_entry); 360 } 361 362 bool ShenandoahBarrierSetC2::is_shenandoah_wb_call(Node* call) { 363 return call->is_CallLeaf() && 364 call->as_CallLeaf()->entry_point() == CAST_FROM_FN_PTR(address, ShenandoahRuntime::write_barrier_JRT); 365 } 366 367 bool ShenandoahBarrierSetC2::is_shenandoah_marking_if(PhaseTransform *phase, Node* n) { 368 if (n->Opcode() != Op_If) { 369 return false; 370 } 371 372 Node* bol = n->in(1); 373 assert(bol->is_Bool(), ""); 374 Node* cmpx = bol->in(1); 375 if (bol->as_Bool()->_test._test == BoolTest::ne && 376 cmpx->is_Cmp() && cmpx->in(2) == phase->intcon(0) && 377 is_shenandoah_state_load(cmpx->in(1)->in(1)) && 378 cmpx->in(1)->in(2)->is_Con() && 379 cmpx->in(1)->in(2) == phase->intcon(ShenandoahHeap::MARKING)) { 380 return true; 381 } 382 383 return false; 384 } 385 386 bool ShenandoahBarrierSetC2::is_shenandoah_state_load(Node* n) { 387 if (!n->is_Load()) return false; 388 const int state_offset = in_bytes(ShenandoahThreadLocalData::gc_state_offset()); 389 return n->in(2)->is_AddP() && n->in(2)->in(2)->Opcode() == Op_ThreadLocal 390 && n->in(2)->in(3)->is_Con() 391 && n->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == state_offset; 392 } 393 394 void ShenandoahBarrierSetC2::shenandoah_write_barrier_pre(GraphKit* kit, 395 bool do_load, 396 Node* obj, 397 Node* adr, 398 uint alias_idx, 399 Node* val, 400 const TypeOopPtr* val_type, 401 Node* pre_val, 402 BasicType bt) const { 403 if (ShenandoahSATBBarrier) { 404 IdealKit ideal(kit); 405 kit->sync_kit(ideal); 406 407 satb_write_barrier_pre(kit, do_load, obj, adr, alias_idx, val, val_type, pre_val, bt); 408 409 ideal.sync_kit(kit); 410 kit->final_sync(ideal); 411 } 412 } 413 414 Node* ShenandoahBarrierSetC2::shenandoah_enqueue_barrier(GraphKit* kit, Node* pre_val) const { 415 return kit->gvn().transform(new ShenandoahEnqueueBarrierNode(pre_val)); 416 } 417 418 // Helper that guards and inserts a pre-barrier. 419 void ShenandoahBarrierSetC2::insert_pre_barrier(GraphKit* kit, Node* base_oop, Node* offset, 420 Node* pre_val, bool need_mem_bar) const { 421 // We could be accessing the referent field of a reference object. If so, when G1 422 // is enabled, we need to log the value in the referent field in an SATB buffer. 423 // This routine performs some compile time filters and generates suitable 424 // runtime filters that guard the pre-barrier code. 425 // Also add memory barrier for non volatile load from the referent field 426 // to prevent commoning of loads across safepoint. 427 428 // Some compile time checks. 429 430 // If offset is a constant, is it java_lang_ref_Reference::_reference_offset? 431 const TypeX* otype = offset->find_intptr_t_type(); 432 if (otype != NULL && otype->is_con() && 433 otype->get_con() != java_lang_ref_Reference::referent_offset) { 434 // Constant offset but not the reference_offset so just return 435 return; 436 } 437 438 // We only need to generate the runtime guards for instances. 439 const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr(); 440 if (btype != NULL) { 441 if (btype->isa_aryptr()) { 442 // Array type so nothing to do 443 return; 444 } 445 446 const TypeInstPtr* itype = btype->isa_instptr(); 447 if (itype != NULL) { 448 // Can the klass of base_oop be statically determined to be 449 // _not_ a sub-class of Reference and _not_ Object? 450 ciKlass* klass = itype->klass(); 451 if ( klass->is_loaded() && 452 !klass->is_subtype_of(kit->env()->Reference_klass()) && 453 !kit->env()->Object_klass()->is_subtype_of(klass)) { 454 return; 455 } 456 } 457 } 458 459 // The compile time filters did not reject base_oop/offset so 460 // we need to generate the following runtime filters 461 // 462 // if (offset == java_lang_ref_Reference::_reference_offset) { 463 // if (instance_of(base, java.lang.ref.Reference)) { 464 // pre_barrier(_, pre_val, ...); 465 // } 466 // } 467 468 float likely = PROB_LIKELY( 0.999); 469 float unlikely = PROB_UNLIKELY(0.999); 470 471 IdealKit ideal(kit); 472 473 Node* referent_off = __ ConX(java_lang_ref_Reference::referent_offset); 474 475 __ if_then(offset, BoolTest::eq, referent_off, unlikely); { 476 // Update graphKit memory and control from IdealKit. 477 kit->sync_kit(ideal); 478 479 Node* ref_klass_con = kit->makecon(TypeKlassPtr::make(kit->env()->Reference_klass())); 480 Node* is_instof = kit->gen_instanceof(base_oop, ref_klass_con); 481 482 // Update IdealKit memory and control from graphKit. 483 __ sync_kit(kit); 484 485 Node* one = __ ConI(1); 486 // is_instof == 0 if base_oop == NULL 487 __ if_then(is_instof, BoolTest::eq, one, unlikely); { 488 489 // Update graphKit from IdeakKit. 490 kit->sync_kit(ideal); 491 492 // Use the pre-barrier to record the value in the referent field 493 satb_write_barrier_pre(kit, false /* do_load */, 494 NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */, 495 pre_val /* pre_val */, 496 T_OBJECT); 497 if (need_mem_bar) { 498 // Add memory barrier to prevent commoning reads from this field 499 // across safepoint since GC can change its value. 500 kit->insert_mem_bar(Op_MemBarCPUOrder); 501 } 502 // Update IdealKit from graphKit. 503 __ sync_kit(kit); 504 505 } __ end_if(); // _ref_type != ref_none 506 } __ end_if(); // offset == referent_offset 507 508 // Final sync IdealKit and GraphKit. 509 kit->final_sync(ideal); 510 } 511 512 #undef __ 513 514 const TypeFunc* ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type() { 515 const Type **fields = TypeTuple::fields(2); 516 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value 517 fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // thread 518 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields); 519 520 // create result type (range) 521 fields = TypeTuple::fields(0); 522 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 523 524 return TypeFunc::make(domain, range); 525 } 526 527 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type() { 528 const Type **fields = TypeTuple::fields(1); 529 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value 530 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); 531 532 // create result type (range) 533 fields = TypeTuple::fields(0); 534 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields); 535 536 return TypeFunc::make(domain, range); 537 } 538 539 const TypeFunc* ShenandoahBarrierSetC2::shenandoah_write_barrier_Type() { 540 const Type **fields = TypeTuple::fields(1); 541 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // original field value 542 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields); 543 544 // create result type (range) 545 fields = TypeTuple::fields(1); 546 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; 547 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields); 548 549 return TypeFunc::make(domain, range); 550 } 551 552 void ShenandoahBarrierSetC2::resolve_address(C2Access& access) const { 553 const TypePtr* adr_type = access.addr().type(); 554 555 if ((access.decorators() & IN_NATIVE) == 0 && (adr_type->isa_instptr() || adr_type->isa_aryptr())) { 556 int off = adr_type->is_ptr()->offset(); 557 int base_off = adr_type->isa_instptr() ? instanceOopDesc::base_offset_in_bytes() : 558 arrayOopDesc::base_offset_in_bytes(adr_type->is_aryptr()->elem()->array_element_basic_type()); 559 assert(off != Type::OffsetTop, "unexpected offset"); 560 if (off == Type::OffsetBot || off >= base_off) { 561 DecoratorSet decorators = access.decorators(); 562 bool is_write = (decorators & C2_WRITE_ACCESS) != 0; 563 GraphKit* kit = NULL; 564 if (access.is_parse_access()) { 565 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 566 kit = parse_access.kit(); 567 } 568 Node* adr = access.addr().node(); 569 assert(adr->is_AddP(), "unexpected address shape"); 570 Node* base = adr->in(AddPNode::Base); 571 572 if (is_write) { 573 if (kit != NULL) { 574 base = shenandoah_write_barrier(kit, base); 575 } else { 576 assert(access.is_opt_access(), "either parse or opt access"); 577 assert((access.decorators() & C2_ARRAY_COPY) != 0, "can be skipped for clone"); 578 } 579 } else { 580 if (adr_type->isa_instptr()) { 581 Compile* C = access.gvn().C; 582 ciField* field = C->alias_type(adr_type)->field(); 583 584 // Insert read barrier for Shenandoah. 585 if (field != NULL && 586 ((ShenandoahOptimizeStaticFinals && field->is_static() && field->is_final()) || 587 (ShenandoahOptimizeInstanceFinals && !field->is_static() && field->is_final()) || 588 (ShenandoahOptimizeStableFinals && field->is_stable()))) { 589 // Skip the barrier for special fields 590 } else { 591 if (kit != NULL) { 592 base = shenandoah_read_barrier(kit, base); 593 } else { 594 assert(access.is_opt_access(), "either parse or opt access"); 595 assert((access.decorators() & C2_ARRAY_COPY) != 0, "can be skipped for arraycopy"); 596 } 597 } 598 } else { 599 if (kit != NULL) { 600 base = shenandoah_read_barrier(kit, base); 601 } else { 602 assert(access.is_opt_access(), "either parse or opt access"); 603 assert((access.decorators() & C2_ARRAY_COPY) != 0, "can be skipped for arraycopy"); 604 } 605 } 606 } 607 if (base != adr->in(AddPNode::Base)) { 608 assert(kit != NULL, "no barrier should have been added"); 609 610 Node* address = adr->in(AddPNode::Address); 611 612 if (address->is_AddP()) { 613 assert(address->in(AddPNode::Base) == adr->in(AddPNode::Base), "unexpected address shape"); 614 assert(!address->in(AddPNode::Address)->is_AddP(), "unexpected address shape"); 615 assert(address->in(AddPNode::Address) == adr->in(AddPNode::Base), "unexpected address shape"); 616 address = address->clone(); 617 address->set_req(AddPNode::Base, base); 618 address->set_req(AddPNode::Address, base); 619 address = kit->gvn().transform(address); 620 } else { 621 assert(address == adr->in(AddPNode::Base), "unexpected address shape"); 622 address = base; 623 } 624 adr = adr->clone(); 625 adr->set_req(AddPNode::Base, base); 626 adr->set_req(AddPNode::Address, address); 627 adr = kit->gvn().transform(adr); 628 access.addr().set_node(adr); 629 } 630 } 631 } 632 } 633 634 Node* ShenandoahBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const { 635 DecoratorSet decorators = access.decorators(); 636 637 const TypePtr* adr_type = access.addr().type(); 638 Node* adr = access.addr().node(); 639 640 bool anonymous = (decorators & ON_UNKNOWN_OOP_REF) != 0; 641 bool on_heap = (decorators & IN_HEAP) != 0; 642 643 if (!access.is_oop() || (!on_heap && !anonymous)) { 644 return BarrierSetC2::store_at_resolved(access, val); 645 } 646 647 if (access.is_parse_access()) { 648 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 649 GraphKit* kit = parse_access.kit(); 650 651 uint adr_idx = kit->C->get_alias_index(adr_type); 652 assert(adr_idx != Compile::AliasIdxTop, "use other store_to_memory factory" ); 653 Node* value = val.node(); 654 value = shenandoah_storeval_barrier(kit, value); 655 val.set_node(value); 656 shenandoah_write_barrier_pre(kit, true /* do_load */, /*kit->control(),*/ access.base(), adr, adr_idx, val.node(), 657 static_cast<const TypeOopPtr*>(val.type()), NULL /* pre_val */, access.type()); 658 } else { 659 assert(access.is_opt_access(), "only for optimization passes"); 660 assert(((decorators & C2_TIGHLY_COUPLED_ALLOC) != 0 || !ShenandoahSATBBarrier) && (decorators & C2_ARRAY_COPY) != 0, "unexpected caller of this code"); 661 C2OptAccess& opt_access = static_cast<C2OptAccess&>(access); 662 PhaseGVN& gvn = opt_access.gvn(); 663 MergeMemNode* mm = opt_access.mem(); 664 665 if (ShenandoahStoreValReadBarrier) { 666 RegionNode* region = new RegionNode(3); 667 const Type* v_t = gvn.type(val.node()); 668 Node* phi = new PhiNode(region, v_t->isa_oopptr() ? v_t->is_oopptr()->cast_to_nonconst() : v_t); 669 Node* cmp = gvn.transform(new CmpPNode(val.node(), gvn.zerocon(T_OBJECT))); 670 Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::ne)); 671 IfNode* iff = new IfNode(opt_access.ctl(), bol, PROB_LIKELY_MAG(3), COUNT_UNKNOWN); 672 673 gvn.transform(iff); 674 if (gvn.is_IterGVN()) { 675 gvn.is_IterGVN()->_worklist.push(iff); 676 } else { 677 gvn.record_for_igvn(iff); 678 } 679 680 Node* null_true = gvn.transform(new IfFalseNode(iff)); 681 Node* null_false = gvn.transform(new IfTrueNode(iff)); 682 region->init_req(1, null_true); 683 region->init_req(2, null_false); 684 phi->init_req(1, gvn.zerocon(T_OBJECT)); 685 Node* cast = new CastPPNode(val.node(), gvn.type(val.node())->join_speculative(TypePtr::NOTNULL)); 686 cast->set_req(0, null_false); 687 cast = gvn.transform(cast); 688 Node* rb = gvn.transform(new ShenandoahReadBarrierNode(null_false, gvn.C->immutable_memory(), cast, false)); 689 phi->init_req(2, rb); 690 opt_access.set_ctl(gvn.transform(region)); 691 val.set_node(gvn.transform(phi)); 692 } 693 if (ShenandoahStoreValEnqueueBarrier) { 694 const TypePtr* adr_type = ShenandoahBarrierNode::brooks_pointer_type(gvn.type(val.node())); 695 int alias = gvn.C->get_alias_index(adr_type); 696 Node* wb = new ShenandoahWriteBarrierNode(gvn.C, opt_access.ctl(), mm->memory_at(alias), val.node()); 697 Node* wb_transformed = gvn.transform(wb); 698 Node* enqueue = gvn.transform(new ShenandoahEnqueueBarrierNode(wb_transformed)); 699 if (wb_transformed == wb) { 700 Node* proj = gvn.transform(new ShenandoahWBMemProjNode(wb)); 701 mm->set_memory_at(alias, proj); 702 } 703 val.set_node(enqueue); 704 } 705 } 706 return BarrierSetC2::store_at_resolved(access, val); 707 } 708 709 Node* ShenandoahBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const { 710 DecoratorSet decorators = access.decorators(); 711 712 Node* adr = access.addr().node(); 713 Node* obj = access.base(); 714 715 bool mismatched = (decorators & C2_MISMATCHED) != 0; 716 bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0; 717 bool on_heap = (decorators & IN_HEAP) != 0; 718 bool on_weak = (decorators & ON_WEAK_OOP_REF) != 0; 719 bool is_unordered = (decorators & MO_UNORDERED) != 0; 720 bool need_cpu_mem_bar = !is_unordered || mismatched || !on_heap; 721 722 Node* top = Compile::current()->top(); 723 724 Node* offset = adr->is_AddP() ? adr->in(AddPNode::Offset) : top; 725 Node* load = BarrierSetC2::load_at_resolved(access, val_type); 726 727 // If we are reading the value of the referent field of a Reference 728 // object (either by using Unsafe directly or through reflection) 729 // then, if SATB is enabled, we need to record the referent in an 730 // SATB log buffer using the pre-barrier mechanism. 731 // Also we need to add memory barrier to prevent commoning reads 732 // from this field across safepoint since GC can change its value. 733 bool need_read_barrier = ShenandoahKeepAliveBarrier && 734 (on_heap && (on_weak || (unknown && offset != top && obj != top))); 735 736 if (!access.is_oop() || !need_read_barrier) { 737 return load; 738 } 739 740 assert(access.is_parse_access(), "entry not supported at optimization time"); 741 C2ParseAccess& parse_access = static_cast<C2ParseAccess&>(access); 742 GraphKit* kit = parse_access.kit(); 743 744 if (on_weak) { 745 // Use the pre-barrier to record the value in the referent field 746 satb_write_barrier_pre(kit, false /* do_load */, 747 NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */, 748 load /* pre_val */, T_OBJECT); 749 // Add memory barrier to prevent commoning reads from this field 750 // across safepoint since GC can change its value. 751 kit->insert_mem_bar(Op_MemBarCPUOrder); 752 } else if (unknown) { 753 // We do not require a mem bar inside pre_barrier if need_mem_bar 754 // is set: the barriers would be emitted by us. 755 insert_pre_barrier(kit, obj, offset, load, !need_cpu_mem_bar); 756 } 757 758 return load; 759 } 760 761 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 762 Node* new_val, const Type* value_type) const { 763 GraphKit* kit = access.kit(); 764 if (access.is_oop()) { 765 new_val = shenandoah_storeval_barrier(kit, new_val); 766 shenandoah_write_barrier_pre(kit, false /* do_load */, 767 NULL, NULL, max_juint, NULL, NULL, 768 expected_val /* pre_val */, T_OBJECT); 769 770 MemNode::MemOrd mo = access.mem_node_mo(); 771 Node* mem = access.memory(); 772 Node* adr = access.addr().node(); 773 const TypePtr* adr_type = access.addr().type(); 774 Node* load_store = NULL; 775 776 #ifdef _LP64 777 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 778 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 779 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 780 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangeNNode(kit->control(), mem, adr, newval_enc, oldval_enc, adr_type, value_type->make_narrowoop(), mo)); 781 } else 782 #endif 783 { 784 load_store = kit->gvn().transform(new ShenandoahCompareAndExchangePNode(kit->control(), mem, adr, new_val, expected_val, adr_type, value_type->is_oopptr(), mo)); 785 } 786 787 access.set_raw_access(load_store); 788 pin_atomic_op(access); 789 790 #ifdef _LP64 791 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 792 return kit->gvn().transform(new DecodeNNode(load_store, load_store->get_ptr_type())); 793 } 794 #endif 795 return load_store; 796 } 797 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, value_type); 798 } 799 800 Node* ShenandoahBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 801 Node* new_val, const Type* value_type) const { 802 GraphKit* kit = access.kit(); 803 if (access.is_oop()) { 804 new_val = shenandoah_storeval_barrier(kit, new_val); 805 shenandoah_write_barrier_pre(kit, false /* do_load */, 806 NULL, NULL, max_juint, NULL, NULL, 807 expected_val /* pre_val */, T_OBJECT); 808 DecoratorSet decorators = access.decorators(); 809 MemNode::MemOrd mo = access.mem_node_mo(); 810 Node* mem = access.memory(); 811 bool is_weak_cas = (decorators & C2_WEAK_CMPXCHG) != 0; 812 Node* load_store = NULL; 813 Node* adr = access.addr().node(); 814 #ifdef _LP64 815 if (adr->bottom_type()->is_ptr_to_narrowoop()) { 816 Node *newval_enc = kit->gvn().transform(new EncodePNode(new_val, new_val->bottom_type()->make_narrowoop())); 817 Node *oldval_enc = kit->gvn().transform(new EncodePNode(expected_val, expected_val->bottom_type()->make_narrowoop())); 818 if (is_weak_cas) { 819 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 820 } else { 821 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapNNode(kit->control(), mem, adr, newval_enc, oldval_enc, mo)); 822 } 823 } else 824 #endif 825 { 826 if (is_weak_cas) { 827 load_store = kit->gvn().transform(new ShenandoahWeakCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 828 } else { 829 load_store = kit->gvn().transform(new ShenandoahCompareAndSwapPNode(kit->control(), mem, adr, new_val, expected_val, mo)); 830 } 831 } 832 access.set_raw_access(load_store); 833 pin_atomic_op(access); 834 return load_store; 835 } 836 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type); 837 } 838 839 Node* ShenandoahBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* val, const Type* value_type) const { 840 GraphKit* kit = access.kit(); 841 if (access.is_oop()) { 842 val = shenandoah_storeval_barrier(kit, val); 843 } 844 Node* result = BarrierSetC2::atomic_xchg_at_resolved(access, val, value_type); 845 if (access.is_oop()) { 846 shenandoah_write_barrier_pre(kit, false /* do_load */, 847 NULL, NULL, max_juint, NULL, NULL, 848 result /* pre_val */, T_OBJECT); 849 } 850 return result; 851 } 852 853 void ShenandoahBarrierSetC2::clone(GraphKit* kit, Node* src, Node* dst, Node* size, bool is_array) const { 854 assert(!src->is_AddP(), "unexpected input"); 855 src = shenandoah_read_barrier(kit, src); 856 BarrierSetC2::clone(kit, src, dst, size, is_array); 857 } 858 859 Node* ShenandoahBarrierSetC2::resolve(GraphKit* kit, Node* n, DecoratorSet decorators) const { 860 bool is_write = decorators & ACCESS_WRITE; 861 if (is_write) { 862 return shenandoah_write_barrier(kit, n); 863 } else { 864 return shenandoah_read_barrier(kit, n); 865 } 866 } 867 868 Node* ShenandoahBarrierSetC2::obj_allocate(PhaseMacroExpand* macro, Node* ctrl, Node* mem, Node* toobig_false, Node* size_in_bytes, 869 Node*& i_o, Node*& needgc_ctrl, 870 Node*& fast_oop_ctrl, Node*& fast_oop_rawmem, 871 intx prefetch_lines) const { 872 PhaseIterGVN& igvn = macro->igvn(); 873 874 // Allocate several words more for the Shenandoah brooks pointer. 875 size_in_bytes = new AddXNode(size_in_bytes, igvn.MakeConX(ShenandoahBrooksPointer::byte_size())); 876 macro->transform_later(size_in_bytes); 877 878 Node* fast_oop = BarrierSetC2::obj_allocate(macro, ctrl, mem, toobig_false, size_in_bytes, 879 i_o, needgc_ctrl, fast_oop_ctrl, fast_oop_rawmem, 880 prefetch_lines); 881 882 // Bump up object for Shenandoah brooks pointer. 883 fast_oop = new AddPNode(macro->top(), fast_oop, igvn.MakeConX(ShenandoahBrooksPointer::byte_size())); 884 macro->transform_later(fast_oop); 885 886 // Initialize Shenandoah brooks pointer to point to the object itself. 887 fast_oop_rawmem = macro->make_store(fast_oop_ctrl, fast_oop_rawmem, fast_oop, ShenandoahBrooksPointer::byte_offset(), fast_oop, T_OBJECT); 888 889 return fast_oop; 890 } 891 892 // Support for GC barriers emitted during parsing 893 bool ShenandoahBarrierSetC2::is_gc_barrier_node(Node* node) const { 894 if (node->Opcode() != Op_CallLeaf && node->Opcode() != Op_CallLeafNoFP) { 895 return false; 896 } 897 CallLeafNode *call = node->as_CallLeaf(); 898 if (call->_name == NULL) { 899 return false; 900 } 901 902 return strcmp(call->_name, "shenandoah_clone_barrier") == 0 || 903 strcmp(call->_name, "shenandoah_cas_obj") == 0 || 904 strcmp(call->_name, "shenandoah_wb_pre") == 0; 905 } 906 907 Node* ShenandoahBarrierSetC2::step_over_gc_barrier(Node* c) const { 908 return ShenandoahBarrierNode::skip_through_barrier(c); 909 } 910 911 bool ShenandoahBarrierSetC2::expand_barriers(Compile* C, PhaseIterGVN& igvn) const { 912 return !ShenandoahWriteBarrierNode::expand(C, igvn); 913 } 914 915 bool ShenandoahBarrierSetC2::optimize_loops(PhaseIdealLoop* phase, LoopOptsMode mode, VectorSet& visited, Node_Stack& nstack, Node_List& worklist) const { 916 if (mode == LoopOptsShenandoahExpand) { 917 assert(UseShenandoahGC, "only for shenandoah"); 918 ShenandoahWriteBarrierNode::pin_and_expand(phase); 919 return true; 920 } else if (mode == LoopOptsShenandoahPostExpand) { 921 assert(UseShenandoahGC, "only for shenandoah"); 922 visited.Clear(); 923 ShenandoahWriteBarrierNode::optimize_after_expansion(visited, nstack, worklist, phase); 924 return true; 925 } 926 GrowableArray<MemoryGraphFixer*> memory_graph_fixers; 927 ShenandoahWriteBarrierNode::optimize_before_expansion(phase, memory_graph_fixers, false); 928 return false; 929 } 930 931 bool ShenandoahBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, bool is_clone, ArrayCopyPhase phase) const { 932 bool is_oop = type == T_OBJECT || type == T_ARRAY; 933 if (!is_oop) { 934 return false; 935 } 936 937 if (tightly_coupled_alloc) { 938 if (phase == Optimization) { 939 return false; 940 } 941 return !is_clone; 942 } 943 if (phase == Optimization) { 944 return !ShenandoahStoreValEnqueueBarrier; 945 } 946 return true; 947 } 948 949 bool ShenandoahBarrierSetC2::clone_needs_postbarrier(ArrayCopyNode *ac, PhaseIterGVN& igvn) { 950 Node* src = ac->in(ArrayCopyNode::Src); 951 const TypeOopPtr* src_type = igvn.type(src)->is_oopptr(); 952 if (src_type->isa_instptr() != NULL) { 953 ciInstanceKlass* ik = src_type->klass()->as_instance_klass(); 954 if ((src_type->klass_is_exact() || (!ik->is_interface() && !ik->has_subklass())) && !ik->has_injected_fields()) { 955 if (ik->has_object_fields()) { 956 return true; 957 } else { 958 if (!src_type->klass_is_exact()) { 959 igvn.C->dependencies()->assert_leaf_type(ik); 960 } 961 } 962 } else { 963 return true; 964 } 965 } else if (src_type->isa_aryptr()) { 966 BasicType src_elem = src_type->klass()->as_array_klass()->element_type()->basic_type(); 967 if (src_elem == T_OBJECT || src_elem == T_ARRAY) { 968 return true; 969 } 970 } else { 971 return true; 972 } 973 return false; 974 } 975 976 void ShenandoahBarrierSetC2::clone_barrier_at_expansion(ArrayCopyNode* ac, Node* call, PhaseIterGVN& igvn) const { 977 assert(ac->is_clonebasic(), "no other kind of arraycopy here"); 978 979 if (!clone_needs_postbarrier(ac, igvn)) { 980 BarrierSetC2::clone_barrier_at_expansion(ac, call, igvn); 981 return; 982 } 983 984 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM; 985 Node* c = new ProjNode(call,TypeFunc::Control); 986 c = igvn.transform(c); 987 Node* m = new ProjNode(call, TypeFunc::Memory); 988 m = igvn.transform(m); 989 990 Node* dest = ac->in(ArrayCopyNode::Dest); 991 assert(dest->is_AddP(), "bad input"); 992 Node* barrier_call = new CallLeafNode(ShenandoahBarrierSetC2::shenandoah_clone_barrier_Type(), 993 CAST_FROM_FN_PTR(address, ShenandoahRuntime::shenandoah_clone_barrier), 994 "shenandoah_clone_barrier", raw_adr_type); 995 barrier_call->init_req(TypeFunc::Control, c); 996 barrier_call->init_req(TypeFunc::I_O , igvn.C->top()); 997 barrier_call->init_req(TypeFunc::Memory , m); 998 barrier_call->init_req(TypeFunc::ReturnAdr, igvn.C->top()); 999 barrier_call->init_req(TypeFunc::FramePtr, igvn.C->top()); 1000 barrier_call->init_req(TypeFunc::Parms+0, dest->in(AddPNode::Base)); 1001 1002 barrier_call = igvn.transform(barrier_call); 1003 c = new ProjNode(barrier_call,TypeFunc::Control); 1004 c = igvn.transform(c); 1005 m = new ProjNode(barrier_call, TypeFunc::Memory); 1006 m = igvn.transform(m); 1007 1008 Node* out_c = ac->proj_out(TypeFunc::Control); 1009 Node* out_m = ac->proj_out(TypeFunc::Memory); 1010 igvn.replace_node(out_c, c); 1011 igvn.replace_node(out_m, m); 1012 } 1013 1014 1015 // Support for macro expanded GC barriers 1016 void ShenandoahBarrierSetC2::register_potential_barrier_node(Node* node) const { 1017 if (node->Opcode() == Op_ShenandoahWriteBarrier) { 1018 state()->add_shenandoah_barrier((ShenandoahWriteBarrierNode*) node); 1019 } 1020 } 1021 1022 void ShenandoahBarrierSetC2::unregister_potential_barrier_node(Node* node) const { 1023 if (node->Opcode() == Op_ShenandoahWriteBarrier) { 1024 state()->remove_shenandoah_barrier((ShenandoahWriteBarrierNode*) node); 1025 } 1026 } 1027 1028 void ShenandoahBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* n) const { 1029 if (is_shenandoah_wb_pre_call(n)) { 1030 shenandoah_eliminate_wb_pre(n, ¯o->igvn()); 1031 } 1032 } 1033 1034 void ShenandoahBarrierSetC2::shenandoah_eliminate_wb_pre(Node* call, PhaseIterGVN* igvn) const { 1035 assert(UseShenandoahGC && is_shenandoah_wb_pre_call(call), ""); 1036 Node* c = call->as_Call()->proj_out(TypeFunc::Control); 1037 c = c->unique_ctrl_out(); 1038 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 1039 c = c->unique_ctrl_out(); 1040 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 1041 Node* iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 1042 assert(iff->is_If(), "expect test"); 1043 if (!is_shenandoah_marking_if(igvn, iff)) { 1044 c = c->unique_ctrl_out(); 1045 assert(c->is_Region() && c->req() == 3, "where's the pre barrier control flow?"); 1046 iff = c->in(1)->is_IfProj() ? c->in(1)->in(0) : c->in(2)->in(0); 1047 assert(is_shenandoah_marking_if(igvn, iff), "expect marking test"); 1048 } 1049 Node* cmpx = iff->in(1)->in(1); 1050 igvn->replace_node(cmpx, igvn->makecon(TypeInt::CC_EQ)); 1051 igvn->rehash_node_delayed(call); 1052 call->del_req(call->req()-1); 1053 } 1054 1055 void ShenandoahBarrierSetC2::enqueue_useful_gc_barrier(PhaseIterGVN* igvn, Node* node) const { 1056 if (node->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(node)) { 1057 igvn->add_users_to_worklist(node); 1058 } 1059 } 1060 1061 void ShenandoahBarrierSetC2::eliminate_useless_gc_barriers(Unique_Node_List &useful, Compile* C) const { 1062 for (uint i = 0; i < useful.size(); i++) { 1063 Node* n = useful.at(i); 1064 if (n->Opcode() == Op_AddP && ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(n)) { 1065 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1066 C->record_for_igvn(n->fast_out(i)); 1067 } 1068 } 1069 } 1070 for (int i = state()->shenandoah_barriers_count()-1; i >= 0; i--) { 1071 ShenandoahWriteBarrierNode* n = state()->shenandoah_barrier(i); 1072 if (!useful.member(n)) { 1073 state()->remove_shenandoah_barrier(n); 1074 } 1075 } 1076 1077 } 1078 1079 bool ShenandoahBarrierSetC2::has_special_unique_user(const Node* node) const { 1080 assert(node->outcnt() == 1, "match only for unique out"); 1081 Node* n = node->unique_out(); 1082 return node->Opcode() == Op_ShenandoahWriteBarrier && n->Opcode() == Op_ShenandoahWBMemProj; 1083 } 1084 1085 void ShenandoahBarrierSetC2::add_users_to_worklist(Unique_Node_List* worklist) const {} 1086 1087 void* ShenandoahBarrierSetC2::create_barrier_state(Arena* comp_arena) const { 1088 return new(comp_arena) ShenandoahBarrierSetC2State(comp_arena); 1089 } 1090 1091 ShenandoahBarrierSetC2State* ShenandoahBarrierSetC2::state() const { 1092 return reinterpret_cast<ShenandoahBarrierSetC2State*>(Compile::current()->barrier_set_state()); 1093 } 1094 1095 // If the BarrierSetC2 state has kept macro nodes in its compilation unit state to be 1096 // expanded later, then now is the time to do so. 1097 bool ShenandoahBarrierSetC2::expand_macro_nodes(PhaseMacroExpand* macro) const { return false; } 1098 1099 #ifdef ASSERT 1100 void ShenandoahBarrierSetC2::verify_gc_barriers(Compile* compile, CompilePhase phase) const { 1101 if (ShenandoahVerifyOptoBarriers && phase == BarrierSetC2::BeforeExpand) { 1102 ShenandoahBarrierNode::verify(Compile::current()->root()); 1103 } else if (phase == BarrierSetC2::BeforeCodeGen) { 1104 // Verify G1 pre-barriers 1105 const int marking_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_active_offset()); 1106 1107 ResourceArea *area = Thread::current()->resource_area(); 1108 Unique_Node_List visited(area); 1109 Node_List worklist(area); 1110 // We're going to walk control flow backwards starting from the Root 1111 worklist.push(compile->root()); 1112 while (worklist.size() > 0) { 1113 Node *x = worklist.pop(); 1114 if (x == NULL || x == compile->top()) continue; 1115 if (visited.member(x)) { 1116 continue; 1117 } else { 1118 visited.push(x); 1119 } 1120 1121 if (x->is_Region()) { 1122 for (uint i = 1; i < x->req(); i++) { 1123 worklist.push(x->in(i)); 1124 } 1125 } else { 1126 worklist.push(x->in(0)); 1127 // We are looking for the pattern: 1128 // /->ThreadLocal 1129 // If->Bool->CmpI->LoadB->AddP->ConL(marking_offset) 1130 // \->ConI(0) 1131 // We want to verify that the If and the LoadB have the same control 1132 // See GraphKit::g1_write_barrier_pre() 1133 if (x->is_If()) { 1134 IfNode *iff = x->as_If(); 1135 if (iff->in(1)->is_Bool() && iff->in(1)->in(1)->is_Cmp()) { 1136 CmpNode *cmp = iff->in(1)->in(1)->as_Cmp(); 1137 if (cmp->Opcode() == Op_CmpI && cmp->in(2)->is_Con() && cmp->in(2)->bottom_type()->is_int()->get_con() == 0 1138 && cmp->in(1)->is_Load()) { 1139 LoadNode *load = cmp->in(1)->as_Load(); 1140 if (load->Opcode() == Op_LoadB && load->in(2)->is_AddP() && load->in(2)->in(2)->Opcode() == Op_ThreadLocal 1141 && load->in(2)->in(3)->is_Con() 1142 && load->in(2)->in(3)->bottom_type()->is_intptr_t()->get_con() == marking_offset) { 1143 1144 Node *if_ctrl = iff->in(0); 1145 Node *load_ctrl = load->in(0); 1146 1147 if (if_ctrl != load_ctrl) { 1148 // Skip possible CProj->NeverBranch in infinite loops 1149 if ((if_ctrl->is_Proj() && if_ctrl->Opcode() == Op_CProj) 1150 && (if_ctrl->in(0)->is_MultiBranch() && if_ctrl->in(0)->Opcode() == Op_NeverBranch)) { 1151 if_ctrl = if_ctrl->in(0)->in(0); 1152 } 1153 } 1154 assert(load_ctrl != NULL && if_ctrl == load_ctrl, "controls must match"); 1155 } 1156 } 1157 } 1158 } 1159 } 1160 } 1161 } 1162 } 1163 #endif 1164 1165 Node* ShenandoahBarrierSetC2::ideal_node(PhaseGVN* phase, Node* n, bool can_reshape) const { 1166 if (is_shenandoah_wb_pre_call(n)) { 1167 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 1168 if (n->req() > cnt) { 1169 Node* addp = n->in(cnt); 1170 if (has_only_shenandoah_wb_pre_uses(addp)) { 1171 n->del_req(cnt); 1172 if (can_reshape) { 1173 phase->is_IterGVN()->_worklist.push(addp); 1174 } 1175 return n; 1176 } 1177 } 1178 } 1179 if (n->Opcode() == Op_CmpP) { 1180 Node* in1 = n->in(1); 1181 Node* in2 = n->in(2); 1182 if (in1->bottom_type() == TypePtr::NULL_PTR) { 1183 in2 = step_over_gc_barrier(in2); 1184 } 1185 if (in2->bottom_type() == TypePtr::NULL_PTR) { 1186 in1 = step_over_gc_barrier(in1); 1187 } 1188 PhaseIterGVN* igvn = phase->is_IterGVN(); 1189 if (in1 != n->in(1)) { 1190 if (igvn != NULL) { 1191 n->set_req_X(1, in1, igvn); 1192 } else { 1193 n->set_req(1, in1); 1194 } 1195 assert(in2 == n->in(2), "only one change"); 1196 return n; 1197 } 1198 if (in2 != n->in(2)) { 1199 if (igvn != NULL) { 1200 n->set_req_X(2, in2, igvn); 1201 } else { 1202 n->set_req(2, in2); 1203 } 1204 return n; 1205 } 1206 } else if (can_reshape && 1207 n->Opcode() == Op_If && 1208 ShenandoahWriteBarrierNode::is_heap_stable_test(n) && 1209 n->in(0) != NULL) { 1210 Node* dom = n->in(0); 1211 Node* prev_dom = n; 1212 int op = n->Opcode(); 1213 int dist = 16; 1214 // Search up the dominator tree for another heap stable test 1215 while (dom->Opcode() != op || // Not same opcode? 1216 !ShenandoahWriteBarrierNode::is_heap_stable_test(dom) || // Not same input 1? 1217 prev_dom->in(0) != dom) { // One path of test does not dominate? 1218 if (dist < 0) return NULL; 1219 1220 dist--; 1221 prev_dom = dom; 1222 dom = IfNode::up_one_dom(dom); 1223 if (!dom) return NULL; 1224 } 1225 1226 // Check that we did not follow a loop back to ourselves 1227 if (n == dom) { 1228 return NULL; 1229 } 1230 1231 return n->as_If()->dominated_by(prev_dom, phase->is_IterGVN()); 1232 } 1233 1234 return NULL; 1235 } 1236 1237 Node* ShenandoahBarrierSetC2::identity_node(PhaseGVN* phase, Node* n) const { 1238 if (n->is_Load()) { 1239 Node *mem = n->in(MemNode::Memory); 1240 Node *value = n->as_Load()->can_see_stored_value(mem, phase); 1241 if (value) { 1242 PhaseIterGVN *igvn = phase->is_IterGVN(); 1243 if (igvn != NULL && 1244 value->is_Phi() && 1245 value->req() > 2 && 1246 value->in(1) != NULL && 1247 value->in(1)->is_ShenandoahBarrier()) { 1248 if (igvn->_worklist.member(value) || 1249 igvn->_worklist.member(value->in(0)) || 1250 (value->in(0)->in(1) != NULL && 1251 value->in(0)->in(1)->is_IfProj() && 1252 (igvn->_worklist.member(value->in(0)->in(1)) || 1253 (value->in(0)->in(1)->in(0) != NULL && 1254 igvn->_worklist.member(value->in(0)->in(1)->in(0)))))) { 1255 igvn->_worklist.push(n); 1256 return n; 1257 } 1258 } 1259 // (This works even when value is a Con, but LoadNode::Value 1260 // usually runs first, producing the singleton type of the Con.) 1261 Node *value_no_barrier = step_over_gc_barrier(value->Opcode() == Op_EncodeP ? value->in(1) : value); 1262 if (value->Opcode() == Op_EncodeP) { 1263 if (value_no_barrier != value->in(1)) { 1264 Node *encode = value->clone(); 1265 encode->set_req(1, value_no_barrier); 1266 encode = phase->transform(encode); 1267 return encode; 1268 } 1269 } else { 1270 return value_no_barrier; 1271 } 1272 } 1273 } 1274 return n; 1275 } 1276 1277 bool ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(Node* n) { 1278 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1279 Node* u = n->fast_out(i); 1280 if (!is_shenandoah_wb_pre_call(u)) { 1281 return false; 1282 } 1283 } 1284 return n->outcnt() > 0; 1285 } 1286 1287 bool ShenandoahBarrierSetC2::flatten_gc_alias_type(const TypePtr*& adr_type) const { 1288 int offset = adr_type->offset(); 1289 if (offset == ShenandoahBrooksPointer::byte_offset()) { 1290 if (adr_type->isa_aryptr()) { 1291 adr_type = TypeAryPtr::make(adr_type->ptr(), adr_type->isa_aryptr()->ary(), adr_type->isa_aryptr()->klass(), false, offset); 1292 } else if (adr_type->isa_instptr()) { 1293 adr_type = TypeInstPtr::make(adr_type->ptr(), ciEnv::current()->Object_klass(), false, NULL, offset); 1294 } 1295 return true; 1296 } else { 1297 return false; 1298 } 1299 } 1300 1301 bool ShenandoahBarrierSetC2::final_graph_reshaping(Compile* compile, Node* n, uint opcode) const { 1302 switch (opcode) { 1303 case Op_CallLeaf: 1304 case Op_CallLeafNoFP: { 1305 assert (n->is_Call(), ""); 1306 CallNode *call = n->as_Call(); 1307 if (ShenandoahBarrierSetC2::is_shenandoah_wb_pre_call(call)) { 1308 uint cnt = ShenandoahBarrierSetC2::write_ref_field_pre_entry_Type()->domain()->cnt(); 1309 if (call->req() > cnt) { 1310 assert(call->req() == cnt + 1, "only one extra input"); 1311 Node *addp = call->in(cnt); 1312 assert(!ShenandoahBarrierSetC2::has_only_shenandoah_wb_pre_uses(addp), "useless address computation?"); 1313 call->del_req(cnt); 1314 } 1315 } 1316 return false; 1317 } 1318 case Op_ShenandoahCompareAndSwapP: 1319 case Op_ShenandoahCompareAndSwapN: 1320 case Op_ShenandoahWeakCompareAndSwapN: 1321 case Op_ShenandoahWeakCompareAndSwapP: 1322 case Op_ShenandoahCompareAndExchangeP: 1323 case Op_ShenandoahCompareAndExchangeN: 1324 #ifdef ASSERT 1325 if( VerifyOptoOopOffsets ) { 1326 MemNode* mem = n->as_Mem(); 1327 // Check to see if address types have grounded out somehow. 1328 const TypeInstPtr *tp = mem->in(MemNode::Address)->bottom_type()->isa_instptr(); 1329 ciInstanceKlass *k = tp->klass()->as_instance_klass(); 1330 bool oop_offset_is_sane = k->contains_field_offset(tp->offset()); 1331 assert( !tp || oop_offset_is_sane, "" ); 1332 } 1333 #endif 1334 return true; 1335 case Op_ShenandoahReadBarrier: 1336 return true; 1337 case Op_ShenandoahWriteBarrier: 1338 assert(false, "should have been expanded already"); 1339 return true; 1340 default: 1341 return false; 1342 } 1343 } 1344 1345 #ifdef ASSERT 1346 bool ShenandoahBarrierSetC2::verify_gc_alias_type(const TypePtr* adr_type, int offset) const { 1347 if (offset == ShenandoahBrooksPointer::byte_offset() && 1348 (adr_type->base() == Type::AryPtr || adr_type->base() == Type::OopPtr)) { 1349 return true; 1350 } else { 1351 return false; 1352 } 1353 } 1354 #endif 1355 1356 bool ShenandoahBarrierSetC2::escape_add_to_con_graph(ConnectionGraph* conn_graph, PhaseGVN* gvn, Unique_Node_List* delayed_worklist, Node* n, uint opcode) const { 1357 switch (opcode) { 1358 case Op_ShenandoahCompareAndExchangeP: 1359 case Op_ShenandoahCompareAndExchangeN: 1360 conn_graph->add_objload_to_connection_graph(n, delayed_worklist); 1361 // fallthrough 1362 case Op_ShenandoahWeakCompareAndSwapP: 1363 case Op_ShenandoahWeakCompareAndSwapN: 1364 case Op_ShenandoahCompareAndSwapP: 1365 case Op_ShenandoahCompareAndSwapN: 1366 conn_graph->add_to_congraph_unsafe_access(n, opcode, delayed_worklist); 1367 return true; 1368 case Op_StoreP: { 1369 Node* adr = n->in(MemNode::Address); 1370 const Type* adr_type = gvn->type(adr); 1371 // Pointer stores in G1 barriers looks like unsafe access. 1372 // Ignore such stores to be able scalar replace non-escaping 1373 // allocations. 1374 if (adr_type->isa_rawptr() && adr->is_AddP()) { 1375 Node* base = conn_graph->get_addp_base(adr); 1376 if (base->Opcode() == Op_LoadP && 1377 base->in(MemNode::Address)->is_AddP()) { 1378 adr = base->in(MemNode::Address); 1379 Node* tls = conn_graph->get_addp_base(adr); 1380 if (tls->Opcode() == Op_ThreadLocal) { 1381 int offs = (int) gvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot); 1382 const int buf_offset = in_bytes(ShenandoahThreadLocalData::satb_mark_queue_buffer_offset()); 1383 if (offs == buf_offset) { 1384 return true; // Pre barrier previous oop value store. 1385 } 1386 } 1387 } 1388 } 1389 return false; 1390 } 1391 case Op_ShenandoahReadBarrier: 1392 case Op_ShenandoahWriteBarrier: 1393 // Barriers 'pass through' its arguments. I.e. what goes in, comes out. 1394 // It doesn't escape. 1395 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahBarrierNode::ValueIn), delayed_worklist); 1396 break; 1397 case Op_ShenandoahEnqueueBarrier: 1398 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), delayed_worklist); 1399 break; 1400 default: 1401 // Nothing 1402 break; 1403 } 1404 return false; 1405 } 1406 1407 bool ShenandoahBarrierSetC2::escape_add_final_edges(ConnectionGraph* conn_graph, PhaseGVN* gvn, Node* n, uint opcode) const { 1408 switch (opcode) { 1409 case Op_ShenandoahCompareAndExchangeP: 1410 case Op_ShenandoahCompareAndExchangeN: { 1411 Node *adr = n->in(MemNode::Address); 1412 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL); 1413 // fallthrough 1414 } 1415 case Op_ShenandoahCompareAndSwapP: 1416 case Op_ShenandoahCompareAndSwapN: 1417 case Op_ShenandoahWeakCompareAndSwapP: 1418 case Op_ShenandoahWeakCompareAndSwapN: 1419 return conn_graph->add_final_edges_unsafe_access(n, opcode); 1420 case Op_ShenandoahReadBarrier: 1421 case Op_ShenandoahWriteBarrier: 1422 // Barriers 'pass through' its arguments. I.e. what goes in, comes out. 1423 // It doesn't escape. 1424 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(ShenandoahBarrierNode::ValueIn), NULL); 1425 return true; 1426 case Op_ShenandoahEnqueueBarrier: 1427 conn_graph->add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(1), NULL); 1428 return true; 1429 default: 1430 // Nothing 1431 break; 1432 } 1433 return false; 1434 } 1435 1436 bool ShenandoahBarrierSetC2::escape_has_out_with_unsafe_object(Node* n) const { 1437 return n->has_out_with(Op_ShenandoahCompareAndExchangeP) || n->has_out_with(Op_ShenandoahCompareAndExchangeN) || 1438 n->has_out_with(Op_ShenandoahCompareAndSwapP, Op_ShenandoahCompareAndSwapN, Op_ShenandoahWeakCompareAndSwapP, Op_ShenandoahWeakCompareAndSwapN); 1439 1440 } 1441 1442 bool ShenandoahBarrierSetC2::escape_is_barrier_node(Node* n) const { 1443 return n->is_ShenandoahBarrier(); 1444 } 1445 1446 bool ShenandoahBarrierSetC2::matcher_find_shared_visit(Matcher* matcher, Matcher::MStack& mstack, Node* n, uint opcode, bool& mem_op, int& mem_addr_idx) const { 1447 switch (opcode) { 1448 case Op_ShenandoahReadBarrier: 1449 if (n->in(ShenandoahBarrierNode::ValueIn)->is_DecodeNarrowPtr()) { 1450 matcher->set_shared(n->in(ShenandoahBarrierNode::ValueIn)->in(1)); 1451 } 1452 matcher->set_shared(n); 1453 return true; 1454 default: 1455 break; 1456 } 1457 return false; 1458 } 1459 1460 bool ShenandoahBarrierSetC2::matcher_find_shared_post_visit(Matcher* matcher, Node* n, uint opcode) const { 1461 switch (opcode) { 1462 case Op_ShenandoahCompareAndExchangeP: 1463 case Op_ShenandoahCompareAndExchangeN: 1464 case Op_ShenandoahWeakCompareAndSwapP: 1465 case Op_ShenandoahWeakCompareAndSwapN: 1466 case Op_ShenandoahCompareAndSwapP: 1467 case Op_ShenandoahCompareAndSwapN: { // Convert trinary to binary-tree 1468 Node* newval = n->in(MemNode::ValueIn); 1469 Node* oldval = n->in(LoadStoreConditionalNode::ExpectedIn); 1470 Node* pair = new BinaryNode(oldval, newval); 1471 n->set_req(MemNode::ValueIn,pair); 1472 n->del_req(LoadStoreConditionalNode::ExpectedIn); 1473 return true; 1474 } 1475 default: 1476 break; 1477 } 1478 return false; 1479 } 1480 1481 bool ShenandoahBarrierSetC2::matcher_is_store_load_barrier(Node* x, uint xop) const { 1482 return xop == Op_ShenandoahCompareAndExchangeP || 1483 xop == Op_ShenandoahCompareAndExchangeN || 1484 xop == Op_ShenandoahWeakCompareAndSwapP || 1485 xop == Op_ShenandoahWeakCompareAndSwapN || 1486 xop == Op_ShenandoahCompareAndSwapN || 1487 xop == Op_ShenandoahCompareAndSwapP; 1488 } 1489 1490 void ShenandoahBarrierSetC2::igvn_add_users_to_worklist(PhaseIterGVN* igvn, Node* use) const { 1491 if (use->is_ShenandoahBarrier()) { 1492 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1493 Node* u = use->fast_out(i2); 1494 Node* cmp = use->find_out_with(Op_CmpP); 1495 if (u->Opcode() == Op_CmpP) { 1496 igvn->_worklist.push(cmp); 1497 } 1498 } 1499 } 1500 } 1501 1502 void ShenandoahBarrierSetC2::ccp_analyze(PhaseCCP* ccp, Unique_Node_List& worklist, Node* use) const { 1503 if (use->is_ShenandoahBarrier()) { 1504 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1505 Node* p = use->fast_out(i2); 1506 if (p->Opcode() == Op_AddP) { 1507 for (DUIterator_Fast i3max, i3 = p->fast_outs(i3max); i3 < i3max; i3++) { 1508 Node* q = p->fast_out(i3); 1509 if (q->is_Load()) { 1510 if(q->bottom_type() != ccp->type(q)) { 1511 worklist.push(q); 1512 } 1513 } 1514 } 1515 } 1516 } 1517 } 1518 } 1519 1520 Node* ShenandoahBarrierSetC2::split_if_pre(PhaseIdealLoop* phase, Node* n) const { 1521 if (n->Opcode() == Op_ShenandoahReadBarrier) { 1522 ((ShenandoahReadBarrierNode*)n)->try_move(phase); 1523 } else if (n->Opcode() == Op_ShenandoahWriteBarrier) { 1524 return ((ShenandoahWriteBarrierNode*)n)->try_split_thru_phi(phase); 1525 } 1526 1527 return NULL; 1528 } 1529 1530 bool ShenandoahBarrierSetC2::build_loop_late_post(PhaseIdealLoop* phase, Node* n) const { 1531 return ShenandoahBarrierNode::build_loop_late_post(phase, n); 1532 } 1533 1534 bool ShenandoahBarrierSetC2::sink_node(PhaseIdealLoop* phase, Node* n, Node* x, Node* x_ctrl, Node* n_ctrl) const { 1535 if (n->is_ShenandoahBarrier()) { 1536 return x->as_ShenandoahBarrier()->sink_node(phase, x_ctrl, n_ctrl); 1537 } 1538 if (n->is_MergeMem()) { 1539 // PhaseIdealLoop::split_if_with_blocks_post() would: 1540 // _igvn._worklist.yank(x); 1541 // which sometimes causes chains of MergeMem which some of 1542 // shenandoah specific code doesn't support 1543 phase->register_new_node(x, x_ctrl); 1544 return true; 1545 } 1546 return false; 1547 }