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