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/tlab_globals.hpp"
27 #include "oops/objArrayKlass.hpp"
28 #include "opto/arraycopynode.hpp"
29 #include "opto/castnode.hpp"
30 #include "opto/convertnode.hpp"
31 #include "opto/graphKit.hpp"
32 #include "opto/macro.hpp"
33 #include "opto/runtime.hpp"
34 #include "opto/vectornode.hpp"
35 #include "runtime/stubRoutines.hpp"
36 #include "utilities/align.hpp"
37 #include "utilities/powerOfTwo.hpp"
38
39 void PhaseMacroExpand::insert_mem_bar(Node** ctrl, Node** mem, int opcode, int alias_idx, Node* precedent) {
40 MemBarNode* mb = MemBarNode::make(C, opcode, alias_idx, precedent);
41 mb->init_req(TypeFunc::Control, *ctrl);
42 mb->init_req(TypeFunc::Memory, *mem);
43 transform_later(mb);
44 *ctrl = new ProjNode(mb,TypeFunc::Control);
45 transform_later(*ctrl);
46 Node* mem_proj = new ProjNode(mb,TypeFunc::Memory);
47 transform_later(mem_proj);
48 if (alias_idx == Compile::AliasIdxBot) {
49 *mem = mem_proj;
50 } else {
51 MergeMemNode* mm = (*mem)->clone()->as_MergeMem();
52 mm->set_memory_at(alias_idx, mem_proj);
53 transform_later(mm);
54 *mem = mm;
55 }
56 }
57
58 Node* PhaseMacroExpand::array_element_address(Node* ary, Node* idx, BasicType elembt, bool raw_base) {
59 uint shift = exact_log2(type2aelembytes(elembt));
60 uint header = arrayOopDesc::base_offset_in_bytes(elembt);
61 Node* base = basic_plus_adr(ary, header, raw_base);
62 #ifdef _LP64
63 // see comment in GraphKit::array_element_address
64 int index_max = max_jint - 1; // array size is max_jint, index is one less
65 const TypeLong* lidxtype = TypeLong::make(CONST64(0), index_max, Type::WidenMax);
66 idx = transform_later( new ConvI2LNode(idx, lidxtype) );
67 #endif
68 Node* scale = new LShiftXNode(idx, intcon(shift));
69 transform_later(scale);
70 return basic_plus_adr(raw_base ? top() : ary, base, scale);
71 }
72
73 Node* PhaseMacroExpand::ConvI2L(Node* offset) {
74 return transform_later(new ConvI2LNode(offset));
75 }
76
77 Node* PhaseMacroExpand::make_leaf_call(Node* ctrl, Node* mem,
78 const TypeFunc* call_type, address call_addr,
79 const char* call_name,
128 }
129
130 IfNode* iff = new IfNode(*ctrl, test, true_prob, COUNT_UNKNOWN);
131 transform_later(iff);
132
133 Node* if_slow = new IfTrueNode(iff);
134 transform_later(if_slow);
135
136 if (region != nullptr) {
137 region->add_req(if_slow);
138 }
139
140 Node* if_fast = new IfFalseNode(iff);
141 transform_later(if_fast);
142
143 *ctrl = if_fast;
144
145 return if_slow;
146 }
147
148 inline Node* PhaseMacroExpand::generate_slow_guard(Node** ctrl, Node* test, RegionNode* region) {
149 return generate_guard(ctrl, test, region, PROB_UNLIKELY_MAG(3));
150 }
151
152 void PhaseMacroExpand::generate_negative_guard(Node** ctrl, Node* index, RegionNode* region) {
153 if ((*ctrl)->is_top())
154 return; // already stopped
155 if (_igvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
156 return; // index is already adequately typed
157 Node* cmp_lt = new CmpINode(index, intcon(0));
158 transform_later(cmp_lt);
159 Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt);
160 transform_later(bol_lt);
161 generate_guard(ctrl, bol_lt, region, PROB_MIN);
162 }
163
164 void PhaseMacroExpand::generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region) {
165 if ((*ctrl)->is_top())
166 return; // already stopped
167 bool zero_offset = _igvn.type(offset) == TypeInt::ZERO;
168 if (zero_offset && subseq_length->eqv_uncast(array_length))
169 return; // common case of whole-array copy
170 Node* last = subseq_length;
171 if (!zero_offset) { // last += offset
265
266 *ctrl = stub_block;
267 }
268
269
270 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) {
271 if ((*ctrl)->is_top()) return nullptr;
272
273 if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
274 return nullptr; // index is already adequately typed
275 Node* cmp_le = new CmpINode(index, intcon(0));
276 transform_later(cmp_le);
277 BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
278 Node* bol_le = new BoolNode(cmp_le, le_or_eq);
279 transform_later(bol_le);
280 Node* is_notp = generate_guard(ctrl, bol_le, nullptr, PROB_MIN);
281
282 return is_notp;
283 }
284
285 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
286 transform_later(call);
287
288 *ctrl = new ProjNode(call,TypeFunc::Control);
289 transform_later(*ctrl);
290 Node* newmem = new ProjNode(call, TypeFunc::Memory);
291 transform_later(newmem);
292
293 uint alias_idx = C->get_alias_index(adr_type);
294 if (alias_idx != Compile::AliasIdxBot) {
295 *mem = MergeMemNode::make(*mem);
296 (*mem)->set_memory_at(alias_idx, newmem);
297 } else {
298 *mem = MergeMemNode::make(newmem);
299 }
300 transform_later(*mem);
301 }
302
303 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
304 Node* src_offset,
359 // }
360 // }
361 // // adjust params for remaining work:
362 // if (slowval != -1) {
363 // n = -1^slowval; src_offset += n; dest_offset += n; length -= n
364 // }
365 // slow_region:
366 // call slow arraycopy(src, src_offset, dest, dest_offset, length)
367 // return // via slow_call_path
368 //
369 // This routine is used from several intrinsics: System.arraycopy,
370 // Object.clone (the array subcase), and Arrays.copyOf[Range].
371 //
372 Node* PhaseMacroExpand::generate_arraycopy(ArrayCopyNode *ac, AllocateArrayNode* alloc,
373 Node** ctrl, MergeMemNode* mem, Node** io,
374 const TypePtr* adr_type,
375 BasicType basic_elem_type,
376 Node* src, Node* src_offset,
377 Node* dest, Node* dest_offset,
378 Node* copy_length,
379 bool disjoint_bases,
380 bool length_never_negative,
381 RegionNode* slow_region) {
382 Node* orig_dest = dest;
383 if (slow_region == nullptr) {
384 slow_region = new RegionNode(1);
385 transform_later(slow_region);
386 }
387
388 bool dest_needs_zeroing = false;
389 bool acopy_to_uninitialized = false;
390
391 // See if this is the initialization of a newly-allocated array.
392 // If so, we will take responsibility here for initializing it to zero.
393 // (Note: Because tightly_coupled_allocation performs checks on the
394 // out-edges of the dest, we need to avoid making derived pointers
395 // from it until we have checked its uses.)
396 if (ReduceBulkZeroing
397 && !(UseTLAB && ZeroTLAB) // pointless if already zeroed
398 && basic_elem_type != T_CONFLICT // avoid corner case
399 && !src->eqv_uncast(dest)
400 && alloc != nullptr
401 && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0) {
402 assert(ac->is_alloc_tightly_coupled(), "sanity");
403 // acopy to uninitialized tightly coupled allocations
404 // needs zeroing outside the copy range
405 // and the acopy itself will be to uninitialized memory
406 acopy_to_uninitialized = true;
407 if (alloc->maybe_set_complete(&_igvn)) {
408 // "You break it, you buy it."
409 InitializeNode* init = alloc->initialization();
410 assert(init->is_complete(), "we just did this");
411 init->set_complete_with_arraycopy();
412 assert(dest->is_CheckCastPP(), "sanity");
413 assert(dest->in(0)->in(0) == init, "dest pinned");
414 adr_type = TypeRawPtr::BOTTOM; // all initializations are into raw memory
415 dest = dest->in(1); // writing to raw memory requires a raw base
416 // From this point on, every exit path is responsible for
417 // initializing any non-copied parts of the object to zero.
418 // Also, if this flag is set we make sure that arraycopy interacts properly
419 // with G1, eliding pre-barriers. See CR 6627983.
420 dest_needs_zeroing = true;
421 } else {
422 // dest_need_zeroing = false;
423 }
424 } else {
425 // No zeroing elimination needed here.
426 alloc = nullptr;
427 acopy_to_uninitialized = false;
428 //dest_needs_zeroing = false;
429 }
430
431 uint alias_idx = C->get_alias_index(adr_type);
432
433 // Results are placed here:
434 enum { fast_path = 1, // normal void-returning assembly stub
435 checked_path = 2, // special assembly stub with cleanup
436 slow_call_path = 3, // something went wrong; call the VM
437 zero_path = 4, // bypass when length of copy is zero
438 bcopy_path = 5, // copy primitive array by 64-bit blocks
439 PATH_LIMIT = 6
440 };
469 checked_i_o = *io;
470 checked_mem = mem->memory_at(alias_idx);
471 checked_value = cv;
472 *ctrl = top();
473 }
474
475 Node* not_pos = generate_nonpositive_guard(ctrl, copy_length, length_never_negative);
476 if (not_pos != nullptr) {
477 Node* local_ctrl = not_pos, *local_io = *io;
478 MergeMemNode* local_mem = MergeMemNode::make(mem);
479 transform_later(local_mem);
480
481 // (6) length must not be negative.
482 if (!length_never_negative) {
483 generate_negative_guard(&local_ctrl, copy_length, slow_region);
484 }
485
486 // copy_length is 0.
487 if (dest_needs_zeroing) {
488 assert(!local_ctrl->is_top(), "no ctrl?");
489 Node* dest_length = alloc->in(AllocateNode::ALength);
490 if (copy_length->eqv_uncast(dest_length)
491 || _igvn.find_int_con(dest_length, 1) <= 0) {
492 // There is no zeroing to do. No need for a secondary raw memory barrier.
493 } else {
494 // Clear the whole thing since there are no source elements to copy.
495 generate_clear_array(local_ctrl, local_mem,
496 adr_type, dest, basic_elem_type,
497 intcon(0), nullptr,
498 alloc->in(AllocateNode::AllocSize));
499 // Use a secondary InitializeNode as raw memory barrier.
500 // Currently it is needed only on this path since other
501 // paths have stub or runtime calls as raw memory barriers.
502 MemBarNode* mb = MemBarNode::make(C, Op_Initialize,
503 Compile::AliasIdxRaw,
504 top());
505 transform_later(mb);
506 mb->set_req(TypeFunc::Control,local_ctrl);
507 mb->set_req(TypeFunc::Memory, local_mem->memory_at(Compile::AliasIdxRaw));
508 local_ctrl = transform_later(new ProjNode(mb, TypeFunc::Control));
509 local_mem->set_memory_at(Compile::AliasIdxRaw, transform_later(new ProjNode(mb, TypeFunc::Memory)));
510
511 InitializeNode* init = mb->as_Initialize();
512 init->set_complete(&_igvn); // (there is no corresponding AllocateNode)
513 }
514 }
515
516 // Present the results of the fast call.
517 result_region->init_req(zero_path, local_ctrl);
518 result_i_o ->init_req(zero_path, local_io);
519 result_memory->init_req(zero_path, local_mem->memory_at(alias_idx));
520 }
521
522 if (!(*ctrl)->is_top() && dest_needs_zeroing) {
523 // We have to initialize the *uncopied* part of the array to zero.
524 // The copy destination is the slice dest[off..off+len]. The other slices
525 // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length].
526 Node* dest_size = alloc->in(AllocateNode::AllocSize);
527 Node* dest_length = alloc->in(AllocateNode::ALength);
528 Node* dest_tail = transform_later( new AddINode(dest_offset, copy_length));
529
530 // If there is a head section that needs zeroing, do it now.
531 if (_igvn.find_int_con(dest_offset, -1) != 0) {
532 generate_clear_array(*ctrl, mem,
533 adr_type, dest, basic_elem_type,
534 intcon(0), dest_offset,
535 nullptr);
536 }
537
538 // Next, perform a dynamic check on the tail length.
539 // It is often zero, and we can win big if we prove this.
540 // There are two wins: Avoid generating the ClearArray
541 // with its attendant messy index arithmetic, and upgrade
542 // the copy to a more hardware-friendly word size of 64 bits.
543 Node* tail_ctl = nullptr;
544 if (!(*ctrl)->is_top() && !dest_tail->eqv_uncast(dest_length)) {
545 Node* cmp_lt = transform_later( new CmpINode(dest_tail, dest_length) );
546 Node* bol_lt = transform_later( new BoolNode(cmp_lt, BoolTest::lt) );
547 tail_ctl = generate_slow_guard(ctrl, bol_lt, nullptr);
548 assert(tail_ctl != nullptr || !(*ctrl)->is_top(), "must be an outcome");
549 }
550
551 // At this point, let's assume there is no tail.
552 if (!(*ctrl)->is_top() && alloc != nullptr && basic_elem_type != T_OBJECT) {
553 // There is no tail. Try an upgrade to a 64-bit copy.
554 bool didit = false;
555 {
556 Node* local_ctrl = *ctrl, *local_io = *io;
557 MergeMemNode* local_mem = MergeMemNode::make(mem);
558 transform_later(local_mem);
559
560 didit = generate_block_arraycopy(&local_ctrl, &local_mem, adr_type,
561 basic_elem_type, src, src_offset,
562 dest, dest_offset, dest_size, acopy_to_uninitialized);
563 if (didit) {
564 // Present the results of the block-copying fast call.
565 result_region->init_req(bcopy_path, local_ctrl);
566 result_i_o ->init_req(bcopy_path, local_io);
567 result_memory->init_req(bcopy_path, local_mem->memory_at(alias_idx));
568 }
569 }
570 if (didit) {
571 *ctrl = top(); // no regular fast path
572 }
573 }
574
575 // Clear the tail, if any.
576 if (tail_ctl != nullptr) {
577 Node* notail_ctl = (*ctrl)->is_top() ? nullptr : *ctrl;
578 *ctrl = tail_ctl;
579 if (notail_ctl == nullptr) {
580 generate_clear_array(*ctrl, mem,
581 adr_type, dest, basic_elem_type,
582 dest_tail, nullptr,
583 dest_size);
584 } else {
585 // Make a local merge.
586 Node* done_ctl = transform_later(new RegionNode(3));
587 Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type));
588 done_ctl->init_req(1, notail_ctl);
589 done_mem->init_req(1, mem->memory_at(alias_idx));
590 generate_clear_array(*ctrl, mem,
591 adr_type, dest, basic_elem_type,
592 dest_tail, nullptr,
593 dest_size);
594 done_ctl->init_req(2, *ctrl);
595 done_mem->init_req(2, mem->memory_at(alias_idx));
596 *ctrl = done_ctl;
597 mem->set_memory_at(alias_idx, done_mem);
598 }
599 }
600 }
601
602 BasicType copy_type = basic_elem_type;
603 assert(basic_elem_type != T_ARRAY, "caller must fix this");
604 if (!(*ctrl)->is_top() && copy_type == T_OBJECT) {
605 // If src and dest have compatible element types, we can copy bits.
606 // Types S[] and D[] are compatible if D is a supertype of S.
607 //
608 // If they are not, we will use checked_oop_disjoint_arraycopy,
609 // which performs a fast optimistic per-oop check, and backs off
610 // further to JVM_ArrayCopy on the first per-oop check that fails.
611 // (Actually, we don't move raw bits only; the GC requires card marks.)
612
613 // We don't need a subtype check for validated copies and Object[].clone()
630 //
631 // Test S[] against D[], not S against D, because (probably)
632 // the secondary supertype cache is less busy for S[] than S.
633 // This usually only matters when D is an interface.
634 Node* not_subtype_ctrl = Phase::gen_subtype_check(src_klass, dest_klass, ctrl, mem, _igvn, nullptr, -1);
635 // Plug failing path into checked_oop_disjoint_arraycopy
636 if (not_subtype_ctrl != top()) {
637 Node* local_ctrl = not_subtype_ctrl;
638 MergeMemNode* local_mem = MergeMemNode::make(mem);
639 transform_later(local_mem);
640
641 // (At this point we can assume disjoint_bases, since types differ.)
642 int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
643 Node* p1 = off_heap_plus_addr(dest_klass, ek_offset);
644 Node* n1 = LoadKlassNode::make(_igvn, C->immutable_memory(), p1, TypeRawPtr::BOTTOM);
645 Node* dest_elem_klass = transform_later(n1);
646 Node* cv = generate_checkcast_arraycopy(&local_ctrl, &local_mem,
647 adr_type,
648 dest_elem_klass,
649 src, src_offset, dest, dest_offset,
650 ConvI2X(copy_length), acopy_to_uninitialized);
651 if (cv == nullptr) cv = intcon(-1); // failure (no stub available)
652 checked_control = local_ctrl;
653 checked_i_o = *io;
654 checked_mem = local_mem->memory_at(alias_idx);
655 checked_value = cv;
656 }
657 }
658 // At this point we know we do not need type checks on oop stores.
659
660 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
661 if (!bs->array_copy_requires_gc_barriers(alloc != nullptr, copy_type, false, false, BarrierSetC2::Expansion)) {
662 // If we do not need gc barriers, copy using the jint or jlong stub.
663 copy_type = LP64_ONLY(UseCompressedOops ? T_INT : T_LONG) NOT_LP64(T_INT);
664 assert(type2aelembytes(basic_elem_type) == type2aelembytes(copy_type),
665 "sizes agree");
666 }
667 }
668
669 if (!(*ctrl)->is_top()) {
670 // Generate the fast path, if possible.
671 Node* local_ctrl = *ctrl;
672 MergeMemNode* local_mem = MergeMemNode::make(mem);
673 transform_later(local_mem);
674 generate_unchecked_arraycopy(&local_ctrl, &local_mem,
675 adr_type, copy_type, disjoint_bases,
676 src, src_offset, dest, dest_offset,
677 ConvI2X(copy_length), acopy_to_uninitialized);
678
679 // Present the results of the fast call.
680 result_region->init_req(fast_path, local_ctrl);
681 result_i_o ->init_req(fast_path, *io);
682 result_memory->init_req(fast_path, local_mem->memory_at(alias_idx));
683 }
684
685 // Here are all the slow paths up to this point, in one bundle:
686 assert(slow_region != nullptr, "allocated on entry");
687 slow_control = slow_region;
688 DEBUG_ONLY(slow_region = (RegionNode*)badAddress);
689
690 *ctrl = checked_control;
691 if (!(*ctrl)->is_top()) {
692 // Clean up after the checked call.
693 // The returned value is either 0 or -1^K,
694 // where K = number of partially transferred array elements.
695 Node* cmp = new CmpINode(checked_value, intcon(0));
696 transform_later(cmp);
697 Node* bol = new BoolNode(cmp, BoolTest::eq);
748 Node* length_minus = new SubINode(copy_length, slow_offset);
749 transform_later(length_minus);
750
751 // Tweak the node variables to adjust the code produced below:
752 src_offset = src_off_plus;
753 dest_offset = dest_off_plus;
754 copy_length = length_minus;
755 }
756 }
757 *ctrl = slow_control;
758 if (!(*ctrl)->is_top()) {
759 Node* local_ctrl = *ctrl, *local_io = slow_i_o;
760 MergeMemNode* local_mem = MergeMemNode::make(mem);
761 transform_later(local_mem);
762
763 // Generate the slow path, if needed.
764 local_mem->set_memory_at(alias_idx, slow_mem);
765
766 if (dest_needs_zeroing) {
767 generate_clear_array(local_ctrl, local_mem,
768 adr_type, dest, basic_elem_type,
769 intcon(0), nullptr,
770 alloc->in(AllocateNode::AllocSize));
771 }
772
773 local_mem = generate_slow_arraycopy(ac,
774 &local_ctrl, local_mem, &local_io,
775 adr_type,
776 src, src_offset, orig_dest, dest_offset,
777 copy_length, /*dest_uninitialized*/false);
778
779 result_region->init_req(slow_call_path, local_ctrl);
780 result_i_o ->init_req(slow_call_path, local_io);
781 result_memory->init_req(slow_call_path, local_mem->memory_at(alias_idx));
782 } else {
783 ShouldNotReachHere(); // no call to generate_slow_arraycopy:
784 // projections were not extracted
785 }
786
787 // Remove unused edges.
788 for (uint i = 1; i < result_region->req(); i++) {
789 if (result_region->in(i) == nullptr) {
790 result_region->init_req(i, top());
791 }
792 }
793
794 // Finished; return the combined state.
795 *ctrl = result_region;
796 *io = result_i_o;
797 mem->set_memory_at(alias_idx, result_memory);
798
799 // mem no longer guaranteed to stay a MergeMemNode
800 Node* out_mem = mem;
801 DEBUG_ONLY(mem = nullptr);
802
803 // The memory edges above are precise in order to model effects around
804 // array copies accurately to allow value numbering of field loads around
805 // arraycopy. Such field loads, both before and after, are common in Java
806 // collections and similar classes involving header/array data structures.
807 //
808 // But with low number of register or when some registers are used or killed
809 // by arraycopy calls it causes registers spilling on stack. See 6544710.
810 // The next memory barrier is added to avoid it. If the arraycopy can be
811 // optimized away (which it can, sometimes) then we can manually remove
812 // the membar also.
813 //
814 // Do not let reads from the cloned object float above the arraycopy.
815 if (alloc != nullptr && !alloc->initialization()->does_not_escape()) {
816 // Do not let stores that initialize this object be reordered with
817 // a subsequent store that would make this object accessible by
818 // other threads.
819 assert(ac->_dest_type == TypeOopPtr::BOTTOM, "non escaping destination shouldn't have narrow slice");
820 insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore, Compile::AliasIdxBot);
821 } else {
822 int mem_bar_alias_idx = Compile::AliasIdxBot;
823 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
824 // The graph was transformed under the assumption the ArrayCopy node only had an effect on a narrow slice. We can't
825 // insert a wide membar now that it's being expanded: a load that uses the input memory state of the ArrayCopy
826 // could then become anti dependent on the membar when it was not anti dependent on the ArrayCopy leading to a
827 // broken graph.
828 mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
829 }
830 insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
831 }
832
833 assert((*ctrl)->is_Proj(), "MemBar control projection");
834 assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node");
835 (*ctrl)->in(0)->isa_MemBar()->set_trailing_expanded_array_copy();
836
837 _igvn.replace_node(_callprojs.fallthrough_memproj, out_mem);
838 if (_callprojs.fallthrough_ioproj != nullptr) {
839 _igvn.replace_node(_callprojs.fallthrough_ioproj, *io);
840 }
841 _igvn.replace_node(_callprojs.fallthrough_catchproj, *ctrl);
842
843 #ifdef ASSERT
844 const TypeOopPtr* dest_t = _igvn.type(orig_dest)->is_oopptr();
845 if (dest_t->is_known_instance()) {
846 ArrayCopyNode* ac = nullptr;
847 assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost");
848 assert(ac == nullptr, "no arraycopy anymore");
849 }
850 #endif
851
852 return out_mem;
853 }
854
855 // Helper for initialization of arrays, creating a ClearArray.
856 // It writes zero bits in [start..end), within the body of an array object.
857 // The memory effects are all chained onto the 'adr_type' alias category.
858 //
859 // Since the object is otherwise uninitialized, we are free
860 // to put a little "slop" around the edges of the cleared area,
861 // as long as it does not go back into the array's header,
862 // or beyond the array end within the heap.
863 //
864 // The lower edge can be rounded down to the nearest jint and the
865 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
866 //
867 // Arguments:
868 // adr_type memory slice where writes are generated
869 // dest oop of the destination array
870 // basic_elem_type element type of the destination
871 // slice_idx array index of first element to store
872 // slice_len number of elements to store (or null)
873 // dest_size total size in bytes of the array object
874 //
875 // Exactly one of slice_len or dest_size must be non-null.
876 // If dest_size is non-null, zeroing extends to the end of the object.
877 // If slice_len is non-null, the slice_idx value must be a constant.
878 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
879 const TypePtr* adr_type,
880 Node* dest,
881 BasicType basic_elem_type,
882 Node* slice_idx,
883 Node* slice_len,
884 Node* dest_size) {
885 // one or the other but not both of slice_len and dest_size:
886 assert((slice_len != nullptr? 1: 0) + (dest_size != nullptr? 1: 0) == 1, "");
887 if (slice_len == nullptr) slice_len = top();
888 if (dest_size == nullptr) dest_size = top();
889
890 uint alias_idx = C->get_alias_index(adr_type);
891
892 // operate on this memory slice:
893 Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on
894
895 // scaling and rounding of indexes:
896 int scale = exact_log2(type2aelembytes(basic_elem_type));
897 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
898 int clear_low = (-1 << scale) & (BytesPerInt - 1);
899 int bump_bit = (-1 << scale) & BytesPerInt;
900
901 // determine constant starts and ends
902 const intptr_t BIG_NEG = -128;
903 assert(BIG_NEG + 2*abase < 0, "neg enough");
904 intptr_t slice_idx_con = (intptr_t) _igvn.find_int_con(slice_idx, BIG_NEG);
905 intptr_t slice_len_con = (intptr_t) _igvn.find_int_con(slice_len, BIG_NEG);
906 if (slice_len_con == 0) {
907 return; // nothing to do here
908 }
909 intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low;
910 intptr_t end_con = _igvn.find_intptr_t_con(dest_size, -1);
911 if (slice_idx_con >= 0 && slice_len_con >= 0) {
912 assert(end_con < 0, "not two cons");
913 end_con = align_up(abase + ((slice_idx_con + slice_len_con) << scale),
914 BytesPerLong);
915 }
916
917 if (start_con >= 0 && end_con >= 0) {
918 // Constant start and end. Simple.
919 mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
920 start_con, end_con, adr_type == TypeRawPtr::BOTTOM, &_igvn);
921 } else if (start_con >= 0 && dest_size != top()) {
922 // Constant start, pre-rounded end after the tail of the array.
923 Node* end = dest_size;
924 mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
925 start_con, end, adr_type == TypeRawPtr::BOTTOM, &_igvn);
926 } else if (start_con >= 0 && slice_len != top()) {
927 // Constant start, non-constant end. End needs rounding up.
928 // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8)
929 intptr_t end_base = abase + (slice_idx_con << scale);
930 int end_round = (-1 << scale) & (BytesPerLong - 1);
931 Node* end = ConvI2X(slice_len);
932 if (scale != 0)
933 end = transform_later(new LShiftXNode(end, intcon(scale) ));
934 end_base += end_round;
935 end = transform_later(new AddXNode(end, MakeConX(end_base)) );
936 end = transform_later(new AndXNode(end, MakeConX(~end_round)) );
937 mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
938 start_con, end, adr_type == TypeRawPtr::BOTTOM, &_igvn);
939 } else if (start_con < 0 && dest_size != top()) {
940 // Non-constant start, pre-rounded end after the tail of the array.
941 // This is almost certainly a "round-to-end" operation.
942 Node* start = slice_idx;
943 start = ConvI2X(start);
944 if (scale != 0)
945 start = transform_later(new LShiftXNode( start, intcon(scale) ));
946 start = transform_later(new AddXNode(start, MakeConX(abase)) );
947 if ((bump_bit | clear_low) != 0) {
948 int to_clear = (bump_bit | clear_low);
949 // Align up mod 8, then store a jint zero unconditionally
950 // just before the mod-8 boundary.
951 if (((abase + bump_bit) & ~to_clear) - bump_bit
952 < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) {
953 bump_bit = 0;
954 assert((abase & to_clear) == 0, "array base must be long-aligned");
955 } else {
956 // Bump 'start' up to (or past) the next jint boundary:
957 start = transform_later( new AddXNode(start, MakeConX(bump_bit)) );
958 assert((abase & clear_low) == 0, "array base must be int-aligned");
959 }
960 // Round bumped 'start' down to jlong boundary in body of array.
961 start = transform_later(new AndXNode(start, MakeConX(~to_clear)) );
962 if (bump_bit != 0) {
963 // Store a zero to the immediately preceding jint:
964 Node* x1 = transform_later(new AddXNode(start, MakeConX(-bump_bit)) );
965 Node* p1 = basic_plus_adr(dest, x1, adr_type == TypeRawPtr::BOTTOM);
966 mem = StoreNode::make(_igvn, ctrl, mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered);
967 mem = transform_later(mem);
968 }
969 }
970 Node* end = dest_size; // pre-rounded
971 mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
972 start, end, adr_type == TypeRawPtr::BOTTOM, &_igvn);
973 } else {
974 // Non-constant start, unrounded non-constant end.
975 // (Nobody zeroes a random midsection of an array using this routine.)
976 ShouldNotReachHere(); // fix caller
977 }
978
979 // Done.
980 merge_mem->set_memory_at(alias_idx, mem);
981 }
982
983 bool PhaseMacroExpand::generate_block_arraycopy(Node** ctrl, MergeMemNode** mem,
984 const TypePtr* adr_type,
985 BasicType basic_elem_type,
986 Node* src, Node* src_offset,
987 Node* dest, Node* dest_offset,
988 Node* dest_size, bool dest_uninitialized) {
989 // See if there is an advantage from block transfer.
990 int scale = exact_log2(type2aelembytes(basic_elem_type));
991 if (scale >= LogBytesPerLong)
992 return false; // it is already a block transfer
993
994 // Look at the alignment of the starting offsets.
995 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
996
997 intptr_t src_off_con = (intptr_t) _igvn.find_int_con(src_offset, -1);
998 intptr_t dest_off_con = (intptr_t) _igvn.find_int_con(dest_offset, -1);
999 if (src_off_con < 0 || dest_off_con < 0) {
1000 // At present, we can only understand constants.
1001 return false;
1002 }
1003
1004 intptr_t src_off = abase + (src_off_con << scale);
1005 intptr_t dest_off = abase + (dest_off_con << scale);
1006
1007 if (((src_off | dest_off) & (BytesPerLong-1)) != 0) {
1008 // Non-aligned; too bad.
1009 // One more chance: Pick off an initial 32-bit word.
1010 // This is a common case, since abase can be odd mod 8.
1011 if (((src_off | dest_off) & (BytesPerLong-1)) == BytesPerInt &&
1012 ((src_off ^ dest_off) & (BytesPerLong-1)) == 0) {
1013 Node* sptr = basic_plus_adr(src, src_off);
1014 Node* dptr = basic_plus_adr(dest, dest_off, adr_type == TypeRawPtr::BOTTOM);
1015 const TypePtr* s_adr_type = _igvn.type(sptr)->is_ptr();
1016 assert(s_adr_type->isa_aryptr(), "impossible slice");
1017 uint s_alias_idx = C->get_alias_index(s_adr_type);
1018 uint d_alias_idx = C->get_alias_index(adr_type);
1019 bool is_mismatched = (basic_elem_type != T_INT);
1020 Node* sval = transform_later(
1021 LoadNode::make(_igvn, *ctrl, (*mem)->memory_at(s_alias_idx), sptr, s_adr_type,
1022 TypeInt::INT, T_INT, MemNode::unordered, LoadNode::DependsOnlyOnTest,
1023 false /*require_atomic_access*/, false /*unaligned*/, is_mismatched));
1024 Node* st = transform_later(
1025 StoreNode::make(_igvn, *ctrl, (*mem)->memory_at(d_alias_idx), dptr, adr_type,
1026 sval, T_INT, MemNode::unordered));
1027 if (is_mismatched) {
1028 st->as_Store()->set_mismatched_access();
1029 }
1030 (*mem)->set_memory_at(d_alias_idx, st);
1031 src_off += BytesPerInt;
1032 dest_off += BytesPerInt;
1033 } else {
1034 return false;
1035 }
1036 }
1037 assert(src_off % BytesPerLong == 0, "");
1038 assert(dest_off % BytesPerLong == 0, "");
1039
1040 // Do this copy by giant steps.
1041 Node* sptr = basic_plus_adr(src, src_off);
1042 Node* dptr = basic_plus_adr(dest, dest_off, adr_type == TypeRawPtr::BOTTOM);
1043 Node* countx = dest_size;
1044 countx = transform_later(new SubXNode(countx, MakeConX(dest_off)));
1045 countx = transform_later(new URShiftXNode(countx, intcon(LogBytesPerLong)));
1046
1047 bool disjoint_bases = true; // since alloc isn't null
1048 generate_unchecked_arraycopy(ctrl, mem,
1049 adr_type, T_LONG, disjoint_bases,
1050 sptr, nullptr, dptr, nullptr, countx, dest_uninitialized);
1051
1052 return true;
1053 }
1054
1055 // Helper function; generates code for the slow case.
1056 // We make a call to a runtime method which emulates the native method,
1057 // but without the native wrapper overhead.
1058 MergeMemNode* PhaseMacroExpand::generate_slow_arraycopy(ArrayCopyNode *ac,
1059 Node** ctrl, Node* mem, Node** io,
1060 const TypePtr* adr_type,
1061 Node* src, Node* src_offset,
1062 Node* dest, Node* dest_offset,
1063 Node* copy_length, bool dest_uninitialized) {
1064 assert(!dest_uninitialized, "Invariant");
1065
1066 const TypeFunc* call_type = OptoRuntime::slow_arraycopy_Type();
1067 CallNode* call = new CallStaticJavaNode(call_type, OptoRuntime::slow_arraycopy_Java(),
1068 "slow_arraycopy", TypePtr::BOTTOM);
1069
1070 call->init_req(TypeFunc::Control, *ctrl);
1071 call->init_req(TypeFunc::I_O , *io);
1072 call->init_req(TypeFunc::Memory , mem);
1073 call->init_req(TypeFunc::ReturnAdr, top());
1074 call->init_req(TypeFunc::FramePtr, top());
1075 call->init_req(TypeFunc::Parms+0, src);
1076 call->init_req(TypeFunc::Parms+1, src_offset);
1077 call->init_req(TypeFunc::Parms+2, dest);
1078 call->init_req(TypeFunc::Parms+3, dest_offset);
1079 call->init_req(TypeFunc::Parms+4, copy_length);
1080 call->copy_call_debug_info(&_igvn, ac);
1081
1082 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
1083 _igvn.replace_node(ac, call);
1084 transform_later(call);
1085
1086 call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1087 *ctrl = _callprojs.fallthrough_catchproj->clone();
1088 transform_later(*ctrl);
1089
1090 Node* m = _callprojs.fallthrough_memproj->clone();
1091 transform_later(m);
1092
1093 uint alias_idx = C->get_alias_index(adr_type);
1094 MergeMemNode* out_mem;
1095 if (alias_idx != Compile::AliasIdxBot) {
1096 out_mem = MergeMemNode::make(mem);
1097 out_mem->set_memory_at(alias_idx, m);
1098 } else {
1099 out_mem = MergeMemNode::make(m);
1100 }
1101 transform_later(out_mem);
1102
1103 // When src is negative and arraycopy is before an infinite loop,_callprojs.fallthrough_ioproj
1104 // could be null. Skip clone and update null fallthrough_ioproj.
1105 if (_callprojs.fallthrough_ioproj != nullptr) {
1106 *io = _callprojs.fallthrough_ioproj->clone();
1107 transform_later(*io);
1108 } else {
1109 *io = nullptr;
1110 }
1111
1112 return out_mem;
1113 }
1114
1115 // Helper function; generates code for cases requiring runtime checks.
1116 Node* PhaseMacroExpand::generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
1117 const TypePtr* adr_type,
1118 Node* dest_elem_klass,
1119 Node* src, Node* src_offset,
1120 Node* dest, Node* dest_offset,
1121 Node* copy_length, bool dest_uninitialized) {
1122 if ((*ctrl)->is_top()) return nullptr;
1123
1124 address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized);
1125 if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path.
1126 return nullptr;
1127 }
1128
1129 // Pick out the parameters required to perform a store-check
1130 // for the target array. This is an optimistic check. It will
1131 // look in each non-null element's class, at the desired klass's
1132 // super_check_offset, for the desired klass.
1133 int sco_offset = in_bytes(Klass::super_check_offset_offset());
1134 Node* p3 = off_heap_plus_addr(dest_elem_klass, sco_offset);
1135 Node* n3 = new LoadINode(nullptr, *mem /*memory(p3)*/, p3, _igvn.type(p3)->is_ptr(), TypeInt::INT, MemNode::unordered);
1136 Node* check_offset = ConvI2X(transform_later(n3));
1137 Node* check_value = dest_elem_klass;
1138
1139 Node* src_start = array_element_address(src, src_offset, T_OBJECT, false);
1140 Node* dest_start = array_element_address(dest, dest_offset, T_OBJECT, adr_type == TypeRawPtr::BOTTOM);
1141
1142 const TypeFunc* call_type = OptoRuntime::checkcast_arraycopy_Type();
1143 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "checkcast_arraycopy", adr_type,
1144 src_start, dest_start, copy_length XTOP, check_offset XTOP, check_value);
1145
1146 finish_arraycopy_call(call, ctrl, mem, adr_type);
1147
1148 Node* proj = new ProjNode(call, TypeFunc::Parms);
1149 transform_later(proj);
1150
1151 return proj;
1152 }
1153
1154 // Helper function; generates code for cases requiring runtime checks.
1155 Node* PhaseMacroExpand::generate_generic_arraycopy(Node** ctrl, MergeMemNode** mem,
1156 const TypePtr* adr_type,
1157 Node* src, Node* src_offset,
1158 Node* dest, Node* dest_offset,
1159 Node* copy_length, bool dest_uninitialized) {
1160 if ((*ctrl)->is_top()) return nullptr;
1167
1168 const TypeFunc* call_type = OptoRuntime::generic_arraycopy_Type();
1169 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "generic_arraycopy", adr_type,
1170 src, src_offset, dest, dest_offset, copy_length);
1171
1172 finish_arraycopy_call(call, ctrl, mem, adr_type);
1173
1174 Node* proj = new ProjNode(call, TypeFunc::Parms);
1175 transform_later(proj);
1176
1177 return proj;
1178 }
1179
1180 // Helper function; generates the fast out-of-line call to an arraycopy stub.
1181 void PhaseMacroExpand::generate_unchecked_arraycopy(Node** ctrl, MergeMemNode** mem,
1182 const TypePtr* adr_type,
1183 BasicType basic_elem_type,
1184 bool disjoint_bases,
1185 Node* src, Node* src_offset,
1186 Node* dest, Node* dest_offset,
1187 Node* copy_length, bool dest_uninitialized) {
1188 if ((*ctrl)->is_top()) {
1189 return;
1190 }
1191
1192 Node* src_start = src;
1193 Node* dest_start = dest;
1194 if (src_offset != nullptr || dest_offset != nullptr) {
1195 src_start = array_element_address(src, src_offset, basic_elem_type, false);
1196 dest_start = array_element_address(dest, dest_offset, basic_elem_type, adr_type == TypeRawPtr::BOTTOM);
1197 }
1198
1199 // Figure out which arraycopy runtime method to call.
1200 const char* copyfunc_name = "arraycopy";
1201 address copyfunc_addr =
1202 basictype2arraycopy(basic_elem_type, src_offset, dest_offset,
1203 disjoint_bases, copyfunc_name, dest_uninitialized);
1204
1205 Node* result_memory = nullptr;
1206 RegionNode* exit_block = nullptr;
1207 if (ArrayOperationPartialInlineSize > 0 && is_subword_type(basic_elem_type) &&
1208 Matcher::vector_width_in_bytes(basic_elem_type) >= 16) {
1209 generate_partial_inlining_block(ctrl, mem, adr_type, &exit_block, &result_memory,
1210 copy_length, src_start, dest_start, basic_elem_type);
1211 }
1212
1213 const TypeFunc* call_type = OptoRuntime::fast_arraycopy_Type();
1214 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, copyfunc_name, adr_type,
1215 src_start, dest_start, copy_length XTOP);
1216
1218
1219 // Connecting remaining edges for exit_block coming from stub_block.
1220 if (exit_block) {
1221 exit_block->init_req(2, *ctrl);
1222
1223 // Memory edge corresponding to stub_region.
1224 result_memory->init_req(2, *mem);
1225
1226 uint alias_idx = C->get_alias_index(adr_type);
1227 if (alias_idx != Compile::AliasIdxBot) {
1228 *mem = MergeMemNode::make(*mem);
1229 (*mem)->set_memory_at(alias_idx, result_memory);
1230 } else {
1231 *mem = MergeMemNode::make(result_memory);
1232 }
1233 transform_later(*mem);
1234 *ctrl = exit_block;
1235 }
1236 }
1237
1238 #undef XTOP
1239
1240 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
1241 Node* ctrl = ac->in(TypeFunc::Control);
1242 Node* io = ac->in(TypeFunc::I_O);
1243 Node* src = ac->in(ArrayCopyNode::Src);
1244 Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
1245 Node* dest = ac->in(ArrayCopyNode::Dest);
1246 Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
1247 Node* length = ac->in(ArrayCopyNode::Length);
1248 MergeMemNode* merge_mem = nullptr;
1249
1250 if (ac->is_clonebasic()) {
1251 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1252 bs->clone_at_expansion(this, ac);
1253 return;
1254 } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) {
1255 Node* mem = ac->in(TypeFunc::Memory);
1256 merge_mem = MergeMemNode::make(mem);
1257 transform_later(merge_mem);
1258
1259 AllocateArrayNode* alloc = nullptr;
1260 if (ac->is_alloc_tightly_coupled()) {
1261 alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1262 assert(alloc != nullptr, "expect alloc");
1263 }
1264
1265 const TypePtr* adr_type = _igvn.type(dest)->is_oopptr()->add_offset(Type::OffsetBot);
1266 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1267 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1268 }
1269 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1270 adr_type, T_OBJECT,
1271 src, src_offset, dest, dest_offset, length,
1272 true, ac->has_negative_length_guard());
1273
1274 return;
1275 }
1276
1277 AllocateArrayNode* alloc = nullptr;
1278 if (ac->is_alloc_tightly_coupled()) {
1279 alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1280 assert(alloc != nullptr, "expect alloc");
1281 }
1282
1283 assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1284
1285 // Compile time checks. If any of these checks cannot be verified at compile time,
1286 // we do not make a fast path for this call. Instead, we let the call remain as it
1287 // is. The checks we choose to mandate at compile time are:
1288 //
1289 // (1) src and dest are arrays.
1290 const Type* src_type = src->Value(&_igvn);
1291 const Type* dest_type = dest->Value(&_igvn);
1292 const TypeAryPtr* top_src = src_type->isa_aryptr();
1293 const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1294
1295 BasicType src_elem = T_CONFLICT;
1296 BasicType dest_elem = T_CONFLICT;
1297
1298 if (top_src != nullptr && top_src->elem() != Type::BOTTOM) {
1299 src_elem = top_src->elem()->array_element_basic_type();
1300 }
1301 if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1302 dest_elem = top_dest->elem()->array_element_basic_type();
1303 }
1304 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
1305 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1306
1307 if (ac->is_arraycopy_validated() &&
1308 dest_elem != T_CONFLICT &&
1309 src_elem == T_CONFLICT) {
1310 src_elem = dest_elem;
1311 }
1312
1313 if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) {
1314 // Conservatively insert a memory barrier on all memory slices.
1315 // Do not let writes into the source float below the arraycopy.
1316 {
1317 Node* mem = ac->in(TypeFunc::Memory);
1318 insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, Compile::AliasIdxBot);
1319
1320 merge_mem = MergeMemNode::make(mem);
1321 transform_later(merge_mem);
1322 }
1323
1324 // Call StubRoutines::generic_arraycopy stub.
1325 Node* mem = generate_arraycopy(ac, nullptr, &ctrl, merge_mem, &io,
1326 TypeRawPtr::BOTTOM, T_CONFLICT,
1327 src, src_offset, dest, dest_offset, length,
1328 // If a negative length guard was generated for the ArrayCopyNode,
1329 // the length of the array can never be negative.
1330 false, ac->has_negative_length_guard());
1331 return;
1332 }
1333
1334 assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");
1335
1336 // (2) src and dest arrays must have elements of the same BasicType
1337 // Figure out the size and type of the elements we will be copying.
1338 if (src_elem != dest_elem || dest_elem == T_VOID) {
1339 // The component types are not the same or are not recognized. Punt.
1340 // (But, avoid the native method wrapper to JVM_ArrayCopy.)
1341 {
1342 Node* mem = ac->in(TypeFunc::Memory);
1343 merge_mem = generate_slow_arraycopy(ac, &ctrl, mem, &io, TypePtr::BOTTOM, src, src_offset, dest, dest_offset, length, false);
1344 }
1345
1346 _igvn.replace_node(_callprojs.fallthrough_memproj, merge_mem);
1347 if (_callprojs.fallthrough_ioproj != nullptr) {
1348 _igvn.replace_node(_callprojs.fallthrough_ioproj, io);
1349 }
1350 _igvn.replace_node(_callprojs.fallthrough_catchproj, ctrl);
1351 return;
1352 }
1353
1354 //---------------------------------------------------------------------------
1355 // We will make a fast path for this call to arraycopy.
1356
1357 // We have the following tests left to perform:
1358 //
1359 // (3) src and dest must not be null.
1360 // (4) src_offset must not be negative.
1361 // (5) dest_offset must not be negative.
1362 // (6) length must not be negative.
1363 // (7) src_offset + length must not exceed length of src.
1364 // (8) dest_offset + length must not exceed length of dest.
1365 // (9) each element of an oop array must be assignable
1366
1367 {
1368 Node* mem = ac->in(TypeFunc::Memory);
1369 merge_mem = MergeMemNode::make(mem);
1370 transform_later(merge_mem);
1371 }
1372
1373 RegionNode* slow_region = new RegionNode(1);
1374 transform_later(slow_region);
1375
1376 if (!ac->is_arraycopy_validated()) {
1377 // (3) operands must not be null
1378 // We currently perform our null checks with the null_check routine.
1379 // This means that the null exceptions will be reported in the caller
1380 // rather than (correctly) reported inside of the native arraycopy call.
1381 // This should be corrected, given time. We do our null check with the
1382 // stack pointer restored.
1383 // null checks done library_call.cpp
1384
1385 // (4) src_offset must not be negative.
1386 generate_negative_guard(&ctrl, src_offset, slow_region);
1387
1388 // (5) dest_offset must not be negative.
1389 generate_negative_guard(&ctrl, dest_offset, slow_region);
1390
1391 // (6) length must not be negative (moved to generate_arraycopy()).
1392 // generate_negative_guard(length, slow_region);
1393
1394 // (7) src_offset + length must not exceed length of src.
1395 Node* alen = ac->in(ArrayCopyNode::SrcLen);
1396 assert(alen != nullptr, "need src len");
1397 generate_limit_guard(&ctrl,
1398 src_offset, length,
1399 alen,
1400 slow_region);
1401
1402 // (8) dest_offset + length must not exceed length of dest.
1403 alen = ac->in(ArrayCopyNode::DestLen);
1404 assert(alen != nullptr, "need dest len");
1405 generate_limit_guard(&ctrl,
1406 dest_offset, length,
1407 alen,
1408 slow_region);
1409
1410 // (9) each element of an oop array must be assignable
1411 // The generate_arraycopy subroutine checks this.
1412 }
1413 // This is where the memory effects are placed:
1414 const TypePtr* adr_type = nullptr;
1415 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1416 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1417 } else {
1418 adr_type = TypeAryPtr::get_array_body_type(dest_elem);
1419 }
1420
1421 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1422 adr_type, dest_elem,
1423 src, src_offset, dest, dest_offset, length,
1424 // If a negative length guard was generated for the ArrayCopyNode,
1425 // the length of the array can never be negative.
1426 false, ac->has_negative_length_guard(), slow_region);
1427 }
|
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/tlab_globals.hpp"
28 #include "oops/objArrayKlass.hpp"
29 #include "opto/arraycopynode.hpp"
30 #include "opto/castnode.hpp"
31 #include "opto/convertnode.hpp"
32 #include "opto/graphKit.hpp"
33 #include "opto/macro.hpp"
34 #include "opto/runtime.hpp"
35 #include "opto/vectornode.hpp"
36 #include "runtime/stubRoutines.hpp"
37 #include "utilities/align.hpp"
38 #include "utilities/powerOfTwo.hpp"
39
40 void PhaseMacroExpand::insert_mem_bar(Node** ctrl, Node** mem, int opcode, int alias_idx, Node* precedent) {
41 MemBarNode* mb = MemBarNode::make(C, opcode, alias_idx, precedent);
42 mb->init_req(TypeFunc::Control, *ctrl);
43 mb->init_req(TypeFunc::Memory, *mem);
44 transform_later(mb);
45 *ctrl = new ProjNode(mb,TypeFunc::Control);
46 transform_later(*ctrl);
47 Node* mem_proj = new ProjNode(mb,TypeFunc::Memory);
48 transform_later(mem_proj);
49 if (alias_idx == Compile::AliasIdxBot) {
50 *mem = mem_proj;
51 } else {
52 MergeMemNode* mm = (*mem)->clone()->as_MergeMem();
53 mm->set_memory_at(alias_idx, mem_proj);
54 transform_later(mm);
55 *mem = mm;
56 }
57 }
58
59 Node* PhaseMacroExpand::array_element_address(Node* ary, Node* idx, BasicType elembt, bool raw_base) {
60 uint shift = exact_log2(type2aelembytes(elembt));
61 const TypeAryPtr* array_type = _igvn.type(ary)->isa_aryptr();
62 if (array_type != nullptr && array_type->is_aryptr()->is_flat()) {
63 // Use T_FLAT_ELEMENT to get proper alignment with COH when fetching the array element address.
64 elembt = T_FLAT_ELEMENT;
65 }
66 uint header = arrayOopDesc::base_offset_in_bytes(elembt);
67 Node* base = basic_plus_adr(ary, header, raw_base);
68 #ifdef _LP64
69 // see comment in GraphKit::array_element_address
70 int index_max = max_jint - 1; // array size is max_jint, index is one less
71 const TypeLong* lidxtype = TypeLong::make(CONST64(0), index_max, Type::WidenMax);
72 idx = transform_later( new ConvI2LNode(idx, lidxtype) );
73 #endif
74 Node* scale = new LShiftXNode(idx, intcon(shift));
75 transform_later(scale);
76 return basic_plus_adr(raw_base ? top() : ary, base, scale);
77 }
78
79 Node* PhaseMacroExpand::ConvI2L(Node* offset) {
80 return transform_later(new ConvI2LNode(offset));
81 }
82
83 Node* PhaseMacroExpand::make_leaf_call(Node* ctrl, Node* mem,
84 const TypeFunc* call_type, address call_addr,
85 const char* call_name,
134 }
135
136 IfNode* iff = new IfNode(*ctrl, test, true_prob, COUNT_UNKNOWN);
137 transform_later(iff);
138
139 Node* if_slow = new IfTrueNode(iff);
140 transform_later(if_slow);
141
142 if (region != nullptr) {
143 region->add_req(if_slow);
144 }
145
146 Node* if_fast = new IfFalseNode(iff);
147 transform_later(if_fast);
148
149 *ctrl = if_fast;
150
151 return if_slow;
152 }
153
154 Node* PhaseMacroExpand::generate_slow_guard(Node** ctrl, Node* test, RegionNode* region) {
155 return generate_guard(ctrl, test, region, PROB_UNLIKELY_MAG(3));
156 }
157
158 inline Node* PhaseMacroExpand::generate_fair_guard(Node** ctrl, Node* test, RegionNode* region) {
159 return generate_guard(ctrl, test, region, PROB_FAIR);
160 }
161
162 void PhaseMacroExpand::generate_negative_guard(Node** ctrl, Node* index, RegionNode* region) {
163 if ((*ctrl)->is_top())
164 return; // already stopped
165 if (_igvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
166 return; // index is already adequately typed
167 Node* cmp_lt = new CmpINode(index, intcon(0));
168 transform_later(cmp_lt);
169 Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt);
170 transform_later(bol_lt);
171 generate_guard(ctrl, bol_lt, region, PROB_MIN);
172 }
173
174 void PhaseMacroExpand::generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region) {
175 if ((*ctrl)->is_top())
176 return; // already stopped
177 bool zero_offset = _igvn.type(offset) == TypeInt::ZERO;
178 if (zero_offset && subseq_length->eqv_uncast(array_length))
179 return; // common case of whole-array copy
180 Node* last = subseq_length;
181 if (!zero_offset) { // last += offset
275
276 *ctrl = stub_block;
277 }
278
279
280 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) {
281 if ((*ctrl)->is_top()) return nullptr;
282
283 if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
284 return nullptr; // index is already adequately typed
285 Node* cmp_le = new CmpINode(index, intcon(0));
286 transform_later(cmp_le);
287 BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
288 Node* bol_le = new BoolNode(cmp_le, le_or_eq);
289 transform_later(bol_le);
290 Node* is_notp = generate_guard(ctrl, bol_le, nullptr, PROB_MIN);
291
292 return is_notp;
293 }
294
295 Node* PhaseMacroExpand::mark_word_test(Node** ctrl, Node* obj, MergeMemNode* mem, uintptr_t mask_val, RegionNode* region) {
296 // Load markword and check if obj is locked
297 Node* mark = make_load_raw(nullptr, mem->memory_at(Compile::AliasIdxRaw), obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
298 Node* locked_bit = MakeConX(markWord::unlocked_value);
299 locked_bit = transform_later(new AndXNode(locked_bit, mark));
300 Node* cmp = transform_later(new CmpXNode(locked_bit, MakeConX(0)));
301 Node* is_unlocked = transform_later(new BoolNode(cmp, BoolTest::ne));
302 IfNode* iff = transform_later(new IfNode(*ctrl, is_unlocked, PROB_MAX, COUNT_UNKNOWN))->as_If();
303 Node* locked_region = transform_later(new RegionNode(3));
304 Node* mark_phi = transform_later(new PhiNode(locked_region, TypeX_X));
305
306 // Unlocked: Use bits from mark word
307 locked_region->init_req(1, transform_later(new IfTrueNode(iff)));
308 mark_phi->init_req(1, mark);
309
310 // Locked: Load prototype header from klass
311 *ctrl = transform_later(new IfFalseNode(iff));
312 // Make loads control dependent to make sure they are only executed if array is locked
313 Node* klass_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());
314 Node* klass = transform_later(LoadKlassNode::make(_igvn, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
315 Node* proto_adr = basic_plus_adr(top(), klass, in_bytes(Klass::prototype_header_offset()));
316 Node* proto = transform_later(LoadNode::make(_igvn, *ctrl, C->immutable_memory(), proto_adr, proto_adr->bottom_type()->is_ptr(), TypeX_X, TypeX_X->basic_type(), MemNode::unordered));
317
318 locked_region->init_req(2, *ctrl);
319 mark_phi->init_req(2, proto);
320 *ctrl = locked_region;
321
322 // Now check if mark word bits are set
323 Node* mask = MakeConX(mask_val);
324 Node* masked = transform_later(new AndXNode(mark_phi, mask));
325 cmp = transform_later(new CmpXNode(masked, mask));
326 Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq));
327 return generate_fair_guard(ctrl, bol, region);
328 }
329
330 Node* PhaseMacroExpand::generate_flat_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
331 return mark_word_test(ctrl, array, mem, markWord::flat_array_bit_in_place, region);
332 }
333
334 Node* PhaseMacroExpand::generate_null_free_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
335 return mark_word_test(ctrl, array, mem, markWord::null_free_array_bit_in_place, region);
336 }
337
338 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
339 transform_later(call);
340
341 *ctrl = new ProjNode(call,TypeFunc::Control);
342 transform_later(*ctrl);
343 Node* newmem = new ProjNode(call, TypeFunc::Memory);
344 transform_later(newmem);
345
346 uint alias_idx = C->get_alias_index(adr_type);
347 if (alias_idx != Compile::AliasIdxBot) {
348 *mem = MergeMemNode::make(*mem);
349 (*mem)->set_memory_at(alias_idx, newmem);
350 } else {
351 *mem = MergeMemNode::make(newmem);
352 }
353 transform_later(*mem);
354 }
355
356 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
357 Node* src_offset,
412 // }
413 // }
414 // // adjust params for remaining work:
415 // if (slowval != -1) {
416 // n = -1^slowval; src_offset += n; dest_offset += n; length -= n
417 // }
418 // slow_region:
419 // call slow arraycopy(src, src_offset, dest, dest_offset, length)
420 // return // via slow_call_path
421 //
422 // This routine is used from several intrinsics: System.arraycopy,
423 // Object.clone (the array subcase), and Arrays.copyOf[Range].
424 //
425 Node* PhaseMacroExpand::generate_arraycopy(ArrayCopyNode *ac, AllocateArrayNode* alloc,
426 Node** ctrl, MergeMemNode* mem, Node** io,
427 const TypePtr* adr_type,
428 BasicType basic_elem_type,
429 Node* src, Node* src_offset,
430 Node* dest, Node* dest_offset,
431 Node* copy_length,
432 Node* dest_length,
433 bool raw_base,
434 bool disjoint_bases,
435 bool length_never_negative,
436 RegionNode* slow_region) {
437 Node* orig_dest = dest;
438 if (slow_region == nullptr) {
439 slow_region = new RegionNode(1);
440 transform_later(slow_region);
441 }
442
443 bool dest_needs_zeroing = false;
444 bool acopy_to_uninitialized = false;
445 Node* init_value = nullptr;
446 Node* raw_init_value = nullptr;
447
448 // See if this is the initialization of a newly-allocated array.
449 // If so, we will take responsibility here for initializing it to zero.
450 // (Note: Because tightly_coupled_allocation performs checks on the
451 // out-edges of the dest, we need to avoid making derived pointers
452 // from it until we have checked its uses.)
453 if (ReduceBulkZeroing
454 && !(UseTLAB && ZeroTLAB) // pointless if already zeroed
455 && basic_elem_type != T_CONFLICT // avoid corner case
456 && !src->eqv_uncast(dest)
457 && alloc != nullptr
458 && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0) {
459 assert(ac->is_alloc_tightly_coupled(), "sanity");
460 // acopy to uninitialized tightly coupled allocations
461 // needs zeroing outside the copy range
462 // and the acopy itself will be to uninitialized memory
463 acopy_to_uninitialized = true;
464 if (alloc->maybe_set_complete(&_igvn)) {
465 // "You break it, you buy it."
466 InitializeNode* init = alloc->initialization();
467 assert(init->is_complete(), "we just did this");
468 init->set_complete_with_arraycopy();
469 assert(dest->is_CheckCastPP(), "sanity");
470 assert(dest->in(0)->in(0) == init, "dest pinned");
471 adr_type = TypeRawPtr::BOTTOM; // all initializations are into raw memory
472 raw_base = true;
473 dest = dest->in(1); // writing to raw memory requires a raw base
474 // From this point on, every exit path is responsible for
475 // initializing any non-copied parts of the object to zero.
476 // Also, if this flag is set we make sure that arraycopy interacts properly
477 // with G1, eliding pre-barriers. See CR 6627983.
478 dest_needs_zeroing = true;
479 init_value = alloc->in(AllocateNode::InitValue);
480 raw_init_value = alloc->in(AllocateNode::RawInitValue);
481 } else {
482 // dest_need_zeroing = false;
483 }
484 } else {
485 // No zeroing elimination needed here.
486 alloc = nullptr;
487 acopy_to_uninitialized = false;
488 //dest_needs_zeroing = false;
489 }
490
491 uint alias_idx = C->get_alias_index(adr_type);
492
493 // Results are placed here:
494 enum { fast_path = 1, // normal void-returning assembly stub
495 checked_path = 2, // special assembly stub with cleanup
496 slow_call_path = 3, // something went wrong; call the VM
497 zero_path = 4, // bypass when length of copy is zero
498 bcopy_path = 5, // copy primitive array by 64-bit blocks
499 PATH_LIMIT = 6
500 };
529 checked_i_o = *io;
530 checked_mem = mem->memory_at(alias_idx);
531 checked_value = cv;
532 *ctrl = top();
533 }
534
535 Node* not_pos = generate_nonpositive_guard(ctrl, copy_length, length_never_negative);
536 if (not_pos != nullptr) {
537 Node* local_ctrl = not_pos, *local_io = *io;
538 MergeMemNode* local_mem = MergeMemNode::make(mem);
539 transform_later(local_mem);
540
541 // (6) length must not be negative.
542 if (!length_never_negative) {
543 generate_negative_guard(&local_ctrl, copy_length, slow_region);
544 }
545
546 // copy_length is 0.
547 if (dest_needs_zeroing) {
548 assert(!local_ctrl->is_top(), "no ctrl?");
549 if (copy_length->eqv_uncast(dest_length)
550 || _igvn.find_int_con(dest_length, 1) <= 0) {
551 // There is no zeroing to do. No need for a secondary raw memory barrier.
552 } else {
553 // Clear the whole thing since there are no source elements to copy.
554 generate_clear_array(local_ctrl, local_mem,
555 adr_type, dest,
556 init_value, raw_init_value,
557 basic_elem_type,
558 intcon(0), nullptr,
559 alloc->in(AllocateNode::AllocSize), raw_base);
560 // Use a secondary InitializeNode as raw memory barrier.
561 // Currently it is needed only on this path since other
562 // paths have stub or runtime calls as raw memory barriers.
563 MemBarNode* mb = MemBarNode::make(C, Op_Initialize,
564 Compile::AliasIdxRaw,
565 top());
566 transform_later(mb);
567 mb->set_req(TypeFunc::Control,local_ctrl);
568 mb->set_req(TypeFunc::Memory, local_mem->memory_at(Compile::AliasIdxRaw));
569 local_ctrl = transform_later(new ProjNode(mb, TypeFunc::Control));
570 local_mem->set_memory_at(Compile::AliasIdxRaw, transform_later(new ProjNode(mb, TypeFunc::Memory)));
571
572 InitializeNode* init = mb->as_Initialize();
573 init->set_complete(&_igvn); // (there is no corresponding AllocateNode)
574 }
575 }
576
577 // Present the results of the fast call.
578 result_region->init_req(zero_path, local_ctrl);
579 result_i_o ->init_req(zero_path, local_io);
580 result_memory->init_req(zero_path, local_mem->memory_at(alias_idx));
581 }
582
583 if (!(*ctrl)->is_top() && dest_needs_zeroing) {
584 // We have to initialize the *uncopied* part of the array to zero.
585 // The copy destination is the slice dest[off..off+len]. The other slices
586 // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length].
587 Node* dest_size = alloc->in(AllocateNode::AllocSize);
588 Node* dest_tail = transform_later( new AddINode(dest_offset, copy_length));
589
590 // If there is a head section that needs zeroing, do it now.
591 if (_igvn.find_int_con(dest_offset, -1) != 0) {
592 generate_clear_array(*ctrl, mem,
593 adr_type, dest,
594 init_value, raw_init_value,
595 basic_elem_type,
596 intcon(0), dest_offset,
597 nullptr, raw_base);
598 }
599
600 // Next, perform a dynamic check on the tail length.
601 // It is often zero, and we can win big if we prove this.
602 // There are two wins: Avoid generating the ClearArray
603 // with its attendant messy index arithmetic, and upgrade
604 // the copy to a more hardware-friendly word size of 64 bits.
605 Node* tail_ctl = nullptr;
606 if (!(*ctrl)->is_top() && !dest_tail->eqv_uncast(dest_length)) {
607 Node* cmp_lt = transform_later( new CmpINode(dest_tail, dest_length) );
608 Node* bol_lt = transform_later( new BoolNode(cmp_lt, BoolTest::lt) );
609 tail_ctl = generate_slow_guard(ctrl, bol_lt, nullptr);
610 assert(tail_ctl != nullptr || !(*ctrl)->is_top(), "must be an outcome");
611 }
612
613 // At this point, let's assume there is no tail.
614 if (!(*ctrl)->is_top() && alloc != nullptr && basic_elem_type != T_OBJECT) {
615 // There is no tail. Try an upgrade to a 64-bit copy.
616 bool didit = false;
617 {
618 Node* local_ctrl = *ctrl, *local_io = *io;
619 MergeMemNode* local_mem = MergeMemNode::make(mem);
620 transform_later(local_mem);
621
622 didit = generate_block_arraycopy(&local_ctrl, &local_mem, adr_type,
623 basic_elem_type, src, src_offset,
624 dest, dest_offset, dest_size, acopy_to_uninitialized, raw_base);
625 if (didit) {
626 // Present the results of the block-copying fast call.
627 result_region->init_req(bcopy_path, local_ctrl);
628 result_i_o ->init_req(bcopy_path, local_io);
629 result_memory->init_req(bcopy_path, local_mem->memory_at(alias_idx));
630 }
631 }
632 if (didit) {
633 *ctrl = top(); // no regular fast path
634 }
635 }
636
637 // Clear the tail, if any.
638 if (tail_ctl != nullptr) {
639 Node* notail_ctl = (*ctrl)->is_top() ? nullptr : *ctrl;
640 *ctrl = tail_ctl;
641 if (notail_ctl == nullptr) {
642 generate_clear_array(*ctrl, mem,
643 adr_type, dest,
644 init_value, raw_init_value,
645 basic_elem_type,
646 dest_tail, nullptr,
647 dest_size, raw_base);
648 } else {
649 // Make a local merge.
650 Node* done_ctl = transform_later(new RegionNode(3));
651 Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type));
652 done_ctl->init_req(1, notail_ctl);
653 done_mem->init_req(1, mem->memory_at(alias_idx));
654 generate_clear_array(*ctrl, mem,
655 adr_type, dest,
656 init_value, raw_init_value,
657 basic_elem_type,
658 dest_tail, nullptr,
659 dest_size, raw_base);
660 done_ctl->init_req(2, *ctrl);
661 done_mem->init_req(2, mem->memory_at(alias_idx));
662 *ctrl = done_ctl;
663 mem->set_memory_at(alias_idx, done_mem);
664 }
665 }
666 }
667
668 BasicType copy_type = basic_elem_type;
669 assert(basic_elem_type != T_ARRAY, "caller must fix this");
670 if (!(*ctrl)->is_top() && copy_type == T_OBJECT) {
671 // If src and dest have compatible element types, we can copy bits.
672 // Types S[] and D[] are compatible if D is a supertype of S.
673 //
674 // If they are not, we will use checked_oop_disjoint_arraycopy,
675 // which performs a fast optimistic per-oop check, and backs off
676 // further to JVM_ArrayCopy on the first per-oop check that fails.
677 // (Actually, we don't move raw bits only; the GC requires card marks.)
678
679 // We don't need a subtype check for validated copies and Object[].clone()
696 //
697 // Test S[] against D[], not S against D, because (probably)
698 // the secondary supertype cache is less busy for S[] than S.
699 // This usually only matters when D is an interface.
700 Node* not_subtype_ctrl = Phase::gen_subtype_check(src_klass, dest_klass, ctrl, mem, _igvn, nullptr, -1);
701 // Plug failing path into checked_oop_disjoint_arraycopy
702 if (not_subtype_ctrl != top()) {
703 Node* local_ctrl = not_subtype_ctrl;
704 MergeMemNode* local_mem = MergeMemNode::make(mem);
705 transform_later(local_mem);
706
707 // (At this point we can assume disjoint_bases, since types differ.)
708 int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
709 Node* p1 = off_heap_plus_addr(dest_klass, ek_offset);
710 Node* n1 = LoadKlassNode::make(_igvn, C->immutable_memory(), p1, TypeRawPtr::BOTTOM);
711 Node* dest_elem_klass = transform_later(n1);
712 Node* cv = generate_checkcast_arraycopy(&local_ctrl, &local_mem,
713 adr_type,
714 dest_elem_klass,
715 src, src_offset, dest, dest_offset,
716 ConvI2X(copy_length), acopy_to_uninitialized, raw_base);
717 if (cv == nullptr) cv = intcon(-1); // failure (no stub available)
718 checked_control = local_ctrl;
719 checked_i_o = *io;
720 checked_mem = local_mem->memory_at(alias_idx);
721 checked_value = cv;
722 }
723 }
724 // At this point we know we do not need type checks on oop stores.
725
726 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
727 if (!bs->array_copy_requires_gc_barriers(alloc != nullptr, copy_type, false, false, BarrierSetC2::Expansion)) {
728 // If we do not need gc barriers, copy using the jint or jlong stub.
729 copy_type = LP64_ONLY(UseCompressedOops ? T_INT : T_LONG) NOT_LP64(T_INT);
730 assert(type2aelembytes(basic_elem_type) == type2aelembytes(copy_type),
731 "sizes agree");
732 }
733 }
734
735 if (!(*ctrl)->is_top()) {
736 // Generate the fast path, if possible.
737 Node* local_ctrl = *ctrl;
738 MergeMemNode* local_mem = MergeMemNode::make(mem);
739 transform_later(local_mem);
740 generate_unchecked_arraycopy(&local_ctrl, &local_mem,
741 adr_type, copy_type, disjoint_bases,
742 src, src_offset, dest, dest_offset,
743 ConvI2X(copy_length), acopy_to_uninitialized,
744 raw_base);
745
746 // Present the results of the fast call.
747 result_region->init_req(fast_path, local_ctrl);
748 result_i_o ->init_req(fast_path, *io);
749 result_memory->init_req(fast_path, local_mem->memory_at(alias_idx));
750 }
751
752 // Here are all the slow paths up to this point, in one bundle:
753 assert(slow_region != nullptr, "allocated on entry");
754 slow_control = slow_region;
755 DEBUG_ONLY(slow_region = (RegionNode*)badAddress);
756
757 *ctrl = checked_control;
758 if (!(*ctrl)->is_top()) {
759 // Clean up after the checked call.
760 // The returned value is either 0 or -1^K,
761 // where K = number of partially transferred array elements.
762 Node* cmp = new CmpINode(checked_value, intcon(0));
763 transform_later(cmp);
764 Node* bol = new BoolNode(cmp, BoolTest::eq);
815 Node* length_minus = new SubINode(copy_length, slow_offset);
816 transform_later(length_minus);
817
818 // Tweak the node variables to adjust the code produced below:
819 src_offset = src_off_plus;
820 dest_offset = dest_off_plus;
821 copy_length = length_minus;
822 }
823 }
824 *ctrl = slow_control;
825 if (!(*ctrl)->is_top()) {
826 Node* local_ctrl = *ctrl, *local_io = slow_i_o;
827 MergeMemNode* local_mem = MergeMemNode::make(mem);
828 transform_later(local_mem);
829
830 // Generate the slow path, if needed.
831 local_mem->set_memory_at(alias_idx, slow_mem);
832
833 if (dest_needs_zeroing) {
834 generate_clear_array(local_ctrl, local_mem,
835 adr_type, dest,
836 init_value, raw_init_value,
837 basic_elem_type,
838 intcon(0), nullptr,
839 alloc->in(AllocateNode::AllocSize),
840 raw_base);
841 }
842
843 local_mem = generate_slow_arraycopy(ac,
844 &local_ctrl, local_mem, &local_io,
845 adr_type,
846 src, src_offset, orig_dest, dest_offset,
847 copy_length, /*dest_uninitialized*/false);
848
849 result_region->init_req(slow_call_path, local_ctrl);
850 result_i_o ->init_req(slow_call_path, local_io);
851 result_memory->init_req(slow_call_path, local_mem->memory_at(alias_idx));
852 } else {
853 ShouldNotReachHere(); // no call to generate_slow_arraycopy:
854 // projections were not extracted
855 }
856
857 // Remove unused edges.
858 for (uint i = 1; i < result_region->req(); i++) {
859 if (result_region->in(i) == nullptr) {
860 result_region->init_req(i, top());
861 }
862 }
863
864 // Finished; return the combined state.
865 *ctrl = result_region;
866 *io = result_i_o;
867 mem->set_memory_at(alias_idx, result_memory);
868
869 // mem no longer guaranteed to stay a MergeMemNode
870 Node* out_mem = mem;
871 DEBUG_ONLY(mem = nullptr);
872
873 // The memory edges above are precise in order to model effects around
874 // array copies accurately to allow value numbering of field loads around
875 // arraycopy. Such field loads, both before and after, are common in Java
876 // collections and similar classes involving header/array data structures.
877 //
878 // But with low number of register or when some registers are used or killed
879 // by arraycopy calls it causes registers spilling on stack. See 6544710.
880 // The next memory barrier is added to avoid it. If the arraycopy can be
881 // optimized away (which it can, sometimes) then we can manually remove // Do not let reads from the cloned object float above the arrayco
882 // the membar also.
883 //
884 // Do not let reads from the cloned object float above the arraycopy.
885 if (alloc != nullptr && !alloc->initialization()->does_not_escape()) {
886 // Do not let stores that initialize this object be reordered with
887 // a subsequent store that would make this object accessible by
888 // other threads.
889 assert(ac->_dest_type == TypeOopPtr::BOTTOM, "non escaping destination shouldn't have narrow slice");
890 insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore, Compile::AliasIdxBot);
891 } else {
892 int mem_bar_alias_idx = Compile::AliasIdxBot;
893 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
894 // The graph was transformed under the assumption the ArrayCopy node only had an effect on a narrow slice. We can't
895 // insert a wide membar now that it's being expanded: a load that uses the input memory state of the ArrayCopy
896 // could then become anti dependent on the membar when it was not anti dependent on the ArrayCopy leading to a
897 // broken graph.
898 mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
899 }
900 insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
901 }
902
903 assert((*ctrl)->is_Proj(), "MemBar control projection");
904 assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node");
905 (*ctrl)->in(0)->isa_MemBar()->set_trailing_expanded_array_copy();
906
907 _igvn.replace_node(_callprojs->fallthrough_memproj, out_mem);
908 if (_callprojs->fallthrough_ioproj != nullptr) {
909 _igvn.replace_node(_callprojs->fallthrough_ioproj, *io);
910 }
911 _igvn.replace_node(_callprojs->fallthrough_catchproj, *ctrl);
912
913 #ifdef ASSERT
914 const TypeOopPtr* dest_t = _igvn.type(orig_dest)->is_oopptr();
915 if (dest_t->is_known_instance()) {
916 ArrayCopyNode* ac = nullptr;
917 assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost");
918 assert(ac == nullptr, "no arraycopy anymore");
919 }
920 #endif
921
922 return out_mem;
923 }
924
925 // Helper for initialization of arrays, creating a ClearArray.
926 // It writes zero bits in [start..end), within the body of an array object.
927 // The memory effects are all chained onto the 'adr_type' alias category.
928 //
929 // Since the object is otherwise uninitialized, we are free
930 // to put a little "slop" around the edges of the cleared area,
931 // as long as it does not go back into the array's header,
932 // or beyond the array end within the heap.
933 //
934 // The lower edge can be rounded down to the nearest jint and the
935 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
936 //
937 // Arguments:
938 // adr_type memory slice where writes are generated
939 // dest oop of the destination array
940 // basic_elem_type element type of the destination
941 // slice_idx array index of first element to store
942 // slice_len number of elements to store (or null)
943 // dest_size total size in bytes of the array object
944 //
945 // Exactly one of slice_len or dest_size must be non-null.
946 // If dest_size is non-null, zeroing extends to the end of the object.
947 // If slice_len is non-null, the slice_idx value must be a constant.
948 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
949 const TypePtr* adr_type,
950 Node* dest,
951 Node* val,
952 Node* raw_val,
953 BasicType basic_elem_type,
954 Node* slice_idx,
955 Node* slice_len,
956 Node* dest_size,
957 const bool raw_base) {
958 // one or the other but not both of slice_len and dest_size:
959 assert((slice_len != nullptr? 1: 0) + (dest_size != nullptr? 1: 0) == 1, "");
960 if (slice_len == nullptr) slice_len = top();
961 if (dest_size == nullptr) dest_size = top();
962
963 uint alias_idx = C->get_alias_index(adr_type);
964
965 // operate on this memory slice:
966 Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on
967
968 // scaling and rounding of indexes:
969 int scale = exact_log2(type2aelembytes(basic_elem_type));
970 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
971 int clear_low = (-1 << scale) & (BytesPerInt - 1);
972 int bump_bit = (-1 << scale) & BytesPerInt;
973
974 // determine constant starts and ends
975 const intptr_t BIG_NEG = -128;
976 assert(BIG_NEG + 2*abase < 0, "neg enough");
977 intptr_t slice_idx_con = (intptr_t) _igvn.find_int_con(slice_idx, BIG_NEG);
978 intptr_t slice_len_con = (intptr_t) _igvn.find_int_con(slice_len, BIG_NEG);
979 if (slice_len_con == 0) {
980 return; // nothing to do here
981 }
982 intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low;
983 intptr_t end_con = _igvn.find_intptr_t_con(dest_size, -1);
984 if (slice_idx_con >= 0 && slice_len_con >= 0) {
985 assert(end_con < 0, "not two cons");
986 end_con = align_up(abase + ((slice_idx_con + slice_len_con) << scale),
987 BytesPerLong);
988 }
989
990 if (start_con >= 0 && end_con >= 0) {
991 // Constant start and end. Simple.
992 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
993 start_con, end_con, raw_base, &_igvn);
994 } else if (start_con >= 0 && dest_size != top()) {
995 // Constant start, pre-rounded end after the tail of the array.
996 Node* end = dest_size;
997 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
998 start_con, end, raw_base, &_igvn);
999 } else if (start_con >= 0 && slice_len != top()) {
1000 // Constant start, non-constant end. End needs rounding up.
1001 // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8)
1002 intptr_t end_base = abase + (slice_idx_con << scale);
1003 int end_round = (-1 << scale) & (BytesPerLong - 1);
1004 Node* end = ConvI2X(slice_len);
1005 if (scale != 0)
1006 end = transform_later(new LShiftXNode(end, intcon(scale) ));
1007 end_base += end_round;
1008 end = transform_later(new AddXNode(end, MakeConX(end_base)) );
1009 end = transform_later(new AndXNode(end, MakeConX(~end_round)) );
1010 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
1011 start_con, end, raw_base, &_igvn);
1012 } else if (start_con < 0 && dest_size != top()) {
1013 // Non-constant start, pre-rounded end after the tail of the array.
1014 // This is almost certainly a "round-to-end" operation.
1015 Node* start = slice_idx;
1016 start = ConvI2X(start);
1017 if (scale != 0)
1018 start = transform_later(new LShiftXNode( start, intcon(scale) ));
1019 start = transform_later(new AddXNode(start, MakeConX(abase)) );
1020 if ((bump_bit | clear_low) != 0) {
1021 int to_clear = (bump_bit | clear_low);
1022 // Align up mod 8, then store a jint zero unconditionally
1023 // just before the mod-8 boundary.
1024 if (((abase + bump_bit) & ~to_clear) - bump_bit
1025 < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) {
1026 bump_bit = 0;
1027 assert((abase & to_clear) == 0, "array base must be long-aligned");
1028 } else {
1029 // Bump 'start' up to (or past) the next jint boundary:
1030 start = transform_later( new AddXNode(start, MakeConX(bump_bit)) );
1031 assert((abase & clear_low) == 0, "array base must be int-aligned");
1032 }
1033 // Round bumped 'start' down to jlong boundary in body of array.
1034 start = transform_later(new AndXNode(start, MakeConX(~to_clear)) );
1035 if (bump_bit != 0) {
1036 // Store a zero to the immediately preceding jint:
1037 Node* x1 = transform_later(new AddXNode(start, MakeConX(-bump_bit)) );
1038 Node* p1 = basic_plus_adr(dest, x1, raw_base);
1039 if (val == nullptr) {
1040 assert(raw_val == nullptr, "val may not be null");
1041 mem = StoreNode::make(_igvn, ctrl, mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered);
1042 } else {
1043 assert(_igvn.type(val)->isa_narrowoop(), "should be narrow oop");
1044 mem = new StoreNNode(ctrl, mem, p1, adr_type, val, MemNode::unordered);
1045 }
1046 mem = transform_later(mem);
1047 }
1048 }
1049 Node* end = dest_size; // pre-rounded
1050 mem = ClearArrayNode::clear_memory(ctrl, mem, dest, raw_val,
1051 start, end, raw_base, &_igvn);
1052 } else {
1053 // Non-constant start, unrounded non-constant end.
1054 // (Nobody zeroes a random midsection of an array using this routine.)
1055 ShouldNotReachHere(); // fix caller
1056 }
1057
1058 // Done.
1059 merge_mem->set_memory_at(alias_idx, mem);
1060 }
1061
1062 bool PhaseMacroExpand::generate_block_arraycopy(Node** ctrl, MergeMemNode** mem,
1063 const TypePtr* adr_type,
1064 BasicType basic_elem_type,
1065 Node* src, Node* src_offset,
1066 Node* dest, Node* dest_offset,
1067 Node* dest_size, bool dest_uninitialized,
1068 const bool raw_base) {
1069 // See if there is an advantage from block transfer.
1070 int scale = exact_log2(type2aelembytes(basic_elem_type));
1071 if (scale >= LogBytesPerLong)
1072 return false; // it is already a block transfer
1073
1074 // Look at the alignment of the starting offsets.
1075 int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
1076
1077 intptr_t src_off_con = (intptr_t) _igvn.find_int_con(src_offset, -1);
1078 intptr_t dest_off_con = (intptr_t) _igvn.find_int_con(dest_offset, -1);
1079 if (src_off_con < 0 || dest_off_con < 0) {
1080 // At present, we can only understand constants.
1081 return false;
1082 }
1083
1084 intptr_t src_off = abase + (src_off_con << scale);
1085 intptr_t dest_off = abase + (dest_off_con << scale);
1086
1087 if (((src_off | dest_off) & (BytesPerLong-1)) != 0) {
1088 // Non-aligned; too bad.
1089 // One more chance: Pick off an initial 32-bit word.
1090 // This is a common case, since abase can be odd mod 8.
1091 if (((src_off | dest_off) & (BytesPerLong-1)) == BytesPerInt &&
1092 ((src_off ^ dest_off) & (BytesPerLong-1)) == 0) {
1093 Node* sptr = basic_plus_adr(src, src_off);
1094 Node* dptr = basic_plus_adr(dest, dest_off, raw_base);
1095 const TypePtr* s_adr_type = _igvn.type(sptr)->is_ptr();
1096 assert(s_adr_type->isa_aryptr(), "impossible slice");
1097 uint s_alias_idx = C->get_alias_index(s_adr_type);
1098 uint d_alias_idx = C->get_alias_index(adr_type);
1099 bool is_mismatched = (basic_elem_type != T_INT);
1100 Node* sval = transform_later(
1101 LoadNode::make(_igvn, *ctrl, (*mem)->memory_at(s_alias_idx), sptr, s_adr_type,
1102 TypeInt::INT, T_INT, MemNode::unordered, LoadNode::DependsOnlyOnTest,
1103 false /*require_atomic_access*/, false /*unaligned*/, is_mismatched));
1104 Node* st = transform_later(
1105 StoreNode::make(_igvn, *ctrl, (*mem)->memory_at(d_alias_idx), dptr, adr_type,
1106 sval, T_INT, MemNode::unordered));
1107 if (is_mismatched) {
1108 st->as_Store()->set_mismatched_access();
1109 }
1110 (*mem)->set_memory_at(d_alias_idx, st);
1111 src_off += BytesPerInt;
1112 dest_off += BytesPerInt;
1113 } else {
1114 return false;
1115 }
1116 }
1117 assert(src_off % BytesPerLong == 0, "");
1118 assert(dest_off % BytesPerLong == 0, "");
1119
1120 // Do this copy by giant steps.
1121 Node* sptr = basic_plus_adr(src, src_off);
1122 Node* dptr = basic_plus_adr(dest, dest_off, raw_base);
1123 Node* countx = dest_size;
1124 countx = transform_later(new SubXNode(countx, MakeConX(dest_off)));
1125 countx = transform_later(new URShiftXNode(countx, intcon(LogBytesPerLong)));
1126
1127 bool disjoint_bases = true; // since alloc isn't null
1128 generate_unchecked_arraycopy(ctrl, mem,
1129 adr_type, T_LONG, disjoint_bases,
1130 sptr, nullptr, dptr, nullptr, countx, dest_uninitialized, raw_base);
1131
1132 return true;
1133 }
1134
1135 // Helper function; generates code for the slow case.
1136 // We make a call to a runtime method which emulates the native method,
1137 // but without the native wrapper overhead.
1138 MergeMemNode* PhaseMacroExpand::generate_slow_arraycopy(ArrayCopyNode *ac,
1139 Node** ctrl, Node* mem, Node** io,
1140 const TypePtr* adr_type,
1141 Node* src, Node* src_offset,
1142 Node* dest, Node* dest_offset,
1143 Node* copy_length, bool dest_uninitialized) {
1144 assert(!dest_uninitialized, "Invariant");
1145
1146 const TypeFunc* call_type = OptoRuntime::slow_arraycopy_Type();
1147 CallNode* call = new CallStaticJavaNode(call_type, OptoRuntime::slow_arraycopy_Java(),
1148 "slow_arraycopy", TypePtr::BOTTOM);
1149
1150 call->init_req(TypeFunc::Control, *ctrl);
1151 call->init_req(TypeFunc::I_O , *io);
1152 call->init_req(TypeFunc::Memory , mem);
1153 call->init_req(TypeFunc::ReturnAdr, top());
1154 call->init_req(TypeFunc::FramePtr, top());
1155 call->init_req(TypeFunc::Parms+0, src);
1156 call->init_req(TypeFunc::Parms+1, src_offset);
1157 call->init_req(TypeFunc::Parms+2, dest);
1158 call->init_req(TypeFunc::Parms+3, dest_offset);
1159 call->init_req(TypeFunc::Parms+4, copy_length);
1160 call->copy_call_debug_info(&_igvn, ac);
1161
1162 call->set_cnt(PROB_UNLIKELY_MAG(4)); // Same effect as RC_UNCOMMON.
1163 _igvn.replace_node(ac, call);
1164 transform_later(call);
1165
1166 _callprojs = call->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1167 *ctrl = _callprojs->fallthrough_catchproj->clone();
1168 transform_later(*ctrl);
1169
1170 Node* m = _callprojs->fallthrough_memproj->clone();
1171 transform_later(m);
1172
1173 uint alias_idx = C->get_alias_index(adr_type);
1174 MergeMemNode* out_mem;
1175 if (alias_idx != Compile::AliasIdxBot) {
1176 out_mem = MergeMemNode::make(mem);
1177 out_mem->set_memory_at(alias_idx, m);
1178 } else {
1179 out_mem = MergeMemNode::make(m);
1180 }
1181 transform_later(out_mem);
1182
1183 // When src is negative and arraycopy is before an infinite loop,_callprojs.fallthrough_ioproj
1184 // could be nullptr. Skip clone and update nullptr fallthrough_ioproj.
1185 if (_callprojs->fallthrough_ioproj != nullptr) {
1186 *io = _callprojs->fallthrough_ioproj->clone();
1187 transform_later(*io);
1188 } else {
1189 *io = nullptr;
1190 }
1191
1192 return out_mem;
1193 }
1194
1195 // Helper function; generates code for cases requiring runtime checks.
1196 Node* PhaseMacroExpand::generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
1197 const TypePtr* adr_type,
1198 Node* dest_elem_klass,
1199 Node* src, Node* src_offset,
1200 Node* dest, Node* dest_offset,
1201 Node* copy_length, bool dest_uninitialized,
1202 const bool raw_base) {
1203 if ((*ctrl)->is_top()) return nullptr;
1204
1205 address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized);
1206 if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path.
1207 return nullptr;
1208 }
1209
1210 // Pick out the parameters required to perform a store-check
1211 // for the target array. This is an optimistic check. It will
1212 // look in each non-null element's class, at the desired klass's
1213 // super_check_offset, for the desired klass.
1214 int sco_offset = in_bytes(Klass::super_check_offset_offset());
1215 Node* p3 = off_heap_plus_addr(dest_elem_klass, sco_offset);
1216 Node* n3 = new LoadINode(nullptr, *mem /*memory(p3)*/, p3, _igvn.type(p3)->is_ptr(), TypeInt::INT, MemNode::unordered);
1217 Node* check_offset = ConvI2X(transform_later(n3));
1218 Node* check_value = dest_elem_klass;
1219
1220 Node* src_start = array_element_address(src, src_offset, T_OBJECT, false);
1221 Node* dest_start = array_element_address(dest, dest_offset, T_OBJECT, raw_base);
1222
1223 const TypeFunc* call_type = OptoRuntime::checkcast_arraycopy_Type();
1224 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "checkcast_arraycopy", adr_type,
1225 src_start, dest_start, copy_length XTOP, check_offset XTOP, check_value);
1226
1227 finish_arraycopy_call(call, ctrl, mem, adr_type);
1228
1229 Node* proj = new ProjNode(call, TypeFunc::Parms);
1230 transform_later(proj);
1231
1232 return proj;
1233 }
1234
1235 // Helper function; generates code for cases requiring runtime checks.
1236 Node* PhaseMacroExpand::generate_generic_arraycopy(Node** ctrl, MergeMemNode** mem,
1237 const TypePtr* adr_type,
1238 Node* src, Node* src_offset,
1239 Node* dest, Node* dest_offset,
1240 Node* copy_length, bool dest_uninitialized) {
1241 if ((*ctrl)->is_top()) return nullptr;
1248
1249 const TypeFunc* call_type = OptoRuntime::generic_arraycopy_Type();
1250 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, "generic_arraycopy", adr_type,
1251 src, src_offset, dest, dest_offset, copy_length);
1252
1253 finish_arraycopy_call(call, ctrl, mem, adr_type);
1254
1255 Node* proj = new ProjNode(call, TypeFunc::Parms);
1256 transform_later(proj);
1257
1258 return proj;
1259 }
1260
1261 // Helper function; generates the fast out-of-line call to an arraycopy stub.
1262 void PhaseMacroExpand::generate_unchecked_arraycopy(Node** ctrl, MergeMemNode** mem,
1263 const TypePtr* adr_type,
1264 BasicType basic_elem_type,
1265 bool disjoint_bases,
1266 Node* src, Node* src_offset,
1267 Node* dest, Node* dest_offset,
1268 Node* copy_length, bool dest_uninitialized,
1269 const bool raw_base) {
1270 if ((*ctrl)->is_top()) {
1271 return;
1272 }
1273
1274 Node* src_start = src;
1275 Node* dest_start = dest;
1276 if (src_offset != nullptr || dest_offset != nullptr) {
1277 src_start = array_element_address(src, src_offset, basic_elem_type, false);
1278 dest_start = array_element_address(dest, dest_offset, basic_elem_type, raw_base);
1279 }
1280
1281 // Figure out which arraycopy runtime method to call.
1282 const char* copyfunc_name = "arraycopy";
1283 address copyfunc_addr =
1284 basictype2arraycopy(basic_elem_type, src_offset, dest_offset,
1285 disjoint_bases, copyfunc_name, dest_uninitialized);
1286
1287 Node* result_memory = nullptr;
1288 RegionNode* exit_block = nullptr;
1289 if (ArrayOperationPartialInlineSize > 0 && is_subword_type(basic_elem_type) &&
1290 Matcher::vector_width_in_bytes(basic_elem_type) >= 16) {
1291 generate_partial_inlining_block(ctrl, mem, adr_type, &exit_block, &result_memory,
1292 copy_length, src_start, dest_start, basic_elem_type);
1293 }
1294
1295 const TypeFunc* call_type = OptoRuntime::fast_arraycopy_Type();
1296 Node* call = make_leaf_call(*ctrl, *mem, call_type, copyfunc_addr, copyfunc_name, adr_type,
1297 src_start, dest_start, copy_length XTOP);
1298
1300
1301 // Connecting remaining edges for exit_block coming from stub_block.
1302 if (exit_block) {
1303 exit_block->init_req(2, *ctrl);
1304
1305 // Memory edge corresponding to stub_region.
1306 result_memory->init_req(2, *mem);
1307
1308 uint alias_idx = C->get_alias_index(adr_type);
1309 if (alias_idx != Compile::AliasIdxBot) {
1310 *mem = MergeMemNode::make(*mem);
1311 (*mem)->set_memory_at(alias_idx, result_memory);
1312 } else {
1313 *mem = MergeMemNode::make(result_memory);
1314 }
1315 transform_later(*mem);
1316 *ctrl = exit_block;
1317 }
1318 }
1319
1320 const TypePtr* PhaseMacroExpand::adjust_for_flat_array(const TypeAryPtr* top_dest, Node*& src_offset,
1321 Node*& dest_offset, Node*& length, BasicType& dest_elem,
1322 Node*& dest_length) {
1323 #ifdef ASSERT
1324 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1325 bool needs_barriers = top_dest->elem()->inline_klass()->contains_oops() &&
1326 bs->array_copy_requires_gc_barriers(dest_length != nullptr, T_OBJECT, false, false, BarrierSetC2::Optimization);
1327 assert(!needs_barriers || StressReflectiveCode, "Flat arracopy would require GC barriers");
1328 #endif
1329 int elem_size = top_dest->flat_elem_size();
1330 if (elem_size >= 8) {
1331 if (elem_size > 8) {
1332 // treat as array of long but scale length, src offset and dest offset
1333 assert((elem_size % 8) == 0, "not a power of 2?");
1334 int factor = elem_size / 8;
1335 length = transform_later(new MulINode(length, intcon(factor)));
1336 src_offset = transform_later(new MulINode(src_offset, intcon(factor)));
1337 dest_offset = transform_later(new MulINode(dest_offset, intcon(factor)));
1338 if (dest_length != nullptr) {
1339 dest_length = transform_later(new MulINode(dest_length, intcon(factor)));
1340 }
1341 elem_size = 8;
1342 }
1343 dest_elem = T_LONG;
1344 } else if (elem_size == 4) {
1345 dest_elem = T_INT;
1346 } else if (elem_size == 2) {
1347 dest_elem = T_CHAR;
1348 } else if (elem_size == 1) {
1349 dest_elem = T_BYTE;
1350 } else {
1351 ShouldNotReachHere();
1352 }
1353 return TypeRawPtr::BOTTOM;
1354 }
1355
1356 #undef XTOP
1357
1358 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
1359 Node* ctrl = ac->in(TypeFunc::Control);
1360 Node* io = ac->in(TypeFunc::I_O);
1361 Node* src = ac->in(ArrayCopyNode::Src);
1362 Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
1363 Node* dest = ac->in(ArrayCopyNode::Dest);
1364 Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
1365 Node* length = ac->in(ArrayCopyNode::Length);
1366 MergeMemNode* merge_mem = nullptr;
1367
1368 if (ac->is_clonebasic()) {
1369 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1370 bs->clone_at_expansion(this, ac);
1371 return;
1372 } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) {
1373 const Type* src_type = _igvn.type(src);
1374 const Type* dest_type = _igvn.type(dest);
1375 const TypeAryPtr* top_src = src_type->isa_aryptr();
1376 // Note: The destination could have type Object (i.e. non-array) when directly invoking the protected method
1377 // Object::clone() with reflection on a declared Object that is an array at runtime. top_dest is then null.
1378 const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1379 BasicType dest_elem = T_OBJECT;
1380 if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1381 dest_elem = top_dest->elem()->array_element_basic_type();
1382 }
1383 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1384
1385 if (top_src != nullptr && top_src->is_flat()) {
1386 // If src is flat, dest is guaranteed to be flat as well
1387 top_dest = top_src;
1388 }
1389
1390 AllocateArrayNode* alloc = nullptr;
1391 Node* dest_length = nullptr;
1392 if (ac->is_alloc_tightly_coupled()) {
1393 alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1394 assert(alloc != nullptr, "expect alloc");
1395 dest_length = alloc->in(AllocateNode::ALength);
1396 }
1397
1398 Node* mem = ac->in(TypeFunc::Memory);
1399 const TypePtr* adr_type = nullptr;
1400 bool raw_base = false;
1401 if (top_dest != nullptr && top_dest->is_flat()) {
1402 assert(dest_length != nullptr || StressReflectiveCode, "must be tightly coupled");
1403 // Copy to a flat array modifies multiple memory slices. Conservatively insert a barrier
1404 // on all slices to prevent writes into the source from floating below the arraycopy.
1405 int mem_bar_alias_idx = Compile::AliasIdxBot;
1406 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1407 mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
1408 }
1409 insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
1410 adr_type = adjust_for_flat_array(top_dest, src_offset, dest_offset, length, dest_elem, dest_length);
1411 } else {
1412 adr_type = dest_type->is_oopptr()->add_offset(Type::OffsetBot);
1413 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1414 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1415 }
1416 if (ac->_src_type != ac->_dest_type) {
1417 adr_type = TypeRawPtr::BOTTOM;
1418 raw_base = true;
1419 }
1420 }
1421 merge_mem = MergeMemNode::make(mem);
1422 transform_later(merge_mem);
1423
1424 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1425 adr_type, dest_elem,
1426 src, src_offset, dest, dest_offset, length,
1427 dest_length,
1428 raw_base, true, ac->has_negative_length_guard());
1429
1430 return;
1431 }
1432
1433 AllocateArrayNode* alloc = nullptr;
1434 if (ac->is_alloc_tightly_coupled()) {
1435 alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1436 assert(alloc != nullptr, "expect alloc");
1437 }
1438
1439 assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1440
1441 // Compile time checks. If any of these checks cannot be verified at compile time,
1442 // we do not make a fast path for this call. Instead, we let the call remain as it
1443 // is. The checks we choose to mandate at compile time are:
1444 //
1445 // (1) src and dest are arrays.
1446 const Type* src_type = src->Value(&_igvn);
1447 const Type* dest_type = dest->Value(&_igvn);
1448 const TypeAryPtr* top_src = src_type->isa_aryptr();
1449 const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1450
1451 BasicType src_elem = T_CONFLICT;
1452 BasicType dest_elem = T_CONFLICT;
1453
1454 if (top_src != nullptr && top_src->elem() != Type::BOTTOM) {
1455 src_elem = top_src->elem()->array_element_basic_type();
1456 }
1457 if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1458 dest_elem = top_dest->elem()->array_element_basic_type();
1459 }
1460 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
1461 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1462
1463 if (ac->is_arraycopy_validated() && dest_elem != T_CONFLICT && src_elem == T_CONFLICT) {
1464 src_elem = dest_elem;
1465 }
1466
1467 if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) {
1468 // Conservatively insert a memory barrier on all memory slices.
1469 // Do not let writes into the source float below the arraycopy.
1470 {
1471 Node* mem = ac->in(TypeFunc::Memory);
1472 insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, Compile::AliasIdxBot);
1473
1474 merge_mem = MergeMemNode::make(mem);
1475 transform_later(merge_mem);
1476 }
1477
1478 // Call StubRoutines::generic_arraycopy stub.
1479 generate_arraycopy(ac, nullptr, &ctrl, merge_mem, &io,
1480 TypeRawPtr::BOTTOM, T_CONFLICT,
1481 src, src_offset, dest, dest_offset, length,
1482 nullptr,
1483 // If a negative length guard was generated for the ArrayCopyNode,
1484 // the length of the array can never be negative.
1485 true, false, ac->has_negative_length_guard());
1486 return;
1487 }
1488
1489 assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");
1490
1491 // (2) src and dest arrays must have elements of the same BasicType
1492 // Figure out the size and type of the elements we will be copying.
1493 //
1494 // We have no stub to copy flat inline type arrays with oop
1495 // fields if we need to emit write barriers.
1496 //
1497 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1498 if (src_elem != dest_elem || top_src->is_flat() != top_dest->is_flat() || dest_elem == T_VOID ||
1499 (top_src->is_flat() && top_dest->elem()->inline_klass()->contains_oops() &&
1500 bs->array_copy_requires_gc_barriers(alloc != nullptr, T_OBJECT, false, false, BarrierSetC2::Optimization))) {
1501 // The component types are not the same or are not recognized. Punt.
1502 // (But, avoid the native method wrapper to JVM_ArrayCopy.)
1503 {
1504 Node* mem = ac->in(TypeFunc::Memory);
1505 merge_mem = generate_slow_arraycopy(ac, &ctrl, mem, &io, TypePtr::BOTTOM, src, src_offset, dest, dest_offset, length, false);
1506 }
1507
1508 _igvn.replace_node(_callprojs->fallthrough_memproj, merge_mem);
1509 if (_callprojs->fallthrough_ioproj != nullptr) {
1510 _igvn.replace_node(_callprojs->fallthrough_ioproj, io);
1511 }
1512 _igvn.replace_node(_callprojs->fallthrough_catchproj, ctrl);
1513 return;
1514 }
1515
1516 //---------------------------------------------------------------------------
1517 // We will make a fast path for this call to arraycopy.
1518
1519 // We have the following tests left to perform:
1520 //
1521 // (3) src and dest must not be null.
1522 // (4) src_offset must not be negative.
1523 // (5) dest_offset must not be negative.
1524 // (6) length must not be negative.
1525 // (7) src_offset + length must not exceed length of src.
1526 // (8) dest_offset + length must not exceed length of dest.
1527 // (9) each element of an oop array must be assignable
1528
1529 Node* mem = ac->in(TypeFunc::Memory);
1530 if (top_dest->is_flat()) {
1531 // Copy to a flat array modifies multiple memory slices. Conservatively insert a barrier
1532 // on all slices to prevent writes into the source from floating below the arraycopy.
1533 int mem_bar_alias_idx = Compile::AliasIdxBot;
1534 if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1535 mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
1536 }
1537 insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
1538 }
1539 merge_mem = MergeMemNode::make(mem);
1540 transform_later(merge_mem);
1541
1542 RegionNode* slow_region = new RegionNode(1);
1543 transform_later(slow_region);
1544
1545 if (!ac->is_arraycopy_validated()) {
1546 // (3) operands must not be null
1547 // We currently perform our null checks with the null_check routine.
1548 // This means that the null exceptions will be reported in the caller
1549 // rather than (correctly) reported inside of the native arraycopy call.
1550 // This should be corrected, given time. We do our null check with the
1551 // stack pointer restored.
1552 // null checks done library_call.cpp
1553
1554 // (4) src_offset must not be negative.
1555 generate_negative_guard(&ctrl, src_offset, slow_region);
1556
1557 // (5) dest_offset must not be negative.
1558 generate_negative_guard(&ctrl, dest_offset, slow_region);
1559
1560 // (6) length must not be negative (moved to generate_arraycopy()).
1561 // generate_negative_guard(length, slow_region);
1562
1563 // (7) src_offset + length must not exceed length of src.
1564 Node* alen = ac->in(ArrayCopyNode::SrcLen);
1565 assert(alen != nullptr, "need src len");
1566 generate_limit_guard(&ctrl,
1567 src_offset, length,
1568 alen,
1569 slow_region);
1570
1571 // (8) dest_offset + length must not exceed length of dest.
1572 alen = ac->in(ArrayCopyNode::DestLen);
1573 assert(alen != nullptr, "need dest len");
1574 generate_limit_guard(&ctrl,
1575 dest_offset, length,
1576 alen,
1577 slow_region);
1578
1579 // (9) each element of an oop array must be assignable
1580 // The generate_arraycopy subroutine checks this.
1581
1582 // TODO 8350865 This is too strong
1583 // We need to be careful here because 'adjust_for_flat_array' will adjust offsets/length etc. which then does not work anymore for the slow call to SharedRuntime::slow_arraycopy_C.
1584 if (!(top_src->is_flat() && top_dest->is_flat() && top_src->is_null_free() == top_dest->is_null_free())) {
1585 generate_flat_array_guard(&ctrl, src, merge_mem, slow_region);
1586 generate_flat_array_guard(&ctrl, dest, merge_mem, slow_region);
1587 generate_null_free_array_guard(&ctrl, dest, merge_mem, slow_region);
1588 }
1589 }
1590
1591 // This is where the memory effects are placed:
1592 const TypePtr* adr_type = nullptr;
1593 Node* dest_length = (alloc != nullptr) ? alloc->in(AllocateNode::ALength) : nullptr;
1594 if (top_src->is_flat() && top_dest->is_flat() &&
1595 top_src->is_null_free() == top_dest->is_null_free()) {
1596 adr_type = adjust_for_flat_array(top_dest, src_offset, dest_offset, length, dest_elem, dest_length);
1597 } else if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1598 adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1599 } else {
1600 adr_type = TypeAryPtr::get_array_body_type(dest_elem);
1601 }
1602
1603 generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1604 adr_type, dest_elem,
1605 src, src_offset, dest, dest_offset, length,
1606 dest_length,
1607 // If a negative length guard was generated for the ArrayCopyNode,
1608 // the length of the array can never be negative.
1609 false, false, ac->has_negative_length_guard(),
1610 slow_region);
1611 }
|