1 /* 2 * Copyright (c) 2016, 2018, 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 "gc/shared/c2/barrierSetC2.hpp" 28 #include "gc/shared/c2/cardTableBarrierSetC2.hpp" 29 #include "opto/arraycopynode.hpp" 30 #include "opto/graphKit.hpp" 31 #include "runtime/sharedRuntime.hpp" 32 #include "utilities/macros.hpp" 33 34 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard) 35 : CallNode(arraycopy_type(), NULL, TypeRawPtr::BOTTOM), 36 _alloc_tightly_coupled(alloc_tightly_coupled), 37 _has_negative_length_guard(has_negative_length_guard), 38 _kind(None), 39 _arguments_validated(false), 40 _src_type(TypeOopPtr::BOTTOM), 41 _dest_type(TypeOopPtr::BOTTOM) { 42 init_class_id(Class_ArrayCopy); 43 init_flags(Flag_is_macro); 44 C->add_macro_node(this); 45 } 46 47 uint ArrayCopyNode::size_of() const { return sizeof(*this); } 48 49 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw, 50 Node* src, Node* src_offset, 51 Node* dest, Node* dest_offset, 52 Node* length, 53 bool alloc_tightly_coupled, 54 bool has_negative_length_guard, 55 Node* src_klass, Node* dest_klass, 56 Node* src_length, Node* dest_length) { 57 58 ArrayCopyNode* ac = new ArrayCopyNode(kit->C, alloc_tightly_coupled, has_negative_length_guard); 59 Node* prev_mem = kit->set_predefined_input_for_runtime_call(ac); 60 61 ac->init_req(ArrayCopyNode::Src, src); 62 ac->init_req(ArrayCopyNode::SrcPos, src_offset); 63 ac->init_req(ArrayCopyNode::Dest, dest); 64 ac->init_req(ArrayCopyNode::DestPos, dest_offset); 65 ac->init_req(ArrayCopyNode::Length, length); 66 ac->init_req(ArrayCopyNode::SrcLen, src_length); 67 ac->init_req(ArrayCopyNode::DestLen, dest_length); 68 ac->init_req(ArrayCopyNode::SrcKlass, src_klass); 69 ac->init_req(ArrayCopyNode::DestKlass, dest_klass); 70 71 if (may_throw) { 72 ac->set_req(TypeFunc::I_O , kit->i_o()); 73 kit->add_safepoint_edges(ac, false); 74 } 75 76 return ac; 77 } 78 79 void ArrayCopyNode::connect_outputs(GraphKit* kit) { 80 kit->set_all_memory_call(this, true); 81 kit->set_control(kit->gvn().transform(new ProjNode(this,TypeFunc::Control))); 82 kit->set_i_o(kit->gvn().transform(new ProjNode(this, TypeFunc::I_O))); 83 kit->make_slow_call_ex(this, kit->env()->Throwable_klass(), true); 84 kit->set_all_memory_call(this); 85 } 86 87 #ifndef PRODUCT 88 const char* ArrayCopyNode::_kind_names[] = {"arraycopy", "arraycopy, validated arguments", "clone", "oop array clone", "CopyOf", "CopyOfRange"}; 89 90 void ArrayCopyNode::dump_spec(outputStream *st) const { 91 CallNode::dump_spec(st); 92 st->print(" (%s%s)", _kind_names[_kind], _alloc_tightly_coupled ? ", tightly coupled allocation" : ""); 93 } 94 95 void ArrayCopyNode::dump_compact_spec(outputStream* st) const { 96 st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : ""); 97 } 98 #endif 99 100 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const { 101 // check that length is constant 102 Node* length = in(ArrayCopyNode::Length); 103 const Type* length_type = phase->type(length); 104 105 if (length_type == Type::TOP) { 106 return -1; 107 } 108 109 assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type"); 110 111 return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1); 112 } 113 114 int ArrayCopyNode::get_count(PhaseGVN *phase) const { 115 Node* src = in(ArrayCopyNode::Src); 116 const Type* src_type = phase->type(src); 117 118 if (is_clonebasic()) { 119 if (src_type->isa_instptr()) { 120 const TypeInstPtr* inst_src = src_type->is_instptr(); 121 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); 122 // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected 123 // fields into account. They are rare anyway so easier to simply 124 // skip instances with injected fields. 125 if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) { 126 return -1; 127 } 128 int nb_fields = ik->nof_nonstatic_fields(); 129 return nb_fields; 130 } else { 131 const TypeAryPtr* ary_src = src_type->isa_aryptr(); 132 assert (ary_src != NULL, "not an array or instance?"); 133 // clone passes a length as a rounded number of longs. If we're 134 // cloning an array we'll do it element by element. If the 135 // length input to ArrayCopyNode is constant, length of input 136 // array must be too. 137 138 assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con() || 139 phase->is_IterGVN(), "inconsistent"); 140 141 if (ary_src->size()->is_con()) { 142 return ary_src->size()->get_con(); 143 } 144 return -1; 145 } 146 } 147 148 return get_length_if_constant(phase); 149 } 150 151 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) { 152 if (!is_clonebasic()) { 153 return NULL; 154 } 155 156 Node* src = in(ArrayCopyNode::Src); 157 Node* dest = in(ArrayCopyNode::Dest); 158 Node* ctl = in(TypeFunc::Control); 159 Node* in_mem = in(TypeFunc::Memory); 160 161 const Type* src_type = phase->type(src); 162 163 assert(src->is_AddP(), "should be base + off"); 164 assert(dest->is_AddP(), "should be base + off"); 165 Node* base_src = src->in(AddPNode::Base); 166 Node* base_dest = dest->in(AddPNode::Base); 167 168 MergeMemNode* mem = MergeMemNode::make(in_mem); 169 170 const TypeInstPtr* inst_src = src_type->isa_instptr(); 171 172 if (inst_src == NULL) { 173 return NULL; 174 } 175 176 if (!inst_src->klass_is_exact()) { 177 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); 178 assert(!ik->is_interface() && !ik->has_subklass(), "inconsistent klass hierarchy"); 179 phase->C->dependencies()->assert_leaf_type(ik); 180 } 181 182 ciInstanceKlass* ik = inst_src->klass()->as_instance_klass(); 183 assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields"); 184 185 for (int i = 0; i < count; i++) { 186 ciField* field = ik->nonstatic_field_at(i); 187 int fieldidx = phase->C->alias_type(field)->index(); 188 const TypePtr* adr_type = phase->C->alias_type(field)->adr_type(); 189 Node* off = phase->MakeConX(field->offset()); 190 Node* next_src = phase->transform(new AddPNode(base_src,base_src,off)); 191 Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off)); 192 BasicType bt = field->layout_type(); 193 194 const Type *type; 195 if (bt == T_OBJECT) { 196 if (!field->type()->is_loaded()) { 197 type = TypeInstPtr::BOTTOM; 198 } else { 199 ciType* field_klass = field->type(); 200 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 201 } 202 } else { 203 type = Type::get_const_basic_type(bt); 204 } 205 206 Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered); 207 v = phase->transform(v); 208 Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered); 209 s = phase->transform(s); 210 mem->set_memory_at(fieldidx, s); 211 } 212 213 if (!finish_transform(phase, can_reshape, ctl, mem)) { 214 // Return NodeSentinel to indicate that the transform failed 215 return NodeSentinel; 216 } 217 218 return mem; 219 } 220 221 bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape, 222 Node*& adr_src, 223 Node*& base_src, 224 Node*& adr_dest, 225 Node*& base_dest, 226 BasicType& copy_type, 227 const Type*& value_type, 228 bool& disjoint_bases) { 229 Node* src = in(ArrayCopyNode::Src); 230 Node* dest = in(ArrayCopyNode::Dest); 231 const Type* src_type = phase->type(src); 232 const TypeAryPtr* ary_src = src_type->isa_aryptr(); 233 234 if (is_arraycopy() || is_copyofrange() || is_copyof()) { 235 const Type* dest_type = phase->type(dest); 236 const TypeAryPtr* ary_dest = dest_type->isa_aryptr(); 237 Node* src_offset = in(ArrayCopyNode::SrcPos); 238 Node* dest_offset = in(ArrayCopyNode::DestPos); 239 240 // newly allocated object is guaranteed to not overlap with source object 241 disjoint_bases = is_alloc_tightly_coupled(); 242 243 if (ary_src == NULL || ary_src->klass() == NULL || 244 ary_dest == NULL || ary_dest->klass() == NULL) { 245 // We don't know if arguments are arrays 246 return false; 247 } 248 249 BasicType src_elem = ary_src->klass()->as_array_klass()->element_type()->basic_type(); 250 BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type(); 251 if (src_elem == T_ARRAY) src_elem = T_OBJECT; 252 if (dest_elem == T_ARRAY) dest_elem = T_OBJECT; 253 254 if (src_elem != dest_elem || dest_elem == T_VOID) { 255 // We don't know if arguments are arrays of the same type 256 return false; 257 } 258 259 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 260 if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() || 261 bs->array_copy_requires_gc_barriers(T_OBJECT))) { 262 // It's an object array copy but we can't emit the card marking 263 // that is needed 264 return false; 265 } 266 267 value_type = ary_src->elem(); 268 269 base_src = src; 270 base_dest = dest; 271 272 uint shift = exact_log2(type2aelembytes(dest_elem)); 273 uint header = arrayOopDesc::base_offset_in_bytes(dest_elem); 274 275 adr_src = src; 276 adr_dest = dest; 277 278 src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size()); 279 dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size()); 280 281 Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift))); 282 Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift))); 283 284 adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale)); 285 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale)); 286 287 adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header)); 288 adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header)); 289 290 adr_src = phase->transform(adr_src); 291 adr_dest = phase->transform(adr_dest); 292 293 copy_type = dest_elem; 294 } else { 295 assert(ary_src != NULL, "should be a clone"); 296 assert(is_clonebasic(), "should be"); 297 298 disjoint_bases = true; 299 assert(src->is_AddP(), "should be base + off"); 300 assert(dest->is_AddP(), "should be base + off"); 301 adr_src = src; 302 base_src = src->in(AddPNode::Base); 303 adr_dest = dest; 304 base_dest = dest->in(AddPNode::Base); 305 306 assert(phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con() == phase->type(dest->in(AddPNode::Offset))->is_intptr_t()->get_con(), "same start offset?"); 307 BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type(); 308 if (elem == T_ARRAY) elem = T_OBJECT; 309 310 int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con(); 311 assert(diff >= 0, "clone should not start after 1st array element"); 312 if (diff > 0) { 313 adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff))); 314 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff))); 315 } 316 317 copy_type = elem; 318 value_type = ary_src->elem(); 319 } 320 return true; 321 } 322 323 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) { 324 const Type* at = phase->type(n); 325 assert(at != Type::TOP, "unexpected type"); 326 const TypePtr* atp = at->isa_ptr(); 327 // adjust atp to be the correct array element address type 328 atp = atp->add_offset(Type::OffsetBot); 329 return atp; 330 } 331 332 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) { 333 Node* ctl = in(TypeFunc::Control); 334 if (!disjoint_bases && count > 1) { 335 Node* src_offset = in(ArrayCopyNode::SrcPos); 336 Node* dest_offset = in(ArrayCopyNode::DestPos); 337 assert(src_offset != NULL && dest_offset != NULL, "should be"); 338 Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset)); 339 Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt)); 340 IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN); 341 342 phase->transform(iff); 343 344 forward_ctl = phase->transform(new IfFalseNode(iff)); 345 backward_ctl = phase->transform(new IfTrueNode(iff)); 346 } else { 347 forward_ctl = ctl; 348 } 349 } 350 351 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase, 352 bool can_reshape, 353 Node* forward_ctl, 354 Node* start_mem_src, 355 Node* start_mem_dest, 356 const TypePtr* atp_src, 357 const TypePtr* atp_dest, 358 Node* adr_src, 359 Node* base_src, 360 Node* adr_dest, 361 Node* base_dest, 362 BasicType copy_type, 363 const Type* value_type, 364 int count) { 365 Node* mem = phase->C->top(); 366 if (!forward_ctl->is_top()) { 367 // copy forward 368 mem = start_mem_dest; 369 uint alias_idx_src = phase->C->get_alias_index(atp_src); 370 uint alias_idx_dest = phase->C->get_alias_index(atp_dest); 371 bool same_alias = (alias_idx_src == alias_idx_dest); 372 373 if (count > 0) { 374 Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered); 375 v = phase->transform(v); 376 mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered); 377 mem = phase->transform(mem); 378 for (int i = 1; i < count; i++) { 379 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i); 380 Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off)); 381 Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off)); 382 v = LoadNode::make(*phase, forward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered); 383 v = phase->transform(v); 384 mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered); 385 mem = phase->transform(mem); 386 } 387 } else if(can_reshape) { 388 PhaseIterGVN* igvn = phase->is_IterGVN(); 389 igvn->_worklist.push(adr_src); 390 igvn->_worklist.push(adr_dest); 391 } 392 } 393 return mem; 394 } 395 396 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase, 397 bool can_reshape, 398 Node* backward_ctl, 399 Node* start_mem_src, 400 Node* start_mem_dest, 401 const TypePtr* atp_src, 402 const TypePtr* atp_dest, 403 Node* adr_src, 404 Node* base_src, 405 Node* adr_dest, 406 Node* base_dest, 407 BasicType copy_type, 408 const Type* value_type, 409 int count) { 410 Node* mem = phase->C->top(); 411 if (!backward_ctl->is_top()) { 412 // copy backward 413 mem = start_mem_dest; 414 uint alias_idx_src = phase->C->get_alias_index(atp_src); 415 uint alias_idx_dest = phase->C->get_alias_index(atp_dest); 416 bool same_alias = (alias_idx_src == alias_idx_dest); 417 418 if (count > 0) { 419 for (int i = count-1; i >= 1; i--) { 420 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i); 421 Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off)); 422 Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off)); 423 Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered); 424 v = phase->transform(v); 425 mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered); 426 mem = phase->transform(mem); 427 } 428 Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered); 429 v = phase->transform(v); 430 mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered); 431 mem = phase->transform(mem); 432 } else if(can_reshape) { 433 PhaseIterGVN* igvn = phase->is_IterGVN(); 434 igvn->_worklist.push(adr_src); 435 igvn->_worklist.push(adr_dest); 436 } 437 } 438 return mem; 439 } 440 441 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape, 442 Node* ctl, Node *mem) { 443 if (can_reshape) { 444 PhaseIterGVN* igvn = phase->is_IterGVN(); 445 igvn->set_delay_transform(false); 446 if (is_clonebasic()) { 447 Node* out_mem = proj_out(TypeFunc::Memory); 448 449 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 450 if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() || 451 out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) { 452 assert(bs->array_copy_requires_gc_barriers(T_OBJECT), "can only happen with card marking"); 453 return false; 454 } 455 456 igvn->replace_node(out_mem->raw_out(0), mem); 457 458 Node* out_ctl = proj_out(TypeFunc::Control); 459 igvn->replace_node(out_ctl, ctl); 460 } else { 461 // replace fallthrough projections of the ArrayCopyNode by the 462 // new memory, control and the input IO. 463 CallProjections callprojs; 464 extract_projections(&callprojs, true, false); 465 466 if (callprojs.fallthrough_ioproj != NULL) { 467 igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O)); 468 } 469 if (callprojs.fallthrough_memproj != NULL) { 470 igvn->replace_node(callprojs.fallthrough_memproj, mem); 471 } 472 if (callprojs.fallthrough_catchproj != NULL) { 473 igvn->replace_node(callprojs.fallthrough_catchproj, ctl); 474 } 475 476 // The ArrayCopyNode is not disconnected. It still has the 477 // projections for the exception case. Replace current 478 // ArrayCopyNode with a dummy new one with a top() control so 479 // that this part of the graph stays consistent but is 480 // eventually removed. 481 482 set_req(0, phase->C->top()); 483 remove_dead_region(phase, can_reshape); 484 } 485 } else { 486 if (in(TypeFunc::Control) != ctl) { 487 // we can't return new memory and control from Ideal at parse time 488 assert(!is_clonebasic(), "added control for clone?"); 489 return false; 490 } 491 } 492 return true; 493 } 494 495 496 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) { 497 if (remove_dead_region(phase, can_reshape)) return this; 498 499 if (StressArrayCopyMacroNode && !can_reshape) { 500 phase->record_for_igvn(this); 501 return NULL; 502 } 503 504 // See if it's a small array copy and we can inline it as 505 // loads/stores 506 // Here we can only do: 507 // - arraycopy if all arguments were validated before and we don't 508 // need card marking 509 // - clone for which we don't need to do card marking 510 511 if (!is_clonebasic() && !is_arraycopy_validated() && 512 !is_copyofrange_validated() && !is_copyof_validated()) { 513 return NULL; 514 } 515 516 assert(in(TypeFunc::Control) != NULL && 517 in(TypeFunc::Memory) != NULL && 518 in(ArrayCopyNode::Src) != NULL && 519 in(ArrayCopyNode::Dest) != NULL && 520 in(ArrayCopyNode::Length) != NULL && 521 ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) || 522 is_clonebasic()), "broken inputs"); 523 524 if (in(TypeFunc::Control)->is_top() || 525 in(TypeFunc::Memory)->is_top() || 526 phase->type(in(ArrayCopyNode::Src)) == Type::TOP || 527 phase->type(in(ArrayCopyNode::Dest)) == Type::TOP || 528 (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) || 529 (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) { 530 return NULL; 531 } 532 533 int count = get_count(phase); 534 535 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) { 536 return NULL; 537 } 538 539 Node* mem = try_clone_instance(phase, can_reshape, count); 540 if (mem != NULL) { 541 return (mem == NodeSentinel) ? NULL : mem; 542 } 543 544 Node* adr_src = NULL; 545 Node* base_src = NULL; 546 Node* adr_dest = NULL; 547 Node* base_dest = NULL; 548 BasicType copy_type = T_ILLEGAL; 549 const Type* value_type = NULL; 550 bool disjoint_bases = false; 551 552 if (!prepare_array_copy(phase, can_reshape, 553 adr_src, base_src, adr_dest, base_dest, 554 copy_type, value_type, disjoint_bases)) { 555 return NULL; 556 } 557 558 Node* src = in(ArrayCopyNode::Src); 559 Node* dest = in(ArrayCopyNode::Dest); 560 const TypePtr* atp_src = get_address_type(phase, src); 561 const TypePtr* atp_dest = get_address_type(phase, dest); 562 uint alias_idx_src = phase->C->get_alias_index(atp_src); 563 uint alias_idx_dest = phase->C->get_alias_index(atp_dest); 564 565 Node *in_mem = in(TypeFunc::Memory); 566 Node *start_mem_src = in_mem; 567 Node *start_mem_dest = in_mem; 568 if (in_mem->is_MergeMem()) { 569 start_mem_src = in_mem->as_MergeMem()->memory_at(alias_idx_src); 570 start_mem_dest = in_mem->as_MergeMem()->memory_at(alias_idx_dest); 571 } 572 573 574 if (can_reshape) { 575 assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms"); 576 phase->is_IterGVN()->set_delay_transform(true); 577 } 578 579 Node* backward_ctl = phase->C->top(); 580 Node* forward_ctl = phase->C->top(); 581 array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl); 582 583 Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl, 584 start_mem_src, start_mem_dest, 585 atp_src, atp_dest, 586 adr_src, base_src, adr_dest, base_dest, 587 copy_type, value_type, count); 588 589 Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl, 590 start_mem_src, start_mem_dest, 591 atp_src, atp_dest, 592 adr_src, base_src, adr_dest, base_dest, 593 copy_type, value_type, count); 594 595 Node* ctl = NULL; 596 if (!forward_ctl->is_top() && !backward_ctl->is_top()) { 597 ctl = new RegionNode(3); 598 mem = new PhiNode(ctl, Type::MEMORY, atp_dest); 599 ctl->init_req(1, forward_ctl); 600 mem->init_req(1, forward_mem); 601 ctl->init_req(2, backward_ctl); 602 mem->init_req(2, backward_mem); 603 ctl = phase->transform(ctl); 604 mem = phase->transform(mem); 605 } else if (!forward_ctl->is_top()) { 606 ctl = forward_ctl; 607 mem = forward_mem; 608 } else { 609 assert(!backward_ctl->is_top(), "no copy?"); 610 ctl = backward_ctl; 611 mem = backward_mem; 612 } 613 614 if (can_reshape) { 615 assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms"); 616 phase->is_IterGVN()->set_delay_transform(false); 617 } 618 619 MergeMemNode* out_mem = MergeMemNode::make(in_mem); 620 out_mem->set_memory_at(alias_idx_dest, mem); 621 mem = out_mem; 622 623 if (!finish_transform(phase, can_reshape, ctl, mem)) { 624 return NULL; 625 } 626 627 return mem; 628 } 629 630 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) { 631 Node* dest = in(ArrayCopyNode::Dest); 632 if (dest->is_top()) { 633 return false; 634 } 635 const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr(); 636 assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded"); 637 assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() || 638 _src_type->is_known_instance(), "result of EA not recorded"); 639 640 if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) { 641 assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance"); 642 return t_oop->instance_id() == _dest_type->instance_id(); 643 } 644 645 return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase); 646 } 647 648 bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call) { 649 if (n != NULL && 650 n->is_Call() && 651 n->as_Call()->may_modify(t_oop, phase) && 652 (n->as_Call()->is_ArrayCopy() || n->as_Call()->is_call_to_arraycopystub())) { 653 call = n->as_Call(); 654 return true; 655 } 656 return false; 657 } 658 659 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) { 660 661 Node* c = mb->in(0); 662 663 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 664 // step over g1 gc barrier if we're at e.g. a clone with ReduceInitialCardMarks off 665 c = bs->step_over_gc_barrier(c); 666 667 CallNode* call = NULL; 668 guarantee(c != NULL, "step_over_gc_barrier failed, there must be something to step to."); 669 if (c->is_Region()) { 670 for (uint i = 1; i < c->req(); i++) { 671 if (c->in(i) != NULL) { 672 Node* n = c->in(i)->in(0); 673 if (may_modify_helper(t_oop, n, phase, call)) { 674 ac = call->isa_ArrayCopy(); 675 assert(c == mb->in(0), "only for clone"); 676 return true; 677 } 678 } 679 } 680 } else if (may_modify_helper(t_oop, c->in(0), phase, call)) { 681 ac = call->isa_ArrayCopy(); 682 #ifdef ASSERT 683 bool use_ReduceInitialCardMarks = BarrierSet::barrier_set()->is_a(BarrierSet::CardTableBarrierSet) && 684 static_cast<CardTableBarrierSetC2*>(bs)->use_ReduceInitialCardMarks(); 685 assert(c == mb->in(0) || (ac != NULL && ac->is_clonebasic() && !use_ReduceInitialCardMarks), "only for clone"); 686 #endif 687 return true; 688 } 689 690 return false; 691 } 692 693 // Does this array copy modify offsets between offset_lo and offset_hi 694 // in the destination array 695 // if must_modify is false, return true if the copy could write 696 // between offset_lo and offset_hi 697 // if must_modify is true, return true if the copy is guaranteed to 698 // write between offset_lo and offset_hi 699 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const { 700 assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies"); 701 702 Node* dest = in(Dest); 703 Node* dest_pos = in(DestPos); 704 Node* len = in(Length); 705 706 const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int(); 707 const TypeInt *len_t = phase->type(len)->isa_int(); 708 const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr(); 709 710 if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) { 711 return !must_modify; 712 } 713 714 BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type(); 715 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem); 716 uint elemsize = type2aelembytes(ary_elem); 717 718 jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header; 719 jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header; 720 jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header; 721 jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header; 722 723 if (must_modify) { 724 if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) { 725 return true; 726 } 727 } else { 728 if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) { 729 return true; 730 } 731 } 732 return false; 733 }