1 /* 2 * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "asm/macroAssembler.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "gc/z/c2/zBarrierSetC2.hpp" 28 #include "gc/z/zBarrierSet.hpp" 29 #include "gc/z/zBarrierSetAssembler.hpp" 30 #include "gc/z/zBarrierSetRuntime.hpp" 31 #include "opto/arraycopynode.hpp" 32 #include "opto/addnode.hpp" 33 #include "opto/block.hpp" 34 #include "opto/compile.hpp" 35 #include "opto/graphKit.hpp" 36 #include "opto/machnode.hpp" 37 #include "opto/macro.hpp" 38 #include "opto/memnode.hpp" 39 #include "opto/node.hpp" 40 #include "opto/output.hpp" 41 #include "opto/regalloc.hpp" 42 #include "opto/rootnode.hpp" 43 #include "opto/runtime.hpp" 44 #include "opto/type.hpp" 45 #include "utilities/debug.hpp" 46 #include "utilities/growableArray.hpp" 47 #include "utilities/macros.hpp" 48 49 template<typename K, typename V, size_t _table_size> 50 class ZArenaHashtable : public ResourceObj { 51 class ZArenaHashtableEntry : public ResourceObj { 52 public: 53 ZArenaHashtableEntry* _next; 54 K _key; 55 V _value; 56 }; 57 58 static const size_t _table_mask = _table_size - 1; 59 60 Arena* _arena; 61 ZArenaHashtableEntry* _table[_table_size]; 62 63 public: 64 class Iterator { 65 ZArenaHashtable* _table; 66 ZArenaHashtableEntry* _current_entry; 67 size_t _current_index; 68 69 public: 70 Iterator(ZArenaHashtable* table) 71 : _table(table), 72 _current_entry(table->_table[0]), 73 _current_index(0) { 74 if (_current_entry == nullptr) { 75 next(); 76 } 77 } 78 79 bool has_next() { return _current_entry != nullptr; } 80 K key() { return _current_entry->_key; } 81 V value() { return _current_entry->_value; } 82 83 void next() { 84 if (_current_entry != nullptr) { 85 _current_entry = _current_entry->_next; 86 } 87 while (_current_entry == nullptr && ++_current_index < _table_size) { 88 _current_entry = _table->_table[_current_index]; 89 } 90 } 91 }; 92 93 ZArenaHashtable(Arena* arena) 94 : _arena(arena), 95 _table() { 96 Copy::zero_to_bytes(&_table, sizeof(_table)); 97 } 98 99 void add(K key, V value) { 100 ZArenaHashtableEntry* entry = new (_arena) ZArenaHashtableEntry(); 101 entry->_key = key; 102 entry->_value = value; 103 entry->_next = _table[key & _table_mask]; 104 _table[key & _table_mask] = entry; 105 } 106 107 V* get(K key) const { 108 for (ZArenaHashtableEntry* e = _table[key & _table_mask]; e != nullptr; e = e->_next) { 109 if (e->_key == key) { 110 return &(e->_value); 111 } 112 } 113 return nullptr; 114 } 115 116 Iterator iterator() { 117 return Iterator(this); 118 } 119 }; 120 121 typedef ZArenaHashtable<intptr_t, bool, 4> ZOffsetTable; 122 123 class ZBarrierSetC2State : public BarrierSetC2State { 124 private: 125 GrowableArray<ZBarrierStubC2*>* _stubs; 126 int _trampoline_stubs_count; 127 int _stubs_start_offset; 128 129 public: 130 ZBarrierSetC2State(Arena* arena) 131 : BarrierSetC2State(arena), 132 _stubs(new (arena) GrowableArray<ZBarrierStubC2*>(arena, 8, 0, nullptr)), 133 _trampoline_stubs_count(0), 134 _stubs_start_offset(0) {} 135 136 GrowableArray<ZBarrierStubC2*>* stubs() { 137 return _stubs; 138 } 139 140 bool needs_liveness_data(const MachNode* mach) const { 141 // Don't need liveness data for nodes without barriers 142 return mach->barrier_data() != ZBarrierElided; 143 } 144 145 bool needs_livein_data() const { 146 return true; 147 } 148 149 void inc_trampoline_stubs_count() { 150 assert(_trampoline_stubs_count != INT_MAX, "Overflow"); 151 ++_trampoline_stubs_count; 152 } 153 154 int trampoline_stubs_count() { 155 return _trampoline_stubs_count; 156 } 157 158 void set_stubs_start_offset(int offset) { 159 _stubs_start_offset = offset; 160 } 161 162 int stubs_start_offset() { 163 return _stubs_start_offset; 164 } 165 }; 166 167 static ZBarrierSetC2State* barrier_set_state() { 168 return reinterpret_cast<ZBarrierSetC2State*>(Compile::current()->barrier_set_state()); 169 } 170 171 void ZBarrierStubC2::register_stub(ZBarrierStubC2* stub) { 172 if (!Compile::current()->output()->in_scratch_emit_size()) { 173 barrier_set_state()->stubs()->append(stub); 174 } 175 } 176 177 void ZBarrierStubC2::inc_trampoline_stubs_count() { 178 if (!Compile::current()->output()->in_scratch_emit_size()) { 179 barrier_set_state()->inc_trampoline_stubs_count(); 180 } 181 } 182 183 int ZBarrierStubC2::trampoline_stubs_count() { 184 return barrier_set_state()->trampoline_stubs_count(); 185 } 186 187 int ZBarrierStubC2::stubs_start_offset() { 188 return barrier_set_state()->stubs_start_offset(); 189 } 190 191 ZBarrierStubC2::ZBarrierStubC2(const MachNode* node) : BarrierStubC2(node) {} 192 193 ZLoadBarrierStubC2* ZLoadBarrierStubC2::create(const MachNode* node, Address ref_addr, Register ref) { 194 AARCH64_ONLY(fatal("Should use ZLoadBarrierStubC2Aarch64::create")); 195 ZLoadBarrierStubC2* const stub = new (Compile::current()->comp_arena()) ZLoadBarrierStubC2(node, ref_addr, ref); 196 register_stub(stub); 197 198 return stub; 199 } 200 201 ZLoadBarrierStubC2::ZLoadBarrierStubC2(const MachNode* node, Address ref_addr, Register ref) 202 : ZBarrierStubC2(node), 203 _ref_addr(ref_addr), 204 _ref(ref) { 205 assert_different_registers(ref, ref_addr.base()); 206 assert_different_registers(ref, ref_addr.index()); 207 // The runtime call updates the value of ref, so we should not spill and 208 // reload its outdated value. 209 dont_preserve(ref); 210 } 211 212 Address ZLoadBarrierStubC2::ref_addr() const { 213 return _ref_addr; 214 } 215 216 Register ZLoadBarrierStubC2::ref() const { 217 return _ref; 218 } 219 220 address ZLoadBarrierStubC2::slow_path() const { 221 const uint8_t barrier_data = _node->barrier_data(); 222 DecoratorSet decorators = DECORATORS_NONE; 223 if (barrier_data & ZBarrierStrong) { 224 decorators |= ON_STRONG_OOP_REF; 225 } 226 if (barrier_data & ZBarrierWeak) { 227 decorators |= ON_WEAK_OOP_REF; 228 } 229 if (barrier_data & ZBarrierPhantom) { 230 decorators |= ON_PHANTOM_OOP_REF; 231 } 232 if (barrier_data & ZBarrierNoKeepalive) { 233 decorators |= AS_NO_KEEPALIVE; 234 } 235 return ZBarrierSetRuntime::load_barrier_on_oop_field_preloaded_addr(decorators); 236 } 237 238 void ZLoadBarrierStubC2::emit_code(MacroAssembler& masm) { 239 ZBarrierSet::assembler()->generate_c2_load_barrier_stub(&masm, static_cast<ZLoadBarrierStubC2*>(this)); 240 } 241 242 ZStoreBarrierStubC2* ZStoreBarrierStubC2::create(const MachNode* node, Address ref_addr, Register new_zaddress, Register new_zpointer, bool is_native, bool is_atomic) { 243 AARCH64_ONLY(fatal("Should use ZStoreBarrierStubC2Aarch64::create")); 244 ZStoreBarrierStubC2* const stub = new (Compile::current()->comp_arena()) ZStoreBarrierStubC2(node, ref_addr, new_zaddress, new_zpointer, is_native, is_atomic); 245 register_stub(stub); 246 247 return stub; 248 } 249 250 ZStoreBarrierStubC2::ZStoreBarrierStubC2(const MachNode* node, Address ref_addr, Register new_zaddress, Register new_zpointer, bool is_native, bool is_atomic) 251 : ZBarrierStubC2(node), 252 _ref_addr(ref_addr), 253 _new_zaddress(new_zaddress), 254 _new_zpointer(new_zpointer), 255 _is_native(is_native), 256 _is_atomic(is_atomic) {} 257 258 Address ZStoreBarrierStubC2::ref_addr() const { 259 return _ref_addr; 260 } 261 262 Register ZStoreBarrierStubC2::new_zaddress() const { 263 return _new_zaddress; 264 } 265 266 Register ZStoreBarrierStubC2::new_zpointer() const { 267 return _new_zpointer; 268 } 269 270 bool ZStoreBarrierStubC2::is_native() const { 271 return _is_native; 272 } 273 274 bool ZStoreBarrierStubC2::is_atomic() const { 275 return _is_atomic; 276 } 277 278 void ZStoreBarrierStubC2::emit_code(MacroAssembler& masm) { 279 ZBarrierSet::assembler()->generate_c2_store_barrier_stub(&masm, static_cast<ZStoreBarrierStubC2*>(this)); 280 } 281 282 uint ZBarrierSetC2::estimated_barrier_size(const Node* node) const { 283 uint8_t barrier_data = MemNode::barrier_data(node); 284 assert(barrier_data != 0, "should be a barrier node"); 285 uint uncolor_or_color_size = node->is_Load() ? 1 : 2; 286 if ((barrier_data & ZBarrierElided) != 0) { 287 return uncolor_or_color_size; 288 } 289 // A compare and branch corresponds to approximately four fast-path Ideal 290 // nodes (Cmp, Bool, If, If projection). The slow path (If projection and 291 // runtime call) is excluded since the corresponding code is laid out 292 // separately and does not directly affect performance. 293 return uncolor_or_color_size + 4; 294 } 295 296 void* ZBarrierSetC2::create_barrier_state(Arena* comp_arena) const { 297 return new (comp_arena) ZBarrierSetC2State(comp_arena); 298 } 299 300 void ZBarrierSetC2::late_barrier_analysis() const { 301 compute_liveness_at_stubs(); 302 analyze_dominating_barriers(); 303 } 304 305 void ZBarrierSetC2::emit_stubs(CodeBuffer& cb) const { 306 MacroAssembler masm(&cb); 307 GrowableArray<ZBarrierStubC2*>* const stubs = barrier_set_state()->stubs(); 308 barrier_set_state()->set_stubs_start_offset(masm.offset()); 309 310 for (int i = 0; i < stubs->length(); i++) { 311 // Make sure there is enough space in the code buffer 312 if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::MAX_inst_size) && cb.blob() == nullptr) { 313 ciEnv::current()->record_failure("CodeCache is full"); 314 return; 315 } 316 317 stubs->at(i)->emit_code(masm); 318 } 319 320 masm.flush(); 321 } 322 323 int ZBarrierSetC2::estimate_stub_size() const { 324 Compile* const C = Compile::current(); 325 BufferBlob* const blob = C->output()->scratch_buffer_blob(); 326 GrowableArray<ZBarrierStubC2*>* const stubs = barrier_set_state()->stubs(); 327 int size = 0; 328 329 for (int i = 0; i < stubs->length(); i++) { 330 CodeBuffer cb(blob->content_begin(), (address)C->output()->scratch_locs_memory() - blob->content_begin()); 331 MacroAssembler masm(&cb); 332 stubs->at(i)->emit_code(masm); 333 size += cb.insts_size(); 334 } 335 336 return size; 337 } 338 339 static void set_barrier_data(C2Access& access) { 340 if (!ZBarrierSet::barrier_needed(access.decorators(), access.type())) { 341 return; 342 } 343 344 if (access.decorators() & C2_TIGHTLY_COUPLED_ALLOC) { 345 access.set_barrier_data(ZBarrierElided); 346 return; 347 } 348 349 uint8_t barrier_data = 0; 350 351 if (access.decorators() & ON_PHANTOM_OOP_REF) { 352 barrier_data |= ZBarrierPhantom; 353 } else if (access.decorators() & ON_WEAK_OOP_REF) { 354 barrier_data |= ZBarrierWeak; 355 } else { 356 barrier_data |= ZBarrierStrong; 357 } 358 359 if (access.decorators() & IN_NATIVE) { 360 barrier_data |= ZBarrierNative; 361 } 362 363 if (access.decorators() & AS_NO_KEEPALIVE) { 364 barrier_data |= ZBarrierNoKeepalive; 365 } 366 367 access.set_barrier_data(barrier_data); 368 } 369 370 Node* ZBarrierSetC2::store_at_resolved(C2Access& access, C2AccessValue& val) const { 371 set_barrier_data(access); 372 return BarrierSetC2::store_at_resolved(access, val); 373 } 374 375 Node* ZBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const { 376 set_barrier_data(access); 377 return BarrierSetC2::load_at_resolved(access, val_type); 378 } 379 380 Node* ZBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 381 Node* new_val, const Type* val_type) const { 382 set_barrier_data(access); 383 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, val_type); 384 } 385 386 Node* ZBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 387 Node* new_val, const Type* value_type) const { 388 set_barrier_data(access); 389 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type); 390 } 391 392 Node* ZBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const { 393 set_barrier_data(access); 394 return BarrierSetC2::atomic_xchg_at_resolved(access, new_val, val_type); 395 } 396 397 bool ZBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, 398 bool is_clone, bool is_clone_instance, 399 ArrayCopyPhase phase) const { 400 if (phase == ArrayCopyPhase::Parsing) { 401 return false; 402 } 403 if (phase == ArrayCopyPhase::Optimization) { 404 return is_clone_instance; 405 } 406 // else ArrayCopyPhase::Expansion 407 return type == T_OBJECT || type == T_ARRAY; 408 } 409 410 #define XTOP LP64_ONLY(COMMA phase->top()) 411 412 void ZBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const { 413 Node* const src = ac->in(ArrayCopyNode::Src); 414 const TypeAryPtr* const ary_ptr = src->get_ptr_type()->isa_aryptr(); 415 416 if (ac->is_clone_array() && ary_ptr != nullptr) { 417 BasicType bt = ary_ptr->elem()->array_element_basic_type(); 418 if (is_reference_type(bt)) { 419 // Clone object array 420 bt = T_OBJECT; 421 } else { 422 // Clone primitive array 423 bt = T_LONG; 424 } 425 426 Node* const ctrl = ac->in(TypeFunc::Control); 427 Node* const mem = ac->in(TypeFunc::Memory); 428 Node* const src = ac->in(ArrayCopyNode::Src); 429 Node* src_offset = ac->in(ArrayCopyNode::SrcPos); 430 Node* const dest = ac->in(ArrayCopyNode::Dest); 431 Node* dest_offset = ac->in(ArrayCopyNode::DestPos); 432 Node* length = ac->in(ArrayCopyNode::Length); 433 434 if (bt == T_OBJECT) { 435 // BarrierSetC2::clone sets the offsets via BarrierSetC2::arraycopy_payload_base_offset 436 // which 8-byte aligns them to allow for word size copies. Make sure the offsets point 437 // to the first element in the array when cloning object arrays. Otherwise, load 438 // barriers are applied to parts of the header. Also adjust the length accordingly. 439 assert(src_offset == dest_offset, "should be equal"); 440 const jlong offset = src_offset->get_long(); 441 if (offset != arrayOopDesc::base_offset_in_bytes(T_OBJECT)) { 442 assert(!UseCompressedClassPointers, "should only happen without compressed class pointers"); 443 assert((arrayOopDesc::base_offset_in_bytes(T_OBJECT) - offset) == BytesPerLong, "unexpected offset"); 444 length = phase->transform_later(new SubLNode(length, phase->longcon(1))); // Size is in longs 445 src_offset = phase->longcon(arrayOopDesc::base_offset_in_bytes(T_OBJECT)); 446 dest_offset = src_offset; 447 } 448 } 449 Node* const payload_src = phase->basic_plus_adr(src, src_offset); 450 Node* const payload_dst = phase->basic_plus_adr(dest, dest_offset); 451 452 const char* copyfunc_name = "arraycopy"; 453 const address copyfunc_addr = phase->basictype2arraycopy(bt, nullptr, nullptr, true, copyfunc_name, true); 454 455 const TypePtr* const raw_adr_type = TypeRawPtr::BOTTOM; 456 const TypeFunc* const call_type = OptoRuntime::fast_arraycopy_Type(); 457 458 Node* const call = phase->make_leaf_call(ctrl, mem, call_type, copyfunc_addr, copyfunc_name, raw_adr_type, payload_src, payload_dst, length XTOP); 459 phase->transform_later(call); 460 461 phase->igvn().replace_node(ac, call); 462 return; 463 } 464 465 // Clone instance or array where 'src' is only known to be an object (ary_ptr 466 // is null). This can happen in bytecode generated dynamically to implement 467 // reflective array clones. 468 clone_in_runtime(phase, ac, ZBarrierSetRuntime::clone_addr(), "ZBarrierSetRuntime::clone"); 469 } 470 471 #undef XTOP 472 473 // == Dominating barrier elision == 474 475 static bool block_has_safepoint(const Block* block, uint from, uint to) { 476 for (uint i = from; i < to; i++) { 477 if (block->get_node(i)->is_MachSafePoint()) { 478 // Safepoint found 479 return true; 480 } 481 } 482 483 // Safepoint not found 484 return false; 485 } 486 487 static bool block_has_safepoint(const Block* block) { 488 return block_has_safepoint(block, 0, block->number_of_nodes()); 489 } 490 491 static uint block_index(const Block* block, const Node* node) { 492 for (uint j = 0; j < block->number_of_nodes(); ++j) { 493 if (block->get_node(j) == node) { 494 return j; 495 } 496 } 497 ShouldNotReachHere(); 498 return 0; 499 } 500 501 // Look through various node aliases 502 static const Node* look_through_node(const Node* node) { 503 while (node != nullptr) { 504 const Node* new_node = node; 505 if (node->is_Mach()) { 506 const MachNode* const node_mach = node->as_Mach(); 507 if (node_mach->ideal_Opcode() == Op_CheckCastPP) { 508 new_node = node->in(1); 509 } 510 if (node_mach->is_SpillCopy()) { 511 new_node = node->in(1); 512 } 513 } 514 if (new_node == node || new_node == nullptr) { 515 break; 516 } else { 517 node = new_node; 518 } 519 } 520 521 return node; 522 } 523 524 // Whether the given offset is undefined. 525 static bool is_undefined(intptr_t offset) { 526 return offset == Type::OffsetTop; 527 } 528 529 // Whether the given offset is unknown. 530 static bool is_unknown(intptr_t offset) { 531 return offset == Type::OffsetBot; 532 } 533 534 // Whether the given offset is concrete (defined and compile-time known). 535 static bool is_concrete(intptr_t offset) { 536 return !is_undefined(offset) && !is_unknown(offset); 537 } 538 539 // Compute base + offset components of the memory address accessed by mach. 540 // Return a node representing the base address, or null if the base cannot be 541 // found or the offset is undefined or a concrete negative value. If a non-null 542 // base is returned, the offset is a concrete, nonnegative value or unknown. 543 static const Node* get_base_and_offset(const MachNode* mach, intptr_t& offset) { 544 const TypePtr* adr_type = nullptr; 545 offset = 0; 546 const Node* base = mach->get_base_and_disp(offset, adr_type); 547 548 if (base == nullptr || base == NodeSentinel) { 549 return nullptr; 550 } 551 552 if (offset == 0 && base->is_Mach() && base->as_Mach()->ideal_Opcode() == Op_AddP) { 553 // The memory address is computed by 'base' and fed to 'mach' via an 554 // indirect memory operand (indicated by offset == 0). The ultimate base and 555 // offset can be fetched directly from the inputs and Ideal type of 'base'. 556 offset = base->bottom_type()->isa_oopptr()->offset(); 557 // Even if 'base' is not an Ideal AddP node anymore, Matcher::ReduceInst() 558 // guarantees that the base address is still available at the same slot. 559 base = base->in(AddPNode::Base); 560 assert(base != nullptr, ""); 561 } 562 563 if (is_undefined(offset) || (is_concrete(offset) && offset < 0)) { 564 return nullptr; 565 } 566 567 return look_through_node(base); 568 } 569 570 // Whether a phi node corresponds to an array allocation. 571 // This test is incomplete: in some edge cases, it might return false even 572 // though the node does correspond to an array allocation. 573 static bool is_array_allocation(const Node* phi) { 574 precond(phi->is_Phi()); 575 // Check whether phi has a successor cast (CheckCastPP) to Java array pointer, 576 // possibly below spill copies and other cast nodes. Limit the exploration to 577 // a single path from the phi node consisting of these node types. 578 const Node* current = phi; 579 while (true) { 580 const Node* next = nullptr; 581 for (DUIterator_Fast imax, i = current->fast_outs(imax); i < imax; i++) { 582 if (!current->fast_out(i)->isa_Mach()) { 583 continue; 584 } 585 const MachNode* succ = current->fast_out(i)->as_Mach(); 586 if (succ->ideal_Opcode() == Op_CheckCastPP) { 587 if (succ->get_ptr_type()->isa_aryptr()) { 588 // Cast to Java array pointer: phi corresponds to an array allocation. 589 return true; 590 } 591 // Other cast: record as candidate for further exploration. 592 next = succ; 593 } else if (succ->is_SpillCopy() && next == nullptr) { 594 // Spill copy, and no better candidate found: record as candidate. 595 next = succ; 596 } 597 } 598 if (next == nullptr) { 599 // No evidence found that phi corresponds to an array allocation, and no 600 // candidates available to continue exploring. 601 return false; 602 } 603 // Continue exploring from the best candidate found. 604 current = next; 605 } 606 ShouldNotReachHere(); 607 } 608 609 // Match the phi node that connects a TLAB allocation fast path with its slowpath 610 static bool is_allocation(const Node* node) { 611 if (node->req() != 3) { 612 return false; 613 } 614 const Node* const fast_node = node->in(2); 615 if (!fast_node->is_Mach()) { 616 return false; 617 } 618 const MachNode* const fast_mach = fast_node->as_Mach(); 619 if (fast_mach->ideal_Opcode() != Op_LoadP) { 620 return false; 621 } 622 const TypePtr* const adr_type = nullptr; 623 intptr_t offset; 624 const Node* const base = get_base_and_offset(fast_mach, offset); 625 if (base == nullptr || !base->is_Mach() || !is_concrete(offset)) { 626 return false; 627 } 628 const MachNode* const base_mach = base->as_Mach(); 629 if (base_mach->ideal_Opcode() != Op_ThreadLocal) { 630 return false; 631 } 632 return offset == in_bytes(Thread::tlab_top_offset()); 633 } 634 635 static void elide_mach_barrier(MachNode* mach) { 636 mach->set_barrier_data(ZBarrierElided); 637 } 638 639 void ZBarrierSetC2::analyze_dominating_barriers_impl(Node_List& accesses, Node_List& access_dominators) const { 640 Compile* const C = Compile::current(); 641 PhaseCFG* const cfg = C->cfg(); 642 643 for (uint i = 0; i < accesses.size(); i++) { 644 MachNode* const access = accesses.at(i)->as_Mach(); 645 intptr_t access_offset; 646 const Node* const access_obj = get_base_and_offset(access, access_offset); 647 Block* const access_block = cfg->get_block_for_node(access); 648 const uint access_index = block_index(access_block, access); 649 650 if (access_obj == nullptr) { 651 // No information available 652 continue; 653 } 654 655 for (uint j = 0; j < access_dominators.size(); j++) { 656 const Node* const mem = access_dominators.at(j); 657 if (mem->is_Phi()) { 658 // Allocation node 659 if (mem != access_obj) { 660 continue; 661 } 662 if (is_unknown(access_offset) && !is_array_allocation(mem)) { 663 // The accessed address has an unknown offset, but the allocated 664 // object cannot be determined to be an array. Avoid eliding in this 665 // case, to be on the safe side. 666 continue; 667 } 668 assert((is_concrete(access_offset) && access_offset >= 0) || (is_unknown(access_offset) && is_array_allocation(mem)), 669 "candidate allocation-dominated access offsets must be either concrete and nonnegative, or unknown (for array allocations only)"); 670 } else { 671 // Access node 672 const MachNode* const mem_mach = mem->as_Mach(); 673 intptr_t mem_offset; 674 const Node* const mem_obj = get_base_and_offset(mem_mach, mem_offset); 675 676 if (mem_obj == nullptr || 677 !is_concrete(access_offset) || 678 !is_concrete(mem_offset)) { 679 // No information available 680 continue; 681 } 682 683 if (mem_obj != access_obj || mem_offset != access_offset) { 684 // Not the same addresses, not a candidate 685 continue; 686 } 687 assert(is_concrete(access_offset) && access_offset >= 0, 688 "candidate non-allocation-dominated access offsets must be concrete and nonnegative"); 689 } 690 691 Block* mem_block = cfg->get_block_for_node(mem); 692 const uint mem_index = block_index(mem_block, mem); 693 694 if (access_block == mem_block) { 695 // Earlier accesses in the same block 696 if (mem_index < access_index && !block_has_safepoint(mem_block, mem_index + 1, access_index)) { 697 elide_mach_barrier(access); 698 } 699 } else if (mem_block->dominates(access_block)) { 700 // Dominating block? Look around for safepoints 701 ResourceMark rm; 702 Block_List stack; 703 VectorSet visited; 704 stack.push(access_block); 705 bool safepoint_found = block_has_safepoint(access_block); 706 while (!safepoint_found && stack.size() > 0) { 707 const Block* const block = stack.pop(); 708 if (visited.test_set(block->_pre_order)) { 709 continue; 710 } 711 if (block_has_safepoint(block)) { 712 safepoint_found = true; 713 break; 714 } 715 if (block == mem_block) { 716 continue; 717 } 718 719 // Push predecessor blocks 720 for (uint p = 1; p < block->num_preds(); ++p) { 721 Block* const pred = cfg->get_block_for_node(block->pred(p)); 722 stack.push(pred); 723 } 724 } 725 726 if (!safepoint_found) { 727 elide_mach_barrier(access); 728 } 729 } 730 } 731 } 732 } 733 734 void ZBarrierSetC2::analyze_dominating_barriers() const { 735 ResourceMark rm; 736 Compile* const C = Compile::current(); 737 PhaseCFG* const cfg = C->cfg(); 738 739 Node_List loads; 740 Node_List load_dominators; 741 742 Node_List stores; 743 Node_List store_dominators; 744 745 Node_List atomics; 746 Node_List atomic_dominators; 747 748 // Step 1 - Find accesses and allocations, and track them in lists 749 for (uint i = 0; i < cfg->number_of_blocks(); ++i) { 750 const Block* const block = cfg->get_block(i); 751 for (uint j = 0; j < block->number_of_nodes(); ++j) { 752 Node* const node = block->get_node(j); 753 if (node->is_Phi()) { 754 if (is_allocation(node)) { 755 load_dominators.push(node); 756 store_dominators.push(node); 757 // An allocation can't be considered to "dominate" an atomic operation. 758 // For example a CAS requires the memory location to be store-good. 759 // When you have a dominating store or atomic instruction, that is 760 // indeed ensured to be the case. However, as for allocations, the 761 // initialized memory location could be raw null, which isn't store-good. 762 } 763 continue; 764 } else if (!node->is_Mach()) { 765 continue; 766 } 767 768 MachNode* const mach = node->as_Mach(); 769 switch (mach->ideal_Opcode()) { 770 case Op_LoadP: 771 if ((mach->barrier_data() & ZBarrierStrong) != 0 && 772 (mach->barrier_data() & ZBarrierNoKeepalive) == 0) { 773 loads.push(mach); 774 load_dominators.push(mach); 775 } 776 break; 777 case Op_StoreP: 778 if (mach->barrier_data() != 0) { 779 stores.push(mach); 780 load_dominators.push(mach); 781 store_dominators.push(mach); 782 atomic_dominators.push(mach); 783 } 784 break; 785 case Op_CompareAndExchangeP: 786 case Op_CompareAndSwapP: 787 case Op_GetAndSetP: 788 if (mach->barrier_data() != 0) { 789 atomics.push(mach); 790 load_dominators.push(mach); 791 store_dominators.push(mach); 792 atomic_dominators.push(mach); 793 } 794 break; 795 796 default: 797 break; 798 } 799 } 800 } 801 802 // Step 2 - Find dominating accesses or allocations for each access 803 analyze_dominating_barriers_impl(loads, load_dominators); 804 analyze_dominating_barriers_impl(stores, store_dominators); 805 analyze_dominating_barriers_impl(atomics, atomic_dominators); 806 } 807 808 void ZBarrierSetC2::eliminate_gc_barrier(PhaseMacroExpand* macro, Node* node) const { 809 eliminate_gc_barrier_data(node); 810 } 811 812 void ZBarrierSetC2::eliminate_gc_barrier_data(Node* node) const { 813 if (node->is_LoadStore()) { 814 LoadStoreNode* loadstore = node->as_LoadStore(); 815 loadstore->set_barrier_data(ZBarrierElided); 816 } else if (node->is_Mem()) { 817 MemNode* mem = node->as_Mem(); 818 mem->set_barrier_data(ZBarrierElided); 819 } 820 } 821 822 #ifndef PRODUCT 823 void ZBarrierSetC2::dump_barrier_data(const MachNode* mach, outputStream* st) const { 824 if ((mach->barrier_data() & ZBarrierStrong) != 0) { 825 st->print("strong "); 826 } 827 if ((mach->barrier_data() & ZBarrierWeak) != 0) { 828 st->print("weak "); 829 } 830 if ((mach->barrier_data() & ZBarrierPhantom) != 0) { 831 st->print("phantom "); 832 } 833 if ((mach->barrier_data() & ZBarrierNoKeepalive) != 0) { 834 st->print("nokeepalive "); 835 } 836 if ((mach->barrier_data() & ZBarrierNative) != 0) { 837 st->print("native "); 838 } 839 if ((mach->barrier_data() & ZBarrierElided) != 0) { 840 st->print("elided "); 841 } 842 } 843 #endif // !PRODUCT