< prev index next >

src/hotspot/share/opto/arraycopynode.cpp

Print this page




  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, TypePtr::BOTTOM),
  36     _kind(None),
  37     _alloc_tightly_coupled(alloc_tightly_coupled),
  38     _has_negative_length_guard(has_negative_length_guard),
  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,


  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::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
 152   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
 153   C2AccessValuePtr addr(adr, adr_type);
 154   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
 155   Node* res = bs->load_at(access, type);
 156   ctl = access.ctl();
 157   return res;
 158 }
 159 


 251   const Type* src_type = phase->type(src);
 252   const TypeAryPtr* ary_src = src_type->isa_aryptr();
 253 
 254   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
 255     const Type* dest_type = phase->type(dest);
 256     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
 257     Node* src_offset = in(ArrayCopyNode::SrcPos);
 258     Node* dest_offset = in(ArrayCopyNode::DestPos);
 259 
 260     // newly allocated object is guaranteed to not overlap with source object
 261     disjoint_bases = is_alloc_tightly_coupled();
 262 
 263     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 264         ary_dest == NULL || ary_dest->klass() == NULL) {
 265       // We don't know if arguments are arrays
 266       return false;
 267     }
 268 
 269     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 270     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 271     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
 272     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;






 273 
 274     if (src_elem != dest_elem || dest_elem == T_VOID) {
 275       // We don't know if arguments are arrays of the same type
 276       return false;
 277     }
 278 
 279     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 280     if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, BarrierSetC2::Optimization)) {
 281       // It's an object array copy but we can't emit the card marking
 282       // that is needed
 283       return false;
 284     }
 285 
 286     value_type = ary_src->elem();
 287 
 288     base_src = src;
 289     base_dest = dest;
 290 
 291     uint shift  = exact_log2(type2aelembytes(dest_elem));




 292     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 293 
 294     adr_src = src;
 295     adr_dest = dest;
 296 
 297     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 298     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 299 
 300     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
 301     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
 302 



 303     adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale));
 304     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale));
 305 
 306     adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header));
 307     adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header));
 308 
 309     adr_src = phase->transform(adr_src);
 310     adr_dest = phase->transform(adr_dest);
 311 
 312     copy_type = dest_elem;
 313   } else {
 314     assert(ary_src != NULL, "should be a clone");
 315     assert(is_clonebasic(), "should be");
 316 
 317     disjoint_bases = true;
 318     assert(src->is_AddP(), "should be base + off");
 319     assert(dest->is_AddP(), "should be base + off");
 320     adr_src = src;
 321     base_src = src->in(AddPNode::Base);
 322     adr_dest = dest;
 323     base_dest = dest->in(AddPNode::Base);
 324 
 325     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?");






 326     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 327     if (elem == T_ARRAY)  elem = T_OBJECT;



 328 
 329     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 330     if (bs->array_copy_requires_gc_barriers(true, elem, true, BarrierSetC2::Optimization)) {
 331       return false;
 332     }
 333 
 334     int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con();
 335     assert(diff >= 0, "clone should not start after 1st array element");
 336     if (diff > 0) {
 337       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
 338       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
 339     }
 340 
 341     copy_type = elem;
 342     value_type = ary_src->elem();
 343   }
 344   return true;
 345 }
 346 
 347 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) {
 348   const Type* at = phase->type(n);
 349   assert(at != Type::TOP, "unexpected type");
 350   const TypePtr* atp = at->isa_ptr();
 351   // adjust atp to be the correct array element address type
 352   atp = atp->add_offset(Type::OffsetBot);
 353   return atp;
 354 }
 355 
 356 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
 357   Node* ctl = in(TypeFunc::Control);
 358   if (!disjoint_bases && count > 1) {

 359     Node* src_offset = in(ArrayCopyNode::SrcPos);
 360     Node* dest_offset = in(ArrayCopyNode::DestPos);
 361     assert(src_offset != NULL && dest_offset != NULL, "should be");
 362     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
 363     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
 364     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 365 
 366     phase->transform(iff);





 367 
 368     forward_ctl = phase->transform(new IfFalseNode(iff));
 369     backward_ctl = phase->transform(new IfTrueNode(iff));





























 370   } else {
 371     forward_ctl = ctl;

















 372   }
 373 }
 374 
 375 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,

 376                                         bool can_reshape,
 377                                         Node*& forward_ctl,
 378                                         MergeMemNode* mm,
 379                                         const TypePtr* atp_src,
 380                                         const TypePtr* atp_dest,
 381                                         Node* adr_src,
 382                                         Node* base_src,
 383                                         Node* adr_dest,
 384                                         Node* base_dest,
 385                                         BasicType copy_type,
 386                                         const Type* value_type,
 387                                         int count) {
 388   if (!forward_ctl->is_top()) {
 389     // copy forward
 390     mm = mm->clone()->as_MergeMem();
 391 
 392     if (count > 0) {
 393       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 394       Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);
 395       store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
 396       for (int i = 1; i < count; i++) {
 397         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 398         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 399         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 400         v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);
 401         store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
 402       }
 403     } else if(can_reshape) {
 404       PhaseIterGVN* igvn = phase->is_IterGVN();
 405       igvn->_worklist.push(adr_src);
 406       igvn->_worklist.push(adr_dest);

 407     }
 408     return mm;
 409   }
 410   return phase->C->top();
 411 }
 412 
 413 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
 414                                          bool can_reshape,
 415                                          Node*& backward_ctl,
 416                                          MergeMemNode* mm,
 417                                          const TypePtr* atp_src,
 418                                          const TypePtr* atp_dest,
 419                                          Node* adr_src,
 420                                          Node* base_src,
 421                                          Node* adr_dest,
 422                                          Node* base_dest,
 423                                          BasicType copy_type,
 424                                          const Type* value_type,
 425                                          int count) {
 426   if (!backward_ctl->is_top()) {
 427     // copy backward
 428     mm = mm->clone()->as_MergeMem();
 429 
 430     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 431     assert(copy_type != T_OBJECT || !bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, BarrierSetC2::Optimization), "only tightly coupled allocations for object arrays");
 432 
 433     if (count > 0) {
 434       for (int i = count-1; i >= 1; i--) {
 435         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 436         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 437         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 438         Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);
 439         store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
 440       }
 441       Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);
 442       store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
 443     } else if(can_reshape) {
 444       PhaseIterGVN* igvn = phase->is_IterGVN();
 445       igvn->_worklist.push(adr_src);
 446       igvn->_worklist.push(adr_dest);

 447     }
 448     return phase->transform(mm);
 449   }
 450   return phase->C->top();
 451 }
 452 
 453 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 454                                      Node* ctl, Node *mem) {
 455   if (can_reshape) {
 456     PhaseIterGVN* igvn = phase->is_IterGVN();
 457     igvn->set_delay_transform(false);
 458     if (is_clonebasic()) {
 459       Node* out_mem = proj_out(TypeFunc::Memory);
 460 
 461       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 462       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 463           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 464         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, BarrierSetC2::Optimization), "can only happen with card marking");
 465         return false;
 466       }
 467 
 468       igvn->replace_node(out_mem->raw_out(0), mem);
 469 
 470       Node* out_ctl = proj_out(TypeFunc::Control);
 471       igvn->replace_node(out_ctl, ctl);
 472     } else {
 473       // replace fallthrough projections of the ArrayCopyNode by the
 474       // new memory, control and the input IO.
 475       CallProjections callprojs;
 476       extract_projections(&callprojs, true, false);
 477 
 478       if (callprojs.fallthrough_ioproj != NULL) {
 479         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
 480       }
 481       if (callprojs.fallthrough_memproj != NULL) {
 482         igvn->replace_node(callprojs.fallthrough_memproj, mem);
 483       }
 484       if (callprojs.fallthrough_catchproj != NULL) {
 485         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
 486       }
 487 
 488       // The ArrayCopyNode is not disconnected. It still has the
 489       // projections for the exception case. Replace current
 490       // ArrayCopyNode with a dummy new one with a top() control so
 491       // that this part of the graph stays consistent but is
 492       // eventually removed.
 493 
 494       set_req(0, phase->C->top());
 495       remove_dead_region(phase, can_reshape);
 496     }
 497   } else {
 498     if (in(TypeFunc::Control) != ctl) {
 499       // we can't return new memory and control from Ideal at parse time









 500       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
 501       phase->record_for_igvn(this);
 502       return false;
 503     }
 504   }
 505   return true;
 506 }
 507 
 508 
 509 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 510   if (remove_dead_region(phase, can_reshape))  return this;




 511 
 512   if (StressArrayCopyMacroNode && !can_reshape) {
 513     phase->record_for_igvn(this);
 514     return NULL;
 515   }
 516 
 517   // See if it's a small array copy and we can inline it as
 518   // loads/stores
 519   // Here we can only do:
 520   // - arraycopy if all arguments were validated before and we don't
 521   // need card marking
 522   // - clone for which we don't need to do card marking
 523 
 524   if (!is_clonebasic() && !is_arraycopy_validated() &&
 525       !is_copyofrange_validated() && !is_copyof_validated()) {
 526     return NULL;
 527   }
 528 
 529   assert(in(TypeFunc::Control) != NULL &&
 530          in(TypeFunc::Memory) != NULL &&


 532          in(ArrayCopyNode::Dest) != NULL &&
 533          in(ArrayCopyNode::Length) != NULL &&
 534          ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) ||
 535           is_clonebasic()), "broken inputs");
 536 
 537   if (in(TypeFunc::Control)->is_top() ||
 538       in(TypeFunc::Memory)->is_top() ||
 539       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
 540       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
 541       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
 542       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
 543     return NULL;
 544   }
 545 
 546   int count = get_count(phase);
 547 
 548   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
 549     return NULL;
 550   }
 551 











 552   Node* mem = try_clone_instance(phase, can_reshape, count);
 553   if (mem != NULL) {
 554     return (mem == NodeSentinel) ? NULL : mem;
 555   }
 556 
 557   Node* adr_src = NULL;
 558   Node* base_src = NULL;
 559   Node* adr_dest = NULL;
 560   Node* base_dest = NULL;
 561   BasicType copy_type = T_ILLEGAL;
 562   const Type* value_type = NULL;
 563   bool disjoint_bases = false;
 564 
 565   if (!prepare_array_copy(phase, can_reshape,
 566                           adr_src, base_src, adr_dest, base_dest,
 567                           copy_type, value_type, disjoint_bases)) {
 568     return NULL;
 569   }
 570 
 571   Node* src = in(ArrayCopyNode::Src);
 572   Node* dest = in(ArrayCopyNode::Dest);
 573   const TypePtr* atp_src = get_address_type(phase, src);
 574   const TypePtr* atp_dest = get_address_type(phase, dest);
 575 
 576   Node *in_mem = in(TypeFunc::Memory);
 577   if (!in_mem->is_MergeMem()) {
 578     in_mem = MergeMemNode::make(in_mem);
 579   }
 580 













 581 
 582   if (can_reshape) {
 583     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 584     phase->is_IterGVN()->set_delay_transform(true);
 585   }
 586 




 587   Node* backward_ctl = phase->C->top();
 588   Node* forward_ctl = phase->C->top();
 589   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
 590 
 591   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
 592                                          in_mem->as_MergeMem(),




 593                                          atp_src, atp_dest,
 594                                          adr_src, base_src, adr_dest, base_dest,
 595                                          copy_type, value_type, count);
 596 
 597   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
 598                                            in_mem->as_MergeMem(),



 599                                            atp_src, atp_dest,
 600                                            adr_src, base_src, adr_dest, base_dest,
 601                                            copy_type, value_type, count);
 602 
 603   Node* ctl = NULL;
 604   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
 605     ctl = new RegionNode(3);
 606     ctl->init_req(1, forward_ctl);
 607     ctl->init_req(2, backward_ctl);
 608     ctl = phase->transform(ctl);
 609     MergeMemNode* forward_mm = forward_mem->as_MergeMem();
 610     MergeMemNode* backward_mm = backward_mem->as_MergeMem();
 611     for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
 612       if (mms.memory() != mms.memory2()) {
 613         Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
 614         phi->init_req(1, mms.memory());
 615         phi->init_req(2, mms.memory2());
 616         phi = phase->transform(phi);
 617         mms.set_memory(phi);
 618       }
 619     }
 620     mem = forward_mem;
 621   } else if (!forward_ctl->is_top()) {
 622     ctl = forward_ctl;
 623     mem = forward_mem;
 624   } else {
 625     assert(!backward_ctl->is_top(), "no copy?");
 626     ctl = backward_ctl;
 627     mem = backward_mem;
 628   }
 629 
 630   if (can_reshape) {
 631     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 632     phase->is_IterGVN()->set_delay_transform(false);
 633   }
 634 
 635   if (!finish_transform(phase, can_reshape, ctl, mem)) {




 636     return NULL;
 637   }
 638 
 639   return mem;
 640 }
 641 
 642 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 643   Node* dest = in(ArrayCopyNode::Dest);
 644   if (dest->is_top()) {
 645     return false;
 646   }
 647   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 648   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 649   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 650          _src_type->is_known_instance(), "result of EA not recorded");
 651 
 652   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 653     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
 654     return t_oop->instance_id() == _dest_type->instance_id();
 655   }




  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 "opto/valuetypenode.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
  36   : CallNode(arraycopy_type(), NULL, TypePtr::BOTTOM),
  37     _kind(None),
  38     _alloc_tightly_coupled(alloc_tightly_coupled),
  39     _has_negative_length_guard(has_negative_length_guard),
  40     _arguments_validated(false),
  41     _src_type(TypeOopPtr::BOTTOM),
  42     _dest_type(TypeOopPtr::BOTTOM) {
  43   init_class_id(Class_ArrayCopy);
  44   init_flags(Flag_is_macro);
  45   C->add_macro_node(this);
  46 }
  47 
  48 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
  49 
  50 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
  51                                    Node* src, Node* src_offset,


  96 void ArrayCopyNode::dump_compact_spec(outputStream* st) const {
  97   st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");
  98 }
  99 #endif
 100 
 101 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {
 102   // check that length is constant
 103   Node* length = in(ArrayCopyNode::Length);
 104   const Type* length_type = phase->type(length);
 105 
 106   if (length_type == Type::TOP) {
 107     return -1;
 108   }
 109 
 110   assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");
 111 
 112   return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);
 113 }
 114 
 115 int ArrayCopyNode::get_count(PhaseGVN *phase) const {
 116   if (is_clonebasic()) {
 117     Node* src = in(ArrayCopyNode::Src);
 118     const Type* src_type = phase->type(src);
 119 
 120     if (src_type == Type::TOP) {
 121       return -1;
 122     }
 123 
 124     if (src_type->isa_instptr()) {
 125       const TypeInstPtr* inst_src = src_type->is_instptr();
 126       ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
 127       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
 128       // fields into account. They are rare anyway so easier to simply
 129       // skip instances with injected fields.
 130       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
 131         return -1;
 132       }
 133       int nb_fields = ik->nof_nonstatic_fields();
 134       return nb_fields;
 135     } else {
 136       const TypeAryPtr* ary_src = src_type->isa_aryptr();
 137       assert (ary_src != NULL, "not an array or instance?");
 138       // clone passes a length as a rounded number of longs. If we're
 139       // cloning an array we'll do it element by element. If the
 140       // length input to ArrayCopyNode is constant, length of input
 141       // array must be too.
 142 
 143       assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con() ||
 144              (ValueArrayFlatten && ary_src->elem()->make_oopptr() != NULL && ary_src->elem()->make_oopptr()->can_be_value_type()) ||
 145              phase->is_IterGVN() || phase->C->inlining_incrementally(), "inconsistent");
 146 
 147       if (ary_src->size()->is_con()) {
 148         return ary_src->size()->get_con();
 149       }
 150       return -1;
 151     }
 152   }
 153 
 154   return get_length_if_constant(phase);
 155 }
 156 
 157 Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
 158   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
 159   C2AccessValuePtr addr(adr, adr_type);
 160   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
 161   Node* res = bs->load_at(access, type);
 162   ctl = access.ctl();
 163   return res;
 164 }
 165 


 257   const Type* src_type = phase->type(src);
 258   const TypeAryPtr* ary_src = src_type->isa_aryptr();
 259 
 260   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
 261     const Type* dest_type = phase->type(dest);
 262     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
 263     Node* src_offset = in(ArrayCopyNode::SrcPos);
 264     Node* dest_offset = in(ArrayCopyNode::DestPos);
 265 
 266     // newly allocated object is guaranteed to not overlap with source object
 267     disjoint_bases = is_alloc_tightly_coupled();
 268 
 269     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 270         ary_dest == NULL || ary_dest->klass() == NULL) {
 271       // We don't know if arguments are arrays
 272       return false;
 273     }
 274 
 275     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 276     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 277     if (src_elem  == T_ARRAY ||
 278         (src_elem == T_VALUETYPE && ary_src->klass()->is_obj_array_klass())) {
 279       src_elem  = T_OBJECT;
 280     }
 281     if (dest_elem == T_ARRAY ||
 282         (dest_elem == T_VALUETYPE && ary_dest->klass()->is_obj_array_klass())) {
 283       dest_elem = T_OBJECT;
 284     }
 285 
 286     if (src_elem != dest_elem || dest_elem == T_VOID) {
 287       // We don't know if arguments are arrays of the same type
 288       return false;
 289     }
 290 
 291     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 292     if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, BarrierSetC2::Optimization)) {
 293       // It's an object array copy but we can't emit the card marking
 294       // that is needed
 295       return false;
 296     }
 297 
 298     value_type = ary_src->elem();
 299 
 300     base_src = src;
 301     base_dest = dest;
 302 
 303     uint shift  = exact_log2(type2aelembytes(dest_elem));
 304     if (dest_elem == T_VALUETYPE) {
 305       ciValueArrayKlass* vak = ary_src->klass()->as_value_array_klass();
 306       shift = vak->log2_element_size();
 307     }
 308     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 309 
 310     adr_src = src;
 311     adr_dest = dest;
 312 
 313     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 314     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 315 
 316     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
 317     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
 318 
 319     adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));
 320     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));
 321 
 322     adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale));
 323     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale));
 324 






 325     copy_type = dest_elem;
 326   } else {
 327     assert(ary_src != NULL, "should be a clone");
 328     assert(is_clonebasic(), "should be");
 329 
 330     disjoint_bases = true;
 331     assert(src->is_AddP(), "should be base + off");
 332     assert(dest->is_AddP(), "should be base + off");
 333     adr_src = src;
 334     base_src = src->in(AddPNode::Base);
 335     adr_dest = dest;
 336     base_dest = dest->in(AddPNode::Base);
 337 
 338     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?");
 339 
 340     if (ary_src->elem()->make_oopptr() != NULL &&
 341         ary_src->elem()->make_oopptr()->can_be_value_type()) {
 342       return false;
 343     }
 344 
 345     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 346     if (elem == T_ARRAY ||
 347         (elem == T_VALUETYPE && ary_src->klass()->is_obj_array_klass())) {
 348       elem = T_OBJECT;
 349     }
 350 
 351     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 352     if (bs->array_copy_requires_gc_barriers(true, elem, true, BarrierSetC2::Optimization)) {
 353       return false;
 354     }
 355 
 356     int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con();
 357     assert(diff >= 0, "clone should not start after 1st array element");
 358     if (diff > 0) {
 359       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
 360       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
 361     }
 362 
 363     copy_type = elem;
 364     value_type = ary_src->elem();
 365   }
 366   return true;
 367 }
 368 
 369 const TypeAryPtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) {
 370   const Type* at = phase->type(n);
 371   assert(at != Type::TOP, "unexpected type");
 372   const TypeAryPtr* atp = at->is_aryptr();
 373   // adjust atp to be the correct array element address type
 374   atp = atp->add_offset(Type::OffsetBot)->is_aryptr();
 375   return atp;
 376 }
 377 
 378 void ArrayCopyNode::array_copy_test_overlap(GraphKit& kit, bool disjoint_bases, int count, Node*& backward_ctl) {
 379   Node* ctl = kit.control();
 380   if (!disjoint_bases && count > 1) {
 381     PhaseGVN& gvn = kit.gvn();
 382     Node* src_offset = in(ArrayCopyNode::SrcPos);
 383     Node* dest_offset = in(ArrayCopyNode::DestPos);
 384     assert(src_offset != NULL && dest_offset != NULL, "should be");
 385     Node* cmp = gvn.transform(new CmpINode(src_offset, dest_offset));
 386     Node *bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
 387     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 388 
 389     gvn.transform(iff);
 390 
 391     kit.set_control(gvn.transform(new IfFalseNode(iff)));
 392     backward_ctl = gvn.transform(new IfTrueNode(iff));
 393   }
 394 }
 395 
 396 void ArrayCopyNode::copy(GraphKit& kit,
 397                          const TypeAryPtr* atp_src,
 398                          const TypeAryPtr* atp_dest,
 399                          int i,
 400                          Node* base_src,
 401                          Node* base_dest,
 402                          Node* adr_src,
 403                          Node* adr_dest,
 404                          BasicType copy_type,
 405                          const Type* value_type) {
 406   if (copy_type == T_VALUETYPE) {
 407     ciValueArrayKlass* vak = atp_src->klass()->as_value_array_klass();
 408     ciValueKlass* vk = vak->element_klass()->as_value_klass();
 409     for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
 410       ciField* field = vk->nonstatic_field_at(j);
 411       int off_in_vt = field->offset() - vk->first_field_offset();
 412       Node* off  = kit.MakeConX(off_in_vt + i * vak->element_byte_size());
 413       ciType* ft = field->type();
 414       BasicType bt = type2field[ft->basic_type()];
 415       assert(!field->is_flattened(), "flattened field encountered");
 416       if (bt == T_VALUETYPE) {
 417         bt = T_OBJECT;
 418       }
 419       const Type* rt = Type::get_const_type(ft);
 420       const TypePtr* adr_type = atp_src->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
 421       Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
 422       Node* v = kit.make_load(kit.control(), next_src, rt, bt, adr_type, MemNode::unordered);
 423 
 424       Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
 425       if (is_java_primitive(bt)) {
 426         kit.store_to_memory(kit.control(), next_dest, v, bt, adr_type, MemNode::unordered);
 427       } else {
 428         const TypeOopPtr* val_type = Type::get_const_type(ft)->is_oopptr();
 429         kit.access_store_at(base_dest, next_dest, adr_type, v,
 430                             val_type, bt, StoreNode::release_if_reference(T_OBJECT));
 431       }
 432     }
 433   } else {
 434     Node* off  = kit.MakeConX(type2aelembytes(copy_type) * i);
 435     Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
 436     Node* v = kit.make_load(kit.control(), next_src, value_type, copy_type, atp_src, MemNode::unordered);
 437     Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
 438     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 439     if (copy_type == T_OBJECT && (bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, BarrierSetC2::Optimization))) {
 440       kit.access_store_at(base_dest, next_dest, atp_dest, v,
 441                           value_type->make_ptr()->is_oopptr(), copy_type,
 442                           StoreNode::release_if_reference(T_OBJECT));
 443     } else {
 444       kit.store_to_memory(kit.control(), next_dest, v, copy_type, atp_dest, MemNode::unordered);
 445     }
 446   }
 447 }
 448 
 449 
 450 void ArrayCopyNode::array_copy_forward(GraphKit& kit,
 451                                        bool can_reshape,
 452                                        const TypeAryPtr* atp_src,
 453                                        const TypeAryPtr* atp_dest,


 454                                        Node* adr_src,
 455                                        Node* base_src,
 456                                        Node* adr_dest,
 457                                        Node* base_dest,
 458                                        BasicType copy_type,
 459                                        const Type* value_type,
 460                                        int count) {
 461   if (!kit.stopped()) {
 462     // copy forward


 463     if (count > 0) {
 464       for (int i = 0; i < count; i++) {
 465         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);







 466       }
 467     } else if(can_reshape) {
 468       PhaseGVN& gvn = kit.gvn();
 469       assert(gvn.is_IterGVN(), "");
 470       gvn.record_for_igvn(adr_src);
 471       gvn.record_for_igvn(adr_dest);
 472     }

 473   }

 474 }
 475 
 476 void ArrayCopyNode::array_copy_backward(GraphKit& kit,
 477                                         bool can_reshape,
 478                                         const TypeAryPtr* atp_src,
 479                                         const TypeAryPtr* atp_dest,


 480                                         Node* adr_src,
 481                                         Node* base_src,
 482                                         Node* adr_dest,
 483                                         Node* base_dest,
 484                                         BasicType copy_type,
 485                                         const Type* value_type,
 486                                         int count) {
 487   if (!kit.stopped()) {
 488     // copy backward
 489     PhaseGVN& gvn = kit.gvn();



 490 
 491     if (count > 0) {
 492       for (int i = count-1; i >= 0; i--) {
 493         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);




 494       }


 495     } else if(can_reshape) {
 496       PhaseGVN& gvn = kit.gvn();
 497       assert(gvn.is_IterGVN(), "");
 498       gvn.record_for_igvn(adr_src);
 499       gvn.record_for_igvn(adr_dest);
 500     }

 501   }

 502 }
 503 
 504 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 505                                      Node* ctl, Node *mem) {
 506   if (can_reshape) {
 507     PhaseIterGVN* igvn = phase->is_IterGVN();
 508     igvn->set_delay_transform(false);
 509     if (is_clonebasic()) {
 510       Node* out_mem = proj_out(TypeFunc::Memory);
 511 
 512       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 513       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 514           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 515         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, BarrierSetC2::Optimization), "can only happen with card marking");
 516         return false;
 517       }
 518 
 519       igvn->replace_node(out_mem->raw_out(0), mem);
 520 
 521       Node* out_ctl = proj_out(TypeFunc::Control);
 522       igvn->replace_node(out_ctl, ctl);
 523     } else {
 524       // replace fallthrough projections of the ArrayCopyNode by the
 525       // new memory, control and the input IO.
 526       CallProjections* callprojs = extract_projections(true, false);

 527 
 528       if (callprojs->fallthrough_ioproj != NULL) {
 529         igvn->replace_node(callprojs->fallthrough_ioproj, in(TypeFunc::I_O));
 530       }
 531       if (callprojs->fallthrough_memproj != NULL) {
 532         igvn->replace_node(callprojs->fallthrough_memproj, mem);
 533       }
 534       if (callprojs->fallthrough_catchproj != NULL) {
 535         igvn->replace_node(callprojs->fallthrough_catchproj, ctl);
 536       }
 537 
 538       // The ArrayCopyNode is not disconnected. It still has the
 539       // projections for the exception case. Replace current
 540       // ArrayCopyNode with a dummy new one with a top() control so
 541       // that this part of the graph stays consistent but is
 542       // eventually removed.
 543 
 544       set_req(0, phase->C->top());
 545       remove_dead_region(phase, can_reshape);
 546     }
 547   } else {
 548     if (in(TypeFunc::Control) != ctl) {
 549       // we can't return new memory and control from Ideal at parse time
 550 #ifdef ASSERT
 551       Node* src = in(ArrayCopyNode::Src);
 552       const Type* src_type = phase->type(src);
 553       const TypeAryPtr* ary_src = src_type->isa_aryptr();
 554       BasicType elem = ary_src != NULL ? ary_src->klass()->as_array_klass()->element_type()->basic_type() : T_CONFLICT;
 555       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 556       assert(!is_clonebasic() || bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, BarrierSetC2::Optimization) ||
 557              (ary_src != NULL && elem == T_VALUETYPE && ary_src->klass()->is_obj_array_klass()), "added control for clone?");
 558 #endif
 559       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
 560       phase->record_for_igvn(this);
 561       return false;
 562     }
 563   }
 564   return true;
 565 }
 566 
 567 
 568 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 569   // Perform any generic optimizations first
 570   Node* result = SafePointNode::Ideal(phase, can_reshape);
 571   if (result != NULL) {
 572     return result;
 573   }
 574 
 575   if (StressArrayCopyMacroNode && !can_reshape) {
 576     phase->record_for_igvn(this);
 577     return NULL;
 578   }
 579 
 580   // See if it's a small array copy and we can inline it as
 581   // loads/stores
 582   // Here we can only do:
 583   // - arraycopy if all arguments were validated before and we don't
 584   // need card marking
 585   // - clone for which we don't need to do card marking
 586 
 587   if (!is_clonebasic() && !is_arraycopy_validated() &&
 588       !is_copyofrange_validated() && !is_copyof_validated()) {
 589     return NULL;
 590   }
 591 
 592   assert(in(TypeFunc::Control) != NULL &&
 593          in(TypeFunc::Memory) != NULL &&


 595          in(ArrayCopyNode::Dest) != NULL &&
 596          in(ArrayCopyNode::Length) != NULL &&
 597          ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) ||
 598           is_clonebasic()), "broken inputs");
 599 
 600   if (in(TypeFunc::Control)->is_top() ||
 601       in(TypeFunc::Memory)->is_top() ||
 602       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
 603       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
 604       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
 605       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
 606     return NULL;
 607   }
 608 
 609   int count = get_count(phase);
 610 
 611   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
 612     return NULL;
 613   }
 614 
 615   Node* src = in(ArrayCopyNode::Src);
 616   Node* dest = in(ArrayCopyNode::Dest);
 617   const Type* src_type = phase->type(src);
 618   const Type* dest_type = phase->type(dest);
 619 
 620   if (src_type->isa_aryptr() && dest_type->isa_instptr()) {
 621     // clone used for load of unknown value type can't be optimized at
 622     // this point
 623     return NULL;
 624   }
 625 
 626   Node* mem = try_clone_instance(phase, can_reshape, count);
 627   if (mem != NULL) {
 628     return (mem == NodeSentinel) ? NULL : mem;
 629   }
 630 
 631   Node* adr_src = NULL;
 632   Node* base_src = NULL;
 633   Node* adr_dest = NULL;
 634   Node* base_dest = NULL;
 635   BasicType copy_type = T_ILLEGAL;
 636   const Type* value_type = NULL;
 637   bool disjoint_bases = false;
 638 
 639   if (!prepare_array_copy(phase, can_reshape,
 640                           adr_src, base_src, adr_dest, base_dest,
 641                           copy_type, value_type, disjoint_bases)) {
 642     return NULL;
 643   }
 644 
 645   JVMState* new_jvms = NULL;
 646   SafePointNode* new_map = NULL;
 647   if (!is_clonebasic()) {
 648     new_jvms =  jvms()->clone_shallow(phase->C);
 649     new_map = new SafePointNode(req(), new_jvms);
 650     for (uint i = TypeFunc::FramePtr; i < req(); i++) {
 651       new_map->init_req(i, in(i));

 652     }
 653     new_jvms->set_map(new_map);
 654   } else {
 655     new_jvms =  new (phase->C) JVMState(0);
 656     new_map = new SafePointNode(TypeFunc::Parms, new_jvms);
 657     new_jvms->set_map(new_map);
 658   }
 659   new_map->set_control(in(TypeFunc::Control));
 660   new_map->set_memory(MergeMemNode::make(in(TypeFunc::Memory)));
 661   new_map->set_i_o(in(TypeFunc::I_O));
 662 
 663   const TypeAryPtr* atp_src = get_address_type(phase, src);
 664   const TypeAryPtr* atp_dest = get_address_type(phase, dest);
 665   uint alias_idx_src = phase->C->get_alias_index(atp_src);
 666   uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 667 
 668   if (can_reshape) {
 669     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 670     phase->is_IterGVN()->set_delay_transform(true);
 671   }
 672 
 673   GraphKit kit(new_jvms, phase);
 674 
 675   SafePointNode* backward_map = NULL;
 676   SafePointNode* forward_map = NULL;
 677   Node* backward_ctl = phase->C->top();


 678 
 679   array_copy_test_overlap(kit, disjoint_bases, count, backward_ctl);
 680 
 681   {
 682     PreserveJVMState pjvms(&kit);
 683 
 684     array_copy_forward(kit, can_reshape,
 685                        atp_src, atp_dest,
 686                        adr_src, base_src, adr_dest, base_dest,
 687                        copy_type, value_type, count);
 688 
 689     forward_map = kit.stop();
 690   }
 691 
 692   kit.set_control(backward_ctl);
 693   array_copy_backward(kit, can_reshape,
 694                       atp_src, atp_dest,
 695                       adr_src, base_src, adr_dest, base_dest,
 696                       copy_type, value_type, count);
 697 
 698   backward_map = kit.stop();
 699 
 700   if (!forward_map->control()->is_top() && !backward_map->control()->is_top()) {
 701     assert(forward_map->i_o() == backward_map->i_o(), "need a phi on IO?");
 702     Node* ctl = new RegionNode(3);
 703     Node* mem = new PhiNode(ctl, Type::MEMORY, TypePtr::BOTTOM);
 704     kit.set_map(forward_map);
 705     ctl->init_req(1, kit.control());
 706     mem->init_req(1, kit.reset_memory());
 707     kit.set_map(backward_map);
 708     ctl->init_req(2, kit.control());
 709     mem->init_req(2, kit.reset_memory());
 710     kit.set_control(phase->transform(ctl));
 711     kit.set_all_memory(phase->transform(mem));
 712   } else if (!forward_map->control()->is_top()) {
 713     kit.set_map(forward_map);
 714   } else {
 715     assert(!backward_map->control()->is_top(), "no copy?");
 716     kit.set_map(backward_map);






 717   }
 718 
 719   if (can_reshape) {
 720     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 721     phase->is_IterGVN()->set_delay_transform(false);
 722   }
 723 
 724   mem = kit.map()->memory();
 725   if (!finish_transform(phase, can_reshape, kit.control(), mem)) {
 726     if (!can_reshape) {
 727       phase->record_for_igvn(this);
 728     }
 729     return NULL;
 730   }
 731 
 732   return mem;
 733 }
 734 
 735 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 736   Node* dest = in(ArrayCopyNode::Dest);
 737   if (dest->is_top()) {
 738     return false;
 739   }
 740   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 741   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 742   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 743          _src_type->is_known_instance(), "result of EA not recorded");
 744 
 745   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 746     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
 747     return t_oop->instance_id() == _dest_type->instance_id();
 748   }


< prev index next >