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