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