< prev index next >

src/hotspot/share/opto/macroArrayCopy.cpp

Print this page

   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 "precompiled.hpp"

  26 #include "gc/shared/barrierSet.hpp"
  27 #include "gc/shared/tlab_globals.hpp"
  28 #include "opto/arraycopynode.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "opto/convertnode.hpp"
  31 #include "opto/vectornode.hpp"
  32 #include "opto/graphKit.hpp"
  33 #include "opto/macro.hpp"
  34 #include "opto/runtime.hpp"
  35 #include "opto/castnode.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, Node* precedent) {
  41   MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, 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);

 122   }
 123 
 124   IfNode* iff = new IfNode(*ctrl, test, true_prob, COUNT_UNKNOWN);
 125   transform_later(iff);
 126 
 127   Node* if_slow = new IfTrueNode(iff);
 128   transform_later(if_slow);
 129 
 130   if (region != NULL) {
 131     region->add_req(if_slow);
 132   }
 133 
 134   Node* if_fast = new IfFalseNode(iff);
 135   transform_later(if_fast);
 136 
 137   *ctrl = if_fast;
 138 
 139   return if_slow;
 140 }
 141 
 142 inline Node* PhaseMacroExpand::generate_slow_guard(Node** ctrl, Node* test, RegionNode* region) {
 143   return generate_guard(ctrl, test, region, PROB_UNLIKELY_MAG(3));
 144 }
 145 




 146 void PhaseMacroExpand::generate_negative_guard(Node** ctrl, Node* index, RegionNode* region) {
 147   if ((*ctrl)->is_top())
 148     return;                // already stopped
 149   if (_igvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
 150     return;                // index is already adequately typed
 151   Node* cmp_lt = new CmpINode(index, intcon(0));
 152   transform_later(cmp_lt);
 153   Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt);
 154   transform_later(bol_lt);
 155   generate_guard(ctrl, bol_lt, region, PROB_MIN);
 156 }
 157 
 158 void PhaseMacroExpand::generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region) {
 159   if ((*ctrl)->is_top())
 160     return;                // already stopped
 161   bool zero_offset = _igvn.type(offset) == TypeInt::ZERO;
 162   if (zero_offset && subseq_length->eqv_uncast(array_length))
 163     return;                // common case of whole-array copy
 164   Node* last = subseq_length;
 165   if (!zero_offset) {            // last += offset

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




















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

 318   // or they are identical (which we can treat as disjoint.)  We can also
 319   // treat a copy with a destination index  less that the source index
 320   // as disjoint since a low->high copy will work correctly in this case.
 321   if (src_offset_inttype != NULL && src_offset_inttype->is_con() &&
 322       dest_offset_inttype != NULL && dest_offset_inttype->is_con()) {
 323     // both indices are constants
 324     int s_offs = src_offset_inttype->get_con();
 325     int d_offs = dest_offset_inttype->get_con();
 326     int element_size = type2aelembytes(t);
 327     aligned = ((arrayOopDesc::base_offset_in_bytes(t) + s_offs * element_size) % HeapWordSize == 0) &&
 328               ((arrayOopDesc::base_offset_in_bytes(t) + d_offs * element_size) % HeapWordSize == 0);
 329     if (s_offs >= d_offs)  disjoint = true;
 330   } else if (src_offset == dest_offset && src_offset != NULL) {
 331     // This can occur if the offsets are identical non-constants.
 332     disjoint = true;
 333   }
 334 
 335   return StubRoutines::select_arraycopy_function(t, aligned, disjoint, name, dest_uninitialized);
 336 }
 337 



















 338 #define XTOP LP64_ONLY(COMMA top())
 339 
 340 // Generate an optimized call to arraycopy.
 341 // Caller must guard against non-arrays.
 342 // Caller must determine a common array basic-type for both arrays.
 343 // Caller must validate offsets against array bounds.
 344 // The slow_region has already collected guard failure paths
 345 // (such as out of bounds length or non-conformable array types).
 346 // The generated code has this shape, in general:
 347 //
 348 //     if (length == 0)  return   // via zero_path
 349 //     slowval = -1
 350 //     if (types unknown) {
 351 //       slowval = call generic copy loop
 352 //       if (slowval == 0)  return  // via checked_path
 353 //     } else if (indexes in bounds) {
 354 //       if ((is object array) && !(array type check)) {
 355 //         slowval = call checked copy loop
 356 //         if (slowval == 0)  return  // via checked_path
 357 //       } else {

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

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


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


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

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


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


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

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


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


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

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


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

 813   // The next memory barrier is added to avoid it. If the arraycopy can be
 814   // optimized away (which it can, sometimes) then we can manually remove
 815   // the membar also.
 816   //
 817   // Do not let reads from the cloned object float above the arraycopy.
 818   if (alloc != NULL && !alloc->initialization()->does_not_escape()) {
 819     // Do not let stores that initialize this object be reordered with
 820     // a subsequent store that would make this object accessible by
 821     // other threads.
 822     insert_mem_bar(ctrl, &out_mem, Op_MemBarStoreStore);
 823   } else {
 824     insert_mem_bar(ctrl, &out_mem, Op_MemBarCPUOrder);
 825   }
 826 
 827   if (is_partial_array_copy) {
 828     assert((*ctrl)->is_Proj(), "MemBar control projection");
 829     assert((*ctrl)->in(0)->isa_MemBar(), "MemBar node");
 830     (*ctrl)->in(0)->isa_MemBar()->set_trailing_partial_array_copy();
 831   }
 832 
 833   _igvn.replace_node(_callprojs.fallthrough_memproj, out_mem);
 834   if (_callprojs.fallthrough_ioproj != NULL) {
 835     _igvn.replace_node(_callprojs.fallthrough_ioproj, *io);
 836   }
 837   _igvn.replace_node(_callprojs.fallthrough_catchproj, *ctrl);
 838 
 839 #ifdef ASSERT
 840   const TypeOopPtr* dest_t = _igvn.type(dest)->is_oopptr();
 841   if (dest_t->is_known_instance() && !is_partial_array_copy) {
 842     ArrayCopyNode* ac = NULL;
 843     assert(ArrayCopyNode::may_modify(dest_t, (*ctrl)->in(0)->as_MemBar(), &_igvn, ac), "dependency on arraycopy lost");
 844     assert(ac == NULL, "no arraycopy anymore");
 845   }
 846 #endif
 847 
 848   return out_mem;
 849 }
 850 
 851 // Helper for initialization of arrays, creating a ClearArray.
 852 // It writes zero bits in [start..end), within the body of an array object.
 853 // The memory effects are all chained onto the 'adr_type' alias category.
 854 //
 855 // Since the object is otherwise uninitialized, we are free
 856 // to put a little "slop" around the edges of the cleared area,
 857 // as long as it does not go back into the array's header,
 858 // or beyond the array end within the heap.
 859 //
 860 // The lower edge can be rounded down to the nearest jint and the
 861 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
 862 //
 863 // Arguments:
 864 //   adr_type           memory slice where writes are generated
 865 //   dest               oop of the destination array
 866 //   basic_elem_type    element type of the destination
 867 //   slice_idx          array index of first element to store
 868 //   slice_len          number of elements to store (or NULL)
 869 //   dest_size          total size in bytes of the array object
 870 //
 871 // Exactly one of slice_len or dest_size must be non-NULL.
 872 // If dest_size is non-NULL, zeroing extends to the end of the object.
 873 // If slice_len is non-NULL, the slice_idx value must be a constant.
 874 void PhaseMacroExpand::generate_clear_array(Node* ctrl, MergeMemNode* merge_mem,
 875                                             const TypePtr* adr_type,
 876                                             Node* dest,


 877                                             BasicType basic_elem_type,
 878                                             Node* slice_idx,
 879                                             Node* slice_len,
 880                                             Node* dest_size) {
 881   // one or the other but not both of slice_len and dest_size:
 882   assert((slice_len != NULL? 1: 0) + (dest_size != NULL? 1: 0) == 1, "");
 883   if (slice_len == NULL)  slice_len = top();
 884   if (dest_size == NULL)  dest_size = top();
 885 
 886   uint alias_idx = C->get_alias_index(adr_type);
 887 
 888   // operate on this memory slice:
 889   Node* mem = merge_mem->memory_at(alias_idx); // memory slice to operate on
 890 
 891   // scaling and rounding of indexes:

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






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

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

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




































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













1255 
1256     AllocateArrayNode* alloc = NULL;

1257     if (ac->is_alloc_tightly_coupled()) {
1258       alloc = AllocateArrayNode::Ideal_array_allocation(dest, &_igvn);
1259       assert(alloc != NULL, "expect alloc");

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













1265     }



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

1269                        true, !ac->is_copyofrange());
1270 
1271     return;
1272   }
1273 
1274   AllocateArrayNode* alloc = NULL;
1275   if (ac->is_alloc_tightly_coupled()) {
1276     alloc = AllocateArrayNode::Ideal_array_allocation(dest, &_igvn);
1277     assert(alloc != NULL, "expect alloc");
1278   }
1279 
1280   assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1281 
1282   // Compile time checks.  If any of these checks cannot be verified at compile time,
1283   // we do not make a fast path for this call.  Instead, we let the call remain as it
1284   // is.  The checks we choose to mandate at compile time are:
1285   //
1286   // (1) src and dest are arrays.
1287   const Type* src_type = src->Value(&_igvn);
1288   const Type* dest_type = dest->Value(&_igvn);
1289   const TypeAryPtr* top_src = src_type->isa_aryptr();
1290   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1291 
1292   BasicType src_elem = T_CONFLICT;
1293   BasicType dest_elem = T_CONFLICT;
1294 
1295   if (top_dest != NULL && top_dest->klass() != NULL) {
1296     dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
1297   }
1298   if (top_src != NULL && top_src->klass() != NULL) {
1299     src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
1300   }
1301   if (is_reference_type(src_elem))  src_elem  = T_OBJECT;
1302   if (is_reference_type(dest_elem)) dest_elem = T_OBJECT;




1303 
1304   if (ac->is_arraycopy_validated() &&
1305       dest_elem != T_CONFLICT &&
1306       src_elem == T_CONFLICT) {
1307     src_elem = dest_elem;
1308   }
1309 
1310   if (src_elem == T_CONFLICT || dest_elem == T_CONFLICT) {
1311     // Conservatively insert a memory barrier on all memory slices.
1312     // Do not let writes into the source float below the arraycopy.
1313     {
1314       Node* mem = ac->in(TypeFunc::Memory);
1315       insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder);
1316 
1317       merge_mem = MergeMemNode::make(mem);
1318       transform_later(merge_mem);
1319     }
1320 
1321     // Call StubRoutines::generic_arraycopy stub.
1322     Node* mem = generate_arraycopy(ac, NULL, &ctrl, merge_mem, &io,
1323                                    TypeRawPtr::BOTTOM, T_CONFLICT,
1324                                    src, src_offset, dest, dest_offset, length,

1325                                    // If a  negative length guard was generated for the ArrayCopyNode,
1326                                    // the length of the array can never be negative.
1327                                    false, ac->has_negative_length_guard());
1328     return;
1329   }
1330 
1331   assert(!ac->is_arraycopy_validated() || (src_elem == dest_elem && dest_elem != T_VOID), "validated but different basic types");

1332 
1333   // (2) src and dest arrays must have elements of the same BasicType
1334   // Figure out the size and type of the elements we will be copying.
1335   if (src_elem != dest_elem || dest_elem == T_VOID) {







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

1368   }


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















1409   }

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




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

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

1424 }

   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 "precompiled.hpp"
  26 #include "ci/ciFlatArrayKlass.hpp"
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shared/tlab_globals.hpp"
  29 #include "opto/arraycopynode.hpp"
  30 #include "oops/objArrayKlass.hpp"
  31 #include "opto/convertnode.hpp"
  32 #include "opto/vectornode.hpp"
  33 #include "opto/graphKit.hpp"
  34 #include "opto/macro.hpp"
  35 #include "opto/runtime.hpp"
  36 #include "opto/castnode.hpp"
  37 #include "runtime/stubRoutines.hpp"
  38 #include "utilities/align.hpp"
  39 #include "utilities/powerOfTwo.hpp"
  40 
  41 void PhaseMacroExpand::insert_mem_bar(Node** ctrl, Node** mem, int opcode, Node* precedent) {
  42   MemBarNode* mb = MemBarNode::make(C, opcode, Compile::AliasIdxBot, precedent);
  43   mb->init_req(TypeFunc::Control, *ctrl);
  44   mb->init_req(TypeFunc::Memory, *mem);
  45   transform_later(mb);
  46   *ctrl = new ProjNode(mb,TypeFunc::Control);

 123   }
 124 
 125   IfNode* iff = new IfNode(*ctrl, test, true_prob, COUNT_UNKNOWN);
 126   transform_later(iff);
 127 
 128   Node* if_slow = new IfTrueNode(iff);
 129   transform_later(if_slow);
 130 
 131   if (region != NULL) {
 132     region->add_req(if_slow);
 133   }
 134 
 135   Node* if_fast = new IfFalseNode(iff);
 136   transform_later(if_fast);
 137 
 138   *ctrl = if_fast;
 139 
 140   return if_slow;
 141 }
 142 
 143 Node* PhaseMacroExpand::generate_slow_guard(Node** ctrl, Node* test, RegionNode* region) {
 144   return generate_guard(ctrl, test, region, PROB_UNLIKELY_MAG(3));
 145 }
 146 
 147 inline Node* PhaseMacroExpand::generate_fair_guard(Node** ctrl, Node* test, RegionNode* region) {
 148   return generate_guard(ctrl, test, region, PROB_FAIR);
 149 }
 150 
 151 void PhaseMacroExpand::generate_negative_guard(Node** ctrl, Node* index, RegionNode* region) {
 152   if ((*ctrl)->is_top())
 153     return;                // already stopped
 154   if (_igvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
 155     return;                // index is already adequately typed
 156   Node* cmp_lt = new CmpINode(index, intcon(0));
 157   transform_later(cmp_lt);
 158   Node* bol_lt = new BoolNode(cmp_lt, BoolTest::lt);
 159   transform_later(bol_lt);
 160   generate_guard(ctrl, bol_lt, region, PROB_MIN);
 161 }
 162 
 163 void PhaseMacroExpand::generate_limit_guard(Node** ctrl, Node* offset, Node* subseq_length, Node* array_length, RegionNode* region) {
 164   if ((*ctrl)->is_top())
 165     return;                // already stopped
 166   bool zero_offset = _igvn.type(offset) == TypeInt::ZERO;
 167   if (zero_offset && subseq_length->eqv_uncast(array_length))
 168     return;                // common case of whole-array copy
 169   Node* last = subseq_length;
 170   if (!zero_offset) {            // last += offset

 271 
 272   *ctrl = stub_block;
 273 }
 274 
 275 
 276 Node* PhaseMacroExpand::generate_nonpositive_guard(Node** ctrl, Node* index, bool never_negative) {
 277   if ((*ctrl)->is_top())  return NULL;
 278 
 279   if (_igvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
 280     return NULL;                // index is already adequately typed
 281   Node* cmp_le = new CmpINode(index, intcon(0));
 282   transform_later(cmp_le);
 283   BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
 284   Node* bol_le = new BoolNode(cmp_le, le_or_eq);
 285   transform_later(bol_le);
 286   Node* is_notp = generate_guard(ctrl, bol_le, NULL, PROB_MIN);
 287 
 288   return is_notp;
 289 }
 290 
 291 Node* PhaseMacroExpand::array_lh_test(Node* array, jint mask) {
 292   Node* klass_adr = basic_plus_adr(array, oopDesc::klass_offset_in_bytes());
 293   Node* klass = transform_later(LoadKlassNode::make(_igvn, NULL, C->immutable_memory(), klass_adr, TypeInstPtr::KLASS, TypeInstKlassPtr::OBJECT));
 294   Node* lh_addr = basic_plus_adr(klass, in_bytes(Klass::layout_helper_offset()));
 295   Node* lh_val = _igvn.transform(LoadNode::make(_igvn, NULL, C->immutable_memory(), lh_addr, lh_addr->bottom_type()->is_ptr(), TypeInt::INT, T_INT, MemNode::unordered));
 296   Node* masked = transform_later(new AndINode(lh_val, intcon(mask)));
 297   Node* cmp = transform_later(new CmpINode(masked, intcon(0)));
 298   return transform_later(new BoolNode(cmp, BoolTest::ne));
 299 }
 300 
 301 Node* PhaseMacroExpand::generate_flat_array_guard(Node** ctrl, Node* array, RegionNode* region) {
 302   assert(UseFlatArray, "can never be flattened");
 303   return generate_fair_guard(ctrl, array_lh_test(array, Klass::_lh_array_tag_flat_value_bit_inplace), region);
 304 }
 305 
 306 Node* PhaseMacroExpand::generate_null_free_array_guard(Node** ctrl, Node* array, RegionNode* region) {
 307   assert(EnableValhalla, "can never be null free");
 308   return generate_fair_guard(ctrl, array_lh_test(array, Klass::_lh_null_free_array_bit_inplace), region);
 309 }
 310 
 311 void PhaseMacroExpand::finish_arraycopy_call(Node* call, Node** ctrl, MergeMemNode** mem, const TypePtr* adr_type) {
 312   transform_later(call);
 313 
 314   *ctrl = new ProjNode(call,TypeFunc::Control);
 315   transform_later(*ctrl);
 316   Node* newmem = new ProjNode(call, TypeFunc::Memory);
 317   transform_later(newmem);
 318 
 319   uint alias_idx = C->get_alias_index(adr_type);
 320   if (alias_idx != Compile::AliasIdxBot) {
 321     *mem = MergeMemNode::make(*mem);
 322     (*mem)->set_memory_at(alias_idx, newmem);
 323   } else {
 324     *mem = MergeMemNode::make(newmem);
 325   }
 326   transform_later(*mem);
 327 }
 328 
 329 address PhaseMacroExpand::basictype2arraycopy(BasicType t,
 330                                               Node* src_offset,

 343   // or they are identical (which we can treat as disjoint.)  We can also
 344   // treat a copy with a destination index  less that the source index
 345   // as disjoint since a low->high copy will work correctly in this case.
 346   if (src_offset_inttype != NULL && src_offset_inttype->is_con() &&
 347       dest_offset_inttype != NULL && dest_offset_inttype->is_con()) {
 348     // both indices are constants
 349     int s_offs = src_offset_inttype->get_con();
 350     int d_offs = dest_offset_inttype->get_con();
 351     int element_size = type2aelembytes(t);
 352     aligned = ((arrayOopDesc::base_offset_in_bytes(t) + s_offs * element_size) % HeapWordSize == 0) &&
 353               ((arrayOopDesc::base_offset_in_bytes(t) + d_offs * element_size) % HeapWordSize == 0);
 354     if (s_offs >= d_offs)  disjoint = true;
 355   } else if (src_offset == dest_offset && src_offset != NULL) {
 356     // This can occur if the offsets are identical non-constants.
 357     disjoint = true;
 358   }
 359 
 360   return StubRoutines::select_arraycopy_function(t, aligned, disjoint, name, dest_uninitialized);
 361 }
 362 
 363 bool PhaseMacroExpand::can_try_zeroing_elimination(AllocateArrayNode* alloc,
 364                                                    Node* src,
 365                                                    Node* dest) const {
 366   const TypeAryPtr* top_dest = _igvn.type(dest)->isa_aryptr();
 367 
 368   if (top_dest != NULL) {
 369     if (top_dest->klass() == NULL) {
 370       return false;
 371     }
 372   }
 373 
 374   return ReduceBulkZeroing
 375     && !(UseTLAB && ZeroTLAB) // pointless if already zeroed
 376     && !src->eqv_uncast(dest)
 377     && alloc != NULL
 378     && _igvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0
 379     && alloc->maybe_set_complete(&_igvn);
 380 }
 381 
 382 #define XTOP LP64_ONLY(COMMA top())
 383 
 384 // Generate an optimized call to arraycopy.
 385 // Caller must guard against non-arrays.
 386 // Caller must determine a common array basic-type for both arrays.
 387 // Caller must validate offsets against array bounds.
 388 // The slow_region has already collected guard failure paths
 389 // (such as out of bounds length or non-conformable array types).
 390 // The generated code has this shape, in general:
 391 //
 392 //     if (length == 0)  return   // via zero_path
 393 //     slowval = -1
 394 //     if (types unknown) {
 395 //       slowval = call generic copy loop
 396 //       if (slowval == 0)  return  // via checked_path
 397 //     } else if (indexes in bounds) {
 398 //       if ((is object array) && !(array type check)) {
 399 //         slowval = call checked copy loop
 400 //         if (slowval == 0)  return  // via checked_path
 401 //       } else {

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

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

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

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

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

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

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

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

1281   if (exit_block) {
1282     exit_block->init_req(2, *ctrl);
1283 
1284     // Memory edge corresponding to stub_region.
1285     result_memory->init_req(2, *mem);
1286 
1287     uint alias_idx = C->get_alias_index(adr_type);
1288     if (alias_idx != Compile::AliasIdxBot) {
1289       *mem = MergeMemNode::make(*mem);
1290       (*mem)->set_memory_at(alias_idx, result_memory);
1291     } else {
1292       *mem = MergeMemNode::make(result_memory);
1293     }
1294     transform_later(*mem);
1295     *ctrl = exit_block;
1296     return true;
1297   }
1298   return false;
1299 }
1300 
1301 const TypePtr* PhaseMacroExpand::adjust_for_flat_array(const TypeAryPtr* top_dest, Node*& src_offset,
1302                                                        Node*& dest_offset, Node*& length, BasicType& dest_elem,
1303                                                        Node*& dest_length) {
1304 #ifdef ASSERT
1305   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1306   bool needs_barriers = top_dest->elem()->inline_klass()->contains_oops() &&
1307     bs->array_copy_requires_gc_barriers(dest_length != NULL, T_OBJECT, false, false, BarrierSetC2::Optimization);
1308   assert(!needs_barriers || StressReflectiveCode, "Flat arracopy would require GC barriers");
1309 #endif
1310   int elem_size = top_dest->klass()->as_flat_array_klass()->element_byte_size();
1311   if (elem_size >= 8) {
1312     if (elem_size > 8) {
1313       // treat as array of long but scale length, src offset and dest offset
1314       assert((elem_size % 8) == 0, "not a power of 2?");
1315       int factor = elem_size / 8;
1316       length = transform_later(new MulINode(length, intcon(factor)));
1317       src_offset = transform_later(new MulINode(src_offset, intcon(factor)));
1318       dest_offset = transform_later(new MulINode(dest_offset, intcon(factor)));
1319       if (dest_length != NULL) {
1320         dest_length = transform_later(new MulINode(dest_length, intcon(factor)));
1321       }
1322       elem_size = 8;
1323     }
1324     dest_elem = T_LONG;
1325   } else if (elem_size == 4) {
1326     dest_elem = T_INT;
1327   } else if (elem_size == 2) {
1328     dest_elem = T_CHAR;
1329   } else if (elem_size == 1) {
1330     dest_elem = T_BYTE;
1331   } else {
1332     ShouldNotReachHere();
1333   }
1334   return TypeRawPtr::BOTTOM;
1335 }
1336 
1337 #undef XTOP
1338 
1339 void PhaseMacroExpand::expand_arraycopy_node(ArrayCopyNode *ac) {
1340   Node* ctrl = ac->in(TypeFunc::Control);
1341   Node* io = ac->in(TypeFunc::I_O);
1342   Node* src = ac->in(ArrayCopyNode::Src);
1343   Node* src_offset = ac->in(ArrayCopyNode::SrcPos);
1344   Node* dest = ac->in(ArrayCopyNode::Dest);
1345   Node* dest_offset = ac->in(ArrayCopyNode::DestPos);
1346   Node* length = ac->in(ArrayCopyNode::Length);
1347   MergeMemNode* merge_mem = NULL;
1348 
1349   if (ac->is_clonebasic()) {
1350     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1351     bs->clone_at_expansion(this, ac);
1352     return;
1353   } else if (ac->is_copyof() || ac->is_copyofrange() || ac->is_clone_oop_array()) {
1354     const Type* src_type = _igvn.type(src);
1355     const Type* dest_type = _igvn.type(dest);
1356     const TypeAryPtr* top_src = src_type->isa_aryptr();
1357     const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1358     BasicType dest_elem = T_OBJECT;
1359     if (top_dest != NULL && top_dest->klass() != NULL) {
1360       dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
1361     }
1362     if (dest_elem == T_ARRAY || (dest_elem == T_PRIMITIVE_OBJECT && top_dest->klass()->is_obj_array_klass())) {
1363       dest_elem = T_OBJECT;
1364     }
1365     if (top_src != NULL && top_src->is_flat()) {
1366       // If src is flat, dest is guaranteed to be flat as well
1367       dest_elem = T_PRIMITIVE_OBJECT;
1368       top_dest = top_src;
1369     }
1370 
1371     AllocateArrayNode* alloc = NULL;
1372     Node* dest_length = NULL;
1373     if (ac->is_alloc_tightly_coupled()) {
1374       alloc = AllocateArrayNode::Ideal_array_allocation(dest, &_igvn);
1375       assert(alloc != NULL, "expect alloc");
1376       dest_length = alloc->in(AllocateNode::ALength);
1377     }
1378 
1379     Node* mem = ac->in(TypeFunc::Memory);
1380     const TypePtr* adr_type = NULL;
1381     if (dest_elem == T_PRIMITIVE_OBJECT) {
1382       assert(dest_length != NULL || StressReflectiveCode, "must be tightly coupled");
1383       // Copy to a flat array modifies multiple memory slices. Conservatively insert a barrier
1384       // on all slices to prevent writes into the source from floating below the arraycopy.
1385       insert_mem_bar(&ctrl, &mem, Op_MemBarCPUOrder);
1386       adr_type = adjust_for_flat_array(top_dest, src_offset, dest_offset, length, dest_elem, dest_length);
1387     } else {
1388       adr_type = dest_type->is_oopptr()->add_offset(Type::OffsetBot);
1389       if (ac->_dest_type != TypeOopPtr::BOTTOM) {
1390         adr_type = ac->_dest_type->add_offset(Type::OffsetBot)->is_ptr();
1391       }
1392       if (ac->_src_type != ac->_dest_type) {
1393         adr_type = TypeRawPtr::BOTTOM;
1394       }
1395     }
1396     merge_mem = MergeMemNode::make(mem);
1397     transform_later(merge_mem);
1398 
1399     generate_arraycopy(ac, alloc, &ctrl, merge_mem, &io,
1400                        adr_type, dest_elem,
1401                        src, src_offset, dest, dest_offset, length,
1402                        dest_length,
1403                        true, !ac->is_copyofrange());

1404     return;
1405   }
1406 
1407   AllocateArrayNode* alloc = NULL;
1408   if (ac->is_alloc_tightly_coupled()) {
1409     alloc = AllocateArrayNode::Ideal_array_allocation(dest, &_igvn);
1410     assert(alloc != NULL, "expect alloc");
1411   }
1412 
1413   assert(ac->is_arraycopy() || ac->is_arraycopy_validated(), "should be an arraycopy");
1414 
1415   // Compile time checks.  If any of these checks cannot be verified at compile time,
1416   // we do not make a fast path for this call.  Instead, we let the call remain as it
1417   // is.  The checks we choose to mandate at compile time are:
1418   //
1419   // (1) src and dest are arrays.
1420   const Type* src_type = src->Value(&_igvn);
1421   const Type* dest_type = dest->Value(&_igvn);
1422   const TypeAryPtr* top_src = src_type->isa_aryptr();
1423   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
1424 
1425   BasicType src_elem = T_CONFLICT;
1426   BasicType dest_elem = T_CONFLICT;
1427 
1428   if (top_dest != NULL && top_dest->klass() != NULL) {
1429     dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
1430   }
1431   if (top_src != NULL && top_src->klass() != NULL) {
1432     src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
1433   }
1434   if (src_elem == T_ARRAY || (src_elem == T_PRIMITIVE_OBJECT && top_src->klass()->is_obj_array_klass())) {
1435     src_elem = T_OBJECT;
1436   }
1437   if (dest_elem == T_ARRAY || (dest_elem == T_PRIMITIVE_OBJECT && top_dest->klass()->is_obj_array_klass())) {
1438     dest_elem = T_OBJECT;
1439   }
1440 
1441   if (ac->is_arraycopy_validated() && dest_elem != T_CONFLICT && src_elem == T_CONFLICT) {


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