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