< prev index next >

src/hotspot/share/opto/arraycopynode.cpp

Print this page

  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 "gc/shared/barrierSet.hpp"
 26 #include "gc/shared/c2/barrierSetC2.hpp"
 27 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
 28 #include "gc/shared/gc_globals.hpp"
 29 #include "opto/arraycopynode.hpp"
 30 #include "opto/graphKit.hpp"

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

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







120     if (src_type->isa_instptr()) {
121       const TypeInstPtr* inst_src = src_type->is_instptr();
122       ciInstanceKlass* ik = inst_src->instance_klass();
123       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
124       // fields into account. They are rare anyway so easier to simply
125       // skip instances with injected fields.
126       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
127         return -1;
128       }
129       int nb_fields = ik->nof_nonstatic_fields();
130       return nb_fields;
131     } else {
132       const TypeAryPtr* ary_src = src_type->isa_aryptr();
133       assert (ary_src != nullptr, "not an array or instance?");
134       // clone passes a length as a rounded number of longs. If we're
135       // cloning an array we'll do it element by element. If the
136       // length of the input array is constant, ArrayCopyNode::Length
137       // must be too. Note that the opposite does not need to hold,
138       // because different input array lengths (e.g. int arrays with
139       // 3 or 4 elements) might lead to the same length input
140       // (e.g. 2 double-words).
141       assert(!ary_src->size()->is_con() || (get_length_if_constant(phase) >= 0) ||

142              phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
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   // Pin the load: if this is an array load, it's going to be dependent on a condition that's not a range check for that
155   // access. If that condition is replaced by an identical dominating one, then an unpinned load would risk floating
156   // above runtime checks that guarantee it is within bounds.
157   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY | C2_UNKNOWN_CONTROL_LOAD;
158   C2AccessValuePtr addr(adr, adr_type);
159   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
160   Node* res = bs->load_at(access, type);
161   ctl = access.ctl();

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

195   if (can_reshape) {
196     phase->is_IterGVN()->_worklist.push(mem);
197   }
198 
199 
200   ciInstanceKlass* ik = inst_src->instance_klass();
201 
202   if (!inst_src->klass_is_exact()) {
203     assert(!ik->is_interface(), "inconsistent klass hierarchy");
204     if (ik->has_subklass()) {
205       // Concurrent class loading.
206       // Fail fast and return NodeSentinel to indicate that the transform failed.
207       return NodeSentinel;
208     } else {
209       phase->C->dependencies()->assert_leaf_type(ik);
210     }
211   }
212 
213   const TypeInstPtr* dest_type = phase->type(base_dest)->is_instptr();
214   if (dest_type->instance_klass() != ik) {

274   Node* src_offset = in(ArrayCopyNode::SrcPos);
275   Node* dest_offset = in(ArrayCopyNode::DestPos);
276 
277   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
278     const Type* dest_type = phase->type(base_dest);
279     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
280 
281     // newly allocated object is guaranteed to not overlap with source object
282     disjoint_bases = is_alloc_tightly_coupled();
283     if (ary_src  == nullptr || ary_src->elem()  == Type::BOTTOM ||
284         ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
285       // We don't know if arguments are arrays
286       return false;
287     }
288 
289     BasicType src_elem = ary_src->elem()->array_element_basic_type();
290     BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
291     if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
292     if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
293 
294     if (src_elem != dest_elem || dest_elem == T_VOID) {

295       // We don't know if arguments are arrays of the same type
296       return false;
297     }
298 
299     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
300     if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {
301       // It's an object array copy but we can't emit the card marking
302       // that is needed

303       return false;
304     }
305 
306     value_type = ary_src->elem();
307 
308     uint shift  = exact_log2(type2aelembytes(dest_elem));
309     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);







310 
311     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
312     if (src_offset->is_top()) {
313       // Offset is out of bounds (the ArrayCopyNode will be removed)
314       return false;
315     }
316     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
317     if (dest_offset->is_top()) {
318       // Offset is out of bounds (the ArrayCopyNode will be removed)
319       if (can_reshape) {
320         // record src_offset, so it can be deleted later (if it is dead)
321         phase->is_IterGVN()->_worklist.push(src_offset);
322       }
323       return false;
324     }
325 
326     Node* hook = new Node(1);
327     hook->init_req(0, dest_offset);
328 
329     Node* src_scale  = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
330 
331     hook->destruct(phase);
332 
333     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
334 
335     adr_src = phase->transform(AddPNode::make_with_base(base_src, src_scale));
336     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, dest_scale));
337 
338     adr_src = phase->transform(AddPNode::make_with_base(base_src, adr_src, phase->MakeConX(header)));
339     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, adr_dest, phase->MakeConX(header)));
340 
341     copy_type = dest_elem;
342   } else {
343     assert(ary_src != nullptr, "should be a clone");
344     assert(is_clonebasic(), "should be");
345 
346     disjoint_bases = true;
347 





348     BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
349     if (is_reference_type(elem, true)) {
350       elem = T_OBJECT;
351     }
352 
353     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
354     if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {



355       return false;
356     }
357 
358     adr_src = phase->transform(AddPNode::make_with_base(base_src, src_offset));
359     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, dest_offset));
360 
361     // The address is offsetted to an aligned address where a raw copy would start.
362     // If the clone copy is decomposed into load-stores - the address is adjusted to
363     // point at where the array starts.
364     const Type* toff = phase->type(src_offset);
365     int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
366     int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
367     assert(diff >= 0, "clone should not start after 1st array element");
368     if (diff > 0) {
369       adr_src = phase->transform(AddPNode::make_with_base(base_src, adr_src, phase->MakeConX(diff)));
370       adr_dest = phase->transform(AddPNode::make_with_base(base_dest, adr_dest, phase->MakeConX(diff)));
371     }
372     copy_type = elem;
373     value_type = ary_src->elem();
374   }
375   return true;
376 }
377 
378 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
379   if (atp == TypeOopPtr::BOTTOM) {
380     atp = phase->type(n)->isa_ptr();
381   }
382   // adjust atp to be the correct array element address type
383   return atp->add_offset(Type::OffsetBot);
384 }
385 
386 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
387   Node* ctl = in(TypeFunc::Control);
388   if (!disjoint_bases && count > 1) {

389     Node* src_offset = in(ArrayCopyNode::SrcPos);
390     Node* dest_offset = in(ArrayCopyNode::DestPos);
391     assert(src_offset != nullptr && dest_offset != nullptr, "should be");
392     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
393     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
394     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
395 
396     phase->transform(iff);





397 
398     forward_ctl = phase->transform(new IfFalseNode(iff));
399     backward_ctl = phase->transform(new IfTrueNode(iff));


































400   } else {
401     forward_ctl = ctl;





































402   }
403 }
404 
405 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
406                                         bool can_reshape,
407                                         Node*& forward_ctl,
408                                         Node* mem,
409                                         const TypePtr* atp_src,
410                                         const TypePtr* atp_dest,
411                                         Node* adr_src,
412                                         Node* base_src,
413                                         Node* adr_dest,
414                                         Node* base_dest,
415                                         BasicType copy_type,
416                                         const Type* value_type,
417                                         int count) {
418   if (!forward_ctl->is_top()) {
419     // copy forward
420     MergeMemNode* mm = MergeMemNode::make(mem);
421 
422     if (count > 0) {
423       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
424       Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);
425       store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
426       for (int i = 1; i < count; i++) {
427         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
428         Node* next_src = phase->transform(AddPNode::make_with_base(base_src,adr_src,off));
429         // We may have narrowed the type of next_src right before calling this method but because this runs with
430         // PhaseIterGVN::_delay_transform true, explicitly update the type of the AddP so it's consistent  with its
431         // base and load() picks the right memory slice.
432         phase->set_type(next_src, next_src->Value(phase));
433         Node* next_dest = phase->transform(AddPNode::make_with_base(base_dest,adr_dest,off));
434         // Same as above
435         phase->set_type(next_dest, next_dest->Value(phase));
436         v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);
437         store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
438       }
439     } else if (can_reshape) {
440       PhaseIterGVN* igvn = phase->is_IterGVN();
441       igvn->_worklist.push(adr_src);
442       igvn->_worklist.push(adr_dest);
443     }
444     return mm;
445   }
446   return phase->C->top();
447 }
448 
449 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
450                                          bool can_reshape,
451                                          Node*& backward_ctl,
452                                          Node* mem,
453                                          const TypePtr* atp_src,
454                                          const TypePtr* atp_dest,
455                                          Node* adr_src,
456                                          Node* base_src,
457                                          Node* adr_dest,
458                                          Node* base_dest,
459                                          BasicType copy_type,
460                                          const Type* value_type,
461                                          int count) {
462   if (!backward_ctl->is_top()) {
463     // copy backward
464     MergeMemNode* mm = MergeMemNode::make(mem);
465 
466     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
467     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");
468 
469     if (count > 0) {
470       for (int i = count-1; i >= 1; i--) {
471         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
472         Node* next_src = phase->transform(AddPNode::make_with_base(base_src,adr_src,off));
473         // We may have narrowed the type of next_src right before calling this method but because this runs with
474         // PhaseIterGVN::_delay_transform true, explicitly update the type of the AddP so it's consistent  with its
475         // base and store() picks the right memory slice.
476         phase->set_type(next_src, next_src->Value(phase));
477         Node* next_dest = phase->transform(AddPNode::make_with_base(base_dest,adr_dest,off));
478         phase->set_type(next_dest, next_dest->Value(phase));
479         Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);
480         store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
481       }
482       Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);
483       store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
484     } else if (can_reshape) {
485       PhaseIterGVN* igvn = phase->is_IterGVN();
486       igvn->_worklist.push(adr_src);
487       igvn->_worklist.push(adr_dest);
488     }
489     return phase->transform(mm);
490   }
491   return phase->C->top();
492 }
493 
494 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
495                                      Node* ctl, Node *mem) {
496   if (can_reshape) {
497     PhaseIterGVN* igvn = phase->is_IterGVN();
498     igvn->set_delay_transform(false);
499     if (is_clonebasic()) {
500       Node* out_mem = proj_out(TypeFunc::Memory);
501 
502       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
503       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
504           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
505         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
506         return false;
507       }
508 
509       igvn->replace_node(out_mem->raw_out(0), mem);
510 
511       Node* out_ctl = proj_out(TypeFunc::Control);
512       igvn->replace_node(out_ctl, ctl);
513     } else {
514       // replace fallthrough projections of the ArrayCopyNode by the
515       // new memory, control and the input IO.
516       CallProjections callprojs;
517       extract_projections(&callprojs, true, false);
518 
519       if (callprojs.fallthrough_ioproj != nullptr) {
520         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
521       }
522       if (callprojs.fallthrough_memproj != nullptr) {
523         igvn->replace_node(callprojs.fallthrough_memproj, mem);
524       }
525       if (callprojs.fallthrough_catchproj != nullptr) {
526         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
527       }
528 
529       // The ArrayCopyNode is not disconnected. It still has the
530       // projections for the exception case. Replace current
531       // ArrayCopyNode with a dummy new one with a top() control so
532       // that this part of the graph stays consistent but is
533       // eventually removed.
534 
535       set_req(0, phase->C->top());
536       remove_dead_region(phase, can_reshape);
537     }
538   } else {
539     if (in(TypeFunc::Control) != ctl) {
540       // we can't return new memory and control from Ideal at parse time
541       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
542       phase->record_for_igvn(this);
543       return false;
544     }
545   }
546   return true;
547 }
548 
549 
550 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
551   if (remove_dead_region(phase, can_reshape))  return this;




552 
553   if (StressArrayCopyMacroNode && !can_reshape) {
554     phase->record_for_igvn(this);
555     return nullptr;
556   }
557 
558   // See if it's a small array copy and we can inline it as
559   // loads/stores
560   // Here we can only do:
561   // - arraycopy if all arguments were validated before and we don't
562   // need card marking
563   // - clone for which we don't need to do card marking
564 
565   if (!is_clonebasic() && !is_arraycopy_validated() &&
566       !is_copyofrange_validated() && !is_copyof_validated()) {
567     return nullptr;
568   }
569 
570   assert(in(TypeFunc::Control) != nullptr &&
571          in(TypeFunc::Memory) != nullptr &&

573          in(ArrayCopyNode::Dest) != nullptr &&
574          in(ArrayCopyNode::Length) != nullptr &&
575          in(ArrayCopyNode::SrcPos) != nullptr &&
576          in(ArrayCopyNode::DestPos) != nullptr, "broken inputs");
577 
578   if (in(TypeFunc::Control)->is_top() ||
579       in(TypeFunc::Memory)->is_top() ||
580       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
581       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
582       (in(ArrayCopyNode::SrcPos) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
583       (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
584     return nullptr;
585   }
586 
587   int count = get_count(phase);
588 
589   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
590     return nullptr;
591   }
592 











593   Node* mem = try_clone_instance(phase, can_reshape, count);
594   if (mem != nullptr) {
595     return (mem == NodeSentinel) ? nullptr : mem;
596   }
597 
598   Node* adr_src = nullptr;
599   Node* base_src = nullptr;
600   Node* adr_dest = nullptr;
601   Node* base_dest = nullptr;
602   BasicType copy_type = T_ILLEGAL;
603   const Type* value_type = nullptr;
604   bool disjoint_bases = false;
605 
606   Node* src = in(ArrayCopyNode::Src);
607   Node* dest = in(ArrayCopyNode::Dest);
608   // EA may have moved an input to a new slice. EA stores the new address types in the ArrayCopy node itself
609   // (_src_type/_dest_type). phase->type(src) and _src_type or phase->type(dest) and _dest_type may be different
610   // when this transformation runs if igvn hasn't had a chance to propagate the new types yet. Make sure the new
611   // types are taken into account so new Load/Store nodes are created on the right slice.
612   const TypePtr* atp_src = get_address_type(phase, _src_type, src);
613   const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);
614   phase->set_type(src, phase->type(src)->join_speculative(atp_src));
615   phase->set_type(dest, phase->type(dest)->join_speculative(atp_dest));
616 
617   if (!prepare_array_copy(phase, can_reshape,
618                           adr_src, base_src, adr_dest, base_dest,
619                           copy_type, value_type, disjoint_bases)) {
620     assert(adr_src == nullptr, "no node can be left behind");
621     assert(adr_dest == nullptr, "no node can be left behind");
622     return nullptr;
623   }
624 
625   Node* in_mem = in(TypeFunc::Memory);

















626 
627   if (can_reshape) {
628     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
629     phase->is_IterGVN()->set_delay_transform(true);
630   }
631 




632   Node* backward_ctl = phase->C->top();
633   Node* forward_ctl = phase->C->top();
634   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
635 
636   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
637                                          in_mem,
638                                          atp_src, atp_dest,
639                                          adr_src, base_src, adr_dest, base_dest,
640                                          copy_type, value_type, count);
641 
642   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
643                                            in_mem,
644                                            atp_src, atp_dest,
645                                            adr_src, base_src, adr_dest, base_dest,
646                                            copy_type, value_type, count);
647 
648   Node* ctl = nullptr;
649   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
650     ctl = new RegionNode(3);
651     ctl->init_req(1, forward_ctl);
652     ctl->init_req(2, backward_ctl);
653     ctl = phase->transform(ctl);
654     MergeMemNode* forward_mm = forward_mem->as_MergeMem();
655     MergeMemNode* backward_mm = backward_mem->as_MergeMem();
656     for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
657       if (mms.memory() != mms.memory2()) {
658         Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
659         phi->init_req(1, mms.memory());
660         phi->init_req(2, mms.memory2());
661         phi = phase->transform(phi);
662         mms.set_memory(phi);
663       }
664     }
665     mem = forward_mem;
666   } else if (!forward_ctl->is_top()) {
667     ctl = forward_ctl;
668     mem = forward_mem;
669   } else {
670     assert(!backward_ctl->is_top(), "no copy?");
671     ctl = backward_ctl;
672     mem = backward_mem;
673   }
674 
675   if (can_reshape) {
676     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
677     phase->is_IterGVN()->set_delay_transform(false);
678   }
679 
680   if (!finish_transform(phase, can_reshape, ctl, mem)) {
681     if (can_reshape) {



682       // put in worklist, so that if it happens to be dead it is removed
683       phase->is_IterGVN()->_worklist.push(mem);
684     }
685     return nullptr;
686   }
687 
688   return mem;
689 }
690 
691 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) {
692   Node* dest = in(ArrayCopyNode::Dest);
693   if (dest->is_top()) {
694     return false;
695   }
696   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
697   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
698   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
699          _src_type->is_known_instance(), "result of EA not recorded");
700 
701   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {

758 // if must_modify is true, return true if the copy is guaranteed to
759 // write between offset_lo and offset_hi
760 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
761   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
762 
763   Node* dest = in(Dest);
764   Node* dest_pos = in(DestPos);
765   Node* len = in(Length);
766 
767   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
768   const TypeInt *len_t = phase->type(len)->isa_int();
769   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
770 
771   if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
772     return !must_modify;
773   }
774 
775   BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
776   if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
777 
778   uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
779   uint elemsize = type2aelembytes(ary_elem);







780 
781   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
782   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
783   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
784   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
785 
786   if (must_modify) {
787     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
788       return true;
789     }
790   } else {
791     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
792       return true;
793     }
794   }
795   return false;
796 }
797 
798 // As an optimization, choose the optimal vector size for bounded copy length
799 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, jlong max_len) {
800   assert(max_len > 0, JLONG_FORMAT, max_len);
801   // We only care whether max_size_in_bytes is not larger than 32, we also want to avoid
802   // multiplication overflow, so clamp max_len to [0, 64]
803   int max_size_in_bytes = MIN2<jlong>(max_len, 64) * type2aelembytes(type);
804   if (ArrayOperationPartialInlineSize > 16 && max_size_in_bytes <= 16) {

  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 "ci/ciFlatArrayKlass.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 "opto/inlinetypenode.hpp"
 33 #include "utilities/powerOfTwo.hpp"
 34 
 35 const TypeFunc* ArrayCopyNode::_arraycopy_type_Type = nullptr;
 36 
 37 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
 38   : CallNode(arraycopy_type(), nullptr, TypePtr::BOTTOM),
 39     _kind(None),
 40     _alloc_tightly_coupled(alloc_tightly_coupled),
 41     _has_negative_length_guard(has_negative_length_guard),
 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,

 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   if (is_clonebasic()) {
119     Node* src = in(ArrayCopyNode::Src);
120     const Type* src_type = phase->type(src);
121 
122     if (src_type == Type::TOP) {
123       return -1;
124     }
125 
126     if (src_type->isa_instptr()) {
127       const TypeInstPtr* inst_src = src_type->is_instptr();
128       ciInstanceKlass* ik = inst_src->instance_klass();
129       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
130       // fields into account. They are rare anyway so easier to simply
131       // skip instances with injected fields.
132       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
133         return -1;
134       }
135       int nb_fields = ik->nof_nonstatic_fields();
136       return nb_fields;
137     } else {
138       const TypeAryPtr* ary_src = src_type->isa_aryptr();
139       assert (ary_src != nullptr, "not an array or instance?");
140       // clone passes a length as a rounded number of longs. If we're
141       // cloning an array we'll do it element by element. If the
142       // length of the input array is constant, ArrayCopyNode::Length
143       // must be too. Note that the opposite does not need to hold,
144       // because different input array lengths (e.g. int arrays with
145       // 3 or 4 elements) might lead to the same length input
146       // (e.g. 2 double-words).
147       assert(!ary_src->size()->is_con() || (get_length_if_constant(phase) >= 0) ||
148              (UseArrayFlattening && ary_src->elem()->make_oopptr() != nullptr && ary_src->elem()->make_oopptr()->can_be_inline_type()) ||
149              phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
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   // Pin the load: if this is an array load, it's going to be dependent on a condition that's not a range check for that
162   // access. If that condition is replaced by an identical dominating one, then an unpinned load would risk floating
163   // above runtime checks that guarantee it is within bounds.
164   DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY | C2_UNKNOWN_CONTROL_LOAD;
165   C2AccessValuePtr addr(adr, adr_type);
166   C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
167   Node* res = bs->load_at(access, type);
168   ctl = access.ctl();

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

282   Node* src_offset = in(ArrayCopyNode::SrcPos);
283   Node* dest_offset = in(ArrayCopyNode::DestPos);
284 
285   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
286     const Type* dest_type = phase->type(base_dest);
287     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
288 
289     // newly allocated object is guaranteed to not overlap with source object
290     disjoint_bases = is_alloc_tightly_coupled();
291     if (ary_src  == nullptr || ary_src->elem()  == Type::BOTTOM ||
292         ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
293       // We don't know if arguments are arrays
294       return false;
295     }
296 
297     BasicType src_elem = ary_src->elem()->array_element_basic_type();
298     BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
299     if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
300     if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
301 
302     // TODO 8350865 What about atomicity?
303     if (src_elem != dest_elem || ary_src->is_null_free() != ary_dest->is_null_free() || ary_src->is_flat() != ary_dest->is_flat() || dest_elem == T_VOID) {
304       // We don't know if arguments are arrays of the same type
305       return false;
306     }
307 
308     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
309     if ((!ary_dest->is_flat() && bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) ||
310         (ary_dest->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
311          bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), T_OBJECT, false, false, BarrierSetC2::Optimization))) {
312       // It's an object array copy but we can't emit the card marking that is needed
313       return false;
314     }
315 
316     value_type = ary_src->elem();
317 
318     uint shift  = exact_log2(type2aelembytes(dest_elem));
319     if (ary_dest->is_flat()) {
320       assert(ary_src->is_flat(), "src and dest must be flat");
321       shift = ary_src->flat_log_elem_size();
322       src_elem = T_FLAT_ELEMENT;
323       dest_elem = T_FLAT_ELEMENT;
324     }
325 
326     const uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
327 
328     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
329     if (src_offset->is_top()) {
330       // Offset is out of bounds (the ArrayCopyNode will be removed)
331       return false;
332     }
333     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
334     if (dest_offset->is_top()) {
335       // Offset is out of bounds (the ArrayCopyNode will be removed)
336       if (can_reshape) {
337         // record src_offset, so it can be deleted later (if it is dead)
338         phase->is_IterGVN()->_worklist.push(src_offset);
339       }
340       return false;
341     }
342 
343     Node* hook = new Node(1);
344     hook->init_req(0, dest_offset);
345 
346     Node* src_scale  = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
347 
348     hook->destruct(phase);
349 
350     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
351 
352     adr_src = phase->transform(AddPNode::make_with_base(base_src, src_scale));
353     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, dest_scale));
354 
355     adr_src = phase->transform(AddPNode::make_with_base(base_src, adr_src, phase->MakeConX(header)));
356     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, adr_dest, phase->MakeConX(header)));
357 
358     copy_type = dest_elem;
359   } else {
360     assert(ary_src != nullptr, "should be a clone");
361     assert(is_clonebasic(), "should be");
362 
363     disjoint_bases = true;
364 
365     if (ary_src->elem()->make_oopptr() != nullptr &&
366         ary_src->elem()->make_oopptr()->can_be_inline_type()) {
367       return false;
368     }
369 
370     BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
371     if (is_reference_type(elem, true)) {
372       elem = T_OBJECT;
373     }
374 
375     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
376     if ((!ary_src->is_flat() && bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) ||
377         (ary_src->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
378          bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization))) {
379       // It's an object array copy but we can't emit the card marking that is needed
380       return false;
381     }
382 
383     adr_src = phase->transform(AddPNode::make_with_base(base_src, src_offset));
384     adr_dest = phase->transform(AddPNode::make_with_base(base_dest, dest_offset));
385 
386     // The address is offsetted to an aligned address where a raw copy would start.
387     // If the clone copy is decomposed into load-stores - the address is adjusted to
388     // point at where the array starts.
389     const Type* toff = phase->type(src_offset);
390     int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
391     int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
392     assert(diff >= 0, "clone should not start after 1st array element");
393     if (diff > 0) {
394       adr_src = phase->transform(AddPNode::make_with_base(base_src, adr_src, phase->MakeConX(diff)));
395       adr_dest = phase->transform(AddPNode::make_with_base(base_dest, adr_dest, phase->MakeConX(diff)));
396     }
397     copy_type = elem;
398     value_type = ary_src->elem();
399   }
400   return true;
401 }
402 
403 const TypeAryPtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
404   if (atp == TypeOopPtr::BOTTOM) {
405     atp = phase->type(n)->isa_ptr();
406   }
407   // adjust atp to be the correct array element address type
408   return atp->add_offset(Type::OffsetBot)->is_aryptr();
409 }
410 
411 void ArrayCopyNode::array_copy_test_overlap(GraphKit& kit, bool disjoint_bases, int count, Node*& backward_ctl) {
412   Node* ctl = kit.control();
413   if (!disjoint_bases && count > 1) {
414     PhaseGVN& gvn = kit.gvn();
415     Node* src_offset = in(ArrayCopyNode::SrcPos);
416     Node* dest_offset = in(ArrayCopyNode::DestPos);
417     assert(src_offset != nullptr && dest_offset != nullptr, "should be");
418     Node* cmp = gvn.transform(new CmpINode(src_offset, dest_offset));
419     Node *bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
420     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
421 
422     gvn.transform(iff);
423 
424     kit.set_control(gvn.transform(new IfFalseNode(iff)));
425     backward_ctl = gvn.transform(new IfTrueNode(iff));
426   }
427 }
428 
429 void ArrayCopyNode::copy(GraphKit& kit,
430                          const TypeAryPtr* atp_src,
431                          const TypeAryPtr* atp_dest,
432                          int i,
433                          Node* base_src,
434                          Node* base_dest,
435                          Node* adr_src,
436                          Node* adr_dest,
437                          BasicType copy_type,
438                          const Type* value_type) {
439   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
440   Node* ctl = kit.control();
441   PhaseGVN& phase = kit.gvn();
442   if (atp_dest->is_flat()) {
443     ciInlineKlass* vk = atp_src->elem()->inline_klass();
444     for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
445       ciField* field = vk->nonstatic_field_at(j);
446       int off_in_vt = field->offset_in_bytes() - vk->payload_offset();
447       Node* off  = kit.MakeConX(off_in_vt + i * atp_src->flat_elem_size());
448       ciType* ft = field->type();
449       BasicType bt = type2field[ft->basic_type()];
450       assert(!field->is_flat(), "flat field encountered");
451       const Type* rt = Type::get_const_type(ft);
452       const TypePtr* adr_type = atp_src->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
453       assert(!bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), bt, false, false, BarrierSetC2::Optimization), "GC barriers required");
454       Node* next_src = kit.gvn().transform(AddPNode::make_with_base(base_src, adr_src, off));
455       // We may have narrowed the type of next_src right before calling this method but because this runs with
456       // PhaseIterGVN::_delay_transform true, explicitly update the type of the AddP so it's consistent  with its
457       // base and load() picks the right memory slice.
458       phase.set_type(next_src, next_src->Value(&phase));
459       Node* next_dest = kit.gvn().transform(AddPNode::make_with_base(base_dest, adr_dest, off));
460       // Same as above
461       phase.set_type(next_src, next_src->Value(&phase));
462       Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, adr_type, rt, bt);
463       store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, adr_type, v, rt, bt);
464     }
465   } else {
466     Node* off = kit.MakeConX(type2aelembytes(copy_type) * i);
467     Node* next_src = kit.gvn().transform(AddPNode::make_with_base(base_src, adr_src, off));
468     // We may have narrowed the type of next_src right before calling this method but because this runs with
469     // PhaseIterGVN::_delay_transform true, explicitly update the type of the AddP so it's consistent  with its
470     // base and load() picks the right memory slice.
471     phase.set_type(next_src, next_src->Value(&phase));
472     Node* next_dest = kit.gvn().transform(AddPNode::make_with_base(base_dest, adr_dest, off));
473     phase.set_type(next_dest, next_dest->Value(&phase));
474     Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, atp_src, value_type, copy_type);
475     store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, atp_dest, v, value_type, copy_type);
476   }
477   kit.set_control(ctl);
478 }
479 
480 
481 void ArrayCopyNode::array_copy_forward(GraphKit& kit,
482                                        bool can_reshape,
483                                        const TypeAryPtr* atp_src,
484                                        const TypeAryPtr* atp_dest,
485                                        Node* adr_src,
486                                        Node* base_src,
487                                        Node* adr_dest,
488                                        Node* base_dest,
489                                        BasicType copy_type,
490                                        const Type* value_type,
491                                        int count) {
492   if (!kit.stopped()) {
493     // copy forward
494     if (count > 0) {
495       for (int i = 0; i < count; i++) {
496         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);
497       }
498     } else if (can_reshape) {
499       PhaseGVN& gvn = kit.gvn();
500       assert(gvn.is_IterGVN(), "");
501       gvn.record_for_igvn(adr_src);
502       gvn.record_for_igvn(adr_dest);
503     }
504   }
505 }
506 
507 void ArrayCopyNode::array_copy_backward(GraphKit& kit,
508                                         bool can_reshape,
509                                         const TypeAryPtr* atp_src,
510                                         const TypeAryPtr* atp_dest,


511                                         Node* adr_src,
512                                         Node* base_src,
513                                         Node* adr_dest,
514                                         Node* base_dest,
515                                         BasicType copy_type,
516                                         const Type* value_type,
517                                         int count) {
518   if (!kit.stopped()) {












































519     // copy backward
520     PhaseGVN& gvn = kit.gvn();



521 
522     if (count > 0) {
523       for (int i = count-1; i >= 0; i--) {
524         copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);









525       }
526     } else if(can_reshape) {
527       PhaseGVN& gvn = kit.gvn();
528       assert(gvn.is_IterGVN(), "");
529       gvn.record_for_igvn(adr_src);
530       gvn.record_for_igvn(adr_dest);

531     }

532   }

533 }
534 
535 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
536                                      Node* ctl, Node *mem) {
537   if (can_reshape) {
538     PhaseIterGVN* igvn = phase->is_IterGVN();
539     igvn->set_delay_transform(false);
540     if (is_clonebasic()) {
541       Node* out_mem = proj_out(TypeFunc::Memory);
542 
543       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
544       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
545           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
546         assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
547         return false;
548       }
549 
550       igvn->replace_node(out_mem->raw_out(0), mem);
551 
552       Node* out_ctl = proj_out(TypeFunc::Control);
553       igvn->replace_node(out_ctl, ctl);
554     } else {
555       // replace fallthrough projections of the ArrayCopyNode by the
556       // new memory, control and the input IO.
557       CallProjections* callprojs = extract_projections(true, false);

558 
559       if (callprojs->fallthrough_ioproj != nullptr) {
560         igvn->replace_node(callprojs->fallthrough_ioproj, in(TypeFunc::I_O));
561       }
562       if (callprojs->fallthrough_memproj != nullptr) {
563         igvn->replace_node(callprojs->fallthrough_memproj, mem);
564       }
565       if (callprojs->fallthrough_catchproj != nullptr) {
566         igvn->replace_node(callprojs->fallthrough_catchproj, ctl);
567       }
568 
569       // The ArrayCopyNode is not disconnected. It still has the
570       // projections for the exception case. Replace current
571       // ArrayCopyNode with a dummy new one with a top() control so
572       // that this part of the graph stays consistent but is
573       // eventually removed.
574 
575       set_req(0, phase->C->top());
576       remove_dead_region(phase, can_reshape);
577     }
578   } else {
579     if (in(TypeFunc::Control) != ctl) {
580       // we can't return new memory and control from Ideal at parse time
581       assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
582       phase->record_for_igvn(this);
583       return false;
584     }
585   }
586   return true;
587 }
588 
589 
590 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
591   // Perform any generic optimizations first
592   Node* result = SafePointNode::Ideal(phase, can_reshape);
593   if (result != nullptr) {
594     return result;
595   }
596 
597   if (StressArrayCopyMacroNode && !can_reshape) {
598     phase->record_for_igvn(this);
599     return nullptr;
600   }
601 
602   // See if it's a small array copy and we can inline it as
603   // loads/stores
604   // Here we can only do:
605   // - arraycopy if all arguments were validated before and we don't
606   // need card marking
607   // - clone for which we don't need to do card marking
608 
609   if (!is_clonebasic() && !is_arraycopy_validated() &&
610       !is_copyofrange_validated() && !is_copyof_validated()) {
611     return nullptr;
612   }
613 
614   assert(in(TypeFunc::Control) != nullptr &&
615          in(TypeFunc::Memory) != nullptr &&

617          in(ArrayCopyNode::Dest) != nullptr &&
618          in(ArrayCopyNode::Length) != nullptr &&
619          in(ArrayCopyNode::SrcPos) != nullptr &&
620          in(ArrayCopyNode::DestPos) != nullptr, "broken inputs");
621 
622   if (in(TypeFunc::Control)->is_top() ||
623       in(TypeFunc::Memory)->is_top() ||
624       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
625       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
626       (in(ArrayCopyNode::SrcPos) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
627       (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
628     return nullptr;
629   }
630 
631   int count = get_count(phase);
632 
633   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
634     return nullptr;
635   }
636 
637   Node* src = in(ArrayCopyNode::Src);
638   Node* dest = in(ArrayCopyNode::Dest);
639   const Type* src_type = phase->type(src);
640   const Type* dest_type = phase->type(dest);
641 
642   if (src_type->isa_aryptr() && dest_type->isa_instptr()) {
643     // clone used for load of unknown inline type can't be optimized at
644     // this point
645     return nullptr;
646   }
647 
648   Node* mem = try_clone_instance(phase, can_reshape, count);
649   if (mem != nullptr) {
650     return (mem == NodeSentinel) ? nullptr : mem;
651   }
652 
653   Node* adr_src = nullptr;
654   Node* base_src = nullptr;
655   Node* adr_dest = nullptr;
656   Node* base_dest = nullptr;
657   BasicType copy_type = T_ILLEGAL;
658   const Type* value_type = nullptr;
659   bool disjoint_bases = false;
660 


661   // EA may have moved an input to a new slice. EA stores the new address types in the ArrayCopy node itself
662   // (_src_type/_dest_type). phase->type(src) and _src_type or phase->type(dest) and _dest_type may be different
663   // when this transformation runs if igvn hasn't had a chance to propagate the new types yet. Make sure the new
664   // types are taken into account so new Load/Store nodes are created on the right slice.
665   const TypeAryPtr* atp_src = get_address_type(phase, _src_type, src);
666   const TypeAryPtr* atp_dest = get_address_type(phase, _dest_type, dest);
667   phase->set_type(src, phase->type(src)->join_speculative(atp_src));
668   phase->set_type(dest, phase->type(dest)->join_speculative(atp_dest));
669 
670   if (!prepare_array_copy(phase, can_reshape,
671                           adr_src, base_src, adr_dest, base_dest,
672                           copy_type, value_type, disjoint_bases)) {
673     assert(adr_src == nullptr, "no node can be left behind");
674     assert(adr_dest == nullptr, "no node can be left behind");
675     return nullptr;
676   }
677 
678   JVMState* new_jvms = nullptr;
679   SafePointNode* new_map = nullptr;
680   if (!is_clonebasic()) {
681     new_jvms = jvms()->clone_shallow(phase->C);
682     new_map = new SafePointNode(req(), new_jvms);
683     for (uint i = TypeFunc::FramePtr; i < req(); i++) {
684       new_map->init_req(i, in(i));
685     }
686     new_jvms->set_map(new_map);
687   } else {
688     new_jvms = new (phase->C) JVMState(0);
689     new_map = new SafePointNode(TypeFunc::Parms, new_jvms);
690     new_jvms->set_map(new_map);
691   }
692   new_map->set_control(in(TypeFunc::Control));
693   new_map->set_memory(MergeMemNode::make(in(TypeFunc::Memory)));
694   new_map->set_i_o(in(TypeFunc::I_O));
695   phase->record_for_igvn(new_map);
696 
697   if (can_reshape) {
698     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
699     phase->is_IterGVN()->set_delay_transform(true);
700   }
701 
702   GraphKit kit(new_jvms, phase);
703 
704   SafePointNode* backward_map = nullptr;
705   SafePointNode* forward_map = nullptr;
706   Node* backward_ctl = phase->C->top();
707 
708   array_copy_test_overlap(kit, disjoint_bases, count, backward_ctl);
709 
710   {
711     PreserveJVMState pjvms(&kit);
712 
713     array_copy_forward(kit, can_reshape,
714                        atp_src, atp_dest,
715                        adr_src, base_src, adr_dest, base_dest,
716                        copy_type, value_type, count);
717 
718     forward_map = kit.stop();
719   }
720 
721   kit.set_control(backward_ctl);
722   array_copy_backward(kit, can_reshape,
723                       atp_src, atp_dest,
724                       adr_src, base_src, adr_dest, base_dest,
725                       copy_type, value_type, count);
726 
727   backward_map = kit.stop();
728 
729   if (!forward_map->control()->is_top() && !backward_map->control()->is_top()) {
730     assert(forward_map->i_o() == backward_map->i_o(), "need a phi on IO?");
731     Node* ctl = new RegionNode(3);
732     Node* mem = new PhiNode(ctl, Type::MEMORY, TypePtr::BOTTOM);
733     kit.set_map(forward_map);
734     ctl->init_req(1, kit.control());
735     mem->init_req(1, kit.reset_memory());
736     kit.set_map(backward_map);
737     ctl->init_req(2, kit.control());
738     mem->init_req(2, kit.reset_memory());
739     kit.set_control(phase->transform(ctl));
740     kit.set_all_memory(phase->transform(mem));
741   } else if (!forward_map->control()->is_top()) {
742     kit.set_map(forward_map);
743   } else {
744     assert(!backward_map->control()->is_top(), "no copy?");
745     kit.set_map(backward_map);

746   }
747 
748   if (can_reshape) {
749     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
750     phase->is_IterGVN()->set_delay_transform(false);
751   }
752 
753   mem = kit.map()->memory();
754   if (!finish_transform(phase, can_reshape, kit.control(), mem)) {
755     if (!can_reshape) {
756       phase->record_for_igvn(this);
757     } else {
758       // put in worklist, so that if it happens to be dead it is removed
759       phase->is_IterGVN()->_worklist.push(mem);
760     }
761     return nullptr;
762   }
763 
764   return mem;
765 }
766 
767 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) {
768   Node* dest = in(ArrayCopyNode::Dest);
769   if (dest->is_top()) {
770     return false;
771   }
772   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
773   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
774   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
775          _src_type->is_known_instance(), "result of EA not recorded");
776 
777   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {

834 // if must_modify is true, return true if the copy is guaranteed to
835 // write between offset_lo and offset_hi
836 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
837   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
838 
839   Node* dest = in(Dest);
840   Node* dest_pos = in(DestPos);
841   Node* len = in(Length);
842 
843   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
844   const TypeInt *len_t = phase->type(len)->isa_int();
845   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
846 
847   if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
848     return !must_modify;
849   }
850 
851   BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
852   if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
853 
854   uint header;
855   uint elem_size;
856   if (ary_t->is_flat()) {
857     header = arrayOopDesc::base_offset_in_bytes(T_FLAT_ELEMENT);
858     elem_size = ary_t->flat_elem_size();
859   } else {
860     header = arrayOopDesc::base_offset_in_bytes(ary_elem);
861     elem_size = type2aelembytes(ary_elem);
862   }
863 
864   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elem_size + header;
865   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elem_size + header;
866   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elem_size + header;
867   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elem_size + header;
868 
869   if (must_modify) {
870     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
871       return true;
872     }
873   } else {
874     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
875       return true;
876     }
877   }
878   return false;
879 }
880 
881 // As an optimization, choose the optimal vector size for bounded copy length
882 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, jlong max_len) {
883   assert(max_len > 0, JLONG_FORMAT, max_len);
884   // We only care whether max_size_in_bytes is not larger than 32, we also want to avoid
885   // multiplication overflow, so clamp max_len to [0, 64]
886   int max_size_in_bytes = MIN2<jlong>(max_len, 64) * type2aelembytes(type);
887   if (ArrayOperationPartialInlineSize > 16 && max_size_in_bytes <= 16) {
< prev index next >