1 /*
   2  * Copyright (c) 2001, 2012, 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 "ci/ciMetadata.hpp"
  27 #include "ci/ciMethodData.hpp"
  28 #include "ci/ciReplay.hpp"
  29 #include "ci/ciUtilities.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "runtime/deoptimization.hpp"
  33 #include "utilities/copy.hpp"
  34 
  35 // ciMethodData
  36 
  37 // ------------------------------------------------------------------
  38 // ciMethodData::ciMethodData
  39 //
  40 ciMethodData::ciMethodData(MethodData* md) : ciMetadata(md) {
  41   assert(md != NULL, "no null method data");
  42   Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));
  43   _data = NULL;
  44   _data_size = 0;
  45   _extra_data_size = 0;
  46   _current_mileage = 0;
  47   _invocation_counter = 0;
  48   _backedge_counter = 0;
  49   _state = empty_state;
  50   _saw_free_extra_data = false;
  51   // Set an initial hint. Don't use set_hint_di() because
  52   // first_di() may be out of bounds if data_size is 0.
  53   _hint_di = first_di();
  54   // Initialize the escape information (to "don't know.");
  55   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
  56 }
  57 
  58 // ------------------------------------------------------------------
  59 // ciMethodData::ciMethodData
  60 //
  61 // No MethodData*.
  62 ciMethodData::ciMethodData() : ciMetadata(NULL) {
  63   Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord));
  64   _data = NULL;
  65   _data_size = 0;
  66   _extra_data_size = 0;
  67   _current_mileage = 0;
  68   _invocation_counter = 0;
  69   _backedge_counter = 0;
  70   _state = empty_state;
  71   _saw_free_extra_data = false;
  72   // Set an initial hint. Don't use set_hint_di() because
  73   // first_di() may be out of bounds if data_size is 0.
  74   _hint_di = first_di();
  75   // Initialize the escape information (to "don't know.");
  76   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
  77 }
  78 
  79 void ciMethodData::load_data() {
  80   MethodData* mdo = get_MethodData();
  81   if (mdo == NULL) {
  82     return;
  83   }
  84 
  85   // To do: don't copy the data if it is not "ripe" -- require a minimum #
  86   // of invocations.
  87 
  88   // Snapshot the data -- actually, take an approximate snapshot of
  89   // the data.  Any concurrently executing threads may be changing the
  90   // data as we copy it.
  91   Copy::disjoint_words((HeapWord*) mdo,
  92                        (HeapWord*) &_orig,
  93                        sizeof(_orig) / HeapWordSize);
  94   Arena* arena = CURRENT_ENV->arena();
  95   _data_size = mdo->data_size();
  96   _extra_data_size = mdo->extra_data_size();
  97   int total_size = _data_size + _extra_data_size;
  98   _data = (intptr_t *) arena->Amalloc(total_size);
  99   Copy::disjoint_words((HeapWord*) mdo->data_base(), (HeapWord*) _data, total_size / HeapWordSize);
 100 
 101   // Traverse the profile data, translating any oops into their
 102   // ci equivalents.
 103   ResourceMark rm;
 104   ciProfileData* ci_data = first_data();
 105   ProfileData* data = mdo->first_data();
 106   while (is_valid(ci_data)) {
 107     ci_data->translate_from(data);
 108     ci_data = next_data(ci_data);
 109     data = mdo->next_data(data);
 110   }
 111   // Note:  Extra data are all BitData, and do not need translation.
 112   _current_mileage = MethodData::mileage_of(mdo->method());
 113   _invocation_counter = mdo->invocation_count();
 114   _backedge_counter = mdo->backedge_count();
 115   _state = mdo->is_mature()? mature_state: immature_state;
 116 
 117   _eflags = mdo->eflags();
 118   _arg_local = mdo->arg_local();
 119   _arg_stack = mdo->arg_stack();
 120   _arg_returned  = mdo->arg_returned();
 121 #ifndef PRODUCT
 122   if (ReplayCompiles) {
 123     ciReplay::initialize(this);
 124   }
 125 #endif
 126 }
 127 
 128 void ciReceiverTypeData::translate_receiver_data_from(const ProfileData* data) {
 129   for (uint row = 0; row < row_limit(); row++) {
 130     Klass* k = data->as_ReceiverTypeData()->receiver(row);
 131     if (k != NULL) {
 132       ciKlass* klass = CURRENT_ENV->get_klass(k);
 133       set_receiver(row, klass);
 134     }
 135   }
 136 }
 137 
 138 
 139 void ciTypeStackSlotEntries::translate_type_data_from(const TypeStackSlotEntries* entries) {
 140   for (int i = 0; i < number_of_arguments(); i++) {
 141     intptr_t k = entries->type(i);
 142     TypeStackSlotEntries::set_type(i, translate_klass(k));
 143   }
 144 }
 145 
 146 // Get the data at an arbitrary (sort of) data index.
 147 ciProfileData* ciMethodData::data_at(int data_index) {
 148   if (out_of_bounds(data_index)) {
 149     return NULL;
 150   }
 151   DataLayout* data_layout = data_layout_at(data_index);
 152 
 153   switch (data_layout->tag()) {
 154   case DataLayout::no_tag:
 155   default:
 156     ShouldNotReachHere();
 157     return NULL;
 158   case DataLayout::bit_data_tag:
 159     return new ciBitData(data_layout);
 160   case DataLayout::counter_data_tag:
 161     return new ciCounterData(data_layout);
 162   case DataLayout::jump_data_tag:
 163     return new ciJumpData(data_layout);
 164   case DataLayout::receiver_type_data_tag:
 165     return new ciReceiverTypeData(data_layout);
 166   case DataLayout::virtual_call_data_tag:
 167     return new ciVirtualCallData(data_layout);
 168   case DataLayout::ret_data_tag:
 169     return new ciRetData(data_layout);
 170   case DataLayout::branch_data_tag:
 171     return new ciBranchData(data_layout);
 172   case DataLayout::multi_branch_data_tag:
 173     return new ciMultiBranchData(data_layout);
 174   case DataLayout::arg_info_data_tag:
 175     return new ciArgInfoData(data_layout);
 176   case DataLayout::call_type_data_tag:
 177     return new ciCallTypeData(data_layout);
 178   case DataLayout::virtual_call_type_data_tag:
 179     return new ciVirtualCallTypeData(data_layout);
 180   };
 181 }
 182 
 183 // Iteration over data.
 184 ciProfileData* ciMethodData::next_data(ciProfileData* current) {
 185   int current_index = dp_to_di(current->dp());
 186   int next_index = current_index + current->size_in_bytes();
 187   ciProfileData* next = data_at(next_index);
 188   return next;
 189 }
 190 
 191 // Translate a bci to its corresponding data, or NULL.
 192 ciProfileData* ciMethodData::bci_to_data(int bci) {
 193   ciProfileData* data = data_before(bci);
 194   for ( ; is_valid(data); data = next_data(data)) {
 195     if (data->bci() == bci) {
 196       set_hint_di(dp_to_di(data->dp()));
 197       return data;
 198     } else if (data->bci() > bci) {
 199       break;
 200     }
 201   }
 202   // bci_to_extra_data(bci) ...
 203   DataLayout* dp  = data_layout_at(data_size());
 204   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 205   for (; dp < end; dp = MethodData::next_extra(dp)) {
 206     if (dp->tag() == DataLayout::no_tag) {
 207       _saw_free_extra_data = true;  // observed an empty slot (common case)
 208       return NULL;
 209     }
 210     if (dp->tag() == DataLayout::arg_info_data_tag) {
 211       break; // ArgInfoData is at the end of extra data section.
 212     }
 213     if (dp->bci() == bci) {
 214       assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 215       return new ciBitData(dp);
 216     }
 217   }
 218   return NULL;
 219 }
 220 
 221 // Conservatively decode the trap_state of a ciProfileData.
 222 int ciMethodData::has_trap_at(ciProfileData* data, int reason) {
 223   typedef Deoptimization::DeoptReason DR_t;
 224   int per_bc_reason
 225     = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);
 226   if (trap_count(reason) == 0) {
 227     // Impossible for this trap to have occurred, regardless of trap_state.
 228     // Note:  This happens if the MDO is empty.
 229     return 0;
 230   } else if (per_bc_reason == Deoptimization::Reason_none) {
 231     // We cannot conclude anything; a trap happened somewhere, maybe here.
 232     return -1;
 233   } else if (data == NULL) {
 234     // No profile here, not even an extra_data record allocated on the fly.
 235     // If there are empty extra_data records, and there had been a trap,
 236     // there would have been a non-null data pointer.  If there are no
 237     // free extra_data records, we must return a conservative -1.
 238     if (_saw_free_extra_data)
 239       return 0;                 // Q.E.D.
 240     else
 241       return -1;                // bail with a conservative answer
 242   } else {
 243     return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);
 244   }
 245 }
 246 
 247 int ciMethodData::trap_recompiled_at(ciProfileData* data) {
 248   if (data == NULL) {
 249     return (_saw_free_extra_data? 0: -1);  // (see previous method)
 250   } else {
 251     return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;
 252   }
 253 }
 254 
 255 void ciMethodData::clear_escape_info() {
 256   VM_ENTRY_MARK;
 257   MethodData* mdo = get_MethodData();
 258   if (mdo != NULL) {
 259     mdo->clear_escape_info();
 260     ArgInfoData *aid = arg_info();
 261     int arg_count = (aid == NULL) ? 0 : aid->number_of_args();
 262     for (int i = 0; i < arg_count; i++) {
 263       set_arg_modified(i, 0);
 264     }
 265   }
 266   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
 267 }
 268 
 269 // copy our escape info to the MethodData* if it exists
 270 void ciMethodData::update_escape_info() {
 271   VM_ENTRY_MARK;
 272   MethodData* mdo = get_MethodData();
 273   if ( mdo != NULL) {
 274     mdo->set_eflags(_eflags);
 275     mdo->set_arg_local(_arg_local);
 276     mdo->set_arg_stack(_arg_stack);
 277     mdo->set_arg_returned(_arg_returned);
 278     int arg_count = mdo->method()->size_of_parameters();
 279     for (int i = 0; i < arg_count; i++) {
 280       mdo->set_arg_modified(i, arg_modified(i));
 281     }
 282   }
 283 }
 284 
 285 void ciMethodData::set_compilation_stats(short loops, short blocks) {
 286   VM_ENTRY_MARK;
 287   MethodData* mdo = get_MethodData();
 288   if (mdo != NULL) {
 289     mdo->set_num_loops(loops);
 290     mdo->set_num_blocks(blocks);
 291   }
 292 }
 293 
 294 void ciMethodData::set_would_profile(bool p) {
 295   VM_ENTRY_MARK;
 296   MethodData* mdo = get_MethodData();
 297   if (mdo != NULL) {
 298     mdo->set_would_profile(p);
 299   }
 300 }
 301 
 302 void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) {
 303   VM_ENTRY_MARK;
 304   MethodData* mdo = get_MethodData();
 305   if (mdo != NULL) {
 306     ProfileData* data = mdo->bci_to_data(bci);
 307     if (data->is_CallTypeData()) {
 308       data->as_CallTypeData()->set_argument_type(i, k->get_Klass());
 309     } else {
 310       assert(data->is_VirtualCallTypeData(), "no arguments!");
 311       data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass());
 312     }
 313   }
 314 }
 315 
 316 bool ciMethodData::has_escape_info() {
 317   return eflag_set(MethodData::estimated);
 318 }
 319 
 320 void ciMethodData::set_eflag(MethodData::EscapeFlag f) {
 321   set_bits(_eflags, f);
 322 }
 323 
 324 void ciMethodData::clear_eflag(MethodData::EscapeFlag f) {
 325   clear_bits(_eflags, f);
 326 }
 327 
 328 bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const {
 329   return mask_bits(_eflags, f) != 0;
 330 }
 331 
 332 void ciMethodData::set_arg_local(int i) {
 333   set_nth_bit(_arg_local, i);
 334 }
 335 
 336 void ciMethodData::set_arg_stack(int i) {
 337   set_nth_bit(_arg_stack, i);
 338 }
 339 
 340 void ciMethodData::set_arg_returned(int i) {
 341   set_nth_bit(_arg_returned, i);
 342 }
 343 
 344 void ciMethodData::set_arg_modified(int arg, uint val) {
 345   ArgInfoData *aid = arg_info();
 346   if (aid == NULL)
 347     return;
 348   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 349   aid->set_arg_modified(arg, val);
 350 }
 351 
 352 bool ciMethodData::is_arg_local(int i) const {
 353   return is_set_nth_bit(_arg_local, i);
 354 }
 355 
 356 bool ciMethodData::is_arg_stack(int i) const {
 357   return is_set_nth_bit(_arg_stack, i);
 358 }
 359 
 360 bool ciMethodData::is_arg_returned(int i) const {
 361   return is_set_nth_bit(_arg_returned, i);
 362 }
 363 
 364 uint ciMethodData::arg_modified(int arg) const {
 365   ArgInfoData *aid = arg_info();
 366   if (aid == NULL)
 367     return 0;
 368   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 369   return aid->arg_modified(arg);
 370 }
 371 
 372 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {
 373   // Get offset within MethodData* of the data array
 374   ByteSize data_offset = MethodData::data_offset();
 375 
 376   // Get cell offset of the ProfileData within data array
 377   int cell_offset = dp_to_di(data->dp());
 378 
 379   // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
 380   int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);
 381 
 382   return in_ByteSize(offset);
 383 }
 384 
 385 ciArgInfoData *ciMethodData::arg_info() const {
 386   // Should be last, have to skip all traps.
 387   DataLayout* dp  = data_layout_at(data_size());
 388   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 389   for (; dp < end; dp = MethodData::next_extra(dp)) {
 390     if (dp->tag() == DataLayout::arg_info_data_tag)
 391       return new ciArgInfoData(dp);
 392   }
 393   return NULL;
 394 }
 395 
 396 
 397 // Implementation of the print method.
 398 void ciMethodData::print_impl(outputStream* st) {
 399   ciMetadata::print_impl(st);
 400 }
 401 
 402 void ciMethodData::dump_replay_data(outputStream* out) {
 403   ResourceMark rm;
 404   MethodData* mdo = get_MethodData();
 405   Method* method = mdo->method();
 406   Klass* holder = method->method_holder();
 407   out->print("ciMethodData %s %s %s %d %d",
 408              holder->name()->as_quoted_ascii(),
 409              method->name()->as_quoted_ascii(),
 410              method->signature()->as_quoted_ascii(),
 411              _state,
 412              current_mileage());
 413 
 414   // dump the contents of the MDO header as raw data
 415   unsigned char* orig = (unsigned char*)&_orig;
 416   int length = sizeof(_orig);
 417   out->print(" orig %d", length);
 418   for (int i = 0; i < length; i++) {
 419     out->print(" %d", orig[i]);
 420   }
 421 
 422   // dump the MDO data as raw data
 423   int elements = data_size() / sizeof(intptr_t);
 424   out->print(" data %d", elements);
 425   for (int i = 0; i < elements; i++) {
 426     // We could use INTPTR_FORMAT here but that's a zero justified
 427     // which makes comparing it with the SA version of this output
 428     // harder.
 429 #ifdef _LP64
 430     out->print(" 0x%" FORMAT64_MODIFIER "x", data()[i]);
 431 #else
 432     out->print(" 0x%x", data()[i]);
 433 #endif
 434   }
 435 
 436   // The MDO contained oop references as ciObjects, so scan for those
 437   // and emit pairs of offset and klass name so that they can be
 438   // reconstructed at runtime.  The first round counts the number of
 439   // oop references and the second actually emits them.
 440   int count = 0;
 441   for (int round = 0; round < 2; round++) {
 442     if (round == 1) out->print(" oops %d", count);
 443     ProfileData* pdata = first_data();
 444     for ( ; is_valid(pdata); pdata = next_data(pdata)) {
 445       if (pdata->is_ReceiverTypeData()) {
 446         ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata;
 447         for (uint i = 0; i < vdata->row_limit(); i++) {
 448           ciKlass* k = vdata->receiver(i);
 449           if (k != NULL) {
 450             if (round == 0) {
 451               count++;
 452             } else {
 453               out->print(" %d %s", dp_to_di(vdata->dp() + in_bytes(vdata->receiver_offset(i))) / sizeof(intptr_t), k->name()->as_quoted_ascii());
 454             }
 455           }
 456         }
 457       } else if (pdata->is_VirtualCallData()) {
 458         ciVirtualCallData* vdata = (ciVirtualCallData*)pdata;
 459         for (uint i = 0; i < vdata->row_limit(); i++) {
 460           ciKlass* k = vdata->receiver(i);
 461           if (k != NULL) {
 462             if (round == 0) {
 463               count++;
 464             } else {
 465               out->print(" %d %s", dp_to_di(vdata->dp() + in_bytes(vdata->receiver_offset(i))) / sizeof(intptr_t), k->name()->as_quoted_ascii());
 466             }
 467           }
 468         }
 469       }
 470     }
 471   }
 472   out->cr();
 473 }
 474 
 475 #ifndef PRODUCT
 476 void ciMethodData::print() {
 477   print_data_on(tty);
 478 }
 479 
 480 void ciMethodData::print_data_on(outputStream* st) {
 481   ResourceMark rm;
 482   ciProfileData* data;
 483   for (data = first_data(); is_valid(data); data = next_data(data)) {
 484     st->print("%d", dp_to_di(data->dp()));
 485     st->fill_to(6);
 486     data->print_data_on(st);
 487   }
 488   st->print_cr("--- Extra data:");
 489   DataLayout* dp  = data_layout_at(data_size());
 490   DataLayout* end = data_layout_at(data_size() + extra_data_size());
 491   for (; dp < end; dp = MethodData::next_extra(dp)) {
 492     if (dp->tag() == DataLayout::no_tag)  continue;
 493     if (dp->tag() == DataLayout::bit_data_tag) {
 494       data = new BitData(dp);
 495     } else {
 496       assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
 497       data = new ciArgInfoData(dp);
 498       dp = end; // ArgInfoData is at the end of extra data section.
 499     }
 500     st->print("%d", dp_to_di(data->dp()));
 501     st->fill_to(6);
 502     data->print_data_on(st);
 503   }
 504 }
 505 
 506 void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) {
 507   if (TypeEntries::is_type_none(k)) {
 508     st->print("none");
 509   } else if (TypeEntries::is_type_unknown(k)) {
 510     st->print("unknown");
 511   } else {
 512     valid_ciklass(k)->print_name_on(st);
 513   }
 514   if (TypeEntries::was_null_seen(k)) {
 515     st->print(" (null seen)");
 516   }
 517 }
 518 
 519 void ciTypeStackSlotEntries::print_data_on(outputStream* st) const {
 520   _pd->tab(st, true);
 521   st->print("argument types");
 522   for (int i = 0; i < number_of_arguments(); i++) {
 523     _pd->tab(st);
 524     st->print("%d: stack (%u) ", i, stack_slot(i));
 525     print_ciklass(st, type(i));
 526     st->cr();
 527   }
 528 }
 529 
 530 void ciCallTypeData::print_data_on(outputStream* st) const {
 531   print_shared(st, "ciCallTypeData");
 532   args()->print_data_on(st);
 533 }
 534 
 535 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const {
 536   uint row;
 537   int entries = 0;
 538   for (row = 0; row < row_limit(); row++) {
 539     if (receiver(row) != NULL)  entries++;
 540   }
 541   st->print_cr("count(%u) entries(%u)", count(), entries);
 542   for (row = 0; row < row_limit(); row++) {
 543     if (receiver(row) != NULL) {
 544       tab(st);
 545       receiver(row)->print_name_on(st);
 546       st->print_cr("(%u)", receiver_count(row));
 547     }
 548   }
 549 }
 550 
 551 void ciReceiverTypeData::print_data_on(outputStream* st) const {
 552   print_shared(st, "ciReceiverTypeData");
 553   print_receiver_data_on(st);
 554 }
 555 
 556 void ciVirtualCallData::print_data_on(outputStream* st) const {
 557   print_shared(st, "ciVirtualCallData");
 558   rtd_super()->print_receiver_data_on(st);
 559 }
 560 
 561 void ciVirtualCallTypeData::print_data_on(outputStream* st) const {
 562   print_shared(st, "ciVirtualCallTypeData");
 563   rtd_super()->print_receiver_data_on(st);
 564   args()->print_data_on(st);
 565 }
 566 #endif