1 /*
   2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "code/debugInfoRec.hpp"
  33 #include "code/nmethod.hpp"
  34 #include "code/pcDesc.hpp"
  35 #include "code/scopeDesc.hpp"
  36 #include "compiler/compilationPolicy.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "interpreter/bytecode.hpp"
  39 #include "interpreter/interpreter.hpp"
  40 #include "interpreter/oopMapCache.hpp"
  41 #include "memory/allocation.inline.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/constantPool.hpp"
  46 #include "oops/method.hpp"
  47 #include "oops/objArrayKlass.hpp"
  48 #include "oops/objArrayOop.inline.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "oops/fieldStreams.inline.hpp"
  51 #include "oops/typeArrayOop.inline.hpp"
  52 #include "oops/verifyOopClosure.hpp"
  53 #include "prims/jvmtiDeferredUpdates.hpp"
  54 #include "prims/jvmtiExport.hpp"
  55 #include "prims/jvmtiThreadState.hpp"
  56 #include "prims/vectorSupport.hpp"
  57 #include "prims/methodHandles.hpp"
  58 #include "runtime/atomic.hpp"
  59 #include "runtime/biasedLocking.hpp"
  60 #include "runtime/deoptimization.hpp"
  61 #include "runtime/escapeBarrier.hpp"
  62 #include "runtime/fieldDescriptor.hpp"
  63 #include "runtime/fieldDescriptor.inline.hpp"
  64 #include "runtime/frame.inline.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/interfaceSupport.inline.hpp"
  67 #include "runtime/jniHandles.inline.hpp"
  68 #include "runtime/keepStackGCProcessed.hpp"
  69 #include "runtime/objectMonitor.inline.hpp"
  70 #include "runtime/osThread.hpp"
  71 #include "runtime/safepointVerifiers.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/stackFrameStream.inline.hpp"
  75 #include "runtime/stackWatermarkSet.hpp"
  76 #include "runtime/stubRoutines.hpp"
  77 #include "runtime/synchronizer.hpp"
  78 #include "runtime/thread.hpp"
  79 #include "runtime/threadSMR.hpp"
  80 #include "runtime/threadWXSetters.inline.hpp"
  81 #include "runtime/vframe.hpp"
  82 #include "runtime/vframeArray.hpp"
  83 #include "runtime/vframe_hp.hpp"
  84 #include "runtime/vmOperations.hpp"
  85 #include "utilities/events.hpp"
  86 #include "utilities/macros.hpp"
  87 #include "utilities/preserveException.hpp"
  88 #include "utilities/xmlstream.hpp"
  89 #if INCLUDE_JFR
  90 #include "jfr/jfrEvents.hpp"
  91 #include "jfr/metadata/jfrSerializer.hpp"
  92 #endif
  93 
  94 bool DeoptimizationMarker::_is_active = false;
  95 
  96 Deoptimization::UnrollBlock::UnrollBlock(int  size_of_deoptimized_frame,
  97                                          int  caller_adjustment,
  98                                          int  caller_actual_parameters,
  99                                          int  number_of_frames,
 100                                          intptr_t* frame_sizes,
 101                                          address* frame_pcs,
 102                                          BasicType return_type,
 103                                          int exec_mode) {
 104   _size_of_deoptimized_frame = size_of_deoptimized_frame;
 105   _caller_adjustment         = caller_adjustment;
 106   _caller_actual_parameters  = caller_actual_parameters;
 107   _number_of_frames          = number_of_frames;
 108   _frame_sizes               = frame_sizes;
 109   _frame_pcs                 = frame_pcs;
 110   _register_block            = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);
 111   _return_type               = return_type;
 112   _initial_info              = 0;
 113   // PD (x86 only)
 114   _counter_temp              = 0;
 115   _unpack_kind               = exec_mode;
 116   _sender_sp_temp            = 0;
 117 
 118   _total_frame_sizes         = size_of_frames();
 119   assert(exec_mode >= 0 && exec_mode < Unpack_LIMIT, "Unexpected exec_mode");
 120 }
 121 
 122 
 123 Deoptimization::UnrollBlock::~UnrollBlock() {
 124   FREE_C_HEAP_ARRAY(intptr_t, _frame_sizes);
 125   FREE_C_HEAP_ARRAY(intptr_t, _frame_pcs);
 126   FREE_C_HEAP_ARRAY(intptr_t, _register_block);
 127 }
 128 
 129 
 130 intptr_t* Deoptimization::UnrollBlock::value_addr_at(int register_number) const {
 131   assert(register_number < RegisterMap::reg_count, "checking register number");
 132   return &_register_block[register_number * 2];
 133 }
 134 
 135 
 136 
 137 int Deoptimization::UnrollBlock::size_of_frames() const {
 138   // Acount first for the adjustment of the initial frame
 139   int result = _caller_adjustment;
 140   for (int index = 0; index < number_of_frames(); index++) {
 141     result += frame_sizes()[index];
 142   }
 143   return result;
 144 }
 145 
 146 
 147 void Deoptimization::UnrollBlock::print() {
 148   ResourceMark rm;
 149   stringStream st;
 150   st.print_cr("UnrollBlock");
 151   st.print_cr("  size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
 152   st.print(   "  frame_sizes: ");
 153   for (int index = 0; index < number_of_frames(); index++) {
 154     st.print(INTX_FORMAT " ", frame_sizes()[index]);
 155   }
 156   st.cr();
 157   tty->print_raw(st.as_string());
 158 }
 159 
 160 
 161 // In order to make fetch_unroll_info work properly with escape
 162 // analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
 163 // The actual reallocation of previously eliminated objects occurs in realloc_objects,
 164 // which is called from the method fetch_unroll_info_helper below.
 165 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
 166   // fetch_unroll_info() is called at the beginning of the deoptimization
 167   // handler. Note this fact before we start generating temporary frames
 168   // that can confuse an asynchronous stack walker. This counter is
 169   // decremented at the end of unpack_frames().
 170   if (TraceDeoptimization) {
 171     tty->print_cr("Deoptimizing thread " INTPTR_FORMAT, p2i(current));
 172   }
 173   current->inc_in_deopt_handler();
 174 
 175   if (exec_mode == Unpack_exception) {
 176     // When we get here, a callee has thrown an exception into a deoptimized
 177     // frame. That throw might have deferred stack watermark checking until
 178     // after unwinding. So we deal with such deferred requests here.
 179     StackWatermarkSet::after_unwind(current);
 180   }
 181 
 182   return fetch_unroll_info_helper(current, exec_mode);
 183 JRT_END
 184 
 185 #if COMPILER2_OR_JVMCI
 186 #ifndef PRODUCT
 187 // print information about reallocated objects
 188 static void print_objects(JavaThread* deoptee_thread,
 189                           GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
 190   ResourceMark rm;
 191   stringStream st;  // change to logStream with logging
 192   st.print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(deoptee_thread));
 193   fieldDescriptor fd;
 194 
 195   for (int i = 0; i < objects->length(); i++) {
 196     ObjectValue* sv = (ObjectValue*) objects->at(i);
 197     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
 198     Handle obj = sv->value();
 199 
 200     st.print("     object <" INTPTR_FORMAT "> of type ", p2i(sv->value()()));
 201     k->print_value_on(&st);
 202     assert(obj.not_null() || realloc_failures, "reallocation was missed");
 203     if (obj.is_null()) {
 204       st.print(" allocation failed");
 205     } else {
 206       st.print(" allocated (%d bytes)", obj->size() * HeapWordSize);
 207     }
 208     st.cr();
 209 
 210     if (Verbose && !obj.is_null()) {
 211       k->oop_print_on(obj(), &st);
 212     }
 213   }
 214   tty->print_raw(st.as_string());
 215 }
 216 #endif
 217 
 218 static bool rematerialize_objects(JavaThread* thread, int exec_mode, CompiledMethod* compiled_method,
 219                                   frame& deoptee, RegisterMap& map, GrowableArray<compiledVFrame*>* chunk,
 220                                   bool& deoptimized_objects) {
 221   bool realloc_failures = false;
 222   assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames");
 223 
 224   JavaThread* deoptee_thread = chunk->at(0)->thread();
 225   assert(exec_mode == Deoptimization::Unpack_none || (deoptee_thread == thread),
 226          "a frame can only be deoptimized by the owner thread");
 227 
 228   GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects();
 229 
 230   // The flag return_oop() indicates call sites which return oop
 231   // in compiled code. Such sites include java method calls,
 232   // runtime calls (for example, used to allocate new objects/arrays
 233   // on slow code path) and any other calls generated in compiled code.
 234   // It is not guaranteed that we can get such information here only
 235   // by analyzing bytecode in deoptimized frames. This is why this flag
 236   // is set during method compilation (see Compile::Process_OopMap_Node()).
 237   // If the previous frame was popped or if we are dispatching an exception,
 238   // we don't have an oop result.
 239   bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution() && (exec_mode == Deoptimization::Unpack_deopt);
 240   Handle return_value;
 241   if (save_oop_result) {
 242     // Reallocation may trigger GC. If deoptimization happened on return from
 243     // call which returns oop we need to save it since it is not in oopmap.
 244     oop result = deoptee.saved_oop_result(&map);
 245     assert(oopDesc::is_oop_or_null(result), "must be oop");
 246     return_value = Handle(thread, result);
 247     assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
 248     if (TraceDeoptimization) {
 249       tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
 250     }
 251   }
 252   if (objects != NULL) {
 253     if (exec_mode == Deoptimization::Unpack_none) {
 254       assert(thread->thread_state() == _thread_in_vm, "assumption");
 255       JavaThread* THREAD = thread; // For exception macros.
 256       // Clear pending OOM if reallocation fails and return true indicating allocation failure
 257       realloc_failures = Deoptimization::realloc_objects(thread, &deoptee, &map, objects, CHECK_AND_CLEAR_(true));
 258       deoptimized_objects = true;
 259     } else {
 260       JavaThread* current = thread; // For JRT_BLOCK
 261       JRT_BLOCK
 262       realloc_failures = Deoptimization::realloc_objects(thread, &deoptee, &map, objects, THREAD);
 263       JRT_END
 264     }
 265     bool skip_internal = (compiled_method != NULL) && !compiled_method->is_compiled_by_jvmci();
 266     Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal);
 267 #ifndef PRODUCT
 268     if (TraceDeoptimization) {
 269       print_objects(deoptee_thread, objects, realloc_failures);
 270     }
 271 #endif
 272   }
 273   if (save_oop_result) {
 274     // Restore result.
 275     deoptee.set_saved_oop_result(&map, return_value());
 276   }
 277   return realloc_failures;
 278 }
 279 
 280 static void restore_eliminated_locks(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures,
 281                                      frame& deoptee, int exec_mode, bool& deoptimized_objects) {
 282   JavaThread* deoptee_thread = chunk->at(0)->thread();
 283   assert(!EscapeBarrier::objs_are_deoptimized(deoptee_thread, deoptee.id()), "must relock just once");
 284   assert(thread == Thread::current(), "should be");
 285   HandleMark hm(thread);
 286 #ifndef PRODUCT
 287   bool first = true;
 288 #endif
 289   for (int i = 0; i < chunk->length(); i++) {
 290     compiledVFrame* cvf = chunk->at(i);
 291     assert (cvf->scope() != NULL,"expect only compiled java frames");
 292     GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
 293     if (monitors->is_nonempty()) {
 294       bool relocked = Deoptimization::relock_objects(thread, monitors, deoptee_thread, deoptee,
 295                                                      exec_mode, realloc_failures);
 296       deoptimized_objects = deoptimized_objects || relocked;
 297 #ifndef PRODUCT
 298       if (PrintDeoptimizationDetails) {
 299         ResourceMark rm;
 300         stringStream st;
 301         for (int j = 0; j < monitors->length(); j++) {
 302           MonitorInfo* mi = monitors->at(j);
 303           if (mi->eliminated()) {
 304             if (first) {
 305               first = false;
 306               st.print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
 307             }
 308             if (exec_mode == Deoptimization::Unpack_none) {
 309               ObjectMonitor* monitor = deoptee_thread->current_waiting_monitor();
 310               if (monitor != NULL && monitor->object() == mi->owner()) {
 311                 st.print_cr("     object <" INTPTR_FORMAT "> DEFERRED relocking after wait", p2i(mi->owner()));
 312                 continue;
 313               }
 314             }
 315             if (mi->owner_is_scalar_replaced()) {
 316               Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
 317               st.print_cr("     failed reallocation for klass %s", k->external_name());
 318             } else {
 319               st.print_cr("     object <" INTPTR_FORMAT "> locked", p2i(mi->owner()));
 320             }
 321           }
 322         }
 323         tty->print_raw(st.as_string());
 324       }
 325 #endif // !PRODUCT
 326     }
 327   }
 328 }
 329 
 330 // Deoptimize objects, that is reallocate and relock them, just before they escape through JVMTI.
 331 // The given vframes cover one physical frame.
 332 bool Deoptimization::deoptimize_objects_internal(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk,
 333                                                  bool& realloc_failures) {
 334   frame deoptee = chunk->at(0)->fr();
 335   JavaThread* deoptee_thread = chunk->at(0)->thread();
 336   CompiledMethod* cm = deoptee.cb()->as_compiled_method_or_null();
 337   RegisterMap map(chunk->at(0)->register_map());
 338   bool deoptimized_objects = false;
 339 
 340   bool const jvmci_enabled = JVMCI_ONLY(UseJVMCICompiler) NOT_JVMCI(false);
 341 
 342   // Reallocate the non-escaping objects and restore their fields.
 343   if (jvmci_enabled COMPILER2_PRESENT(|| (DoEscapeAnalysis && EliminateAllocations)
 344                                       || EliminateAutoBox || EnableVectorAggressiveReboxing)) {
 345     realloc_failures = rematerialize_objects(thread, Unpack_none, cm, deoptee, map, chunk, deoptimized_objects);
 346   }
 347 
 348   // Revoke biases of objects with eliminated locks in the given frame.
 349   Deoptimization::revoke_for_object_deoptimization(deoptee_thread, deoptee, &map, thread);
 350 
 351   // MonitorInfo structures used in eliminate_locks are not GC safe.
 352   NoSafepointVerifier no_safepoint;
 353 
 354   // Now relock objects if synchronization on them was eliminated.
 355   if (jvmci_enabled COMPILER2_PRESENT(|| ((DoEscapeAnalysis || EliminateNestedLocks) && EliminateLocks))) {
 356     restore_eliminated_locks(thread, chunk, realloc_failures, deoptee, Unpack_none, deoptimized_objects);
 357   }
 358   return deoptimized_objects;
 359 }
 360 #endif // COMPILER2_OR_JVMCI
 361 
 362 // This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)
 363 Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* current, int exec_mode) {
 364   // When we get here we are about to unwind the deoptee frame. In order to
 365   // catch not yet safe to use frames, the following stack watermark barrier
 366   // poll will make such frames safe to use.
 367   StackWatermarkSet::before_unwind(current);
 368 
 369   // Note: there is a safepoint safety issue here. No matter whether we enter
 370   // via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once
 371   // the vframeArray is created.
 372   //
 373 
 374   // Allocate our special deoptimization ResourceMark
 375   DeoptResourceMark* dmark = new DeoptResourceMark(current);
 376   assert(current->deopt_mark() == NULL, "Pending deopt!");
 377   current->set_deopt_mark(dmark);
 378 
 379   frame stub_frame = current->last_frame(); // Makes stack walkable as side effect
 380   RegisterMap map(current, true);
 381   RegisterMap dummy_map(current, false);
 382   // Now get the deoptee with a valid map
 383   frame deoptee = stub_frame.sender(&map);
 384   // Set the deoptee nmethod
 385   assert(current->deopt_compiled_method() == NULL, "Pending deopt!");
 386   CompiledMethod* cm = deoptee.cb()->as_compiled_method_or_null();
 387   current->set_deopt_compiled_method(cm);
 388 
 389   if (VerifyStack) {
 390     current->validate_frame_layout();
 391   }
 392 
 393   // Create a growable array of VFrames where each VFrame represents an inlined
 394   // Java frame.  This storage is allocated with the usual system arena.
 395   assert(deoptee.is_compiled_frame(), "Wrong frame type");
 396   GrowableArray<compiledVFrame*>* chunk = new GrowableArray<compiledVFrame*>(10);
 397   vframe* vf = vframe::new_vframe(&deoptee, &map, current);
 398   while (!vf->is_top()) {
 399     assert(vf->is_compiled_frame(), "Wrong frame type");
 400     chunk->push(compiledVFrame::cast(vf));
 401     vf = vf->sender();
 402   }
 403   assert(vf->is_compiled_frame(), "Wrong frame type");
 404   chunk->push(compiledVFrame::cast(vf));
 405 
 406   bool realloc_failures = false;
 407 
 408 #if COMPILER2_OR_JVMCI
 409   bool const jvmci_enabled = JVMCI_ONLY(EnableJVMCI) NOT_JVMCI(false);
 410 
 411   // Reallocate the non-escaping objects and restore their fields. Then
 412   // relock objects if synchronization on them was eliminated.
 413   if (jvmci_enabled COMPILER2_PRESENT( || (DoEscapeAnalysis && EliminateAllocations)
 414                                        || EliminateAutoBox || EnableVectorAggressiveReboxing )) {
 415     bool unused;
 416     realloc_failures = rematerialize_objects(current, exec_mode, cm, deoptee, map, chunk, unused);
 417   }
 418 #endif // COMPILER2_OR_JVMCI
 419 
 420   // Revoke biases, done with in java state.
 421   // No safepoints allowed after this
 422   revoke_from_deopt_handler(current, deoptee, &map);
 423 
 424   // Ensure that no safepoint is taken after pointers have been stored
 425   // in fields of rematerialized objects.  If a safepoint occurs from here on
 426   // out the java state residing in the vframeArray will be missed.
 427   // Locks may be rebaised in a safepoint.
 428   NoSafepointVerifier no_safepoint;
 429 
 430 #if COMPILER2_OR_JVMCI
 431   if ((jvmci_enabled COMPILER2_PRESENT( || ((DoEscapeAnalysis || EliminateNestedLocks) && EliminateLocks) ))
 432       && !EscapeBarrier::objs_are_deoptimized(current, deoptee.id())) {
 433     bool unused;
 434     restore_eliminated_locks(current, chunk, realloc_failures, deoptee, exec_mode, unused);
 435   }
 436 #endif // COMPILER2_OR_JVMCI
 437 
 438   ScopeDesc* trap_scope = chunk->at(0)->scope();
 439   Handle exceptionObject;
 440   if (trap_scope->rethrow_exception()) {
 441     if (PrintDeoptimizationDetails) {
 442       tty->print_cr("Exception to be rethrown in the interpreter for method %s::%s at bci %d", trap_scope->method()->method_holder()->name()->as_C_string(), trap_scope->method()->name()->as_C_string(), trap_scope->bci());
 443     }
 444     GrowableArray<ScopeValue*>* expressions = trap_scope->expressions();
 445     guarantee(expressions != NULL && expressions->length() > 0, "must have exception to throw");
 446     ScopeValue* topOfStack = expressions->top();
 447     exceptionObject = StackValue::create_stack_value(&deoptee, &map, topOfStack)->get_obj();
 448     guarantee(exceptionObject() != NULL, "exception oop can not be null");
 449   }
 450 
 451   vframeArray* array = create_vframeArray(current, deoptee, &map, chunk, realloc_failures);
 452 #if COMPILER2_OR_JVMCI
 453   if (realloc_failures) {
 454     pop_frames_failed_reallocs(current, array);
 455   }
 456 #endif
 457 
 458   assert(current->vframe_array_head() == NULL, "Pending deopt!");
 459   current->set_vframe_array_head(array);
 460 
 461   // Now that the vframeArray has been created if we have any deferred local writes
 462   // added by jvmti then we can free up that structure as the data is now in the
 463   // vframeArray
 464 
 465   JvmtiDeferredUpdates::delete_updates_for_frame(current, array->original().id());
 466 
 467   // Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info.
 468   CodeBlob* cb = stub_frame.cb();
 469   // Verify we have the right vframeArray
 470   assert(cb->frame_size() >= 0, "Unexpected frame size");
 471   intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size();
 472 
 473   // If the deopt call site is a MethodHandle invoke call site we have
 474   // to adjust the unpack_sp.
 475   nmethod* deoptee_nm = deoptee.cb()->as_nmethod_or_null();
 476   if (deoptee_nm != NULL && deoptee_nm->is_method_handle_return(deoptee.pc()))
 477     unpack_sp = deoptee.unextended_sp();
 478 
 479 #ifdef ASSERT
 480   assert(cb->is_deoptimization_stub() ||
 481          cb->is_uncommon_trap_stub() ||
 482          strcmp("Stub<DeoptimizationStub.deoptimizationHandler>", cb->name()) == 0 ||
 483          strcmp("Stub<UncommonTrapStub.uncommonTrapHandler>", cb->name()) == 0,
 484          "unexpected code blob: %s", cb->name());
 485 #endif
 486 
 487   // This is a guarantee instead of an assert because if vframe doesn't match
 488   // we will unpack the wrong deoptimized frame and wind up in strange places
 489   // where it will be very difficult to figure out what went wrong. Better
 490   // to die an early death here than some very obscure death later when the
 491   // trail is cold.
 492   // Note: on ia64 this guarantee can be fooled by frames with no memory stack
 493   // in that it will fail to detect a problem when there is one. This needs
 494   // more work in tiger timeframe.
 495   guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack");
 496 
 497   int number_of_frames = array->frames();
 498 
 499   // Compute the vframes' sizes.  Note that frame_sizes[] entries are ordered from outermost to innermost
 500   // virtual activation, which is the reverse of the elements in the vframes array.
 501   intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames, mtCompiler);
 502   // +1 because we always have an interpreter return address for the final slot.
 503   address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1, mtCompiler);
 504   int popframe_extra_args = 0;
 505   // Create an interpreter return address for the stub to use as its return
 506   // address so the skeletal frames are perfectly walkable
 507   frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0);
 508 
 509   // PopFrame requires that the preserved incoming arguments from the recently-popped topmost
 510   // activation be put back on the expression stack of the caller for reexecution
 511   if (JvmtiExport::can_pop_frame() && current->popframe_forcing_deopt_reexecution()) {
 512     popframe_extra_args = in_words(current->popframe_preserved_args_size_in_words());
 513   }
 514 
 515   // Find the current pc for sender of the deoptee. Since the sender may have been deoptimized
 516   // itself since the deoptee vframeArray was created we must get a fresh value of the pc rather
 517   // than simply use array->sender.pc(). This requires us to walk the current set of frames
 518   //
 519   frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame
 520   deopt_sender = deopt_sender.sender(&dummy_map);     // Now deoptee caller
 521 
 522   // It's possible that the number of parameters at the call site is
 523   // different than number of arguments in the callee when method
 524   // handles are used.  If the caller is interpreted get the real
 525   // value so that the proper amount of space can be added to it's
 526   // frame.
 527   bool caller_was_method_handle = false;
 528   if (deopt_sender.is_interpreted_frame()) {
 529     methodHandle method(current, deopt_sender.interpreter_frame_method());
 530     Bytecode_invoke cur = Bytecode_invoke_check(method, deopt_sender.interpreter_frame_bci());
 531     if (cur.is_invokedynamic() || cur.is_invokehandle()) {
 532       // Method handle invokes may involve fairly arbitrary chains of
 533       // calls so it's impossible to know how much actual space the
 534       // caller has for locals.
 535       caller_was_method_handle = true;
 536     }
 537   }
 538 
 539   //
 540   // frame_sizes/frame_pcs[0] oldest frame (int or c2i)
 541   // frame_sizes/frame_pcs[1] next oldest frame (int)
 542   // frame_sizes/frame_pcs[n] youngest frame (int)
 543   //
 544   // Now a pc in frame_pcs is actually the return address to the frame's caller (a frame
 545   // owns the space for the return address to it's caller).  Confusing ain't it.
 546   //
 547   // The vframe array can address vframes with indices running from
 548   // 0.._frames-1. Index  0 is the youngest frame and _frame - 1 is the oldest (root) frame.
 549   // When we create the skeletal frames we need the oldest frame to be in the zero slot
 550   // in the frame_sizes/frame_pcs so the assembly code can do a trivial walk.
 551   // so things look a little strange in this loop.
 552   //
 553   int callee_parameters = 0;
 554   int callee_locals = 0;
 555   for (int index = 0; index < array->frames(); index++ ) {
 556     // frame[number_of_frames - 1 ] = on_stack_size(youngest)
 557     // frame[number_of_frames - 2 ] = on_stack_size(sender(youngest))
 558     // frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest)))
 559     frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters,
 560                                                                                                     callee_locals,
 561                                                                                                     index == 0,
 562                                                                                                     popframe_extra_args);
 563     // This pc doesn't have to be perfect just good enough to identify the frame
 564     // as interpreted so the skeleton frame will be walkable
 565     // The correct pc will be set when the skeleton frame is completely filled out
 566     // The final pc we store in the loop is wrong and will be overwritten below
 567     frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0) - frame::pc_return_offset;
 568 
 569     callee_parameters = array->element(index)->method()->size_of_parameters();
 570     callee_locals = array->element(index)->method()->max_locals();
 571     popframe_extra_args = 0;
 572   }
 573 
 574   // Compute whether the root vframe returns a float or double value.
 575   BasicType return_type;
 576   {
 577     methodHandle method(current, array->element(0)->method());
 578     Bytecode_invoke invoke = Bytecode_invoke_check(method, array->element(0)->bci());
 579     return_type = invoke.is_valid() ? invoke.result_type() : T_ILLEGAL;
 580   }
 581 
 582   // Compute information for handling adapters and adjusting the frame size of the caller.
 583   int caller_adjustment = 0;
 584 
 585   // Compute the amount the oldest interpreter frame will have to adjust
 586   // its caller's stack by. If the caller is a compiled frame then
 587   // we pretend that the callee has no parameters so that the
 588   // extension counts for the full amount of locals and not just
 589   // locals-parms. This is because without a c2i adapter the parm
 590   // area as created by the compiled frame will not be usable by
 591   // the interpreter. (Depending on the calling convention there
 592   // may not even be enough space).
 593 
 594   // QQQ I'd rather see this pushed down into last_frame_adjust
 595   // and have it take the sender (aka caller).
 596 
 597   if (deopt_sender.is_compiled_frame() || caller_was_method_handle) {
 598     caller_adjustment = last_frame_adjust(0, callee_locals);
 599   } else if (callee_locals > callee_parameters) {
 600     // The caller frame may need extending to accommodate
 601     // non-parameter locals of the first unpacked interpreted frame.
 602     // Compute that adjustment.
 603     caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);
 604   }
 605 
 606   // If the sender is deoptimized the we must retrieve the address of the handler
 607   // since the frame will "magically" show the original pc before the deopt
 608   // and we'd undo the deopt.
 609 
 610   frame_pcs[0] = deopt_sender.raw_pc();
 611 
 612   assert(CodeCache::find_blob_unsafe(frame_pcs[0]) != NULL, "bad pc");
 613 
 614 #if INCLUDE_JVMCI
 615   if (exceptionObject() != NULL) {
 616     current->set_exception_oop(exceptionObject());
 617     exec_mode = Unpack_exception;
 618   }
 619 #endif
 620 
 621   if (current->frames_to_pop_failed_realloc() > 0 && exec_mode != Unpack_uncommon_trap) {
 622     assert(current->has_pending_exception(), "should have thrown OOME");
 623     current->set_exception_oop(current->pending_exception());
 624     current->clear_pending_exception();
 625     exec_mode = Unpack_exception;
 626   }
 627 
 628 #if INCLUDE_JVMCI
 629   if (current->frames_to_pop_failed_realloc() > 0) {
 630     current->set_pending_monitorenter(false);
 631   }
 632 #endif
 633 
 634   UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord,
 635                                       caller_adjustment * BytesPerWord,
 636                                       caller_was_method_handle ? 0 : callee_parameters,
 637                                       number_of_frames,
 638                                       frame_sizes,
 639                                       frame_pcs,
 640                                       return_type,
 641                                       exec_mode);
 642   // On some platforms, we need a way to pass some platform dependent
 643   // information to the unpacking code so the skeletal frames come out
 644   // correct (initial fp value, unextended sp, ...)
 645   info->set_initial_info((intptr_t) array->sender().initial_deoptimization_info());
 646 
 647   if (array->frames() > 1) {
 648     if (VerifyStack && TraceDeoptimization) {
 649       tty->print_cr("Deoptimizing method containing inlining");
 650     }
 651   }
 652 
 653   array->set_unroll_block(info);
 654   return info;
 655 }
 656 
 657 // Called to cleanup deoptimization data structures in normal case
 658 // after unpacking to stack and when stack overflow error occurs
 659 void Deoptimization::cleanup_deopt_info(JavaThread *thread,
 660                                         vframeArray *array) {
 661 
 662   // Get array if coming from exception
 663   if (array == NULL) {
 664     array = thread->vframe_array_head();
 665   }
 666   thread->set_vframe_array_head(NULL);
 667 
 668   // Free the previous UnrollBlock
 669   vframeArray* old_array = thread->vframe_array_last();
 670   thread->set_vframe_array_last(array);
 671 
 672   if (old_array != NULL) {
 673     UnrollBlock* old_info = old_array->unroll_block();
 674     old_array->set_unroll_block(NULL);
 675     delete old_info;
 676     delete old_array;
 677   }
 678 
 679   // Deallocate any resource creating in this routine and any ResourceObjs allocated
 680   // inside the vframeArray (StackValueCollections)
 681 
 682   delete thread->deopt_mark();
 683   thread->set_deopt_mark(NULL);
 684   thread->set_deopt_compiled_method(NULL);
 685 
 686 
 687   if (JvmtiExport::can_pop_frame()) {
 688     // Regardless of whether we entered this routine with the pending
 689     // popframe condition bit set, we should always clear it now
 690     thread->clear_popframe_condition();
 691   }
 692 
 693   // unpack_frames() is called at the end of the deoptimization handler
 694   // and (in C2) at the end of the uncommon trap handler. Note this fact
 695   // so that an asynchronous stack walker can work again. This counter is
 696   // incremented at the beginning of fetch_unroll_info() and (in C2) at
 697   // the beginning of uncommon_trap().
 698   thread->dec_in_deopt_handler();
 699 }
 700 
 701 // Moved from cpu directories because none of the cpus has callee save values.
 702 // If a cpu implements callee save values, move this to deoptimization_<cpu>.cpp.
 703 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 704 
 705   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 706   // the days we had adapter frames. When we deoptimize a situation where a
 707   // compiled caller calls a compiled caller will have registers it expects
 708   // to survive the call to the callee. If we deoptimize the callee the only
 709   // way we can restore these registers is to have the oldest interpreter
 710   // frame that we create restore these values. That is what this routine
 711   // will accomplish.
 712 
 713   // At the moment we have modified c2 to not have any callee save registers
 714   // so this problem does not exist and this routine is just a place holder.
 715 
 716   assert(f->is_interpreted_frame(), "must be interpreted");
 717 }
 718 
 719 #ifndef PRODUCT
 720 static bool falls_through(Bytecodes::Code bc) {
 721   switch (bc) {
 722     // List may be incomplete.  Here we really only care about bytecodes where compiled code
 723     // can deoptimize.
 724     case Bytecodes::_goto:
 725     case Bytecodes::_goto_w:
 726     case Bytecodes::_athrow:
 727       return false;
 728     default:
 729       return true;
 730   }
 731 }
 732 #endif
 733 
 734 // Return BasicType of value being returned
 735 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
 736 
 737   // We are already active in the special DeoptResourceMark any ResourceObj's we
 738   // allocate will be freed at the end of the routine.
 739 
 740   // JRT_LEAF methods don't normally allocate handles and there is a
 741   // NoHandleMark to enforce that. It is actually safe to use Handles
 742   // in a JRT_LEAF method, and sometimes desirable, but to do so we
 743   // must use ResetNoHandleMark to bypass the NoHandleMark, and
 744   // then use a HandleMark to ensure any Handles we do create are
 745   // cleaned up in this scope.
 746   ResetNoHandleMark rnhm;
 747   HandleMark hm(thread);
 748 
 749   frame stub_frame = thread->last_frame();
 750 
 751   // Since the frame to unpack is the top frame of this thread, the vframe_array_head
 752   // must point to the vframeArray for the unpack frame.
 753   vframeArray* array = thread->vframe_array_head();
 754 
 755 #ifndef PRODUCT
 756   if (TraceDeoptimization) {
 757     tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d",
 758                   p2i(thread), p2i(array), exec_mode);
 759   }
 760 #endif
 761   Events::log_deopt_message(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d",
 762               p2i(stub_frame.pc()), p2i(stub_frame.sp()), exec_mode);
 763 
 764   UnrollBlock* info = array->unroll_block();
 765 
 766   // We set the last_Java frame. But the stack isn't really parsable here. So we
 767   // clear it to make sure JFR understands not to try and walk stacks from events
 768   // in here.
 769   intptr_t* sp = thread->frame_anchor()->last_Java_sp();
 770   thread->frame_anchor()->set_last_Java_sp(NULL);
 771 
 772   // Unpack the interpreter frames and any adapter frame (c2 only) we might create.
 773   array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());
 774 
 775   thread->frame_anchor()->set_last_Java_sp(sp);
 776 
 777   BasicType bt = info->return_type();
 778 
 779   // If we have an exception pending, claim that the return type is an oop
 780   // so the deopt_blob does not overwrite the exception_oop.
 781 
 782   if (exec_mode == Unpack_exception)
 783     bt = T_OBJECT;
 784 
 785   // Cleanup thread deopt data
 786   cleanup_deopt_info(thread, array);
 787 
 788 #ifndef PRODUCT
 789   if (VerifyStack) {
 790     ResourceMark res_mark;
 791     // Clear pending exception to not break verification code (restored afterwards)
 792     PreserveExceptionMark pm(thread);
 793 
 794     thread->validate_frame_layout();
 795 
 796     // Verify that the just-unpacked frames match the interpreter's
 797     // notions of expression stack and locals
 798     vframeArray* cur_array = thread->vframe_array_last();
 799     RegisterMap rm(thread, false);
 800     rm.set_include_argument_oops(false);
 801     bool is_top_frame = true;
 802     int callee_size_of_parameters = 0;
 803     int callee_max_locals = 0;
 804     for (int i = 0; i < cur_array->frames(); i++) {
 805       vframeArrayElement* el = cur_array->element(i);
 806       frame* iframe = el->iframe();
 807       guarantee(iframe->is_interpreted_frame(), "Wrong frame type");
 808 
 809       // Get the oop map for this bci
 810       InterpreterOopMap mask;
 811       int cur_invoke_parameter_size = 0;
 812       bool try_next_mask = false;
 813       int next_mask_expression_stack_size = -1;
 814       int top_frame_expression_stack_adjustment = 0;
 815       methodHandle mh(thread, iframe->interpreter_frame_method());
 816       OopMapCache::compute_one_oop_map(mh, iframe->interpreter_frame_bci(), &mask);
 817       BytecodeStream str(mh, iframe->interpreter_frame_bci());
 818       int max_bci = mh->code_size();
 819       // Get to the next bytecode if possible
 820       assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");
 821       // Check to see if we can grab the number of outgoing arguments
 822       // at an uncommon trap for an invoke (where the compiler
 823       // generates debug info before the invoke has executed)
 824       Bytecodes::Code cur_code = str.next();
 825       Bytecodes::Code next_code = Bytecodes::_shouldnotreachhere;
 826       if (Bytecodes::is_invoke(cur_code)) {
 827         Bytecode_invoke invoke(mh, iframe->interpreter_frame_bci());
 828         cur_invoke_parameter_size = invoke.size_of_parameters();
 829         if (i != 0 && !invoke.is_invokedynamic() && MethodHandles::has_member_arg(invoke.klass(), invoke.name())) {
 830           callee_size_of_parameters++;
 831         }
 832       }
 833       if (str.bci() < max_bci) {
 834         next_code = str.next();
 835         if (next_code >= 0) {
 836           // The interpreter oop map generator reports results before
 837           // the current bytecode has executed except in the case of
 838           // calls. It seems to be hard to tell whether the compiler
 839           // has emitted debug information matching the "state before"
 840           // a given bytecode or the state after, so we try both
 841           if (!Bytecodes::is_invoke(cur_code) && falls_through(cur_code)) {
 842             // Get expression stack size for the next bytecode
 843             InterpreterOopMap next_mask;
 844             OopMapCache::compute_one_oop_map(mh, str.bci(), &next_mask);
 845             next_mask_expression_stack_size = next_mask.expression_stack_size();
 846             if (Bytecodes::is_invoke(next_code)) {
 847               Bytecode_invoke invoke(mh, str.bci());
 848               next_mask_expression_stack_size += invoke.size_of_parameters();
 849             }
 850             // Need to subtract off the size of the result type of
 851             // the bytecode because this is not described in the
 852             // debug info but returned to the interpreter in the TOS
 853             // caching register
 854             BasicType bytecode_result_type = Bytecodes::result_type(cur_code);
 855             if (bytecode_result_type != T_ILLEGAL) {
 856               top_frame_expression_stack_adjustment = type2size[bytecode_result_type];
 857             }
 858             assert(top_frame_expression_stack_adjustment >= 0, "stack adjustment must be positive");
 859             try_next_mask = true;
 860           }
 861         }
 862       }
 863 
 864       // Verify stack depth and oops in frame
 865       // This assertion may be dependent on the platform we're running on and may need modification (tested on x86 and sparc)
 866       if (!(
 867             /* SPARC */
 868             (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_size_of_parameters) ||
 869             /* x86 */
 870             (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) ||
 871             (try_next_mask &&
 872              (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size -
 873                                                                     top_frame_expression_stack_adjustment))) ||
 874             (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||
 875             (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) &&
 876              (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))
 877             )) {
 878         {
 879           // Print out some information that will help us debug the problem
 880           tty->print_cr("Wrong number of expression stack elements during deoptimization");
 881           tty->print_cr("  Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1);
 882           tty->print_cr("  Current code %s", Bytecodes::name(cur_code));
 883           if (try_next_mask) {
 884             tty->print_cr("  Next code %s", Bytecodes::name(next_code));
 885           }
 886           tty->print_cr("  Fabricated interpreter frame had %d expression stack elements",
 887                         iframe->interpreter_frame_expression_stack_size());
 888           tty->print_cr("  Interpreter oop map had %d expression stack elements", mask.expression_stack_size());
 889           tty->print_cr("  try_next_mask = %d", try_next_mask);
 890           tty->print_cr("  next_mask_expression_stack_size = %d", next_mask_expression_stack_size);
 891           tty->print_cr("  callee_size_of_parameters = %d", callee_size_of_parameters);
 892           tty->print_cr("  callee_max_locals = %d", callee_max_locals);
 893           tty->print_cr("  top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);
 894           tty->print_cr("  exec_mode = %d", exec_mode);
 895           tty->print_cr("  cur_invoke_parameter_size = %d", cur_invoke_parameter_size);
 896           tty->print_cr("  Thread = " INTPTR_FORMAT ", thread ID = %d", p2i(thread), thread->osthread()->thread_id());
 897           tty->print_cr("  Interpreted frames:");
 898           for (int k = 0; k < cur_array->frames(); k++) {
 899             vframeArrayElement* el = cur_array->element(k);
 900             tty->print_cr("    %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
 901           }
 902           cur_array->print_on_2(tty);
 903         }
 904         guarantee(false, "wrong number of expression stack elements during deopt");
 905       }
 906       VerifyOopClosure verify;
 907       iframe->oops_interpreted_do(&verify, &rm, false);
 908       callee_size_of_parameters = mh->size_of_parameters();
 909       callee_max_locals = mh->max_locals();
 910       is_top_frame = false;
 911     }
 912   }
 913 #endif /* !PRODUCT */
 914 
 915   return bt;
 916 JRT_END
 917 
 918 class DeoptimizeMarkedClosure : public HandshakeClosure {
 919  public:
 920   DeoptimizeMarkedClosure() : HandshakeClosure("Deoptimize") {}
 921   void do_thread(Thread* thread) {
 922     JavaThread* jt = thread->as_Java_thread();
 923     jt->deoptimize_marked_methods();
 924   }
 925 };
 926 
 927 void Deoptimization::deoptimize_all_marked(nmethod* nmethod_only) {
 928   ResourceMark rm;
 929   DeoptimizationMarker dm;
 930 
 931   // Make the dependent methods not entrant
 932   if (nmethod_only != NULL) {
 933     nmethod_only->mark_for_deoptimization();
 934     nmethod_only->make_not_entrant();
 935   } else {
 936     MutexLocker mu(SafepointSynchronize::is_at_safepoint() ? NULL : CodeCache_lock, Mutex::_no_safepoint_check_flag);
 937     CodeCache::make_marked_nmethods_not_entrant();
 938   }
 939 
 940   DeoptimizeMarkedClosure deopt;
 941   if (SafepointSynchronize::is_at_safepoint()) {
 942     Threads::java_threads_do(&deopt);
 943   } else {
 944     Handshake::execute(&deopt);
 945   }
 946 }
 947 
 948 Deoptimization::DeoptAction Deoptimization::_unloaded_action
 949   = Deoptimization::Action_reinterpret;
 950 
 951 #if COMPILER2_OR_JVMCI
 952 template<typename CacheType>
 953 class BoxCacheBase : public CHeapObj<mtCompiler> {
 954 protected:
 955   static InstanceKlass* find_cache_klass(Symbol* klass_name) {
 956     ResourceMark rm;
 957     char* klass_name_str = klass_name->as_C_string();
 958     InstanceKlass* ik = SystemDictionary::find_instance_klass(klass_name, Handle(), Handle());
 959     guarantee(ik != NULL, "%s must be loaded", klass_name_str);
 960     guarantee(ik->is_initialized(), "%s must be initialized", klass_name_str);
 961     CacheType::compute_offsets(ik);
 962     return ik;
 963   }
 964 };
 965 
 966 template<typename PrimitiveType, typename CacheType, typename BoxType> class BoxCache  : public BoxCacheBase<CacheType> {
 967   PrimitiveType _low;
 968   PrimitiveType _high;
 969   jobject _cache;
 970 protected:
 971   static BoxCache<PrimitiveType, CacheType, BoxType> *_singleton;
 972   BoxCache(Thread* thread) {
 973     InstanceKlass* ik = BoxCacheBase<CacheType>::find_cache_klass(CacheType::symbol());
 974     objArrayOop cache = CacheType::cache(ik);
 975     assert(cache->length() > 0, "Empty cache");
 976     _low = BoxType::value(cache->obj_at(0));
 977     _high = _low + cache->length() - 1;
 978     _cache = JNIHandles::make_global(Handle(thread, cache));
 979   }
 980   ~BoxCache() {
 981     JNIHandles::destroy_global(_cache);
 982   }
 983 public:
 984   static BoxCache<PrimitiveType, CacheType, BoxType>* singleton(Thread* thread) {
 985     if (_singleton == NULL) {
 986       BoxCache<PrimitiveType, CacheType, BoxType>* s = new BoxCache<PrimitiveType, CacheType, BoxType>(thread);
 987       if (!Atomic::replace_if_null(&_singleton, s)) {
 988         delete s;
 989       }
 990     }
 991     return _singleton;
 992   }
 993   oop lookup(PrimitiveType value) {
 994     if (_low <= value && value <= _high) {
 995       int offset = value - _low;
 996       return objArrayOop(JNIHandles::resolve_non_null(_cache))->obj_at(offset);
 997     }
 998     return NULL;
 999   }
1000   oop lookup_raw(intptr_t raw_value) {
1001     // Have to cast to avoid little/big-endian problems.
1002     if (sizeof(PrimitiveType) > sizeof(jint)) {
1003       jlong value = (jlong)raw_value;
1004       return lookup(value);
1005     }
1006     PrimitiveType value = (PrimitiveType)*((jint*)&raw_value);
1007     return lookup(value);
1008   }
1009 };
1010 
1011 typedef BoxCache<jint, java_lang_Integer_IntegerCache, java_lang_Integer> IntegerBoxCache;
1012 typedef BoxCache<jlong, java_lang_Long_LongCache, java_lang_Long> LongBoxCache;
1013 typedef BoxCache<jchar, java_lang_Character_CharacterCache, java_lang_Character> CharacterBoxCache;
1014 typedef BoxCache<jshort, java_lang_Short_ShortCache, java_lang_Short> ShortBoxCache;
1015 typedef BoxCache<jbyte, java_lang_Byte_ByteCache, java_lang_Byte> ByteBoxCache;
1016 
1017 template<> BoxCache<jint, java_lang_Integer_IntegerCache, java_lang_Integer>* BoxCache<jint, java_lang_Integer_IntegerCache, java_lang_Integer>::_singleton = NULL;
1018 template<> BoxCache<jlong, java_lang_Long_LongCache, java_lang_Long>* BoxCache<jlong, java_lang_Long_LongCache, java_lang_Long>::_singleton = NULL;
1019 template<> BoxCache<jchar, java_lang_Character_CharacterCache, java_lang_Character>* BoxCache<jchar, java_lang_Character_CharacterCache, java_lang_Character>::_singleton = NULL;
1020 template<> BoxCache<jshort, java_lang_Short_ShortCache, java_lang_Short>* BoxCache<jshort, java_lang_Short_ShortCache, java_lang_Short>::_singleton = NULL;
1021 template<> BoxCache<jbyte, java_lang_Byte_ByteCache, java_lang_Byte>* BoxCache<jbyte, java_lang_Byte_ByteCache, java_lang_Byte>::_singleton = NULL;
1022 
1023 class BooleanBoxCache : public BoxCacheBase<java_lang_Boolean> {
1024   jobject _true_cache;
1025   jobject _false_cache;
1026 protected:
1027   static BooleanBoxCache *_singleton;
1028   BooleanBoxCache(Thread *thread) {
1029     InstanceKlass* ik = find_cache_klass(java_lang_Boolean::symbol());
1030     _true_cache = JNIHandles::make_global(Handle(thread, java_lang_Boolean::get_TRUE(ik)));
1031     _false_cache = JNIHandles::make_global(Handle(thread, java_lang_Boolean::get_FALSE(ik)));
1032   }
1033   ~BooleanBoxCache() {
1034     JNIHandles::destroy_global(_true_cache);
1035     JNIHandles::destroy_global(_false_cache);
1036   }
1037 public:
1038   static BooleanBoxCache* singleton(Thread* thread) {
1039     if (_singleton == NULL) {
1040       BooleanBoxCache* s = new BooleanBoxCache(thread);
1041       if (!Atomic::replace_if_null(&_singleton, s)) {
1042         delete s;
1043       }
1044     }
1045     return _singleton;
1046   }
1047   oop lookup_raw(intptr_t raw_value) {
1048     // Have to cast to avoid little/big-endian problems.
1049     jboolean value = (jboolean)*((jint*)&raw_value);
1050     return lookup(value);
1051   }
1052   oop lookup(jboolean value) {
1053     if (value != 0) {
1054       return JNIHandles::resolve_non_null(_true_cache);
1055     }
1056     return JNIHandles::resolve_non_null(_false_cache);
1057   }
1058 };
1059 
1060 BooleanBoxCache* BooleanBoxCache::_singleton = NULL;
1061 
1062 oop Deoptimization::get_cached_box(AutoBoxObjectValue* bv, frame* fr, RegisterMap* reg_map, TRAPS) {
1063    Klass* k = java_lang_Class::as_Klass(bv->klass()->as_ConstantOopReadValue()->value()());
1064    BasicType box_type = vmClasses::box_klass_type(k);
1065    if (box_type != T_OBJECT) {
1066      StackValue* value = StackValue::create_stack_value(fr, reg_map, bv->field_at(box_type == T_LONG ? 1 : 0));
1067      switch(box_type) {
1068        case T_INT:     return IntegerBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1069        case T_CHAR:    return CharacterBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1070        case T_SHORT:   return ShortBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1071        case T_BYTE:    return ByteBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1072        case T_BOOLEAN: return BooleanBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1073        case T_LONG:    return LongBoxCache::singleton(THREAD)->lookup_raw(value->get_int());
1074        default:;
1075      }
1076    }
1077    return NULL;
1078 }
1079 
1080 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS) {
1081   Handle pending_exception(THREAD, thread->pending_exception());
1082   const char* exception_file = thread->exception_file();
1083   int exception_line = thread->exception_line();
1084   thread->clear_pending_exception();
1085 
1086   bool failures = false;
1087 
1088   for (int i = 0; i < objects->length(); i++) {
1089     assert(objects->at(i)->is_object(), "invalid debug information");
1090     ObjectValue* sv = (ObjectValue*) objects->at(i);
1091 
1092     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
1093     oop obj = NULL;
1094 
1095     if (k->is_instance_klass()) {
1096       if (sv->is_auto_box()) {
1097         AutoBoxObjectValue* abv = (AutoBoxObjectValue*) sv;
1098         obj = get_cached_box(abv, fr, reg_map, THREAD);
1099         if (obj != NULL) {
1100           // Set the flag to indicate the box came from a cache, so that we can skip the field reassignment for it.
1101           abv->set_cached(true);
1102         }
1103       }
1104 
1105       InstanceKlass* ik = InstanceKlass::cast(k);
1106       if (obj == NULL) {
1107 #ifdef COMPILER2
1108         if (EnableVectorSupport && VectorSupport::is_vector(ik)) {
1109           obj = VectorSupport::allocate_vector(ik, fr, reg_map, sv, THREAD);
1110         } else {
1111           obj = ik->allocate_instance(THREAD);
1112         }
1113 #else
1114         obj = ik->allocate_instance(THREAD);
1115 #endif // COMPILER2
1116       }
1117     } else if (k->is_typeArray_klass()) {
1118       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1119       assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
1120       int len = sv->field_size() / type2size[ak->element_type()];
1121       obj = ak->allocate(len, THREAD);
1122     } else if (k->is_objArray_klass()) {
1123       ObjArrayKlass* ak = ObjArrayKlass::cast(k);
1124       obj = ak->allocate(sv->field_size(), THREAD);
1125     }
1126 
1127     if (obj == NULL) {
1128       failures = true;
1129     }
1130 
1131     assert(sv->value().is_null(), "redundant reallocation");
1132     assert(obj != NULL || HAS_PENDING_EXCEPTION, "allocation should succeed or we should get an exception");
1133     CLEAR_PENDING_EXCEPTION;
1134     sv->set_value(obj);
1135   }
1136 
1137   if (failures) {
1138     THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
1139   } else if (pending_exception.not_null()) {
1140     thread->set_pending_exception(pending_exception(), exception_file, exception_line);
1141   }
1142 
1143   return failures;
1144 }
1145 
1146 #if INCLUDE_JVMCI
1147 /**
1148  * For primitive types whose kind gets "erased" at runtime (shorts become stack ints),
1149  * we need to somehow be able to recover the actual kind to be able to write the correct
1150  * amount of bytes.
1151  * For that purpose, this method assumes that, for an entry spanning n bytes at index i,
1152  * the entries at index n + 1 to n + i are 'markers'.
1153  * For example, if we were writing a short at index 4 of a byte array of size 8, the
1154  * expected form of the array would be:
1155  *
1156  * {b0, b1, b2, b3, INT, marker, b6, b7}
1157  *
1158  * Thus, in order to get back the size of the entry, we simply need to count the number
1159  * of marked entries
1160  *
1161  * @param virtualArray the virtualized byte array
1162  * @param i index of the virtual entry we are recovering
1163  * @return The number of bytes the entry spans
1164  */
1165 static int count_number_of_bytes_for_entry(ObjectValue *virtualArray, int i) {
1166   int index = i;
1167   while (++index < virtualArray->field_size() &&
1168            virtualArray->field_at(index)->is_marker()) {}
1169   return index - i;
1170 }
1171 
1172 /**
1173  * If there was a guarantee for byte array to always start aligned to a long, we could
1174  * do a simple check on the parity of the index. Unfortunately, that is not always the
1175  * case. Thus, we check alignment of the actual address we are writing to.
1176  * In the unlikely case index 0 is 5-aligned for example, it would then be possible to
1177  * write a long to index 3.
1178  */
1179 static jbyte* check_alignment_get_addr(typeArrayOop obj, int index, int expected_alignment) {
1180     jbyte* res = obj->byte_at_addr(index);
1181     assert((((intptr_t) res) % expected_alignment) == 0, "Non-aligned write");
1182     return res;
1183 }
1184 
1185 static void byte_array_put(typeArrayOop obj, intptr_t val, int index, int byte_count) {
1186   switch (byte_count) {
1187     case 1:
1188       obj->byte_at_put(index, (jbyte) *((jint *) &val));
1189       break;
1190     case 2:
1191       *((jshort *) check_alignment_get_addr(obj, index, 2)) = (jshort) *((jint *) &val);
1192       break;
1193     case 4:
1194       *((jint *) check_alignment_get_addr(obj, index, 4)) = (jint) *((jint *) &val);
1195       break;
1196     case 8:
1197       *((jlong *) check_alignment_get_addr(obj, index, 8)) = (jlong) *((jlong *) &val);
1198       break;
1199     default:
1200       ShouldNotReachHere();
1201   }
1202 }
1203 #endif // INCLUDE_JVMCI
1204 
1205 
1206 // restore elements of an eliminated type array
1207 void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) {
1208   int index = 0;
1209   intptr_t val;
1210 
1211   for (int i = 0; i < sv->field_size(); i++) {
1212     StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
1213     switch(type) {
1214     case T_LONG: case T_DOUBLE: {
1215       assert(value->type() == T_INT, "Agreement.");
1216       StackValue* low =
1217         StackValue::create_stack_value(fr, reg_map, sv->field_at(++i));
1218 #ifdef _LP64
1219       jlong res = (jlong)low->get_int();
1220 #else
1221       jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());
1222 #endif
1223       obj->long_at_put(index, res);
1224       break;
1225     }
1226 
1227     // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.
1228     case T_INT: case T_FLOAT: { // 4 bytes.
1229       assert(value->type() == T_INT, "Agreement.");
1230       bool big_value = false;
1231       if (i + 1 < sv->field_size() && type == T_INT) {
1232         if (sv->field_at(i)->is_location()) {
1233           Location::Type type = ((LocationValue*) sv->field_at(i))->location().type();
1234           if (type == Location::dbl || type == Location::lng) {
1235             big_value = true;
1236           }
1237         } else if (sv->field_at(i)->is_constant_int()) {
1238           ScopeValue* next_scope_field = sv->field_at(i + 1);
1239           if (next_scope_field->is_constant_long() || next_scope_field->is_constant_double()) {
1240             big_value = true;
1241           }
1242         }
1243       }
1244 
1245       if (big_value) {
1246         StackValue* low = StackValue::create_stack_value(fr, reg_map, sv->field_at(++i));
1247   #ifdef _LP64
1248         jlong res = (jlong)low->get_int();
1249   #else
1250         jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());
1251   #endif
1252         obj->int_at_put(index, (jint)*((jint*)&res));
1253         obj->int_at_put(++index, (jint)*(((jint*)&res) + 1));
1254       } else {
1255         val = value->get_int();
1256         obj->int_at_put(index, (jint)*((jint*)&val));
1257       }
1258       break;
1259     }
1260 
1261     case T_SHORT:
1262       assert(value->type() == T_INT, "Agreement.");
1263       val = value->get_int();
1264       obj->short_at_put(index, (jshort)*((jint*)&val));
1265       break;
1266 
1267     case T_CHAR:
1268       assert(value->type() == T_INT, "Agreement.");
1269       val = value->get_int();
1270       obj->char_at_put(index, (jchar)*((jint*)&val));
1271       break;
1272 
1273     case T_BYTE: {
1274       assert(value->type() == T_INT, "Agreement.");
1275       // The value we get is erased as a regular int. We will need to find its actual byte count 'by hand'.
1276       val = value->get_int();
1277 #if INCLUDE_JVMCI
1278       int byte_count = count_number_of_bytes_for_entry(sv, i);
1279       byte_array_put(obj, val, index, byte_count);
1280       // According to byte_count contract, the values from i + 1 to i + byte_count are illegal values. Skip.
1281       i += byte_count - 1; // Balance the loop counter.
1282       index += byte_count;
1283       // index has been updated so continue at top of loop
1284       continue;
1285 #else
1286       obj->byte_at_put(index, (jbyte)*((jint*)&val));
1287       break;
1288 #endif // INCLUDE_JVMCI
1289     }
1290 
1291     case T_BOOLEAN: {
1292       assert(value->type() == T_INT, "Agreement.");
1293       val = value->get_int();
1294       obj->bool_at_put(index, (jboolean)*((jint*)&val));
1295       break;
1296     }
1297 
1298       default:
1299         ShouldNotReachHere();
1300     }
1301     index++;
1302   }
1303 }
1304 
1305 // restore fields of an eliminated object array
1306 void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {
1307   for (int i = 0; i < sv->field_size(); i++) {
1308     StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
1309     assert(value->type() == T_OBJECT, "object element expected");
1310     obj->obj_at_put(i, value->get_obj()());
1311   }
1312 }
1313 
1314 class ReassignedField {
1315 public:
1316   int _offset;
1317   BasicType _type;
1318 public:
1319   ReassignedField() {
1320     _offset = 0;
1321     _type = T_ILLEGAL;
1322   }
1323 };
1324 
1325 int compare(ReassignedField* left, ReassignedField* right) {
1326   return left->_offset - right->_offset;
1327 }
1328 
1329 // Restore fields of an eliminated instance object using the same field order
1330 // returned by HotSpotResolvedObjectTypeImpl.getInstanceFields(true)
1331 static int reassign_fields_by_klass(InstanceKlass* klass, frame* fr, RegisterMap* reg_map, ObjectValue* sv, int svIndex, oop obj, bool skip_internal) {
1332   GrowableArray<ReassignedField>* fields = new GrowableArray<ReassignedField>();
1333   InstanceKlass* ik = klass;
1334   while (ik != NULL) {
1335     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1336       if (!fs.access_flags().is_static() && (!skip_internal || !fs.access_flags().is_internal())) {
1337         ReassignedField field;
1338         field._offset = fs.offset();
1339         field._type = Signature::basic_type(fs.signature());
1340         fields->append(field);
1341       }
1342     }
1343     ik = ik->superklass();
1344   }
1345   fields->sort(compare);
1346   for (int i = 0; i < fields->length(); i++) {
1347     intptr_t val;
1348     ScopeValue* scope_field = sv->field_at(svIndex);
1349     StackValue* value = StackValue::create_stack_value(fr, reg_map, scope_field);
1350     int offset = fields->at(i)._offset;
1351     BasicType type = fields->at(i)._type;
1352     switch (type) {
1353       case T_OBJECT: case T_ARRAY:
1354         assert(value->type() == T_OBJECT, "Agreement.");
1355         obj->obj_field_put(offset, value->get_obj()());
1356         break;
1357 
1358       // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem.
1359       case T_INT: case T_FLOAT: { // 4 bytes.
1360         assert(value->type() == T_INT, "Agreement.");
1361         bool big_value = false;
1362         if (i+1 < fields->length() && fields->at(i+1)._type == T_INT) {
1363           if (scope_field->is_location()) {
1364             Location::Type type = ((LocationValue*) scope_field)->location().type();
1365             if (type == Location::dbl || type == Location::lng) {
1366               big_value = true;
1367             }
1368           }
1369           if (scope_field->is_constant_int()) {
1370             ScopeValue* next_scope_field = sv->field_at(svIndex + 1);
1371             if (next_scope_field->is_constant_long() || next_scope_field->is_constant_double()) {
1372               big_value = true;
1373             }
1374           }
1375         }
1376 
1377         if (big_value) {
1378           i++;
1379           assert(i < fields->length(), "second T_INT field needed");
1380           assert(fields->at(i)._type == T_INT, "T_INT field needed");
1381         } else {
1382           val = value->get_int();
1383           obj->int_field_put(offset, (jint)*((jint*)&val));
1384           break;
1385         }
1386       }
1387         /* no break */
1388 
1389       case T_LONG: case T_DOUBLE: {
1390         assert(value->type() == T_INT, "Agreement.");
1391         StackValue* low = StackValue::create_stack_value(fr, reg_map, sv->field_at(++svIndex));
1392 #ifdef _LP64
1393         jlong res = (jlong)low->get_int();
1394 #else
1395         jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int());
1396 #endif
1397         obj->long_field_put(offset, res);
1398         break;
1399       }
1400 
1401       case T_SHORT:
1402         assert(value->type() == T_INT, "Agreement.");
1403         val = value->get_int();
1404         obj->short_field_put(offset, (jshort)*((jint*)&val));
1405         break;
1406 
1407       case T_CHAR:
1408         assert(value->type() == T_INT, "Agreement.");
1409         val = value->get_int();
1410         obj->char_field_put(offset, (jchar)*((jint*)&val));
1411         break;
1412 
1413       case T_BYTE:
1414         assert(value->type() == T_INT, "Agreement.");
1415         val = value->get_int();
1416         obj->byte_field_put(offset, (jbyte)*((jint*)&val));
1417         break;
1418 
1419       case T_BOOLEAN:
1420         assert(value->type() == T_INT, "Agreement.");
1421         val = value->get_int();
1422         obj->bool_field_put(offset, (jboolean)*((jint*)&val));
1423         break;
1424 
1425       default:
1426         ShouldNotReachHere();
1427     }
1428     svIndex++;
1429   }
1430   return svIndex;
1431 }
1432 
1433 // restore fields of all eliminated objects and arrays
1434 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, bool skip_internal) {
1435   for (int i = 0; i < objects->length(); i++) {
1436     ObjectValue* sv = (ObjectValue*) objects->at(i);
1437     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
1438     Handle obj = sv->value();
1439     assert(obj.not_null() || realloc_failures, "reallocation was missed");
1440     if (PrintDeoptimizationDetails) {
1441       tty->print_cr("reassign fields for object of type %s!", k->name()->as_C_string());
1442     }
1443     if (obj.is_null()) {
1444       continue;
1445     }
1446 
1447     // Don't reassign fields of boxes that came from a cache. Caches may be in CDS.
1448     if (sv->is_auto_box() && ((AutoBoxObjectValue*) sv)->is_cached()) {
1449       continue;
1450     }
1451 #ifdef COMPILER2
1452     if (EnableVectorSupport && VectorSupport::is_vector(k)) {
1453       assert(sv->field_size() == 1, "%s not a vector", k->name()->as_C_string());
1454       ScopeValue* payload = sv->field_at(0);
1455       if (payload->is_location() &&
1456           payload->as_LocationValue()->location().type() == Location::vector) {
1457         if (PrintDeoptimizationDetails) {
1458           tty->print_cr("skip field reassignment for this vector - it should be assigned already");
1459           if (Verbose) {
1460             Handle obj = sv->value();
1461             k->oop_print_on(obj(), tty);
1462           }
1463         }
1464         continue; // Such vector's value was already restored in VectorSupport::allocate_vector().
1465       }
1466       // Else fall-through to do assignment for scalar-replaced boxed vector representation
1467       // which could be restored after vector object allocation.
1468     }
1469 #endif
1470     if (k->is_instance_klass()) {
1471       InstanceKlass* ik = InstanceKlass::cast(k);
1472       reassign_fields_by_klass(ik, fr, reg_map, sv, 0, obj(), skip_internal);
1473     } else if (k->is_typeArray_klass()) {
1474       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1475       reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());
1476     } else if (k->is_objArray_klass()) {
1477       reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());
1478     }
1479   }
1480 }
1481 
1482 
1483 // relock objects for which synchronization was eliminated
1484 bool Deoptimization::relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,
1485                                     JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures) {
1486   bool relocked_objects = false;
1487   for (int i = 0; i < monitors->length(); i++) {
1488     MonitorInfo* mon_info = monitors->at(i);
1489     if (mon_info->eliminated()) {
1490       assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed");
1491       relocked_objects = true;
1492       if (!mon_info->owner_is_scalar_replaced()) {
1493         Handle obj(thread, mon_info->owner());
1494         markWord mark = obj->mark();
1495         if (UseBiasedLocking && mark.has_bias_pattern()) {
1496           // New allocated objects may have the mark set to anonymously biased.
1497           // Also the deoptimized method may called methods with synchronization
1498           // where the thread-local object is bias locked to the current thread.
1499           assert(mark.is_biased_anonymously() ||
1500                  mark.biased_locker() == deoptee_thread, "should be locked to current thread");
1501           // Reset mark word to unbiased prototype.
1502           markWord unbiased_prototype = markWord::prototype().set_age(mark.age());
1503           obj->set_mark(unbiased_prototype);
1504         } else if (exec_mode == Unpack_none) {
1505           if (LockingMode == LM_LEGACY && mark.has_locker() && fr.sp() > (intptr_t*)mark.locker()) {
1506             // With exec_mode == Unpack_none obj may be thread local and locked in
1507             // a callee frame. In this case the bias was revoked before in revoke_for_object_deoptimization().
1508             // Make the lock in the callee a recursive lock and restore the displaced header.
1509             markWord dmw = mark.displaced_mark_helper();
1510             mark.locker()->set_displaced_header(markWord::encode((BasicLock*) NULL));
1511             obj->set_mark(dmw);
1512           }
1513           if (mark.has_monitor()) {
1514             // defer relocking if the deoptee thread is currently waiting for obj
1515             ObjectMonitor* waiting_monitor = deoptee_thread->current_waiting_monitor();
1516             if (waiting_monitor != NULL && waiting_monitor->object() == obj()) {
1517               assert(fr.is_deoptimized_frame(), "frame must be scheduled for deoptimization");
1518               mon_info->lock()->set_displaced_header(markWord::unused_mark());
1519               JvmtiDeferredUpdates::inc_relock_count_after_wait(deoptee_thread);
1520               continue;
1521             }
1522           }
1523         }
1524         if (LockingMode == LM_LIGHTWEIGHT && exec_mode == Unpack_none) {
1525           // We have lost information about the correct state of the lock stack.
1526           // Inflate the locks instead. Enter then inflate to avoid races with
1527           // deflation.
1528           ObjectSynchronizer::enter_for(obj, nullptr, deoptee_thread);
1529           assert(mon_info->owner()->is_locked(), "object must be locked now");
1530           ObjectMonitor* mon = ObjectSynchronizer::inflate_for(deoptee_thread, obj(), ObjectSynchronizer::inflate_cause_vm_internal);
1531           assert(mon->owner() == deoptee_thread, "must be");
1532         } else {
1533           BasicLock* lock = mon_info->lock();
1534           ObjectSynchronizer::enter_for(obj, lock, deoptee_thread);
1535           assert(mon_info->owner()->is_locked(), "object must be locked now");
1536         }
1537       }
1538     }
1539   }
1540   return relocked_objects;
1541 }
1542 #endif // COMPILER2_OR_JVMCI
1543 
1544 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
1545   Events::log_deopt_message(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, p2i(fr.pc()), p2i(fr.sp()));
1546 
1547 #ifndef PRODUCT
1548   if (PrintDeoptimizationDetails) {
1549     ResourceMark rm;
1550     stringStream st;
1551     st.print("DEOPT PACKING thread " INTPTR_FORMAT " ", p2i(thread));
1552     fr.print_on(&st);
1553     st.print_cr("     Virtual frames (innermost first):");
1554     for (int index = 0; index < chunk->length(); index++) {
1555       compiledVFrame* vf = chunk->at(index);
1556       st.print("       %2d - ", index);
1557       vf->print_value_on(&st);
1558       int bci = chunk->at(index)->raw_bci();
1559       const char* code_name;
1560       if (bci == SynchronizationEntryBCI) {
1561         code_name = "sync entry";
1562       } else {
1563         Bytecodes::Code code = vf->method()->code_at(bci);
1564         code_name = Bytecodes::name(code);
1565       }
1566       st.print(" - %s", code_name);
1567       st.print_cr(" @ bci %d ", bci);
1568       if (Verbose) {
1569         vf->print_on(&st);
1570         st.cr();
1571       }
1572     }
1573     tty->print_raw(st.as_string());
1574   }
1575 #endif
1576 
1577   // Register map for next frame (used for stack crawl).  We capture
1578   // the state of the deopt'ing frame's caller.  Thus if we need to
1579   // stuff a C2I adapter we can properly fill in the callee-save
1580   // register locations.
1581   frame caller = fr.sender(reg_map);
1582   int frame_size = caller.sp() - fr.sp();
1583 
1584   frame sender = caller;
1585 
1586   // Since the Java thread being deoptimized will eventually adjust it's own stack,
1587   // the vframeArray containing the unpacking information is allocated in the C heap.
1588   // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
1589   vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);
1590 
1591   // Compare the vframeArray to the collected vframes
1592   assert(array->structural_compare(thread, chunk), "just checking");
1593 
1594 #ifndef PRODUCT
1595   if (PrintDeoptimizationDetails) {
1596     tty->print_cr("     Created vframeArray " INTPTR_FORMAT, p2i(array));
1597   }
1598 #endif // PRODUCT
1599 
1600   return array;
1601 }
1602 
1603 #if COMPILER2_OR_JVMCI
1604 void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {
1605   // Reallocation of some scalar replaced objects failed. Record
1606   // that we need to pop all the interpreter frames for the
1607   // deoptimized compiled frame.
1608   assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");
1609   thread->set_frames_to_pop_failed_realloc(array->frames());
1610   // Unlock all monitors here otherwise the interpreter will see a
1611   // mix of locked and unlocked monitors (because of failed
1612   // reallocations of synchronized objects) and be confused.
1613   for (int i = 0; i < array->frames(); i++) {
1614     MonitorChunk* monitors = array->element(i)->monitors();
1615     if (monitors != NULL) {
1616       for (int j = 0; j < monitors->number_of_monitors(); j++) {
1617         BasicObjectLock* src = monitors->at(j);
1618         if (src->obj() != NULL) {
1619           ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
1620         }
1621       }
1622       array->element(i)->free_monitors(thread);
1623 #ifdef ASSERT
1624       array->element(i)->set_removed_monitors();
1625 #endif
1626     }
1627   }
1628 }
1629 #endif
1630 
1631 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke,
1632                              bool only_eliminated) {
1633   GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1634   Thread* thread = Thread::current();
1635   for (int i = 0; i < monitors->length(); i++) {
1636     MonitorInfo* mon_info = monitors->at(i);
1637     if (mon_info->eliminated() == only_eliminated &&
1638         !mon_info->owner_is_scalar_replaced() &&
1639         mon_info->owner() != NULL) {
1640       objects_to_revoke->append(Handle(thread, mon_info->owner()));
1641     }
1642   }
1643 }
1644 
1645 static void get_monitors_from_stack(GrowableArray<Handle>* objects_to_revoke, JavaThread* thread,
1646                                     frame fr, RegisterMap* map, bool only_eliminated) {
1647   // Unfortunately we don't have a RegisterMap available in most of
1648   // the places we want to call this routine so we need to walk the
1649   // stack again to update the register map.
1650   if (map == NULL || !map->update_map()) {
1651     StackFrameStream sfs(thread, true /* update */, true /* process_frames */);
1652     bool found = false;
1653     while (!found && !sfs.is_done()) {
1654       frame* cur = sfs.current();
1655       sfs.next();
1656       found = cur->id() == fr.id();
1657     }
1658     assert(found, "frame to be deoptimized not found on target thread's stack");
1659     map = sfs.register_map();
1660   }
1661 
1662   vframe* vf = vframe::new_vframe(&fr, map, thread);
1663   compiledVFrame* cvf = compiledVFrame::cast(vf);
1664   // Revoke monitors' biases in all scopes
1665   while (!cvf->is_top()) {
1666     collect_monitors(cvf, objects_to_revoke, only_eliminated);
1667     cvf = compiledVFrame::cast(cvf->sender());
1668   }
1669   collect_monitors(cvf, objects_to_revoke, only_eliminated);
1670 }
1671 
1672 void Deoptimization::revoke_from_deopt_handler(JavaThread* thread, frame fr, RegisterMap* map) {
1673   if (!UseBiasedLocking) {
1674     return;
1675   }
1676   assert(thread == Thread::current(), "should be");
1677   ResourceMark rm(thread);
1678   HandleMark hm(thread);
1679   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1680   get_monitors_from_stack(objects_to_revoke, thread, fr, map, false);
1681 
1682   int len = objects_to_revoke->length();
1683   for (int i = 0; i < len; i++) {
1684     oop obj = (objects_to_revoke->at(i))();
1685     BiasedLocking::revoke_own_lock(thread, objects_to_revoke->at(i));
1686     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
1687   }
1688 }
1689 
1690 // Revoke the bias of objects with eliminated locking to prepare subsequent relocking.
1691 void Deoptimization::revoke_for_object_deoptimization(JavaThread* deoptee_thread, frame fr,
1692                                                       RegisterMap* map, JavaThread* thread) {
1693   if (!UseBiasedLocking) {
1694     return;
1695   }
1696   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1697   assert(KeepStackGCProcessedMark::stack_is_kept_gc_processed(deoptee_thread), "must be");
1698   // Collect monitors but only those with eliminated locking.
1699   get_monitors_from_stack(objects_to_revoke, deoptee_thread, fr, map, true);
1700 
1701   int len = objects_to_revoke->length();
1702   for (int i = 0; i < len; i++) {
1703     oop obj = (objects_to_revoke->at(i))();
1704     markWord mark = obj->mark();
1705     if (!mark.has_bias_pattern() ||
1706         mark.is_biased_anonymously() || // eliminated locking does not bias an object if it wasn't before
1707         !obj->klass()->prototype_header().has_bias_pattern() || // bulk revoke ignores eliminated monitors
1708         (obj->klass()->prototype_header().bias_epoch() != mark.bias_epoch())) { // bulk rebias ignores eliminated monitors
1709       // We reach here regularly if there's just eliminated locking on obj.
1710       // We must not call BiasedLocking::revoke_own_lock() in this case, as we
1711       // would hit assertions because it is a prerequisite that there has to be
1712       // non-eliminated locking on obj by deoptee_thread.
1713       // Luckily we don't have to revoke here because obj has to be a
1714       // non-escaping obj and can be relocked without revoking the bias. See
1715       // Deoptimization::relock_objects().
1716       continue;
1717     }
1718     BiasedLocking::revoke(thread, objects_to_revoke->at(i));
1719     assert(!objects_to_revoke->at(i)->mark().has_bias_pattern(), "biases should be revoked by now");
1720   }
1721 }
1722 
1723 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1724   assert(fr.can_be_deoptimized(), "checking frame type");
1725 
1726   gather_statistics(reason, Action_none, Bytecodes::_illegal);
1727 
1728   if (LogCompilation && xtty != NULL) {
1729     CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1730     assert(cm != NULL, "only compiled methods can deopt");
1731 
1732     ttyLocker ttyl;
1733     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1734     cm->log_identity(xtty);
1735     xtty->end_head();
1736     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1737       xtty->begin_elem("jvms bci='%d'", sd->bci());
1738       xtty->method(sd->method());
1739       xtty->end_elem();
1740       if (sd->is_top())  break;
1741     }
1742     xtty->tail("deoptimized");
1743   }
1744 
1745   // Patch the compiled method so that when execution returns to it we will
1746   // deopt the execution state and return to the interpreter.
1747   fr.deoptimize(thread);
1748 }
1749 
1750 void Deoptimization::deoptimize(JavaThread* thread, frame fr, DeoptReason reason) {
1751   // Deoptimize only if the frame comes from compile code.
1752   // Do not deoptimize the frame which is already patched
1753   // during the execution of the loops below.
1754   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1755     return;
1756   }
1757   ResourceMark rm;
1758   DeoptimizationMarker dm;
1759   deoptimize_single_frame(thread, fr, reason);
1760 }
1761 
1762 #if INCLUDE_JVMCI
1763 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1764   // there is no exception handler for this pc => deoptimize
1765   cm->make_not_entrant();
1766 
1767   // Use Deoptimization::deoptimize for all of its side-effects:
1768   // gathering traps statistics, logging...
1769   // it also patches the return pc but we do not care about that
1770   // since we return a continuation to the deopt_blob below.
1771   JavaThread* thread = JavaThread::current();
1772   RegisterMap reg_map(thread, false);
1773   frame runtime_frame = thread->last_frame();
1774   frame caller_frame = runtime_frame.sender(&reg_map);
1775   assert(caller_frame.cb()->as_compiled_method_or_null() == cm, "expect top frame compiled method");
1776   vframe* vf = vframe::new_vframe(&caller_frame, &reg_map, thread);
1777   compiledVFrame* cvf = compiledVFrame::cast(vf);
1778   ScopeDesc* imm_scope = cvf->scope();
1779   MethodData* imm_mdo = get_method_data(thread, methodHandle(thread, imm_scope->method()), true);
1780   if (imm_mdo != NULL) {
1781     ProfileData* pdata = imm_mdo->allocate_bci_to_data(imm_scope->bci(), NULL);
1782     if (pdata != NULL && pdata->is_BitData()) {
1783       BitData* bit_data = (BitData*) pdata;
1784       bit_data->set_exception_seen();
1785     }
1786   }
1787 
1788   Deoptimization::deoptimize(thread, caller_frame, Deoptimization::Reason_not_compiled_exception_handler);
1789 
1790   MethodData* trap_mdo = get_method_data(thread, methodHandle(thread, cm->method()), true);
1791   if (trap_mdo != NULL) {
1792     trap_mdo->inc_trap_count(Deoptimization::Reason_not_compiled_exception_handler);
1793   }
1794 
1795   return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
1796 }
1797 #endif
1798 
1799 void Deoptimization::deoptimize_frame_internal(JavaThread* thread, intptr_t* id, DeoptReason reason) {
1800   assert(thread == Thread::current() ||
1801          thread->is_handshake_safe_for(Thread::current()) ||
1802          SafepointSynchronize::is_at_safepoint(),
1803          "can only deoptimize other thread at a safepoint/handshake");
1804   // Compute frame and register map based on thread and sp.
1805   RegisterMap reg_map(thread, false);
1806   frame fr = thread->last_frame();
1807   while (fr.id() != id) {
1808     fr = fr.sender(&reg_map);
1809   }
1810   deoptimize(thread, fr, reason);
1811 }
1812 
1813 
1814 void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id, DeoptReason reason) {
1815   Thread* current = Thread::current();
1816   if (thread == current || thread->is_handshake_safe_for(current)) {
1817     Deoptimization::deoptimize_frame_internal(thread, id, reason);
1818   } else {
1819     VM_DeoptimizeFrame deopt(thread, id, reason);
1820     VMThread::execute(&deopt);
1821   }
1822 }
1823 
1824 void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) {
1825   deoptimize_frame(thread, id, Reason_constraint);
1826 }
1827 
1828 // JVMTI PopFrame support
1829 JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address))
1830 {
1831   thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address);
1832 }
1833 JRT_END
1834 
1835 MethodData*
1836 Deoptimization::get_method_data(JavaThread* thread, const methodHandle& m,
1837                                 bool create_if_missing) {
1838   JavaThread* THREAD = thread; // For exception macros.
1839   MethodData* mdo = m()->method_data();
1840   if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) {
1841     // Build an MDO.  Ignore errors like OutOfMemory;
1842     // that simply means we won't have an MDO to update.
1843     Method::build_interpreter_method_data(m, THREAD);
1844     if (HAS_PENDING_EXCEPTION) {
1845       // Only metaspace OOM is expected. No Java code executed.
1846       assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOM error here");
1847       CLEAR_PENDING_EXCEPTION;
1848     }
1849     mdo = m()->method_data();
1850   }
1851   return mdo;
1852 }
1853 
1854 #if COMPILER2_OR_JVMCI
1855 void Deoptimization::load_class_by_index(const constantPoolHandle& constant_pool, int index, TRAPS) {
1856   // In case of an unresolved klass entry, load the class.
1857   // This path is exercised from case _ldc in Parse::do_one_bytecode,
1858   // and probably nowhere else.
1859   // Even that case would benefit from simply re-interpreting the
1860   // bytecode, without paying special attention to the class index.
1861   // So this whole "class index" feature should probably be removed.
1862 
1863   if (constant_pool->tag_at(index).is_unresolved_klass()) {
1864     Klass* tk = constant_pool->klass_at(index, THREAD);
1865     if (HAS_PENDING_EXCEPTION) {
1866       // Exception happened during classloading. We ignore the exception here, since it
1867       // is going to be rethrown since the current activation is going to be deoptimized and
1868       // the interpreter will re-execute the bytecode.
1869       // Do not clear probable Async Exceptions.
1870       CLEAR_PENDING_NONASYNC_EXCEPTION;
1871       // Class loading called java code which may have caused a stack
1872       // overflow. If the exception was thrown right before the return
1873       // to the runtime the stack is no longer guarded. Reguard the
1874       // stack otherwise if we return to the uncommon trap blob and the
1875       // stack bang causes a stack overflow we crash.
1876       JavaThread* jt = THREAD;
1877       bool guard_pages_enabled = jt->stack_overflow_state()->reguard_stack_if_needed();
1878       assert(guard_pages_enabled, "stack banging in uncommon trap blob may cause crash");
1879     }
1880     return;
1881   }
1882 
1883   assert(!constant_pool->tag_at(index).is_symbol(),
1884          "no symbolic names here, please");
1885 }
1886 
1887 #if INCLUDE_JFR
1888 
1889 class DeoptReasonSerializer : public JfrSerializer {
1890  public:
1891   void serialize(JfrCheckpointWriter& writer) {
1892     writer.write_count((u4)(Deoptimization::Reason_LIMIT + 1)); // + Reason::many (-1)
1893     for (int i = -1; i < Deoptimization::Reason_LIMIT; ++i) {
1894       writer.write_key((u8)i);
1895       writer.write(Deoptimization::trap_reason_name(i));
1896     }
1897   }
1898 };
1899 
1900 class DeoptActionSerializer : public JfrSerializer {
1901  public:
1902   void serialize(JfrCheckpointWriter& writer) {
1903     static const u4 nof_actions = Deoptimization::Action_LIMIT;
1904     writer.write_count(nof_actions);
1905     for (u4 i = 0; i < Deoptimization::Action_LIMIT; ++i) {
1906       writer.write_key(i);
1907       writer.write(Deoptimization::trap_action_name((int)i));
1908     }
1909   }
1910 };
1911 
1912 static void register_serializers() {
1913   static int critical_section = 0;
1914   if (1 == critical_section || Atomic::cmpxchg(&critical_section, 0, 1) == 1) {
1915     return;
1916   }
1917   JfrSerializer::register_serializer(TYPE_DEOPTIMIZATIONREASON, true, new DeoptReasonSerializer());
1918   JfrSerializer::register_serializer(TYPE_DEOPTIMIZATIONACTION, true, new DeoptActionSerializer());
1919 }
1920 
1921 static void post_deoptimization_event(CompiledMethod* nm,
1922                                       const Method* method,
1923                                       int trap_bci,
1924                                       int instruction,
1925                                       Deoptimization::DeoptReason reason,
1926                                       Deoptimization::DeoptAction action) {
1927   assert(nm != NULL, "invariant");
1928   assert(method != NULL, "invariant");
1929   if (EventDeoptimization::is_enabled()) {
1930     static bool serializers_registered = false;
1931     if (!serializers_registered) {
1932       register_serializers();
1933       serializers_registered = true;
1934     }
1935     EventDeoptimization event;
1936     event.set_compileId(nm->compile_id());
1937     event.set_compiler(nm->compiler_type());
1938     event.set_method(method);
1939     event.set_lineNumber(method->line_number_from_bci(trap_bci));
1940     event.set_bci(trap_bci);
1941     event.set_instruction(instruction);
1942     event.set_reason(reason);
1943     event.set_action(action);
1944     event.commit();
1945   }
1946 }
1947 
1948 #endif // INCLUDE_JFR
1949 
1950 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
1951   HandleMark hm(current);
1952 
1953   // uncommon_trap() is called at the beginning of the uncommon trap
1954   // handler. Note this fact before we start generating temporary frames
1955   // that can confuse an asynchronous stack walker. This counter is
1956   // decremented at the end of unpack_frames().
1957   current->inc_in_deopt_handler();
1958 
1959   // We need to update the map if we have biased locking.
1960 #if INCLUDE_JVMCI
1961   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
1962   RegisterMap reg_map(current, true);
1963 #else
1964   RegisterMap reg_map(current, UseBiasedLocking);
1965 #endif
1966   frame stub_frame = current->last_frame();
1967   frame fr = stub_frame.sender(&reg_map);
1968   // Make sure the calling nmethod is not getting deoptimized and removed
1969   // before we are done with it.
1970   nmethodLocker nl(fr.pc());
1971 
1972   // Log a message
1973   Events::log_deopt_message(current, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
1974               trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
1975 
1976   {
1977     ResourceMark rm;
1978 
1979     DeoptReason reason = trap_request_reason(trap_request);
1980     DeoptAction action = trap_request_action(trap_request);
1981 #if INCLUDE_JVMCI
1982     int debug_id = trap_request_debug_id(trap_request);
1983 #endif
1984     jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1985 
1986     vframe*  vf  = vframe::new_vframe(&fr, &reg_map, current);
1987     compiledVFrame* cvf = compiledVFrame::cast(vf);
1988 
1989     CompiledMethod* nm = cvf->code();
1990 
1991     ScopeDesc*      trap_scope  = cvf->scope();
1992 
1993     bool is_receiver_constraint_failure = COMPILER2_PRESENT(VerifyReceiverTypes &&) (reason == Deoptimization::Reason_receiver_constraint);
1994 
1995     if (TraceDeoptimization || is_receiver_constraint_failure) {
1996       tty->print_cr("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT ", method=%s" JVMCI_ONLY(", debug_id=%d"), trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin(), trap_scope->method()->name_and_sig_as_C_string()
1997 #if INCLUDE_JVMCI
1998           , debug_id
1999 #endif
2000           );
2001     }
2002 
2003     methodHandle    trap_method(current, trap_scope->method());
2004     int             trap_bci    = trap_scope->bci();
2005 #if INCLUDE_JVMCI
2006     jlong           speculation = current->pending_failed_speculation();
2007     if (nm->is_compiled_by_jvmci()) {
2008       nm->as_nmethod()->update_speculation(current);
2009     } else {
2010       assert(speculation == 0, "There should not be a speculation for methods compiled by non-JVMCI compilers");
2011     }
2012 
2013     if (trap_bci == SynchronizationEntryBCI) {
2014       trap_bci = 0;
2015       current->set_pending_monitorenter(true);
2016     }
2017 
2018     if (reason == Deoptimization::Reason_transfer_to_interpreter) {
2019       current->set_pending_transfer_to_interpreter(true);
2020     }
2021 #endif
2022 
2023     Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
2024     // Record this event in the histogram.
2025     gather_statistics(reason, action, trap_bc);
2026 
2027     // Ensure that we can record deopt. history:
2028     // Need MDO to record RTM code generation state.
2029     bool create_if_missing = ProfileTraps || UseCodeAging RTM_OPT_ONLY( || UseRTMLocking );
2030 
2031     methodHandle profiled_method;
2032 #if INCLUDE_JVMCI
2033     if (nm->is_compiled_by_jvmci()) {
2034       profiled_method = methodHandle(current, nm->method());
2035     } else {
2036       profiled_method = trap_method;
2037     }
2038 #else
2039     profiled_method = trap_method;
2040 #endif
2041 
2042     MethodData* trap_mdo =
2043       get_method_data(current, profiled_method, create_if_missing);
2044 
2045     JFR_ONLY(post_deoptimization_event(nm, trap_method(), trap_bci, trap_bc, reason, action);)
2046 
2047     // Log a message
2048     Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s",
2049                               trap_reason_name(reason), trap_action_name(action), p2i(fr.pc()),
2050                               trap_method->name_and_sig_as_C_string(), trap_bci, nm->compiler_name());
2051 
2052     // Print a bunch of diagnostics, if requested.
2053     if (TraceDeoptimization || LogCompilation || is_receiver_constraint_failure) {
2054       ResourceMark rm;
2055       ttyLocker ttyl;
2056       char buf[100];
2057       if (xtty != NULL) {
2058         xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT "' %s",
2059                          os::current_thread_id(),
2060                          format_trap_request(buf, sizeof(buf), trap_request));
2061 #if INCLUDE_JVMCI
2062         if (speculation != 0) {
2063           xtty->print(" speculation='" JLONG_FORMAT "'", speculation);
2064         }
2065 #endif
2066         nm->log_identity(xtty);
2067       }
2068       Symbol* class_name = NULL;
2069       bool unresolved = false;
2070       if (unloaded_class_index >= 0) {
2071         constantPoolHandle constants (current, trap_method->constants());
2072         if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
2073           class_name = constants->klass_name_at(unloaded_class_index);
2074           unresolved = true;
2075           if (xtty != NULL)
2076             xtty->print(" unresolved='1'");
2077         } else if (constants->tag_at(unloaded_class_index).is_symbol()) {
2078           class_name = constants->symbol_at(unloaded_class_index);
2079         }
2080         if (xtty != NULL)
2081           xtty->name(class_name);
2082       }
2083       if (xtty != NULL && trap_mdo != NULL && (int)reason < (int)MethodData::_trap_hist_limit) {
2084         // Dump the relevant MDO state.
2085         // This is the deopt count for the current reason, any previous
2086         // reasons or recompiles seen at this point.
2087         int dcnt = trap_mdo->trap_count(reason);
2088         if (dcnt != 0)
2089           xtty->print(" count='%d'", dcnt);
2090         ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
2091         int dos = (pdata == NULL)? 0: pdata->trap_state();
2092         if (dos != 0) {
2093           xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
2094           if (trap_state_is_recompiled(dos)) {
2095             int recnt2 = trap_mdo->overflow_recompile_count();
2096             if (recnt2 != 0)
2097               xtty->print(" recompiles2='%d'", recnt2);
2098           }
2099         }
2100       }
2101       if (xtty != NULL) {
2102         xtty->stamp();
2103         xtty->end_head();
2104       }
2105       if (TraceDeoptimization) {  // make noise on the tty
2106         tty->print("Uncommon trap occurred in");
2107         nm->method()->print_short_name(tty);
2108         tty->print(" compiler=%s compile_id=%d", nm->compiler_name(), nm->compile_id());
2109 #if INCLUDE_JVMCI
2110         if (nm->is_nmethod()) {
2111           const char* installed_code_name = nm->as_nmethod()->jvmci_name();
2112           if (installed_code_name != NULL) {
2113             tty->print(" (JVMCI: installed code name=%s) ", installed_code_name);
2114           }
2115         }
2116 #endif
2117         tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d" JVMCI_ONLY(" debug_id=%d"),
2118                    p2i(fr.pc()),
2119                    os::current_thread_id(),
2120                    trap_reason_name(reason),
2121                    trap_action_name(action),
2122                    unloaded_class_index
2123 #if INCLUDE_JVMCI
2124                    , debug_id
2125 #endif
2126                    );
2127         if (class_name != NULL) {
2128           tty->print(unresolved ? " unresolved class: " : " symbol: ");
2129           class_name->print_symbol_on(tty);
2130         }
2131         tty->cr();
2132       }
2133       if (xtty != NULL) {
2134         // Log the precise location of the trap.
2135         for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {
2136           xtty->begin_elem("jvms bci='%d'", sd->bci());
2137           xtty->method(sd->method());
2138           xtty->end_elem();
2139           if (sd->is_top())  break;
2140         }
2141         xtty->tail("uncommon_trap");
2142       }
2143     }
2144     // (End diagnostic printout.)
2145 
2146     if (is_receiver_constraint_failure) {
2147       fatal("missing receiver type check");
2148     }
2149 
2150     // Load class if necessary
2151     if (unloaded_class_index >= 0) {
2152       constantPoolHandle constants(current, trap_method->constants());
2153       load_class_by_index(constants, unloaded_class_index, THREAD);
2154     }
2155 
2156     // Flush the nmethod if necessary and desirable.
2157     //
2158     // We need to avoid situations where we are re-flushing the nmethod
2159     // because of a hot deoptimization site.  Repeated flushes at the same
2160     // point need to be detected by the compiler and avoided.  If the compiler
2161     // cannot avoid them (or has a bug and "refuses" to avoid them), this
2162     // module must take measures to avoid an infinite cycle of recompilation
2163     // and deoptimization.  There are several such measures:
2164     //
2165     //   1. If a recompilation is ordered a second time at some site X
2166     //   and for the same reason R, the action is adjusted to 'reinterpret',
2167     //   to give the interpreter time to exercise the method more thoroughly.
2168     //   If this happens, the method's overflow_recompile_count is incremented.
2169     //
2170     //   2. If the compiler fails to reduce the deoptimization rate, then
2171     //   the method's overflow_recompile_count will begin to exceed the set
2172     //   limit PerBytecodeRecompilationCutoff.  If this happens, the action
2173     //   is adjusted to 'make_not_compilable', and the method is abandoned
2174     //   to the interpreter.  This is a performance hit for hot methods,
2175     //   but is better than a disastrous infinite cycle of recompilations.
2176     //   (Actually, only the method containing the site X is abandoned.)
2177     //
2178     //   3. In parallel with the previous measures, if the total number of
2179     //   recompilations of a method exceeds the much larger set limit
2180     //   PerMethodRecompilationCutoff, the method is abandoned.
2181     //   This should only happen if the method is very large and has
2182     //   many "lukewarm" deoptimizations.  The code which enforces this
2183     //   limit is elsewhere (class nmethod, class Method).
2184     //
2185     // Note that the per-BCI 'is_recompiled' bit gives the compiler one chance
2186     // to recompile at each bytecode independently of the per-BCI cutoff.
2187     //
2188     // The decision to update code is up to the compiler, and is encoded
2189     // in the Action_xxx code.  If the compiler requests Action_none
2190     // no trap state is changed, no compiled code is changed, and the
2191     // computation suffers along in the interpreter.
2192     //
2193     // The other action codes specify various tactics for decompilation
2194     // and recompilation.  Action_maybe_recompile is the loosest, and
2195     // allows the compiled code to stay around until enough traps are seen,
2196     // and until the compiler gets around to recompiling the trapping method.
2197     //
2198     // The other actions cause immediate removal of the present code.
2199 
2200     // Traps caused by injected profile shouldn't pollute trap counts.
2201     bool injected_profile_trap = trap_method->has_injected_profile() &&
2202                                  (reason == Reason_intrinsic || reason == Reason_unreached);
2203 
2204     bool update_trap_state = (reason != Reason_tenured) && !injected_profile_trap;
2205     bool make_not_entrant = false;
2206     bool make_not_compilable = false;
2207     bool reprofile = false;
2208     switch (action) {
2209     case Action_none:
2210       // Keep the old code.
2211       update_trap_state = false;
2212       break;
2213     case Action_maybe_recompile:
2214       // Do not need to invalidate the present code, but we can
2215       // initiate another
2216       // Start compiler without (necessarily) invalidating the nmethod.
2217       // The system will tolerate the old code, but new code should be
2218       // generated when possible.
2219       break;
2220     case Action_reinterpret:
2221       // Go back into the interpreter for a while, and then consider
2222       // recompiling form scratch.
2223       make_not_entrant = true;
2224       // Reset invocation counter for outer most method.
2225       // This will allow the interpreter to exercise the bytecodes
2226       // for a while before recompiling.
2227       // By contrast, Action_make_not_entrant is immediate.
2228       //
2229       // Note that the compiler will track null_check, null_assert,
2230       // range_check, and class_check events and log them as if they
2231       // had been traps taken from compiled code.  This will update
2232       // the MDO trap history so that the next compilation will
2233       // properly detect hot trap sites.
2234       reprofile = true;
2235       break;
2236     case Action_make_not_entrant:
2237       // Request immediate recompilation, and get rid of the old code.
2238       // Make them not entrant, so next time they are called they get
2239       // recompiled.  Unloaded classes are loaded now so recompile before next
2240       // time they are called.  Same for uninitialized.  The interpreter will
2241       // link the missing class, if any.
2242       make_not_entrant = true;
2243       break;
2244     case Action_make_not_compilable:
2245       // Give up on compiling this method at all.
2246       make_not_entrant = true;
2247       make_not_compilable = true;
2248       break;
2249     default:
2250       ShouldNotReachHere();
2251     }
2252 
2253     // Setting +ProfileTraps fixes the following, on all platforms:
2254     // 4852688: ProfileInterpreter is off by default for ia64.  The result is
2255     // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the
2256     // recompile relies on a MethodData* to record heroic opt failures.
2257 
2258     // Whether the interpreter is producing MDO data or not, we also need
2259     // to use the MDO to detect hot deoptimization points and control
2260     // aggressive optimization.
2261     bool inc_recompile_count = false;
2262     ProfileData* pdata = NULL;
2263     if (ProfileTraps && CompilerConfig::is_c2_or_jvmci_compiler_enabled() && update_trap_state && trap_mdo != NULL) {
2264       assert(trap_mdo == get_method_data(current, profiled_method, false), "sanity");
2265       uint this_trap_count = 0;
2266       bool maybe_prior_trap = false;
2267       bool maybe_prior_recompile = false;
2268       pdata = query_update_method_data(trap_mdo, trap_bci, reason, true,
2269 #if INCLUDE_JVMCI
2270                                    nm->is_compiled_by_jvmci() && nm->is_osr_method(),
2271 #endif
2272                                    nm->method(),
2273                                    //outputs:
2274                                    this_trap_count,
2275                                    maybe_prior_trap,
2276                                    maybe_prior_recompile);
2277       // Because the interpreter also counts null, div0, range, and class
2278       // checks, these traps from compiled code are double-counted.
2279       // This is harmless; it just means that the PerXTrapLimit values
2280       // are in effect a little smaller than they look.
2281 
2282       DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
2283       if (per_bc_reason != Reason_none) {
2284         // Now take action based on the partially known per-BCI history.
2285         if (maybe_prior_trap
2286             && this_trap_count >= (uint)PerBytecodeTrapLimit) {
2287           // If there are too many traps at this BCI, force a recompile.
2288           // This will allow the compiler to see the limit overflow, and
2289           // take corrective action, if possible.  The compiler generally
2290           // does not use the exact PerBytecodeTrapLimit value, but instead
2291           // changes its tactics if it sees any traps at all.  This provides
2292           // a little hysteresis, delaying a recompile until a trap happens
2293           // several times.
2294           //
2295           // Actually, since there is only one bit of counter per BCI,
2296           // the possible per-BCI counts are {0,1,(per-method count)}.
2297           // This produces accurate results if in fact there is only
2298           // one hot trap site, but begins to get fuzzy if there are
2299           // many sites.  For example, if there are ten sites each
2300           // trapping two or more times, they each get the blame for
2301           // all of their traps.
2302           make_not_entrant = true;
2303         }
2304 
2305         // Detect repeated recompilation at the same BCI, and enforce a limit.
2306         if (make_not_entrant && maybe_prior_recompile) {
2307           // More than one recompile at this point.
2308           inc_recompile_count = maybe_prior_trap;
2309         }
2310       } else {
2311         // For reasons which are not recorded per-bytecode, we simply
2312         // force recompiles unconditionally.
2313         // (Note that PerMethodRecompilationCutoff is enforced elsewhere.)
2314         make_not_entrant = true;
2315       }
2316 
2317       // Go back to the compiler if there are too many traps in this method.
2318       if (this_trap_count >= per_method_trap_limit(reason)) {
2319         // If there are too many traps in this method, force a recompile.
2320         // This will allow the compiler to see the limit overflow, and
2321         // take corrective action, if possible.
2322         // (This condition is an unlikely backstop only, because the
2323         // PerBytecodeTrapLimit is more likely to take effect first,
2324         // if it is applicable.)
2325         make_not_entrant = true;
2326       }
2327 
2328       // Here's more hysteresis:  If there has been a recompile at
2329       // this trap point already, run the method in the interpreter
2330       // for a while to exercise it more thoroughly.
2331       if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {
2332         reprofile = true;
2333       }
2334     }
2335 
2336     // Take requested actions on the method:
2337 
2338     // Recompile
2339     if (make_not_entrant) {
2340       if (!nm->make_not_entrant()) {
2341         return; // the call did not change nmethod's state
2342       }
2343 
2344       if (pdata != NULL) {
2345         // Record the recompilation event, if any.
2346         int tstate0 = pdata->trap_state();
2347         int tstate1 = trap_state_set_recompiled(tstate0, true);
2348         if (tstate1 != tstate0)
2349           pdata->set_trap_state(tstate1);
2350       }
2351 
2352 #if INCLUDE_RTM_OPT
2353       // Restart collecting RTM locking abort statistic if the method
2354       // is recompiled for a reason other than RTM state change.
2355       // Assume that in new recompiled code the statistic could be different,
2356       // for example, due to different inlining.
2357       if ((reason != Reason_rtm_state_change) && (trap_mdo != NULL) &&
2358           UseRTMDeopt && (nm->as_nmethod()->rtm_state() != ProfileRTM)) {
2359         trap_mdo->atomic_set_rtm_state(ProfileRTM);
2360       }
2361 #endif
2362       // For code aging we count traps separately here, using make_not_entrant()
2363       // as a guard against simultaneous deopts in multiple threads.
2364       if (reason == Reason_tenured && trap_mdo != NULL) {
2365         trap_mdo->inc_tenure_traps();
2366       }
2367     }
2368 
2369     if (inc_recompile_count) {
2370       trap_mdo->inc_overflow_recompile_count();
2371       if ((uint)trap_mdo->overflow_recompile_count() >
2372           (uint)PerBytecodeRecompilationCutoff) {
2373         // Give up on the method containing the bad BCI.
2374         if (trap_method() == nm->method()) {
2375           make_not_compilable = true;
2376         } else {
2377           trap_method->set_not_compilable("overflow_recompile_count > PerBytecodeRecompilationCutoff", CompLevel_full_optimization);
2378           // But give grace to the enclosing nm->method().
2379         }
2380       }
2381     }
2382 
2383     // Reprofile
2384     if (reprofile) {
2385       CompilationPolicy::reprofile(trap_scope, nm->is_osr_method());
2386     }
2387 
2388     // Give up compiling
2389     if (make_not_compilable && !nm->method()->is_not_compilable(CompLevel_full_optimization)) {
2390       assert(make_not_entrant, "consistent");
2391       nm->method()->set_not_compilable("give up compiling", CompLevel_full_optimization);
2392     }
2393 
2394   } // Free marked resources
2395 
2396 }
2397 JRT_END
2398 
2399 ProfileData*
2400 Deoptimization::query_update_method_data(MethodData* trap_mdo,
2401                                          int trap_bci,
2402                                          Deoptimization::DeoptReason reason,
2403                                          bool update_total_trap_count,
2404 #if INCLUDE_JVMCI
2405                                          bool is_osr,
2406 #endif
2407                                          Method* compiled_method,
2408                                          //outputs:
2409                                          uint& ret_this_trap_count,
2410                                          bool& ret_maybe_prior_trap,
2411                                          bool& ret_maybe_prior_recompile) {
2412   bool maybe_prior_trap = false;
2413   bool maybe_prior_recompile = false;
2414   uint this_trap_count = 0;
2415   if (update_total_trap_count) {
2416     uint idx = reason;
2417 #if INCLUDE_JVMCI
2418     if (is_osr) {
2419       // Upper half of history array used for traps in OSR compilations
2420       idx += Reason_TRAP_HISTORY_LENGTH;
2421     }
2422 #endif
2423     uint prior_trap_count = trap_mdo->trap_count(idx);
2424     this_trap_count  = trap_mdo->inc_trap_count(idx);
2425 
2426     // If the runtime cannot find a place to store trap history,
2427     // it is estimated based on the general condition of the method.
2428     // If the method has ever been recompiled, or has ever incurred
2429     // a trap with the present reason , then this BCI is assumed
2430     // (pessimistically) to be the culprit.
2431     maybe_prior_trap      = (prior_trap_count != 0);
2432     maybe_prior_recompile = (trap_mdo->decompile_count() != 0);
2433   }
2434   ProfileData* pdata = NULL;
2435 
2436 
2437   // For reasons which are recorded per bytecode, we check per-BCI data.
2438   DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
2439   assert(per_bc_reason != Reason_none || update_total_trap_count, "must be");
2440   if (per_bc_reason != Reason_none) {
2441     // Find the profile data for this BCI.  If there isn't one,
2442     // try to allocate one from the MDO's set of spares.
2443     // This will let us detect a repeated trap at this point.
2444     pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : NULL);
2445 
2446     if (pdata != NULL) {
2447       if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) {
2448         if (LogCompilation && xtty != NULL) {
2449           ttyLocker ttyl;
2450           // no more room for speculative traps in this MDO
2451           xtty->elem("speculative_traps_oom");
2452         }
2453       }
2454       // Query the trap state of this profile datum.
2455       int tstate0 = pdata->trap_state();
2456       if (!trap_state_has_reason(tstate0, per_bc_reason))
2457         maybe_prior_trap = false;
2458       if (!trap_state_is_recompiled(tstate0))
2459         maybe_prior_recompile = false;
2460 
2461       // Update the trap state of this profile datum.
2462       int tstate1 = tstate0;
2463       // Record the reason.
2464       tstate1 = trap_state_add_reason(tstate1, per_bc_reason);
2465       // Store the updated state on the MDO, for next time.
2466       if (tstate1 != tstate0)
2467         pdata->set_trap_state(tstate1);
2468     } else {
2469       if (LogCompilation && xtty != NULL) {
2470         ttyLocker ttyl;
2471         // Missing MDP?  Leave a small complaint in the log.
2472         xtty->elem("missing_mdp bci='%d'", trap_bci);
2473       }
2474     }
2475   }
2476 
2477   // Return results:
2478   ret_this_trap_count = this_trap_count;
2479   ret_maybe_prior_trap = maybe_prior_trap;
2480   ret_maybe_prior_recompile = maybe_prior_recompile;
2481   return pdata;
2482 }
2483 
2484 void
2485 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2486   ResourceMark rm;
2487   // Ignored outputs:
2488   uint ignore_this_trap_count;
2489   bool ignore_maybe_prior_trap;
2490   bool ignore_maybe_prior_recompile;
2491   assert(!reason_is_speculate(reason), "reason speculate only used by compiler");
2492   // JVMCI uses the total counts to determine if deoptimizations are happening too frequently -> do not adjust total counts
2493   bool update_total_counts = true JVMCI_ONLY( && !UseJVMCICompiler);
2494   query_update_method_data(trap_mdo, trap_bci,
2495                            (DeoptReason)reason,
2496                            update_total_counts,
2497 #if INCLUDE_JVMCI
2498                            false,
2499 #endif
2500                            NULL,
2501                            ignore_this_trap_count,
2502                            ignore_maybe_prior_trap,
2503                            ignore_maybe_prior_recompile);
2504 }
2505 
2506 Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
2507   // Enable WXWrite: current function is called from methods compiled by C2 directly
2508   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
2509 
2510   if (TraceDeoptimization) {
2511     tty->print("Uncommon trap ");
2512   }
2513   // Still in Java no safepoints
2514   {
2515     // This enters VM and may safepoint
2516     uncommon_trap_inner(current, trap_request);
2517   }
2518   HandleMark hm(current);
2519   return fetch_unroll_info_helper(current, exec_mode);
2520 }
2521 
2522 // Local derived constants.
2523 // Further breakdown of DataLayout::trap_state, as promised by DataLayout.
2524 const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
2525 const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;
2526 
2527 //---------------------------trap_state_reason---------------------------------
2528 Deoptimization::DeoptReason
2529 Deoptimization::trap_state_reason(int trap_state) {
2530   // This assert provides the link between the width of DataLayout::trap_bits
2531   // and the encoding of "recorded" reasons.  It ensures there are enough
2532   // bits to store all needed reasons in the per-BCI MDO profile.
2533   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2534   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2535   trap_state -= recompile_bit;
2536   if (trap_state == DS_REASON_MASK) {
2537     return Reason_many;
2538   } else {
2539     assert((int)Reason_none == 0, "state=0 => Reason_none");
2540     return (DeoptReason)trap_state;
2541   }
2542 }
2543 //-------------------------trap_state_has_reason-------------------------------
2544 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2545   assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason");
2546   assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
2547   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2548   trap_state -= recompile_bit;
2549   if (trap_state == DS_REASON_MASK) {
2550     return -1;  // true, unspecifically (bottom of state lattice)
2551   } else if (trap_state == reason) {
2552     return 1;   // true, definitely
2553   } else if (trap_state == 0) {
2554     return 0;   // false, definitely (top of state lattice)
2555   } else {
2556     return 0;   // false, definitely
2557   }
2558 }
2559 //-------------------------trap_state_add_reason-------------------------------
2560 int Deoptimization::trap_state_add_reason(int trap_state, int reason) {
2561   assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason");
2562   int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
2563   trap_state -= recompile_bit;
2564   if (trap_state == DS_REASON_MASK) {
2565     return trap_state + recompile_bit;     // already at state lattice bottom
2566   } else if (trap_state == reason) {
2567     return trap_state + recompile_bit;     // the condition is already true
2568   } else if (trap_state == 0) {
2569     return reason + recompile_bit;          // no condition has yet been true
2570   } else {
2571     return DS_REASON_MASK + recompile_bit;  // fall to state lattice bottom
2572   }
2573 }
2574 //-----------------------trap_state_is_recompiled------------------------------
2575 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2576   return (trap_state & DS_RECOMPILE_BIT) != 0;
2577 }
2578 //-----------------------trap_state_set_recompiled-----------------------------
2579 int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) {
2580   if (z)  return trap_state |  DS_RECOMPILE_BIT;
2581   else    return trap_state & ~DS_RECOMPILE_BIT;
2582 }
2583 //---------------------------format_trap_state---------------------------------
2584 // This is used for debugging and diagnostics, including LogFile output.
2585 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2586                                               int trap_state) {
2587   assert(buflen > 0, "sanity");
2588   DeoptReason reason      = trap_state_reason(trap_state);
2589   bool        recomp_flag = trap_state_is_recompiled(trap_state);
2590   // Re-encode the state from its decoded components.
2591   int decoded_state = 0;
2592   if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many)
2593     decoded_state = trap_state_add_reason(decoded_state, reason);
2594   if (recomp_flag)
2595     decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag);
2596   // If the state re-encodes properly, format it symbolically.
2597   // Because this routine is used for debugging and diagnostics,
2598   // be robust even if the state is a strange value.
2599   size_t len;
2600   if (decoded_state != trap_state) {
2601     // Random buggy state that doesn't decode??
2602     len = jio_snprintf(buf, buflen, "#%d", trap_state);
2603   } else {
2604     len = jio_snprintf(buf, buflen, "%s%s",
2605                        trap_reason_name(reason),
2606                        recomp_flag ? " recompiled" : "");
2607   }
2608   return buf;
2609 }
2610 
2611 
2612 //--------------------------------statics--------------------------------------
2613 const char* Deoptimization::_trap_reason_name[] = {
2614   // Note:  Keep this in sync. with enum DeoptReason.
2615   "none",
2616   "null_check",
2617   "null_assert" JVMCI_ONLY("_or_unreached0"),
2618   "range_check",
2619   "class_check",
2620   "array_check",
2621   "intrinsic" JVMCI_ONLY("_or_type_checked_inlining"),
2622   "bimorphic" JVMCI_ONLY("_or_optimized_type_check"),
2623   "profile_predicate",
2624   "unloaded",
2625   "uninitialized",
2626   "initialized",
2627   "unreached",
2628   "unhandled",
2629   "constraint",
2630   "div0_check",
2631   "age",
2632   "predicate",
2633   "loop_limit_check",
2634   "speculate_class_check",
2635   "speculate_null_check",
2636   "speculate_null_assert",
2637   "rtm_state_change",
2638   "unstable_if",
2639   "unstable_fused_if",
2640   "receiver_constraint",
2641 #if INCLUDE_JVMCI
2642   "aliasing",
2643   "transfer_to_interpreter",
2644   "not_compiled_exception_handler",
2645   "unresolved",
2646   "jsr_mismatch",
2647 #endif
2648   "tenured"
2649 };
2650 const char* Deoptimization::_trap_action_name[] = {
2651   // Note:  Keep this in sync. with enum DeoptAction.
2652   "none",
2653   "maybe_recompile",
2654   "reinterpret",
2655   "make_not_entrant",
2656   "make_not_compilable"
2657 };
2658 
2659 const char* Deoptimization::trap_reason_name(int reason) {
2660   // Check that every reason has a name
2661   STATIC_ASSERT(sizeof(_trap_reason_name)/sizeof(const char*) == Reason_LIMIT);
2662 
2663   if (reason == Reason_many)  return "many";
2664   if ((uint)reason < Reason_LIMIT)
2665     return _trap_reason_name[reason];
2666   static char buf[20];
2667   sprintf(buf, "reason%d", reason);
2668   return buf;
2669 }
2670 const char* Deoptimization::trap_action_name(int action) {
2671   // Check that every action has a name
2672   STATIC_ASSERT(sizeof(_trap_action_name)/sizeof(const char*) == Action_LIMIT);
2673 
2674   if ((uint)action < Action_LIMIT)
2675     return _trap_action_name[action];
2676   static char buf[20];
2677   sprintf(buf, "action%d", action);
2678   return buf;
2679 }
2680 
2681 // This is used for debugging and diagnostics, including LogFile output.
2682 const char* Deoptimization::format_trap_request(char* buf, size_t buflen,
2683                                                 int trap_request) {
2684   jint unloaded_class_index = trap_request_index(trap_request);
2685   const char* reason = trap_reason_name(trap_request_reason(trap_request));
2686   const char* action = trap_action_name(trap_request_action(trap_request));
2687 #if INCLUDE_JVMCI
2688   int debug_id = trap_request_debug_id(trap_request);
2689 #endif
2690   size_t len;
2691   if (unloaded_class_index < 0) {
2692     len = jio_snprintf(buf, buflen, "reason='%s' action='%s'" JVMCI_ONLY(" debug_id='%d'"),
2693                        reason, action
2694 #if INCLUDE_JVMCI
2695                        ,debug_id
2696 #endif
2697                        );
2698   } else {
2699     len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'" JVMCI_ONLY(" debug_id='%d'"),
2700                        reason, action, unloaded_class_index
2701 #if INCLUDE_JVMCI
2702                        ,debug_id
2703 #endif
2704                        );
2705   }
2706   return buf;
2707 }
2708 
2709 juint Deoptimization::_deoptimization_hist
2710         [Deoptimization::Reason_LIMIT]
2711     [1 + Deoptimization::Action_LIMIT]
2712         [Deoptimization::BC_CASE_LIMIT]
2713   = {0};
2714 
2715 enum {
2716   LSB_BITS = 8,
2717   LSB_MASK = right_n_bits(LSB_BITS)
2718 };
2719 
2720 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2721                                        Bytecodes::Code bc) {
2722   assert(reason >= 0 && reason < Reason_LIMIT, "oob");
2723   assert(action >= 0 && action < Action_LIMIT, "oob");
2724   _deoptimization_hist[Reason_none][0][0] += 1;  // total
2725   _deoptimization_hist[reason][0][0]      += 1;  // per-reason total
2726   juint* cases = _deoptimization_hist[reason][1+action];
2727   juint* bc_counter_addr = NULL;
2728   juint  bc_counter      = 0;
2729   // Look for an unused counter, or an exact match to this BC.
2730   if (bc != Bytecodes::_illegal) {
2731     for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2732       juint* counter_addr = &cases[bc_case];
2733       juint  counter = *counter_addr;
2734       if ((counter == 0 && bc_counter_addr == NULL)
2735           || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
2736         // this counter is either free or is already devoted to this BC
2737         bc_counter_addr = counter_addr;
2738         bc_counter = counter | bc;
2739       }
2740     }
2741   }
2742   if (bc_counter_addr == NULL) {
2743     // Overflow, or no given bytecode.
2744     bc_counter_addr = &cases[BC_CASE_LIMIT-1];
2745     bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
2746   }
2747   *bc_counter_addr = bc_counter + (1 << LSB_BITS);
2748 }
2749 
2750 jint Deoptimization::total_deoptimization_count() {
2751   return _deoptimization_hist[Reason_none][0][0];
2752 }
2753 
2754 void Deoptimization::print_statistics() {
2755   juint total = total_deoptimization_count();
2756   juint account = total;
2757   if (total != 0) {
2758     ttyLocker ttyl;
2759     if (xtty != NULL)  xtty->head("statistics type='deoptimization'");
2760     tty->print_cr("Deoptimization traps recorded:");
2761     #define PRINT_STAT_LINE(name, r) \
2762       tty->print_cr("  %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
2763     PRINT_STAT_LINE("total", total);
2764     // For each non-zero entry in the histogram, print the reason,
2765     // the action, and (if specifically known) the type of bytecode.
2766     for (int reason = 0; reason < Reason_LIMIT; reason++) {
2767       for (int action = 0; action < Action_LIMIT; action++) {
2768         juint* cases = _deoptimization_hist[reason][1+action];
2769         for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
2770           juint counter = cases[bc_case];
2771           if (counter != 0) {
2772             char name[1*K];
2773             Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
2774             if (bc_case == BC_CASE_LIMIT && (int)bc == 0)
2775               bc = Bytecodes::_illegal;
2776             sprintf(name, "%s/%s/%s",
2777                     trap_reason_name(reason),
2778                     trap_action_name(action),
2779                     Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
2780             juint r = counter >> LSB_BITS;
2781             tty->print_cr("  %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
2782             account -= r;
2783           }
2784         }
2785       }
2786     }
2787     if (account != 0) {
2788       PRINT_STAT_LINE("unaccounted", account);
2789     }
2790     #undef PRINT_STAT_LINE
2791     if (xtty != NULL)  xtty->tail("statistics");
2792   }
2793 }
2794 
2795 #else // COMPILER2_OR_JVMCI
2796 
2797 
2798 // Stubs for C1 only system.
2799 bool Deoptimization::trap_state_is_recompiled(int trap_state) {
2800   return false;
2801 }
2802 
2803 const char* Deoptimization::trap_reason_name(int reason) {
2804   return "unknown";
2805 }
2806 
2807 void Deoptimization::print_statistics() {
2808   // no output
2809 }
2810 
2811 void
2812 Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
2813   // no udpate
2814 }
2815 
2816 int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
2817   return 0;
2818 }
2819 
2820 void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
2821                                        Bytecodes::Code bc) {
2822   // no update
2823 }
2824 
2825 const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
2826                                               int trap_state) {
2827   jio_snprintf(buf, buflen, "#%d", trap_state);
2828   return buf;
2829 }
2830 
2831 #endif // COMPILER2_OR_JVMCI