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