1 /* 2 * Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "ci/ciMetadata.hpp" 26 #include "ci/ciMethodData.hpp" 27 #include "ci/ciReplay.hpp" 28 #include "ci/ciUtilities.inline.hpp" 29 #include "compiler/compiler_globals.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/klass.inline.hpp" 33 #include "oops/methodData.inline.hpp" 34 #include "oops/trainingData.hpp" 35 #include "runtime/deoptimization.hpp" 36 #include "utilities/copy.hpp" 37 38 // ciMethodData 39 40 // ------------------------------------------------------------------ 41 // ciMethodData::ciMethodData 42 // 43 ciMethodData::ciMethodData(MethodData* md) 44 : ciMetadata(md), 45 _data_size(0), _extra_data_size(0), _data(nullptr), 46 _parameters_data_offset(0), 47 _exception_handlers_data_offset(0), 48 // Set an initial hint. Don't use set_hint_di() because 49 // first_di() may be out of bounds if data_size is 0. 50 _hint_di(first_di()), 51 _state(empty_state), 52 _saw_free_extra_data(false), 53 // Initialize the escape information (to "don't know."); 54 _eflags(0), _arg_local(0), _arg_stack(0), _arg_returned(0), 55 _invocation_counter(0), 56 _orig() {} 57 58 59 static bool is_klass_loaded(Klass* k) { 60 return TrainingData::is_klass_loaded(k); 61 } 62 63 // Check for entries that reference an unloaded method 64 class PrepareExtraDataClosure : public CleanExtraDataClosure { 65 MethodData* _mdo; 66 SafepointStateTracker _safepoint_tracker; 67 GrowableArray<Method*> _uncached_methods; 68 69 public: 70 PrepareExtraDataClosure(MethodData* mdo) 71 : _mdo(mdo), 72 _safepoint_tracker(SafepointSynchronize::safepoint_state_tracker()), 73 _uncached_methods() 74 { } 75 76 bool is_live(Method* m) { 77 Klass* holder = m->method_holder(); 78 if (holder == nullptr || !holder->is_loader_present_and_alive() || !is_klass_loaded(holder)) { 79 return false; 80 } 81 if (CURRENT_ENV->cached_metadata(m) == nullptr) { 82 // Uncached entries need to be pre-populated. 83 _uncached_methods.append(m); 84 } 85 return true; 86 } 87 88 bool has_safepointed() { 89 return _safepoint_tracker.safepoint_state_changed(); 90 } 91 92 bool finish() { 93 if (_uncached_methods.length() == 0) { 94 // Preparation finished iff all Methods* were already cached. 95 return true; 96 } 97 // We are currently holding the extra_data_lock and ensuring 98 // no safepoint breaks the lock. 99 _mdo->check_extra_data_locked(); 100 101 // We now want to cache some method data. This could cause a safepoint. 102 // We temporarily release the lock and allow safepoints, and revert that 103 // at the end of the scope. This is safe, since we currently do not hold 104 // any extra_method_data: finish is called only after clean_extra_data, 105 // and the outer scope that first aquired the lock should not hold any 106 // extra_method_data while cleaning is performed, as the offsets can change. 107 MutexUnlocker mu(_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag); 108 109 for (int i = 0; i < _uncached_methods.length(); ++i) { 110 if (has_safepointed()) { 111 // The metadata in the growable array might contain stale 112 // entries after a safepoint. 113 return false; 114 } 115 Method* method = _uncached_methods.at(i); 116 // Populating ciEnv caches may cause safepoints due 117 // to taking the Compile_lock with safepoint checks. 118 (void)CURRENT_ENV->get_method(method); 119 } 120 return false; 121 } 122 }; 123 124 void ciMethodData::prepare_metadata() { 125 MethodData* mdo = get_MethodData(); 126 127 for (;;) { 128 ResourceMark rm; 129 PrepareExtraDataClosure cl(mdo); 130 mdo->clean_extra_data(&cl); 131 if (cl.finish()) { 132 // When encountering uncached metadata, the Compile_lock might be 133 // acquired when creating ciMetadata handles, causing safepoints 134 // which requires a new round of preparation to clean out potentially 135 // new unloading metadata. 136 return; 137 } 138 } 139 } 140 141 void ciMethodData::load_remaining_extra_data() { 142 MethodData* mdo = get_MethodData(); 143 144 // Lock to read ProfileData, and ensure lock is not unintentionally broken by a safepoint 145 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag); 146 147 // Deferred metadata cleaning due to concurrent class unloading. 148 prepare_metadata(); 149 // After metadata preparation, there is no stale metadata, 150 // and no safepoints can introduce more stale metadata. 151 NoSafepointVerifier no_safepoint; 152 153 assert((mdo->data_size() == _data_size) && (mdo->extra_data_size() == _extra_data_size), "sanity, unchanged"); 154 assert(extra_data_base() == (DataLayout*)((address) _data + _data_size), "sanity"); 155 156 // Copy the extra data once it is prepared (i.e. cache populated, no release of extra data lock anymore) 157 Copy::disjoint_words_atomic((HeapWord*) mdo->extra_data_base(), 158 (HeapWord*) extra_data_base(), 159 // copy everything from extra_data_base() up to parameters_data_base() 160 pointer_delta(parameters_data_base(), extra_data_base(), HeapWordSize)); 161 162 // skip parameter data copying. Already done in 'load_data' 163 164 // copy exception handler data 165 Copy::disjoint_words_atomic((HeapWord*) mdo->exception_handler_data_base(), 166 (HeapWord*) exception_handler_data_base(), 167 exception_handler_data_size() / HeapWordSize); 168 169 // speculative trap entries also hold a pointer to a Method so need to be translated 170 DataLayout* dp_src = mdo->extra_data_base(); 171 DataLayout* end_src = mdo->args_data_limit(); 172 DataLayout* dp_dst = extra_data_base(); 173 for (;; dp_src = MethodData::next_extra(dp_src), dp_dst = MethodData::next_extra(dp_dst)) { 174 assert(dp_src < end_src, "moved past end of extra data"); 175 assert(((intptr_t)dp_dst) - ((intptr_t)extra_data_base()) == ((intptr_t)dp_src) - ((intptr_t)mdo->extra_data_base()), "source and destination don't match"); 176 177 int tag = dp_src->tag(); 178 switch(tag) { 179 case DataLayout::speculative_trap_data_tag: { 180 ciSpeculativeTrapData data_dst(dp_dst); 181 SpeculativeTrapData data_src(dp_src); 182 data_dst.translate_from(&data_src); 183 break; 184 } 185 case DataLayout::bit_data_tag: 186 break; 187 case DataLayout::no_tag: 188 case DataLayout::arg_info_data_tag: 189 // An empty slot or ArgInfoData entry marks the end of the trap data 190 { 191 return; // Need a block to avoid SS compiler bug 192 } 193 default: 194 fatal("bad tag = %d", tag); 195 } 196 } 197 } 198 199 bool ciMethodData::load_data() { 200 MethodData* mdo = get_MethodData(); 201 if (mdo == nullptr) { 202 return false; 203 } 204 205 // To do: don't copy the data if it is not "ripe" -- require a minimum # 206 // of invocations. 207 208 // Snapshot the data and extra parameter data first without the extra trap and arg info data. 209 // Those are copied in a second step. Actually, an approximate snapshot of the data is taken. 210 // Any concurrently executing threads may be changing the data as we copy it. 211 // 212 // The first snapshot step requires two copies (data entries and parameter data entries) since 213 // the MDO is laid out as follows: 214 // 215 // data_base: --------------------------- 216 // | data entries | 217 // | ... | 218 // extra_data_base: --------------------------- 219 // | trap data entries | 220 // | ... | 221 // | one arg info data entry | 222 // | data for each arg | 223 // | ... | 224 // args_data_limit: --------------------------- 225 // | parameter data entries | 226 // | ... | 227 // param_data_limit: --------------------------- 228 // | ex handler data entries | 229 // | ... | 230 // extra_data_limit: --------------------------- 231 // 232 // _data_size = extra_data_base - data_base 233 // _extra_data_size = extra_data_limit - extra_data_base 234 // total_size = _data_size + _extra_data_size 235 // args_data_limit = param_data_base 236 // param_data_limit = exception_handler_data_base 237 // extra_data_limit = extra_data_limit 238 239 #ifndef ZERO 240 // Some Zero platforms do not have expected alignment, and do not use 241 // this code. static_assert would still fire and fail for them. 242 static_assert(sizeof(_orig) % HeapWordSize == 0, "align"); 243 #endif 244 Copy::disjoint_words_atomic((HeapWord*) &mdo->_compiler_counters, 245 (HeapWord*) &_orig, 246 sizeof(_orig) / HeapWordSize); 247 Arena* arena = CURRENT_ENV->arena(); 248 _data_size = mdo->data_size(); 249 _extra_data_size = mdo->extra_data_size(); 250 int total_size = _data_size + _extra_data_size; 251 _data = (intptr_t *) arena->Amalloc(total_size); 252 Copy::disjoint_words_atomic((HeapWord*) mdo->data_base(), 253 (HeapWord*) _data, 254 _data_size / HeapWordSize); 255 // Copy offsets. This is used below 256 _parameters_data_offset = mdo->parameters_type_data_di(); 257 _exception_handlers_data_offset = mdo->exception_handlers_data_di(); 258 259 int parameters_data_size = mdo->parameters_size_in_bytes(); 260 if (parameters_data_size > 0) { 261 // Snapshot the parameter data 262 Copy::disjoint_words_atomic((HeapWord*) mdo->parameters_data_base(), 263 (HeapWord*) parameters_data_base(), 264 parameters_data_size / HeapWordSize); 265 } 266 // Traverse the profile data, translating any oops into their 267 // ci equivalents. 268 ResourceMark rm; 269 ciProfileData* ci_data = first_data(); 270 ProfileData* data = mdo->first_data(); 271 while (is_valid(ci_data)) { 272 ci_data->translate_from(data); 273 ci_data = next_data(ci_data); 274 data = mdo->next_data(data); 275 } 276 if (mdo->parameters_type_data() != nullptr) { 277 DataLayout* parameters_data = data_layout_at(_parameters_data_offset); 278 ciParametersTypeData* parameters = new ciParametersTypeData(parameters_data); 279 parameters->translate_from(mdo->parameters_type_data()); 280 } 281 282 assert((DataLayout*) ((address)_data + total_size - parameters_data_size - exception_handler_data_size()) == args_data_limit(), 283 "sanity - parameter data starts after the argument data of the single ArgInfoData entry"); 284 load_remaining_extra_data(); 285 286 // Note: Extra data are all BitData, and do not need translation. 287 _invocation_counter = mdo->invocation_count(); 288 if (_invocation_counter == 0 && mdo->backedge_count() > 0) { 289 // Avoid skewing counter data during OSR compilation. 290 // Sometimes, MDO is allocated during the very first invocation and OSR compilation is triggered 291 // solely by backedge counter while invocation counter stays zero. In such case, it's important 292 // to observe non-zero invocation count to properly scale profile counts (see ciMethod::scale_count()). 293 _invocation_counter = 1; 294 } 295 296 _state = mdo->is_mature() ? mature_state : immature_state; 297 _eflags = mdo->eflags(); 298 _arg_local = mdo->arg_local(); 299 _arg_stack = mdo->arg_stack(); 300 _arg_returned = mdo->arg_returned(); 301 if (ReplayCompiles) { 302 ciReplay::initialize(this); 303 if (is_empty()) { 304 return false; 305 } 306 } 307 return true; 308 } 309 310 void ciReceiverTypeData::translate_receiver_data_from(const ProfileData* data) { 311 for (uint row = 0; row < row_limit(); row++) { 312 Klass* k = data->as_ReceiverTypeData()->receiver(row); 313 if (k != nullptr && k->class_loader_data() != nullptr && is_klass_loaded(k)) { 314 if (k->is_loader_alive()) { 315 ciKlass* klass = CURRENT_ENV->get_klass(k); 316 set_receiver(row, klass); 317 } else { 318 // With concurrent class unloading, the MDO could have stale metadata; override it 319 clear_row(row); 320 } 321 } else { 322 set_receiver(row, nullptr); 323 } 324 } 325 } 326 327 void ciTypeStackSlotEntries::translate_type_data_from(const TypeStackSlotEntries* entries) { 328 for (int i = 0; i < number_of_entries(); i++) { 329 intptr_t k = entries->type(i); 330 Klass* klass = (Klass*)klass_part(k); 331 if (klass == nullptr || !klass->is_loader_present_and_alive() || !is_klass_loaded(klass)) { 332 // With concurrent class unloading, the MDO could have stale metadata; override it 333 TypeStackSlotEntries::set_type(i, TypeStackSlotEntries::with_status((Klass*)nullptr, k)); 334 } else { 335 TypeStackSlotEntries::set_type(i, translate_klass(k)); 336 } 337 } 338 } 339 340 void ciReturnTypeEntry::translate_type_data_from(const ReturnTypeEntry* ret) { 341 intptr_t k = ret->type(); 342 Klass* klass = (Klass*)klass_part(k); 343 if (klass == nullptr || !klass->is_loader_present_and_alive() || !is_klass_loaded(klass)) { 344 // With concurrent class unloading, the MDO could have stale metadata; override it 345 set_type(ReturnTypeEntry::with_status((Klass*)nullptr, k)); 346 } else { 347 set_type(translate_klass(k)); 348 } 349 } 350 351 void ciSpeculativeTrapData::translate_from(const ProfileData* data) { 352 Method* m = data->as_SpeculativeTrapData()->method(); 353 ciMethod* ci_m = CURRENT_ENV->get_method(m); 354 set_method(ci_m); 355 } 356 357 // Get the data at an arbitrary (sort of) data index. 358 ciProfileData* ciMethodData::data_at(int data_index) { 359 if (out_of_bounds(data_index)) { 360 return nullptr; 361 } 362 DataLayout* data_layout = data_layout_at(data_index); 363 return data_from(data_layout); 364 } 365 366 ciProfileData* ciMethodData::data_from(DataLayout* data_layout) { 367 switch (data_layout->tag()) { 368 case DataLayout::no_tag: 369 default: 370 ShouldNotReachHere(); 371 return nullptr; 372 case DataLayout::bit_data_tag: 373 return new ciBitData(data_layout); 374 case DataLayout::counter_data_tag: 375 return new ciCounterData(data_layout); 376 case DataLayout::jump_data_tag: 377 return new ciJumpData(data_layout); 378 case DataLayout::receiver_type_data_tag: 379 return new ciReceiverTypeData(data_layout); 380 case DataLayout::virtual_call_data_tag: 381 return new ciVirtualCallData(data_layout); 382 case DataLayout::ret_data_tag: 383 return new ciRetData(data_layout); 384 case DataLayout::branch_data_tag: 385 return new ciBranchData(data_layout); 386 case DataLayout::multi_branch_data_tag: 387 return new ciMultiBranchData(data_layout); 388 case DataLayout::arg_info_data_tag: 389 return new ciArgInfoData(data_layout); 390 case DataLayout::call_type_data_tag: 391 return new ciCallTypeData(data_layout); 392 case DataLayout::virtual_call_type_data_tag: 393 return new ciVirtualCallTypeData(data_layout); 394 case DataLayout::parameters_type_data_tag: 395 return new ciParametersTypeData(data_layout); 396 }; 397 } 398 399 // Iteration over data. 400 ciProfileData* ciMethodData::next_data(ciProfileData* current) { 401 int current_index = dp_to_di(current->dp()); 402 int next_index = current_index + current->size_in_bytes(); 403 ciProfileData* next = data_at(next_index); 404 return next; 405 } 406 407 DataLayout* ciMethodData::next_data_layout_helper(DataLayout* current, bool extra) { 408 int current_index = dp_to_di((address)current); 409 int next_index = current_index + current->size_in_bytes(); 410 if (extra ? out_of_bounds_extra(next_index) : out_of_bounds(next_index)) { 411 return nullptr; 412 } 413 DataLayout* next = data_layout_at(next_index); 414 return next; 415 } 416 417 DataLayout* ciMethodData::next_data_layout(DataLayout* current) { 418 return next_data_layout_helper(current, false); 419 } 420 421 DataLayout* ciMethodData::next_extra_data_layout(DataLayout* current) { 422 return next_data_layout_helper(current, true); 423 } 424 425 ciProfileData* ciMethodData::bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots) { 426 DataLayout* dp = extra_data_base(); 427 DataLayout* end = args_data_limit(); 428 two_free_slots = false; 429 for (;dp < end; dp = MethodData::next_extra(dp)) { 430 switch(dp->tag()) { 431 case DataLayout::no_tag: 432 _saw_free_extra_data = true; // observed an empty slot (common case) 433 two_free_slots = (MethodData::next_extra(dp)->tag() == DataLayout::no_tag); 434 return nullptr; 435 case DataLayout::arg_info_data_tag: 436 return nullptr; // ArgInfoData is after the trap data right before the parameter data. 437 case DataLayout::bit_data_tag: 438 if (m == nullptr && dp->bci() == bci) { 439 return new ciBitData(dp); 440 } 441 break; 442 case DataLayout::speculative_trap_data_tag: { 443 ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp); 444 // data->method() might be null if the MDO is snapshotted 445 // concurrently with a trap 446 if (m != nullptr && data->method() == m && dp->bci() == bci) { 447 return data; 448 } 449 break; 450 } 451 default: 452 fatal("bad tag = %d", dp->tag()); 453 } 454 } 455 return nullptr; 456 } 457 458 // Translate a bci to its corresponding data, or nullptr. 459 ciProfileData* ciMethodData::bci_to_data(int bci, ciMethod* m) { 460 // If m is not nullptr we look for a SpeculativeTrapData entry 461 if (m == nullptr) { 462 DataLayout* data_layout = data_layout_before(bci); 463 for ( ; is_valid(data_layout); data_layout = next_data_layout(data_layout)) { 464 if (data_layout->bci() == bci) { 465 set_hint_di(dp_to_di((address)data_layout)); 466 return data_from(data_layout); 467 } else if (data_layout->bci() > bci) { 468 break; 469 } 470 } 471 } 472 bool two_free_slots = false; 473 ciProfileData* result = bci_to_extra_data(bci, m, two_free_slots); 474 if (result != nullptr) { 475 return result; 476 } 477 if (m != nullptr && !two_free_slots) { 478 // We were looking for a SpeculativeTrapData entry we didn't 479 // find. Room is not available for more SpeculativeTrapData 480 // entries, look in the non SpeculativeTrapData entries. 481 return bci_to_data(bci, nullptr); 482 } 483 return nullptr; 484 } 485 486 ciBitData ciMethodData::exception_handler_bci_to_data(int bci) { 487 assert(ProfileExceptionHandlers, "not profiling"); 488 assert(_data != nullptr, "must be initialized"); 489 for (DataLayout* data = exception_handler_data_base(); data < exception_handler_data_limit(); data = next_extra_data_layout(data)) { 490 assert(data != nullptr, "out of bounds?"); 491 if (data->bci() == bci) { 492 return ciBitData(data); 493 } 494 } 495 // called with invalid bci or wrong Method/MethodData 496 ShouldNotReachHere(); 497 return ciBitData(nullptr); 498 } 499 500 // Conservatively decode the trap_state of a ciProfileData. 501 int ciMethodData::has_trap_at(ciProfileData* data, int reason) { 502 typedef Deoptimization::DeoptReason DR_t; 503 int per_bc_reason 504 = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason); 505 if (trap_count(reason) == 0) { 506 // Impossible for this trap to have occurred, regardless of trap_state. 507 // Note: This happens if the MDO is empty. 508 return 0; 509 } else if (per_bc_reason == Deoptimization::Reason_none) { 510 // We cannot conclude anything; a trap happened somewhere, maybe here. 511 return -1; 512 } else if (data == nullptr) { 513 // No profile here, not even an extra_data record allocated on the fly. 514 // If there are empty extra_data records, and there had been a trap, 515 // there would have been a non-null data pointer. If there are no 516 // free extra_data records, we must return a conservative -1. 517 if (_saw_free_extra_data) 518 return 0; // Q.E.D. 519 else 520 return -1; // bail with a conservative answer 521 } else { 522 return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason); 523 } 524 } 525 526 int ciMethodData::trap_recompiled_at(ciProfileData* data) { 527 if (data == nullptr) { 528 return (_saw_free_extra_data? 0: -1); // (see previous method) 529 } else { 530 return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0; 531 } 532 } 533 534 void ciMethodData::clear_escape_info() { 535 VM_ENTRY_MARK; 536 MethodData* mdo = get_MethodData(); 537 if (mdo != nullptr) { 538 mdo->clear_escape_info(); 539 ArgInfoData *aid = arg_info(); 540 int arg_count = (aid == nullptr) ? 0 : aid->number_of_args(); 541 for (int i = 0; i < arg_count; i++) { 542 set_arg_modified(i, 0); 543 } 544 } 545 _eflags = _arg_local = _arg_stack = _arg_returned = 0; 546 } 547 548 // copy our escape info to the MethodData* if it exists 549 void ciMethodData::update_escape_info() { 550 VM_ENTRY_MARK; 551 MethodData* mdo = get_MethodData(); 552 if ( mdo != nullptr) { 553 mdo->set_eflags(_eflags); 554 mdo->set_arg_local(_arg_local); 555 mdo->set_arg_stack(_arg_stack); 556 mdo->set_arg_returned(_arg_returned); 557 int arg_count = mdo->method()->size_of_parameters(); 558 for (int i = 0; i < arg_count; i++) { 559 mdo->set_arg_modified(i, arg_modified(i)); 560 } 561 } 562 } 563 564 void ciMethodData::set_compilation_stats(short loops, short blocks) { 565 VM_ENTRY_MARK; 566 MethodData* mdo = get_MethodData(); 567 if (mdo != nullptr) { 568 mdo->set_num_loops(loops); 569 mdo->set_num_blocks(blocks); 570 } 571 } 572 573 void ciMethodData::set_would_profile(bool p) { 574 VM_ENTRY_MARK; 575 MethodData* mdo = get_MethodData(); 576 if (mdo != nullptr) { 577 mdo->set_would_profile(p); 578 } 579 } 580 581 void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) { 582 VM_ENTRY_MARK; 583 MethodData* mdo = get_MethodData(); 584 if (mdo != nullptr) { 585 // Lock to read ProfileData, and ensure lock is not broken by a safepoint 586 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag); 587 588 ProfileData* data = mdo->bci_to_data(bci); 589 if (data != nullptr) { 590 if (data->is_CallTypeData()) { 591 data->as_CallTypeData()->set_argument_type(i, k->get_Klass()); 592 } else { 593 assert(data->is_VirtualCallTypeData(), "no arguments!"); 594 data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass()); 595 } 596 } 597 } 598 } 599 600 void ciMethodData::set_parameter_type(int i, ciKlass* k) { 601 VM_ENTRY_MARK; 602 MethodData* mdo = get_MethodData(); 603 if (mdo != nullptr) { 604 mdo->parameters_type_data()->set_type(i, k->get_Klass()); 605 } 606 } 607 608 void ciMethodData::set_return_type(int bci, ciKlass* k) { 609 VM_ENTRY_MARK; 610 MethodData* mdo = get_MethodData(); 611 if (mdo != nullptr) { 612 // Lock to read ProfileData, and ensure lock is not broken by a safepoint 613 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag); 614 615 ProfileData* data = mdo->bci_to_data(bci); 616 if (data != nullptr) { 617 if (data->is_CallTypeData()) { 618 data->as_CallTypeData()->set_return_type(k->get_Klass()); 619 } else { 620 assert(data->is_VirtualCallTypeData(), "no arguments!"); 621 data->as_VirtualCallTypeData()->set_return_type(k->get_Klass()); 622 } 623 } 624 } 625 } 626 627 bool ciMethodData::has_escape_info() { 628 return eflag_set(MethodData::estimated); 629 } 630 631 void ciMethodData::set_eflag(MethodData::EscapeFlag f) { 632 set_bits(_eflags, f); 633 } 634 635 bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const { 636 return mask_bits(_eflags, f) != 0; 637 } 638 639 void ciMethodData::set_arg_local(int i) { 640 set_nth_bit(_arg_local, i); 641 } 642 643 void ciMethodData::set_arg_stack(int i) { 644 set_nth_bit(_arg_stack, i); 645 } 646 647 void ciMethodData::set_arg_returned(int i) { 648 set_nth_bit(_arg_returned, i); 649 } 650 651 void ciMethodData::set_arg_modified(int arg, uint val) { 652 ArgInfoData *aid = arg_info(); 653 if (aid == nullptr) 654 return; 655 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number"); 656 aid->set_arg_modified(arg, val); 657 } 658 659 bool ciMethodData::is_arg_local(int i) const { 660 return is_set_nth_bit(_arg_local, i); 661 } 662 663 bool ciMethodData::is_arg_stack(int i) const { 664 return is_set_nth_bit(_arg_stack, i); 665 } 666 667 bool ciMethodData::is_arg_returned(int i) const { 668 return is_set_nth_bit(_arg_returned, i); 669 } 670 671 uint ciMethodData::arg_modified(int arg) const { 672 ArgInfoData *aid = arg_info(); 673 if (aid == nullptr) 674 return 0; 675 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number"); 676 return aid->arg_modified(arg); 677 } 678 679 ciParametersTypeData* ciMethodData::parameters_type_data() const { 680 return parameter_data_size() != 0 ? new ciParametersTypeData(data_layout_at(_parameters_data_offset)) : nullptr; 681 } 682 683 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) { 684 // Get offset within MethodData* of the data array 685 ByteSize data_offset = MethodData::data_offset(); 686 687 // Get cell offset of the ProfileData within data array 688 int cell_offset = dp_to_di(data->dp()); 689 690 // Add in counter_offset, the # of bytes into the ProfileData of counter or flag 691 int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data); 692 693 return in_ByteSize(offset); 694 } 695 696 ciArgInfoData *ciMethodData::arg_info() const { 697 // Should be last, have to skip all traps. 698 DataLayout* dp = extra_data_base(); 699 DataLayout* end = args_data_limit(); 700 for (; dp < end; dp = MethodData::next_extra(dp)) { 701 if (dp->tag() == DataLayout::arg_info_data_tag) 702 return new ciArgInfoData(dp); 703 } 704 return nullptr; 705 } 706 707 708 // Implementation of the print method. 709 void ciMethodData::print_impl(outputStream* st) { 710 ciMetadata::print_impl(st); 711 } 712 713 void ciMethodData::dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k) { 714 if (k != nullptr) { 715 if (round == 0) { 716 count++; 717 } else { 718 out->print(" %d %s", (int)(dp_to_di(pdata->dp() + in_bytes(offset)) / sizeof(intptr_t)), 719 CURRENT_ENV->replay_name(k)); 720 } 721 } 722 } 723 724 template<class T> void ciMethodData::dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* vdata) { 725 for (uint i = 0; i < vdata->row_limit(); i++) { 726 dump_replay_data_type_helper(out, round, count, vdata, vdata->receiver_offset(i), vdata->receiver(i)); 727 } 728 } 729 730 template<class T> void ciMethodData::dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data) { 731 if (call_type_data->has_arguments()) { 732 for (int i = 0; i < call_type_data->number_of_arguments(); i++) { 733 dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->argument_type_offset(i), call_type_data->valid_argument_type(i)); 734 } 735 } 736 if (call_type_data->has_return()) { 737 dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->return_type_offset(), call_type_data->valid_return_type()); 738 } 739 } 740 741 void ciMethodData::dump_replay_data_extra_data_helper(outputStream* out, int round, int& count) { 742 DataLayout* dp = extra_data_base(); 743 DataLayout* end = args_data_limit(); 744 745 for (;dp < end; dp = MethodData::next_extra(dp)) { 746 switch(dp->tag()) { 747 case DataLayout::no_tag: 748 case DataLayout::arg_info_data_tag: 749 return; 750 case DataLayout::bit_data_tag: 751 break; 752 case DataLayout::speculative_trap_data_tag: { 753 ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp); 754 ciMethod* m = data->method(); 755 if (m != nullptr) { 756 if (round == 0) { 757 count++; 758 } else { 759 out->print(" %d ", (int)(dp_to_di(((address)dp) + in_bytes(ciSpeculativeTrapData::method_offset())) / sizeof(intptr_t))); 760 m->dump_name_as_ascii(out); 761 } 762 } 763 break; 764 } 765 default: 766 fatal("bad tag = %d", dp->tag()); 767 } 768 } 769 } 770 771 void ciMethodData::dump_replay_data(outputStream* out) { 772 ResourceMark rm; 773 MethodData* mdo = get_MethodData(); 774 Method* method = mdo->method(); 775 out->print("ciMethodData "); 776 ciMethod::dump_name_as_ascii(out, method); 777 out->print(" %d %d", _state, _invocation_counter); 778 779 // dump the contents of the MDO header as raw data 780 unsigned char* orig = (unsigned char*)&_orig; 781 int length = sizeof(_orig); 782 out->print(" orig %d", length); 783 for (int i = 0; i < length; i++) { 784 out->print(" %d", orig[i]); 785 } 786 787 // dump the MDO data as raw data 788 int elements = (data_size() + extra_data_size()) / sizeof(intptr_t); 789 out->print(" data %d", elements); 790 for (int i = 0; i < elements; i++) { 791 // We could use INTPTR_FORMAT here but that's zero justified 792 // which makes comparing it with the SA version of this output 793 // harder. data()'s element type is intptr_t. 794 out->print(" 0x%zx", data()[i]); 795 } 796 797 // The MDO contained oop references as ciObjects, so scan for those 798 // and emit pairs of offset and klass name so that they can be 799 // reconstructed at runtime. The first round counts the number of 800 // oop references and the second actually emits them. 801 ciParametersTypeData* parameters = parameters_type_data(); 802 for (int count = 0, round = 0; round < 2; round++) { 803 if (round == 1) out->print(" oops %d", count); 804 ProfileData* pdata = first_data(); 805 for ( ; is_valid(pdata); pdata = next_data(pdata)) { 806 if (pdata->is_VirtualCallData()) { 807 ciVirtualCallData* vdata = (ciVirtualCallData*)pdata; 808 dump_replay_data_receiver_type_helper<ciVirtualCallData>(out, round, count, vdata); 809 if (pdata->is_VirtualCallTypeData()) { 810 ciVirtualCallTypeData* call_type_data = (ciVirtualCallTypeData*)pdata; 811 dump_replay_data_call_type_helper<ciVirtualCallTypeData>(out, round, count, call_type_data); 812 } 813 } else if (pdata->is_ReceiverTypeData()) { 814 ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata; 815 dump_replay_data_receiver_type_helper<ciReceiverTypeData>(out, round, count, vdata); 816 } else if (pdata->is_CallTypeData()) { 817 ciCallTypeData* call_type_data = (ciCallTypeData*)pdata; 818 dump_replay_data_call_type_helper<ciCallTypeData>(out, round, count, call_type_data); 819 } 820 } 821 if (parameters != nullptr) { 822 for (int i = 0; i < parameters->number_of_parameters(); i++) { 823 dump_replay_data_type_helper(out, round, count, parameters, ParametersTypeData::type_offset(i), parameters->valid_parameter_type(i)); 824 } 825 } 826 } 827 for (int count = 0, round = 0; round < 2; round++) { 828 if (round == 1) out->print(" methods %d", count); 829 dump_replay_data_extra_data_helper(out, round, count); 830 } 831 out->cr(); 832 } 833 834 #ifndef PRODUCT 835 void ciMethodData::print() { 836 print_data_on(tty); 837 } 838 839 void ciMethodData::print_data_on(outputStream* st) { 840 ResourceMark rm; 841 ciParametersTypeData* parameters = parameters_type_data(); 842 if (parameters != nullptr) { 843 parameters->print_data_on(st); 844 } 845 ciProfileData* data; 846 for (data = first_data(); is_valid(data); data = next_data(data)) { 847 st->print("%d", dp_to_di(data->dp())); 848 st->fill_to(6); 849 data->print_data_on(st); 850 } 851 st->print_cr("--- Extra data:"); 852 DataLayout* dp = extra_data_base(); 853 DataLayout* end = args_data_limit(); 854 for (;; dp = MethodData::next_extra(dp)) { 855 assert(dp < end, "moved past end of extra data"); 856 switch (dp->tag()) { 857 case DataLayout::no_tag: 858 continue; 859 case DataLayout::bit_data_tag: 860 data = new BitData(dp); 861 break; 862 case DataLayout::arg_info_data_tag: 863 data = new ciArgInfoData(dp); 864 dp = end; // ArgInfoData is after the trap data right before the parameter data. 865 break; 866 case DataLayout::speculative_trap_data_tag: 867 data = new ciSpeculativeTrapData(dp); 868 break; 869 default: 870 fatal("unexpected tag %d", dp->tag()); 871 } 872 st->print("%d", dp_to_di(data->dp())); 873 st->fill_to(6); 874 data->print_data_on(st); 875 if (dp >= end) return; 876 } 877 } 878 879 void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) { 880 if (TypeEntries::is_type_none(k)) { 881 st->print("none"); 882 } else if (TypeEntries::is_type_unknown(k)) { 883 st->print("unknown"); 884 } else { 885 valid_ciklass(k)->print_name_on(st); 886 } 887 if (TypeEntries::was_null_seen(k)) { 888 st->print(" (null seen)"); 889 } 890 } 891 892 void ciTypeStackSlotEntries::print_data_on(outputStream* st) const { 893 for (int i = 0; i < number_of_entries(); i++) { 894 _pd->tab(st); 895 st->print("%d: stack (%u) ", i, stack_slot(i)); 896 print_ciklass(st, type(i)); 897 st->cr(); 898 } 899 } 900 901 void ciReturnTypeEntry::print_data_on(outputStream* st) const { 902 _pd->tab(st); 903 st->print("ret "); 904 print_ciklass(st, type()); 905 st->cr(); 906 } 907 908 void ciCallTypeData::print_data_on(outputStream* st, const char* extra) const { 909 print_shared(st, "ciCallTypeData", extra); 910 if (has_arguments()) { 911 tab(st, true); 912 st->print_cr("argument types"); 913 args()->print_data_on(st); 914 } 915 if (has_return()) { 916 tab(st, true); 917 st->print_cr("return type"); 918 ret()->print_data_on(st); 919 } 920 } 921 922 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const { 923 uint row; 924 int entries = 0; 925 for (row = 0; row < row_limit(); row++) { 926 if (receiver(row) != nullptr) entries++; 927 } 928 st->print_cr("count(%u) entries(%u)", count(), entries); 929 for (row = 0; row < row_limit(); row++) { 930 if (receiver(row) != nullptr) { 931 tab(st); 932 receiver(row)->print_name_on(st); 933 st->print_cr("(%u)", receiver_count(row)); 934 } 935 } 936 } 937 938 void ciReceiverTypeData::print_data_on(outputStream* st, const char* extra) const { 939 print_shared(st, "ciReceiverTypeData", extra); 940 print_receiver_data_on(st); 941 } 942 943 void ciVirtualCallData::print_data_on(outputStream* st, const char* extra) const { 944 print_shared(st, "ciVirtualCallData", extra); 945 rtd_super()->print_receiver_data_on(st); 946 } 947 948 void ciVirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const { 949 print_shared(st, "ciVirtualCallTypeData", extra); 950 rtd_super()->print_receiver_data_on(st); 951 if (has_arguments()) { 952 tab(st, true); 953 st->print("argument types"); 954 args()->print_data_on(st); 955 } 956 if (has_return()) { 957 tab(st, true); 958 st->print("return type"); 959 ret()->print_data_on(st); 960 } 961 } 962 963 void ciParametersTypeData::print_data_on(outputStream* st, const char* extra) const { 964 st->print_cr("ciParametersTypeData"); 965 parameters()->print_data_on(st); 966 } 967 968 void ciSpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const { 969 st->print_cr("ciSpeculativeTrapData"); 970 tab(st); 971 method()->print_short_name(st); 972 st->cr(); 973 } 974 #endif