762 vframeArrayElement* el = cur_array->element(k);
763 tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
764 }
765 cur_array->print_on_2(tty);
766 } // release tty lock before calling guarantee
767 guarantee(false, "wrong number of expression stack elements during deopt");
768 }
769 VerifyOopClosure verify;
770 iframe->oops_interpreted_do(&verify, &rm, false);
771 callee_size_of_parameters = mh->size_of_parameters();
772 callee_max_locals = mh->max_locals();
773 is_top_frame = false;
774 }
775 }
776 #endif /* !PRODUCT */
777
778
779 return bt;
780 JRT_END
781
782
783 int Deoptimization::deoptimize_dependents() {
784 Threads::deoptimized_wrt_marked_nmethods();
785 return 0;
786 }
787
788 Deoptimization::DeoptAction Deoptimization::_unloaded_action
789 = Deoptimization::Action_reinterpret;
790
791 #if COMPILER2_OR_JVMCI
792 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {
793 Handle pending_exception(THREAD, thread->pending_exception());
794 const char* exception_file = thread->exception_file();
795 int exception_line = thread->exception_line();
796 thread->clear_pending_exception();
797
798 bool failures = false;
799
800 for (int i = 0; i < objects->length(); i++) {
801 assert(objects->at(i)->is_object(), "invalid debug information");
802 ObjectValue* sv = (ObjectValue*) objects->at(i);
803
804 Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
805 oop obj = NULL;
1229 array->element(i)->free_monitors(thread);
1230 #ifdef ASSERT
1231 array->element(i)->set_removed_monitors();
1232 #endif
1233 }
1234 }
1235 }
1236 #endif
1237
1238 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
1239 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1240 Thread* thread = Thread::current();
1241 for (int i = 0; i < monitors->length(); i++) {
1242 MonitorInfo* mon_info = monitors->at(i);
1243 if (!mon_info->eliminated() && mon_info->owner() != NULL) {
1244 objects_to_revoke->append(Handle(thread, mon_info->owner()));
1245 }
1246 }
1247 }
1248
1249
1250 void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
1251 if (!UseBiasedLocking) {
1252 return;
1253 }
1254
1255 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1256
1257 // Unfortunately we don't have a RegisterMap available in most of
1258 // the places we want to call this routine so we need to walk the
1259 // stack again to update the register map.
1260 if (map == NULL || !map->update_map()) {
1261 StackFrameStream sfs(thread, true);
1262 bool found = false;
1263 while (!found && !sfs.is_done()) {
1264 frame* cur = sfs.current();
1265 sfs.next();
1266 found = cur->id() == fr.id();
1267 }
1268 assert(found, "frame to be deoptimized not found on target thread's stack");
1269 map = sfs.register_map();
1270 }
1271
1272 vframe* vf = vframe::new_vframe(&fr, map, thread);
1273 compiledVFrame* cvf = compiledVFrame::cast(vf);
1274 // Revoke monitors' biases in all scopes
1275 while (!cvf->is_top()) {
1276 collect_monitors(cvf, objects_to_revoke);
1277 cvf = compiledVFrame::cast(cvf->sender());
1278 }
1279 collect_monitors(cvf, objects_to_revoke);
1280
1281 if (SafepointSynchronize::is_at_safepoint()) {
1282 BiasedLocking::revoke_at_safepoint(objects_to_revoke);
1283 } else {
1284 BiasedLocking::revoke(objects_to_revoke);
1285 }
1286 }
1287
1288
1289 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1290 assert(fr.can_be_deoptimized(), "checking frame type");
1291
1292 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1293
1294 if (LogCompilation && xtty != NULL) {
1295 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1296 assert(cm != NULL, "only compiled methods can deopt");
1297
1298 ttyLocker ttyl;
1299 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1300 cm->log_identity(xtty);
1301 xtty->end_head();
1302 for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1303 xtty->begin_elem("jvms bci='%d'", sd->bci());
1304 xtty->method(sd->method());
1305 xtty->end_elem();
1306 if (sd->is_top()) break;
1307 }
1308 xtty->tail("deoptimized");
1309 }
1310
1311 // Patch the compiled method so that when execution returns to it we will
1312 // deopt the execution state and return to the interpreter.
1313 fr.deoptimize(thread);
1314 }
1315
1316 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) {
1317 deoptimize(thread, fr, map, Reason_constraint);
1318 }
1319
1320 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {
1321 // Deoptimize only if the frame comes from compile code.
1322 // Do not deoptimize the frame which is already patched
1323 // during the execution of the loops below.
1324 if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1325 return;
1326 }
1327 ResourceMark rm;
1328 DeoptimizationMarker dm;
1329 if (UseBiasedLocking) {
1330 revoke_biases_of_monitors(thread, fr, map);
1331 }
1332 deoptimize_single_frame(thread, fr, reason);
1333
1334 }
1335
1336 #if INCLUDE_JVMCI
1337 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1338 // there is no exception handler for this pc => deoptimize
1339 cm->make_not_entrant();
1340
1341 // Use Deoptimization::deoptimize for all of its side-effects:
1342 // revoking biases of monitors, gathering traps statistics, logging...
1343 // it also patches the return pc but we do not care about that
1344 // since we return a continuation to the deopt_blob below.
1345 JavaThread* thread = JavaThread::current();
1346 RegisterMap reg_map(thread, UseBiasedLocking);
1347 frame runtime_frame = thread->last_frame();
1348 frame caller_frame = runtime_frame.sender(®_map);
1349 assert(caller_frame.cb()->as_compiled_method_or_null() == cm, "expect top frame compiled method");
1350 Deoptimization::deoptimize(thread, caller_frame, ®_map, Deoptimization::Reason_not_compiled_exception_handler);
1475 #if INCLUDE_JVMCI
1476 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
1477 RegisterMap reg_map(thread, true);
1478 #else
1479 RegisterMap reg_map(thread, UseBiasedLocking);
1480 #endif
1481 frame stub_frame = thread->last_frame();
1482 frame fr = stub_frame.sender(®_map);
1483 // Make sure the calling nmethod is not getting deoptimized and removed
1484 // before we are done with it.
1485 nmethodLocker nl(fr.pc());
1486
1487 // Log a message
1488 Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
1489 trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
1490
1491 {
1492 ResourceMark rm;
1493
1494 // Revoke biases of any monitors in the frame to ensure we can migrate them
1495 revoke_biases_of_monitors(thread, fr, ®_map);
1496
1497 DeoptReason reason = trap_request_reason(trap_request);
1498 DeoptAction action = trap_request_action(trap_request);
1499 #if INCLUDE_JVMCI
1500 int debug_id = trap_request_debug_id(trap_request);
1501 #endif
1502 jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1503
1504 vframe* vf = vframe::new_vframe(&fr, ®_map, thread);
1505 compiledVFrame* cvf = compiledVFrame::cast(vf);
1506
1507 CompiledMethod* nm = cvf->code();
1508
1509 ScopeDesc* trap_scope = cvf->scope();
1510
1511 if (TraceDeoptimization) {
1512 ttyLocker ttyl;
1513 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()
1514 #if INCLUDE_JVMCI
1515 , debug_id
|
762 vframeArrayElement* el = cur_array->element(k);
763 tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
764 }
765 cur_array->print_on_2(tty);
766 } // release tty lock before calling guarantee
767 guarantee(false, "wrong number of expression stack elements during deopt");
768 }
769 VerifyOopClosure verify;
770 iframe->oops_interpreted_do(&verify, &rm, false);
771 callee_size_of_parameters = mh->size_of_parameters();
772 callee_max_locals = mh->max_locals();
773 is_top_frame = false;
774 }
775 }
776 #endif /* !PRODUCT */
777
778
779 return bt;
780 JRT_END
781
782 class DeoptimizeMarkedTC : public ThreadClosure {
783 bool _in_handshake;
784 public:
785 DeoptimizeMarkedTC(bool in_handshake) : _in_handshake(in_handshake) {}
786 virtual void do_thread(Thread* thread) {
787 assert(thread->is_Java_thread(), "must be");
788 JavaThread* jt = (JavaThread*)thread;
789 jt->deoptimize_marked_methods(_in_handshake);
790 }
791 };
792
793 void Deoptimization::deoptimize_all_marked() {
794 ResourceMark rm;
795 DeoptimizationMarker dm;
796
797 if (SafepointSynchronize::is_at_safepoint()) {
798 DeoptimizeMarkedTC deopt(false);
799 // Make the dependent methods not entrant
800 CodeCache::make_marked_nmethods_not_entrant();
801 Threads::java_threads_do(&deopt);
802 } else {
803 // Make the dependent methods not entrant
804 {
805 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
806 CodeCache::make_marked_nmethods_not_entrant();
807 }
808 DeoptimizeMarkedTC deopt(true);
809 Handshake::execute(&deopt);
810 }
811 }
812
813 Deoptimization::DeoptAction Deoptimization::_unloaded_action
814 = Deoptimization::Action_reinterpret;
815
816 #if COMPILER2_OR_JVMCI
817 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {
818 Handle pending_exception(THREAD, thread->pending_exception());
819 const char* exception_file = thread->exception_file();
820 int exception_line = thread->exception_line();
821 thread->clear_pending_exception();
822
823 bool failures = false;
824
825 for (int i = 0; i < objects->length(); i++) {
826 assert(objects->at(i)->is_object(), "invalid debug information");
827 ObjectValue* sv = (ObjectValue*) objects->at(i);
828
829 Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
830 oop obj = NULL;
1254 array->element(i)->free_monitors(thread);
1255 #ifdef ASSERT
1256 array->element(i)->set_removed_monitors();
1257 #endif
1258 }
1259 }
1260 }
1261 #endif
1262
1263 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
1264 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1265 Thread* thread = Thread::current();
1266 for (int i = 0; i < monitors->length(); i++) {
1267 MonitorInfo* mon_info = monitors->at(i);
1268 if (!mon_info->eliminated() && mon_info->owner() != NULL) {
1269 objects_to_revoke->append(Handle(thread, mon_info->owner()));
1270 }
1271 }
1272 }
1273
1274 static void get_monitors_from_stack(GrowableArray<Handle>* objects_to_revoke, JavaThread* thread, frame fr, RegisterMap* map) {
1275 // Unfortunately we don't have a RegisterMap available in most of
1276 // the places we want to call this routine so we need to walk the
1277 // stack again to update the register map.
1278 if (map == NULL || !map->update_map()) {
1279 StackFrameStream sfs(thread, true);
1280 bool found = false;
1281 while (!found && !sfs.is_done()) {
1282 frame* cur = sfs.current();
1283 sfs.next();
1284 found = cur->id() == fr.id();
1285 }
1286 assert(found, "frame to be deoptimized not found on target thread's stack");
1287 map = sfs.register_map();
1288 }
1289
1290 vframe* vf = vframe::new_vframe(&fr, map, thread);
1291 compiledVFrame* cvf = compiledVFrame::cast(vf);
1292 // Revoke monitors' biases in all scopes
1293 while (!cvf->is_top()) {
1294 collect_monitors(cvf, objects_to_revoke);
1295 cvf = compiledVFrame::cast(cvf->sender());
1296 }
1297 collect_monitors(cvf, objects_to_revoke);
1298 }
1299
1300 void Deoptimization::inflate_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
1301 if (!UseBiasedLocking) {
1302 return;
1303 }
1304 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1305 get_monitors_from_stack(objects_to_revoke, thread, fr, map);
1306
1307 if (SafepointSynchronize::is_at_safepoint()) {
1308 BiasedLocking::revoke_at_safepoint(objects_to_revoke);
1309 } else {
1310 BiasedLocking::revoke(objects_to_revoke);
1311 }
1312 }
1313
1314 void Deoptimization::inflate_monitors_handshake(JavaThread* thread, frame fr, RegisterMap* map) {
1315 if (!UseBiasedLocking) {
1316 return;
1317 }
1318 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1319 get_monitors_from_stack(objects_to_revoke, thread, fr, map);
1320
1321 int len = objects_to_revoke->length();
1322 for (int i = 0; i < len; i++) {
1323 oop obj = (objects_to_revoke->at(i))();
1324 markOop mark = obj->mark();
1325 assert(!mark->has_bias_pattern() || mark->biased_locker() == thread, "Can't revoke");
1326 BiasedLocking::revoke_and_rebias_in_handshake(objects_to_revoke->at(i), thread);
1327 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
1328 ObjectSynchronizer::inflate(thread, obj, ObjectSynchronizer::inflate_cause_vm_internal);
1329 }
1330 }
1331
1332
1333 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1334 assert(fr.can_be_deoptimized(), "checking frame type");
1335
1336 gather_statistics(reason, Action_none, Bytecodes::_illegal);
1337
1338 if (LogCompilation && xtty != NULL) {
1339 CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1340 assert(cm != NULL, "only compiled methods can deopt");
1341
1342 ttyLocker ttyl;
1343 xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1344 cm->log_identity(xtty);
1345 xtty->end_head();
1346 for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1347 xtty->begin_elem("jvms bci='%d'", sd->bci());
1348 xtty->method(sd->method());
1349 xtty->end_elem();
1350 if (sd->is_top()) break;
1351 }
1352 xtty->tail("deoptimized");
1353 }
1354
1355 // Patch the compiled method so that when execution returns to it we will
1356 // deopt the execution state and return to the interpreter.
1357 fr.deoptimize(thread);
1358 }
1359
1360 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, bool in_handshake) {
1361 deopt_thread(in_handshake, thread, fr, map, Reason_constraint);
1362 }
1363
1364 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {
1365 deopt_thread(false, thread, fr, map, reason);
1366 }
1367
1368 void Deoptimization::deopt_thread(bool in_handshake, JavaThread* thread,
1369 frame fr, RegisterMap *map, DeoptReason reason) {
1370 // Deoptimize only if the frame comes from compile code.
1371 // Do not deoptimize the frame which is already patched
1372 // during the execution of the loops below.
1373 if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1374 return;
1375 }
1376 ResourceMark rm;
1377 DeoptimizationMarker dm;
1378 if (UseBiasedLocking) {
1379 if (in_handshake) {
1380 inflate_monitors_handshake(thread, fr, map);
1381 } else {
1382 inflate_monitors(thread, fr, map);
1383 }
1384 }
1385 deoptimize_single_frame(thread, fr, reason);
1386
1387 }
1388
1389 #if INCLUDE_JVMCI
1390 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1391 // there is no exception handler for this pc => deoptimize
1392 cm->make_not_entrant();
1393
1394 // Use Deoptimization::deoptimize for all of its side-effects:
1395 // revoking biases of monitors, gathering traps statistics, logging...
1396 // it also patches the return pc but we do not care about that
1397 // since we return a continuation to the deopt_blob below.
1398 JavaThread* thread = JavaThread::current();
1399 RegisterMap reg_map(thread, UseBiasedLocking);
1400 frame runtime_frame = thread->last_frame();
1401 frame caller_frame = runtime_frame.sender(®_map);
1402 assert(caller_frame.cb()->as_compiled_method_or_null() == cm, "expect top frame compiled method");
1403 Deoptimization::deoptimize(thread, caller_frame, ®_map, Deoptimization::Reason_not_compiled_exception_handler);
1528 #if INCLUDE_JVMCI
1529 // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
1530 RegisterMap reg_map(thread, true);
1531 #else
1532 RegisterMap reg_map(thread, UseBiasedLocking);
1533 #endif
1534 frame stub_frame = thread->last_frame();
1535 frame fr = stub_frame.sender(®_map);
1536 // Make sure the calling nmethod is not getting deoptimized and removed
1537 // before we are done with it.
1538 nmethodLocker nl(fr.pc());
1539
1540 // Log a message
1541 Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
1542 trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
1543
1544 {
1545 ResourceMark rm;
1546
1547 // Revoke biases of any monitors in the frame to ensure we can migrate them
1548 fix_monitors(thread, fr, ®_map);
1549
1550 DeoptReason reason = trap_request_reason(trap_request);
1551 DeoptAction action = trap_request_action(trap_request);
1552 #if INCLUDE_JVMCI
1553 int debug_id = trap_request_debug_id(trap_request);
1554 #endif
1555 jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1556
1557 vframe* vf = vframe::new_vframe(&fr, ®_map, thread);
1558 compiledVFrame* cvf = compiledVFrame::cast(vf);
1559
1560 CompiledMethod* nm = cvf->code();
1561
1562 ScopeDesc* trap_scope = cvf->scope();
1563
1564 if (TraceDeoptimization) {
1565 ttyLocker ttyl;
1566 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()
1567 #if INCLUDE_JVMCI
1568 , debug_id
|