src/share/vm/oops/methodData.cpp

Tue, 08 Apr 2014 09:51:25 +0200

author
roland
date
Tue, 08 Apr 2014 09:51:25 +0200
changeset 9183
f95c67788f18
parent 7365
600c44255e5f
child 9203
53eec13fbaa5
child 9507
7e72702243a4
permissions
-rw-r--r--

8038636: speculative traps break when classes are redefined
Summary: remove speculative traps that point to methods that are redefined
Reviewed-by: kvn, twisti

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

mercurial