1 /*
   2  * Copyright (c) 2000, 2022, 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   int size = MethodData::compute_allocation_size_in_words(method);
 687 
 688   return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
 689     MethodData(method);
 690 }
 691 
 692 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
 693   if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) {
 694     return no_profile_data;
 695   }
 696   switch (code) {
 697   case Bytecodes::_checkcast:
 698   case Bytecodes::_instanceof:
 699     if (TypeProfileCasts) {
 700       return ReceiverTypeData::static_cell_count();
 701     } else {
 702       return BitData::static_cell_count();
 703     }
 704   case Bytecodes::_aaload:
 705   case Bytecodes::_aastore:
 706     return ArrayLoadStoreData::static_cell_count();
 707   case Bytecodes::_invokespecial:
 708   case Bytecodes::_invokestatic:
 709     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 710       return variable_cell_count;
 711     } else {
 712       return CounterData::static_cell_count();
 713     }
 714   case Bytecodes::_goto:
 715   case Bytecodes::_goto_w:
 716   case Bytecodes::_jsr:
 717   case Bytecodes::_jsr_w:
 718     return JumpData::static_cell_count();
 719   case Bytecodes::_invokevirtual:
 720   case Bytecodes::_invokeinterface:
 721     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 722       return variable_cell_count;
 723     } else {
 724       return VirtualCallData::static_cell_count();
 725     }
 726   case Bytecodes::_invokedynamic:
 727     if (MethodData::profile_arguments() || MethodData::profile_return()) {
 728       return variable_cell_count;
 729     } else {
 730       return CounterData::static_cell_count();
 731     }
 732   case Bytecodes::_ret:
 733     return RetData::static_cell_count();
 734   case Bytecodes::_ifeq:
 735   case Bytecodes::_ifne:
 736   case Bytecodes::_iflt:
 737   case Bytecodes::_ifge:
 738   case Bytecodes::_ifgt:
 739   case Bytecodes::_ifle:
 740   case Bytecodes::_if_icmpeq:
 741   case Bytecodes::_if_icmpne:
 742   case Bytecodes::_if_icmplt:
 743   case Bytecodes::_if_icmpge:
 744   case Bytecodes::_if_icmpgt:
 745   case Bytecodes::_if_icmple:
 746   case Bytecodes::_ifnull:
 747   case Bytecodes::_ifnonnull:
 748     return BranchData::static_cell_count();
 749   case Bytecodes::_if_acmpne:
 750   case Bytecodes::_if_acmpeq:
 751     return ACmpData::static_cell_count();
 752   case Bytecodes::_lookupswitch:
 753   case Bytecodes::_tableswitch:
 754     return variable_cell_count;
 755   default:
 756     return no_profile_data;
 757   }
 758 }
 759 
 760 // Compute the size of the profiling information corresponding to
 761 // the current bytecode.
 762 int MethodData::compute_data_size(BytecodeStream* stream) {
 763   int cell_count = bytecode_cell_count(stream->code());
 764   if (cell_count == no_profile_data) {
 765     return 0;
 766   }
 767   if (cell_count == variable_cell_count) {
 768     switch (stream->code()) {
 769     case Bytecodes::_lookupswitch:
 770     case Bytecodes::_tableswitch:
 771       cell_count = MultiBranchData::compute_cell_count(stream);
 772       break;
 773     case Bytecodes::_invokespecial:
 774     case Bytecodes::_invokestatic:
 775     case Bytecodes::_invokedynamic:
 776       assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
 777       if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
 778           profile_return_for_invoke(stream->method(), stream->bci())) {
 779         cell_count = CallTypeData::compute_cell_count(stream);
 780       } else {
 781         cell_count = CounterData::static_cell_count();
 782       }
 783       break;
 784     case Bytecodes::_invokevirtual:
 785     case Bytecodes::_invokeinterface: {
 786       assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
 787       if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
 788           profile_return_for_invoke(stream->method(), stream->bci())) {
 789         cell_count = VirtualCallTypeData::compute_cell_count(stream);
 790       } else {
 791         cell_count = VirtualCallData::static_cell_count();
 792       }
 793       break;
 794     }
 795     default:
 796       fatal("unexpected bytecode for var length profile data");
 797     }
 798   }
 799   // Note:  cell_count might be zero, meaning that there is just
 800   //        a DataLayout header, with no extra cells.
 801   assert(cell_count >= 0, "sanity");
 802   return DataLayout::compute_size_in_bytes(cell_count);
 803 }
 804 
 805 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
 806   // Bytecodes for which we may use speculation
 807   switch (code) {
 808   case Bytecodes::_checkcast:
 809   case Bytecodes::_instanceof:
 810   case Bytecodes::_aaload:
 811   case Bytecodes::_aastore:
 812   case Bytecodes::_invokevirtual:
 813   case Bytecodes::_invokeinterface:
 814   case Bytecodes::_if_acmpeq:
 815   case Bytecodes::_if_acmpne:
 816   case Bytecodes::_ifnull:
 817   case Bytecodes::_ifnonnull:
 818   case Bytecodes::_invokestatic:
 819 #ifdef COMPILER2
 820     if (CompilerConfig::is_c2_enabled()) {
 821       return UseTypeSpeculation;
 822     }
 823 #endif
 824   default:
 825     return false;
 826   }
 827   return false;
 828 }
 829 
 830 #if INCLUDE_JVMCI
 831 
 832 void* FailedSpeculation::operator new(size_t size, size_t fs_size) throw() {
 833   return CHeapObj<mtCompiler>::operator new(fs_size, std::nothrow);
 834 }
 835 
 836 FailedSpeculation::FailedSpeculation(address speculation, int speculation_len) : _data_len(speculation_len), _next(NULL) {
 837   memcpy(data(), speculation, speculation_len);
 838 }
 839 
 840 // A heuristic check to detect nmethods that outlive a failed speculations list.
 841 static void guarantee_failed_speculations_alive(nmethod* nm, FailedSpeculation** failed_speculations_address) {
 842   jlong head = (jlong)(address) *failed_speculations_address;
 843   if ((head & 0x1) == 0x1) {
 844     stringStream st;
 845     if (nm != NULL) {
 846       st.print("%d", nm->compile_id());
 847       Method* method = nm->method();
 848       st.print_raw("{");
 849       if (method != NULL) {
 850         method->print_name(&st);
 851       } else {
 852         const char* jvmci_name = nm->jvmci_name();
 853         if (jvmci_name != NULL) {
 854           st.print_raw(jvmci_name);
 855         }
 856       }
 857       st.print_raw("}");
 858     } else {
 859       st.print("<unknown>");
 860     }
 861     fatal("Adding to failed speculations list that appears to have been freed. Source: %s", st.as_string());
 862   }
 863 }
 864 
 865 bool FailedSpeculation::add_failed_speculation(nmethod* nm, FailedSpeculation** failed_speculations_address, address speculation, int speculation_len) {
 866   assert(failed_speculations_address != NULL, "must be");
 867   size_t fs_size = sizeof(FailedSpeculation) + speculation_len;
 868   FailedSpeculation* fs = new (fs_size) FailedSpeculation(speculation, speculation_len);
 869   if (fs == NULL) {
 870     // no memory -> ignore failed speculation
 871     return false;
 872   }
 873 
 874   guarantee(is_aligned(fs, sizeof(FailedSpeculation*)), "FailedSpeculation objects must be pointer aligned");
 875   guarantee_failed_speculations_alive(nm, failed_speculations_address);
 876 
 877   FailedSpeculation** cursor = failed_speculations_address;
 878   do {
 879     if (*cursor == NULL) {
 880       FailedSpeculation* old_fs = Atomic::cmpxchg(cursor, (FailedSpeculation*) NULL, fs);
 881       if (old_fs == NULL) {
 882         // Successfully appended fs to end of the list
 883         return true;
 884       }
 885       cursor = old_fs->next_adr();
 886     } else {
 887       cursor = (*cursor)->next_adr();
 888     }
 889   } while (true);
 890 }
 891 
 892 void FailedSpeculation::free_failed_speculations(FailedSpeculation** failed_speculations_address) {
 893   assert(failed_speculations_address != NULL, "must be");
 894   FailedSpeculation* fs = *failed_speculations_address;
 895   while (fs != NULL) {
 896     FailedSpeculation* next = fs->next();
 897     delete fs;
 898     fs = next;
 899   }
 900 
 901   // Write an unaligned value to failed_speculations_address to denote
 902   // that it is no longer a valid pointer. This is allows for the check
 903   // in add_failed_speculation against adding to a freed failed
 904   // speculations list.
 905   long* head = (long*) failed_speculations_address;
 906   (*head) = (*head) | 0x1;
 907 }
 908 #endif // INCLUDE_JVMCI
 909 
 910 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) {
 911 #if INCLUDE_JVMCI
 912   if (ProfileTraps) {
 913     // Assume that up to 30% of the possibly trapping BCIs with no MDP will need to allocate one.
 914     int extra_data_count = MIN2(empty_bc_count, MAX2(4, (empty_bc_count * 30) / 100));
 915 
 916     // Make sure we have a minimum number of extra data slots to
 917     // allocate SpeculativeTrapData entries. We would want to have one
 918     // entry per compilation that inlines this method and for which
 919     // some type speculation assumption fails. So the room we need for
 920     // the SpeculativeTrapData entries doesn't directly depend on the
 921     // size of the method. Because it's hard to estimate, we reserve
 922     // space for an arbitrary number of entries.
 923     int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
 924       (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
 925 
 926     return MAX2(extra_data_count, spec_data_count);
 927   } else {
 928     return 0;
 929   }
 930 #else // INCLUDE_JVMCI
 931   if (ProfileTraps) {
 932     // Assume that up to 3% of BCIs with no MDP will need to allocate one.
 933     int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
 934     // If the method is large, let the extra BCIs grow numerous (to ~1%).
 935     int one_percent_of_data
 936       = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
 937     if (extra_data_count < one_percent_of_data)
 938       extra_data_count = one_percent_of_data;
 939     if (extra_data_count > empty_bc_count)
 940       extra_data_count = empty_bc_count;  // no need for more
 941 
 942     // Make sure we have a minimum number of extra data slots to
 943     // allocate SpeculativeTrapData entries. We would want to have one
 944     // entry per compilation that inlines this method and for which
 945     // some type speculation assumption fails. So the room we need for
 946     // the SpeculativeTrapData entries doesn't directly depend on the
 947     // size of the method. Because it's hard to estimate, we reserve
 948     // space for an arbitrary number of entries.
 949     int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
 950       (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
 951 
 952     return MAX2(extra_data_count, spec_data_count);
 953   } else {
 954     return 0;
 955   }
 956 #endif // INCLUDE_JVMCI
 957 }
 958 
 959 // Compute the size of the MethodData* necessary to store
 960 // profiling information about a given method.  Size is in bytes.
 961 int MethodData::compute_allocation_size_in_bytes(const methodHandle& method) {
 962   int data_size = 0;
 963   BytecodeStream stream(method);
 964   Bytecodes::Code c;
 965   int empty_bc_count = 0;  // number of bytecodes lacking data
 966   bool needs_speculative_traps = false;
 967   while ((c = stream.next()) >= 0) {
 968     int size_in_bytes = compute_data_size(&stream);
 969     data_size += size_in_bytes;
 970     if (size_in_bytes == 0 JVMCI_ONLY(&& Bytecodes::can_trap(c)))  empty_bc_count += 1;
 971     needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
 972   }
 973   int object_size = in_bytes(data_offset()) + data_size;
 974 
 975   // Add some extra DataLayout cells (at least one) to track stray traps.
 976   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
 977   object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
 978 
 979   // Add a cell to record information about modified arguments.
 980   int arg_size = method->size_of_parameters();
 981   object_size += DataLayout::compute_size_in_bytes(arg_size+1);
 982 
 983   // Reserve room for an area of the MDO dedicated to profiling of
 984   // parameters
 985   int args_cell = ParametersTypeData::compute_cell_count(method());
 986   if (args_cell > 0) {
 987     object_size += DataLayout::compute_size_in_bytes(args_cell);
 988   }
 989   return object_size;
 990 }
 991 
 992 // Compute the size of the MethodData* necessary to store
 993 // profiling information about a given method.  Size is in words
 994 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
 995   int byte_size = compute_allocation_size_in_bytes(method);
 996   int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
 997   return align_metadata_size(word_size);
 998 }
 999 
1000 // Initialize an individual data segment.  Returns the size of
1001 // the segment in bytes.
1002 int MethodData::initialize_data(BytecodeStream* stream,
1003                                        int data_index) {
1004   if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) {
1005     return 0;
1006   }
1007   int cell_count = -1;
1008   int tag = DataLayout::no_tag;
1009   DataLayout* data_layout = data_layout_at(data_index);
1010   Bytecodes::Code c = stream->code();
1011   switch (c) {
1012   case Bytecodes::_checkcast:
1013   case Bytecodes::_instanceof:
1014     if (TypeProfileCasts) {
1015       cell_count = ReceiverTypeData::static_cell_count();
1016       tag = DataLayout::receiver_type_data_tag;
1017     } else {
1018       cell_count = BitData::static_cell_count();
1019       tag = DataLayout::bit_data_tag;
1020     }
1021     break;
1022   case Bytecodes::_aaload:
1023   case Bytecodes::_aastore:
1024     cell_count = ArrayLoadStoreData::static_cell_count();
1025     tag = DataLayout::array_load_store_data_tag;
1026     break;
1027   case Bytecodes::_invokespecial:
1028   case Bytecodes::_invokestatic: {
1029     int counter_data_cell_count = CounterData::static_cell_count();
1030     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1031         profile_return_for_invoke(stream->method(), stream->bci())) {
1032       cell_count = CallTypeData::compute_cell_count(stream);
1033     } else {
1034       cell_count = counter_data_cell_count;
1035     }
1036     if (cell_count > counter_data_cell_count) {
1037       tag = DataLayout::call_type_data_tag;
1038     } else {
1039       tag = DataLayout::counter_data_tag;
1040     }
1041     break;
1042   }
1043   case Bytecodes::_goto:
1044   case Bytecodes::_goto_w:
1045   case Bytecodes::_jsr:
1046   case Bytecodes::_jsr_w:
1047     cell_count = JumpData::static_cell_count();
1048     tag = DataLayout::jump_data_tag;
1049     break;
1050   case Bytecodes::_invokevirtual:
1051   case Bytecodes::_invokeinterface: {
1052     int virtual_call_data_cell_count = VirtualCallData::static_cell_count();
1053     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1054         profile_return_for_invoke(stream->method(), stream->bci())) {
1055       cell_count = VirtualCallTypeData::compute_cell_count(stream);
1056     } else {
1057       cell_count = virtual_call_data_cell_count;
1058     }
1059     if (cell_count > virtual_call_data_cell_count) {
1060       tag = DataLayout::virtual_call_type_data_tag;
1061     } else {
1062       tag = DataLayout::virtual_call_data_tag;
1063     }
1064     break;
1065   }
1066   case Bytecodes::_invokedynamic: {
1067     // %%% should make a type profile for any invokedynamic that takes a ref argument
1068     int counter_data_cell_count = CounterData::static_cell_count();
1069     if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1070         profile_return_for_invoke(stream->method(), stream->bci())) {
1071       cell_count = CallTypeData::compute_cell_count(stream);
1072     } else {
1073       cell_count = counter_data_cell_count;
1074     }
1075     if (cell_count > counter_data_cell_count) {
1076       tag = DataLayout::call_type_data_tag;
1077     } else {
1078       tag = DataLayout::counter_data_tag;
1079     }
1080     break;
1081   }
1082   case Bytecodes::_ret:
1083     cell_count = RetData::static_cell_count();
1084     tag = DataLayout::ret_data_tag;
1085     break;
1086   case Bytecodes::_ifeq:
1087   case Bytecodes::_ifne:
1088   case Bytecodes::_iflt:
1089   case Bytecodes::_ifge:
1090   case Bytecodes::_ifgt:
1091   case Bytecodes::_ifle:
1092   case Bytecodes::_if_icmpeq:
1093   case Bytecodes::_if_icmpne:
1094   case Bytecodes::_if_icmplt:
1095   case Bytecodes::_if_icmpge:
1096   case Bytecodes::_if_icmpgt:
1097   case Bytecodes::_if_icmple:
1098   case Bytecodes::_ifnull:
1099   case Bytecodes::_ifnonnull:
1100     cell_count = BranchData::static_cell_count();
1101     tag = DataLayout::branch_data_tag;
1102     break;
1103   case Bytecodes::_if_acmpeq:
1104   case Bytecodes::_if_acmpne:
1105     cell_count = ACmpData::static_cell_count();
1106     tag = DataLayout::acmp_data_tag;
1107     break;
1108   case Bytecodes::_lookupswitch:
1109   case Bytecodes::_tableswitch:
1110     cell_count = MultiBranchData::compute_cell_count(stream);
1111     tag = DataLayout::multi_branch_data_tag;
1112     break;
1113   default:
1114     break;
1115   }
1116   assert(tag == DataLayout::multi_branch_data_tag ||
1117          ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1118           (tag == DataLayout::call_type_data_tag ||
1119            tag == DataLayout::counter_data_tag ||
1120            tag == DataLayout::virtual_call_type_data_tag ||
1121            tag == DataLayout::virtual_call_data_tag)) ||
1122          cell_count == bytecode_cell_count(c), "cell counts must agree");
1123   if (cell_count >= 0) {
1124     assert(tag != DataLayout::no_tag, "bad tag");
1125     assert(bytecode_has_profile(c), "agree w/ BHP");
1126     data_layout->initialize(tag, stream->bci(), cell_count);
1127     return DataLayout::compute_size_in_bytes(cell_count);
1128   } else {
1129     assert(!bytecode_has_profile(c), "agree w/ !BHP");
1130     return 0;
1131   }
1132 }
1133 
1134 // Get the data at an arbitrary (sort of) data index.
1135 ProfileData* MethodData::data_at(int data_index) const {
1136   if (out_of_bounds(data_index)) {
1137     return NULL;
1138   }
1139   DataLayout* data_layout = data_layout_at(data_index);
1140   return data_layout->data_in();
1141 }
1142 
1143 int DataLayout::cell_count() {
1144   switch (tag()) {
1145   case DataLayout::no_tag:
1146   default:
1147     ShouldNotReachHere();
1148     return 0;
1149   case DataLayout::bit_data_tag:
1150     return BitData::static_cell_count();
1151   case DataLayout::counter_data_tag:
1152     return CounterData::static_cell_count();
1153   case DataLayout::jump_data_tag:
1154     return JumpData::static_cell_count();
1155   case DataLayout::receiver_type_data_tag:
1156     return ReceiverTypeData::static_cell_count();
1157   case DataLayout::virtual_call_data_tag:
1158     return VirtualCallData::static_cell_count();
1159   case DataLayout::ret_data_tag:
1160     return RetData::static_cell_count();
1161   case DataLayout::branch_data_tag:
1162     return BranchData::static_cell_count();
1163   case DataLayout::multi_branch_data_tag:
1164     return ((new MultiBranchData(this))->cell_count());
1165   case DataLayout::arg_info_data_tag:
1166     return ((new ArgInfoData(this))->cell_count());
1167   case DataLayout::call_type_data_tag:
1168     return ((new CallTypeData(this))->cell_count());
1169   case DataLayout::virtual_call_type_data_tag:
1170     return ((new VirtualCallTypeData(this))->cell_count());
1171   case DataLayout::parameters_type_data_tag:
1172     return ((new ParametersTypeData(this))->cell_count());
1173   case DataLayout::speculative_trap_data_tag:
1174     return SpeculativeTrapData::static_cell_count();
1175   case DataLayout::array_load_store_data_tag:
1176     return ((new ArrayLoadStoreData(this))->cell_count());
1177   case DataLayout::acmp_data_tag:
1178     return ((new ACmpData(this))->cell_count());
1179   }
1180 }
1181 ProfileData* DataLayout::data_in() {
1182   switch (tag()) {
1183   case DataLayout::no_tag:
1184   default:
1185     ShouldNotReachHere();
1186     return NULL;
1187   case DataLayout::bit_data_tag:
1188     return new BitData(this);
1189   case DataLayout::counter_data_tag:
1190     return new CounterData(this);
1191   case DataLayout::jump_data_tag:
1192     return new JumpData(this);
1193   case DataLayout::receiver_type_data_tag:
1194     return new ReceiverTypeData(this);
1195   case DataLayout::virtual_call_data_tag:
1196     return new VirtualCallData(this);
1197   case DataLayout::ret_data_tag:
1198     return new RetData(this);
1199   case DataLayout::branch_data_tag:
1200     return new BranchData(this);
1201   case DataLayout::multi_branch_data_tag:
1202     return new MultiBranchData(this);
1203   case DataLayout::arg_info_data_tag:
1204     return new ArgInfoData(this);
1205   case DataLayout::call_type_data_tag:
1206     return new CallTypeData(this);
1207   case DataLayout::virtual_call_type_data_tag:
1208     return new VirtualCallTypeData(this);
1209   case DataLayout::parameters_type_data_tag:
1210     return new ParametersTypeData(this);
1211   case DataLayout::speculative_trap_data_tag:
1212     return new SpeculativeTrapData(this);
1213   case DataLayout::array_load_store_data_tag:
1214     return new ArrayLoadStoreData(this);
1215   case DataLayout::acmp_data_tag:
1216     return new ACmpData(this);
1217   }
1218 }
1219 
1220 // Iteration over data.
1221 ProfileData* MethodData::next_data(ProfileData* current) const {
1222   int current_index = dp_to_di(current->dp());
1223   int next_index = current_index + current->size_in_bytes();
1224   ProfileData* next = data_at(next_index);
1225   return next;
1226 }
1227 
1228 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1229   int current_index = dp_to_di((address)current);
1230   int next_index = current_index + current->size_in_bytes();
1231   if (out_of_bounds(next_index)) {
1232     return NULL;
1233   }
1234   DataLayout* next = data_layout_at(next_index);
1235   return next;
1236 }
1237 
1238 // Give each of the data entries a chance to perform specific
1239 // data initialization.
1240 void MethodData::post_initialize(BytecodeStream* stream) {
1241   ResourceMark rm;
1242   ProfileData* data;
1243   for (data = first_data(); is_valid(data); data = next_data(data)) {
1244     stream->set_start(data->bci());
1245     stream->next();
1246     data->post_initialize(stream, this);
1247   }
1248   if (_parameters_type_data_di != no_parameters) {
1249     parameters_type_data()->post_initialize(NULL, this);
1250   }
1251 }
1252 
1253 // Initialize the MethodData* corresponding to a given method.
1254 MethodData::MethodData(const methodHandle& method)
1255   : _method(method()),
1256     // Holds Compile_lock
1257     _extra_data_lock(Mutex::safepoint-2, "MDOExtraData_lock"),
1258     _compiler_counters(),
1259     _parameters_type_data_di(parameters_uninitialized) {
1260   initialize();
1261 }
1262 
1263 void MethodData::initialize() {
1264   Thread* thread = Thread::current();
1265   NoSafepointVerifier no_safepoint;  // init function atomic wrt GC
1266   ResourceMark rm(thread);
1267 
1268   init();
1269   set_creation_mileage(mileage_of(method()));
1270 
1271   // Go through the bytecodes and allocate and initialize the
1272   // corresponding data cells.
1273   int data_size = 0;
1274   int empty_bc_count = 0;  // number of bytecodes lacking data
1275   _data[0] = 0;  // apparently not set below.
1276   BytecodeStream stream(methodHandle(thread, method()));
1277   Bytecodes::Code c;
1278   bool needs_speculative_traps = false;
1279   while ((c = stream.next()) >= 0) {
1280     int size_in_bytes = initialize_data(&stream, data_size);
1281     data_size += size_in_bytes;
1282     if (size_in_bytes == 0 JVMCI_ONLY(&& Bytecodes::can_trap(c)))  empty_bc_count += 1;
1283     needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
1284   }
1285   _data_size = data_size;
1286   int object_size = in_bytes(data_offset()) + data_size;
1287 
1288   // Add some extra DataLayout cells (at least one) to track stray traps.
1289   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
1290   int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
1291 
1292   // Let's zero the space for the extra data
1293   if (extra_size > 0) {
1294     Copy::zero_to_bytes(((address)_data) + data_size, extra_size);
1295   }
1296 
1297   // Add a cell to record information about modified arguments.
1298   // Set up _args_modified array after traps cells so that
1299   // the code for traps cells works.
1300   DataLayout *dp = data_layout_at(data_size + extra_size);
1301 
1302   int arg_size = method()->size_of_parameters();
1303   dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
1304 
1305   int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1);
1306   object_size += extra_size + arg_data_size;
1307 
1308   int parms_cell = ParametersTypeData::compute_cell_count(method());
1309   // If we are profiling parameters, we reserved an area near the end
1310   // of the MDO after the slots for bytecodes (because there's no bci
1311   // for method entry so they don't fit with the framework for the
1312   // profiling of bytecodes). We store the offset within the MDO of
1313   // this area (or -1 if no parameter is profiled)
1314   if (parms_cell > 0) {
1315     object_size += DataLayout::compute_size_in_bytes(parms_cell);
1316     _parameters_type_data_di = data_size + extra_size + arg_data_size;
1317     DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size);
1318     dp->initialize(DataLayout::parameters_type_data_tag, 0, parms_cell);
1319   } else {
1320     _parameters_type_data_di = no_parameters;
1321   }
1322 
1323   // Set an initial hint. Don't use set_hint_di() because
1324   // first_di() may be out of bounds if data_size is 0.
1325   // In that situation, _hint_di is never used, but at
1326   // least well-defined.
1327   _hint_di = first_di();
1328 
1329   post_initialize(&stream);
1330 
1331   assert(object_size == compute_allocation_size_in_bytes(methodHandle(thread, _method)), "MethodData: computed size != initialized size");
1332   set_size(object_size);
1333 }
1334 
1335 void MethodData::init() {
1336   _compiler_counters = CompilerCounters(); // reset compiler counters
1337   _invocation_counter.init();
1338   _backedge_counter.init();
1339   _invocation_counter_start = 0;
1340   _backedge_counter_start = 0;
1341 
1342   // Set per-method invoke- and backedge mask.
1343   double scale = 1.0;
1344   methodHandle mh(Thread::current(), _method);
1345   CompilerOracle::has_option_value(mh, CompileCommand::CompileThresholdScaling, scale);
1346   _invoke_mask = right_n_bits(CompilerConfig::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1347   _backedge_mask = right_n_bits(CompilerConfig::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1348 
1349   _tenure_traps = 0;
1350   _num_loops = 0;
1351   _num_blocks = 0;
1352   _would_profile = unknown;
1353 
1354 #if INCLUDE_JVMCI
1355   _jvmci_ir_size = 0;
1356   _failed_speculations = NULL;
1357 #endif
1358 
1359 #if INCLUDE_RTM_OPT
1360   _rtm_state = NoRTM; // No RTM lock eliding by default
1361   if (UseRTMLocking &&
1362       !CompilerOracle::has_option(mh, CompileCommand::NoRTMLockEliding)) {
1363     if (CompilerOracle::has_option(mh, CompileCommand::UseRTMLockEliding) || !UseRTMDeopt) {
1364       // Generate RTM lock eliding code without abort ratio calculation code.
1365       _rtm_state = UseRTM;
1366     } else if (UseRTMDeopt) {
1367       // Generate RTM lock eliding code and include abort ratio calculation
1368       // code if UseRTMDeopt is on.
1369       _rtm_state = ProfileRTM;
1370     }
1371   }
1372 #endif
1373 
1374   // Initialize escape flags.
1375   clear_escape_info();
1376 }
1377 
1378 // Get a measure of how much mileage the method has on it.
1379 int MethodData::mileage_of(Method* method) {
1380   return MAX2(method->invocation_count(), method->backedge_count());
1381 }
1382 
1383 bool MethodData::is_mature() const {
1384   return CompilationPolicy::is_mature(_method);
1385 }
1386 
1387 // Translate a bci to its corresponding data index (di).
1388 address MethodData::bci_to_dp(int bci) {
1389   ResourceMark rm;
1390   DataLayout* data = data_layout_before(bci);
1391   DataLayout* prev = NULL;
1392   for ( ; is_valid(data); data = next_data_layout(data)) {
1393     if (data->bci() >= bci) {
1394       if (data->bci() == bci)  set_hint_di(dp_to_di((address)data));
1395       else if (prev != NULL)   set_hint_di(dp_to_di((address)prev));
1396       return (address)data;
1397     }
1398     prev = data;
1399   }
1400   return (address)limit_data_position();
1401 }
1402 
1403 // Translate a bci to its corresponding data, or NULL.
1404 ProfileData* MethodData::bci_to_data(int bci) {
1405   DataLayout* data = data_layout_before(bci);
1406   for ( ; is_valid(data); data = next_data_layout(data)) {
1407     if (data->bci() == bci) {
1408       set_hint_di(dp_to_di((address)data));
1409       return data->data_in();
1410     } else if (data->bci() > bci) {
1411       break;
1412     }
1413   }
1414   return bci_to_extra_data(bci, NULL, false);
1415 }
1416 
1417 DataLayout* MethodData::next_extra(DataLayout* dp) {
1418   int nb_cells = 0;
1419   switch(dp->tag()) {
1420   case DataLayout::bit_data_tag:
1421   case DataLayout::no_tag:
1422     nb_cells = BitData::static_cell_count();
1423     break;
1424   case DataLayout::speculative_trap_data_tag:
1425     nb_cells = SpeculativeTrapData::static_cell_count();
1426     break;
1427   default:
1428     fatal("unexpected tag %d", dp->tag());
1429   }
1430   return (DataLayout*)((address)dp + DataLayout::compute_size_in_bytes(nb_cells));
1431 }
1432 
1433 ProfileData* MethodData::bci_to_extra_data_helper(int bci, Method* m, DataLayout*& dp, bool concurrent) {
1434   DataLayout* end = args_data_limit();
1435 
1436   for (;; dp = next_extra(dp)) {
1437     assert(dp < end, "moved past end of extra data");
1438     // No need for "Atomic::load_acquire" ops,
1439     // since the data structure is monotonic.
1440     switch(dp->tag()) {
1441     case DataLayout::no_tag:
1442       return NULL;
1443     case DataLayout::arg_info_data_tag:
1444       dp = end;
1445       return NULL; // ArgInfoData is at the end of extra data section.
1446     case DataLayout::bit_data_tag:
1447       if (m == NULL && dp->bci() == bci) {
1448         return new BitData(dp);
1449       }
1450       break;
1451     case DataLayout::speculative_trap_data_tag:
1452       if (m != NULL) {
1453         SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1454         // data->method() may be null in case of a concurrent
1455         // allocation. Maybe it's for the same method. Try to use that
1456         // entry in that case.
1457         if (dp->bci() == bci) {
1458           if (data->method() == NULL) {
1459             assert(concurrent, "impossible because no concurrent allocation");
1460             return NULL;
1461           } else if (data->method() == m) {
1462             return data;
1463           }
1464         }
1465       }
1466       break;
1467     default:
1468       fatal("unexpected tag %d", dp->tag());
1469     }
1470   }
1471   return NULL;
1472 }
1473 
1474 
1475 // Translate a bci to its corresponding extra data, or NULL.
1476 ProfileData* MethodData::bci_to_extra_data(int bci, Method* m, bool create_if_missing) {
1477   // This code assumes an entry for a SpeculativeTrapData is 2 cells
1478   assert(2*DataLayout::compute_size_in_bytes(BitData::static_cell_count()) ==
1479          DataLayout::compute_size_in_bytes(SpeculativeTrapData::static_cell_count()),
1480          "code needs to be adjusted");
1481 
1482   // Do not create one of these if method has been redefined.
1483   if (m != NULL && m->is_old()) {
1484     return NULL;
1485   }
1486 
1487   DataLayout* dp  = extra_data_base();
1488   DataLayout* end = args_data_limit();
1489 
1490   // Allocation in the extra data space has to be atomic because not
1491   // all entries have the same size and non atomic concurrent
1492   // allocation would result in a corrupted extra data space.
1493   ProfileData* result = bci_to_extra_data_helper(bci, m, dp, true);
1494   if (result != NULL) {
1495     return result;
1496   }
1497 
1498   if (create_if_missing && dp < end) {
1499     MutexLocker ml(&_extra_data_lock);
1500     // Check again now that we have the lock. Another thread may
1501     // have added extra data entries.
1502     ProfileData* result = bci_to_extra_data_helper(bci, m, dp, false);
1503     if (result != NULL || dp >= end) {
1504       return result;
1505     }
1506 
1507     assert(dp->tag() == DataLayout::no_tag || (dp->tag() == DataLayout::speculative_trap_data_tag && m != NULL), "should be free");
1508     assert(next_extra(dp)->tag() == DataLayout::no_tag || next_extra(dp)->tag() == DataLayout::arg_info_data_tag, "should be free or arg info");
1509     u1 tag = m == NULL ? DataLayout::bit_data_tag : DataLayout::speculative_trap_data_tag;
1510     // SpeculativeTrapData is 2 slots. Make sure we have room.
1511     if (m != NULL && next_extra(dp)->tag() != DataLayout::no_tag) {
1512       return NULL;
1513     }
1514     DataLayout temp;
1515     temp.initialize(tag, bci, 0);
1516 
1517     dp->set_header(temp.header());
1518     assert(dp->tag() == tag, "sane");
1519     assert(dp->bci() == bci, "no concurrent allocation");
1520     if (tag == DataLayout::bit_data_tag) {
1521       return new BitData(dp);
1522     } else {
1523       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1524       data->set_method(m);
1525       return data;
1526     }
1527   }
1528   return NULL;
1529 }
1530 
1531 ArgInfoData *MethodData::arg_info() {
1532   DataLayout* dp    = extra_data_base();
1533   DataLayout* end   = args_data_limit();
1534   for (; dp < end; dp = next_extra(dp)) {
1535     if (dp->tag() == DataLayout::arg_info_data_tag)
1536       return new ArgInfoData(dp);
1537   }
1538   return NULL;
1539 }
1540 
1541 // Printing
1542 
1543 void MethodData::print_on(outputStream* st) const {
1544   assert(is_methodData(), "should be method data");
1545   st->print("method data for ");
1546   method()->print_value_on(st);
1547   st->cr();
1548   print_data_on(st);
1549 }
1550 
1551 void MethodData::print_value_on(outputStream* st) const {
1552   assert(is_methodData(), "should be method data");
1553   st->print("method data for ");
1554   method()->print_value_on(st);
1555 }
1556 
1557 void MethodData::print_data_on(outputStream* st) const {
1558   ResourceMark rm;
1559   ProfileData* data = first_data();
1560   if (_parameters_type_data_di != no_parameters) {
1561     parameters_type_data()->print_data_on(st);
1562   }
1563   for ( ; is_valid(data); data = next_data(data)) {
1564     st->print("%d", dp_to_di(data->dp()));
1565     st->fill_to(6);
1566     data->print_data_on(st, this);
1567   }
1568   st->print_cr("--- Extra data:");
1569   DataLayout* dp    = extra_data_base();
1570   DataLayout* end   = args_data_limit();
1571   for (;; dp = next_extra(dp)) {
1572     assert(dp < end, "moved past end of extra data");
1573     // No need for "Atomic::load_acquire" ops,
1574     // since the data structure is monotonic.
1575     switch(dp->tag()) {
1576     case DataLayout::no_tag:
1577       continue;
1578     case DataLayout::bit_data_tag:
1579       data = new BitData(dp);
1580       break;
1581     case DataLayout::speculative_trap_data_tag:
1582       data = new SpeculativeTrapData(dp);
1583       break;
1584     case DataLayout::arg_info_data_tag:
1585       data = new ArgInfoData(dp);
1586       dp = end; // ArgInfoData is at the end of extra data section.
1587       break;
1588     default:
1589       fatal("unexpected tag %d", dp->tag());
1590     }
1591     st->print("%d", dp_to_di(data->dp()));
1592     st->fill_to(6);
1593     data->print_data_on(st);
1594     if (dp >= end) return;
1595   }
1596 }
1597 
1598 // Verification
1599 
1600 void MethodData::verify_on(outputStream* st) {
1601   guarantee(is_methodData(), "object must be method data");
1602   // guarantee(m->is_perm(), "should be in permspace");
1603   this->verify_data_on(st);
1604 }
1605 
1606 void MethodData::verify_data_on(outputStream* st) {
1607   NEEDS_CLEANUP;
1608   // not yet implemented.
1609 }
1610 
1611 bool MethodData::profile_jsr292(const methodHandle& m, int bci) {
1612   if (m->is_compiled_lambda_form()) {
1613     return true;
1614   }
1615 
1616   Bytecode_invoke inv(m , bci);
1617   return inv.is_invokedynamic() || inv.is_invokehandle();
1618 }
1619 
1620 bool MethodData::profile_unsafe(const methodHandle& m, int bci) {
1621   Bytecode_invoke inv(m , bci);
1622   if (inv.is_invokevirtual()) {
1623     Symbol* klass = inv.klass();
1624     if (klass == vmSymbols::jdk_internal_misc_Unsafe() ||
1625         klass == vmSymbols::sun_misc_Unsafe() ||
1626         klass == vmSymbols::jdk_internal_misc_ScopedMemoryAccess()) {
1627       Symbol* name = inv.name();
1628       if (name->starts_with("get") || name->starts_with("put")) {
1629         return true;
1630       }
1631     }
1632   }
1633   return false;
1634 }
1635 
1636 int MethodData::profile_arguments_flag() {
1637   return TypeProfileLevel % 10;
1638 }
1639 
1640 bool MethodData::profile_arguments() {
1641   return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all;
1642 }
1643 
1644 bool MethodData::profile_arguments_jsr292_only() {
1645   return profile_arguments_flag() == type_profile_jsr292;
1646 }
1647 
1648 bool MethodData::profile_all_arguments() {
1649   return profile_arguments_flag() == type_profile_all;
1650 }
1651 
1652 bool MethodData::profile_arguments_for_invoke(const methodHandle& m, int bci) {
1653   if (!profile_arguments()) {
1654     return false;
1655   }
1656 
1657   if (profile_all_arguments()) {
1658     return true;
1659   }
1660 
1661   if (profile_unsafe(m, bci)) {
1662     return true;
1663   }
1664 
1665   assert(profile_arguments_jsr292_only(), "inconsistent");
1666   return profile_jsr292(m, bci);
1667 }
1668 
1669 int MethodData::profile_return_flag() {
1670   return (TypeProfileLevel % 100) / 10;
1671 }
1672 
1673 bool MethodData::profile_return() {
1674   return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1675 }
1676 
1677 bool MethodData::profile_return_jsr292_only() {
1678   return profile_return_flag() == type_profile_jsr292;
1679 }
1680 
1681 bool MethodData::profile_all_return() {
1682   return profile_return_flag() == type_profile_all;
1683 }
1684 
1685 bool MethodData::profile_return_for_invoke(const methodHandle& m, int bci) {
1686   if (!profile_return()) {
1687     return false;
1688   }
1689 
1690   if (profile_all_return()) {
1691     return true;
1692   }
1693 
1694   assert(profile_return_jsr292_only(), "inconsistent");
1695   return profile_jsr292(m, bci);
1696 }
1697 
1698 int MethodData::profile_parameters_flag() {
1699   return TypeProfileLevel / 100;
1700 }
1701 
1702 bool MethodData::profile_parameters() {
1703   return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all;
1704 }
1705 
1706 bool MethodData::profile_parameters_jsr292_only() {
1707   return profile_parameters_flag() == type_profile_jsr292;
1708 }
1709 
1710 bool MethodData::profile_all_parameters() {
1711   return profile_parameters_flag() == type_profile_all;
1712 }
1713 
1714 bool MethodData::profile_parameters_for_method(const methodHandle& m) {
1715   if (!profile_parameters()) {
1716     return false;
1717   }
1718 
1719   if (profile_all_parameters()) {
1720     return true;
1721   }
1722 
1723   assert(profile_parameters_jsr292_only(), "inconsistent");
1724   return m->is_compiled_lambda_form();
1725 }
1726 
1727 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) {
1728   log_trace(cds)("Iter(MethodData): %p", this);
1729   it->push(&_method);
1730 }
1731 
1732 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1733   if (shift == 0) {
1734     return;
1735   }
1736   if (!reset) {
1737     // Move all cells of trap entry at dp left by "shift" cells
1738     intptr_t* start = (intptr_t*)dp;
1739     intptr_t* end = (intptr_t*)next_extra(dp);
1740     for (intptr_t* ptr = start; ptr < end; ptr++) {
1741       *(ptr-shift) = *ptr;
1742     }
1743   } else {
1744     // Reset "shift" cells stopping at dp
1745     intptr_t* start = ((intptr_t*)dp) - shift;
1746     intptr_t* end = (intptr_t*)dp;
1747     for (intptr_t* ptr = start; ptr < end; ptr++) {
1748       *ptr = 0;
1749     }
1750   }
1751 }
1752 
1753 // Check for entries that reference an unloaded method
1754 class CleanExtraDataKlassClosure : public CleanExtraDataClosure {
1755   bool _always_clean;
1756 public:
1757   CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {}
1758   bool is_live(Method* m) {
1759     return !(_always_clean) && m->method_holder()->is_loader_alive();
1760   }
1761 };
1762 
1763 // Check for entries that reference a redefined method
1764 class CleanExtraDataMethodClosure : public CleanExtraDataClosure {
1765 public:
1766   CleanExtraDataMethodClosure() {}
1767   bool is_live(Method* m) { return !m->is_old(); }
1768 };
1769 
1770 
1771 // Remove SpeculativeTrapData entries that reference an unloaded or
1772 // redefined method
1773 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) {
1774   DataLayout* dp  = extra_data_base();
1775   DataLayout* end = args_data_limit();
1776 
1777   int shift = 0;
1778   for (; dp < end; dp = next_extra(dp)) {
1779     switch(dp->tag()) {
1780     case DataLayout::speculative_trap_data_tag: {
1781       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1782       Method* m = data->method();
1783       assert(m != NULL, "should have a method");
1784       if (!cl->is_live(m)) {
1785         // "shift" accumulates the number of cells for dead
1786         // SpeculativeTrapData entries that have been seen so
1787         // far. Following entries must be shifted left by that many
1788         // cells to remove the dead SpeculativeTrapData entries.
1789         shift += (int)((intptr_t*)next_extra(dp) - (intptr_t*)dp);
1790       } else {
1791         // Shift this entry left if it follows dead
1792         // SpeculativeTrapData entries
1793         clean_extra_data_helper(dp, shift);
1794       }
1795       break;
1796     }
1797     case DataLayout::bit_data_tag:
1798       // Shift this entry left if it follows dead SpeculativeTrapData
1799       // entries
1800       clean_extra_data_helper(dp, shift);
1801       continue;
1802     case DataLayout::no_tag:
1803     case DataLayout::arg_info_data_tag:
1804       // We are at end of the live trap entries. The previous "shift"
1805       // cells contain entries that are either dead or were shifted
1806       // left. They need to be reset to no_tag
1807       clean_extra_data_helper(dp, shift, true);
1808       return;
1809     default:
1810       fatal("unexpected tag %d", dp->tag());
1811     }
1812   }
1813 }
1814 
1815 // Verify there's no unloaded or redefined method referenced by a
1816 // SpeculativeTrapData entry
1817 void MethodData::verify_extra_data_clean(CleanExtraDataClosure* cl) {
1818 #ifdef ASSERT
1819   DataLayout* dp  = extra_data_base();
1820   DataLayout* end = args_data_limit();
1821 
1822   for (; dp < end; dp = next_extra(dp)) {
1823     switch(dp->tag()) {
1824     case DataLayout::speculative_trap_data_tag: {
1825       SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1826       Method* m = data->method();
1827       assert(m != NULL && cl->is_live(m), "Method should exist");
1828       break;
1829     }
1830     case DataLayout::bit_data_tag:
1831       continue;
1832     case DataLayout::no_tag:
1833     case DataLayout::arg_info_data_tag:
1834       return;
1835     default:
1836       fatal("unexpected tag %d", dp->tag());
1837     }
1838   }
1839 #endif
1840 }
1841 
1842 void MethodData::clean_method_data(bool always_clean) {
1843   ResourceMark rm;
1844   for (ProfileData* data = first_data();
1845        is_valid(data);
1846        data = next_data(data)) {
1847     data->clean_weak_klass_links(always_clean);
1848   }
1849   ParametersTypeData* parameters = parameters_type_data();
1850   if (parameters != NULL) {
1851     parameters->clean_weak_klass_links(always_clean);
1852   }
1853 
1854   CleanExtraDataKlassClosure cl(always_clean);
1855   clean_extra_data(&cl);
1856   verify_extra_data_clean(&cl);
1857 }
1858 
1859 // This is called during redefinition to clean all "old" redefined
1860 // methods out of MethodData for all methods.
1861 void MethodData::clean_weak_method_links() {
1862   ResourceMark rm;
1863   CleanExtraDataMethodClosure cl;
1864   clean_extra_data(&cl);
1865   verify_extra_data_clean(&cl);
1866 }