src/share/vm/runtime/deoptimization.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/deoptimization.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,385 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_RUNTIME_DEOPTIMIZATION_HPP
    1.29 +#define SHARE_VM_RUNTIME_DEOPTIMIZATION_HPP
    1.30 +
    1.31 +#include "memory/allocation.hpp"
    1.32 +#include "runtime/frame.inline.hpp"
    1.33 +
    1.34 +class ProfileData;
    1.35 +class vframeArray;
    1.36 +class MonitorValue;
    1.37 +class ObjectValue;
    1.38 +
    1.39 +class Deoptimization : AllStatic {
    1.40 +  friend class VMStructs;
    1.41 +
    1.42 + public:
    1.43 +  // What condition caused the deoptimization?
    1.44 +  enum DeoptReason {
    1.45 +    Reason_many = -1,             // indicates presence of several reasons
    1.46 +    Reason_none = 0,              // indicates absence of a relevant deopt.
    1.47 +    // Next 7 reasons are recorded per bytecode in DataLayout::trap_bits
    1.48 +    Reason_null_check,            // saw unexpected null or zero divisor (@bci)
    1.49 +    Reason_null_assert,           // saw unexpected non-null or non-zero (@bci)
    1.50 +    Reason_range_check,           // saw unexpected array index (@bci)
    1.51 +    Reason_class_check,           // saw unexpected object class (@bci)
    1.52 +    Reason_array_check,           // saw unexpected array class (aastore @bci)
    1.53 +    Reason_intrinsic,             // saw unexpected operand to intrinsic (@bci)
    1.54 +    Reason_bimorphic,             // saw unexpected object class in bimorphic inlining (@bci)
    1.55 +
    1.56 +    Reason_unloaded,              // unloaded class or constant pool entry
    1.57 +    Reason_uninitialized,         // bad class state (uninitialized)
    1.58 +    Reason_unreached,             // code is not reached, compiler
    1.59 +    Reason_unhandled,             // arbitrary compiler limitation
    1.60 +    Reason_constraint,            // arbitrary runtime constraint violated
    1.61 +    Reason_div0_check,            // a null_check due to division by zero
    1.62 +    Reason_age,                   // nmethod too old; tier threshold reached
    1.63 +    Reason_predicate,             // compiler generated predicate failed
    1.64 +    Reason_loop_limit_check,      // compiler generated loop limits check failed
    1.65 +    Reason_speculate_class_check, // saw unexpected object class from type speculation
    1.66 +    Reason_rtm_state_change,      // rtm state change detected
    1.67 +    Reason_LIMIT,
    1.68 +    // Note:  Keep this enum in sync. with _trap_reason_name.
    1.69 +    Reason_RECORDED_LIMIT = Reason_bimorphic  // some are not recorded per bc
    1.70 +    // Note:  Reason_RECORDED_LIMIT should be < 8 to fit into 3 bits of
    1.71 +    // DataLayout::trap_bits.  This dependency is enforced indirectly
    1.72 +    // via asserts, to avoid excessive direct header-to-header dependencies.
    1.73 +    // See Deoptimization::trap_state_reason and class DataLayout.
    1.74 +  };
    1.75 +
    1.76 +  // What action must be taken by the runtime?
    1.77 +  enum DeoptAction {
    1.78 +    Action_none,                  // just interpret, do not invalidate nmethod
    1.79 +    Action_maybe_recompile,       // recompile the nmethod; need not invalidate
    1.80 +    Action_reinterpret,           // invalidate the nmethod, reset IC, maybe recompile
    1.81 +    Action_make_not_entrant,      // invalidate the nmethod, recompile (probably)
    1.82 +    Action_make_not_compilable,   // invalidate the nmethod and do not compile
    1.83 +    Action_LIMIT
    1.84 +    // Note:  Keep this enum in sync. with _trap_action_name.
    1.85 +  };
    1.86 +
    1.87 +  enum {
    1.88 +    _action_bits = 3,
    1.89 +    _reason_bits = 5,
    1.90 +    _action_shift = 0,
    1.91 +    _reason_shift = _action_shift+_action_bits,
    1.92 +    BC_CASE_LIMIT = PRODUCT_ONLY(1) NOT_PRODUCT(4) // for _deoptimization_hist
    1.93 +  };
    1.94 +
    1.95 +  enum UnpackType {
    1.96 +    Unpack_deopt                = 0, // normal deoptimization, use pc computed in unpack_vframe_on_stack
    1.97 +    Unpack_exception            = 1, // exception is pending
    1.98 +    Unpack_uncommon_trap        = 2, // redo last byte code (C2 only)
    1.99 +    Unpack_reexecute            = 3  // reexecute bytecode (C1 only)
   1.100 +  };
   1.101 +
   1.102 +  // Checks all compiled methods. Invalid methods are deleted and
   1.103 +  // corresponding activations are deoptimized.
   1.104 +  static int deoptimize_dependents();
   1.105 +
   1.106 +  // Deoptimizes a frame lazily. nmethod gets patched deopt happens on return to the frame
   1.107 +  static void deoptimize(JavaThread* thread, frame fr, RegisterMap *reg_map);
   1.108 +
   1.109 +  private:
   1.110 +  // Does the actual work for deoptimizing a single frame
   1.111 +  static void deoptimize_single_frame(JavaThread* thread, frame fr);
   1.112 +
   1.113 +  // Helper function to revoke biases of all monitors in frame if UseBiasedLocking
   1.114 +  // is enabled
   1.115 +  static void revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map);
   1.116 +  // Helper function to revoke biases of all monitors in frames
   1.117 +  // executing in a particular CodeBlob if UseBiasedLocking is enabled
   1.118 +  static void revoke_biases_of_monitors(CodeBlob* cb);
   1.119 +
   1.120 +#ifdef COMPILER2
   1.121 +  // Support for restoring non-escaping objects
   1.122 +  static bool realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS);
   1.123 +  static void reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type);
   1.124 +  static void reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj);
   1.125 +  static void reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects);
   1.126 +  static void relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread);
   1.127 +  NOT_PRODUCT(static void print_objects(GrowableArray<ScopeValue*>* objects);)
   1.128 +#endif // COMPILER2
   1.129 +
   1.130 +  public:
   1.131 +  static vframeArray* create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk);
   1.132 +
   1.133 +  // Interface used for unpacking deoptimized frames
   1.134 +
   1.135 +  // UnrollBlock is returned by fetch_unroll_info() to the deoptimization handler (blob).
   1.136 +  // This is only a CheapObj to ease debugging after a deopt failure
   1.137 +  class UnrollBlock : public CHeapObj<mtCompiler> {
   1.138 +   private:
   1.139 +    int       _size_of_deoptimized_frame; // Size, in bytes, of current deoptimized frame
   1.140 +    int       _caller_adjustment;         // Adjustment, in bytes, to caller's SP by initial interpreted frame
   1.141 +    int       _number_of_frames;          // Number frames to unroll
   1.142 +    int       _total_frame_sizes;         // Total of number*sizes frames
   1.143 +    intptr_t* _frame_sizes;               // Array of frame sizes, in bytes, for unrolling the stack
   1.144 +    address*  _frame_pcs;                 // Array of frame pc's, in bytes, for unrolling the stack
   1.145 +    intptr_t* _register_block;            // Block for storing callee-saved registers.
   1.146 +    BasicType _return_type;               // Tells if we have to restore double or long return value
   1.147 +    intptr_t  _initial_info;              // Platform dependent data for the sender frame (was FP on x86)
   1.148 +    int       _caller_actual_parameters;  // The number of actual arguments at the
   1.149 +                                          // interpreted caller of the deoptimized frame
   1.150 +
   1.151 +    // The following fields are used as temps during the unpacking phase
   1.152 +    // (which is tight on registers, especially on x86). They really ought
   1.153 +    // to be PD variables but that involves moving this class into its own
   1.154 +    // file to use the pd include mechanism. Maybe in a later cleanup ...
   1.155 +    intptr_t  _counter_temp;              // SHOULD BE PD VARIABLE (x86 frame count temp)
   1.156 +    intptr_t  _unpack_kind;               // SHOULD BE PD VARIABLE (x86 unpack kind)
   1.157 +    intptr_t  _sender_sp_temp;            // SHOULD BE PD VARIABLE (x86 sender_sp)
   1.158 +   public:
   1.159 +    // Constructor
   1.160 +    UnrollBlock(int  size_of_deoptimized_frame,
   1.161 +                int  caller_adjustment,
   1.162 +                int  caller_actual_parameters,
   1.163 +                int  number_of_frames,
   1.164 +                intptr_t* frame_sizes,
   1.165 +                address* frames_pcs,
   1.166 +                BasicType return_type);
   1.167 +    ~UnrollBlock();
   1.168 +
   1.169 +    // Returns where a register is located.
   1.170 +    intptr_t* value_addr_at(int register_number) const;
   1.171 +
   1.172 +    // Accessors
   1.173 +    intptr_t* frame_sizes()  const { return _frame_sizes; }
   1.174 +    int number_of_frames()  const { return _number_of_frames; }
   1.175 +    address*  frame_pcs()   const { return _frame_pcs ; }
   1.176 +
   1.177 +    // Returns the total size of frames
   1.178 +    int size_of_frames() const;
   1.179 +
   1.180 +    void set_initial_info(intptr_t info) { _initial_info = info; }
   1.181 +
   1.182 +    int caller_actual_parameters() const { return _caller_actual_parameters; }
   1.183 +
   1.184 +    // Accessors used by the code generator for the unpack stub.
   1.185 +    static int size_of_deoptimized_frame_offset_in_bytes() { return offset_of(UnrollBlock, _size_of_deoptimized_frame); }
   1.186 +    static int caller_adjustment_offset_in_bytes()         { return offset_of(UnrollBlock, _caller_adjustment);         }
   1.187 +    static int number_of_frames_offset_in_bytes()          { return offset_of(UnrollBlock, _number_of_frames);          }
   1.188 +    static int frame_sizes_offset_in_bytes()               { return offset_of(UnrollBlock, _frame_sizes);               }
   1.189 +    static int total_frame_sizes_offset_in_bytes()         { return offset_of(UnrollBlock, _total_frame_sizes);         }
   1.190 +    static int frame_pcs_offset_in_bytes()                 { return offset_of(UnrollBlock, _frame_pcs);                 }
   1.191 +    static int register_block_offset_in_bytes()            { return offset_of(UnrollBlock, _register_block);            }
   1.192 +    static int return_type_offset_in_bytes()               { return offset_of(UnrollBlock, _return_type);               }
   1.193 +    static int counter_temp_offset_in_bytes()              { return offset_of(UnrollBlock, _counter_temp);              }
   1.194 +    static int initial_info_offset_in_bytes()              { return offset_of(UnrollBlock, _initial_info);              }
   1.195 +    static int unpack_kind_offset_in_bytes()               { return offset_of(UnrollBlock, _unpack_kind);               }
   1.196 +    static int sender_sp_temp_offset_in_bytes()            { return offset_of(UnrollBlock, _sender_sp_temp);            }
   1.197 +
   1.198 +    BasicType return_type() const { return _return_type; }
   1.199 +    void print();
   1.200 +  };
   1.201 +
   1.202 +  //** Returns an UnrollBlock continuing information
   1.203 +  // how to make room for the resulting interpreter frames.
   1.204 +  // Called by assembly stub after execution has returned to
   1.205 +  // deoptimized frame.
   1.206 +  // @argument thread.     Thread where stub_frame resides.
   1.207 +  // @see OptoRuntime::deoptimization_fetch_unroll_info_C
   1.208 +  static UnrollBlock* fetch_unroll_info(JavaThread* thread);
   1.209 +
   1.210 +  //** Unpacks vframeArray onto execution stack
   1.211 +  // Called by assembly stub after execution has returned to
   1.212 +  // deoptimized frame and after the stack unrolling.
   1.213 +  // @argument thread.     Thread where stub_frame resides.
   1.214 +  // @argument exec_mode.  Determines how execution should be continuted in top frame.
   1.215 +  //                       0 means continue after current byte code
   1.216 +  //                       1 means exception has happened, handle exception
   1.217 +  //                       2 means reexecute current bytecode (for uncommon traps).
   1.218 +  // @see OptoRuntime::deoptimization_unpack_frames_C
   1.219 +  // Return BasicType of call return type, if any
   1.220 +  static BasicType unpack_frames(JavaThread* thread, int exec_mode);
   1.221 +
   1.222 +  // Cleans up deoptimization bits on thread after unpacking or in the
   1.223 +  // case of an exception.
   1.224 +  static void cleanup_deopt_info(JavaThread  *thread,
   1.225 +                                 vframeArray * array);
   1.226 +
   1.227 +  // Restores callee saved values from deoptimized frame into oldest interpreter frame
   1.228 +  // so caller of the deoptimized frame will get back the values it expects.
   1.229 +  static void unwind_callee_save_values(frame* f, vframeArray* vframe_array);
   1.230 +
   1.231 +  //** Performs an uncommon trap for compiled code.
   1.232 +  // The top most compiler frame is converted into interpreter frames
   1.233 +  static UnrollBlock* uncommon_trap(JavaThread* thread, jint unloaded_class_index);
   1.234 +  // Helper routine that enters the VM and may block
   1.235 +  static void uncommon_trap_inner(JavaThread* thread, jint unloaded_class_index);
   1.236 +
   1.237 +  //** Deoptimizes the frame identified by id.
   1.238 +  // Only called from VMDeoptimizeFrame
   1.239 +  // @argument thread.     Thread where stub_frame resides.
   1.240 +  // @argument id.         id of frame that should be deoptimized.
   1.241 +  static void deoptimize_frame_internal(JavaThread* thread, intptr_t* id);
   1.242 +
   1.243 +  // If thread is not the current thread then execute
   1.244 +  // VM_DeoptimizeFrame otherwise deoptimize directly.
   1.245 +  static void deoptimize_frame(JavaThread* thread, intptr_t* id);
   1.246 +
   1.247 +  // Statistics
   1.248 +  static void gather_statistics(DeoptReason reason, DeoptAction action,
   1.249 +                                Bytecodes::Code bc = Bytecodes::_illegal);
   1.250 +  static void print_statistics();
   1.251 +
   1.252 +  // How much room to adjust the last frame's SP by, to make space for
   1.253 +  // the callee's interpreter frame (which expects locals to be next to
   1.254 +  // incoming arguments)
   1.255 +  static int last_frame_adjust(int callee_parameters, int callee_locals);
   1.256 +
   1.257 +  // trap_request codes
   1.258 +  static DeoptReason trap_request_reason(int trap_request) {
   1.259 +    if (trap_request < 0)
   1.260 +      return (DeoptReason)
   1.261 +        ((~(trap_request) >> _reason_shift) & right_n_bits(_reason_bits));
   1.262 +    else
   1.263 +      // standard reason for unloaded CP entry
   1.264 +      return Reason_unloaded;
   1.265 +  }
   1.266 +  static DeoptAction trap_request_action(int trap_request) {
   1.267 +    if (trap_request < 0)
   1.268 +      return (DeoptAction)
   1.269 +        ((~(trap_request) >> _action_shift) & right_n_bits(_action_bits));
   1.270 +    else
   1.271 +      // standard action for unloaded CP entry
   1.272 +      return _unloaded_action;
   1.273 +  }
   1.274 +  static int trap_request_index(int trap_request) {
   1.275 +    if (trap_request < 0)
   1.276 +      return -1;
   1.277 +    else
   1.278 +      return trap_request;
   1.279 +  }
   1.280 +  static int make_trap_request(DeoptReason reason, DeoptAction action,
   1.281 +                               int index = -1) {
   1.282 +    assert((1 << _reason_bits) >= Reason_LIMIT, "enough bits");
   1.283 +    assert((1 << _action_bits) >= Action_LIMIT, "enough bits");
   1.284 +    int trap_request;
   1.285 +    if (index != -1)
   1.286 +      trap_request = index;
   1.287 +    else
   1.288 +      trap_request = (~(((reason) << _reason_shift)
   1.289 +                        + ((action) << _action_shift)));
   1.290 +    assert(reason == trap_request_reason(trap_request), "valid reason");
   1.291 +    assert(action == trap_request_action(trap_request), "valid action");
   1.292 +    assert(index  == trap_request_index(trap_request),  "valid index");
   1.293 +    return trap_request;
   1.294 +  }
   1.295 +
   1.296 +  // The trap_state stored in a MDO is decoded here.
   1.297 +  // It records two items of information.
   1.298 +  //  reason:  If a deoptimization happened here, what its reason was,
   1.299 +  //           or if there were multiple deopts with differing reasons.
   1.300 +  //  recompiled: If a deoptimization here triggered a recompilation.
   1.301 +  // Note that not all reasons are recorded per-bci.
   1.302 +  static DeoptReason trap_state_reason(int trap_state);
   1.303 +  static int  trap_state_has_reason(int trap_state, int reason);
   1.304 +  static int  trap_state_add_reason(int trap_state, int reason);
   1.305 +  static bool trap_state_is_recompiled(int trap_state);
   1.306 +  static int  trap_state_set_recompiled(int trap_state, bool z);
   1.307 +  static const char* format_trap_state(char* buf, size_t buflen,
   1.308 +                                       int trap_state);
   1.309 +
   1.310 +  static bool reason_is_recorded_per_bytecode(DeoptReason reason) {
   1.311 +    return reason > Reason_none && reason <= Reason_RECORDED_LIMIT;
   1.312 +  }
   1.313 +
   1.314 +  static DeoptReason reason_recorded_per_bytecode_if_any(DeoptReason reason) {
   1.315 +    if (reason_is_recorded_per_bytecode(reason))
   1.316 +      return reason;
   1.317 +    else if (reason == Reason_div0_check) // null check due to divide-by-zero?
   1.318 +      return Reason_null_check;           // recorded per BCI as a null check
   1.319 +    else if (reason == Reason_speculate_class_check)
   1.320 +      return Reason_class_check;
   1.321 +    else
   1.322 +      return Reason_none;
   1.323 +  }
   1.324 +
   1.325 +  static bool reason_is_speculate(int reason) {
   1.326 +    if (reason == Reason_speculate_class_check) {
   1.327 +      return true;
   1.328 +    }
   1.329 +    return false;
   1.330 +  }
   1.331 +
   1.332 +  static uint per_method_trap_limit(int reason) {
   1.333 +    return reason_is_speculate(reason) ? (uint)PerMethodSpecTrapLimit : (uint)PerMethodTrapLimit;
   1.334 +  }
   1.335 +
   1.336 +  static const char* trap_reason_name(int reason);
   1.337 +  static const char* trap_action_name(int action);
   1.338 +  // Format like reason='foo' action='bar' index='123'.
   1.339 +  // This is suitable both for XML and for tty output.
   1.340 +  static const char* format_trap_request(char* buf, size_t buflen,
   1.341 +                                         int trap_request);
   1.342 +
   1.343 +  static jint total_deoptimization_count();
   1.344 +  static jint deoptimization_count(DeoptReason reason);
   1.345 +
   1.346 +  // JVMTI PopFrame support
   1.347 +
   1.348 +  // Preserves incoming arguments to the popped frame when it is
   1.349 +  // returning to a deoptimized caller
   1.350 +  static void popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address);
   1.351 +
   1.352 + private:
   1.353 +  static MethodData* get_method_data(JavaThread* thread, methodHandle m, bool create_if_missing);
   1.354 +  // Update the mdo's count and per-BCI reason bits, returning previous state:
   1.355 +  static ProfileData* query_update_method_data(MethodData* trap_mdo,
   1.356 +                                               int trap_bci,
   1.357 +                                               DeoptReason reason,
   1.358 +                                               Method* compiled_method,
   1.359 +                                               //outputs:
   1.360 +                                               uint& ret_this_trap_count,
   1.361 +                                               bool& ret_maybe_prior_trap,
   1.362 +                                               bool& ret_maybe_prior_recompile);
   1.363 +  // class loading support for uncommon trap
   1.364 +  static void load_class_by_index(constantPoolHandle constant_pool, int index, TRAPS);
   1.365 +  static void load_class_by_index(constantPoolHandle constant_pool, int index);
   1.366 +
   1.367 +  static UnrollBlock* fetch_unroll_info_helper(JavaThread* thread);
   1.368 +
   1.369 +  static DeoptAction _unloaded_action; // == Action_reinterpret;
   1.370 +  static const char* _trap_reason_name[Reason_LIMIT];
   1.371 +  static const char* _trap_action_name[Action_LIMIT];
   1.372 +
   1.373 +  static juint _deoptimization_hist[Reason_LIMIT][1+Action_LIMIT][BC_CASE_LIMIT];
   1.374 +  // Note:  Histogram array size is 1-2 Kb.
   1.375 +
   1.376 + public:
   1.377 +  static void update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason);
   1.378 +};
   1.379 +
   1.380 +class DeoptimizationMarker : StackObj {  // for profiling
   1.381 +  static bool _is_active;
   1.382 +public:
   1.383 +  DeoptimizationMarker()  { _is_active = true; }
   1.384 +  ~DeoptimizationMarker() { _is_active = false; }
   1.385 +  static bool is_active() { return _is_active; }
   1.386 +};
   1.387 +
   1.388 +#endif // SHARE_VM_RUNTIME_DEOPTIMIZATION_HPP

mercurial