Path: blob/master/src/hotspot/share/runtime/deoptimization.hpp
40951 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_RUNTIME_DEOPTIMIZATION_HPP25#define SHARE_RUNTIME_DEOPTIMIZATION_HPP2627#include "interpreter/bytecodes.hpp"28#include "memory/allocation.hpp"29#include "runtime/frame.hpp"3031class ProfileData;32class vframeArray;33class MonitorInfo;34class MonitorValue;35class ObjectValue;36class AutoBoxObjectValue;37class ScopeValue;38class compiledVFrame;3940template<class E> class GrowableArray;4142class Deoptimization : AllStatic {43friend class VMStructs;44friend class EscapeBarrier;4546public:47// What condition caused the deoptimization?48enum DeoptReason {49Reason_many = -1, // indicates presence of several reasons50Reason_none = 0, // indicates absence of a relevant deopt.51// Next 8 reasons are recorded per bytecode in DataLayout::trap_bits.52// This is more complicated for JVMCI as JVMCI may deoptimize to *some* bytecode before the53// bytecode that actually caused the deopt (with inlining, JVMCI may even deoptimize to a54// bytecode in another method):55// - bytecode y in method b() causes deopt56// - JVMCI deoptimizes to bytecode x in method a()57// -> the deopt reason will be recorded for method a() at bytecode x58Reason_null_check, // saw unexpected null or zero divisor (@bci)59Reason_null_assert, // saw unexpected non-null or non-zero (@bci)60Reason_range_check, // saw unexpected array index (@bci)61Reason_class_check, // saw unexpected object class (@bci)62Reason_array_check, // saw unexpected array class (aastore @bci)63Reason_intrinsic, // saw unexpected operand to intrinsic (@bci)64Reason_bimorphic, // saw unexpected object class in bimorphic inlining (@bci)6566#if INCLUDE_JVMCI67Reason_unreached0 = Reason_null_assert,68Reason_type_checked_inlining = Reason_intrinsic,69Reason_optimized_type_check = Reason_bimorphic,70#endif7172Reason_profile_predicate, // compiler generated predicate moved from frequent branch in a loop failed7374// recorded per method75Reason_unloaded, // unloaded class or constant pool entry76Reason_uninitialized, // bad class state (uninitialized)77Reason_initialized, // class has been fully initialized78Reason_unreached, // code is not reached, compiler79Reason_unhandled, // arbitrary compiler limitation80Reason_constraint, // arbitrary runtime constraint violated81Reason_div0_check, // a null_check due to division by zero82Reason_age, // nmethod too old; tier threshold reached83Reason_predicate, // compiler generated predicate failed84Reason_loop_limit_check, // compiler generated loop limits check failed85Reason_speculate_class_check, // saw unexpected object class from type speculation86Reason_speculate_null_check, // saw unexpected null from type speculation87Reason_speculate_null_assert, // saw unexpected null from type speculation88Reason_rtm_state_change, // rtm state change detected89Reason_unstable_if, // a branch predicted always false was taken90Reason_unstable_fused_if, // fused two ifs that had each one untaken branch. One is now taken.91#if INCLUDE_JVMCI92Reason_aliasing, // optimistic assumption about aliasing failed93Reason_transfer_to_interpreter, // explicit transferToInterpreter()94Reason_not_compiled_exception_handler,95Reason_unresolved,96Reason_jsr_mismatch,97#endif9899// Reason_tenured is counted separately, add normal counted Reasons above.100// Related to MethodData::_trap_hist_limit where Reason_tenured isn't included101Reason_tenured, // age of the code has reached the limit102Reason_LIMIT,103104// Note: Keep this enum in sync. with _trap_reason_name.105Reason_RECORDED_LIMIT = Reason_profile_predicate // some are not recorded per bc106// Note: Reason_RECORDED_LIMIT should fit into 31 bits of107// DataLayout::trap_bits. This dependency is enforced indirectly108// via asserts, to avoid excessive direct header-to-header dependencies.109// See Deoptimization::trap_state_reason and class DataLayout.110};111112// What action must be taken by the runtime?113enum DeoptAction {114Action_none, // just interpret, do not invalidate nmethod115Action_maybe_recompile, // recompile the nmethod; need not invalidate116Action_reinterpret, // invalidate the nmethod, reset IC, maybe recompile117Action_make_not_entrant, // invalidate the nmethod, recompile (probably)118Action_make_not_compilable, // invalidate the nmethod and do not compile119Action_LIMIT120// Note: Keep this enum in sync. with _trap_action_name.121};122123enum {124_action_bits = 3,125_reason_bits = 5,126_debug_id_bits = 23,127_action_shift = 0,128_reason_shift = _action_shift+_action_bits,129_debug_id_shift = _reason_shift+_reason_bits,130BC_CASE_LIMIT = PRODUCT_ONLY(1) NOT_PRODUCT(4) // for _deoptimization_hist131};132133enum UnpackType {134Unpack_deopt = 0, // normal deoptimization, use pc computed in unpack_vframe_on_stack135Unpack_exception = 1, // exception is pending136Unpack_uncommon_trap = 2, // redo last byte code (C2 only)137Unpack_reexecute = 3, // reexecute bytecode (C1 only)138Unpack_none = 4, // not deoptimizing the frame, just reallocating/relocking for JVMTI139Unpack_LIMIT = 5140};141142#if INCLUDE_JVMCI143// Can reconstruct virtualized unsafe large accesses to byte arrays.144static const int _support_large_access_byte_array_virtualization = 1;145#endif146147// Make all nmethods that are marked_for_deoptimization not_entrant and deoptimize any live148// activations using those nmethods. If an nmethod is passed as an argument then it is149// marked_for_deoptimization and made not_entrant. Otherwise a scan of the code cache is done to150// find all marked nmethods and they are made not_entrant.151static void deoptimize_all_marked(nmethod* nmethod_only = NULL);152153private:154// Revoke biased locks at deopt.155static void revoke_from_deopt_handler(JavaThread* thread, frame fr, RegisterMap* map);156157static void revoke_for_object_deoptimization(JavaThread* deoptee_thread, frame fr,158RegisterMap* map, JavaThread* thread);159160public:161// Deoptimizes a frame lazily. Deopt happens on return to the frame.162static void deoptimize(JavaThread* thread, frame fr, DeoptReason reason = Reason_constraint);163164#if INCLUDE_JVMCI165static address deoptimize_for_missing_exception_handler(CompiledMethod* cm);166#endif167168static oop get_cached_box(AutoBoxObjectValue* bv, frame* fr, RegisterMap* reg_map, TRAPS);169170private:171// Does the actual work for deoptimizing a single frame172static void deoptimize_single_frame(JavaThread* thread, frame fr, DeoptReason reason);173174#if COMPILER2_OR_JVMCI175// Deoptimize objects, that is reallocate and relock them, just before they176// escape through JVMTI. The given vframes cover one physical frame.177static bool deoptimize_objects_internal(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk,178bool& realloc_failures);179180public:181182// Support for restoring non-escaping objects183static bool realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS);184static void reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type);185static void reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj);186static void reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, bool skip_internal);187static bool relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,188JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures);189static void pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array);190NOT_PRODUCT(static void print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures);)191#endif // COMPILER2_OR_JVMCI192193public:194static vframeArray* create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures);195196// Interface used for unpacking deoptimized frames197198// UnrollBlock is returned by fetch_unroll_info() to the deoptimization handler (blob).199// This is only a CheapObj to ease debugging after a deopt failure200class UnrollBlock : public CHeapObj<mtCompiler> {201friend class VMStructs;202friend class JVMCIVMStructs;203private:204int _size_of_deoptimized_frame; // Size, in bytes, of current deoptimized frame205int _caller_adjustment; // Adjustment, in bytes, to caller's SP by initial interpreted frame206int _number_of_frames; // Number frames to unroll207int _total_frame_sizes; // Total of number*sizes frames208intptr_t* _frame_sizes; // Array of frame sizes, in bytes, for unrolling the stack209address* _frame_pcs; // Array of frame pc's, in bytes, for unrolling the stack210intptr_t* _register_block; // Block for storing callee-saved registers.211BasicType _return_type; // Tells if we have to restore double or long return value212intptr_t _initial_info; // Platform dependent data for the sender frame (was FP on x86)213int _caller_actual_parameters; // The number of actual arguments at the214// interpreted caller of the deoptimized frame215int _unpack_kind; // exec_mode that can be changed during fetch_unroll_info216217// The following fields are used as temps during the unpacking phase218// (which is tight on registers, especially on x86). They really ought219// to be PD variables but that involves moving this class into its own220// file to use the pd include mechanism. Maybe in a later cleanup ...221intptr_t _counter_temp; // SHOULD BE PD VARIABLE (x86 frame count temp)222intptr_t _sender_sp_temp; // SHOULD BE PD VARIABLE (x86 sender_sp)223public:224// Constructor225UnrollBlock(int size_of_deoptimized_frame,226int caller_adjustment,227int caller_actual_parameters,228int number_of_frames,229intptr_t* frame_sizes,230address* frames_pcs,231BasicType return_type,232int unpack_kind);233~UnrollBlock();234235// Returns where a register is located.236intptr_t* value_addr_at(int register_number) const;237238// Accessors239intptr_t* frame_sizes() const { return _frame_sizes; }240int number_of_frames() const { return _number_of_frames; }241address* frame_pcs() const { return _frame_pcs ; }242int unpack_kind() const { return _unpack_kind; }243244// Returns the total size of frames245int size_of_frames() const;246247void set_initial_info(intptr_t info) { _initial_info = info; }248249int caller_actual_parameters() const { return _caller_actual_parameters; }250251// Accessors used by the code generator for the unpack stub.252static int size_of_deoptimized_frame_offset_in_bytes() { return offset_of(UnrollBlock, _size_of_deoptimized_frame); }253static int caller_adjustment_offset_in_bytes() { return offset_of(UnrollBlock, _caller_adjustment); }254static int number_of_frames_offset_in_bytes() { return offset_of(UnrollBlock, _number_of_frames); }255static int frame_sizes_offset_in_bytes() { return offset_of(UnrollBlock, _frame_sizes); }256static int total_frame_sizes_offset_in_bytes() { return offset_of(UnrollBlock, _total_frame_sizes); }257static int frame_pcs_offset_in_bytes() { return offset_of(UnrollBlock, _frame_pcs); }258static int register_block_offset_in_bytes() { return offset_of(UnrollBlock, _register_block); }259static int return_type_offset_in_bytes() { return offset_of(UnrollBlock, _return_type); }260static int counter_temp_offset_in_bytes() { return offset_of(UnrollBlock, _counter_temp); }261static int initial_info_offset_in_bytes() { return offset_of(UnrollBlock, _initial_info); }262static int unpack_kind_offset_in_bytes() { return offset_of(UnrollBlock, _unpack_kind); }263static int sender_sp_temp_offset_in_bytes() { return offset_of(UnrollBlock, _sender_sp_temp); }264265BasicType return_type() const { return _return_type; }266void print();267};268269//** Returns an UnrollBlock continuing information270// how to make room for the resulting interpreter frames.271// Called by assembly stub after execution has returned to272// deoptimized frame.273// @argument thread. Thread where stub_frame resides.274// @see OptoRuntime::deoptimization_fetch_unroll_info_C275static UnrollBlock* fetch_unroll_info(JavaThread* current, int exec_mode);276277//** Unpacks vframeArray onto execution stack278// Called by assembly stub after execution has returned to279// deoptimized frame and after the stack unrolling.280// @argument thread. Thread where stub_frame resides.281// @argument exec_mode. Determines how execution should be continued in top frame.282// 0 means continue after current byte code283// 1 means exception has happened, handle exception284// 2 means reexecute current bytecode (for uncommon traps).285// @see OptoRuntime::deoptimization_unpack_frames_C286// Return BasicType of call return type, if any287static BasicType unpack_frames(JavaThread* thread, int exec_mode);288289// Cleans up deoptimization bits on thread after unpacking or in the290// case of an exception.291static void cleanup_deopt_info(JavaThread *thread,292vframeArray * array);293294// Restores callee saved values from deoptimized frame into oldest interpreter frame295// so caller of the deoptimized frame will get back the values it expects.296static void unwind_callee_save_values(frame* f, vframeArray* vframe_array);297298//** Performs an uncommon trap for compiled code.299// The top most compiler frame is converted into interpreter frames300static UnrollBlock* uncommon_trap(JavaThread* current, jint unloaded_class_index, jint exec_mode);301// Helper routine that enters the VM and may block302static void uncommon_trap_inner(JavaThread* current, jint unloaded_class_index);303304//** Deoptimizes the frame identified by id.305// Only called from VMDeoptimizeFrame306// @argument thread. Thread where stub_frame resides.307// @argument id. id of frame that should be deoptimized.308static void deoptimize_frame_internal(JavaThread* thread, intptr_t* id, DeoptReason reason);309310// if thread is not the current thread then execute311// VM_DeoptimizeFrame otherwise deoptimize directly.312static void deoptimize_frame(JavaThread* thread, intptr_t* id, DeoptReason reason);313static void deoptimize_frame(JavaThread* thread, intptr_t* id);314315// Statistics316static void gather_statistics(DeoptReason reason, DeoptAction action,317Bytecodes::Code bc = Bytecodes::_illegal);318static void print_statistics();319320// How much room to adjust the last frame's SP by, to make space for321// the callee's interpreter frame (which expects locals to be next to322// incoming arguments)323static int last_frame_adjust(int callee_parameters, int callee_locals);324325// trap_request codes326static DeoptReason trap_request_reason(int trap_request) {327if (trap_request < 0)328return (DeoptReason)329((~(trap_request) >> _reason_shift) & right_n_bits(_reason_bits));330else331// standard reason for unloaded CP entry332return Reason_unloaded;333}334static DeoptAction trap_request_action(int trap_request) {335if (trap_request < 0)336return (DeoptAction)337((~(trap_request) >> _action_shift) & right_n_bits(_action_bits));338else339// standard action for unloaded CP entry340return _unloaded_action;341}342static int trap_request_debug_id(int trap_request) {343if (trap_request < 0) {344return ((~(trap_request) >> _debug_id_shift) & right_n_bits(_debug_id_bits));345} else {346// standard action for unloaded CP entry347return 0;348}349}350static int trap_request_index(int trap_request) {351if (trap_request < 0)352return -1;353else354return trap_request;355}356static int make_trap_request(DeoptReason reason, DeoptAction action,357int index = -1) {358assert((1 << _reason_bits) >= Reason_LIMIT, "enough bits");359assert((1 << _action_bits) >= Action_LIMIT, "enough bits");360int trap_request;361if (index != -1)362trap_request = index;363else364trap_request = (~(((reason) << _reason_shift)365+ ((action) << _action_shift)));366assert(reason == trap_request_reason(trap_request), "valid reason");367assert(action == trap_request_action(trap_request), "valid action");368assert(index == trap_request_index(trap_request), "valid index");369return trap_request;370}371372// The trap_state stored in a MDO is decoded here.373// It records two items of information.374// reason: If a deoptimization happened here, what its reason was,375// or if there were multiple deopts with differing reasons.376// recompiled: If a deoptimization here triggered a recompilation.377// Note that not all reasons are recorded per-bci.378static DeoptReason trap_state_reason(int trap_state);379static int trap_state_has_reason(int trap_state, int reason);380static int trap_state_add_reason(int trap_state, int reason);381static bool trap_state_is_recompiled(int trap_state);382static int trap_state_set_recompiled(int trap_state, bool z);383static const char* format_trap_state(char* buf, size_t buflen,384int trap_state);385386static bool reason_is_recorded_per_bytecode(DeoptReason reason) {387return reason > Reason_none && reason <= Reason_RECORDED_LIMIT;388}389390static DeoptReason reason_recorded_per_bytecode_if_any(DeoptReason reason) {391if (reason_is_recorded_per_bytecode(reason))392return reason;393else if (reason == Reason_div0_check) // null check due to divide-by-zero?394return Reason_null_check; // recorded per BCI as a null check395else if (reason == Reason_speculate_class_check)396return Reason_class_check;397else if (reason == Reason_speculate_null_check)398return Reason_null_check;399else if (reason == Reason_speculate_null_assert)400return Reason_null_assert;401else if (reason == Reason_unstable_if)402return Reason_intrinsic;403else if (reason == Reason_unstable_fused_if)404return Reason_range_check;405else406return Reason_none;407}408409static bool reason_is_speculate(int reason) {410if (reason == Reason_speculate_class_check ||411reason == Reason_speculate_null_check ||412reason == Reason_speculate_null_assert) {413return true;414}415return false;416}417418static DeoptReason reason_null_check(bool speculative) {419return speculative ? Deoptimization::Reason_speculate_null_check : Deoptimization::Reason_null_check;420}421422static DeoptReason reason_class_check(bool speculative) {423return speculative ? Deoptimization::Reason_speculate_class_check : Deoptimization::Reason_class_check;424}425426static DeoptReason reason_null_assert(bool speculative) {427return speculative ? Deoptimization::Reason_speculate_null_assert : Deoptimization::Reason_null_assert;428}429430static uint per_method_trap_limit(int reason) {431return reason_is_speculate(reason) ? (uint)PerMethodSpecTrapLimit : (uint)PerMethodTrapLimit;432}433434static const char* trap_reason_name(int reason);435static const char* trap_action_name(int action);436// Format like reason='foo' action='bar' index='123'.437// This is suitable both for XML and for tty output.438static const char* format_trap_request(char* buf, size_t buflen,439int trap_request);440441static jint total_deoptimization_count();442443// JVMTI PopFrame support444445// Preserves incoming arguments to the popped frame when it is446// returning to a deoptimized caller447static void popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address);448449static MethodData* get_method_data(JavaThread* thread, const methodHandle& m, bool create_if_missing);450private:451// Update the mdo's count and per-BCI reason bits, returning previous state:452static ProfileData* query_update_method_data(MethodData* trap_mdo,453int trap_bci,454DeoptReason reason,455bool update_total_trap_count,456#if INCLUDE_JVMCI457bool is_osr,458#endif459Method* compiled_method,460//outputs:461uint& ret_this_trap_count,462bool& ret_maybe_prior_trap,463bool& ret_maybe_prior_recompile);464// class loading support for uncommon trap465static void load_class_by_index(const constantPoolHandle& constant_pool, int index, TRAPS);466467static UnrollBlock* fetch_unroll_info_helper(JavaThread* current, int exec_mode);468469static DeoptAction _unloaded_action; // == Action_reinterpret;470static const char* _trap_reason_name[];471static const char* _trap_action_name[];472473static juint _deoptimization_hist[Reason_LIMIT][1+Action_LIMIT][BC_CASE_LIMIT];474// Note: Histogram array size is 1-2 Kb.475476public:477static void update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason);478};479480481class DeoptimizationMarker : StackObj { // for profiling482static bool _is_active;483public:484DeoptimizationMarker() { _is_active = true; }485~DeoptimizationMarker() { _is_active = false; }486static bool is_active() { return _is_active; }487};488489#endif // SHARE_RUNTIME_DEOPTIMIZATION_HPP490491492