< 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

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











































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

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

 379                                            bool disjoint_bases,
 380                                            bool length_never_negative,
 381                                            RegionNode* slow_region) {
 382   if (slow_region == nullptr) {
 383     slow_region = new RegionNode(1);
 384     transform_later(slow_region);
 385   }
 386 
 387   Node* original_dest = dest;
 388   bool  dest_needs_zeroing   = false;
 389   bool  acopy_to_uninitialized = false;


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


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

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


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


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

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


 583                              dest_tail, nullptr,
 584                              dest_size);
 585       } else {
 586         // Make a local merge.
 587         Node* done_ctl = transform_later(new RegionNode(3));
 588         Node* done_mem = transform_later(new PhiNode(done_ctl, Type::MEMORY, adr_type));
 589         done_ctl->init_req(1, notail_ctl);
 590         done_mem->init_req(1, mem->memory_at(alias_idx));
 591         generate_clear_array(*ctrl, mem,
 592                              adr_type, dest, basic_elem_type,


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

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


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

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


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






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

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

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




































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













1260 
1261     AllocateArrayNode* alloc = nullptr;

1262     if (ac->is_alloc_tightly_coupled()) {
1263       alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1264       assert(alloc != nullptr, "expect alloc");

1265     }
1266 
1267     const TypePtr* adr_type = _igvn.type(dest)->is_oopptr()->add_offset(Type::OffsetBot);
1268     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1269       adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();

















1270     }



1271     generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1272                        adr_type, T_OBJECT,
1273                        src, src_offset, dest, dest_offset, length,

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

1333     return;
1334   }
1335 
1336   assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");
1337 
1338   // (2) src and dest arrays must have elements of the same BasicType
1339   // Figure out the size and type of the elements we will be copying.
1340   if (src_elem != dest_elem || dest_elem == T_VOID) {







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





1373   }


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








1414   }

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





1418     adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1419   } else {
1420     adr_type = TypeAryPtr::get_array_body_type(dest_elem);
1421   }
1422 
1423   generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1424                      adr_type, dest_elem,
1425                      src, src_offset, dest, dest_offset, length,

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

1429 }

   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

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

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

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

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

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

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

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

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

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

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


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