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