1 /* 2 * Copyright (c) 2015, 2019, 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 "classfile/javaClasses.hpp" 26 #include "gc/z/c2/zBarrierSetC2.hpp" 27 #include "gc/z/zBarrierSet.hpp" 28 #include "gc/z/zBarrierSetAssembler.hpp" 29 #include "gc/z/zBarrierSetRuntime.hpp" 30 #include "opto/arraycopynode.hpp" 31 #include "opto/addnode.hpp" 32 #include "opto/block.hpp" 33 #include "opto/compile.hpp" 34 #include "opto/graphKit.hpp" 35 #include "opto/machnode.hpp" 36 #include "opto/macro.hpp" 37 #include "opto/memnode.hpp" 38 #include "opto/node.hpp" 39 #include "opto/output.hpp" 40 #include "opto/regalloc.hpp" 41 #include "opto/rootnode.hpp" 42 #include "opto/runtime.hpp" 43 #include "opto/type.hpp" 44 #include "utilities/growableArray.hpp" 45 #include "utilities/macros.hpp" 46 47 class ZBarrierSetC2State : public ResourceObj { 48 private: 49 GrowableArray<ZLoadBarrierStubC2*>* _stubs; 50 Node_Array _live; 51 52 public: 53 ZBarrierSetC2State(Arena* arena) : 54 _stubs(new (arena) GrowableArray<ZLoadBarrierStubC2*>(arena, 8, 0, NULL)), 55 _live(arena) {} 56 57 GrowableArray<ZLoadBarrierStubC2*>* stubs() { 58 return _stubs; 59 } 60 61 RegMask* live(const Node* node) { 62 if (!node->is_Mach()) { 63 // Don't need liveness for non-MachNodes 64 return NULL; 65 } 66 67 const MachNode* const mach = node->as_Mach(); 68 if (mach->barrier_data() == ZLoadBarrierElided) { 69 // Don't need liveness data for nodes without barriers 70 return NULL; 71 } 72 73 RegMask* live = (RegMask*)_live[node->_idx]; 74 if (live == NULL) { 75 live = new (Compile::current()->comp_arena()->Amalloc_D(sizeof(RegMask))) RegMask(); 76 _live.map(node->_idx, (Node*)live); 77 } 78 79 return live; 80 } 81 }; 82 83 static ZBarrierSetC2State* barrier_set_state() { 84 return reinterpret_cast<ZBarrierSetC2State*>(Compile::current()->barrier_set_state()); 85 } 86 87 ZLoadBarrierStubC2* ZLoadBarrierStubC2::create(const MachNode* node, Address ref_addr, Register ref, Register tmp, uint8_t barrier_data) { 88 ZLoadBarrierStubC2* const stub = new (Compile::current()->comp_arena()) ZLoadBarrierStubC2(node, ref_addr, ref, tmp, barrier_data); 89 if (!Compile::current()->output()->in_scratch_emit_size()) { 90 barrier_set_state()->stubs()->append(stub); 91 } 92 93 return stub; 94 } 95 96 ZLoadBarrierStubC2::ZLoadBarrierStubC2(const MachNode* node, Address ref_addr, Register ref, Register tmp, uint8_t barrier_data) : 97 _node(node), 98 _ref_addr(ref_addr), 99 _ref(ref), 100 _tmp(tmp), 101 _barrier_data(barrier_data), 102 _entry(), 103 _continuation() { 104 assert_different_registers(ref, ref_addr.base()); 105 assert_different_registers(ref, ref_addr.index()); 106 } 107 108 Address ZLoadBarrierStubC2::ref_addr() const { 109 return _ref_addr; 110 } 111 112 Register ZLoadBarrierStubC2::ref() const { 113 return _ref; 114 } 115 116 Register ZLoadBarrierStubC2::tmp() const { 117 return _tmp; 118 } 119 120 address ZLoadBarrierStubC2::slow_path() const { 121 DecoratorSet decorators = DECORATORS_NONE; 122 if (_barrier_data & ZLoadBarrierStrong) { 123 decorators |= ON_STRONG_OOP_REF; 124 } 125 if (_barrier_data & ZLoadBarrierWeak) { 126 decorators |= ON_WEAK_OOP_REF; 127 } 128 if (_barrier_data & ZLoadBarrierPhantom) { 129 decorators |= ON_PHANTOM_OOP_REF; 130 } 131 if (_barrier_data & ZLoadBarrierNoKeepalive) { 132 decorators |= AS_NO_KEEPALIVE; 133 } 134 return ZBarrierSetRuntime::load_barrier_on_oop_field_preloaded_addr(decorators); 135 } 136 137 RegMask& ZLoadBarrierStubC2::live() const { 138 return *barrier_set_state()->live(_node); 139 } 140 141 Label* ZLoadBarrierStubC2::entry() { 142 // The _entry will never be bound when in_scratch_emit_size() is true. 143 // However, we still need to return a label that is not bound now, but 144 // will eventually be bound. Any lable will do, as it will only act as 145 // a placeholder, so we return the _continuation label. 146 return Compile::current()->output()->in_scratch_emit_size() ? &_continuation : &_entry; 147 } 148 149 Label* ZLoadBarrierStubC2::continuation() { 150 return &_continuation; 151 } 152 153 void* ZBarrierSetC2::create_barrier_state(Arena* comp_arena) const { 154 return new (comp_arena) ZBarrierSetC2State(comp_arena); 155 } 156 157 void ZBarrierSetC2::late_barrier_analysis() const { 158 analyze_dominating_barriers(); 159 compute_liveness_at_stubs(); 160 } 161 162 void ZBarrierSetC2::emit_stubs(CodeBuffer& cb) const { 163 MacroAssembler masm(&cb); 164 GrowableArray<ZLoadBarrierStubC2*>* const stubs = barrier_set_state()->stubs(); 165 166 for (int i = 0; i < stubs->length(); i++) { 167 // Make sure there is enough space in the code buffer 168 if (cb.insts()->maybe_expand_to_ensure_remaining(PhaseOutput::MAX_inst_size) && cb.blob() == NULL) { 169 ciEnv::current()->record_failure("CodeCache is full"); 170 return; 171 } 172 173 ZBarrierSet::assembler()->generate_c2_load_barrier_stub(&masm, stubs->at(i)); 174 } 175 176 masm.flush(); 177 } 178 179 int ZBarrierSetC2::estimate_stub_size() const { 180 Compile* const C = Compile::current(); 181 BufferBlob* const blob = C->output()->scratch_buffer_blob(); 182 GrowableArray<ZLoadBarrierStubC2*>* const stubs = barrier_set_state()->stubs(); 183 int size = 0; 184 185 for (int i = 0; i < stubs->length(); i++) { 186 CodeBuffer cb(blob->content_begin(), (address)C->output()->scratch_locs_memory() - blob->content_begin()); 187 MacroAssembler masm(&cb); 188 ZBarrierSet::assembler()->generate_c2_load_barrier_stub(&masm, stubs->at(i)); 189 size += cb.insts_size(); 190 } 191 192 return size; 193 } 194 195 static void set_barrier_data(C2Access& access) { 196 if (ZBarrierSet::barrier_needed(access.decorators(), access.type())) { 197 uint8_t barrier_data = 0; 198 199 if (access.decorators() & ON_PHANTOM_OOP_REF) { 200 barrier_data |= ZLoadBarrierPhantom; 201 } else if (access.decorators() & ON_WEAK_OOP_REF) { 202 barrier_data |= ZLoadBarrierWeak; 203 } else { 204 barrier_data |= ZLoadBarrierStrong; 205 } 206 207 if (access.decorators() & AS_NO_KEEPALIVE) { 208 barrier_data |= ZLoadBarrierNoKeepalive; 209 } 210 211 access.set_barrier_data(barrier_data); 212 } 213 } 214 215 Node* ZBarrierSetC2::load_at_resolved(C2Access& access, const Type* val_type) const { 216 set_barrier_data(access); 217 return BarrierSetC2::load_at_resolved(access, val_type); 218 } 219 220 Node* ZBarrierSetC2::atomic_cmpxchg_val_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 221 Node* new_val, const Type* val_type) const { 222 set_barrier_data(access); 223 return BarrierSetC2::atomic_cmpxchg_val_at_resolved(access, expected_val, new_val, val_type); 224 } 225 226 Node* ZBarrierSetC2::atomic_cmpxchg_bool_at_resolved(C2AtomicParseAccess& access, Node* expected_val, 227 Node* new_val, const Type* value_type) const { 228 set_barrier_data(access); 229 return BarrierSetC2::atomic_cmpxchg_bool_at_resolved(access, expected_val, new_val, value_type); 230 } 231 232 Node* ZBarrierSetC2::atomic_xchg_at_resolved(C2AtomicParseAccess& access, Node* new_val, const Type* val_type) const { 233 set_barrier_data(access); 234 return BarrierSetC2::atomic_xchg_at_resolved(access, new_val, val_type); 235 } 236 237 bool ZBarrierSetC2::array_copy_requires_gc_barriers(bool tightly_coupled_alloc, BasicType type, 238 bool is_clone, bool is_clone_instance, 239 ArrayCopyPhase phase) const { 240 if (phase == ArrayCopyPhase::Parsing) { 241 return false; 242 } 243 if (phase == ArrayCopyPhase::Optimization) { 244 return is_clone_instance; 245 } 246 // else ArrayCopyPhase::Expansion 247 return type == T_OBJECT || type == T_ARRAY; 248 } 249 250 // This TypeFunc assumes a 64bit system 251 static const TypeFunc* clone_type() { 252 // Create input type (domain) 253 const Type** domain_fields = TypeTuple::fields(4); 254 domain_fields[TypeFunc::Parms + 0] = TypeInstPtr::NOTNULL; // src 255 domain_fields[TypeFunc::Parms + 1] = TypeInstPtr::NOTNULL; // dst 256 domain_fields[TypeFunc::Parms + 2] = TypeLong::LONG; // size lower 257 domain_fields[TypeFunc::Parms + 3] = Type::HALF; // size upper 258 const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + 4, domain_fields); 259 260 // Create result type (range) 261 const Type** range_fields = TypeTuple::fields(0); 262 const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 0, range_fields); 263 264 return TypeFunc::make(domain, range); 265 } 266 267 #define XTOP LP64_ONLY(COMMA phase->top()) 268 269 void ZBarrierSetC2::clone_at_expansion(PhaseMacroExpand* phase, ArrayCopyNode* ac) const { 270 Node* const src = ac->in(ArrayCopyNode::Src); 271 const TypeAryPtr* ary_ptr = src->get_ptr_type()->isa_aryptr(); 272 273 if (ac->is_clone_array() && ary_ptr != NULL) { 274 BasicType bt = ary_ptr->elem()->array_element_basic_type(); 275 if (is_reference_type(bt)) { 276 // Clone object array 277 bt = T_OBJECT; 278 } else { 279 // Clone primitive array 280 bt = T_LONG; 281 } 282 283 Node* ctrl = ac->in(TypeFunc::Control); 284 Node* mem = ac->in(TypeFunc::Memory); 285 Node* src = ac->in(ArrayCopyNode::Src); 286 Node* src_offset = ac->in(ArrayCopyNode::SrcPos); 287 Node* dest = ac->in(ArrayCopyNode::Dest); 288 Node* dest_offset = ac->in(ArrayCopyNode::DestPos); 289 Node* length = ac->in(ArrayCopyNode::Length); 290 291 if (bt == T_OBJECT) { 292 // BarrierSetC2::clone sets the offsets via BarrierSetC2::arraycopy_payload_base_offset 293 // which 8-byte aligns them to allow for word size copies. Make sure the offsets point 294 // to the first element in the array when cloning object arrays. Otherwise, load 295 // barriers are applied to parts of the header. Also adjust the length accordingly. 296 assert(src_offset == dest_offset, "should be equal"); 297 jlong offset = src_offset->get_long(); 298 if (offset != arrayOopDesc::base_offset_in_bytes(T_OBJECT)) { 299 assert(!UseCompressedClassPointers || UseCompactObjectHeaders, "should only happen without compressed class pointers or with compact object headers"); 300 assert((arrayOopDesc::base_offset_in_bytes(T_OBJECT) - offset) == BytesPerLong, "unexpected offset"); 301 length = phase->transform_later(new SubLNode(length, phase->longcon(1))); // Size is in longs 302 src_offset = phase->longcon(arrayOopDesc::base_offset_in_bytes(T_OBJECT)); 303 dest_offset = src_offset; 304 } 305 } 306 Node* payload_src = phase->basic_plus_adr(src, src_offset); 307 Node* payload_dst = phase->basic_plus_adr(dest, dest_offset); 308 309 const char* copyfunc_name = "arraycopy"; 310 address copyfunc_addr = phase->basictype2arraycopy(bt, NULL, NULL, true, copyfunc_name, true); 311 312 const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM; 313 const TypeFunc* call_type = OptoRuntime::fast_arraycopy_Type(); 314 315 Node* call = phase->make_leaf_call(ctrl, mem, call_type, copyfunc_addr, copyfunc_name, raw_adr_type, payload_src, payload_dst, length XTOP); 316 phase->transform_later(call); 317 318 phase->igvn().replace_node(ac, call); 319 return; 320 } 321 322 // Clone instance 323 Node* const ctrl = ac->in(TypeFunc::Control); 324 Node* const mem = ac->in(TypeFunc::Memory); 325 Node* const dst = ac->in(ArrayCopyNode::Dest); 326 Node* const size = ac->in(ArrayCopyNode::Length); 327 328 assert(size->bottom_type()->is_long(), "Should be long"); 329 330 // The native clone we are calling here expects the instance size in words 331 // Add header/offset size to payload size to get instance size. 332 Node* const base_offset = phase->longcon(arraycopy_payload_base_offset(ac->is_clone_array()) >> LogBytesPerLong); 333 Node* const full_size = phase->transform_later(new AddLNode(size, base_offset)); 334 335 Node* const call = phase->make_leaf_call(ctrl, 336 mem, 337 clone_type(), 338 ZBarrierSetRuntime::clone_addr(), 339 "ZBarrierSetRuntime::clone", 340 TypeRawPtr::BOTTOM, 341 src, 342 dst, 343 full_size, 344 phase->top()); 345 phase->transform_later(call); 346 phase->igvn().replace_node(ac, call); 347 } 348 349 #undef XTOP 350 351 // == Dominating barrier elision == 352 353 static bool block_has_safepoint(const Block* block, uint from, uint to) { 354 for (uint i = from; i < to; i++) { 355 if (block->get_node(i)->is_MachSafePoint()) { 356 // Safepoint found 357 return true; 358 } 359 } 360 361 // Safepoint not found 362 return false; 363 } 364 365 static bool block_has_safepoint(const Block* block) { 366 return block_has_safepoint(block, 0, block->number_of_nodes()); 367 } 368 369 static uint block_index(const Block* block, const Node* node) { 370 for (uint j = 0; j < block->number_of_nodes(); ++j) { 371 if (block->get_node(j) == node) { 372 return j; 373 } 374 } 375 ShouldNotReachHere(); 376 return 0; 377 } 378 379 void ZBarrierSetC2::analyze_dominating_barriers() const { 380 ResourceMark rm; 381 Compile* const C = Compile::current(); 382 PhaseCFG* const cfg = C->cfg(); 383 Block_List worklist; 384 Node_List mem_ops; 385 Node_List barrier_loads; 386 387 // Step 1 - Find accesses, and track them in lists 388 for (uint i = 0; i < cfg->number_of_blocks(); ++i) { 389 const Block* const block = cfg->get_block(i); 390 for (uint j = 0; j < block->number_of_nodes(); ++j) { 391 const Node* const node = block->get_node(j); 392 if (!node->is_Mach()) { 393 continue; 394 } 395 396 MachNode* const mach = node->as_Mach(); 397 switch (mach->ideal_Opcode()) { 398 case Op_LoadP: 399 if ((mach->barrier_data() & ZLoadBarrierStrong) != 0) { 400 barrier_loads.push(mach); 401 } 402 if ((mach->barrier_data() & (ZLoadBarrierStrong | ZLoadBarrierNoKeepalive)) == 403 ZLoadBarrierStrong) { 404 mem_ops.push(mach); 405 } 406 break; 407 case Op_CompareAndExchangeP: 408 case Op_CompareAndSwapP: 409 case Op_GetAndSetP: 410 if ((mach->barrier_data() & ZLoadBarrierStrong) != 0) { 411 barrier_loads.push(mach); 412 } 413 case Op_StoreP: 414 mem_ops.push(mach); 415 break; 416 417 default: 418 break; 419 } 420 } 421 } 422 423 // Step 2 - Find dominating accesses for each load 424 for (uint i = 0; i < barrier_loads.size(); i++) { 425 MachNode* const load = barrier_loads.at(i)->as_Mach(); 426 const TypePtr* load_adr_type = NULL; 427 intptr_t load_offset = 0; 428 const Node* const load_obj = load->get_base_and_disp(load_offset, load_adr_type); 429 Block* const load_block = cfg->get_block_for_node(load); 430 const uint load_index = block_index(load_block, load); 431 432 for (uint j = 0; j < mem_ops.size(); j++) { 433 MachNode* mem = mem_ops.at(j)->as_Mach(); 434 const TypePtr* mem_adr_type = NULL; 435 intptr_t mem_offset = 0; 436 const Node* mem_obj = mem->get_base_and_disp(mem_offset, mem_adr_type); 437 Block* mem_block = cfg->get_block_for_node(mem); 438 uint mem_index = block_index(mem_block, mem); 439 440 if (load_obj == NodeSentinel || mem_obj == NodeSentinel || 441 load_obj == NULL || mem_obj == NULL || 442 load_offset < 0 || mem_offset < 0) { 443 continue; 444 } 445 446 if (mem_obj != load_obj || mem_offset != load_offset) { 447 // Not the same addresses, not a candidate 448 continue; 449 } 450 451 if (load_block == mem_block) { 452 // Earlier accesses in the same block 453 if (mem_index < load_index && !block_has_safepoint(mem_block, mem_index + 1, load_index)) { 454 load->set_barrier_data(ZLoadBarrierElided); 455 } 456 } else if (mem_block->dominates(load_block)) { 457 // Dominating block? Look around for safepoints 458 ResourceMark rm; 459 Block_List stack; 460 VectorSet visited; 461 stack.push(load_block); 462 bool safepoint_found = block_has_safepoint(load_block); 463 while (!safepoint_found && stack.size() > 0) { 464 Block* block = stack.pop(); 465 if (visited.test_set(block->_pre_order)) { 466 continue; 467 } 468 if (block_has_safepoint(block)) { 469 safepoint_found = true; 470 break; 471 } 472 if (block == mem_block) { 473 continue; 474 } 475 476 // Push predecessor blocks 477 for (uint p = 1; p < block->num_preds(); ++p) { 478 Block* pred = cfg->get_block_for_node(block->pred(p)); 479 stack.push(pred); 480 } 481 } 482 483 if (!safepoint_found) { 484 load->set_barrier_data(ZLoadBarrierElided); 485 } 486 } 487 } 488 } 489 } 490 491 // == Reduced spilling optimization == 492 493 void ZBarrierSetC2::compute_liveness_at_stubs() const { 494 ResourceMark rm; 495 Compile* const C = Compile::current(); 496 Arena* const A = Thread::current()->resource_area(); 497 PhaseCFG* const cfg = C->cfg(); 498 PhaseRegAlloc* const regalloc = C->regalloc(); 499 RegMask* const live = NEW_ARENA_ARRAY(A, RegMask, cfg->number_of_blocks() * sizeof(RegMask)); 500 ZBarrierSetAssembler* const bs = ZBarrierSet::assembler(); 501 Block_List worklist; 502 503 for (uint i = 0; i < cfg->number_of_blocks(); ++i) { 504 new ((void*)(live + i)) RegMask(); 505 worklist.push(cfg->get_block(i)); 506 } 507 508 while (worklist.size() > 0) { 509 const Block* const block = worklist.pop(); 510 RegMask& old_live = live[block->_pre_order]; 511 RegMask new_live; 512 513 // Initialize to union of successors 514 for (uint i = 0; i < block->_num_succs; i++) { 515 const uint succ_id = block->_succs[i]->_pre_order; 516 new_live.OR(live[succ_id]); 517 } 518 519 // Walk block backwards, computing liveness 520 for (int i = block->number_of_nodes() - 1; i >= 0; --i) { 521 const Node* const node = block->get_node(i); 522 523 // Remove def bits 524 const OptoReg::Name first = bs->refine_register(node, regalloc->get_reg_first(node)); 525 const OptoReg::Name second = bs->refine_register(node, regalloc->get_reg_second(node)); 526 if (first != OptoReg::Bad) { 527 new_live.Remove(first); 528 } 529 if (second != OptoReg::Bad) { 530 new_live.Remove(second); 531 } 532 533 // Add use bits 534 for (uint j = 1; j < node->req(); ++j) { 535 const Node* const use = node->in(j); 536 const OptoReg::Name first = bs->refine_register(use, regalloc->get_reg_first(use)); 537 const OptoReg::Name second = bs->refine_register(use, regalloc->get_reg_second(use)); 538 if (first != OptoReg::Bad) { 539 new_live.Insert(first); 540 } 541 if (second != OptoReg::Bad) { 542 new_live.Insert(second); 543 } 544 } 545 546 // If this node tracks liveness, update it 547 RegMask* const regs = barrier_set_state()->live(node); 548 if (regs != NULL) { 549 regs->OR(new_live); 550 } 551 } 552 553 // Now at block top, see if we have any changes 554 new_live.SUBTRACT(old_live); 555 if (new_live.is_NotEmpty()) { 556 // Liveness has refined, update and propagate to prior blocks 557 old_live.OR(new_live); 558 for (uint i = 1; i < block->num_preds(); ++i) { 559 Block* const pred = cfg->get_block_for_node(block->pred(i)); 560 worklist.push(pred); 561 } 562 } 563 } 564 }