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