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