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