< prev index next >

src/hotspot/share/opto/arraycopynode.cpp

Print this page

  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 "gc/shared/gc_globals.hpp"
 30 #include "opto/arraycopynode.hpp"
 31 #include "opto/graphKit.hpp"

 32 #include "runtime/sharedRuntime.hpp"
 33 #include "utilities/macros.hpp"
 34 #include "utilities/powerOfTwo.hpp"
 35 
 36 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
 37   : CallNode(arraycopy_type(), NULL, TypePtr::BOTTOM),
 38     _kind(None),
 39     _alloc_tightly_coupled(alloc_tightly_coupled),
 40     _has_negative_length_guard(has_negative_length_guard),
 41     _arguments_validated(false),
 42     _src_type(TypeOopPtr::BOTTOM),
 43     _dest_type(TypeOopPtr::BOTTOM) {
 44   init_class_id(Class_ArrayCopy);
 45   init_flags(Flag_is_macro);
 46   C->add_macro_node(this);
 47 }
 48 
 49 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
 50 
 51 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,

 97 void ArrayCopyNode::dump_compact_spec(outputStream* st) const {
 98   st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");
 99 }
100 #endif
101 
102 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {
103   // check that length is constant
104   Node* length = in(ArrayCopyNode::Length);
105   const Type* length_type = phase->type(length);
106 
107   if (length_type == Type::TOP) {
108     return -1;
109   }
110 
111   assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");
112 
113   return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);
114 }
115 
116 int ArrayCopyNode::get_count(PhaseGVN *phase) const {
117   Node* src = in(ArrayCopyNode::Src);
118   const Type* src_type = phase->type(src);
119 
120   if (is_clonebasic()) {







121     if (src_type->isa_instptr()) {
122       const TypeInstPtr* inst_src = src_type->is_instptr();
123       ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
124       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
125       // fields into account. They are rare anyway so easier to simply
126       // skip instances with injected fields.
127       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
128         return -1;
129       }
130       int nb_fields = ik->nof_nonstatic_fields();
131       return nb_fields;
132     } else {
133       const TypeAryPtr* ary_src = src_type->isa_aryptr();
134       assert (ary_src != NULL, "not an array or instance?");
135       // clone passes a length as a rounded number of longs. If we're
136       // cloning an array we'll do it element by element. If the
137       // length input to ArrayCopyNode is constant, length of input
138       // array must be too.
139 
140       assert((get_length_if_constant(phase) == -1) != ary_src->size()->is_con() ||

141              phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
142 
143       if (ary_src->size()->is_con()) {
144         return ary_src->size()->get_con();
145       }
146       return -1;
147     }
148   }
149 
150   return get_length_if_constant(phase);
151 }
152 
153 Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
154   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
155   C2AccessValuePtr addr(adr, adr_type);
156   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
157   Node* res = bs->load_at(access, type);
158   ctl = access.ctl();
159   return res;
160 }

173 
174 
175 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
176   if (!is_clonebasic()) {
177     return NULL;
178   }
179 
180   Node* base_src = in(ArrayCopyNode::Src);
181   Node* base_dest = in(ArrayCopyNode::Dest);
182   Node* ctl = in(TypeFunc::Control);
183   Node* in_mem = in(TypeFunc::Memory);
184 
185   const Type* src_type = phase->type(base_src);
186   const TypeInstPtr* inst_src = src_type->isa_instptr();
187   if (inst_src == NULL) {
188     return NULL;
189   }
190 
191   MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
192   PhaseIterGVN* igvn = phase->is_IterGVN();

193   if (igvn != NULL) {
194     igvn->_worklist.push(mem);
195   }
196 
197   if (!inst_src->klass_is_exact()) {
198     ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
199     assert(!ik->is_interface(), "inconsistent klass hierarchy");
200     if (ik->has_subklass()) {
201       // Concurrent class loading.
202       // Fail fast and return NodeSentinel to indicate that the transform failed.
203       return NodeSentinel;
204     } else {
205       phase->C->dependencies()->assert_leaf_type(ik);
206     }
207   }
208 
209   ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
210   assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
211 
212   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();

256   const TypeAryPtr* ary_src = src_type->isa_aryptr();
257 
258   Node* src_offset = in(ArrayCopyNode::SrcPos);
259   Node* dest_offset = in(ArrayCopyNode::DestPos);
260 
261   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
262     const Type* dest_type = phase->type(base_dest);
263     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
264 
265     // newly allocated object is guaranteed to not overlap with source object
266     disjoint_bases = is_alloc_tightly_coupled();
267 
268     if (ary_src  == NULL || ary_src->klass()  == NULL ||
269         ary_dest == NULL || ary_dest->klass() == NULL) {
270       // We don't know if arguments are arrays
271       return false;
272     }
273 
274     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
275     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
276     if (is_reference_type(src_elem))   src_elem  = T_OBJECT;
277     if (is_reference_type(dest_elem))  dest_elem = T_OBJECT;




278 
279     if (src_elem != dest_elem || dest_elem == T_VOID) {
280       // We don't know if arguments are arrays of the same type
281       return false;
282     }
283 
284     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
285     if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {
286       // It's an object array copy but we can't emit the card marking
287       // that is needed

288       return false;
289     }
290 
291     value_type = ary_src->elem();
292 
293     uint shift  = exact_log2(type2aelembytes(dest_elem));




294     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
295 
296     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
297     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
298     if (src_offset->is_top() || dest_offset->is_top()) {
299       // Offset is out of bounds (the ArrayCopyNode will be removed)
300       return false;
301     }
302 
303     Node* src_scale  = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
304     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
305 
306     adr_src          = phase->transform(new AddPNode(base_src, base_src, src_scale));
307     adr_dest         = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));
308 
309     adr_src          = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));
310     adr_dest         = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));
311 
312     copy_type = dest_elem;
313   } else {
314     assert(ary_src != NULL, "should be a clone");
315     assert(is_clonebasic(), "should be");
316 
317     disjoint_bases = true;
318 
319     adr_src  = phase->transform(new AddPNode(base_src, base_src, src_offset));
320     adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));


321 
322     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
323     if (is_reference_type(elem)) {
324       elem = T_OBJECT;
325     }
326 
327     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
328     if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {



329       return false;
330     }
331 



332     // The address is offseted to an aligned address where a raw copy would start.
333     // If the clone copy is decomposed into load-stores - the address is adjusted to
334     // point at where the array starts.
335     const Type* toff = phase->type(src_offset);
336     int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
337     int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
338     assert(diff >= 0, "clone should not start after 1st array element");
339     if (diff > 0) {
340       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
341       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
342     }
343     copy_type = elem;
344     value_type = ary_src->elem();
345   }
346   return true;
347 }
348 
349 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
350   if (atp == TypeOopPtr::BOTTOM) {
351     atp = phase->type(n)->isa_ptr();
352   }
353   // adjust atp to be the correct array element address type
354   return atp->add_offset(Type::OffsetBot);
355 }
356 
357 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
358   Node* ctl = in(TypeFunc::Control);
359   if (!disjoint_bases && count > 1) {

360     Node* src_offset = in(ArrayCopyNode::SrcPos);
361     Node* dest_offset = in(ArrayCopyNode::DestPos);
362     assert(src_offset != NULL && dest_offset != NULL, "should be");
363     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
364     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
365     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
366 
367     phase->transform(iff);





368 
369     forward_ctl = phase->transform(new IfFalseNode(iff));
370     backward_ctl = phase->transform(new IfTrueNode(iff));































371   } else {
372     forward_ctl = ctl;




373   }

374 }
375 
376 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
377                                         bool can_reshape,
378                                         Node*& forward_ctl,
379                                         Node* mem,
380                                         const TypePtr* atp_src,
381                                         const TypePtr* atp_dest,
382                                         Node* adr_src,
383                                         Node* base_src,
384                                         Node* adr_dest,
385                                         Node* base_dest,
386                                         BasicType copy_type,
387                                         const Type* value_type,
388                                         int count) {
389   if (!forward_ctl->is_top()) {
390     // copy forward
391     MergeMemNode* mm = MergeMemNode::make(mem);
392 













393     if (count > 0) {
394       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
395       Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);
396       store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
397       for (int i = 1; i < count; i++) {
398         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
399         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
400         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
401         v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);
402         store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
403       }
404     } else if (can_reshape) {
405       PhaseIterGVN* igvn = phase->is_IterGVN();
406       igvn->_worklist.push(adr_src);
407       igvn->_worklist.push(adr_dest);

408     }
409     return mm;
410   }
411   return phase->C->top();
412 }
413 
414 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
415                                          bool can_reshape,
416                                          Node*& backward_ctl,
417                                          Node* mem,
418                                          const TypePtr* atp_src,
419                                          const TypePtr* atp_dest,
420                                          Node* adr_src,
421                                          Node* base_src,
422                                          Node* adr_dest,
423                                          Node* base_dest,
424                                          BasicType copy_type,
425                                          const Type* value_type,
426                                          int count) {
427   if (!backward_ctl->is_top()) {
428     // copy backward
429     MergeMemNode* mm = MergeMemNode::make(mem);
430 
431     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
432     assert(copy_type != T_OBJECT || !bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, false, BarrierSetC2::Optimization), "only tightly coupled allocations for object arrays");
433 
434     if (count > 0) {
435       for (int i = count-1; i >= 1; i--) {
436         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
437         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
438         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
439         Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);
440         store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
441       }
442       Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);
443       store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
444     } else if (can_reshape) {
445       PhaseIterGVN* igvn = phase->is_IterGVN();
446       igvn->_worklist.push(adr_src);
447       igvn->_worklist.push(adr_dest);
448     }
449     return phase->transform(mm);
450   }
451   return phase->C->top();
452 }
453 
454 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
455                                      Node* ctl, Node *mem) {
456   if (can_reshape) {
457     PhaseIterGVN* igvn = phase->is_IterGVN();
458     igvn->set_delay_transform(false);
459     if (is_clonebasic()) {
460       Node* out_mem = proj_out(TypeFunc::Memory);
461 
462       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
463       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
464           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
465         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
466         return false;
467       }
468 
469       igvn->replace_node(out_mem->raw_out(0), mem);
470 
471       Node* out_ctl = proj_out(TypeFunc::Control);
472       igvn->replace_node(out_ctl, ctl);
473     } else {
474       // replace fallthrough projections of the ArrayCopyNode by the
475       // new memory, control and the input IO.
476       CallProjections callprojs;
477       extract_projections(&callprojs, true, false);
478 
479       if (callprojs.fallthrough_ioproj != NULL) {
480         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
481       }
482       if (callprojs.fallthrough_memproj != NULL) {
483         igvn->replace_node(callprojs.fallthrough_memproj, mem);
484       }
485       if (callprojs.fallthrough_catchproj != NULL) {
486         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
487       }
488 
489       // The ArrayCopyNode is not disconnected. It still has the
490       // projections for the exception case. Replace current
491       // ArrayCopyNode with a dummy new one with a top() control so
492       // that this part of the graph stays consistent but is
493       // eventually removed.
494 
495       set_req(0, phase->C->top());
496       remove_dead_region(phase, can_reshape);
497     }
498   } else {
499     if (in(TypeFunc::Control) != ctl) {
500       // we can't return new memory and control from Ideal at parse time









501       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
502       phase->record_for_igvn(this);
503       return false;
504     }
505   }
506   return true;
507 }
508 
509 
510 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
511   if (remove_dead_region(phase, can_reshape))  return this;




512 
513   if (StressArrayCopyMacroNode && !can_reshape) {
514     phase->record_for_igvn(this);
515     return NULL;
516   }
517 
518   // See if it's a small array copy and we can inline it as
519   // loads/stores
520   // Here we can only do:
521   // - arraycopy if all arguments were validated before and we don't
522   // need card marking
523   // - clone for which we don't need to do card marking
524 
525   if (!is_clonebasic() && !is_arraycopy_validated() &&
526       !is_copyofrange_validated() && !is_copyof_validated()) {
527     return NULL;
528   }
529 
530   assert(in(TypeFunc::Control) != NULL &&
531          in(TypeFunc::Memory) != NULL &&

533          in(ArrayCopyNode::Dest) != NULL &&
534          in(ArrayCopyNode::Length) != NULL &&
535          in(ArrayCopyNode::SrcPos) != NULL &&
536          in(ArrayCopyNode::DestPos) != NULL, "broken inputs");
537 
538   if (in(TypeFunc::Control)->is_top() ||
539       in(TypeFunc::Memory)->is_top() ||
540       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
541       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
542       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
543       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
544     return NULL;
545   }
546 
547   int count = get_count(phase);
548 
549   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
550     return NULL;
551   }
552 











553   Node* mem = try_clone_instance(phase, can_reshape, count);
554   if (mem != NULL) {
555     return (mem == NodeSentinel) ? NULL : mem;
556   }
557 
558   Node* adr_src = NULL;
559   Node* base_src = NULL;
560   Node* adr_dest = NULL;
561   Node* base_dest = NULL;
562   BasicType copy_type = T_ILLEGAL;
563   const Type* value_type = NULL;
564   bool disjoint_bases = false;
565 
566   if (!prepare_array_copy(phase, can_reshape,
567                           adr_src, base_src, adr_dest, base_dest,
568                           copy_type, value_type, disjoint_bases)) {
569     return NULL;
570   }
571 
572   Node* src = in(ArrayCopyNode::Src);
573   Node* dest = in(ArrayCopyNode::Dest);
574   const TypePtr* atp_src = get_address_type(phase, _src_type, src);
575   const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);
576   Node* in_mem = in(TypeFunc::Memory);
















577 
578   if (can_reshape) {
579     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
580     phase->is_IterGVN()->set_delay_transform(true);
581   }
582 




583   Node* backward_ctl = phase->C->top();
584   Node* forward_ctl = phase->C->top();
585   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
586 
587   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
588                                          in_mem,
589                                          atp_src, atp_dest,
590                                          adr_src, base_src, adr_dest, base_dest,
591                                          copy_type, value_type, count);
592 
593   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
594                                            in_mem,
595                                            atp_src, atp_dest,
596                                            adr_src, base_src, adr_dest, base_dest,
597                                            copy_type, value_type, count);
598 
599   Node* ctl = NULL;
600   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
601     ctl = new RegionNode(3);
602     ctl->init_req(1, forward_ctl);
603     ctl->init_req(2, backward_ctl);
604     ctl = phase->transform(ctl);
605     MergeMemNode* forward_mm = forward_mem->as_MergeMem();
606     MergeMemNode* backward_mm = backward_mem->as_MergeMem();
607     for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
608       if (mms.memory() != mms.memory2()) {
609         Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
610         phi->init_req(1, mms.memory());
611         phi->init_req(2, mms.memory2());
612         phi = phase->transform(phi);
613         mms.set_memory(phi);
614       }
615     }
616     mem = forward_mem;
617   } else if (!forward_ctl->is_top()) {
618     ctl = forward_ctl;
619     mem = forward_mem;
620   } else {
621     assert(!backward_ctl->is_top(), "no copy?");
622     ctl = backward_ctl;
623     mem = backward_mem;
624   }
625 
626   if (can_reshape) {
627     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
628     phase->is_IterGVN()->set_delay_transform(false);
629   }
630 
631   if (!finish_transform(phase, can_reshape, ctl, mem)) {




632     return NULL;
633   }
634 
635   return mem;
636 }
637 
638 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
639   Node* dest = in(ArrayCopyNode::Dest);
640   if (dest->is_top()) {
641     return false;
642   }
643   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
644   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
645   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
646          _src_type->is_known_instance(), "result of EA not recorded");
647 
648   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
649     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
650     return t_oop->instance_id() == _dest_type->instance_id();
651   }

704 // in the destination array
705 // if must_modify is false, return true if the copy could write
706 // between offset_lo and offset_hi
707 // if must_modify is true, return true if the copy is guaranteed to
708 // write between offset_lo and offset_hi
709 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {
710   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
711 
712   Node* dest = in(Dest);
713   Node* dest_pos = in(DestPos);
714   Node* len = in(Length);
715 
716   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
717   const TypeInt *len_t = phase->type(len)->isa_int();
718   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
719 
720   if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {
721     return !must_modify;
722   }
723 
724   BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();

725   uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
726   uint elemsize = type2aelembytes(ary_elem);



727 
728   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
729   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
730   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
731   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
732 
733   if (must_modify) {
734     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
735       return true;
736     }
737   } else {
738     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
739       return true;
740     }
741   }
742   return false;
743 }
744 
745 // As an optimization, choose optimum vector size for copy length known at compile time.
746 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {

  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 "ci/ciFlatArrayKlass.hpp"
 27 #include "gc/shared/barrierSet.hpp"
 28 #include "gc/shared/c2/barrierSetC2.hpp"
 29 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
 30 #include "gc/shared/gc_globals.hpp"
 31 #include "opto/arraycopynode.hpp"
 32 #include "opto/graphKit.hpp"
 33 #include "opto/inlinetypenode.hpp"
 34 #include "runtime/sharedRuntime.hpp"
 35 #include "utilities/macros.hpp"
 36 #include "utilities/powerOfTwo.hpp"
 37 
 38 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
 39   : CallNode(arraycopy_type(), NULL, TypePtr::BOTTOM),
 40     _kind(None),
 41     _alloc_tightly_coupled(alloc_tightly_coupled),
 42     _has_negative_length_guard(has_negative_length_guard),
 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,

 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   if (is_clonebasic()) {
120     Node* src = in(ArrayCopyNode::Src);
121     const Type* src_type = phase->type(src);
122 
123     if (src_type == Type::TOP) {
124       return -1;
125     }
126 
127     if (src_type->isa_instptr()) {
128       const TypeInstPtr* inst_src = src_type->is_instptr();
129       ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
130       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
131       // fields into account. They are rare anyway so easier to simply
132       // skip instances with injected fields.
133       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
134         return -1;
135       }
136       int nb_fields = ik->nof_nonstatic_fields();
137       return nb_fields;
138     } else {
139       const TypeAryPtr* ary_src = src_type->isa_aryptr();
140       assert (ary_src != NULL, "not an array or instance?");
141       // clone passes a length as a rounded number of longs. If we're
142       // cloning an array we'll do it element by element. If the
143       // length input to ArrayCopyNode is constant, length of input
144       // array must be too.
145 
146       assert((get_length_if_constant(phase) == -1) != ary_src->size()->is_con() ||
147              (UseFlatArray && ary_src->elem()->make_oopptr() != NULL && ary_src->elem()->make_oopptr()->can_be_inline_type()) ||
148              phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
149 
150       if (ary_src->size()->is_con()) {
151         return ary_src->size()->get_con();
152       }
153       return -1;
154     }
155   }
156 
157   return get_length_if_constant(phase);
158 }
159 
160 Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
161   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
162   C2AccessValuePtr addr(adr, adr_type);
163   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
164   Node* res = bs->load_at(access, type);
165   ctl = access.ctl();
166   return res;
167 }

180 
181 
182 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
183   if (!is_clonebasic()) {
184     return NULL;
185   }
186 
187   Node* base_src = in(ArrayCopyNode::Src);
188   Node* base_dest = in(ArrayCopyNode::Dest);
189   Node* ctl = in(TypeFunc::Control);
190   Node* in_mem = in(TypeFunc::Memory);
191 
192   const Type* src_type = phase->type(base_src);
193   const TypeInstPtr* inst_src = src_type->isa_instptr();
194   if (inst_src == NULL) {
195     return NULL;
196   }
197 
198   MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
199   PhaseIterGVN* igvn = phase->is_IterGVN();
200   phase->record_for_igvn(mem);
201   if (igvn != NULL) {
202     igvn->_worklist.push(mem);
203   }
204 
205   if (!inst_src->klass_is_exact()) {
206     ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
207     assert(!ik->is_interface(), "inconsistent klass hierarchy");
208     if (ik->has_subklass()) {
209       // Concurrent class loading.
210       // Fail fast and return NodeSentinel to indicate that the transform failed.
211       return NodeSentinel;
212     } else {
213       phase->C->dependencies()->assert_leaf_type(ik);
214     }
215   }
216 
217   ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
218   assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
219 
220   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();

264   const TypeAryPtr* ary_src = src_type->isa_aryptr();
265 
266   Node* src_offset = in(ArrayCopyNode::SrcPos);
267   Node* dest_offset = in(ArrayCopyNode::DestPos);
268 
269   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
270     const Type* dest_type = phase->type(base_dest);
271     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
272 
273     // newly allocated object is guaranteed to not overlap with source object
274     disjoint_bases = is_alloc_tightly_coupled();
275 
276     if (ary_src  == NULL || ary_src->klass()  == NULL ||
277         ary_dest == NULL || ary_dest->klass() == NULL) {
278       // We don't know if arguments are arrays
279       return false;
280     }
281 
282     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
283     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
284     if (src_elem == T_ARRAY || (src_elem == T_INLINE_TYPE && ary_src->klass()->is_obj_array_klass())) {
285       src_elem  = T_OBJECT;
286     }
287     if (dest_elem == T_ARRAY || (dest_elem == T_INLINE_TYPE && ary_dest->klass()->is_obj_array_klass())) {
288       dest_elem = T_OBJECT;
289     }
290 
291     if (src_elem != dest_elem || dest_elem == T_VOID) {
292       // We don't know if arguments are arrays of the same type
293       return false;
294     }
295 
296     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
297     if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization) ||
298         (src_elem == T_INLINE_TYPE && ary_src->elem()->inline_klass()->contains_oops() &&
299          bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), T_OBJECT, false, false, BarrierSetC2::Optimization))) {
300       // It's an object array copy but we can't emit the card marking that is needed
301       return false;
302     }
303 
304     value_type = ary_src->elem();
305 
306     uint shift  = exact_log2(type2aelembytes(dest_elem));
307     if (dest_elem == T_INLINE_TYPE) {
308       ciFlatArrayKlass* vak = ary_src->klass()->as_flat_array_klass();
309       shift = vak->log2_element_size();
310     }
311     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
312 
313     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
314     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
315     if (src_offset->is_top() || dest_offset->is_top()) {
316       // Offset is out of bounds (the ArrayCopyNode will be removed)
317       return false;
318     }
319 
320     Node* src_scale  = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
321     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
322 
323     adr_src          = phase->transform(new AddPNode(base_src, base_src, src_scale));
324     adr_dest         = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));
325 
326     adr_src          = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));
327     adr_dest         = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));
328 
329     copy_type = dest_elem;
330   } else {
331     assert(ary_src != NULL, "should be a clone");
332     assert(is_clonebasic(), "should be");
333 
334     disjoint_bases = true;
335 
336     if (ary_src->elem()->make_oopptr() != NULL &&
337         ary_src->elem()->make_oopptr()->can_be_inline_type()) {
338       return false;
339     }
340 
341     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
342     if (elem == T_ARRAY || (elem == T_INLINE_TYPE && ary_src->klass()->is_obj_array_klass())) {
343       elem = T_OBJECT;
344     }
345 
346     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
347     if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization) ||
348         (elem == T_INLINE_TYPE && ary_src->elem()->inline_klass()->contains_oops() &&
349          bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization))) {
350       // It's an object array copy but we can't emit the card marking that is needed
351       return false;
352     }
353 
354     adr_src  = phase->transform(new AddPNode(base_src, base_src, src_offset));
355     adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));
356 
357     // The address is offseted to an aligned address where a raw copy would start.
358     // If the clone copy is decomposed into load-stores - the address is adjusted to
359     // point at where the array starts.
360     const Type* toff = phase->type(src_offset);
361     int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
362     int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
363     assert(diff >= 0, "clone should not start after 1st array element");
364     if (diff > 0) {
365       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
366       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
367     }
368     copy_type = elem;
369     value_type = ary_src->elem();
370   }
371   return true;
372 }
373 
374 const TypeAryPtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
375   if (atp == TypeOopPtr::BOTTOM) {
376     atp = phase->type(n)->isa_ptr();
377   }
378   // adjust atp to be the correct array element address type
379   return atp->add_offset(Type::OffsetBot)->is_aryptr();
380 }
381 
382 void ArrayCopyNode::array_copy_test_overlap(GraphKit& kit, bool disjoint_bases, int count, Node*& backward_ctl) {
383   Node* ctl = kit.control();
384   if (!disjoint_bases && count > 1) {
385     PhaseGVN& gvn = kit.gvn();
386     Node* src_offset = in(ArrayCopyNode::SrcPos);
387     Node* dest_offset = in(ArrayCopyNode::DestPos);
388     assert(src_offset != NULL && dest_offset != NULL, "should be");
389     Node* cmp = gvn.transform(new CmpINode(src_offset, dest_offset));
390     Node *bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
391     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
392 
393     gvn.transform(iff);
394 
395     kit.set_control(gvn.transform(new IfFalseNode(iff)));
396     backward_ctl = gvn.transform(new IfTrueNode(iff));
397   }
398 }
399 
400 void ArrayCopyNode::copy(GraphKit& kit,
401                          const TypeAryPtr* atp_src,
402                          const TypeAryPtr* atp_dest,
403                          int i,
404                          Node* base_src,
405                          Node* base_dest,
406                          Node* adr_src,
407                          Node* adr_dest,
408                          BasicType copy_type,
409                          const Type* value_type) {
410   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
411   Node* ctl = kit.control();
412   if (copy_type == T_INLINE_TYPE) {
413     ciFlatArrayKlass* vak = atp_src->klass()->as_flat_array_klass();
414     ciInlineKlass* vk = vak->element_klass()->as_inline_klass();
415     for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
416       ciField* field = vk->nonstatic_field_at(j);
417       int off_in_vt = field->offset() - vk->first_field_offset();
418       Node* off  = kit.MakeConX(off_in_vt + i * vak->element_byte_size());
419       ciType* ft = field->type();
420       BasicType bt = type2field[ft->basic_type()];
421       assert(!field->is_flattened(), "flattened field encountered");
422       if (bt == T_INLINE_TYPE) {
423         bt = T_OBJECT;
424       }
425       const Type* rt = Type::get_const_type(ft);
426       const TypePtr* adr_type = atp_src->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
427       assert(!bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), bt, false, false, BarrierSetC2::Optimization), "GC barriers required");
428       Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
429       Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
430       Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, adr_type, rt, bt);
431       store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, adr_type, v, rt, bt);
432     }
433   } else {
434     Node* off = kit.MakeConX(type2aelembytes(copy_type) * i);
435     Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
436     Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
437     Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, atp_src, value_type, copy_type);
438     store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, atp_dest, v, value_type, copy_type);
439   }
440   kit.set_control(ctl);
441 }
442 
















443 
444 void ArrayCopyNode::array_copy_forward(GraphKit& kit,
445                                        bool can_reshape,
446                                        const TypeAryPtr* atp_src,
447                                        const TypeAryPtr* atp_dest,
448                                        Node* adr_src,
449                                        Node* base_src,
450                                        Node* adr_dest,
451                                        Node* base_dest,
452                                        BasicType copy_type,
453                                        const Type* value_type,
454                                        int count) {
455   if (!kit.stopped()) {
456     // copy forward
457     if (count > 0) {
458       for (int i = 0; i < count; i++) {
459         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);







460       }
461     } else if (can_reshape) {
462       PhaseGVN& gvn = kit.gvn();
463       assert(gvn.is_IterGVN(), "");
464       gvn.record_for_igvn(adr_src);
465       gvn.record_for_igvn(adr_dest);
466     }

467   }

468 }
469 
470 void ArrayCopyNode::array_copy_backward(GraphKit& kit,
471                                         bool can_reshape,
472                                         const TypeAryPtr* atp_src,
473                                         const TypeAryPtr* atp_dest,
474                                         Node* adr_src,
475                                         Node* base_src,
476                                         Node* adr_dest,
477                                         Node* base_dest,
478                                         BasicType copy_type,
479                                         const Type* value_type,
480                                         int count) {
481   if (!kit.stopped()) {


482     // copy backward
483     PhaseGVN& gvn = kit.gvn();



484 
485     if (count > 0) {
486       for (int i = count-1; i >= 0; i--) {
487         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);




488       }
489     } else if(can_reshape) {
490       PhaseGVN& gvn = kit.gvn();
491       assert(gvn.is_IterGVN(), "");
492       gvn.record_for_igvn(adr_src);
493       gvn.record_for_igvn(adr_dest);

494     }

495   }

496 }
497 
498 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
499                                      Node* ctl, Node *mem) {
500   if (can_reshape) {
501     PhaseIterGVN* igvn = phase->is_IterGVN();
502     igvn->set_delay_transform(false);
503     if (is_clonebasic()) {
504       Node* out_mem = proj_out(TypeFunc::Memory);
505 
506       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
507       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
508           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
509         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
510         return false;
511       }
512 
513       igvn->replace_node(out_mem->raw_out(0), mem);
514 
515       Node* out_ctl = proj_out(TypeFunc::Control);
516       igvn->replace_node(out_ctl, ctl);
517     } else {
518       // replace fallthrough projections of the ArrayCopyNode by the
519       // new memory, control and the input IO.
520       CallProjections* callprojs = extract_projections(true, false);

521 
522       if (callprojs->fallthrough_ioproj != NULL) {
523         igvn->replace_node(callprojs->fallthrough_ioproj, in(TypeFunc::I_O));
524       }
525       if (callprojs->fallthrough_memproj != NULL) {
526         igvn->replace_node(callprojs->fallthrough_memproj, mem);
527       }
528       if (callprojs->fallthrough_catchproj != NULL) {
529         igvn->replace_node(callprojs->fallthrough_catchproj, ctl);
530       }
531 
532       // The ArrayCopyNode is not disconnected. It still has the
533       // projections for the exception case. Replace current
534       // ArrayCopyNode with a dummy new one with a top() control so
535       // that this part of the graph stays consistent but is
536       // eventually removed.
537 
538       set_req(0, phase->C->top());
539       remove_dead_region(phase, can_reshape);
540     }
541   } else {
542     if (in(TypeFunc::Control) != ctl) {
543       // we can't return new memory and control from Ideal at parse time
544 #ifdef ASSERT
545       Node* src = in(ArrayCopyNode::Src);
546       const Type* src_type = phase->type(src);
547       const TypeAryPtr* ary_src = src_type->isa_aryptr();
548       BasicType elem = ary_src != NULL ? ary_src->klass()->as_array_klass()->element_type()->basic_type() : T_CONFLICT;
549       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
550       assert(!is_clonebasic() || bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization) ||
551              (ary_src != NULL && elem == T_INLINE_TYPE && ary_src->klass()->is_obj_array_klass()), "added control for clone?");
552 #endif
553       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
554       phase->record_for_igvn(this);
555       return false;
556     }
557   }
558   return true;
559 }
560 
561 
562 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
563   // Perform any generic optimizations first
564   Node* result = SafePointNode::Ideal(phase, can_reshape);
565   if (result != NULL) {
566     return result;
567   }
568 
569   if (StressArrayCopyMacroNode && !can_reshape) {
570     phase->record_for_igvn(this);
571     return NULL;
572   }
573 
574   // See if it's a small array copy and we can inline it as
575   // loads/stores
576   // Here we can only do:
577   // - arraycopy if all arguments were validated before and we don't
578   // need card marking
579   // - clone for which we don't need to do card marking
580 
581   if (!is_clonebasic() && !is_arraycopy_validated() &&
582       !is_copyofrange_validated() && !is_copyof_validated()) {
583     return NULL;
584   }
585 
586   assert(in(TypeFunc::Control) != NULL &&
587          in(TypeFunc::Memory) != NULL &&

589          in(ArrayCopyNode::Dest) != NULL &&
590          in(ArrayCopyNode::Length) != NULL &&
591          in(ArrayCopyNode::SrcPos) != NULL &&
592          in(ArrayCopyNode::DestPos) != NULL, "broken inputs");
593 
594   if (in(TypeFunc::Control)->is_top() ||
595       in(TypeFunc::Memory)->is_top() ||
596       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
597       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
598       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
599       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
600     return NULL;
601   }
602 
603   int count = get_count(phase);
604 
605   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
606     return NULL;
607   }
608 
609   Node* src = in(ArrayCopyNode::Src);
610   Node* dest = in(ArrayCopyNode::Dest);
611   const Type* src_type = phase->type(src);
612   const Type* dest_type = phase->type(dest);
613 
614   if (src_type->isa_aryptr() && dest_type->isa_instptr()) {
615     // clone used for load of unknown inline type can't be optimized at
616     // this point
617     return NULL;
618   }
619 
620   Node* mem = try_clone_instance(phase, can_reshape, count);
621   if (mem != NULL) {
622     return (mem == NodeSentinel) ? NULL : mem;
623   }
624 
625   Node* adr_src = NULL;
626   Node* base_src = NULL;
627   Node* adr_dest = NULL;
628   Node* base_dest = NULL;
629   BasicType copy_type = T_ILLEGAL;
630   const Type* value_type = NULL;
631   bool disjoint_bases = false;
632 
633   if (!prepare_array_copy(phase, can_reshape,
634                           adr_src, base_src, adr_dest, base_dest,
635                           copy_type, value_type, disjoint_bases)) {
636     return NULL;
637   }
638 
639   JVMState* new_jvms = NULL;
640   SafePointNode* new_map = NULL;
641   if (!is_clonebasic()) {
642     new_jvms = jvms()->clone_shallow(phase->C);
643     new_map = new SafePointNode(req(), new_jvms);
644     for (uint i = TypeFunc::FramePtr; i < req(); i++) {
645       new_map->init_req(i, in(i));
646     }
647     new_jvms->set_map(new_map);
648   } else {
649     new_jvms = new (phase->C) JVMState(0);
650     new_map = new SafePointNode(TypeFunc::Parms, new_jvms);
651     new_jvms->set_map(new_map);
652   }
653   new_map->set_control(in(TypeFunc::Control));
654   new_map->set_memory(MergeMemNode::make(in(TypeFunc::Memory)));
655   new_map->set_i_o(in(TypeFunc::I_O));
656   phase->record_for_igvn(new_map);
657 
658   const TypeAryPtr* atp_src = get_address_type(phase, _src_type, src);
659   const TypeAryPtr* atp_dest = get_address_type(phase, _dest_type, dest);
660 
661   if (can_reshape) {
662     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
663     phase->is_IterGVN()->set_delay_transform(true);
664   }
665 
666   GraphKit kit(new_jvms, phase);
667 
668   SafePointNode* backward_map = NULL;
669   SafePointNode* forward_map = NULL;
670   Node* backward_ctl = phase->C->top();
671 
672   array_copy_test_overlap(kit, disjoint_bases, count, backward_ctl);
673 
674   {
675     PreserveJVMState pjvms(&kit);
676 
677     array_copy_forward(kit, can_reshape,
678                        atp_src, atp_dest,
679                        adr_src, base_src, adr_dest, base_dest,
680                        copy_type, value_type, count);
681 
682     forward_map = kit.stop();
683   }
684 
685   kit.set_control(backward_ctl);
686   array_copy_backward(kit, can_reshape,
687                       atp_src, atp_dest,
688                       adr_src, base_src, adr_dest, base_dest,
689                       copy_type, value_type, count);
690 
691   backward_map = kit.stop();
692 
693   if (!forward_map->control()->is_top() && !backward_map->control()->is_top()) {
694     assert(forward_map->i_o() == backward_map->i_o(), "need a phi on IO?");
695     Node* ctl = new RegionNode(3);
696     Node* mem = new PhiNode(ctl, Type::MEMORY, TypePtr::BOTTOM);
697     kit.set_map(forward_map);
698     ctl->init_req(1, kit.control());
699     mem->init_req(1, kit.reset_memory());
700     kit.set_map(backward_map);
701     ctl->init_req(2, kit.control());
702     mem->init_req(2, kit.reset_memory());
703     kit.set_control(phase->transform(ctl));
704     kit.set_all_memory(phase->transform(mem));
705   } else if (!forward_map->control()->is_top()) {
706     kit.set_map(forward_map);
707   } else {
708     assert(!backward_map->control()->is_top(), "no copy?");
709     kit.set_map(backward_map);

710   }
711 
712   if (can_reshape) {
713     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
714     phase->is_IterGVN()->set_delay_transform(false);
715   }
716 
717   mem = kit.map()->memory();
718   if (!finish_transform(phase, can_reshape, kit.control(), mem)) {
719     if (!can_reshape) {
720       phase->record_for_igvn(this);
721     }
722     return NULL;
723   }
724 
725   return mem;
726 }
727 
728 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
729   Node* dest = in(ArrayCopyNode::Dest);
730   if (dest->is_top()) {
731     return false;
732   }
733   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
734   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
735   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
736          _src_type->is_known_instance(), "result of EA not recorded");
737 
738   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
739     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
740     return t_oop->instance_id() == _dest_type->instance_id();
741   }

794 // in the destination array
795 // if must_modify is false, return true if the copy could write
796 // between offset_lo and offset_hi
797 // if must_modify is true, return true if the copy is guaranteed to
798 // write between offset_lo and offset_hi
799 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {
800   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
801 
802   Node* dest = in(Dest);
803   Node* dest_pos = in(DestPos);
804   Node* len = in(Length);
805 
806   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
807   const TypeInt *len_t = phase->type(len)->isa_int();
808   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
809 
810   if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {
811     return !must_modify;
812   }
813 
814   ciArrayKlass* klass = ary_t->klass()->as_array_klass();
815   BasicType ary_elem = klass->element_type()->basic_type();
816   uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
817   uint elemsize = type2aelembytes(ary_elem);
818   if (klass->is_flat_array_klass()) {
819     elemsize = klass->as_flat_array_klass()->element_byte_size();
820   }
821 
822   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
823   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
824   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
825   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
826 
827   if (must_modify) {
828     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
829       return true;
830     }
831   } else {
832     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
833       return true;
834     }
835   }
836   return false;
837 }
838 
839 // As an optimization, choose optimum vector size for copy length known at compile time.
840 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {
< prev index next >