1 /* 2 * Copyright (c) 2012, 2025, 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 25 #include "gc/shared/barrierSet.hpp" 26 #include "gc/shared/tlab_globals.hpp" 27 #include "opto/arraycopynode.hpp" 28 #include "oops/objArrayKlass.hpp" 29 #include "opto/convertnode.hpp" 30 #include "opto/vectornode.hpp" 31 #include "opto/graphKit.hpp" 32 #include "opto/macro.hpp" 33 #include "opto/runtime.hpp" 34 #include "opto/castnode.hpp" 35 #include "runtime/stubRoutines.hpp" 36 #include "utilities/align.hpp" 37 #include "utilities/powerOfTwo.hpp" 38 39 void PhaseMacroExpand::insert_mem_bar(Node** ctrl, Node** mem, int opcode, Node* precedent) { 40 MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, precedent); 41 mb->init_req(TypeFunc::Control, *ctrl); 42 mb->init_req(TypeFunc::Memory, *mem); 43 transform_later(mb); 44 *ctrl = new ProjNode(mb,TypeFunc::Control); 45 transform_later(*ctrl); 46 Node* mem_proj = new ProjNode(mb,TypeFunc::Memory); 47 transform_later(mem_proj); 48 *mem = mem_proj; 49 } 50 51 Node* PhaseMacroExpand::array_element_address(Node* ary, Node* idx, BasicType elembt) { 52 uint shift = exact_log2(type2aelembytes(elembt)); 53 uint header = arrayOopDesc::base_offset_in_bytes(elembt); 54 Node* base = basic_plus_adr(ary, header); 55 #ifdef _LP64 56 // see comment in GraphKit::array_element_address 57 int index_max = max_jint - 1; // array size is max_jint, index is one less 58 const TypeLong* lidxtype = TypeLong::make(CONST64(0), index_max, Type::WidenMax); 59 idx = transform_later( new ConvI2LNode(idx, lidxtype) ); 60 #endif 61 Node* scale = new LShiftXNode(idx, intcon(shift)); 62 transform_later(scale); 63 return basic_plus_adr(ary, base, scale); 64 } 65 66 Node* PhaseMacroExpand::ConvI2L(Node* offset) { 67 return transform_later(new ConvI2LNode(offset)); 68 } 69 70 Node* PhaseMacroExpand::make_leaf_call(Node* ctrl, Node* mem, 71 const TypeFunc* call_type, address call_addr, 72 const char* call_name, 73 const TypePtr* adr_type, 74 Node* parm0, Node* parm1, 75 Node* parm2, Node* parm3, 76 Node* parm4, Node* parm5, 77 Node* parm6, Node* parm7) { 78 Node* call = new CallLeafNoFPNode(call_type, call_addr, call_name, adr_type); 79 call->init_req(TypeFunc::Control, ctrl); 80 call->init_req(TypeFunc::I_O , top()); 81 call->init_req(TypeFunc::Memory , mem); 82 call->init_req(TypeFunc::ReturnAdr, top()); 83 call->init_req(TypeFunc::FramePtr, top()); 84 85 // Hook each parm in order. Stop looking at the first null. 86 if (parm0 != nullptr) { call->init_req(TypeFunc::Parms+0, parm0); 87 if (parm1 != nullptr) { call->init_req(TypeFunc::Parms+1, parm1); 88 if (parm2 != nullptr) { call->init_req(TypeFunc::Parms+2, parm2); 89 if (parm3 != nullptr) { call->init_req(TypeFunc::Parms+3, parm3); 90 if (parm4 != nullptr) { call->init_req(TypeFunc::Parms+4, parm4); 91 if (parm5 != nullptr) { call->init_req(TypeFunc::Parms+5, parm5); 92 if (parm6 != nullptr) { call->init_req(TypeFunc::Parms+6, parm6); 93 if (parm7 != nullptr) { call->init_req(TypeFunc::Parms+7, parm7); 94 /* close each nested if ===> */ } } } } } } } } 95 assert(call->in(call->req()-1) != nullptr, "must initialize all parms"); 96 97 return call; 98 } 99 100 101 //------------------------------generate_guard--------------------------- 102 // Helper function for generating guarded fast-slow graph structures. 103 // The given 'test', if true, guards a slow path. If the test fails 104 // then a fast path can be taken. (We generally hope it fails.) 105 // In all cases, GraphKit::control() is updated to the fast path. 106 // The returned value represents the control for the slow path. 107 // The return value is never 'top'; it is either a valid control 108 // or null if it is obvious that the slow path can never be taken. 109 // Also, if region and the slow control are not null, the slow edge 110 // is appended to the region. 111 Node* PhaseMacroExpand::generate_guard(Node** ctrl, Node* test, RegionNode* region, float true_prob) { 112 if ((*ctrl)->is_top()) { 113 // Already short circuited. 114 return nullptr; 115 } 116 // Build an if node and its projections. 117 // If test is true we take the slow path, which we assume is uncommon. 118 if (_igvn.type(test) == TypeInt::ZERO) { 119 // The slow branch is never taken. No need to build this guard. 120 return nullptr; 121 } 122 123 IfNode* iff = new IfNode(*ctrl, test, true_prob, COUNT_UNKNOWN); 124 transform_later(iff); 125 126 Node* if_slow = new IfTrueNode(iff); 127 transform_later(if_slow); 128 129 if (region != nullptr) { 130 region->add_req(if_slow); 131 } 132 133 Node* if_fast = new IfFalseNode(iff); 134 transform_later(if_fast); 135 136 *ctrl = if_fast; 137 138 return if_slow; 139 } 140 141 inline Node* PhaseMacroExpand::generate_slow_guard(Node** ctrl, Node* test, RegionNode* region) { 142 return generate_guard(ctrl, test, region, PROB_UNLIKELY_MAG(3)); 143 } 144 145 void PhaseMacroExpand::generate_negative_guard(Node** ctrl, Node* index, RegionNode* region) { 146 if ((*ctrl)->is_top()) 147 return; // already stopped 148 if (_igvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint] 149 return; // index is already adequately typed 150 Node* cmp_lt = new CmpINode(index, intcon(0)); 151 transform_later(cmp_lt); 152 Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt); 153 transform_later(bol_lt); 154 generate_guard(ctrl, bol_lt, region, PROB_MIN); 155 } 156 157 void PhaseMacroExpand::generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region) { 158 if ((*ctrl)->is_top()) 159 return; // already stopped 160 bool zero_offset = _igvn.type(offset) == TypeInt::ZERO; 161 if (zero_offset && subseq_length->eqv_uncast(array_length)) 162 return; // common case of whole-array copy 163 Node* last = subseq_length; 164 if (!zero_offset) { // last += offset 165 last = new AddINode(last, offset); 166 transform_later(last); 167 } 168 Node* cmp_lt = new CmpUNode(array_length, last); 169 transform_later(cmp_lt); 170 Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt); 171 transform_later(bol_lt); 172 generate_guard(ctrl, bol_lt, region, PROB_MIN); 173 } 174 175 // 176 // Partial in-lining handling for smaller conjoint/disjoint array copies having 177 // length(in bytes) less than ArrayOperationPartialInlineSize. 178 // if (length <= ArrayOperationPartialInlineSize) { 179 // partial_inlining_block: 180 // mask = Mask_Gen 181 // vload = LoadVectorMasked src , mask 182 // StoreVectorMasked dst, mask, vload 183 // } else { 184 // stub_block: 185 // callstub array_copy 186 // } 187 // exit_block: 188 // Phi = label partial_inlining_block:mem , label stub_block:mem (filled by caller) 189 // mem = MergeMem (Phi) 190 // control = stub_block 191 // 192 // Exit_block and associated phi(memory) are partially initialized for partial_in-lining_block 193 // edges. Remaining edges for exit_block coming from stub_block are connected by the caller 194 // post stub nodes creation. 195 // 196 197 void PhaseMacroExpand::generate_partial_inlining_block(Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type, 198 RegionNode** exit_block, Node** result_memory, Node* length, 199 Node* src_start, Node* dst_start, BasicType type) { 200 const TypePtr *src_adr_type = _igvn.type(src_start)->isa_ptr(); 201 Node* inline_block = nullptr; 202 Node* stub_block = nullptr; 203 204 int const_len = -1; 205 const TypeInt* lty = nullptr; 206 uint shift = exact_log2(type2aelembytes(type)); 207 if (length->Opcode() == Op_ConvI2L) { 208 lty = _igvn.type(length->in(1))->isa_int(); 209 } else { 210 lty = _igvn.type(length)->isa_int(); 211 } 212 if (lty && lty->is_con()) { 213 const_len = lty->get_con() << shift; 214 } 215 216 // Return if copy length is greater than partial inline size limit or 217 // target does not supports masked load/stores. 218 int lane_count = ArrayCopyNode::get_partial_inline_vector_lane_count(type, const_len); 219 if ( const_len > ArrayOperationPartialInlineSize || 220 !Matcher::match_rule_supported_vector(Op_LoadVectorMasked, lane_count, type) || 221 !Matcher::match_rule_supported_vector(Op_StoreVectorMasked, lane_count, type) || 222 !Matcher::match_rule_supported_vector(Op_VectorMaskGen, lane_count, type)) { 223 return; 224 } 225 226 int inline_limit = ArrayOperationPartialInlineSize / type2aelembytes(type); 227 Node* casted_length = new CastLLNode(*ctrl, length, TypeLong::make(0, inline_limit, Type::WidenMin)); 228 transform_later(casted_length); 229 Node* copy_bytes = new LShiftXNode(length, intcon(shift)); 230 transform_later(copy_bytes); 231 232 Node* cmp_le = new CmpULNode(copy_bytes, longcon(ArrayOperationPartialInlineSize)); 233 transform_later(cmp_le); 234 Node* bol_le = new BoolNode(cmp_le, BoolTest::le); 235 transform_later(bol_le); 236 inline_block = generate_guard(ctrl, bol_le, nullptr, PROB_FAIR); 237 stub_block = *ctrl; 238 239 Node* mask_gen = VectorMaskGenNode::make(casted_length, type); 240 transform_later(mask_gen); 241 242 unsigned vec_size = lane_count * type2aelembytes(type); 243 if (C->max_vector_size() < vec_size) { 244 C->set_max_vector_size(vec_size); 245 } 246 247 const TypeVect * vt = TypeVect::make(type, lane_count); 248 Node* mm = (*mem)->memory_at(C->get_alias_index(src_adr_type)); 249 Node* masked_load = new LoadVectorMaskedNode(inline_block, mm, src_start, 250 src_adr_type, vt, mask_gen); 251 transform_later(masked_load); 252 253 mm = (*mem)->memory_at(C->get_alias_index(adr_type)); 254 Node* masked_store = new StoreVectorMaskedNode(inline_block, mm, dst_start, 255 masked_load, adr_type, mask_gen); 256 transform_later(masked_store); 257 258 // Convergence region for inline_block and stub_block. 259 *exit_block = new RegionNode(3); 260 transform_later(*exit_block); 261 (*exit_block)->init_req(1, inline_block); 262 *result_memory = new PhiNode(*exit_block, Type::MEMORY, adr_type); 263 transform_later(*result_memory); 264 (*result_memory)->init_req(1, masked_store); 265 266 *ctrl = stub_block; 267 } 268 269 270 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) { 271 if ((*ctrl)->is_top()) return nullptr; 272 273 if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint] 274 return nullptr; // index is already adequately typed 275 Node* cmp_le = new CmpINode(index, intcon(0)); 276 transform_later(cmp_le); 277 BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le); 278 Node* bol_le = new BoolNode(cmp_le, le_or_eq); 279 transform_later(bol_le); 280 Node* is_notp = generate_guard(ctrl, bol_le, nullptr, PROB_MIN); 281 282 return is_notp; 283 } 284 285 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) { 286 transform_later(call); 287 288 *ctrl = new ProjNode(call,TypeFunc::Control); 289 transform_later(*ctrl); 290 Node* newmem = new ProjNode(call, TypeFunc::Memory); 291 transform_later(newmem); 292 293 uint alias_idx = C->get_alias_index(adr_type); 294 if (alias_idx != Compile::AliasIdxBot) { 295 *mem = MergeMemNode::make(*mem); 296 (*mem)->set_memory_at(alias_idx, newmem); 297 } else { 298 *mem = MergeMemNode::make(newmem); 299 } 300 transform_later(*mem); 301 } 302 303 address PhaseMacroExpand::basictype2arraycopy(BasicType t, 304 Node* src_offset, 305 Node* dest_offset, 306 bool disjoint_bases, 307 const char* &name, 308 bool dest_uninitialized) { 309 const TypeInt* src_offset_inttype = _igvn.find_int_type(src_offset); 310 const TypeInt* dest_offset_inttype = _igvn.find_int_type(dest_offset); 311 312 bool aligned = false; 313 bool disjoint = disjoint_bases; 314 315 // if the offsets are the same, we can treat the memory regions as 316 // disjoint, because either the memory regions are in different arrays, 317 // or they are identical (which we can treat as disjoint.) We can also 318 // treat a copy with a destination index less that the source index 319 // as disjoint since a low->high copy will work correctly in this case. 320 if (src_offset_inttype != nullptr && src_offset_inttype->is_con() && 321 dest_offset_inttype != nullptr && dest_offset_inttype->is_con()) { 322 // both indices are constants 323 int s_offs = src_offset_inttype->get_con(); 324 int d_offs = dest_offset_inttype->get_con(); 325 int element_size = type2aelembytes(t); 326 aligned = ((arrayOopDesc::base_offset_in_bytes(t) + (uint)s_offs * element_size) % HeapWordSize == 0) && 327 ((arrayOopDesc::base_offset_in_bytes(t) + (uint)d_offs * element_size) % HeapWordSize == 0); 328 if (s_offs >= d_offs) disjoint = true; 329 } else if (src_offset == dest_offset && src_offset != nullptr) { 330 // This can occur if the offsets are identical non-constants. 331 disjoint = true; 332 } 333 334 return StubRoutines::select_arraycopy_function(t, aligned, disjoint, name, dest_uninitialized); 335 } 336 337 #define XTOP LP64_ONLY(COMMA top()) 338 339 // Generate an optimized call to arraycopy. 340 // Caller must guard against non-arrays. 341 // Caller must determine a common array basic-type for both arrays. 342 // Caller must validate offsets against array bounds. 343 // The slow_region has already collected guard failure paths 344 // (such as out of bounds length or non-conformable array types). 345 // The generated code has this shape, in general: 346 // 347 // if (length == 0) return // via zero_path 348 // slowval = -1 349 // if (types unknown) { 350 // slowval = call generic copy loop 351 // if (slowval == 0) return // via checked_path 352 // } else if (indexes in bounds) { 353 // if ((is object array) && !(array type check)) { 354 // slowval = call checked copy loop 355 // if (slowval == 0) return // via checked_path 356 // } else { 357 // call bulk copy loop 358 // return // via fast_path 359 // } 360 // } 361 // // adjust params for remaining work: 362 // if (slowval != -1) { 363 // n = -1^slowval; src_offset += n; dest_offset += n; length -= n 364 // } 365 // slow_region: 366 // call slow arraycopy(src, src_offset, dest, dest_offset, length) 367 // return // via slow_call_path 368 // 369 // This routine is used from several intrinsics: System.arraycopy, 370 // Object.clone (the array subcase), and Arrays.copyOf[Range]. 371 // 372 Node* PhaseMacroExpand::generate_arraycopy(ArrayCopyNode *ac, AllocateArrayNode* alloc, 373 Node** ctrl, MergeMemNode* mem, Node** io, 374 const TypePtr* adr_type, 375 BasicType basic_elem_type, 376 Node* src, Node* src_offset, 377 Node* dest, Node* dest_offset, 378 Node* copy_length, 379 bool disjoint_bases, 380 bool length_never_negative, 381 RegionNode* slow_region) { 382 if (slow_region == nullptr) { 383 slow_region = new RegionNode(1); 384 transform_later(slow_region); 385 } 386 387 Node* original_dest = dest; 388 bool dest_needs_zeroing = false; 389 bool acopy_to_uninitialized = false; 390 391 // See if this is the initialization of a newly-allocated array. 392 // If so, we will take responsibility here for initializing it to zero. 393 // (Note: Because tightly_coupled_allocation performs checks on the 394 // out-edges of the dest, we need to avoid making derived pointers 395 // from it until we have checked its uses.) 396 if (ReduceBulkZeroing 397 && !(UseTLAB && ZeroTLAB) // pointless if already zeroed 398 && basic_elem_type != T_CONFLICT // avoid corner case 399 && !src->eqv_uncast(dest) 400 && alloc != nullptr 401 && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0) { 402 assert(ac->is_alloc_tightly_coupled(), "sanity"); 403 // acopy to uninitialized tightly coupled allocations 404 // needs zeroing outside the copy range 405 // and the acopy itself will be to uninitialized memory 406 acopy_to_uninitialized = true; 407 if (alloc->maybe_set_complete(&_igvn)) { 408 // "You break it, you buy it." 409 InitializeNode* init = alloc->initialization(); 410 assert(init->is_complete(), "we just did this"); 411 init->set_complete_with_arraycopy(); 412 assert(dest->is_CheckCastPP(), "sanity"); 413 assert(dest->in(0)->in(0) == init, "dest pinned"); 414 adr_type = TypeRawPtr::BOTTOM; // all initializations are into raw memory 415 // From this point on, every exit path is responsible for 416 // initializing any non-copied parts of the object to zero. 417 // Also, if this flag is set we make sure that arraycopy interacts properly 418 // with G1, eliding pre-barriers. See CR 6627983. 419 dest_needs_zeroing = true; 420 } else { 421 // dest_need_zeroing = false; 422 } 423 } else { 424 // No zeroing elimination needed here. 425 alloc = nullptr; 426 acopy_to_uninitialized = false; 427 //original_dest = dest; 428 //dest_needs_zeroing = false; 429 } 430 431 uint alias_idx = C->get_alias_index(adr_type); 432 433 // Results are placed here: 434 enum { fast_path = 1, // normal void-returning assembly stub 435 checked_path = 2, // special assembly stub with cleanup 436 slow_call_path = 3, // something went wrong; call the VM 437 zero_path = 4, // bypass when length of copy is zero 438 bcopy_path = 5, // copy primitive array by 64-bit blocks 439 PATH_LIMIT = 6 440 }; 441 RegionNode* result_region = new RegionNode(PATH_LIMIT); 442 PhiNode* result_i_o = new PhiNode(result_region, Type::ABIO); 443 PhiNode* result_memory = new PhiNode(result_region, Type::MEMORY, adr_type); 444 assert(adr_type != TypePtr::BOTTOM, "must be RawMem or a T[] slice"); 445 transform_later(result_region); 446 transform_later(result_i_o); 447 transform_later(result_memory); 448 449 // The slow_control path: 450 Node* slow_control; 451 Node* slow_i_o = *io; 452 Node* slow_mem = mem->memory_at(alias_idx); 453 DEBUG_ONLY(slow_control = (Node*) badAddress); 454 455 // Checked control path: 456 Node* checked_control = top(); 457 Node* checked_mem = nullptr; 458 Node* checked_i_o = nullptr; 459 Node* checked_value = nullptr; 460 461 if (basic_elem_type == T_CONFLICT) { 462 assert(!dest_needs_zeroing, ""); 463 Node* cv = generate_generic_arraycopy(ctrl, &mem, 464 adr_type, 465 src, src_offset, dest, dest_offset, 466 copy_length, acopy_to_uninitialized); 467 if (cv == nullptr) cv = intcon(-1); // failure (no stub available) 468 checked_control = *ctrl; 469 checked_i_o = *io; 470 checked_mem = mem->memory_at(alias_idx); 471 checked_value = cv; 472 *ctrl = top(); 473 } 474 475 Node* not_pos = generate_nonpositive_guard(ctrl, copy_length, length_never_negative); 476 if (not_pos != nullptr) { 477 Node* local_ctrl = not_pos, *local_io = *io; 478 MergeMemNode* local_mem = MergeMemNode::make(mem); 479 transform_later(local_mem); 480 481 // (6) length must not be negative. 482 if (!length_never_negative) { 483 generate_negative_guard(&local_ctrl, copy_length, slow_region); 484 } 485 486 // copy_length is 0. 487 if (dest_needs_zeroing) { 488 assert(!local_ctrl->is_top(), "no ctrl?"); 489 Node* dest_length = alloc->in(AllocateNode::ALength); 490 if (copy_length->eqv_uncast(dest_length) 491 || _igvn.find_int_con(dest_length, 1) <= 0) { 492 // There is no zeroing to do. No need for a secondary raw memory barrier. 493 } else { 494 // Clear the whole thing since there are no source elements to copy. 495 generate_clear_array(local_ctrl, local_mem, 496 adr_type, dest, basic_elem_type, 497 intcon(0), nullptr, 498 alloc->in(AllocateNode::AllocSize)); 499 // Use a secondary InitializeNode as raw memory barrier. 500 // Currently it is needed only on this path since other 501 // paths have stub or runtime calls as raw memory barriers. 502 MemBarNode* mb = MemBarNode::make(C, Op_Initialize, 503 Compile::AliasIdxRaw, 504 top()); 505 transform_later(mb); 506 mb->set_req(TypeFunc::Control,local_ctrl); 507 mb->set_req(TypeFunc::Memory, local_mem->memory_at(Compile::AliasIdxRaw)); 508 local_ctrl = transform_later(new ProjNode(mb, TypeFunc::Control)); 509 local_mem->set_memory_at(Compile::AliasIdxRaw, transform_later(new ProjNode(mb, TypeFunc::Memory))); 510 511 InitializeNode* init = mb->as_Initialize(); 512 init->set_complete(&_igvn); // (there is no corresponding AllocateNode) 513 } 514 } 515 516 // Present the results of the fast call. 517 result_region->init_req(zero_path, local_ctrl); 518 result_i_o ->init_req(zero_path, local_io); 519 result_memory->init_req(zero_path, local_mem->memory_at(alias_idx)); 520 } 521 522 if (!(*ctrl)->is_top() && dest_needs_zeroing) { 523 // We have to initialize the *uncopied* part of the array to zero. 524 // The copy destination is the slice dest[off..off+len]. The other slices 525 // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length]. 526 Node* dest_size = alloc->in(AllocateNode::AllocSize); 527 Node* dest_length = alloc->in(AllocateNode::ALength); 528 Node* dest_tail = transform_later( new AddINode(dest_offset, copy_length)); 529 530 // If there is a head section that needs zeroing, do it now. 531 if (_igvn.find_int_con(dest_offset, -1) != 0) { 532 generate_clear_array(*ctrl, mem, 533 adr_type, dest, basic_elem_type, 534 intcon(0), dest_offset, 535 nullptr); 536 } 537 538 // Next, perform a dynamic check on the tail length. 539 // It is often zero, and we can win big if we prove this. 540 // There are two wins: Avoid generating the ClearArray 541 // with its attendant messy index arithmetic, and upgrade 542 // the copy to a more hardware-friendly word size of 64 bits. 543 Node* tail_ctl = nullptr; 544 if (!(*ctrl)->is_top() && !dest_tail->eqv_uncast(dest_length)) { 545 Node* cmp_lt = transform_later( new CmpINode(dest_tail, dest_length) ); 546 Node* bol_lt = transform_later( new BoolNode(cmp_lt, BoolTest::lt) ); 547 tail_ctl = generate_slow_guard(ctrl, bol_lt, nullptr); 548 assert(tail_ctl != nullptr || !(*ctrl)->is_top(), "must be an outcome"); 549 } 550 551 // At this point, let's assume there is no tail. 552 if (!(*ctrl)->is_top() && alloc != nullptr && basic_elem_type != T_OBJECT) { 553 // There is no tail. Try an upgrade to a 64-bit copy. 554 bool didit = false; 555 { 556 Node* local_ctrl = *ctrl, *local_io = *io; 557 MergeMemNode* local_mem = MergeMemNode::make(mem); 558 transform_later(local_mem); 559 560 didit = generate_block_arraycopy(&local_ctrl, &local_mem, local_io, 561 adr_type, basic_elem_type, alloc, 562 src, src_offset, dest, dest_offset, 563 dest_size, acopy_to_uninitialized); 564 if (didit) { 565 // Present the results of the block-copying fast call. 566 result_region->init_req(bcopy_path, local_ctrl); 567 result_i_o ->init_req(bcopy_path, local_io); 568 result_memory->init_req(bcopy_path, local_mem->memory_at(alias_idx)); 569 } 570 } 571 if (didit) { 572 *ctrl = top(); // no regular fast path 573 } 574 } 575 576 // Clear the tail, if any. 577 if (tail_ctl != nullptr) { 578 Node* notail_ctl = (*ctrl)->is_top() ? nullptr : *ctrl; 579 *ctrl = tail_ctl; 580 if (notail_ctl == nullptr) { 581 generate_clear_array(*ctrl, mem, 582 adr_type, dest, basic_elem_type, 583 dest_tail, nullptr, 584 dest_size); 585 } else { 586 // Make a local merge. 587 Node* done_ctl = transform_later(new RegionNode(3)); 588 Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type)); 589 done_ctl->init_req(1, notail_ctl); 590 done_mem->init_req(1, mem->memory_at(alias_idx)); 591 generate_clear_array(*ctrl, mem, 592 adr_type, dest, basic_elem_type, 593 dest_tail, nullptr, 594 dest_size); 595 done_ctl->init_req(2, *ctrl); 596 done_mem->init_req(2, mem->memory_at(alias_idx)); 597 *ctrl = done_ctl; 598 mem->set_memory_at(alias_idx, done_mem); 599 } 600 } 601 } 602 603 BasicType copy_type = basic_elem_type; 604 assert(basic_elem_type != T_ARRAY, "caller must fix this"); 605 if (!(*ctrl)->is_top() && copy_type == T_OBJECT) { 606 // If src and dest have compatible element types, we can copy bits. 607 // Types S[] and D[] are compatible if D is a supertype of S. 608 // 609 // If they are not, we will use checked_oop_disjoint_arraycopy, 610 // which performs a fast optimistic per-oop check, and backs off 611 // further to JVM_ArrayCopy on the first per-oop check that fails. 612 // (Actually, we don't move raw bits only; the GC requires card marks.) 613 614 // We don't need a subtype check for validated copies and Object[].clone() 615 bool skip_subtype_check = ac->is_arraycopy_validated() || ac->is_copyof_validated() || 616 ac->is_copyofrange_validated() || ac->is_clone_oop_array(); 617 if (!skip_subtype_check) { 618 // Get the klass* for both src and dest 619 Node* src_klass = ac->in(ArrayCopyNode::SrcKlass); 620 Node* dest_klass = ac->in(ArrayCopyNode::DestKlass); 621 622 assert(src_klass != nullptr && dest_klass != nullptr, "should have klasses"); 623 624 // Generate the subtype check. 625 // This might fold up statically, or then again it might not. 626 // 627 // Non-static example: Copying List<String>.elements to a new String[]. 628 // The backing store for a List<String> is always an Object[], 629 // but its elements are always type String, if the generic types 630 // are correct at the source level. 631 // 632 // Test S[] against D[], not S against D, because (probably) 633 // the secondary supertype cache is less busy for S[] than S. 634 // This usually only matters when D is an interface. 635 Node* not_subtype_ctrl = Phase::gen_subtype_check(src_klass, dest_klass, ctrl, mem, _igvn, nullptr, -1); 636 // Plug failing path into checked_oop_disjoint_arraycopy 637 if (not_subtype_ctrl != top()) { 638 Node* local_ctrl = not_subtype_ctrl; 639 MergeMemNode* local_mem = MergeMemNode::make(mem); 640 transform_later(local_mem); 641 642 // (At this point we can assume disjoint_bases, since types differ.) 643 int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset()); 644 Node* p1 = basic_plus_adr(dest_klass, ek_offset); 645 Node* n1 = LoadKlassNode::make(_igvn, nullptr, C->immutable_memory(), p1, TypeRawPtr::BOTTOM); 646 Node* dest_elem_klass = transform_later(n1); 647 Node* cv = generate_checkcast_arraycopy(&local_ctrl, &local_mem, 648 adr_type, 649 dest_elem_klass, 650 src, src_offset, dest, dest_offset, 651 ConvI2X(copy_length), acopy_to_uninitialized); 652 if (cv == nullptr) cv = intcon(-1); // failure (no stub available) 653 checked_control = local_ctrl; 654 checked_i_o = *io; 655 checked_mem = local_mem->memory_at(alias_idx); 656 checked_value = cv; 657 } 658 } 659 // At this point we know we do not need type checks on oop stores. 660 661 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 662 if (!bs->array_copy_requires_gc_barriers(alloc != nullptr, copy_type, false, false, BarrierSetC2::Expansion)) { 663 // If we do not need gc barriers, copy using the jint or jlong stub. 664 copy_type = LP64_ONLY(UseCompressedOops ? T_INT : T_LONG) NOT_LP64(T_INT); 665 assert(type2aelembytes(basic_elem_type) == type2aelembytes(copy_type), 666 "sizes agree"); 667 } 668 } 669 670 bool is_partial_array_copy = false; 671 if (!(*ctrl)->is_top()) { 672 // Generate the fast path, if possible. 673 Node* local_ctrl = *ctrl; 674 MergeMemNode* local_mem = MergeMemNode::make(mem); 675 transform_later(local_mem); 676 is_partial_array_copy = generate_unchecked_arraycopy(&local_ctrl, &local_mem, 677 adr_type, copy_type, disjoint_bases, 678 src, src_offset, dest, dest_offset, 679 ConvI2X(copy_length), acopy_to_uninitialized); 680 681 // Present the results of the fast call. 682 result_region->init_req(fast_path, local_ctrl); 683 result_i_o ->init_req(fast_path, *io); 684 result_memory->init_req(fast_path, local_mem->memory_at(alias_idx)); 685 } 686 687 // Here are all the slow paths up to this point, in one bundle: 688 assert(slow_region != nullptr, "allocated on entry"); 689 slow_control = slow_region; 690 DEBUG_ONLY(slow_region = (RegionNode*)badAddress); 691 692 *ctrl = checked_control; 693 if (!(*ctrl)->is_top()) { 694 // Clean up after the checked call. 695 // The returned value is either 0 or -1^K, 696 // where K = number of partially transferred array elements. 697 Node* cmp = new CmpINode(checked_value, intcon(0)); 698 transform_later(cmp); 699 Node* bol = new BoolNode(cmp, BoolTest::eq); 700 transform_later(bol); 701 IfNode* iff = new IfNode(*ctrl, bol, PROB_MAX, COUNT_UNKNOWN); 702 transform_later(iff); 703 704 // If it is 0, we are done, so transfer to the end. 705 Node* checks_done = new IfTrueNode(iff); 706 transform_later(checks_done); 707 result_region->init_req(checked_path, checks_done); 708 result_i_o ->init_req(checked_path, checked_i_o); 709 result_memory->init_req(checked_path, checked_mem); 710 711 // If it is not zero, merge into the slow call. 712 *ctrl = new IfFalseNode(iff); 713 transform_later(*ctrl); 714 RegionNode* slow_reg2 = new RegionNode(3); 715 PhiNode* slow_i_o2 = new PhiNode(slow_reg2, Type::ABIO); 716 PhiNode* slow_mem2 = new PhiNode(slow_reg2, Type::MEMORY, adr_type); 717 transform_later(slow_reg2); 718 transform_later(slow_i_o2); 719 transform_later(slow_mem2); 720 slow_reg2 ->init_req(1, slow_control); 721 slow_i_o2 ->init_req(1, slow_i_o); 722 slow_mem2 ->init_req(1, slow_mem); 723 slow_reg2 ->init_req(2, *ctrl); 724 slow_i_o2 ->init_req(2, checked_i_o); 725 slow_mem2 ->init_req(2, checked_mem); 726 727 slow_control = slow_reg2; 728 slow_i_o = slow_i_o2; 729 slow_mem = slow_mem2; 730 731 if (alloc != nullptr) { 732 // We'll restart from the very beginning, after zeroing the whole thing. 733 // This can cause double writes, but that's OK since dest is brand new. 734 // So we ignore the low 31 bits of the value returned from the stub. 735 } else { 736 // We must continue the copy exactly where it failed, or else 737 // another thread might see the wrong number of writes to dest. 738 Node* checked_offset = new XorINode(checked_value, intcon(-1)); 739 Node* slow_offset = new PhiNode(slow_reg2, TypeInt::INT); 740 transform_later(checked_offset); 741 transform_later(slow_offset); 742 slow_offset->init_req(1, intcon(0)); 743 slow_offset->init_req(2, checked_offset); 744 745 // Adjust the arguments by the conditionally incoming offset. 746 Node* src_off_plus = new AddINode(src_offset, slow_offset); 747 transform_later(src_off_plus); 748 Node* dest_off_plus = new AddINode(dest_offset, slow_offset); 749 transform_later(dest_off_plus); 750 Node* length_minus = new SubINode(copy_length, slow_offset); 751 transform_later(length_minus); 752 753 // Tweak the node variables to adjust the code produced below: 754 src_offset = src_off_plus; 755 dest_offset = dest_off_plus; 756 copy_length = length_minus; 757 } 758 } 759 *ctrl = slow_control; 760 if (!(*ctrl)->is_top()) { 761 Node* local_ctrl = *ctrl, *local_io = slow_i_o; 762 MergeMemNode* local_mem = MergeMemNode::make(mem); 763 transform_later(local_mem); 764 765 // Generate the slow path, if needed. 766 local_mem->set_memory_at(alias_idx, slow_mem); 767 768 if (dest_needs_zeroing) { 769 generate_clear_array(local_ctrl, local_mem, 770 adr_type, dest, basic_elem_type, 771 intcon(0), nullptr, 772 alloc->in(AllocateNode::AllocSize)); 773 } 774 775 local_mem = generate_slow_arraycopy(ac, 776 &local_ctrl, local_mem, &local_io, 777 adr_type, 778 src, src_offset, dest, dest_offset, 779 copy_length, /*dest_uninitialized*/false); 780 781 result_region->init_req(slow_call_path, local_ctrl); 782 result_i_o ->init_req(slow_call_path, local_io); 783 result_memory->init_req(slow_call_path, local_mem->memory_at(alias_idx)); 784 } else { 785 ShouldNotReachHere(); // no call to generate_slow_arraycopy: 786 // projections were not extracted 787 } 788 789 // Remove unused edges. 790 for (uint i = 1; i < result_region->req(); i++) { 791 if (result_region->in(i) == nullptr) { 792 result_region->init_req(i, top()); 793 } 794 } 795 796 // Finished; return the combined state. 797 *ctrl = result_region; 798 *io = result_i_o; 799 mem->set_memory_at(alias_idx, result_memory); 800 801 // mem no longer guaranteed to stay a MergeMemNode 802 Node* out_mem = mem; 803 DEBUG_ONLY(mem = nullptr); 804 805 // The memory edges above are precise in order to model effects around 806 // array copies accurately to allow value numbering of field loads around 807 // arraycopy. Such field loads, both before and after, are common in Java 808 // collections and similar classes involving header/array data structures. 809 // 810 // But with low number of register or when some registers are used or killed 811 // by arraycopy calls it causes registers spilling on stack. See 6544710. 812 // The next memory barrier is added to avoid it. If the arraycopy can be 813 // optimized away (which it can, sometimes) then we can manually remove 814 // the membar also. 815 // 816 // Do not let reads from the cloned object float above the arraycopy. 817 if (alloc != nullptr && !alloc->initialization()->does_not_escape()) { 818 // Do not let stores that initialize this object be reordered with 819 // a subsequent store that would make this object accessible by 820 // other threads. 821 insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore); 822 } else { 823 insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder); 824 } 825 826 if (is_partial_array_copy) { 827 assert((*ctrl)->is_Proj(), "MemBar control projection"); 828 assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node"); 829 (*ctrl)->in(0)->isa_MemBar()->set_trailing_partial_array_copy(); 830 } 831 832 _igvn.replace_node(_callprojs.fallthrough_memproj, out_mem); 833 if (_callprojs.fallthrough_ioproj != nullptr) { 834 _igvn.replace_node(_callprojs.fallthrough_ioproj, *io); 835 } 836 _igvn.replace_node(_callprojs.fallthrough_catchproj, *ctrl); 837 838 #ifdef ASSERT 839 const TypeOopPtr* dest_t = _igvn.type(dest)->is_oopptr(); 840 if (dest_t->is_known_instance() && !is_partial_array_copy) { 841 ArrayCopyNode* ac = nullptr; 842 assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost"); 843 assert(ac == nullptr, "no arraycopy anymore"); 844 } 845 #endif 846 847 return out_mem; 848 } 849 850 // Helper for initialization of arrays, creating a ClearArray. 851 // It writes zero bits in [start..end), within the body of an array object. 852 // The memory effects are all chained onto the 'adr_type' alias category. 853 // 854 // Since the object is otherwise uninitialized, we are free 855 // to put a little "slop" around the edges of the cleared area, 856 // as long as it does not go back into the array's header, 857 // or beyond the array end within the heap. 858 // 859 // The lower edge can be rounded down to the nearest jint and the 860 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes. 861 // 862 // Arguments: 863 // adr_type memory slice where writes are generated 864 // dest oop of the destination array 865 // basic_elem_type element type of the destination 866 // slice_idx array index of first element to store 867 // slice_len number of elements to store (or null) 868 // dest_size total size in bytes of the array object 869 // 870 // Exactly one of slice_len or dest_size must be non-null. 871 // If dest_size is non-null, zeroing extends to the end of the object. 872 // If slice_len is non-null, the slice_idx value must be a constant. 873 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem, 874 const TypePtr* adr_type, 875 Node* dest, 876 BasicType basic_elem_type, 877 Node* slice_idx, 878 Node* slice_len, 879 Node* dest_size) { 880 // one or the other but not both of slice_len and dest_size: 881 assert((slice_len != nullptr? 1: 0) + (dest_size != nullptr? 1: 0) == 1, ""); 882 if (slice_len == nullptr) slice_len = top(); 883 if (dest_size == nullptr) dest_size = top(); 884 885 uint alias_idx = C->get_alias_index(adr_type); 886 887 // operate on this memory slice: 888 Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on 889 890 // scaling and rounding of indexes: 891 int scale = exact_log2(type2aelembytes(basic_elem_type)); 892 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type); 893 int clear_low = (-1 << scale) & (BytesPerInt - 1); 894 int bump_bit = (-1 << scale) & BytesPerInt; 895 896 // determine constant starts and ends 897 const intptr_t BIG_NEG = -128; 898 assert(BIG_NEG + 2*abase < 0, "neg enough"); 899 intptr_t slice_idx_con = (intptr_t) _igvn.find_int_con(slice_idx, BIG_NEG); 900 intptr_t slice_len_con = (intptr_t) _igvn.find_int_con(slice_len, BIG_NEG); 901 if (slice_len_con == 0) { 902 return; // nothing to do here 903 } 904 intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low; 905 intptr_t end_con = _igvn.find_intptr_t_con(dest_size, -1); 906 if (slice_idx_con >= 0 && slice_len_con >= 0) { 907 assert(end_con < 0, "not two cons"); 908 end_con = align_up(abase + ((slice_idx_con + slice_len_con) << scale), 909 BytesPerLong); 910 } 911 912 if (start_con >= 0 && end_con >= 0) { 913 // Constant start and end. Simple. 914 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, 915 start_con, end_con, &_igvn); 916 } else if (start_con >= 0 && dest_size != top()) { 917 // Constant start, pre-rounded end after the tail of the array. 918 Node* end = dest_size; 919 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, 920 start_con, end, &_igvn); 921 } else if (start_con >= 0 && slice_len != top()) { 922 // Constant start, non-constant end. End needs rounding up. 923 // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8) 924 intptr_t end_base = abase + (slice_idx_con << scale); 925 int end_round = (-1 << scale) & (BytesPerLong - 1); 926 Node* end = ConvI2X(slice_len); 927 if (scale != 0) 928 end = transform_later(new LShiftXNode(end, intcon(scale) )); 929 end_base += end_round; 930 end = transform_later(new AddXNode(end, MakeConX(end_base)) ); 931 end = transform_later(new AndXNode(end, MakeConX(~end_round)) ); 932 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, 933 start_con, end, &_igvn); 934 } else if (start_con < 0 && dest_size != top()) { 935 // Non-constant start, pre-rounded end after the tail of the array. 936 // This is almost certainly a "round-to-end" operation. 937 Node* start = slice_idx; 938 start = ConvI2X(start); 939 if (scale != 0) 940 start = transform_later(new LShiftXNode( start, intcon(scale) )); 941 start = transform_later(new AddXNode(start, MakeConX(abase)) ); 942 if ((bump_bit | clear_low) != 0) { 943 int to_clear = (bump_bit | clear_low); 944 // Align up mod 8, then store a jint zero unconditionally 945 // just before the mod-8 boundary. 946 if (((abase + bump_bit) & ~to_clear) - bump_bit 947 < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) { 948 bump_bit = 0; 949 assert((abase & to_clear) == 0, "array base must be long-aligned"); 950 } else { 951 // Bump 'start' up to (or past) the next jint boundary: 952 start = transform_later( new AddXNode(start, MakeConX(bump_bit)) ); 953 assert((abase & clear_low) == 0, "array base must be int-aligned"); 954 } 955 // Round bumped 'start' down to jlong boundary in body of array. 956 start = transform_later(new AndXNode(start, MakeConX(~to_clear)) ); 957 if (bump_bit != 0) { 958 // Store a zero to the immediately preceding jint: 959 Node* x1 = transform_later(new AddXNode(start, MakeConX(-bump_bit)) ); 960 Node* p1 = basic_plus_adr(dest, x1); 961 mem = StoreNode::make(_igvn, ctrl, mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered); 962 mem = transform_later(mem); 963 } 964 } 965 Node* end = dest_size; // pre-rounded 966 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, 967 start, end, &_igvn); 968 } else { 969 // Non-constant start, unrounded non-constant end. 970 // (Nobody zeroes a random midsection of an array using this routine.) 971 ShouldNotReachHere(); // fix caller 972 } 973 974 // Done. 975 merge_mem->set_memory_at(alias_idx, mem); 976 } 977 978 bool PhaseMacroExpand::generate_block_arraycopy(Node** ctrl, MergeMemNode** mem, Node* io, 979 const TypePtr* adr_type, 980 BasicType basic_elem_type, 981 AllocateNode* alloc, 982 Node* src, Node* src_offset, 983 Node* dest, Node* dest_offset, 984 Node* dest_size, bool dest_uninitialized) { 985 // See if there is an advantage from block transfer. 986 int scale = exact_log2(type2aelembytes(basic_elem_type)); 987 if (scale >= LogBytesPerLong) 988 return false; // it is already a block transfer 989 990 // Look at the alignment of the starting offsets. 991 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type); 992 993 intptr_t src_off_con = (intptr_t) _igvn.find_int_con(src_offset, -1); 994 intptr_t dest_off_con = (intptr_t) _igvn.find_int_con(dest_offset, -1); 995 if (src_off_con < 0 || dest_off_con < 0) { 996 // At present, we can only understand constants. 997 return false; 998 } 999 1000 intptr_t src_off = abase + (src_off_con << scale); 1001 intptr_t dest_off = abase + (dest_off_con << scale); 1002 1003 if (((src_off | dest_off) & (BytesPerLong-1)) != 0) { 1004 // Non-aligned; too bad. 1005 // One more chance: Pick off an initial 32-bit word. 1006 // This is a common case, since abase can be odd mod 8. 1007 if (((src_off | dest_off) & (BytesPerLong-1)) == BytesPerInt && 1008 ((src_off ^ dest_off) & (BytesPerLong-1)) == 0) { 1009 Node* sptr = basic_plus_adr(src, src_off); 1010 Node* dptr = basic_plus_adr(dest, dest_off); 1011 const TypePtr* s_adr_type = _igvn.type(sptr)->is_ptr(); 1012 assert(s_adr_type->isa_aryptr(), "impossible slice"); 1013 uint s_alias_idx = C->get_alias_index(s_adr_type); 1014 uint d_alias_idx = C->get_alias_index(adr_type); 1015 bool is_mismatched = (basic_elem_type != T_INT); 1016 Node* sval = transform_later( 1017 LoadNode::make(_igvn, *ctrl, (*mem)->memory_at(s_alias_idx), sptr, s_adr_type, 1018 TypeInt::INT, T_INT, MemNode::unordered, LoadNode::DependsOnlyOnTest, 1019 false /*require_atomic_access*/, false /*unaligned*/, is_mismatched)); 1020 Node* st = transform_later( 1021 StoreNode::make(_igvn, *ctrl, (*mem)->memory_at(d_alias_idx), dptr, adr_type, 1022 sval, T_INT, MemNode::unordered)); 1023 if (is_mismatched) { 1024 st->as_Store()->set_mismatched_access(); 1025 } 1026 (*mem)->set_memory_at(d_alias_idx, st); 1027 src_off += BytesPerInt; 1028 dest_off += BytesPerInt; 1029 } else { 1030 return false; 1031 } 1032 } 1033 assert(src_off % BytesPerLong == 0, ""); 1034 assert(dest_off % BytesPerLong == 0, ""); 1035 1036 // Do this copy by giant steps. 1037 Node* sptr = basic_plus_adr(src, src_off); 1038 Node* dptr = basic_plus_adr(dest, dest_off); 1039 Node* countx = dest_size; 1040 countx = transform_later(new SubXNode(countx, MakeConX(dest_off))); 1041 countx = transform_later(new URShiftXNode(countx, intcon(LogBytesPerLong))); 1042 1043 bool disjoint_bases = true; // since alloc isn't null 1044 generate_unchecked_arraycopy(ctrl, mem, 1045 adr_type, T_LONG, disjoint_bases, 1046 sptr, nullptr, dptr, nullptr, countx, dest_uninitialized); 1047 1048 return true; 1049 } 1050 1051 // Helper function; generates code for the slow case. 1052 // We make a call to a runtime method which emulates the native method, 1053 // but without the native wrapper overhead. 1054 MergeMemNode* PhaseMacroExpand::generate_slow_arraycopy(ArrayCopyNode *ac, 1055 Node** ctrl, Node* mem, Node** io, 1056 const TypePtr* adr_type, 1057 Node* src, Node* src_offset, 1058 Node* dest, Node* dest_offset, 1059 Node* copy_length, bool dest_uninitialized) { 1060 assert(!dest_uninitialized, "Invariant"); 1061 1062 const TypeFunc* call_type = OptoRuntime::slow_arraycopy_Type(); 1063 CallNode* call = new CallStaticJavaNode(call_type, OptoRuntime::slow_arraycopy_Java(), 1064 "slow_arraycopy", TypePtr::BOTTOM); 1065 1066 call->init_req(TypeFunc::Control, *ctrl); 1067 call->init_req(TypeFunc::I_O , *io); 1068 call->init_req(TypeFunc::Memory , mem); 1069 call->init_req(TypeFunc::ReturnAdr, top()); 1070 call->init_req(TypeFunc::FramePtr, top()); 1071 call->init_req(TypeFunc::Parms+0, src); 1072 call->init_req(TypeFunc::Parms+1, src_offset); 1073 call->init_req(TypeFunc::Parms+2, dest); 1074 call->init_req(TypeFunc::Parms+3, dest_offset); 1075 call->init_req(TypeFunc::Parms+4, copy_length); 1076 call->copy_call_debug_info(&_igvn, ac); 1077 1078 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON. 1079 _igvn.replace_node(ac, call); 1080 transform_later(call); 1081 1082 call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/); 1083 *ctrl = _callprojs.fallthrough_catchproj->clone(); 1084 transform_later(*ctrl); 1085 1086 Node* m = _callprojs.fallthrough_memproj->clone(); 1087 transform_later(m); 1088 1089 uint alias_idx = C->get_alias_index(adr_type); 1090 MergeMemNode* out_mem; 1091 if (alias_idx != Compile::AliasIdxBot) { 1092 out_mem = MergeMemNode::make(mem); 1093 out_mem->set_memory_at(alias_idx, m); 1094 } else { 1095 out_mem = MergeMemNode::make(m); 1096 } 1097 transform_later(out_mem); 1098 1099 // When src is negative and arraycopy is before an infinite loop,_callprojs.fallthrough_ioproj 1100 // could be null. Skip clone and update null fallthrough_ioproj. 1101 if (_callprojs.fallthrough_ioproj != nullptr) { 1102 *io = _callprojs.fallthrough_ioproj->clone(); 1103 transform_later(*io); 1104 } else { 1105 *io = nullptr; 1106 } 1107 1108 return out_mem; 1109 } 1110 1111 // Helper function; generates code for cases requiring runtime checks. 1112 Node* PhaseMacroExpand::generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem, 1113 const TypePtr* adr_type, 1114 Node* dest_elem_klass, 1115 Node* src, Node* src_offset, 1116 Node* dest, Node* dest_offset, 1117 Node* copy_length, bool dest_uninitialized) { 1118 if ((*ctrl)->is_top()) return nullptr; 1119 1120 address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized); 1121 if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path. 1122 return nullptr; 1123 } 1124 1125 // Pick out the parameters required to perform a store-check 1126 // for the target array. This is an optimistic check. It will 1127 // look in each non-null element's class, at the desired klass's 1128 // super_check_offset, for the desired klass. 1129 int sco_offset = in_bytes(Klass::super_check_offset_offset()); 1130 Node* p3 = basic_plus_adr(dest_elem_klass, sco_offset); 1131 Node* n3 = new LoadINode(nullptr, *mem /*memory(p3)*/, p3, _igvn.type(p3)->is_ptr(), TypeInt::INT, MemNode::unordered); 1132 Node* check_offset = ConvI2X(transform_later(n3)); 1133 Node* check_value = dest_elem_klass; 1134 1135 Node* src_start = array_element_address(src, src_offset, T_OBJECT); 1136 Node* dest_start = array_element_address(dest, dest_offset, T_OBJECT); 1137 1138 const TypeFunc* call_type = OptoRuntime::checkcast_arraycopy_Type(); 1139 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "checkcast_arraycopy", adr_type, 1140 src_start, dest_start, copy_length XTOP, check_offset XTOP, check_value); 1141 1142 finish_arraycopy_call(call, ctrl, mem, adr_type); 1143 1144 Node* proj = new ProjNode(call, TypeFunc::Parms); 1145 transform_later(proj); 1146 1147 return proj; 1148 } 1149 1150 // Helper function; generates code for cases requiring runtime checks. 1151 Node* PhaseMacroExpand::generate_generic_arraycopy(Node** ctrl, MergeMemNode** mem, 1152 const TypePtr* adr_type, 1153 Node* src, Node* src_offset, 1154 Node* dest, Node* dest_offset, 1155 Node* copy_length, bool dest_uninitialized) { 1156 if ((*ctrl)->is_top()) return nullptr; 1157 assert(!dest_uninitialized, "Invariant"); 1158 1159 address copyfunc_addr = StubRoutines::generic_arraycopy(); 1160 if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path. 1161 return nullptr; 1162 } 1163 1164 const TypeFunc* call_type = OptoRuntime::generic_arraycopy_Type(); 1165 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "generic_arraycopy", adr_type, 1166 src, src_offset, dest, dest_offset, copy_length); 1167 1168 finish_arraycopy_call(call, ctrl, mem, adr_type); 1169 1170 Node* proj = new ProjNode(call, TypeFunc::Parms); 1171 transform_later(proj); 1172 1173 return proj; 1174 } 1175 1176 // Helper function; generates the fast out-of-line call to an arraycopy stub. 1177 bool PhaseMacroExpand::generate_unchecked_arraycopy(Node** ctrl, MergeMemNode** mem, 1178 const TypePtr* adr_type, 1179 BasicType basic_elem_type, 1180 bool disjoint_bases, 1181 Node* src, Node* src_offset, 1182 Node* dest, Node* dest_offset, 1183 Node* copy_length, bool dest_uninitialized) { 1184 if ((*ctrl)->is_top()) return false; 1185 1186 Node* src_start = src; 1187 Node* dest_start = dest; 1188 if (src_offset != nullptr || dest_offset != nullptr) { 1189 src_start = array_element_address(src, src_offset, basic_elem_type); 1190 dest_start = array_element_address(dest, dest_offset, basic_elem_type); 1191 } 1192 1193 // Figure out which arraycopy runtime method to call. 1194 const char* copyfunc_name = "arraycopy"; 1195 address copyfunc_addr = 1196 basictype2arraycopy(basic_elem_type, src_offset, dest_offset, 1197 disjoint_bases, copyfunc_name, dest_uninitialized); 1198 1199 Node* result_memory = nullptr; 1200 RegionNode* exit_block = nullptr; 1201 if (ArrayOperationPartialInlineSize > 0 && is_subword_type(basic_elem_type) && 1202 Matcher::vector_width_in_bytes(basic_elem_type) >= 16) { 1203 generate_partial_inlining_block(ctrl, mem, adr_type, &exit_block, &result_memory, 1204 copy_length, src_start, dest_start, basic_elem_type); 1205 } 1206 1207 const TypeFunc* call_type = OptoRuntime::fast_arraycopy_Type(); 1208 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, copyfunc_name, adr_type, 1209 src_start, dest_start, copy_length XTOP); 1210 1211 finish_arraycopy_call(call, ctrl, mem, adr_type); 1212 1213 // Connecting remaining edges for exit_block coming from stub_block. 1214 if (exit_block) { 1215 exit_block->init_req(2, *ctrl); 1216 1217 // Memory edge corresponding to stub_region. 1218 result_memory->init_req(2, *mem); 1219 1220 uint alias_idx = C->get_alias_index(adr_type); 1221 if (alias_idx != Compile::AliasIdxBot) { 1222 *mem = MergeMemNode::make(*mem); 1223 (*mem)->set_memory_at(alias_idx, result_memory); 1224 } else { 1225 *mem = MergeMemNode::make(result_memory); 1226 } 1227 transform_later(*mem); 1228 *ctrl = exit_block; 1229 return true; 1230 } 1231 return false; 1232 } 1233 1234 #undef XTOP 1235 1236 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) { 1237 Node* ctrl = ac->in(TypeFunc::Control); 1238 Node* io = ac->in(TypeFunc::I_O); 1239 Node* src = ac->in(ArrayCopyNode::Src); 1240 Node* src_offset = ac->in(ArrayCopyNode::SrcPos); 1241 Node* dest = ac->in(ArrayCopyNode::Dest); 1242 Node* dest_offset = ac->in(ArrayCopyNode::DestPos); 1243 Node* length = ac->in(ArrayCopyNode::Length); 1244 MergeMemNode* merge_mem = nullptr; 1245 1246 if (ac->is_clonebasic()) { 1247 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1248 bs->clone_at_expansion(this, ac); 1249 return; 1250 } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) { 1251 Node* mem = ac->in(TypeFunc::Memory); 1252 merge_mem = MergeMemNode::make(mem); 1253 transform_later(merge_mem); 1254 1255 AllocateArrayNode* alloc = nullptr; 1256 if (ac->is_alloc_tightly_coupled()) { 1257 alloc = AllocateArrayNode::Ideal_array_allocation(dest); 1258 assert(alloc != nullptr, "expect alloc"); 1259 } 1260 1261 const TypePtr* adr_type = _igvn.type(dest)->is_oopptr()->add_offset(Type::OffsetBot); 1262 if (ac->_dest_type != TypeOopPtr::BOTTOM) { 1263 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr(); 1264 } 1265 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io, 1266 adr_type, T_OBJECT, 1267 src, src_offset, dest, dest_offset, length, 1268 true, ac->has_negative_length_guard()); 1269 1270 return; 1271 } 1272 1273 AllocateArrayNode* alloc = nullptr; 1274 if (ac->is_alloc_tightly_coupled()) { 1275 alloc = AllocateArrayNode::Ideal_array_allocation(dest); 1276 assert(alloc != nullptr, "expect alloc"); 1277 } 1278 1279 assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy"); 1280 1281 // Compile time checks. If any of these checks cannot be verified at compile time, 1282 // we do not make a fast path for this call. Instead, we let the call remain as it 1283 // is. The checks we choose to mandate at compile time are: 1284 // 1285 // (1) src and dest are arrays. 1286 const Type* src_type = src->Value(&_igvn); 1287 const Type* dest_type = dest->Value(&_igvn); 1288 const TypeAryPtr* top_src = src_type->isa_aryptr(); 1289 const TypeAryPtr* top_dest = dest_type->isa_aryptr(); 1290 1291 BasicType src_elem = T_CONFLICT; 1292 BasicType dest_elem = T_CONFLICT; 1293 1294 if (top_src != nullptr && top_src->elem() != Type::BOTTOM) { 1295 src_elem = top_src->elem()->array_element_basic_type(); 1296 } 1297 if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) { 1298 dest_elem = top_dest->elem()->array_element_basic_type(); 1299 } 1300 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT; 1301 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT; 1302 1303 if (ac->is_arraycopy_validated() && 1304 dest_elem != T_CONFLICT && 1305 src_elem == T_CONFLICT) { 1306 src_elem = dest_elem; 1307 } 1308 1309 if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) { 1310 // Conservatively insert a memory barrier on all memory slices. 1311 // Do not let writes into the source float below the arraycopy. 1312 { 1313 Node* mem = ac->in(TypeFunc::Memory); 1314 insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder); 1315 1316 merge_mem = MergeMemNode::make(mem); 1317 transform_later(merge_mem); 1318 } 1319 1320 // Call StubRoutines::generic_arraycopy stub. 1321 Node* mem = generate_arraycopy(ac, nullptr, &ctrl, merge_mem, &io, 1322 TypeRawPtr::BOTTOM, T_CONFLICT, 1323 src, src_offset, dest, dest_offset, length, 1324 // If a negative length guard was generated for the ArrayCopyNode, 1325 // the length of the array can never be negative. 1326 false, ac->has_negative_length_guard()); 1327 return; 1328 } 1329 1330 assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types"); 1331 1332 // (2) src and dest arrays must have elements of the same BasicType 1333 // Figure out the size and type of the elements we will be copying. 1334 if (src_elem != dest_elem || dest_elem == T_VOID) { 1335 // The component types are not the same or are not recognized. Punt. 1336 // (But, avoid the native method wrapper to JVM_ArrayCopy.) 1337 { 1338 Node* mem = ac->in(TypeFunc::Memory); 1339 merge_mem = generate_slow_arraycopy(ac, &ctrl, mem, &io, TypePtr::BOTTOM, src, src_offset, dest, dest_offset, length, false); 1340 } 1341 1342 _igvn.replace_node(_callprojs.fallthrough_memproj, merge_mem); 1343 if (_callprojs.fallthrough_ioproj != nullptr) { 1344 _igvn.replace_node(_callprojs.fallthrough_ioproj, io); 1345 } 1346 _igvn.replace_node(_callprojs.fallthrough_catchproj, ctrl); 1347 return; 1348 } 1349 1350 //--------------------------------------------------------------------------- 1351 // We will make a fast path for this call to arraycopy. 1352 1353 // We have the following tests left to perform: 1354 // 1355 // (3) src and dest must not be null. 1356 // (4) src_offset must not be negative. 1357 // (5) dest_offset must not be negative. 1358 // (6) length must not be negative. 1359 // (7) src_offset + length must not exceed length of src. 1360 // (8) dest_offset + length must not exceed length of dest. 1361 // (9) each element of an oop array must be assignable 1362 1363 { 1364 Node* mem = ac->in(TypeFunc::Memory); 1365 merge_mem = MergeMemNode::make(mem); 1366 transform_later(merge_mem); 1367 } 1368 1369 RegionNode* slow_region = new RegionNode(1); 1370 transform_later(slow_region); 1371 1372 if (!ac->is_arraycopy_validated()) { 1373 // (3) operands must not be null 1374 // We currently perform our null checks with the null_check routine. 1375 // This means that the null exceptions will be reported in the caller 1376 // rather than (correctly) reported inside of the native arraycopy call. 1377 // This should be corrected, given time. We do our null check with the 1378 // stack pointer restored. 1379 // null checks done library_call.cpp 1380 1381 // (4) src_offset must not be negative. 1382 generate_negative_guard(&ctrl, src_offset, slow_region); 1383 1384 // (5) dest_offset must not be negative. 1385 generate_negative_guard(&ctrl, dest_offset, slow_region); 1386 1387 // (6) length must not be negative (moved to generate_arraycopy()). 1388 // generate_negative_guard(length, slow_region); 1389 1390 // (7) src_offset + length must not exceed length of src. 1391 Node* alen = ac->in(ArrayCopyNode::SrcLen); 1392 assert(alen != nullptr, "need src len"); 1393 generate_limit_guard(&ctrl, 1394 src_offset, length, 1395 alen, 1396 slow_region); 1397 1398 // (8) dest_offset + length must not exceed length of dest. 1399 alen = ac->in(ArrayCopyNode::DestLen); 1400 assert(alen != nullptr, "need dest len"); 1401 generate_limit_guard(&ctrl, 1402 dest_offset, length, 1403 alen, 1404 slow_region); 1405 1406 // (9) each element of an oop array must be assignable 1407 // The generate_arraycopy subroutine checks this. 1408 } 1409 // This is where the memory effects are placed: 1410 const TypePtr* adr_type = nullptr; 1411 if (ac->_dest_type != TypeOopPtr::BOTTOM) { 1412 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr(); 1413 } else { 1414 adr_type = TypeAryPtr::get_array_body_type(dest_elem); 1415 } 1416 1417 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io, 1418 adr_type, dest_elem, 1419 src, src_offset, dest, dest_offset, length, 1420 // If a negative length guard was generated for the ArrayCopyNode, 1421 // the length of the array can never be negative. 1422 false, ac->has_negative_length_guard(), slow_region); 1423 }