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