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