hotspot/src/share/vm/opto/graphKit.cpp
changeset 21099 46e6bbecd9e5
parent 21089 e1986ff6fe2e
child 22234 da823d78ad65
child 22845 d8812d0ff387
equal deleted inserted replaced
21098:1820670a3362 21099:46e6bbecd9e5
  2096       set_argument(j, arg);
  2096       set_argument(j, arg);
  2097     }
  2097     }
  2098   }
  2098   }
  2099 }
  2099 }
  2100 
  2100 
       
  2101 /**
       
  2102  * Record profiling data exact_kls for Node n with the type system so
       
  2103  * that it can propagate it (speculation)
       
  2104  *
       
  2105  * @param n          node that the type applies to
       
  2106  * @param exact_kls  type from profiling
       
  2107  *
       
  2108  * @return           node with improved type
       
  2109  */
       
  2110 Node* GraphKit::record_profile_for_speculation(Node* n, ciKlass* exact_kls) {
       
  2111   const TypeOopPtr* current_type = _gvn.type(n)->isa_oopptr();
       
  2112   assert(UseTypeSpeculation, "type speculation must be on");
       
  2113   if (exact_kls != NULL &&
       
  2114       // nothing to improve if type is already exact
       
  2115       (current_type == NULL ||
       
  2116        (!current_type->klass_is_exact() &&
       
  2117         (current_type->speculative() == NULL ||
       
  2118          !current_type->speculative()->klass_is_exact())))) {
       
  2119     const TypeKlassPtr* tklass = TypeKlassPtr::make(exact_kls);
       
  2120     const TypeOopPtr* xtype = tklass->as_instance_type();
       
  2121     assert(xtype->klass_is_exact(), "Should be exact");
       
  2122 
       
  2123     // Build a type with a speculative type (what we think we know
       
  2124     // about the type but will need a guard when we use it)
       
  2125     const TypeOopPtr* spec_type = TypeOopPtr::make(TypePtr::BotPTR, Type::OffsetBot, TypeOopPtr::InstanceBot, xtype);
       
  2126     // We're changing the type, we need a new cast node to carry the
       
  2127     // new type. The new type depends on the control: what profiling
       
  2128     // tells us is only valid from here as far as we can tell.
       
  2129     Node* cast = new(C) CastPPNode(n, spec_type);
       
  2130     cast->init_req(0, control());
       
  2131     cast = _gvn.transform(cast);
       
  2132     replace_in_map(n, cast);
       
  2133     n = cast;
       
  2134   }
       
  2135   return n;
       
  2136 }
       
  2137 
       
  2138 /**
       
  2139  * Record profiling data from receiver profiling at an invoke with the
       
  2140  * type system so that it can propagate it (speculation)
       
  2141  *
       
  2142  * @param n  receiver node
       
  2143  *
       
  2144  * @return           node with improved type
       
  2145  */
       
  2146 Node* GraphKit::record_profiled_receiver_for_speculation(Node* n) {
       
  2147   if (!UseTypeSpeculation) {
       
  2148     return n;
       
  2149   }
       
  2150   ciKlass* exact_kls = profile_has_unique_klass();
       
  2151   return record_profile_for_speculation(n, exact_kls);
       
  2152 }
       
  2153 
       
  2154 /**
       
  2155  * Record profiling data from argument profiling at an invoke with the
       
  2156  * type system so that it can propagate it (speculation)
       
  2157  *
       
  2158  * @param dest_method  target method for the call
       
  2159  * @param bc           what invoke bytecode is this?
       
  2160  */
       
  2161 void GraphKit::record_profiled_arguments_for_speculation(ciMethod* dest_method, Bytecodes::Code bc) {
       
  2162   if (!UseTypeSpeculation) {
       
  2163     return;
       
  2164   }
       
  2165   const TypeFunc* tf    = TypeFunc::make(dest_method);
       
  2166   int             nargs = tf->_domain->_cnt - TypeFunc::Parms;
       
  2167   int skip = Bytecodes::has_receiver(bc) ? 1 : 0;
       
  2168   for (int j = skip, i = 0; j < nargs && i < TypeProfileArgsLimit; j++) {
       
  2169     const Type *targ = tf->_domain->field_at(j + TypeFunc::Parms);
       
  2170     if (targ->basic_type() == T_OBJECT || targ->basic_type() == T_ARRAY) {
       
  2171       ciKlass* better_type = method()->argument_profiled_type(bci(), i);
       
  2172       if (better_type != NULL) {
       
  2173         record_profile_for_speculation(argument(j), better_type);
       
  2174       }
       
  2175       i++;
       
  2176     }
       
  2177   }
       
  2178 }
       
  2179 
       
  2180 /**
       
  2181  * Record profiling data from parameter profiling at an invoke with
       
  2182  * the type system so that it can propagate it (speculation)
       
  2183  */
       
  2184 void GraphKit::record_profiled_parameters_for_speculation() {
       
  2185   if (!UseTypeSpeculation) {
       
  2186     return;
       
  2187   }
       
  2188   for (int i = 0, j = 0; i < method()->arg_size() ; i++) {
       
  2189     if (_gvn.type(local(i))->isa_oopptr()) {
       
  2190       ciKlass* better_type = method()->parameter_profiled_type(j);
       
  2191       if (better_type != NULL) {
       
  2192         record_profile_for_speculation(local(i), better_type);
       
  2193       }
       
  2194       j++;
       
  2195     }
       
  2196   }
       
  2197 }
       
  2198 
  2101 void GraphKit::round_double_result(ciMethod* dest_method) {
  2199 void GraphKit::round_double_result(ciMethod* dest_method) {
  2102   // A non-strict method may return a double value which has an extended
  2200   // A non-strict method may return a double value which has an extended
  2103   // exponent, but this must not be visible in a caller which is 'strict'
  2201   // exponent, but this must not be visible in a caller which is 'strict'
  2104   // If a strict caller invokes a non-strict callee, round a double result
  2202   // If a strict caller invokes a non-strict callee, round a double result
  2105 
  2203 
  2633 
  2731 
  2634 //------------------------maybe_cast_profiled_receiver-------------------------
  2732 //------------------------maybe_cast_profiled_receiver-------------------------
  2635 // If the profile has seen exactly one type, narrow to exactly that type.
  2733 // If the profile has seen exactly one type, narrow to exactly that type.
  2636 // Subsequent type checks will always fold up.
  2734 // Subsequent type checks will always fold up.
  2637 Node* GraphKit::maybe_cast_profiled_receiver(Node* not_null_obj,
  2735 Node* GraphKit::maybe_cast_profiled_receiver(Node* not_null_obj,
  2638                                              ciProfileData* data,
  2736                                              ciKlass* require_klass,
  2639                                              ciKlass* require_klass) {
  2737                                             ciKlass* spec_klass,
       
  2738                                              bool safe_for_replace) {
  2640   if (!UseTypeProfile || !TypeProfileCasts) return NULL;
  2739   if (!UseTypeProfile || !TypeProfileCasts) return NULL;
  2641   if (data == NULL)  return NULL;
       
  2642 
  2740 
  2643   // Make sure we haven't already deoptimized from this tactic.
  2741   // Make sure we haven't already deoptimized from this tactic.
  2644   if (too_many_traps(Deoptimization::Reason_class_check))
  2742   if (too_many_traps(Deoptimization::Reason_class_check))
  2645     return NULL;
  2743     return NULL;
  2646 
  2744 
  2647   // (No, this isn't a call, but it's enough like a virtual call
  2745   // (No, this isn't a call, but it's enough like a virtual call
  2648   // to use the same ciMethod accessor to get the profile info...)
  2746   // to use the same ciMethod accessor to get the profile info...)
  2649   ciCallProfile profile = method()->call_profile_at_bci(bci());
  2747   // If we have a speculative type use it instead of profiling (which
  2650   if (profile.count() >= 0 &&         // no cast failures here
  2748   // may not help us)
  2651       profile.has_receiver(0) &&
  2749   ciKlass* exact_kls = spec_klass == NULL ? profile_has_unique_klass() : spec_klass;
  2652       profile.morphism() == 1) {
  2750   if (exact_kls != NULL) {// no cast failures here
  2653     ciKlass* exact_kls = profile.receiver(0);
       
  2654     if (require_klass == NULL ||
  2751     if (require_klass == NULL ||
  2655         static_subtype_check(require_klass, exact_kls) == SSC_always_true) {
  2752         static_subtype_check(require_klass, exact_kls) == SSC_always_true) {
  2656       // If we narrow the type to match what the type profile sees,
  2753       // If we narrow the type to match what the type profile sees or
  2657       // we can then remove the rest of the cast.
  2754       // the speculative type, we can then remove the rest of the
       
  2755       // cast.
  2658       // This is a win, even if the exact_kls is very specific,
  2756       // This is a win, even if the exact_kls is very specific,
  2659       // because downstream operations, such as method calls,
  2757       // because downstream operations, such as method calls,
  2660       // will often benefit from the sharper type.
  2758       // will often benefit from the sharper type.
  2661       Node* exact_obj = not_null_obj; // will get updated in place...
  2759       Node* exact_obj = not_null_obj; // will get updated in place...
  2662       Node* slow_ctl  = type_check_receiver(exact_obj, exact_kls, 1.0,
  2760       Node* slow_ctl  = type_check_receiver(exact_obj, exact_kls, 1.0,
  2664       { PreserveJVMState pjvms(this);
  2762       { PreserveJVMState pjvms(this);
  2665         set_control(slow_ctl);
  2763         set_control(slow_ctl);
  2666         uncommon_trap(Deoptimization::Reason_class_check,
  2764         uncommon_trap(Deoptimization::Reason_class_check,
  2667                       Deoptimization::Action_maybe_recompile);
  2765                       Deoptimization::Action_maybe_recompile);
  2668       }
  2766       }
       
  2767       if (safe_for_replace) {
       
  2768         replace_in_map(not_null_obj, exact_obj);
       
  2769       }
       
  2770       return exact_obj;
       
  2771     }
       
  2772     // assert(ssc == SSC_always_true)... except maybe the profile lied to us.
       
  2773   }
       
  2774 
       
  2775   return NULL;
       
  2776 }
       
  2777 
       
  2778 /**
       
  2779  * Cast obj to type and emit guard unless we had too many traps here
       
  2780  * already
       
  2781  *
       
  2782  * @param obj       node being casted
       
  2783  * @param type      type to cast the node to
       
  2784  * @param not_null  true if we know node cannot be null
       
  2785  */
       
  2786 Node* GraphKit::maybe_cast_profiled_obj(Node* obj,
       
  2787                                         ciKlass* type,
       
  2788                                         bool not_null) {
       
  2789   // type == NULL if profiling tells us this object is always null
       
  2790   if (type != NULL) {
       
  2791     if (!too_many_traps(Deoptimization::Reason_null_check) &&
       
  2792         !too_many_traps(Deoptimization::Reason_class_check)) {
       
  2793       Node* not_null_obj = NULL;
       
  2794       // not_null is true if we know the object is not null and
       
  2795       // there's no need for a null check
       
  2796       if (!not_null) {
       
  2797         Node* null_ctl = top();
       
  2798         not_null_obj = null_check_oop(obj, &null_ctl, true, true);
       
  2799         assert(null_ctl->is_top(), "no null control here");
       
  2800       } else {
       
  2801         not_null_obj = obj;
       
  2802       }
       
  2803 
       
  2804       Node* exact_obj = not_null_obj;
       
  2805       ciKlass* exact_kls = type;
       
  2806       Node* slow_ctl  = type_check_receiver(exact_obj, exact_kls, 1.0,
       
  2807                                             &exact_obj);
       
  2808       {
       
  2809         PreserveJVMState pjvms(this);
       
  2810         set_control(slow_ctl);
       
  2811         uncommon_trap(Deoptimization::Reason_class_check,
       
  2812                       Deoptimization::Action_maybe_recompile);
       
  2813       }
  2669       replace_in_map(not_null_obj, exact_obj);
  2814       replace_in_map(not_null_obj, exact_obj);
  2670       return exact_obj;
  2815       obj = exact_obj;
  2671     }
  2816     }
  2672     // assert(ssc == SSC_always_true)... except maybe the profile lied to us.
  2817   } else {
  2673   }
  2818     if (!too_many_traps(Deoptimization::Reason_null_assert)) {
  2674 
  2819       Node* exact_obj = null_assert(obj);
  2675   return NULL;
  2820       replace_in_map(obj, exact_obj);
  2676 }
  2821       obj = exact_obj;
  2677 
  2822     }
       
  2823   }
       
  2824   return obj;
       
  2825 }
  2678 
  2826 
  2679 //-------------------------------gen_instanceof--------------------------------
  2827 //-------------------------------gen_instanceof--------------------------------
  2680 // Generate an instance-of idiom.  Used by both the instance-of bytecode
  2828 // Generate an instance-of idiom.  Used by both the instance-of bytecode
  2681 // and the reflective instance-of call.
  2829 // and the reflective instance-of call.
  2682 Node* GraphKit::gen_instanceof(Node* obj, Node* superklass) {
  2830 Node* GraphKit::gen_instanceof(Node* obj, Node* superklass, bool safe_for_replace) {
  2683   kill_dead_locals();           // Benefit all the uncommon traps
  2831   kill_dead_locals();           // Benefit all the uncommon traps
  2684   assert( !stopped(), "dead parse path should be checked in callers" );
  2832   assert( !stopped(), "dead parse path should be checked in callers" );
  2685   assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()),
  2833   assert(!TypePtr::NULL_PTR->higher_equal(_gvn.type(superklass)->is_klassptr()),
  2686          "must check for not-null not-dead klass in callers");
  2834          "must check for not-null not-dead klass in callers");
  2687 
  2835 
  2690   RegionNode* region = new(C) RegionNode(PATH_LIMIT);
  2838   RegionNode* region = new(C) RegionNode(PATH_LIMIT);
  2691   Node*       phi    = new(C) PhiNode(region, TypeInt::BOOL);
  2839   Node*       phi    = new(C) PhiNode(region, TypeInt::BOOL);
  2692   C->set_has_split_ifs(true); // Has chance for split-if optimization
  2840   C->set_has_split_ifs(true); // Has chance for split-if optimization
  2693 
  2841 
  2694   ciProfileData* data = NULL;
  2842   ciProfileData* data = NULL;
  2695   bool safe_for_replace = false;
       
  2696   if (java_bc() == Bytecodes::_instanceof) {  // Only for the bytecode
  2843   if (java_bc() == Bytecodes::_instanceof) {  // Only for the bytecode
  2697     data = method()->method_data()->bci_to_data(bci());
  2844     data = method()->method_data()->bci_to_data(bci());
  2698     safe_for_replace = true;
       
  2699   }
  2845   }
  2700   bool never_see_null = (ProfileDynamicTypes  // aggressive use of profile
  2846   bool never_see_null = (ProfileDynamicTypes  // aggressive use of profile
  2701                          && seems_never_null(obj, data));
  2847                          && seems_never_null(obj, data));
  2702 
  2848 
  2703   // Null check; get casted pointer; set region slot 3
  2849   // Null check; get casted pointer; set region slot 3
  2717     assert(_null_path == PATH_LIMIT-1, "delete last");
  2863     assert(_null_path == PATH_LIMIT-1, "delete last");
  2718     region->del_req(_null_path);
  2864     region->del_req(_null_path);
  2719     phi   ->del_req(_null_path);
  2865     phi   ->del_req(_null_path);
  2720   }
  2866   }
  2721 
  2867 
  2722   if (ProfileDynamicTypes && data != NULL) {
  2868   // Do we know the type check always succeed?
  2723     Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, data, NULL);
  2869   bool known_statically = false;
  2724     if (stopped()) {            // Profile disagrees with this path.
  2870   if (_gvn.type(superklass)->singleton()) {
  2725       set_control(null_ctl);    // Null is the only remaining possibility.
  2871     ciKlass* superk = _gvn.type(superklass)->is_klassptr()->klass();
  2726       return intcon(0);
  2872     ciKlass* subk = _gvn.type(obj)->is_oopptr()->klass();
  2727     }
  2873     if (subk != NULL && subk->is_loaded()) {
  2728     if (cast_obj != NULL)
  2874       int static_res = static_subtype_check(superk, subk);
  2729       not_null_obj = cast_obj;
  2875       known_statically = (static_res == SSC_always_true || static_res == SSC_always_false);
       
  2876     }
       
  2877   }
       
  2878 
       
  2879   if (known_statically && UseTypeSpeculation) {
       
  2880     // If we know the type check always succeed then we don't use the
       
  2881     // profiling data at this bytecode. Don't lose it, feed it to the
       
  2882     // type system as a speculative type.
       
  2883     not_null_obj = record_profiled_receiver_for_speculation(not_null_obj);
       
  2884   } else {
       
  2885     const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
       
  2886     // We may not have profiling here or it may not help us. If we
       
  2887     // have a speculative type use it to perform an exact cast.
       
  2888     ciKlass* spec_obj_type = obj_type->speculative_type();
       
  2889     if (spec_obj_type != NULL || (ProfileDynamicTypes && data != NULL)) {
       
  2890       Node* cast_obj = maybe_cast_profiled_receiver(not_null_obj, NULL, spec_obj_type, safe_for_replace);
       
  2891       if (stopped()) {            // Profile disagrees with this path.
       
  2892         set_control(null_ctl);    // Null is the only remaining possibility.
       
  2893         return intcon(0);
       
  2894       }
       
  2895       if (cast_obj != NULL) {
       
  2896         not_null_obj = cast_obj;
       
  2897       }
       
  2898     }
  2730   }
  2899   }
  2731 
  2900 
  2732   // Load the object's klass
  2901   // Load the object's klass
  2733   Node* obj_klass = load_object_klass(not_null_obj);
  2902   Node* obj_klass = load_object_klass(not_null_obj);
  2734 
  2903 
  2771   if (tk->singleton()) {
  2940   if (tk->singleton()) {
  2772     const TypeOopPtr* objtp = _gvn.type(obj)->isa_oopptr();
  2941     const TypeOopPtr* objtp = _gvn.type(obj)->isa_oopptr();
  2773     if (objtp != NULL && objtp->klass() != NULL) {
  2942     if (objtp != NULL && objtp->klass() != NULL) {
  2774       switch (static_subtype_check(tk->klass(), objtp->klass())) {
  2943       switch (static_subtype_check(tk->klass(), objtp->klass())) {
  2775       case SSC_always_true:
  2944       case SSC_always_true:
  2776         return obj;
  2945         // If we know the type check always succeed then we don't use
       
  2946         // the profiling data at this bytecode. Don't lose it, feed it
       
  2947         // to the type system as a speculative type.
       
  2948         return record_profiled_receiver_for_speculation(obj);
  2777       case SSC_always_false:
  2949       case SSC_always_false:
  2778         // It needs a null check because a null will *pass* the cast check.
  2950         // It needs a null check because a null will *pass* the cast check.
  2779         // A non-null value will always produce an exception.
  2951         // A non-null value will always produce an exception.
  2780         return null_assert(obj);
  2952         return null_assert(obj);
  2781       }
  2953       }
  2820     region->del_req(_null_path);
  2992     region->del_req(_null_path);
  2821     phi   ->del_req(_null_path);
  2993     phi   ->del_req(_null_path);
  2822   }
  2994   }
  2823 
  2995 
  2824   Node* cast_obj = NULL;
  2996   Node* cast_obj = NULL;
  2825   if (data != NULL &&
  2997   const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
  2826       // Counter has never been decremented (due to cast failure).
  2998   // We may not have profiling here or it may not help us. If we have
  2827       // ...This is a reasonable thing to expect.  It is true of
  2999   // a speculative type use it to perform an exact cast.
  2828       // all casts inserted by javac to implement generic types.
  3000   ciKlass* spec_obj_type = obj_type->speculative_type();
  2829       data->as_CounterData()->count() >= 0) {
  3001   if (spec_obj_type != NULL ||
  2830     cast_obj = maybe_cast_profiled_receiver(not_null_obj, data, tk->klass());
  3002       (data != NULL &&
       
  3003        // Counter has never been decremented (due to cast failure).
       
  3004        // ...This is a reasonable thing to expect.  It is true of
       
  3005        // all casts inserted by javac to implement generic types.
       
  3006        data->as_CounterData()->count() >= 0)) {
       
  3007     cast_obj = maybe_cast_profiled_receiver(not_null_obj, tk->klass(), spec_obj_type, safe_for_replace);
  2831     if (cast_obj != NULL) {
  3008     if (cast_obj != NULL) {
  2832       if (failure_control != NULL) // failure is now impossible
  3009       if (failure_control != NULL) // failure is now impossible
  2833         (*failure_control) = top();
  3010         (*failure_control) = top();
  2834       // adjust the type of the phi to the exact klass:
  3011       // adjust the type of the phi to the exact klass:
  2835       phi->raise_bottom_type(_gvn.type(cast_obj)->meet(TypePtr::NULL_PTR));
  3012       phi->raise_bottom_type(_gvn.type(cast_obj)->meet(TypePtr::NULL_PTR));