Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/deoptimization.cpp
32285 views
/*1* Copyright (c) 1997, 2016, 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#include "precompiled.hpp"25#include "classfile/systemDictionary.hpp"26#include "code/debugInfoRec.hpp"27#include "code/nmethod.hpp"28#include "code/pcDesc.hpp"29#include "code/scopeDesc.hpp"30#include "interpreter/bytecode.hpp"31#include "interpreter/interpreter.hpp"32#include "interpreter/oopMapCache.hpp"33#include "memory/allocation.inline.hpp"34#include "memory/oopFactory.hpp"35#include "memory/resourceArea.hpp"36#include "oops/method.hpp"37#include "oops/oop.inline.hpp"38#include "prims/jvmtiThreadState.hpp"39#include "runtime/biasedLocking.hpp"40#include "runtime/compilationPolicy.hpp"41#include "runtime/deoptimization.hpp"42#include "runtime/interfaceSupport.hpp"43#include "runtime/sharedRuntime.hpp"44#include "runtime/signature.hpp"45#include "runtime/stubRoutines.hpp"46#include "runtime/thread.hpp"47#include "runtime/threadWXSetters.inline.hpp"48#include "runtime/vframe.hpp"49#include "runtime/vframeArray.hpp"50#include "runtime/vframe_hp.hpp"51#include "utilities/events.hpp"52#include "utilities/xmlstream.hpp"53#ifdef TARGET_ARCH_x8654# include "vmreg_x86.inline.hpp"55#endif56#ifdef TARGET_ARCH_aarch3257# include "vmreg_aarch32.inline.hpp"58#endif59#ifdef TARGET_ARCH_aarch6460# include "vmreg_aarch64.inline.hpp"61#endif62#ifdef TARGET_ARCH_sparc63# include "vmreg_sparc.inline.hpp"64#endif65#ifdef TARGET_ARCH_zero66# include "vmreg_zero.inline.hpp"67#endif68#ifdef TARGET_ARCH_arm69# include "vmreg_arm.inline.hpp"70#endif71#ifdef TARGET_ARCH_ppc72# include "vmreg_ppc.inline.hpp"73#endif74#ifdef COMPILER275#if defined AD_MD_HPP76# include AD_MD_HPP77#elif defined TARGET_ARCH_MODEL_x86_3278# include "adfiles/ad_x86_32.hpp"79#elif defined TARGET_ARCH_MODEL_x86_6480# include "adfiles/ad_x86_64.hpp"81#elif defined TARGET_ARCH_MODEL_aarch3282# include "adfiles/ad_aarch32.hpp"83#elif defined TARGET_ARCH_MODEL_aarch6484# include "adfiles/ad_aarch64.hpp"85#elif defined TARGET_ARCH_MODEL_sparc86# include "adfiles/ad_sparc.hpp"87#elif defined TARGET_ARCH_MODEL_zero88# include "adfiles/ad_zero.hpp"89#elif defined TARGET_ARCH_MODEL_ppc_6490# include "adfiles/ad_ppc_64.hpp"91#endif92#endif // COMPILER29394PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC9596bool DeoptimizationMarker::_is_active = false;9798Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame,99int caller_adjustment,100int caller_actual_parameters,101int number_of_frames,102intptr_t* frame_sizes,103address* frame_pcs,104BasicType return_type) {105_size_of_deoptimized_frame = size_of_deoptimized_frame;106_caller_adjustment = caller_adjustment;107_caller_actual_parameters = caller_actual_parameters;108_number_of_frames = number_of_frames;109_frame_sizes = frame_sizes;110_frame_pcs = frame_pcs;111_register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);112_return_type = return_type;113_initial_info = 0;114// PD (x86 only)115_counter_temp = 0;116_unpack_kind = 0;117_sender_sp_temp = 0;118119_total_frame_sizes = size_of_frames();120}121122123Deoptimization::UnrollBlock::~UnrollBlock() {124FREE_C_HEAP_ARRAY(intptr_t, _frame_sizes, mtCompiler);125FREE_C_HEAP_ARRAY(intptr_t, _frame_pcs, mtCompiler);126FREE_C_HEAP_ARRAY(intptr_t, _register_block, mtCompiler);127}128129130intptr_t* Deoptimization::UnrollBlock::value_addr_at(int register_number) const {131assert(register_number < RegisterMap::reg_count, "checking register number");132return &_register_block[register_number * 2];133}134135136137int Deoptimization::UnrollBlock::size_of_frames() const {138// Acount first for the adjustment of the initial frame139int result = _caller_adjustment;140for (int index = 0; index < number_of_frames(); index++) {141result += frame_sizes()[index];142}143return result;144}145146147void Deoptimization::UnrollBlock::print() {148ttyLocker ttyl;149tty->print_cr("UnrollBlock");150tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);151tty->print( " frame_sizes: ");152for (int index = 0; index < number_of_frames(); index++) {153tty->print("%d ", frame_sizes()[index]);154}155tty->cr();156}157158159// In order to make fetch_unroll_info work properly with escape160// analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and161// ResetNoHandleMark and HandleMark were removed from it. The actual reallocation162// of previously eliminated objects occurs in realloc_objects, which is163// called from the method fetch_unroll_info_helper below.164JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread))165// It is actually ok to allocate handles in a leaf method. It causes no safepoints,166// but makes the entry a little slower. There is however a little dance we have to167// do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro168169// fetch_unroll_info() is called at the beginning of the deoptimization170// handler. Note this fact before we start generating temporary frames171// that can confuse an asynchronous stack walker. This counter is172// decremented at the end of unpack_frames().173thread->inc_in_deopt_handler();174175return fetch_unroll_info_helper(thread);176JRT_END177178179// This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)180Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread) {181182// Note: there is a safepoint safety issue here. No matter whether we enter183// via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once184// the vframeArray is created.185//186187// Allocate our special deoptimization ResourceMark188DeoptResourceMark* dmark = new DeoptResourceMark(thread);189assert(thread->deopt_mark() == NULL, "Pending deopt!");190thread->set_deopt_mark(dmark);191192frame stub_frame = thread->last_frame(); // Makes stack walkable as side effect193RegisterMap map(thread, true);194RegisterMap dummy_map(thread, false);195// Now get the deoptee with a valid map196frame deoptee = stub_frame.sender(&map);197// Set the deoptee nmethod198assert(thread->deopt_nmethod() == NULL, "Pending deopt!");199thread->set_deopt_nmethod(deoptee.cb()->as_nmethod_or_null());200201if (VerifyStack) {202thread->validate_frame_layout();203}204205// Create a growable array of VFrames where each VFrame represents an inlined206// Java frame. This storage is allocated with the usual system arena.207assert(deoptee.is_compiled_frame(), "Wrong frame type");208GrowableArray<compiledVFrame*>* chunk = new GrowableArray<compiledVFrame*>(10);209vframe* vf = vframe::new_vframe(&deoptee, &map, thread);210while (!vf->is_top()) {211assert(vf->is_compiled_frame(), "Wrong frame type");212chunk->push(compiledVFrame::cast(vf));213vf = vf->sender();214}215assert(vf->is_compiled_frame(), "Wrong frame type");216chunk->push(compiledVFrame::cast(vf));217218bool realloc_failures = false;219220#ifdef COMPILER2221// Reallocate the non-escaping objects and restore their fields. Then222// relock objects if synchronization on them was eliminated.223if (DoEscapeAnalysis || EliminateNestedLocks) {224if (EliminateAllocations) {225assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames");226GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects();227228// The flag return_oop() indicates call sites which return oop229// in compiled code. Such sites include java method calls,230// runtime calls (for example, used to allocate new objects/arrays231// on slow code path) and any other calls generated in compiled code.232// It is not guaranteed that we can get such information here only233// by analyzing bytecode in deoptimized frames. This is why this flag234// is set during method compilation (see Compile::Process_OopMap_Node()).235// If the previous frame was popped, we don't have a result.236bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution();237Handle return_value;238if (save_oop_result) {239// Reallocation may trigger GC. If deoptimization happened on return from240// call which returns oop we need to save it since it is not in oopmap.241oop result = deoptee.saved_oop_result(&map);242assert(result == NULL || result->is_oop(), "must be oop");243return_value = Handle(thread, result);244assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");245if (TraceDeoptimization) {246ttyLocker ttyl;247tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);248}249}250if (objects != NULL) {251JRT_BLOCK252realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD);253JRT_END254reassign_fields(&deoptee, &map, objects, realloc_failures);255#ifndef PRODUCT256if (TraceDeoptimization) {257ttyLocker ttyl;258tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);259print_objects(objects, realloc_failures);260}261#endif262}263if (save_oop_result) {264// Restore result.265deoptee.set_saved_oop_result(&map, return_value());266}267}268if (EliminateLocks) {269#ifndef PRODUCT270bool first = true;271#endif272for (int i = 0; i < chunk->length(); i++) {273compiledVFrame* cvf = chunk->at(i);274assert (cvf->scope() != NULL,"expect only compiled java frames");275GrowableArray<MonitorInfo*>* monitors = cvf->monitors();276if (monitors->is_nonempty()) {277relock_objects(monitors, thread, realloc_failures);278#ifndef PRODUCT279if (TraceDeoptimization) {280ttyLocker ttyl;281for (int j = 0; j < monitors->length(); j++) {282MonitorInfo* mi = monitors->at(j);283if (mi->eliminated()) {284if (first) {285first = false;286tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);287}288if (mi->owner_is_scalar_replaced()) {289Klass* k = java_lang_Class::as_Klass(mi->owner_klass());290tty->print_cr(" failed reallocation for klass %s", k->external_name());291} else {292tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner());293}294}295}296}297#endif298}299}300}301}302#endif // COMPILER2303// Ensure that no safepoint is taken after pointers have been stored304// in fields of rematerialized objects. If a safepoint occurs from here on305// out the java state residing in the vframeArray will be missed.306No_Safepoint_Verifier no_safepoint;307308vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);309#ifdef COMPILER2310if (realloc_failures) {311pop_frames_failed_reallocs(thread, array);312}313#endif314315assert(thread->vframe_array_head() == NULL, "Pending deopt!");316thread->set_vframe_array_head(array);317318// Now that the vframeArray has been created if we have any deferred local writes319// added by jvmti then we can free up that structure as the data is now in the320// vframeArray321322if (thread->deferred_locals() != NULL) {323GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread->deferred_locals();324int i = 0;325do {326// Because of inlining we could have multiple vframes for a single frame327// and several of the vframes could have deferred writes. Find them all.328if (list->at(i)->id() == array->original().id()) {329jvmtiDeferredLocalVariableSet* dlv = list->at(i);330list->remove_at(i);331// individual jvmtiDeferredLocalVariableSet are CHeapObj's332delete dlv;333} else {334i++;335}336} while ( i < list->length() );337if (list->length() == 0) {338thread->set_deferred_locals(NULL);339// free the list and elements back to C heap.340delete list;341}342343}344345#ifndef SHARK346// Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info.347CodeBlob* cb = stub_frame.cb();348// Verify we have the right vframeArray349assert(cb->frame_size() >= 0, "Unexpected frame size");350intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size();351352// If the deopt call site is a MethodHandle invoke call site we have353// to adjust the unpack_sp.354nmethod* deoptee_nm = deoptee.cb()->as_nmethod_or_null();355if (deoptee_nm != NULL && deoptee_nm->is_method_handle_return(deoptee.pc()))356unpack_sp = deoptee.unextended_sp();357358#ifdef ASSERT359assert(cb->is_deoptimization_stub() || cb->is_uncommon_trap_stub(), "just checking");360#endif361#else362intptr_t* unpack_sp = stub_frame.sender(&dummy_map).unextended_sp();363#endif // !SHARK364365// This is a guarantee instead of an assert because if vframe doesn't match366// we will unpack the wrong deoptimized frame and wind up in strange places367// where it will be very difficult to figure out what went wrong. Better368// to die an early death here than some very obscure death later when the369// trail is cold.370// Note: on ia64 this guarantee can be fooled by frames with no memory stack371// in that it will fail to detect a problem when there is one. This needs372// more work in tiger timeframe.373guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack");374375int number_of_frames = array->frames();376377// Compute the vframes' sizes. Note that frame_sizes[] entries are ordered from outermost to innermost378// virtual activation, which is the reverse of the elements in the vframes array.379intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames, mtCompiler);380// +1 because we always have an interpreter return address for the final slot.381address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1, mtCompiler);382int popframe_extra_args = 0;383// Create an interpreter return address for the stub to use as its return384// address so the skeletal frames are perfectly walkable385frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0);386387// PopFrame requires that the preserved incoming arguments from the recently-popped topmost388// activation be put back on the expression stack of the caller for reexecution389if (JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) {390popframe_extra_args = in_words(thread->popframe_preserved_args_size_in_words());391}392393// Find the current pc for sender of the deoptee. Since the sender may have been deoptimized394// itself since the deoptee vframeArray was created we must get a fresh value of the pc rather395// than simply use array->sender.pc(). This requires us to walk the current set of frames396//397frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame398deopt_sender = deopt_sender.sender(&dummy_map); // Now deoptee caller399400// It's possible that the number of paramters at the call site is401// different than number of arguments in the callee when method402// handles are used. If the caller is interpreted get the real403// value so that the proper amount of space can be added to it's404// frame.405bool caller_was_method_handle = false;406if (deopt_sender.is_interpreted_frame()) {407methodHandle method = deopt_sender.interpreter_frame_method();408Bytecode_invoke cur = Bytecode_invoke_check(method, deopt_sender.interpreter_frame_bci());409if (cur.is_invokedynamic() || cur.is_invokehandle()) {410// Method handle invokes may involve fairly arbitrary chains of411// calls so it's impossible to know how much actual space the412// caller has for locals.413caller_was_method_handle = true;414}415}416417//418// frame_sizes/frame_pcs[0] oldest frame (int or c2i)419// frame_sizes/frame_pcs[1] next oldest frame (int)420// frame_sizes/frame_pcs[n] youngest frame (int)421//422// Now a pc in frame_pcs is actually the return address to the frame's caller (a frame423// owns the space for the return address to it's caller). Confusing ain't it.424//425// The vframe array can address vframes with indices running from426// 0.._frames-1. Index 0 is the youngest frame and _frame - 1 is the oldest (root) frame.427// When we create the skeletal frames we need the oldest frame to be in the zero slot428// in the frame_sizes/frame_pcs so the assembly code can do a trivial walk.429// so things look a little strange in this loop.430//431int callee_parameters = 0;432int callee_locals = 0;433for (int index = 0; index < array->frames(); index++ ) {434// frame[number_of_frames - 1 ] = on_stack_size(youngest)435// frame[number_of_frames - 2 ] = on_stack_size(sender(youngest))436// frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest)))437frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters,438callee_locals,439index == 0,440popframe_extra_args);441// This pc doesn't have to be perfect just good enough to identify the frame442// as interpreted so the skeleton frame will be walkable443// The correct pc will be set when the skeleton frame is completely filled out444// The final pc we store in the loop is wrong and will be overwritten below445frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0) - frame::pc_return_offset;446447callee_parameters = array->element(index)->method()->size_of_parameters();448callee_locals = array->element(index)->method()->max_locals();449popframe_extra_args = 0;450}451452// Compute whether the root vframe returns a float or double value.453BasicType return_type;454{455HandleMark hm;456methodHandle method(thread, array->element(0)->method());457Bytecode_invoke invoke = Bytecode_invoke_check(method, array->element(0)->bci());458return_type = invoke.is_valid() ? invoke.result_type() : T_ILLEGAL;459}460461// Compute information for handling adapters and adjusting the frame size of the caller.462int caller_adjustment = 0;463464// Compute the amount the oldest interpreter frame will have to adjust465// its caller's stack by. If the caller is a compiled frame then466// we pretend that the callee has no parameters so that the467// extension counts for the full amount of locals and not just468// locals-parms. This is because without a c2i adapter the parm469// area as created by the compiled frame will not be usable by470// the interpreter. (Depending on the calling convention there471// may not even be enough space).472473// QQQ I'd rather see this pushed down into last_frame_adjust474// and have it take the sender (aka caller).475476if (deopt_sender.is_compiled_frame() || caller_was_method_handle) {477caller_adjustment = last_frame_adjust(0, callee_locals);478} else if (callee_locals > callee_parameters) {479// The caller frame may need extending to accommodate480// non-parameter locals of the first unpacked interpreted frame.481// Compute that adjustment.482caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);483}484485// If the sender is deoptimized the we must retrieve the address of the handler486// since the frame will "magically" show the original pc before the deopt487// and we'd undo the deopt.488489frame_pcs[0] = deopt_sender.raw_pc();490491#ifndef SHARK492assert(CodeCache::find_blob_unsafe(frame_pcs[0]) != NULL, "bad pc");493#endif // SHARK494495UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord,496caller_adjustment * BytesPerWord,497caller_was_method_handle ? 0 : callee_parameters,498number_of_frames,499frame_sizes,500frame_pcs,501return_type);502// On some platforms, we need a way to pass some platform dependent503// information to the unpacking code so the skeletal frames come out504// correct (initial fp value, unextended sp, ...)505info->set_initial_info((intptr_t) array->sender().initial_deoptimization_info());506507if (array->frames() > 1) {508if (VerifyStack && TraceDeoptimization) {509ttyLocker ttyl;510tty->print_cr("Deoptimizing method containing inlining");511}512}513514array->set_unroll_block(info);515return info;516}517518// Called to cleanup deoptimization data structures in normal case519// after unpacking to stack and when stack overflow error occurs520void Deoptimization::cleanup_deopt_info(JavaThread *thread,521vframeArray *array) {522523// Get array if coming from exception524if (array == NULL) {525array = thread->vframe_array_head();526}527thread->set_vframe_array_head(NULL);528529// Free the previous UnrollBlock530vframeArray* old_array = thread->vframe_array_last();531thread->set_vframe_array_last(array);532533if (old_array != NULL) {534UnrollBlock* old_info = old_array->unroll_block();535old_array->set_unroll_block(NULL);536delete old_info;537delete old_array;538}539540// Deallocate any resource creating in this routine and any ResourceObjs allocated541// inside the vframeArray (StackValueCollections)542543delete thread->deopt_mark();544thread->set_deopt_mark(NULL);545thread->set_deopt_nmethod(NULL);546547548if (JvmtiExport::can_pop_frame()) {549#ifndef CC_INTERP550// Regardless of whether we entered this routine with the pending551// popframe condition bit set, we should always clear it now552thread->clear_popframe_condition();553#else554// C++ interpeter will clear has_pending_popframe when it enters555// with method_resume. For deopt_resume2 we clear it now.556if (thread->popframe_forcing_deopt_reexecution())557thread->clear_popframe_condition();558#endif /* CC_INTERP */559}560561// unpack_frames() is called at the end of the deoptimization handler562// and (in C2) at the end of the uncommon trap handler. Note this fact563// so that an asynchronous stack walker can work again. This counter is564// incremented at the beginning of fetch_unroll_info() and (in C2) at565// the beginning of uncommon_trap().566thread->dec_in_deopt_handler();567}568569570// Return BasicType of value being returned571JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))572573// We are already active int he special DeoptResourceMark any ResourceObj's we574// allocate will be freed at the end of the routine.575576// It is actually ok to allocate handles in a leaf method. It causes no safepoints,577// but makes the entry a little slower. There is however a little dance we have to578// do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro579ResetNoHandleMark rnhm; // No-op in release/product versions580HandleMark hm;581582frame stub_frame = thread->last_frame();583584// Since the frame to unpack is the top frame of this thread, the vframe_array_head585// must point to the vframeArray for the unpack frame.586vframeArray* array = thread->vframe_array_head();587588#ifndef PRODUCT589if (TraceDeoptimization) {590ttyLocker ttyl;591tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d", thread, array, exec_mode);592}593#endif594Events::log(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d",595stub_frame.pc(), stub_frame.sp(), exec_mode);596597UnrollBlock* info = array->unroll_block();598599// Unpack the interpreter frames and any adapter frame (c2 only) we might create.600array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());601602BasicType bt = info->return_type();603604// If we have an exception pending, claim that the return type is an oop605// so the deopt_blob does not overwrite the exception_oop.606607if (exec_mode == Unpack_exception)608bt = T_OBJECT;609610// Cleanup thread deopt data611cleanup_deopt_info(thread, array);612613#ifndef PRODUCT614if (VerifyStack) {615ResourceMark res_mark;616617thread->validate_frame_layout();618619// Verify that the just-unpacked frames match the interpreter's620// notions of expression stack and locals621vframeArray* cur_array = thread->vframe_array_last();622RegisterMap rm(thread, false);623rm.set_include_argument_oops(false);624bool is_top_frame = true;625int callee_size_of_parameters = 0;626int callee_max_locals = 0;627for (int i = 0; i < cur_array->frames(); i++) {628vframeArrayElement* el = cur_array->element(i);629frame* iframe = el->iframe();630guarantee(iframe->is_interpreted_frame(), "Wrong frame type");631632// Get the oop map for this bci633InterpreterOopMap mask;634int cur_invoke_parameter_size = 0;635bool try_next_mask = false;636int next_mask_expression_stack_size = -1;637int top_frame_expression_stack_adjustment = 0;638methodHandle mh(thread, iframe->interpreter_frame_method());639OopMapCache::compute_one_oop_map(mh, iframe->interpreter_frame_bci(), &mask);640BytecodeStream str(mh);641str.set_start(iframe->interpreter_frame_bci());642int max_bci = mh->code_size();643// Get to the next bytecode if possible644assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");645// Check to see if we can grab the number of outgoing arguments646// at an uncommon trap for an invoke (where the compiler647// generates debug info before the invoke has executed)648Bytecodes::Code cur_code = str.next();649if (cur_code == Bytecodes::_invokevirtual ||650cur_code == Bytecodes::_invokespecial ||651cur_code == Bytecodes::_invokestatic ||652cur_code == Bytecodes::_invokeinterface ||653cur_code == Bytecodes::_invokedynamic) {654Bytecode_invoke invoke(mh, iframe->interpreter_frame_bci());655Symbol* signature = invoke.signature();656ArgumentSizeComputer asc(signature);657cur_invoke_parameter_size = asc.size();658if (invoke.has_receiver()) {659// Add in receiver660++cur_invoke_parameter_size;661}662if (i != 0 && !invoke.is_invokedynamic() && MethodHandles::has_member_arg(invoke.klass(), invoke.name())) {663callee_size_of_parameters++;664}665}666if (str.bci() < max_bci) {667Bytecodes::Code bc = str.next();668if (bc >= 0) {669// The interpreter oop map generator reports results before670// the current bytecode has executed except in the case of671// calls. It seems to be hard to tell whether the compiler672// has emitted debug information matching the "state before"673// a given bytecode or the state after, so we try both674switch (cur_code) {675case Bytecodes::_invokevirtual:676case Bytecodes::_invokespecial:677case Bytecodes::_invokestatic:678case Bytecodes::_invokeinterface:679case Bytecodes::_invokedynamic:680case Bytecodes::_athrow:681break;682default: {683InterpreterOopMap next_mask;684OopMapCache::compute_one_oop_map(mh, str.bci(), &next_mask);685next_mask_expression_stack_size = next_mask.expression_stack_size();686// Need to subtract off the size of the result type of687// the bytecode because this is not described in the688// debug info but returned to the interpreter in the TOS689// caching register690BasicType bytecode_result_type = Bytecodes::result_type(cur_code);691if (bytecode_result_type != T_ILLEGAL) {692top_frame_expression_stack_adjustment = type2size[bytecode_result_type];693}694assert(top_frame_expression_stack_adjustment >= 0, "");695try_next_mask = true;696break;697}698}699}700}701702// Verify stack depth and oops in frame703// This assertion may be dependent on the platform we're running on and may need modification (tested on x86 and sparc)704if (!(705/* SPARC */706(iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_size_of_parameters) ||707/* x86 */708(iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) ||709(try_next_mask &&710(iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size -711top_frame_expression_stack_adjustment))) ||712(is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||713(is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) &&714(iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))715)) {716ttyLocker ttyl;717718// Print out some information that will help us debug the problem719tty->print_cr("Wrong number of expression stack elements during deoptimization");720tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1);721tty->print_cr(" Fabricated interpreter frame had %d expression stack elements",722iframe->interpreter_frame_expression_stack_size());723tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size());724tty->print_cr(" try_next_mask = %d", try_next_mask);725tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size);726tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters);727tty->print_cr(" callee_max_locals = %d", callee_max_locals);728tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);729tty->print_cr(" exec_mode = %d", exec_mode);730tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size);731tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = " UINTX_FORMAT, thread, thread->osthread()->thread_id());732tty->print_cr(" Interpreted frames:");733for (int k = 0; k < cur_array->frames(); k++) {734vframeArrayElement* el = cur_array->element(k);735tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());736}737cur_array->print_on_2(tty);738guarantee(false, "wrong number of expression stack elements during deopt");739}740VerifyOopClosure verify;741iframe->oops_interpreted_do(&verify, NULL, &rm, false);742callee_size_of_parameters = mh->size_of_parameters();743callee_max_locals = mh->max_locals();744is_top_frame = false;745}746}747#endif /* !PRODUCT */748749750return bt;751JRT_END752753754int Deoptimization::deoptimize_dependents() {755Threads::deoptimized_wrt_marked_nmethods();756return 0;757}758759760#ifdef COMPILER2761bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {762Handle pending_exception(thread->pending_exception());763const char* exception_file = thread->exception_file();764int exception_line = thread->exception_line();765thread->clear_pending_exception();766767bool failures = false;768769for (int i = 0; i < objects->length(); i++) {770assert(objects->at(i)->is_object(), "invalid debug information");771ObjectValue* sv = (ObjectValue*) objects->at(i);772773KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));774oop obj = NULL;775776if (k->oop_is_instance()) {777InstanceKlass* ik = InstanceKlass::cast(k());778obj = ik->allocate_instance(THREAD);779} else if (k->oop_is_typeArray()) {780TypeArrayKlass* ak = TypeArrayKlass::cast(k());781assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");782int len = sv->field_size() / type2size[ak->element_type()];783obj = ak->allocate(len, THREAD);784} else if (k->oop_is_objArray()) {785ObjArrayKlass* ak = ObjArrayKlass::cast(k());786obj = ak->allocate(sv->field_size(), THREAD);787}788789if (obj == NULL) {790failures = true;791}792793assert(sv->value().is_null(), "redundant reallocation");794assert(obj != NULL || HAS_PENDING_EXCEPTION, "allocation should succeed or we should get an exception");795CLEAR_PENDING_EXCEPTION;796sv->set_value(obj);797}798799if (failures) {800THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);801} else if (pending_exception.not_null()) {802thread->set_pending_exception(pending_exception(), exception_file, exception_line);803}804805return failures;806}807808// This assumes that the fields are stored in ObjectValue in the same order809// they are yielded by do_nonstatic_fields.810class FieldReassigner: public FieldClosure {811frame* _fr;812RegisterMap* _reg_map;813ObjectValue* _sv;814InstanceKlass* _ik;815oop _obj;816817int _i;818public:819FieldReassigner(frame* fr, RegisterMap* reg_map, ObjectValue* sv, oop obj) :820_fr(fr), _reg_map(reg_map), _sv(sv), _obj(obj), _i(0) {}821822int i() const { return _i; }823824825void do_field(fieldDescriptor* fd) {826intptr_t val;827StackValue* value =828StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(i()));829int offset = fd->offset();830switch (fd->field_type()) {831case T_OBJECT: case T_ARRAY:832assert(value->type() == T_OBJECT, "Agreement.");833_obj->obj_field_put(offset, value->get_obj()());834break;835836case T_LONG: case T_DOUBLE: {837assert(value->type() == T_INT, "Agreement.");838StackValue* low =839StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(++_i));840#ifdef _LP64841jlong res = (jlong)low->get_int();842#else843#ifdef SPARC844// For SPARC we have to swap high and low words.845jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int());846#else847jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());848#endif //SPARC849#endif850_obj->long_field_put(offset, res);851break;852}853// Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.854case T_INT: case T_FLOAT: // 4 bytes.855assert(value->type() == T_INT, "Agreement.");856val = value->get_int();857_obj->int_field_put(offset, (jint)*((jint*)&val));858break;859860case T_SHORT:861assert(value->type() == T_INT, "Agreement.");862val = value->get_int();863_obj->short_field_put(offset, (jshort)*((jint*)&val));864break;865866case T_CHAR:867assert(value->type() == T_INT, "Agreement.");868val = value->get_int();869_obj->char_field_put(offset, (jchar)*((jint*)&val));870break;871872case T_BYTE:873assert(value->type() == T_INT, "Agreement.");874val = value->get_int();875_obj->byte_field_put(offset, (jbyte)*((jint*)&val));876break;877878case T_BOOLEAN:879assert(value->type() == T_INT, "Agreement.");880val = value->get_int();881_obj->bool_field_put(offset, (jboolean)*((jint*)&val));882break;883884default:885ShouldNotReachHere();886}887_i++;888}889};890891// restore elements of an eliminated type array892void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) {893int index = 0;894intptr_t val;895896for (int i = 0; i < sv->field_size(); i++) {897StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));898switch(type) {899case T_LONG: case T_DOUBLE: {900assert(value->type() == T_INT, "Agreement.");901StackValue* low =902StackValue::create_stack_value(fr, reg_map, sv->field_at(++i));903#ifdef _LP64904jlong res = (jlong)low->get_int();905#else906#ifdef SPARC907// For SPARC we have to swap high and low words.908jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int());909#else910jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());911#endif //SPARC912#endif913obj->long_at_put(index, res);914break;915}916917// Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.918case T_INT: case T_FLOAT: // 4 bytes.919assert(value->type() == T_INT, "Agreement.");920val = value->get_int();921obj->int_at_put(index, (jint)*((jint*)&val));922break;923924case T_SHORT:925assert(value->type() == T_INT, "Agreement.");926val = value->get_int();927obj->short_at_put(index, (jshort)*((jint*)&val));928break;929930case T_CHAR:931assert(value->type() == T_INT, "Agreement.");932val = value->get_int();933obj->char_at_put(index, (jchar)*((jint*)&val));934break;935936case T_BYTE:937assert(value->type() == T_INT, "Agreement.");938val = value->get_int();939obj->byte_at_put(index, (jbyte)*((jint*)&val));940break;941942case T_BOOLEAN:943assert(value->type() == T_INT, "Agreement.");944val = value->get_int();945obj->bool_at_put(index, (jboolean)*((jint*)&val));946break;947948default:949ShouldNotReachHere();950}951index++;952}953}954955956// restore fields of an eliminated object array957void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {958for (int i = 0; i < sv->field_size(); i++) {959StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));960assert(value->type() == T_OBJECT, "object element expected");961obj->obj_at_put(i, value->get_obj()());962}963}964965966// restore fields of all eliminated objects and arrays967void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures) {968for (int i = 0; i < objects->length(); i++) {969ObjectValue* sv = (ObjectValue*) objects->at(i);970KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));971Handle obj = sv->value();972assert(obj.not_null() || realloc_failures, "reallocation was missed");973if (obj.is_null()) {974continue;975}976977if (k->oop_is_instance()) {978InstanceKlass* ik = InstanceKlass::cast(k());979FieldReassigner reassign(fr, reg_map, sv, obj());980ik->do_nonstatic_fields(&reassign);981} else if (k->oop_is_typeArray()) {982TypeArrayKlass* ak = TypeArrayKlass::cast(k());983reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());984} else if (k->oop_is_objArray()) {985reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());986}987}988}989990991// relock objects for which synchronization was eliminated992void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread, bool realloc_failures) {993for (int i = 0; i < monitors->length(); i++) {994MonitorInfo* mon_info = monitors->at(i);995if (mon_info->eliminated()) {996assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed");997if (!mon_info->owner_is_scalar_replaced()) {998Handle obj = Handle(mon_info->owner());999markOop mark = obj->mark();1000if (UseBiasedLocking && mark->has_bias_pattern()) {1001// New allocated objects may have the mark set to anonymously biased.1002// Also the deoptimized method may called methods with synchronization1003// where the thread-local object is bias locked to the current thread.1004assert(mark->is_biased_anonymously() ||1005mark->biased_locker() == thread, "should be locked to current thread");1006// Reset mark word to unbiased prototype.1007markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());1008obj->set_mark(unbiased_prototype);1009}1010BasicLock* lock = mon_info->lock();1011ObjectSynchronizer::slow_enter(obj, lock, thread);1012assert(mon_info->owner()->is_locked(), "object must be locked now");1013}1014}1015}1016}101710181019#ifndef PRODUCT1020// print information about reallocated objects1021void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures) {1022fieldDescriptor fd;10231024for (int i = 0; i < objects->length(); i++) {1025ObjectValue* sv = (ObjectValue*) objects->at(i);1026KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));1027Handle obj = sv->value();10281029tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());1030k->print_value();1031assert(obj.not_null() || realloc_failures, "reallocation was missed");1032if (obj.is_null()) {1033tty->print(" allocation failed");1034} else {1035tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);1036}1037tty->cr();10381039if (Verbose && !obj.is_null()) {1040k->oop_print_on(obj(), tty);1041}1042}1043}1044#endif1045#endif // COMPILER210461047vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {1048Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());10491050#ifndef PRODUCT1051if (TraceDeoptimization) {1052ttyLocker ttyl;1053tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", thread);1054fr.print_on(tty);1055tty->print_cr(" Virtual frames (innermost first):");1056for (int index = 0; index < chunk->length(); index++) {1057compiledVFrame* vf = chunk->at(index);1058tty->print(" %2d - ", index);1059vf->print_value();1060int bci = chunk->at(index)->raw_bci();1061const char* code_name;1062if (bci == SynchronizationEntryBCI) {1063code_name = "sync entry";1064} else {1065Bytecodes::Code code = vf->method()->code_at(bci);1066code_name = Bytecodes::name(code);1067}1068tty->print(" - %s", code_name);1069tty->print_cr(" @ bci %d ", bci);1070if (Verbose) {1071vf->print();1072tty->cr();1073}1074}1075}1076#endif10771078// Register map for next frame (used for stack crawl). We capture1079// the state of the deopt'ing frame's caller. Thus if we need to1080// stuff a C2I adapter we can properly fill in the callee-save1081// register locations.1082frame caller = fr.sender(reg_map);1083int frame_size = caller.sp() - fr.sp();10841085frame sender = caller;10861087// Since the Java thread being deoptimized will eventually adjust it's own stack,1088// the vframeArray containing the unpacking information is allocated in the C heap.1089// For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().1090vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);10911092// Compare the vframeArray to the collected vframes1093assert(array->structural_compare(thread, chunk), "just checking");10941095#ifndef PRODUCT1096if (TraceDeoptimization) {1097ttyLocker ttyl;1098tty->print_cr(" Created vframeArray " INTPTR_FORMAT, array);1099}1100#endif // PRODUCT11011102return array;1103}11041105#ifdef COMPILER21106void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {1107// Reallocation of some scalar replaced objects failed. Record1108// that we need to pop all the interpreter frames for the1109// deoptimized compiled frame.1110assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");1111thread->set_frames_to_pop_failed_realloc(array->frames());1112// Unlock all monitors here otherwise the interpreter will see a1113// mix of locked and unlocked monitors (because of failed1114// reallocations of synchronized objects) and be confused.1115for (int i = 0; i < array->frames(); i++) {1116MonitorChunk* monitors = array->element(i)->monitors();1117if (monitors != NULL) {1118for (int j = 0; j < monitors->number_of_monitors(); j++) {1119BasicObjectLock* src = monitors->at(j);1120if (src->obj() != NULL) {1121ObjectSynchronizer::fast_exit(src->obj(), src->lock(), thread);1122}1123}1124array->element(i)->free_monitors(thread);1125#ifdef ASSERT1126array->element(i)->set_removed_monitors();1127#endif1128}1129}1130}1131#endif11321133static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {1134GrowableArray<MonitorInfo*>* monitors = cvf->monitors();1135for (int i = 0; i < monitors->length(); i++) {1136MonitorInfo* mon_info = monitors->at(i);1137if (!mon_info->eliminated() && mon_info->owner() != NULL) {1138objects_to_revoke->append(Handle(mon_info->owner()));1139}1140}1141}114211431144void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) {1145if (!UseBiasedLocking) {1146return;1147}11481149GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();11501151// Unfortunately we don't have a RegisterMap available in most of1152// the places we want to call this routine so we need to walk the1153// stack again to update the register map.1154if (map == NULL || !map->update_map()) {1155StackFrameStream sfs(thread, true);1156bool found = false;1157while (!found && !sfs.is_done()) {1158frame* cur = sfs.current();1159sfs.next();1160found = cur->id() == fr.id();1161}1162assert(found, "frame to be deoptimized not found on target thread's stack");1163map = sfs.register_map();1164}11651166vframe* vf = vframe::new_vframe(&fr, map, thread);1167compiledVFrame* cvf = compiledVFrame::cast(vf);1168// Revoke monitors' biases in all scopes1169while (!cvf->is_top()) {1170collect_monitors(cvf, objects_to_revoke);1171cvf = compiledVFrame::cast(cvf->sender());1172}1173collect_monitors(cvf, objects_to_revoke);11741175if (SafepointSynchronize::is_at_safepoint()) {1176BiasedLocking::revoke_at_safepoint(objects_to_revoke);1177} else {1178BiasedLocking::revoke(objects_to_revoke);1179}1180}118111821183void Deoptimization::revoke_biases_of_monitors(CodeBlob* cb) {1184if (!UseBiasedLocking) {1185return;1186}11871188assert(SafepointSynchronize::is_at_safepoint(), "must only be called from safepoint");1189GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();1190for (JavaThread* jt = Threads::first(); jt != NULL ; jt = jt->next()) {1191if (jt->has_last_Java_frame()) {1192StackFrameStream sfs(jt, true);1193while (!sfs.is_done()) {1194frame* cur = sfs.current();1195if (cb->contains(cur->pc())) {1196vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt);1197compiledVFrame* cvf = compiledVFrame::cast(vf);1198// Revoke monitors' biases in all scopes1199while (!cvf->is_top()) {1200collect_monitors(cvf, objects_to_revoke);1201cvf = compiledVFrame::cast(cvf->sender());1202}1203collect_monitors(cvf, objects_to_revoke);1204}1205sfs.next();1206}1207}1208}1209BiasedLocking::revoke_at_safepoint(objects_to_revoke);1210}121112121213void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr) {1214assert(fr.can_be_deoptimized(), "checking frame type");12151216gather_statistics(Reason_constraint, Action_none, Bytecodes::_illegal);12171218// Patch the nmethod so that when execution returns to it we will1219// deopt the execution state and return to the interpreter.1220fr.deoptimize(thread);1221}12221223void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) {1224// Deoptimize only if the frame comes from compile code.1225// Do not deoptimize the frame which is already patched1226// during the execution of the loops below.1227if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {1228return;1229}1230ResourceMark rm;1231DeoptimizationMarker dm;1232if (UseBiasedLocking) {1233revoke_biases_of_monitors(thread, fr, map);1234}1235deoptimize_single_frame(thread, fr);12361237}123812391240void Deoptimization::deoptimize_frame_internal(JavaThread* thread, intptr_t* id) {1241assert(thread == Thread::current() || SafepointSynchronize::is_at_safepoint(),1242"can only deoptimize other thread at a safepoint");1243// Compute frame and register map based on thread and sp.1244RegisterMap reg_map(thread, UseBiasedLocking);1245frame fr = thread->last_frame();1246while (fr.id() != id) {1247fr = fr.sender(®_map);1248}1249deoptimize(thread, fr, ®_map);1250}125112521253void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) {1254if (thread == Thread::current()) {1255Deoptimization::deoptimize_frame_internal(thread, id);1256} else {1257VM_DeoptimizeFrame deopt(thread, id);1258VMThread::execute(&deopt);1259}1260}126112621263// JVMTI PopFrame support1264JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address))1265{1266thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address);1267}1268JRT_END126912701271#if defined(COMPILER2) || defined(SHARK)1272void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index, TRAPS) {1273// in case of an unresolved klass entry, load the class.1274if (constant_pool->tag_at(index).is_unresolved_klass()) {1275Klass* tk = constant_pool->klass_at(index, CHECK);1276return;1277}12781279if (!constant_pool->tag_at(index).is_symbol()) return;12801281Handle class_loader (THREAD, constant_pool->pool_holder()->class_loader());1282Symbol* symbol = constant_pool->symbol_at(index);12831284// class name?1285if (symbol->byte_at(0) != '(') {1286Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain());1287SystemDictionary::resolve_or_null(symbol, class_loader, protection_domain, CHECK);1288return;1289}12901291// then it must be a signature!1292ResourceMark rm(THREAD);1293for (SignatureStream ss(symbol); !ss.is_done(); ss.next()) {1294if (ss.is_object()) {1295Symbol* class_name = ss.as_symbol(CHECK);1296Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain());1297SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK);1298}1299}1300}130113021303void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index) {1304EXCEPTION_MARK;1305load_class_by_index(constant_pool, index, THREAD);1306if (HAS_PENDING_EXCEPTION) {1307// Exception happened during classloading. We ignore the exception here, since it1308// is going to be rethrown since the current activation is going to be deoptimized and1309// the interpreter will re-execute the bytecode.1310CLEAR_PENDING_EXCEPTION;1311// Class loading called java code which may have caused a stack1312// overflow. If the exception was thrown right before the return1313// to the runtime the stack is no longer guarded. Reguard the1314// stack otherwise if we return to the uncommon trap blob and the1315// stack bang causes a stack overflow we crash.1316assert(THREAD->is_Java_thread(), "only a java thread can be here");1317JavaThread* thread = (JavaThread*)THREAD;1318bool guard_pages_enabled = thread->stack_yellow_zone_enabled();1319if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();1320assert(guard_pages_enabled, "stack banging in uncommon trap blob may cause crash");1321}1322}13231324JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) {1325HandleMark hm;13261327// uncommon_trap() is called at the beginning of the uncommon trap1328// handler. Note this fact before we start generating temporary frames1329// that can confuse an asynchronous stack walker. This counter is1330// decremented at the end of unpack_frames().1331thread->inc_in_deopt_handler();13321333// We need to update the map if we have biased locking.1334RegisterMap reg_map(thread, UseBiasedLocking);1335frame stub_frame = thread->last_frame();1336frame fr = stub_frame.sender(®_map);1337// Make sure the calling nmethod is not getting deoptimized and removed1338// before we are done with it.1339nmethodLocker nl(fr.pc());13401341// Log a message1342Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT,1343trap_request, fr.pc());13441345{1346ResourceMark rm;13471348// Revoke biases of any monitors in the frame to ensure we can migrate them1349revoke_biases_of_monitors(thread, fr, ®_map);13501351DeoptReason reason = trap_request_reason(trap_request);1352DeoptAction action = trap_request_action(trap_request);1353jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -113541355vframe* vf = vframe::new_vframe(&fr, ®_map, thread);1356compiledVFrame* cvf = compiledVFrame::cast(vf);13571358nmethod* nm = cvf->code();13591360ScopeDesc* trap_scope = cvf->scope();1361methodHandle trap_method = trap_scope->method();1362int trap_bci = trap_scope->bci();1363Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);13641365// Record this event in the histogram.1366gather_statistics(reason, action, trap_bc);13671368// Ensure that we can record deopt. history:1369// Need MDO to record RTM code generation state.1370bool create_if_missing = ProfileTraps RTM_OPT_ONLY( || UseRTMLocking );13711372MethodData* trap_mdo =1373get_method_data(thread, trap_method, create_if_missing);13741375// Log a message1376Events::log_deopt_message(thread, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d",1377trap_reason_name(reason), trap_action_name(action), fr.pc(),1378trap_method->name_and_sig_as_C_string(), trap_bci);13791380// Print a bunch of diagnostics, if requested.1381if (TraceDeoptimization || LogCompilation) {1382ResourceMark rm;1383ttyLocker ttyl;1384char buf[100];1385if (xtty != NULL) {1386xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT "' %s",1387os::current_thread_id(),1388format_trap_request(buf, sizeof(buf), trap_request));1389nm->log_identity(xtty);1390}1391Symbol* class_name = NULL;1392bool unresolved = false;1393if (unloaded_class_index >= 0) {1394constantPoolHandle constants (THREAD, trap_method->constants());1395if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {1396class_name = constants->klass_name_at(unloaded_class_index);1397unresolved = true;1398if (xtty != NULL)1399xtty->print(" unresolved='1'");1400} else if (constants->tag_at(unloaded_class_index).is_symbol()) {1401class_name = constants->symbol_at(unloaded_class_index);1402}1403if (xtty != NULL)1404xtty->name(class_name);1405}1406if (xtty != NULL && trap_mdo != NULL) {1407// Dump the relevant MDO state.1408// This is the deopt count for the current reason, any previous1409// reasons or recompiles seen at this point.1410int dcnt = trap_mdo->trap_count(reason);1411if (dcnt != 0)1412xtty->print(" count='%d'", dcnt);1413ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);1414int dos = (pdata == NULL)? 0: pdata->trap_state();1415if (dos != 0) {1416xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));1417if (trap_state_is_recompiled(dos)) {1418int recnt2 = trap_mdo->overflow_recompile_count();1419if (recnt2 != 0)1420xtty->print(" recompiles2='%d'", recnt2);1421}1422}1423}1424if (xtty != NULL) {1425xtty->stamp();1426xtty->end_head();1427}1428if (TraceDeoptimization) { // make noise on the tty1429tty->print("Uncommon trap occurred in");1430nm->method()->print_short_name(tty);1431tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d",1432fr.pc(),1433os::current_thread_id(),1434trap_reason_name(reason),1435trap_action_name(action),1436unloaded_class_index);1437if (class_name != NULL) {1438tty->print(unresolved ? " unresolved class: " : " symbol: ");1439class_name->print_symbol_on(tty);1440}1441tty->cr();1442}1443if (xtty != NULL) {1444// Log the precise location of the trap.1445for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {1446xtty->begin_elem("jvms bci='%d'", sd->bci());1447xtty->method(sd->method());1448xtty->end_elem();1449if (sd->is_top()) break;1450}1451xtty->tail("uncommon_trap");1452}1453}1454// (End diagnostic printout.)14551456// Load class if necessary1457if (unloaded_class_index >= 0) {1458constantPoolHandle constants(THREAD, trap_method->constants());1459load_class_by_index(constants, unloaded_class_index);1460}14611462// Flush the nmethod if necessary and desirable.1463//1464// We need to avoid situations where we are re-flushing the nmethod1465// because of a hot deoptimization site. Repeated flushes at the same1466// point need to be detected by the compiler and avoided. If the compiler1467// cannot avoid them (or has a bug and "refuses" to avoid them), this1468// module must take measures to avoid an infinite cycle of recompilation1469// and deoptimization. There are several such measures:1470//1471// 1. If a recompilation is ordered a second time at some site X1472// and for the same reason R, the action is adjusted to 'reinterpret',1473// to give the interpreter time to exercise the method more thoroughly.1474// If this happens, the method's overflow_recompile_count is incremented.1475//1476// 2. If the compiler fails to reduce the deoptimization rate, then1477// the method's overflow_recompile_count will begin to exceed the set1478// limit PerBytecodeRecompilationCutoff. If this happens, the action1479// is adjusted to 'make_not_compilable', and the method is abandoned1480// to the interpreter. This is a performance hit for hot methods,1481// but is better than a disastrous infinite cycle of recompilations.1482// (Actually, only the method containing the site X is abandoned.)1483//1484// 3. In parallel with the previous measures, if the total number of1485// recompilations of a method exceeds the much larger set limit1486// PerMethodRecompilationCutoff, the method is abandoned.1487// This should only happen if the method is very large and has1488// many "lukewarm" deoptimizations. The code which enforces this1489// limit is elsewhere (class nmethod, class Method).1490//1491// Note that the per-BCI 'is_recompiled' bit gives the compiler one chance1492// to recompile at each bytecode independently of the per-BCI cutoff.1493//1494// The decision to update code is up to the compiler, and is encoded1495// in the Action_xxx code. If the compiler requests Action_none1496// no trap state is changed, no compiled code is changed, and the1497// computation suffers along in the interpreter.1498//1499// The other action codes specify various tactics for decompilation1500// and recompilation. Action_maybe_recompile is the loosest, and1501// allows the compiled code to stay around until enough traps are seen,1502// and until the compiler gets around to recompiling the trapping method.1503//1504// The other actions cause immediate removal of the present code.15051506// Traps caused by injected profile shouldn't pollute trap counts.1507bool injected_profile_trap = trap_method->has_injected_profile() &&1508(reason == Reason_intrinsic || reason == Reason_unreached);1509bool update_trap_state = !injected_profile_trap;1510bool make_not_entrant = false;1511bool make_not_compilable = false;1512bool reprofile = false;1513switch (action) {1514case Action_none:1515// Keep the old code.1516update_trap_state = false;1517break;1518case Action_maybe_recompile:1519// Do not need to invalidate the present code, but we can1520// initiate another1521// Start compiler without (necessarily) invalidating the nmethod.1522// The system will tolerate the old code, but new code should be1523// generated when possible.1524break;1525case Action_reinterpret:1526// Go back into the interpreter for a while, and then consider1527// recompiling form scratch.1528make_not_entrant = true;1529// Reset invocation counter for outer most method.1530// This will allow the interpreter to exercise the bytecodes1531// for a while before recompiling.1532// By contrast, Action_make_not_entrant is immediate.1533//1534// Note that the compiler will track null_check, null_assert,1535// range_check, and class_check events and log them as if they1536// had been traps taken from compiled code. This will update1537// the MDO trap history so that the next compilation will1538// properly detect hot trap sites.1539reprofile = true;1540break;1541case Action_make_not_entrant:1542// Request immediate recompilation, and get rid of the old code.1543// Make them not entrant, so next time they are called they get1544// recompiled. Unloaded classes are loaded now so recompile before next1545// time they are called. Same for uninitialized. The interpreter will1546// link the missing class, if any.1547make_not_entrant = true;1548break;1549case Action_make_not_compilable:1550// Give up on compiling this method at all.1551make_not_entrant = true;1552make_not_compilable = true;1553break;1554default:1555ShouldNotReachHere();1556}15571558// Setting +ProfileTraps fixes the following, on all platforms:1559// 4852688: ProfileInterpreter is off by default for ia64. The result is1560// infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the1561// recompile relies on a MethodData* to record heroic opt failures.15621563// Whether the interpreter is producing MDO data or not, we also need1564// to use the MDO to detect hot deoptimization points and control1565// aggressive optimization.1566bool inc_recompile_count = false;1567ProfileData* pdata = NULL;1568if (ProfileTraps && update_trap_state && trap_mdo != NULL) {1569assert(trap_mdo == get_method_data(thread, trap_method, false), "sanity");1570uint this_trap_count = 0;1571bool maybe_prior_trap = false;1572bool maybe_prior_recompile = false;1573pdata = query_update_method_data(trap_mdo, trap_bci, reason,1574nm->method(),1575//outputs:1576this_trap_count,1577maybe_prior_trap,1578maybe_prior_recompile);1579// Because the interpreter also counts null, div0, range, and class1580// checks, these traps from compiled code are double-counted.1581// This is harmless; it just means that the PerXTrapLimit values1582// are in effect a little smaller than they look.15831584DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);1585if (per_bc_reason != Reason_none) {1586// Now take action based on the partially known per-BCI history.1587if (maybe_prior_trap1588&& this_trap_count >= (uint)PerBytecodeTrapLimit) {1589// If there are too many traps at this BCI, force a recompile.1590// This will allow the compiler to see the limit overflow, and1591// take corrective action, if possible. The compiler generally1592// does not use the exact PerBytecodeTrapLimit value, but instead1593// changes its tactics if it sees any traps at all. This provides1594// a little hysteresis, delaying a recompile until a trap happens1595// several times.1596//1597// Actually, since there is only one bit of counter per BCI,1598// the possible per-BCI counts are {0,1,(per-method count)}.1599// This produces accurate results if in fact there is only1600// one hot trap site, but begins to get fuzzy if there are1601// many sites. For example, if there are ten sites each1602// trapping two or more times, they each get the blame for1603// all of their traps.1604make_not_entrant = true;1605}16061607// Detect repeated recompilation at the same BCI, and enforce a limit.1608if (make_not_entrant && maybe_prior_recompile) {1609// More than one recompile at this point.1610inc_recompile_count = maybe_prior_trap;1611}1612} else {1613// For reasons which are not recorded per-bytecode, we simply1614// force recompiles unconditionally.1615// (Note that PerMethodRecompilationCutoff is enforced elsewhere.)1616make_not_entrant = true;1617}16181619// Go back to the compiler if there are too many traps in this method.1620if (this_trap_count >= per_method_trap_limit(reason)) {1621// If there are too many traps in this method, force a recompile.1622// This will allow the compiler to see the limit overflow, and1623// take corrective action, if possible.1624// (This condition is an unlikely backstop only, because the1625// PerBytecodeTrapLimit is more likely to take effect first,1626// if it is applicable.)1627make_not_entrant = true;1628}16291630// Here's more hysteresis: If there has been a recompile at1631// this trap point already, run the method in the interpreter1632// for a while to exercise it more thoroughly.1633if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {1634reprofile = true;1635}16361637}16381639// Take requested actions on the method:16401641// Recompile1642if (make_not_entrant) {1643if (!nm->make_not_entrant()) {1644return; // the call did not change nmethod's state1645}16461647if (pdata != NULL) {1648// Record the recompilation event, if any.1649int tstate0 = pdata->trap_state();1650int tstate1 = trap_state_set_recompiled(tstate0, true);1651if (tstate1 != tstate0)1652pdata->set_trap_state(tstate1);1653}16541655#if INCLUDE_RTM_OPT1656// Restart collecting RTM locking abort statistic if the method1657// is recompiled for a reason other than RTM state change.1658// Assume that in new recompiled code the statistic could be different,1659// for example, due to different inlining.1660if ((reason != Reason_rtm_state_change) && (trap_mdo != NULL) &&1661UseRTMDeopt && (nm->rtm_state() != ProfileRTM)) {1662trap_mdo->atomic_set_rtm_state(ProfileRTM);1663}1664#endif1665}16661667if (inc_recompile_count) {1668trap_mdo->inc_overflow_recompile_count();1669if ((uint)trap_mdo->overflow_recompile_count() >1670(uint)PerBytecodeRecompilationCutoff) {1671// Give up on the method containing the bad BCI.1672if (trap_method() == nm->method()) {1673make_not_compilable = true;1674} else {1675trap_method->set_not_compilable(CompLevel_full_optimization, true, "overflow_recompile_count > PerBytecodeRecompilationCutoff");1676// But give grace to the enclosing nm->method().1677}1678}1679}16801681// Reprofile1682if (reprofile) {1683CompilationPolicy::policy()->reprofile(trap_scope, nm->is_osr_method());1684}16851686// Give up compiling1687if (make_not_compilable && !nm->method()->is_not_compilable(CompLevel_full_optimization)) {1688assert(make_not_entrant, "consistent");1689nm->method()->set_not_compilable(CompLevel_full_optimization);1690}16911692} // Free marked resources16931694}1695JRT_END16961697MethodData*1698Deoptimization::get_method_data(JavaThread* thread, methodHandle m,1699bool create_if_missing) {1700Thread* THREAD = thread;1701MethodData* mdo = m()->method_data();1702if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) {1703// Build an MDO. Ignore errors like OutOfMemory;1704// that simply means we won't have an MDO to update.1705Method::build_interpreter_method_data(m, THREAD);1706if (HAS_PENDING_EXCEPTION) {1707assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here");1708CLEAR_PENDING_EXCEPTION;1709}1710mdo = m()->method_data();1711}1712return mdo;1713}17141715ProfileData*1716Deoptimization::query_update_method_data(MethodData* trap_mdo,1717int trap_bci,1718Deoptimization::DeoptReason reason,1719Method* compiled_method,1720//outputs:1721uint& ret_this_trap_count,1722bool& ret_maybe_prior_trap,1723bool& ret_maybe_prior_recompile) {1724uint prior_trap_count = trap_mdo->trap_count(reason);1725uint this_trap_count = trap_mdo->inc_trap_count(reason);17261727// If the runtime cannot find a place to store trap history,1728// it is estimated based on the general condition of the method.1729// If the method has ever been recompiled, or has ever incurred1730// a trap with the present reason , then this BCI is assumed1731// (pessimistically) to be the culprit.1732bool maybe_prior_trap = (prior_trap_count != 0);1733bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0);1734ProfileData* pdata = NULL;173517361737// For reasons which are recorded per bytecode, we check per-BCI data.1738DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);1739if (per_bc_reason != Reason_none) {1740// Find the profile data for this BCI. If there isn't one,1741// try to allocate one from the MDO's set of spares.1742// This will let us detect a repeated trap at this point.1743pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : NULL);17441745if (pdata != NULL) {1746if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) {1747if (LogCompilation && xtty != NULL) {1748ttyLocker ttyl;1749// no more room for speculative traps in this MDO1750xtty->elem("speculative_traps_oom");1751}1752}1753// Query the trap state of this profile datum.1754int tstate0 = pdata->trap_state();1755if (!trap_state_has_reason(tstate0, per_bc_reason))1756maybe_prior_trap = false;1757if (!trap_state_is_recompiled(tstate0))1758maybe_prior_recompile = false;17591760// Update the trap state of this profile datum.1761int tstate1 = tstate0;1762// Record the reason.1763tstate1 = trap_state_add_reason(tstate1, per_bc_reason);1764// Store the updated state on the MDO, for next time.1765if (tstate1 != tstate0)1766pdata->set_trap_state(tstate1);1767} else {1768if (LogCompilation && xtty != NULL) {1769ttyLocker ttyl;1770// Missing MDP? Leave a small complaint in the log.1771xtty->elem("missing_mdp bci='%d'", trap_bci);1772}1773}1774}17751776// Return results:1777ret_this_trap_count = this_trap_count;1778ret_maybe_prior_trap = maybe_prior_trap;1779ret_maybe_prior_recompile = maybe_prior_recompile;1780return pdata;1781}17821783void1784Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {1785ResourceMark rm;1786// Ignored outputs:1787uint ignore_this_trap_count;1788bool ignore_maybe_prior_trap;1789bool ignore_maybe_prior_recompile;1790assert(!reason_is_speculate(reason), "reason speculate only used by compiler");1791query_update_method_data(trap_mdo, trap_bci,1792(DeoptReason)reason,1793NULL,1794ignore_this_trap_count,1795ignore_maybe_prior_trap,1796ignore_maybe_prior_recompile);1797}17981799Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) {1800// Enable WXWrite: current function is called from methods compiled by C2 directly1801MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));18021803// Still in Java no safepoints1804{1805// This enters VM and may safepoint1806uncommon_trap_inner(thread, trap_request);1807}1808return fetch_unroll_info_helper(thread);1809}18101811// Local derived constants.1812// Further breakdown of DataLayout::trap_state, as promised by DataLayout.1813const int DS_REASON_MASK = DataLayout::trap_mask >> 1;1814const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;18151816//---------------------------trap_state_reason---------------------------------1817Deoptimization::DeoptReason1818Deoptimization::trap_state_reason(int trap_state) {1819// This assert provides the link between the width of DataLayout::trap_bits1820// and the encoding of "recorded" reasons. It ensures there are enough1821// bits to store all needed reasons in the per-BCI MDO profile.1822assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");1823int recompile_bit = (trap_state & DS_RECOMPILE_BIT);1824trap_state -= recompile_bit;1825if (trap_state == DS_REASON_MASK) {1826return Reason_many;1827} else {1828assert((int)Reason_none == 0, "state=0 => Reason_none");1829return (DeoptReason)trap_state;1830}1831}1832//-------------------------trap_state_has_reason-------------------------------1833int Deoptimization::trap_state_has_reason(int trap_state, int reason) {1834assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason");1835assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");1836int recompile_bit = (trap_state & DS_RECOMPILE_BIT);1837trap_state -= recompile_bit;1838if (trap_state == DS_REASON_MASK) {1839return -1; // true, unspecifically (bottom of state lattice)1840} else if (trap_state == reason) {1841return 1; // true, definitely1842} else if (trap_state == 0) {1843return 0; // false, definitely (top of state lattice)1844} else {1845return 0; // false, definitely1846}1847}1848//-------------------------trap_state_add_reason-------------------------------1849int Deoptimization::trap_state_add_reason(int trap_state, int reason) {1850assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason");1851int recompile_bit = (trap_state & DS_RECOMPILE_BIT);1852trap_state -= recompile_bit;1853if (trap_state == DS_REASON_MASK) {1854return trap_state + recompile_bit; // already at state lattice bottom1855} else if (trap_state == reason) {1856return trap_state + recompile_bit; // the condition is already true1857} else if (trap_state == 0) {1858return reason + recompile_bit; // no condition has yet been true1859} else {1860return DS_REASON_MASK + recompile_bit; // fall to state lattice bottom1861}1862}1863//-----------------------trap_state_is_recompiled------------------------------1864bool Deoptimization::trap_state_is_recompiled(int trap_state) {1865return (trap_state & DS_RECOMPILE_BIT) != 0;1866}1867//-----------------------trap_state_set_recompiled-----------------------------1868int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) {1869if (z) return trap_state | DS_RECOMPILE_BIT;1870else return trap_state & ~DS_RECOMPILE_BIT;1871}1872//---------------------------format_trap_state---------------------------------1873// This is used for debugging and diagnostics, including LogFile output.1874const char* Deoptimization::format_trap_state(char* buf, size_t buflen,1875int trap_state) {1876DeoptReason reason = trap_state_reason(trap_state);1877bool recomp_flag = trap_state_is_recompiled(trap_state);1878// Re-encode the state from its decoded components.1879int decoded_state = 0;1880if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many)1881decoded_state = trap_state_add_reason(decoded_state, reason);1882if (recomp_flag)1883decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag);1884// If the state re-encodes properly, format it symbolically.1885// Because this routine is used for debugging and diagnostics,1886// be robust even if the state is a strange value.1887size_t len;1888if (decoded_state != trap_state) {1889// Random buggy state that doesn't decode??1890len = jio_snprintf(buf, buflen, "#%d", trap_state);1891} else {1892len = jio_snprintf(buf, buflen, "%s%s",1893trap_reason_name(reason),1894recomp_flag ? " recompiled" : "");1895}1896return buf;1897}189818991900//--------------------------------statics--------------------------------------1901Deoptimization::DeoptAction Deoptimization::_unloaded_action1902= Deoptimization::Action_reinterpret;1903const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = {1904// Note: Keep this in sync. with enum DeoptReason.1905"none",1906"null_check",1907"null_assert",1908"range_check",1909"class_check",1910"array_check",1911"intrinsic",1912"bimorphic",1913"unloaded",1914"uninitialized",1915"unreached",1916"unhandled",1917"constraint",1918"div0_check",1919"age",1920"predicate",1921"loop_limit_check",1922"speculate_class_check",1923"rtm_state_change",1924"unstable_if"1925};1926const char* Deoptimization::_trap_action_name[Action_LIMIT] = {1927// Note: Keep this in sync. with enum DeoptAction.1928"none",1929"maybe_recompile",1930"reinterpret",1931"make_not_entrant",1932"make_not_compilable"1933};19341935const char* Deoptimization::trap_reason_name(int reason) {1936if (reason == Reason_many) return "many";1937if ((uint)reason < Reason_LIMIT)1938return _trap_reason_name[reason];1939static char buf[20];1940sprintf(buf, "reason%d", reason);1941return buf;1942}1943const char* Deoptimization::trap_action_name(int action) {1944if ((uint)action < Action_LIMIT)1945return _trap_action_name[action];1946static char buf[20];1947sprintf(buf, "action%d", action);1948return buf;1949}19501951// This is used for debugging and diagnostics, including LogFile output.1952const char* Deoptimization::format_trap_request(char* buf, size_t buflen,1953int trap_request) {1954jint unloaded_class_index = trap_request_index(trap_request);1955const char* reason = trap_reason_name(trap_request_reason(trap_request));1956const char* action = trap_action_name(trap_request_action(trap_request));1957size_t len;1958if (unloaded_class_index < 0) {1959len = jio_snprintf(buf, buflen, "reason='%s' action='%s'",1960reason, action);1961} else {1962len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'",1963reason, action, unloaded_class_index);1964}1965return buf;1966}19671968juint Deoptimization::_deoptimization_hist1969[Deoptimization::Reason_LIMIT]1970[1 + Deoptimization::Action_LIMIT]1971[Deoptimization::BC_CASE_LIMIT]1972= {0};19731974enum {1975LSB_BITS = 8,1976LSB_MASK = right_n_bits(LSB_BITS)1977};19781979void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,1980Bytecodes::Code bc) {1981assert(reason >= 0 && reason < Reason_LIMIT, "oob");1982assert(action >= 0 && action < Action_LIMIT, "oob");1983_deoptimization_hist[Reason_none][0][0] += 1; // total1984_deoptimization_hist[reason][0][0] += 1; // per-reason total1985juint* cases = _deoptimization_hist[reason][1+action];1986juint* bc_counter_addr = NULL;1987juint bc_counter = 0;1988// Look for an unused counter, or an exact match to this BC.1989if (bc != Bytecodes::_illegal) {1990for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {1991juint* counter_addr = &cases[bc_case];1992juint counter = *counter_addr;1993if ((counter == 0 && bc_counter_addr == NULL)1994|| (Bytecodes::Code)(counter & LSB_MASK) == bc) {1995// this counter is either free or is already devoted to this BC1996bc_counter_addr = counter_addr;1997bc_counter = counter | bc;1998}1999}2000}2001if (bc_counter_addr == NULL) {2002// Overflow, or no given bytecode.2003bc_counter_addr = &cases[BC_CASE_LIMIT-1];2004bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB2005}2006*bc_counter_addr = bc_counter + (1 << LSB_BITS);2007}20082009jint Deoptimization::total_deoptimization_count() {2010return _deoptimization_hist[Reason_none][0][0];2011}20122013jint Deoptimization::deoptimization_count(DeoptReason reason) {2014assert(reason >= 0 && reason < Reason_LIMIT, "oob");2015return _deoptimization_hist[reason][0][0];2016}20172018void Deoptimization::print_statistics() {2019juint total = total_deoptimization_count();2020juint account = total;2021if (total != 0) {2022ttyLocker ttyl;2023if (xtty != NULL) xtty->head("statistics type='deoptimization'");2024tty->print_cr("Deoptimization traps recorded:");2025#define PRINT_STAT_LINE(name, r) \2026tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);2027PRINT_STAT_LINE("total", total);2028// For each non-zero entry in the histogram, print the reason,2029// the action, and (if specifically known) the type of bytecode.2030for (int reason = 0; reason < Reason_LIMIT; reason++) {2031for (int action = 0; action < Action_LIMIT; action++) {2032juint* cases = _deoptimization_hist[reason][1+action];2033for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {2034juint counter = cases[bc_case];2035if (counter != 0) {2036char name[1*K];2037Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);2038if (bc_case == BC_CASE_LIMIT && (int)bc == 0)2039bc = Bytecodes::_illegal;2040sprintf(name, "%s/%s/%s",2041trap_reason_name(reason),2042trap_action_name(action),2043Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");2044juint r = counter >> LSB_BITS;2045tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);2046account -= r;2047}2048}2049}2050}2051if (account != 0) {2052PRINT_STAT_LINE("unaccounted", account);2053}2054#undef PRINT_STAT_LINE2055if (xtty != NULL) xtty->tail("statistics");2056}2057}2058#else // COMPILER2 || SHARK205920602061// Stubs for C1 only system.2062bool Deoptimization::trap_state_is_recompiled(int trap_state) {2063return false;2064}20652066const char* Deoptimization::trap_reason_name(int reason) {2067return "unknown";2068}20692070void Deoptimization::print_statistics() {2071// no output2072}20732074void2075Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {2076// no udpate2077}20782079int Deoptimization::trap_state_has_reason(int trap_state, int reason) {2080return 0;2081}20822083void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,2084Bytecodes::Code bc) {2085// no update2086}20872088const char* Deoptimization::format_trap_state(char* buf, size_t buflen,2089int trap_state) {2090jio_snprintf(buf, buflen, "#%d", trap_state);2091return buf;2092}20932094#endif // COMPILER2 || SHARK209520962097