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 (ShenandoahStoreValEnqueueBarrier) {
 158     return phase->transform(new ShenandoahEnqueueBarrierNode(v));
 159   }
 160   return v;
 161 }
 162 #endif
 163 
 164 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
 165   if (!is_clonebasic()) {
 166     return NULL;
 167   }
 168 
 169   Node* src = in(ArrayCopyNode::Src);
 170   Node* dest = in(ArrayCopyNode::Dest);
 171   Node* ctl = in(TypeFunc::Control);
 172   Node* in_mem = in(TypeFunc::Memory);
 173 
 174   const Type* src_type = phase->type(src);
 175 
 176   assert(src->is_AddP(), "should be base + off");
 177   assert(dest->is_AddP(), "should be base + off");
 178   Node* base_src = src->in(AddPNode::Base);
 179   Node* base_dest = dest->in(AddPNode::Base);
 180 
 181   MergeMemNode* mem = MergeMemNode::make(in_mem);
 182 
 183   const TypeInstPtr* inst_src = src_type->isa_instptr();
 184 
 185   if (inst_src == NULL) {
 186     return NULL;
 187   }
 188 
 189   if (!inst_src->klass_is_exact()) {
 190     ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
 191     assert(!ik->is_interface() && !ik->has_subklass(), "inconsistent klass hierarchy");
 192     phase->C->dependencies()->assert_leaf_type(ik);
 193   }
 194 
 195   ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
 196   assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
 197 
 198   for (int i = 0; i < count; i++) {
 199     ciField* field = ik->nonstatic_field_at(i);
 200     int fieldidx = phase->C->alias_type(field)->index();
 201     const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();
 202     Node* off = phase->MakeConX(field->offset());
 203     Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));
 204     Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));
 205     BasicType bt = field->layout_type();
 206 
 207     const Type *type;
 208     if (bt == T_OBJECT) {
 209       if (!field->type()->is_loaded()) {
 210         type = TypeInstPtr::BOTTOM;
 211       } else {
 212         ciType* field_klass = field->type();
 213         type = TypeOopPtr::make_from_klass(field_klass->as_klass());
 214       }
 215     } else {
 216       type = Type::get_const_basic_type(bt);
 217     }
 218 
 219     Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered);
 220     v = phase->transform(v);
 221 #if INCLUDE_SHENANDOAHGC
 222     if (UseShenandoahGC && bt == T_OBJECT) {
 223       v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mem, ctl);
 224     }
 225 #endif
 226     Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered);
 227     s = phase->transform(s);
 228     mem->set_memory_at(fieldidx, s);
 229   }
 230 
 231   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 232     // Return NodeSentinel to indicate that the transform failed
 233     return NodeSentinel;
 234   }
 235 
 236   return mem;
 237 }
 238 
 239 bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,
 240                                        Node*& adr_src,
 241                                        Node*& base_src,
 242                                        Node*& adr_dest,
 243                                        Node*& base_dest,
 244                                        BasicType& copy_type,
 245                                        const Type*& value_type,
 246                                        bool& disjoint_bases) {
 247   Node* src = in(ArrayCopyNode::Src);
 248   Node* dest = in(ArrayCopyNode::Dest);
 249   const Type* src_type = phase->type(src);
 250   const TypeAryPtr* ary_src = src_type->isa_aryptr();
 251 
 252   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
 253     const Type* dest_type = phase->type(dest);
 254     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
 255     Node* src_offset = in(ArrayCopyNode::SrcPos);
 256     Node* dest_offset = in(ArrayCopyNode::DestPos);
 257 
 258     // newly allocated object is guaranteed to not overlap with source object
 259     disjoint_bases = is_alloc_tightly_coupled();
 260 
 261     if (ary_src  == NULL || ary_src->klass()  == NULL ||
 262         ary_dest == NULL || ary_dest->klass() == NULL) {
 263       // We don't know if arguments are arrays
 264       return false;
 265     }
 266 
 267     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 268     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
 269     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
 270     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
 271 
 272     if (src_elem != dest_elem || dest_elem == T_VOID) {
 273       // We don't know if arguments are arrays of the same type
 274       return false;
 275     }
 276 
 277     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 278     if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() ||
 279          (bs->array_copy_requires_gc_barriers(T_OBJECT) SHENANDOAHGC_ONLY(&& !ShenandoahStoreValEnqueueBarrier)))) {
 280       // It's an object array copy but we can't emit the card marking
 281       // that is needed
 282       return false;
 283     }
 284 
 285     value_type = ary_src->elem();
 286 
 287     base_src = src;
 288     base_dest = dest;
 289 
 290     uint shift  = exact_log2(type2aelembytes(dest_elem));
 291     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
 292 
 293     adr_src = src;
 294     adr_dest = dest;
 295 
 296     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
 297     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
 298 
 299     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
 300     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
 301 
 302     adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale));
 303     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale));
 304 
 305     adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header));
 306     adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header));
 307 
 308     adr_src = phase->transform(adr_src);
 309     adr_dest = phase->transform(adr_dest);
 310 
 311     copy_type = dest_elem;
 312   } else {
 313     assert(ary_src != NULL, "should be a clone");
 314     assert(is_clonebasic(), "should be");
 315 
 316     disjoint_bases = true;
 317     assert(src->is_AddP(), "should be base + off");
 318     assert(dest->is_AddP(), "should be base + off");
 319     adr_src = src;
 320     base_src = src->in(AddPNode::Base);
 321     adr_dest = dest;
 322     base_dest = dest->in(AddPNode::Base);
 323 
 324     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?");
 325     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
 326     if (elem == T_ARRAY)  elem = T_OBJECT;
 327 
 328     int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con();
 329     assert(diff >= 0, "clone should not start after 1st array element");
 330     if (diff > 0) {
 331       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
 332       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
 333     }
 334 
 335     copy_type = elem;
 336     value_type = ary_src->elem();
 337   }
 338   return true;
 339 }
 340 
 341 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN *phase, Node* n) {
 342   const Type* at = phase->type(n);
 343   assert(at != Type::TOP, "unexpected type");
 344   const TypePtr* atp = at->isa_ptr();
 345   // adjust atp to be the correct array element address type
 346   atp = atp->add_offset(Type::OffsetBot);
 347   return atp;
 348 }
 349 
 350 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
 351   Node* ctl = in(TypeFunc::Control);
 352   if (!disjoint_bases && count > 1) {
 353     Node* src_offset = in(ArrayCopyNode::SrcPos);
 354     Node* dest_offset = in(ArrayCopyNode::DestPos);
 355     assert(src_offset != NULL && dest_offset != NULL, "should be");
 356     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
 357     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
 358     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
 359 
 360     phase->transform(iff);
 361 
 362     forward_ctl = phase->transform(new IfFalseNode(iff));
 363     backward_ctl = phase->transform(new IfTrueNode(iff));
 364   } else {
 365     forward_ctl = ctl;
 366   }
 367 }
 368 
 369 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
 370                                         bool can_reshape,
 371                                         Node*& forward_ctl,
 372                                         MergeMemNode* mm,
 373                                         const TypePtr* atp_src,
 374                                         const TypePtr* atp_dest,
 375                                         Node* adr_src,
 376                                         Node* base_src,
 377                                         Node* adr_dest,
 378                                         Node* base_dest,
 379                                         BasicType copy_type,
 380                                         const Type* value_type,
 381                                         int count) {
 382   if (!forward_ctl->is_top()) {
 383     // copy forward
 384     mm = mm->clone()->as_MergeMem();
 385     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 386     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 387     Node *start_mem_src = mm->memory_at(alias_idx_src);
 388     Node *start_mem_dest = mm->memory_at(alias_idx_dest);
 389     Node* mem = start_mem_dest;
 390     bool same_alias = (alias_idx_src == alias_idx_dest);
 391 
 392     if (count > 0) {
 393       Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 394       v = phase->transform(v);
 395 #if INCLUDE_SHENANDOAHGC
 396       if (UseShenandoahGC && copy_type == T_OBJECT) {
 397         v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, forward_ctl);
 398       }
 399 #endif
 400       mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 401       mem = phase->transform(mem);
 402       for (int i = 1; i < count; i++) {
 403         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 404         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 405         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 406         v = LoadNode::make(*phase, forward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 407         v = phase->transform(v);
 408 #if INCLUDE_SHENANDOAHGC
 409         if (UseShenandoahGC && copy_type == T_OBJECT) {
 410           v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, forward_ctl);
 411         }
 412 #endif
 413         mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 414         mem = phase->transform(mem);
 415       }
 416       mm->set_memory_at(alias_idx_dest, mem);
 417     } else if(can_reshape) {
 418       PhaseIterGVN* igvn = phase->is_IterGVN();
 419       igvn->_worklist.push(adr_src);
 420       igvn->_worklist.push(adr_dest);
 421     }
 422     return mm;
 423   }
 424   return phase->C->top();
 425 }
 426 
 427 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
 428                                          bool can_reshape,
 429                                          Node*& backward_ctl,
 430                                          MergeMemNode* mm,
 431                                          const TypePtr* atp_src,
 432                                          const TypePtr* atp_dest,
 433                                          Node* adr_src,
 434                                          Node* base_src,
 435                                          Node* adr_dest,
 436                                          Node* base_dest,
 437                                          BasicType copy_type,
 438                                          const Type* value_type,
 439                                          int count) {
 440   if (!backward_ctl->is_top()) {
 441     // copy backward
 442     mm = mm->clone()->as_MergeMem();
 443     uint alias_idx_src = phase->C->get_alias_index(atp_src);
 444     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
 445     Node *start_mem_src = mm->memory_at(alias_idx_src);
 446     Node *start_mem_dest = mm->memory_at(alias_idx_dest);
 447     Node* mem = start_mem_dest;
 448 
 449     assert(copy_type != T_OBJECT SHENANDOAHGC_ONLY(|| ShenandoahStoreValEnqueueBarrier), "only tightly coupled allocations for object arrays");
 450     bool same_alias = (alias_idx_src == alias_idx_dest);
 451 
 452     if (count > 0) {
 453       for (int i = count-1; i >= 1; i--) {
 454         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
 455         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
 456         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
 457         Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
 458         v = phase->transform(v);
 459 #if INCLUDE_SHENANDOAHGC
 460         if (UseShenandoahGC && copy_type == T_OBJECT) {
 461           v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, backward_ctl);
 462         }
 463 #endif
 464         mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
 465         mem = phase->transform(mem);
 466       }
 467       Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
 468       v = phase->transform(v);
 469 #if INCLUDE_SHENANDOAHGC
 470       if (UseShenandoahGC && copy_type == T_OBJECT) {
 471         v = shenandoah_add_storeval_barrier(phase, can_reshape, v, mm, backward_ctl);
 472       }
 473 #endif
 474       mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
 475       mem = phase->transform(mem);
 476       mm->set_memory_at(alias_idx_dest, mem);
 477     } else if(can_reshape) {
 478       PhaseIterGVN* igvn = phase->is_IterGVN();
 479       igvn->_worklist.push(adr_src);
 480       igvn->_worklist.push(adr_dest);
 481     }
 482     return phase->transform(mm);
 483   }
 484   return phase->C->top();
 485 }
 486 
 487 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
 488                                      Node* ctl, Node *mem) {
 489   if (can_reshape) {
 490     PhaseIterGVN* igvn = phase->is_IterGVN();
 491     igvn->set_delay_transform(false);
 492     if (is_clonebasic()) {
 493       Node* out_mem = proj_out(TypeFunc::Memory);
 494 
 495       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 496       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
 497           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
 498         assert(bs->array_copy_requires_gc_barriers(T_OBJECT), "can only happen with card marking");
 499         return false;
 500       }
 501 
 502       igvn->replace_node(out_mem->raw_out(0), mem);
 503 
 504       Node* out_ctl = proj_out(TypeFunc::Control);
 505       igvn->replace_node(out_ctl, ctl);
 506     } else {
 507       // replace fallthrough projections of the ArrayCopyNode by the
 508       // new memory, control and the input IO.
 509       CallProjections callprojs;
 510       extract_projections(&callprojs, true, false);
 511 
 512       if (callprojs.fallthrough_ioproj != NULL) {
 513         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
 514       }
 515       if (callprojs.fallthrough_memproj != NULL) {
 516         igvn->replace_node(callprojs.fallthrough_memproj, mem);
 517       }
 518       if (callprojs.fallthrough_catchproj != NULL) {
 519         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
 520       }
 521 
 522       // The ArrayCopyNode is not disconnected. It still has the
 523       // projections for the exception case. Replace current
 524       // ArrayCopyNode with a dummy new one with a top() control so
 525       // that this part of the graph stays consistent but is
 526       // eventually removed.
 527 
 528       set_req(0, phase->C->top());
 529       remove_dead_region(phase, can_reshape);
 530     }
 531   } else {
 532     if (in(TypeFunc::Control) != ctl) {
 533       // we can't return new memory and control from Ideal at parse time
 534       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
 535       phase->record_for_igvn(this);
 536       return false;
 537     }
 538   }
 539   return true;
 540 }
 541 
 542 
 543 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 544   if (remove_dead_region(phase, can_reshape))  return this;
 545 
 546   if (StressArrayCopyMacroNode && !can_reshape) {
 547     phase->record_for_igvn(this);
 548     return NULL;
 549   }
 550 
 551   // See if it's a small array copy and we can inline it as
 552   // loads/stores
 553   // Here we can only do:
 554   // - arraycopy if all arguments were validated before and we don't
 555   // need card marking
 556   // - clone for which we don't need to do card marking
 557 
 558   if (!is_clonebasic() && !is_arraycopy_validated() &&
 559       !is_copyofrange_validated() && !is_copyof_validated()) {
 560     return NULL;
 561   }
 562 
 563   assert(in(TypeFunc::Control) != NULL &&
 564          in(TypeFunc::Memory) != NULL &&
 565          in(ArrayCopyNode::Src) != NULL &&
 566          in(ArrayCopyNode::Dest) != NULL &&
 567          in(ArrayCopyNode::Length) != NULL &&
 568          ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) ||
 569           is_clonebasic()), "broken inputs");
 570 
 571   if (in(TypeFunc::Control)->is_top() ||
 572       in(TypeFunc::Memory)->is_top() ||
 573       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
 574       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
 575       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
 576       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
 577     return NULL;
 578   }
 579 
 580   int count = get_count(phase);
 581 
 582   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
 583     return NULL;
 584   }
 585 
 586   Node* mem = try_clone_instance(phase, can_reshape, count);
 587   if (mem != NULL) {
 588     return (mem == NodeSentinel) ? NULL : mem;
 589   }
 590 
 591   Node* adr_src = NULL;
 592   Node* base_src = NULL;
 593   Node* adr_dest = NULL;
 594   Node* base_dest = NULL;
 595   BasicType copy_type = T_ILLEGAL;
 596   const Type* value_type = NULL;
 597   bool disjoint_bases = false;
 598 
 599   if (!prepare_array_copy(phase, can_reshape,
 600                           adr_src, base_src, adr_dest, base_dest,
 601                           copy_type, value_type, disjoint_bases)) {
 602     return NULL;
 603   }
 604 
 605   Node* src = in(ArrayCopyNode::Src);
 606   Node* dest = in(ArrayCopyNode::Dest);
 607   const TypePtr* atp_src = get_address_type(phase, src);
 608   const TypePtr* atp_dest = get_address_type(phase, dest);
 609 
 610   Node *in_mem = in(TypeFunc::Memory);
 611   if (!in_mem->is_MergeMem()) {
 612     in_mem = MergeMemNode::make(in_mem);
 613   }
 614 
 615 
 616   if (can_reshape) {
 617     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
 618     phase->is_IterGVN()->set_delay_transform(true);
 619   }
 620 
 621   Node* backward_ctl = phase->C->top();
 622   Node* forward_ctl = phase->C->top();
 623   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
 624 
 625   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
 626                                          in_mem->as_MergeMem(),
 627                                          atp_src, atp_dest,
 628                                          adr_src, base_src, adr_dest, base_dest,
 629                                          copy_type, value_type, count);
 630 
 631   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
 632                                            in_mem->as_MergeMem(),
 633                                            atp_src, atp_dest,
 634                                            adr_src, base_src, adr_dest, base_dest,
 635                                            copy_type, value_type, count);
 636 
 637   Node* ctl = NULL;
 638   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
 639     ctl = new RegionNode(3);
 640     ctl->init_req(1, forward_ctl);
 641     ctl->init_req(2, backward_ctl);
 642     ctl = phase->transform(ctl);
 643     MergeMemNode* forward_mm = forward_mem->as_MergeMem();
 644     MergeMemNode* backward_mm = backward_mem->as_MergeMem();
 645     for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
 646       if (mms.memory() != mms.memory2()) {
 647         Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
 648         phi->init_req(1, mms.memory());
 649         phi->init_req(2, mms.memory2());
 650         phi = phase->transform(phi);
 651         mms.set_memory(phi);
 652       }
 653     }
 654     mem = forward_mem;
 655   } else if (!forward_ctl->is_top()) {
 656     ctl = forward_ctl;
 657     mem = forward_mem;
 658   } else {
 659     assert(!backward_ctl->is_top(), "no copy?");
 660     ctl = backward_ctl;
 661     mem = backward_mem;
 662   }
 663 
 664   if (can_reshape) {
 665     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
 666     phase->is_IterGVN()->set_delay_transform(false);
 667   }
 668 
 669   if (!finish_transform(phase, can_reshape, ctl, mem)) {
 670     return NULL;
 671   }
 672 
 673   return mem;
 674 }
 675 
 676 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
 677   Node* dest = in(ArrayCopyNode::Dest);
 678   if (dest->is_top()) {
 679     return false;
 680   }
 681   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
 682   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
 683   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
 684          _src_type->is_known_instance(), "result of EA not recorded");
 685 
 686   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
 687     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
 688     return t_oop->instance_id() == _dest_type->instance_id();
 689   }
 690 
 691   return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase);
 692 }
 693 
 694 bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call) {
 695   if (n != NULL &&
 696       n->is_Call() &&
 697       n->as_Call()->may_modify(t_oop, phase) &&
 698       (n->as_Call()->is_ArrayCopy() || n->as_Call()->is_call_to_arraycopystub())) {
 699     call = n->as_Call();
 700     return true;
 701   }
 702   return false;
 703 }
 704 
 705 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) {
 706 
 707   Node* c = mb->in(0);
 708 
 709   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 710   // step over g1 gc barrier if we're at e.g. a clone with ReduceInitialCardMarks off
 711   c = bs->step_over_gc_barrier(c);
 712 
 713   CallNode* call = NULL;
 714   guarantee(c != NULL, "step_over_gc_barrier failed, there must be something to step to.");
 715   if (c->is_Region()) {
 716     for (uint i = 1; i < c->req(); i++) {
 717       if (c->in(i) != NULL) {
 718         Node* n = c->in(i)->in(0);
 719         if (may_modify_helper(t_oop, n, phase, call)) {
 720           ac = call->isa_ArrayCopy();
 721           assert(c == mb->in(0), "only for clone");
 722           return true;
 723         }
 724       }
 725     }
 726   } else if (may_modify_helper(t_oop, c->in(0), phase, call)) {
 727     ac = call->isa_ArrayCopy();
 728 #ifdef ASSERT
 729     bool use_ReduceInitialCardMarks = BarrierSet::barrier_set()->is_a(BarrierSet::CardTableBarrierSet) &&
 730       static_cast<CardTableBarrierSetC2*>(bs)->use_ReduceInitialCardMarks();
 731     assert(c == mb->in(0) || (ac != NULL && ac->is_clonebasic() && !use_ReduceInitialCardMarks), "only for clone");
 732 #endif
 733     return true;
 734   }
 735 
 736   return false;
 737 }
 738 
 739 // Does this array copy modify offsets between offset_lo and offset_hi
 740 // in the destination array
 741 // if must_modify is false, return true if the copy could write
 742 // between offset_lo and offset_hi
 743 // if must_modify is true, return true if the copy is guaranteed to
 744 // write between offset_lo and offset_hi
 745 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {
 746   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
 747 
 748   Node* dest = in(Dest);
 749   Node* dest_pos = in(DestPos);
 750   Node* len = in(Length);
 751 
 752   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
 753   const TypeInt *len_t = phase->type(len)->isa_int();
 754   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
 755 
 756   if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {
 757     return !must_modify;
 758   }
 759 
 760   BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();
 761   uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
 762   uint elemsize = type2aelembytes(ary_elem);
 763 
 764   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
 765   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
 766   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
 767   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
 768 
 769   if (must_modify) {
 770     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
 771       return true;
 772     }
 773   } else {
 774     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
 775       return true;
 776     }
 777   }
 778   return false;
 779 }