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