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