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