1 /*
   2  * Copyright (c) 2000, 2010, 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 "gc_implementation/shared/markSweep.inline.hpp"
  28 #include "interpreter/bytecode.hpp"
  29 #include "interpreter/bytecodeStream.hpp"
  30 #include "interpreter/linkResolver.hpp"
  31 #include "oops/methodDataOop.hpp"
  32 #include "oops/oop.inline.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);
  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 }
  60 
  61 void DataLayout::follow_weak_refs(BoolObjectClosure* cl) {
  62   ResourceMark m;
  63   data_in()->follow_weak_refs(cl);
  64 }
  65 
  66 
  67 // ==================================================================
  68 // ProfileData
  69 //
  70 // A ProfileData object is created to refer to a section of profiling
  71 // data in a structured way.
  72 
  73 // Constructor for invalid ProfileData.
  74 ProfileData::ProfileData() {
  75   _data = NULL;
  76 }
  77 
  78 #ifndef PRODUCT
  79 void ProfileData::print_shared(outputStream* st, const char* name) {
  80   st->print("bci: %d", bci());
  81   st->fill_to(tab_width_one);
  82   st->print("%s", name);
  83   tab(st);
  84   int trap = trap_state();
  85   if (trap != 0) {
  86     char buf[100];
  87     st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
  88   }
  89   int flags = data()->flags();
  90   if (flags != 0)
  91     st->print("flags(%d) ", flags);
  92 }
  93 
  94 void ProfileData::tab(outputStream* st) {
  95   st->fill_to(tab_width_two);
  96 }
  97 #endif // !PRODUCT
  98 
  99 // ==================================================================
 100 // BitData
 101 //
 102 // A BitData corresponds to a one-bit flag.  This is used to indicate
 103 // whether a checkcast bytecode has seen a null value.
 104 
 105 
 106 #ifndef PRODUCT
 107 void BitData::print_data_on(outputStream* st) {
 108   print_shared(st, "BitData");
 109 }
 110 #endif // !PRODUCT
 111 
 112 // ==================================================================
 113 // CounterData
 114 //
 115 // A CounterData corresponds to a simple counter.
 116 
 117 #ifndef PRODUCT
 118 void CounterData::print_data_on(outputStream* st) {
 119   print_shared(st, "CounterData");
 120   st->print_cr("count(%u)", count());
 121 }
 122 #endif // !PRODUCT
 123 
 124 // ==================================================================
 125 // JumpData
 126 //
 127 // A JumpData is used to access profiling information for a direct
 128 // branch.  It is a counter, used for counting the number of branches,
 129 // plus a data displacement, used for realigning the data pointer to
 130 // the corresponding target bci.
 131 
 132 void JumpData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 133   assert(stream->bci() == bci(), "wrong pos");
 134   int target;
 135   Bytecodes::Code c = stream->code();
 136   if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
 137     target = stream->dest_w();
 138   } else {
 139     target = stream->dest();
 140   }
 141   int my_di = mdo->dp_to_di(dp());
 142   int target_di = mdo->bci_to_di(target);
 143   int offset = target_di - my_di;
 144   set_displacement(offset);
 145 }
 146 
 147 #ifndef PRODUCT
 148 void JumpData::print_data_on(outputStream* st) {
 149   print_shared(st, "JumpData");
 150   st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
 151 }
 152 #endif // !PRODUCT
 153 
 154 // ==================================================================
 155 // ReceiverTypeData
 156 //
 157 // A ReceiverTypeData is used to access profiling information about a
 158 // dynamic type check.  It consists of a counter which counts the total times
 159 // that the check is reached, and a series of (klassOop, count) pairs
 160 // which are used to store a type profile for the receiver of the check.
 161 
 162 void ReceiverTypeData::follow_contents() {
 163   // This is a set of weak references that need
 164   // to be followed at the end of the strong marking
 165   // phase. Memoize this object so it can be visited
 166   // in the weak roots processing phase.
 167   MarkSweep::revisit_mdo(data());
 168 }
 169 
 170 #ifndef SERIALGC
 171 void ReceiverTypeData::follow_contents(ParCompactionManager* cm) {
 172   // This is a set of weak references that need
 173   // to be followed at the end of the strong marking
 174   // phase. Memoize this object so it can be visited
 175   // in the weak roots processing phase.
 176   PSParallelCompact::revisit_mdo(cm, data());
 177 }
 178 #endif // SERIALGC
 179 
 180 void ReceiverTypeData::oop_iterate(OopClosure* blk) {
 181   if (blk->should_remember_mdo()) {
 182     // This is a set of weak references that need
 183     // to be followed at the end of the strong marking
 184     // phase. Memoize this object so it can be visited
 185     // in the weak roots processing phase.
 186     blk->remember_mdo(data());
 187   } else { // normal scan
 188     for (uint row = 0; row < row_limit(); row++) {
 189       if (receiver(row) != NULL) {
 190         oop* adr = adr_receiver(row);
 191         blk->do_oop(adr);
 192       }
 193     }
 194   }
 195 }
 196 
 197 void ReceiverTypeData::oop_iterate_m(OopClosure* blk, MemRegion mr) {
 198   // Currently, this interface is called only during card-scanning for
 199   // a young gen gc, in which case this object cannot contribute anything,
 200   // since it does not contain any references that cross out of
 201   // the perm gen. However, for future more general use we allow
 202   // the possibility of calling for instance from more general
 203   // iterators (for example, a future regionalized perm gen for G1,
 204   // or the possibility of moving some references out of perm in
 205   // the case of other collectors). In that case, you will need
 206   // to relax or remove some of the assertions below.
 207 #ifdef ASSERT
 208   // Verify that none of the embedded oop references cross out of
 209   // this generation.
 210   for (uint row = 0; row < row_limit(); row++) {
 211     if (receiver(row) != NULL) {
 212       oop* adr = adr_receiver(row);
 213       CollectedHeap* h = Universe::heap();
 214       assert(h->is_permanent(adr) && h->is_permanent_or_null(*adr), "Not intra-perm");
 215     }
 216   }
 217 #endif // ASSERT
 218   assert(!blk->should_remember_mdo(), "Not expected to remember MDO");
 219   return;   // Nothing to do, see comment above
 220 #if 0
 221   if (blk->should_remember_mdo()) {
 222     // This is a set of weak references that need
 223     // to be followed at the end of the strong marking
 224     // phase. Memoize this object so it can be visited
 225     // in the weak roots processing phase.
 226     blk->remember_mdo(data());
 227   } else { // normal scan
 228     for (uint row = 0; row < row_limit(); row++) {
 229       if (receiver(row) != NULL) {
 230         oop* adr = adr_receiver(row);
 231         if (mr.contains(adr)) {
 232           blk->do_oop(adr);
 233         } else if ((HeapWord*)adr >= mr.end()) {
 234           // Test that the current cursor and the two ends of the range
 235           // that we may have skipped iterating over are monotonically ordered;
 236           // this is just a paranoid assertion, just in case represetations
 237           // should change in the future rendering the short-circuit return
 238           // here invalid.
 239           assert((row+1 >= row_limit() || adr_receiver(row+1) > adr) &&
 240                  (row+2 >= row_limit() || adr_receiver(row_limit()-1) > adr_receiver(row+1)), "Reducing?");
 241           break; // remaining should be outside this mr too
 242         }
 243       }
 244     }
 245   }
 246 #endif
 247 }
 248 
 249 void ReceiverTypeData::adjust_pointers() {
 250   for (uint row = 0; row < row_limit(); row++) {
 251     if (receiver(row) != NULL) {
 252       MarkSweep::adjust_pointer(adr_receiver(row));
 253     }
 254   }
 255 }
 256 
 257 void ReceiverTypeData::follow_weak_refs(BoolObjectClosure* is_alive_cl) {
 258   for (uint row = 0; row < row_limit(); row++) {
 259     klassOop p = receiver(row);
 260     if (p != NULL && !is_alive_cl->do_object_b(p)) {
 261       clear_row(row);
 262     }
 263   }
 264 }
 265 
 266 #ifndef SERIALGC
 267 void ReceiverTypeData::update_pointers() {
 268   for (uint row = 0; row < row_limit(); row++) {
 269     if (receiver_unchecked(row) != NULL) {
 270       PSParallelCompact::adjust_pointer(adr_receiver(row));
 271     }
 272   }
 273 }
 274 
 275 void ReceiverTypeData::update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {
 276   // The loop bounds could be computed based on beg_addr/end_addr and the
 277   // boundary test hoisted outside the loop (see klassVTable for an example);
 278   // however, row_limit() is small enough (2) to make that less efficient.
 279   for (uint row = 0; row < row_limit(); row++) {
 280     if (receiver_unchecked(row) != NULL) {
 281       PSParallelCompact::adjust_pointer(adr_receiver(row), beg_addr, end_addr);
 282     }
 283   }
 284 }
 285 #endif // SERIALGC
 286 
 287 #ifndef PRODUCT
 288 void ReceiverTypeData::print_receiver_data_on(outputStream* st) {
 289   uint row;
 290   int entries = 0;
 291   for (row = 0; row < row_limit(); row++) {
 292     if (receiver(row) != NULL)  entries++;
 293   }
 294   st->print_cr("count(%u) entries(%u)", count(), entries);
 295   int total = count();
 296   for (row = 0; row < row_limit(); row++) {
 297     if (receiver(row) != NULL) {
 298       total += receiver_count(row);
 299     }
 300   }
 301   for (row = 0; row < row_limit(); row++) {
 302     if (receiver(row) != NULL) {
 303       tab(st);
 304       receiver(row)->print_value_on(st);
 305       st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
 306     }
 307   }
 308 }
 309 void ReceiverTypeData::print_data_on(outputStream* st) {
 310   print_shared(st, "ReceiverTypeData");
 311   print_receiver_data_on(st);
 312 }
 313 void VirtualCallData::print_data_on(outputStream* st) {
 314   print_shared(st, "VirtualCallData");
 315   print_receiver_data_on(st);
 316 }
 317 #endif // !PRODUCT
 318 
 319 // ==================================================================
 320 // RetData
 321 //
 322 // A RetData is used to access profiling information for a ret bytecode.
 323 // It is composed of a count of the number of times that the ret has
 324 // been executed, followed by a series of triples of the form
 325 // (bci, count, di) which count the number of times that some bci was the
 326 // target of the ret and cache a corresponding displacement.
 327 
 328 void RetData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 329   for (uint row = 0; row < row_limit(); row++) {
 330     set_bci_displacement(row, -1);
 331     set_bci(row, no_bci);
 332   }
 333   // release so other threads see a consistent state.  bci is used as
 334   // a valid flag for bci_displacement.
 335   OrderAccess::release();
 336 }
 337 
 338 // This routine needs to atomically update the RetData structure, so the
 339 // caller needs to hold the RetData_lock before it gets here.  Since taking
 340 // the lock can block (and allow GC) and since RetData is a ProfileData is a
 341 // wrapper around a derived oop, taking the lock in _this_ method will
 342 // basically cause the 'this' pointer's _data field to contain junk after the
 343 // lock.  We require the caller to take the lock before making the ProfileData
 344 // structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
 345 address RetData::fixup_ret(int return_bci, methodDataHandle h_mdo) {
 346   // First find the mdp which corresponds to the return bci.
 347   address mdp = h_mdo->bci_to_dp(return_bci);
 348 
 349   // Now check to see if any of the cache slots are open.
 350   for (uint row = 0; row < row_limit(); row++) {
 351     if (bci(row) == no_bci) {
 352       set_bci_displacement(row, mdp - dp());
 353       set_bci_count(row, DataLayout::counter_increment);
 354       // Barrier to ensure displacement is written before the bci; allows
 355       // the interpreter to read displacement without fear of race condition.
 356       release_set_bci(row, return_bci);
 357       break;
 358     }
 359   }
 360   return mdp;
 361 }
 362 
 363 
 364 #ifndef PRODUCT
 365 void RetData::print_data_on(outputStream* st) {
 366   print_shared(st, "RetData");
 367   uint row;
 368   int entries = 0;
 369   for (row = 0; row < row_limit(); row++) {
 370     if (bci(row) != no_bci)  entries++;
 371   }
 372   st->print_cr("count(%u) entries(%u)", count(), entries);
 373   for (row = 0; row < row_limit(); row++) {
 374     if (bci(row) != no_bci) {
 375       tab(st);
 376       st->print_cr("bci(%d: count(%u) displacement(%d))",
 377                    bci(row), bci_count(row), bci_displacement(row));
 378     }
 379   }
 380 }
 381 #endif // !PRODUCT
 382 
 383 // ==================================================================
 384 // BranchData
 385 //
 386 // A BranchData is used to access profiling data for a two-way branch.
 387 // It consists of taken and not_taken counts as well as a data displacement
 388 // for the taken case.
 389 
 390 void BranchData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 391   assert(stream->bci() == bci(), "wrong pos");
 392   int target = stream->dest();
 393   int my_di = mdo->dp_to_di(dp());
 394   int target_di = mdo->bci_to_di(target);
 395   int offset = target_di - my_di;
 396   set_displacement(offset);
 397 }
 398 
 399 #ifndef PRODUCT
 400 void BranchData::print_data_on(outputStream* st) {
 401   print_shared(st, "BranchData");
 402   st->print_cr("taken(%u) displacement(%d)",
 403                taken(), displacement());
 404   tab(st);
 405   st->print_cr("not taken(%u)", not_taken());
 406 }
 407 #endif
 408 
 409 // ==================================================================
 410 // MultiBranchData
 411 //
 412 // A MultiBranchData is used to access profiling information for
 413 // a multi-way branch (*switch bytecodes).  It consists of a series
 414 // of (count, displacement) pairs, which count the number of times each
 415 // case was taken and specify the data displacment for each branch target.
 416 
 417 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
 418   int cell_count = 0;
 419   if (stream->code() == Bytecodes::_tableswitch) {
 420     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 421     cell_count = 1 + per_case_cell_count * (1 + sw->length()); // 1 for default
 422   } else {
 423     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 424     cell_count = 1 + per_case_cell_count * (sw->number_of_pairs() + 1); // 1 for default
 425   }
 426   return cell_count;
 427 }
 428 
 429 void MultiBranchData::post_initialize(BytecodeStream* stream,
 430                                       methodDataOop mdo) {
 431   assert(stream->bci() == bci(), "wrong pos");
 432   int target;
 433   int my_di;
 434   int target_di;
 435   int offset;
 436   if (stream->code() == Bytecodes::_tableswitch) {
 437     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 438     int len = sw->length();
 439     assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
 440     for (int count = 0; count < len; count++) {
 441       target = sw->dest_offset_at(count) + bci();
 442       my_di = mdo->dp_to_di(dp());
 443       target_di = mdo->bci_to_di(target);
 444       offset = target_di - my_di;
 445       set_displacement_at(count, offset);
 446     }
 447     target = sw->default_offset() + bci();
 448     my_di = mdo->dp_to_di(dp());
 449     target_di = mdo->bci_to_di(target);
 450     offset = target_di - my_di;
 451     set_default_displacement(offset);
 452 
 453   } else {
 454     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 455     int npairs = sw->number_of_pairs();
 456     assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
 457     for (int count = 0; count < npairs; count++) {
 458       LookupswitchPair *pair = sw->pair_at(count);
 459       target = pair->offset() + bci();
 460       my_di = mdo->dp_to_di(dp());
 461       target_di = mdo->bci_to_di(target);
 462       offset = target_di - my_di;
 463       set_displacement_at(count, offset);
 464     }
 465     target = sw->default_offset() + bci();
 466     my_di = mdo->dp_to_di(dp());
 467     target_di = mdo->bci_to_di(target);
 468     offset = target_di - my_di;
 469     set_default_displacement(offset);
 470   }
 471 }
 472 
 473 #ifndef PRODUCT
 474 void MultiBranchData::print_data_on(outputStream* st) {
 475   print_shared(st, "MultiBranchData");
 476   st->print_cr("default_count(%u) displacement(%d)",
 477                default_count(), default_displacement());
 478   int cases = number_of_cases();
 479   for (int i = 0; i < cases; i++) {
 480     tab(st);
 481     st->print_cr("count(%u) displacement(%d)",
 482                  count_at(i), displacement_at(i));
 483   }
 484 }
 485 #endif
 486 
 487 #ifndef PRODUCT
 488 void ArgInfoData::print_data_on(outputStream* st) {
 489   print_shared(st, "ArgInfoData");
 490   int nargs = number_of_args();
 491   for (int i = 0; i < nargs; i++) {
 492     st->print("  0x%x", arg_modified(i));
 493   }
 494   st->cr();
 495 }
 496 
 497 #endif
 498 // ==================================================================
 499 // methodDataOop
 500 //
 501 // A methodDataOop holds information which has been collected about
 502 // a method.
 503 
 504 int methodDataOopDesc::bytecode_cell_count(Bytecodes::Code code) {
 505   switch (code) {
 506   case Bytecodes::_checkcast:
 507   case Bytecodes::_instanceof:
 508   case Bytecodes::_aastore:
 509     if (TypeProfileCasts) {
 510       return ReceiverTypeData::static_cell_count();
 511     } else {
 512       return BitData::static_cell_count();
 513     }
 514   case Bytecodes::_invokespecial:
 515   case Bytecodes::_invokestatic:
 516     return CounterData::static_cell_count();
 517   case Bytecodes::_goto:
 518   case Bytecodes::_goto_w:
 519   case Bytecodes::_jsr:
 520   case Bytecodes::_jsr_w:
 521     return JumpData::static_cell_count();
 522   case Bytecodes::_invokevirtual:
 523   case Bytecodes::_invokeinterface:
 524     return VirtualCallData::static_cell_count();
 525   case Bytecodes::_invokedynamic:
 526     return CounterData::static_cell_count();
 527   case Bytecodes::_ret:
 528     return RetData::static_cell_count();
 529   case Bytecodes::_ifeq:
 530   case Bytecodes::_ifne:
 531   case Bytecodes::_iflt:
 532   case Bytecodes::_ifge:
 533   case Bytecodes::_ifgt:
 534   case Bytecodes::_ifle:
 535   case Bytecodes::_if_icmpeq:
 536   case Bytecodes::_if_icmpne:
 537   case Bytecodes::_if_icmplt:
 538   case Bytecodes::_if_icmpge:
 539   case Bytecodes::_if_icmpgt:
 540   case Bytecodes::_if_icmple:
 541   case Bytecodes::_if_acmpeq:
 542   case Bytecodes::_if_acmpne:
 543   case Bytecodes::_ifnull:
 544   case Bytecodes::_ifnonnull:
 545     return BranchData::static_cell_count();
 546   case Bytecodes::_lookupswitch:
 547   case Bytecodes::_tableswitch:
 548     return variable_cell_count;
 549   }
 550   return no_profile_data;
 551 }
 552 
 553 // Compute the size of the profiling information corresponding to
 554 // the current bytecode.
 555 int methodDataOopDesc::compute_data_size(BytecodeStream* stream) {
 556   int cell_count = bytecode_cell_count(stream->code());
 557   if (cell_count == no_profile_data) {
 558     return 0;
 559   }
 560   if (cell_count == variable_cell_count) {
 561     cell_count = MultiBranchData::compute_cell_count(stream);
 562   }
 563   // Note:  cell_count might be zero, meaning that there is just
 564   //        a DataLayout header, with no extra cells.
 565   assert(cell_count >= 0, "sanity");
 566   return DataLayout::compute_size_in_bytes(cell_count);
 567 }
 568 
 569 int methodDataOopDesc::compute_extra_data_count(int data_size, int empty_bc_count) {
 570   if (ProfileTraps) {
 571     // Assume that up to 3% of BCIs with no MDP will need to allocate one.
 572     int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
 573     // If the method is large, let the extra BCIs grow numerous (to ~1%).
 574     int one_percent_of_data
 575       = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
 576     if (extra_data_count < one_percent_of_data)
 577       extra_data_count = one_percent_of_data;
 578     if (extra_data_count > empty_bc_count)
 579       extra_data_count = empty_bc_count;  // no need for more
 580     return extra_data_count;
 581   } else {
 582     return 0;
 583   }
 584 }
 585 
 586 // Compute the size of the methodDataOop necessary to store
 587 // profiling information about a given method.  Size is in bytes.
 588 int methodDataOopDesc::compute_allocation_size_in_bytes(methodHandle method) {
 589   int data_size = 0;
 590   BytecodeStream stream(method);
 591   Bytecodes::Code c;
 592   int empty_bc_count = 0;  // number of bytecodes lacking data
 593   while ((c = stream.next()) >= 0) {
 594     int size_in_bytes = compute_data_size(&stream);
 595     data_size += size_in_bytes;
 596     if (size_in_bytes == 0)  empty_bc_count += 1;
 597   }
 598   int object_size = in_bytes(data_offset()) + data_size;
 599 
 600   // Add some extra DataLayout cells (at least one) to track stray traps.
 601   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 602   object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
 603 
 604   // Add a cell to record information about modified arguments.
 605   int arg_size = method->size_of_parameters();
 606   object_size += DataLayout::compute_size_in_bytes(arg_size+1);
 607   return object_size;
 608 }
 609 
 610 // Compute the size of the methodDataOop necessary to store
 611 // profiling information about a given method.  Size is in words
 612 int methodDataOopDesc::compute_allocation_size_in_words(methodHandle method) {
 613   int byte_size = compute_allocation_size_in_bytes(method);
 614   int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
 615   return align_object_size(word_size);
 616 }
 617 
 618 // Initialize an individual data segment.  Returns the size of
 619 // the segment in bytes.
 620 int methodDataOopDesc::initialize_data(BytecodeStream* stream,
 621                                        int data_index) {
 622   int cell_count = -1;
 623   int tag = DataLayout::no_tag;
 624   DataLayout* data_layout = data_layout_at(data_index);
 625   Bytecodes::Code c = stream->code();
 626   switch (c) {
 627   case Bytecodes::_checkcast:
 628   case Bytecodes::_instanceof:
 629   case Bytecodes::_aastore:
 630     if (TypeProfileCasts) {
 631       cell_count = ReceiverTypeData::static_cell_count();
 632       tag = DataLayout::receiver_type_data_tag;
 633     } else {
 634       cell_count = BitData::static_cell_count();
 635       tag = DataLayout::bit_data_tag;
 636     }
 637     break;
 638   case Bytecodes::_invokespecial:
 639   case Bytecodes::_invokestatic:
 640     cell_count = CounterData::static_cell_count();
 641     tag = DataLayout::counter_data_tag;
 642     break;
 643   case Bytecodes::_goto:
 644   case Bytecodes::_goto_w:
 645   case Bytecodes::_jsr:
 646   case Bytecodes::_jsr_w:
 647     cell_count = JumpData::static_cell_count();
 648     tag = DataLayout::jump_data_tag;
 649     break;
 650   case Bytecodes::_invokevirtual:
 651   case Bytecodes::_invokeinterface:
 652     cell_count = VirtualCallData::static_cell_count();
 653     tag = DataLayout::virtual_call_data_tag;
 654     break;
 655   case Bytecodes::_invokedynamic:
 656     // %%% should make a type profile for any invokedynamic that takes a ref argument
 657     cell_count = CounterData::static_cell_count();
 658     tag = DataLayout::counter_data_tag;
 659     break;
 660   case Bytecodes::_ret:
 661     cell_count = RetData::static_cell_count();
 662     tag = DataLayout::ret_data_tag;
 663     break;
 664   case Bytecodes::_ifeq:
 665   case Bytecodes::_ifne:
 666   case Bytecodes::_iflt:
 667   case Bytecodes::_ifge:
 668   case Bytecodes::_ifgt:
 669   case Bytecodes::_ifle:
 670   case Bytecodes::_if_icmpeq:
 671   case Bytecodes::_if_icmpne:
 672   case Bytecodes::_if_icmplt:
 673   case Bytecodes::_if_icmpge:
 674   case Bytecodes::_if_icmpgt:
 675   case Bytecodes::_if_icmple:
 676   case Bytecodes::_if_acmpeq:
 677   case Bytecodes::_if_acmpne:
 678   case Bytecodes::_ifnull:
 679   case Bytecodes::_ifnonnull:
 680     cell_count = BranchData::static_cell_count();
 681     tag = DataLayout::branch_data_tag;
 682     break;
 683   case Bytecodes::_lookupswitch:
 684   case Bytecodes::_tableswitch:
 685     cell_count = MultiBranchData::compute_cell_count(stream);
 686     tag = DataLayout::multi_branch_data_tag;
 687     break;
 688   }
 689   assert(tag == DataLayout::multi_branch_data_tag ||
 690          cell_count == bytecode_cell_count(c), "cell counts must agree");
 691   if (cell_count >= 0) {
 692     assert(tag != DataLayout::no_tag, "bad tag");
 693     assert(bytecode_has_profile(c), "agree w/ BHP");
 694     data_layout->initialize(tag, stream->bci(), cell_count);
 695     return DataLayout::compute_size_in_bytes(cell_count);
 696   } else {
 697     assert(!bytecode_has_profile(c), "agree w/ !BHP");
 698     return 0;
 699   }
 700 }
 701 
 702 // Get the data at an arbitrary (sort of) data index.
 703 ProfileData* methodDataOopDesc::data_at(int data_index) {
 704   if (out_of_bounds(data_index)) {
 705     return NULL;
 706   }
 707   DataLayout* data_layout = data_layout_at(data_index);
 708   return data_layout->data_in();
 709 }
 710 
 711 ProfileData* DataLayout::data_in() {
 712   switch (tag()) {
 713   case DataLayout::no_tag:
 714   default:
 715     ShouldNotReachHere();
 716     return NULL;
 717   case DataLayout::bit_data_tag:
 718     return new BitData(this);
 719   case DataLayout::counter_data_tag:
 720     return new CounterData(this);
 721   case DataLayout::jump_data_tag:
 722     return new JumpData(this);
 723   case DataLayout::receiver_type_data_tag:
 724     return new ReceiverTypeData(this);
 725   case DataLayout::virtual_call_data_tag:
 726     return new VirtualCallData(this);
 727   case DataLayout::ret_data_tag:
 728     return new RetData(this);
 729   case DataLayout::branch_data_tag:
 730     return new BranchData(this);
 731   case DataLayout::multi_branch_data_tag:
 732     return new MultiBranchData(this);
 733   case DataLayout::arg_info_data_tag:
 734     return new ArgInfoData(this);
 735   };
 736 }
 737 
 738 // Iteration over data.
 739 ProfileData* methodDataOopDesc::next_data(ProfileData* current) {
 740   int current_index = dp_to_di(current->dp());
 741   int next_index = current_index + current->size_in_bytes();
 742   ProfileData* next = data_at(next_index);
 743   return next;
 744 }
 745 
 746 // Give each of the data entries a chance to perform specific
 747 // data initialization.
 748 void methodDataOopDesc::post_initialize(BytecodeStream* stream) {
 749   ResourceMark rm;
 750   ProfileData* data;
 751   for (data = first_data(); is_valid(data); data = next_data(data)) {
 752     stream->set_start(data->bci());
 753     stream->next();
 754     data->post_initialize(stream, this);
 755   }
 756 }
 757 
 758 // Initialize the methodDataOop corresponding to a given method.
 759 void methodDataOopDesc::initialize(methodHandle method) {
 760   ResourceMark rm;
 761   // Set the method back-pointer.
 762   _method = method();
 763 
 764   if (TieredCompilation) {
 765     _invocation_counter.init();
 766     _backedge_counter.init();
 767     _num_loops = 0;
 768     _num_blocks = 0;
 769     _highest_comp_level = 0;
 770     _highest_osr_comp_level = 0;
 771     _would_profile = false;
 772   }
 773   set_creation_mileage(mileage_of(method()));
 774 
 775   // Initialize flags and trap history.
 776   _nof_decompiles = 0;
 777   _nof_overflow_recompiles = 0;
 778   _nof_overflow_traps = 0;
 779   assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
 780   Copy::zero_to_words((HeapWord*) &_trap_hist,
 781                       sizeof(_trap_hist) / sizeof(HeapWord));
 782 
 783   // Go through the bytecodes and allocate and initialize the
 784   // corresponding data cells.
 785   int data_size = 0;
 786   int empty_bc_count = 0;  // number of bytecodes lacking data
 787   BytecodeStream stream(method);
 788   Bytecodes::Code c;
 789   while ((c = stream.next()) >= 0) {
 790     int size_in_bytes = initialize_data(&stream, data_size);
 791     data_size += size_in_bytes;
 792     if (size_in_bytes == 0)  empty_bc_count += 1;
 793   }
 794   _data_size = data_size;
 795   int object_size = in_bytes(data_offset()) + data_size;
 796 
 797   // Add some extra DataLayout cells (at least one) to track stray traps.
 798   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 799   int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
 800 
 801   // Add a cell to record information about modified arguments.
 802   // Set up _args_modified array after traps cells so that
 803   // the code for traps cells works.
 804   DataLayout *dp = data_layout_at(data_size + extra_size);
 805 
 806   int arg_size = method->size_of_parameters();
 807   dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
 808 
 809   object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
 810 
 811   // Set an initial hint. Don't use set_hint_di() because
 812   // first_di() may be out of bounds if data_size is 0.
 813   // In that situation, _hint_di is never used, but at
 814   // least well-defined.
 815   _hint_di = first_di();
 816 
 817   post_initialize(&stream);
 818 
 819   set_object_is_parsable(object_size);
 820 }
 821 
 822 // Get a measure of how much mileage the method has on it.
 823 int methodDataOopDesc::mileage_of(methodOop method) {
 824   int mileage = 0;
 825   if (TieredCompilation) {
 826     mileage = MAX2(method->invocation_count(), method->backedge_count());
 827   } else {
 828     int iic = method->interpreter_invocation_count();
 829     if (mileage < iic)  mileage = iic;
 830     InvocationCounter* ic = method->invocation_counter();
 831     InvocationCounter* bc = method->backedge_counter();
 832     int icval = ic->count();
 833     if (ic->carry()) icval += CompileThreshold;
 834     if (mileage < icval)  mileage = icval;
 835     int bcval = bc->count();
 836     if (bc->carry()) bcval += CompileThreshold;
 837     if (mileage < bcval)  mileage = bcval;
 838   }
 839   return mileage;
 840 }
 841 
 842 bool methodDataOopDesc::is_mature() const {
 843   return CompilationPolicy::policy()->is_mature(_method);
 844 }
 845 
 846 // Translate a bci to its corresponding data index (di).
 847 address methodDataOopDesc::bci_to_dp(int bci) {
 848   ResourceMark rm;
 849   ProfileData* data = data_before(bci);
 850   ProfileData* prev = NULL;
 851   for ( ; is_valid(data); data = next_data(data)) {
 852     if (data->bci() >= bci) {
 853       if (data->bci() == bci)  set_hint_di(dp_to_di(data->dp()));
 854       else if (prev != NULL)   set_hint_di(dp_to_di(prev->dp()));
 855       return data->dp();
 856     }
 857     prev = data;
 858   }
 859   return (address)limit_data_position();
 860 }
 861 
 862 // Translate a bci to its corresponding data, or NULL.
 863 ProfileData* methodDataOopDesc::bci_to_data(int bci) {
 864   ProfileData* data = data_before(bci);
 865   for ( ; is_valid(data); data = next_data(data)) {
 866     if (data->bci() == bci) {
 867       set_hint_di(dp_to_di(data->dp()));
 868       return data;
 869     } else if (data->bci() > bci) {
 870       break;
 871     }
 872   }
 873   return bci_to_extra_data(bci, false);
 874 }
 875 
 876 // Translate a bci to its corresponding extra data, or NULL.
 877 ProfileData* methodDataOopDesc::bci_to_extra_data(int bci, bool create_if_missing) {
 878   DataLayout* dp    = extra_data_base();
 879   DataLayout* end   = extra_data_limit();
 880   DataLayout* avail = NULL;
 881   for (; dp < end; dp = next_extra(dp)) {
 882     // No need for "OrderAccess::load_acquire" ops,
 883     // since the data structure is monotonic.
 884     if (dp->tag() == DataLayout::no_tag)  break;
 885     if (dp->tag() == DataLayout::arg_info_data_tag) {
 886       dp = end; // ArgInfoData is at the end of extra data section.
 887       break;
 888     }
 889     if (dp->bci() == bci) {
 890       assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 891       return new BitData(dp);
 892     }
 893   }
 894   if (create_if_missing && dp < end) {
 895     // Allocate this one.  There is no mutual exclusion,
 896     // so two threads could allocate different BCIs to the
 897     // same data layout.  This means these extra data
 898     // records, like most other MDO contents, must not be
 899     // trusted too much.
 900     DataLayout temp;
 901     temp.initialize(DataLayout::bit_data_tag, bci, 0);
 902     dp->release_set_header(temp.header());
 903     assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 904     //NO: assert(dp->bci() == bci, "no concurrent allocation");
 905     return new BitData(dp);
 906   }
 907   return NULL;
 908 }
 909 
 910 ArgInfoData *methodDataOopDesc::arg_info() {
 911   DataLayout* dp    = extra_data_base();
 912   DataLayout* end   = extra_data_limit();
 913   for (; dp < end; dp = next_extra(dp)) {
 914     if (dp->tag() == DataLayout::arg_info_data_tag)
 915       return new ArgInfoData(dp);
 916   }
 917   return NULL;
 918 }
 919 
 920 #ifndef PRODUCT
 921 void methodDataOopDesc::print_data_on(outputStream* st) {
 922   ResourceMark rm;
 923   ProfileData* data = first_data();
 924   for ( ; is_valid(data); data = next_data(data)) {
 925     st->print("%d", dp_to_di(data->dp()));
 926     st->fill_to(6);
 927     data->print_data_on(st);
 928   }
 929   st->print_cr("--- Extra data:");
 930   DataLayout* dp    = extra_data_base();
 931   DataLayout* end   = extra_data_limit();
 932   for (; dp < end; dp = next_extra(dp)) {
 933     // No need for "OrderAccess::load_acquire" ops,
 934     // since the data structure is monotonic.
 935     if (dp->tag() == DataLayout::no_tag)  continue;
 936     if (dp->tag() == DataLayout::bit_data_tag) {
 937       data = new BitData(dp);
 938     } else {
 939       assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
 940       data = new ArgInfoData(dp);
 941       dp = end; // ArgInfoData is at the end of extra data section.
 942     }
 943     st->print("%d", dp_to_di(data->dp()));
 944     st->fill_to(6);
 945     data->print_data_on(st);
 946   }
 947 }
 948 #endif
 949 
 950 void methodDataOopDesc::verify_data_on(outputStream* st) {
 951   NEEDS_CLEANUP;
 952   // not yet implemented.
 953 }