/*
 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmClasses.hpp"
#include "code/codeCache.hpp"
#include "code/debugInfoRec.hpp"
#include "code/nmethod.hpp"
#include "code/pcDesc.hpp"
#include "code/scopeDesc.hpp"
#include "compiler/compilationPolicy.hpp"
#include "compiler/compilerDefinitions.inline.hpp"
#include "gc/shared/collectedHeap.hpp"
#include "gc/shared/memAllocator.hpp"
#include "interpreter/bytecode.inline.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/interpreter.hpp"
#include "interpreter/oopMapCache.hpp"
#include "jvm.h"
#include "logging/log.hpp"
#include "logging/logLevel.hpp"
#include "logging/logMessage.hpp"
#include "logging/logStream.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/arrayOop.inline.hpp"
#include "oops/constantPool.hpp"
#include "oops/fieldStreams.inline.hpp"
#include "oops/flatArrayKlass.hpp"
#include "oops/flatArrayOop.hpp"
#include "oops/inlineKlass.inline.hpp"
#include "oops/method.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "oops/verifyOopClosure.hpp"
#include "prims/jvmtiDeferredUpdates.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiThreadState.hpp"
#include "prims/methodHandles.hpp"
#include "prims/vectorSupport.hpp"
#include "runtime/arguments.hpp"
#include "runtime/atomicAccess.hpp"
#include "runtime/basicLock.inline.hpp"
#include "runtime/continuation.hpp"
#include "runtime/continuationEntry.inline.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/escapeBarrier.hpp"
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaThread.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/keepStackGCProcessed.hpp"
#include "runtime/lockStack.inline.hpp"
#include "runtime/objectMonitor.inline.hpp"
#include "runtime/osThread.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/stackFrameStream.inline.hpp"
#include "runtime/stackValue.hpp"
#include "runtime/stackWatermarkSet.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vframe_hp.hpp"
#include "runtime/vframeArray.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/checkedCast.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/macros.hpp"
#include "utilities/preserveException.hpp"
#include "utilities/xmlstream.hpp"
#if INCLUDE_JFR
#include "jfr/jfr.inline.hpp"
#include "jfr/jfrEvents.hpp"
#include "jfr/metadata/jfrSerializer.hpp"
#endif

uint64_t DeoptimizationScope::_committed_deopt_gen = 0;
uint64_t DeoptimizationScope::_active_deopt_gen    = 1;
bool     DeoptimizationScope::_committing_in_progress = false;

DeoptimizationScope::DeoptimizationScope() : _required_gen(0) {
  DEBUG_ONLY(_deopted = false;)

  MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
  // If there is nothing to deopt _required_gen is the same as comitted.
  _required_gen = DeoptimizationScope::_committed_deopt_gen;
}

DeoptimizationScope::~DeoptimizationScope() {
  assert(_deopted, "Deopt not executed");
}

void DeoptimizationScope::mark(nmethod* nm, bool inc_recompile_counts) {
  if (!nm->can_be_deoptimized()) {
    return;
  }

  ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);

  // If it's already marked but we still need it to be deopted.
  if (nm->is_marked_for_deoptimization()) {
    dependent(nm);
    return;
  }

  MACOS_AARCH64_ONLY(os::thread_wx_enable_write());
  nmethod::DeoptimizationStatus status =
    inc_recompile_counts ? nmethod::deoptimize : nmethod::deoptimize_noupdate;
  AtomicAccess::store(&nm->_deoptimization_status, status);

  // Make sure active is not committed
  assert(DeoptimizationScope::_committed_deopt_gen < DeoptimizationScope::_active_deopt_gen, "Must be");
  assert(nm->_deoptimization_generation == 0, "Is already marked");

  nm->_deoptimization_generation = DeoptimizationScope::_active_deopt_gen;
  _required_gen                  = DeoptimizationScope::_active_deopt_gen;
}

void DeoptimizationScope::dependent(nmethod* nm) {
  ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);

  // A method marked by someone else may have a _required_gen lower than what we marked with.
  // Therefore only store it if it's higher than _required_gen.
  if (_required_gen < nm->_deoptimization_generation) {
    _required_gen = nm->_deoptimization_generation;
  }
}

void DeoptimizationScope::deoptimize_marked() {
  assert(!_deopted, "Already deopted");

  // We are not alive yet.
  if (!Universe::is_fully_initialized()) {
    DEBUG_ONLY(_deopted = true;)
    return;
  }

  // Safepoints are a special case, handled here.
  if (SafepointSynchronize::is_at_safepoint()) {
    DeoptimizationScope::_committed_deopt_gen = DeoptimizationScope::_active_deopt_gen;
    DeoptimizationScope::_active_deopt_gen++;
    Deoptimization::deoptimize_all_marked();
    DEBUG_ONLY(_deopted = true;)
    return;
  }

  uint64_t comitting = 0;
  bool wait = false;
  while (true) {
    {
      ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);

      // First we check if we or someone else already deopted the gen we want.
      if (DeoptimizationScope::_committed_deopt_gen >= _required_gen) {
        DEBUG_ONLY(_deopted = true;)
        return;
      }
      if (!_committing_in_progress) {
        // The version we are about to commit.
        comitting = DeoptimizationScope::_active_deopt_gen;
        // Make sure new marks use a higher gen.
        DeoptimizationScope::_active_deopt_gen++;
        _committing_in_progress = true;
        wait = false;
      } else {
        // Another thread is handshaking and committing a gen.
        wait = true;
      }
    }
    if (wait) {
      // Wait and let the concurrent handshake be performed.
      ThreadBlockInVM tbivm(JavaThread::current());
      os::naked_yield();
    } else {
      // Performs the handshake.
      Deoptimization::deoptimize_all_marked(); // May safepoint and an additional deopt may have occurred.
      DEBUG_ONLY(_deopted = true;)
      {
        ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);

        // Make sure that committed doesn't go backwards.
        // Should only happen if we did a deopt during a safepoint above.
        if (DeoptimizationScope::_committed_deopt_gen < comitting) {
          DeoptimizationScope::_committed_deopt_gen = comitting;
        }
        _committing_in_progress = false;

        assert(DeoptimizationScope::_committed_deopt_gen >= _required_gen, "Must be");

        return;
      }
    }
  }
}

Deoptimization::UnrollBlock::UnrollBlock(int  size_of_deoptimized_frame,
                                         int  caller_adjustment,
                                         int  caller_actual_parameters,
                                         int  number_of_frames,
                                         intptr_t* frame_sizes,
                                         address* frame_pcs,
                                         BasicType return_type,
                                         int exec_mode) {
  _size_of_deoptimized_frame = size_of_deoptimized_frame;
  _caller_adjustment         = caller_adjustment;
  _caller_actual_parameters  = caller_actual_parameters;
  _number_of_frames          = number_of_frames;
  _frame_sizes               = frame_sizes;
  _frame_pcs                 = frame_pcs;
  _register_block            = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);
  _return_type               = return_type;
  _initial_info              = 0;
  // PD (x86 only)
  _counter_temp              = 0;
  _unpack_kind               = exec_mode;
  _sender_sp_temp            = 0;

  _total_frame_sizes         = size_of_frames();
  assert(exec_mode >= 0 && exec_mode < Unpack_LIMIT, "Unexpected exec_mode");
}

Deoptimization::UnrollBlock::~UnrollBlock() {
  FREE_C_HEAP_ARRAY(_frame_sizes);
  FREE_C_HEAP_ARRAY(_frame_pcs);
  FREE_C_HEAP_ARRAY(_register_block);
}

int Deoptimization::UnrollBlock::size_of_frames() const {
  // Account first for the adjustment of the initial frame
  intptr_t result = _caller_adjustment;
  for (int index = 0; index < number_of_frames(); index++) {
    result += frame_sizes()[index];
  }
  return checked_cast<int>(result);
}

void Deoptimization::UnrollBlock::print_on(outputStream* out) {
  ResourceMark rm;
  stringStream st;
  st.print_cr("UnrollBlock");
  st.print_cr("  size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
  st.print(   "  frame_sizes: ");
  for (int index = 0; index < number_of_frames(); index++) {
    st.print("%zd ", frame_sizes()[index]);
  }
  st.cr();
  out->print_raw(st.freeze());
}

// In order to make fetch_unroll_info work properly with escape
// analysis, the method was changed from JRT_LEAF to JRT_BLOCK_ENTRY.
// The actual reallocation of previously eliminated objects occurs in realloc_objects,
// which is called from the method fetch_unroll_info_helper below.
JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* current, int exec_mode))
  // fetch_unroll_info() is called at the beginning of the deoptimization
  // handler. Note this fact before we start generating temporary frames
  // that can confuse an asynchronous stack walker. This counter is
  // decremented at the end of unpack_frames().
  current->inc_in_deopt_handler();

  if (exec_mode == Unpack_exception) {
    // When we get here, a callee has thrown an exception into a deoptimized
    // frame. That throw might have deferred stack watermark checking until
    // after unwinding. So we deal with such deferred requests here.
    StackWatermarkSet::after_unwind(current);
  }

  return fetch_unroll_info_helper(current, exec_mode);
JRT_END

#ifdef COMPILER2

static Klass* get_refined_array_klass(Klass* k, frame* fr, RegisterMap* map, ObjectValue* sv, TRAPS) {
  // If it's an array, get the properties
  if (k->is_array_klass() && !k->is_typeArray_klass()) {
    assert(k->is_unrefined_objArray_klass(), "Expected unrefined array klass");
    nmethod* nm = fr->cb()->as_nmethod_or_null();
    assert(sv->has_properties(), "Property information is missing");
    uint32_t value = checked_cast<uint32_t>(StackValue::create_stack_value(fr, map, sv->properties())->get_jint());
    ArrayDescription description = ArrayDescription::from_value(value);
    k = ObjArrayKlass::cast(k)->klass_from_description(description, THREAD);
  }
  return k;
}

// print information about reallocated objects
static void log_objects(JavaThread* current, JavaThread* deoptee_thread, frame* deoptee, RegisterMap* map,
                        GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
  LogMessage(deoptimization) msg;
  NonInterleavingLogStream ls(LogLevel::Debug, msg);
  NonInterleavingLogStream trace_ls(LogLevel::Trace, msg);

  if (ls.is_enabled()) {
    ResourceMark rm;
    ls.print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(deoptee_thread));
    fieldDescriptor fd;

    for (int i = 0; i < objects->length(); i++) {
      ObjectValue* sv = (ObjectValue*) objects->at(i);
      Handle obj = sv->value();

      if (obj.is_null()) {
        ls.print_cr("     nullptr");
        continue;
      }

      Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
      k = get_refined_array_klass(k, deoptee, map, sv, current);

      ls.print("     object <" INTPTR_FORMAT "> of type ", p2i(sv->value()()));
      k->print_value_on(&ls);
      ls.print_cr(" allocated (%zu bytes)", obj->size() * HeapWordSize);

      if (trace_ls.is_enabled()) {
        k->oop_print_on(obj(), &trace_ls);
      }
    }
  }
}

static bool rematerialize_objects(JavaThread* current, int exec_mode, nmethod* compiled_method,
                                  frame& deoptee, RegisterMap& map, GrowableArray<compiledVFrame*>* chunk,
                                  bool& deoptimized_objects) {
  bool realloc_failures = false;
  assert (chunk->at(0)->scope() != nullptr,"expect only compiled java frames");
  assert(current == JavaThread::current(), "should be current");

  JavaThread* deoptee_thread = chunk->at(0)->thread();
  assert(exec_mode == Deoptimization::Unpack_none || (deoptee_thread == current),
         "a frame can only be deoptimized by the owner thread");

  GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects_to_rematerialize(deoptee, map);

  // The flag return_oop() indicates call sites which return oop
  // in compiled code. Such sites include java method calls,
  // runtime calls (for example, used to allocate new objects/arrays
  // on slow code path) and any other calls generated in compiled code.
  // It is not guaranteed that we can get such information here only
  // by analyzing bytecode in deoptimized frames. This is why this flag
  // is set during method compilation (see Compile::Process_OopMap_Node()).
  // If the previous frame was popped or if we are dispatching an exception,
  // we don't have an oop result.
  ScopeDesc* scope = chunk->at(0)->scope();
  bool save_oop_result = scope->return_oop() && !current->popframe_forcing_deopt_reexecution() && (exec_mode == Deoptimization::Unpack_deopt);
  // In case of the return of multiple values, we must take care
  // of all oop return values.
  GrowableArray<Handle> return_oops;
  InlineKlass* vk = nullptr;
  if (save_oop_result && scope->return_scalarized()) {
    vk = InlineKlass::returned_inline_klass(map);
    if (vk != nullptr) {
      vk->save_oop_fields(map, return_oops);
      save_oop_result = false;
    }
  }
  if (save_oop_result) {
    // Reallocation may trigger GC. If deoptimization happened on return from
    // call which returns oop we need to save it since it is not in oopmap.
    oop result = deoptee.saved_oop_result(&map);
    assert(oopDesc::is_oop_or_null(result), "must be oop");
    return_oops.push(Handle(current, result));
    assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
    log_debug(deoptimization)("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(current));
  }
  if (objects != nullptr || vk != nullptr) {
    if (exec_mode == Deoptimization::Unpack_none) {
      assert(current->thread_state() == _thread_in_vm, "assumption");
      JavaThread* THREAD = current; // For exception macros.
      // Clear pending OOM if reallocation fails and return true indicating allocation failure
      if (vk != nullptr) {
        realloc_failures = Deoptimization::realloc_inline_type_result(vk, map, return_oops, CHECK_AND_CLEAR_(true));
      }
      if (objects != nullptr) {
        realloc_failures = realloc_failures || Deoptimization::realloc_objects(current, &deoptee, &map, objects, CHECK_AND_CLEAR_(true));
        guarantee(compiled_method != nullptr, "deopt must be associated with an nmethod");
        Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, CHECK_AND_CLEAR_(true));
      }
      deoptimized_objects = true;
    } else {
      JRT_BLOCK
      if (vk != nullptr) {
        realloc_failures = Deoptimization::realloc_inline_type_result(vk, map, return_oops, THREAD);
      }
      if (objects != nullptr) {
        realloc_failures = realloc_failures || Deoptimization::realloc_objects(current, &deoptee, &map, objects, THREAD);
        guarantee(compiled_method != nullptr, "deopt must be associated with an nmethod");
        Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, THREAD);
      }
      JRT_END
    }
    if (objects != nullptr)  {
      log_objects(current, deoptee_thread, &deoptee, &map, objects, realloc_failures);
    }
  }
  if (save_oop_result || vk != nullptr) {
    // Restore result.
    assert(return_oops.length() == 1, "no inline type");
    deoptee.set_saved_oop_result(&map, return_oops.pop()());
  }
  return realloc_failures;
}

static void log_eliminated_monitors(JavaThread* current, JavaThread* deoptee_thread,
                                    GrowableArray<MonitorInfo*>* monitors,
                                    int exec_mode,
                                    bool& first) {
#ifndef PRODUCT
  LogMessage(deoptimization) msg;
  NonInterleavingLogStream ls(LogLevel::Debug, msg);
  if (ls.is_enabled()) {
    ResourceMark rm(current);

    for (int j = 0; j < monitors->length(); j++) {
      MonitorInfo* mi = monitors->at(j);
      if (mi->eliminated()) {
        if (first) {
          first = false;
          ls.print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, p2i(current));
        }
        if (exec_mode == Deoptimization::Unpack_none) {
          ObjectMonitor* monitor = deoptee_thread->current_waiting_monitor();
          if (monitor != nullptr && monitor->object() == mi->owner()) {
            ls.print_cr("     object <" INTPTR_FORMAT "> DEFERRED relocking after wait", p2i(mi->owner()));
            continue;
          }
        }
        if (mi->owner_is_scalar_replaced()) {
          Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
          ls.print_cr("     failed reallocation for klass %s", k->external_name());
        } else {
          ls.print_cr("     object <" INTPTR_FORMAT "> locked", p2i(mi->owner()));
        }
      }
    }
  }
#endif // !PRODUCT
}

static void restore_eliminated_locks(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures,
                                     frame& deoptee, int exec_mode, bool& deoptimized_objects) {
  JavaThread* deoptee_thread = chunk->at(0)->thread();
  assert(!EscapeBarrier::objs_are_deoptimized(deoptee_thread, deoptee.id()), "must relock just once");
  assert(thread == Thread::current(), "should be");
  HandleMark hm(thread);
  bool first = true;

  // Start locking from outermost/oldest frame
  for (int i = (chunk->length() - 1); i >= 0; i--) {
    compiledVFrame* cvf = chunk->at(i);
    assert (cvf->scope() != nullptr,"expect only compiled java frames");
    GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
    if (monitors->is_nonempty()) {
      bool relocked = Deoptimization::relock_objects(thread, monitors, deoptee_thread, deoptee,
                                                     exec_mode, realloc_failures);
      deoptimized_objects = deoptimized_objects || relocked;
      log_eliminated_monitors(thread, deoptee_thread, monitors, exec_mode, first);
    }
  }
}

// Deoptimize objects, that is reallocate and relock them, just before they escape through JVMTI.
// The given vframes cover one physical frame.
bool Deoptimization::deoptimize_objects_internal(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk,
                                                 bool& realloc_failures) {
  frame deoptee = chunk->at(0)->fr();
  JavaThread* deoptee_thread = chunk->at(0)->thread();
  nmethod* nm = deoptee.cb()->as_nmethod_or_null();
  RegisterMap map(chunk->at(0)->register_map());
  bool deoptimized_objects = false;

  // Reallocate the non-escaping objects and restore their fields.
  if (((DoEscapeAnalysis || Arguments::is_valhalla_enabled()) && EliminateAllocations) ||
      EliminateAutoBox || EnableVectorAggressiveReboxing) {
    realloc_failures = rematerialize_objects(thread, Unpack_none, nm, deoptee, map, chunk, deoptimized_objects);
  }

  // MonitorInfo structures used in eliminate_locks are not GC safe.
  NoSafepointVerifier no_safepoint;

  // Now relock objects if synchronization on them was eliminated.
  if ((DoEscapeAnalysis || EliminateNestedLocks) && EliminateLocks) {
    restore_eliminated_locks(thread, chunk, realloc_failures, deoptee, Unpack_none, deoptimized_objects);
  }
  return deoptimized_objects;
}
#endif // COMPILER2

// This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)
Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* current, int exec_mode) {
  JFR_ONLY(Jfr::check_and_process_sample_request(current);)
  // When we get here we are about to unwind the deoptee frame. In order to
  // catch not yet safe to use frames, the following stack watermark barrier
  // poll will make such frames safe to use.
  StackWatermarkSet::before_unwind(current);

  // Note: there is a safepoint safety issue here. No matter whether we enter
  // via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once
  // the vframeArray is created.
  //

  // Allocate our special deoptimization ResourceMark
  DeoptResourceMark* dmark = new DeoptResourceMark(current);
  assert(current->deopt_mark() == nullptr, "Pending deopt!");
  current->set_deopt_mark(dmark);

  frame stub_frame = current->last_frame(); // Makes stack walkable as side effect
  RegisterMap map(current,
                  RegisterMap::UpdateMap::include,
                  RegisterMap::ProcessFrames::include,
                  RegisterMap::WalkContinuation::skip);
  RegisterMap dummy_map(current,
                        RegisterMap::UpdateMap::skip,
                        RegisterMap::ProcessFrames::include,
                        RegisterMap::WalkContinuation::skip);
  // Now get the deoptee with a valid map
  frame deoptee = stub_frame.sender(&map);
  if (exec_mode == Unpack_deopt) {
    assert(deoptee.is_deoptimized_frame(), "frame is not marked for deoptimization");
  }
  // Set the deoptee nmethod
  assert(current->deopt_compiled_method() == nullptr, "Pending deopt!");
  nmethod* nm = deoptee.cb()->as_nmethod_or_null();
  current->set_deopt_compiled_method(nm);

  if (VerifyStack) {
    current->validate_frame_layout();
  }

  // Create a growable array of VFrames where each VFrame represents an inlined
  // Java frame.  This storage is allocated with the usual system arena.
  assert(deoptee.is_compiled_frame(), "Wrong frame type");
  GrowableArray<compiledVFrame*>* chunk = new GrowableArray<compiledVFrame*>(10);
  vframe* vf = vframe::new_vframe(&deoptee, &map, current);
  while (!vf->is_top()) {
    assert(vf->is_compiled_frame(), "Wrong frame type");
    chunk->push(compiledVFrame::cast(vf));
    vf = vf->sender();
  }
  assert(vf->is_compiled_frame(), "Wrong frame type");
  chunk->push(compiledVFrame::cast(vf));

  bool realloc_failures = false;

#ifdef COMPILER2
  // Reallocate the non-escaping objects and restore their fields. Then
  // relock objects if synchronization on them was eliminated.
  if (((DoEscapeAnalysis || Arguments::is_valhalla_enabled()) && EliminateAllocations) ||
      EliminateAutoBox || EnableVectorAggressiveReboxing) {
    bool unused;
    realloc_failures = rematerialize_objects(current, exec_mode, nm, deoptee, map, chunk, unused);
  }
#endif // COMPILER2

  // Ensure that no safepoint is taken after pointers have been stored
  // in fields of rematerialized objects.  If a safepoint occurs from here on
  // out the java state residing in the vframeArray will be missed.
  // Locks may be rebaised in a safepoint.
  NoSafepointVerifier no_safepoint;

#ifdef COMPILER2
  if (((DoEscapeAnalysis || EliminateNestedLocks) && EliminateLocks) &&
      !EscapeBarrier::objs_are_deoptimized(current, deoptee.id())) {
    bool unused = false;
    restore_eliminated_locks(current, chunk, realloc_failures, deoptee, exec_mode, unused);
  }
#endif // COMPILER2

  ScopeDesc* trap_scope = chunk->at(0)->scope();
  Handle exceptionObject;
  if (trap_scope->rethrow_exception()) {
    log_develop_debug(deoptimization)("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());

    GrowableArray<ScopeValue*>* expressions = trap_scope->expressions();
    guarantee(expressions != nullptr && expressions->length() == 1, "should have only exception on stack");
    guarantee(exec_mode != Unpack_exception, "rethrow_exception set with Unpack_exception");
    ScopeValue* topOfStack = expressions->top();
    exceptionObject = StackValue::create_stack_value(&deoptee, &map, topOfStack)->get_obj();
    guarantee(exceptionObject() != nullptr, "exception oop can not be null");
  }

  vframeArray* array = create_vframeArray(current, deoptee, &map, chunk, realloc_failures);
#ifdef COMPILER2
  if (realloc_failures) {
    // This destroys all ScopedValue bindings.
    current->clear_scopedValueBindings();
    pop_frames_failed_reallocs(current, array);
  }
#endif // COMPILER2

  assert(current->vframe_array_head() == nullptr, "Pending deopt!");
  current->set_vframe_array_head(array);

  // Now that the vframeArray has been created if we have any deferred local writes
  // added by jvmti then we can free up that structure as the data is now in the
  // vframeArray

  JvmtiDeferredUpdates::delete_updates_for_frame(current, array->original().id());

  // Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info.
  CodeBlob* cb = stub_frame.cb();
  // Verify we have the right vframeArray
  assert(cb->frame_size() >= 0, "Unexpected frame size");
  intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size();
  assert(unpack_sp == deoptee.unextended_sp(), "must be");

#ifdef ASSERT
  assert(cb->is_deoptimization_stub() ||
         cb->is_uncommon_trap_stub() ||
         strcmp("Stub<DeoptimizationStub.deoptimizationHandler>", cb->name()) == 0 ||
         strcmp("Stub<UncommonTrapStub.uncommonTrapHandler>", cb->name()) == 0,
         "unexpected code blob: %s", cb->name());
#endif

  // This is a guarantee instead of an assert because if vframe doesn't match
  // we will unpack the wrong deoptimized frame and wind up in strange places
  // where it will be very difficult to figure out what went wrong. Better
  // to die an early death here than some very obscure death later when the
  // trail is cold.
  guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack");

  int number_of_frames = array->frames();

  // Compute the vframes' sizes.  Note that frame_sizes[] entries are ordered from outermost to innermost
  // virtual activation, which is the reverse of the elements in the vframes array.
  intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames, mtCompiler);
  // +1 because we always have an interpreter return address for the final slot.
  address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1, mtCompiler);
  int popframe_extra_args = 0;
  // Create an interpreter return address for the stub to use as its return
  // address so the skeletal frames are perfectly walkable
  frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0);

  // PopFrame requires that the preserved incoming arguments from the recently-popped topmost
  // activation be put back on the expression stack of the caller for reexecution
  if (JvmtiExport::can_pop_frame() && current->popframe_forcing_deopt_reexecution()) {
    popframe_extra_args = in_words(current->popframe_preserved_args_size_in_words());
  }

  // Find the current pc for sender of the deoptee. Since the sender may have been deoptimized
  // itself since the deoptee vframeArray was created we must get a fresh value of the pc rather
  // than simply use array->sender.pc(). This requires us to walk the current set of frames
  //
  frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame
  deopt_sender = deopt_sender.sender(&dummy_map);     // Now deoptee caller

  // It's possible that the number of parameters at the call site is
  // different than number of arguments in the callee when method
  // handles are used.  If the caller is interpreted get the real
  // value so that the proper amount of space can be added to it's
  // frame.
  bool caller_was_method_handle = false;
  if (deopt_sender.is_interpreted_frame()) {
    methodHandle method(current, deopt_sender.interpreter_frame_method());
    Bytecode_invoke cur(method, deopt_sender.interpreter_frame_bci());
    if (cur.has_member_arg()) {
      // This should cover all real-world cases.  One exception is a pathological chain of
      // MH.linkToXXX() linker calls, which only trusted code could do anyway.  To handle that case, we
      // would need to get the size from the resolved method entry.  Another exception would
      // be an invokedynamic with an adapter that is really a MethodHandle linker.
      caller_was_method_handle = true;
    }
  }

  //
  // frame_sizes/frame_pcs[0] oldest frame (int or c2i)
  // frame_sizes/frame_pcs[1] next oldest frame (int)
  // frame_sizes/frame_pcs[n] youngest frame (int)
  //
  // Now a pc in frame_pcs is actually the return address to the frame's caller (a frame
  // owns the space for the return address to it's caller).  Confusing ain't it.
  //
  // The vframe array can address vframes with indices running from
  // 0.._frames-1. Index  0 is the youngest frame and _frame - 1 is the oldest (root) frame.
  // When we create the skeletal frames we need the oldest frame to be in the zero slot
  // in the frame_sizes/frame_pcs so the assembly code can do a trivial walk.
  // so things look a little strange in this loop.
  //
  int callee_parameters = 0;
  int callee_locals = 0;
  for (int index = 0; index < array->frames(); index++ ) {
    // frame[number_of_frames - 1 ] = on_stack_size(youngest)
    // frame[number_of_frames - 2 ] = on_stack_size(sender(youngest))
    // frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest)))
    frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters,
                                                                                                    callee_locals,
                                                                                                    index == 0,
                                                                                                    popframe_extra_args);
    // This pc doesn't have to be perfect just good enough to identify the frame
    // as interpreted so the skeleton frame will be walkable
    // The correct pc will be set when the skeleton frame is completely filled out
    // The final pc we store in the loop is wrong and will be overwritten below
    frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0);

    callee_parameters = array->element(index)->method()->size_of_parameters();
    callee_locals = array->element(index)->method()->max_locals();
    popframe_extra_args = 0;
  }

  // Compute whether the root vframe returns a float or double value.
  BasicType return_type;
  {
    methodHandle method(current, array->element(0)->method());
    Bytecode_invoke invoke = Bytecode_invoke_check(method, array->element(0)->bci());
    return_type = invoke.is_valid() ? invoke.result_type() : T_ILLEGAL;
  }

  // Compute information for handling adapters and adjusting the frame size of the caller.
  int caller_adjustment = 0;

  // Compute the amount the oldest interpreter frame will have to adjust
  // its caller's stack by. If the caller is a compiled frame then
  // we pretend that the callee has no parameters so that the
  // extension counts for the full amount of locals and not just
  // locals-parms. This is because without a c2i adapter the parm
  // area as created by the compiled frame will not be usable by
  // the interpreter. (Depending on the calling convention there
  // may not even be enough space).

  // QQQ I'd rather see this pushed down into last_frame_adjust
  // and have it take the sender (aka caller).

  if (!deopt_sender.is_interpreted_frame() || caller_was_method_handle) {
    caller_adjustment = last_frame_adjust(0, callee_locals);
  } else if (callee_locals > callee_parameters) {
    // The caller frame may need extending to accommodate
    // non-parameter locals of the first unpacked interpreted frame.
    // Compute that adjustment.
    caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);
  }

  // If the sender is deoptimized we must retrieve the address of the handler
  // since the frame will "magically" show the original pc before the deopt
  // and we'd undo the deopt.

  frame_pcs[0] = Continuation::is_cont_barrier_frame(deoptee) ? StubRoutines::cont_returnBarrier() : deopt_sender.raw_pc();
  if (Continuation::is_continuation_enterSpecial(deopt_sender)) {
    ContinuationEntry::from_frame(deopt_sender)->set_argsize(0);
  }

  assert(CodeCache::find_blob(frame_pcs[0]) != nullptr, "bad pc");

  if (current->frames_to_pop_failed_realloc() > 0 && exec_mode != Unpack_uncommon_trap) {
    assert(current->has_pending_exception(), "should have thrown OOME");
    current->set_exception_oop(current->pending_exception());
    current->clear_pending_exception();
    exec_mode = Unpack_exception;
  }

  int caller_actual_parameters = -1; // value not used except for interpreted frames, see below
  if (deopt_sender.is_interpreted_frame()) {
    caller_actual_parameters = callee_parameters + (caller_was_method_handle ? 1 : 0);
  }

  UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord,
                                      caller_adjustment * BytesPerWord,
                                      caller_actual_parameters,
                                      number_of_frames,
                                      frame_sizes,
                                      frame_pcs,
                                      return_type,
                                      exec_mode);
  // On some platforms, we need a way to pass some platform dependent
  // information to the unpacking code so the skeletal frames come out
  // correct (initial fp value, unextended sp, ...)
  info->set_initial_info((intptr_t) array->sender().initial_deoptimization_info());

  if (array->frames() > 1) {
    if (VerifyStack) {
      log_debug(deoptimization)("Deoptimizing method containing inlining");
    }
  }

  array->set_unroll_block(info);
  return info;
}

// Called to cleanup deoptimization data structures in normal case
// after unpacking to stack and when stack overflow error occurs
void Deoptimization::cleanup_deopt_info(JavaThread *thread,
                                        vframeArray *array) {

  // Get array if coming from exception
  if (array == nullptr) {
    array = thread->vframe_array_head();
  }
  thread->set_vframe_array_head(nullptr);

  // Free the previous UnrollBlock
  vframeArray* old_array = thread->vframe_array_last();
  thread->set_vframe_array_last(array);

  if (old_array != nullptr) {
    UnrollBlock* old_info = old_array->unroll_block();
    old_array->set_unroll_block(nullptr);
    delete old_info;
    delete old_array;
  }

  // Deallocate any resource creating in this routine and any ResourceObjs allocated
  // inside the vframeArray (StackValueCollections)

  delete thread->deopt_mark();
  thread->set_deopt_mark(nullptr);
  thread->set_deopt_compiled_method(nullptr);


  if (JvmtiExport::can_pop_frame()) {
    // Regardless of whether we entered this routine with the pending
    // popframe condition bit set, we should always clear it now
    thread->clear_popframe_condition();
  }

  // unpack_frames() is called at the end of the deoptimization handler
  // and (in C2) at the end of the uncommon trap handler. Note this fact
  // so that an asynchronous stack walker can work again. This counter is
  // incremented at the beginning of fetch_unroll_info() and (in C2) at
  // the beginning of uncommon_trap().
  thread->dec_in_deopt_handler();
}

// Moved from cpu directories because none of the cpus has callee save values.
// If a cpu implements callee save values, move this to deoptimization_<cpu>.cpp.
void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {

  // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
  // the days we had adapter frames. When we deoptimize a situation where a
  // compiled caller calls a compiled caller will have registers it expects
  // to survive the call to the callee. If we deoptimize the callee the only
  // way we can restore these registers is to have the oldest interpreter
  // frame that we create restore these values. That is what this routine
  // will accomplish.

  // At the moment we have modified c2 to not have any callee save registers
  // so this problem does not exist and this routine is just a place holder.

  assert(f->is_interpreted_frame(), "must be interpreted");
}

#ifndef PRODUCT
#ifdef ASSERT
// Return true if the execution after the provided bytecode continues at the
// next bytecode in the code. This is not the case for gotos, returns, and
// throws.
static bool falls_through(Bytecodes::Code bc) {
  switch (bc) {
    case Bytecodes::_goto:
    case Bytecodes::_goto_w:
    case Bytecodes::_athrow:
    case Bytecodes::_areturn:
    case Bytecodes::_dreturn:
    case Bytecodes::_freturn:
    case Bytecodes::_ireturn:
    case Bytecodes::_lreturn:
    case Bytecodes::_jsr:
    case Bytecodes::_ret:
    case Bytecodes::_return:
    case Bytecodes::_lookupswitch:
    case Bytecodes::_tableswitch:
      return false;
    default:
      return true;
  }
}
#endif
#endif

// Return BasicType of value being returned
JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
  assert(thread == JavaThread::current(), "pre-condition");

  // We are already active in the special DeoptResourceMark any ResourceObj's we
  // allocate will be freed at the end of the routine.

  // JRT_LEAF methods don't normally allocate handles and there is a
  // NoHandleMark to enforce that. It is actually safe to use Handles
  // in a JRT_LEAF method, and sometimes desirable, but to do so we
  // must use ResetNoHandleMark to bypass the NoHandleMark, and
  // then use a HandleMark to ensure any Handles we do create are
  // cleaned up in this scope.
  ResetNoHandleMark rnhm;
  HandleMark hm(thread);

  frame stub_frame = thread->last_frame();

  Continuation::notify_deopt(thread, stub_frame.sp());

  // Since the frame to unpack is the top frame of this thread, the vframe_array_head
  // must point to the vframeArray for the unpack frame.
  vframeArray* array = thread->vframe_array_head();
  UnrollBlock* info = array->unroll_block();

  // We set the last_Java frame. But the stack isn't really parsable here. So we
  // clear it to make sure JFR understands not to try and walk stacks from events
  // in here.
  intptr_t* sp = thread->frame_anchor()->last_Java_sp();
  thread->frame_anchor()->set_last_Java_sp(nullptr);

  // Unpack the interpreter frames and any adapter frame (c2 only) we might create.
  array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());

  thread->frame_anchor()->set_last_Java_sp(sp);

  BasicType bt = info->return_type();

  // If we have an exception pending, claim that the return type is an oop
  // so the deopt_blob does not overwrite the exception_oop.

  if (exec_mode == Unpack_exception)
    bt = T_OBJECT;

  // Cleanup thread deopt data
  cleanup_deopt_info(thread, array);

#ifndef PRODUCT
  if (VerifyStack) {
    ResourceMark res_mark;
    // Clear pending exception to not break verification code (restored afterwards)
    PreserveExceptionMark pm(thread);

    thread->validate_frame_layout();

    // Verify that the just-unpacked frames match the interpreter's
    // notions of expression stack and locals
    vframeArray* cur_array = thread->vframe_array_last();
    RegisterMap rm(thread,
                   RegisterMap::UpdateMap::skip,
                   RegisterMap::ProcessFrames::include,
                   RegisterMap::WalkContinuation::skip);
    rm.set_include_argument_oops(false);
    int callee_size_of_parameters = 0;
    for (int frame_idx = 0; frame_idx < cur_array->frames(); frame_idx++) {
      bool is_top_frame = (frame_idx == 0);
      vframeArrayElement* el = cur_array->element(frame_idx);
      frame* iframe = el->iframe();
      guarantee(iframe->is_interpreted_frame(), "Wrong frame type");
      methodHandle mh(thread, iframe->interpreter_frame_method());
      bool reexecute = el->should_reexecute();

      int cur_invoke_parameter_size = 0;
      int top_frame_expression_stack_adjustment = 0;
      int max_bci = mh->code_size();
      BytecodeStream str(mh, iframe->interpreter_frame_bci());
      assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");
      Bytecodes::Code cur_code = str.next();

      if (!reexecute && !Bytecodes::is_invoke(cur_code)) {
        // We can only compute OopMaps for the before state, so we need to roll forward
        // to the next bytecode.
        assert(is_top_frame, "must be");
        assert(falls_through(cur_code), "must be");
        assert(cur_code != Bytecodes::_illegal, "illegal bytecode");
        assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");

        // Need to subtract off the size of the result type of
        // the bytecode because this is not described in the
        // debug info but returned to the interpreter in the TOS
        // caching register
        BasicType bytecode_result_type = Bytecodes::result_type(cur_code);
        if (bytecode_result_type != T_ILLEGAL) {
          top_frame_expression_stack_adjustment = type2size[bytecode_result_type];
        }
        assert(top_frame_expression_stack_adjustment >= 0, "stack adjustment must be positive");

        cur_code = str.next();
        // Reflect the fact that we have rolled forward and now need
        // top_frame_expression_stack_adjustment
        reexecute = true;
      }

      assert(cur_code != Bytecodes::_illegal, "illegal bytecode");
      assert(str.bci() < max_bci, "bci in interpreter frame out of bounds");

      // Get the oop map for this bci
      InterpreterOopMap mask;
      OopMapCache::compute_one_oop_map(mh, str.bci(), &mask);
      // Check to see if we can grab the number of outgoing arguments
      // at an uncommon trap for an invoke (where the compiler
      // generates debug info before the invoke has executed)
      if (Bytecodes::is_invoke(cur_code)) {
        Bytecode_invoke invoke(mh, str.bci());
        cur_invoke_parameter_size = invoke.size_of_parameters();
        if (!is_top_frame && invoke.has_member_arg()) {
          callee_size_of_parameters++;
        }
      }

      // Verify stack depth and oops in frame
      auto match = [&]() {
        int iframe_expr_ssize = iframe->interpreter_frame_expression_stack_size();
        // This should only be needed for C1
        if (is_top_frame && exec_mode == Unpack_exception && iframe_expr_ssize == 0) {
          return true;
        }
        if (reexecute) {
          int expr_ssize_before = iframe_expr_ssize + top_frame_expression_stack_adjustment;
          int oopmap_expr_invoke_ssize = mask.expression_stack_size() + cur_invoke_parameter_size;
          return expr_ssize_before == oopmap_expr_invoke_ssize;
        } else {
          int oopmap_expr_callee_ssize = mask.expression_stack_size() + callee_size_of_parameters;
          return iframe_expr_ssize == oopmap_expr_callee_ssize;
        }
      };
      if (!match()) {
        // Print out some information that will help us debug the problem
        tty->print_cr("Wrong number of expression stack elements during deoptimization");
        tty->print_cr("  Error occurred while verifying frame %d (0..%d, 0 is topmost)", frame_idx, cur_array->frames() - 1);
        tty->print_cr("  Current code %s", Bytecodes::name(cur_code));
        tty->print_cr("  Fabricated interpreter frame had %d expression stack elements",
                      iframe->interpreter_frame_expression_stack_size());
        tty->print_cr("  Interpreter oop map had %d expression stack elements", mask.expression_stack_size());
        tty->print_cr("  callee_size_of_parameters = %d", callee_size_of_parameters);
        tty->print_cr("  top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);
        tty->print_cr("  exec_mode = %d", exec_mode);
        tty->print_cr("  original should_reexecute = %s", el->should_reexecute() ? "true" : "false");
        tty->print_cr("  reexecute = %s%s", reexecute ? "true" : "false",
                      (reexecute != el->should_reexecute()) ? " (changed)" : "");
        tty->print_cr("  cur_invoke_parameter_size = %d", cur_invoke_parameter_size);
        tty->print_cr("  Thread = " INTPTR_FORMAT ", thread ID = %d", p2i(thread), thread->osthread()->thread_id());
        tty->print_cr("  Interpreted frames:");
        for (int k = 0; k < cur_array->frames(); k++) {
          vframeArrayElement* el = cur_array->element(k);
          tty->print_cr("    %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
        }
        cur_array->print_on_2(tty);
        guarantee(false, "wrong number of expression stack elements during deopt");
      }
      VerifyOopClosure verify;
      iframe->oops_interpreted_do(&verify, &rm, false);
      callee_size_of_parameters = mh->size_of_parameters();
    }
  }
#endif // !PRODUCT

  return bt;
JRT_END

class DeoptimizeMarkedHandshakeClosure : public HandshakeClosure {
 public:
  DeoptimizeMarkedHandshakeClosure() : HandshakeClosure("Deoptimize") {}
  void do_thread(Thread* thread) {
    JavaThread* jt = JavaThread::cast(thread);
    jt->deoptimize_marked_methods();
  }
};

void Deoptimization::deoptimize_all_marked() {
  ResourceMark rm;

  // Make the dependent methods not entrant
  CodeCache::make_marked_nmethods_deoptimized();

  DeoptimizeMarkedHandshakeClosure deopt;
  if (SafepointSynchronize::is_at_safepoint()) {
    Threads::java_threads_do(&deopt);
  } else {
    Handshake::execute(&deopt);
  }
}

Deoptimization::DeoptAction Deoptimization::_unloaded_action
  = Deoptimization::Action_reinterpret;

#ifdef COMPILER2
bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS) {
  Handle pending_exception(THREAD, thread->pending_exception());
  const char* exception_file = thread->exception_file();
  int exception_line = thread->exception_line();
  thread->clear_pending_exception();

  bool failures = false;

  for (int i = 0; i < objects->length(); i++) {
    assert(objects->at(i)->is_object(), "invalid debug information");
    ObjectValue* sv = (ObjectValue*) objects->at(i);

    Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());

    k = get_refined_array_klass(k, fr, reg_map, sv, THREAD);

    // Check if the object may be null and has an additional null_marker input that needs
    // to be checked before using the field values. Skip re-allocation if it is null.
    if (k->is_inline_klass() && sv->has_properties()) {
      jint null_marker = StackValue::create_stack_value(fr, reg_map, sv->properties())->get_jint();
      if (null_marker == 0) {
        continue;
      }
    }

    oop obj = nullptr;
    bool cache_init_error = false;
    if (k->is_instance_klass()) {
      InstanceKlass* ik = InstanceKlass::cast(k);
      if (obj == nullptr && !cache_init_error) {
        InternalOOMEMark iom(THREAD);
        if (EnableVectorSupport && VectorSupport::is_vector(ik)) {
          obj = VectorSupport::allocate_vector(ik, fr, reg_map, sv, THREAD);
        } else {
          obj = ik->allocate_instance(THREAD);
        }
      }
    } else if (k->is_flatArray_klass()) {
      FlatArrayKlass* ak = FlatArrayKlass::cast(k);
      // Inline type array must be zeroed because not all memory is reassigned
      InternalOOMEMark iom(THREAD);
      obj = ak->allocate_instance(sv->field_size(), THREAD);
    } else if (k->is_typeArray_klass()) {
      TypeArrayKlass* ak = TypeArrayKlass::cast(k);
      assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
      int len = sv->field_size() / type2size[ak->element_type()];
      InternalOOMEMark iom(THREAD);
      obj = ak->allocate_instance(len, THREAD);
    } else if (k->is_refArray_klass()) {
      RefArrayKlass* ak = RefArrayKlass::cast(k);
      InternalOOMEMark iom(THREAD);
      obj = ak->allocate_instance(sv->field_size(), THREAD);
    }

    if (obj == nullptr) {
      failures = true;
    }

    assert(sv->value().is_null(), "redundant reallocation");
    assert(obj != nullptr || HAS_PENDING_EXCEPTION || cache_init_error, "allocation should succeed or we should get an exception");
    CLEAR_PENDING_EXCEPTION;
    sv->set_value(obj);
  }

  if (failures) {
    THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
  } else if (pending_exception.not_null()) {
    thread->set_pending_exception(pending_exception(), exception_file, exception_line);
  }

  return failures;
}

// We're deoptimizing at the return of a call, inline type fields are
// in registers. When we go back to the interpreter, it will expect a
// reference to an inline type instance. Allocate and initialize it from
// the register values here.
bool Deoptimization::realloc_inline_type_result(InlineKlass* vk, const RegisterMap& map, GrowableArray<Handle>& return_oops, TRAPS) {
  oop new_vt = vk->realloc_result(map, return_oops, THREAD);
  if (new_vt == nullptr) {
    CLEAR_PENDING_EXCEPTION;
    THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), true);
  }
  return_oops.clear();
  return_oops.push(Handle(THREAD, new_vt));
  return false;
}

// restore elements of an eliminated type array
void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) {
  int index = 0;

  for (int i = 0; i < sv->field_size(); i++) {
    StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
    switch(type) {
    case T_LONG: case T_DOUBLE: {
      assert(value->type() == T_INT, "Agreement.");
      StackValue* low =
        StackValue::create_stack_value(fr, reg_map, sv->field_at(++i));
#ifdef _LP64
      jlong res = (jlong)low->get_intptr();
#else
      jlong res = jlong_from(value->get_jint(), low->get_jint());
#endif
      obj->long_at_put(index, res);
      break;
    }

    case T_INT: case T_FLOAT: { // 4 bytes.
      assert(value->type() == T_INT, "Agreement.");
      obj->int_at_put(index, value->get_jint());
      break;
    }

    case T_SHORT:
      assert(value->type() == T_INT, "Agreement.");
      obj->short_at_put(index, (jshort)value->get_jint());
      break;

    case T_CHAR:
      assert(value->type() == T_INT, "Agreement.");
      obj->char_at_put(index, (jchar)value->get_jint());
      break;

    case T_BYTE: {
      assert(value->type() == T_INT, "Agreement.");
      obj->byte_at_put(index, (jbyte)value->get_jint());
      break;
    }

    case T_BOOLEAN: {
      assert(value->type() == T_INT, "Agreement.");
      obj->bool_at_put(index, (jboolean)value->get_jint());
      break;
    }

      default:
        ShouldNotReachHere();
    }
    index++;
  }
}

// restore fields of an eliminated object array
void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {
  for (int i = 0; i < sv->field_size(); i++) {
    StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
    assert(value->type() == T_OBJECT, "object element expected");
    obj->obj_at_put(i, value->get_obj()());
  }
}

class ReassignedField {
public:
  int _offset;
  BasicType _type;
  InstanceKlass* _klass;
  bool _is_flat;
  bool _is_null_free;
public:
  ReassignedField() : _offset(0), _type(T_ILLEGAL), _klass(nullptr), _is_flat(false), _is_null_free(false) { }
};

// Gets the fields of `klass` that are eliminated by escape analysis and need to be reassigned
static GrowableArray<ReassignedField>* get_reassigned_fields(InstanceKlass* klass, GrowableArray<ReassignedField>* fields) {
  InstanceKlass* super = klass->super();
  if (super != nullptr) {
    get_reassigned_fields(super, fields);
  }
  for (AllFieldStream fs(klass); !fs.done(); fs.next()) {
    if (!fs.access_flags().is_static() && !fs.field_flags().is_injected()) {
      ReassignedField field;
      field._offset = fs.offset();
      field._type = Signature::basic_type(fs.signature());
      if (fs.is_flat()) {
        field._is_flat = true;
        field._is_null_free = fs.is_null_free_inline_type();
        // Resolve klass of flat inline type field
        field._klass = InlineKlass::cast(klass->get_inline_type_field_klass(fs.index()));
      }
      fields->append(field);
    }
  }
  return fields;
}

// Restore fields of an eliminated instance object employing the same field order used by the
// compiler when it scalarizes an object at safepoints.
static int reassign_fields_by_klass(InstanceKlass* klass, frame* fr, RegisterMap* reg_map, ObjectValue* sv, int svIndex, oop obj, int base_offset, TRAPS) {
  GrowableArray<ReassignedField>* fields = get_reassigned_fields(klass, new GrowableArray<ReassignedField>());
  for (int i = 0; i < fields->length(); i++) {
    BasicType type = fields->at(i)._type;
    int offset = base_offset + fields->at(i)._offset;
    // Check for flat inline type field before accessing the ScopeValue because it might not have any fields
    if (fields->at(i)._is_flat) {
      // Recursively re-assign flat inline type fields
      InstanceKlass* vk = fields->at(i)._klass;
      assert(vk != nullptr, "must be resolved");
      offset -= InlineKlass::cast(vk)->payload_offset(); // Adjust offset to omit oop header
      svIndex = reassign_fields_by_klass(vk, fr, reg_map, sv, svIndex, obj, offset, CHECK_0);
      if (!fields->at(i)._is_null_free) {
        ScopeValue* scope_field = sv->field_at(svIndex);
        StackValue* value = StackValue::create_stack_value(fr, reg_map, scope_field);
        int nm_offset = offset + InlineKlass::cast(vk)->null_marker_offset();
        obj->bool_field_put(nm_offset, value->get_jint() & 1);
        svIndex++;
      }
      continue; // Continue because we don't need to increment svIndex
    }

    ScopeValue* scope_field = sv->field_at(svIndex);
    StackValue* value = StackValue::create_stack_value(fr, reg_map, scope_field);
    switch (type) {
      case T_OBJECT: case T_ARRAY:
        assert(value->type() == T_OBJECT, "Agreement.");
        obj->obj_field_put(offset, value->get_obj()());
        break;

      case T_INT: case T_FLOAT: { // 4 bytes.
        assert(value->type() == T_INT, "Agreement.");
        bool big_value = false;
        if (i+1 < fields->length() && fields->at(i+1)._type == T_INT) {
          if (scope_field->is_location()) {
            Location::Type type = ((LocationValue*) scope_field)->location().type();
            if (type == Location::dbl || type == Location::lng) {
              big_value = true;
            }
          }
          if (scope_field->is_constant_int()) {
            ScopeValue* next_scope_field = sv->field_at(svIndex + 1);
            if (next_scope_field->is_constant_long() || next_scope_field->is_constant_double()) {
              big_value = true;
            }
          }
        }

        if (big_value) {
          i++;
          assert(i < fields->length(), "second T_INT field needed");
          assert(fields->at(i)._type == T_INT, "T_INT field needed");
        } else {
          obj->int_field_put(offset, value->get_jint());
          break;
        }
      }
        /* no break */

      case T_LONG: case T_DOUBLE: {
        assert(value->type() == T_INT, "Agreement.");
        StackValue* low = StackValue::create_stack_value(fr, reg_map, sv->field_at(++svIndex));
#ifdef _LP64
        jlong res = (jlong)low->get_intptr();
#else
        jlong res = jlong_from(value->get_jint(), low->get_jint());
#endif
        obj->long_field_put(offset, res);
        break;
      }

      case T_SHORT:
        assert(value->type() == T_INT, "Agreement.");
        obj->short_field_put(offset, (jshort)value->get_jint());
        break;

      case T_CHAR:
        assert(value->type() == T_INT, "Agreement.");
        obj->char_field_put(offset, (jchar)value->get_jint());
        break;

      case T_BYTE:
        assert(value->type() == T_INT, "Agreement.");
        obj->byte_field_put(offset, (jbyte)value->get_jint());
        break;

      case T_BOOLEAN:
        assert(value->type() == T_INT, "Agreement.");
        obj->bool_field_put(offset, (jboolean)value->get_jint());
        break;

      default:
        ShouldNotReachHere();
    }
    svIndex++;
  }
  return svIndex;
}

// restore fields of an eliminated inline type array
void Deoptimization::reassign_flat_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, flatArrayOop obj, FlatArrayKlass* vak, TRAPS) {
  InlineKlass* vk = vak->element_klass();
  assert(vk->maybe_flat_in_array(), "should only be used for flat inline type arrays");
  // Adjust offset to omit oop header
  int base_offset = arrayOopDesc::base_offset_in_bytes(T_FLAT_ELEMENT) - vk->payload_offset();
  // Initialize all elements of the flat inline type array
  for (int i = 0; i < sv->field_size(); i++) {
    ObjectValue* val = sv->field_at(i)->as_ObjectValue();
    int offset = base_offset + (i << Klass::layout_helper_log2_element_size(vak->layout_helper()));
    reassign_fields_by_klass(vk, fr, reg_map, val, 0, (oop)obj, offset, CHECK);
    if (!obj->is_null_free_array()) {
      jboolean null_marker_value;
      if (val->has_properties()) {
        null_marker_value = StackValue::create_stack_value(fr, reg_map, val->properties())->get_jint() & 1;
      } else {
        null_marker_value = 1;
      }
      obj->bool_field_put(offset + vk->null_marker_offset(), null_marker_value);
    }
  }
}

// restore fields of all eliminated objects and arrays
void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, TRAPS) {
  for (int i = 0; i < objects->length(); i++) {
    assert(objects->at(i)->is_object(), "invalid debug information");
    ObjectValue* sv = (ObjectValue*) objects->at(i);
    Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
    k = get_refined_array_klass(k, fr, reg_map, sv, THREAD);

    Handle obj = sv->value();
    assert(obj.not_null() || realloc_failures || sv->has_properties(), "reallocation was missed");
    log_develop_debug(deoptimization)("reassign fields for object of type %s!", k->name()->as_C_string());

    if (obj.is_null()) {
      continue;
    }

    if (EnableVectorSupport && VectorSupport::is_vector(k)) {
      assert(sv->field_size() == 1, "%s not a vector", k->name()->as_C_string());
      ScopeValue* payload = sv->field_at(0);
      if (payload->is_location() &&
          payload->as_LocationValue()->location().type() == Location::vector) {
#ifndef PRODUCT
        if (const LogTarget(Debug, deoptimization) lt; lt.is_enabled()) {
          LogStream ls(lt);
          ls.print_cr("skip field reassignment for this vector - it should be assigned already");
          if (Verbose) {
            Handle obj = sv->value();
            k->oop_print_on(obj(), &ls);
          }
        }
#endif // !PRODUCT
        continue; // Such vector's value was already restored in VectorSupport::allocate_vector().
      }
      // Else fall-through to do assignment for scalar-replaced boxed vector representation
      // which could be restored after vector object allocation.
    }
    if (k->is_instance_klass()) {
      InstanceKlass* ik = InstanceKlass::cast(k);
      reassign_fields_by_klass(ik, fr, reg_map, sv, 0, obj(), 0, CHECK);
    } else if (k->is_flatArray_klass()) {
      FlatArrayKlass* vak = FlatArrayKlass::cast(k);
      reassign_flat_array_elements(fr, reg_map, sv, (flatArrayOop) obj(), vak, CHECK);
    } else if (k->is_typeArray_klass()) {
      TypeArrayKlass* ak = TypeArrayKlass::cast(k);
      reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());
    } else if (k->is_refArray_klass()) {
      reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());
    }
  }
  // These objects may escape when we return to Interpreter after deoptimization.
  // We need barrier so that stores that initialize these objects can't be reordered
  // with subsequent stores that make these objects accessible by other threads.
  OrderAccess::storestore();
}


// relock objects for which synchronization was eliminated
bool Deoptimization::relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,
                                    JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures) {
  bool relocked_objects = false;
  for (int i = 0; i < monitors->length(); i++) {
    MonitorInfo* mon_info = monitors->at(i);
    if (mon_info->eliminated()) {
      assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed");
      relocked_objects = true;
      if (!mon_info->owner_is_scalar_replaced()) {
        Handle obj(thread, mon_info->owner());
        markWord mark = obj->mark();
        if (exec_mode == Unpack_none) {
          if (mark.has_monitor()) {
            // defer relocking if the deoptee thread is currently waiting for obj
            ObjectMonitor* waiting_monitor = deoptee_thread->current_waiting_monitor();
            if (waiting_monitor != nullptr && waiting_monitor->object() == obj()) {
              assert(fr.is_deoptimized_frame(), "frame must be scheduled for deoptimization");
              mon_info->lock()->clear_object_monitor_cache();
              JvmtiDeferredUpdates::inc_relock_count_after_wait(deoptee_thread);
              continue;
            }
          }
        }
        BasicLock* lock = mon_info->lock();
        // We have lost information about the correct state of the lock stack.
        // Entering may create an invalid lock stack. Inflate the lock if it
        // was fast_locked to restore the valid lock stack.
        // The BasicLock cache is expected to be either a valid ObjectMonitor*
        // or nullptr. Right now it is garbage, hence we set it to nullptr.
        lock->clear_object_monitor_cache();
        ObjectSynchronizer::enter_for(obj, lock, deoptee_thread);
        if (deoptee_thread->lock_stack().contains(obj())) {
            ObjectSynchronizer::inflate_fast_locked_object(obj(), ObjectSynchronizer::InflateCause::inflate_cause_vm_internal,
                                                           deoptee_thread, thread);
        }
        assert(mon_info->owner()->is_locked(), "object must be locked now");
        assert(obj->mark().has_monitor(), "must be");
        assert(!deoptee_thread->lock_stack().contains(obj()), "must be");
        assert(ObjectSynchronizer::read_monitor(obj(), obj->mark())->has_owner(deoptee_thread), "must be");
      }
    }
  }
  return relocked_objects;
}
#endif // COMPILER2

static void log_deopt_packing(JavaThread* thread, frame fr, vframeArray* array, GrowableArray<compiledVFrame*>* chunk) {

  LogMessage(deoptimization) msg;
  NonInterleavingLogStream ls(LogLevel::Debug, msg);
  if (ls.is_enabled()) {

    ResourceMark rm(thread);
    ls.print_cr("DEOPT PACKING thread=" INTPTR_FORMAT " vframeArray=" INTPTR_FORMAT, p2i(thread), p2i(array));
    ls.print_cr("   Virtual frames (innermost/newest first):");
    for (int index = 0; index < chunk->length(); index++) {
      compiledVFrame* vf = chunk->at(index);
      int bci = vf->raw_bci();
      const char* code_name;
      if (bci == SynchronizationEntryBCI) {
        code_name = "sync entry";
      } else {
        Bytecodes::Code code = vf->method()->code_at(bci);
        code_name = Bytecodes::name(code);
      }

      ls.print("      VFrame %d (" INTPTR_FORMAT ")", index, p2i(vf));
      ls.print(" - %s", vf->method()->name_and_sig_as_C_string());
      ls.print(" - %s", code_name);
      ls.print_cr(" @ bci=%d ", bci);
    }
  }
}


vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
  Events::log_deopt_message(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, p2i(fr.pc()), p2i(fr.sp()));

  // Register map for next frame (used for stack crawl).  We capture
  // the state of the deopt'ing frame's caller.  Thus if we need to
  // stuff a C2I adapter we can properly fill in the callee-save
  // register locations.
  frame caller = fr.sender(reg_map);
  int frame_size = pointer_delta_as_int(caller.sp(), fr.sp());

  frame sender = caller;

  // Since the Java thread being deoptimized will eventually adjust it's own stack,
  // the vframeArray containing the unpacking information is allocated in the C heap.
  // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
  vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);

  // Compare the vframeArray to the collected vframes
  assert(array->structural_compare(thread, chunk), "just checking");

  log_deopt_packing(thread, fr, array, chunk);
  return array;
}

#ifdef COMPILER2
void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {
  // Reallocation of some scalar replaced objects failed. Record
  // that we need to pop all the interpreter frames for the
  // deoptimized compiled frame.
  assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");
  thread->set_frames_to_pop_failed_realloc(array->frames());
  // Unlock all monitors here otherwise the interpreter will see a
  // mix of locked and unlocked monitors (because of failed
  // reallocations of synchronized objects) and be confused.
  for (int i = 0; i < array->frames(); i++) {
    MonitorChunk* monitors = array->element(i)->monitors();
    if (monitors != nullptr) {
      // Unlock in reverse order starting from most nested monitor.
      for (int j = (monitors->number_of_monitors() - 1); j >= 0; j--) {
        BasicObjectLock* src = monitors->at(j);
        if (src->obj() != nullptr) {
          ObjectSynchronizer::exit(src->obj(), src->lock(), thread);
        }
      }
      array->element(i)->free_monitors();
#ifdef ASSERT
      array->element(i)->set_removed_monitors();
#endif // ASSERT
    }
  }
}
#endif // COMPILER2

void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
  assert(fr.can_be_deoptimized(), "checking frame type");

  gather_statistics(reason, Action_none, Bytecodes::_illegal);

  if (LogCompilation && xtty != nullptr) {
    nmethod* nm = fr.cb()->as_nmethod_or_null();
    assert(nm != nullptr, "only compiled methods can deopt");

    ttyLocker ttyl;
    xtty->begin_head("deoptimized thread='%zu' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
    nm->log_identity(xtty);
    xtty->end_head();
    for (ScopeDesc* sd = nm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
      xtty->begin_elem("jvms bci='%d'", sd->bci());
      xtty->method(sd->method());
      xtty->end_elem();
      if (sd->is_top())  break;
    }
    xtty->tail("deoptimized");
  }

  Continuation::notify_deopt(thread, fr.sp());

  // Patch the compiled method so that when execution returns to it we will
  // deopt the execution state and return to the interpreter.
  fr.deoptimize(thread);
}

void Deoptimization::deoptimize(JavaThread* thread, frame fr, DeoptReason reason) {
  // Deoptimize only if the frame comes from compiled code.
  // Do not deoptimize the frame which is already patched
  // during the execution of the loops below.
  if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
    return;
  }
  ResourceMark rm;
  deoptimize_single_frame(thread, fr, reason);
}

address Deoptimization::deoptimize_for_missing_exception_handler(nmethod* nm, bool make_not_entrant) {
  // there is no exception handler for this pc => deoptimize
  if (make_not_entrant) {
    nm->make_not_entrant(nmethod::InvalidationReason::MISSING_EXCEPTION_HANDLER);
  }

  // Use Deoptimization::deoptimize for all of its side-effects:
  // gathering traps statistics, logging...
  // it also patches the return pc but we do not care about that
  // since we return a continuation to the deopt_blob below.
  JavaThread* thread = JavaThread::current();
  RegisterMap reg_map(thread,
                      RegisterMap::UpdateMap::skip,
                      RegisterMap::ProcessFrames::include,
                      RegisterMap::WalkContinuation::skip);
  frame runtime_frame = thread->last_frame();
  frame caller_frame = runtime_frame.sender(&reg_map);
  assert(caller_frame.cb()->as_nmethod_or_null() == nm, "expect top frame compiled method");

  Deoptimization::deoptimize(thread, caller_frame, Deoptimization::Reason_not_compiled_exception_handler);

  return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
}

void Deoptimization::deoptimize_frame_internal(JavaThread* thread, intptr_t* id, DeoptReason reason) {
  assert(thread == Thread::current() ||
         thread->is_handshake_safe_for(Thread::current()) ||
         SafepointSynchronize::is_at_safepoint(),
         "can only deoptimize other thread at a safepoint/handshake");
  // Compute frame and register map based on thread and sp.
  RegisterMap reg_map(thread,
                      RegisterMap::UpdateMap::skip,
                      RegisterMap::ProcessFrames::include,
                      RegisterMap::WalkContinuation::skip);
  frame fr = thread->last_frame();
  while (fr.id() != id) {
    fr = fr.sender(&reg_map);
  }
  deoptimize(thread, fr, reason);
}


void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id, DeoptReason reason) {
  Thread* current = Thread::current();
  if (thread == current || thread->is_handshake_safe_for(current)) {
    Deoptimization::deoptimize_frame_internal(thread, id, reason);
  } else {
    VM_DeoptimizeFrame deopt(thread, id, reason);
    VMThread::execute(&deopt);
  }
}

void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) {
  deoptimize_frame(thread, id, Reason_constraint);
}

// JVMTI PopFrame support
JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address))
{
  assert(thread == JavaThread::current(), "pre-condition");
  thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address);
}
JRT_END

MethodData*
Deoptimization::get_method_data(JavaThread* thread, const methodHandle& m,
                                bool create_if_missing) {
  JavaThread* THREAD = thread; // For exception macros.
  MethodData* mdo = m()->method_data();
  if (mdo == nullptr && create_if_missing && !HAS_PENDING_EXCEPTION) {
    // Build an MDO.  Ignore errors like OutOfMemory;
    // that simply means we won't have an MDO to update.
    Method::build_profiling_method_data(m, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      // Only metaspace OOM is expected. No Java code executed.
      assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOM error here");
      CLEAR_PENDING_EXCEPTION;
    }
    mdo = m()->method_data();
  }
  return mdo;
}

#ifdef COMPILER2
void Deoptimization::load_class_by_index(const constantPoolHandle& constant_pool, int index, TRAPS) {
  // In case of an unresolved klass entry, load the class.
  // This path is exercised from case _ldc in Parse::do_one_bytecode,
  // and probably nowhere else.
  // Even that case would benefit from simply re-interpreting the
  // bytecode, without paying special attention to the class index.
  // So this whole "class index" feature should probably be removed.

  if (constant_pool->tag_at(index).is_unresolved_klass()) {
    Klass* tk = constant_pool->klass_at(index, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      // Exception happened during classloading. We ignore the exception here, since it
      // is going to be rethrown since the current activation is going to be deoptimized and
      // the interpreter will re-execute the bytecode.
      // Do not clear probable Async Exceptions.
      CLEAR_PENDING_NONASYNC_EXCEPTION;
      // Class loading called java code which may have caused a stack
      // overflow. If the exception was thrown right before the return
      // to the runtime the stack is no longer guarded. Reguard the
      // stack otherwise if we return to the uncommon trap blob and the
      // stack bang causes a stack overflow we crash.
      JavaThread* jt = THREAD;
      bool guard_pages_enabled = jt->stack_overflow_state()->reguard_stack_if_needed();
      assert(guard_pages_enabled, "stack banging in uncommon trap blob may cause crash");
    }
    return;
  }

  assert(!constant_pool->tag_at(index).is_symbol(),
         "no symbolic names here, please");
}

#if INCLUDE_JFR

class DeoptReasonSerializer : public JfrSerializer {
 public:
  void serialize(JfrCheckpointWriter& writer) {
    writer.write_count((u4)(Deoptimization::Reason_LIMIT + 1)); // + Reason::many (-1)
    for (int i = -1; i < Deoptimization::Reason_LIMIT; ++i) {
      writer.write_key((u8)i);
      writer.write(Deoptimization::trap_reason_name(i));
    }
  }
};

class DeoptActionSerializer : public JfrSerializer {
 public:
  void serialize(JfrCheckpointWriter& writer) {
    static const u4 nof_actions = Deoptimization::Action_LIMIT;
    writer.write_count(nof_actions);
    for (u4 i = 0; i < Deoptimization::Action_LIMIT; ++i) {
      writer.write_key(i);
      writer.write(Deoptimization::trap_action_name((int)i));
    }
  }
};

static void register_serializers() {
  static int critical_section = 0;
  if (1 == critical_section || AtomicAccess::cmpxchg(&critical_section, 0, 1) == 1) {
    return;
  }
  JfrSerializer::register_serializer(TYPE_DEOPTIMIZATIONREASON, true, new DeoptReasonSerializer());
  JfrSerializer::register_serializer(TYPE_DEOPTIMIZATIONACTION, true, new DeoptActionSerializer());
}

static void post_deoptimization_event(nmethod* nm,
                                      const Method* method,
                                      int trap_bci,
                                      int instruction,
                                      Deoptimization::DeoptReason reason,
                                      Deoptimization::DeoptAction action) {
  assert(nm != nullptr, "invariant");
  assert(method != nullptr, "invariant");
  if (EventDeoptimization::is_enabled()) {
    static bool serializers_registered = false;
    if (!serializers_registered) {
      register_serializers();
      serializers_registered = true;
    }
    EventDeoptimization event;
    event.set_compileId(nm->compile_id());
    event.set_compiler(nm->compiler_type());
    event.set_method(method);
    event.set_lineNumber(method->line_number_from_bci(trap_bci));
    event.set_bci(trap_bci);
    event.set_instruction(instruction);
    event.set_reason(reason);
    event.set_action(action);
    event.commit();
  }
}

#endif // INCLUDE_JFR

static void log_uncommon_trap(nmethod* nm, Method* tm, intptr_t pc, frame& fr, int trap_bci,
                              const char* reason_name, const char* reason_action, const char* class_name,
                              bool unresolved) {
  LogMessage(deoptimization) msg;
  NonInterleavingLogStream ls(LogLevel::Debug, msg);

  if (ls.is_enabled()) {
    bool is_osr = nm->is_osr_method();
    ls.print("UNCOMMON TRAP method=%s", tm->name_and_sig_as_C_string());
    ls.print(" trap_bci=%d ", trap_bci);
    if (is_osr) {
      ls.print("osr_bci=%d ", nm->osr_entry_bci());
    }
    ls.print("reason=%s ", reason_name);
    ls.print("action=%s ", reason_action);
    if (class_name != nullptr) {
      ls.print("%s class_name=%s ", unresolved ? "unresolved" : "", class_name);
    }
    ls.print("cid=%4d %s level=%d ",
             nm->compile_id(), (is_osr ? "osr" : ""), nm->comp_level());
    ls.print_cr("pc=" INTPTR_FORMAT " relative_pc=" INTPTR_FORMAT,
             pc, fr.pc() - nm->code_begin());

  }
}

JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* current, jint trap_request)) {
  HandleMark hm(current);

  // uncommon_trap() is called at the beginning of the uncommon trap
  // handler. Note this fact before we start generating temporary frames
  // that can confuse an asynchronous stack walker. This counter is
  // decremented at the end of unpack_frames().

  current->inc_in_deopt_handler();

  RegisterMap reg_map(current,
                      RegisterMap::UpdateMap::skip,
                      RegisterMap::ProcessFrames::include,
                      RegisterMap::WalkContinuation::skip);
  frame stub_frame = current->last_frame();
  frame fr = stub_frame.sender(&reg_map);

  {
    ResourceMark rm(current);

    DeoptReason reason = trap_request_reason(trap_request);
    DeoptAction action = trap_request_action(trap_request);
    jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1

    vframe*  vf  = vframe::new_vframe(&fr, &reg_map, current);
    compiledVFrame* cvf = compiledVFrame::cast(vf);

    nmethod* nm = cvf->code();

    ScopeDesc*      trap_scope  = cvf->scope();

    bool is_receiver_constraint_failure = COMPILER2_PRESENT(VerifyReceiverTypes &&) (reason == Deoptimization::Reason_receiver_constraint);
    if (is_receiver_constraint_failure) {
      tty->print_cr("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT ", method=%s", trap_scope->bci(),
                    p2i(fr.pc()), fr.pc() - nm->code_begin(), trap_scope->method()->name_and_sig_as_C_string());
    }

    methodHandle    trap_method(current, trap_scope->method());
    int             trap_bci    = trap_scope->bci();
    Bytecodes::Code trap_bc     = trap_method->java_code_at(trap_bci);
    // Record this event in the histogram.
    gather_statistics(reason, action, trap_bc);

    // Ensure that we can record deopt. history:
    bool create_if_missing = ProfileTraps;

    methodHandle profiled_method;
    profiled_method = trap_method;

    MethodData* trap_mdo =
      get_method_data(current, profiled_method, create_if_missing);

    Symbol* class_name = nullptr;
    bool unresolved = false;
    if (unloaded_class_index >= 0) {
      constantPoolHandle constants (current, trap_method->constants());
      if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
        class_name = constants->klass_name_at(unloaded_class_index);
        unresolved = true;
      } else if (constants->tag_at(unloaded_class_index).is_symbol()) {
        class_name = constants->symbol_at(unloaded_class_index);
      }
    }
    { // Log Deoptimization event for JFR, UL and event system
      Method* tm = trap_method();
      const char* reason_name = trap_reason_name(reason);
      const char* reason_action = trap_action_name(action);
      intptr_t pc = p2i(fr.pc());

      JFR_ONLY(post_deoptimization_event(nm, tm, trap_bci, trap_bc, reason, action);)

      ResourceMark rm(current);

      const char* class_name_str = nullptr;
      const char* class_name_msg = nullptr;
      stringStream st, stm;
      if (class_name != nullptr) {
        class_name->print_symbol_on(&st);
        class_name_str = st.freeze();
        stm.print("class=%s ", class_name_str);
        class_name_msg = stm.freeze();
      } else {
        class_name_msg = "";
      }
      log_uncommon_trap(nm, tm, pc, fr, trap_bci, reason_name, reason_action, class_name_str, unresolved);
      Events::log_deopt_message(current, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d %s%s",
                                reason_name, reason_action, pc,
                                tm->name_and_sig_as_C_string(), trap_bci, class_name_msg, nm->compiler_name());
    }


    // Print a bunch of diagnostics, if requested to the compilation log file.
    if (LogCompilation || is_receiver_constraint_failure) {
      ResourceMark rm(current);

      // Lock to read ProfileData, and ensure lock is not broken by a safepoint
      // We must do this already now, since we cannot acquire this lock while
      // holding the tty lock (lock ordering by rank).
      ConditionalMutexLocker ml((trap_mdo != nullptr) ? trap_mdo->extra_data_lock() : nullptr,
                                (trap_mdo != nullptr),
                                Mutex::_no_safepoint_check_flag);

      ttyLocker ttyl;

      char buf[100];
      if (xtty != nullptr) {
        xtty->begin_head("uncommon_trap thread='%zu' %s",
                         os::current_thread_id(),
                         Deoptimization::format_trap_request(buf, sizeof(buf), trap_request));
        nm->log_identity(xtty);
      }
      if (class_name != nullptr) {
        if (xtty != nullptr) {
          if (unresolved) {
            xtty->print(" unresolved='1'");
          }
          xtty->name(class_name);
        }
      }
      if (xtty != nullptr && trap_mdo != nullptr && (int)reason < (int)MethodData::_trap_hist_limit) {
        // Dump the relevant MDO state.
        // This is the deopt count for the current reason, any previous
        // reasons or recompiles seen at this point.
        int dcnt = trap_mdo->trap_count(reason);
        if (dcnt != 0)
          xtty->print(" count='%d'", dcnt);

        // We need to lock to read the ProfileData. But to keep the locks ordered, we need to
        // lock extra_data_lock before the tty lock.
        ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
        int dos = (pdata == nullptr)? 0: pdata->trap_state();
        if (dos != 0) {
          xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
          if (trap_state_is_recompiled(dos)) {
            int recnt2 = trap_mdo->overflow_recompile_count();
            if (recnt2 != 0)
              xtty->print(" recompiles2='%d'", recnt2);
          }
        }
      }
      if (xtty != nullptr) {
        xtty->stamp();
        xtty->end_head();
      }
      if (xtty != nullptr) {
        // Log the precise location of the trap.
        for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {
          xtty->begin_elem("jvms bci='%d'", sd->bci());
          xtty->method(sd->method());
          xtty->end_elem();
          if (sd->is_top())  break;
        }
        xtty->tail("uncommon_trap");
      }
    }

    if (is_receiver_constraint_failure) {
      fatal("missing receiver type check");
    }

    // Load class if necessary
    if (unloaded_class_index >= 0) {
      constantPoolHandle constants(current, trap_method->constants());
      load_class_by_index(constants, unloaded_class_index, THREAD);
    }

    // Flush the nmethod if necessary and desirable.
    //
    // We need to avoid situations where we are re-flushing the nmethod
    // because of a hot deoptimization site.  Repeated flushes at the same
    // point need to be detected by the compiler and avoided.  If the compiler
    // cannot avoid them (or has a bug and "refuses" to avoid them), this
    // module must take measures to avoid an infinite cycle of recompilation
    // and deoptimization.  There are several such measures:
    //
    //   1. If a recompilation is ordered a second time at some site X
    //   and for the same reason R, the action is adjusted to 'reinterpret',
    //   to give the interpreter time to exercise the method more thoroughly.
    //   If this happens, the method's overflow_recompile_count is incremented.
    //
    //   2. If the compiler fails to reduce the deoptimization rate, then
    //   the method's overflow_recompile_count will begin to exceed the set
    //   limit PerBytecodeRecompilationCutoff.  If this happens, the action
    //   is adjusted to 'make_not_compilable', and the method is abandoned
    //   to the interpreter.  This is a performance hit for hot methods,
    //   but is better than a disastrous infinite cycle of recompilations.
    //   (Actually, only the method containing the site X is abandoned.)
    //
    //   3. In parallel with the previous measures, if the total number of
    //   recompilations of a method exceeds the much larger set limit
    //   PerMethodRecompilationCutoff, the method is abandoned.
    //   This should only happen if the method is very large and has
    //   many "lukewarm" deoptimizations.  The code which enforces this
    //   limit is elsewhere (class nmethod, class Method).
    //
    // Note that the per-BCI 'is_recompiled' bit gives the compiler one chance
    // to recompile at each bytecode independently of the per-BCI cutoff.
    //
    // The decision to update code is up to the compiler, and is encoded
    // in the Action_xxx code.  If the compiler requests Action_none
    // no trap state is changed, no compiled code is changed, and the
    // computation suffers along in the interpreter.
    //
    // The other action codes specify various tactics for decompilation
    // and recompilation.  Action_maybe_recompile is the loosest, and
    // allows the compiled code to stay around until enough traps are seen,
    // and until the compiler gets around to recompiling the trapping method.
    //
    // The other actions cause immediate removal of the present code.

    // Traps caused by injected profile shouldn't pollute trap counts.
    bool injected_profile_trap = trap_method->has_injected_profile() &&
                                 (reason == Reason_intrinsic || reason == Reason_unreached);

    bool update_trap_state = (reason != Reason_tenured) && !injected_profile_trap;
    bool make_not_entrant = false;
    bool make_not_compilable = false;
    bool reprofile = false;
    switch (action) {
    case Action_none:
      // Keep the old code.
      update_trap_state = false;
      break;
    case Action_maybe_recompile:
      // Do not need to invalidate the present code, but we can
      // initiate another
      // Start compiler without (necessarily) invalidating the nmethod.
      // The system will tolerate the old code, but new code should be
      // generated when possible.
      break;
    case Action_reinterpret:
      // Go back into the interpreter for a while, and then consider
      // recompiling form scratch.
      make_not_entrant = true;
      // Reset invocation counter for outer most method.
      // This will allow the interpreter to exercise the bytecodes
      // for a while before recompiling.
      // By contrast, Action_make_not_entrant is immediate.
      //
      // Note that the compiler will track null_check, null_assert,
      // range_check, and class_check events and log them as if they
      // had been traps taken from compiled code.  This will update
      // the MDO trap history so that the next compilation will
      // properly detect hot trap sites.
      reprofile = true;
      break;
    case Action_make_not_entrant:
      // Request immediate recompilation, and get rid of the old code.
      // Make them not entrant, so next time they are called they get
      // recompiled.  Unloaded classes are loaded now so recompile before next
      // time they are called.  Same for uninitialized.  The interpreter will
      // link the missing class, if any.
      make_not_entrant = true;
      break;
    case Action_make_not_compilable:
      // Give up on compiling this method at all.
      make_not_entrant = true;
      make_not_compilable = true;
      break;
    default:
      ShouldNotReachHere();
    }

    // Setting +ProfileTraps fixes the following, on all platforms:
    // The result is infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the
    // recompile relies on a MethodData* to record heroic opt failures.

    // Whether the interpreter is producing MDO data or not, we also need
    // to use the MDO to detect hot deoptimization points and control
    // aggressive optimization.
    bool inc_recompile_count = false;

    // Lock to read ProfileData, and ensure lock is not broken by a safepoint
    ConditionalMutexLocker ml((trap_mdo != nullptr) ? trap_mdo->extra_data_lock() : nullptr,
                              (trap_mdo != nullptr),
                              Mutex::_no_safepoint_check_flag);
    ProfileData* pdata = nullptr;
    if (ProfileTraps && CompilerConfig::is_c2_enabled() && update_trap_state && trap_mdo != nullptr) {
      assert(trap_mdo == get_method_data(current, profiled_method, false), "sanity");
      uint this_trap_count = 0;
      bool maybe_prior_trap = false;
      bool maybe_prior_recompile = false;

      pdata = query_update_method_data(trap_mdo, trap_bci, reason, nm->method(),
                                   //outputs:
                                   this_trap_count,
                                   maybe_prior_trap,
                                   maybe_prior_recompile);
      // Because the interpreter also counts null, div0, range, and class
      // checks, these traps from compiled code are double-counted.
      // This is harmless; it just means that the PerXTrapLimit values
      // are in effect a little smaller than they look.

      DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
      if (per_bc_reason != Reason_none) {
        // Now take action based on the partially known per-BCI history.
        if (maybe_prior_trap
            && this_trap_count >= (uint)PerBytecodeTrapLimit) {
          // If there are too many traps at this BCI, force a recompile.
          // This will allow the compiler to see the limit overflow, and
          // take corrective action, if possible.  The compiler generally
          // does not use the exact PerBytecodeTrapLimit value, but instead
          // changes its tactics if it sees any traps at all.  This provides
          // a little hysteresis, delaying a recompile until a trap happens
          // several times.
          //
          // Actually, since there is only one bit of counter per BCI,
          // the possible per-BCI counts are {0,1,(per-method count)}.
          // This produces accurate results if in fact there is only
          // one hot trap site, but begins to get fuzzy if there are
          // many sites.  For example, if there are ten sites each
          // trapping two or more times, they each get the blame for
          // all of their traps.
          make_not_entrant = true;
        }

        // Detect repeated recompilation at the same BCI, and enforce a limit.
        if (make_not_entrant && maybe_prior_recompile) {
          // More than one recompile at this point.
          inc_recompile_count = maybe_prior_trap;
        }
      } else {
        // For reasons which are not recorded per-bytecode, we simply
        // force recompiles unconditionally.
        // (Note that PerMethodRecompilationCutoff is enforced elsewhere.)
        make_not_entrant = true;
      }

      // Go back to the compiler if there are too many traps in this method.
      if (this_trap_count >= per_method_trap_limit(reason)) {
        // If there are too many traps in this method, force a recompile.
        // This will allow the compiler to see the limit overflow, and
        // take corrective action, if possible.
        // (This condition is an unlikely backstop only, because the
        // PerBytecodeTrapLimit is more likely to take effect first,
        // if it is applicable.)
        make_not_entrant = true;
      }

      // Here's more hysteresis:  If there has been a recompile at
      // this trap point already, run the method in the interpreter
      // for a while to exercise it more thoroughly.
      if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) {
        reprofile = true;
      }
    }

    // Take requested actions on the method:

    // Recompile
    if (make_not_entrant) {
      if (!nm->make_not_entrant(nmethod::InvalidationReason::UNCOMMON_TRAP)) {
        return; // the call did not change nmethod's state
      }

      if (pdata != nullptr) {
        // Record the recompilation event, if any.
        int tstate0 = pdata->trap_state();
        int tstate1 = trap_state_set_recompiled(tstate0, true);
        if (tstate1 != tstate0)
          pdata->set_trap_state(tstate1);
      }

      // For code aging we count traps separately here, using make_not_entrant()
      // as a guard against simultaneous deopts in multiple threads.
      if (reason == Reason_tenured && trap_mdo != nullptr) {
        trap_mdo->inc_tenure_traps();
      }
    }
    if (inc_recompile_count) {
      trap_mdo->inc_overflow_recompile_count();
      if ((uint)trap_mdo->overflow_recompile_count() >
          (uint)PerBytecodeRecompilationCutoff) {
        // Give up on the method containing the bad BCI.
        if (trap_method() == nm->method()) {
          make_not_compilable = true;
        } else {
          trap_method->set_not_compilable("overflow_recompile_count > PerBytecodeRecompilationCutoff", CompLevel_full_optimization);
          // But give grace to the enclosing nm->method().
        }
      }
    }

    // Reprofile
    if (reprofile) {
      CompilationPolicy::reprofile(trap_scope, nm->is_osr_method());
    }

    // Give up compiling
    if (make_not_compilable && !nm->method()->is_not_compilable(CompLevel_full_optimization)) {
      assert(make_not_entrant, "consistent");
      nm->method()->set_not_compilable("give up compiling", CompLevel_full_optimization);
    }

    if (ProfileExceptionHandlers && trap_mdo != nullptr) {
      BitData* exception_handler_data = trap_mdo->exception_handler_bci_to_data_or_null(trap_bci);
      if (exception_handler_data != nullptr) {
        // uncommon trap at the start of an exception handler.
        // C2 generates these for un-entered exception handlers.
        // mark the handler as entered to avoid generating
        // another uncommon trap the next time the handler is compiled
        exception_handler_data->set_exception_handler_entered();
      }
    }

  } // Free marked resources

}
JRT_END

ProfileData*
Deoptimization::query_update_method_data(MethodData* trap_mdo,
                                         int trap_bci,
                                         Deoptimization::DeoptReason reason,
                                         Method* compiled_method,
                                         //outputs:
                                         uint& ret_this_trap_count,
                                         bool& ret_maybe_prior_trap,
                                         bool& ret_maybe_prior_recompile) {
  trap_mdo->check_extra_data_locked();

  bool maybe_prior_trap = false;
  bool maybe_prior_recompile = false;
  uint this_trap_count = 0;
  uint idx = reason;
  uint prior_trap_count = trap_mdo->trap_count(idx);
  this_trap_count  = trap_mdo->inc_trap_count(idx);

  // If the runtime cannot find a place to store trap history,
  // it is estimated based on the general condition of the method.
  // If the method has ever been recompiled, or has ever incurred
  // a trap with the present reason , then this BCI is assumed
  // (pessimistically) to be the culprit.
  maybe_prior_trap      = (prior_trap_count != 0);
  maybe_prior_recompile = (trap_mdo->decompile_count() != 0);
  ProfileData* pdata = nullptr;


  // For reasons which are recorded per bytecode, we check per-BCI data.
  DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason);
  if (per_bc_reason != Reason_none) {
    // Find the profile data for this BCI.  If there isn't one,
    // try to allocate one from the MDO's set of spares.
    // This will let us detect a repeated trap at this point.
    pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : nullptr);

    if (pdata != nullptr) {
      if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) {
        if (LogCompilation && xtty != nullptr) {
          ttyLocker ttyl;
          // no more room for speculative traps in this MDO
          xtty->elem("speculative_traps_oom");
        }
      }
      // Query the trap state of this profile datum.
      int tstate0 = pdata->trap_state();
      if (!trap_state_has_reason(tstate0, per_bc_reason))
        maybe_prior_trap = false;
      if (!trap_state_is_recompiled(tstate0))
        maybe_prior_recompile = false;

      // Update the trap state of this profile datum.
      int tstate1 = tstate0;
      // Record the reason.
      tstate1 = trap_state_add_reason(tstate1, per_bc_reason);
      // Store the updated state on the MDO, for next time.
      if (tstate1 != tstate0)
        pdata->set_trap_state(tstate1);
    } else {
      if (LogCompilation && xtty != nullptr) {
        ttyLocker ttyl;
        // Missing MDP?  Leave a small complaint in the log.
        xtty->elem("missing_mdp bci='%d'", trap_bci);
      }
    }
  }

  // Return results:
  ret_this_trap_count = this_trap_count;
  ret_maybe_prior_trap = maybe_prior_trap;
  ret_maybe_prior_recompile = maybe_prior_recompile;
  return pdata;
}

void
Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
  ResourceMark rm;
  // Ignored outputs:
  uint ignore_this_trap_count;
  bool ignore_maybe_prior_trap;
  bool ignore_maybe_prior_recompile;
  assert(!reason_is_speculate(reason), "reason speculate only used by compiler");

  // Lock to read ProfileData, and ensure lock is not broken by a safepoint
  MutexLocker ml(trap_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);

  query_update_method_data(trap_mdo, trap_bci,
                           (DeoptReason)reason,
                           nullptr,
                           ignore_this_trap_count,
                           ignore_maybe_prior_trap,
                           ignore_maybe_prior_recompile);
}

Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* current, jint trap_request, jint exec_mode) {
  // Enable WXWrite: current function is called from methods compiled by C2 directly
  MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));

  // Still in Java no safepoints
  {
    // This enters VM and may safepoint
    uncommon_trap_inner(current, trap_request);
  }
  HandleMark hm(current);
  return fetch_unroll_info_helper(current, exec_mode);
}

// Local derived constants.
// Further breakdown of DataLayout::trap_state, as promised by DataLayout.
const int DS_REASON_MASK   = ((uint)DataLayout::trap_mask) >> 1;
const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK;

//---------------------------trap_state_reason---------------------------------
Deoptimization::DeoptReason
Deoptimization::trap_state_reason(int trap_state) {
  // This assert provides the link between the width of DataLayout::trap_bits
  // and the encoding of "recorded" reasons.  It ensures there are enough
  // bits to store all needed reasons in the per-BCI MDO profile.
  assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
  int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
  trap_state -= recompile_bit;
  if (trap_state == DS_REASON_MASK) {
    return Reason_many;
  } else {
    assert((int)Reason_none == 0, "state=0 => Reason_none");
    return (DeoptReason)trap_state;
  }
}
//-------------------------trap_state_has_reason-------------------------------
int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
  assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason");
  assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits");
  int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
  trap_state -= recompile_bit;
  if (trap_state == DS_REASON_MASK) {
    return -1;  // true, unspecifically (bottom of state lattice)
  } else if (trap_state == reason) {
    return 1;   // true, definitely
  } else if (trap_state == 0) {
    return 0;   // false, definitely (top of state lattice)
  } else {
    return 0;   // false, definitely
  }
}
//-------------------------trap_state_add_reason-------------------------------
int Deoptimization::trap_state_add_reason(int trap_state, int reason) {
  assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason");
  int recompile_bit = (trap_state & DS_RECOMPILE_BIT);
  trap_state -= recompile_bit;
  if (trap_state == DS_REASON_MASK) {
    return trap_state + recompile_bit;     // already at state lattice bottom
  } else if (trap_state == reason) {
    return trap_state + recompile_bit;     // the condition is already true
  } else if (trap_state == 0) {
    return reason + recompile_bit;          // no condition has yet been true
  } else {
    return DS_REASON_MASK + recompile_bit;  // fall to state lattice bottom
  }
}
//-----------------------trap_state_is_recompiled------------------------------
bool Deoptimization::trap_state_is_recompiled(int trap_state) {
  return (trap_state & DS_RECOMPILE_BIT) != 0;
}
//-----------------------trap_state_set_recompiled-----------------------------
int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) {
  if (z)  return trap_state |  DS_RECOMPILE_BIT;
  else    return trap_state & ~DS_RECOMPILE_BIT;
}
//---------------------------format_trap_state---------------------------------
// This is used for debugging and diagnostics, including LogFile output.
const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
                                              int trap_state) {
  assert(buflen > 0, "sanity");
  DeoptReason reason      = trap_state_reason(trap_state);
  bool        recomp_flag = trap_state_is_recompiled(trap_state);
  // Re-encode the state from its decoded components.
  int decoded_state = 0;
  if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many)
    decoded_state = trap_state_add_reason(decoded_state, reason);
  if (recomp_flag)
    decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag);
  // If the state re-encodes properly, format it symbolically.
  // Because this routine is used for debugging and diagnostics,
  // be robust even if the state is a strange value.
  size_t len;
  if (decoded_state != trap_state) {
    // Random buggy state that doesn't decode??
    len = jio_snprintf(buf, buflen, "#%d", trap_state);
  } else {
    len = jio_snprintf(buf, buflen, "%s%s",
                       trap_reason_name(reason),
                       recomp_flag ? " recompiled" : "");
  }
  return buf;
}


//--------------------------------statics--------------------------------------
const char* Deoptimization::_trap_reason_name[] = {
  // Note:  Keep this in sync. with enum DeoptReason.
  "none",
  "null_check",
  "null_assert",
  "range_check",
  "class_check",
  "array_check",
  "intrinsic",
  "bimorphic",
  "profile_predicate",
  "auto_vectorization_check",
  "unloaded",
  "uninitialized",
  "initialized",
  "unreached",
  "unhandled",
  "constraint",
  "div0_check",
  "age",
  "predicate",
  "loop_limit_check",
  "speculate_class_check",
  "speculate_null_check",
  "speculate_null_assert",
  "unstable_if",
  "unstable_fused_if",
  "receiver_constraint",
  "not_compiled_exception_handler",
  "short_running_loop",
  "tenured"
};
const char* Deoptimization::_trap_action_name[] = {
  // Note:  Keep this in sync. with enum DeoptAction.
  "none",
  "maybe_recompile",
  "reinterpret",
  "make_not_entrant",
  "make_not_compilable"
};

const char* Deoptimization::trap_reason_name(int reason) {
  // Check that every reason has a name
  STATIC_ASSERT(sizeof(_trap_reason_name)/sizeof(const char*) == Reason_LIMIT);

  if (reason == Reason_many)  return "many";
  if ((uint)reason < Reason_LIMIT)
    return _trap_reason_name[reason];
  static char buf[20];
  os::snprintf_checked(buf, sizeof(buf), "reason%d", reason);
  return buf;
}
const char* Deoptimization::trap_action_name(int action) {
  // Check that every action has a name
  STATIC_ASSERT(sizeof(_trap_action_name)/sizeof(const char*) == Action_LIMIT);

  if ((uint)action < Action_LIMIT)
    return _trap_action_name[action];
  static char buf[20];
  os::snprintf_checked(buf, sizeof(buf), "action%d", action);
  return buf;
}

// This is used for debugging and diagnostics, including LogFile output.
const char* Deoptimization::format_trap_request(char* buf, size_t buflen,
                                                int trap_request) {
  jint unloaded_class_index = trap_request_index(trap_request);
  const char* reason = trap_reason_name(trap_request_reason(trap_request));
  const char* action = trap_action_name(trap_request_action(trap_request));
  size_t len;
  if (unloaded_class_index < 0) {
    len = jio_snprintf(buf, buflen, "reason='%s' action='%s'", reason, action);
  } else {
    len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'",
                       reason, action, unloaded_class_index);
  }
  return buf;
}

juint Deoptimization::_deoptimization_hist
        [Deoptimization::Reason_LIMIT]
    [1 + Deoptimization::Action_LIMIT]
        [Deoptimization::BC_CASE_LIMIT]
  = {0};

enum {
  LSB_BITS = 8,
  LSB_MASK = right_n_bits(LSB_BITS)
};

void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
                                       Bytecodes::Code bc) {
  assert(reason >= 0 && reason < Reason_LIMIT, "oob");
  assert(action >= 0 && action < Action_LIMIT, "oob");
  _deoptimization_hist[Reason_none][0][0] += 1;  // total
  _deoptimization_hist[reason][0][0]      += 1;  // per-reason total
  juint* cases = _deoptimization_hist[reason][1+action];
  juint* bc_counter_addr = nullptr;
  juint  bc_counter      = 0;
  // Look for an unused counter, or an exact match to this BC.
  if (bc != Bytecodes::_illegal) {
    for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
      juint* counter_addr = &cases[bc_case];
      juint  counter = *counter_addr;
      if ((counter == 0 && bc_counter_addr == nullptr)
          || (Bytecodes::Code)(counter & LSB_MASK) == bc) {
        // this counter is either free or is already devoted to this BC
        bc_counter_addr = counter_addr;
        bc_counter = counter | bc;
      }
    }
  }
  if (bc_counter_addr == nullptr) {
    // Overflow, or no given bytecode.
    bc_counter_addr = &cases[BC_CASE_LIMIT-1];
    bc_counter = (*bc_counter_addr & ~LSB_MASK);  // clear LSB
  }
  *bc_counter_addr = bc_counter + (1 << LSB_BITS);
}

jint Deoptimization::total_deoptimization_count() {
  return _deoptimization_hist[Reason_none][0][0];
}

// Get the deopt count for a specific reason and a specific action. If either
// one of 'reason' or 'action' is null, the method returns the sum of all
// deoptimizations with the specific 'action' or 'reason' respectively.
// If both arguments are null, the method returns the total deopt count.
jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
  if (reason_str == nullptr && action_str == nullptr) {
    return total_deoptimization_count();
  }
  juint counter = 0;
  for (int reason = 0; reason < Reason_LIMIT; reason++) {
    if (reason_str == nullptr || !strcmp(reason_str, trap_reason_name(reason))) {
      for (int action = 0; action < Action_LIMIT; action++) {
        if (action_str == nullptr || !strcmp(action_str, trap_action_name(action))) {
          juint* cases = _deoptimization_hist[reason][1+action];
          for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
            counter += cases[bc_case] >> LSB_BITS;
          }
        }
      }
    }
  }
  return counter;
}

void Deoptimization::print_statistics() {
  juint total = total_deoptimization_count();
  juint account = total;
  if (total != 0) {
    ttyLocker ttyl;
    if (xtty != nullptr)  xtty->head("statistics type='deoptimization'");
    tty->print_cr("Deoptimization traps recorded:");
    #define PRINT_STAT_LINE(name, r) \
      tty->print_cr("  %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name);
    PRINT_STAT_LINE("total", total);
    // For each non-zero entry in the histogram, print the reason,
    // the action, and (if specifically known) the type of bytecode.
    for (int reason = 0; reason < Reason_LIMIT; reason++) {
      for (int action = 0; action < Action_LIMIT; action++) {
        juint* cases = _deoptimization_hist[reason][1+action];
        for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) {
          juint counter = cases[bc_case];
          if (counter != 0) {
            char name[1*K];
            Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK);
            os::snprintf_checked(name, sizeof(name), "%s/%s/%s",
                    trap_reason_name(reason),
                    trap_action_name(action),
                    Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other");
            juint r = counter >> LSB_BITS;
            tty->print_cr("  %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total);
            account -= r;
          }
        }
      }
    }
    if (account != 0) {
      PRINT_STAT_LINE("unaccounted", account);
    }
    #undef PRINT_STAT_LINE
    if (xtty != nullptr)  xtty->tail("statistics");
  }
}

#else // COMPILER2

// Stubs for C1 only system.
bool Deoptimization::trap_state_is_recompiled(int trap_state) {
  return false;
}

const char* Deoptimization::trap_reason_name(int reason) {
  return "unknown";
}

jint Deoptimization::total_deoptimization_count() {
  return 0;
}

jint Deoptimization::deoptimization_count(const char *reason_str, const char *action_str) {
  return 0;
}

void Deoptimization::print_statistics() {
  // no output
}

void
Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) {
  // no update
}

int Deoptimization::trap_state_has_reason(int trap_state, int reason) {
  return 0;
}

void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action,
                                       Bytecodes::Code bc) {
  // no update
}

const char* Deoptimization::format_trap_state(char* buf, size_t buflen,
                                              int trap_state) {
  jio_snprintf(buf, buflen, "#%d", trap_state);
  return buf;
}

#endif // COMPILER2
