< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page

  32 #include "code/nmethod.hpp"
  33 #include "code/pcDesc.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "compiler/compilationPolicy.hpp"
  36 #include "compiler/compilerDefinitions.inline.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "interpreter/bytecode.hpp"
  39 #include "interpreter/bytecodeStream.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "jvm.h"
  43 #include "logging/log.hpp"
  44 #include "logging/logLevel.hpp"
  45 #include "logging/logMessage.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constantPool.hpp"


  52 #include "oops/fieldStreams.inline.hpp"
  53 #include "oops/method.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"

  57 #include "oops/typeArrayOop.inline.hpp"
  58 #include "oops/verifyOopClosure.hpp"
  59 #include "prims/jvmtiDeferredUpdates.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/vectorSupport.hpp"
  64 #include "runtime/atomic.hpp"
  65 #include "runtime/continuation.hpp"
  66 #include "runtime/continuationEntry.inline.hpp"
  67 #include "runtime/deoptimization.hpp"
  68 #include "runtime/escapeBarrier.hpp"
  69 #include "runtime/fieldDescriptor.hpp"
  70 #include "runtime/fieldDescriptor.inline.hpp"
  71 #include "runtime/frame.inline.hpp"
  72 #include "runtime/handles.inline.hpp"
  73 #include "runtime/interfaceSupport.inline.hpp"
  74 #include "runtime/javaThread.hpp"
  75 #include "runtime/jniHandles.inline.hpp"
  76 #include "runtime/keepStackGCProcessed.hpp"

 289 
 290   return fetch_unroll_info_helper(current, exec_mode);
 291 JRT_END
 292 
 293 #if COMPILER2_OR_JVMCI
 294 // print information about reallocated objects
 295 static void print_objects(JavaThread* deoptee_thread,
 296                           GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
 297   ResourceMark rm;
 298   stringStream st;  // change to logStream with logging
 299   st.print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(deoptee_thread));
 300   fieldDescriptor fd;
 301 
 302   for (int i = 0; i < objects->length(); i++) {
 303     ObjectValue* sv = (ObjectValue*) objects->at(i);
 304     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
 305     Handle obj = sv->value();
 306 
 307     st.print("     object <" INTPTR_FORMAT "> of type ", p2i(sv->value()()));
 308     k->print_value_on(&st);
 309     assert(obj.not_null() || realloc_failures, "reallocation was missed");
 310     if (obj.is_null()) {
 311       st.print(" allocation failed");




 312     } else {
 313       st.print(" allocated (" SIZE_FORMAT " bytes)", obj->size() * HeapWordSize);
 314     }
 315     st.cr();
 316 
 317     if (Verbose && !obj.is_null()) {
 318       k->oop_print_on(obj(), &st);
 319     }
 320   }
 321   tty->print_raw(st.freeze());
 322 }
 323 
 324 static bool rematerialize_objects(JavaThread* thread, int exec_mode, CompiledMethod* compiled_method,
 325                                   frame& deoptee, RegisterMap& map, GrowableArray<compiledVFrame*>* chunk,
 326                                   bool& deoptimized_objects) {
 327   bool realloc_failures = false;
 328   assert (chunk->at(0)->scope() != nullptr,"expect only compiled java frames");
 329 
 330   JavaThread* deoptee_thread = chunk->at(0)->thread();
 331   assert(exec_mode == Deoptimization::Unpack_none || (deoptee_thread == thread),
 332          "a frame can only be deoptimized by the owner thread");
 333 
 334   GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects_to_rematerialize(deoptee, map);
 335 
 336   // The flag return_oop() indicates call sites which return oop
 337   // in compiled code. Such sites include java method calls,
 338   // runtime calls (for example, used to allocate new objects/arrays
 339   // on slow code path) and any other calls generated in compiled code.
 340   // It is not guaranteed that we can get such information here only
 341   // by analyzing bytecode in deoptimized frames. This is why this flag
 342   // is set during method compilation (see Compile::Process_OopMap_Node()).
 343   // If the previous frame was popped or if we are dispatching an exception,
 344   // we don't have an oop result.
 345   bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution() && (exec_mode == Deoptimization::Unpack_deopt);
 346   Handle return_value;











 347   if (save_oop_result) {
 348     // Reallocation may trigger GC. If deoptimization happened on return from
 349     // call which returns oop we need to save it since it is not in oopmap.
 350     oop result = deoptee.saved_oop_result(&map);
 351     assert(oopDesc::is_oop_or_null(result), "must be oop");
 352     return_value = Handle(thread, result);
 353     assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
 354     if (TraceDeoptimization) {
 355       tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
 356       tty->cr();
 357     }
 358   }
 359   if (objects != nullptr) {
 360     if (exec_mode == Deoptimization::Unpack_none) {
 361       assert(thread->thread_state() == _thread_in_vm, "assumption");
 362       JavaThread* THREAD = thread; // For exception macros.
 363       // Clear pending OOM if reallocation fails and return true indicating allocation failure
 364       realloc_failures = Deoptimization::realloc_objects(thread, &deoptee, &map, objects, CHECK_AND_CLEAR_(true));







 365       deoptimized_objects = true;
 366     } else {
 367       JavaThread* current = thread; // For JRT_BLOCK
 368       JRT_BLOCK
 369       realloc_failures = Deoptimization::realloc_objects(thread, &deoptee, &map, objects, THREAD);







 370       JRT_END
 371     }
 372     bool skip_internal = (compiled_method != nullptr) && !compiled_method->is_compiled_by_jvmci();
 373     Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal);
 374     if (TraceDeoptimization) {
 375       print_objects(deoptee_thread, objects, realloc_failures);
 376     }
 377   }
 378   if (save_oop_result) {
 379     // Restore result.
 380     deoptee.set_saved_oop_result(&map, return_value());

 381   }
 382   return realloc_failures;
 383 }
 384 
 385 static void restore_eliminated_locks(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures,
 386                                      frame& deoptee, int exec_mode, bool& deoptimized_objects) {
 387   JavaThread* deoptee_thread = chunk->at(0)->thread();
 388   assert(!EscapeBarrier::objs_are_deoptimized(deoptee_thread, deoptee.id()), "must relock just once");
 389   assert(thread == Thread::current(), "should be");
 390   HandleMark hm(thread);
 391 #ifndef PRODUCT
 392   bool first = true;
 393 #endif // !PRODUCT
 394   // Start locking from outermost/oldest frame
 395   for (int i = (chunk->length() - 1); i >= 0; i--) {
 396     compiledVFrame* cvf = chunk->at(i);
 397     assert (cvf->scope() != nullptr,"expect only compiled java frames");
 398     GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
 399     if (monitors->is_nonempty()) {
 400       bool relocked = Deoptimization::relock_objects(thread, monitors, deoptee_thread, deoptee,

 696   // its caller's stack by. If the caller is a compiled frame then
 697   // we pretend that the callee has no parameters so that the
 698   // extension counts for the full amount of locals and not just
 699   // locals-parms. This is because without a c2i adapter the parm
 700   // area as created by the compiled frame will not be usable by
 701   // the interpreter. (Depending on the calling convention there
 702   // may not even be enough space).
 703 
 704   // QQQ I'd rather see this pushed down into last_frame_adjust
 705   // and have it take the sender (aka caller).
 706 
 707   if (!deopt_sender.is_interpreted_frame() || caller_was_method_handle) {
 708     caller_adjustment = last_frame_adjust(0, callee_locals);
 709   } else if (callee_locals > callee_parameters) {
 710     // The caller frame may need extending to accommodate
 711     // non-parameter locals of the first unpacked interpreted frame.
 712     // Compute that adjustment.
 713     caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);
 714   }
 715 
 716   // If the sender is deoptimized the we must retrieve the address of the handler
 717   // since the frame will "magically" show the original pc before the deopt
 718   // and we'd undo the deopt.
 719 
 720   frame_pcs[0] = Continuation::is_cont_barrier_frame(deoptee) ? StubRoutines::cont_returnBarrier() : deopt_sender.raw_pc();
 721   if (Continuation::is_continuation_enterSpecial(deopt_sender)) {
 722     ContinuationEntry::from_frame(deopt_sender)->set_argsize(0);
 723   }
 724 
 725   assert(CodeCache::find_blob(frame_pcs[0]) != nullptr, "bad pc");
 726 
 727 #if INCLUDE_JVMCI
 728   if (exceptionObject() != nullptr) {
 729     current->set_exception_oop(exceptionObject());
 730     exec_mode = Unpack_exception;
 731   }
 732 #endif
 733 
 734   if (current->frames_to_pop_failed_realloc() > 0 && exec_mode != Unpack_uncommon_trap) {
 735     assert(current->has_pending_exception(), "should have thrown OOME");
 736     current->set_exception_oop(current->pending_exception());

1196        case T_LONG:    return LongBoxCache::singleton(THREAD)->lookup_raw(value->get_intptr(), cache_init_error);
1197        default:;
1198      }
1199    }
1200    return nullptr;
1201 }
1202 #endif // INCLUDE_JVMCI
1203 
1204 #if COMPILER2_OR_JVMCI
1205 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS) {
1206   Handle pending_exception(THREAD, thread->pending_exception());
1207   const char* exception_file = thread->exception_file();
1208   int exception_line = thread->exception_line();
1209   thread->clear_pending_exception();
1210 
1211   bool failures = false;
1212 
1213   for (int i = 0; i < objects->length(); i++) {
1214     assert(objects->at(i)->is_object(), "invalid debug information");
1215     ObjectValue* sv = (ObjectValue*) objects->at(i);
1216 
1217     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
1218     oop obj = nullptr;
1219 











1220     bool cache_init_error = false;
1221     if (k->is_instance_klass()) {
1222 #if INCLUDE_JVMCI
1223       CompiledMethod* cm = fr->cb()->as_compiled_method_or_null();
1224       if (cm->is_compiled_by_jvmci() && sv->is_auto_box()) {
1225         AutoBoxObjectValue* abv = (AutoBoxObjectValue*) sv;
1226         obj = get_cached_box(abv, fr, reg_map, cache_init_error, THREAD);
1227         if (obj != nullptr) {
1228           // Set the flag to indicate the box came from a cache, so that we can skip the field reassignment for it.
1229           abv->set_cached(true);
1230         } else if (cache_init_error) {
1231           // Results in an OOME which is valid (as opposed to a class initialization error)
1232           // and is fine for the rare case a cache initialization failing.
1233           failures = true;
1234         }
1235       }
1236 #endif // INCLUDE_JVMCI
1237 
1238       InstanceKlass* ik = InstanceKlass::cast(k);
1239       if (obj == nullptr && !cache_init_error) {
1240 #if COMPILER2_OR_JVMCI
1241         if (EnableVectorSupport && VectorSupport::is_vector(ik)) {
1242           obj = VectorSupport::allocate_vector(ik, fr, reg_map, sv, THREAD);
1243         } else {
1244           obj = ik->allocate_instance(THREAD);
1245         }
1246 #else
1247         obj = ik->allocate_instance(THREAD);
1248 #endif // COMPILER2_OR_JVMCI
1249       }




1250     } else if (k->is_typeArray_klass()) {
1251       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1252       assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
1253       int len = sv->field_size() / type2size[ak->element_type()];
1254       obj = ak->allocate(len, THREAD);
1255     } else if (k->is_objArray_klass()) {
1256       ObjArrayKlass* ak = ObjArrayKlass::cast(k);
1257       obj = ak->allocate(sv->field_size(), THREAD);
1258     }
1259 
1260     if (obj == nullptr) {
1261       failures = true;
1262     }
1263 
1264     assert(sv->value().is_null(), "redundant reallocation");
1265     assert(obj != nullptr || HAS_PENDING_EXCEPTION || cache_init_error, "allocation should succeed or we should get an exception");
1266     CLEAR_PENDING_EXCEPTION;
1267     sv->set_value(obj);
1268   }
1269 
1270   if (failures) {
1271     THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
1272   } else if (pending_exception.not_null()) {
1273     thread->set_pending_exception(pending_exception(), exception_file, exception_line);
1274   }
1275 
1276   return failures;
1277 }
1278 















1279 #if INCLUDE_JVMCI
1280 /**
1281  * For primitive types whose kind gets "erased" at runtime (shorts become stack ints),
1282  * we need to somehow be able to recover the actual kind to be able to write the correct
1283  * amount of bytes.
1284  * For that purpose, this method assumes that, for an entry spanning n bytes at index i,
1285  * the entries at index n + 1 to n + i are 'markers'.
1286  * For example, if we were writing a short at index 4 of a byte array of size 8, the
1287  * expected form of the array would be:
1288  *
1289  * {b0, b1, b2, b3, INT, marker, b6, b7}
1290  *
1291  * Thus, in order to get back the size of the entry, we simply need to count the number
1292  * of marked entries
1293  *
1294  * @param virtualArray the virtualized byte array
1295  * @param i index of the virtual entry we are recovering
1296  * @return The number of bytes the entry spans
1297  */
1298 static int count_number_of_bytes_for_entry(ObjectValue *virtualArray, int i) {

1424       default:
1425         ShouldNotReachHere();
1426     }
1427     index++;
1428   }
1429 }
1430 
1431 // restore fields of an eliminated object array
1432 void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {
1433   for (int i = 0; i < sv->field_size(); i++) {
1434     StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
1435     assert(value->type() == T_OBJECT, "object element expected");
1436     obj->obj_at_put(i, value->get_obj()());
1437   }
1438 }
1439 
1440 class ReassignedField {
1441 public:
1442   int _offset;
1443   BasicType _type;


1444 public:
1445   ReassignedField() {
1446     _offset = 0;
1447     _type = T_ILLEGAL;
1448   }
1449 };
1450 
1451 int compare(ReassignedField* left, ReassignedField* right) {
1452   return left->_offset - right->_offset;
1453 }
1454 
1455 // Restore fields of an eliminated instance object using the same field order
1456 // returned by HotSpotResolvedObjectTypeImpl.getInstanceFields(true)
1457 static int reassign_fields_by_klass(InstanceKlass* klass, frame* fr, RegisterMap* reg_map, ObjectValue* sv, int svIndex, oop obj, bool skip_internal) {
1458   GrowableArray<ReassignedField>* fields = new GrowableArray<ReassignedField>();
1459   InstanceKlass* ik = klass;
1460   while (ik != nullptr) {
1461     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1462       if (!fs.access_flags().is_static() && (!skip_internal || !fs.field_flags().is_injected())) {
1463         ReassignedField field;
1464         field._offset = fs.offset();
1465         field._type = Signature::basic_type(fs.signature());









1466         fields->append(field);
1467       }
1468     }
1469     ik = ik->superklass();
1470   }
1471   fields->sort(compare);
1472   for (int i = 0; i < fields->length(); i++) {











1473     ScopeValue* scope_field = sv->field_at(svIndex);
1474     StackValue* value = StackValue::create_stack_value(fr, reg_map, scope_field);
1475     int offset = fields->at(i)._offset;
1476     BasicType type = fields->at(i)._type;
1477     switch (type) {
1478       case T_OBJECT: case T_ARRAY:

1479         assert(value->type() == T_OBJECT, "Agreement.");
1480         obj->obj_field_put(offset, value->get_obj()());
1481         break;
1482 
1483       case T_INT: case T_FLOAT: { // 4 bytes.
1484         assert(value->type() == T_INT, "Agreement.");
1485         bool big_value = false;
1486         if (i+1 < fields->length() && fields->at(i+1)._type == T_INT) {
1487           if (scope_field->is_location()) {
1488             Location::Type type = ((LocationValue*) scope_field)->location().type();
1489             if (type == Location::dbl || type == Location::lng) {
1490               big_value = true;
1491             }
1492           }
1493           if (scope_field->is_constant_int()) {
1494             ScopeValue* next_scope_field = sv->field_at(svIndex + 1);
1495             if (next_scope_field->is_constant_long() || next_scope_field->is_constant_double()) {
1496               big_value = true;
1497             }
1498           }

1532         break;
1533 
1534       case T_BYTE:
1535         assert(value->type() == T_INT, "Agreement.");
1536         obj->byte_field_put(offset, (jbyte)value->get_jint());
1537         break;
1538 
1539       case T_BOOLEAN:
1540         assert(value->type() == T_INT, "Agreement.");
1541         obj->bool_field_put(offset, (jboolean)value->get_jint());
1542         break;
1543 
1544       default:
1545         ShouldNotReachHere();
1546     }
1547     svIndex++;
1548   }
1549   return svIndex;
1550 }
1551 














1552 // restore fields of all eliminated objects and arrays
1553 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, bool skip_internal) {
1554   for (int i = 0; i < objects->length(); i++) {
1555     assert(objects->at(i)->is_object(), "invalid debug information");
1556     ObjectValue* sv = (ObjectValue*) objects->at(i);
1557     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
1558     Handle obj = sv->value();
1559     assert(obj.not_null() || realloc_failures, "reallocation was missed");
1560 #ifndef PRODUCT
1561     if (PrintDeoptimizationDetails) {
1562       tty->print_cr("reassign fields for object of type %s!", k->name()->as_C_string());
1563     }
1564 #endif // !PRODUCT
1565 
1566     if (obj.is_null()) {
1567       continue;
1568     }
1569 
1570 #if INCLUDE_JVMCI
1571     // Don't reassign fields of boxes that came from a cache. Caches may be in CDS.
1572     if (sv->is_auto_box() && ((AutoBoxObjectValue*) sv)->is_cached()) {
1573       continue;
1574     }
1575 #endif // INCLUDE_JVMCI
1576 #if COMPILER2_OR_JVMCI
1577     if (EnableVectorSupport && VectorSupport::is_vector(k)) {
1578       assert(sv->field_size() == 1, "%s not a vector", k->name()->as_C_string());
1579       ScopeValue* payload = sv->field_at(0);
1580       if (payload->is_location() &&
1581           payload->as_LocationValue()->location().type() == Location::vector) {
1582 #ifndef PRODUCT
1583         if (PrintDeoptimizationDetails) {
1584           tty->print_cr("skip field reassignment for this vector - it should be assigned already");
1585           if (Verbose) {
1586             Handle obj = sv->value();
1587             k->oop_print_on(obj(), tty);
1588           }
1589         }
1590 #endif // !PRODUCT
1591         continue; // Such vector's value was already restored in VectorSupport::allocate_vector().
1592       }
1593       // Else fall-through to do assignment for scalar-replaced boxed vector representation
1594       // which could be restored after vector object allocation.
1595     }
1596 #endif /* !COMPILER2_OR_JVMCI */
1597     if (k->is_instance_klass()) {
1598       InstanceKlass* ik = InstanceKlass::cast(k);
1599       reassign_fields_by_klass(ik, fr, reg_map, sv, 0, obj(), skip_internal);



1600     } else if (k->is_typeArray_klass()) {
1601       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1602       reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());
1603     } else if (k->is_objArray_klass()) {
1604       reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());
1605     }
1606   }
1607   // These objects may escape when we return to Interpreter after deoptimization.
1608   // We need barrier so that stores that initialize these objects can't be reordered
1609   // with subsequent stores that make these objects accessible by other threads.
1610   OrderAccess::storestore();
1611 }
1612 
1613 
1614 // relock objects for which synchronization was eliminated
1615 bool Deoptimization::relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,
1616                                     JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures) {
1617   bool relocked_objects = false;
1618   for (int i = 0; i < monitors->length(); i++) {
1619     MonitorInfo* mon_info = monitors->at(i);

1754     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1755     cm->log_identity(xtty);
1756     xtty->end_head();
1757     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1758       xtty->begin_elem("jvms bci='%d'", sd->bci());
1759       xtty->method(sd->method());
1760       xtty->end_elem();
1761       if (sd->is_top())  break;
1762     }
1763     xtty->tail("deoptimized");
1764   }
1765 
1766   Continuation::notify_deopt(thread, fr.sp());
1767 
1768   // Patch the compiled method so that when execution returns to it we will
1769   // deopt the execution state and return to the interpreter.
1770   fr.deoptimize(thread);
1771 }
1772 
1773 void Deoptimization::deoptimize(JavaThread* thread, frame fr, DeoptReason reason) {
1774   // Deoptimize only if the frame comes from compile code.
1775   // Do not deoptimize the frame which is already patched
1776   // during the execution of the loops below.
1777   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1778     return;
1779   }
1780   ResourceMark rm;
1781   deoptimize_single_frame(thread, fr, reason);
1782 }
1783 
1784 #if INCLUDE_JVMCI
1785 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1786   // there is no exception handler for this pc => deoptimize
1787   cm->make_not_entrant();
1788 
1789   // Use Deoptimization::deoptimize for all of its side-effects:
1790   // gathering traps statistics, logging...
1791   // it also patches the return pc but we do not care about that
1792   // since we return a continuation to the deopt_blob below.
1793   JavaThread* thread = JavaThread::current();
1794   RegisterMap reg_map(thread,

  32 #include "code/nmethod.hpp"
  33 #include "code/pcDesc.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "compiler/compilationPolicy.hpp"
  36 #include "compiler/compilerDefinitions.inline.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "interpreter/bytecode.hpp"
  39 #include "interpreter/bytecodeStream.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "jvm.h"
  43 #include "logging/log.hpp"
  44 #include "logging/logLevel.hpp"
  45 #include "logging/logMessage.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constantPool.hpp"
  52 #include "oops/flatArrayKlass.hpp"
  53 #include "oops/flatArrayOop.hpp"
  54 #include "oops/fieldStreams.inline.hpp"
  55 #include "oops/method.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/inlineKlass.inline.hpp"
  60 #include "oops/typeArrayOop.inline.hpp"
  61 #include "oops/verifyOopClosure.hpp"
  62 #include "prims/jvmtiDeferredUpdates.hpp"
  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/jvmtiThreadState.hpp"
  65 #include "prims/methodHandles.hpp"
  66 #include "prims/vectorSupport.hpp"
  67 #include "runtime/atomic.hpp"
  68 #include "runtime/continuation.hpp"
  69 #include "runtime/continuationEntry.inline.hpp"
  70 #include "runtime/deoptimization.hpp"
  71 #include "runtime/escapeBarrier.hpp"
  72 #include "runtime/fieldDescriptor.hpp"
  73 #include "runtime/fieldDescriptor.inline.hpp"
  74 #include "runtime/frame.inline.hpp"
  75 #include "runtime/handles.inline.hpp"
  76 #include "runtime/interfaceSupport.inline.hpp"
  77 #include "runtime/javaThread.hpp"
  78 #include "runtime/jniHandles.inline.hpp"
  79 #include "runtime/keepStackGCProcessed.hpp"

 292 
 293   return fetch_unroll_info_helper(current, exec_mode);
 294 JRT_END
 295 
 296 #if COMPILER2_OR_JVMCI
 297 // print information about reallocated objects
 298 static void print_objects(JavaThread* deoptee_thread,
 299                           GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
 300   ResourceMark rm;
 301   stringStream st;  // change to logStream with logging
 302   st.print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(deoptee_thread));
 303   fieldDescriptor fd;
 304 
 305   for (int i = 0; i < objects->length(); i++) {
 306     ObjectValue* sv = (ObjectValue*) objects->at(i);
 307     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
 308     Handle obj = sv->value();
 309 
 310     st.print("     object <" INTPTR_FORMAT "> of type ", p2i(sv->value()()));
 311     k->print_value_on(&st);
 312     assert(obj.not_null() || k->is_inline_klass() || realloc_failures, "reallocation was missed");
 313     if (obj.is_null()) {
 314       if (k->is_inline_klass()) {
 315         st.print(" is null");
 316       } else {
 317         st.print(" allocation failed");
 318       }
 319     } else {
 320       st.print(" allocated (" SIZE_FORMAT " bytes)", obj->size() * HeapWordSize);
 321     }
 322     st.cr();
 323 
 324     if (Verbose && !obj.is_null()) {
 325       k->oop_print_on(obj(), &st);
 326     }
 327   }
 328   tty->print_raw(st.freeze());
 329 }
 330 
 331 static bool rematerialize_objects(JavaThread* thread, int exec_mode, CompiledMethod* compiled_method,
 332                                   frame& deoptee, RegisterMap& map, GrowableArray<compiledVFrame*>* chunk,
 333                                   bool& deoptimized_objects) {
 334   bool realloc_failures = false;
 335   assert (chunk->at(0)->scope() != nullptr,"expect only compiled java frames");
 336 
 337   JavaThread* deoptee_thread = chunk->at(0)->thread();
 338   assert(exec_mode == Deoptimization::Unpack_none || (deoptee_thread == thread),
 339          "a frame can only be deoptimized by the owner thread");
 340 
 341   GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects_to_rematerialize(deoptee, map);
 342 
 343   // The flag return_oop() indicates call sites which return oop
 344   // in compiled code. Such sites include java method calls,
 345   // runtime calls (for example, used to allocate new objects/arrays
 346   // on slow code path) and any other calls generated in compiled code.
 347   // It is not guaranteed that we can get such information here only
 348   // by analyzing bytecode in deoptimized frames. This is why this flag
 349   // is set during method compilation (see Compile::Process_OopMap_Node()).
 350   // If the previous frame was popped or if we are dispatching an exception,
 351   // we don't have an oop result.
 352   ScopeDesc* scope = chunk->at(0)->scope();
 353   bool save_oop_result = scope->return_oop() && !thread->popframe_forcing_deopt_reexecution() && (exec_mode == Deoptimization::Unpack_deopt);
 354   // In case of the return of multiple values, we must take care
 355   // of all oop return values.
 356   GrowableArray<Handle> return_oops;
 357   InlineKlass* vk = nullptr;
 358   if (save_oop_result && scope->return_scalarized()) {
 359     vk = InlineKlass::returned_inline_klass(map);
 360     if (vk != nullptr) {
 361       vk->save_oop_fields(map, return_oops);
 362       save_oop_result = false;
 363     }
 364   }
 365   if (save_oop_result) {
 366     // Reallocation may trigger GC. If deoptimization happened on return from
 367     // call which returns oop we need to save it since it is not in oopmap.
 368     oop result = deoptee.saved_oop_result(&map);
 369     assert(oopDesc::is_oop_or_null(result), "must be oop");
 370     return_oops.push(Handle(thread, result));
 371     assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
 372     if (TraceDeoptimization) {
 373       tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
 374       tty->cr();
 375     }
 376   }
 377   if (objects != nullptr || vk != nullptr) {
 378     if (exec_mode == Deoptimization::Unpack_none) {
 379       assert(thread->thread_state() == _thread_in_vm, "assumption");
 380       JavaThread* THREAD = thread; // For exception macros.
 381       // Clear pending OOM if reallocation fails and return true indicating allocation failure
 382       if (vk != nullptr) {
 383         realloc_failures = Deoptimization::realloc_inline_type_result(vk, map, return_oops, CHECK_AND_CLEAR_(true));
 384       }
 385       if (objects != nullptr) {
 386         realloc_failures = realloc_failures || Deoptimization::realloc_objects(thread, &deoptee, &map, objects, CHECK_AND_CLEAR_(true));
 387         bool skip_internal = (compiled_method != nullptr) && !compiled_method->is_compiled_by_jvmci();
 388         Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal, CHECK_AND_CLEAR_(true));
 389       }
 390       deoptimized_objects = true;
 391     } else {
 392       JavaThread* current = thread; // For JRT_BLOCK
 393       JRT_BLOCK
 394       if (vk != nullptr) {
 395         realloc_failures = Deoptimization::realloc_inline_type_result(vk, map, return_oops, THREAD);
 396       }
 397       if (objects != nullptr) {
 398         realloc_failures = realloc_failures || Deoptimization::realloc_objects(thread, &deoptee, &map, objects, THREAD);
 399         bool skip_internal = (compiled_method != nullptr) && !compiled_method->is_compiled_by_jvmci();
 400         Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal, THREAD);
 401       }
 402       JRT_END
 403     }
 404     if (TraceDeoptimization && objects != nullptr) {


 405       print_objects(deoptee_thread, objects, realloc_failures);
 406     }
 407   }
 408   if (save_oop_result || vk != nullptr) {
 409     // Restore result.
 410     assert(return_oops.length() == 1, "no inline type");
 411     deoptee.set_saved_oop_result(&map, return_oops.pop()());
 412   }
 413   return realloc_failures;
 414 }
 415 
 416 static void restore_eliminated_locks(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures,
 417                                      frame& deoptee, int exec_mode, bool& deoptimized_objects) {
 418   JavaThread* deoptee_thread = chunk->at(0)->thread();
 419   assert(!EscapeBarrier::objs_are_deoptimized(deoptee_thread, deoptee.id()), "must relock just once");
 420   assert(thread == Thread::current(), "should be");
 421   HandleMark hm(thread);
 422 #ifndef PRODUCT
 423   bool first = true;
 424 #endif // !PRODUCT
 425   // Start locking from outermost/oldest frame
 426   for (int i = (chunk->length() - 1); i >= 0; i--) {
 427     compiledVFrame* cvf = chunk->at(i);
 428     assert (cvf->scope() != nullptr,"expect only compiled java frames");
 429     GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
 430     if (monitors->is_nonempty()) {
 431       bool relocked = Deoptimization::relock_objects(thread, monitors, deoptee_thread, deoptee,

 727   // its caller's stack by. If the caller is a compiled frame then
 728   // we pretend that the callee has no parameters so that the
 729   // extension counts for the full amount of locals and not just
 730   // locals-parms. This is because without a c2i adapter the parm
 731   // area as created by the compiled frame will not be usable by
 732   // the interpreter. (Depending on the calling convention there
 733   // may not even be enough space).
 734 
 735   // QQQ I'd rather see this pushed down into last_frame_adjust
 736   // and have it take the sender (aka caller).
 737 
 738   if (!deopt_sender.is_interpreted_frame() || caller_was_method_handle) {
 739     caller_adjustment = last_frame_adjust(0, callee_locals);
 740   } else if (callee_locals > callee_parameters) {
 741     // The caller frame may need extending to accommodate
 742     // non-parameter locals of the first unpacked interpreted frame.
 743     // Compute that adjustment.
 744     caller_adjustment = last_frame_adjust(callee_parameters, callee_locals);
 745   }
 746 
 747   // If the sender is deoptimized we must retrieve the address of the handler
 748   // since the frame will "magically" show the original pc before the deopt
 749   // and we'd undo the deopt.
 750 
 751   frame_pcs[0] = Continuation::is_cont_barrier_frame(deoptee) ? StubRoutines::cont_returnBarrier() : deopt_sender.raw_pc();
 752   if (Continuation::is_continuation_enterSpecial(deopt_sender)) {
 753     ContinuationEntry::from_frame(deopt_sender)->set_argsize(0);
 754   }
 755 
 756   assert(CodeCache::find_blob(frame_pcs[0]) != nullptr, "bad pc");
 757 
 758 #if INCLUDE_JVMCI
 759   if (exceptionObject() != nullptr) {
 760     current->set_exception_oop(exceptionObject());
 761     exec_mode = Unpack_exception;
 762   }
 763 #endif
 764 
 765   if (current->frames_to_pop_failed_realloc() > 0 && exec_mode != Unpack_uncommon_trap) {
 766     assert(current->has_pending_exception(), "should have thrown OOME");
 767     current->set_exception_oop(current->pending_exception());

1227        case T_LONG:    return LongBoxCache::singleton(THREAD)->lookup_raw(value->get_intptr(), cache_init_error);
1228        default:;
1229      }
1230    }
1231    return nullptr;
1232 }
1233 #endif // INCLUDE_JVMCI
1234 
1235 #if COMPILER2_OR_JVMCI
1236 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, TRAPS) {
1237   Handle pending_exception(THREAD, thread->pending_exception());
1238   const char* exception_file = thread->exception_file();
1239   int exception_line = thread->exception_line();
1240   thread->clear_pending_exception();
1241 
1242   bool failures = false;
1243 
1244   for (int i = 0; i < objects->length(); i++) {
1245     assert(objects->at(i)->is_object(), "invalid debug information");
1246     ObjectValue* sv = (ObjectValue*) objects->at(i);

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

1248 
1249     // Check if the object may be null and has an additional is_init input that needs
1250     // to be checked before using the field values. Skip re-allocation if it is null.
1251     if (sv->maybe_null()) {
1252       assert(k->is_inline_klass(), "must be an inline klass");
1253       jint is_init = StackValue::create_stack_value(fr, reg_map, sv->is_init())->get_jint();
1254       if (is_init == 0) {
1255         continue;
1256       }
1257     }
1258 
1259     oop obj = nullptr;
1260     bool cache_init_error = false;
1261     if (k->is_instance_klass()) {
1262 #if INCLUDE_JVMCI
1263       CompiledMethod* cm = fr->cb()->as_compiled_method_or_null();
1264       if (cm->is_compiled_by_jvmci() && sv->is_auto_box()) {
1265         AutoBoxObjectValue* abv = (AutoBoxObjectValue*) sv;
1266         obj = get_cached_box(abv, fr, reg_map, cache_init_error, THREAD);
1267         if (obj != nullptr) {
1268           // Set the flag to indicate the box came from a cache, so that we can skip the field reassignment for it.
1269           abv->set_cached(true);
1270         } else if (cache_init_error) {
1271           // Results in an OOME which is valid (as opposed to a class initialization error)
1272           // and is fine for the rare case a cache initialization failing.
1273           failures = true;
1274         }
1275       }
1276 #endif // INCLUDE_JVMCI
1277 
1278       InstanceKlass* ik = InstanceKlass::cast(k);
1279       if (obj == nullptr && !cache_init_error) {
1280 #if COMPILER2_OR_JVMCI
1281         if (EnableVectorSupport && VectorSupport::is_vector(ik)) {
1282           obj = VectorSupport::allocate_vector(ik, fr, reg_map, sv, THREAD);
1283         } else {
1284           obj = ik->allocate_instance(THREAD);
1285         }
1286 #else
1287         obj = ik->allocate_instance(THREAD);
1288 #endif // COMPILER2_OR_JVMCI
1289       }
1290     } else if (k->is_flatArray_klass()) {
1291       FlatArrayKlass* ak = FlatArrayKlass::cast(k);
1292       // Inline type array must be zeroed because not all memory is reassigned
1293       obj = ak->allocate(sv->field_size(), THREAD);
1294     } else if (k->is_typeArray_klass()) {
1295       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1296       assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
1297       int len = sv->field_size() / type2size[ak->element_type()];
1298       obj = ak->allocate(len, THREAD);
1299     } else if (k->is_objArray_klass()) {
1300       ObjArrayKlass* ak = ObjArrayKlass::cast(k);
1301       obj = ak->allocate(sv->field_size(), THREAD);
1302     }
1303 
1304     if (obj == nullptr) {
1305       failures = true;
1306     }
1307 
1308     assert(sv->value().is_null(), "redundant reallocation");
1309     assert(obj != nullptr || HAS_PENDING_EXCEPTION || cache_init_error, "allocation should succeed or we should get an exception");
1310     CLEAR_PENDING_EXCEPTION;
1311     sv->set_value(obj);
1312   }
1313 
1314   if (failures) {
1315     THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
1316   } else if (pending_exception.not_null()) {
1317     thread->set_pending_exception(pending_exception(), exception_file, exception_line);
1318   }
1319 
1320   return failures;
1321 }
1322 
1323 // We're deoptimizing at the return of a call, inline type fields are
1324 // in registers. When we go back to the interpreter, it will expect a
1325 // reference to an inline type instance. Allocate and initialize it from
1326 // the register values here.
1327 bool Deoptimization::realloc_inline_type_result(InlineKlass* vk, const RegisterMap& map, GrowableArray<Handle>& return_oops, TRAPS) {
1328   oop new_vt = vk->realloc_result(map, return_oops, THREAD);
1329   if (new_vt == nullptr) {
1330     CLEAR_PENDING_EXCEPTION;
1331     THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), true);
1332   }
1333   return_oops.clear();
1334   return_oops.push(Handle(THREAD, new_vt));
1335   return false;
1336 }
1337 
1338 #if INCLUDE_JVMCI
1339 /**
1340  * For primitive types whose kind gets "erased" at runtime (shorts become stack ints),
1341  * we need to somehow be able to recover the actual kind to be able to write the correct
1342  * amount of bytes.
1343  * For that purpose, this method assumes that, for an entry spanning n bytes at index i,
1344  * the entries at index n + 1 to n + i are 'markers'.
1345  * For example, if we were writing a short at index 4 of a byte array of size 8, the
1346  * expected form of the array would be:
1347  *
1348  * {b0, b1, b2, b3, INT, marker, b6, b7}
1349  *
1350  * Thus, in order to get back the size of the entry, we simply need to count the number
1351  * of marked entries
1352  *
1353  * @param virtualArray the virtualized byte array
1354  * @param i index of the virtual entry we are recovering
1355  * @return The number of bytes the entry spans
1356  */
1357 static int count_number_of_bytes_for_entry(ObjectValue *virtualArray, int i) {

1483       default:
1484         ShouldNotReachHere();
1485     }
1486     index++;
1487   }
1488 }
1489 
1490 // restore fields of an eliminated object array
1491 void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) {
1492   for (int i = 0; i < sv->field_size(); i++) {
1493     StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i));
1494     assert(value->type() == T_OBJECT, "object element expected");
1495     obj->obj_at_put(i, value->get_obj()());
1496   }
1497 }
1498 
1499 class ReassignedField {
1500 public:
1501   int _offset;
1502   BasicType _type;
1503   InstanceKlass* _klass;
1504   bool _is_flat;
1505 public:
1506   ReassignedField() : _offset(0), _type(T_ILLEGAL), _klass(nullptr), _is_flat(false) { }



1507 };
1508 
1509 int compare(ReassignedField* left, ReassignedField* right) {
1510   return left->_offset - right->_offset;
1511 }
1512 
1513 // Restore fields of an eliminated instance object using the same field order
1514 // returned by HotSpotResolvedObjectTypeImpl.getInstanceFields(true)
1515 static int reassign_fields_by_klass(InstanceKlass* klass, frame* fr, RegisterMap* reg_map, ObjectValue* sv, int svIndex, oop obj, bool skip_internal, int base_offset, TRAPS) {
1516   GrowableArray<ReassignedField>* fields = new GrowableArray<ReassignedField>();
1517   InstanceKlass* ik = klass;
1518   while (ik != nullptr) {
1519     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1520       if (!fs.access_flags().is_static() && (!skip_internal || !fs.field_flags().is_injected())) {
1521         ReassignedField field;
1522         field._offset = fs.offset();
1523         field._type = Signature::basic_type(fs.signature());
1524         if (fs.is_null_free_inline_type()) {
1525           if (fs.is_flat()) {
1526             field._is_flat = true;
1527             // Resolve klass of flat inline type field
1528             field._klass = InlineKlass::cast(klass->get_inline_type_field_klass(fs.index()));
1529           } else {
1530             field._type = T_OBJECT;  // Can be removed once Q-descriptors have been removed.
1531           }
1532         }
1533         fields->append(field);
1534       }
1535     }
1536     ik = ik->superklass();
1537   }
1538   fields->sort(compare);
1539   for (int i = 0; i < fields->length(); i++) {
1540     BasicType type = fields->at(i)._type;
1541     int offset = base_offset + fields->at(i)._offset;
1542     // Check for flat inline type field before accessing the ScopeValue because it might not have any fields
1543     if (fields->at(i)._is_flat) {
1544       // Recursively re-assign flat inline type fields
1545       InstanceKlass* vk = fields->at(i)._klass;
1546       assert(vk != nullptr, "must be resolved");
1547       offset -= InlineKlass::cast(vk)->first_field_offset(); // Adjust offset to omit oop header
1548       svIndex = reassign_fields_by_klass(vk, fr, reg_map, sv, svIndex, obj, skip_internal, offset, CHECK_0);
1549       continue; // Continue because we don't need to increment svIndex
1550     }
1551     ScopeValue* scope_field = sv->field_at(svIndex);
1552     StackValue* value = StackValue::create_stack_value(fr, reg_map, scope_field);


1553     switch (type) {
1554       case T_OBJECT:
1555       case T_ARRAY:
1556         assert(value->type() == T_OBJECT, "Agreement.");
1557         obj->obj_field_put(offset, value->get_obj()());
1558         break;
1559 
1560       case T_INT: case T_FLOAT: { // 4 bytes.
1561         assert(value->type() == T_INT, "Agreement.");
1562         bool big_value = false;
1563         if (i+1 < fields->length() && fields->at(i+1)._type == T_INT) {
1564           if (scope_field->is_location()) {
1565             Location::Type type = ((LocationValue*) scope_field)->location().type();
1566             if (type == Location::dbl || type == Location::lng) {
1567               big_value = true;
1568             }
1569           }
1570           if (scope_field->is_constant_int()) {
1571             ScopeValue* next_scope_field = sv->field_at(svIndex + 1);
1572             if (next_scope_field->is_constant_long() || next_scope_field->is_constant_double()) {
1573               big_value = true;
1574             }
1575           }

1609         break;
1610 
1611       case T_BYTE:
1612         assert(value->type() == T_INT, "Agreement.");
1613         obj->byte_field_put(offset, (jbyte)value->get_jint());
1614         break;
1615 
1616       case T_BOOLEAN:
1617         assert(value->type() == T_INT, "Agreement.");
1618         obj->bool_field_put(offset, (jboolean)value->get_jint());
1619         break;
1620 
1621       default:
1622         ShouldNotReachHere();
1623     }
1624     svIndex++;
1625   }
1626   return svIndex;
1627 }
1628 
1629 // restore fields of an eliminated inline type array
1630 void Deoptimization::reassign_flat_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, flatArrayOop obj, FlatArrayKlass* vak, bool skip_internal, TRAPS) {
1631   InlineKlass* vk = vak->element_klass();
1632   assert(vk->flat_array(), "should only be used for flat inline type arrays");
1633   // Adjust offset to omit oop header
1634   int base_offset = arrayOopDesc::base_offset_in_bytes(T_PRIMITIVE_OBJECT) - InlineKlass::cast(vk)->first_field_offset();
1635   // Initialize all elements of the flat inline type array
1636   for (int i = 0; i < sv->field_size(); i++) {
1637     ScopeValue* val = sv->field_at(i);
1638     int offset = base_offset + (i << Klass::layout_helper_log2_element_size(vak->layout_helper()));
1639     reassign_fields_by_klass(vk, fr, reg_map, val->as_ObjectValue(), 0, (oop)obj, skip_internal, offset, CHECK);
1640   }
1641 }
1642 
1643 // restore fields of all eliminated objects and arrays
1644 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures, bool skip_internal, TRAPS) {
1645   for (int i = 0; i < objects->length(); i++) {
1646     assert(objects->at(i)->is_object(), "invalid debug information");
1647     ObjectValue* sv = (ObjectValue*) objects->at(i);
1648     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
1649     Handle obj = sv->value();
1650     assert(obj.not_null() || realloc_failures || sv->maybe_null(), "reallocation was missed");
1651 #ifndef PRODUCT
1652     if (PrintDeoptimizationDetails) {
1653       tty->print_cr("reassign fields for object of type %s!", k->name()->as_C_string());
1654     }
1655 #endif // !PRODUCT
1656 
1657     if (obj.is_null()) {
1658       continue;
1659     }
1660 
1661 #if INCLUDE_JVMCI
1662     // Don't reassign fields of boxes that came from a cache. Caches may be in CDS.
1663     if (sv->is_auto_box() && ((AutoBoxObjectValue*) sv)->is_cached()) {
1664       continue;
1665     }
1666 #endif // INCLUDE_JVMCI
1667 #if COMPILER2_OR_JVMCI
1668     if (EnableVectorSupport && VectorSupport::is_vector(k)) {
1669       assert(sv->field_size() == 1, "%s not a vector", k->name()->as_C_string());
1670       ScopeValue* payload = sv->field_at(0);
1671       if (payload->is_location() &&
1672           payload->as_LocationValue()->location().type() == Location::vector) {
1673 #ifndef PRODUCT
1674         if (PrintDeoptimizationDetails) {
1675           tty->print_cr("skip field reassignment for this vector - it should be assigned already");
1676           if (Verbose) {
1677             Handle obj = sv->value();
1678             k->oop_print_on(obj(), tty);
1679           }
1680         }
1681 #endif // !PRODUCT
1682         continue; // Such vector's value was already restored in VectorSupport::allocate_vector().
1683       }
1684       // Else fall-through to do assignment for scalar-replaced boxed vector representation
1685       // which could be restored after vector object allocation.
1686     }
1687 #endif /* !COMPILER2_OR_JVMCI */
1688     if (k->is_instance_klass()) {
1689       InstanceKlass* ik = InstanceKlass::cast(k);
1690       reassign_fields_by_klass(ik, fr, reg_map, sv, 0, obj(), skip_internal, 0, CHECK);
1691     } else if (k->is_flatArray_klass()) {
1692       FlatArrayKlass* vak = FlatArrayKlass::cast(k);
1693       reassign_flat_array_elements(fr, reg_map, sv, (flatArrayOop) obj(), vak, skip_internal, CHECK);
1694     } else if (k->is_typeArray_klass()) {
1695       TypeArrayKlass* ak = TypeArrayKlass::cast(k);
1696       reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type());
1697     } else if (k->is_objArray_klass()) {
1698       reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj());
1699     }
1700   }
1701   // These objects may escape when we return to Interpreter after deoptimization.
1702   // We need barrier so that stores that initialize these objects can't be reordered
1703   // with subsequent stores that make these objects accessible by other threads.
1704   OrderAccess::storestore();
1705 }
1706 
1707 
1708 // relock objects for which synchronization was eliminated
1709 bool Deoptimization::relock_objects(JavaThread* thread, GrowableArray<MonitorInfo*>* monitors,
1710                                     JavaThread* deoptee_thread, frame& fr, int exec_mode, bool realloc_failures) {
1711   bool relocked_objects = false;
1712   for (int i = 0; i < monitors->length(); i++) {
1713     MonitorInfo* mon_info = monitors->at(i);

1848     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1849     cm->log_identity(xtty);
1850     xtty->end_head();
1851     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1852       xtty->begin_elem("jvms bci='%d'", sd->bci());
1853       xtty->method(sd->method());
1854       xtty->end_elem();
1855       if (sd->is_top())  break;
1856     }
1857     xtty->tail("deoptimized");
1858   }
1859 
1860   Continuation::notify_deopt(thread, fr.sp());
1861 
1862   // Patch the compiled method so that when execution returns to it we will
1863   // deopt the execution state and return to the interpreter.
1864   fr.deoptimize(thread);
1865 }
1866 
1867 void Deoptimization::deoptimize(JavaThread* thread, frame fr, DeoptReason reason) {
1868   // Deoptimize only if the frame comes from compiled code.
1869   // Do not deoptimize the frame which is already patched
1870   // during the execution of the loops below.
1871   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1872     return;
1873   }
1874   ResourceMark rm;
1875   deoptimize_single_frame(thread, fr, reason);
1876 }
1877 
1878 #if INCLUDE_JVMCI
1879 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1880   // there is no exception handler for this pc => deoptimize
1881   cm->make_not_entrant();
1882 
1883   // Use Deoptimization::deoptimize for all of its side-effects:
1884   // gathering traps statistics, logging...
1885   // it also patches the return pc but we do not care about that
1886   // since we return a continuation to the deopt_blob below.
1887   JavaThread* thread = JavaThread::current();
1888   RegisterMap reg_map(thread,
< prev index next >