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   assert(ary_src != NULL, "should be an array copy/clone");
 229 
 230   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
 231     const Type* dest_type = phase->type(dest);
 232     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
 233     Node* src_offset = in(ArrayCopyNode::SrcPos);
 234     Node* dest_offset = in(ArrayCopyNode::DestPos);
 235 
 236     // newly allocated object is guaranteed to not overlap with source object
 237     disjoint_bases = is_alloc_tightly_coupled();
 238 
 239     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 240         ary_dest == NULL || ary_dest->klass() == NULL) {
 241       // We don't know if arguments are arrays
 242       return false;
 243     }
 244 
 245     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 246     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 247     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
 248     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
 249 
 250     if (src_elem != dest_elem || dest_elem == T_VOID) {
 251       // We don't know if arguments are arrays of the same type
 252       return false;
 253     }
 254 
 255     if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() || !GraphKit::use_ReduceInitialCardMarks())) {
 256       // It's an object array copy but we can't emit the card marking
 257       // that is needed
 258       return false;
 259     }
 260 
 261     value_type = ary_src->elem();
 262 
 263     base_src = src;
 264     base_dest = dest;
 265 
 266     uint shift  = exact_log2(type2aelembytes(dest_elem));
 267     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 268 
 269     adr_src = src;
 270     adr_dest = dest;
 271 
 272     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 273     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 274 
 275     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
 276     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
 277 
 278     adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale));
 279     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale));
 280 
 281     adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header));
 282     adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header));
 283 
 284     adr_src = phase->transform(adr_src);
 285     adr_dest = phase->transform(adr_dest);
 286 
 287     copy_type = dest_elem;
 288   } else {
 289     assert (is_clonebasic(), "should be");
 290 
 291     disjoint_bases = true;
 292     assert(src->is_AddP(), "should be base + off");
 293     assert(dest->is_AddP(), "should be base + off");
 294     adr_src = src;
 295     base_src = src->in(AddPNode::Base);
 296     adr_dest = dest;
 297     base_dest = dest->in(AddPNode::Base);
 298 
 299     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?");
 300     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 301     if (elem == T_ARRAY)  elem = T_OBJECT;
 302 
 303     int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con();
 304     assert(diff >= 0, "clone should not start after 1st array element");
 305     if (diff > 0) {
 306       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
 307       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
 308     }
 309 
 310     copy_type = elem;
 311     value_type = ary_src->elem();
 312   }
 313   return true;
 314 }
 315 
 316 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) {
 317   const Type* at = phase->type(n);
 318   assert(at != Type::TOP, "unexpected type");
 319   const TypePtr* atp = at->isa_ptr();
 320   // adjust atp to be the correct array element address type
 321   atp = atp->add_offset(Type::OffsetBot);
 322   return atp;
 323 }
 324 
 325 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
 326   Node* ctl = in(TypeFunc::Control);
 327   if (!disjoint_bases && count > 1) {
 328     Node* src_offset = in(ArrayCopyNode::SrcPos);
 329     Node* dest_offset = in(ArrayCopyNode::DestPos);
 330     assert(src_offset != NULL && dest_offset != NULL, "should be");
 331     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
 332     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
 333     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 334 
 335     phase->transform(iff);
 336 
 337     forward_ctl = phase->transform(new IfFalseNode(iff));
 338     backward_ctl = phase->transform(new IfTrueNode(iff));
 339   } else {
 340     forward_ctl = ctl;
 341   }
 342 }
 343 
 344 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
 345                                         bool can_reshape,
 346                                         Node* forward_ctl,
 347                                         Node* start_mem_src,
 348                                         Node* start_mem_dest,
 349                                         const TypePtr* atp_src,
 350                                         const TypePtr* atp_dest,
 351                                         Node* adr_src,
 352                                         Node* base_src,
 353                                         Node* adr_dest,
 354                                         Node* base_dest,
 355                                         BasicType copy_type,
 356                                         const Type* value_type,
 357                                         int count) {
 358   Node* mem = phase->C->top();
 359   if (!forward_ctl->is_top()) {
 360     // copy forward
 361     mem = start_mem_dest;
 362 
 363     if (count > 0) {
 364       Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 365       v = phase->transform(v);
 366       mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 367       mem = phase->transform(mem);
 368       for (int i = 1; i < count; i++) {
 369         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 370         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 371         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 372         v = LoadNode::make(*phase, forward_ctl, mem, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 373         v = phase->transform(v);
 374         mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 375         mem = phase->transform(mem);
 376       }
 377     } else if(can_reshape) {
 378       PhaseIterGVN* igvn = phase->is_IterGVN();
 379       igvn->_worklist.push(adr_src);
 380       igvn->_worklist.push(adr_dest);
 381     }
 382   }
 383   return mem;
 384 }
 385 
 386 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
 387                                          bool can_reshape,
 388                                          Node* backward_ctl,
 389                                          Node* start_mem_src,
 390                                          Node* start_mem_dest,
 391                                          const TypePtr* atp_src,
 392                                          const TypePtr* atp_dest,
 393                                          Node* adr_src,
 394                                          Node* base_src,
 395                                          Node* adr_dest,
 396                                          Node* base_dest,
 397                                          BasicType copy_type,
 398                                          const Type* value_type,
 399                                          int count) {
 400   Node* mem = phase->C->top();
 401   if (!backward_ctl->is_top()) {
 402     // copy backward
 403     mem = start_mem_dest;
 404 
 405     if (count > 0) {
 406       for (int i = count-1; i >= 1; i--) {
 407         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 408         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 409         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 410         Node* v = LoadNode::make(*phase, backward_ctl, mem, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 411         v = phase->transform(v);
 412         mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 413         mem = phase->transform(mem);
 414       }
 415       Node* v = LoadNode::make(*phase, backward_ctl, mem, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 416       v = phase->transform(v);
 417       mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 418       mem = phase->transform(mem);
 419     } else if(can_reshape) {
 420       PhaseIterGVN* igvn = phase->is_IterGVN();
 421       igvn->_worklist.push(adr_src);
 422       igvn->_worklist.push(adr_dest);
 423     }
 424   }
 425   return mem;
 426 }
 427 
 428 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 429                                      Node* ctl, Node *mem) {
 430   if (can_reshape) {
 431     PhaseIterGVN* igvn = phase->is_IterGVN();
 432     igvn->set_delay_transform(false);
 433     if (is_clonebasic()) {
 434       Node* out_mem = proj_out(TypeFunc::Memory);
 435 
 436       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 437           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 438         assert(!GraphKit::use_ReduceInitialCardMarks(), "can only happen with card marking");
 439         return false;
 440       }
 441 
 442       igvn->replace_node(out_mem->raw_out(0), mem);
 443 
 444       Node* out_ctl = proj_out(TypeFunc::Control);
 445       igvn->replace_node(out_ctl, ctl);
 446     } else {
 447       // replace fallthrough projections of the ArrayCopyNode by the
 448       // new memory, control and the input IO.
 449       CallProjections callprojs;
 450       extract_projections(&callprojs, true, false);
 451 
 452       if (callprojs.fallthrough_ioproj != NULL) {
 453         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
 454       }
 455       if (callprojs.fallthrough_memproj != NULL) {
 456         igvn->replace_node(callprojs.fallthrough_memproj, mem);
 457       }
 458       if (callprojs.fallthrough_catchproj != NULL) {
 459         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
 460       }
 461 
 462       // The ArrayCopyNode is not disconnected. It still has the
 463       // projections for the exception case. Replace current
 464       // ArrayCopyNode with a dummy new one with a top() control so
 465       // that this part of the graph stays consistent but is
 466       // eventually removed.
 467 
 468       set_req(0, phase->C->top());
 469       remove_dead_region(phase, can_reshape);
 470     }
 471   } else {
 472     if (in(TypeFunc::Control) != ctl) {
 473       // we can't return new memory and control from Ideal at parse time
 474       assert(!is_clonebasic(), "added control for clone?");
 475       return false;
 476     }
 477   }
 478   return true;
 479 }
 480 
 481 
 482 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 483   if (remove_dead_region(phase, can_reshape))  return this;
 484 
 485   if (StressArrayCopyMacroNode && !can_reshape) {
 486     phase->record_for_igvn(this);
 487     return NULL;
 488   }
 489 
 490   // See if it's a small array copy and we can inline it as
 491   // loads/stores
 492   // Here we can only do:
 493   // - arraycopy if all arguments were validated before and we don't
 494   // need card marking
 495   // - clone for which we don't need to do card marking
 496 
 497   if (!is_clonebasic() && !is_arraycopy_validated() &&
 498       !is_copyofrange_validated() && !is_copyof_validated()) {
 499     return NULL;
 500   }
 501 
 502   assert(in(TypeFunc::Control) != NULL &&
 503          in(TypeFunc::Memory) != NULL &&
 504          in(ArrayCopyNode::Src) != NULL &&
 505          in(ArrayCopyNode::Dest) != NULL &&
 506          in(ArrayCopyNode::Length) != NULL &&
 507          ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) ||
 508           is_clonebasic()), "broken inputs");
 509 
 510   if (in(TypeFunc::Control)->is_top() ||
 511       in(TypeFunc::Memory)->is_top() ||
 512       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
 513       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
 514       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
 515       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
 516     return NULL;
 517   }
 518 
 519   int count = get_count(phase);
 520 
 521   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
 522     return NULL;
 523   }
 524 
 525   Node* mem = try_clone_instance(phase, can_reshape, count);
 526   if (mem != NULL) {
 527     return (mem == NodeSentinel) ? NULL : mem;
 528   }
 529 
 530   Node* adr_src = NULL;
 531   Node* base_src = NULL;
 532   Node* adr_dest = NULL;
 533   Node* base_dest = NULL;
 534   BasicType copy_type = T_ILLEGAL;
 535   const Type* value_type = NULL;
 536   bool disjoint_bases = false;
 537 
 538   if (!prepare_array_copy(phase, can_reshape,
 539                           adr_src, base_src, adr_dest, base_dest,
 540                           copy_type, value_type, disjoint_bases)) {
 541     return NULL;
 542   }
 543 
 544   Node* src = in(ArrayCopyNode::Src);
 545   Node* dest = in(ArrayCopyNode::Dest);
 546   const TypePtr* atp_src = get_address_type(phase, src);
 547   const TypePtr* atp_dest = get_address_type(phase, dest);
 548   uint alias_idx_src = phase->C->get_alias_index(atp_src);
 549   uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 550 
 551   Node *in_mem = in(TypeFunc::Memory);
 552   Node *start_mem_src = in_mem;
 553   Node *start_mem_dest = in_mem;
 554   if (in_mem->is_MergeMem()) {
 555     start_mem_src = in_mem->as_MergeMem()->memory_at(alias_idx_src);
 556     start_mem_dest = in_mem->as_MergeMem()->memory_at(alias_idx_dest);
 557   }
 558 
 559 
 560   if (can_reshape) {
 561     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 562     phase->is_IterGVN()->set_delay_transform(true);
 563   }
 564 
 565   Node* backward_ctl = phase->C->top();
 566   Node* forward_ctl = phase->C->top();
 567   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
 568 
 569   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
 570                                          start_mem_src, start_mem_dest,
 571                                          atp_src, atp_dest,
 572                                          adr_src, base_src, adr_dest, base_dest,
 573                                          copy_type, value_type, count);
 574 
 575   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
 576                                            start_mem_src, start_mem_dest,
 577                                            atp_src, atp_dest,
 578                                            adr_src, base_src, adr_dest, base_dest,
 579                                            copy_type, value_type, count);
 580 
 581   Node* ctl = NULL;
 582   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
 583     ctl = new RegionNode(3);
 584     mem = new PhiNode(ctl, Type::MEMORY, atp_dest);
 585     ctl->init_req(1, forward_ctl);
 586     mem->init_req(1, forward_mem);
 587     ctl->init_req(2, backward_ctl);
 588     mem->init_req(2, backward_mem);
 589     ctl = phase->transform(ctl);
 590     mem = phase->transform(mem);
 591   } else if (!forward_ctl->is_top()) {
 592     ctl = forward_ctl;
 593     mem = forward_mem;
 594   } else {
 595     assert(!backward_ctl->is_top(), "no copy?");
 596     ctl = backward_ctl;
 597     mem = backward_mem;
 598   }
 599 
 600   if (can_reshape) {
 601     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 602     phase->is_IterGVN()->set_delay_transform(false);
 603   }
 604 
 605   MergeMemNode* out_mem = MergeMemNode::make(in_mem);
 606   out_mem->set_memory_at(alias_idx_dest, mem);
 607   mem = out_mem;
 608 
 609   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 610     return NULL;
 611   }
 612 
 613   return mem;
 614 }
 615 
 616 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 617   Node* dest = in(ArrayCopyNode::Dest);
 618   if (dest->is_top()) {
 619     return false;
 620   }
 621   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 622   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 623   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 624          _src_type->is_known_instance(), "result of EA not recorded");
 625 
 626   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 627     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
 628     return t_oop->instance_id() == _dest_type->instance_id();
 629   }
 630 
 631   return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase);
 632 }
 633 
 634 bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, ArrayCopyNode*& ac) {
 635   if (n->Opcode() == Op_StoreCM ||
 636       n->Opcode() == Op_StoreB) {
 637     // Ignore card mark stores
 638     n = n->in(MemNode::Memory);
 639   }
 640 
 641   if (n->is_Proj()) {
 642     n = n->in(0);
 643     if (n->is_Call() && n->as_Call()->may_modify(t_oop, phase)) {
 644       if (n->isa_ArrayCopy() != NULL) {
 645         ac = n->as_ArrayCopy();
 646       }
 647       return true;
 648     }
 649   }
 650   return false;
 651 }
 652 
 653 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) {
 654   Node* mem = mb->in(TypeFunc::Memory);
 655 
 656   if (mem->is_MergeMem()) {
 657     Node* n = mem->as_MergeMem()->memory_at(Compile::AliasIdxRaw);
 658     if (may_modify_helper(t_oop, n, phase, ac)) {
 659       return true;
 660     } else if (n->is_Phi()) {
 661       for (uint i = 1; i < n->req(); i++) {
 662         if (n->in(i) != NULL) {
 663           if (may_modify_helper(t_oop, n->in(i), phase, ac)) {
 664             return true;
 665           }
 666         }
 667       }
 668     }
 669   }
 670 
 671   return false;
 672 }
 673 
 674 // Does this array copy modify offsets between offset_lo and offset_hi
 675 // in the destination array
 676 // if must_modify is false, return true if the copy could write
 677 // between offset_lo and offset_hi
 678 // if must_modify is true, return true if the copy is guaranteed to
 679 // write between offset_lo and offset_hi
 680 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) {
 681   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
 682 
 683   Node* dest = in(ArrayCopyNode::Dest);
 684   Node* src_pos = in(ArrayCopyNode::SrcPos);
 685   Node* dest_pos = in(ArrayCopyNode::DestPos);
 686   Node* len = in(ArrayCopyNode::Length);
 687 
 688   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
 689   const TypeInt *len_t = phase->type(len)->isa_int();
 690   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
 691 
 692   if (dest_pos_t != NULL && len_t != NULL && ary_t != NULL) {
 693     BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();
 694     uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
 695     uint elemsize = type2aelembytes(ary_elem);
 696 
 697     jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
 698     jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
 699     jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
 700     jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
 701 
 702     if (must_modify) {
 703       if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
 704         return true;
 705       }
 706     } else {
 707       if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
 708         return true;
 709       }
 710     }
 711   }
 712   return false;
 713 }