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();
181 }
182
183 Node* out_mem = proj_out_or_null(TypeFunc::Memory);
184 if (can_reshape && out_mem == nullptr) { // dead node?
185 return NodeSentinel;
186 }
187
188
189 Node* base_src = in(ArrayCopyNode::Src);
190 Node* base_dest = in(ArrayCopyNode::Dest);
191 Node* ctl = in(TypeFunc::Control);
192 Node* in_mem = in(TypeFunc::Memory);
193
194 const Type* src_type = phase->type(base_src);
195 const TypeInstPtr* inst_src = src_type->isa_instptr();
196 if (inst_src == nullptr) {
197 return nullptr;
198 }
199
200 MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
201 if (can_reshape) {
202 phase->is_IterGVN()->_worklist.push(mem);
203 }
204
205
206 ciInstanceKlass* ik = inst_src->instance_klass();
207
208 if (!inst_src->klass_is_exact()) {
209 assert(!ik->is_interface(), "inconsistent klass hierarchy");
210 if (ik->has_subklass()) {
211 // Concurrent class loading.
212 // Fail fast and return NodeSentinel to indicate that the transform failed.
213 return NodeSentinel;
214 } else {
215 phase->C->dependencies()->assert_leaf_type(ik);
216 }
217 }
218
219 const TypeInstPtr* dest_type = phase->type(base_dest)->is_instptr();
220 if (dest_type->instance_klass() != ik) {
293 Node* src_offset = in(ArrayCopyNode::SrcPos);
294 Node* dest_offset = in(ArrayCopyNode::DestPos);
295
296 if (is_arraycopy() || is_copyofrange() || is_copyof()) {
297 const Type* dest_type = phase->type(base_dest);
298 const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
299
300 // newly allocated object is guaranteed to not overlap with source object
301 disjoint_bases = is_alloc_tightly_coupled();
302 if (ary_src == nullptr || ary_src->elem() == Type::BOTTOM ||
303 ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
304 // We don't know if arguments are arrays
305 return false;
306 }
307
308 BasicType src_elem = ary_src->elem()->array_element_basic_type();
309 BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
310 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
311 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
312
313 if (src_elem != dest_elem || dest_elem == T_VOID) {
314 // We don't know if arguments are arrays of the same type
315 return false;
316 }
317
318 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
319 if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {
320 // It's an object array copy but we can't emit the card marking
321 // that is needed
322 return false;
323 }
324
325 value_type = ary_src->elem();
326
327 uint shift = exact_log2(type2aelembytes(dest_elem));
328 uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
329
330 src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
331 if (src_offset->is_top()) {
332 // Offset is out of bounds (the ArrayCopyNode will be removed)
333 return false;
334 }
335 dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
336 if (dest_offset->is_top()) {
337 // Offset is out of bounds (the ArrayCopyNode will be removed)
338 if (can_reshape) {
339 // record src_offset, so it can be deleted later (if it is dead)
340 phase->is_IterGVN()->_worklist.push(src_offset);
341 }
342 return false;
343 }
344
345 Node* hook = new Node(1);
346 hook->init_req(0, dest_offset);
347
348 Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
349
350 hook->destruct(phase);
351
352 Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
353
354 adr_src = make_and_transform_addp(phase, base_src, src_scale);
355 adr_dest = make_and_transform_addp(phase, base_dest, dest_scale);
356
357 adr_src = make_and_transform_addp(phase, base_src, adr_src, phase->MakeConX(header));
358 adr_dest = make_and_transform_addp(phase, base_dest, adr_dest, phase->MakeConX(header));
359 copy_type = dest_elem;
360 } else {
361 assert(ary_src != nullptr, "should be a clone");
362 assert(is_clonebasic(), "should be");
363
364 disjoint_bases = true;
365
366 BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
367 if (is_reference_type(elem, true)) {
368 elem = T_OBJECT;
369 }
370
371 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
372 if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {
373 return false;
374 }
375
376 adr_src = make_and_transform_addp(phase, base_src, src_offset);
377 adr_dest = make_and_transform_addp(phase, base_dest, dest_offset);
378
379 // The address is offsetted to an aligned address where a raw copy would start.
380 // If the clone copy is decomposed into load-stores - the address is adjusted to
381 // point at where the array starts.
382 const Type* toff = phase->type(src_offset);
383 int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
384 int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
385 assert(diff >= 0, "clone should not start after 1st array element");
386 if (diff > 0) {
387 adr_src = make_and_transform_addp(phase, base_src, adr_src, phase->MakeConX(diff));
388 adr_dest = make_and_transform_addp(phase, base_dest, adr_dest, phase->MakeConX(diff));
389 }
390 copy_type = elem;
391 value_type = ary_src->elem();
392 }
393 return true;
394 }
395
396 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
397 if (atp == TypeOopPtr::BOTTOM) {
398 atp = phase->type(n)->isa_ptr();
399 }
400 // adjust atp to be the correct array element address type
401 return atp->add_offset(Type::OffsetBot);
402 }
403
404 const TypePtr* ArrayCopyNode::get_src_adr_type(PhaseGVN* phase) const {
405 return get_address_type(phase, _src_type, in(Src));
406 }
407
408 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
409 Node* ctl = in(TypeFunc::Control);
410 if (!disjoint_bases && count > 1) {
411 Node* src_offset = in(ArrayCopyNode::SrcPos);
412 Node* dest_offset = in(ArrayCopyNode::DestPos);
413 assert(src_offset != nullptr && dest_offset != nullptr, "should be");
414 Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
415 Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
416 IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
417
418 phase->transform(iff);
419
420 forward_ctl = phase->transform(new IfFalseNode(iff));
421 backward_ctl = phase->transform(new IfTrueNode(iff));
422 } else {
423 forward_ctl = ctl;
424 }
425 }
426
427 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
428 bool can_reshape,
429 Node*& forward_ctl,
430 Node* mem,
431 const TypePtr* atp_src,
432 const TypePtr* atp_dest,
433 Node* adr_src,
434 Node* base_src,
435 Node* adr_dest,
436 Node* base_dest,
437 BasicType copy_type,
438 const Type* value_type,
439 int count) {
440 if (!forward_ctl->is_top()) {
441 // copy forward
442 MergeMemNode* mm = MergeMemNode::make(mem);
443
444 if (count > 0) {
445 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
446 Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);
447 store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
448 for (int i = 1; i < count; i++) {
449 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);
450 Node* next_src = make_and_transform_addp(phase, base_src,adr_src,off);
451 Node* next_dest = make_and_transform_addp(phase, base_dest,adr_dest,off);
452 // Same as above
453 phase->set_type(next_dest, next_dest->Value(phase));
454 v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);
455 store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
456 }
457 } else if (can_reshape) {
458 PhaseIterGVN* igvn = phase->is_IterGVN();
459 igvn->_worklist.push(adr_src);
460 igvn->_worklist.push(adr_dest);
461 }
462 return mm;
463 }
464 return phase->C->top();
465 }
466
467 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
468 bool can_reshape,
469 Node*& backward_ctl,
470 Node* mem,
471 const TypePtr* atp_src,
472 const TypePtr* atp_dest,
473 Node* adr_src,
474 Node* base_src,
475 Node* adr_dest,
476 Node* base_dest,
477 BasicType copy_type,
478 const Type* value_type,
479 int count) {
480 if (!backward_ctl->is_top()) {
481 // copy backward
482 MergeMemNode* mm = MergeMemNode::make(mem);
483
484 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
485 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");
486
487 if (count > 0) {
488 for (int i = count-1; i >= 1; i--) {
489 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);
490 Node* next_src = make_and_transform_addp(phase, base_src,adr_src,off);
491 Node* next_dest = make_and_transform_addp(phase, base_dest,adr_dest,off);
492 Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);
493 store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
494 }
495 Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);
496 store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
497 } else if (can_reshape) {
498 PhaseIterGVN* igvn = phase->is_IterGVN();
499 igvn->_worklist.push(adr_src);
500 igvn->_worklist.push(adr_dest);
501 }
502 return phase->transform(mm);
503 }
504 return phase->C->top();
505 }
506
507 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
508 Node* ctl, Node *mem) {
509 if (can_reshape) {
510 PhaseIterGVN* igvn = phase->is_IterGVN();
511 igvn->set_delay_transform(false);
512 if (is_clonebasic()) {
513 Node* out_mem = proj_out(TypeFunc::Memory);
514
515 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
516 if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
517 out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
518 assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
519 return false;
520 }
521
522 igvn->replace_node(out_mem->raw_out(0), mem);
523
524 Node* out_ctl = proj_out(TypeFunc::Control);
525 igvn->replace_node(out_ctl, ctl);
526 } else {
527 // replace fallthrough projections of the ArrayCopyNode by the
528 // new memory, control and the input IO.
529 CallProjections callprojs;
530 extract_projections(&callprojs, true, false);
531
532 if (callprojs.fallthrough_ioproj != nullptr) {
533 igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
534 }
535 if (callprojs.fallthrough_memproj != nullptr) {
536 igvn->replace_node(callprojs.fallthrough_memproj, mem);
537 }
538 if (callprojs.fallthrough_catchproj != nullptr) {
539 igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
540 }
541
542 // The ArrayCopyNode is not disconnected. It still has the
543 // projections for the exception case. Replace current
544 // ArrayCopyNode with a dummy new one with a top() control so
545 // that this part of the graph stays consistent but is
546 // eventually removed.
547
548 set_req(0, phase->C->top());
549 remove_dead_region(phase, can_reshape);
550 }
551 } else {
552 if (in(TypeFunc::Control) != ctl) {
553 // we can't return new memory and control from Ideal at parse time
554 assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
555 phase->record_for_igvn(this);
556 return false;
557 }
558 }
559 return true;
560 }
561
562
563 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
564 if (remove_dead_region(phase, can_reshape)) return this;
565
566 if (StressArrayCopyMacroNode && !can_reshape) {
567 phase->record_for_igvn(this);
568 return nullptr;
569 }
570
571 // See if it's a small array copy and we can inline it as
572 // loads/stores
573 // Here we can only do:
574 // - arraycopy if all arguments were validated before and we don't
575 // need card marking
576 // - clone for which we don't need to do card marking
577
578 if (!is_clonebasic() && !is_arraycopy_validated() &&
579 !is_copyofrange_validated() && !is_copyof_validated()) {
580 return nullptr;
581 }
582
583 assert(in(TypeFunc::Control) != nullptr &&
584 in(TypeFunc::Memory) != nullptr &&
586 in(ArrayCopyNode::Dest) != nullptr &&
587 in(ArrayCopyNode::Length) != nullptr &&
588 in(ArrayCopyNode::SrcPos) != nullptr &&
589 in(ArrayCopyNode::DestPos) != nullptr, "broken inputs");
590
591 if (in(TypeFunc::Control)->is_top() ||
592 in(TypeFunc::Memory)->is_top() ||
593 phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
594 phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
595 (in(ArrayCopyNode::SrcPos) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
596 (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
597 return nullptr;
598 }
599
600 int count = get_count(phase);
601
602 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
603 return nullptr;
604 }
605
606 Node* mem = try_clone_instance(phase, can_reshape, count);
607 if (mem != nullptr) {
608 return (mem == NodeSentinel) ? nullptr : mem;
609 }
610
611 Node* adr_src = nullptr;
612 Node* base_src = nullptr;
613 Node* adr_dest = nullptr;
614 Node* base_dest = nullptr;
615 BasicType copy_type = T_ILLEGAL;
616 const Type* value_type = nullptr;
617 bool disjoint_bases = false;
618
619 Node* src = in(ArrayCopyNode::Src);
620 Node* dest = in(ArrayCopyNode::Dest);
621 // EA may have moved an input to a new slice. EA stores the new address types in the ArrayCopy node itself
622 // (_src_type/_dest_type). phase->type(src) and _src_type or phase->type(dest) and _dest_type may be different
623 // when this transformation runs if igvn hasn't had a chance to propagate the new types yet. Make sure the new
624 // types are taken into account so new Load/Store nodes are created on the right slice.
625 const TypePtr* atp_src = get_address_type(phase, _src_type, src);
626 const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);
627 phase->set_type(src, phase->type(src)->join_speculative(atp_src));
628 phase->set_type(dest, phase->type(dest)->join_speculative(atp_dest));
629
630 // Control flow is going to be created, it's easier to do with _delay_transform set to true.
631
632 // prepare_array_copy() doesn't build control flow, but it creates AddP nodes. The src/dest type possibly gets
633 // narrowed above. If a newly created AddP node is commoned with a pre-existing one, then the type narrowing is lost.
634 // Setting _delay_transform before prepare_array_copy() guarantees this doesn't happen.
635 if (can_reshape) {
636 assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
637 phase->is_IterGVN()->set_delay_transform(true);
638 }
639
640 if (!prepare_array_copy(phase, can_reshape,
641 adr_src, base_src, adr_dest, base_dest,
642 copy_type, value_type, disjoint_bases)) {
643 assert(adr_src == nullptr, "no node can be left behind");
644 assert(adr_dest == nullptr, "no node can be left behind");
645 if (can_reshape) {
646 assert(phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
647 phase->is_IterGVN()->set_delay_transform(false);
648 }
649
650 return nullptr;
651 }
652
653 Node* in_mem = in(TypeFunc::Memory);
654
655 Node* backward_ctl = phase->C->top();
656 Node* forward_ctl = phase->C->top();
657 array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
658
659 Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
660 in_mem,
661 atp_src, atp_dest,
662 adr_src, base_src, adr_dest, base_dest,
663 copy_type, value_type, count);
664
665 Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
666 in_mem,
667 atp_src, atp_dest,
668 adr_src, base_src, adr_dest, base_dest,
669 copy_type, value_type, count);
670
671 Node* ctl = nullptr;
672 if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
673 ctl = new RegionNode(3);
674 ctl->init_req(1, forward_ctl);
675 ctl->init_req(2, backward_ctl);
676 ctl = phase->transform(ctl);
677 MergeMemNode* forward_mm = forward_mem->as_MergeMem();
678 MergeMemNode* backward_mm = backward_mem->as_MergeMem();
679 for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
680 if (mms.memory() != mms.memory2()) {
681 Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
682 phi->init_req(1, mms.memory());
683 phi->init_req(2, mms.memory2());
684 phi = phase->transform(phi);
685 mms.set_memory(phi);
686 }
687 }
688 mem = forward_mem;
689 } else if (!forward_ctl->is_top()) {
690 ctl = forward_ctl;
691 mem = forward_mem;
692 } else {
693 assert(!backward_ctl->is_top(), "no copy?");
694 ctl = backward_ctl;
695 mem = backward_mem;
696 }
697
698 if (can_reshape) {
699 assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
700 phase->is_IterGVN()->set_delay_transform(false);
701 }
702
703 if (!finish_transform(phase, can_reshape, ctl, mem)) {
704 if (can_reshape) {
705 // put in worklist, so that if it happens to be dead it is removed
706 phase->is_IterGVN()->_worklist.push(mem);
707 }
708 return nullptr;
709 }
710
711 return mem;
712 }
713
714 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) const {
715 Node* dest = in(ArrayCopyNode::Dest);
716 if (dest->is_top()) {
717 return false;
718 }
719 const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
720 assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
721 assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
722 _src_type->is_known_instance(), "result of EA not recorded");
723
724 if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
781 // if must_modify is true, return true if the copy is guaranteed to
782 // write between offset_lo and offset_hi
783 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
784 assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
785
786 Node* dest = in(Dest);
787 Node* dest_pos = in(DestPos);
788 Node* len = in(Length);
789
790 const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
791 const TypeInt *len_t = phase->type(len)->isa_int();
792 const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
793
794 if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
795 return !must_modify;
796 }
797
798 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
799 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
800
801 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
802 uint elemsize = type2aelembytes(ary_elem);
803
804 jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
805 jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
806 jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
807 jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
808
809 if (must_modify) {
810 if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
811 return true;
812 }
813 } else {
814 if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
815 return true;
816 }
817 }
818 return false;
819 }
820
821 // As an optimization, choose the optimal vector size for bounded copy length
822 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, jlong max_len) {
823 assert(max_len > 0, JLONG_FORMAT, max_len);
824 // We only care whether max_size_in_bytes is not larger than 32, we also want to avoid
825 // multiplication overflow, so clamp max_len to [0, 64]
826 int max_size_in_bytes = MIN2<jlong>(max_len, 64) * type2aelembytes(type);
827 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();
188 }
189
190 Node* out_mem = proj_out_or_null(TypeFunc::Memory);
191 if (can_reshape && out_mem == nullptr) { // dead node?
192 return NodeSentinel;
193 }
194
195
196 Node* base_src = in(ArrayCopyNode::Src);
197 Node* base_dest = in(ArrayCopyNode::Dest);
198 Node* ctl = in(TypeFunc::Control);
199 Node* in_mem = in(TypeFunc::Memory);
200
201 const Type* src_type = phase->type(base_src);
202 const TypeInstPtr* inst_src = src_type->isa_instptr();
203 if (inst_src == nullptr) {
204 return nullptr;
205 }
206
207 MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
208 phase->record_for_igvn(mem);
209 if (can_reshape) {
210 phase->is_IterGVN()->_worklist.push(mem);
211 }
212
213
214 ciInstanceKlass* ik = inst_src->instance_klass();
215
216 if (!inst_src->klass_is_exact()) {
217 assert(!ik->is_interface(), "inconsistent klass hierarchy");
218 if (ik->has_subklass()) {
219 // Concurrent class loading.
220 // Fail fast and return NodeSentinel to indicate that the transform failed.
221 return NodeSentinel;
222 } else {
223 phase->C->dependencies()->assert_leaf_type(ik);
224 }
225 }
226
227 const TypeInstPtr* dest_type = phase->type(base_dest)->is_instptr();
228 if (dest_type->instance_klass() != ik) {
301 Node* src_offset = in(ArrayCopyNode::SrcPos);
302 Node* dest_offset = in(ArrayCopyNode::DestPos);
303
304 if (is_arraycopy() || is_copyofrange() || is_copyof()) {
305 const Type* dest_type = phase->type(base_dest);
306 const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
307
308 // newly allocated object is guaranteed to not overlap with source object
309 disjoint_bases = is_alloc_tightly_coupled();
310 if (ary_src == nullptr || ary_src->elem() == Type::BOTTOM ||
311 ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
312 // We don't know if arguments are arrays
313 return false;
314 }
315
316 BasicType src_elem = ary_src->elem()->array_element_basic_type();
317 BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
318 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
319 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
320
321 // TODO 8251971 What about atomicity?
322 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) {
323 // We don't know if arguments are arrays of the same type
324 return false;
325 }
326
327 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
328 if ((!ary_dest->is_flat() && bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) ||
329 (ary_dest->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
330 bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), T_OBJECT, false, false, BarrierSetC2::Optimization))) {
331 // It's an object array copy but we can't emit the card marking that is needed
332 return false;
333 }
334
335 value_type = ary_src->elem();
336
337 uint shift = exact_log2(type2aelembytes(dest_elem));
338 if (ary_dest->is_flat()) {
339 assert(ary_src->is_flat(), "src and dest must be flat");
340 shift = ary_src->flat_log_elem_size();
341 src_elem = T_FLAT_ELEMENT;
342 dest_elem = T_FLAT_ELEMENT;
343 }
344
345 const uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
346
347 src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
348 if (src_offset->is_top()) {
349 // Offset is out of bounds (the ArrayCopyNode will be removed)
350 return false;
351 }
352 dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
353 if (dest_offset->is_top()) {
354 // Offset is out of bounds (the ArrayCopyNode will be removed)
355 if (can_reshape) {
356 // record src_offset, so it can be deleted later (if it is dead)
357 phase->is_IterGVN()->_worklist.push(src_offset);
358 }
359 return false;
360 }
361
362 Node* hook = new Node(1);
363 hook->init_req(0, dest_offset);
364
365 Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
366
367 hook->destruct(phase);
368
369 Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
370
371 adr_src = make_and_transform_addp(phase, base_src, src_scale);
372 adr_dest = make_and_transform_addp(phase, base_dest, dest_scale);
373
374 adr_src = make_and_transform_addp(phase, base_src, adr_src, phase->MakeConX(header));
375 adr_dest = make_and_transform_addp(phase, base_dest, adr_dest, phase->MakeConX(header));
376 copy_type = dest_elem;
377 } else {
378 assert(ary_src != nullptr, "should be a clone");
379 assert(is_clonebasic(), "should be");
380
381 disjoint_bases = true;
382
383 if (ary_src->elem()->make_oopptr() != nullptr &&
384 ary_src->elem()->make_oopptr()->can_be_inline_type()) {
385 return false;
386 }
387
388 BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
389 if (is_reference_type(elem, true)) {
390 elem = T_OBJECT;
391 }
392
393 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
394 if ((!ary_src->is_flat() && bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) ||
395 (ary_src->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
396 bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization))) {
397 // It's an object array copy but we can't emit the card marking that is needed
398 return false;
399 }
400
401 adr_src = make_and_transform_addp(phase, base_src, src_offset);
402 adr_dest = make_and_transform_addp(phase, base_dest, dest_offset);
403
404 // The address is offsetted to an aligned address where a raw copy would start.
405 // If the clone copy is decomposed into load-stores - the address is adjusted to
406 // point at where the array starts.
407 const Type* toff = phase->type(src_offset);
408 int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
409 int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
410 assert(diff >= 0, "clone should not start after 1st array element");
411 if (diff > 0) {
412 adr_src = make_and_transform_addp(phase, base_src, adr_src, phase->MakeConX(diff));
413 adr_dest = make_and_transform_addp(phase, base_dest, adr_dest, phase->MakeConX(diff));
414 }
415 copy_type = elem;
416 value_type = ary_src->elem();
417 }
418 return true;
419 }
420
421 const TypeAryPtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
422 if (atp == TypeOopPtr::BOTTOM) {
423 atp = phase->type(n)->isa_ptr();
424 }
425 // adjust atp to be the correct array element address type
426 return atp->add_offset(Type::OffsetBot)->is_aryptr();
427 }
428
429 const TypePtr* ArrayCopyNode::get_src_adr_type(PhaseGVN* phase) const {
430 return get_address_type(phase, _src_type, in(Src));
431 }
432
433 void ArrayCopyNode::array_copy_test_overlap(GraphKit& kit, bool disjoint_bases, int count, Node*& backward_ctl) {
434 Node* ctl = kit.control();
435 if (!disjoint_bases && count > 1) {
436 PhaseGVN& gvn = kit.gvn();
437 Node* src_offset = in(ArrayCopyNode::SrcPos);
438 Node* dest_offset = in(ArrayCopyNode::DestPos);
439 assert(src_offset != nullptr && dest_offset != nullptr, "should be");
440 Node* cmp = gvn.transform(new CmpINode(src_offset, dest_offset));
441 Node *bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
442 IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
443
444 gvn.transform(iff);
445
446 kit.set_control(gvn.transform(new IfFalseNode(iff)));
447 backward_ctl = gvn.transform(new IfTrueNode(iff));
448 }
449 }
450
451 void ArrayCopyNode::copy(GraphKit& kit,
452 const TypeAryPtr* atp_src,
453 const TypeAryPtr* atp_dest,
454 int i,
455 Node* base_src,
456 Node* base_dest,
457 Node* adr_src,
458 Node* adr_dest,
459 BasicType copy_type,
460 const Type* value_type) {
461 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
462 Node* ctl = kit.control();
463 if (atp_dest->is_flat()) {
464 ciInlineKlass* vk = atp_src->elem()->inline_klass();
465 for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
466 ciField* field = vk->nonstatic_field_at(j);
467 int off_in_vt = field->offset_in_bytes() - vk->payload_offset();
468 Node* off = kit.MakeConX(off_in_vt + i * atp_src->flat_elem_size());
469 ciType* ft = field->type();
470 BasicType bt = type2field[ft->basic_type()];
471 assert(!field->is_flat(), "flat field encountered");
472 const Type* rt = Type::get_const_type(ft);
473 const TypePtr* adr_type = atp_src->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
474 assert(!bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), bt, false, false, BarrierSetC2::Optimization), "GC barriers required");
475 Node* next_src = make_and_transform_addp(&kit.gvn(), base_src, adr_src, off);
476 Node* next_dest = make_and_transform_addp(&kit.gvn(), base_dest, adr_dest, off);
477 Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, adr_type, rt, bt);
478 store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, adr_type, v, rt, bt);
479 }
480 } else {
481 Node* off = kit.MakeConX(type2aelembytes(copy_type) * i);
482 Node* next_src = make_and_transform_addp(&kit.gvn(), base_src, adr_src, off);
483 Node* next_dest = make_and_transform_addp(&kit.gvn(), base_dest, adr_dest, off);
484 Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, atp_src, value_type, copy_type);
485 store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, atp_dest, v, value_type, copy_type);
486 }
487 kit.set_control(ctl);
488 }
489
490
491 void ArrayCopyNode::array_copy_forward(GraphKit& kit,
492 bool can_reshape,
493 const TypeAryPtr* atp_src,
494 const TypeAryPtr* atp_dest,
495 Node* adr_src,
496 Node* base_src,
497 Node* adr_dest,
498 Node* base_dest,
499 BasicType copy_type,
500 const Type* value_type,
501 int count) {
502 if (!kit.stopped()) {
503 // copy forward
504 if (count > 0) {
505 for (int i = 0; i < count; i++) {
506 copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);
507 }
508 } else if (can_reshape) {
509 PhaseGVN& gvn = kit.gvn();
510 assert(gvn.is_IterGVN(), "");
511 gvn.record_for_igvn(adr_src);
512 gvn.record_for_igvn(adr_dest);
513 }
514 }
515 }
516
517 void ArrayCopyNode::array_copy_backward(GraphKit& kit,
518 bool can_reshape,
519 const TypeAryPtr* atp_src,
520 const TypeAryPtr* atp_dest,
521 Node* adr_src,
522 Node* base_src,
523 Node* adr_dest,
524 Node* base_dest,
525 BasicType copy_type,
526 const Type* value_type,
527 int count) {
528 if (!kit.stopped()) {
529 // copy backward
530
531 if (count > 0) {
532 for (int i = count-1; i >= 0; i--) {
533 copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);
534 }
535 } else if(can_reshape) {
536 PhaseGVN& gvn = kit.gvn();
537 assert(gvn.is_IterGVN(), "");
538 gvn.record_for_igvn(adr_src);
539 gvn.record_for_igvn(adr_dest);
540 }
541 }
542 }
543
544 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
545 Node* ctl, Node *mem) {
546 if (can_reshape) {
547 PhaseIterGVN* igvn = phase->is_IterGVN();
548 igvn->set_delay_transform(false);
549 if (is_clonebasic()) {
550 Node* out_mem = proj_out(TypeFunc::Memory);
551
552 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
553 if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
554 out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
555 assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
556 return false;
557 }
558
559 igvn->replace_node(out_mem->raw_out(0), mem);
560
561 Node* out_ctl = proj_out(TypeFunc::Control);
562 igvn->replace_node(out_ctl, ctl);
563 } else {
564 // replace fallthrough projections of the ArrayCopyNode by the
565 // new memory, control and the input IO.
566 CallProjections* callprojs = extract_projections(true, false);
567
568 if (callprojs->fallthrough_ioproj != nullptr) {
569 igvn->replace_node(callprojs->fallthrough_ioproj, in(TypeFunc::I_O));
570 }
571 if (callprojs->fallthrough_memproj != nullptr) {
572 igvn->replace_node(callprojs->fallthrough_memproj, mem);
573 }
574 if (callprojs->fallthrough_catchproj != nullptr) {
575 igvn->replace_node(callprojs->fallthrough_catchproj, ctl);
576 }
577
578 // The ArrayCopyNode is not disconnected. It still has the
579 // projections for the exception case. Replace current
580 // ArrayCopyNode with a dummy new one with a top() control so
581 // that this part of the graph stays consistent but is
582 // eventually removed.
583
584 set_req(0, phase->C->top());
585 remove_dead_region(phase, can_reshape);
586 }
587 } else {
588 if (in(TypeFunc::Control) != ctl) {
589 // we can't return new memory and control from Ideal at parse time
590 assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
591 phase->record_for_igvn(this);
592 return false;
593 }
594 }
595 return true;
596 }
597
598
599 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
600 // Perform any generic optimizations first
601 Node* result = SafePointNode::Ideal(phase, can_reshape);
602 if (result != nullptr) {
603 return result;
604 }
605
606 if (StressArrayCopyMacroNode && !can_reshape) {
607 phase->record_for_igvn(this);
608 return nullptr;
609 }
610
611 // See if it's a small array copy and we can inline it as
612 // loads/stores
613 // Here we can only do:
614 // - arraycopy if all arguments were validated before and we don't
615 // need card marking
616 // - clone for which we don't need to do card marking
617
618 if (!is_clonebasic() && !is_arraycopy_validated() &&
619 !is_copyofrange_validated() && !is_copyof_validated()) {
620 return nullptr;
621 }
622
623 assert(in(TypeFunc::Control) != nullptr &&
624 in(TypeFunc::Memory) != nullptr &&
626 in(ArrayCopyNode::Dest) != nullptr &&
627 in(ArrayCopyNode::Length) != nullptr &&
628 in(ArrayCopyNode::SrcPos) != nullptr &&
629 in(ArrayCopyNode::DestPos) != nullptr, "broken inputs");
630
631 if (in(TypeFunc::Control)->is_top() ||
632 in(TypeFunc::Memory)->is_top() ||
633 phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
634 phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
635 (in(ArrayCopyNode::SrcPos) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
636 (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
637 return nullptr;
638 }
639
640 int count = get_count(phase);
641
642 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
643 return nullptr;
644 }
645
646 Node* src = in(ArrayCopyNode::Src);
647 Node* dest = in(ArrayCopyNode::Dest);
648 const Type* src_type = phase->type(src);
649 const Type* dest_type = phase->type(dest);
650
651 if (src_type->isa_aryptr() && dest_type->isa_instptr()) {
652 // clone used for load of unknown inline type can't be optimized at
653 // this point
654 return nullptr;
655 }
656
657 Node* mem = try_clone_instance(phase, can_reshape, count);
658 if (mem != nullptr) {
659 return (mem == NodeSentinel) ? nullptr : mem;
660 }
661
662 Node* adr_src = nullptr;
663 Node* base_src = nullptr;
664 Node* adr_dest = nullptr;
665 Node* base_dest = nullptr;
666 BasicType copy_type = T_ILLEGAL;
667 const Type* value_type = nullptr;
668 bool disjoint_bases = false;
669
670 // EA may have moved an input to a new slice. EA stores the new address types in the ArrayCopy node itself
671 // (_src_type/_dest_type). phase->type(src) and _src_type or phase->type(dest) and _dest_type may be different
672 // when this transformation runs if igvn hasn't had a chance to propagate the new types yet. Make sure the new
673 // types are taken into account so new Load/Store nodes are created on the right slice.
674 const TypeAryPtr* atp_src = get_address_type(phase, _src_type, src);
675 const TypeAryPtr* atp_dest = get_address_type(phase, _dest_type, dest);
676 phase->set_type(src, phase->type(src)->join_speculative(atp_src));
677 phase->set_type(dest, phase->type(dest)->join_speculative(atp_dest));
678
679 // Control flow is going to be created, it's easier to do with _delay_transform set to true.
680
681 // prepare_array_copy() doesn't build control flow, but it creates AddP nodes. The src/dest type possibly gets
682 // narrowed above. If a newly created AddP node is commoned with a pre-existing one, then the type narrowing is lost.
683 // Setting _delay_transform before prepare_array_copy() guarantees this doesn't happen.
684 if (can_reshape) {
685 assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
686 phase->is_IterGVN()->set_delay_transform(true);
687 }
688
689 if (!prepare_array_copy(phase, can_reshape,
690 adr_src, base_src, adr_dest, base_dest,
691 copy_type, value_type, disjoint_bases)) {
692 assert(adr_src == nullptr, "no node can be left behind");
693 assert(adr_dest == nullptr, "no node can be left behind");
694 if (can_reshape) {
695 assert(phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
696 phase->is_IterGVN()->set_delay_transform(false);
697 }
698
699 return nullptr;
700 }
701
702 JVMState* new_jvms = nullptr;
703 SafePointNode* new_map = nullptr;
704 if (!is_clonebasic()) {
705 new_jvms = jvms()->clone_shallow(phase->C);
706 new_map = new SafePointNode(req(), new_jvms);
707 for (uint i = TypeFunc::FramePtr; i < req(); i++) {
708 new_map->init_req(i, in(i));
709 }
710 new_jvms->set_map(new_map);
711 } else {
712 new_jvms = new (phase->C) JVMState(0);
713 new_map = new SafePointNode(TypeFunc::Parms, new_jvms);
714 new_jvms->set_map(new_map);
715 }
716 new_map->set_control(in(TypeFunc::Control));
717 new_map->set_memory(MergeMemNode::make(in(TypeFunc::Memory)));
718 new_map->set_i_o(in(TypeFunc::I_O));
719 phase->record_for_igvn(new_map);
720
721 GraphKit kit(new_jvms, phase);
722
723 SafePointNode* backward_map = nullptr;
724 SafePointNode* forward_map = nullptr;
725 Node* backward_ctl = phase->C->top();
726
727 array_copy_test_overlap(kit, disjoint_bases, count, backward_ctl);
728
729 {
730 PreserveJVMState pjvms(&kit);
731
732 array_copy_forward(kit, can_reshape,
733 atp_src, atp_dest,
734 adr_src, base_src, adr_dest, base_dest,
735 copy_type, value_type, count);
736
737 forward_map = kit.stop();
738 }
739
740 kit.set_control(backward_ctl);
741 array_copy_backward(kit, can_reshape,
742 atp_src, atp_dest,
743 adr_src, base_src, adr_dest, base_dest,
744 copy_type, value_type, count);
745
746 backward_map = kit.stop();
747
748 if (!forward_map->control()->is_top() && !backward_map->control()->is_top()) {
749 assert(forward_map->i_o() == backward_map->i_o(), "need a phi on IO?");
750 Node* ctl = new RegionNode(3);
751 Node* mem = new PhiNode(ctl, Type::MEMORY, TypePtr::BOTTOM);
752 kit.set_map(forward_map);
753 ctl->init_req(1, kit.control());
754 mem->init_req(1, kit.reset_memory());
755 kit.set_map(backward_map);
756 ctl->init_req(2, kit.control());
757 mem->init_req(2, kit.reset_memory());
758 kit.set_control(phase->transform(ctl));
759 kit.set_all_memory(phase->transform(mem));
760 } else if (!forward_map->control()->is_top()) {
761 kit.set_map(forward_map);
762 } else {
763 assert(!backward_map->control()->is_top(), "no copy?");
764 kit.set_map(backward_map);
765 }
766
767 if (can_reshape) {
768 assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
769 phase->is_IterGVN()->set_delay_transform(false);
770 }
771
772 mem = kit.map()->memory();
773 if (!finish_transform(phase, can_reshape, kit.control(), mem)) {
774 if (!can_reshape) {
775 phase->record_for_igvn(this);
776 } else {
777 // put in worklist, so that if it happens to be dead it is removed
778 phase->is_IterGVN()->_worklist.push(mem);
779 }
780 return nullptr;
781 }
782
783 return mem;
784 }
785
786 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) const {
787 Node* dest = in(ArrayCopyNode::Dest);
788 if (dest->is_top()) {
789 return false;
790 }
791 const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
792 assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
793 assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
794 _src_type->is_known_instance(), "result of EA not recorded");
795
796 if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
853 // if must_modify is true, return true if the copy is guaranteed to
854 // write between offset_lo and offset_hi
855 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
856 assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
857
858 Node* dest = in(Dest);
859 Node* dest_pos = in(DestPos);
860 Node* len = in(Length);
861
862 const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
863 const TypeInt *len_t = phase->type(len)->isa_int();
864 const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
865
866 if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
867 return !must_modify;
868 }
869
870 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
871 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
872
873 uint header;
874 uint elem_size;
875 if (ary_t->is_flat()) {
876 header = arrayOopDesc::base_offset_in_bytes(T_FLAT_ELEMENT);
877 elem_size = ary_t->flat_elem_size();
878 } else {
879 header = arrayOopDesc::base_offset_in_bytes(ary_elem);
880 elem_size = type2aelembytes(ary_elem);
881 }
882
883 jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elem_size + header;
884 jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elem_size + header;
885 jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elem_size + header;
886 jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elem_size + header;
887
888 if (must_modify) {
889 if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
890 return true;
891 }
892 } else {
893 if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
894 return true;
895 }
896 }
897 return false;
898 }
899
900 // As an optimization, choose the optimal vector size for bounded copy length
901 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, jlong max_len) {
902 assert(max_len > 0, JLONG_FORMAT, max_len);
903 // We only care whether max_size_in_bytes is not larger than 32, we also want to avoid
904 // multiplication overflow, so clamp max_len to [0, 64]
905 int max_size_in_bytes = MIN2<jlong>(max_len, 64) * type2aelembytes(type);
906 if (ArrayOperationPartialInlineSize > 16 && max_size_in_bytes <= 16) {
|