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