comparison src/share/vm/runtime/deoptimization.cpp @ 20804:7848fc12602b

Merge with jdk8u40-b25
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Tue, 07 Apr 2015 14:58:49 +0200
parents 71040f48cc34 793204f5528a
children be896a1983c0
comparison
equal deleted inserted replaced
20184:84105dcdb05b 20804:7848fc12602b
221 vf = vf->sender(); 221 vf = vf->sender();
222 } 222 }
223 assert(vf->is_compiled_frame(), "Wrong frame type"); 223 assert(vf->is_compiled_frame(), "Wrong frame type");
224 chunk->push(compiledVFrame::cast(vf)); 224 chunk->push(compiledVFrame::cast(vf));
225 225
226 bool realloc_failures = false;
227
226 #if defined(COMPILER2) || defined(GRAAL) 228 #if defined(COMPILER2) || defined(GRAAL)
227 // Reallocate the non-escaping objects and restore their fields. Then 229 // Reallocate the non-escaping objects and restore their fields. Then
228 // relock objects if synchronization on them was eliminated. 230 // relock objects if synchronization on them was eliminated.
229 #ifdef COMPILER2 231 #ifdef COMPILER2
230 if (DoEscapeAnalysis || EliminateNestedLocks) { 232 if (DoEscapeAnalysis || EliminateNestedLocks) {
238 // runtime calls (for example, used to allocate new objects/arrays 240 // runtime calls (for example, used to allocate new objects/arrays
239 // on slow code path) and any other calls generated in compiled code. 241 // on slow code path) and any other calls generated in compiled code.
240 // It is not guaranteed that we can get such information here only 242 // It is not guaranteed that we can get such information here only
241 // by analyzing bytecode in deoptimized frames. This is why this flag 243 // by analyzing bytecode in deoptimized frames. This is why this flag
242 // is set during method compilation (see Compile::Process_OopMap_Node()). 244 // is set during method compilation (see Compile::Process_OopMap_Node()).
243 bool save_oop_result = chunk->at(0)->scope()->return_oop(); 245 // If the previous frame was popped, we don't have a result.
246 bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution();
244 Handle return_value; 247 Handle return_value;
245 if (save_oop_result) { 248 if (save_oop_result) {
246 // Reallocation may trigger GC. If deoptimization happened on return from 249 // Reallocation may trigger GC. If deoptimization happened on return from
247 // call which returns oop we need to save it since it is not in oopmap. 250 // call which returns oop we need to save it since it is not in oopmap.
248 oop result = deoptee.saved_oop_result(&map); 251 oop result = deoptee.saved_oop_result(&map);
252 if (TraceDeoptimization) { 255 if (TraceDeoptimization) {
253 ttyLocker ttyl; 256 ttyLocker ttyl;
254 tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread); 257 tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);
255 } 258 }
256 } 259 }
257 bool reallocated = false;
258 if (objects != NULL) { 260 if (objects != NULL) {
259 JRT_BLOCK 261 JRT_BLOCK
260 reallocated = realloc_objects(thread, &deoptee, objects, THREAD); 262 realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD);
261 JRT_END 263 JRT_END
262 } 264 reassign_fields(&deoptee, &map, objects, realloc_failures);
263 if (reallocated) {
264 reassign_fields(&deoptee, &map, objects);
265 #ifndef PRODUCT 265 #ifndef PRODUCT
266 if (TraceDeoptimization) { 266 if (TraceDeoptimization) {
267 ttyLocker ttyl; 267 ttyLocker ttyl;
268 tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread); 268 tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);
269 print_objects(objects); 269 print_objects(objects, realloc_failures);
270 } 270 }
271 #endif 271 #endif
272 } 272 }
273 if (save_oop_result) { 273 if (save_oop_result) {
274 // Restore result. 274 // Restore result.
284 for (int i = 0; i < chunk->length(); i++) { 284 for (int i = 0; i < chunk->length(); i++) {
285 compiledVFrame* cvf = chunk->at(i); 285 compiledVFrame* cvf = chunk->at(i);
286 assert (cvf->scope() != NULL,"expect only compiled java frames"); 286 assert (cvf->scope() != NULL,"expect only compiled java frames");
287 GrowableArray<MonitorInfo*>* monitors = cvf->monitors(); 287 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
288 if (monitors->is_nonempty()) { 288 if (monitors->is_nonempty()) {
289 relock_objects(monitors, thread); 289 relock_objects(monitors, thread, realloc_failures);
290 #ifndef PRODUCT 290 #ifndef PRODUCT
291 if (PrintDeoptimizationDetails) { 291 if (PrintDeoptimizationDetails) {
292 ttyLocker ttyl; 292 ttyLocker ttyl;
293 for (int j = 0; j < monitors->length(); j++) { 293 for (int j = 0; j < monitors->length(); j++) {
294 MonitorInfo* mi = monitors->at(j); 294 MonitorInfo* mi = monitors->at(j);
295 if (mi->eliminated()) { 295 if (mi->eliminated()) {
296 if (first) { 296 if (first) {
297 first = false; 297 first = false;
298 tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread); 298 tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);
299 } 299 }
300 tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner()); 300 if (mi->owner_is_scalar_replaced()) {
301 Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
302 tty->print_cr(" failed reallocation for klass %s", k->external_name());
303 } else {
304 tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner());
305 }
301 } 306 }
302 } 307 }
303 } 308 }
304 #endif // !PRODUCT 309 #endif
305 } 310 }
306 } 311 }
307 #ifdef COMPILER2 312 #ifdef COMPILER2
308 } 313 }
309 } 314 }
313 // Ensure that no safepoint is taken after pointers have been stored 318 // Ensure that no safepoint is taken after pointers have been stored
314 // in fields of rematerialized objects. If a safepoint occurs from here on 319 // in fields of rematerialized objects. If a safepoint occurs from here on
315 // out the java state residing in the vframeArray will be missed. 320 // out the java state residing in the vframeArray will be missed.
316 No_Safepoint_Verifier no_safepoint; 321 No_Safepoint_Verifier no_safepoint;
317 322
318 vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk); 323 vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
319 324 #if defined(COMPILER2) || defined(GRAAL)
320 assert(thread->vframe_array_head() == NULL, "Pending deopt!");; 325 if (realloc_failures) {
326 pop_frames_failed_reallocs(thread, array);
327 }
328 #endif
329
330 assert(thread->vframe_array_head() == NULL, "Pending deopt!");
321 thread->set_vframe_array_head(array); 331 thread->set_vframe_array_head(array);
322 332
323 // Now that the vframeArray has been created if we have any deferred local writes 333 // Now that the vframeArray has been created if we have any deferred local writes
324 // added by jvmti then we can free up that structure as the data is now in the 334 // added by jvmti then we can free up that structure as the data is now in the
325 // vframeArray 335 // vframeArray
717 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) || 727 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) ||
718 (try_next_mask && 728 (try_next_mask &&
719 (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size - 729 (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size -
720 top_frame_expression_stack_adjustment))) || 730 top_frame_expression_stack_adjustment))) ||
721 (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) || 731 (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||
722 (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute) && 732 (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) &&
723 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size)) 733 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))
724 )) { 734 )) {
725 ttyLocker ttyl; 735 ttyLocker ttyl;
726 736
727 // Print out some information that will help us debug the problem 737 // Print out some information that will help us debug the problem
771 Handle pending_exception(thread->pending_exception()); 781 Handle pending_exception(thread->pending_exception());
772 const char* exception_file = thread->exception_file(); 782 const char* exception_file = thread->exception_file();
773 int exception_line = thread->exception_line(); 783 int exception_line = thread->exception_line();
774 thread->clear_pending_exception(); 784 thread->clear_pending_exception();
775 785
786 bool failures = false;
787
776 for (int i = 0; i < objects->length(); i++) { 788 for (int i = 0; i < objects->length(); i++) {
777 assert(objects->at(i)->is_object(), "invalid debug information"); 789 assert(objects->at(i)->is_object(), "invalid debug information");
778 ObjectValue* sv = (ObjectValue*) objects->at(i); 790 ObjectValue* sv = (ObjectValue*) objects->at(i);
779 791
780 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); 792 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
781 oop obj = NULL; 793 oop obj = NULL;
782 794
783 if (k->oop_is_instance()) { 795 if (k->oop_is_instance()) {
784 InstanceKlass* ik = InstanceKlass::cast(k()); 796 InstanceKlass* ik = InstanceKlass::cast(k());
785 obj = ik->allocate_instance(CHECK_(false)); 797 obj = ik->allocate_instance(THREAD);
786 } else if (k->oop_is_typeArray()) { 798 } else if (k->oop_is_typeArray()) {
787 TypeArrayKlass* ak = TypeArrayKlass::cast(k()); 799 TypeArrayKlass* ak = TypeArrayKlass::cast(k());
788 assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length"); 800 assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
789 int len = sv->field_size() / type2size[ak->element_type()]; 801 int len = sv->field_size() / type2size[ak->element_type()];
790 obj = ak->allocate(len, CHECK_(false)); 802 obj = ak->allocate(len, THREAD);
791 } else if (k->oop_is_objArray()) { 803 } else if (k->oop_is_objArray()) {
792 ObjArrayKlass* ak = ObjArrayKlass::cast(k()); 804 ObjArrayKlass* ak = ObjArrayKlass::cast(k());
793 obj = ak->allocate(sv->field_size(), CHECK_(false)); 805 obj = ak->allocate(sv->field_size(), THREAD);
794 } 806 }
795 807
796 assert(obj != NULL, "allocation failed"); 808 if (obj == NULL) {
809 failures = true;
810 }
811
797 assert(sv->value().is_null(), "redundant reallocation"); 812 assert(sv->value().is_null(), "redundant reallocation");
813 assert(obj != NULL || HAS_PENDING_EXCEPTION, "allocation should succeed or we should get an exception");
814 CLEAR_PENDING_EXCEPTION;
798 sv->set_value(obj); 815 sv->set_value(obj);
799 } 816 }
800 817
801 if (pending_exception.not_null()) { 818 if (failures) {
819 THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
820 } else if (pending_exception.not_null()) {
802 thread->set_pending_exception(pending_exception(), exception_file, exception_line); 821 thread->set_pending_exception(pending_exception(), exception_file, exception_line);
803 } 822 }
804 823
805 return true; 824 return failures;
806 } 825 }
807 826
808 // restore elements of an eliminated type array 827 // restore elements of an eliminated type array
809 void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) { 828 void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) {
810 int index = 0; 829 int index = 0;
1010 } 1029 }
1011 return svIndex; 1030 return svIndex;
1012 } 1031 }
1013 1032
1014 // restore fields of all eliminated objects and arrays 1033 // restore fields of all eliminated objects and arrays
1015 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects) { 1034 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
1016 for (int i = 0; i < objects->length(); i++) { 1035 for (int i = 0; i < objects->length(); i++) {
1017 ObjectValue* sv = (ObjectValue*) objects->at(i); 1036 ObjectValue* sv = (ObjectValue*) objects->at(i);
1018 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); 1037 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
1019 Handle obj = sv->value(); 1038 Handle obj = sv->value();
1020 assert(obj.not_null(), "reallocation was missed"); 1039 assert(obj.not_null() || realloc_failures, "reallocation was missed");
1040 if (obj.is_null()) {
1041 continue;
1042 }
1021 if (PrintDeoptimizationDetails) { 1043 if (PrintDeoptimizationDetails) {
1022 tty->print_cr("reassign fields for object of type %s!", k->name()->as_C_string()); 1044 tty->print_cr("reassign fields for object of type %s!", k->name()->as_C_string());
1023 } 1045 }
1024 1046
1025 if (k->oop_is_instance()) { 1047 if (k->oop_is_instance()) {
1034 } 1056 }
1035 } 1057 }
1036 1058
1037 1059
1038 // relock objects for which synchronization was eliminated 1060 // relock objects for which synchronization was eliminated
1039 void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread) { 1061 void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread, bool realloc_failures) {
1040 for (int i = 0; i < monitors->length(); i++) { 1062 for (int i = 0; i < monitors->length(); i++) {
1041 MonitorInfo* mon_info = monitors->at(i); 1063 MonitorInfo* mon_info = monitors->at(i);
1042 if (mon_info->eliminated()) { 1064 if (mon_info->eliminated()) {
1043 assert(mon_info->owner() != NULL, "reallocation was missed"); 1065 assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed");
1044 Handle obj = Handle(mon_info->owner()); 1066 if (!mon_info->owner_is_scalar_replaced()) {
1045 markOop mark = obj->mark(); 1067 Handle obj = Handle(mon_info->owner());
1046 if (UseBiasedLocking && mark->has_bias_pattern()) { 1068 markOop mark = obj->mark();
1047 // New allocated objects may have the mark set to anonymously biased. 1069 if (UseBiasedLocking && mark->has_bias_pattern()) {
1048 // Also the deoptimized method may called methods with synchronization 1070 // New allocated objects may have the mark set to anonymously biased.
1049 // where the thread-local object is bias locked to the current thread. 1071 // Also the deoptimized method may called methods with synchronization
1050 assert(mark->is_biased_anonymously() || 1072 // where the thread-local object is bias locked to the current thread.
1051 mark->biased_locker() == thread, "should be locked to current thread"); 1073 assert(mark->is_biased_anonymously() ||
1052 // Reset mark word to unbiased prototype. 1074 mark->biased_locker() == thread, "should be locked to current thread");
1053 markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age()); 1075 // Reset mark word to unbiased prototype.
1054 obj->set_mark(unbiased_prototype); 1076 markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());
1055 } 1077 obj->set_mark(unbiased_prototype);
1056 BasicLock* lock = mon_info->lock(); 1078 }
1057 ObjectSynchronizer::slow_enter(obj, lock, thread); 1079 BasicLock* lock = mon_info->lock();
1058 } 1080 ObjectSynchronizer::slow_enter(obj, lock, thread);
1059 assert(mon_info->owner()->is_locked(), "object must be locked now"); 1081 assert(mon_info->owner()->is_locked(), "object must be locked now");
1082 }
1083 }
1060 } 1084 }
1061 } 1085 }
1062 1086
1063 1087
1064 #ifndef PRODUCT 1088 #ifndef PRODUCT
1065 // print information about reallocated objects 1089 // print information about reallocated objects
1066 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects) { 1090 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
1067 fieldDescriptor fd; 1091 fieldDescriptor fd;
1068 1092
1069 for (int i = 0; i < objects->length(); i++) { 1093 for (int i = 0; i < objects->length(); i++) {
1070 ObjectValue* sv = (ObjectValue*) objects->at(i); 1094 ObjectValue* sv = (ObjectValue*) objects->at(i);
1071 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); 1095 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
1072 Handle obj = sv->value(); 1096 Handle obj = sv->value();
1073 1097
1074 tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()()); 1098 tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());
1075 k->print_value(); 1099 k->print_value();
1076 tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize); 1100 assert(obj.not_null() || realloc_failures, "reallocation was missed");
1101 if (obj.is_null()) {
1102 tty->print(" allocation failed");
1103 } else {
1104 tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);
1105 }
1077 tty->cr(); 1106 tty->cr();
1078 1107
1079 if (Verbose) { 1108 if (Verbose && !obj.is_null()) {
1080 k->oop_print_on(obj(), tty); 1109 k->oop_print_on(obj(), tty);
1081 } 1110 }
1082 } 1111 }
1083 } 1112 }
1084 #endif 1113 #endif
1085 #endif // COMPILER2 || GRAAL 1114 #endif // COMPILER2 || GRAAL
1086 1115
1087 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk) { 1116 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
1088 Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp()); 1117 Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());
1089 1118
1090 #ifndef PRODUCT 1119 #ifndef PRODUCT
1091 if (PrintDeoptimizationDetails) { 1120 if (PrintDeoptimizationDetails) {
1092 ttyLocker ttyl; 1121 ttyLocker ttyl;
1125 frame sender = caller; 1154 frame sender = caller;
1126 1155
1127 // Since the Java thread being deoptimized will eventually adjust it's own stack, 1156 // Since the Java thread being deoptimized will eventually adjust it's own stack,
1128 // the vframeArray containing the unpacking information is allocated in the C heap. 1157 // the vframeArray containing the unpacking information is allocated in the C heap.
1129 // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames(). 1158 // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
1130 vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr); 1159 vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);
1131 1160
1132 // Compare the vframeArray to the collected vframes 1161 // Compare the vframeArray to the collected vframes
1133 assert(array->structural_compare(thread, chunk), "just checking"); 1162 assert(array->structural_compare(thread, chunk), "just checking");
1134 1163
1135 #ifndef PRODUCT 1164 #ifndef PRODUCT
1140 #endif // PRODUCT 1169 #endif // PRODUCT
1141 1170
1142 return array; 1171 return array;
1143 } 1172 }
1144 1173
1174 #if defined(COMPILER2) || defined(GRAAL)
1175 void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {
1176 // Reallocation of some scalar replaced objects failed. Record
1177 // that we need to pop all the interpreter frames for the
1178 // deoptimized compiled frame.
1179 assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");
1180 thread->set_frames_to_pop_failed_realloc(array->frames());
1181 // Unlock all monitors here otherwise the interpreter will see a
1182 // mix of locked and unlocked monitors (because of failed
1183 // reallocations of synchronized objects) and be confused.
1184 for (int i = 0; i < array->frames(); i++) {
1185 MonitorChunk* monitors = array->element(i)->monitors();
1186 if (monitors != NULL) {
1187 for (int j = 0; j < monitors->number_of_monitors(); j++) {
1188 BasicObjectLock* src = monitors->at(j);
1189 if (src->obj() != NULL) {
1190 ObjectSynchronizer::fast_exit(src->obj(), src->lock(), thread);
1191 }
1192 }
1193 array->element(i)->free_monitors(thread);
1194 #ifdef ASSERT
1195 array->element(i)->set_removed_monitors();
1196 #endif
1197 }
1198 }
1199 }
1200 #endif
1145 1201
1146 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) { 1202 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
1147 GrowableArray<MonitorInfo*>* monitors = cvf->monitors(); 1203 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1148 for (int i = 0; i < monitors->length(); i++) { 1204 for (int i = 0; i < monitors->length(); i++) {
1149 MonitorInfo* mon_info = monitors->at(i); 1205 MonitorInfo* mon_info = monitors->at(i);
2083 "age", 2139 "age",
2084 "predicate", 2140 "predicate",
2085 "loop_limit_check", 2141 "loop_limit_check",
2086 "speculate_class_check", 2142 "speculate_class_check",
2087 "rtm_state_change", 2143 "rtm_state_change",
2144 "unstable_if"
2088 #ifdef GRAAL 2145 #ifdef GRAAL
2089 "aliasing", 2146 "aliasing",
2090 "transfer_to_interpreter", 2147 "transfer_to_interpreter",
2091 "not_compiled_exception_handler", 2148 "not_compiled_exception_handler",
2092 "unresolved", 2149 "unresolved",