< prev index next >

src/hotspot/share/opto/macroArrayCopy.cpp

Print this page

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 

  25 #include "gc/shared/barrierSet.hpp"
  26 #include "gc/shared/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) {
  59   uint shift  = exact_log2(type2aelembytes(elembt));





  60   uint header = arrayOopDesc::base_offset_in_bytes(elembt);
  61   Node* base =  basic_plus_adr(ary, header);
  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(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

 272 
 273   *ctrl = stub_block;
 274 }
 275 
 276 
 277 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) {
 278   if ((*ctrl)->is_top())  return nullptr;
 279 
 280   if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
 281     return nullptr;                // index is already adequately typed
 282   Node* cmp_le = new CmpINode(index, intcon(0));
 283   transform_later(cmp_le);
 284   BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
 285   Node* bol_le = new BoolNode(cmp_le, le_or_eq);
 286   transform_later(bol_le);
 287   Node* is_notp = generate_guard(ctrl, bol_le, nullptr, PROB_MIN);
 288 
 289   return is_notp;
 290 }
 291 











































 292 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
 293   transform_later(call);
 294 
 295   *ctrl = new ProjNode(call,TypeFunc::Control);
 296   transform_later(*ctrl);
 297   Node* newmem = new ProjNode(call, TypeFunc::Memory);
 298   transform_later(newmem);
 299 
 300   uint alias_idx = C->get_alias_index(adr_type);
 301   if (alias_idx != Compile::AliasIdxBot) {
 302     *mem = MergeMemNode::make(*mem);
 303     (*mem)->set_memory_at(alias_idx, newmem);
 304   } else {
 305     *mem = MergeMemNode::make(newmem);
 306   }
 307   transform_later(*mem);
 308 }
 309 
 310 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
 311                                               Node* src_offset,

 366 //       }
 367 //     }
 368 //     // adjust params for remaining work:
 369 //     if (slowval != -1) {
 370 //       n = -1^slowval; src_offset += n; dest_offset += n; length -= n
 371 //     }
 372 //   slow_region:
 373 //     call slow arraycopy(src, src_offset, dest, dest_offset, length)
 374 //     return  // via slow_call_path
 375 //
 376 // This routine is used from several intrinsics:  System.arraycopy,
 377 // Object.clone (the array subcase), and Arrays.copyOf[Range].
 378 //
 379 Node* PhaseMacroExpand::generate_arraycopy(ArrayCopyNode *ac, AllocateArrayNode* alloc,
 380                                            Node** ctrl, MergeMemNode* mem, Node** io,
 381                                            const TypePtr* adr_type,
 382                                            BasicType basic_elem_type,
 383                                            Node* src,  Node* src_offset,
 384                                            Node* dest, Node* dest_offset,
 385                                            Node* copy_length,

 386                                            bool disjoint_bases,
 387                                            bool length_never_negative,
 388                                            RegionNode* slow_region) {
 389   if (slow_region == nullptr) {
 390     slow_region = new RegionNode(1);
 391     transform_later(slow_region);
 392   }
 393 
 394   Node* original_dest = dest;
 395   bool  dest_needs_zeroing   = false;
 396   bool  acopy_to_uninitialized = false;


 397 
 398   // See if this is the initialization of a newly-allocated array.
 399   // If so, we will take responsibility here for initializing it to zero.
 400   // (Note:  Because tightly_coupled_allocation performs checks on the
 401   // out-edges of the dest, we need to avoid making derived pointers
 402   // from it until we have checked its uses.)
 403   if (ReduceBulkZeroing
 404       && !(UseTLAB && ZeroTLAB) // pointless if already zeroed
 405       && basic_elem_type != T_CONFLICT // avoid corner case
 406       && !src->eqv_uncast(dest)
 407       && alloc != nullptr
 408       && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0) {
 409     assert(ac->is_alloc_tightly_coupled(), "sanity");
 410     // acopy to uninitialized tightly coupled allocations
 411     // needs zeroing outside the copy range
 412     // and the acopy itself will be to uninitialized memory
 413     acopy_to_uninitialized = true;
 414     if (alloc->maybe_set_complete(&_igvn)) {
 415       // "You break it, you buy it."
 416       InitializeNode* init = alloc->initialization();
 417       assert(init->is_complete(), "we just did this");
 418       init->set_complete_with_arraycopy();
 419       assert(dest->is_CheckCastPP(), "sanity");
 420       assert(dest->in(0)->in(0) == init, "dest pinned");
 421       adr_type = TypeRawPtr::BOTTOM;  // all initializations are into raw memory
 422       // From this point on, every exit path is responsible for
 423       // initializing any non-copied parts of the object to zero.
 424       // Also, if this flag is set we make sure that arraycopy interacts properly
 425       // with G1, eliding pre-barriers. See CR 6627983.
 426       dest_needs_zeroing = true;


 427     } else {
 428       // dest_need_zeroing = false;
 429     }
 430   } else {
 431     // No zeroing elimination needed here.
 432     alloc                  = nullptr;
 433     acopy_to_uninitialized = false;
 434     //original_dest        = dest;
 435     //dest_needs_zeroing   = false;
 436   }
 437 
 438   uint alias_idx = C->get_alias_index(adr_type);
 439 
 440   // Results are placed here:
 441   enum { fast_path        = 1,  // normal void-returning assembly stub
 442          checked_path     = 2,  // special assembly stub with cleanup
 443          slow_call_path   = 3,  // something went wrong; call the VM
 444          zero_path        = 4,  // bypass when length of copy is zero
 445          bcopy_path       = 5,  // copy primitive array by 64-bit blocks
 446          PATH_LIMIT       = 6

 476     checked_i_o     = *io;
 477     checked_mem     = mem->memory_at(alias_idx);
 478     checked_value   = cv;
 479     *ctrl = top();
 480   }
 481 
 482   Node* not_pos = generate_nonpositive_guard(ctrl, copy_length, length_never_negative);
 483   if (not_pos != nullptr) {
 484     Node* local_ctrl = not_pos, *local_io = *io;
 485     MergeMemNode* local_mem = MergeMemNode::make(mem);
 486     transform_later(local_mem);
 487 
 488     // (6) length must not be negative.
 489     if (!length_never_negative) {
 490       generate_negative_guard(&local_ctrl, copy_length, slow_region);
 491     }
 492 
 493     // copy_length is 0.
 494     if (dest_needs_zeroing) {
 495       assert(!local_ctrl->is_top(), "no ctrl?");
 496       Node* dest_length = alloc->in(AllocateNode::ALength);
 497       if (copy_length->eqv_uncast(dest_length)
 498           || _igvn.find_int_con(dest_length, 1) <= 0) {
 499         // There is no zeroing to do. No need for a secondary raw memory barrier.
 500       } else {
 501         // Clear the whole thing since there are no source elements to copy.
 502         generate_clear_array(local_ctrl, local_mem,
 503                              adr_type, dest, basic_elem_type,


 504                              intcon(0), nullptr,
 505                              alloc->in(AllocateNode::AllocSize));
 506         // Use a secondary InitializeNode as raw memory barrier.
 507         // Currently it is needed only on this path since other
 508         // paths have stub or runtime calls as raw memory barriers.
 509         MemBarNode* mb = MemBarNode::make(C, Op_Initialize,
 510                                           Compile::AliasIdxRaw,
 511                                           top());
 512         transform_later(mb);
 513         mb->set_req(TypeFunc::Control,local_ctrl);
 514         mb->set_req(TypeFunc::Memory, local_mem->memory_at(Compile::AliasIdxRaw));
 515         local_ctrl = transform_later(new ProjNode(mb, TypeFunc::Control));
 516         local_mem->set_memory_at(Compile::AliasIdxRaw, transform_later(new ProjNode(mb, TypeFunc::Memory)));
 517 
 518         InitializeNode* init = mb->as_Initialize();
 519         init->set_complete(&_igvn);  // (there is no corresponding AllocateNode)
 520       }
 521     }
 522 
 523     // Present the results of the fast call.
 524     result_region->init_req(zero_path, local_ctrl);
 525     result_i_o   ->init_req(zero_path, local_io);
 526     result_memory->init_req(zero_path, local_mem->memory_at(alias_idx));
 527   }
 528 
 529   if (!(*ctrl)->is_top() && dest_needs_zeroing) {
 530     // We have to initialize the *uncopied* part of the array to zero.
 531     // The copy destination is the slice dest[off..off+len].  The other slices
 532     // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length].
 533     Node* dest_size   = alloc->in(AllocateNode::AllocSize);
 534     Node* dest_length = alloc->in(AllocateNode::ALength);
 535     Node* dest_tail   = transform_later( new AddINode(dest_offset, copy_length));
 536 
 537     // If there is a head section that needs zeroing, do it now.
 538     if (_igvn.find_int_con(dest_offset, -1) != 0) {
 539       generate_clear_array(*ctrl, mem,
 540                            adr_type, dest, basic_elem_type,


 541                            intcon(0), dest_offset,
 542                            nullptr);
 543     }
 544 
 545     // Next, perform a dynamic check on the tail length.
 546     // It is often zero, and we can win big if we prove this.
 547     // There are two wins:  Avoid generating the ClearArray
 548     // with its attendant messy index arithmetic, and upgrade
 549     // the copy to a more hardware-friendly word size of 64 bits.
 550     Node* tail_ctl = nullptr;
 551     if (!(*ctrl)->is_top() && !dest_tail->eqv_uncast(dest_length)) {
 552       Node* cmp_lt   = transform_later( new CmpINode(dest_tail, dest_length) );
 553       Node* bol_lt   = transform_later( new BoolNode(cmp_lt, BoolTest::lt) );
 554       tail_ctl = generate_slow_guard(ctrl, bol_lt, nullptr);
 555       assert(tail_ctl != nullptr || !(*ctrl)->is_top(), "must be an outcome");
 556     }
 557 
 558     // At this point, let's assume there is no tail.
 559     if (!(*ctrl)->is_top() && alloc != nullptr && basic_elem_type != T_OBJECT) {
 560       // There is no tail.  Try an upgrade to a 64-bit copy.

 569                                          src, src_offset, dest, dest_offset,
 570                                          dest_size, acopy_to_uninitialized);
 571         if (didit) {
 572           // Present the results of the block-copying fast call.
 573           result_region->init_req(bcopy_path, local_ctrl);
 574           result_i_o   ->init_req(bcopy_path, local_io);
 575           result_memory->init_req(bcopy_path, local_mem->memory_at(alias_idx));
 576         }
 577       }
 578       if (didit) {
 579         *ctrl = top();     // no regular fast path
 580       }
 581     }
 582 
 583     // Clear the tail, if any.
 584     if (tail_ctl != nullptr) {
 585       Node* notail_ctl = (*ctrl)->is_top() ? nullptr : *ctrl;
 586       *ctrl = tail_ctl;
 587       if (notail_ctl == nullptr) {
 588         generate_clear_array(*ctrl, mem,
 589                              adr_type, dest, basic_elem_type,


 590                              dest_tail, nullptr,
 591                              dest_size);
 592       } else {
 593         // Make a local merge.
 594         Node* done_ctl = transform_later(new RegionNode(3));
 595         Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type));
 596         done_ctl->init_req(1, notail_ctl);
 597         done_mem->init_req(1, mem->memory_at(alias_idx));
 598         generate_clear_array(*ctrl, mem,
 599                              adr_type, dest, basic_elem_type,


 600                              dest_tail, nullptr,
 601                              dest_size);
 602         done_ctl->init_req(2, *ctrl);
 603         done_mem->init_req(2, mem->memory_at(alias_idx));
 604         *ctrl = done_ctl;
 605         mem->set_memory_at(alias_idx, done_mem);
 606       }
 607     }
 608   }
 609 
 610   BasicType copy_type = basic_elem_type;
 611   assert(basic_elem_type != T_ARRAY, "caller must fix this");
 612   if (!(*ctrl)->is_top() && copy_type == T_OBJECT) {
 613     // If src and dest have compatible element types, we can copy bits.
 614     // Types S[] and D[] are compatible if D is a supertype of S.
 615     //
 616     // If they are not, we will use checked_oop_disjoint_arraycopy,
 617     // which performs a fast optimistic per-oop check, and backs off
 618     // further to JVM_ArrayCopy on the first per-oop check that fails.
 619     // (Actually, we don't move raw bits only; the GC requires card marks.)

 756       Node* length_minus  = new SubINode(copy_length, slow_offset);
 757       transform_later(length_minus);
 758 
 759       // Tweak the node variables to adjust the code produced below:
 760       src_offset  = src_off_plus;
 761       dest_offset = dest_off_plus;
 762       copy_length = length_minus;
 763     }
 764   }
 765   *ctrl = slow_control;
 766   if (!(*ctrl)->is_top()) {
 767     Node* local_ctrl = *ctrl, *local_io = slow_i_o;
 768     MergeMemNode* local_mem = MergeMemNode::make(mem);
 769     transform_later(local_mem);
 770 
 771     // Generate the slow path, if needed.
 772     local_mem->set_memory_at(alias_idx, slow_mem);
 773 
 774     if (dest_needs_zeroing) {
 775       generate_clear_array(local_ctrl, local_mem,
 776                            adr_type, dest, basic_elem_type,


 777                            intcon(0), nullptr,
 778                            alloc->in(AllocateNode::AllocSize));
 779     }
 780 
 781     local_mem = generate_slow_arraycopy(ac,
 782                                         &local_ctrl, local_mem, &local_io,
 783                                         adr_type,
 784                                         src, src_offset, dest, dest_offset,
 785                                         copy_length, /*dest_uninitialized*/false);
 786 
 787     result_region->init_req(slow_call_path, local_ctrl);
 788     result_i_o   ->init_req(slow_call_path, local_io);
 789     result_memory->init_req(slow_call_path, local_mem->memory_at(alias_idx));
 790   } else {
 791     ShouldNotReachHere(); // no call to generate_slow_arraycopy:
 792                           // projections were not extracted
 793   }
 794 
 795   // Remove unused edges.
 796   for (uint i = 1; i < result_region->req(); i++) {

 825     // a subsequent store that would make this object accessible by
 826     // other threads.
 827     assert(ac->_dest_type == TypeOopPtr::BOTTOM, "non escaping destination shouldn't have narrow slice");
 828     insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore, Compile::AliasIdxBot);
 829   } else {
 830     int mem_bar_alias_idx = Compile::AliasIdxBot;
 831     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
 832       // The graph was transformed under the assumption the ArrayCopy node only had an effect on a narrow slice. We can't
 833       // insert a wide membar now that it's being expanded: a load that uses the input memory state of the ArrayCopy
 834       // could then become anti dependent on the membar when it was not anti dependent on the ArrayCopy leading to a
 835       // broken graph.
 836       mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
 837     }
 838     insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
 839   }
 840 
 841   assert((*ctrl)->is_Proj(), "MemBar control projection");
 842   assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node");
 843   (*ctrl)->in(0)->isa_MemBar()->set_trailing_expanded_array_copy();
 844 
 845   _igvn.replace_node(_callprojs.fallthrough_memproj, out_mem);
 846   if (_callprojs.fallthrough_ioproj != nullptr) {
 847     _igvn.replace_node(_callprojs.fallthrough_ioproj, *io);
 848   }
 849   _igvn.replace_node(_callprojs.fallthrough_catchproj, *ctrl);
 850 
 851 #ifdef ASSERT
 852   const TypeOopPtr* dest_t = _igvn.type(dest)->is_oopptr();
 853   if (dest_t->is_known_instance()) {
 854     ArrayCopyNode* ac = nullptr;
 855     assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost");
 856     assert(ac == nullptr, "no arraycopy anymore");
 857   }
 858 #endif
 859 
 860   return out_mem;
 861 }
 862 
 863 // Helper for initialization of arrays, creating a ClearArray.
 864 // It writes zero bits in [start..end), within the body of an array object.
 865 // The memory effects are all chained onto the 'adr_type' alias category.
 866 //
 867 // Since the object is otherwise uninitialized, we are free
 868 // to put a little "slop" around the edges of the cleared area,
 869 // as long as it does not go back into the array's header,
 870 // or beyond the array end within the heap.
 871 //
 872 // The lower edge can be rounded down to the nearest jint and the
 873 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
 874 //
 875 // Arguments:
 876 //   adr_type           memory slice where writes are generated
 877 //   dest               oop of the destination array
 878 //   basic_elem_type    element type of the destination
 879 //   slice_idx          array index of first element to store
 880 //   slice_len          number of elements to store (or null)
 881 //   dest_size          total size in bytes of the array object
 882 //
 883 // Exactly one of slice_len or dest_size must be non-null.
 884 // If dest_size is non-null, zeroing extends to the end of the object.
 885 // If slice_len is non-null, the slice_idx value must be a constant.
 886 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
 887                                             const TypePtr* adr_type,
 888                                             Node* dest,


 889                                             BasicType basic_elem_type,
 890                                             Node* slice_idx,
 891                                             Node* slice_len,
 892                                             Node* dest_size) {
 893   // one or the other but not both of slice_len and dest_size:
 894   assert((slice_len != nullptr? 1: 0) + (dest_size != nullptr? 1: 0) == 1, "");
 895   if (slice_len == nullptr)  slice_len = top();
 896   if (dest_size == nullptr)  dest_size = top();
 897 
 898   uint alias_idx = C->get_alias_index(adr_type);
 899 
 900   // operate on this memory slice:
 901   Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on
 902 
 903   // scaling and rounding of indexes:
 904   int scale = exact_log2(type2aelembytes(basic_elem_type));
 905   int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
 906   int clear_low = (-1 << scale) & (BytesPerInt  - 1);
 907   int bump_bit  = (-1 << scale) & BytesPerInt;
 908 
 909   // determine constant starts and ends
 910   const intptr_t BIG_NEG = -128;
 911   assert(BIG_NEG + 2*abase < 0, "neg enough");
 912   intptr_t slice_idx_con = (intptr_t) _igvn.find_int_con(slice_idx, BIG_NEG);
 913   intptr_t slice_len_con = (intptr_t) _igvn.find_int_con(slice_len, BIG_NEG);
 914   if (slice_len_con == 0) {
 915     return;                     // nothing to do here
 916   }
 917   intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low;
 918   intptr_t end_con   = _igvn.find_intptr_t_con(dest_size, -1);
 919   if (slice_idx_con >= 0 && slice_len_con >= 0) {
 920     assert(end_con < 0, "not two cons");
 921     end_con = align_up(abase + ((slice_idx_con + slice_len_con) << scale),
 922                        BytesPerLong);
 923   }
 924 
 925   if (start_con >= 0 && end_con >= 0) {
 926     // Constant start and end.  Simple.
 927     mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
 928                                        start_con, end_con, &_igvn);
 929   } else if (start_con >= 0 && dest_size != top()) {
 930     // Constant start, pre-rounded end after the tail of the array.
 931     Node* end = dest_size;
 932     mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
 933                                        start_con, end, &_igvn);
 934   } else if (start_con >= 0 && slice_len != top()) {
 935     // Constant start, non-constant end.  End needs rounding up.
 936     // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8)
 937     intptr_t end_base  = abase + (slice_idx_con << scale);
 938     int      end_round = (-1 << scale) & (BytesPerLong  - 1);
 939     Node*    end       = ConvI2X(slice_len);
 940     if (scale != 0)
 941       end = transform_later(new LShiftXNode(end, intcon(scale) ));
 942     end_base += end_round;
 943     end = transform_later(new AddXNode(end, MakeConX(end_base)) );
 944     end = transform_later(new AndXNode(end, MakeConX(~end_round)) );
 945     mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
 946                                        start_con, end, &_igvn);
 947   } else if (start_con < 0 && dest_size != top()) {
 948     // Non-constant start, pre-rounded end after the tail of the array.
 949     // This is almost certainly a "round-to-end" operation.
 950     Node* start = slice_idx;
 951     start = ConvI2X(start);
 952     if (scale != 0)
 953       start = transform_later(new LShiftXNode( start, intcon(scale) ));
 954     start = transform_later(new AddXNode(start, MakeConX(abase)) );
 955     if ((bump_bit | clear_low) != 0) {
 956       int to_clear = (bump_bit | clear_low);
 957       // Align up mod 8, then store a jint zero unconditionally
 958       // just before the mod-8 boundary.
 959       if (((abase + bump_bit) & ~to_clear) - bump_bit
 960           < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) {
 961         bump_bit = 0;
 962         assert((abase & to_clear) == 0, "array base must be long-aligned");
 963       } else {
 964         // Bump 'start' up to (or past) the next jint boundary:
 965         start = transform_later( new AddXNode(start, MakeConX(bump_bit)) );
 966         assert((abase & clear_low) == 0, "array base must be int-aligned");
 967       }
 968       // Round bumped 'start' down to jlong boundary in body of array.
 969       start = transform_later(new AndXNode(start, MakeConX(~to_clear)) );
 970       if (bump_bit != 0) {
 971         // Store a zero to the immediately preceding jint:
 972         Node* x1 = transform_later(new AddXNode(start, MakeConX(-bump_bit)) );
 973         Node* p1 = basic_plus_adr(dest, x1);
 974         mem = StoreNode::make(_igvn, ctrl, mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered);






 975         mem = transform_later(mem);
 976       }
 977     }
 978     Node* end = dest_size; // pre-rounded
 979     mem = ClearArrayNode::clear_memory(ctrl, mem, dest,
 980                                        start, end, &_igvn);
 981   } else {
 982     // Non-constant start, unrounded non-constant end.
 983     // (Nobody zeroes a random midsection of an array using this routine.)
 984     ShouldNotReachHere();       // fix caller
 985   }
 986 
 987   // Done.
 988   merge_mem->set_memory_at(alias_idx, mem);
 989 }
 990 
 991 bool PhaseMacroExpand::generate_block_arraycopy(Node** ctrl, MergeMemNode** mem, Node* io,
 992                                                 const TypePtr* adr_type,
 993                                                 BasicType basic_elem_type,
 994                                                 AllocateNode* alloc,
 995                                                 Node* src,  Node* src_offset,
 996                                                 Node* dest, Node* dest_offset,
 997                                                 Node* dest_size, bool dest_uninitialized) {
 998   // See if there is an advantage from block transfer.
 999   int scale = exact_log2(type2aelembytes(basic_elem_type));

1075   const TypeFunc* call_type = OptoRuntime::slow_arraycopy_Type();
1076   CallNode* call = new CallStaticJavaNode(call_type, OptoRuntime::slow_arraycopy_Java(),
1077                                           "slow_arraycopy", TypePtr::BOTTOM);
1078 
1079   call->init_req(TypeFunc::Control, *ctrl);
1080   call->init_req(TypeFunc::I_O    , *io);
1081   call->init_req(TypeFunc::Memory , mem);
1082   call->init_req(TypeFunc::ReturnAdr, top());
1083   call->init_req(TypeFunc::FramePtr, top());
1084   call->init_req(TypeFunc::Parms+0, src);
1085   call->init_req(TypeFunc::Parms+1, src_offset);
1086   call->init_req(TypeFunc::Parms+2, dest);
1087   call->init_req(TypeFunc::Parms+3, dest_offset);
1088   call->init_req(TypeFunc::Parms+4, copy_length);
1089   call->copy_call_debug_info(&_igvn, ac);
1090 
1091   call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
1092   _igvn.replace_node(ac, call);
1093   transform_later(call);
1094 
1095   call->extract_projections(&_callprojs, false /*separate_io_proj*/, false /*do_asserts*/);
1096   *ctrl = _callprojs.fallthrough_catchproj->clone();
1097   transform_later(*ctrl);
1098 
1099   Node* m = _callprojs.fallthrough_memproj->clone();
1100   transform_later(m);
1101 
1102   uint alias_idx = C->get_alias_index(adr_type);
1103   MergeMemNode* out_mem;
1104   if (alias_idx != Compile::AliasIdxBot) {
1105     out_mem = MergeMemNode::make(mem);
1106     out_mem->set_memory_at(alias_idx, m);
1107   } else {
1108     out_mem = MergeMemNode::make(m);
1109   }
1110   transform_later(out_mem);
1111 
1112   // When src is negative and arraycopy is before an infinite loop,_callprojs.fallthrough_ioproj
1113   // could be null. Skip clone and update null fallthrough_ioproj.
1114   if (_callprojs.fallthrough_ioproj != nullptr) {
1115     *io = _callprojs.fallthrough_ioproj->clone();
1116     transform_later(*io);
1117   } else {
1118     *io = nullptr;
1119   }
1120 
1121   return out_mem;
1122 }
1123 
1124 // Helper function; generates code for cases requiring runtime checks.
1125 Node* PhaseMacroExpand::generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
1126                                                      const TypePtr* adr_type,
1127                                                      Node* dest_elem_klass,
1128                                                      Node* src,  Node* src_offset,
1129                                                      Node* dest, Node* dest_offset,
1130                                                      Node* copy_length, bool dest_uninitialized) {
1131   if ((*ctrl)->is_top())  return nullptr;
1132 
1133   address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized);
1134   if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path.
1135     return nullptr;

1227 
1228   // Connecting remaining edges for exit_block coming from stub_block.
1229   if (exit_block) {
1230     exit_block->init_req(2, *ctrl);
1231 
1232     // Memory edge corresponding to stub_region.
1233     result_memory->init_req(2, *mem);
1234 
1235     uint alias_idx = C->get_alias_index(adr_type);
1236     if (alias_idx != Compile::AliasIdxBot) {
1237       *mem = MergeMemNode::make(*mem);
1238       (*mem)->set_memory_at(alias_idx, result_memory);
1239     } else {
1240       *mem = MergeMemNode::make(result_memory);
1241     }
1242     transform_later(*mem);
1243     *ctrl = exit_block;
1244   }
1245 }
1246 




































1247 #undef XTOP
1248 
1249 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
1250   Node* ctrl = ac->in(TypeFunc::Control);
1251   Node* io = ac->in(TypeFunc::I_O);
1252   Node* src = ac->in(ArrayCopyNode::Src);
1253   Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
1254   Node* dest = ac->in(ArrayCopyNode::Dest);
1255   Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
1256   Node* length = ac->in(ArrayCopyNode::Length);
1257   MergeMemNode* merge_mem = nullptr;
1258 
1259   if (ac->is_clonebasic()) {
1260     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1261     bs->clone_at_expansion(this, ac);
1262     return;
1263   } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) {
1264     Node* mem = ac->in(TypeFunc::Memory);
1265     merge_mem = MergeMemNode::make(mem);
1266     transform_later(merge_mem);













1267 
1268     AllocateArrayNode* alloc = nullptr;

1269     if (ac->is_alloc_tightly_coupled()) {
1270       alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1271       assert(alloc != nullptr, "expect alloc");

1272     }
1273 
1274     const TypePtr* adr_type = _igvn.type(dest)->is_oopptr()->add_offset(Type::OffsetBot);
1275     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1276       adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();

















1277     }



1278     generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1279                        adr_type, T_OBJECT,
1280                        src, src_offset, dest, dest_offset, length,

1281                        true, ac->has_negative_length_guard());
1282 
1283     return;
1284   }
1285 
1286   AllocateArrayNode* alloc = nullptr;
1287   if (ac->is_alloc_tightly_coupled()) {
1288     alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1289     assert(alloc != nullptr, "expect alloc");
1290   }
1291 
1292   assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1293 
1294   // Compile time checks.  If any of these checks cannot be verified at compile time,
1295   // we do not make a fast path for this call.  Instead, we let the call remain as it
1296   // is.  The checks we choose to mandate at compile time are:
1297   //
1298   // (1) src and dest are arrays.
1299   const Type* src_type = src->Value(&_igvn);
1300   const Type* dest_type = dest->Value(&_igvn);
1301   const TypeAryPtr* top_src = src_type->isa_aryptr();
1302   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1303 
1304   BasicType src_elem = T_CONFLICT;
1305   BasicType dest_elem = T_CONFLICT;
1306 
1307   if (top_src != nullptr && top_src->elem() != Type::BOTTOM) {
1308     src_elem = top_src->elem()->array_element_basic_type();
1309   }
1310   if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1311     dest_elem = top_dest->elem()->array_element_basic_type();
1312   }
1313   if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
1314   if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1315 
1316   if (ac->is_arraycopy_validated() &&
1317       dest_elem != T_CONFLICT &&
1318       src_elem == T_CONFLICT) {
1319     src_elem = dest_elem;
1320   }
1321 
1322   if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) {
1323     // Conservatively insert a memory barrier on all memory slices.
1324     // Do not let writes into the source float below the arraycopy.
1325     {
1326       Node* mem = ac->in(TypeFunc::Memory);
1327       insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, Compile::AliasIdxBot);
1328 
1329       merge_mem = MergeMemNode::make(mem);
1330       transform_later(merge_mem);
1331     }
1332 
1333     // Call StubRoutines::generic_arraycopy stub.
1334     Node* mem = generate_arraycopy(ac, nullptr, &ctrl, merge_mem, &io,
1335                                    TypeRawPtr::BOTTOM, T_CONFLICT,
1336                                    src, src_offset, dest, dest_offset, length,
1337                                    // If a  negative length guard was generated for the ArrayCopyNode,
1338                                    // the length of the array can never be negative.
1339                                    false, ac->has_negative_length_guard());

1340     return;
1341   }
1342 
1343   assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");
1344 
1345   // (2) src and dest arrays must have elements of the same BasicType
1346   // Figure out the size and type of the elements we will be copying.
1347   if (src_elem != dest_elem || dest_elem == T_VOID) {







1348     // The component types are not the same or are not recognized.  Punt.
1349     // (But, avoid the native method wrapper to JVM_ArrayCopy.)
1350     {
1351       Node* mem = ac->in(TypeFunc::Memory);
1352       merge_mem = generate_slow_arraycopy(ac, &ctrl, mem, &io, TypePtr::BOTTOM, src, src_offset, dest, dest_offset, length, false);
1353     }
1354 
1355     _igvn.replace_node(_callprojs.fallthrough_memproj, merge_mem);
1356     if (_callprojs.fallthrough_ioproj != nullptr) {
1357       _igvn.replace_node(_callprojs.fallthrough_ioproj, io);
1358     }
1359     _igvn.replace_node(_callprojs.fallthrough_catchproj, ctrl);
1360     return;
1361   }
1362 
1363   //---------------------------------------------------------------------------
1364   // We will make a fast path for this call to arraycopy.
1365 
1366   // We have the following tests left to perform:
1367   //
1368   // (3) src and dest must not be null.
1369   // (4) src_offset must not be negative.
1370   // (5) dest_offset must not be negative.
1371   // (6) length must not be negative.
1372   // (7) src_offset + length must not exceed length of src.
1373   // (8) dest_offset + length must not exceed length of dest.
1374   // (9) each element of an oop array must be assignable
1375 
1376   {
1377     Node* mem = ac->in(TypeFunc::Memory);
1378     merge_mem = MergeMemNode::make(mem);
1379     transform_later(merge_mem);





1380   }


1381 
1382   RegionNode* slow_region = new RegionNode(1);
1383   transform_later(slow_region);
1384 
1385   if (!ac->is_arraycopy_validated()) {
1386     // (3) operands must not be null
1387     // We currently perform our null checks with the null_check routine.
1388     // This means that the null exceptions will be reported in the caller
1389     // rather than (correctly) reported inside of the native arraycopy call.
1390     // This should be corrected, given time.  We do our null check with the
1391     // stack pointer restored.
1392     // null checks done library_call.cpp
1393 
1394     // (4) src_offset must not be negative.
1395     generate_negative_guard(&ctrl, src_offset, slow_region);
1396 
1397     // (5) dest_offset must not be negative.
1398     generate_negative_guard(&ctrl, dest_offset, slow_region);
1399 
1400     // (6) length must not be negative (moved to generate_arraycopy()).
1401     // generate_negative_guard(length, slow_region);
1402 
1403     // (7) src_offset + length must not exceed length of src.
1404     Node* alen = ac->in(ArrayCopyNode::SrcLen);
1405     assert(alen != nullptr, "need src len");
1406     generate_limit_guard(&ctrl,
1407                          src_offset, length,
1408                          alen,
1409                          slow_region);
1410 
1411     // (8) dest_offset + length must not exceed length of dest.
1412     alen = ac->in(ArrayCopyNode::DestLen);
1413     assert(alen != nullptr, "need dest len");
1414     generate_limit_guard(&ctrl,
1415                          dest_offset, length,
1416                          alen,
1417                          slow_region);
1418 
1419     // (9) each element of an oop array must be assignable
1420     // The generate_arraycopy subroutine checks this.






















1421   }

1422   // This is where the memory effects are placed:
1423   const TypePtr* adr_type = nullptr;
1424   if (ac->_dest_type != TypeOopPtr::BOTTOM) {




1425     adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1426   } else {
1427     adr_type = TypeAryPtr::get_array_body_type(dest_elem);
1428   }
1429 
1430   generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1431                      adr_type, dest_elem,
1432                      src, src_offset, dest, dest_offset, length,

1433                      // If a  negative length guard was generated for the ArrayCopyNode,
1434                      // the length of the array can never be negative.
1435                      false, ac->has_negative_length_guard(), slow_region);

1436 }

   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) {
  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);
  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(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

 282 
 283   *ctrl = stub_block;
 284 }
 285 
 286 
 287 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) {
 288   if ((*ctrl)->is_top())  return nullptr;
 289 
 290   if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
 291     return nullptr;                // index is already adequately typed
 292   Node* cmp_le = new CmpINode(index, intcon(0));
 293   transform_later(cmp_le);
 294   BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
 295   Node* bol_le = new BoolNode(cmp_le, le_or_eq);
 296   transform_later(bol_le);
 297   Node* is_notp = generate_guard(ctrl, bol_le, nullptr, PROB_MIN);
 298 
 299   return is_notp;
 300 }
 301 
 302 Node* PhaseMacroExpand::mark_word_test(Node** ctrl, Node* obj, MergeMemNode* mem, uintptr_t mask_val, RegionNode* region) {
 303   // Load markword and check if obj is locked
 304   Node* mark = make_load(nullptr, mem->memory_at(Compile::AliasIdxRaw), obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
 305   Node* locked_bit = MakeConX(markWord::unlocked_value);
 306   locked_bit = transform_later(new AndXNode(locked_bit, mark));
 307   Node* cmp = transform_later(new CmpXNode(locked_bit, MakeConX(0)));
 308   Node* is_unlocked = transform_later(new BoolNode(cmp, BoolTest::ne));
 309   IfNode* iff = transform_later(new IfNode(*ctrl, is_unlocked, PROB_MAX, COUNT_UNKNOWN))->as_If();
 310   Node* locked_region = transform_later(new RegionNode(3));
 311   Node* mark_phi = transform_later(new PhiNode(locked_region, TypeX_X));
 312 
 313   // Unlocked: Use bits from mark word
 314   locked_region->init_req(1, transform_later(new IfTrueNode(iff)));
 315   mark_phi->init_req(1, mark);
 316 
 317   // Locked: Load prototype header from klass
 318   *ctrl = transform_later(new IfFalseNode(iff));
 319   // Make loads control dependent to make sure they are only executed if array is locked
 320   Node* klass_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());
 321   Node* klass = transform_later(LoadKlassNode::make(_igvn, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
 322   Node* proto_adr = basic_plus_adr(klass, in_bytes(Klass::prototype_header_offset()));
 323   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));
 324 
 325   locked_region->init_req(2, *ctrl);
 326   mark_phi->init_req(2, proto);
 327   *ctrl = locked_region;
 328 
 329   // Now check if mark word bits are set
 330   Node* mask = MakeConX(mask_val);
 331   Node* masked = transform_later(new AndXNode(mark_phi, mask));
 332   cmp = transform_later(new CmpXNode(masked, mask));
 333   Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq));
 334   return generate_fair_guard(ctrl, bol, region);
 335 }
 336 
 337 Node* PhaseMacroExpand::generate_flat_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
 338   return mark_word_test(ctrl, array, mem, markWord::flat_array_bit_in_place, region);
 339 }
 340 
 341 Node* PhaseMacroExpand::generate_null_free_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
 342   return mark_word_test(ctrl, array, mem, markWord::null_free_array_bit_in_place, region);
 343 }
 344 
 345 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
 346   transform_later(call);
 347 
 348   *ctrl = new ProjNode(call,TypeFunc::Control);
 349   transform_later(*ctrl);
 350   Node* newmem = new ProjNode(call, TypeFunc::Memory);
 351   transform_later(newmem);
 352 
 353   uint alias_idx = C->get_alias_index(adr_type);
 354   if (alias_idx != Compile::AliasIdxBot) {
 355     *mem = MergeMemNode::make(*mem);
 356     (*mem)->set_memory_at(alias_idx, newmem);
 357   } else {
 358     *mem = MergeMemNode::make(newmem);
 359   }
 360   transform_later(*mem);
 361 }
 362 
 363 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
 364                                               Node* src_offset,

 419 //       }
 420 //     }
 421 //     // adjust params for remaining work:
 422 //     if (slowval != -1) {
 423 //       n = -1^slowval; src_offset += n; dest_offset += n; length -= n
 424 //     }
 425 //   slow_region:
 426 //     call slow arraycopy(src, src_offset, dest, dest_offset, length)
 427 //     return  // via slow_call_path
 428 //
 429 // This routine is used from several intrinsics:  System.arraycopy,
 430 // Object.clone (the array subcase), and Arrays.copyOf[Range].
 431 //
 432 Node* PhaseMacroExpand::generate_arraycopy(ArrayCopyNode *ac, AllocateArrayNode* alloc,
 433                                            Node** ctrl, MergeMemNode* mem, Node** io,
 434                                            const TypePtr* adr_type,
 435                                            BasicType basic_elem_type,
 436                                            Node* src,  Node* src_offset,
 437                                            Node* dest, Node* dest_offset,
 438                                            Node* copy_length,
 439                                            Node* dest_length,
 440                                            bool disjoint_bases,
 441                                            bool length_never_negative,
 442                                            RegionNode* slow_region) {
 443   if (slow_region == nullptr) {
 444     slow_region = new RegionNode(1);
 445     transform_later(slow_region);
 446   }
 447 
 448   Node* original_dest = dest;
 449   bool  dest_needs_zeroing   = false;
 450   bool  acopy_to_uninitialized = false;
 451   Node* init_value = nullptr;
 452   Node* raw_init_value = nullptr;
 453 
 454   // See if this is the initialization of a newly-allocated array.
 455   // If so, we will take responsibility here for initializing it to zero.
 456   // (Note:  Because tightly_coupled_allocation performs checks on the
 457   // out-edges of the dest, we need to avoid making derived pointers
 458   // from it until we have checked its uses.)
 459   if (ReduceBulkZeroing
 460       && !(UseTLAB && ZeroTLAB) // pointless if already zeroed
 461       && basic_elem_type != T_CONFLICT // avoid corner case
 462       && !src->eqv_uncast(dest)
 463       && alloc != nullptr
 464       && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0) {
 465     assert(ac->is_alloc_tightly_coupled(), "sanity");
 466     // acopy to uninitialized tightly coupled allocations
 467     // needs zeroing outside the copy range
 468     // and the acopy itself will be to uninitialized memory
 469     acopy_to_uninitialized = true;
 470     if (alloc->maybe_set_complete(&_igvn)) {
 471       // "You break it, you buy it."
 472       InitializeNode* init = alloc->initialization();
 473       assert(init->is_complete(), "we just did this");
 474       init->set_complete_with_arraycopy();
 475       assert(dest->is_CheckCastPP(), "sanity");
 476       assert(dest->in(0)->in(0) == init, "dest pinned");
 477       adr_type = TypeRawPtr::BOTTOM;  // all initializations are into raw memory
 478       // From this point on, every exit path is responsible for
 479       // initializing any non-copied parts of the object to zero.
 480       // Also, if this flag is set we make sure that arraycopy interacts properly
 481       // with G1, eliding pre-barriers. See CR 6627983.
 482       dest_needs_zeroing = true;
 483       init_value = alloc->in(AllocateNode::InitValue);
 484       raw_init_value = alloc->in(AllocateNode::RawInitValue);
 485     } else {
 486       // dest_need_zeroing = false;
 487     }
 488   } else {
 489     // No zeroing elimination needed here.
 490     alloc                  = nullptr;
 491     acopy_to_uninitialized = false;
 492     //original_dest        = dest;
 493     //dest_needs_zeroing   = false;
 494   }
 495 
 496   uint alias_idx = C->get_alias_index(adr_type);
 497 
 498   // Results are placed here:
 499   enum { fast_path        = 1,  // normal void-returning assembly stub
 500          checked_path     = 2,  // special assembly stub with cleanup
 501          slow_call_path   = 3,  // something went wrong; call the VM
 502          zero_path        = 4,  // bypass when length of copy is zero
 503          bcopy_path       = 5,  // copy primitive array by 64-bit blocks
 504          PATH_LIMIT       = 6

 534     checked_i_o     = *io;
 535     checked_mem     = mem->memory_at(alias_idx);
 536     checked_value   = cv;
 537     *ctrl = top();
 538   }
 539 
 540   Node* not_pos = generate_nonpositive_guard(ctrl, copy_length, length_never_negative);
 541   if (not_pos != nullptr) {
 542     Node* local_ctrl = not_pos, *local_io = *io;
 543     MergeMemNode* local_mem = MergeMemNode::make(mem);
 544     transform_later(local_mem);
 545 
 546     // (6) length must not be negative.
 547     if (!length_never_negative) {
 548       generate_negative_guard(&local_ctrl, copy_length, slow_region);
 549     }
 550 
 551     // copy_length is 0.
 552     if (dest_needs_zeroing) {
 553       assert(!local_ctrl->is_top(), "no ctrl?");

 554       if (copy_length->eqv_uncast(dest_length)
 555           || _igvn.find_int_con(dest_length, 1) <= 0) {
 556         // There is no zeroing to do. No need for a secondary raw memory barrier.
 557       } else {
 558         // Clear the whole thing since there are no source elements to copy.
 559         generate_clear_array(local_ctrl, local_mem,
 560                              adr_type, dest,
 561                              init_value, raw_init_value,
 562                              basic_elem_type,
 563                              intcon(0), nullptr,
 564                              alloc->in(AllocateNode::AllocSize));
 565         // Use a secondary InitializeNode as raw memory barrier.
 566         // Currently it is needed only on this path since other
 567         // paths have stub or runtime calls as raw memory barriers.
 568         MemBarNode* mb = MemBarNode::make(C, Op_Initialize,
 569                                           Compile::AliasIdxRaw,
 570                                           top());
 571         transform_later(mb);
 572         mb->set_req(TypeFunc::Control,local_ctrl);
 573         mb->set_req(TypeFunc::Memory, local_mem->memory_at(Compile::AliasIdxRaw));
 574         local_ctrl = transform_later(new ProjNode(mb, TypeFunc::Control));
 575         local_mem->set_memory_at(Compile::AliasIdxRaw, transform_later(new ProjNode(mb, TypeFunc::Memory)));
 576 
 577         InitializeNode* init = mb->as_Initialize();
 578         init->set_complete(&_igvn);  // (there is no corresponding AllocateNode)
 579       }
 580     }
 581 
 582     // Present the results of the fast call.
 583     result_region->init_req(zero_path, local_ctrl);
 584     result_i_o   ->init_req(zero_path, local_io);
 585     result_memory->init_req(zero_path, local_mem->memory_at(alias_idx));
 586   }
 587 
 588   if (!(*ctrl)->is_top() && dest_needs_zeroing) {
 589     // We have to initialize the *uncopied* part of the array to zero.
 590     // The copy destination is the slice dest[off..off+len].  The other slices
 591     // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length].
 592     Node* dest_size   = alloc->in(AllocateNode::AllocSize);

 593     Node* dest_tail   = transform_later( new AddINode(dest_offset, copy_length));
 594 
 595     // If there is a head section that needs zeroing, do it now.
 596     if (_igvn.find_int_con(dest_offset, -1) != 0) {
 597       generate_clear_array(*ctrl, mem,
 598                            adr_type, dest,
 599                            init_value, raw_init_value,
 600                            basic_elem_type,
 601                            intcon(0), dest_offset,
 602                            nullptr);
 603     }
 604 
 605     // Next, perform a dynamic check on the tail length.
 606     // It is often zero, and we can win big if we prove this.
 607     // There are two wins:  Avoid generating the ClearArray
 608     // with its attendant messy index arithmetic, and upgrade
 609     // the copy to a more hardware-friendly word size of 64 bits.
 610     Node* tail_ctl = nullptr;
 611     if (!(*ctrl)->is_top() && !dest_tail->eqv_uncast(dest_length)) {
 612       Node* cmp_lt   = transform_later( new CmpINode(dest_tail, dest_length) );
 613       Node* bol_lt   = transform_later( new BoolNode(cmp_lt, BoolTest::lt) );
 614       tail_ctl = generate_slow_guard(ctrl, bol_lt, nullptr);
 615       assert(tail_ctl != nullptr || !(*ctrl)->is_top(), "must be an outcome");
 616     }
 617 
 618     // At this point, let's assume there is no tail.
 619     if (!(*ctrl)->is_top() && alloc != nullptr && basic_elem_type != T_OBJECT) {
 620       // There is no tail.  Try an upgrade to a 64-bit copy.

 629                                          src, src_offset, dest, dest_offset,
 630                                          dest_size, acopy_to_uninitialized);
 631         if (didit) {
 632           // Present the results of the block-copying fast call.
 633           result_region->init_req(bcopy_path, local_ctrl);
 634           result_i_o   ->init_req(bcopy_path, local_io);
 635           result_memory->init_req(bcopy_path, local_mem->memory_at(alias_idx));
 636         }
 637       }
 638       if (didit) {
 639         *ctrl = top();     // no regular fast path
 640       }
 641     }
 642 
 643     // Clear the tail, if any.
 644     if (tail_ctl != nullptr) {
 645       Node* notail_ctl = (*ctrl)->is_top() ? nullptr : *ctrl;
 646       *ctrl = tail_ctl;
 647       if (notail_ctl == nullptr) {
 648         generate_clear_array(*ctrl, mem,
 649                              adr_type, dest,
 650                              init_value, raw_init_value,
 651                              basic_elem_type,
 652                              dest_tail, nullptr,
 653                              dest_size);
 654       } else {
 655         // Make a local merge.
 656         Node* done_ctl = transform_later(new RegionNode(3));
 657         Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type));
 658         done_ctl->init_req(1, notail_ctl);
 659         done_mem->init_req(1, mem->memory_at(alias_idx));
 660         generate_clear_array(*ctrl, mem,
 661                              adr_type, dest,
 662                              init_value, raw_init_value,
 663                              basic_elem_type,
 664                              dest_tail, nullptr,
 665                              dest_size);
 666         done_ctl->init_req(2, *ctrl);
 667         done_mem->init_req(2, mem->memory_at(alias_idx));
 668         *ctrl = done_ctl;
 669         mem->set_memory_at(alias_idx, done_mem);
 670       }
 671     }
 672   }
 673 
 674   BasicType copy_type = basic_elem_type;
 675   assert(basic_elem_type != T_ARRAY, "caller must fix this");
 676   if (!(*ctrl)->is_top() && copy_type == T_OBJECT) {
 677     // If src and dest have compatible element types, we can copy bits.
 678     // Types S[] and D[] are compatible if D is a supertype of S.
 679     //
 680     // If they are not, we will use checked_oop_disjoint_arraycopy,
 681     // which performs a fast optimistic per-oop check, and backs off
 682     // further to JVM_ArrayCopy on the first per-oop check that fails.
 683     // (Actually, we don't move raw bits only; the GC requires card marks.)

 820       Node* length_minus  = new SubINode(copy_length, slow_offset);
 821       transform_later(length_minus);
 822 
 823       // Tweak the node variables to adjust the code produced below:
 824       src_offset  = src_off_plus;
 825       dest_offset = dest_off_plus;
 826       copy_length = length_minus;
 827     }
 828   }
 829   *ctrl = slow_control;
 830   if (!(*ctrl)->is_top()) {
 831     Node* local_ctrl = *ctrl, *local_io = slow_i_o;
 832     MergeMemNode* local_mem = MergeMemNode::make(mem);
 833     transform_later(local_mem);
 834 
 835     // Generate the slow path, if needed.
 836     local_mem->set_memory_at(alias_idx, slow_mem);
 837 
 838     if (dest_needs_zeroing) {
 839       generate_clear_array(local_ctrl, local_mem,
 840                            adr_type, dest,
 841                            init_value, raw_init_value,
 842                            basic_elem_type,
 843                            intcon(0), nullptr,
 844                            alloc->in(AllocateNode::AllocSize));
 845     }
 846 
 847     local_mem = generate_slow_arraycopy(ac,
 848                                         &local_ctrl, local_mem, &local_io,
 849                                         adr_type,
 850                                         src, src_offset, dest, dest_offset,
 851                                         copy_length, /*dest_uninitialized*/false);
 852 
 853     result_region->init_req(slow_call_path, local_ctrl);
 854     result_i_o   ->init_req(slow_call_path, local_io);
 855     result_memory->init_req(slow_call_path, local_mem->memory_at(alias_idx));
 856   } else {
 857     ShouldNotReachHere(); // no call to generate_slow_arraycopy:
 858                           // projections were not extracted
 859   }
 860 
 861   // Remove unused edges.
 862   for (uint i = 1; i < result_region->req(); i++) {

 891     // a subsequent store that would make this object accessible by
 892     // other threads.
 893     assert(ac->_dest_type == TypeOopPtr::BOTTOM, "non escaping destination shouldn't have narrow slice");
 894     insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore, Compile::AliasIdxBot);
 895   } else {
 896     int mem_bar_alias_idx = Compile::AliasIdxBot;
 897     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
 898       // The graph was transformed under the assumption the ArrayCopy node only had an effect on a narrow slice. We can't
 899       // insert a wide membar now that it's being expanded: a load that uses the input memory state of the ArrayCopy
 900       // could then become anti dependent on the membar when it was not anti dependent on the ArrayCopy leading to a
 901       // broken graph.
 902       mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
 903     }
 904     insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
 905   }
 906 
 907   assert((*ctrl)->is_Proj(), "MemBar control projection");
 908   assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node");
 909   (*ctrl)->in(0)->isa_MemBar()->set_trailing_expanded_array_copy();
 910 
 911   _igvn.replace_node(_callprojs->fallthrough_memproj, out_mem);
 912   if (_callprojs->fallthrough_ioproj != nullptr) {
 913     _igvn.replace_node(_callprojs->fallthrough_ioproj, *io);
 914   }
 915   _igvn.replace_node(_callprojs->fallthrough_catchproj, *ctrl);
 916 
 917 #ifdef ASSERT
 918   const TypeOopPtr* dest_t = _igvn.type(dest)->is_oopptr();
 919   if (dest_t->is_known_instance()) {
 920     ArrayCopyNode* ac = nullptr;
 921     assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost");
 922     assert(ac == nullptr, "no arraycopy anymore");
 923   }
 924 #endif
 925 
 926   return out_mem;
 927 }
 928 
 929 // Helper for initialization of arrays, creating a ClearArray.
 930 // It writes zero bits in [start..end), within the body of an array object.
 931 // The memory effects are all chained onto the 'adr_type' alias category.
 932 //
 933 // Since the object is otherwise uninitialized, we are free
 934 // to put a little "slop" around the edges of the cleared area,
 935 // as long as it does not go back into the array's header,
 936 // or beyond the array end within the heap.
 937 //
 938 // The lower edge can be rounded down to the nearest jint and the
 939 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
 940 //
 941 // Arguments:
 942 //   adr_type           memory slice where writes are generated
 943 //   dest               oop of the destination array
 944 //   basic_elem_type    element type of the destination
 945 //   slice_idx          array index of first element to store
 946 //   slice_len          number of elements to store (or null)
 947 //   dest_size          total size in bytes of the array object
 948 //
 949 // Exactly one of slice_len or dest_size must be non-null.
 950 // If dest_size is non-null, zeroing extends to the end of the object.
 951 // If slice_len is non-null, the slice_idx value must be a constant.
 952 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
 953                                             const TypePtr* adr_type,
 954                                             Node* dest,
 955                                             Node* val,
 956                                             Node* raw_val,
 957                                             BasicType basic_elem_type,
 958                                             Node* slice_idx,
 959                                             Node* slice_len,
 960                                             Node* dest_size) {
 961   // one or the other but not both of slice_len and dest_size:
 962   assert((slice_len != nullptr? 1: 0) + (dest_size != nullptr? 1: 0) == 1, "");
 963   if (slice_len == nullptr)  slice_len = top();
 964   if (dest_size == nullptr)  dest_size = top();
 965 
 966   uint alias_idx = C->get_alias_index(adr_type);
 967 
 968   // operate on this memory slice:
 969   Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on
 970 
 971   // scaling and rounding of indexes:
 972   int scale = exact_log2(type2aelembytes(basic_elem_type));
 973   int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
 974   int clear_low = (-1 << scale) & (BytesPerInt  - 1);
 975   int bump_bit  = (-1 << scale) & BytesPerInt;
 976 
 977   // determine constant starts and ends
 978   const intptr_t BIG_NEG = -128;
 979   assert(BIG_NEG + 2*abase < 0, "neg enough");
 980   intptr_t slice_idx_con = (intptr_t) _igvn.find_int_con(slice_idx, BIG_NEG);
 981   intptr_t slice_len_con = (intptr_t) _igvn.find_int_con(slice_len, BIG_NEG);
 982   if (slice_len_con == 0) {
 983     return;                     // nothing to do here
 984   }
 985   intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low;
 986   intptr_t end_con   = _igvn.find_intptr_t_con(dest_size, -1);
 987   if (slice_idx_con >= 0 && slice_len_con >= 0) {
 988     assert(end_con < 0, "not two cons");
 989     end_con = align_up(abase + ((slice_idx_con + slice_len_con) << scale),
 990                        BytesPerLong);
 991   }
 992 
 993   if (start_con >= 0 && end_con >= 0) {
 994     // Constant start and end.  Simple.
 995     mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
 996                                        start_con, end_con, &_igvn);
 997   } else if (start_con >= 0 && dest_size != top()) {
 998     // Constant start, pre-rounded end after the tail of the array.
 999     Node* end = dest_size;
1000     mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
1001                                        start_con, end, &_igvn);
1002   } else if (start_con >= 0 && slice_len != top()) {
1003     // Constant start, non-constant end.  End needs rounding up.
1004     // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8)
1005     intptr_t end_base  = abase + (slice_idx_con << scale);
1006     int      end_round = (-1 << scale) & (BytesPerLong  - 1);
1007     Node*    end       = ConvI2X(slice_len);
1008     if (scale != 0)
1009       end = transform_later(new LShiftXNode(end, intcon(scale) ));
1010     end_base += end_round;
1011     end = transform_later(new AddXNode(end, MakeConX(end_base)) );
1012     end = transform_later(new AndXNode(end, MakeConX(~end_round)) );
1013     mem = ClearArrayNode::clear_memory(ctrl, mem, dest, val, raw_val,
1014                                        start_con, end, &_igvn);
1015   } else if (start_con < 0 && dest_size != top()) {
1016     // Non-constant start, pre-rounded end after the tail of the array.
1017     // This is almost certainly a "round-to-end" operation.
1018     Node* start = slice_idx;
1019     start = ConvI2X(start);
1020     if (scale != 0)
1021       start = transform_later(new LShiftXNode( start, intcon(scale) ));
1022     start = transform_later(new AddXNode(start, MakeConX(abase)) );
1023     if ((bump_bit | clear_low) != 0) {
1024       int to_clear = (bump_bit | clear_low);
1025       // Align up mod 8, then store a jint zero unconditionally
1026       // just before the mod-8 boundary.
1027       if (((abase + bump_bit) & ~to_clear) - bump_bit
1028           < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) {
1029         bump_bit = 0;
1030         assert((abase & to_clear) == 0, "array base must be long-aligned");
1031       } else {
1032         // Bump 'start' up to (or past) the next jint boundary:
1033         start = transform_later( new AddXNode(start, MakeConX(bump_bit)) );
1034         assert((abase & clear_low) == 0, "array base must be int-aligned");
1035       }
1036       // Round bumped 'start' down to jlong boundary in body of array.
1037       start = transform_later(new AndXNode(start, MakeConX(~to_clear)) );
1038       if (bump_bit != 0) {
1039         // Store a zero to the immediately preceding jint:
1040         Node* x1 = transform_later(new AddXNode(start, MakeConX(-bump_bit)) );
1041         Node* p1 = basic_plus_adr(dest, x1);
1042         if (val == nullptr) {
1043           assert(raw_val == nullptr, "val may not be null");
1044           mem = StoreNode::make(_igvn, ctrl, mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered);
1045         } else {
1046           assert(_igvn.type(val)->isa_narrowoop(), "should be narrow oop");
1047           mem = new StoreNNode(ctrl, mem, p1, adr_type, val, MemNode::unordered);
1048         }
1049         mem = transform_later(mem);
1050       }
1051     }
1052     Node* end = dest_size; // pre-rounded
1053     mem = ClearArrayNode::clear_memory(ctrl, mem, dest, raw_val,
1054                                        start, end, &_igvn);
1055   } else {
1056     // Non-constant start, unrounded non-constant end.
1057     // (Nobody zeroes a random midsection of an array using this routine.)
1058     ShouldNotReachHere();       // fix caller
1059   }
1060 
1061   // Done.
1062   merge_mem->set_memory_at(alias_idx, mem);
1063 }
1064 
1065 bool PhaseMacroExpand::generate_block_arraycopy(Node** ctrl, MergeMemNode** mem, Node* io,
1066                                                 const TypePtr* adr_type,
1067                                                 BasicType basic_elem_type,
1068                                                 AllocateNode* alloc,
1069                                                 Node* src,  Node* src_offset,
1070                                                 Node* dest, Node* dest_offset,
1071                                                 Node* dest_size, bool dest_uninitialized) {
1072   // See if there is an advantage from block transfer.
1073   int scale = exact_log2(type2aelembytes(basic_elem_type));

1149   const TypeFunc* call_type = OptoRuntime::slow_arraycopy_Type();
1150   CallNode* call = new CallStaticJavaNode(call_type, OptoRuntime::slow_arraycopy_Java(),
1151                                           "slow_arraycopy", TypePtr::BOTTOM);
1152 
1153   call->init_req(TypeFunc::Control, *ctrl);
1154   call->init_req(TypeFunc::I_O    , *io);
1155   call->init_req(TypeFunc::Memory , mem);
1156   call->init_req(TypeFunc::ReturnAdr, top());
1157   call->init_req(TypeFunc::FramePtr, top());
1158   call->init_req(TypeFunc::Parms+0, src);
1159   call->init_req(TypeFunc::Parms+1, src_offset);
1160   call->init_req(TypeFunc::Parms+2, dest);
1161   call->init_req(TypeFunc::Parms+3, dest_offset);
1162   call->init_req(TypeFunc::Parms+4, copy_length);
1163   call->copy_call_debug_info(&_igvn, ac);
1164 
1165   call->set_cnt(PROB_UNLIKELY_MAG(4));  // Same effect as RC_UNCOMMON.
1166   _igvn.replace_node(ac, call);
1167   transform_later(call);
1168 
1169   _callprojs = call->extract_projections(false /*separate_io_proj*/, false /*do_asserts*/);
1170   *ctrl = _callprojs->fallthrough_catchproj->clone();
1171   transform_later(*ctrl);
1172 
1173   Node* m = _callprojs->fallthrough_memproj->clone();
1174   transform_later(m);
1175 
1176   uint alias_idx = C->get_alias_index(adr_type);
1177   MergeMemNode* out_mem;
1178   if (alias_idx != Compile::AliasIdxBot) {
1179     out_mem = MergeMemNode::make(mem);
1180     out_mem->set_memory_at(alias_idx, m);
1181   } else {
1182     out_mem = MergeMemNode::make(m);
1183   }
1184   transform_later(out_mem);
1185 
1186   // When src is negative and arraycopy is before an infinite loop,_callprojs.fallthrough_ioproj
1187   // could be nullptr. Skip clone and update nullptr fallthrough_ioproj.
1188   if (_callprojs->fallthrough_ioproj != nullptr) {
1189     *io = _callprojs->fallthrough_ioproj->clone();
1190     transform_later(*io);
1191   } else {
1192     *io = nullptr;
1193   }
1194 
1195   return out_mem;
1196 }
1197 
1198 // Helper function; generates code for cases requiring runtime checks.
1199 Node* PhaseMacroExpand::generate_checkcast_arraycopy(Node** ctrl, MergeMemNode** mem,
1200                                                      const TypePtr* adr_type,
1201                                                      Node* dest_elem_klass,
1202                                                      Node* src,  Node* src_offset,
1203                                                      Node* dest, Node* dest_offset,
1204                                                      Node* copy_length, bool dest_uninitialized) {
1205   if ((*ctrl)->is_top())  return nullptr;
1206 
1207   address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized);
1208   if (copyfunc_addr == nullptr) { // Stub was not generated, go slow path.
1209     return nullptr;

1301 
1302   // Connecting remaining edges for exit_block coming from stub_block.
1303   if (exit_block) {
1304     exit_block->init_req(2, *ctrl);
1305 
1306     // Memory edge corresponding to stub_region.
1307     result_memory->init_req(2, *mem);
1308 
1309     uint alias_idx = C->get_alias_index(adr_type);
1310     if (alias_idx != Compile::AliasIdxBot) {
1311       *mem = MergeMemNode::make(*mem);
1312       (*mem)->set_memory_at(alias_idx, result_memory);
1313     } else {
1314       *mem = MergeMemNode::make(result_memory);
1315     }
1316     transform_later(*mem);
1317     *ctrl = exit_block;
1318   }
1319 }
1320 
1321 const TypePtr* PhaseMacroExpand::adjust_for_flat_array(const TypeAryPtr* top_dest, Node*& src_offset,
1322                                                        Node*& dest_offset, Node*& length, BasicType& dest_elem,
1323                                                        Node*& dest_length) {
1324 #ifdef ASSERT
1325   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1326   bool needs_barriers = top_dest->elem()->inline_klass()->contains_oops() &&
1327     bs->array_copy_requires_gc_barriers(dest_length != nullptr, T_OBJECT, false, false, BarrierSetC2::Optimization);
1328   assert(!needs_barriers || StressReflectiveCode, "Flat arracopy would require GC barriers");
1329 #endif
1330   int elem_size = top_dest->flat_elem_size();
1331   if (elem_size >= 8) {
1332     if (elem_size > 8) {
1333       // treat as array of long but scale length, src offset and dest offset
1334       assert((elem_size % 8) == 0, "not a power of 2?");
1335       int factor = elem_size / 8;
1336       length = transform_later(new MulINode(length, intcon(factor)));
1337       src_offset = transform_later(new MulINode(src_offset, intcon(factor)));
1338       dest_offset = transform_later(new MulINode(dest_offset, intcon(factor)));
1339       if (dest_length != nullptr) {
1340         dest_length = transform_later(new MulINode(dest_length, intcon(factor)));
1341       }
1342       elem_size = 8;
1343     }
1344     dest_elem = T_LONG;
1345   } else if (elem_size == 4) {
1346     dest_elem = T_INT;
1347   } else if (elem_size == 2) {
1348     dest_elem = T_CHAR;
1349   } else if (elem_size == 1) {
1350     dest_elem = T_BYTE;
1351   } else {
1352     ShouldNotReachHere();
1353   }
1354   return TypeRawPtr::BOTTOM;
1355 }
1356 
1357 #undef XTOP
1358 
1359 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
1360   Node* ctrl = ac->in(TypeFunc::Control);
1361   Node* io = ac->in(TypeFunc::I_O);
1362   Node* src = ac->in(ArrayCopyNode::Src);
1363   Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
1364   Node* dest = ac->in(ArrayCopyNode::Dest);
1365   Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
1366   Node* length = ac->in(ArrayCopyNode::Length);
1367   MergeMemNode* merge_mem = nullptr;
1368 
1369   if (ac->is_clonebasic()) {
1370     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1371     bs->clone_at_expansion(this, ac);
1372     return;
1373   } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) {
1374     const Type* src_type = _igvn.type(src);
1375     const Type* dest_type = _igvn.type(dest);
1376     const TypeAryPtr* top_src = src_type->isa_aryptr();
1377     // Note: The destination could have type Object (i.e. non-array) when directly invoking the protected method
1378     //       Object::clone() with reflection on a declared Object that is an array at runtime. top_dest is then null.
1379     const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1380     BasicType dest_elem = T_OBJECT;
1381     if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1382       dest_elem = top_dest->elem()->array_element_basic_type();
1383     }
1384     if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1385 
1386     if (top_src != nullptr && top_src->is_flat()) {
1387       // If src is flat, dest is guaranteed to be flat as well
1388       top_dest = top_src;
1389     }
1390 
1391     AllocateArrayNode* alloc = nullptr;
1392     Node* dest_length = nullptr;
1393     if (ac->is_alloc_tightly_coupled()) {
1394       alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1395       assert(alloc != nullptr, "expect alloc");
1396       dest_length = alloc->in(AllocateNode::ALength);
1397     }
1398 
1399     Node* mem = ac->in(TypeFunc::Memory);
1400     const TypePtr* adr_type = nullptr;
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       }
1419     }
1420     merge_mem = MergeMemNode::make(mem);
1421     transform_later(merge_mem);
1422 
1423     generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1424                        adr_type, dest_elem,
1425                        src, src_offset, dest, dest_offset, length,
1426                        dest_length,
1427                        true, ac->has_negative_length_guard());
1428 
1429     return;
1430   }
1431 
1432   AllocateArrayNode* alloc = nullptr;
1433   if (ac->is_alloc_tightly_coupled()) {
1434     alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1435     assert(alloc != nullptr, "expect alloc");
1436   }
1437 
1438   assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1439 
1440   // Compile time checks.  If any of these checks cannot be verified at compile time,
1441   // we do not make a fast path for this call.  Instead, we let the call remain as it
1442   // is.  The checks we choose to mandate at compile time are:
1443   //
1444   // (1) src and dest are arrays.
1445   const Type* src_type = src->Value(&_igvn);
1446   const Type* dest_type = dest->Value(&_igvn);
1447   const TypeAryPtr* top_src = src_type->isa_aryptr();
1448   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1449 
1450   BasicType src_elem = T_CONFLICT;
1451   BasicType dest_elem = T_CONFLICT;
1452 
1453   if (top_src != nullptr && top_src->elem() != Type::BOTTOM) {
1454     src_elem = top_src->elem()->array_element_basic_type();
1455   }
1456   if (top_dest != nullptr && top_dest->elem() != Type::BOTTOM) {
1457     dest_elem = top_dest->elem()->array_element_basic_type();
1458   }
1459   if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
1460   if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
1461 
1462   if (ac->is_arraycopy_validated() && dest_elem != T_CONFLICT && src_elem == T_CONFLICT) {


1463     src_elem = dest_elem;
1464   }
1465 
1466   if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) {
1467     // Conservatively insert a memory barrier on all memory slices.
1468     // Do not let writes into the source float below the arraycopy.
1469     {
1470       Node* mem = ac->in(TypeFunc::Memory);
1471       insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, Compile::AliasIdxBot);
1472 
1473       merge_mem = MergeMemNode::make(mem);
1474       transform_later(merge_mem);
1475     }
1476 
1477     // Call StubRoutines::generic_arraycopy stub.
1478     generate_arraycopy(ac, nullptr, &ctrl, merge_mem, &io,
1479                        TypeRawPtr::BOTTOM, T_CONFLICT,
1480                        src, src_offset, dest, dest_offset, length,
1481                        nullptr,
1482                        // If a  negative length guard was generated for the ArrayCopyNode,
1483                        // the length of the array can never be negative.
1484                        false, ac->has_negative_length_guard());
1485     return;
1486   }
1487 
1488   assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");
1489 
1490   // (2) src and dest arrays must have elements of the same BasicType
1491   // Figure out the size and type of the elements we will be copying.
1492   //
1493   // We have no stub to copy flat inline type arrays with oop
1494   // fields if we need to emit write barriers.
1495   //
1496   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1497   if (src_elem != dest_elem || top_src->is_flat() != top_dest->is_flat() || dest_elem == T_VOID ||
1498       (top_src->is_flat() && top_dest->elem()->inline_klass()->contains_oops() &&
1499        bs->array_copy_requires_gc_barriers(alloc != nullptr, T_OBJECT, false, false, BarrierSetC2::Optimization))) {
1500     // The component types are not the same or are not recognized.  Punt.
1501     // (But, avoid the native method wrapper to JVM_ArrayCopy.)
1502     {
1503       Node* mem = ac->in(TypeFunc::Memory);
1504       merge_mem = generate_slow_arraycopy(ac, &ctrl, mem, &io, TypePtr::BOTTOM, src, src_offset, dest, dest_offset, length, false);
1505     }
1506 
1507     _igvn.replace_node(_callprojs->fallthrough_memproj, merge_mem);
1508     if (_callprojs->fallthrough_ioproj != nullptr) {
1509       _igvn.replace_node(_callprojs->fallthrough_ioproj, io);
1510     }
1511     _igvn.replace_node(_callprojs->fallthrough_catchproj, ctrl);
1512     return;
1513   }
1514 
1515   //---------------------------------------------------------------------------
1516   // We will make a fast path for this call to arraycopy.
1517 
1518   // We have the following tests left to perform:
1519   //
1520   // (3) src and dest must not be null.
1521   // (4) src_offset must not be negative.
1522   // (5) dest_offset must not be negative.
1523   // (6) length must not be negative.
1524   // (7) src_offset + length must not exceed length of src.
1525   // (8) dest_offset + length must not exceed length of dest.
1526   // (9) each element of an oop array must be assignable
1527 
1528   Node* mem = ac->in(TypeFunc::Memory);
1529   if (top_dest->is_flat()) {
1530     // Copy to a flat array modifies multiple memory slices. Conservatively insert a barrier
1531     // on all slices to prevent writes into the source from floating below the arraycopy.
1532     int mem_bar_alias_idx = Compile::AliasIdxBot;
1533     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1534       mem_bar_alias_idx = C->get_alias_index(ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr());
1535     }
1536     insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder, mem_bar_alias_idx);
1537   }
1538   merge_mem = MergeMemNode::make(mem);
1539   transform_later(merge_mem);
1540 
1541   RegionNode* slow_region = new RegionNode(1);
1542   transform_later(slow_region);
1543 
1544   if (!ac->is_arraycopy_validated()) {
1545     // (3) operands must not be null
1546     // We currently perform our null checks with the null_check routine.
1547     // This means that the null exceptions will be reported in the caller
1548     // rather than (correctly) reported inside of the native arraycopy call.
1549     // This should be corrected, given time.  We do our null check with the
1550     // stack pointer restored.
1551     // null checks done library_call.cpp
1552 
1553     // (4) src_offset must not be negative.
1554     generate_negative_guard(&ctrl, src_offset, slow_region);
1555 
1556     // (5) dest_offset must not be negative.
1557     generate_negative_guard(&ctrl, dest_offset, slow_region);
1558 
1559     // (6) length must not be negative (moved to generate_arraycopy()).
1560     // generate_negative_guard(length, slow_region);
1561 
1562     // (7) src_offset + length must not exceed length of src.
1563     Node* alen = ac->in(ArrayCopyNode::SrcLen);
1564     assert(alen != nullptr, "need src len");
1565     generate_limit_guard(&ctrl,
1566                          src_offset, length,
1567                          alen,
1568                          slow_region);
1569 
1570     // (8) dest_offset + length must not exceed length of dest.
1571     alen = ac->in(ArrayCopyNode::DestLen);
1572     assert(alen != nullptr, "need dest len");
1573     generate_limit_guard(&ctrl,
1574                          dest_offset, length,
1575                          alen,
1576                          slow_region);
1577 
1578     // (9) each element of an oop array must be assignable
1579     // The generate_arraycopy subroutine checks this.
1580 
1581     // TODO 8350865 Fix below logic. Also handle atomicity.
1582     // 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.
1583     if (!(top_src->is_flat() && top_dest->is_flat())) {
1584       generate_flat_array_guard(&ctrl, src, merge_mem, slow_region);
1585       generate_flat_array_guard(&ctrl, dest, merge_mem, slow_region);
1586     }
1587 
1588     // Handle inline type arrays
1589     if (!top_src->is_flat()) {
1590       if (UseArrayFlattening && !top_src->is_not_flat()) {
1591         // Src might be flat and dest might not be flat. Go to the slow path if src is flat.
1592         generate_flat_array_guard(&ctrl, src, merge_mem, slow_region);
1593       }
1594       if (EnableValhalla) {
1595         // No validation. The subtype check emitted at macro expansion time will not go to the slow
1596         // path but call checkcast_arraycopy which can not handle flat/null-free inline type arrays.
1597         generate_null_free_array_guard(&ctrl, dest, merge_mem, slow_region);
1598       }
1599     } else {
1600       assert(top_dest->is_flat(), "dest array must be flat");
1601     }
1602   }
1603 
1604   // This is where the memory effects are placed:
1605   const TypePtr* adr_type = nullptr;
1606   Node* dest_length = (alloc != nullptr) ? alloc->in(AllocateNode::ALength) : nullptr;
1607 
1608   if (top_src->is_flat() && top_dest->is_flat()) {
1609     adr_type = adjust_for_flat_array(top_dest, src_offset, dest_offset, length, dest_elem, dest_length);
1610   } else if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1611     adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1612   } else {
1613     adr_type = TypeAryPtr::get_array_body_type(dest_elem);
1614   }
1615 
1616   generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1617                      adr_type, dest_elem,
1618                      src, src_offset, dest, dest_offset, length,
1619                      dest_length,
1620                      // If a  negative length guard was generated for the ArrayCopyNode,
1621                      // the length of the array can never be negative.
1622                      false, ac->has_negative_length_guard(),
1623                      slow_region);
1624 }
< prev index next >