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