1 /*
   2  * Copyright (c) 2000, 2026, 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 "cds/cdsConfig.hpp"
  26 #include "ci/ciMethodData.hpp"
  27 #include "classfile/systemDictionaryShared.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "compiler/compilationPolicy.hpp"
  30 #include "compiler/compilerDefinitions.inline.hpp"
  31 #include "compiler/compilerOracle.hpp"
  32 #include "interpreter/bytecode.hpp"
  33 #include "interpreter/bytecodeStream.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "memory/metaspaceClosure.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "oops/method.inline.hpp"
  39 #include "oops/methodData.inline.hpp"
  40 #include "prims/jvmtiRedefineClasses.hpp"
  41 #include "runtime/atomicAccess.hpp"
  42 #include "runtime/deoptimization.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/orderAccess.hpp"
  45 #include "runtime/safepointVerifiers.hpp"
  46 #include "runtime/signature.hpp"
  47 #include "utilities/align.hpp"
  48 #include "utilities/checkedCast.hpp"
  49 #include "utilities/copy.hpp"
  50 
  51 // ==================================================================
  52 // DataLayout
  53 //
  54 // Overlay for generic profiling data.
  55 
  56 // Some types of data layouts need a length field.
  57 bool DataLayout::needs_array_len(u1 tag) {
  58   return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag);
  59 }
  60 
  61 // Perform generic initialization of the data.  More specific
  62 // initialization occurs in overrides of ProfileData::post_initialize.
  63 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
  64   _header._bits = (intptr_t)0;
  65   _header._struct._tag = tag;
  66   _header._struct._bci = bci;
  67   for (int i = 0; i < cell_count; i++) {
  68     set_cell_at(i, (intptr_t)0);
  69   }
  70   if (needs_array_len(tag)) {
  71     set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
  72   }
  73   if (tag == call_type_data_tag) {
  74     CallTypeData::initialize(this, cell_count);
  75   } else if (tag == virtual_call_type_data_tag) {
  76     VirtualCallTypeData::initialize(this, cell_count);
  77   }
  78 }
  79 
  80 void DataLayout::clean_weak_klass_links(bool always_clean) {
  81   ResourceMark m;
  82   data_in()->clean_weak_klass_links(always_clean);
  83 }
  84 
  85 
  86 // ==================================================================
  87 // ProfileData
  88 //
  89 // A ProfileData object is created to refer to a section of profiling
  90 // data in a structured way.
  91 
  92 // Constructor for invalid ProfileData.
  93 ProfileData::ProfileData() {
  94   _data = nullptr;
  95 }
  96 
  97 char* ProfileData::print_data_on_helper(const MethodData* md) const {
  98   DataLayout* dp  = md->extra_data_base();
  99   DataLayout* end = md->args_data_limit();
 100   stringStream ss;
 101   for (;; dp = MethodData::next_extra(dp)) {
 102     assert(dp < end, "moved past end of extra data");
 103     switch(dp->tag()) {
 104     case DataLayout::speculative_trap_data_tag:
 105       if (dp->bci() == bci()) {
 106         SpeculativeTrapData* data = new SpeculativeTrapData(dp);
 107         int trap = data->trap_state();
 108         char buf[100];
 109         ss.print("trap/");
 110         data->method()->print_short_name(&ss);
 111         ss.print("(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
 112       }
 113       break;
 114     case DataLayout::bit_data_tag:
 115       break;
 116     case DataLayout::no_tag:
 117     case DataLayout::arg_info_data_tag:
 118       return ss.as_string();
 119       break;
 120     default:
 121       fatal("unexpected tag %d", dp->tag());
 122     }
 123   }
 124   return nullptr;
 125 }
 126 
 127 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
 128   print_data_on(st, print_data_on_helper(md));
 129 }
 130 
 131 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
 132   st->print("bci: %d ", bci());
 133   st->fill_to(tab_width_one + 1);
 134   st->print("%s", name);
 135   tab(st);
 136   int trap = trap_state();
 137   if (trap != 0) {
 138     char buf[100];
 139     st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
 140   }
 141   if (extra != nullptr) {
 142     st->print("%s", extra);
 143   }
 144   int flags = data()->flags();
 145   if (flags != 0) {
 146     st->print("flags(%d) %p/%d", flags, data(), in_bytes(DataLayout::flags_offset()));
 147   }
 148 }
 149 
 150 void ProfileData::tab(outputStream* st, bool first) const {
 151   st->fill_to(first ? tab_width_one : tab_width_two);
 152 }
 153 
 154 // ==================================================================
 155 // BitData
 156 //
 157 // A BitData corresponds to a one-bit flag.  This is used to indicate
 158 // whether a checkcast bytecode has seen a null value.
 159 
 160 
 161 void BitData::print_data_on(outputStream* st, const char* extra) const {
 162   print_shared(st, "BitData", extra);
 163   st->cr();
 164 }
 165 
 166 // ==================================================================
 167 // CounterData
 168 //
 169 // A CounterData corresponds to a simple counter.
 170 
 171 void CounterData::print_data_on(outputStream* st, const char* extra) const {
 172   print_shared(st, "CounterData", extra);
 173   st->print_cr("count(%u)", count());
 174 }
 175 
 176 // ==================================================================
 177 // JumpData
 178 //
 179 // A JumpData is used to access profiling information for a direct
 180 // branch.  It is a counter, used for counting the number of branches,
 181 // plus a data displacement, used for realigning the data pointer to
 182 // the corresponding target bci.
 183 
 184 void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 185   assert(stream->bci() == bci(), "wrong pos");
 186   int target;
 187   Bytecodes::Code c = stream->code();
 188   if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
 189     target = stream->dest_w();
 190   } else {
 191     target = stream->dest();
 192   }
 193   int my_di = mdo->dp_to_di(dp());
 194   int target_di = mdo->bci_to_di(target);
 195   int offset = target_di - my_di;
 196   set_displacement(offset);
 197 }
 198 
 199 void JumpData::print_data_on(outputStream* st, const char* extra) const {
 200   print_shared(st, "JumpData", extra);
 201   st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
 202 }
 203 
 204 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
 205   // Parameter profiling include the receiver
 206   int args_count = include_receiver ? 1 : 0;
 207   ResourceMark rm;
 208   ReferenceArgumentCount rac(signature);
 209   args_count += rac.count();
 210   args_count = MIN2(args_count, max);
 211   return args_count * per_arg_cell_count;
 212 }
 213 
 214 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
 215   assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
 216   assert(TypeStackSlotEntries::per_arg_count() > SingleTypeEntry::static_cell_count(), "code to test for arguments/results broken");
 217   const methodHandle m = stream->method();
 218   int bci = stream->bci();
 219   Bytecode_invoke inv(m, bci);
 220   int args_cell = 0;
 221   if (MethodData::profile_arguments_for_invoke(m, bci)) {
 222     args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
 223   }
 224   int ret_cell = 0;
 225   if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
 226     ret_cell = SingleTypeEntry::static_cell_count();
 227   }
 228   int header_cell = 0;
 229   if (args_cell + ret_cell > 0) {
 230     header_cell = header_cell_count();
 231   }
 232 
 233   return header_cell + args_cell + ret_cell;
 234 }
 235 
 236 class ArgumentOffsetComputer : public SignatureIterator {
 237 private:
 238   int _max;
 239   int _offset;
 240   GrowableArray<int> _offsets;
 241 
 242   friend class SignatureIterator;  // so do_parameters_on can call do_type
 243   void do_type(BasicType type) {
 244     if (is_reference_type(type) && _offsets.length() < _max) {
 245       _offsets.push(_offset);
 246     }
 247     _offset += parameter_type_word_count(type);
 248   }
 249 
 250  public:
 251   ArgumentOffsetComputer(Symbol* signature, int max)
 252     : SignatureIterator(signature),
 253       _max(max), _offset(0),
 254       _offsets(max) {
 255     do_parameters_on(this);  // non-virtual template execution
 256   }
 257 
 258   int off_at(int i) const { return _offsets.at(i); }
 259 };
 260 
 261 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) {
 262   ResourceMark rm;
 263   int start = 0;
 264   // Parameter profiling include the receiver
 265   if (include_receiver && has_receiver) {
 266     set_stack_slot(0, 0);
 267     set_type(0, type_none());
 268     start += 1;
 269   }
 270   ArgumentOffsetComputer aos(signature, _number_of_entries-start);
 271   for (int i = start; i < _number_of_entries; i++) {
 272     set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
 273     set_type(i, type_none());
 274   }
 275 }
 276 
 277 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 278   assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
 279   Bytecode_invoke inv(stream->method(), stream->bci());
 280 
 281   if (has_arguments()) {
 282 #ifdef ASSERT
 283     ResourceMark rm;
 284     ReferenceArgumentCount rac(inv.signature());
 285     int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
 286     assert(count > 0, "room for args type but none found?");
 287     check_number_of_arguments(count);
 288 #endif
 289     _args.post_initialize(inv.signature(), inv.has_receiver(), false);
 290   }
 291 
 292   if (has_return()) {
 293     assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
 294     _ret.post_initialize();
 295   }
 296 }
 297 
 298 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 299   assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
 300   Bytecode_invoke inv(stream->method(), stream->bci());
 301 
 302   if (has_arguments()) {
 303 #ifdef ASSERT
 304     ResourceMark rm;
 305     ReferenceArgumentCount rac(inv.signature());
 306     int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
 307     assert(count > 0, "room for args type but none found?");
 308     check_number_of_arguments(count);
 309 #endif
 310     _args.post_initialize(inv.signature(), inv.has_receiver(), false);
 311   }
 312 
 313   if (has_return()) {
 314     assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
 315     _ret.post_initialize();
 316   }
 317 }
 318 
 319 static bool is_excluded(Klass* k) {
 320 #if INCLUDE_CDS
 321   if (CDSConfig::is_at_aot_safepoint()) {
 322     // Check for CDS exclusion only at CDS safe point.
 323     if (k->is_instance_klass() && !InstanceKlass::cast(k)->is_loaded()) {
 324       log_debug(aot, training)("Purged %s from MDO: unloaded class", k->name()->as_C_string());
 325       return true;
 326     } else {
 327       bool excluded = SystemDictionaryShared::should_be_excluded(k) || !SystemDictionaryShared::is_builtin_loader(k->class_loader_data());
 328       if (excluded) {
 329         log_debug(aot, training)("Purged %s from MDO: excluded class", k->name()->as_C_string());
 330       }
 331       return excluded;
 332     }
 333   }
 334 #endif
 335   return false;
 336 }
 337 
 338 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
 339   for (int i = 0; i < _number_of_entries; i++) {
 340     intptr_t p = type(i);
 341     Klass* k = (Klass*)klass_part(p);
 342     if (k != nullptr) {
 343       if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
 344         continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 345       }
 346       if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
 347         set_type(i, with_status((Klass*)nullptr, p));
 348       }
 349     }
 350   }
 351 }
 352 
 353 void TypeStackSlotEntries::metaspace_pointers_do(MetaspaceClosure* it) {
 354   for (int i = 0; i < _number_of_entries; i++) {
 355     Klass** k = (Klass**)type_adr(i); // tagged
 356     it->push(k);
 357   }
 358 }
 359 
 360 void SingleTypeEntry::clean_weak_klass_links(bool always_clean) {
 361   intptr_t p = type();
 362   Klass* k = (Klass*)klass_part(p);
 363   if (k != nullptr) {
 364     if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
 365       return; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 366     }
 367     if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
 368       set_type(with_status((Klass*)nullptr, p));
 369     }
 370   }
 371 }
 372 
 373 void SingleTypeEntry::metaspace_pointers_do(MetaspaceClosure* it) {
 374   Klass** k = (Klass**)type_adr(); // tagged
 375   it->push(k);
 376 }
 377 
 378 bool TypeEntriesAtCall::return_profiling_enabled() {
 379   return MethodData::profile_return();
 380 }
 381 
 382 bool TypeEntriesAtCall::arguments_profiling_enabled() {
 383   return MethodData::profile_arguments();
 384 }
 385 
 386 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
 387   if (is_type_none(k)) {
 388     st->print("none");
 389   } else if (is_type_unknown(k)) {
 390     st->print("unknown");
 391   } else {
 392     valid_klass(k)->print_value_on(st);
 393   }
 394   if (was_null_seen(k)) {
 395     st->print(" (null seen)");
 396   }
 397 }
 398 
 399 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
 400   for (int i = 0; i < _number_of_entries; i++) {
 401     _pd->tab(st);
 402     st->print("%d: stack(%u) ", i, stack_slot(i));
 403     print_klass(st, type(i));
 404     st->cr();
 405   }
 406 }
 407 
 408 void SingleTypeEntry::print_data_on(outputStream* st) const {
 409   _pd->tab(st);
 410   print_klass(st, type());
 411   st->cr();
 412 }
 413 
 414 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
 415   CounterData::print_data_on(st, extra);
 416   if (has_arguments()) {
 417     tab(st, true);
 418     st->print("argument types");
 419     _args.print_data_on(st);
 420   }
 421   if (has_return()) {
 422     tab(st, true);
 423     st->print("return type");
 424     _ret.print_data_on(st);
 425   }
 426 }
 427 
 428 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
 429   VirtualCallData::print_data_on(st, extra);
 430   if (has_arguments()) {
 431     tab(st, true);
 432     st->print("argument types");
 433     _args.print_data_on(st);
 434   }
 435   if (has_return()) {
 436     tab(st, true);
 437     st->print("return type");
 438     _ret.print_data_on(st);
 439   }
 440 }
 441 
 442 // ==================================================================
 443 // ReceiverTypeData
 444 //
 445 // A ReceiverTypeData is used to access profiling information about a
 446 // dynamic type check.  It consists of a counter which counts the total times
 447 // that the check is reached, and a series of (Klass*, count) pairs
 448 // which are used to store a type profile for the receiver of the check.
 449 
 450 void ReceiverTypeData::clean_weak_klass_links(bool always_clean) {
 451     for (uint row = 0; row < row_limit(); row++) {
 452     Klass* p = receiver(row);
 453     if (p != nullptr) {
 454       if (!always_clean && p->is_instance_klass() && InstanceKlass::cast(p)->is_not_initialized()) {
 455         continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
 456       }
 457       if (always_clean || !p->is_loader_present_and_alive() || is_excluded(p)) {
 458         clear_row(row);
 459       }
 460     }
 461   }
 462 }
 463 
 464 void ReceiverTypeData::metaspace_pointers_do(MetaspaceClosure *it) {
 465   for (uint row = 0; row < row_limit(); row++) {
 466     Klass** recv = (Klass**)intptr_at_adr(receiver_cell_index(row));
 467     it->push(recv);
 468   }
 469 }
 470 
 471 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
 472   uint row;
 473   int entries = 0;
 474   for (row = 0; row < row_limit(); row++) {
 475     if (receiver(row) != nullptr)  entries++;
 476   }
 477   st->print_cr("count(%u) entries(%u)", count(), entries);
 478   int total = count();
 479   for (row = 0; row < row_limit(); row++) {
 480     if (receiver(row) != nullptr) {
 481       total += receiver_count(row);
 482     }
 483   }
 484   for (row = 0; row < row_limit(); row++) {
 485     if (receiver(row) != nullptr) {
 486       tab(st);
 487       receiver(row)->print_value_on(st);
 488       st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
 489     }
 490   }
 491 }
 492 void ReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
 493   print_shared(st, "ReceiverTypeData", extra);
 494   print_receiver_data_on(st);
 495 }
 496 
 497 void VirtualCallData::print_data_on(outputStream* st, const char* extra) const {
 498   print_shared(st, "VirtualCallData", extra);
 499   print_receiver_data_on(st);
 500 }
 501 
 502 // ==================================================================
 503 // RetData
 504 //
 505 // A RetData is used to access profiling information for a ret bytecode.
 506 // It is composed of a count of the number of times that the ret has
 507 // been executed, followed by a series of triples of the form
 508 // (bci, count, di) which count the number of times that some bci was the
 509 // target of the ret and cache a corresponding displacement.
 510 
 511 void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 512   for (uint row = 0; row < row_limit(); row++) {
 513     set_bci_displacement(row, -1);
 514     set_bci(row, no_bci);
 515   }
 516   // release so other threads see a consistent state.  bci is used as
 517   // a valid flag for bci_displacement.
 518   OrderAccess::release();
 519 }
 520 
 521 // This routine needs to atomically update the RetData structure, so the
 522 // caller needs to hold the RetData_lock before it gets here.  Since taking
 523 // the lock can block (and allow GC) and since RetData is a ProfileData is a
 524 // wrapper around a derived oop, taking the lock in _this_ method will
 525 // basically cause the 'this' pointer's _data field to contain junk after the
 526 // lock.  We require the caller to take the lock before making the ProfileData
 527 // structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
 528 address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
 529   // First find the mdp which corresponds to the return bci.
 530   address mdp = h_mdo->bci_to_dp(return_bci);
 531 
 532   // Now check to see if any of the cache slots are open.
 533   for (uint row = 0; row < row_limit(); row++) {
 534     if (bci(row) == no_bci) {
 535       set_bci_displacement(row, checked_cast<int>(mdp - dp()));
 536       set_bci_count(row, DataLayout::counter_increment);
 537       // Barrier to ensure displacement is written before the bci; allows
 538       // the interpreter to read displacement without fear of race condition.
 539       release_set_bci(row, return_bci);
 540       break;
 541     }
 542   }
 543   return mdp;
 544 }
 545 
 546 void RetData::print_data_on(outputStream* st, const char* extra) const {
 547   print_shared(st, "RetData", extra);
 548   uint row;
 549   int entries = 0;
 550   for (row = 0; row < row_limit(); row++) {
 551     if (bci(row) != no_bci)  entries++;
 552   }
 553   st->print_cr("count(%u) entries(%u)", count(), entries);
 554   for (row = 0; row < row_limit(); row++) {
 555     if (bci(row) != no_bci) {
 556       tab(st);
 557       st->print_cr("bci(%d: count(%u) displacement(%d))",
 558                    bci(row), bci_count(row), bci_displacement(row));
 559     }
 560   }
 561 }
 562 
 563 // ==================================================================
 564 // BranchData
 565 //
 566 // A BranchData is used to access profiling data for a two-way branch.
 567 // It consists of taken and not_taken counts as well as a data displacement
 568 // for the taken case.
 569 
 570 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 571   assert(stream->bci() == bci(), "wrong pos");
 572   int target = stream->dest();
 573   int my_di = mdo->dp_to_di(dp());
 574   int target_di = mdo->bci_to_di(target);
 575   int offset = target_di - my_di;
 576   set_displacement(offset);
 577 }
 578 
 579 void BranchData::print_data_on(outputStream* st, const char* extra) const {
 580   print_shared(st, "BranchData", extra);
 581   if (data()->flags()) {
 582     st->cr();
 583     tab(st);
 584   }
 585   st->print_cr("taken(%u) displacement(%d)",
 586                taken(), displacement());
 587   tab(st);
 588   st->print_cr("not taken(%u)", not_taken());
 589 }
 590 
 591 // ==================================================================
 592 // MultiBranchData
 593 //
 594 // A MultiBranchData is used to access profiling information for
 595 // a multi-way branch (*switch bytecodes).  It consists of a series
 596 // of (count, displacement) pairs, which count the number of times each
 597 // case was taken and specify the data displacement for each branch target.
 598 
 599 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
 600   int cell_count = 0;
 601   if (stream->code() == Bytecodes::_tableswitch) {
 602     Bytecode_tableswitch sw(stream->method()(), stream->bcp());
 603     cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
 604   } else {
 605     Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
 606     cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
 607   }
 608   return cell_count;
 609 }
 610 
 611 void MultiBranchData::post_initialize(BytecodeStream* stream,
 612                                       MethodData* mdo) {
 613   assert(stream->bci() == bci(), "wrong pos");
 614   int target;
 615   int my_di;
 616   int target_di;
 617   int offset;
 618   if (stream->code() == Bytecodes::_tableswitch) {
 619     Bytecode_tableswitch sw(stream->method()(), stream->bcp());
 620     int len = sw.length();
 621     assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
 622     for (int count = 0; count < len; count++) {
 623       target = sw.dest_offset_at(count) + bci();
 624       my_di = mdo->dp_to_di(dp());
 625       target_di = mdo->bci_to_di(target);
 626       offset = target_di - my_di;
 627       set_displacement_at(count, offset);
 628     }
 629     target = sw.default_offset() + bci();
 630     my_di = mdo->dp_to_di(dp());
 631     target_di = mdo->bci_to_di(target);
 632     offset = target_di - my_di;
 633     set_default_displacement(offset);
 634 
 635   } else {
 636     Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
 637     int npairs = sw.number_of_pairs();
 638     assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
 639     for (int count = 0; count < npairs; count++) {
 640       LookupswitchPair pair = sw.pair_at(count);
 641       target = pair.offset() + bci();
 642       my_di = mdo->dp_to_di(dp());
 643       target_di = mdo->bci_to_di(target);
 644       offset = target_di - my_di;
 645       set_displacement_at(count, offset);
 646     }
 647     target = sw.default_offset() + bci();
 648     my_di = mdo->dp_to_di(dp());
 649     target_di = mdo->bci_to_di(target);
 650     offset = target_di - my_di;
 651     set_default_displacement(offset);
 652   }
 653 }
 654 
 655 void MultiBranchData::print_data_on(outputStream* st, const char* extra) const {
 656   print_shared(st, "MultiBranchData", extra);
 657   st->print_cr("default_count(%u) displacement(%d)",
 658                default_count(), default_displacement());
 659   int cases = number_of_cases();
 660   for (int i = 0; i < cases; i++) {
 661     tab(st);
 662     st->print_cr("count(%u) displacement(%d)",
 663                  count_at(i), displacement_at(i));
 664   }
 665 }
 666 
 667 void ArgInfoData::print_data_on(outputStream* st, const char* extra) const {
 668   print_shared(st, "ArgInfoData", extra);
 669   int args_size = size_of_args();
 670   for (int i = 0; i < args_size; i++) {
 671     st->print("  0x%x", arg_modified(i));
 672   }
 673   st->cr();
 674 }
 675 
 676 int ParametersTypeData::compute_cell_count(Method* m) {
 677   if (!MethodData::profile_parameters_for_method(methodHandle(Thread::current(), m))) {
 678     return 0;
 679   }
 680   int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit;
 681   int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max);
 682   if (obj_args > 0) {
 683     return obj_args + 1; // 1 cell for array len
 684   }
 685   return 0;
 686 }
 687 
 688 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
 689   _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
 690 }
 691 
 692 bool ParametersTypeData::profiling_enabled() {
 693   return MethodData::profile_parameters();
 694 }
 695 
 696 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
 697   print_shared(st, "ParametersTypeData", extra);
 698   tab(st);
 699   _parameters.print_data_on(st);
 700   st->cr();
 701 }
 702 
 703 void SpeculativeTrapData::metaspace_pointers_do(MetaspaceClosure* it) {
 704   Method** m = (Method**)intptr_at_adr(speculative_trap_method);
 705   it->push(m);
 706 }
 707 
 708 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
 709   print_shared(st, "SpeculativeTrapData", extra);
 710   tab(st);
 711   method()->print_short_name(st);
 712   st->cr();
 713 }
 714 
 715 void ArrayStoreData::print_data_on(outputStream* st, const char* extra) const {
 716   print_shared(st, "ArrayStore", extra);
 717   st->cr();
 718   tab(st, true);
 719   st->print("array");
 720   _array.print_data_on(st);
 721   tab(st, true);
 722   st->print("element");
 723   if (null_seen()) {
 724     st->print(" (null seen)");
 725   }
 726   tab(st);
 727   print_receiver_data_on(st);
 728 }
 729 
 730 void ArrayLoadData::print_data_on(outputStream* st, const char* extra) const {
 731   print_shared(st, "ArrayLoad", extra);
 732   st->cr();
 733   tab(st, true);
 734   st->print("array");
 735   _array.print_data_on(st);
 736   tab(st, true);
 737   st->print("element");
 738   _element.print_data_on(st);
 739 }
 740 
 741 void ACmpData::print_data_on(outputStream* st, const char* extra) const {
 742   BranchData::print_data_on(st, extra);
 743   tab(st, true);
 744   st->print("left");
 745   _left.print_data_on(st);
 746   tab(st, true);
 747   st->print("right");
 748   _right.print_data_on(st);
 749 }
 750 
 751 // ==================================================================
 752 // MethodData*
 753 //
 754 // A MethodData* holds information which has been collected about
 755 // a method.
 756 
 757 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
 758   assert(!THREAD->owns_locks(), "Should not own any locks");
 759   int size = MethodData::compute_allocation_size_in_words(method);
 760 
 761   return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
 762     MethodData(method);
 763 }
 764 
 765 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
 766   switch (code) {
 767   case Bytecodes::_checkcast:
 768   case Bytecodes::_instanceof:
 769     if (TypeProfileCasts) {
 770       return ReceiverTypeData::static_cell_count();
 771     } else {
 772       return BitData::static_cell_count();
 773     }
 774   case Bytecodes::_aaload:
 775     return ArrayLoadData::static_cell_count();
 776   case Bytecodes::_aastore:
 777     return ArrayStoreData::static_cell_count();
 778   case Bytecodes::_invokespecial:
 779   case Bytecodes::_invokestatic:
 780     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 781       return variable_cell_count;
 782     } else {
 783       return CounterData::static_cell_count();
 784     }
 785   case Bytecodes::_goto:
 786   case Bytecodes::_goto_w:
 787   case Bytecodes::_jsr:
 788   case Bytecodes::_jsr_w:
 789     return JumpData::static_cell_count();
 790   case Bytecodes::_invokevirtual:
 791   case Bytecodes::_invokeinterface:
 792     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 793       return variable_cell_count;
 794     } else {
 795       return VirtualCallData::static_cell_count();
 796     }
 797   case Bytecodes::_invokedynamic:
 798     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 799       return variable_cell_count;
 800     } else {
 801       return CounterData::static_cell_count();
 802     }
 803   case Bytecodes::_ret:
 804     return RetData::static_cell_count();
 805   case Bytecodes::_ifeq:
 806   case Bytecodes::_ifne:
 807   case Bytecodes::_iflt:
 808   case Bytecodes::_ifge:
 809   case Bytecodes::_ifgt:
 810   case Bytecodes::_ifle:
 811   case Bytecodes::_if_icmpeq:
 812   case Bytecodes::_if_icmpne:
 813   case Bytecodes::_if_icmplt:
 814   case Bytecodes::_if_icmpge:
 815   case Bytecodes::_if_icmpgt:
 816   case Bytecodes::_if_icmple:
 817   case Bytecodes::_ifnull:
 818   case Bytecodes::_ifnonnull:
 819     return BranchData::static_cell_count();
 820   case Bytecodes::_if_acmpne:
 821   case Bytecodes::_if_acmpeq:
 822     return ACmpData::static_cell_count();
 823   case Bytecodes::_lookupswitch:
 824   case Bytecodes::_tableswitch:
 825     return variable_cell_count;
 826   default:
 827     return no_profile_data;
 828   }
 829 }
 830 
 831 // Compute the size of the profiling information corresponding to
 832 // the current bytecode.
 833 int MethodData::compute_data_size(BytecodeStream* stream) {
 834   int cell_count = bytecode_cell_count(stream->code());
 835   if (cell_count == no_profile_data) {
 836     return 0;
 837   }
 838   if (cell_count == variable_cell_count) {
 839     switch (stream->code()) {
 840     case Bytecodes::_lookupswitch:
 841     case Bytecodes::_tableswitch:
 842       cell_count = MultiBranchData::compute_cell_count(stream);
 843       break;
 844     case Bytecodes::_invokespecial:
 845     case Bytecodes::_invokestatic:
 846     case Bytecodes::_invokedynamic:
 847       assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
 848       if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
 849           profile_return_for_invoke(stream->method(), stream->bci())) {
 850         cell_count = CallTypeData::compute_cell_count(stream);
 851       } else {
 852         cell_count = CounterData::static_cell_count();
 853       }
 854       break;
 855     case Bytecodes::_invokevirtual:
 856     case Bytecodes::_invokeinterface: {
 857       assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
 858       if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
 859           profile_return_for_invoke(stream->method(), stream->bci())) {
 860         cell_count = VirtualCallTypeData::compute_cell_count(stream);
 861       } else {
 862         cell_count = VirtualCallData::static_cell_count();
 863       }
 864       break;
 865     }
 866     default:
 867       fatal("unexpected bytecode for var length profile data");
 868     }
 869   }
 870   // Note:  cell_count might be zero, meaning that there is just
 871   //        a DataLayout header, with no extra cells.
 872   assert(cell_count >= 0, "sanity");
 873   return DataLayout::compute_size_in_bytes(cell_count);
 874 }
 875 
 876 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
 877   // Bytecodes for which we may use speculation
 878   switch (code) {
 879   case Bytecodes::_checkcast:
 880   case Bytecodes::_instanceof:
 881   case Bytecodes::_aaload:
 882   case Bytecodes::_aastore:
 883   case Bytecodes::_invokevirtual:
 884   case Bytecodes::_invokeinterface:
 885   case Bytecodes::_if_acmpeq:
 886   case Bytecodes::_if_acmpne:
 887   case Bytecodes::_ifnull:
 888   case Bytecodes::_ifnonnull:
 889   case Bytecodes::_invokestatic:
 890 #ifdef COMPILER2
 891     if (CompilerConfig::is_c2_enabled()) {
 892       return UseTypeSpeculation;
 893     }
 894 #endif
 895   default:
 896     return false;
 897   }
 898   return false;
 899 }
 900 
 901 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) {
 902   if (ProfileTraps) {
 903     // Assume that up to 3% of BCIs with no MDP will need to allocate one.
 904     int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
 905     // If the method is large, let the extra BCIs grow numerous (to ~1%).
 906     int one_percent_of_data
 907       = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
 908     if (extra_data_count < one_percent_of_data)
 909       extra_data_count = one_percent_of_data;
 910     if (extra_data_count > empty_bc_count)
 911       extra_data_count = empty_bc_count;  // no need for more
 912 
 913     // Make sure we have a minimum number of extra data slots to
 914     // allocate SpeculativeTrapData entries. We would want to have one
 915     // entry per compilation that inlines this method and for which
 916     // some type speculation assumption fails. So the room we need for
 917     // the SpeculativeTrapData entries doesn't directly depend on the
 918     // size of the method. Because it's hard to estimate, we reserve
 919     // space for an arbitrary number of entries.
 920     int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
 921       (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
 922 
 923     return MAX2(extra_data_count, spec_data_count);
 924   } else {
 925     return 0;
 926   }
 927 }
 928 
 929 // Compute the size of the MethodData* necessary to store
 930 // profiling information about a given method.  Size is in bytes.
 931 int MethodData::compute_allocation_size_in_bytes(const methodHandle& method) {
 932   int data_size = 0;
 933   BytecodeStream stream(method);
 934   Bytecodes::Code c;
 935   int empty_bc_count = 0;  // number of bytecodes lacking data
 936   bool needs_speculative_traps = false;
 937   while ((c = stream.next()) >= 0) {
 938     int size_in_bytes = compute_data_size(&stream);
 939     data_size += size_in_bytes;
 940     if (size_in_bytes == 0) {
 941       empty_bc_count += 1;
 942     }
 943     needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
 944   }
 945   int object_size = in_bytes(data_offset()) + data_size;
 946 
 947   // Add some extra DataLayout cells (at least one) to track stray traps.
 948   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
 949   object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
 950 
 951   // Add a cell to record information about modified arguments.
 952   int arg_size = method->size_of_parameters();
 953   object_size += DataLayout::compute_size_in_bytes(arg_size+1);
 954 
 955   // Reserve room for an area of the MDO dedicated to profiling of
 956   // parameters
 957   int args_cell = ParametersTypeData::compute_cell_count(method());
 958   if (args_cell > 0) {
 959     object_size += DataLayout::compute_size_in_bytes(args_cell);
 960   }
 961 
 962   if (ProfileExceptionHandlers && method()->has_exception_handler()) {
 963     int num_exception_handlers = method()->exception_table_length();
 964     object_size += num_exception_handlers * single_exception_handler_data_size();
 965   }
 966 
 967   return object_size;
 968 }
 969 
 970 // Compute the size of the MethodData* necessary to store
 971 // profiling information about a given method.  Size is in words
 972 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
 973   int byte_size = compute_allocation_size_in_bytes(method);
 974   int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
 975   return align_metadata_size(word_size);
 976 }
 977 
 978 // Initialize an individual data segment.  Returns the size of
 979 // the segment in bytes.
 980 int MethodData::initialize_data(BytecodeStream* stream,
 981                                        int data_index) {
 982   int cell_count = -1;
 983   u1 tag = DataLayout::no_tag;
 984   DataLayout* data_layout = data_layout_at(data_index);
 985   Bytecodes::Code c = stream->code();
 986   switch (c) {
 987   case Bytecodes::_checkcast:
 988   case Bytecodes::_instanceof:
 989     if (TypeProfileCasts) {
 990       cell_count = ReceiverTypeData::static_cell_count();
 991       tag = DataLayout::receiver_type_data_tag;
 992     } else {
 993       cell_count = BitData::static_cell_count();
 994       tag = DataLayout::bit_data_tag;
 995     }
 996     break;
 997   case Bytecodes::_aaload:
 998     cell_count = ArrayLoadData::static_cell_count();
 999     tag = DataLayout::array_load_data_tag;
1000     break;
1001   case Bytecodes::_aastore:
1002     cell_count = ArrayStoreData::static_cell_count();
1003     tag = DataLayout::array_store_data_tag;
1004     break;
1005   case Bytecodes::_invokespecial:
1006   case Bytecodes::_invokestatic: {
1007     int counter_data_cell_count = CounterData::static_cell_count();
1008     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1009         profile_return_for_invoke(stream->method(), stream->bci())) {
1010       cell_count = CallTypeData::compute_cell_count(stream);
1011     } else {
1012       cell_count = counter_data_cell_count;
1013     }
1014     if (cell_count > counter_data_cell_count) {
1015       tag = DataLayout::call_type_data_tag;
1016     } else {
1017       tag = DataLayout::counter_data_tag;
1018     }
1019     break;
1020   }
1021   case Bytecodes::_goto:
1022   case Bytecodes::_goto_w:
1023   case Bytecodes::_jsr:
1024   case Bytecodes::_jsr_w:
1025     cell_count = JumpData::static_cell_count();
1026     tag = DataLayout::jump_data_tag;
1027     break;
1028   case Bytecodes::_invokevirtual:
1029   case Bytecodes::_invokeinterface: {
1030     int virtual_call_data_cell_count = VirtualCallData::static_cell_count();
1031     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1032         profile_return_for_invoke(stream->method(), stream->bci())) {
1033       cell_count = VirtualCallTypeData::compute_cell_count(stream);
1034     } else {
1035       cell_count = virtual_call_data_cell_count;
1036     }
1037     if (cell_count > virtual_call_data_cell_count) {
1038       tag = DataLayout::virtual_call_type_data_tag;
1039     } else {
1040       tag = DataLayout::virtual_call_data_tag;
1041     }
1042     break;
1043   }
1044   case Bytecodes::_invokedynamic: {
1045     // %%% should make a type profile for any invokedynamic that takes a ref argument
1046     int counter_data_cell_count = CounterData::static_cell_count();
1047     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1048         profile_return_for_invoke(stream->method(), stream->bci())) {
1049       cell_count = CallTypeData::compute_cell_count(stream);
1050     } else {
1051       cell_count = counter_data_cell_count;
1052     }
1053     if (cell_count > counter_data_cell_count) {
1054       tag = DataLayout::call_type_data_tag;
1055     } else {
1056       tag = DataLayout::counter_data_tag;
1057     }
1058     break;
1059   }
1060   case Bytecodes::_ret:
1061     cell_count = RetData::static_cell_count();
1062     tag = DataLayout::ret_data_tag;
1063     break;
1064   case Bytecodes::_ifeq:
1065   case Bytecodes::_ifne:
1066   case Bytecodes::_iflt:
1067   case Bytecodes::_ifge:
1068   case Bytecodes::_ifgt:
1069   case Bytecodes::_ifle:
1070   case Bytecodes::_if_icmpeq:
1071   case Bytecodes::_if_icmpne:
1072   case Bytecodes::_if_icmplt:
1073   case Bytecodes::_if_icmpge:
1074   case Bytecodes::_if_icmpgt:
1075   case Bytecodes::_if_icmple:
1076   case Bytecodes::_ifnull:
1077   case Bytecodes::_ifnonnull:
1078     cell_count = BranchData::static_cell_count();
1079     tag = DataLayout::branch_data_tag;
1080     break;
1081   case Bytecodes::_if_acmpeq:
1082   case Bytecodes::_if_acmpne:
1083     cell_count = ACmpData::static_cell_count();
1084     tag = DataLayout::acmp_data_tag;
1085     break;
1086   case Bytecodes::_lookupswitch:
1087   case Bytecodes::_tableswitch:
1088     cell_count = MultiBranchData::compute_cell_count(stream);
1089     tag = DataLayout::multi_branch_data_tag;
1090     break;
1091   default:
1092     break;
1093   }
1094   assert(tag == DataLayout::multi_branch_data_tag ||
1095          ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1096           (tag == DataLayout::call_type_data_tag ||
1097            tag == DataLayout::counter_data_tag ||
1098            tag == DataLayout::virtual_call_type_data_tag ||
1099            tag == DataLayout::virtual_call_data_tag)) ||
1100          cell_count == bytecode_cell_count(c), "cell counts must agree");
1101   if (cell_count >= 0) {
1102     assert(tag != DataLayout::no_tag, "bad tag");
1103     assert(bytecode_has_profile(c), "agree w/ BHP");
1104     data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1105     return DataLayout::compute_size_in_bytes(cell_count);
1106   } else {
1107     assert(!bytecode_has_profile(c), "agree w/ !BHP");
1108     return 0;
1109   }
1110 }
1111 
1112 // Get the data at an arbitrary (sort of) data index.
1113 ProfileData* MethodData::data_at(int data_index) const {
1114   if (out_of_bounds(data_index)) {
1115     return nullptr;
1116   }
1117   DataLayout* data_layout = data_layout_at(data_index);
1118   return data_layout->data_in();
1119 }
1120 
1121 int DataLayout::cell_count() {
1122   switch (tag()) {
1123   case DataLayout::no_tag:
1124   default:
1125     ShouldNotReachHere();
1126     return 0;
1127   case DataLayout::bit_data_tag:
1128     return BitData::static_cell_count();
1129   case DataLayout::counter_data_tag:
1130     return CounterData::static_cell_count();
1131   case DataLayout::jump_data_tag:
1132     return JumpData::static_cell_count();
1133   case DataLayout::receiver_type_data_tag:
1134     return ReceiverTypeData::static_cell_count();
1135   case DataLayout::virtual_call_data_tag:
1136     return VirtualCallData::static_cell_count();
1137   case DataLayout::ret_data_tag:
1138     return RetData::static_cell_count();
1139   case DataLayout::branch_data_tag:
1140     return BranchData::static_cell_count();
1141   case DataLayout::multi_branch_data_tag:
1142     return ((new MultiBranchData(this))->cell_count());
1143   case DataLayout::arg_info_data_tag:
1144     return ((new ArgInfoData(this))->cell_count());
1145   case DataLayout::call_type_data_tag:
1146     return ((new CallTypeData(this))->cell_count());
1147   case DataLayout::virtual_call_type_data_tag:
1148     return ((new VirtualCallTypeData(this))->cell_count());
1149   case DataLayout::parameters_type_data_tag:
1150     return ((new ParametersTypeData(this))->cell_count());
1151   case DataLayout::speculative_trap_data_tag:
1152     return SpeculativeTrapData::static_cell_count();
1153   case DataLayout::array_store_data_tag:
1154     return ((new ArrayStoreData(this))->cell_count());
1155   case DataLayout::array_load_data_tag:
1156     return ((new ArrayLoadData(this))->cell_count());
1157   case DataLayout::acmp_data_tag:
1158     return ((new ACmpData(this))->cell_count());
1159   }
1160 }
1161 ProfileData* DataLayout::data_in() {
1162   switch (tag()) {
1163   case DataLayout::no_tag:
1164   default:
1165     ShouldNotReachHere();
1166     return nullptr;
1167   case DataLayout::bit_data_tag:
1168     return new BitData(this);
1169   case DataLayout::counter_data_tag:
1170     return new CounterData(this);
1171   case DataLayout::jump_data_tag:
1172     return new JumpData(this);
1173   case DataLayout::receiver_type_data_tag:
1174     return new ReceiverTypeData(this);
1175   case DataLayout::virtual_call_data_tag:
1176     return new VirtualCallData(this);
1177   case DataLayout::ret_data_tag:
1178     return new RetData(this);
1179   case DataLayout::branch_data_tag:
1180     return new BranchData(this);
1181   case DataLayout::multi_branch_data_tag:
1182     return new MultiBranchData(this);
1183   case DataLayout::arg_info_data_tag:
1184     return new ArgInfoData(this);
1185   case DataLayout::call_type_data_tag:
1186     return new CallTypeData(this);
1187   case DataLayout::virtual_call_type_data_tag:
1188     return new VirtualCallTypeData(this);
1189   case DataLayout::parameters_type_data_tag:
1190     return new ParametersTypeData(this);
1191   case DataLayout::speculative_trap_data_tag:
1192     return new SpeculativeTrapData(this);
1193   case DataLayout::array_store_data_tag:
1194     return new ArrayStoreData(this);
1195   case DataLayout::array_load_data_tag:
1196     return new ArrayLoadData(this);
1197   case DataLayout::acmp_data_tag:
1198     return new ACmpData(this);
1199   }
1200 }
1201 
1202 // Iteration over data.
1203 ProfileData* MethodData::next_data(ProfileData* current) const {
1204   int current_index = dp_to_di(current->dp());
1205   int next_index = current_index + current->size_in_bytes();
1206   ProfileData* next = data_at(next_index);
1207   return next;
1208 }
1209 
1210 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1211   int current_index = dp_to_di((address)current);
1212   int next_index = current_index + current->size_in_bytes();
1213   if (out_of_bounds(next_index)) {
1214     return nullptr;
1215   }
1216   DataLayout* next = data_layout_at(next_index);
1217   return next;
1218 }
1219 
1220 // Give each of the data entries a chance to perform specific
1221 // data initialization.
1222 void MethodData::post_initialize(BytecodeStream* stream) {
1223   ResourceMark rm;
1224   ProfileData* data;
1225   for (data = first_data(); is_valid(data); data = next_data(data)) {
1226     stream->set_start(data->bci());
1227     stream->next();
1228     data->post_initialize(stream, this);
1229   }
1230   if (_parameters_type_data_di != no_parameters) {
1231     parameters_type_data()->post_initialize(nullptr, this);
1232   }
1233 }
1234 
1235 // Initialize the MethodData* corresponding to a given method.
1236 MethodData::MethodData(const methodHandle& method)
1237   : _method(method()),
1238     // Holds Compile_lock
1239     _compiler_counters(),
1240     _parameters_type_data_di(parameters_uninitialized) {
1241     _extra_data_lock = nullptr;
1242     initialize();
1243 }
1244 
1245 #if INCLUDE_CDS
1246 MethodData::MethodData() {
1247   // Used by cppVtables.cpp only
1248   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
1249 }
1250 #endif
1251 
1252 void MethodData::initialize() {
1253   Thread* thread = Thread::current();
1254   NoSafepointVerifier no_safepoint;  // init function atomic wrt GC
1255   ResourceMark rm(thread);
1256 
1257   init();
1258 
1259   // Go through the bytecodes and allocate and initialize the
1260   // corresponding data cells.
1261   int data_size = 0;
1262   int empty_bc_count = 0;  // number of bytecodes lacking data
1263   _data[0] = 0;  // apparently not set below.
1264   BytecodeStream stream(methodHandle(thread, method()));
1265   Bytecodes::Code c;
1266   bool needs_speculative_traps = false;
1267   while ((c = stream.next()) >= 0) {
1268     int size_in_bytes = initialize_data(&stream, data_size);
1269     data_size += size_in_bytes;
1270     if (size_in_bytes == 0) {
1271       empty_bc_count += 1;
1272     }
1273     needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
1274   }
1275   _data_size = data_size;
1276   int object_size = in_bytes(data_offset()) + data_size;
1277 
1278   // Add some extra DataLayout cells (at least one) to track stray traps.
1279   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
1280   int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
1281 
1282   // Let's zero the space for the extra data
1283   if (extra_size > 0) {
1284     Copy::zero_to_bytes(((address)_data) + data_size, extra_size);
1285   }
1286 
1287   // Add a cell to record information about modified arguments.
1288   // Set up _args_modified array after traps cells so that
1289   // the code for traps cells works.
1290   DataLayout *dp = data_layout_at(data_size + extra_size);
1291 
1292   int arg_size = method()->size_of_parameters();
1293   dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
1294 
1295   int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1);
1296   object_size += extra_size + arg_data_size;
1297 
1298   int parms_cell = ParametersTypeData::compute_cell_count(method());
1299   // If we are profiling parameters, we reserved an area near the end
1300   // of the MDO after the slots for bytecodes (because there's no bci
1301   // for method entry so they don't fit with the framework for the
1302   // profiling of bytecodes). We store the offset within the MDO of
1303   // this area (or -1 if no parameter is profiled)
1304   int parm_data_size = 0;
1305   if (parms_cell > 0) {
1306     parm_data_size = DataLayout::compute_size_in_bytes(parms_cell);
1307     object_size += parm_data_size;
1308     _parameters_type_data_di = data_size + extra_size + arg_data_size;
1309     DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size);
1310     dp->initialize(DataLayout::parameters_type_data_tag, 0, parms_cell);
1311   } else {
1312     _parameters_type_data_di = no_parameters;
1313   }
1314 
1315   _exception_handler_data_di = data_size + extra_size + arg_data_size + parm_data_size;
1316   if (ProfileExceptionHandlers && method()->has_exception_handler()) {
1317     int num_exception_handlers = method()->exception_table_length();
1318     object_size += num_exception_handlers * single_exception_handler_data_size();
1319     ExceptionTableElement* exception_handlers = method()->exception_table_start();
1320     for (int i = 0; i < num_exception_handlers; i++) {
1321       DataLayout *dp = exception_handler_data_at(i);
1322       dp->initialize(DataLayout::bit_data_tag, exception_handlers[i].handler_pc, single_exception_handler_data_cell_count());
1323     }
1324   }
1325 
1326   // Set an initial hint. Don't use set_hint_di() because
1327   // first_di() may be out of bounds if data_size is 0.
1328   // In that situation, _hint_di is never used, but at
1329   // least well-defined.
1330   _hint_di = first_di();
1331 
1332   post_initialize(&stream);
1333 
1334   assert(object_size == compute_allocation_size_in_bytes(methodHandle(thread, _method)), "MethodData: computed size != initialized size");
1335   set_size(object_size);
1336 }
1337 
1338 void MethodData::init() {
1339   _compiler_counters = CompilerCounters(); // reset compiler counters
1340   _invocation_counter.init();
1341   _backedge_counter.init();
1342   _invocation_counter_start = 0;
1343   _backedge_counter_start = 0;
1344 
1345   // Set per-method invoke- and backedge mask.
1346   double scale = 1.0;
1347   methodHandle mh(Thread::current(), _method);
1348   CompilerOracle::has_option_value(mh, CompileCommandEnum::CompileThresholdScaling, scale);
1349   _invoke_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1350   _backedge_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1351 
1352   _tenure_traps = 0;
1353   _num_loops = 0;
1354   _num_blocks = 0;
1355   _would_profile = unknown;
1356 
1357   // Initialize escape flags.
1358   clear_escape_info();
1359 }
1360 
1361 bool MethodData::is_mature() const {
1362   return CompilationPolicy::is_mature(const_cast<MethodData*>(this));
1363 }
1364 
1365 // Translate a bci to its corresponding data index (di).
1366 address MethodData::bci_to_dp(int bci) {
1367   ResourceMark rm;
1368   DataLayout* data = data_layout_before(bci);
1369   DataLayout* prev = nullptr;
1370   for ( ; is_valid(data); data = next_data_layout(data)) {
1371     if (data->bci() >= bci) {
1372       if (data->bci() == bci)  set_hint_di(dp_to_di((address)data));
1373       else if (prev != nullptr)   set_hint_di(dp_to_di((address)prev));
1374       return (address)data;
1375     }
1376     prev = data;
1377   }
1378   return (address)limit_data_position();
1379 }
1380 
1381 // Translate a bci to its corresponding data, or null.
1382 ProfileData* MethodData::bci_to_data(int bci) {
1383   check_extra_data_locked();
1384 
1385   DataLayout* data = data_layout_before(bci);
1386   for ( ; is_valid(data); data = next_data_layout(data)) {
1387     if (data->bci() == bci) {
1388       set_hint_di(dp_to_di((address)data));
1389       return data->data_in();
1390     } else if (data->bci() > bci) {
1391       break;
1392     }
1393   }
1394   return bci_to_extra_data(bci, nullptr, false);
1395 }
1396 
1397 DataLayout* MethodData::exception_handler_bci_to_data_helper(int bci) {
1398   assert(ProfileExceptionHandlers, "not profiling");
1399   for (int i = 0; i < num_exception_handler_data(); i++) {
1400     DataLayout* exception_handler_data = exception_handler_data_at(i);
1401     if (exception_handler_data->bci() == bci) {
1402       return exception_handler_data;
1403     }
1404   }
1405   return nullptr;
1406 }
1407 
1408 BitData* MethodData::exception_handler_bci_to_data_or_null(int bci) {
1409   DataLayout* data = exception_handler_bci_to_data_helper(bci);
1410   return data != nullptr ? new BitData(data) : nullptr;
1411 }
1412 
1413 BitData MethodData::exception_handler_bci_to_data(int bci) {
1414   DataLayout* data = exception_handler_bci_to_data_helper(bci);
1415   assert(data != nullptr, "invalid bci");
1416   return BitData(data);
1417 }
1418 
1419 DataLayout* MethodData::next_extra(DataLayout* dp) {
1420   int nb_cells = 0;
1421   switch(dp->tag()) {
1422   case DataLayout::bit_data_tag:
1423   case DataLayout::no_tag:
1424     nb_cells = BitData::static_cell_count();
1425     break;
1426   case DataLayout::speculative_trap_data_tag:
1427     nb_cells = SpeculativeTrapData::static_cell_count();
1428     break;
1429   default:
1430     fatal("unexpected tag %d", dp->tag());
1431   }
1432   return (DataLayout*)((address)dp + DataLayout::compute_size_in_bytes(nb_cells));
1433 }
1434 
1435 ProfileData* MethodData::bci_to_extra_data_find(int bci, Method* m, DataLayout*& dp) {
1436   check_extra_data_locked();
1437 
1438   DataLayout* end = args_data_limit();
1439 
1440   for (;; dp = next_extra(dp)) {
1441     assert(dp < end, "moved past end of extra data");
1442     // No need for "AtomicAccess::load_acquire" ops,
1443     // since the data structure is monotonic.
1444     switch(dp->tag()) {
1445     case DataLayout::no_tag:
1446       return nullptr;
1447     case DataLayout::arg_info_data_tag:
1448       dp = end;
1449       return nullptr; // ArgInfoData is at the end of extra data section.
1450     case DataLayout::bit_data_tag:
1451       if (m == nullptr && dp->bci() == bci) {
1452         return new BitData(dp);
1453       }
1454       break;
1455     case DataLayout::speculative_trap_data_tag:
1456       if (m != nullptr) {
1457         SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1458         if (dp->bci() == bci) {
1459           assert(data->method() != nullptr, "method must be set");
1460           if (data->method() == m) {
1461             return data;
1462           }
1463         }
1464       }
1465       break;
1466     default:
1467       fatal("unexpected tag %d", dp->tag());
1468     }
1469   }
1470   return nullptr;
1471 }
1472 
1473 
1474 // Translate a bci to its corresponding extra data, or null.
1475 ProfileData* MethodData::bci_to_extra_data(int bci, Method* m, bool create_if_missing) {
1476   check_extra_data_locked();
1477 
1478   // This code assumes an entry for a SpeculativeTrapData is 2 cells
1479   assert(2*DataLayout::compute_size_in_bytes(BitData::static_cell_count()) ==
1480          DataLayout::compute_size_in_bytes(SpeculativeTrapData::static_cell_count()),
1481          "code needs to be adjusted");
1482 
1483   // Do not create one of these if method has been redefined.
1484   if (m != nullptr && m->is_old()) {
1485     return nullptr;
1486   }
1487 
1488   DataLayout* dp  = extra_data_base();
1489   DataLayout* end = args_data_limit();
1490 
1491   // Find if already exists
1492   ProfileData* result = bci_to_extra_data_find(bci, m, dp);
1493   if (result != nullptr || dp >= end) {
1494     return result;
1495   }
1496 
1497   if (create_if_missing) {
1498     // Not found -> Allocate
1499     assert(dp->tag() == DataLayout::no_tag || (dp->tag() == DataLayout::speculative_trap_data_tag && m != nullptr), "should be free");
1500     assert(next_extra(dp)->tag() == DataLayout::no_tag || next_extra(dp)->tag() == DataLayout::arg_info_data_tag, "should be free or arg info");
1501     u1 tag = m == nullptr ? DataLayout::bit_data_tag : DataLayout::speculative_trap_data_tag;
1502     // SpeculativeTrapData is 2 slots. Make sure we have room.
1503     if (m != nullptr && next_extra(dp)->tag() != DataLayout::no_tag) {
1504       return nullptr;
1505     }
1506     DataLayout temp;
1507     temp.initialize(tag, checked_cast<u2>(bci), 0);
1508 
1509     dp->set_header(temp.header());
1510     assert(dp->tag() == tag, "sane");
1511     assert(dp->bci() == bci, "no concurrent allocation");
1512     if (tag == DataLayout::bit_data_tag) {
1513       return new BitData(dp);
1514     } else {
1515       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1516       data->set_method(m);
1517       return data;
1518     }
1519   }
1520   return nullptr;
1521 }
1522 
1523 ArgInfoData *MethodData::arg_info() {
1524   DataLayout* dp    = extra_data_base();
1525   DataLayout* end   = args_data_limit();
1526   for (; dp < end; dp = next_extra(dp)) {
1527     if (dp->tag() == DataLayout::arg_info_data_tag)
1528       return new ArgInfoData(dp);
1529   }
1530   return nullptr;
1531 }
1532 
1533 // Printing
1534 
1535 void MethodData::print_on(outputStream* st) const {
1536   assert(is_methodData(), "should be method data");
1537   st->print("method data for ");
1538   method()->print_value_on(st);
1539   st->cr();
1540   print_data_on(st);
1541 }
1542 
1543 void MethodData::print_value_on(outputStream* st) const {
1544   assert(is_methodData(), "should be method data");
1545   st->print("method data for ");
1546   method()->print_value_on(st);
1547 }
1548 
1549 void MethodData::print_data_on(outputStream* st) const {
1550   Mutex* lock = const_cast<MethodData*>(this)->extra_data_lock();
1551   ConditionalMutexLocker ml(lock, !lock->owned_by_self(),
1552                             Mutex::_no_safepoint_check_flag);
1553   ResourceMark rm;
1554   ProfileData* data = first_data();
1555   if (_parameters_type_data_di != no_parameters) {
1556     parameters_type_data()->print_data_on(st);
1557   }
1558   for ( ; is_valid(data); data = next_data(data)) {
1559     st->print("%d", dp_to_di(data->dp()));
1560     st->fill_to(6);
1561     data->print_data_on(st, this);
1562   }
1563 
1564   st->print_cr("--- Extra data:");
1565   DataLayout* dp    = extra_data_base();
1566   DataLayout* end   = args_data_limit();
1567   for (;; dp = next_extra(dp)) {
1568     assert(dp < end, "moved past end of extra data");
1569     // No need for "AtomicAccess::load_acquire" ops,
1570     // since the data structure is monotonic.
1571     switch(dp->tag()) {
1572     case DataLayout::no_tag:
1573       continue;
1574     case DataLayout::bit_data_tag:
1575       data = new BitData(dp);
1576       break;
1577     case DataLayout::speculative_trap_data_tag:
1578       data = new SpeculativeTrapData(dp);
1579       break;
1580     case DataLayout::arg_info_data_tag:
1581       data = new ArgInfoData(dp);
1582       dp = end; // ArgInfoData is at the end of extra data section.
1583       break;
1584     default:
1585       fatal("unexpected tag %d", dp->tag());
1586     }
1587     st->print("%d", dp_to_di(data->dp()));
1588     st->fill_to(6);
1589     data->print_data_on(st);
1590     if (dp >= end) return;
1591   }
1592 }
1593 
1594 // Verification
1595 
1596 void MethodData::verify_on(outputStream* st) {
1597   guarantee(is_methodData(), "object must be method data");
1598   // guarantee(m->is_perm(), "should be in permspace");
1599   this->verify_data_on(st);
1600 }
1601 
1602 void MethodData::verify_data_on(outputStream* st) {
1603   NEEDS_CLEANUP;
1604   // not yet implemented.
1605 }
1606 
1607 bool MethodData::profile_jsr292(const methodHandle& m, int bci) {
1608   if (m->is_compiled_lambda_form()) {
1609     return true;
1610   }
1611 
1612   Bytecode_invoke inv(m , bci);
1613   return inv.is_invokedynamic() || inv.is_invokehandle();
1614 }
1615 
1616 bool MethodData::profile_unsafe(const methodHandle& m, int bci) {
1617   Bytecode_invoke inv(m , bci);
1618   if (inv.is_invokevirtual()) {
1619     Symbol* klass = inv.klass();
1620     if (klass == vmSymbols::jdk_internal_misc_Unsafe() ||
1621         klass == vmSymbols::sun_misc_Unsafe() ||
1622         klass == vmSymbols::jdk_internal_misc_ScopedMemoryAccess()) {
1623       Symbol* name = inv.name();
1624       if (name->starts_with("get") || name->starts_with("put")) {
1625         return true;
1626       }
1627     }
1628   }
1629   return false;
1630 }
1631 
1632 int MethodData::profile_arguments_flag() {
1633   return TypeProfileLevel % 10;
1634 }
1635 
1636 bool MethodData::profile_arguments() {
1637   return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all && TypeProfileArgsLimit > 0;
1638 }
1639 
1640 bool MethodData::profile_arguments_jsr292_only() {
1641   return profile_arguments_flag() == type_profile_jsr292;
1642 }
1643 
1644 bool MethodData::profile_all_arguments() {
1645   return profile_arguments_flag() == type_profile_all;
1646 }
1647 
1648 bool MethodData::profile_arguments_for_invoke(const methodHandle& m, int bci) {
1649   if (!profile_arguments()) {
1650     return false;
1651   }
1652 
1653   if (profile_all_arguments()) {
1654     return true;
1655   }
1656 
1657   if (profile_unsafe(m, bci)) {
1658     return true;
1659   }
1660 
1661   assert(profile_arguments_jsr292_only(), "inconsistent");
1662   return profile_jsr292(m, bci);
1663 }
1664 
1665 int MethodData::profile_return_flag() {
1666   return (TypeProfileLevel % 100) / 10;
1667 }
1668 
1669 bool MethodData::profile_return() {
1670   return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1671 }
1672 
1673 bool MethodData::profile_return_jsr292_only() {
1674   return profile_return_flag() == type_profile_jsr292;
1675 }
1676 
1677 bool MethodData::profile_all_return() {
1678   return profile_return_flag() == type_profile_all;
1679 }
1680 
1681 bool MethodData::profile_return_for_invoke(const methodHandle& m, int bci) {
1682   if (!profile_return()) {
1683     return false;
1684   }
1685 
1686   if (profile_all_return()) {
1687     return true;
1688   }
1689 
1690   assert(profile_return_jsr292_only(), "inconsistent");
1691   return profile_jsr292(m, bci);
1692 }
1693 
1694 int MethodData::profile_parameters_flag() {
1695   return TypeProfileLevel / 100;
1696 }
1697 
1698 bool MethodData::profile_parameters() {
1699   return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all;
1700 }
1701 
1702 bool MethodData::profile_parameters_jsr292_only() {
1703   return profile_parameters_flag() == type_profile_jsr292;
1704 }
1705 
1706 bool MethodData::profile_all_parameters() {
1707   return profile_parameters_flag() == type_profile_all;
1708 }
1709 
1710 bool MethodData::profile_parameters_for_method(const methodHandle& m) {
1711   if (!profile_parameters()) {
1712     return false;
1713   }
1714 
1715   if (profile_all_parameters()) {
1716     return true;
1717   }
1718 
1719   assert(profile_parameters_jsr292_only(), "inconsistent");
1720   return m->is_compiled_lambda_form();
1721 }
1722 
1723 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) {
1724   log_trace(aot, training)("Iter(MethodData): %p for %p %s", this, _method, _method->name_and_sig_as_C_string());
1725   it->push(&_method);
1726   if (_parameters_type_data_di != no_parameters) {
1727     parameters_type_data()->metaspace_pointers_do(it);
1728   }
1729   for (ProfileData* data = first_data(); is_valid(data); data = next_data(data)) {
1730     data->metaspace_pointers_do(it);
1731   }
1732   for (DataLayout* dp = extra_data_base();
1733                    dp < extra_data_limit();
1734                    dp = MethodData::next_extra(dp)) {
1735     if (dp->tag() == DataLayout::speculative_trap_data_tag) {
1736       ResourceMark rm;
1737       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1738       data->metaspace_pointers_do(it);
1739     } else if (dp->tag() == DataLayout::no_tag ||
1740                dp->tag() == DataLayout::arg_info_data_tag) {
1741       break;
1742     }
1743   }
1744 }
1745 
1746 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1747   check_extra_data_locked();
1748 
1749   if (shift == 0) {
1750     return;
1751   }
1752   if (!reset) {
1753     // Move all cells of trap entry at dp left by "shift" cells
1754     intptr_t* start = (intptr_t*)dp;
1755     intptr_t* end = (intptr_t*)next_extra(dp);
1756     for (intptr_t* ptr = start; ptr < end; ptr++) {
1757       *(ptr-shift) = *ptr;
1758     }
1759   } else {
1760     // Reset "shift" cells stopping at dp
1761     intptr_t* start = ((intptr_t*)dp) - shift;
1762     intptr_t* end = (intptr_t*)dp;
1763     for (intptr_t* ptr = start; ptr < end; ptr++) {
1764       *ptr = 0;
1765     }
1766   }
1767 }
1768 
1769 // Check for entries that reference an unloaded method
1770 class CleanExtraDataKlassClosure : public CleanExtraDataClosure {
1771   bool _always_clean;
1772 public:
1773   CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {}
1774   bool is_live(Method* m) {
1775     if (!_always_clean && m->method_holder()->is_instance_klass() && InstanceKlass::cast(m->method_holder())->is_not_initialized()) {
1776       return true; // TODO: treat as unloaded instead?
1777     }
1778     return !(_always_clean) && m->method_holder()->is_loader_alive();
1779   }
1780 };
1781 
1782 // Check for entries that reference a redefined method
1783 class CleanExtraDataMethodClosure : public CleanExtraDataClosure {
1784 public:
1785   CleanExtraDataMethodClosure() {}
1786   bool is_live(Method* m) { return !m->is_old(); }
1787 };
1788 
1789 Mutex* MethodData::extra_data_lock() {
1790   Mutex* lock = AtomicAccess::load_acquire(&_extra_data_lock);
1791   if (lock == nullptr) {
1792     // This lock could be acquired while we are holding DumpTimeTable_lock/nosafepoint
1793     lock = new Mutex(Mutex::nosafepoint-1, "MDOExtraData_lock");
1794     Mutex* old = AtomicAccess::cmpxchg(&_extra_data_lock, (Mutex*)nullptr, lock);
1795     if (old != nullptr) {
1796       // Another thread created the lock before us. Use that lock instead.
1797       delete lock;
1798       return old;
1799     }
1800   }
1801   return lock;
1802 }
1803 
1804 // Remove SpeculativeTrapData entries that reference an unloaded or
1805 // redefined method
1806 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) {
1807   check_extra_data_locked();
1808 
1809   DataLayout* dp  = extra_data_base();
1810   DataLayout* end = args_data_limit();
1811 
1812   int shift = 0;
1813   for (; dp < end; dp = next_extra(dp)) {
1814     switch(dp->tag()) {
1815     case DataLayout::speculative_trap_data_tag: {
1816       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1817       Method* m = data->method();
1818       assert(m != nullptr, "should have a method");
1819       if (is_excluded(m->method_holder()) || !cl->is_live(m)) {
1820         // "shift" accumulates the number of cells for dead
1821         // SpeculativeTrapData entries that have been seen so
1822         // far. Following entries must be shifted left by that many
1823         // cells to remove the dead SpeculativeTrapData entries.
1824         shift += (int)((intptr_t*)next_extra(dp) - (intptr_t*)dp);
1825       } else {
1826         // Shift this entry left if it follows dead
1827         // SpeculativeTrapData entries
1828         clean_extra_data_helper(dp, shift);
1829       }
1830       break;
1831     }
1832     case DataLayout::bit_data_tag:
1833       // Shift this entry left if it follows dead SpeculativeTrapData
1834       // entries
1835       clean_extra_data_helper(dp, shift);
1836       continue;
1837     case DataLayout::no_tag:
1838     case DataLayout::arg_info_data_tag:
1839       // We are at end of the live trap entries. The previous "shift"
1840       // cells contain entries that are either dead or were shifted
1841       // left. They need to be reset to no_tag
1842       clean_extra_data_helper(dp, shift, true);
1843       return;
1844     default:
1845       fatal("unexpected tag %d", dp->tag());
1846     }
1847   }
1848 }
1849 
1850 // Verify there's no unloaded or redefined method referenced by a
1851 // SpeculativeTrapData entry
1852 void MethodData::verify_extra_data_clean(CleanExtraDataClosure* cl) {
1853   check_extra_data_locked();
1854 
1855 #ifdef ASSERT
1856   DataLayout* dp  = extra_data_base();
1857   DataLayout* end = args_data_limit();
1858 
1859   for (; dp < end; dp = next_extra(dp)) {
1860     switch(dp->tag()) {
1861     case DataLayout::speculative_trap_data_tag: {
1862       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1863       Method* m = data->method();
1864       assert(m != nullptr && cl->is_live(m), "Method should exist");
1865       break;
1866     }
1867     case DataLayout::bit_data_tag:
1868       continue;
1869     case DataLayout::no_tag:
1870     case DataLayout::arg_info_data_tag:
1871       return;
1872     default:
1873       fatal("unexpected tag %d", dp->tag());
1874     }
1875   }
1876 #endif
1877 }
1878 
1879 void MethodData::clean_method_data(bool always_clean) {
1880   ResourceMark rm;
1881   for (ProfileData* data = first_data();
1882        is_valid(data);
1883        data = next_data(data)) {
1884     data->clean_weak_klass_links(always_clean);
1885   }
1886   ParametersTypeData* parameters = parameters_type_data();
1887   if (parameters != nullptr) {
1888     parameters->clean_weak_klass_links(always_clean);
1889   }
1890 
1891   CleanExtraDataKlassClosure cl(always_clean);
1892 
1893   // Lock to modify extra data, and prevent Safepoint from breaking the lock
1894   MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1895 
1896   clean_extra_data(&cl);
1897   verify_extra_data_clean(&cl);
1898 }
1899 
1900 // This is called during redefinition to clean all "old" redefined
1901 // methods out of MethodData for all methods.
1902 void MethodData::clean_weak_method_links() {
1903   ResourceMark rm;
1904   CleanExtraDataMethodClosure cl;
1905 
1906   // Lock to modify extra data, and prevent Safepoint from breaking the lock
1907   MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1908 
1909   clean_extra_data(&cl);
1910   verify_extra_data_clean(&cl);
1911 }
1912 
1913 void MethodData::deallocate_contents(ClassLoaderData* loader_data) {
1914   release_C_heap_structures();
1915 }
1916 
1917 #if INCLUDE_CDS
1918 void MethodData::remove_unshareable_info() {
1919   _extra_data_lock = nullptr;
1920 }
1921 
1922 void MethodData::restore_unshareable_info(TRAPS) {
1923   //_extra_data_lock = new Mutex(Mutex::nosafepoint, "MDOExtraData_lock");
1924 }
1925 #endif // INCLUDE_CDS
1926 
1927 #ifdef ASSERT
1928 void MethodData::check_extra_data_locked() const {
1929     // Cast const away, just to be able to verify the lock
1930     // Usually we only want non-const accesses on the lock,
1931     // so this here is an exception.
1932     MethodData* self = (MethodData*)this;
1933     assert(self->extra_data_lock()->owned_by_self() || CDSConfig::is_dumping_archive(), "must have lock");
1934     assert(!Thread::current()->is_Java_thread() ||
1935            JavaThread::current()->is_in_no_safepoint_scope(),
1936            "JavaThread must have NoSafepointVerifier inside lock scope");
1937 }
1938 #endif