src/hotspot/share/runtime/deoptimization.cpp
changeset 58226 408c445d04e8
parent 57893 49fea19f0726
child 58545 725244418646
equal deleted inserted replaced
58225:4eebb9aadbe3 58226:408c445d04e8
   155   thread->inc_in_deopt_handler();
   155   thread->inc_in_deopt_handler();
   156 
   156 
   157   return fetch_unroll_info_helper(thread, exec_mode);
   157   return fetch_unroll_info_helper(thread, exec_mode);
   158 JRT_END
   158 JRT_END
   159 
   159 
       
   160 #if COMPILER2_OR_JVMCI
       
   161 static bool eliminate_allocations(JavaThread* thread, int exec_mode, CompiledMethod* compiled_method,
       
   162                                   frame& deoptee, RegisterMap& map, GrowableArray<compiledVFrame*>* chunk) {
       
   163   bool realloc_failures = false;
       
   164   assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames");
       
   165 
       
   166   GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects();
       
   167 
       
   168   // The flag return_oop() indicates call sites which return oop
       
   169   // in compiled code. Such sites include java method calls,
       
   170   // runtime calls (for example, used to allocate new objects/arrays
       
   171   // on slow code path) and any other calls generated in compiled code.
       
   172   // It is not guaranteed that we can get such information here only
       
   173   // by analyzing bytecode in deoptimized frames. This is why this flag
       
   174   // is set during method compilation (see Compile::Process_OopMap_Node()).
       
   175   // If the previous frame was popped or if we are dispatching an exception,
       
   176   // we don't have an oop result.
       
   177   bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution() && (exec_mode == Deoptimization::Unpack_deopt);
       
   178   Handle return_value;
       
   179   if (save_oop_result) {
       
   180     // Reallocation may trigger GC. If deoptimization happened on return from
       
   181     // call which returns oop we need to save it since it is not in oopmap.
       
   182     oop result = deoptee.saved_oop_result(&map);
       
   183     assert(oopDesc::is_oop_or_null(result), "must be oop");
       
   184     return_value = Handle(thread, result);
       
   185     assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
       
   186     if (TraceDeoptimization) {
       
   187       ttyLocker ttyl;
       
   188       tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
       
   189     }
       
   190   }
       
   191   if (objects != NULL) {
       
   192     JRT_BLOCK
       
   193       realloc_failures = Deoptimization::realloc_objects(thread, &deoptee, &map, objects, THREAD);
       
   194     JRT_END
       
   195     bool skip_internal = (compiled_method != NULL) && !compiled_method->is_compiled_by_jvmci();
       
   196     Deoptimization::reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal);
       
   197 #ifndef PRODUCT
       
   198     if (TraceDeoptimization) {
       
   199       ttyLocker ttyl;
       
   200       tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
       
   201       Deoptimization::print_objects(objects, realloc_failures);
       
   202     }
       
   203 #endif
       
   204   }
       
   205   if (save_oop_result) {
       
   206     // Restore result.
       
   207     deoptee.set_saved_oop_result(&map, return_value());
       
   208   }
       
   209   return realloc_failures;
       
   210 }
       
   211 
       
   212 static void eliminate_locks(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
       
   213 #ifndef PRODUCT
       
   214   bool first = true;
       
   215 #endif
       
   216   for (int i = 0; i < chunk->length(); i++) {
       
   217     compiledVFrame* cvf = chunk->at(i);
       
   218     assert (cvf->scope() != NULL,"expect only compiled java frames");
       
   219     GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
       
   220     if (monitors->is_nonempty()) {
       
   221       Deoptimization::relock_objects(monitors, thread, realloc_failures);
       
   222 #ifndef PRODUCT
       
   223       if (PrintDeoptimizationDetails) {
       
   224         ttyLocker ttyl;
       
   225         for (int j = 0; j < monitors->length(); j++) {
       
   226           MonitorInfo* mi = monitors->at(j);
       
   227           if (mi->eliminated()) {
       
   228             if (first) {
       
   229               first = false;
       
   230               tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
       
   231             }
       
   232             if (mi->owner_is_scalar_replaced()) {
       
   233               Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
       
   234               tty->print_cr("     failed reallocation for klass %s", k->external_name());
       
   235             } else {
       
   236               tty->print_cr("     object <" INTPTR_FORMAT "> locked", p2i(mi->owner()));
       
   237             }
       
   238           }
       
   239         }
       
   240       }
       
   241 #endif // !PRODUCT
       
   242     }
       
   243   }
       
   244 }
       
   245 #endif // COMPILER2_OR_JVMCI
   160 
   246 
   161 // This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)
   247 // This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap)
   162 Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread, int exec_mode) {
   248 Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread, int exec_mode) {
   163 
   249 
   164   // Note: there is a safepoint safety issue here. No matter whether we enter
   250   // Note: there is a safepoint safety issue here. No matter whether we enter
   199   chunk->push(compiledVFrame::cast(vf));
   285   chunk->push(compiledVFrame::cast(vf));
   200 
   286 
   201   bool realloc_failures = false;
   287   bool realloc_failures = false;
   202 
   288 
   203 #if COMPILER2_OR_JVMCI
   289 #if COMPILER2_OR_JVMCI
       
   290 #if INCLUDE_JVMCI
       
   291   bool jvmci_enabled = true;
       
   292 #else
       
   293   bool jvmci_enabled = false;
       
   294 #endif
       
   295 
   204   // Reallocate the non-escaping objects and restore their fields. Then
   296   // Reallocate the non-escaping objects and restore their fields. Then
   205   // relock objects if synchronization on them was eliminated.
   297   // relock objects if synchronization on them was eliminated.
   206 #if !INCLUDE_JVMCI
   298   if (jvmci_enabled || ((DoEscapeAnalysis || EliminateNestedLocks) && EliminateAllocations)) {
   207   if (DoEscapeAnalysis || EliminateNestedLocks) {
   299     realloc_failures = eliminate_allocations(thread, exec_mode, cm, deoptee, map, chunk);
   208     if (EliminateAllocations) {
   300   }
   209 #endif // INCLUDE_JVMCI
   301 #endif // COMPILER2_OR_JVMCI
   210       assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames");
   302 
   211       GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects();
   303   // Revoke biases, done with in java state.
   212 
   304   // No safepoints allowed after this
   213       // The flag return_oop() indicates call sites which return oop
   305   revoke_from_deopt_handler(thread, deoptee, &map);
   214       // in compiled code. Such sites include java method calls,
   306 
   215       // runtime calls (for example, used to allocate new objects/arrays
   307   // Ensure that no safepoint is taken after pointers have been stored
   216       // on slow code path) and any other calls generated in compiled code.
   308   // in fields of rematerialized objects.  If a safepoint occurs from here on
   217       // It is not guaranteed that we can get such information here only
   309   // out the java state residing in the vframeArray will be missed.
   218       // by analyzing bytecode in deoptimized frames. This is why this flag
   310   // Locks may be rebaised in a safepoint.
   219       // is set during method compilation (see Compile::Process_OopMap_Node()).
   311   NoSafepointVerifier no_safepoint;
   220       // If the previous frame was popped or if we are dispatching an exception,
   312 
   221       // we don't have an oop result.
   313 #if COMPILER2_OR_JVMCI
   222       bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution() && (exec_mode == Unpack_deopt);
   314   if (jvmci_enabled || ((DoEscapeAnalysis || EliminateNestedLocks) && EliminateLocks)) {
   223       Handle return_value;
   315     eliminate_locks(thread, chunk, realloc_failures);
   224       if (save_oop_result) {
   316   }
   225         // Reallocation may trigger GC. If deoptimization happened on return from
       
   226         // call which returns oop we need to save it since it is not in oopmap.
       
   227         oop result = deoptee.saved_oop_result(&map);
       
   228         assert(oopDesc::is_oop_or_null(result), "must be oop");
       
   229         return_value = Handle(thread, result);
       
   230         assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
       
   231         if (TraceDeoptimization) {
       
   232           ttyLocker ttyl;
       
   233           tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
       
   234         }
       
   235       }
       
   236       if (objects != NULL) {
       
   237         JRT_BLOCK
       
   238           realloc_failures = realloc_objects(thread, &deoptee, &map, objects, THREAD);
       
   239         JRT_END
       
   240         bool skip_internal = (cm != NULL) && !cm->is_compiled_by_jvmci();
       
   241         reassign_fields(&deoptee, &map, objects, realloc_failures, skip_internal);
       
   242 #ifndef PRODUCT
       
   243         if (TraceDeoptimization) {
       
   244           ttyLocker ttyl;
       
   245           tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
       
   246           print_objects(objects, realloc_failures);
       
   247         }
       
   248 #endif
       
   249       }
       
   250       if (save_oop_result) {
       
   251         // Restore result.
       
   252         deoptee.set_saved_oop_result(&map, return_value());
       
   253       }
       
   254 #if !INCLUDE_JVMCI
       
   255     }
       
   256     if (EliminateLocks) {
       
   257 #endif // INCLUDE_JVMCI
       
   258 #ifndef PRODUCT
       
   259       bool first = true;
       
   260 #endif
       
   261       for (int i = 0; i < chunk->length(); i++) {
       
   262         compiledVFrame* cvf = chunk->at(i);
       
   263         assert (cvf->scope() != NULL,"expect only compiled java frames");
       
   264         GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
       
   265         if (monitors->is_nonempty()) {
       
   266           relock_objects(monitors, thread, realloc_failures);
       
   267 #ifndef PRODUCT
       
   268           if (PrintDeoptimizationDetails) {
       
   269             ttyLocker ttyl;
       
   270             for (int j = 0; j < monitors->length(); j++) {
       
   271               MonitorInfo* mi = monitors->at(j);
       
   272               if (mi->eliminated()) {
       
   273                 if (first) {
       
   274                   first = false;
       
   275                   tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
       
   276                 }
       
   277                 if (mi->owner_is_scalar_replaced()) {
       
   278                   Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
       
   279                   tty->print_cr("     failed reallocation for klass %s", k->external_name());
       
   280                 } else {
       
   281                   tty->print_cr("     object <" INTPTR_FORMAT "> locked", p2i(mi->owner()));
       
   282                 }
       
   283               }
       
   284             }
       
   285           }
       
   286 #endif // !PRODUCT
       
   287         }
       
   288       }
       
   289 #if !INCLUDE_JVMCI
       
   290     }
       
   291   }
       
   292 #endif // INCLUDE_JVMCI
       
   293 #endif // COMPILER2_OR_JVMCI
   317 #endif // COMPILER2_OR_JVMCI
   294 
   318 
   295   ScopeDesc* trap_scope = chunk->at(0)->scope();
   319   ScopeDesc* trap_scope = chunk->at(0)->scope();
   296   Handle exceptionObject;
   320   Handle exceptionObject;
   297   if (trap_scope->rethrow_exception()) {
   321   if (trap_scope->rethrow_exception()) {
   302     guarantee(expressions != NULL && expressions->length() > 0, "must have exception to throw");
   326     guarantee(expressions != NULL && expressions->length() > 0, "must have exception to throw");
   303     ScopeValue* topOfStack = expressions->top();
   327     ScopeValue* topOfStack = expressions->top();
   304     exceptionObject = StackValue::create_stack_value(&deoptee, &map, topOfStack)->get_obj();
   328     exceptionObject = StackValue::create_stack_value(&deoptee, &map, topOfStack)->get_obj();
   305     guarantee(exceptionObject() != NULL, "exception oop can not be null");
   329     guarantee(exceptionObject() != NULL, "exception oop can not be null");
   306   }
   330   }
   307 
       
   308   // Ensure that no safepoint is taken after pointers have been stored
       
   309   // in fields of rematerialized objects.  If a safepoint occurs from here on
       
   310   // out the java state residing in the vframeArray will be missed.
       
   311   NoSafepointVerifier no_safepoint;
       
   312 
   331 
   313   vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
   332   vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
   314 #if COMPILER2_OR_JVMCI
   333 #if COMPILER2_OR_JVMCI
   315   if (realloc_failures) {
   334   if (realloc_failures) {
   316     pop_frames_failed_reallocs(thread, array);
   335     pop_frames_failed_reallocs(thread, array);
   777 
   796 
   778 
   797 
   779   return bt;
   798   return bt;
   780 JRT_END
   799 JRT_END
   781 
   800 
   782 
   801 class DeoptimizeMarkedTC : public ThreadClosure {
   783 int Deoptimization::deoptimize_dependents() {
   802  public:
   784   Threads::deoptimized_wrt_marked_nmethods();
   803   virtual void do_thread(Thread* thread) {
   785   return 0;
   804     assert(thread->is_Java_thread(), "must be");
       
   805     JavaThread* jt = (JavaThread*)thread;
       
   806     jt->deoptimize_marked_methods();
       
   807   }
       
   808 };
       
   809 
       
   810 void Deoptimization::deoptimize_all_marked() {
       
   811   ResourceMark rm;
       
   812   DeoptimizationMarker dm;
       
   813 
       
   814   if (SafepointSynchronize::is_at_safepoint()) {
       
   815     DeoptimizeMarkedTC deopt;
       
   816     // Make the dependent methods not entrant
       
   817     CodeCache::make_marked_nmethods_not_entrant();
       
   818     Threads::java_threads_do(&deopt);
       
   819   } else {
       
   820     // Make the dependent methods not entrant
       
   821     {
       
   822       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
       
   823       CodeCache::make_marked_nmethods_not_entrant();
       
   824     }
       
   825     DeoptimizeMarkedTC deopt;
       
   826     Handshake::execute(&deopt);
       
   827   }
   786 }
   828 }
   787 
   829 
   788 Deoptimization::DeoptAction Deoptimization::_unloaded_action
   830 Deoptimization::DeoptAction Deoptimization::_unloaded_action
   789   = Deoptimization::Action_reinterpret;
   831   = Deoptimization::Action_reinterpret;
   790 
   832 
  1395       objects_to_revoke->append(Handle(thread, mon_info->owner()));
  1437       objects_to_revoke->append(Handle(thread, mon_info->owner()));
  1396     }
  1438     }
  1397   }
  1439   }
  1398 }
  1440 }
  1399 
  1441 
  1400 
  1442 static void get_monitors_from_stack(GrowableArray<Handle>* objects_to_revoke, JavaThread* thread, frame fr, RegisterMap* map) {
  1401 void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
       
  1402   if (!UseBiasedLocking) {
       
  1403     return;
       
  1404   }
       
  1405 
       
  1406   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
       
  1407 
       
  1408   // Unfortunately we don't have a RegisterMap available in most of
  1443   // Unfortunately we don't have a RegisterMap available in most of
  1409   // the places we want to call this routine so we need to walk the
  1444   // the places we want to call this routine so we need to walk the
  1410   // stack again to update the register map.
  1445   // stack again to update the register map.
  1411   if (map == NULL || !map->update_map()) {
  1446   if (map == NULL || !map->update_map()) {
  1412     StackFrameStream sfs(thread, true);
  1447     StackFrameStream sfs(thread, true);
  1426   while (!cvf->is_top()) {
  1461   while (!cvf->is_top()) {
  1427     collect_monitors(cvf, objects_to_revoke);
  1462     collect_monitors(cvf, objects_to_revoke);
  1428     cvf = compiledVFrame::cast(cvf->sender());
  1463     cvf = compiledVFrame::cast(cvf->sender());
  1429   }
  1464   }
  1430   collect_monitors(cvf, objects_to_revoke);
  1465   collect_monitors(cvf, objects_to_revoke);
  1431 
  1466 }
  1432   if (SafepointSynchronize::is_at_safepoint()) {
  1467 
  1433     BiasedLocking::revoke_at_safepoint(objects_to_revoke);
  1468 void Deoptimization::revoke_from_deopt_handler(JavaThread* thread, frame fr, RegisterMap* map) {
  1434   } else {
  1469   if (!UseBiasedLocking) {
  1435     BiasedLocking::revoke(objects_to_revoke, thread);
  1470     return;
       
  1471   }
       
  1472   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
       
  1473   get_monitors_from_stack(objects_to_revoke, thread, fr, map);
       
  1474 
       
  1475   int len = objects_to_revoke->length();
       
  1476   for (int i = 0; i < len; i++) {
       
  1477     oop obj = (objects_to_revoke->at(i))();
       
  1478     BiasedLocking::revoke_own_lock(objects_to_revoke->at(i), thread);
       
  1479     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
  1436   }
  1480   }
  1437 }
  1481 }
  1438 
  1482 
  1439 
  1483 
  1440 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
  1484 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
  1462   // Patch the compiled method so that when execution returns to it we will
  1506   // Patch the compiled method so that when execution returns to it we will
  1463   // deopt the execution state and return to the interpreter.
  1507   // deopt the execution state and return to the interpreter.
  1464   fr.deoptimize(thread);
  1508   fr.deoptimize(thread);
  1465 }
  1509 }
  1466 
  1510 
  1467 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) {
       
  1468   deoptimize(thread, fr, map, Reason_constraint);
       
  1469 }
       
  1470 
       
  1471 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {
  1511 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {
  1472   // Deoptimize only if the frame comes from compile code.
  1512   // Deoptimize only if the frame comes from compile code.
  1473   // Do not deoptimize the frame which is already patched
  1513   // Do not deoptimize the frame which is already patched
  1474   // during the execution of the loops below.
  1514   // during the execution of the loops below.
  1475   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
  1515   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
  1476     return;
  1516     return;
  1477   }
  1517   }
  1478   ResourceMark rm;
  1518   ResourceMark rm;
  1479   DeoptimizationMarker dm;
  1519   DeoptimizationMarker dm;
  1480   if (UseBiasedLocking) {
       
  1481     revoke_biases_of_monitors(thread, fr, map);
       
  1482   }
       
  1483   deoptimize_single_frame(thread, fr, reason);
  1520   deoptimize_single_frame(thread, fr, reason);
  1484 
       
  1485 }
  1521 }
  1486 
  1522 
  1487 #if INCLUDE_JVMCI
  1523 #if INCLUDE_JVMCI
  1488 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
  1524 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
  1489   // there is no exception handler for this pc => deoptimize
  1525   // there is no exception handler for this pc => deoptimize
  1639   Events::log_deopt_message(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
  1675   Events::log_deopt_message(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
  1640               trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
  1676               trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
  1641 
  1677 
  1642   {
  1678   {
  1643     ResourceMark rm;
  1679     ResourceMark rm;
  1644 
       
  1645     // Revoke biases of any monitors in the frame to ensure we can migrate them
       
  1646     revoke_biases_of_monitors(thread, fr, &reg_map);
       
  1647 
  1680 
  1648     DeoptReason reason = trap_request_reason(trap_request);
  1681     DeoptReason reason = trap_request_reason(trap_request);
  1649     DeoptAction action = trap_request_action(trap_request);
  1682     DeoptAction action = trap_request_action(trap_request);
  1650 #if INCLUDE_JVMCI
  1683 #if INCLUDE_JVMCI
  1651     int debug_id = trap_request_debug_id(trap_request);
  1684     int debug_id = trap_request_debug_id(trap_request);