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