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