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