hotspot/src/share/vm/opto/library_call.cpp
changeset 26166 4b49fd58bbd9
parent 25930 eae8b7490d2c
child 26171 ddd28cb3f2b9
equal deleted inserted replaced
25938:d1161ea75e14 26166:4b49fd58bbd9
   144   Node* generate_slow_guard(Node* test, RegionNode* region);
   144   Node* generate_slow_guard(Node* test, RegionNode* region);
   145   Node* generate_fair_guard(Node* test, RegionNode* region);
   145   Node* generate_fair_guard(Node* test, RegionNode* region);
   146   Node* generate_negative_guard(Node* index, RegionNode* region,
   146   Node* generate_negative_guard(Node* index, RegionNode* region,
   147                                 // resulting CastII of index:
   147                                 // resulting CastII of index:
   148                                 Node* *pos_index = NULL);
   148                                 Node* *pos_index = NULL);
   149   Node* generate_nonpositive_guard(Node* index, bool never_negative,
       
   150                                    // resulting CastII of index:
       
   151                                    Node* *pos_index = NULL);
       
   152   Node* generate_limit_guard(Node* offset, Node* subseq_length,
   149   Node* generate_limit_guard(Node* offset, Node* subseq_length,
   153                              Node* array_length,
   150                              Node* array_length,
   154                              RegionNode* region);
   151                              RegionNode* region);
   155   Node* generate_current_thread(Node* &tls_output);
   152   Node* generate_current_thread(Node* &tls_output);
   156   address basictype2arraycopy(BasicType t, Node *src_offset, Node *dest_offset,
       
   157                               bool disjoint_bases, const char* &name, bool dest_uninitialized);
       
   158   Node* load_mirror_from_klass(Node* klass);
   153   Node* load_mirror_from_klass(Node* klass);
   159   Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
   154   Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
   160                                       RegionNode* region, int null_path,
   155                                       RegionNode* region, int null_path,
   161                                       int offset);
   156                                       int offset);
   162   Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
   157   Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
   262   bool inline_native_hashcode(bool is_virtual, bool is_static);
   257   bool inline_native_hashcode(bool is_virtual, bool is_static);
   263   bool inline_native_getClass();
   258   bool inline_native_getClass();
   264 
   259 
   265   // Helper functions for inlining arraycopy
   260   // Helper functions for inlining arraycopy
   266   bool inline_arraycopy();
   261   bool inline_arraycopy();
   267   void generate_arraycopy(const TypePtr* adr_type,
       
   268                           BasicType basic_elem_type,
       
   269                           Node* src,  Node* src_offset,
       
   270                           Node* dest, Node* dest_offset,
       
   271                           Node* copy_length,
       
   272                           bool disjoint_bases = false,
       
   273                           bool length_never_negative = false,
       
   274                           RegionNode* slow_region = NULL);
       
   275   AllocateArrayNode* tightly_coupled_allocation(Node* ptr,
   262   AllocateArrayNode* tightly_coupled_allocation(Node* ptr,
   276                                                 RegionNode* slow_region);
   263                                                 RegionNode* slow_region);
   277   void generate_clear_array(const TypePtr* adr_type,
       
   278                             Node* dest,
       
   279                             BasicType basic_elem_type,
       
   280                             Node* slice_off,
       
   281                             Node* slice_len,
       
   282                             Node* slice_end);
       
   283   bool generate_block_arraycopy(const TypePtr* adr_type,
       
   284                                 BasicType basic_elem_type,
       
   285                                 AllocateNode* alloc,
       
   286                                 Node* src,  Node* src_offset,
       
   287                                 Node* dest, Node* dest_offset,
       
   288                                 Node* dest_size, bool dest_uninitialized);
       
   289   void generate_slow_arraycopy(const TypePtr* adr_type,
       
   290                                Node* src,  Node* src_offset,
       
   291                                Node* dest, Node* dest_offset,
       
   292                                Node* copy_length, bool dest_uninitialized);
       
   293   Node* generate_checkcast_arraycopy(const TypePtr* adr_type,
       
   294                                      Node* dest_elem_klass,
       
   295                                      Node* src,  Node* src_offset,
       
   296                                      Node* dest, Node* dest_offset,
       
   297                                      Node* copy_length, bool dest_uninitialized);
       
   298   Node* generate_generic_arraycopy(const TypePtr* adr_type,
       
   299                                    Node* src,  Node* src_offset,
       
   300                                    Node* dest, Node* dest_offset,
       
   301                                    Node* copy_length, bool dest_uninitialized);
       
   302   void generate_unchecked_arraycopy(const TypePtr* adr_type,
       
   303                                     BasicType basic_elem_type,
       
   304                                     bool disjoint_bases,
       
   305                                     Node* src,  Node* src_offset,
       
   306                                     Node* dest, Node* dest_offset,
       
   307                                     Node* copy_length, bool dest_uninitialized);
       
   308   typedef enum { LS_xadd, LS_xchg, LS_cmpxchg } LoadStoreKind;
   264   typedef enum { LS_xadd, LS_xchg, LS_cmpxchg } LoadStoreKind;
   309   bool inline_unsafe_load_store(BasicType type,  LoadStoreKind kind);
   265   bool inline_unsafe_load_store(BasicType type,  LoadStoreKind kind);
   310   bool inline_unsafe_ordered_store(BasicType type);
   266   bool inline_unsafe_ordered_store(BasicType type);
   311   bool inline_unsafe_fence(vmIntrinsics::ID id);
   267   bool inline_unsafe_fence(vmIntrinsics::ID id);
   312   bool inline_fp_conversions(vmIntrinsics::ID id);
   268   bool inline_fp_conversions(vmIntrinsics::ID id);
  1045     Node* ccast = new CastIINode(index, TypeInt::POS);
  1001     Node* ccast = new CastIINode(index, TypeInt::POS);
  1046     ccast->set_req(0, control());
  1002     ccast->set_req(0, control());
  1047     (*pos_index) = _gvn.transform(ccast);
  1003     (*pos_index) = _gvn.transform(ccast);
  1048   }
  1004   }
  1049   return is_neg;
  1005   return is_neg;
  1050 }
       
  1051 
       
  1052 inline Node* LibraryCallKit::generate_nonpositive_guard(Node* index, bool never_negative,
       
  1053                                                         Node* *pos_index) {
       
  1054   if (stopped())
       
  1055     return NULL;                // already stopped
       
  1056   if (_gvn.type(index)->higher_equal(TypeInt::POS1)) // [1,maxint]
       
  1057     return NULL;                // index is already adequately typed
       
  1058   Node* cmp_le = _gvn.transform(new CmpINode(index, intcon(0)));
       
  1059   BoolTest::mask le_or_eq = (never_negative ? BoolTest::eq : BoolTest::le);
       
  1060   Node* bol_le = _gvn.transform(new BoolNode(cmp_le, le_or_eq));
       
  1061   Node* is_notp = generate_guard(bol_le, NULL, PROB_MIN);
       
  1062   if (is_notp != NULL && pos_index != NULL) {
       
  1063     // Emulate effect of Parse::adjust_map_after_if.
       
  1064     Node* ccast = new CastIINode(index, TypeInt::POS1);
       
  1065     ccast->set_req(0, control());
       
  1066     (*pos_index) = _gvn.transform(ccast);
       
  1067   }
       
  1068   return is_notp;
       
  1069 }
  1006 }
  1070 
  1007 
  1071 // Make sure that 'position' is a valid limit index, in [0..length].
  1008 // Make sure that 'position' is a valid limit index, in [0..length].
  1072 // There are two equivalent plans for checking this:
  1009 // There are two equivalent plans for checking this:
  1073 //   A. (offset + copyLength)  unsigned<=  arrayLength
  1010 //   A. (offset + copyLength)  unsigned<=  arrayLength
  3926       // Generate a direct call to the right arraycopy function(s).
  3863       // Generate a direct call to the right arraycopy function(s).
  3927       // We know the copy is disjoint but we might not know if the
  3864       // We know the copy is disjoint but we might not know if the
  3928       // oop stores need checking.
  3865       // oop stores need checking.
  3929       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
  3866       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
  3930       // This will fail a store-check if x contains any non-nulls.
  3867       // This will fail a store-check if x contains any non-nulls.
  3931       bool disjoint_bases = true;
  3868 
  3932       // if start > orig_length then the length of the copy may be
  3869       Node* alloc = tightly_coupled_allocation(newcopy, NULL);
  3933       // negative.
  3870 
  3934       bool length_never_negative = !is_copyOfRange;
  3871       ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, alloc != NULL);
  3935       generate_arraycopy(TypeAryPtr::OOPS, T_OBJECT,
  3872       if (!is_copyOfRange) {
  3936                          original, start, newcopy, intcon(0), moved,
  3873         ac->set_copyof();
  3937                          disjoint_bases, length_never_negative);
  3874       } else {
       
  3875         ac->set_copyofrange();
       
  3876       }
       
  3877       Node* n = _gvn.transform(ac);
       
  3878       assert(n == ac, "cannot disappear");
       
  3879       ac->connect_outputs(this);
  3938     }
  3880     }
  3939   } // original reexecute is set back here
  3881   } // original reexecute is set back here
  3940 
  3882 
  3941   C->set_has_split_ifs(true); // Has chance for split-if optimization
  3883   C->set_has_split_ifs(true); // Has chance for split-if optimization
  3942   if (!stopped()) {
  3884   if (!stopped()) {
  4443   Node* countx = size;
  4385   Node* countx = size;
  4444   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
  4386   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
  4445   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
  4387   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
  4446 
  4388 
  4447   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
  4389   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
  4448   bool disjoint_bases = true;
  4390 
  4449   generate_unchecked_arraycopy(raw_adr_type, T_LONG, disjoint_bases,
  4391   ArrayCopyNode* ac = ArrayCopyNode::make(this, false, src, NULL, dest, NULL, countx, false);
  4450                                src, NULL, dest, NULL, countx,
  4392   ac->set_clonebasic();
  4451                                /*dest_uninitialized*/true);
  4393   Node* n = _gvn.transform(ac);
       
  4394   assert(n == ac, "cannot disappear");
       
  4395   set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), raw_adr_type);
  4452 
  4396 
  4453   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
  4397   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
  4454   if (card_mark) {
  4398   if (card_mark) {
  4455     assert(!is_array, "");
  4399     assert(!is_array, "");
  4456     // Put in store barrier for any and all oops we are sticking
  4400     // Put in store barrier for any and all oops we are sticking
  4555         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
  4499         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
  4556         if (is_obja != NULL) {
  4500         if (is_obja != NULL) {
  4557           PreserveJVMState pjvms2(this);
  4501           PreserveJVMState pjvms2(this);
  4558           set_control(is_obja);
  4502           set_control(is_obja);
  4559           // Generate a direct call to the right arraycopy function(s).
  4503           // Generate a direct call to the right arraycopy function(s).
  4560           bool disjoint_bases = true;
  4504           Node* alloc = tightly_coupled_allocation(alloc_obj, NULL);
  4561           bool length_never_negative = true;
  4505           ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, alloc != NULL);
  4562           generate_arraycopy(TypeAryPtr::OOPS, T_OBJECT,
  4506           ac->set_cloneoop();
  4563                              obj, intcon(0), alloc_obj, intcon(0),
  4507           Node* n = _gvn.transform(ac);
  4564                              obj_length,
  4508           assert(n == ac, "cannot disappear");
  4565                              disjoint_bases, length_never_negative);
  4509           ac->connect_outputs(this);
       
  4510 
  4566           result_reg->init_req(_objArray_path, control());
  4511           result_reg->init_req(_objArray_path, control());
  4567           result_val->init_req(_objArray_path, alloc_obj);
  4512           result_val->init_req(_objArray_path, alloc_obj);
  4568           result_i_o ->set_req(_objArray_path, i_o());
  4513           result_i_o ->set_req(_objArray_path, i_o());
  4569           result_mem ->set_req(_objArray_path, reset_memory());
  4514           result_mem ->set_req(_objArray_path, reset_memory());
  4570         }
  4515         }
  4654 
  4599 
  4655   set_result(_gvn.transform(result_val));
  4600   set_result(_gvn.transform(result_val));
  4656   return true;
  4601   return true;
  4657 }
  4602 }
  4658 
  4603 
  4659 //------------------------------basictype2arraycopy----------------------------
       
  4660 address LibraryCallKit::basictype2arraycopy(BasicType t,
       
  4661                                             Node* src_offset,
       
  4662                                             Node* dest_offset,
       
  4663                                             bool disjoint_bases,
       
  4664                                             const char* &name,
       
  4665                                             bool dest_uninitialized) {
       
  4666   const TypeInt* src_offset_inttype  = gvn().find_int_type(src_offset);;
       
  4667   const TypeInt* dest_offset_inttype = gvn().find_int_type(dest_offset);;
       
  4668 
       
  4669   bool aligned = false;
       
  4670   bool disjoint = disjoint_bases;
       
  4671 
       
  4672   // if the offsets are the same, we can treat the memory regions as
       
  4673   // disjoint, because either the memory regions are in different arrays,
       
  4674   // or they are identical (which we can treat as disjoint.)  We can also
       
  4675   // treat a copy with a destination index  less that the source index
       
  4676   // as disjoint since a low->high copy will work correctly in this case.
       
  4677   if (src_offset_inttype != NULL && src_offset_inttype->is_con() &&
       
  4678       dest_offset_inttype != NULL && dest_offset_inttype->is_con()) {
       
  4679     // both indices are constants
       
  4680     int s_offs = src_offset_inttype->get_con();
       
  4681     int d_offs = dest_offset_inttype->get_con();
       
  4682     int element_size = type2aelembytes(t);
       
  4683     aligned = ((arrayOopDesc::base_offset_in_bytes(t) + s_offs * element_size) % HeapWordSize == 0) &&
       
  4684               ((arrayOopDesc::base_offset_in_bytes(t) + d_offs * element_size) % HeapWordSize == 0);
       
  4685     if (s_offs >= d_offs)  disjoint = true;
       
  4686   } else if (src_offset == dest_offset && src_offset != NULL) {
       
  4687     // This can occur if the offsets are identical non-constants.
       
  4688     disjoint = true;
       
  4689   }
       
  4690 
       
  4691   return StubRoutines::select_arraycopy_function(t, aligned, disjoint, name, dest_uninitialized);
       
  4692 }
       
  4693 
       
  4694 
       
  4695 //------------------------------inline_arraycopy-----------------------
  4604 //------------------------------inline_arraycopy-----------------------
  4696 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
  4605 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
  4697 //                                                      Object dest, int destPos,
  4606 //                                                      Object dest, int destPos,
  4698 //                                                      int length);
  4607 //                                                      int length);
  4699 bool LibraryCallKit::inline_arraycopy() {
  4608 bool LibraryCallKit::inline_arraycopy() {
  4702   Node* src_offset  = argument(1);  // type: int
  4611   Node* src_offset  = argument(1);  // type: int
  4703   Node* dest        = argument(2);  // type: oop
  4612   Node* dest        = argument(2);  // type: oop
  4704   Node* dest_offset = argument(3);  // type: int
  4613   Node* dest_offset = argument(3);  // type: int
  4705   Node* length      = argument(4);  // type: int
  4614   Node* length      = argument(4);  // type: int
  4706 
  4615 
  4707   // Compile time checks.  If any of these checks cannot be verified at compile time,
  4616   // The following tests must be performed
  4708   // we do not make a fast path for this call.  Instead, we let the call remain as it
       
  4709   // is.  The checks we choose to mandate at compile time are:
       
  4710   //
       
  4711   // (1) src and dest are arrays.
  4617   // (1) src and dest are arrays.
  4712   const Type* src_type  = src->Value(&_gvn);
  4618   // (2) src and dest arrays must have elements of the same BasicType
  4713   const Type* dest_type = dest->Value(&_gvn);
  4619   // (3) src and dest must not be null.
       
  4620   // (4) src_offset must not be negative.
       
  4621   // (5) dest_offset must not be negative.
       
  4622   // (6) length must not be negative.
       
  4623   // (7) src_offset + length must not exceed length of src.
       
  4624   // (8) dest_offset + length must not exceed length of dest.
       
  4625   // (9) each element of an oop array must be assignable
       
  4626 
       
  4627   // (3) src and dest must not be null.
       
  4628   // always do this here because we need the JVM state for uncommon traps
       
  4629   src  = null_check(src,  T_ARRAY);
       
  4630   dest = null_check(dest, T_ARRAY);
       
  4631 
       
  4632   bool notest = false;
       
  4633 
       
  4634   const Type* src_type  = _gvn.type(src);
       
  4635   const Type* dest_type = _gvn.type(dest);
  4714   const TypeAryPtr* top_src  = src_type->isa_aryptr();
  4636   const TypeAryPtr* top_src  = src_type->isa_aryptr();
  4715   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
  4637   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
  4716 
  4638 
  4717   // Do we have the type of src?
  4639   // Do we have the type of src?
  4718   bool has_src = (top_src != NULL && top_src->klass() != NULL);
  4640   bool has_src = (top_src != NULL && top_src->klass() != NULL);
  4766         dest_spec = true;
  4688         dest_spec = true;
  4767       }
  4689       }
  4768     }
  4690     }
  4769   }
  4691   }
  4770 
  4692 
  4771   if (!has_src || !has_dest) {
  4693   if (has_src && has_dest) {
  4772     // Conservatively insert a memory barrier on all memory slices.
  4694     BasicType src_elem  = top_src->klass()->as_array_klass()->element_type()->basic_type();
  4773     // Do not let writes into the source float below the arraycopy.
  4695     BasicType dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
  4774     insert_mem_bar(Op_MemBarCPUOrder);
  4696     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
  4775 
  4697     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
  4776     // Call StubRoutines::generic_arraycopy stub.
  4698 
  4777     generate_arraycopy(TypeRawPtr::BOTTOM, T_CONFLICT,
  4699     if (src_elem == dest_elem && src_elem == T_OBJECT) {
  4778                        src, src_offset, dest, dest_offset, length);
  4700       // If both arrays are object arrays then having the exact types
  4779 
  4701       // for both will remove the need for a subtype check at runtime
  4780     // Do not let reads from the destination float above the arraycopy.
  4702       // before the call and may make it possible to pick a faster copy
  4781     // Since we cannot type the arrays, we don't know which slices
  4703       // routine (without a subtype check on every element)
  4782     // might be affected.  We could restrict this barrier only to those
  4704       // Do we have the exact type of src?
  4783     // memory slices which pertain to array elements--but don't bother.
  4705       bool could_have_src = src_spec;
  4784     if (!InsertMemBarAfterArraycopy)
  4706       // Do we have the exact type of dest?
  4785       // (If InsertMemBarAfterArraycopy, there is already one in place.)
  4707       bool could_have_dest = dest_spec;
  4786       insert_mem_bar(Op_MemBarCPUOrder);
  4708       ciKlass* src_k = top_src->klass();
  4787     return true;
  4709       ciKlass* dest_k = top_dest->klass();
  4788   }
  4710       if (!src_spec) {
  4789 
  4711         src_k = src_type->speculative_type_not_null();
  4790   // (2) src and dest arrays must have elements of the same BasicType
  4712         if (src_k != NULL && src_k->is_array_klass()) {
  4791   // Figure out the size and type of the elements we will be copying.
       
  4792   BasicType src_elem  =  top_src->klass()->as_array_klass()->element_type()->basic_type();
       
  4793   BasicType dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
       
  4794   if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
       
  4795   if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
       
  4796 
       
  4797   if (src_elem != dest_elem || dest_elem == T_VOID) {
       
  4798     // The component types are not the same or are not recognized.  Punt.
       
  4799     // (But, avoid the native method wrapper to JVM_ArrayCopy.)
       
  4800     generate_slow_arraycopy(TypePtr::BOTTOM,
       
  4801                             src, src_offset, dest, dest_offset, length,
       
  4802                             /*dest_uninitialized*/false);
       
  4803     return true;
       
  4804   }
       
  4805 
       
  4806   if (src_elem == T_OBJECT) {
       
  4807     // If both arrays are object arrays then having the exact types
       
  4808     // for both will remove the need for a subtype check at runtime
       
  4809     // before the call and may make it possible to pick a faster copy
       
  4810     // routine (without a subtype check on every element)
       
  4811     // Do we have the exact type of src?
       
  4812     bool could_have_src = src_spec;
       
  4813     // Do we have the exact type of dest?
       
  4814     bool could_have_dest = dest_spec;
       
  4815     ciKlass* src_k = top_src->klass();
       
  4816     ciKlass* dest_k = top_dest->klass();
       
  4817     if (!src_spec) {
       
  4818       src_k = src_type->speculative_type_not_null();
       
  4819       if (src_k != NULL && src_k->is_array_klass()) {
       
  4820           could_have_src = true;
  4713           could_have_src = true;
  4821       }
       
  4822     }
       
  4823     if (!dest_spec) {
       
  4824       dest_k = dest_type->speculative_type_not_null();
       
  4825       if (dest_k != NULL && dest_k->is_array_klass()) {
       
  4826         could_have_dest = true;
       
  4827       }
       
  4828     }
       
  4829     if (could_have_src && could_have_dest) {
       
  4830       // If we can have both exact types, emit the missing guards
       
  4831       if (could_have_src && !src_spec) {
       
  4832         src = maybe_cast_profiled_obj(src, src_k);
       
  4833       }
       
  4834       if (could_have_dest && !dest_spec) {
       
  4835         dest = maybe_cast_profiled_obj(dest, dest_k);
       
  4836       }
       
  4837     }
       
  4838   }
       
  4839 
       
  4840   //---------------------------------------------------------------------------
       
  4841   // We will make a fast path for this call to arraycopy.
       
  4842 
       
  4843   // We have the following tests left to perform:
       
  4844   //
       
  4845   // (3) src and dest must not be null.
       
  4846   // (4) src_offset must not be negative.
       
  4847   // (5) dest_offset must not be negative.
       
  4848   // (6) length must not be negative.
       
  4849   // (7) src_offset + length must not exceed length of src.
       
  4850   // (8) dest_offset + length must not exceed length of dest.
       
  4851   // (9) each element of an oop array must be assignable
       
  4852 
       
  4853   RegionNode* slow_region = new RegionNode(1);
       
  4854   record_for_igvn(slow_region);
       
  4855 
       
  4856   // (3) operands must not be null
       
  4857   // We currently perform our null checks with the null_check routine.
       
  4858   // This means that the null exceptions will be reported in the caller
       
  4859   // rather than (correctly) reported inside of the native arraycopy call.
       
  4860   // This should be corrected, given time.  We do our null check with the
       
  4861   // stack pointer restored.
       
  4862   src  = null_check(src,  T_ARRAY);
       
  4863   dest = null_check(dest, T_ARRAY);
       
  4864 
       
  4865   // (4) src_offset must not be negative.
       
  4866   generate_negative_guard(src_offset, slow_region);
       
  4867 
       
  4868   // (5) dest_offset must not be negative.
       
  4869   generate_negative_guard(dest_offset, slow_region);
       
  4870 
       
  4871   // (6) length must not be negative (moved to generate_arraycopy()).
       
  4872   // generate_negative_guard(length, slow_region);
       
  4873 
       
  4874   // (7) src_offset + length must not exceed length of src.
       
  4875   generate_limit_guard(src_offset, length,
       
  4876                        load_array_length(src),
       
  4877                        slow_region);
       
  4878 
       
  4879   // (8) dest_offset + length must not exceed length of dest.
       
  4880   generate_limit_guard(dest_offset, length,
       
  4881                        load_array_length(dest),
       
  4882                        slow_region);
       
  4883 
       
  4884   // (9) each element of an oop array must be assignable
       
  4885   // The generate_arraycopy subroutine checks this.
       
  4886 
       
  4887   // This is where the memory effects are placed:
       
  4888   const TypePtr* adr_type = TypeAryPtr::get_array_body_type(dest_elem);
       
  4889   generate_arraycopy(adr_type, dest_elem,
       
  4890                      src, src_offset, dest, dest_offset, length,
       
  4891                      false, false, slow_region);
       
  4892 
       
  4893   return true;
       
  4894 }
       
  4895 
       
  4896 //-----------------------------generate_arraycopy----------------------
       
  4897 // Generate an optimized call to arraycopy.
       
  4898 // Caller must guard against non-arrays.
       
  4899 // Caller must determine a common array basic-type for both arrays.
       
  4900 // Caller must validate offsets against array bounds.
       
  4901 // The slow_region has already collected guard failure paths
       
  4902 // (such as out of bounds length or non-conformable array types).
       
  4903 // The generated code has this shape, in general:
       
  4904 //
       
  4905 //     if (length == 0)  return   // via zero_path
       
  4906 //     slowval = -1
       
  4907 //     if (types unknown) {
       
  4908 //       slowval = call generic copy loop
       
  4909 //       if (slowval == 0)  return  // via checked_path
       
  4910 //     } else if (indexes in bounds) {
       
  4911 //       if ((is object array) && !(array type check)) {
       
  4912 //         slowval = call checked copy loop
       
  4913 //         if (slowval == 0)  return  // via checked_path
       
  4914 //       } else {
       
  4915 //         call bulk copy loop
       
  4916 //         return  // via fast_path
       
  4917 //       }
       
  4918 //     }
       
  4919 //     // adjust params for remaining work:
       
  4920 //     if (slowval != -1) {
       
  4921 //       n = -1^slowval; src_offset += n; dest_offset += n; length -= n
       
  4922 //     }
       
  4923 //   slow_region:
       
  4924 //     call slow arraycopy(src, src_offset, dest, dest_offset, length)
       
  4925 //     return  // via slow_call_path
       
  4926 //
       
  4927 // This routine is used from several intrinsics:  System.arraycopy,
       
  4928 // Object.clone (the array subcase), and Arrays.copyOf[Range].
       
  4929 //
       
  4930 void
       
  4931 LibraryCallKit::generate_arraycopy(const TypePtr* adr_type,
       
  4932                                    BasicType basic_elem_type,
       
  4933                                    Node* src,  Node* src_offset,
       
  4934                                    Node* dest, Node* dest_offset,
       
  4935                                    Node* copy_length,
       
  4936                                    bool disjoint_bases,
       
  4937                                    bool length_never_negative,
       
  4938                                    RegionNode* slow_region) {
       
  4939 
       
  4940   if (slow_region == NULL) {
       
  4941     slow_region = new RegionNode(1);
       
  4942     record_for_igvn(slow_region);
       
  4943   }
       
  4944 
       
  4945   Node* original_dest      = dest;
       
  4946   AllocateArrayNode* alloc = NULL;  // used for zeroing, if needed
       
  4947   bool  dest_uninitialized = false;
       
  4948 
       
  4949   // See if this is the initialization of a newly-allocated array.
       
  4950   // If so, we will take responsibility here for initializing it to zero.
       
  4951   // (Note:  Because tightly_coupled_allocation performs checks on the
       
  4952   // out-edges of the dest, we need to avoid making derived pointers
       
  4953   // from it until we have checked its uses.)
       
  4954   if (ReduceBulkZeroing
       
  4955       && !ZeroTLAB              // pointless if already zeroed
       
  4956       && basic_elem_type != T_CONFLICT // avoid corner case
       
  4957       && !src->eqv_uncast(dest)
       
  4958       && ((alloc = tightly_coupled_allocation(dest, slow_region))
       
  4959           != NULL)
       
  4960       && _gvn.find_int_con(alloc->in(AllocateNode::ALength), 1) > 0
       
  4961       && alloc->maybe_set_complete(&_gvn)) {
       
  4962     // "You break it, you buy it."
       
  4963     InitializeNode* init = alloc->initialization();
       
  4964     assert(init->is_complete(), "we just did this");
       
  4965     init->set_complete_with_arraycopy();
       
  4966     assert(dest->is_CheckCastPP(), "sanity");
       
  4967     assert(dest->in(0)->in(0) == init, "dest pinned");
       
  4968     adr_type = TypeRawPtr::BOTTOM;  // all initializations are into raw memory
       
  4969     // From this point on, every exit path is responsible for
       
  4970     // initializing any non-copied parts of the object to zero.
       
  4971     // Also, if this flag is set we make sure that arraycopy interacts properly
       
  4972     // with G1, eliding pre-barriers. See CR 6627983.
       
  4973     dest_uninitialized = true;
       
  4974   } else {
       
  4975     // No zeroing elimination here.
       
  4976     alloc             = NULL;
       
  4977     //original_dest   = dest;
       
  4978     //dest_uninitialized = false;
       
  4979   }
       
  4980 
       
  4981   // Results are placed here:
       
  4982   enum { fast_path        = 1,  // normal void-returning assembly stub
       
  4983          checked_path     = 2,  // special assembly stub with cleanup
       
  4984          slow_call_path   = 3,  // something went wrong; call the VM
       
  4985          zero_path        = 4,  // bypass when length of copy is zero
       
  4986          bcopy_path       = 5,  // copy primitive array by 64-bit blocks
       
  4987          PATH_LIMIT       = 6
       
  4988   };
       
  4989   RegionNode* result_region = new RegionNode(PATH_LIMIT);
       
  4990   PhiNode*    result_i_o    = new PhiNode(result_region, Type::ABIO);
       
  4991   PhiNode*    result_memory = new PhiNode(result_region, Type::MEMORY, adr_type);
       
  4992   record_for_igvn(result_region);
       
  4993   _gvn.set_type_bottom(result_i_o);
       
  4994   _gvn.set_type_bottom(result_memory);
       
  4995   assert(adr_type != TypePtr::BOTTOM, "must be RawMem or a T[] slice");
       
  4996 
       
  4997   // The slow_control path:
       
  4998   Node* slow_control;
       
  4999   Node* slow_i_o = i_o();
       
  5000   Node* slow_mem = memory(adr_type);
       
  5001   debug_only(slow_control = (Node*) badAddress);
       
  5002 
       
  5003   // Checked control path:
       
  5004   Node* checked_control = top();
       
  5005   Node* checked_mem     = NULL;
       
  5006   Node* checked_i_o     = NULL;
       
  5007   Node* checked_value   = NULL;
       
  5008 
       
  5009   if (basic_elem_type == T_CONFLICT) {
       
  5010     assert(!dest_uninitialized, "");
       
  5011     Node* cv = generate_generic_arraycopy(adr_type,
       
  5012                                           src, src_offset, dest, dest_offset,
       
  5013                                           copy_length, dest_uninitialized);
       
  5014     if (cv == NULL)  cv = intcon(-1);  // failure (no stub available)
       
  5015     checked_control = control();
       
  5016     checked_i_o     = i_o();
       
  5017     checked_mem     = memory(adr_type);
       
  5018     checked_value   = cv;
       
  5019     set_control(top());         // no fast path
       
  5020   }
       
  5021 
       
  5022   Node* not_pos = generate_nonpositive_guard(copy_length, length_never_negative);
       
  5023   if (not_pos != NULL) {
       
  5024     PreserveJVMState pjvms(this);
       
  5025     set_control(not_pos);
       
  5026 
       
  5027     // (6) length must not be negative.
       
  5028     if (!length_never_negative) {
       
  5029       generate_negative_guard(copy_length, slow_region);
       
  5030     }
       
  5031 
       
  5032     // copy_length is 0.
       
  5033     if (!stopped() && dest_uninitialized) {
       
  5034       Node* dest_length = alloc->in(AllocateNode::ALength);
       
  5035       if (copy_length->eqv_uncast(dest_length)
       
  5036           || _gvn.find_int_con(dest_length, 1) <= 0) {
       
  5037         // There is no zeroing to do. No need for a secondary raw memory barrier.
       
  5038       } else {
       
  5039         // Clear the whole thing since there are no source elements to copy.
       
  5040         generate_clear_array(adr_type, dest, basic_elem_type,
       
  5041                              intcon(0), NULL,
       
  5042                              alloc->in(AllocateNode::AllocSize));
       
  5043         // Use a secondary InitializeNode as raw memory barrier.
       
  5044         // Currently it is needed only on this path since other
       
  5045         // paths have stub or runtime calls as raw memory barriers.
       
  5046         InitializeNode* init = insert_mem_bar_volatile(Op_Initialize,
       
  5047                                                        Compile::AliasIdxRaw,
       
  5048                                                        top())->as_Initialize();
       
  5049         init->set_complete(&_gvn);  // (there is no corresponding AllocateNode)
       
  5050       }
       
  5051     }
       
  5052 
       
  5053     // Present the results of the fast call.
       
  5054     result_region->init_req(zero_path, control());
       
  5055     result_i_o   ->init_req(zero_path, i_o());
       
  5056     result_memory->init_req(zero_path, memory(adr_type));
       
  5057   }
       
  5058 
       
  5059   if (!stopped() && dest_uninitialized) {
       
  5060     // We have to initialize the *uncopied* part of the array to zero.
       
  5061     // The copy destination is the slice dest[off..off+len].  The other slices
       
  5062     // are dest_head = dest[0..off] and dest_tail = dest[off+len..dest.length].
       
  5063     Node* dest_size   = alloc->in(AllocateNode::AllocSize);
       
  5064     Node* dest_length = alloc->in(AllocateNode::ALength);
       
  5065     Node* dest_tail   = _gvn.transform(new AddINode(dest_offset, copy_length));
       
  5066 
       
  5067     // If there is a head section that needs zeroing, do it now.
       
  5068     if (find_int_con(dest_offset, -1) != 0) {
       
  5069       generate_clear_array(adr_type, dest, basic_elem_type,
       
  5070                            intcon(0), dest_offset,
       
  5071                            NULL);
       
  5072     }
       
  5073 
       
  5074     // Next, perform a dynamic check on the tail length.
       
  5075     // It is often zero, and we can win big if we prove this.
       
  5076     // There are two wins:  Avoid generating the ClearArray
       
  5077     // with its attendant messy index arithmetic, and upgrade
       
  5078     // the copy to a more hardware-friendly word size of 64 bits.
       
  5079     Node* tail_ctl = NULL;
       
  5080     if (!stopped() && !dest_tail->eqv_uncast(dest_length)) {
       
  5081       Node* cmp_lt   = _gvn.transform(new CmpINode(dest_tail, dest_length));
       
  5082       Node* bol_lt   = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
       
  5083       tail_ctl = generate_slow_guard(bol_lt, NULL);
       
  5084       assert(tail_ctl != NULL || !stopped(), "must be an outcome");
       
  5085     }
       
  5086 
       
  5087     // At this point, let's assume there is no tail.
       
  5088     if (!stopped() && alloc != NULL && basic_elem_type != T_OBJECT) {
       
  5089       // There is no tail.  Try an upgrade to a 64-bit copy.
       
  5090       bool didit = false;
       
  5091       { PreserveJVMState pjvms(this);
       
  5092         didit = generate_block_arraycopy(adr_type, basic_elem_type, alloc,
       
  5093                                          src, src_offset, dest, dest_offset,
       
  5094                                          dest_size, dest_uninitialized);
       
  5095         if (didit) {
       
  5096           // Present the results of the block-copying fast call.
       
  5097           result_region->init_req(bcopy_path, control());
       
  5098           result_i_o   ->init_req(bcopy_path, i_o());
       
  5099           result_memory->init_req(bcopy_path, memory(adr_type));
       
  5100         }
  4714         }
  5101       }
  4715       }
  5102       if (didit)
  4716       if (!dest_spec) {
  5103         set_control(top());     // no regular fast path
  4717         dest_k = dest_type->speculative_type_not_null();
  5104     }
  4718         if (dest_k != NULL && dest_k->is_array_klass()) {
  5105 
  4719           could_have_dest = true;
  5106     // Clear the tail, if any.
  4720         }
  5107     if (tail_ctl != NULL) {
       
  5108       Node* notail_ctl = stopped() ? NULL : control();
       
  5109       set_control(tail_ctl);
       
  5110       if (notail_ctl == NULL) {
       
  5111         generate_clear_array(adr_type, dest, basic_elem_type,
       
  5112                              dest_tail, NULL,
       
  5113                              dest_size);
       
  5114       } else {
       
  5115         // Make a local merge.
       
  5116         Node* done_ctl = new RegionNode(3);
       
  5117         Node* done_mem = new PhiNode(done_ctl, Type::MEMORY, adr_type);
       
  5118         done_ctl->init_req(1, notail_ctl);
       
  5119         done_mem->init_req(1, memory(adr_type));
       
  5120         generate_clear_array(adr_type, dest, basic_elem_type,
       
  5121                              dest_tail, NULL,
       
  5122                              dest_size);
       
  5123         done_ctl->init_req(2, control());
       
  5124         done_mem->init_req(2, memory(adr_type));
       
  5125         set_control( _gvn.transform(done_ctl));
       
  5126         set_memory(  _gvn.transform(done_mem), adr_type );
       
  5127       }
  4721       }
  5128     }
  4722       if (could_have_src && could_have_dest) {
  5129   }
  4723         // If we can have both exact types, emit the missing guards
  5130 
  4724         if (could_have_src && !src_spec) {
  5131   BasicType copy_type = basic_elem_type;
  4725           src = maybe_cast_profiled_obj(src, src_k);
  5132   assert(basic_elem_type != T_ARRAY, "caller must fix this");
  4726         }
  5133   if (!stopped() && copy_type == T_OBJECT) {
  4727         if (could_have_dest && !dest_spec) {
  5134     // If src and dest have compatible element types, we can copy bits.
  4728           dest = maybe_cast_profiled_obj(dest, dest_k);
  5135     // Types S[] and D[] are compatible if D is a supertype of S.
  4729         }
  5136     //
  4730       }
  5137     // If they are not, we will use checked_oop_disjoint_arraycopy,
  4731     }
  5138     // which performs a fast optimistic per-oop check, and backs off
  4732   }
  5139     // further to JVM_ArrayCopy on the first per-oop check that fails.
  4733 
  5140     // (Actually, we don't move raw bits only; the GC requires card marks.)
  4734   if (!too_many_traps(Deoptimization::Reason_intrinsic) && !src->is_top() && !dest->is_top()) {
  5141 
  4735     // validate arguments: enables transformation the ArrayCopyNode
  5142     // Get the Klass* for both src and dest
  4736     notest = true;
       
  4737 
       
  4738     RegionNode* slow_region = new RegionNode(1);
       
  4739     record_for_igvn(slow_region);
       
  4740 
       
  4741     // (1) src and dest are arrays.
       
  4742     generate_non_array_guard(load_object_klass(src), slow_region);
       
  4743     generate_non_array_guard(load_object_klass(dest), slow_region);
       
  4744 
       
  4745     // (2) src and dest arrays must have elements of the same BasicType
       
  4746     // done at macro expansion or at Ideal transformation time
       
  4747 
       
  4748     // (4) src_offset must not be negative.
       
  4749     generate_negative_guard(src_offset, slow_region);
       
  4750 
       
  4751     // (5) dest_offset must not be negative.
       
  4752     generate_negative_guard(dest_offset, slow_region);
       
  4753 
       
  4754     // (7) src_offset + length must not exceed length of src.
       
  4755     generate_limit_guard(src_offset, length,
       
  4756                          load_array_length(src),
       
  4757                          slow_region);
       
  4758 
       
  4759     // (8) dest_offset + length must not exceed length of dest.
       
  4760     generate_limit_guard(dest_offset, length,
       
  4761                          load_array_length(dest),
       
  4762                          slow_region);
       
  4763 
       
  4764     // (9) each element of an oop array must be assignable
  5143     Node* src_klass  = load_object_klass(src);
  4765     Node* src_klass  = load_object_klass(src);
  5144     Node* dest_klass = load_object_klass(dest);
  4766     Node* dest_klass = load_object_klass(dest);
  5145 
       
  5146     // Generate the subtype check.
       
  5147     // This might fold up statically, or then again it might not.
       
  5148     //
       
  5149     // Non-static example:  Copying List<String>.elements to a new String[].
       
  5150     // The backing store for a List<String> is always an Object[],
       
  5151     // but its elements are always type String, if the generic types
       
  5152     // are correct at the source level.
       
  5153     //
       
  5154     // Test S[] against D[], not S against D, because (probably)
       
  5155     // the secondary supertype cache is less busy for S[] than S.
       
  5156     // This usually only matters when D is an interface.
       
  5157     Node* not_subtype_ctrl = gen_subtype_check(src_klass, dest_klass);
  4767     Node* not_subtype_ctrl = gen_subtype_check(src_klass, dest_klass);
  5158     // Plug failing path into checked_oop_disjoint_arraycopy
  4768 
  5159     if (not_subtype_ctrl != top()) {
  4769     if (not_subtype_ctrl != top()) {
  5160       PreserveJVMState pjvms(this);
  4770       PreserveJVMState pjvms(this);
  5161       set_control(not_subtype_ctrl);
  4771       set_control(not_subtype_ctrl);
  5162       // (At this point we can assume disjoint_bases, since types differ.)
  4772       uncommon_trap(Deoptimization::Reason_intrinsic,
  5163       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
  4773                     Deoptimization::Action_make_not_entrant);
  5164       Node* p1 = basic_plus_adr(dest_klass, ek_offset);
  4774       assert(stopped(), "Should be stopped");
  5165       Node* n1 = LoadKlassNode::make(_gvn, immutable_memory(), p1, TypeRawPtr::BOTTOM);
  4775     }
  5166       Node* dest_elem_klass = _gvn.transform(n1);
  4776     {
  5167       Node* cv = generate_checkcast_arraycopy(adr_type,
  4777       PreserveJVMState pjvms(this);
  5168                                               dest_elem_klass,
  4778       set_control(_gvn.transform(slow_region));
  5169                                               src, src_offset, dest, dest_offset,
  4779       uncommon_trap(Deoptimization::Reason_intrinsic,
  5170                                               ConvI2X(copy_length), dest_uninitialized);
  4780                     Deoptimization::Action_make_not_entrant);
  5171       if (cv == NULL)  cv = intcon(-1);  // failure (no stub available)
  4781       assert(stopped(), "Should be stopped");
  5172       checked_control = control();
  4782     }
  5173       checked_i_o     = i_o();
  4783   }
  5174       checked_mem     = memory(adr_type);
  4784 
  5175       checked_value   = cv;
  4785   if (stopped()) {
  5176     }
  4786     return true;
  5177     // At this point we know we do not need type checks on oop stores.
  4787   }
  5178 
  4788 
  5179     // Let's see if we need card marks:
  4789   AllocateArrayNode* alloc = tightly_coupled_allocation(dest, NULL);
  5180     if (alloc != NULL && use_ReduceInitialCardMarks()) {
  4790   ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != NULL);
  5181       // If we do not need card marks, copy using the jint or jlong stub.
  4791 
  5182       copy_type = LP64_ONLY(UseCompressedOops ? T_INT : T_LONG) NOT_LP64(T_INT);
  4792   if (notest) {
  5183       assert(type2aelembytes(basic_elem_type) == type2aelembytes(copy_type),
  4793     ac->set_arraycopy_notest();
  5184              "sizes agree");
  4794   }
  5185     }
  4795 
  5186   }
  4796   Node* n = _gvn.transform(ac);
  5187 
  4797   assert(n == ac, "cannot disappear");
  5188   if (!stopped()) {
  4798   ac->connect_outputs(this);
  5189     // Generate the fast path, if possible.
  4799 
  5190     PreserveJVMState pjvms(this);
  4800   return true;
  5191     generate_unchecked_arraycopy(adr_type, copy_type, disjoint_bases,
       
  5192                                  src, src_offset, dest, dest_offset,
       
  5193                                  ConvI2X(copy_length), dest_uninitialized);
       
  5194 
       
  5195     // Present the results of the fast call.
       
  5196     result_region->init_req(fast_path, control());
       
  5197     result_i_o   ->init_req(fast_path, i_o());
       
  5198     result_memory->init_req(fast_path, memory(adr_type));
       
  5199   }
       
  5200 
       
  5201   // Here are all the slow paths up to this point, in one bundle:
       
  5202   slow_control = top();
       
  5203   if (slow_region != NULL)
       
  5204     slow_control = _gvn.transform(slow_region);
       
  5205   DEBUG_ONLY(slow_region = (RegionNode*)badAddress);
       
  5206 
       
  5207   set_control(checked_control);
       
  5208   if (!stopped()) {
       
  5209     // Clean up after the checked call.
       
  5210     // The returned value is either 0 or -1^K,
       
  5211     // where K = number of partially transferred array elements.
       
  5212     Node* cmp = _gvn.transform(new CmpINode(checked_value, intcon(0)));
       
  5213     Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
       
  5214     IfNode* iff = create_and_map_if(control(), bol, PROB_MAX, COUNT_UNKNOWN);
       
  5215 
       
  5216     // If it is 0, we are done, so transfer to the end.
       
  5217     Node* checks_done = _gvn.transform(new IfTrueNode(iff));
       
  5218     result_region->init_req(checked_path, checks_done);
       
  5219     result_i_o   ->init_req(checked_path, checked_i_o);
       
  5220     result_memory->init_req(checked_path, checked_mem);
       
  5221 
       
  5222     // If it is not zero, merge into the slow call.
       
  5223     set_control( _gvn.transform(new IfFalseNode(iff) ));
       
  5224     RegionNode* slow_reg2 = new RegionNode(3);
       
  5225     PhiNode*    slow_i_o2 = new PhiNode(slow_reg2, Type::ABIO);
       
  5226     PhiNode*    slow_mem2 = new PhiNode(slow_reg2, Type::MEMORY, adr_type);
       
  5227     record_for_igvn(slow_reg2);
       
  5228     slow_reg2  ->init_req(1, slow_control);
       
  5229     slow_i_o2  ->init_req(1, slow_i_o);
       
  5230     slow_mem2  ->init_req(1, slow_mem);
       
  5231     slow_reg2  ->init_req(2, control());
       
  5232     slow_i_o2  ->init_req(2, checked_i_o);
       
  5233     slow_mem2  ->init_req(2, checked_mem);
       
  5234 
       
  5235     slow_control = _gvn.transform(slow_reg2);
       
  5236     slow_i_o     = _gvn.transform(slow_i_o2);
       
  5237     slow_mem     = _gvn.transform(slow_mem2);
       
  5238 
       
  5239     if (alloc != NULL) {
       
  5240       // We'll restart from the very beginning, after zeroing the whole thing.
       
  5241       // This can cause double writes, but that's OK since dest is brand new.
       
  5242       // So we ignore the low 31 bits of the value returned from the stub.
       
  5243     } else {
       
  5244       // We must continue the copy exactly where it failed, or else
       
  5245       // another thread might see the wrong number of writes to dest.
       
  5246       Node* checked_offset = _gvn.transform(new XorINode(checked_value, intcon(-1)));
       
  5247       Node* slow_offset    = new PhiNode(slow_reg2, TypeInt::INT);
       
  5248       slow_offset->init_req(1, intcon(0));
       
  5249       slow_offset->init_req(2, checked_offset);
       
  5250       slow_offset  = _gvn.transform(slow_offset);
       
  5251 
       
  5252       // Adjust the arguments by the conditionally incoming offset.
       
  5253       Node* src_off_plus  = _gvn.transform(new AddINode(src_offset,  slow_offset));
       
  5254       Node* dest_off_plus = _gvn.transform(new AddINode(dest_offset, slow_offset));
       
  5255       Node* length_minus  = _gvn.transform(new SubINode(copy_length, slow_offset));
       
  5256 
       
  5257       // Tweak the node variables to adjust the code produced below:
       
  5258       src_offset  = src_off_plus;
       
  5259       dest_offset = dest_off_plus;
       
  5260       copy_length = length_minus;
       
  5261     }
       
  5262   }
       
  5263 
       
  5264   set_control(slow_control);
       
  5265   if (!stopped()) {
       
  5266     // Generate the slow path, if needed.
       
  5267     PreserveJVMState pjvms(this);   // replace_in_map may trash the map
       
  5268 
       
  5269     set_memory(slow_mem, adr_type);
       
  5270     set_i_o(slow_i_o);
       
  5271 
       
  5272     if (dest_uninitialized) {
       
  5273       generate_clear_array(adr_type, dest, basic_elem_type,
       
  5274                            intcon(0), NULL,
       
  5275                            alloc->in(AllocateNode::AllocSize));
       
  5276     }
       
  5277 
       
  5278     generate_slow_arraycopy(adr_type,
       
  5279                             src, src_offset, dest, dest_offset,
       
  5280                             copy_length, /*dest_uninitialized*/false);
       
  5281 
       
  5282     result_region->init_req(slow_call_path, control());
       
  5283     result_i_o   ->init_req(slow_call_path, i_o());
       
  5284     result_memory->init_req(slow_call_path, memory(adr_type));
       
  5285   }
       
  5286 
       
  5287   // Remove unused edges.
       
  5288   for (uint i = 1; i < result_region->req(); i++) {
       
  5289     if (result_region->in(i) == NULL)
       
  5290       result_region->init_req(i, top());
       
  5291   }
       
  5292 
       
  5293   // Finished; return the combined state.
       
  5294   set_control( _gvn.transform(result_region));
       
  5295   set_i_o(     _gvn.transform(result_i_o)    );
       
  5296   set_memory(  _gvn.transform(result_memory), adr_type );
       
  5297 
       
  5298   // The memory edges above are precise in order to model effects around
       
  5299   // array copies accurately to allow value numbering of field loads around
       
  5300   // arraycopy.  Such field loads, both before and after, are common in Java
       
  5301   // collections and similar classes involving header/array data structures.
       
  5302   //
       
  5303   // But with low number of register or when some registers are used or killed
       
  5304   // by arraycopy calls it causes registers spilling on stack. See 6544710.
       
  5305   // The next memory barrier is added to avoid it. If the arraycopy can be
       
  5306   // optimized away (which it can, sometimes) then we can manually remove
       
  5307   // the membar also.
       
  5308   //
       
  5309   // Do not let reads from the cloned object float above the arraycopy.
       
  5310   if (alloc != NULL) {
       
  5311     // Do not let stores that initialize this object be reordered with
       
  5312     // a subsequent store that would make this object accessible by
       
  5313     // other threads.
       
  5314     // Record what AllocateNode this StoreStore protects so that
       
  5315     // escape analysis can go from the MemBarStoreStoreNode to the
       
  5316     // AllocateNode and eliminate the MemBarStoreStoreNode if possible
       
  5317     // based on the escape status of the AllocateNode.
       
  5318     insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
       
  5319   } else if (InsertMemBarAfterArraycopy)
       
  5320     insert_mem_bar(Op_MemBarCPUOrder);
       
  5321 }
  4801 }
  5322 
  4802 
  5323 
  4803 
  5324 // Helper function which determines if an arraycopy immediately follows
  4804 // Helper function which determines if an arraycopy immediately follows
  5325 // an allocation, with no intervening tests or other escapes for the object.
  4805 // an allocation, with no intervening tests or other escapes for the object.
  5395   // a new control state to which we will anchor the destination pointer.
  4875   // a new control state to which we will anchor the destination pointer.
  5396 
  4876 
  5397   return alloc;
  4877   return alloc;
  5398 }
  4878 }
  5399 
  4879 
  5400 // Helper for initialization of arrays, creating a ClearArray.
       
  5401 // It writes zero bits in [start..end), within the body of an array object.
       
  5402 // The memory effects are all chained onto the 'adr_type' alias category.
       
  5403 //
       
  5404 // Since the object is otherwise uninitialized, we are free
       
  5405 // to put a little "slop" around the edges of the cleared area,
       
  5406 // as long as it does not go back into the array's header,
       
  5407 // or beyond the array end within the heap.
       
  5408 //
       
  5409 // The lower edge can be rounded down to the nearest jint and the
       
  5410 // upper edge can be rounded up to the nearest MinObjAlignmentInBytes.
       
  5411 //
       
  5412 // Arguments:
       
  5413 //   adr_type           memory slice where writes are generated
       
  5414 //   dest               oop of the destination array
       
  5415 //   basic_elem_type    element type of the destination
       
  5416 //   slice_idx          array index of first element to store
       
  5417 //   slice_len          number of elements to store (or NULL)
       
  5418 //   dest_size          total size in bytes of the array object
       
  5419 //
       
  5420 // Exactly one of slice_len or dest_size must be non-NULL.
       
  5421 // If dest_size is non-NULL, zeroing extends to the end of the object.
       
  5422 // If slice_len is non-NULL, the slice_idx value must be a constant.
       
  5423 void
       
  5424 LibraryCallKit::generate_clear_array(const TypePtr* adr_type,
       
  5425                                      Node* dest,
       
  5426                                      BasicType basic_elem_type,
       
  5427                                      Node* slice_idx,
       
  5428                                      Node* slice_len,
       
  5429                                      Node* dest_size) {
       
  5430   // one or the other but not both of slice_len and dest_size:
       
  5431   assert((slice_len != NULL? 1: 0) + (dest_size != NULL? 1: 0) == 1, "");
       
  5432   if (slice_len == NULL)  slice_len = top();
       
  5433   if (dest_size == NULL)  dest_size = top();
       
  5434 
       
  5435   // operate on this memory slice:
       
  5436   Node* mem = memory(adr_type); // memory slice to operate on
       
  5437 
       
  5438   // scaling and rounding of indexes:
       
  5439   int scale = exact_log2(type2aelembytes(basic_elem_type));
       
  5440   int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
       
  5441   int clear_low = (-1 << scale) & (BytesPerInt  - 1);
       
  5442   int bump_bit  = (-1 << scale) & BytesPerInt;
       
  5443 
       
  5444   // determine constant starts and ends
       
  5445   const intptr_t BIG_NEG = -128;
       
  5446   assert(BIG_NEG + 2*abase < 0, "neg enough");
       
  5447   intptr_t slice_idx_con = (intptr_t) find_int_con(slice_idx, BIG_NEG);
       
  5448   intptr_t slice_len_con = (intptr_t) find_int_con(slice_len, BIG_NEG);
       
  5449   if (slice_len_con == 0) {
       
  5450     return;                     // nothing to do here
       
  5451   }
       
  5452   intptr_t start_con = (abase + (slice_idx_con << scale)) & ~clear_low;
       
  5453   intptr_t end_con   = find_intptr_t_con(dest_size, -1);
       
  5454   if (slice_idx_con >= 0 && slice_len_con >= 0) {
       
  5455     assert(end_con < 0, "not two cons");
       
  5456     end_con = round_to(abase + ((slice_idx_con + slice_len_con) << scale),
       
  5457                        BytesPerLong);
       
  5458   }
       
  5459 
       
  5460   if (start_con >= 0 && end_con >= 0) {
       
  5461     // Constant start and end.  Simple.
       
  5462     mem = ClearArrayNode::clear_memory(control(), mem, dest,
       
  5463                                        start_con, end_con, &_gvn);
       
  5464   } else if (start_con >= 0 && dest_size != top()) {
       
  5465     // Constant start, pre-rounded end after the tail of the array.
       
  5466     Node* end = dest_size;
       
  5467     mem = ClearArrayNode::clear_memory(control(), mem, dest,
       
  5468                                        start_con, end, &_gvn);
       
  5469   } else if (start_con >= 0 && slice_len != top()) {
       
  5470     // Constant start, non-constant end.  End needs rounding up.
       
  5471     // End offset = round_up(abase + ((slice_idx_con + slice_len) << scale), 8)
       
  5472     intptr_t end_base  = abase + (slice_idx_con << scale);
       
  5473     int      end_round = (-1 << scale) & (BytesPerLong  - 1);
       
  5474     Node*    end       = ConvI2X(slice_len);
       
  5475     if (scale != 0)
       
  5476       end = _gvn.transform(new LShiftXNode(end, intcon(scale) ));
       
  5477     end_base += end_round;
       
  5478     end = _gvn.transform(new AddXNode(end, MakeConX(end_base)));
       
  5479     end = _gvn.transform(new AndXNode(end, MakeConX(~end_round)));
       
  5480     mem = ClearArrayNode::clear_memory(control(), mem, dest,
       
  5481                                        start_con, end, &_gvn);
       
  5482   } else if (start_con < 0 && dest_size != top()) {
       
  5483     // Non-constant start, pre-rounded end after the tail of the array.
       
  5484     // This is almost certainly a "round-to-end" operation.
       
  5485     Node* start = slice_idx;
       
  5486     start = ConvI2X(start);
       
  5487     if (scale != 0)
       
  5488       start = _gvn.transform(new LShiftXNode( start, intcon(scale) ));
       
  5489     start = _gvn.transform(new AddXNode(start, MakeConX(abase)));
       
  5490     if ((bump_bit | clear_low) != 0) {
       
  5491       int to_clear = (bump_bit | clear_low);
       
  5492       // Align up mod 8, then store a jint zero unconditionally
       
  5493       // just before the mod-8 boundary.
       
  5494       if (((abase + bump_bit) & ~to_clear) - bump_bit
       
  5495           < arrayOopDesc::length_offset_in_bytes() + BytesPerInt) {
       
  5496         bump_bit = 0;
       
  5497         assert((abase & to_clear) == 0, "array base must be long-aligned");
       
  5498       } else {
       
  5499         // Bump 'start' up to (or past) the next jint boundary:
       
  5500         start = _gvn.transform(new AddXNode(start, MakeConX(bump_bit)));
       
  5501         assert((abase & clear_low) == 0, "array base must be int-aligned");
       
  5502       }
       
  5503       // Round bumped 'start' down to jlong boundary in body of array.
       
  5504       start = _gvn.transform(new AndXNode(start, MakeConX(~to_clear)));
       
  5505       if (bump_bit != 0) {
       
  5506         // Store a zero to the immediately preceding jint:
       
  5507         Node* x1 = _gvn.transform(new AddXNode(start, MakeConX(-bump_bit)));
       
  5508         Node* p1 = basic_plus_adr(dest, x1);
       
  5509         mem = StoreNode::make(_gvn, control(), mem, p1, adr_type, intcon(0), T_INT, MemNode::unordered);
       
  5510         mem = _gvn.transform(mem);
       
  5511       }
       
  5512     }
       
  5513     Node* end = dest_size; // pre-rounded
       
  5514     mem = ClearArrayNode::clear_memory(control(), mem, dest,
       
  5515                                        start, end, &_gvn);
       
  5516   } else {
       
  5517     // Non-constant start, unrounded non-constant end.
       
  5518     // (Nobody zeroes a random midsection of an array using this routine.)
       
  5519     ShouldNotReachHere();       // fix caller
       
  5520   }
       
  5521 
       
  5522   // Done.
       
  5523   set_memory(mem, adr_type);
       
  5524 }
       
  5525 
       
  5526 
       
  5527 bool
       
  5528 LibraryCallKit::generate_block_arraycopy(const TypePtr* adr_type,
       
  5529                                          BasicType basic_elem_type,
       
  5530                                          AllocateNode* alloc,
       
  5531                                          Node* src,  Node* src_offset,
       
  5532                                          Node* dest, Node* dest_offset,
       
  5533                                          Node* dest_size, bool dest_uninitialized) {
       
  5534   // See if there is an advantage from block transfer.
       
  5535   int scale = exact_log2(type2aelembytes(basic_elem_type));
       
  5536   if (scale >= LogBytesPerLong)
       
  5537     return false;               // it is already a block transfer
       
  5538 
       
  5539   // Look at the alignment of the starting offsets.
       
  5540   int abase = arrayOopDesc::base_offset_in_bytes(basic_elem_type);
       
  5541 
       
  5542   intptr_t src_off_con  = (intptr_t) find_int_con(src_offset, -1);
       
  5543   intptr_t dest_off_con = (intptr_t) find_int_con(dest_offset, -1);
       
  5544   if (src_off_con < 0 || dest_off_con < 0)
       
  5545     // At present, we can only understand constants.
       
  5546     return false;
       
  5547 
       
  5548   intptr_t src_off  = abase + (src_off_con  << scale);
       
  5549   intptr_t dest_off = abase + (dest_off_con << scale);
       
  5550 
       
  5551   if (((src_off | dest_off) & (BytesPerLong-1)) != 0) {
       
  5552     // Non-aligned; too bad.
       
  5553     // One more chance:  Pick off an initial 32-bit word.
       
  5554     // This is a common case, since abase can be odd mod 8.
       
  5555     if (((src_off | dest_off) & (BytesPerLong-1)) == BytesPerInt &&
       
  5556         ((src_off ^ dest_off) & (BytesPerLong-1)) == 0) {
       
  5557       Node* sptr = basic_plus_adr(src,  src_off);
       
  5558       Node* dptr = basic_plus_adr(dest, dest_off);
       
  5559       Node* sval = make_load(control(), sptr, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
       
  5560       store_to_memory(control(), dptr, sval, T_INT, adr_type, MemNode::unordered);
       
  5561       src_off += BytesPerInt;
       
  5562       dest_off += BytesPerInt;
       
  5563     } else {
       
  5564       return false;
       
  5565     }
       
  5566   }
       
  5567   assert(src_off % BytesPerLong == 0, "");
       
  5568   assert(dest_off % BytesPerLong == 0, "");
       
  5569 
       
  5570   // Do this copy by giant steps.
       
  5571   Node* sptr  = basic_plus_adr(src,  src_off);
       
  5572   Node* dptr  = basic_plus_adr(dest, dest_off);
       
  5573   Node* countx = dest_size;
       
  5574   countx = _gvn.transform(new SubXNode(countx, MakeConX(dest_off)));
       
  5575   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong)));
       
  5576 
       
  5577   bool disjoint_bases = true;   // since alloc != NULL
       
  5578   generate_unchecked_arraycopy(adr_type, T_LONG, disjoint_bases,
       
  5579                                sptr, NULL, dptr, NULL, countx, dest_uninitialized);
       
  5580 
       
  5581   return true;
       
  5582 }
       
  5583 
       
  5584 
       
  5585 // Helper function; generates code for the slow case.
       
  5586 // We make a call to a runtime method which emulates the native method,
       
  5587 // but without the native wrapper overhead.
       
  5588 void
       
  5589 LibraryCallKit::generate_slow_arraycopy(const TypePtr* adr_type,
       
  5590                                         Node* src,  Node* src_offset,
       
  5591                                         Node* dest, Node* dest_offset,
       
  5592                                         Node* copy_length, bool dest_uninitialized) {
       
  5593   assert(!dest_uninitialized, "Invariant");
       
  5594   Node* call = make_runtime_call(RC_NO_LEAF | RC_UNCOMMON,
       
  5595                                  OptoRuntime::slow_arraycopy_Type(),
       
  5596                                  OptoRuntime::slow_arraycopy_Java(),
       
  5597                                  "slow_arraycopy", adr_type,
       
  5598                                  src, src_offset, dest, dest_offset,
       
  5599                                  copy_length);
       
  5600 
       
  5601   // Handle exceptions thrown by this fellow:
       
  5602   make_slow_call_ex(call, env()->Throwable_klass(), false);
       
  5603 }
       
  5604 
       
  5605 // Helper function; generates code for cases requiring runtime checks.
       
  5606 Node*
       
  5607 LibraryCallKit::generate_checkcast_arraycopy(const TypePtr* adr_type,
       
  5608                                              Node* dest_elem_klass,
       
  5609                                              Node* src,  Node* src_offset,
       
  5610                                              Node* dest, Node* dest_offset,
       
  5611                                              Node* copy_length, bool dest_uninitialized) {
       
  5612   if (stopped())  return NULL;
       
  5613 
       
  5614   address copyfunc_addr = StubRoutines::checkcast_arraycopy(dest_uninitialized);
       
  5615   if (copyfunc_addr == NULL) { // Stub was not generated, go slow path.
       
  5616     return NULL;
       
  5617   }
       
  5618 
       
  5619   // Pick out the parameters required to perform a store-check
       
  5620   // for the target array.  This is an optimistic check.  It will
       
  5621   // look in each non-null element's class, at the desired klass's
       
  5622   // super_check_offset, for the desired klass.
       
  5623   int sco_offset = in_bytes(Klass::super_check_offset_offset());
       
  5624   Node* p3 = basic_plus_adr(dest_elem_klass, sco_offset);
       
  5625   Node* n3 = new LoadINode(NULL, memory(p3), p3, _gvn.type(p3)->is_ptr(), TypeInt::INT, MemNode::unordered);
       
  5626   Node* check_offset = ConvI2X(_gvn.transform(n3));
       
  5627   Node* check_value  = dest_elem_klass;
       
  5628 
       
  5629   Node* src_start  = array_element_address(src,  src_offset,  T_OBJECT);
       
  5630   Node* dest_start = array_element_address(dest, dest_offset, T_OBJECT);
       
  5631 
       
  5632   // (We know the arrays are never conjoint, because their types differ.)
       
  5633   Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
       
  5634                                  OptoRuntime::checkcast_arraycopy_Type(),
       
  5635                                  copyfunc_addr, "checkcast_arraycopy", adr_type,
       
  5636                                  // five arguments, of which two are
       
  5637                                  // intptr_t (jlong in LP64)
       
  5638                                  src_start, dest_start,
       
  5639                                  copy_length XTOP,
       
  5640                                  check_offset XTOP,
       
  5641                                  check_value);
       
  5642 
       
  5643   return _gvn.transform(new ProjNode(call, TypeFunc::Parms));
       
  5644 }
       
  5645 
       
  5646 
       
  5647 // Helper function; generates code for cases requiring runtime checks.
       
  5648 Node*
       
  5649 LibraryCallKit::generate_generic_arraycopy(const TypePtr* adr_type,
       
  5650                                            Node* src,  Node* src_offset,
       
  5651                                            Node* dest, Node* dest_offset,
       
  5652                                            Node* copy_length, bool dest_uninitialized) {
       
  5653   assert(!dest_uninitialized, "Invariant");
       
  5654   if (stopped())  return NULL;
       
  5655   address copyfunc_addr = StubRoutines::generic_arraycopy();
       
  5656   if (copyfunc_addr == NULL) { // Stub was not generated, go slow path.
       
  5657     return NULL;
       
  5658   }
       
  5659 
       
  5660   Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
       
  5661                     OptoRuntime::generic_arraycopy_Type(),
       
  5662                     copyfunc_addr, "generic_arraycopy", adr_type,
       
  5663                     src, src_offset, dest, dest_offset, copy_length);
       
  5664 
       
  5665   return _gvn.transform(new ProjNode(call, TypeFunc::Parms));
       
  5666 }
       
  5667 
       
  5668 // Helper function; generates the fast out-of-line call to an arraycopy stub.
       
  5669 void
       
  5670 LibraryCallKit::generate_unchecked_arraycopy(const TypePtr* adr_type,
       
  5671                                              BasicType basic_elem_type,
       
  5672                                              bool disjoint_bases,
       
  5673                                              Node* src,  Node* src_offset,
       
  5674                                              Node* dest, Node* dest_offset,
       
  5675                                              Node* copy_length, bool dest_uninitialized) {
       
  5676   if (stopped())  return;               // nothing to do
       
  5677 
       
  5678   Node* src_start  = src;
       
  5679   Node* dest_start = dest;
       
  5680   if (src_offset != NULL || dest_offset != NULL) {
       
  5681     assert(src_offset != NULL && dest_offset != NULL, "");
       
  5682     src_start  = array_element_address(src,  src_offset,  basic_elem_type);
       
  5683     dest_start = array_element_address(dest, dest_offset, basic_elem_type);
       
  5684   }
       
  5685 
       
  5686   // Figure out which arraycopy runtime method to call.
       
  5687   const char* copyfunc_name = "arraycopy";
       
  5688   address     copyfunc_addr =
       
  5689       basictype2arraycopy(basic_elem_type, src_offset, dest_offset,
       
  5690                           disjoint_bases, copyfunc_name, dest_uninitialized);
       
  5691 
       
  5692   // Call it.  Note that the count_ix value is not scaled to a byte-size.
       
  5693   make_runtime_call(RC_LEAF|RC_NO_FP,
       
  5694                     OptoRuntime::fast_arraycopy_Type(),
       
  5695                     copyfunc_addr, copyfunc_name, adr_type,
       
  5696                     src_start, dest_start, copy_length XTOP);
       
  5697 }
       
  5698 
       
  5699 //-------------inline_encodeISOArray-----------------------------------
  4880 //-------------inline_encodeISOArray-----------------------------------
  5700 // encode char[] to byte[] in ISO_8859_1
  4881 // encode char[] to byte[] in ISO_8859_1
  5701 bool LibraryCallKit::inline_encodeISOArray() {
  4882 bool LibraryCallKit::inline_encodeISOArray() {
  5702   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
  4883   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
  5703   // no receiver since it is static method
  4884   // no receiver since it is static method