< prev index next >

src/share/vm/opto/library_call.cpp

Print this page
rev 8961 : [mq]: diff-shenandoah.patch


  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "compiler/compileLog.hpp"

  31 #include "oops/objArrayKlass.hpp"
  32 #include "opto/addnode.hpp"
  33 #include "opto/arraycopynode.hpp"
  34 #include "opto/c2compiler.hpp"
  35 #include "opto/callGenerator.hpp"
  36 #include "opto/castnode.hpp"
  37 #include "opto/cfgnode.hpp"
  38 #include "opto/convertnode.hpp"
  39 #include "opto/countbitsnode.hpp"
  40 #include "opto/intrinsicnode.hpp"
  41 #include "opto/idealKit.hpp"
  42 #include "opto/mathexactnode.hpp"
  43 #include "opto/movenode.hpp"
  44 #include "opto/mulnode.hpp"
  45 #include "opto/narrowptrnode.hpp"
  46 #include "opto/opaquenode.hpp"
  47 #include "opto/parse.hpp"
  48 #include "opto/runtime.hpp"

  49 #include "opto/subnode.hpp"
  50 #include "prims/nativeLookup.hpp"
  51 #include "runtime/sharedRuntime.hpp"
  52 #include "trace/traceMacros.hpp"
  53 
  54 class LibraryIntrinsic : public InlineCallGenerator {
  55   // Extend the set of intrinsics known to the runtime:
  56  public:
  57  private:
  58   bool             _is_virtual;
  59   bool             _does_virtual_dispatch;
  60   int8_t           _predicates_count;  // Intrinsic is predicated by several conditions
  61   int8_t           _last_predicate; // Last generated predicate
  62   vmIntrinsics::ID _intrinsic_id;
  63 
  64  public:
  65   LibraryIntrinsic(ciMethod* m, bool is_virtual, int predicates_count, bool does_virtual_dispatch, vmIntrinsics::ID id)
  66     : InlineCallGenerator(m),
  67       _is_virtual(is_virtual),
  68       _does_virtual_dispatch(does_virtual_dispatch),


 955   C->set_has_split_ifs(true); // Has chance for split-if optimization
 956 
 957   return _gvn.transform(result);
 958 }
 959 
 960 //------------------------------inline_string_compareTo------------------------
 961 // public int java.lang.String.compareTo(String anotherString);
 962 bool LibraryCallKit::inline_string_compareTo() {
 963   Node* receiver = null_check(argument(0));
 964   Node* arg      = null_check(argument(1));
 965   if (stopped()) {
 966     return true;
 967   }
 968   set_result(make_string_method_node(Op_StrComp, receiver, arg));
 969   return true;
 970 }
 971 
 972 //------------------------------inline_string_equals------------------------
 973 bool LibraryCallKit::inline_string_equals() {
 974   Node* receiver = null_check_receiver();





 975   // NOTE: Do not null check argument for String.equals() because spec
 976   // allows to specify NULL as argument.
 977   Node* argument = this->argument(1);





 978   if (stopped()) {
 979     return true;
 980   }
 981 
 982   // paths (plus control) merge
 983   RegionNode* region = new RegionNode(5);
 984   Node* phi = new PhiNode(region, TypeInt::BOOL);
 985 
 986   // does source == target string?
 987   Node* cmp = _gvn.transform(new CmpPNode(receiver, argument));
 988   Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
 989 
 990   Node* if_eq = generate_slow_guard(bol, NULL);
 991   if (if_eq != NULL) {
 992     // receiver == argument
 993     phi->init_req(2, intcon(1));
 994     region->init_req(2, if_eq);
 995   }
 996 
 997   // get String klass for instanceOf


1006     //instanceOf == true, fallthrough
1007 
1008     if (inst_false != NULL) {
1009       phi->init_req(3, intcon(0));
1010       region->init_req(3, inst_false);
1011     }
1012   }
1013 
1014   if (!stopped()) {
1015     const TypeOopPtr* string_type = TypeOopPtr::make_from_klass(klass);
1016 
1017     // Properly cast the argument to String
1018     argument = _gvn.transform(new CheckCastPPNode(control(), argument, string_type));
1019     // This path is taken only when argument's type is String:NotNull.
1020     argument = cast_not_null(argument, false);
1021 
1022     Node* no_ctrl = NULL;
1023 
1024     // Get start addr of receiver
1025     Node* receiver_val    = load_String_value(no_ctrl, receiver);





1026     Node* receiver_offset = load_String_offset(no_ctrl, receiver);
1027     Node* receiver_start = array_element_address(receiver_val, receiver_offset, T_CHAR);
1028 
1029     // Get length of receiver
1030     Node* receiver_cnt  = load_String_length(no_ctrl, receiver);
1031 
1032     // Get start addr of argument
1033     Node* argument_val    = load_String_value(no_ctrl, argument);





1034     Node* argument_offset = load_String_offset(no_ctrl, argument);
1035     Node* argument_start = array_element_address(argument_val, argument_offset, T_CHAR);
1036 
1037     // Get length of argument
1038     Node* argument_cnt  = load_String_length(no_ctrl, argument);
1039 
1040     // Check for receiver count != argument count
1041     Node* cmp = _gvn.transform(new CmpINode(receiver_cnt, argument_cnt));
1042     Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
1043     Node* if_ne = generate_slow_guard(bol, NULL);
1044     if (if_ne != NULL) {
1045       phi->init_req(4, intcon(0));
1046       region->init_req(4, if_ne);
1047     }
1048 
1049     // Check for count == 0 is done by assembler code for StrEquals.
1050 
1051     if (!stopped()) {
1052       Node* equals = make_string_method_node(Op_StrEquals, receiver_start, receiver_cnt, argument_start, argument_cnt);
1053       phi->init_req(1, equals);
1054       region->init_req(1, control());
1055     }
1056   }
1057 
1058   // post merge
1059   set_control(_gvn.transform(region));
1060   record_for_igvn(region);
1061 
1062   set_result(_gvn.transform(phi));
1063   return true;
1064 }
1065 
1066 //------------------------------inline_array_equals----------------------------
1067 bool LibraryCallKit::inline_array_equals() {
1068   Node* arg1 = argument(0);
1069   Node* arg2 = argument(1);




1070   set_result(_gvn.transform(new AryEqNode(control(), memory(TypeAryPtr::CHARS), arg1, arg2)));
1071   return true;
1072 }
1073 
1074 // Java version of String.indexOf(constant string)
1075 // class StringDecl {
1076 //   StringDecl(char[] ca) {
1077 //     offset = 0;
1078 //     count = ca.length;
1079 //     value = ca;
1080 //   }
1081 //   int offset;
1082 //   int count;
1083 //   char[] value;
1084 // }
1085 //
1086 // static int string_indexOf_J(StringDecl string_object, char[] target_object,
1087 //                             int targetOffset, int cache_i, int md2) {
1088 //   int cache = cache_i;
1089 //   int sourceOffset = string_object.offset;


2135   case vmIntrinsics::_reverseBytes_l:           n = new ReverseBytesLNode( 0,   arg);  break;
2136   default:  fatal_unexpected_iid(id);  break;
2137   }
2138   set_result(_gvn.transform(n));
2139   return true;
2140 }
2141 
2142 //----------------------------inline_unsafe_access----------------------------
2143 
2144 const static BasicType T_ADDRESS_HOLDER = T_LONG;
2145 
2146 // Helper that guards and inserts a pre-barrier.
2147 void LibraryCallKit::insert_pre_barrier(Node* base_oop, Node* offset,
2148                                         Node* pre_val, bool need_mem_bar) {
2149   // We could be accessing the referent field of a reference object. If so, when G1
2150   // is enabled, we need to log the value in the referent field in an SATB buffer.
2151   // This routine performs some compile time filters and generates suitable
2152   // runtime filters that guard the pre-barrier code.
2153   // Also add memory barrier for non volatile load from the referent field
2154   // to prevent commoning of loads across safepoint.
2155   if (!UseG1GC && !need_mem_bar)
2156     return;
2157 
2158   // Some compile time checks.
2159 
2160   // If offset is a constant, is it java_lang_ref_Reference::_reference_offset?
2161   const TypeX* otype = offset->find_intptr_t_type();
2162   if (otype != NULL && otype->is_con() &&
2163       otype->get_con() != java_lang_ref_Reference::referent_offset) {
2164     // Constant offset but not the reference_offset so just return
2165     return;
2166   }
2167 
2168   // We only need to generate the runtime guards for instances.
2169   const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();
2170   if (btype != NULL) {
2171     if (btype->isa_aryptr()) {
2172       // Array type so nothing to do
2173       return;
2174     }
2175 


2324         vtype = T_ADDRESS;  // it is really a C void*
2325       assert(vtype == type, "putter must accept the expected value");
2326     }
2327 #endif // ASSERT
2328  }
2329 #endif //PRODUCT
2330 
2331   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2332 
2333   Node* receiver = argument(0);  // type: oop
2334 
2335   // Build address expression.
2336   Node* adr;
2337   Node* heap_base_oop = top();
2338   Node* offset = top();
2339   Node* val;
2340 
2341   if (!is_native_ptr) {
2342     // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2343     Node* base = argument(1);  // type: oop












2344     // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2345     offset = argument(2);  // type: long
2346     // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2347     // to be plain byte offsets, which are also the same as those accepted
2348     // by oopDesc::field_base.
2349     assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2350            "fieldOffset must be byte-scaled");
2351     // 32-bit machines ignore the high half!
2352     offset = ConvL2X(offset);
2353     adr = make_unsafe_address(base, offset);
2354     heap_base_oop = base;
2355     val = is_store ? argument(4) : NULL;
2356   } else {
2357     Node* ptr = argument(1);  // type: long
2358     ptr = ConvL2X(ptr);  // adjust Java long to machine word
2359     adr = make_unsafe_address(NULL, ptr);
2360     val = is_store ? argument(3) : NULL;
2361   }
2362 
2363   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();


2475     // the end of this method.  So, pushing the load onto the stack at a later
2476     // point is fine.
2477     set_result(p);
2478   } else {
2479     // place effect of store into memory
2480     switch (type) {
2481     case T_DOUBLE:
2482       val = dstore_rounding(val);
2483       break;
2484     case T_ADDRESS:
2485       // Repackage the long as a pointer.
2486       val = ConvL2X(val);
2487       val = _gvn.transform(new CastX2PNode(val));
2488       break;
2489     }
2490 
2491     MemNode::MemOrd mo = is_volatile ? MemNode::release : MemNode::unordered;
2492     if (type != T_OBJECT ) {
2493       (void) store_to_memory(control(), adr, val, type, adr_type, mo, is_volatile);
2494     } else {

2495       // Possibly an oop being stored to Java heap or native memory
2496       if (!TypePtr::NULL_PTR->higher_equal(_gvn.type(heap_base_oop))) {
2497         // oop to Java heap.
2498         (void) store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type, mo);
2499       } else {
2500         // We can't tell at compile time if we are storing in the Java heap or outside
2501         // of it. So we need to emit code to conditionally do the proper type of
2502         // store.
2503 
2504         IdealKit ideal(this);
2505 #define __ ideal.
2506         // QQQ who knows what probability is here??
2507         __ if_then(heap_base_oop, BoolTest::ne, null(), PROB_UNLIKELY(0.999)); {
2508           // Sync IdealKit and graphKit.
2509           sync_kit(ideal);
2510           Node* st = store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type, mo);
2511           // Update IdealKit memory.
2512           __ sync_kit(this);
2513         } __ else_(); {
2514           __ store(__ ctrl(), adr, val, type, alias_type->index(), mo, is_volatile);


2604     const bool two_slot_type = type2size[type] == 2;
2605     receiver = argument(0);  // type: oop
2606     base     = argument(1);  // type: oop
2607     offset   = argument(2);  // type: long
2608     oldval   = argument(4);  // type: oop, int, or long
2609     newval   = argument(two_slot_type ? 6 : 5);  // type: oop, int, or long
2610   } else if (kind == LS_xadd || kind == LS_xchg){
2611     receiver = argument(0);  // type: oop
2612     base     = argument(1);  // type: oop
2613     offset   = argument(2);  // type: long
2614     oldval   = NULL;
2615     newval   = argument(4);  // type: oop, int, or long
2616   }
2617 
2618   // Null check receiver.
2619   receiver = null_check(receiver);
2620   if (stopped()) {
2621     return true;
2622   }
2623 


2624   // Build field offset expression.
2625   // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2626   // to be plain byte offsets, which are also the same as those accepted
2627   // by oopDesc::field_base.
2628   assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2629   // 32-bit machines ignore the high half of long offsets
2630   offset = ConvL2X(offset);
2631   Node* adr = make_unsafe_address(base, offset);
2632   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2633 
2634   // For CAS, unlike inline_unsafe_access, there seems no point in
2635   // trying to refine types. Just use the coarse types here.
2636   const Type *value_type = Type::get_const_basic_type(type);
2637   Compile::AliasType* alias_type = C->alias_type(adr_type);
2638   assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2639 
2640   if (kind == LS_xchg && type == T_OBJECT) {
2641     const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2642     if (tjp != NULL) {
2643       value_type = tjp;


2645   }
2646 
2647   int alias_idx = C->get_alias_index(adr_type);
2648 
2649   // Memory-model-wise, a LoadStore acts like a little synchronized
2650   // block, so needs barriers on each side.  These don't translate
2651   // into actual barriers on most machines, but we still need rest of
2652   // compiler to respect ordering.
2653 
2654   insert_mem_bar(Op_MemBarRelease);
2655   insert_mem_bar(Op_MemBarCPUOrder);
2656 
2657   // 4984716: MemBars must be inserted before this
2658   //          memory node in order to avoid a false
2659   //          dependency which will confuse the scheduler.
2660   Node *mem = memory(alias_idx);
2661 
2662   // For now, we handle only those cases that actually exist: ints,
2663   // longs, and Object. Adding others should be straightforward.
2664   Node* load_store;

2665   switch(type) {
2666   case T_INT:
2667     if (kind == LS_xadd) {
2668       load_store = _gvn.transform(new GetAndAddINode(control(), mem, adr, newval, adr_type));
2669     } else if (kind == LS_xchg) {
2670       load_store = _gvn.transform(new GetAndSetINode(control(), mem, adr, newval, adr_type));
2671     } else if (kind == LS_cmpxchg) {
2672       load_store = _gvn.transform(new CompareAndSwapINode(control(), mem, adr, newval, oldval));
2673     } else {
2674       ShouldNotReachHere();
2675     }

2676     break;
2677   case T_LONG:
2678     if (kind == LS_xadd) {
2679       load_store = _gvn.transform(new GetAndAddLNode(control(), mem, adr, newval, adr_type));
2680     } else if (kind == LS_xchg) {
2681       load_store = _gvn.transform(new GetAndSetLNode(control(), mem, adr, newval, adr_type));
2682     } else if (kind == LS_cmpxchg) {
2683       load_store = _gvn.transform(new CompareAndSwapLNode(control(), mem, adr, newval, oldval));
2684     } else {
2685       ShouldNotReachHere();
2686     }

2687     break;
2688   case T_OBJECT:
2689     // Transformation of a value which could be NULL pointer (CastPP #NULL)
2690     // could be delayed during Parse (for example, in adjust_map_after_if()).
2691     // Execute transformation here to avoid barrier generation in such case.
2692     if (_gvn.type(newval) == TypePtr::NULL_PTR)
2693       newval = _gvn.makecon(TypePtr::NULL_PTR);
2694 


2695     // Reference stores need a store barrier.
2696     if (kind == LS_xchg) {
2697       // If pre-barrier must execute before the oop store, old value will require do_load here.
2698       if (!can_move_pre_barrier()) {
2699         pre_barrier(true /* do_load*/,
2700                     control(), base, adr, alias_idx, newval, value_type->make_oopptr(),
2701                     NULL /* pre_val*/,
2702                     T_OBJECT);
2703       } // Else move pre_barrier to use load_store value, see below.
2704     } else if (kind == LS_cmpxchg) {
2705       // Same as for newval above:
2706       if (_gvn.type(oldval) == TypePtr::NULL_PTR) {
2707         oldval = _gvn.makecon(TypePtr::NULL_PTR);
2708       }
2709       // The only known value which might get overwritten is oldval.
2710       pre_barrier(false /* do_load */,
2711                   control(), NULL, NULL, max_juint, NULL, NULL,
2712                   oldval /* pre_val */,
2713                   T_OBJECT);
2714     } else {
2715       ShouldNotReachHere();
2716     }
2717 
2718 #ifdef _LP64
2719     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2720       Node *newval_enc = _gvn.transform(new EncodePNode(newval, newval->bottom_type()->make_narrowoop()));
2721       if (kind == LS_xchg) {
2722         load_store = _gvn.transform(new GetAndSetNNode(control(), mem, adr,
2723                                                        newval_enc, adr_type, value_type->make_narrowoop()));
2724       } else {
2725         assert(kind == LS_cmpxchg, "wrong LoadStore operation");
2726         Node *oldval_enc = _gvn.transform(new EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
2727         load_store = _gvn.transform(new CompareAndSwapNNode(control(), mem, adr,
2728                                                                 newval_enc, oldval_enc));
2729       }

2730     } else
2731 #endif
2732     {
2733       if (kind == LS_xchg) {
2734         load_store = _gvn.transform(new GetAndSetPNode(control(), mem, adr, newval, adr_type, value_type->is_oopptr()));

2735       } else {
2736         assert(kind == LS_cmpxchg, "wrong LoadStore operation");
2737         load_store = _gvn.transform(new CompareAndSwapPNode(control(), mem, adr, newval, oldval));





























































2738       }
2739     }
2740     if (kind == LS_cmpxchg) {
2741       // Emit the post barrier only when the actual store happened.
2742       // This makes sense to check only for compareAndSet that can fail to set the value.
2743       // CAS success path is marked more likely since we anticipate this is a performance
2744       // critical path, while CAS failure path can use the penalty for going through unlikely
2745       // path as backoff. Which is still better than doing a store barrier there.
2746       IdealKit ideal(this);
2747       ideal.if_then(load_store, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
2748         sync_kit(ideal);
2749         post_barrier(ideal.ctrl(), load_store, base, adr, alias_idx, newval, T_OBJECT, true);
2750         ideal.sync_kit(this);
2751       } ideal.end_if();
2752       final_sync(ideal);
2753     } else {
2754       post_barrier(control(), load_store, base, adr, alias_idx, newval, T_OBJECT, true);
2755     }
2756     break;
2757   default:
2758     fatal(err_msg_res("unexpected type %d: %s", type, type2name(type)));
2759     break;
2760   }
2761 
2762   // SCMemProjNodes represent the memory state of a LoadStore. Their
2763   // main role is to prevent LoadStore nodes from being optimized away
2764   // when their results aren't used.
2765   Node* proj = _gvn.transform(new SCMemProjNode(load_store));
2766   set_memory(proj, alias_idx);
2767 
2768   if (type == T_OBJECT && kind == LS_xchg) {
2769 #ifdef _LP64
2770     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2771       load_store = _gvn.transform(new DecodeNNode(load_store, load_store->get_ptr_type()));
2772     }
2773 #endif
2774     if (can_move_pre_barrier()) {
2775       // Don't need to load pre_val. The old value is returned by load_store.
2776       // The pre_barrier can execute after the xchg as long as no safepoint
2777       // gets inserted between them.
2778       pre_barrier(false /* do_load */,
2779                   control(), NULL, NULL, max_juint, NULL, NULL,
2780                   load_store /* pre_val */,
2781                   T_OBJECT);
2782     }
2783   }
2784 
2785   // Add the trailing membar surrounding the access
2786   insert_mem_bar(Op_MemBarCPUOrder);
2787   insert_mem_bar(Op_MemBarAcquire);
2788 
2789   assert(type2size[load_store->bottom_type()->basic_type()] == type2size[rtype], "result type should match");
2790   set_result(load_store);
2791   return true;
2792 }
2793 
2794 //----------------------------inline_unsafe_ordered_store----------------------
2795 // public native void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x);
2796 // public native void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x);
2797 // public native void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x);
2798 bool LibraryCallKit::inline_unsafe_ordered_store(BasicType type) {
2799   // This is another variant of inline_unsafe_access, differing in
2800   // that it always issues store-store ("release") barrier and ensures
2801   // store-atomicity (which only matters for "long").
2802 
2803   if (callee()->is_static())  return false;  // caller must have the capability!
2804 
2805 #ifndef PRODUCT
2806   {
2807     ResourceMark rm;
2808     // Check the signatures.
2809     ciSignature* sig = callee()->signature();
2810 #ifdef ASSERT


2814     assert(sig->type_at(0)->basic_type() == T_OBJECT, "base is object");
2815     assert(sig->type_at(1)->basic_type() == T_LONG, "offset is long");
2816 #endif // ASSERT
2817   }
2818 #endif //PRODUCT
2819 
2820   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2821 
2822   // Get arguments:
2823   Node* receiver = argument(0);  // type: oop
2824   Node* base     = argument(1);  // type: oop
2825   Node* offset   = argument(2);  // type: long
2826   Node* val      = argument(4);  // type: oop, int, or long
2827 
2828   // Null check receiver.
2829   receiver = null_check(receiver);
2830   if (stopped()) {
2831     return true;
2832   }
2833 


2834   // Build field offset expression.
2835   assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2836   // 32-bit machines ignore the high half of long offsets
2837   offset = ConvL2X(offset);
2838   Node* adr = make_unsafe_address(base, offset);
2839   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2840   const Type *value_type = Type::get_const_basic_type(type);
2841   Compile::AliasType* alias_type = C->alias_type(adr_type);
2842 
2843   insert_mem_bar(Op_MemBarRelease);
2844   insert_mem_bar(Op_MemBarCPUOrder);
2845   // Ensure that the store is atomic for longs:
2846   const bool require_atomic_access = true;
2847   Node* store;
2848   if (type == T_OBJECT) // reference stores need a store barrier.

2849     store = store_oop_to_unknown(control(), base, adr, adr_type, val, type, MemNode::release);

2850   else {
2851     store = store_to_memory(control(), adr, val, type, adr_type, MemNode::release, require_atomic_access);
2852   }
2853   insert_mem_bar(Op_MemBarCPUOrder);
2854   return true;
2855 }
2856 
2857 bool LibraryCallKit::inline_unsafe_fence(vmIntrinsics::ID id) {
2858   // Regardless of form, don't allow previous ld/st to move down,
2859   // then issue acquire, release, or volatile mem_bar.
2860   insert_mem_bar(Op_MemBarCPUOrder);
2861   switch(id) {
2862     case vmIntrinsics::_loadFence:
2863       insert_mem_bar(Op_LoadFence);
2864       return true;
2865     case vmIntrinsics::_storeFence:
2866       insert_mem_bar(Op_StoreFence);
2867       return true;
2868     case vmIntrinsics::_fullFence:
2869       insert_mem_bar(Op_MemBarVolatile);


3151   Node* bits = intcon(modifier_bits);
3152   Node* mbit = _gvn.transform(new AndINode(mods, mask));
3153   Node* cmp  = _gvn.transform(new CmpINode(mbit, bits));
3154   Node* bol  = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3155   return generate_fair_guard(bol, region);
3156 }
3157 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3158   return generate_access_flags_guard(kls, JVM_ACC_INTERFACE, 0, region);
3159 }
3160 
3161 //-------------------------inline_native_Class_query-------------------
3162 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
3163   const Type* return_type = TypeInt::BOOL;
3164   Node* prim_return_value = top();  // what happens if it's a primitive class?
3165   bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3166   bool expect_prim = false;     // most of these guys expect to work on refs
3167 
3168   enum { _normal_path = 1, _prim_path = 2, PATH_LIMIT };
3169 
3170   Node* mirror = argument(0);





3171   Node* obj    = top();
3172 
3173   switch (id) {
3174   case vmIntrinsics::_isInstance:
3175     // nothing is an instance of a primitive type
3176     prim_return_value = intcon(0);
3177     obj = argument(1);



3178     break;
3179   case vmIntrinsics::_getModifiers:
3180     prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3181     assert(is_power_of_2((int)JVM_ACC_WRITTEN_FLAGS+1), "change next line");
3182     return_type = TypeInt::make(0, JVM_ACC_WRITTEN_FLAGS, Type::WidenMin);
3183     break;
3184   case vmIntrinsics::_isInterface:
3185     prim_return_value = intcon(0);
3186     break;
3187   case vmIntrinsics::_isArray:
3188     prim_return_value = intcon(0);
3189     expect_prim = true;  // cf. ObjectStreamClass.getClassSignature
3190     break;
3191   case vmIntrinsics::_isPrimitive:
3192     prim_return_value = intcon(1);
3193     expect_prim = true;  // obviously
3194     break;
3195   case vmIntrinsics::_getSuperclass:
3196     prim_return_value = null();
3197     return_type = TypeInstPtr::MIRROR->cast_to_ptr_type(TypePtr::BotPTR);


3405     PreserveJVMState pjvms(this);
3406     set_control(_gvn.transform(region));
3407     uncommon_trap(Deoptimization::Reason_intrinsic,
3408                   Deoptimization::Action_maybe_recompile);
3409   }
3410   if (!stopped()) {
3411     set_result(res);
3412   }
3413   return true;
3414 }
3415 
3416 
3417 //--------------------------inline_native_subtype_check------------------------
3418 // This intrinsic takes the JNI calls out of the heart of
3419 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
3420 bool LibraryCallKit::inline_native_subtype_check() {
3421   // Pull both arguments off the stack.
3422   Node* args[2];                // two java.lang.Class mirrors: superc, subc
3423   args[0] = argument(0);
3424   args[1] = argument(1);









3425   Node* klasses[2];             // corresponding Klasses: superk, subk
3426   klasses[0] = klasses[1] = top();
3427 
3428   enum {
3429     // A full decision tree on {superc is prim, subc is prim}:
3430     _prim_0_path = 1,           // {P,N} => false
3431                                 // {P,P} & superc!=subc => false
3432     _prim_same_path,            // {P,P} & superc==subc => true
3433     _prim_1_path,               // {N,P} => false
3434     _ref_subtype_path,          // {N,N} & subtype check wins => true
3435     _both_ref_path,             // {N,N} & subtype check loses => false
3436     PATH_LIMIT
3437   };
3438 
3439   RegionNode* region = new RegionNode(PATH_LIMIT);
3440   Node*       phi    = new PhiNode(region, TypeInt::BOOL);
3441   record_for_igvn(region);
3442 
3443   const TypePtr* adr_type = TypeRawPtr::BOTTOM;   // memory type of loads
3444   const TypeKlassPtr* kls_type = TypeKlassPtr::OBJECT_OR_NULL;


3711 
3712     // Bail out if length is negative.
3713     // Without this the new_array would throw
3714     // NegativeArraySizeException but IllegalArgumentException is what
3715     // should be thrown
3716     generate_negative_guard(length, bailout, &length);
3717 
3718     if (bailout->req() > 1) {
3719       PreserveJVMState pjvms(this);
3720       set_control(_gvn.transform(bailout));
3721       uncommon_trap(Deoptimization::Reason_intrinsic,
3722                     Deoptimization::Action_maybe_recompile);
3723     }
3724 
3725     if (!stopped()) {
3726       // How many elements will we copy from the original?
3727       // The answer is MinI(orig_length - start, length).
3728       Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
3729       Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
3730 


3731       // Generate a direct call to the right arraycopy function(s).
3732       // We know the copy is disjoint but we might not know if the
3733       // oop stores need checking.
3734       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
3735       // This will fail a store-check if x contains any non-nulls.
3736 
3737       // ArrayCopyNode:Ideal may transform the ArrayCopyNode to
3738       // loads/stores but it is legal only if we're sure the
3739       // Arrays.copyOf would succeed. So we need all input arguments
3740       // to the copyOf to be validated, including that the copy to the
3741       // new array won't trigger an ArrayStoreException. That subtype
3742       // check can be optimized if we know something on the type of
3743       // the input array from type speculation.
3744       if (_gvn.type(klass_node)->singleton()) {
3745         ciKlass* subk   = _gvn.type(load_object_klass(original))->is_klassptr()->klass();
3746         ciKlass* superk = _gvn.type(klass_node)->is_klassptr()->klass();
3747 
3748         int test = C->static_subtype_check(superk, subk);
3749         if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
3750           const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();


3892   PhiNode*    result_val = new PhiNode(result_reg, TypeInt::INT);
3893   PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
3894   PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
3895   Node* obj = NULL;
3896   if (!is_static) {
3897     // Check for hashing null object
3898     obj = null_check_receiver();
3899     if (stopped())  return true;        // unconditionally null
3900     result_reg->init_req(_null_path, top());
3901     result_val->init_req(_null_path, top());
3902   } else {
3903     // Do a null check, and return zero if null.
3904     // System.identityHashCode(null) == 0
3905     obj = argument(0);
3906     Node* null_ctl = top();
3907     obj = null_check_oop(obj, &null_ctl);
3908     result_reg->init_req(_null_path, null_ctl);
3909     result_val->init_req(_null_path, _gvn.intcon(0));
3910   }
3911 




3912   // Unconditionally null?  Then return right away.
3913   if (stopped()) {
3914     set_control( result_reg->in(_null_path));
3915     if (!stopped())
3916       set_result(result_val->in(_null_path));
3917     return true;
3918   }
3919 
3920   // We only go to the fast case code if we pass a number of guards.  The
3921   // paths which do not pass are accumulated in the slow_region.
3922   RegionNode* slow_region = new RegionNode(1);
3923   record_for_igvn(slow_region);
3924 
3925   // If this is a virtual call, we generate a funny guard.  We pull out
3926   // the vtable entry corresponding to hashCode() from the target object.
3927   // If the target method which we are calling happens to be the native
3928   // Object hashCode() method, we pass the guard.  We do not need this
3929   // guard for non-virtual calls -- the caller is known to be the native
3930   // Object hashCode().
3931   if (is_virtual) {


4207 #endif //_LP64
4208 
4209 //----------------------inline_unsafe_copyMemory-------------------------
4210 // public native void sun.misc.Unsafe.copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4211 bool LibraryCallKit::inline_unsafe_copyMemory() {
4212   if (callee()->is_static())  return false;  // caller must have the capability!
4213   null_check_receiver();  // null-check receiver
4214   if (stopped())  return true;
4215 
4216   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
4217 
4218   Node* src_ptr =         argument(1);   // type: oop
4219   Node* src_off = ConvL2X(argument(2));  // type: long
4220   Node* dst_ptr =         argument(4);   // type: oop
4221   Node* dst_off = ConvL2X(argument(5));  // type: long
4222   Node* size    = ConvL2X(argument(7));  // type: long
4223 
4224   assert(Unsafe_field_offset_to_byte_offset(11) == 11,
4225          "fieldOffset must be byte-scaled");
4226 



4227   Node* src = make_unsafe_address(src_ptr, src_off);
4228   Node* dst = make_unsafe_address(dst_ptr, dst_off);
4229 
4230   // Conservatively insert a memory barrier on all memory slices.
4231   // Do not let writes of the copy source or destination float below the copy.
4232   insert_mem_bar(Op_MemBarCPUOrder);
4233 
4234   // Call it.  Note that the length argument is not scaled.
4235   make_runtime_call(RC_LEAF|RC_NO_FP,
4236                     OptoRuntime::fast_arraycopy_Type(),
4237                     StubRoutines::unsafe_arraycopy(),
4238                     "unsafe_arraycopy",
4239                     TypeRawPtr::BOTTOM,
4240                     src, dst, size XTOP);
4241 
4242   // Do not let reads of the copy destination float above the copy.
4243   insert_mem_bar(Op_MemBarCPUOrder);
4244 
4245   return true;
4246 }
4247 
4248 //------------------------clone_coping-----------------------------------
4249 // Helper function for inline_native_clone.
4250 void LibraryCallKit::copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark) {
4251   assert(obj_size != NULL, "");
4252   Node* raw_obj = alloc_obj->in(1);
4253   assert(alloc_obj->is_CheckCastPP() && raw_obj->is_Proj() && raw_obj->in(0)->is_Allocate(), "");
4254 


4255   AllocateNode* alloc = NULL;
4256   if (ReduceBulkZeroing) {
4257     // We will be completely responsible for initializing this object -
4258     // mark Initialize node as complete.
4259     alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
4260     // The object was just allocated - there should be no any stores!
4261     guarantee(alloc != NULL && alloc->maybe_set_complete(&_gvn), "");
4262     // Mark as complete_with_arraycopy so that on AllocateNode
4263     // expansion, we know this AllocateNode is initialized by an array
4264     // copy and a StoreStore barrier exists after the array copy.
4265     alloc->initialization()->set_complete_with_arraycopy();
4266   }
4267 
4268   // Copy the fastest available way.
4269   // TODO: generate fields copies for small objects instead.
4270   Node* src  = obj;
4271   Node* dest = alloc_obj;
4272   Node* size = _gvn.transform(obj_size);
4273 
4274   // Exclude the header but include array length to copy by 8 bytes words.


4292   }
4293   src  = basic_plus_adr(src,  base_off);
4294   dest = basic_plus_adr(dest, base_off);
4295 
4296   // Compute the length also, if needed:
4297   Node* countx = size;
4298   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
4299   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
4300 
4301   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4302 
4303   ArrayCopyNode* ac = ArrayCopyNode::make(this, false, src, NULL, dest, NULL, countx, false);
4304   ac->set_clonebasic();
4305   Node* n = _gvn.transform(ac);
4306   if (n == ac) {
4307     set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), raw_adr_type);
4308   } else {
4309     set_all_memory(n);
4310   }
4311 









4312   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
4313   if (card_mark) {
4314     assert(!is_array, "");
4315     // Put in store barrier for any and all oops we are sticking
4316     // into this object.  (We could avoid this if we could prove
4317     // that the object type contains no oop fields at all.)
4318     Node* no_particular_value = NULL;
4319     Node* no_particular_field = NULL;
4320     int raw_adr_idx = Compile::AliasIdxRaw;
4321     post_barrier(control(),
4322                  memory(raw_adr_type),
4323                  alloc_obj,
4324                  no_particular_field,
4325                  raw_adr_idx,
4326                  no_particular_value,
4327                  T_OBJECT,
4328                  false);
4329   }
4330 
4331   // Do not let reads from the cloned object float above the arraycopy.


4418 
4419     const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4420     int raw_adr_idx = Compile::AliasIdxRaw;
4421 
4422     Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)NULL);
4423     if (array_ctl != NULL) {
4424       // It's an array.
4425       PreserveJVMState pjvms(this);
4426       set_control(array_ctl);
4427       Node* obj_length = load_array_length(obj);
4428       Node* obj_size  = NULL;
4429       Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size);  // no arguments to push
4430 
4431       if (!use_ReduceInitialCardMarks()) {
4432         // If it is an oop array, it requires very special treatment,
4433         // because card marking is required on each card of the array.
4434         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
4435         if (is_obja != NULL) {
4436           PreserveJVMState pjvms2(this);
4437           set_control(is_obja);



4438           // Generate a direct call to the right arraycopy function(s).
4439           Node* alloc = tightly_coupled_allocation(alloc_obj, NULL);
4440           ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, alloc != NULL);
4441           ac->set_cloneoop();
4442           Node* n = _gvn.transform(ac);
4443           assert(n == ac, "cannot disappear");
4444           ac->connect_outputs(this);
4445 
4446           result_reg->init_req(_objArray_path, control());
4447           result_val->init_req(_objArray_path, alloc_obj);
4448           result_i_o ->set_req(_objArray_path, i_o());
4449           result_mem ->set_req(_objArray_path, reset_memory());
4450         }
4451       }
4452       // Otherwise, there are no card marks to worry about.
4453       // (We can dispense with card marks if we know the allocation
4454       //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4455       //  causes the non-eden paths to take compensating steps to
4456       //  simulate a fresh allocation, so that no further
4457       //  card marks are required in compiled code to initialize


4666     _gvn.hash_delete(dest);
4667     dest->set_req(0, control());
4668     Node* destx = _gvn.transform(dest);
4669     assert(destx == dest, "where has the allocation result gone?");
4670   }
4671 }
4672 
4673 
4674 //------------------------------inline_arraycopy-----------------------
4675 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4676 //                                                      Object dest, int destPos,
4677 //                                                      int length);
4678 bool LibraryCallKit::inline_arraycopy() {
4679   // Get the arguments.
4680   Node* src         = argument(0);  // type: oop
4681   Node* src_offset  = argument(1);  // type: int
4682   Node* dest        = argument(2);  // type: oop
4683   Node* dest_offset = argument(3);  // type: int
4684   Node* length      = argument(4);  // type: int
4685 


4686 
4687   // Check for allocation before we add nodes that would confuse
4688   // tightly_coupled_allocation()
4689   AllocateArrayNode* alloc = tightly_coupled_allocation(dest, NULL);
4690 
4691   int saved_reexecute_sp = -1;
4692   JVMState* saved_jvms = arraycopy_restore_alloc_state(alloc, saved_reexecute_sp);
4693   // See arraycopy_restore_alloc_state() comment
4694   // if alloc == NULL we don't have to worry about a tightly coupled allocation so we can emit all needed guards
4695   // if saved_jvms != NULL (then alloc != NULL) then we can handle guards and a tightly coupled allocation
4696   // if saved_jvms == NULL and alloc != NULL, we can’t emit any guards
4697   bool can_emit_guards = (alloc == NULL || saved_jvms != NULL);
4698 
4699   // The following tests must be performed
4700   // (1) src and dest are arrays.
4701   // (2) src and dest arrays must have elements of the same BasicType
4702   // (3) src and dest must not be null.
4703   // (4) src_offset must not be negative.
4704   // (5) dest_offset must not be negative.
4705   // (6) length must not be negative.


4905   Node* n = _gvn.transform(ac);
4906   if (n == ac) {
4907     ac->connect_outputs(this);
4908   } else {
4909     assert(validated, "shouldn't transform if all arguments not validated");
4910     set_all_memory(n);
4911   }
4912 
4913   return true;
4914 }
4915 
4916 
4917 // Helper function which determines if an arraycopy immediately follows
4918 // an allocation, with no intervening tests or other escapes for the object.
4919 AllocateArrayNode*
4920 LibraryCallKit::tightly_coupled_allocation(Node* ptr,
4921                                            RegionNode* slow_region) {
4922   if (stopped())             return NULL;  // no fast path
4923   if (C->AliasLevel() == 0)  return NULL;  // no MergeMems around
4924 


4925   AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
4926   if (alloc == NULL)  return NULL;
4927 
4928   Node* rawmem = memory(Compile::AliasIdxRaw);
4929   // Is the allocation's memory state untouched?
4930   if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
4931     // Bail out if there have been raw-memory effects since the allocation.
4932     // (Example:  There might have been a call or safepoint.)
4933     return NULL;
4934   }
4935   rawmem = rawmem->in(0)->as_Initialize()->memory(Compile::AliasIdxRaw);
4936   if (!(rawmem->is_Proj() && rawmem->in(0) == alloc)) {
4937     return NULL;
4938   }
4939 
4940   // There must be no unexpected observers of this allocation.
4941   for (DUIterator_Fast imax, i = ptr->fast_outs(imax); i < imax; i++) {
4942     Node* obs = ptr->fast_out(i);
4943     if (obs != this->map()) {
4944       return NULL;


4984 
4985   // If we get this far, we have an allocation which immediately
4986   // precedes the arraycopy, and we can take over zeroing the new object.
4987   // The arraycopy will finish the initialization, and provide
4988   // a new control state to which we will anchor the destination pointer.
4989 
4990   return alloc;
4991 }
4992 
4993 //-------------inline_encodeISOArray-----------------------------------
4994 // encode char[] to byte[] in ISO_8859_1
4995 bool LibraryCallKit::inline_encodeISOArray() {
4996   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
4997   // no receiver since it is static method
4998   Node *src         = argument(0);
4999   Node *src_offset  = argument(1);
5000   Node *dst         = argument(2);
5001   Node *dst_offset  = argument(3);
5002   Node *length      = argument(4);
5003 



5004   const Type* src_type = src->Value(&_gvn);
5005   const Type* dst_type = dst->Value(&_gvn);
5006   const TypeAryPtr* top_src = src_type->isa_aryptr();
5007   const TypeAryPtr* top_dest = dst_type->isa_aryptr();
5008   if (top_src  == NULL || top_src->klass()  == NULL ||
5009       top_dest == NULL || top_dest->klass() == NULL) {
5010     // failed array check
5011     return false;
5012   }
5013 
5014   // Figure out the size and type of the elements we will be copying.
5015   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5016   BasicType dst_elem = dst_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5017   if (src_elem != T_CHAR || dst_elem != T_BYTE) {
5018     return false;
5019   }
5020   Node* src_start = array_element_address(src, src_offset, src_elem);
5021   Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
5022   // 'src_start' points to src array + scaled offset
5023   // 'dst_start' points to dst array + scaled offset


5033 
5034 //-------------inline_multiplyToLen-----------------------------------
5035 bool LibraryCallKit::inline_multiplyToLen() {
5036   assert(UseMultiplyToLenIntrinsic, "not implemented on this platform");
5037 
5038   address stubAddr = StubRoutines::multiplyToLen();
5039   if (stubAddr == NULL) {
5040     return false; // Intrinsic's stub is not implemented on this platform
5041   }
5042   const char* stubName = "multiplyToLen";
5043 
5044   assert(callee()->signature()->size() == 5, "multiplyToLen has 5 parameters");
5045 
5046   // no receiver because it is a static method
5047   Node* x    = argument(0);
5048   Node* xlen = argument(1);
5049   Node* y    = argument(2);
5050   Node* ylen = argument(3);
5051   Node* z    = argument(4);
5052 




5053   const Type* x_type = x->Value(&_gvn);
5054   const Type* y_type = y->Value(&_gvn);
5055   const TypeAryPtr* top_x = x_type->isa_aryptr();
5056   const TypeAryPtr* top_y = y_type->isa_aryptr();
5057   if (top_x  == NULL || top_x->klass()  == NULL ||
5058       top_y == NULL || top_y->klass() == NULL) {
5059     // failed array check
5060     return false;
5061   }
5062 
5063   BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5064   BasicType y_elem = y_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5065   if (x_elem != T_INT || y_elem != T_INT) {
5066     return false;
5067   }
5068 
5069   // Set the original stack and the reexecute bit for the interpreter to reexecute
5070   // the bytecode that invokes BigInteger.multiplyToLen() if deoptimization happens
5071   // on the return from z array allocation in runtime.
5072   { PreserveReexecuteState preexecs(this);


5133   return true;
5134 }
5135 
5136 //-------------inline_squareToLen------------------------------------
5137 bool LibraryCallKit::inline_squareToLen() {
5138   assert(UseSquareToLenIntrinsic, "not implementated on this platform");
5139 
5140   address stubAddr = StubRoutines::squareToLen();
5141   if (stubAddr == NULL) {
5142     return false; // Intrinsic's stub is not implemented on this platform
5143   }
5144   const char* stubName = "squareToLen";
5145 
5146   assert(callee()->signature()->size() == 4, "implSquareToLen has 4 parameters");
5147 
5148   Node* x    = argument(0);
5149   Node* len  = argument(1);
5150   Node* z    = argument(2);
5151   Node* zlen = argument(3);
5152 



5153   const Type* x_type = x->Value(&_gvn);
5154   const Type* z_type = z->Value(&_gvn);
5155   const TypeAryPtr* top_x = x_type->isa_aryptr();
5156   const TypeAryPtr* top_z = z_type->isa_aryptr();
5157   if (top_x  == NULL || top_x->klass()  == NULL ||
5158       top_z  == NULL || top_z->klass()  == NULL) {
5159     // failed array check
5160     return false;
5161   }
5162 
5163   BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5164   BasicType z_elem = z_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5165   if (x_elem != T_INT || z_elem != T_INT) {
5166     return false;
5167   }
5168 
5169 
5170   Node* x_start = array_element_address(x, intcon(0), x_elem);
5171   Node* z_start = array_element_address(z, intcon(0), z_elem);
5172 


5180 }
5181 
5182 //-------------inline_mulAdd------------------------------------------
5183 bool LibraryCallKit::inline_mulAdd() {
5184   assert(UseMulAddIntrinsic, "not implementated on this platform");
5185 
5186   address stubAddr = StubRoutines::mulAdd();
5187   if (stubAddr == NULL) {
5188     return false; // Intrinsic's stub is not implemented on this platform
5189   }
5190   const char* stubName = "mulAdd";
5191 
5192   assert(callee()->signature()->size() == 5, "mulAdd has 5 parameters");
5193 
5194   Node* out      = argument(0);
5195   Node* in       = argument(1);
5196   Node* offset   = argument(2);
5197   Node* len      = argument(3);
5198   Node* k        = argument(4);
5199 



5200   const Type* out_type = out->Value(&_gvn);
5201   const Type* in_type = in->Value(&_gvn);
5202   const TypeAryPtr* top_out = out_type->isa_aryptr();
5203   const TypeAryPtr* top_in = in_type->isa_aryptr();
5204   if (top_out  == NULL || top_out->klass()  == NULL ||
5205       top_in == NULL || top_in->klass() == NULL) {
5206     // failed array check
5207     return false;
5208   }
5209 
5210   BasicType out_elem = out_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5211   BasicType in_elem = in_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5212   if (out_elem != T_INT || in_elem != T_INT) {
5213     return false;
5214   }
5215 
5216   Node* outlen = load_array_length(out);
5217   Node* new_offset = _gvn.transform(new SubINode(outlen, offset));
5218   Node* out_start = array_element_address(out, intcon(0), out_elem);
5219   Node* in_start = array_element_address(in, intcon(0), in_elem);


5229 
5230 //-------------inline_montgomeryMultiply-----------------------------------
5231 bool LibraryCallKit::inline_montgomeryMultiply() {
5232   address stubAddr = StubRoutines::montgomeryMultiply();
5233   if (stubAddr == NULL) {
5234     return false; // Intrinsic's stub is not implemented on this platform
5235   }
5236 
5237   assert(UseMontgomeryMultiplyIntrinsic, "not implemented on this platform");
5238   const char* stubName = "montgomery_square";
5239 
5240   assert(callee()->signature()->size() == 7, "montgomeryMultiply has 7 parameters");
5241 
5242   Node* a    = argument(0);
5243   Node* b    = argument(1);
5244   Node* n    = argument(2);
5245   Node* len  = argument(3);
5246   Node* inv  = argument(4);
5247   Node* m    = argument(6);
5248 





5249   const Type* a_type = a->Value(&_gvn);
5250   const TypeAryPtr* top_a = a_type->isa_aryptr();
5251   const Type* b_type = b->Value(&_gvn);
5252   const TypeAryPtr* top_b = b_type->isa_aryptr();
5253   const Type* n_type = a->Value(&_gvn);
5254   const TypeAryPtr* top_n = n_type->isa_aryptr();
5255   const Type* m_type = a->Value(&_gvn);
5256   const TypeAryPtr* top_m = m_type->isa_aryptr();
5257   if (top_a  == NULL || top_a->klass()  == NULL ||
5258       top_b == NULL || top_b->klass()  == NULL ||
5259       top_n == NULL || top_n->klass()  == NULL ||
5260       top_m == NULL || top_m->klass()  == NULL) {
5261     // failed array check
5262     return false;
5263   }
5264 
5265   BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5266   BasicType b_elem = b_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5267   BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5268   BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();


5288   return true;
5289 }
5290 
5291 bool LibraryCallKit::inline_montgomerySquare() {
5292   address stubAddr = StubRoutines::montgomerySquare();
5293   if (stubAddr == NULL) {
5294     return false; // Intrinsic's stub is not implemented on this platform
5295   }
5296 
5297   assert(UseMontgomerySquareIntrinsic, "not implemented on this platform");
5298   const char* stubName = "montgomery_square";
5299 
5300   assert(callee()->signature()->size() == 6, "montgomerySquare has 6 parameters");
5301 
5302   Node* a    = argument(0);
5303   Node* n    = argument(1);
5304   Node* len  = argument(2);
5305   Node* inv  = argument(3);
5306   Node* m    = argument(5);
5307 




5308   const Type* a_type = a->Value(&_gvn);
5309   const TypeAryPtr* top_a = a_type->isa_aryptr();
5310   const Type* n_type = a->Value(&_gvn);
5311   const TypeAryPtr* top_n = n_type->isa_aryptr();
5312   const Type* m_type = a->Value(&_gvn);
5313   const TypeAryPtr* top_m = m_type->isa_aryptr();
5314   if (top_a  == NULL || top_a->klass()  == NULL ||
5315       top_n == NULL || top_n->klass()  == NULL ||
5316       top_m == NULL || top_m->klass()  == NULL) {
5317     // failed array check
5318     return false;
5319   }
5320 
5321   BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5322   BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5323   BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5324   if (a_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5325     return false;
5326   }
5327 


5374   crc = _gvn.transform(new URShiftINode(crc, intcon(8)));
5375   result = _gvn.transform(new XorINode(crc, result));
5376   result = _gvn.transform(new XorINode(result, M1));
5377   set_result(result);
5378   return true;
5379 }
5380 
5381 /**
5382  * Calculate CRC32 for byte[] array.
5383  * int java.util.zip.CRC32.updateBytes(int crc, byte[] buf, int off, int len)
5384  */
5385 bool LibraryCallKit::inline_updateBytesCRC32() {
5386   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5387   assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5388   // no receiver since it is static method
5389   Node* crc     = argument(0); // type: int
5390   Node* src     = argument(1); // type: oop
5391   Node* offset  = argument(2); // type: int
5392   Node* length  = argument(3); // type: int
5393 


5394   const Type* src_type = src->Value(&_gvn);
5395   const TypeAryPtr* top_src = src_type->isa_aryptr();
5396   if (top_src  == NULL || top_src->klass()  == NULL) {
5397     // failed array check
5398     return false;
5399   }
5400 
5401   // Figure out the size and type of the elements we will be copying.
5402   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5403   if (src_elem != T_BYTE) {
5404     return false;
5405   }
5406 
5407   // 'src_start' points to src array + scaled offset
5408   Node* src_start = array_element_address(src, offset, src_elem);
5409 
5410   // We assume that range check is done by caller.
5411   // TODO: generate range check (offset+length < src.length) in debug VM.
5412 
5413   // Call the stub.


5476   Node* src     = argument(1); // type: oop
5477   Node* offset  = argument(2); // type: int
5478   Node* end     = argument(3); // type: int
5479 
5480   Node* length = _gvn.transform(new SubINode(end, offset));
5481 
5482   const Type* src_type = src->Value(&_gvn);
5483   const TypeAryPtr* top_src = src_type->isa_aryptr();
5484   if (top_src  == NULL || top_src->klass()  == NULL) {
5485     // failed array check
5486     return false;
5487   }
5488 
5489   // Figure out the size and type of the elements we will be copying.
5490   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5491   if (src_elem != T_BYTE) {
5492     return false;
5493   }
5494 
5495   // 'src_start' points to src array + scaled offset

5496   Node* src_start = array_element_address(src, offset, src_elem);
5497 
5498   // static final int[] byteTable in class CRC32C
5499   Node* table = get_table_from_crc32c_class(callee()->holder());

5500   Node* table_start = array_element_address(table, intcon(0), T_INT);
5501 
5502   // We assume that range check is done by caller.
5503   // TODO: generate range check (offset+length < src.length) in debug VM.
5504 
5505   // Call the stub.
5506   address stubAddr = StubRoutines::updateBytesCRC32C();
5507   const char *stubName = "updateBytesCRC32C";
5508 
5509   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5510                                  stubAddr, stubName, TypePtr::BOTTOM,
5511                                  crc, src_start, length, table_start);
5512   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5513   set_result(result);
5514   return true;
5515 }
5516 
5517 //------------------------------inline_updateDirectByteBufferCRC32C-----------------------
5518 //
5519 // Calculate CRC32C for DirectByteBuffer.


5523   assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5524   assert(callee()->signature()->size() == 5, "updateDirectByteBuffer has 4 parameters and one is long");
5525   assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5526   // no receiver since it is a static method
5527   Node* crc     = argument(0); // type: int
5528   Node* src     = argument(1); // type: long
5529   Node* offset  = argument(3); // type: int
5530   Node* end     = argument(4); // type: int
5531 
5532   Node* length = _gvn.transform(new SubINode(end, offset));
5533 
5534   src = ConvL2X(src);  // adjust Java long to machine word
5535   Node* base = _gvn.transform(new CastX2PNode(src));
5536   offset = ConvI2X(offset);
5537 
5538   // 'src_start' points to src array + scaled offset
5539   Node* src_start = basic_plus_adr(top(), base, offset);
5540 
5541   // static final int[] byteTable in class CRC32C
5542   Node* table = get_table_from_crc32c_class(callee()->holder());

5543   Node* table_start = array_element_address(table, intcon(0), T_INT);
5544 
5545   // Call the stub.
5546   address stubAddr = StubRoutines::updateBytesCRC32C();
5547   const char *stubName = "updateBytesCRC32C";
5548 
5549   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5550                                  stubAddr, stubName, TypePtr::BOTTOM,
5551                                  crc, src_start, length, table_start);
5552   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5553   set_result(result);
5554   return true;
5555 }
5556 
5557 //------------------------------inline_updateBytesAdler32----------------------
5558 //
5559 // Calculate Adler32 checksum for byte[] array.
5560 // int java.util.zip.Adler32.updateBytes(int crc, byte[] buf, int off, int len)
5561 //
5562 bool LibraryCallKit::inline_updateBytesAdler32() {


5566   // no receiver since it is static method
5567   Node* crc     = argument(0); // type: int
5568   Node* src     = argument(1); // type: oop
5569   Node* offset  = argument(2); // type: int
5570   Node* length  = argument(3); // type: int
5571 
5572   const Type* src_type = src->Value(&_gvn);
5573   const TypeAryPtr* top_src = src_type->isa_aryptr();
5574   if (top_src  == NULL || top_src->klass()  == NULL) {
5575     // failed array check
5576     return false;
5577   }
5578 
5579   // Figure out the size and type of the elements we will be copying.
5580   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5581   if (src_elem != T_BYTE) {
5582     return false;
5583   }
5584 
5585   // 'src_start' points to src array + scaled offset

5586   Node* src_start = array_element_address(src, offset, src_elem);
5587 
5588   // We assume that range check is done by caller.
5589   // TODO: generate range check (offset+length < src.length) in debug VM.
5590 
5591   // Call the stub.
5592   address stubAddr = StubRoutines::updateBytesAdler32();
5593   const char *stubName = "updateBytesAdler32";
5594 
5595   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5596                                  stubAddr, stubName, TypePtr::BOTTOM,
5597                                  crc, src_start, length);
5598   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5599   set_result(result);
5600   return true;
5601 }
5602 
5603 //------------------------------inline_updateByteBufferAdler32---------------
5604 //
5605 // Calculate Adler32 checksum for DirectByteBuffer.


5628 
5629   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5630                                  stubAddr, stubName, TypePtr::BOTTOM,
5631                                  crc, src_start, length);
5632 
5633   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5634   set_result(result);
5635   return true;
5636 }
5637 
5638 //----------------------------inline_reference_get----------------------------
5639 // public T java.lang.ref.Reference.get();
5640 bool LibraryCallKit::inline_reference_get() {
5641   const int referent_offset = java_lang_ref_Reference::referent_offset;
5642   guarantee(referent_offset > 0, "should have already been set");
5643 
5644   // Get the argument:
5645   Node* reference_obj = null_check_receiver();
5646   if (stopped()) return true;
5647 




5648   Node* adr = basic_plus_adr(reference_obj, reference_obj, referent_offset);
5649 
5650   ciInstanceKlass* klass = env()->Object_klass();
5651   const TypeOopPtr* object_type = TypeOopPtr::make_from_klass(klass);
5652 
5653   Node* no_ctrl = NULL;
5654   Node* result = make_load(no_ctrl, adr, object_type, T_OBJECT, MemNode::unordered);
5655 
5656   // Use the pre-barrier to record the value in the referent field
5657   pre_barrier(false /* do_load */,
5658               control(),
5659               NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
5660               result /* pre_val */,
5661               T_OBJECT);
5662 
5663   // Add memory barrier to prevent commoning reads from this field
5664   // across safepoint since GC can change its value.
5665   insert_mem_bar(Op_MemBarCPUOrder);
5666 
5667   set_result(result);


5676     const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5677     assert(tinst != NULL, "obj is null");
5678     assert(tinst->klass()->is_loaded(), "obj is not loaded");
5679     assert(!is_exact || tinst->klass_is_exact(), "klass not exact");
5680     fromKls = tinst->klass()->as_instance_klass();
5681   } else {
5682     assert(is_static, "only for static field access");
5683   }
5684   ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5685                                               ciSymbol::make(fieldTypeString),
5686                                               is_static);
5687 
5688   assert (field != NULL, "undefined field");
5689   if (field == NULL) return (Node *) NULL;
5690 
5691   if (is_static) {
5692     const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5693     fromObj = makecon(tip);
5694   }
5695 


5696   // Next code  copied from Parse::do_get_xxx():
5697 
5698   // Compute address and memory type.
5699   int offset  = field->offset_in_bytes();
5700   bool is_vol = field->is_volatile();
5701   ciType* field_klass = field->type();
5702   assert(field_klass->is_loaded(), "should be loaded");
5703   const TypePtr* adr_type = C->alias_type(field)->adr_type();
5704   Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5705   BasicType bt = field->layout_type();
5706 
5707   // Build the resultant type of the load
5708   const Type *type;
5709   if (bt == T_OBJECT) {
5710     type = TypeOopPtr::make_from_klass(field_klass->as_klass());
5711   } else {
5712     type = Type::get_const_basic_type(bt);
5713   }
5714 
5715   if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_vol) {


5736   assert(UseAES, "need AES instruction support");
5737 
5738   switch(id) {
5739   case vmIntrinsics::_aescrypt_encryptBlock:
5740     stubAddr = StubRoutines::aescrypt_encryptBlock();
5741     stubName = "aescrypt_encryptBlock";
5742     break;
5743   case vmIntrinsics::_aescrypt_decryptBlock:
5744     stubAddr = StubRoutines::aescrypt_decryptBlock();
5745     stubName = "aescrypt_decryptBlock";
5746     break;
5747   }
5748   if (stubAddr == NULL) return false;
5749 
5750   Node* aescrypt_object = argument(0);
5751   Node* src             = argument(1);
5752   Node* src_offset      = argument(2);
5753   Node* dest            = argument(3);
5754   Node* dest_offset     = argument(4);
5755 




5756   // (1) src and dest are arrays.
5757   const Type* src_type = src->Value(&_gvn);
5758   const Type* dest_type = dest->Value(&_gvn);
5759   const TypeAryPtr* top_src = src_type->isa_aryptr();
5760   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
5761   assert (top_src  != NULL && top_src->klass()  != NULL &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
5762 
5763   // for the quick and dirty code we will skip all the checks.
5764   // we are just trying to get the call to be generated.
5765   Node* src_start  = src;
5766   Node* dest_start = dest;
5767   if (src_offset != NULL || dest_offset != NULL) {
5768     assert(src_offset != NULL && dest_offset != NULL, "");
5769     src_start  = array_element_address(src,  src_offset,  T_BYTE);
5770     dest_start = array_element_address(dest, dest_offset, T_BYTE);
5771   }
5772 
5773   // now need to get the start of its expanded key array
5774   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
5775   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);


5804 
5805   switch(id) {
5806   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
5807     stubAddr = StubRoutines::cipherBlockChaining_encryptAESCrypt();
5808     stubName = "cipherBlockChaining_encryptAESCrypt";
5809     break;
5810   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
5811     stubAddr = StubRoutines::cipherBlockChaining_decryptAESCrypt();
5812     stubName = "cipherBlockChaining_decryptAESCrypt";
5813     break;
5814   }
5815   if (stubAddr == NULL) return false;
5816 
5817   Node* cipherBlockChaining_object = argument(0);
5818   Node* src                        = argument(1);
5819   Node* src_offset                 = argument(2);
5820   Node* len                        = argument(3);
5821   Node* dest                       = argument(4);
5822   Node* dest_offset                = argument(5);
5823 




5824   // (1) src and dest are arrays.
5825   const Type* src_type = src->Value(&_gvn);
5826   const Type* dest_type = dest->Value(&_gvn);
5827   const TypeAryPtr* top_src = src_type->isa_aryptr();
5828   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
5829   assert (top_src  != NULL && top_src->klass()  != NULL
5830           &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
5831 
5832   // checks are the responsibility of the caller
5833   Node* src_start  = src;
5834   Node* dest_start = dest;
5835   if (src_offset != NULL || dest_offset != NULL) {
5836     assert(src_offset != NULL && dest_offset != NULL, "");
5837     src_start  = array_element_address(src,  src_offset,  T_BYTE);
5838     dest_start = array_element_address(dest, dest_offset, T_BYTE);
5839   }
5840 
5841   // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
5842   // (because of the predicated logic executed earlier).
5843   // so we cast it here safely.


5848 
5849   // cast it to what we know it will be at runtime
5850   const TypeInstPtr* tinst = _gvn.type(cipherBlockChaining_object)->isa_instptr();
5851   assert(tinst != NULL, "CBC obj is null");
5852   assert(tinst->klass()->is_loaded(), "CBC obj is not loaded");
5853   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
5854   assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
5855 
5856   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
5857   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
5858   const TypeOopPtr* xtype = aklass->as_instance_type();
5859   Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
5860   aescrypt_object = _gvn.transform(aescrypt_object);
5861 
5862   // we need to get the start of the aescrypt_object's expanded key array
5863   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
5864   if (k_start == NULL) return false;
5865 
5866   // similarly, get the start address of the r vector
5867   Node* objRvec = load_field_from_object(cipherBlockChaining_object, "r", "[B", /*is_exact*/ false);



5868   if (objRvec == NULL) return false;
5869   Node* r_start = array_element_address(objRvec, intcon(0), T_BYTE);
5870 
5871   Node* cbcCrypt;
5872   if (Matcher::pass_original_key_for_aes()) {
5873     // on SPARC we need to pass the original key since key expansion needs to happen in intrinsics due to
5874     // compatibility issues between Java key expansion and SPARC crypto instructions
5875     Node* original_k_start = get_original_key_start_from_aescrypt_object(aescrypt_object);
5876     if (original_k_start == NULL) return false;
5877 
5878     // Call the stub, passing src_start, dest_start, k_start, r_start, src_len and original_k_start
5879     cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
5880                                  OptoRuntime::cipherBlockChaining_aescrypt_Type(),
5881                                  stubAddr, stubName, TypePtr::BOTTOM,
5882                                  src_start, dest_start, k_start, r_start, len, original_k_start);
5883   } else {
5884     // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
5885     cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
5886                                  OptoRuntime::cipherBlockChaining_aescrypt_Type(),
5887                                  stubAddr, stubName, TypePtr::BOTTOM,
5888                                  src_start, dest_start, k_start, r_start, len);
5889   }
5890 
5891   // return cipher length (int)
5892   Node* retvalue = _gvn.transform(new ProjNode(cbcCrypt, TypeFunc::Parms));
5893   set_result(retvalue);
5894   return true;
5895 }
5896 
5897 //------------------------------get_key_start_from_aescrypt_object-----------------------
5898 Node * LibraryCallKit::get_key_start_from_aescrypt_object(Node *aescrypt_object) {
5899   Node* objAESCryptKey = load_field_from_object(aescrypt_object, "K", "[I", /*is_exact*/ false);
5900   assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
5901   if (objAESCryptKey == NULL) return (Node *) NULL;


5902 
5903   // now have the array, need to get the start address of the K array
5904   Node* k_start = array_element_address(objAESCryptKey, intcon(0), T_INT);
5905   return k_start;
5906 }
5907 
5908 //------------------------------get_original_key_start_from_aescrypt_object-----------------------
5909 Node * LibraryCallKit::get_original_key_start_from_aescrypt_object(Node *aescrypt_object) {
5910   Node* objAESCryptKey = load_field_from_object(aescrypt_object, "lastKey", "[B", /*is_exact*/ false);
5911   assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
5912   if (objAESCryptKey == NULL) return (Node *) NULL;
5913 
5914   // now have the array, need to get the start address of the lastKey array
5915   Node* original_k_start = array_element_address(objAESCryptKey, intcon(0), T_BYTE);
5916   return original_k_start;
5917 }
5918 
5919 //----------------------------inline_cipherBlockChaining_AESCrypt_predicate----------------------------
5920 // Return node representing slow path of predicate check.
5921 // the pseudo code we want to emulate with this predicate is:




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "compiler/compileLog.hpp"
  31 #include "gc/shenandoah/shenandoahRuntime.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "opto/addnode.hpp"
  34 #include "opto/arraycopynode.hpp"
  35 #include "opto/c2compiler.hpp"
  36 #include "opto/callGenerator.hpp"
  37 #include "opto/castnode.hpp"
  38 #include "opto/cfgnode.hpp"
  39 #include "opto/convertnode.hpp"
  40 #include "opto/countbitsnode.hpp"
  41 #include "opto/intrinsicnode.hpp"
  42 #include "opto/idealKit.hpp"
  43 #include "opto/mathexactnode.hpp"
  44 #include "opto/movenode.hpp"
  45 #include "opto/mulnode.hpp"
  46 #include "opto/narrowptrnode.hpp"
  47 #include "opto/opaquenode.hpp"
  48 #include "opto/parse.hpp"
  49 #include "opto/runtime.hpp"
  50 #include "opto/shenandoahSupport.hpp"
  51 #include "opto/subnode.hpp"
  52 #include "prims/nativeLookup.hpp"
  53 #include "runtime/sharedRuntime.hpp"
  54 #include "trace/traceMacros.hpp"
  55 
  56 class LibraryIntrinsic : public InlineCallGenerator {
  57   // Extend the set of intrinsics known to the runtime:
  58  public:
  59  private:
  60   bool             _is_virtual;
  61   bool             _does_virtual_dispatch;
  62   int8_t           _predicates_count;  // Intrinsic is predicated by several conditions
  63   int8_t           _last_predicate; // Last generated predicate
  64   vmIntrinsics::ID _intrinsic_id;
  65 
  66  public:
  67   LibraryIntrinsic(ciMethod* m, bool is_virtual, int predicates_count, bool does_virtual_dispatch, vmIntrinsics::ID id)
  68     : InlineCallGenerator(m),
  69       _is_virtual(is_virtual),
  70       _does_virtual_dispatch(does_virtual_dispatch),


 957   C->set_has_split_ifs(true); // Has chance for split-if optimization
 958 
 959   return _gvn.transform(result);
 960 }
 961 
 962 //------------------------------inline_string_compareTo------------------------
 963 // public int java.lang.String.compareTo(String anotherString);
 964 bool LibraryCallKit::inline_string_compareTo() {
 965   Node* receiver = null_check(argument(0));
 966   Node* arg      = null_check(argument(1));
 967   if (stopped()) {
 968     return true;
 969   }
 970   set_result(make_string_method_node(Op_StrComp, receiver, arg));
 971   return true;
 972 }
 973 
 974 //------------------------------inline_string_equals------------------------
 975 bool LibraryCallKit::inline_string_equals() {
 976   Node* receiver = null_check_receiver();
 977 
 978   if (ShenandoahVerifyReadsToFromSpace) {
 979     receiver = shenandoah_read_barrier(receiver);
 980   }
 981 
 982   // NOTE: Do not null check argument for String.equals() because spec
 983   // allows to specify NULL as argument.
 984   Node* argument = this->argument(1);
 985 
 986   if (ShenandoahVerifyReadsToFromSpace) {
 987     argument = shenandoah_read_barrier(argument);
 988   }
 989 
 990   if (stopped()) {
 991     return true;
 992   }
 993 
 994   // paths (plus control) merge
 995   RegionNode* region = new RegionNode(5);
 996   Node* phi = new PhiNode(region, TypeInt::BOOL);
 997 
 998   // does source == target string?
 999   Node* cmp = _gvn.transform(new CmpPNode(receiver, argument));
1000   Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
1001 
1002   Node* if_eq = generate_slow_guard(bol, NULL);
1003   if (if_eq != NULL) {
1004     // receiver == argument
1005     phi->init_req(2, intcon(1));
1006     region->init_req(2, if_eq);
1007   }
1008 
1009   // get String klass for instanceOf


1018     //instanceOf == true, fallthrough
1019 
1020     if (inst_false != NULL) {
1021       phi->init_req(3, intcon(0));
1022       region->init_req(3, inst_false);
1023     }
1024   }
1025 
1026   if (!stopped()) {
1027     const TypeOopPtr* string_type = TypeOopPtr::make_from_klass(klass);
1028 
1029     // Properly cast the argument to String
1030     argument = _gvn.transform(new CheckCastPPNode(control(), argument, string_type));
1031     // This path is taken only when argument's type is String:NotNull.
1032     argument = cast_not_null(argument, false);
1033 
1034     Node* no_ctrl = NULL;
1035 
1036     // Get start addr of receiver
1037     Node* receiver_val    = load_String_value(no_ctrl, receiver);
1038 
1039     if (ShenandoahVerifyReadsToFromSpace) {
1040       receiver_val = shenandoah_read_barrier(receiver_val);
1041     }
1042 
1043     Node* receiver_offset = load_String_offset(no_ctrl, receiver);
1044     Node* receiver_start = array_element_address(receiver_val, receiver_offset, T_CHAR);
1045 
1046     // Get length of receiver
1047     Node* receiver_cnt  = load_String_length(no_ctrl, receiver);
1048 
1049     // Get start addr of argument
1050     Node* argument_val    = load_String_value(no_ctrl, argument);
1051 
1052     if (ShenandoahVerifyReadsToFromSpace) {
1053       argument_val = shenandoah_read_barrier(argument_val);
1054     }
1055 
1056     Node* argument_offset = load_String_offset(no_ctrl, argument);
1057     Node* argument_start = array_element_address(argument_val, argument_offset, T_CHAR);
1058 
1059     // Get length of argument
1060     Node* argument_cnt  = load_String_length(no_ctrl, argument);
1061 
1062     // Check for receiver count != argument count
1063     Node* cmp = _gvn.transform(new CmpINode(receiver_cnt, argument_cnt));
1064     Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
1065     Node* if_ne = generate_slow_guard(bol, NULL);
1066     if (if_ne != NULL) {
1067       phi->init_req(4, intcon(0));
1068       region->init_req(4, if_ne);
1069     }
1070 
1071     // Check for count == 0 is done by assembler code for StrEquals.
1072 
1073     if (!stopped()) {
1074       Node* equals = make_string_method_node(Op_StrEquals, receiver_start, receiver_cnt, argument_start, argument_cnt);
1075       phi->init_req(1, equals);
1076       region->init_req(1, control());
1077     }
1078   }
1079 
1080   // post merge
1081   set_control(_gvn.transform(region));
1082   record_for_igvn(region);
1083 
1084   set_result(_gvn.transform(phi));
1085   return true;
1086 }
1087 
1088 //------------------------------inline_array_equals----------------------------
1089 bool LibraryCallKit::inline_array_equals() {
1090   Node* arg1 = argument(0);
1091   Node* arg2 = argument(1);
1092 
1093   arg1 = shenandoah_read_barrier(arg1);
1094   arg2 = shenandoah_read_barrier(arg2);
1095 
1096   set_result(_gvn.transform(new AryEqNode(control(), memory(TypeAryPtr::CHARS), arg1, arg2)));
1097   return true;
1098 }
1099 
1100 // Java version of String.indexOf(constant string)
1101 // class StringDecl {
1102 //   StringDecl(char[] ca) {
1103 //     offset = 0;
1104 //     count = ca.length;
1105 //     value = ca;
1106 //   }
1107 //   int offset;
1108 //   int count;
1109 //   char[] value;
1110 // }
1111 //
1112 // static int string_indexOf_J(StringDecl string_object, char[] target_object,
1113 //                             int targetOffset, int cache_i, int md2) {
1114 //   int cache = cache_i;
1115 //   int sourceOffset = string_object.offset;


2161   case vmIntrinsics::_reverseBytes_l:           n = new ReverseBytesLNode( 0,   arg);  break;
2162   default:  fatal_unexpected_iid(id);  break;
2163   }
2164   set_result(_gvn.transform(n));
2165   return true;
2166 }
2167 
2168 //----------------------------inline_unsafe_access----------------------------
2169 
2170 const static BasicType T_ADDRESS_HOLDER = T_LONG;
2171 
2172 // Helper that guards and inserts a pre-barrier.
2173 void LibraryCallKit::insert_pre_barrier(Node* base_oop, Node* offset,
2174                                         Node* pre_val, bool need_mem_bar) {
2175   // We could be accessing the referent field of a reference object. If so, when G1
2176   // is enabled, we need to log the value in the referent field in an SATB buffer.
2177   // This routine performs some compile time filters and generates suitable
2178   // runtime filters that guard the pre-barrier code.
2179   // Also add memory barrier for non volatile load from the referent field
2180   // to prevent commoning of loads across safepoint.
2181   if (!(UseG1GC || UseShenandoahGC) && !need_mem_bar)
2182     return;
2183 
2184   // Some compile time checks.
2185 
2186   // If offset is a constant, is it java_lang_ref_Reference::_reference_offset?
2187   const TypeX* otype = offset->find_intptr_t_type();
2188   if (otype != NULL && otype->is_con() &&
2189       otype->get_con() != java_lang_ref_Reference::referent_offset) {
2190     // Constant offset but not the reference_offset so just return
2191     return;
2192   }
2193 
2194   // We only need to generate the runtime guards for instances.
2195   const TypeOopPtr* btype = base_oop->bottom_type()->isa_oopptr();
2196   if (btype != NULL) {
2197     if (btype->isa_aryptr()) {
2198       // Array type so nothing to do
2199       return;
2200     }
2201 


2350         vtype = T_ADDRESS;  // it is really a C void*
2351       assert(vtype == type, "putter must accept the expected value");
2352     }
2353 #endif // ASSERT
2354  }
2355 #endif //PRODUCT
2356 
2357   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2358 
2359   Node* receiver = argument(0);  // type: oop
2360 
2361   // Build address expression.
2362   Node* adr;
2363   Node* heap_base_oop = top();
2364   Node* offset = top();
2365   Node* val;
2366 
2367   if (!is_native_ptr) {
2368     // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2369     Node* base = argument(1);  // type: oop
2370     if (UseShenandoahGC) {
2371       // Note: if we don't null-check here, we generate a read barrier with a built-in
2372       // null-check. This will later be attempted to be split on the phi, which
2373       // results in a load on a NULL-based address on the null-path, which blows up.
2374       // It will go away when we do late-insertion of read barriers.
2375       base = null_check(base);
2376     }
2377     if (is_store) {
2378       base = shenandoah_write_barrier(base);
2379     } else {
2380       base = shenandoah_read_barrier(base);
2381     }
2382     // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2383     offset = argument(2);  // type: long
2384     // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2385     // to be plain byte offsets, which are also the same as those accepted
2386     // by oopDesc::field_base.
2387     assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2388            "fieldOffset must be byte-scaled");
2389     // 32-bit machines ignore the high half!
2390     offset = ConvL2X(offset);
2391     adr = make_unsafe_address(base, offset);
2392     heap_base_oop = base;
2393     val = is_store ? argument(4) : NULL;
2394   } else {
2395     Node* ptr = argument(1);  // type: long
2396     ptr = ConvL2X(ptr);  // adjust Java long to machine word
2397     adr = make_unsafe_address(NULL, ptr);
2398     val = is_store ? argument(3) : NULL;
2399   }
2400 
2401   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();


2513     // the end of this method.  So, pushing the load onto the stack at a later
2514     // point is fine.
2515     set_result(p);
2516   } else {
2517     // place effect of store into memory
2518     switch (type) {
2519     case T_DOUBLE:
2520       val = dstore_rounding(val);
2521       break;
2522     case T_ADDRESS:
2523       // Repackage the long as a pointer.
2524       val = ConvL2X(val);
2525       val = _gvn.transform(new CastX2PNode(val));
2526       break;
2527     }
2528 
2529     MemNode::MemOrd mo = is_volatile ? MemNode::release : MemNode::unordered;
2530     if (type != T_OBJECT ) {
2531       (void) store_to_memory(control(), adr, val, type, adr_type, mo, is_volatile);
2532     } else {
2533       val = shenandoah_read_barrier_nomem(val);
2534       // Possibly an oop being stored to Java heap or native memory
2535       if (!TypePtr::NULL_PTR->higher_equal(_gvn.type(heap_base_oop))) {
2536         // oop to Java heap.
2537         (void) store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type, mo);
2538       } else {
2539         // We can't tell at compile time if we are storing in the Java heap or outside
2540         // of it. So we need to emit code to conditionally do the proper type of
2541         // store.
2542 
2543         IdealKit ideal(this);
2544 #define __ ideal.
2545         // QQQ who knows what probability is here??
2546         __ if_then(heap_base_oop, BoolTest::ne, null(), PROB_UNLIKELY(0.999)); {
2547           // Sync IdealKit and graphKit.
2548           sync_kit(ideal);
2549           Node* st = store_oop_to_unknown(control(), heap_base_oop, adr, adr_type, val, type, mo);
2550           // Update IdealKit memory.
2551           __ sync_kit(this);
2552         } __ else_(); {
2553           __ store(__ ctrl(), adr, val, type, alias_type->index(), mo, is_volatile);


2643     const bool two_slot_type = type2size[type] == 2;
2644     receiver = argument(0);  // type: oop
2645     base     = argument(1);  // type: oop
2646     offset   = argument(2);  // type: long
2647     oldval   = argument(4);  // type: oop, int, or long
2648     newval   = argument(two_slot_type ? 6 : 5);  // type: oop, int, or long
2649   } else if (kind == LS_xadd || kind == LS_xchg){
2650     receiver = argument(0);  // type: oop
2651     base     = argument(1);  // type: oop
2652     offset   = argument(2);  // type: long
2653     oldval   = NULL;
2654     newval   = argument(4);  // type: oop, int, or long
2655   }
2656 
2657   // Null check receiver.
2658   receiver = null_check(receiver);
2659   if (stopped()) {
2660     return true;
2661   }
2662 
2663   base = shenandoah_write_barrier(base);
2664 
2665   // Build field offset expression.
2666   // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2667   // to be plain byte offsets, which are also the same as those accepted
2668   // by oopDesc::field_base.
2669   assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2670   // 32-bit machines ignore the high half of long offsets
2671   offset = ConvL2X(offset);
2672   Node* adr = make_unsafe_address(base, offset);
2673   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2674 
2675   // For CAS, unlike inline_unsafe_access, there seems no point in
2676   // trying to refine types. Just use the coarse types here.
2677   const Type *value_type = Type::get_const_basic_type(type);
2678   Compile::AliasType* alias_type = C->alias_type(adr_type);
2679   assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2680 
2681   if (kind == LS_xchg && type == T_OBJECT) {
2682     const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2683     if (tjp != NULL) {
2684       value_type = tjp;


2686   }
2687 
2688   int alias_idx = C->get_alias_index(adr_type);
2689 
2690   // Memory-model-wise, a LoadStore acts like a little synchronized
2691   // block, so needs barriers on each side.  These don't translate
2692   // into actual barriers on most machines, but we still need rest of
2693   // compiler to respect ordering.
2694 
2695   insert_mem_bar(Op_MemBarRelease);
2696   insert_mem_bar(Op_MemBarCPUOrder);
2697 
2698   // 4984716: MemBars must be inserted before this
2699   //          memory node in order to avoid a false
2700   //          dependency which will confuse the scheduler.
2701   Node *mem = memory(alias_idx);
2702 
2703   // For now, we handle only those cases that actually exist: ints,
2704   // longs, and Object. Adding others should be straightforward.
2705   Node* load_store;
2706   Node* result;
2707   switch(type) {
2708   case T_INT:
2709     if (kind == LS_xadd) {
2710       load_store = _gvn.transform(new GetAndAddINode(control(), mem, adr, newval, adr_type));
2711     } else if (kind == LS_xchg) {
2712       load_store = _gvn.transform(new GetAndSetINode(control(), mem, adr, newval, adr_type));
2713     } else if (kind == LS_cmpxchg) {
2714       load_store = _gvn.transform(new CompareAndSwapINode(control(), mem, adr, newval, oldval));
2715     } else {
2716       ShouldNotReachHere();
2717     }
2718     result = load_store;
2719     break;
2720   case T_LONG:
2721     if (kind == LS_xadd) {
2722       load_store = _gvn.transform(new GetAndAddLNode(control(), mem, adr, newval, adr_type));
2723     } else if (kind == LS_xchg) {
2724       load_store = _gvn.transform(new GetAndSetLNode(control(), mem, adr, newval, adr_type));
2725     } else if (kind == LS_cmpxchg) {
2726       load_store = _gvn.transform(new CompareAndSwapLNode(control(), mem, adr, newval, oldval));
2727     } else {
2728       ShouldNotReachHere();
2729     }
2730     result = load_store;
2731     break;
2732   case T_OBJECT:
2733     // Transformation of a value which could be NULL pointer (CastPP #NULL)
2734     // could be delayed during Parse (for example, in adjust_map_after_if()).
2735     // Execute transformation here to avoid barrier generation in such case.
2736     if (_gvn.type(newval) == TypePtr::NULL_PTR)
2737       newval = _gvn.makecon(TypePtr::NULL_PTR);
2738 
2739     newval = shenandoah_read_barrier_nomem(newval);
2740 
2741     // Reference stores need a store barrier.
2742     if (kind == LS_xchg) {
2743       // If pre-barrier must execute before the oop store, old value will require do_load here.
2744       if (!can_move_pre_barrier()) {
2745         pre_barrier(true /* do_load*/,
2746                     control(), base, adr, alias_idx, newval, value_type->make_oopptr(),
2747                     NULL /* pre_val*/,
2748                     T_OBJECT);
2749       } // Else move pre_barrier to use load_store value, see below.
2750     } else if (kind == LS_cmpxchg) {
2751       // Same as for newval above:
2752       if (_gvn.type(oldval) == TypePtr::NULL_PTR) {
2753         oldval = _gvn.makecon(TypePtr::NULL_PTR);
2754       }
2755       // The only known value which might get overwritten is oldval.
2756       pre_barrier(false /* do_load */,
2757                   control(), NULL, NULL, max_juint, NULL, NULL,
2758                   oldval /* pre_val */,
2759                   T_OBJECT);
2760     } else {
2761       ShouldNotReachHere();
2762     }
2763 
2764 #ifdef _LP64
2765     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2766       Node *newval_enc = _gvn.transform(new EncodePNode(newval, newval->bottom_type()->make_narrowoop()));
2767       if (kind == LS_xchg) {
2768         load_store = _gvn.transform(new GetAndSetNNode(control(), mem, adr,
2769                                                        newval_enc, adr_type, value_type->make_narrowoop()));
2770       } else {
2771         assert(kind == LS_cmpxchg, "wrong LoadStore operation");
2772         Node *oldval_enc = _gvn.transform(new EncodePNode(oldval, oldval->bottom_type()->make_narrowoop()));
2773         load_store = _gvn.transform(new CompareAndSwapNNode(control(), mem, adr,
2774                                                                 newval_enc, oldval_enc));
2775       }
2776       result = load_store;
2777     } else
2778 #endif
2779     {
2780       if (kind == LS_xchg) {
2781         load_store = _gvn.transform(new GetAndSetPNode(control(), mem, adr, newval, adr_type, value_type->is_oopptr()));
2782         result = load_store;
2783       } else {
2784         assert(kind == LS_cmpxchg, "wrong LoadStore operation");
2785         load_store = _gvn.transform(new CompareAndSwapPNode(control(), mem, adr, newval, oldval));
2786         result = load_store;
2787 
2788         if (UseShenandoahGC) {
2789           // if (! success)
2790           Node* cmp_true = _gvn.transform(new CmpINode(load_store, intcon(1)));
2791           Node* tst_true = _gvn.transform(new BoolNode(cmp_true, BoolTest::eq));
2792           IfNode* iff = create_and_map_if(control(), tst_true, PROB_LIKELY_MAG(2), COUNT_UNKNOWN);
2793           Node* iftrue = _gvn.transform(new IfTrueNode(iff));
2794           Node* iffalse = _gvn.transform(new IfFalseNode(iff));
2795 
2796           enum { _success_path = 1, _fail_path, _shenandoah_path, PATH_LIMIT };
2797           RegionNode* region = new RegionNode(PATH_LIMIT);
2798           Node*       phi    = new PhiNode(region, TypeInt::BOOL);
2799           // success -> return result of CAS1.
2800           region->init_req(_success_path, iftrue);
2801           phi   ->init_req(_success_path, load_store);
2802 
2803           // failure
2804           set_control(iffalse);
2805 
2806           // if (read_barrier(expected) == read_barrier(old)
2807           oldval = shenandoah_read_barrier(oldval);
2808 
2809           // Load old value from memory. We shuold really use what we get back from the CAS,
2810           // if we can.
2811           Node* current = make_load(control(), adr, TypeInstPtr::BOTTOM, type, MemNode::unordered);
2812           // read_barrier(old)
2813           Node* new_current = shenandoah_read_barrier(current);
2814 
2815           Node* chk = _gvn.transform(new CmpPNode(new_current, oldval));
2816           Node* test = _gvn.transform(new BoolNode(chk, BoolTest::eq));
2817 
2818           IfNode* iff2 = create_and_map_if(control(), test, PROB_UNLIKELY_MAG(2), COUNT_UNKNOWN);
2819           Node* iftrue2 = _gvn.transform(new IfTrueNode(iff2));
2820           Node* iffalse2 = _gvn.transform(new IfFalseNode(iff2));
2821 
2822           // If they are not equal, it's a legitimate failure and we return the result of CAS1.
2823           region->init_req(_fail_path, iffalse2);
2824           phi   ->init_req(_fail_path, load_store);
2825 
2826           // Otherwise we retry with old.
2827           set_control(iftrue2);
2828 
2829           Node *call = make_runtime_call(RC_LEAF | RC_NO_IO,
2830                                          OptoRuntime::shenandoah_cas_obj_Type(),
2831                                          CAST_FROM_FN_PTR(address, ShenandoahRuntime::compare_and_swap_object),
2832                                          "shenandoah_cas_obj",
2833                                          NULL,
2834                                          adr, newval, current);
2835 
2836           Node* retval = _gvn.transform(new ProjNode(call, TypeFunc::Parms + 0));
2837 
2838           region->init_req(_shenandoah_path, control());
2839           phi   ->init_req(_shenandoah_path, retval);
2840 
2841           set_control(_gvn.transform(region));
2842           record_for_igvn(region);
2843           phi = _gvn.transform(phi);
2844           result = phi;
2845         }
2846 
2847       }
2848     }
2849     if (kind == LS_cmpxchg) {
2850       // Emit the post barrier only when the actual store happened.
2851       // This makes sense to check only for compareAndSet that can fail to set the value.
2852       // CAS success path is marked more likely since we anticipate this is a performance
2853       // critical path, while CAS failure path can use the penalty for going through unlikely
2854       // path as backoff. Which is still better than doing a store barrier there.
2855       IdealKit ideal(this);
2856       ideal.if_then(result, BoolTest::ne, ideal.ConI(0), PROB_STATIC_FREQUENT); {
2857         sync_kit(ideal);
2858         post_barrier(ideal.ctrl(), result, base, adr, alias_idx, newval, T_OBJECT, true);
2859         ideal.sync_kit(this);
2860       } ideal.end_if();
2861       final_sync(ideal);
2862     } else {
2863       post_barrier(control(), result, base, adr, alias_idx, newval, T_OBJECT, true);
2864     }
2865     break;
2866   default:
2867     fatal(err_msg_res("unexpected type %d: %s", type, type2name(type)));
2868     break;
2869   }
2870 
2871   // SCMemProjNodes represent the memory state of a LoadStore. Their
2872   // main role is to prevent LoadStore nodes from being optimized away
2873   // when their results aren't used.
2874   Node* proj = _gvn.transform(new SCMemProjNode(load_store));
2875   set_memory(proj, alias_idx);
2876 
2877   if (type == T_OBJECT && kind == LS_xchg) {
2878 #ifdef _LP64
2879     if (adr->bottom_type()->is_ptr_to_narrowoop()) {
2880       result = _gvn.transform(new DecodeNNode(result, result->get_ptr_type()));
2881     }
2882 #endif
2883     if (can_move_pre_barrier()) {
2884       // Don't need to load pre_val. The old value is returned by load_store.
2885       // The pre_barrier can execute after the xchg as long as no safepoint
2886       // gets inserted between them.
2887       pre_barrier(false /* do_load */,
2888                   control(), NULL, NULL, max_juint, NULL, NULL,
2889                   result /* pre_val */,
2890                   T_OBJECT);
2891     }
2892   }
2893 
2894   // Add the trailing membar surrounding the access
2895   insert_mem_bar(Op_MemBarCPUOrder);
2896   insert_mem_bar(Op_MemBarAcquire);
2897 
2898   assert(type2size[result->bottom_type()->basic_type()] == type2size[rtype], "result type should match");
2899   set_result(result);
2900   return true;
2901 }
2902 
2903 //----------------------------inline_unsafe_ordered_store----------------------
2904 // public native void sun.misc.Unsafe.putOrderedObject(Object o, long offset, Object x);
2905 // public native void sun.misc.Unsafe.putOrderedInt(Object o, long offset, int x);
2906 // public native void sun.misc.Unsafe.putOrderedLong(Object o, long offset, long x);
2907 bool LibraryCallKit::inline_unsafe_ordered_store(BasicType type) {
2908   // This is another variant of inline_unsafe_access, differing in
2909   // that it always issues store-store ("release") barrier and ensures
2910   // store-atomicity (which only matters for "long").
2911 
2912   if (callee()->is_static())  return false;  // caller must have the capability!
2913 
2914 #ifndef PRODUCT
2915   {
2916     ResourceMark rm;
2917     // Check the signatures.
2918     ciSignature* sig = callee()->signature();
2919 #ifdef ASSERT


2923     assert(sig->type_at(0)->basic_type() == T_OBJECT, "base is object");
2924     assert(sig->type_at(1)->basic_type() == T_LONG, "offset is long");
2925 #endif // ASSERT
2926   }
2927 #endif //PRODUCT
2928 
2929   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2930 
2931   // Get arguments:
2932   Node* receiver = argument(0);  // type: oop
2933   Node* base     = argument(1);  // type: oop
2934   Node* offset   = argument(2);  // type: long
2935   Node* val      = argument(4);  // type: oop, int, or long
2936 
2937   // Null check receiver.
2938   receiver = null_check(receiver);
2939   if (stopped()) {
2940     return true;
2941   }
2942 
2943   base = shenandoah_write_barrier(base);
2944 
2945   // Build field offset expression.
2946   assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2947   // 32-bit machines ignore the high half of long offsets
2948   offset = ConvL2X(offset);
2949   Node* adr = make_unsafe_address(base, offset);
2950   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2951   const Type *value_type = Type::get_const_basic_type(type);
2952   Compile::AliasType* alias_type = C->alias_type(adr_type);
2953 
2954   insert_mem_bar(Op_MemBarRelease);
2955   insert_mem_bar(Op_MemBarCPUOrder);
2956   // Ensure that the store is atomic for longs:
2957   const bool require_atomic_access = true;
2958   Node* store;
2959   if (type == T_OBJECT) { // reference stores need a store barrier.
2960     val = shenandoah_read_barrier_nomem(val);
2961     store = store_oop_to_unknown(control(), base, adr, adr_type, val, type, MemNode::release);
2962   }
2963   else {
2964     store = store_to_memory(control(), adr, val, type, adr_type, MemNode::release, require_atomic_access);
2965   }
2966   insert_mem_bar(Op_MemBarCPUOrder);
2967   return true;
2968 }
2969 
2970 bool LibraryCallKit::inline_unsafe_fence(vmIntrinsics::ID id) {
2971   // Regardless of form, don't allow previous ld/st to move down,
2972   // then issue acquire, release, or volatile mem_bar.
2973   insert_mem_bar(Op_MemBarCPUOrder);
2974   switch(id) {
2975     case vmIntrinsics::_loadFence:
2976       insert_mem_bar(Op_LoadFence);
2977       return true;
2978     case vmIntrinsics::_storeFence:
2979       insert_mem_bar(Op_StoreFence);
2980       return true;
2981     case vmIntrinsics::_fullFence:
2982       insert_mem_bar(Op_MemBarVolatile);


3264   Node* bits = intcon(modifier_bits);
3265   Node* mbit = _gvn.transform(new AndINode(mods, mask));
3266   Node* cmp  = _gvn.transform(new CmpINode(mbit, bits));
3267   Node* bol  = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3268   return generate_fair_guard(bol, region);
3269 }
3270 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3271   return generate_access_flags_guard(kls, JVM_ACC_INTERFACE, 0, region);
3272 }
3273 
3274 //-------------------------inline_native_Class_query-------------------
3275 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
3276   const Type* return_type = TypeInt::BOOL;
3277   Node* prim_return_value = top();  // what happens if it's a primitive class?
3278   bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3279   bool expect_prim = false;     // most of these guys expect to work on refs
3280 
3281   enum { _normal_path = 1, _prim_path = 2, PATH_LIMIT };
3282 
3283   Node* mirror = argument(0);
3284 
3285   if (ShenandoahVerifyReadsToFromSpace) {
3286     mirror = shenandoah_read_barrier(mirror);
3287   }
3288 
3289   Node* obj    = top();
3290 
3291   switch (id) {
3292   case vmIntrinsics::_isInstance:
3293     // nothing is an instance of a primitive type
3294     prim_return_value = intcon(0);
3295     obj = argument(1);
3296     if (ShenandoahVerifyReadsToFromSpace) {
3297       obj = shenandoah_read_barrier(obj);
3298     }
3299     break;
3300   case vmIntrinsics::_getModifiers:
3301     prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3302     assert(is_power_of_2((int)JVM_ACC_WRITTEN_FLAGS+1), "change next line");
3303     return_type = TypeInt::make(0, JVM_ACC_WRITTEN_FLAGS, Type::WidenMin);
3304     break;
3305   case vmIntrinsics::_isInterface:
3306     prim_return_value = intcon(0);
3307     break;
3308   case vmIntrinsics::_isArray:
3309     prim_return_value = intcon(0);
3310     expect_prim = true;  // cf. ObjectStreamClass.getClassSignature
3311     break;
3312   case vmIntrinsics::_isPrimitive:
3313     prim_return_value = intcon(1);
3314     expect_prim = true;  // obviously
3315     break;
3316   case vmIntrinsics::_getSuperclass:
3317     prim_return_value = null();
3318     return_type = TypeInstPtr::MIRROR->cast_to_ptr_type(TypePtr::BotPTR);


3526     PreserveJVMState pjvms(this);
3527     set_control(_gvn.transform(region));
3528     uncommon_trap(Deoptimization::Reason_intrinsic,
3529                   Deoptimization::Action_maybe_recompile);
3530   }
3531   if (!stopped()) {
3532     set_result(res);
3533   }
3534   return true;
3535 }
3536 
3537 
3538 //--------------------------inline_native_subtype_check------------------------
3539 // This intrinsic takes the JNI calls out of the heart of
3540 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
3541 bool LibraryCallKit::inline_native_subtype_check() {
3542   // Pull both arguments off the stack.
3543   Node* args[2];                // two java.lang.Class mirrors: superc, subc
3544   args[0] = argument(0);
3545   args[1] = argument(1);
3546 
3547   // We need write barriers here, because for primitive types we later compare
3548   // the two Class objects using ==, and those would give false negatives
3549   // if one obj is in from-space, and one in to-space.
3550   // TODO: Consider doing improved == comparison that only needs read barriers
3551   // on the false-path.
3552   args[0] = shenandoah_write_barrier(args[0]);
3553   args[1] = shenandoah_write_barrier(args[1]);
3554 
3555   Node* klasses[2];             // corresponding Klasses: superk, subk
3556   klasses[0] = klasses[1] = top();
3557 
3558   enum {
3559     // A full decision tree on {superc is prim, subc is prim}:
3560     _prim_0_path = 1,           // {P,N} => false
3561                                 // {P,P} & superc!=subc => false
3562     _prim_same_path,            // {P,P} & superc==subc => true
3563     _prim_1_path,               // {N,P} => false
3564     _ref_subtype_path,          // {N,N} & subtype check wins => true
3565     _both_ref_path,             // {N,N} & subtype check loses => false
3566     PATH_LIMIT
3567   };
3568 
3569   RegionNode* region = new RegionNode(PATH_LIMIT);
3570   Node*       phi    = new PhiNode(region, TypeInt::BOOL);
3571   record_for_igvn(region);
3572 
3573   const TypePtr* adr_type = TypeRawPtr::BOTTOM;   // memory type of loads
3574   const TypeKlassPtr* kls_type = TypeKlassPtr::OBJECT_OR_NULL;


3841 
3842     // Bail out if length is negative.
3843     // Without this the new_array would throw
3844     // NegativeArraySizeException but IllegalArgumentException is what
3845     // should be thrown
3846     generate_negative_guard(length, bailout, &length);
3847 
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       original = shenandoah_read_barrier(original);
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       // ArrayCopyNode:Ideal may transform the ArrayCopyNode to
3870       // loads/stores but it is legal only if we're sure the
3871       // Arrays.copyOf would succeed. So we need all input arguments
3872       // to the copyOf to be validated, including that the copy to the
3873       // new array won't trigger an ArrayStoreException. That subtype
3874       // check can be optimized if we know something on the type of
3875       // the input array from type speculation.
3876       if (_gvn.type(klass_node)->singleton()) {
3877         ciKlass* subk   = _gvn.type(load_object_klass(original))->is_klassptr()->klass();
3878         ciKlass* superk = _gvn.type(klass_node)->is_klassptr()->klass();
3879 
3880         int test = C->static_subtype_check(superk, subk);
3881         if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
3882           const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();


4024   PhiNode*    result_val = new PhiNode(result_reg, TypeInt::INT);
4025   PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
4026   PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4027   Node* obj = NULL;
4028   if (!is_static) {
4029     // Check for hashing null object
4030     obj = null_check_receiver();
4031     if (stopped())  return true;        // unconditionally null
4032     result_reg->init_req(_null_path, top());
4033     result_val->init_req(_null_path, top());
4034   } else {
4035     // Do a null check, and return zero if null.
4036     // System.identityHashCode(null) == 0
4037     obj = argument(0);
4038     Node* null_ctl = top();
4039     obj = null_check_oop(obj, &null_ctl);
4040     result_reg->init_req(_null_path, null_ctl);
4041     result_val->init_req(_null_path, _gvn.intcon(0));
4042   }
4043 
4044   if (ShenandoahVerifyReadsToFromSpace) {
4045     obj = shenandoah_read_barrier(obj);
4046   }
4047 
4048   // Unconditionally null?  Then return right away.
4049   if (stopped()) {
4050     set_control( result_reg->in(_null_path));
4051     if (!stopped())
4052       set_result(result_val->in(_null_path));
4053     return true;
4054   }
4055 
4056   // We only go to the fast case code if we pass a number of guards.  The
4057   // paths which do not pass are accumulated in the slow_region.
4058   RegionNode* slow_region = new RegionNode(1);
4059   record_for_igvn(slow_region);
4060 
4061   // If this is a virtual call, we generate a funny guard.  We pull out
4062   // the vtable entry corresponding to hashCode() from the target object.
4063   // If the target method which we are calling happens to be the native
4064   // Object hashCode() method, we pass the guard.  We do not need this
4065   // guard for non-virtual calls -- the caller is known to be the native
4066   // Object hashCode().
4067   if (is_virtual) {


4343 #endif //_LP64
4344 
4345 //----------------------inline_unsafe_copyMemory-------------------------
4346 // public native void sun.misc.Unsafe.copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4347 bool LibraryCallKit::inline_unsafe_copyMemory() {
4348   if (callee()->is_static())  return false;  // caller must have the capability!
4349   null_check_receiver();  // null-check receiver
4350   if (stopped())  return true;
4351 
4352   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
4353 
4354   Node* src_ptr =         argument(1);   // type: oop
4355   Node* src_off = ConvL2X(argument(2));  // type: long
4356   Node* dst_ptr =         argument(4);   // type: oop
4357   Node* dst_off = ConvL2X(argument(5));  // type: long
4358   Node* size    = ConvL2X(argument(7));  // type: long
4359 
4360   assert(Unsafe_field_offset_to_byte_offset(11) == 11,
4361          "fieldOffset must be byte-scaled");
4362 
4363   src_ptr = shenandoah_read_barrier(src_ptr);
4364   dst_ptr = shenandoah_write_barrier(dst_ptr);
4365 
4366   Node* src = make_unsafe_address(src_ptr, src_off);
4367   Node* dst = make_unsafe_address(dst_ptr, dst_off);
4368 
4369   // Conservatively insert a memory barrier on all memory slices.
4370   // Do not let writes of the copy source or destination float below the copy.
4371   insert_mem_bar(Op_MemBarCPUOrder);
4372 
4373   // Call it.  Note that the length argument is not scaled.
4374   make_runtime_call(RC_LEAF|RC_NO_FP,
4375                     OptoRuntime::fast_arraycopy_Type(),
4376                     StubRoutines::unsafe_arraycopy(),
4377                     "unsafe_arraycopy",
4378                     TypeRawPtr::BOTTOM,
4379                     src, dst, size XTOP);
4380 
4381   // Do not let reads of the copy destination float above the copy.
4382   insert_mem_bar(Op_MemBarCPUOrder);
4383 
4384   return true;
4385 }
4386 
4387 //------------------------clone_coping-----------------------------------
4388 // Helper function for inline_native_clone.
4389 void LibraryCallKit::copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array, bool card_mark) {
4390   assert(obj_size != NULL, "");
4391   Node* raw_obj = alloc_obj->in(1);
4392   assert(alloc_obj->is_CheckCastPP() && raw_obj->is_Proj() && raw_obj->in(0)->is_Allocate(), "");
4393 
4394   obj = shenandoah_read_barrier(obj);
4395 
4396   AllocateNode* alloc = NULL;
4397   if (ReduceBulkZeroing) {
4398     // We will be completely responsible for initializing this object -
4399     // mark Initialize node as complete.
4400     alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
4401     // The object was just allocated - there should be no any stores!
4402     guarantee(alloc != NULL && alloc->maybe_set_complete(&_gvn), "");
4403     // Mark as complete_with_arraycopy so that on AllocateNode
4404     // expansion, we know this AllocateNode is initialized by an array
4405     // copy and a StoreStore barrier exists after the array copy.
4406     alloc->initialization()->set_complete_with_arraycopy();
4407   }
4408 
4409   // Copy the fastest available way.
4410   // TODO: generate fields copies for small objects instead.
4411   Node* src  = obj;
4412   Node* dest = alloc_obj;
4413   Node* size = _gvn.transform(obj_size);
4414 
4415   // Exclude the header but include array length to copy by 8 bytes words.


4433   }
4434   src  = basic_plus_adr(src,  base_off);
4435   dest = basic_plus_adr(dest, base_off);
4436 
4437   // Compute the length also, if needed:
4438   Node* countx = size;
4439   countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
4440   countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong) ));
4441 
4442   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4443 
4444   ArrayCopyNode* ac = ArrayCopyNode::make(this, false, src, NULL, dest, NULL, countx, false);
4445   ac->set_clonebasic();
4446   Node* n = _gvn.transform(ac);
4447   if (n == ac) {
4448     set_predefined_output_for_runtime_call(ac, ac->in(TypeFunc::Memory), raw_adr_type);
4449   } else {
4450     set_all_memory(n);
4451   }
4452 
4453   if (UseShenandoahGC) {
4454     // Make sure that references in the cloned object are updated for Shenandoah.
4455     make_runtime_call(RC_LEAF|RC_NO_FP,
4456                       OptoRuntime::shenandoah_clone_barrier_Type(),
4457                       CAST_FROM_FN_PTR(address, SharedRuntime::shenandoah_clone_barrier),
4458                       "shenandoah_clone_barrier", TypePtr::BOTTOM,
4459                       alloc_obj);
4460   }
4461 
4462   // If necessary, emit some card marks afterwards.  (Non-arrays only.)
4463   if (card_mark) {
4464     assert(!is_array, "");
4465     // Put in store barrier for any and all oops we are sticking
4466     // into this object.  (We could avoid this if we could prove
4467     // that the object type contains no oop fields at all.)
4468     Node* no_particular_value = NULL;
4469     Node* no_particular_field = NULL;
4470     int raw_adr_idx = Compile::AliasIdxRaw;
4471     post_barrier(control(),
4472                  memory(raw_adr_type),
4473                  alloc_obj,
4474                  no_particular_field,
4475                  raw_adr_idx,
4476                  no_particular_value,
4477                  T_OBJECT,
4478                  false);
4479   }
4480 
4481   // Do not let reads from the cloned object float above the arraycopy.


4568 
4569     const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
4570     int raw_adr_idx = Compile::AliasIdxRaw;
4571 
4572     Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)NULL);
4573     if (array_ctl != NULL) {
4574       // It's an array.
4575       PreserveJVMState pjvms(this);
4576       set_control(array_ctl);
4577       Node* obj_length = load_array_length(obj);
4578       Node* obj_size  = NULL;
4579       Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size);  // no arguments to push
4580 
4581       if (!use_ReduceInitialCardMarks()) {
4582         // If it is an oop array, it requires very special treatment,
4583         // because card marking is required on each card of the array.
4584         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)NULL);
4585         if (is_obja != NULL) {
4586           PreserveJVMState pjvms2(this);
4587           set_control(is_obja);
4588 
4589           obj = shenandoah_read_barrier(obj);
4590 
4591           // Generate a direct call to the right arraycopy function(s).
4592           Node* alloc = tightly_coupled_allocation(alloc_obj, NULL);
4593           ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, alloc != NULL);
4594           ac->set_cloneoop();
4595           Node* n = _gvn.transform(ac);
4596           assert(n == ac, "cannot disappear");
4597           ac->connect_outputs(this);
4598 
4599           result_reg->init_req(_objArray_path, control());
4600           result_val->init_req(_objArray_path, alloc_obj);
4601           result_i_o ->set_req(_objArray_path, i_o());
4602           result_mem ->set_req(_objArray_path, reset_memory());
4603         }
4604       }
4605       // Otherwise, there are no card marks to worry about.
4606       // (We can dispense with card marks if we know the allocation
4607       //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4608       //  causes the non-eden paths to take compensating steps to
4609       //  simulate a fresh allocation, so that no further
4610       //  card marks are required in compiled code to initialize


4819     _gvn.hash_delete(dest);
4820     dest->set_req(0, control());
4821     Node* destx = _gvn.transform(dest);
4822     assert(destx == dest, "where has the allocation result gone?");
4823   }
4824 }
4825 
4826 
4827 //------------------------------inline_arraycopy-----------------------
4828 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4829 //                                                      Object dest, int destPos,
4830 //                                                      int length);
4831 bool LibraryCallKit::inline_arraycopy() {
4832   // Get the arguments.
4833   Node* src         = argument(0);  // type: oop
4834   Node* src_offset  = argument(1);  // type: int
4835   Node* dest        = argument(2);  // type: oop
4836   Node* dest_offset = argument(3);  // type: int
4837   Node* length      = argument(4);  // type: int
4838 
4839   src = shenandoah_read_barrier(src);
4840   dest = shenandoah_write_barrier(dest);
4841 
4842   // Check for allocation before we add nodes that would confuse
4843   // tightly_coupled_allocation()
4844   AllocateArrayNode* alloc = tightly_coupled_allocation(dest, NULL);
4845 
4846   int saved_reexecute_sp = -1;
4847   JVMState* saved_jvms = arraycopy_restore_alloc_state(alloc, saved_reexecute_sp);
4848   // See arraycopy_restore_alloc_state() comment
4849   // if alloc == NULL we don't have to worry about a tightly coupled allocation so we can emit all needed guards
4850   // if saved_jvms != NULL (then alloc != NULL) then we can handle guards and a tightly coupled allocation
4851   // if saved_jvms == NULL and alloc != NULL, we can’t emit any guards
4852   bool can_emit_guards = (alloc == NULL || saved_jvms != NULL);
4853 
4854   // The following tests must be performed
4855   // (1) src and dest are arrays.
4856   // (2) src and dest arrays must have elements of the same BasicType
4857   // (3) src and dest must not be null.
4858   // (4) src_offset must not be negative.
4859   // (5) dest_offset must not be negative.
4860   // (6) length must not be negative.


5060   Node* n = _gvn.transform(ac);
5061   if (n == ac) {
5062     ac->connect_outputs(this);
5063   } else {
5064     assert(validated, "shouldn't transform if all arguments not validated");
5065     set_all_memory(n);
5066   }
5067 
5068   return true;
5069 }
5070 
5071 
5072 // Helper function which determines if an arraycopy immediately follows
5073 // an allocation, with no intervening tests or other escapes for the object.
5074 AllocateArrayNode*
5075 LibraryCallKit::tightly_coupled_allocation(Node* ptr,
5076                                            RegionNode* slow_region) {
5077   if (stopped())             return NULL;  // no fast path
5078   if (C->AliasLevel() == 0)  return NULL;  // no MergeMems around
5079 
5080   ptr = ShenandoahBarrierNode::skip_through_barrier(ptr);
5081 
5082   AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
5083   if (alloc == NULL)  return NULL;
5084 
5085   Node* rawmem = memory(Compile::AliasIdxRaw);
5086   // Is the allocation's memory state untouched?
5087   if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
5088     // Bail out if there have been raw-memory effects since the allocation.
5089     // (Example:  There might have been a call or safepoint.)
5090     return NULL;
5091   }
5092   rawmem = rawmem->in(0)->as_Initialize()->memory(Compile::AliasIdxRaw);
5093   if (!(rawmem->is_Proj() && rawmem->in(0) == alloc)) {
5094     return NULL;
5095   }
5096 
5097   // There must be no unexpected observers of this allocation.
5098   for (DUIterator_Fast imax, i = ptr->fast_outs(imax); i < imax; i++) {
5099     Node* obs = ptr->fast_out(i);
5100     if (obs != this->map()) {
5101       return NULL;


5141 
5142   // If we get this far, we have an allocation which immediately
5143   // precedes the arraycopy, and we can take over zeroing the new object.
5144   // The arraycopy will finish the initialization, and provide
5145   // a new control state to which we will anchor the destination pointer.
5146 
5147   return alloc;
5148 }
5149 
5150 //-------------inline_encodeISOArray-----------------------------------
5151 // encode char[] to byte[] in ISO_8859_1
5152 bool LibraryCallKit::inline_encodeISOArray() {
5153   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
5154   // no receiver since it is static method
5155   Node *src         = argument(0);
5156   Node *src_offset  = argument(1);
5157   Node *dst         = argument(2);
5158   Node *dst_offset  = argument(3);
5159   Node *length      = argument(4);
5160 
5161   src = shenandoah_read_barrier(src);
5162   dst = shenandoah_write_barrier(dst);
5163 
5164   const Type* src_type = src->Value(&_gvn);
5165   const Type* dst_type = dst->Value(&_gvn);
5166   const TypeAryPtr* top_src = src_type->isa_aryptr();
5167   const TypeAryPtr* top_dest = dst_type->isa_aryptr();
5168   if (top_src  == NULL || top_src->klass()  == NULL ||
5169       top_dest == NULL || top_dest->klass() == NULL) {
5170     // failed array check
5171     return false;
5172   }
5173 
5174   // Figure out the size and type of the elements we will be copying.
5175   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5176   BasicType dst_elem = dst_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5177   if (src_elem != T_CHAR || dst_elem != T_BYTE) {
5178     return false;
5179   }
5180   Node* src_start = array_element_address(src, src_offset, src_elem);
5181   Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
5182   // 'src_start' points to src array + scaled offset
5183   // 'dst_start' points to dst array + scaled offset


5193 
5194 //-------------inline_multiplyToLen-----------------------------------
5195 bool LibraryCallKit::inline_multiplyToLen() {
5196   assert(UseMultiplyToLenIntrinsic, "not implemented on this platform");
5197 
5198   address stubAddr = StubRoutines::multiplyToLen();
5199   if (stubAddr == NULL) {
5200     return false; // Intrinsic's stub is not implemented on this platform
5201   }
5202   const char* stubName = "multiplyToLen";
5203 
5204   assert(callee()->signature()->size() == 5, "multiplyToLen has 5 parameters");
5205 
5206   // no receiver because it is a static method
5207   Node* x    = argument(0);
5208   Node* xlen = argument(1);
5209   Node* y    = argument(2);
5210   Node* ylen = argument(3);
5211   Node* z    = argument(4);
5212 
5213   x = shenandoah_read_barrier(x);
5214   y = shenandoah_read_barrier(y);
5215   z = shenandoah_write_barrier(z);
5216 
5217   const Type* x_type = x->Value(&_gvn);
5218   const Type* y_type = y->Value(&_gvn);
5219   const TypeAryPtr* top_x = x_type->isa_aryptr();
5220   const TypeAryPtr* top_y = y_type->isa_aryptr();
5221   if (top_x  == NULL || top_x->klass()  == NULL ||
5222       top_y == NULL || top_y->klass() == NULL) {
5223     // failed array check
5224     return false;
5225   }
5226 
5227   BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5228   BasicType y_elem = y_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5229   if (x_elem != T_INT || y_elem != T_INT) {
5230     return false;
5231   }
5232 
5233   // Set the original stack and the reexecute bit for the interpreter to reexecute
5234   // the bytecode that invokes BigInteger.multiplyToLen() if deoptimization happens
5235   // on the return from z array allocation in runtime.
5236   { PreserveReexecuteState preexecs(this);


5297   return true;
5298 }
5299 
5300 //-------------inline_squareToLen------------------------------------
5301 bool LibraryCallKit::inline_squareToLen() {
5302   assert(UseSquareToLenIntrinsic, "not implementated on this platform");
5303 
5304   address stubAddr = StubRoutines::squareToLen();
5305   if (stubAddr == NULL) {
5306     return false; // Intrinsic's stub is not implemented on this platform
5307   }
5308   const char* stubName = "squareToLen";
5309 
5310   assert(callee()->signature()->size() == 4, "implSquareToLen has 4 parameters");
5311 
5312   Node* x    = argument(0);
5313   Node* len  = argument(1);
5314   Node* z    = argument(2);
5315   Node* zlen = argument(3);
5316 
5317   x = shenandoah_read_barrier(x);
5318   z = shenandoah_write_barrier(z);
5319 
5320   const Type* x_type = x->Value(&_gvn);
5321   const Type* z_type = z->Value(&_gvn);
5322   const TypeAryPtr* top_x = x_type->isa_aryptr();
5323   const TypeAryPtr* top_z = z_type->isa_aryptr();
5324   if (top_x  == NULL || top_x->klass()  == NULL ||
5325       top_z  == NULL || top_z->klass()  == NULL) {
5326     // failed array check
5327     return false;
5328   }
5329 
5330   BasicType x_elem = x_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5331   BasicType z_elem = z_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5332   if (x_elem != T_INT || z_elem != T_INT) {
5333     return false;
5334   }
5335 
5336 
5337   Node* x_start = array_element_address(x, intcon(0), x_elem);
5338   Node* z_start = array_element_address(z, intcon(0), z_elem);
5339 


5347 }
5348 
5349 //-------------inline_mulAdd------------------------------------------
5350 bool LibraryCallKit::inline_mulAdd() {
5351   assert(UseMulAddIntrinsic, "not implementated on this platform");
5352 
5353   address stubAddr = StubRoutines::mulAdd();
5354   if (stubAddr == NULL) {
5355     return false; // Intrinsic's stub is not implemented on this platform
5356   }
5357   const char* stubName = "mulAdd";
5358 
5359   assert(callee()->signature()->size() == 5, "mulAdd has 5 parameters");
5360 
5361   Node* out      = argument(0);
5362   Node* in       = argument(1);
5363   Node* offset   = argument(2);
5364   Node* len      = argument(3);
5365   Node* k        = argument(4);
5366 
5367   in = shenandoah_read_barrier(in);
5368   out = shenandoah_write_barrier(out);
5369 
5370   const Type* out_type = out->Value(&_gvn);
5371   const Type* in_type = in->Value(&_gvn);
5372   const TypeAryPtr* top_out = out_type->isa_aryptr();
5373   const TypeAryPtr* top_in = in_type->isa_aryptr();
5374   if (top_out  == NULL || top_out->klass()  == NULL ||
5375       top_in == NULL || top_in->klass() == NULL) {
5376     // failed array check
5377     return false;
5378   }
5379 
5380   BasicType out_elem = out_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5381   BasicType in_elem = in_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5382   if (out_elem != T_INT || in_elem != T_INT) {
5383     return false;
5384   }
5385 
5386   Node* outlen = load_array_length(out);
5387   Node* new_offset = _gvn.transform(new SubINode(outlen, offset));
5388   Node* out_start = array_element_address(out, intcon(0), out_elem);
5389   Node* in_start = array_element_address(in, intcon(0), in_elem);


5399 
5400 //-------------inline_montgomeryMultiply-----------------------------------
5401 bool LibraryCallKit::inline_montgomeryMultiply() {
5402   address stubAddr = StubRoutines::montgomeryMultiply();
5403   if (stubAddr == NULL) {
5404     return false; // Intrinsic's stub is not implemented on this platform
5405   }
5406 
5407   assert(UseMontgomeryMultiplyIntrinsic, "not implemented on this platform");
5408   const char* stubName = "montgomery_square";
5409 
5410   assert(callee()->signature()->size() == 7, "montgomeryMultiply has 7 parameters");
5411 
5412   Node* a    = argument(0);
5413   Node* b    = argument(1);
5414   Node* n    = argument(2);
5415   Node* len  = argument(3);
5416   Node* inv  = argument(4);
5417   Node* m    = argument(6);
5418 
5419   a = shenandoah_read_barrier(a);
5420   b = shenandoah_read_barrier(b);
5421   n = shenandoah_read_barrier(n);
5422   m = shenandoah_write_barrier(m);
5423 
5424   const Type* a_type = a->Value(&_gvn);
5425   const TypeAryPtr* top_a = a_type->isa_aryptr();
5426   const Type* b_type = b->Value(&_gvn);
5427   const TypeAryPtr* top_b = b_type->isa_aryptr();
5428   const Type* n_type = a->Value(&_gvn);
5429   const TypeAryPtr* top_n = n_type->isa_aryptr();
5430   const Type* m_type = a->Value(&_gvn);
5431   const TypeAryPtr* top_m = m_type->isa_aryptr();
5432   if (top_a  == NULL || top_a->klass()  == NULL ||
5433       top_b == NULL || top_b->klass()  == NULL ||
5434       top_n == NULL || top_n->klass()  == NULL ||
5435       top_m == NULL || top_m->klass()  == NULL) {
5436     // failed array check
5437     return false;
5438   }
5439 
5440   BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5441   BasicType b_elem = b_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5442   BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5443   BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();


5463   return true;
5464 }
5465 
5466 bool LibraryCallKit::inline_montgomerySquare() {
5467   address stubAddr = StubRoutines::montgomerySquare();
5468   if (stubAddr == NULL) {
5469     return false; // Intrinsic's stub is not implemented on this platform
5470   }
5471 
5472   assert(UseMontgomerySquareIntrinsic, "not implemented on this platform");
5473   const char* stubName = "montgomery_square";
5474 
5475   assert(callee()->signature()->size() == 6, "montgomerySquare has 6 parameters");
5476 
5477   Node* a    = argument(0);
5478   Node* n    = argument(1);
5479   Node* len  = argument(2);
5480   Node* inv  = argument(3);
5481   Node* m    = argument(5);
5482 
5483   a = shenandoah_read_barrier(a);
5484   n = shenandoah_read_barrier(n);
5485   m = shenandoah_write_barrier(m);
5486 
5487   const Type* a_type = a->Value(&_gvn);
5488   const TypeAryPtr* top_a = a_type->isa_aryptr();
5489   const Type* n_type = a->Value(&_gvn);
5490   const TypeAryPtr* top_n = n_type->isa_aryptr();
5491   const Type* m_type = a->Value(&_gvn);
5492   const TypeAryPtr* top_m = m_type->isa_aryptr();
5493   if (top_a  == NULL || top_a->klass()  == NULL ||
5494       top_n == NULL || top_n->klass()  == NULL ||
5495       top_m == NULL || top_m->klass()  == NULL) {
5496     // failed array check
5497     return false;
5498   }
5499 
5500   BasicType a_elem = a_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5501   BasicType n_elem = n_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5502   BasicType m_elem = m_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5503   if (a_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5504     return false;
5505   }
5506 


5553   crc = _gvn.transform(new URShiftINode(crc, intcon(8)));
5554   result = _gvn.transform(new XorINode(crc, result));
5555   result = _gvn.transform(new XorINode(result, M1));
5556   set_result(result);
5557   return true;
5558 }
5559 
5560 /**
5561  * Calculate CRC32 for byte[] array.
5562  * int java.util.zip.CRC32.updateBytes(int crc, byte[] buf, int off, int len)
5563  */
5564 bool LibraryCallKit::inline_updateBytesCRC32() {
5565   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5566   assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5567   // no receiver since it is static method
5568   Node* crc     = argument(0); // type: int
5569   Node* src     = argument(1); // type: oop
5570   Node* offset  = argument(2); // type: int
5571   Node* length  = argument(3); // type: int
5572 
5573   src = shenandoah_read_barrier(src);
5574 
5575   const Type* src_type = src->Value(&_gvn);
5576   const TypeAryPtr* top_src = src_type->isa_aryptr();
5577   if (top_src  == NULL || top_src->klass()  == NULL) {
5578     // failed array check
5579     return false;
5580   }
5581 
5582   // Figure out the size and type of the elements we will be copying.
5583   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5584   if (src_elem != T_BYTE) {
5585     return false;
5586   }
5587 
5588   // 'src_start' points to src array + scaled offset
5589   Node* src_start = array_element_address(src, offset, src_elem);
5590 
5591   // We assume that range check is done by caller.
5592   // TODO: generate range check (offset+length < src.length) in debug VM.
5593 
5594   // Call the stub.


5657   Node* src     = argument(1); // type: oop
5658   Node* offset  = argument(2); // type: int
5659   Node* end     = argument(3); // type: int
5660 
5661   Node* length = _gvn.transform(new SubINode(end, offset));
5662 
5663   const Type* src_type = src->Value(&_gvn);
5664   const TypeAryPtr* top_src = src_type->isa_aryptr();
5665   if (top_src  == NULL || top_src->klass()  == NULL) {
5666     // failed array check
5667     return false;
5668   }
5669 
5670   // Figure out the size and type of the elements we will be copying.
5671   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5672   if (src_elem != T_BYTE) {
5673     return false;
5674   }
5675 
5676   // 'src_start' points to src array + scaled offset
5677   src = shenandoah_read_barrier(src);
5678   Node* src_start = array_element_address(src, offset, src_elem);
5679 
5680   // static final int[] byteTable in class CRC32C
5681   Node* table = get_table_from_crc32c_class(callee()->holder());
5682   table = shenandoah_read_barrier(table);
5683   Node* table_start = array_element_address(table, intcon(0), T_INT);
5684 
5685   // We assume that range check is done by caller.
5686   // TODO: generate range check (offset+length < src.length) in debug VM.
5687 
5688   // Call the stub.
5689   address stubAddr = StubRoutines::updateBytesCRC32C();
5690   const char *stubName = "updateBytesCRC32C";
5691 
5692   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5693                                  stubAddr, stubName, TypePtr::BOTTOM,
5694                                  crc, src_start, length, table_start);
5695   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5696   set_result(result);
5697   return true;
5698 }
5699 
5700 //------------------------------inline_updateDirectByteBufferCRC32C-----------------------
5701 //
5702 // Calculate CRC32C for DirectByteBuffer.


5706   assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5707   assert(callee()->signature()->size() == 5, "updateDirectByteBuffer has 4 parameters and one is long");
5708   assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5709   // no receiver since it is a static method
5710   Node* crc     = argument(0); // type: int
5711   Node* src     = argument(1); // type: long
5712   Node* offset  = argument(3); // type: int
5713   Node* end     = argument(4); // type: int
5714 
5715   Node* length = _gvn.transform(new SubINode(end, offset));
5716 
5717   src = ConvL2X(src);  // adjust Java long to machine word
5718   Node* base = _gvn.transform(new CastX2PNode(src));
5719   offset = ConvI2X(offset);
5720 
5721   // 'src_start' points to src array + scaled offset
5722   Node* src_start = basic_plus_adr(top(), base, offset);
5723 
5724   // static final int[] byteTable in class CRC32C
5725   Node* table = get_table_from_crc32c_class(callee()->holder());
5726   table = shenandoah_read_barrier(table);
5727   Node* table_start = array_element_address(table, intcon(0), T_INT);
5728 
5729   // Call the stub.
5730   address stubAddr = StubRoutines::updateBytesCRC32C();
5731   const char *stubName = "updateBytesCRC32C";
5732 
5733   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5734                                  stubAddr, stubName, TypePtr::BOTTOM,
5735                                  crc, src_start, length, table_start);
5736   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5737   set_result(result);
5738   return true;
5739 }
5740 
5741 //------------------------------inline_updateBytesAdler32----------------------
5742 //
5743 // Calculate Adler32 checksum for byte[] array.
5744 // int java.util.zip.Adler32.updateBytes(int crc, byte[] buf, int off, int len)
5745 //
5746 bool LibraryCallKit::inline_updateBytesAdler32() {


5750   // no receiver since it is static method
5751   Node* crc     = argument(0); // type: int
5752   Node* src     = argument(1); // type: oop
5753   Node* offset  = argument(2); // type: int
5754   Node* length  = argument(3); // type: int
5755 
5756   const Type* src_type = src->Value(&_gvn);
5757   const TypeAryPtr* top_src = src_type->isa_aryptr();
5758   if (top_src  == NULL || top_src->klass()  == NULL) {
5759     // failed array check
5760     return false;
5761   }
5762 
5763   // Figure out the size and type of the elements we will be copying.
5764   BasicType src_elem = src_type->isa_aryptr()->klass()->as_array_klass()->element_type()->basic_type();
5765   if (src_elem != T_BYTE) {
5766     return false;
5767   }
5768 
5769   // 'src_start' points to src array + scaled offset
5770   src = shenandoah_read_barrier(src);
5771   Node* src_start = array_element_address(src, offset, src_elem);
5772 
5773   // We assume that range check is done by caller.
5774   // TODO: generate range check (offset+length < src.length) in debug VM.
5775 
5776   // Call the stub.
5777   address stubAddr = StubRoutines::updateBytesAdler32();
5778   const char *stubName = "updateBytesAdler32";
5779 
5780   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5781                                  stubAddr, stubName, TypePtr::BOTTOM,
5782                                  crc, src_start, length);
5783   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5784   set_result(result);
5785   return true;
5786 }
5787 
5788 //------------------------------inline_updateByteBufferAdler32---------------
5789 //
5790 // Calculate Adler32 checksum for DirectByteBuffer.


5813 
5814   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5815                                  stubAddr, stubName, TypePtr::BOTTOM,
5816                                  crc, src_start, length);
5817 
5818   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5819   set_result(result);
5820   return true;
5821 }
5822 
5823 //----------------------------inline_reference_get----------------------------
5824 // public T java.lang.ref.Reference.get();
5825 bool LibraryCallKit::inline_reference_get() {
5826   const int referent_offset = java_lang_ref_Reference::referent_offset;
5827   guarantee(referent_offset > 0, "should have already been set");
5828 
5829   // Get the argument:
5830   Node* reference_obj = null_check_receiver();
5831   if (stopped()) return true;
5832 
5833   if (ShenandoahVerifyReadsToFromSpace) {
5834     reference_obj = shenandoah_read_barrier(reference_obj);
5835   }
5836 
5837   Node* adr = basic_plus_adr(reference_obj, reference_obj, referent_offset);
5838 
5839   ciInstanceKlass* klass = env()->Object_klass();
5840   const TypeOopPtr* object_type = TypeOopPtr::make_from_klass(klass);
5841 
5842   Node* no_ctrl = NULL;
5843   Node* result = make_load(no_ctrl, adr, object_type, T_OBJECT, MemNode::unordered);
5844 
5845   // Use the pre-barrier to record the value in the referent field
5846   pre_barrier(false /* do_load */,
5847               control(),
5848               NULL /* obj */, NULL /* adr */, max_juint /* alias_idx */, NULL /* val */, NULL /* val_type */,
5849               result /* pre_val */,
5850               T_OBJECT);
5851 
5852   // Add memory barrier to prevent commoning reads from this field
5853   // across safepoint since GC can change its value.
5854   insert_mem_bar(Op_MemBarCPUOrder);
5855 
5856   set_result(result);


5865     const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5866     assert(tinst != NULL, "obj is null");
5867     assert(tinst->klass()->is_loaded(), "obj is not loaded");
5868     assert(!is_exact || tinst->klass_is_exact(), "klass not exact");
5869     fromKls = tinst->klass()->as_instance_klass();
5870   } else {
5871     assert(is_static, "only for static field access");
5872   }
5873   ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5874                                               ciSymbol::make(fieldTypeString),
5875                                               is_static);
5876 
5877   assert (field != NULL, "undefined field");
5878   if (field == NULL) return (Node *) NULL;
5879 
5880   if (is_static) {
5881     const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5882     fromObj = makecon(tip);
5883   }
5884 
5885   fromObj = shenandoah_read_barrier(fromObj);
5886 
5887   // Next code  copied from Parse::do_get_xxx():
5888 
5889   // Compute address and memory type.
5890   int offset  = field->offset_in_bytes();
5891   bool is_vol = field->is_volatile();
5892   ciType* field_klass = field->type();
5893   assert(field_klass->is_loaded(), "should be loaded");
5894   const TypePtr* adr_type = C->alias_type(field)->adr_type();
5895   Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5896   BasicType bt = field->layout_type();
5897 
5898   // Build the resultant type of the load
5899   const Type *type;
5900   if (bt == T_OBJECT) {
5901     type = TypeOopPtr::make_from_klass(field_klass->as_klass());
5902   } else {
5903     type = Type::get_const_basic_type(bt);
5904   }
5905 
5906   if (support_IRIW_for_not_multiple_copy_atomic_cpu && is_vol) {


5927   assert(UseAES, "need AES instruction support");
5928 
5929   switch(id) {
5930   case vmIntrinsics::_aescrypt_encryptBlock:
5931     stubAddr = StubRoutines::aescrypt_encryptBlock();
5932     stubName = "aescrypt_encryptBlock";
5933     break;
5934   case vmIntrinsics::_aescrypt_decryptBlock:
5935     stubAddr = StubRoutines::aescrypt_decryptBlock();
5936     stubName = "aescrypt_decryptBlock";
5937     break;
5938   }
5939   if (stubAddr == NULL) return false;
5940 
5941   Node* aescrypt_object = argument(0);
5942   Node* src             = argument(1);
5943   Node* src_offset      = argument(2);
5944   Node* dest            = argument(3);
5945   Node* dest_offset     = argument(4);
5946 
5947   // Resolve src and dest arrays for ShenandoahGC.
5948   src = shenandoah_read_barrier(src);
5949   dest = shenandoah_write_barrier(dest);
5950 
5951   // (1) src and dest are arrays.
5952   const Type* src_type = src->Value(&_gvn);
5953   const Type* dest_type = dest->Value(&_gvn);
5954   const TypeAryPtr* top_src = src_type->isa_aryptr();
5955   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
5956   assert (top_src  != NULL && top_src->klass()  != NULL &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
5957 
5958   // for the quick and dirty code we will skip all the checks.
5959   // we are just trying to get the call to be generated.
5960   Node* src_start  = src;
5961   Node* dest_start = dest;
5962   if (src_offset != NULL || dest_offset != NULL) {
5963     assert(src_offset != NULL && dest_offset != NULL, "");
5964     src_start  = array_element_address(src,  src_offset,  T_BYTE);
5965     dest_start = array_element_address(dest, dest_offset, T_BYTE);
5966   }
5967 
5968   // now need to get the start of its expanded key array
5969   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
5970   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);


5999 
6000   switch(id) {
6001   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
6002     stubAddr = StubRoutines::cipherBlockChaining_encryptAESCrypt();
6003     stubName = "cipherBlockChaining_encryptAESCrypt";
6004     break;
6005   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
6006     stubAddr = StubRoutines::cipherBlockChaining_decryptAESCrypt();
6007     stubName = "cipherBlockChaining_decryptAESCrypt";
6008     break;
6009   }
6010   if (stubAddr == NULL) return false;
6011 
6012   Node* cipherBlockChaining_object = argument(0);
6013   Node* src                        = argument(1);
6014   Node* src_offset                 = argument(2);
6015   Node* len                        = argument(3);
6016   Node* dest                       = argument(4);
6017   Node* dest_offset                = argument(5);
6018 
6019   // Resolve src and dest arrays for ShenandoahGC.
6020   src = shenandoah_read_barrier(src);
6021   dest = shenandoah_write_barrier(dest);
6022 
6023   // (1) src and dest are arrays.
6024   const Type* src_type = src->Value(&_gvn);
6025   const Type* dest_type = dest->Value(&_gvn);
6026   const TypeAryPtr* top_src = src_type->isa_aryptr();
6027   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6028   assert (top_src  != NULL && top_src->klass()  != NULL
6029           &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6030 
6031   // checks are the responsibility of the caller
6032   Node* src_start  = src;
6033   Node* dest_start = dest;
6034   if (src_offset != NULL || dest_offset != NULL) {
6035     assert(src_offset != NULL && dest_offset != NULL, "");
6036     src_start  = array_element_address(src,  src_offset,  T_BYTE);
6037     dest_start = array_element_address(dest, dest_offset, T_BYTE);
6038   }
6039 
6040   // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6041   // (because of the predicated logic executed earlier).
6042   // so we cast it here safely.


6047 
6048   // cast it to what we know it will be at runtime
6049   const TypeInstPtr* tinst = _gvn.type(cipherBlockChaining_object)->isa_instptr();
6050   assert(tinst != NULL, "CBC obj is null");
6051   assert(tinst->klass()->is_loaded(), "CBC obj is not loaded");
6052   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6053   assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6054 
6055   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6056   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6057   const TypeOopPtr* xtype = aklass->as_instance_type();
6058   Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6059   aescrypt_object = _gvn.transform(aescrypt_object);
6060 
6061   // we need to get the start of the aescrypt_object's expanded key array
6062   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6063   if (k_start == NULL) return false;
6064 
6065   // similarly, get the start address of the r vector
6066   Node* objRvec = load_field_from_object(cipherBlockChaining_object, "r", "[B", /*is_exact*/ false);
6067 
6068   objRvec = shenandoah_write_barrier(objRvec);
6069 
6070   if (objRvec == NULL) return false;
6071   Node* r_start = array_element_address(objRvec, intcon(0), T_BYTE);
6072 
6073   Node* cbcCrypt;
6074   if (Matcher::pass_original_key_for_aes()) {
6075     // on SPARC we need to pass the original key since key expansion needs to happen in intrinsics due to
6076     // compatibility issues between Java key expansion and SPARC crypto instructions
6077     Node* original_k_start = get_original_key_start_from_aescrypt_object(aescrypt_object);
6078     if (original_k_start == NULL) return false;
6079 
6080     // Call the stub, passing src_start, dest_start, k_start, r_start, src_len and original_k_start
6081     cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6082                                  OptoRuntime::cipherBlockChaining_aescrypt_Type(),
6083                                  stubAddr, stubName, TypePtr::BOTTOM,
6084                                  src_start, dest_start, k_start, r_start, len, original_k_start);
6085   } else {
6086     // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6087     cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6088                                  OptoRuntime::cipherBlockChaining_aescrypt_Type(),
6089                                  stubAddr, stubName, TypePtr::BOTTOM,
6090                                  src_start, dest_start, k_start, r_start, len);
6091   }
6092 
6093   // return cipher length (int)
6094   Node* retvalue = _gvn.transform(new ProjNode(cbcCrypt, TypeFunc::Parms));
6095   set_result(retvalue);
6096   return true;
6097 }
6098 
6099 //------------------------------get_key_start_from_aescrypt_object-----------------------
6100 Node * LibraryCallKit::get_key_start_from_aescrypt_object(Node *aescrypt_object) {
6101   Node* objAESCryptKey = load_field_from_object(aescrypt_object, "K", "[I", /*is_exact*/ false);
6102   assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
6103   if (objAESCryptKey == NULL) return (Node *) NULL;
6104 
6105   objAESCryptKey = shenandoah_read_barrier(objAESCryptKey);
6106 
6107   // now have the array, need to get the start address of the K array
6108   Node* k_start = array_element_address(objAESCryptKey, intcon(0), T_INT);
6109   return k_start;
6110 }
6111 
6112 //------------------------------get_original_key_start_from_aescrypt_object-----------------------
6113 Node * LibraryCallKit::get_original_key_start_from_aescrypt_object(Node *aescrypt_object) {
6114   Node* objAESCryptKey = load_field_from_object(aescrypt_object, "lastKey", "[B", /*is_exact*/ false);
6115   assert (objAESCryptKey != NULL, "wrong version of com.sun.crypto.provider.AESCrypt");
6116   if (objAESCryptKey == NULL) return (Node *) NULL;
6117 
6118   // now have the array, need to get the start address of the lastKey array
6119   Node* original_k_start = array_element_address(objAESCryptKey, intcon(0), T_BYTE);
6120   return original_k_start;
6121 }
6122 
6123 //----------------------------inline_cipherBlockChaining_AESCrypt_predicate----------------------------
6124 // Return node representing slow path of predicate check.
6125 // the pseudo code we want to emulate with this predicate is:


< prev index next >