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