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