< 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   bool  dest_needs_zeroing   = false;
 388   bool  acopy_to_uninitialized = false;


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


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

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


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


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

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


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


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

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


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

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


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






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

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

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




































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













1256 
1257     AllocateArrayNode* alloc = nullptr;

1258     if (ac->is_alloc_tightly_coupled()) {
1259       alloc = AllocateArrayNode::Ideal_array_allocation(dest);
1260       assert(alloc != nullptr, "expect alloc");

1261     }
1262 
1263     const TypePtr* adr_type = _igvn.type(dest)->is_oopptr()->add_offset(Type::OffsetBot);
1264     if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1265       adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();

















1266     }



1267     generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1268                        adr_type, T_OBJECT,
1269                        src, src_offset, dest, dest_offset, length,

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

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







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





1369   }


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








1410   }

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





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

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

1425 }

   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_raw(nullptr, mem->memory_at(Compile::AliasIdxRaw), obj, oopDesc::mark_offset_in_bytes(), TypeX_X, TypeX_X->basic_type());
 298   Node* locked_bit = MakeConX(markWord::unlocked_value);
 299   locked_bit = transform_later(new AndXNode(locked_bit, mark));
 300   Node* cmp = transform_later(new CmpXNode(locked_bit, MakeConX(0)));
 301   Node* is_unlocked = transform_later(new BoolNode(cmp, BoolTest::ne));
 302   IfNode* iff = transform_later(new IfNode(*ctrl, is_unlocked, PROB_MAX, COUNT_UNKNOWN))->as_If();
 303   Node* locked_region = transform_later(new RegionNode(3));
 304   Node* mark_phi = transform_later(new PhiNode(locked_region, TypeX_X));
 305 
 306   // Unlocked: Use bits from mark word
 307   locked_region->init_req(1, transform_later(new IfTrueNode(iff)));
 308   mark_phi->init_req(1, mark);
 309 
 310   // Locked: Load prototype header from klass
 311   *ctrl = transform_later(new IfFalseNode(iff));
 312   // Make loads control dependent to make sure they are only executed if array is locked
 313   Node* klass_adr = basic_plus_adr(obj, oopDesc::klass_offset_in_bytes());
 314   Node* klass = transform_later(LoadKlassNode::make(_igvn, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
 315   Node* proto_adr = basic_plus_adr(top(), klass, in_bytes(Klass::prototype_header_offset()));
 316   Node* proto = transform_later(LoadNode::make(_igvn, *ctrl, C->immutable_memory(), proto_adr, proto_adr->bottom_type()->is_ptr(), TypeX_X, TypeX_X->basic_type(), MemNode::unordered));
 317 
 318   locked_region->init_req(2, *ctrl);
 319   mark_phi->init_req(2, proto);
 320   *ctrl = locked_region;
 321 
 322   // Now check if mark word bits are set
 323   Node* mask = MakeConX(mask_val);
 324   Node* masked = transform_later(new AndXNode(mark_phi, mask));
 325   cmp = transform_later(new CmpXNode(masked, mask));
 326   Node* bol = transform_later(new BoolNode(cmp, BoolTest::eq));
 327   return generate_fair_guard(ctrl, bol, region);
 328 }
 329 
 330 Node* PhaseMacroExpand::generate_flat_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
 331   return mark_word_test(ctrl, array, mem, markWord::flat_array_bit_in_place, region);
 332 }
 333 
 334 Node* PhaseMacroExpand::generate_null_free_array_guard(Node** ctrl, Node* array, MergeMemNode* mem, RegionNode* region) {
 335   return mark_word_test(ctrl, array, mem, markWord::null_free_array_bit_in_place, region);
 336 }
 337 
 338 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
 339   transform_later(call);
 340 
 341   *ctrl = new ProjNode(call,TypeFunc::Control);
 342   transform_later(*ctrl);
 343   Node* newmem = new ProjNode(call, TypeFunc::Memory);
 344   transform_later(newmem);
 345 
 346   uint alias_idx = C->get_alias_index(adr_type);
 347   if (alias_idx != Compile::AliasIdxBot) {
 348     *mem = MergeMemNode::make(*mem);
 349     (*mem)->set_memory_at(alias_idx, newmem);
 350   } else {
 351     *mem = MergeMemNode::make(newmem);
 352   }
 353   transform_later(*mem);
 354 }
 355 
 356 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
 357                                               Node* src_offset,

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

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

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

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

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

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

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

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

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


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