1 /*
   2  * Copyright (c) 2001, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciMethodData.cpp.incl"
  27 
  28 // ciMethodData
  29 
  30 // ------------------------------------------------------------------
  31 // ciMethodData::ciMethodData
  32 //
  33 ciMethodData::ciMethodData(methodDataHandle h_md) : ciObject(h_md) {
  34   assert(h_md() != NULL, "no null method data");
  35   Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));
  36   _data = NULL;
  37   _data_size = 0;
  38   _extra_data_size = 0;
  39   _current_mileage = 0;
  40   _invocation_counter = 0;
  41   _backedge_counter = 0;
  42   _state = empty_state;
  43   _saw_free_extra_data = false;
  44   // Set an initial hint. Don't use set_hint_di() because
  45   // first_di() may be out of bounds if data_size is 0.
  46   _hint_di = first_di();
  47   // Initialize the escape information (to "don't know.");
  48   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
  49 }
  50 
  51 // ------------------------------------------------------------------
  52 // ciMethodData::ciMethodData
  53 //
  54 // No methodDataOop.
  55 ciMethodData::ciMethodData() : ciObject() {
  56   Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));
  57   _data = NULL;
  58   _data_size = 0;
  59   _extra_data_size = 0;
  60   _current_mileage = 0;
  61   _invocation_counter = 0;
  62   _backedge_counter = 0;
  63   _state = empty_state;
  64   _saw_free_extra_data = false;
  65   // Set an initial hint. Don't use set_hint_di() because
  66   // first_di() may be out of bounds if data_size is 0.
  67   _hint_di = first_di();
  68   // Initialize the escape information (to "don't know.");
  69   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
  70 }
  71 
  72 void ciMethodData::load_data() {
  73   methodDataOop mdo = get_methodDataOop();
  74   if (mdo == NULL) return;
  75 
  76   // To do: don't copy the data if it is not "ripe" -- require a minimum #
  77   // of invocations.
  78 
  79   // Snapshot the data -- actually, take an approximate snapshot of
  80   // the data.  Any concurrently executing threads may be changing the
  81   // data as we copy it.
  82   int skip_header = oopDesc::header_size();
  83   Copy::disjoint_words((HeapWord*) mdo              + skip_header,
  84                        (HeapWord*) &_orig           + skip_header,
  85                        sizeof(_orig) / HeapWordSize - skip_header);
  86   DEBUG_ONLY(*_orig.adr_method() = NULL);  // no dangling oops, please
  87   Arena* arena = CURRENT_ENV->arena();
  88   _data_size = mdo->data_size();
  89   _extra_data_size = mdo->extra_data_size();
  90   int total_size = _data_size + _extra_data_size;
  91   _data = (intptr_t *) arena->Amalloc(total_size);
  92   Copy::disjoint_words((HeapWord*) mdo->data_base(), (HeapWord*) _data, total_size / HeapWordSize);
  93 
  94   // Traverse the profile data, translating any oops into their
  95   // ci equivalents.
  96   ResourceMark rm;
  97   ciProfileData* ci_data = first_data();
  98   ProfileData* data = mdo->first_data();
  99   while (is_valid(ci_data)) {
 100     ci_data->translate_from(data);
 101     ci_data = next_data(ci_data);
 102     data = mdo->next_data(data);
 103   }
 104   // Note:  Extra data are all BitData, and do not need translation.
 105   _current_mileage = methodDataOopDesc::mileage_of(mdo->method());
 106   _invocation_counter = mdo->invocation_count();
 107   _backedge_counter = mdo->backedge_count();
 108   _state = mdo->is_mature()? mature_state: immature_state;
 109 
 110   _eflags = mdo->eflags();
 111   _arg_local = mdo->arg_local();
 112   _arg_stack = mdo->arg_stack();
 113   _arg_returned  = mdo->arg_returned();
 114 }
 115 
 116 void ciReceiverTypeData::translate_receiver_data_from(ProfileData* data) {
 117   for (uint row = 0; row < row_limit(); row++) {
 118     klassOop k = data->as_ReceiverTypeData()->receiver(row);
 119     if (k != NULL) {
 120       ciKlass* klass = CURRENT_ENV->get_object(k)->as_klass();
 121       set_receiver(row, klass);
 122     }
 123   }
 124 }
 125 
 126 
 127 // Get the data at an arbitrary (sort of) data index.
 128 ciProfileData* ciMethodData::data_at(int data_index) {
 129   if (out_of_bounds(data_index)) {
 130     return NULL;
 131   }
 132   DataLayout* data_layout = data_layout_at(data_index);
 133 
 134   switch (data_layout->tag()) {
 135   case DataLayout::no_tag:
 136   default:
 137     ShouldNotReachHere();
 138     return NULL;
 139   case DataLayout::bit_data_tag:
 140     return new ciBitData(data_layout);
 141   case DataLayout::counter_data_tag:
 142     return new ciCounterData(data_layout);
 143   case DataLayout::jump_data_tag:
 144     return new ciJumpData(data_layout);
 145   case DataLayout::receiver_type_data_tag:
 146     return new ciReceiverTypeData(data_layout);
 147   case DataLayout::virtual_call_data_tag:
 148     return new ciVirtualCallData(data_layout);
 149   case DataLayout::ret_data_tag:
 150     return new ciRetData(data_layout);
 151   case DataLayout::branch_data_tag:
 152     return new ciBranchData(data_layout);
 153   case DataLayout::multi_branch_data_tag:
 154     return new ciMultiBranchData(data_layout);
 155   case DataLayout::arg_info_data_tag:
 156     return new ciArgInfoData(data_layout);
 157   };
 158 }
 159 
 160 // Iteration over data.
 161 ciProfileData* ciMethodData::next_data(ciProfileData* current) {
 162   int current_index = dp_to_di(current->dp());
 163   int next_index = current_index + current->size_in_bytes();
 164   ciProfileData* next = data_at(next_index);
 165   return next;
 166 }
 167 
 168 // Translate a bci to its corresponding data, or NULL.
 169 ciProfileData* ciMethodData::bci_to_data(int bci) {
 170   ciProfileData* data = data_before(bci);
 171   for ( ; is_valid(data); data = next_data(data)) {
 172     if (data->bci() == bci) {
 173       set_hint_di(dp_to_di(data->dp()));
 174       return data;
 175     } else if (data->bci() > bci) {
 176       break;
 177     }
 178   }
 179   // bci_to_extra_data(bci) ...
 180   DataLayout* dp  = data_layout_at(data_size());
 181   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 182   for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) {
 183     if (dp->tag() == DataLayout::no_tag) {
 184       _saw_free_extra_data = true;  // observed an empty slot (common case)
 185       return NULL;
 186     }
 187     if (dp->tag() == DataLayout::arg_info_data_tag) {
 188       break; // ArgInfoData is at the end of extra data section.
 189     }
 190     if (dp->bci() == bci) {
 191       assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 192       return new ciBitData(dp);
 193     }
 194   }
 195   return NULL;
 196 }
 197 
 198 // Conservatively decode the trap_state of a ciProfileData.
 199 int ciMethodData::has_trap_at(ciProfileData* data, int reason) {
 200   typedef Deoptimization::DeoptReason DR_t;
 201   int per_bc_reason
 202     = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);
 203   if (trap_count(reason) == 0) {
 204     // Impossible for this trap to have occurred, regardless of trap_state.
 205     // Note:  This happens if the MDO is empty.
 206     return 0;
 207   } else if (per_bc_reason == Deoptimization::Reason_none) {
 208     // We cannot conclude anything; a trap happened somewhere, maybe here.
 209     return -1;
 210   } else if (data == NULL) {
 211     // No profile here, not even an extra_data record allocated on the fly.
 212     // If there are empty extra_data records, and there had been a trap,
 213     // there would have been a non-null data pointer.  If there are no
 214     // free extra_data records, we must return a conservative -1.
 215     if (_saw_free_extra_data)
 216       return 0;                 // Q.E.D.
 217     else
 218       return -1;                // bail with a conservative answer
 219   } else {
 220     return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);
 221   }
 222 }
 223 
 224 int ciMethodData::trap_recompiled_at(ciProfileData* data) {
 225   if (data == NULL) {
 226     return (_saw_free_extra_data? 0: -1);  // (see previous method)
 227   } else {
 228     return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;
 229   }
 230 }
 231 
 232 void ciMethodData::clear_escape_info() {
 233   VM_ENTRY_MARK;
 234   methodDataOop mdo = get_methodDataOop();
 235   if (mdo != NULL) {
 236     mdo->clear_escape_info();
 237     ArgInfoData *aid = arg_info();
 238     int arg_count = (aid == NULL) ? 0 : aid->number_of_args();
 239     for (int i = 0; i < arg_count; i++) {
 240       set_arg_modified(i, 0);
 241     }
 242   }
 243   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
 244 }
 245 
 246 // copy our escape info to the methodDataOop if it exists
 247 void ciMethodData::update_escape_info() {
 248   VM_ENTRY_MARK;
 249   methodDataOop mdo = get_methodDataOop();
 250   if ( mdo != NULL) {
 251     mdo->set_eflags(_eflags);
 252     mdo->set_arg_local(_arg_local);
 253     mdo->set_arg_stack(_arg_stack);
 254     mdo->set_arg_returned(_arg_returned);
 255     int arg_count = mdo->method()->size_of_parameters();
 256     for (int i = 0; i < arg_count; i++) {
 257       mdo->set_arg_modified(i, arg_modified(i));
 258     }
 259   }
 260 }
 261 
 262 void ciMethodData::set_compilation_stats(short loops, short blocks) {
 263   VM_ENTRY_MARK;
 264   methodDataOop mdo = get_methodDataOop();
 265   if (mdo != NULL) {
 266     mdo->set_num_loops(loops);
 267     mdo->set_num_blocks(blocks);
 268   }
 269 }
 270 
 271 void ciMethodData::set_would_profile(bool p) {
 272   VM_ENTRY_MARK;
 273   methodDataOop mdo = get_methodDataOop();
 274   if (mdo != NULL) {
 275     mdo->set_would_profile(p);
 276   }
 277 }
 278 
 279 bool ciMethodData::has_escape_info() {
 280   return eflag_set(methodDataOopDesc::estimated);
 281 }
 282 
 283 void ciMethodData::set_eflag(methodDataOopDesc::EscapeFlag f) {
 284   set_bits(_eflags, f);
 285 }
 286 
 287 void ciMethodData::clear_eflag(methodDataOopDesc::EscapeFlag f) {
 288   clear_bits(_eflags, f);
 289 }
 290 
 291 bool ciMethodData::eflag_set(methodDataOopDesc::EscapeFlag f) const {
 292   return mask_bits(_eflags, f) != 0;
 293 }
 294 
 295 void ciMethodData::set_arg_local(int i) {
 296   set_nth_bit(_arg_local, i);
 297 }
 298 
 299 void ciMethodData::set_arg_stack(int i) {
 300   set_nth_bit(_arg_stack, i);
 301 }
 302 
 303 void ciMethodData::set_arg_returned(int i) {
 304   set_nth_bit(_arg_returned, i);
 305 }
 306 
 307 void ciMethodData::set_arg_modified(int arg, uint val) {
 308   ArgInfoData *aid = arg_info();
 309   if (aid == NULL)
 310     return;
 311   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 312   aid->set_arg_modified(arg, val);
 313 }
 314 
 315 bool ciMethodData::is_arg_local(int i) const {
 316   return is_set_nth_bit(_arg_local, i);
 317 }
 318 
 319 bool ciMethodData::is_arg_stack(int i) const {
 320   return is_set_nth_bit(_arg_stack, i);
 321 }
 322 
 323 bool ciMethodData::is_arg_returned(int i) const {
 324   return is_set_nth_bit(_arg_returned, i);
 325 }
 326 
 327 uint ciMethodData::arg_modified(int arg) const {
 328   ArgInfoData *aid = arg_info();
 329   if (aid == NULL)
 330     return 0;
 331   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 332   return aid->arg_modified(arg);
 333 }
 334 
 335 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {
 336   // Get offset within methodDataOop of the data array
 337   ByteSize data_offset = methodDataOopDesc::data_offset();
 338 
 339   // Get cell offset of the ProfileData within data array
 340   int cell_offset = dp_to_di(data->dp());
 341 
 342   // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
 343   int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);
 344 
 345   return in_ByteSize(offset);
 346 }
 347 
 348 ciArgInfoData *ciMethodData::arg_info() const {
 349   // Should be last, have to skip all traps.
 350   DataLayout* dp  = data_layout_at(data_size());
 351   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 352   for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) {
 353     if (dp->tag() == DataLayout::arg_info_data_tag)
 354       return new ciArgInfoData(dp);
 355   }
 356   return NULL;
 357 }
 358 
 359 
 360 // Implementation of the print method.
 361 void ciMethodData::print_impl(outputStream* st) {
 362   ciObject::print_impl(st);
 363 }
 364 
 365 #ifndef PRODUCT
 366 void ciMethodData::print() {
 367   print_data_on(tty);
 368 }
 369 
 370 void ciMethodData::print_data_on(outputStream* st) {
 371   ResourceMark rm;
 372   ciProfileData* data;
 373   for (data = first_data(); is_valid(data); data = next_data(data)) {
 374     st->print("%d", dp_to_di(data->dp()));
 375     st->fill_to(6);
 376     data->print_data_on(st);
 377   }
 378   st->print_cr("--- Extra data:");
 379   DataLayout* dp  = data_layout_at(data_size());
 380   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 381   for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) {
 382     if (dp->tag() == DataLayout::no_tag)  continue;
 383     if (dp->tag() == DataLayout::bit_data_tag) {
 384       data = new BitData(dp);
 385     } else {
 386       assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
 387       data = new ciArgInfoData(dp);
 388       dp = end; // ArgInfoData is at the end of extra data section.
 389     }
 390     st->print("%d", dp_to_di(data->dp()));
 391     st->fill_to(6);
 392     data->print_data_on(st);
 393   }
 394 }
 395 
 396 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) {
 397   uint row;
 398   int entries = 0;
 399   for (row = 0; row < row_limit(); row++) {
 400     if (receiver(row) != NULL)  entries++;
 401   }
 402   st->print_cr("count(%u) entries(%u)", count(), entries);
 403   for (row = 0; row < row_limit(); row++) {
 404     if (receiver(row) != NULL) {
 405       tab(st);
 406       receiver(row)->print_name_on(st);
 407       st->print_cr("(%u)", receiver_count(row));
 408     }
 409   }
 410 }
 411 
 412 void ciReceiverTypeData::print_data_on(outputStream* st) {
 413   print_shared(st, "ciReceiverTypeData");
 414   print_receiver_data_on(st);
 415 }
 416 
 417 void ciVirtualCallData::print_data_on(outputStream* st) {
 418   print_shared(st, "ciVirtualCallData");
 419   rtd_super()->print_receiver_data_on(st);
 420 }
 421 #endif