src/share/vm/opto/library_call.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/opto

src/share/vm/opto/library_call.cpp

Print this page
rev 6807 : 7173584: Implement arraycopy as a macro node
Summary: delay the conversion of arraycopy to stub calls to macro expansion
Reviewed-by:


 129   }
 130 
 131  private:
 132   void fatal_unexpected_iid(vmIntrinsics::ID iid) {
 133     fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
 134   }
 135 
 136   void  set_result(Node* n) { assert(_result == NULL, "only set once"); _result = n; }
 137   void  set_result(RegionNode* region, PhiNode* value);
 138   Node*     result() { return _result; }
 139 
 140   virtual int reexecute_sp() { return _reexecute_sp; }
 141 
 142   // Helper functions to inline natives
 143   Node* generate_guard(Node* test, RegionNode* region, float true_prob);
 144   Node* generate_slow_guard(Node* test, RegionNode* region);
 145   Node* generate_fair_guard(Node* test, RegionNode* region);
 146   Node* generate_negative_guard(Node* index, RegionNode* region,
 147                                 // resulting CastII of index:
 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,
 153                              Node* array_length,
 154                              RegionNode* region);
 155   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);
 159   Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
 160                                       RegionNode* region, int null_path,
 161                                       int offset);
 162   Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
 163                                RegionNode* region, int null_path) {
 164     int offset = java_lang_Class::klass_offset_in_bytes();
 165     return load_klass_from_mirror_common(mirror, never_see_null,
 166                                          region, null_path,
 167                                          offset);
 168   }
 169   Node* load_array_klass_from_mirror(Node* mirror, bool never_see_null,
 170                                      RegionNode* region, int null_path) {
 171     int offset = java_lang_Class::array_klass_offset_in_bytes();
 172     return load_klass_from_mirror_common(mirror, never_see_null,
 173                                          region, null_path,
 174                                          offset);
 175   }
 176   Node* generate_access_flags_guard(Node* kls,
 177                                     int modifier_mask, int modifier_bits,


 247   bool inline_native_threadID();
 248 #endif
 249   bool inline_native_time_funcs(address method, const char* funcName);
 250   bool inline_native_isInterrupted();
 251   bool inline_native_Class_query(vmIntrinsics::ID id);
 252   bool inline_native_subtype_check();
 253 
 254   bool inline_native_newArray();
 255   bool inline_native_getLength();
 256   bool inline_array_copyOf(bool is_copyOfRange);
 257   bool inline_array_equals();
 258   void copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark);
 259   bool inline_native_clone(bool is_virtual);
 260   bool inline_native_Reflection_getCallerClass();
 261   // Helper function for inlining native object hash method
 262   bool inline_native_hashcode(bool is_virtual, bool is_static);
 263   bool inline_native_getClass();
 264 
 265   // Helper functions for inlining arraycopy
 266   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,
 276                                                 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;
 309   bool inline_unsafe_load_store(BasicType type,  LoadStoreKind kind);
 310   bool inline_unsafe_ordered_store(BasicType type);
 311   bool inline_unsafe_fence(vmIntrinsics::ID id);
 312   bool inline_fp_conversions(vmIntrinsics::ID id);
 313   bool inline_number_methods(vmIntrinsics::ID id);
 314   bool inline_reference_get();
 315   bool inline_aescrypt_Block(vmIntrinsics::ID id);
 316   bool inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id);
 317   Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting);
 318   Node* get_key_start_from_aescrypt_object(Node* aescrypt_object);
 319   Node* get_original_key_start_from_aescrypt_object(Node* aescrypt_object);
 320   bool inline_sha_implCompress(vmIntrinsics::ID id);
 321   bool inline_digestBase_implCompressMB(int predicate);
 322   bool inline_sha_implCompressMB(Node* digestBaseObj, ciInstanceKlass* instklass_SHA,
 323                                  bool long_state, address stubAddr, const char *stubName,
 324                                  Node* src_start, Node* ofs, Node* limit);
 325   Node* get_state_from_sha_object(Node *sha_object);
 326   Node* get_state_from_sha5_object(Node *sha_object);
 327   Node* inline_digestBase_implCompressMB_predicate(int predicate);


1032 }
1033 
1034 inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
1035                                                      Node* *pos_index) {
1036   if (stopped())
1037     return NULL;                // already stopped
1038   if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
1039     return NULL;                // index is already adequately typed
1040   Node* cmp_lt = _gvn.transform(new CmpINode(index, intcon(0)));
1041   Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
1042   Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
1043   if (is_neg != NULL && pos_index != NULL) {
1044     // Emulate effect of Parse::adjust_map_after_if.
1045     Node* ccast = new CastIINode(index, TypeInt::POS);
1046     ccast->set_req(0, control());
1047     (*pos_index) = _gvn.transform(ccast);
1048   }
1049   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 }
1070 
1071 // Make sure that 'position' is a valid limit index, in [0..length].
1072 // There are two equivalent plans for checking this:
1073 //   A. (offset + copyLength)  unsigned<=  arrayLength
1074 //   B. offset  <=  (arrayLength - copyLength)
1075 // We require that all of the values above, except for the sum and
1076 // difference, are already known to be non-negative.
1077 // Plan A is robust in the face of overflow, if offset and copyLength
1078 // are both hugely positive.
1079 //
1080 // Plan B is less direct and intuitive, but it does not overflow at
1081 // all, since the difference of two non-negatives is always
1082 // representable.  Whenever Java methods must perform the equivalent
1083 // check they generally use Plan B instead of Plan A.
1084 // For the moment we use Plan A.
1085 inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
1086                                                   Node* subseq_length,
1087                                                   Node* array_length,
1088                                                   RegionNode* region) {
1089   if (stopped())
1090     return NULL;                // already stopped


3911     if (bailout->req() > 1) {
3912       PreserveJVMState pjvms(this);
3913       set_control(_gvn.transform(bailout));
3914       uncommon_trap(Deoptimization::Reason_intrinsic,
3915                     Deoptimization::Action_maybe_recompile);
3916     }
3917 
3918     if (!stopped()) {
3919       // How many elements will we copy from the original?
3920       // The answer is MinI(orig_length - start, length).
3921       Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
3922       Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
3923 
3924       newcopy = new_array(klass_node, length, 0);  // no argments to push
3925 
3926       // Generate a direct call to the right arraycopy function(s).
3927       // We know the copy is disjoint but we might not know if the
3928       // oop stores need checking.
3929       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
3930       // This will fail a store-check if x contains any non-nulls.
3931       bool disjoint_bases = true;
3932       // if start > orig_length then the length of the copy may be
3933       // negative.
3934       bool length_never_negative = !is_copyOfRange;
3935       generate_arraycopy(TypeAryPtr::OOPS, T_OBJECT,
3936                          original, start, newcopy, intcon(0), moved,
3937                          disjoint_bases, length_never_negative);





3938     }
3939   } // original reexecute is set back here
3940 
3941   C->set_has_split_ifs(true); // Has chance for split-if optimization
3942   if (!stopped()) {
3943     set_result(newcopy);
3944   }
3945   return true;
3946 }
3947 
3948 
3949 //----------------------generate_virtual_guard---------------------------
3950 // Helper for hashCode and clone.  Peeks inside the vtable to avoid a call.
3951 Node* LibraryCallKit::generate_virtual_guard(Node* obj_klass,
3952                                              RegionNode* slow_region) {
3953   ciMethod* method = callee();
3954   int vtable_index = method->vtable_index();
3955   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
3956          err_msg_res("bad index %d", vtable_index));
3957   // Get the Method* out of the appropriate vtable entry.


4428   if (base_off % BytesPerLong != 0) {
4429     assert(UseCompressedClassPointers, "");
4430     if (is_array) {
4431       // Exclude length to copy by 8 bytes words.
4432       base_off += sizeof(int);
4433     } else {
4434       // Include klass to copy by 8 bytes words.
4435       base_off = instanceOopDesc::klass_offset_in_bytes();
4436     }
4437     assert(base_off % BytesPerLong == 0, "expect 8 bytes alignment");
4438   }
4439   src  = basic_plus_adr(src,  base_off);
4440   dest = basic_plus_adr(dest, base_off);
4441 
4442   // Compute the length also, if needed:
4443   Node* countx = size;
4444   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
4445   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
4446 
4447   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4448   bool disjoint_bases = true;
4449   generate_unchecked_arraycopy(raw_adr_type, T_LONG, disjoint_bases,
4450                                src, NULL, dest, NULL, countx,
4451                                /*dest_uninitialized*/true);


4452 
4453   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
4454   if (card_mark) {
4455     assert(!is_array, "");
4456     // Put in store barrier for any and all oops we are sticking
4457     // into this object.  (We could avoid this if we could prove
4458     // that the object type contains no oop fields at all.)
4459     Node* no_particular_value = NULL;
4460     Node* no_particular_field = NULL;
4461     int raw_adr_idx = Compile::AliasIdxRaw;
4462     post_barrier(control(),
4463                  memory(raw_adr_type),
4464                  alloc_obj,
4465                  no_particular_field,
4466                  raw_adr_idx,
4467                  no_particular_value,
4468                  T_OBJECT,
4469                  false);
4470   }
4471 


4540     const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4541     int raw_adr_idx = Compile::AliasIdxRaw;
4542 
4543     Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)NULL);
4544     if (array_ctl != NULL) {
4545       // It's an array.
4546       PreserveJVMState pjvms(this);
4547       set_control(array_ctl);
4548       Node* obj_length = load_array_length(obj);
4549       Node* obj_size  = NULL;
4550       Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size);  // no arguments to push
4551 
4552       if (!use_ReduceInitialCardMarks()) {
4553         // If it is an oop array, it requires very special treatment,
4554         // because card marking is required on each card of the array.
4555         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
4556         if (is_obja != NULL) {
4557           PreserveJVMState pjvms2(this);
4558           set_control(is_obja);
4559           // Generate a direct call to the right arraycopy function(s).
4560           bool disjoint_bases = true;
4561           bool length_never_negative = true;
4562           generate_arraycopy(TypeAryPtr::OOPS, T_OBJECT,
4563                              obj, intcon(0), alloc_obj, intcon(0),
4564                              obj_length,
4565                              disjoint_bases, length_never_negative);

4566           result_reg->init_req(_objArray_path, control());
4567           result_val->init_req(_objArray_path, alloc_obj);
4568           result_i_o ->set_req(_objArray_path, i_o());
4569           result_mem ->set_req(_objArray_path, reset_memory());
4570         }
4571       }
4572       // Otherwise, there are no card marks to worry about.
4573       // (We can dispense with card marks if we know the allocation
4574       //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4575       //  causes the non-eden paths to take compensating steps to
4576       //  simulate a fresh allocation, so that no further
4577       //  card marks are required in compiled code to initialize
4578       //  the object.)
4579 
4580       if (!stopped()) {
4581         copy_to_clone(obj, alloc_obj, obj_size, true, false);
4582 
4583         // Present the results of the copy.
4584         result_reg->init_req(_array_path, control());
4585         result_val->init_req(_array_path, alloc_obj);


4639       PreserveJVMState pjvms(this);
4640       CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_clone, is_virtual);
4641       Node* slow_result = set_results_for_java_call(slow_call);
4642       // this->control() comes from set_results_for_java_call
4643       result_reg->init_req(_slow_path, control());
4644       result_val->init_req(_slow_path, slow_result);
4645       result_i_o ->set_req(_slow_path, i_o());
4646       result_mem ->set_req(_slow_path, reset_memory());
4647     }
4648 
4649     // Return the combined state.
4650     set_control(    _gvn.transform(result_reg));
4651     set_i_o(        _gvn.transform(result_i_o));
4652     set_all_memory( _gvn.transform(result_mem));
4653   } // original reexecute is set back here
4654 
4655   set_result(_gvn.transform(result_val));
4656   return true;
4657 }
4658 
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-----------------------
4696 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4697 //                                                      Object dest, int destPos,
4698 //                                                      int length);
4699 bool LibraryCallKit::inline_arraycopy() {
4700   // Get the arguments.
4701   Node* src         = argument(0);  // type: oop
4702   Node* src_offset  = argument(1);  // type: int
4703   Node* dest        = argument(2);  // type: oop
4704   Node* dest_offset = argument(3);  // type: int
4705   Node* length      = argument(4);  // type: int
4706 
4707   // Compile time checks.  If any of these checks cannot be verified at compile time,
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.
4712   const Type* src_type  = src->Value(&_gvn);
4713   const Type* dest_type = dest->Value(&_gvn);
















4714   const TypeAryPtr* top_src  = src_type->isa_aryptr();
4715   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
4716 
4717   // Do we have the type of src?
4718   bool has_src = (top_src != NULL && top_src->klass() != NULL);
4719   // Do we have the type of dest?
4720   bool has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4721   // Is the type for src from speculation?
4722   bool src_spec = false;
4723   // Is the type for dest from speculation?
4724   bool dest_spec = false;
4725 
4726   if (!has_src || !has_dest) {
4727     // We don't have sufficient type information, let's see if
4728     // speculative types can help. We need to have types for both src
4729     // and dest so that it pays off.
4730 
4731     // Do we already have or could we have type information for src
4732     bool could_have_src = has_src;
4733     // Do we already have or could we have type information for dest


4751 
4752     if (could_have_src && could_have_dest) {
4753       // This is going to pay off so emit the required guards
4754       if (!has_src) {
4755         src = maybe_cast_profiled_obj(src, src_k);
4756         src_type  = _gvn.type(src);
4757         top_src  = src_type->isa_aryptr();
4758         has_src = (top_src != NULL && top_src->klass() != NULL);
4759         src_spec = true;
4760       }
4761       if (!has_dest) {
4762         dest = maybe_cast_profiled_obj(dest, dest_k);
4763         dest_type  = _gvn.type(dest);
4764         top_dest  = dest_type->isa_aryptr();
4765         has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4766         dest_spec = true;
4767       }
4768     }
4769   }
4770 
4771   if (!has_src || !has_dest) {
4772     // Conservatively insert a memory barrier on all memory slices.
4773     // Do not let writes into the source float below the arraycopy.
4774     insert_mem_bar(Op_MemBarCPUOrder);
4775 
4776     // Call StubRoutines::generic_arraycopy stub.
4777     generate_arraycopy(TypeRawPtr::BOTTOM, T_CONFLICT,
4778                        src, src_offset, dest, dest_offset, length);
4779 
4780     // Do not let reads from the destination float above the arraycopy.
4781     // Since we cannot type the arrays, we don't know which slices
4782     // might be affected.  We could restrict this barrier only to those
4783     // memory slices which pertain to array elements--but don't bother.
4784     if (!InsertMemBarAfterArraycopy)
4785       // (If InsertMemBarAfterArraycopy, there is already one in place.)
4786       insert_mem_bar(Op_MemBarCPUOrder);
4787     return true;
4788   }
4789 
4790   // (2) src and dest arrays must have elements of the same BasicType
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;
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         }
5101       }
5102       if (didit)
5103         set_control(top());     // no regular fast path
5104     }
5105 
5106     // Clear the tail, if any.
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       }
5128     }
5129   }
5130 
5131   BasicType copy_type = basic_elem_type;
5132   assert(basic_elem_type != T_ARRAY, "caller must fix this");
5133   if (!stopped() && copy_type == T_OBJECT) {
5134     // If src and dest have compatible element types, we can copy bits.
5135     // Types S[] and D[] are compatible if D is a supertype of S.
5136     //
5137     // If they are not, we will use checked_oop_disjoint_arraycopy,
5138     // which performs a fast optimistic per-oop check, and backs off
5139     // further to JVM_ArrayCopy on the first per-oop check that fails.
5140     // (Actually, we don't move raw bits only; the GC requires card marks.)
5141 
5142     // Get the Klass* for both src and dest
5143     Node* src_klass  = load_object_klass(src);
5144     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);
5158     // Plug failing path into checked_oop_disjoint_arraycopy
5159     if (not_subtype_ctrl != top()) {
5160       PreserveJVMState pjvms(this);
5161       set_control(not_subtype_ctrl);
5162       // (At this point we can assume disjoint_bases, since types differ.)
5163       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
5164       Node* p1 = basic_plus_adr(dest_klass, ek_offset);
5165       Node* n1 = LoadKlassNode::make(_gvn, immutable_memory(), p1, TypeRawPtr::BOTTOM);
5166       Node* dest_elem_klass = _gvn.transform(n1);
5167       Node* cv = generate_checkcast_arraycopy(adr_type,
5168                                               dest_elem_klass,
5169                                               src, src_offset, dest, dest_offset,
5170                                               ConvI2X(copy_length), dest_uninitialized);
5171       if (cv == NULL)  cv = intcon(-1);  // failure (no stub available)
5172       checked_control = control();
5173       checked_i_o     = i_o();
5174       checked_mem     = memory(adr_type);
5175       checked_value   = cv;
5176     }
5177     // At this point we know we do not need type checks on oop stores.
5178 
5179     // Let's see if we need card marks:
5180     if (alloc != NULL && use_ReduceInitialCardMarks()) {
5181       // If we do not need card marks, copy using the jint or jlong stub.
5182       copy_type = LP64_ONLY(UseCompressedOops ? T_INT : T_LONG) NOT_LP64(T_INT);
5183       assert(type2aelembytes(basic_elem_type) == type2aelembytes(copy_type),
5184              "sizes agree");
5185     }
5186   }
5187 
5188   if (!stopped()) {
5189     // Generate the fast path, if possible.
5190     PreserveJVMState pjvms(this);
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 }
5322 
5323 
5324 // Helper function which determines if an arraycopy immediately follows
5325 // an allocation, with no intervening tests or other escapes for the object.
5326 AllocateArrayNode*
5327 LibraryCallKit::tightly_coupled_allocation(Node* ptr,
5328                                            RegionNode* slow_region) {
5329   if (stopped())             return NULL;  // no fast path
5330   if (C->AliasLevel() == 0)  return NULL;  // no MergeMems around
5331 
5332   AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
5333   if (alloc == NULL)  return NULL;
5334 
5335   Node* rawmem = memory(Compile::AliasIdxRaw);
5336   // Is the allocation's memory state untouched?
5337   if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
5338     // Bail out if there have been raw-memory effects since the allocation.
5339     // (Example:  There might have been a call or safepoint.)
5340     return NULL;


5378         Node* obs = not_ctl->fast_out(j);
5379         if (obs->in(0) == not_ctl && obs->is_Call() &&
5380             (obs->as_Call()->entry_point() == SharedRuntime::uncommon_trap_blob()->entry_point())) {
5381           found_trap = true; break;
5382         }
5383       }
5384       if (found_trap) {
5385         ctl = iff->in(0);       // This test feeds a harmless uncommon trap.
5386         continue;
5387       }
5388     }
5389     return NULL;
5390   }
5391 
5392   // If we get this far, we have an allocation which immediately
5393   // precedes the arraycopy, and we can take over zeroing the new object.
5394   // The arraycopy will finish the initialization, and provide
5395   // a new control state to which we will anchor the destination pointer.
5396 
5397   return alloc;
5398 }
5399 
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-----------------------------------
5700 // encode char[] to byte[] in ISO_8859_1
5701 bool LibraryCallKit::inline_encodeISOArray() {
5702   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
5703   // no receiver since it is static method
5704   Node *src         = argument(0);
5705   Node *src_offset  = argument(1);
5706   Node *dst         = argument(2);
5707   Node *dst_offset  = argument(3);
5708   Node *length      = argument(4);
5709 
5710   const Type* src_type = src->Value(&_gvn);
5711   const Type* dst_type = dst->Value(&_gvn);
5712   const TypeAryPtr* top_src = src_type->isa_aryptr();
5713   const TypeAryPtr* top_dest = dst_type->isa_aryptr();
5714   if (top_src  == NULL || top_src->klass()  == NULL ||
5715       top_dest == NULL || top_dest->klass() == NULL) {
5716     // failed array check




 129   }
 130 
 131  private:
 132   void fatal_unexpected_iid(vmIntrinsics::ID iid) {
 133     fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
 134   }
 135 
 136   void  set_result(Node* n) { assert(_result == NULL, "only set once"); _result = n; }
 137   void  set_result(RegionNode* region, PhiNode* value);
 138   Node*     result() { return _result; }
 139 
 140   virtual int reexecute_sp() { return _reexecute_sp; }
 141 
 142   // Helper functions to inline natives
 143   Node* generate_guard(Node* test, RegionNode* region, float true_prob);
 144   Node* generate_slow_guard(Node* test, RegionNode* region);
 145   Node* generate_fair_guard(Node* test, RegionNode* region);
 146   Node* generate_negative_guard(Node* index, RegionNode* region,
 147                                 // resulting CastII of index:
 148                                 Node* *pos_index = NULL);



 149   Node* generate_limit_guard(Node* offset, Node* subseq_length,
 150                              Node* array_length,
 151                              RegionNode* region);
 152   Node* generate_current_thread(Node* &tls_output);


 153   Node* load_mirror_from_klass(Node* klass);
 154   Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null,
 155                                       RegionNode* region, int null_path,
 156                                       int offset);
 157   Node* load_klass_from_mirror(Node* mirror, bool never_see_null,
 158                                RegionNode* region, int null_path) {
 159     int offset = java_lang_Class::klass_offset_in_bytes();
 160     return load_klass_from_mirror_common(mirror, never_see_null,
 161                                          region, null_path,
 162                                          offset);
 163   }
 164   Node* load_array_klass_from_mirror(Node* mirror, bool never_see_null,
 165                                      RegionNode* region, int null_path) {
 166     int offset = java_lang_Class::array_klass_offset_in_bytes();
 167     return load_klass_from_mirror_common(mirror, never_see_null,
 168                                          region, null_path,
 169                                          offset);
 170   }
 171   Node* generate_access_flags_guard(Node* kls,
 172                                     int modifier_mask, int modifier_bits,


 242   bool inline_native_threadID();
 243 #endif
 244   bool inline_native_time_funcs(address method, const char* funcName);
 245   bool inline_native_isInterrupted();
 246   bool inline_native_Class_query(vmIntrinsics::ID id);
 247   bool inline_native_subtype_check();
 248 
 249   bool inline_native_newArray();
 250   bool inline_native_getLength();
 251   bool inline_array_copyOf(bool is_copyOfRange);
 252   bool inline_array_equals();
 253   void copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark);
 254   bool inline_native_clone(bool is_virtual);
 255   bool inline_native_Reflection_getCallerClass();
 256   // Helper function for inlining native object hash method
 257   bool inline_native_hashcode(bool is_virtual, bool is_static);
 258   bool inline_native_getClass();
 259 
 260   // Helper functions for inlining arraycopy
 261   bool inline_arraycopy();








 262   AllocateArrayNode* tightly_coupled_allocation(Node* ptr,
 263                                                 RegionNode* slow_region);































 264   typedef enum { LS_xadd, LS_xchg, LS_cmpxchg } LoadStoreKind;
 265   bool inline_unsafe_load_store(BasicType type,  LoadStoreKind kind);
 266   bool inline_unsafe_ordered_store(BasicType type);
 267   bool inline_unsafe_fence(vmIntrinsics::ID id);
 268   bool inline_fp_conversions(vmIntrinsics::ID id);
 269   bool inline_number_methods(vmIntrinsics::ID id);
 270   bool inline_reference_get();
 271   bool inline_aescrypt_Block(vmIntrinsics::ID id);
 272   bool inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id);
 273   Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting);
 274   Node* get_key_start_from_aescrypt_object(Node* aescrypt_object);
 275   Node* get_original_key_start_from_aescrypt_object(Node* aescrypt_object);
 276   bool inline_sha_implCompress(vmIntrinsics::ID id);
 277   bool inline_digestBase_implCompressMB(int predicate);
 278   bool inline_sha_implCompressMB(Node* digestBaseObj, ciInstanceKlass* instklass_SHA,
 279                                  bool long_state, address stubAddr, const char *stubName,
 280                                  Node* src_start, Node* ofs, Node* limit);
 281   Node* get_state_from_sha_object(Node *sha_object);
 282   Node* get_state_from_sha5_object(Node *sha_object);
 283   Node* inline_digestBase_implCompressMB_predicate(int predicate);


 988 }
 989 
 990 inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
 991                                                      Node* *pos_index) {
 992   if (stopped())
 993     return NULL;                // already stopped
 994   if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
 995     return NULL;                // index is already adequately typed
 996   Node* cmp_lt = _gvn.transform(new CmpINode(index, intcon(0)));
 997   Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
 998   Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
 999   if (is_neg != NULL && pos_index != NULL) {
1000     // Emulate effect of Parse::adjust_map_after_if.
1001     Node* ccast = new CastIINode(index, TypeInt::POS);
1002     ccast->set_req(0, control());
1003     (*pos_index) = _gvn.transform(ccast);
1004   }
1005   return is_neg;
1006 }
1007 



















1008 // Make sure that 'position' is a valid limit index, in [0..length].
1009 // There are two equivalent plans for checking this:
1010 //   A. (offset + copyLength)  unsigned<=  arrayLength
1011 //   B. offset  <=  (arrayLength - copyLength)
1012 // We require that all of the values above, except for the sum and
1013 // difference, are already known to be non-negative.
1014 // Plan A is robust in the face of overflow, if offset and copyLength
1015 // are both hugely positive.
1016 //
1017 // Plan B is less direct and intuitive, but it does not overflow at
1018 // all, since the difference of two non-negatives is always
1019 // representable.  Whenever Java methods must perform the equivalent
1020 // check they generally use Plan B instead of Plan A.
1021 // For the moment we use Plan A.
1022 inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
1023                                                   Node* subseq_length,
1024                                                   Node* array_length,
1025                                                   RegionNode* region) {
1026   if (stopped())
1027     return NULL;                // already stopped


3848     if (bailout->req() > 1) {
3849       PreserveJVMState pjvms(this);
3850       set_control(_gvn.transform(bailout));
3851       uncommon_trap(Deoptimization::Reason_intrinsic,
3852                     Deoptimization::Action_maybe_recompile);
3853     }
3854 
3855     if (!stopped()) {
3856       // How many elements will we copy from the original?
3857       // The answer is MinI(orig_length - start, length).
3858       Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
3859       Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
3860 
3861       newcopy = new_array(klass_node, length, 0);  // no argments to push
3862 
3863       // Generate a direct call to the right arraycopy function(s).
3864       // We know the copy is disjoint but we might not know if the
3865       // oop stores need checking.
3866       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
3867       // This will fail a store-check if x contains any non-nulls.
3868 
3869       Node* alloc = tightly_coupled_allocation(newcopy, NULL);
3870 
3871       ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, alloc != NULL);
3872       if (!is_copyOfRange) {
3873         ac->set_copyof();
3874       } else {
3875         ac->set_copyofrange();
3876       }
3877       Node* n = _gvn.transform(ac);
3878       assert(n == ac, "cannot disappear");
3879       ac->connect_outputs(this);
3880     }
3881   } // original reexecute is set back here
3882 
3883   C->set_has_split_ifs(true); // Has chance for split-if optimization
3884   if (!stopped()) {
3885     set_result(newcopy);
3886   }
3887   return true;
3888 }
3889 
3890 
3891 //----------------------generate_virtual_guard---------------------------
3892 // Helper for hashCode and clone.  Peeks inside the vtable to avoid a call.
3893 Node* LibraryCallKit::generate_virtual_guard(Node* obj_klass,
3894                                              RegionNode* slow_region) {
3895   ciMethod* method = callee();
3896   int vtable_index = method->vtable_index();
3897   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
3898          err_msg_res("bad index %d", vtable_index));
3899   // Get the Method* out of the appropriate vtable entry.


4370   if (base_off % BytesPerLong != 0) {
4371     assert(UseCompressedClassPointers, "");
4372     if (is_array) {
4373       // Exclude length to copy by 8 bytes words.
4374       base_off += sizeof(int);
4375     } else {
4376       // Include klass to copy by 8 bytes words.
4377       base_off = instanceOopDesc::klass_offset_in_bytes();
4378     }
4379     assert(base_off % BytesPerLong == 0, "expect 8 bytes alignment");
4380   }
4381   src  = basic_plus_adr(src,  base_off);
4382   dest = basic_plus_adr(dest, base_off);
4383 
4384   // Compute the length also, if needed:
4385   Node* countx = size;
4386   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
4387   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
4388 
4389   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4390 
4391   ArrayCopyNode* ac = ArrayCopyNode::make(this, false, src, NULL, dest, NULL, countx, false);
4392   ac->set_clonebasic();
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);
4396 
4397   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
4398   if (card_mark) {
4399     assert(!is_array, "");
4400     // Put in store barrier for any and all oops we are sticking
4401     // into this object.  (We could avoid this if we could prove
4402     // that the object type contains no oop fields at all.)
4403     Node* no_particular_value = NULL;
4404     Node* no_particular_field = NULL;
4405     int raw_adr_idx = Compile::AliasIdxRaw;
4406     post_barrier(control(),
4407                  memory(raw_adr_type),
4408                  alloc_obj,
4409                  no_particular_field,
4410                  raw_adr_idx,
4411                  no_particular_value,
4412                  T_OBJECT,
4413                  false);
4414   }
4415 


4484     const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4485     int raw_adr_idx = Compile::AliasIdxRaw;
4486 
4487     Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)NULL);
4488     if (array_ctl != NULL) {
4489       // It's an array.
4490       PreserveJVMState pjvms(this);
4491       set_control(array_ctl);
4492       Node* obj_length = load_array_length(obj);
4493       Node* obj_size  = NULL;
4494       Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size);  // no arguments to push
4495 
4496       if (!use_ReduceInitialCardMarks()) {
4497         // If it is an oop array, it requires very special treatment,
4498         // because card marking is required on each card of the array.
4499         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
4500         if (is_obja != NULL) {
4501           PreserveJVMState pjvms2(this);
4502           set_control(is_obja);
4503           // Generate a direct call to the right arraycopy function(s).
4504           Node* alloc = tightly_coupled_allocation(alloc_obj, NULL);
4505           ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, alloc != NULL);
4506           ac->set_cloneoop();
4507           Node* n = _gvn.transform(ac);
4508           assert(n == ac, "cannot disappear");
4509           ac->connect_outputs(this);
4510 
4511           result_reg->init_req(_objArray_path, control());
4512           result_val->init_req(_objArray_path, alloc_obj);
4513           result_i_o ->set_req(_objArray_path, i_o());
4514           result_mem ->set_req(_objArray_path, reset_memory());
4515         }
4516       }
4517       // Otherwise, there are no card marks to worry about.
4518       // (We can dispense with card marks if we know the allocation
4519       //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4520       //  causes the non-eden paths to take compensating steps to
4521       //  simulate a fresh allocation, so that no further
4522       //  card marks are required in compiled code to initialize
4523       //  the object.)
4524 
4525       if (!stopped()) {
4526         copy_to_clone(obj, alloc_obj, obj_size, true, false);
4527 
4528         // Present the results of the copy.
4529         result_reg->init_req(_array_path, control());
4530         result_val->init_req(_array_path, alloc_obj);


4584       PreserveJVMState pjvms(this);
4585       CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_clone, is_virtual);
4586       Node* slow_result = set_results_for_java_call(slow_call);
4587       // this->control() comes from set_results_for_java_call
4588       result_reg->init_req(_slow_path, control());
4589       result_val->init_req(_slow_path, slow_result);
4590       result_i_o ->set_req(_slow_path, i_o());
4591       result_mem ->set_req(_slow_path, reset_memory());
4592     }
4593 
4594     // Return the combined state.
4595     set_control(    _gvn.transform(result_reg));
4596     set_i_o(        _gvn.transform(result_i_o));
4597     set_all_memory( _gvn.transform(result_mem));
4598   } // original reexecute is set back here
4599 
4600   set_result(_gvn.transform(result_val));
4601   return true;
4602 }
4603 




































4604 //------------------------------inline_arraycopy-----------------------
4605 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4606 //                                                      Object dest, int destPos,
4607 //                                                      int length);
4608 bool LibraryCallKit::inline_arraycopy() {
4609   // Get the arguments.
4610   Node* src         = argument(0);  // type: oop
4611   Node* src_offset  = argument(1);  // type: int
4612   Node* dest        = argument(2);  // type: oop
4613   Node* dest_offset = argument(3);  // type: int
4614   Node* length      = argument(4);  // type: int
4615 
4616   // The following tests must be performed



4617   // (1) src and dest are arrays.
4618   // (2) src and dest arrays must have elements of the same BasicType
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);
4636   const TypeAryPtr* top_src  = src_type->isa_aryptr();
4637   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
4638 
4639   // Do we have the type of src?
4640   bool has_src = (top_src != NULL && top_src->klass() != NULL);
4641   // Do we have the type of dest?
4642   bool has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4643   // Is the type for src from speculation?
4644   bool src_spec = false;
4645   // Is the type for dest from speculation?
4646   bool dest_spec = false;
4647 
4648   if (!has_src || !has_dest) {
4649     // We don't have sufficient type information, let's see if
4650     // speculative types can help. We need to have types for both src
4651     // and dest so that it pays off.
4652 
4653     // Do we already have or could we have type information for src
4654     bool could_have_src = has_src;
4655     // Do we already have or could we have type information for dest


4673 
4674     if (could_have_src && could_have_dest) {
4675       // This is going to pay off so emit the required guards
4676       if (!has_src) {
4677         src = maybe_cast_profiled_obj(src, src_k);
4678         src_type  = _gvn.type(src);
4679         top_src  = src_type->isa_aryptr();
4680         has_src = (top_src != NULL && top_src->klass() != NULL);
4681         src_spec = true;
4682       }
4683       if (!has_dest) {
4684         dest = maybe_cast_profiled_obj(dest, dest_k);
4685         dest_type  = _gvn.type(dest);
4686         top_dest  = dest_type->isa_aryptr();
4687         has_dest = (top_dest != NULL && top_dest->klass() != NULL);
4688         dest_spec = true;
4689       }
4690     }
4691   }
4692 
4693   if (has_src && has_dest) {




















4694     BasicType src_elem  = top_src->klass()->as_array_klass()->element_type()->basic_type();
4695     BasicType dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
4696     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
4697     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
4698   
4699     if (src_elem == dest_elem && src_elem == T_OBJECT) {









4700       // If both arrays are object arrays then having the exact types
4701       // for both will remove the need for a subtype check at runtime
4702       // before the call and may make it possible to pick a faster copy
4703       // routine (without a subtype check on every element)
4704       // Do we have the exact type of src?
4705       bool could_have_src = src_spec;
4706       // Do we have the exact type of dest?
4707       bool could_have_dest = dest_spec;
4708       ciKlass* src_k = top_src->klass();
4709       ciKlass* dest_k = top_dest->klass();
4710       if (!src_spec) {
4711         src_k = src_type->speculative_type_not_null();
4712         if (src_k != NULL && src_k->is_array_klass()) {
4713           could_have_src = true;
4714         }
4715       }
4716       if (!dest_spec) {
4717         dest_k = dest_type->speculative_type_not_null();
4718         if (dest_k != NULL && dest_k->is_array_klass()) {
4719           could_have_dest = true;
4720         }
4721       }
4722       if (could_have_src && could_have_dest) {
4723         // If we can have both exact types, emit the missing guards
4724         if (could_have_src && !src_spec) {
4725           src = maybe_cast_profiled_obj(src, src_k);
4726         }
4727         if (could_have_dest && !dest_spec) {
4728           dest = maybe_cast_profiled_obj(dest, dest_k);
4729         }
4730       }
4731     }
4732   }
4733 
4734   if (!too_many_traps(Deoptimization::Reason_intrinsic) && !src->is_top() && !dest->is_top()) {
4735     // validate arguments: enables transformation the ArrayCopyNode
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


































































































































































































































































4765     Node* src_klass  = load_object_klass(src);
4766     Node* dest_klass = load_object_klass(dest);












4767     Node* not_subtype_ctrl = gen_subtype_check(src_klass, dest_klass);
4768 
4769     if (not_subtype_ctrl != top()) {
4770       PreserveJVMState pjvms(this);
4771       set_control(not_subtype_ctrl);
4772       uncommon_trap(Deoptimization::Reason_intrinsic,
4773                     Deoptimization::Action_make_not_entrant);
4774       assert(stopped(), "Should be stopped");





















4775     }
4776     {


4777       PreserveJVMState pjvms(this);
4778       set_control(_gvn.transform(slow_region));
4779       uncommon_trap(Deoptimization::Reason_intrinsic,
4780                     Deoptimization::Action_make_not_entrant);
4781       assert(stopped(), "Should be stopped");


































































4782     }
4783   }
4784 
4785   if (stopped()) {
4786     return true;










4787   }
4788 
4789   AllocateArrayNode* alloc = tightly_coupled_allocation(dest, NULL);
4790   ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != NULL);






4791 
4792   if (notest) {
4793     ac->set_arraycopy_notest();


4794   }
4795 
4796   Node* n = _gvn.transform(ac);
4797   assert(n == ac, "cannot disappear");
4798   ac->connect_outputs(this);

4799 
4800   return true;






















4801 }
4802 
4803 
4804 // Helper function which determines if an arraycopy immediately follows
4805 // an allocation, with no intervening tests or other escapes for the object.
4806 AllocateArrayNode*
4807 LibraryCallKit::tightly_coupled_allocation(Node* ptr,
4808                                            RegionNode* slow_region) {
4809   if (stopped())             return NULL;  // no fast path
4810   if (C->AliasLevel() == 0)  return NULL;  // no MergeMems around
4811 
4812   AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
4813   if (alloc == NULL)  return NULL;
4814 
4815   Node* rawmem = memory(Compile::AliasIdxRaw);
4816   // Is the allocation's memory state untouched?
4817   if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
4818     // Bail out if there have been raw-memory effects since the allocation.
4819     // (Example:  There might have been a call or safepoint.)
4820     return NULL;


4858         Node* obs = not_ctl->fast_out(j);
4859         if (obs->in(0) == not_ctl && obs->is_Call() &&
4860             (obs->as_Call()->entry_point() == SharedRuntime::uncommon_trap_blob()->entry_point())) {
4861           found_trap = true; break;
4862         }
4863       }
4864       if (found_trap) {
4865         ctl = iff->in(0);       // This test feeds a harmless uncommon trap.
4866         continue;
4867       }
4868     }
4869     return NULL;
4870   }
4871 
4872   // If we get this far, we have an allocation which immediately
4873   // precedes the arraycopy, and we can take over zeroing the new object.
4874   // The arraycopy will finish the initialization, and provide
4875   // a new control state to which we will anchor the destination pointer.
4876 
4877   return alloc;











































































































































































































































































































4878 }
4879 
4880 //-------------inline_encodeISOArray-----------------------------------
4881 // encode char[] to byte[] in ISO_8859_1
4882 bool LibraryCallKit::inline_encodeISOArray() {
4883   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
4884   // no receiver since it is static method
4885   Node *src         = argument(0);
4886   Node *src_offset  = argument(1);
4887   Node *dst         = argument(2);
4888   Node *dst_offset  = argument(3);
4889   Node *length      = argument(4);
4890 
4891   const Type* src_type = src->Value(&_gvn);
4892   const Type* dst_type = dst->Value(&_gvn);
4893   const TypeAryPtr* top_src = src_type->isa_aryptr();
4894   const TypeAryPtr* top_dest = dst_type->isa_aryptr();
4895   if (top_src  == NULL || top_src->klass()  == NULL ||
4896       top_dest == NULL || top_dest->klass() == NULL) {
4897     // failed array check


src/share/vm/opto/library_call.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File