1 /*
   2  * Copyright (c) 2001, 2023, 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.inline.hpp"
  30 #include "compiler/compiler_globals.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "oops/klass.inline.hpp"
  34 #include "runtime/deoptimization.hpp"
  35 #include "utilities/copy.hpp"
  36 
  37 // ciMethodData
  38 
  39 // ------------------------------------------------------------------
  40 // ciMethodData::ciMethodData
  41 //
  42 ciMethodData::ciMethodData(MethodData* md)
  43 : ciMetadata(md),
  44   _data_size(0), _extra_data_size(0), _data(nullptr),
  45   _parameters_data_offset(0),
  46   _exception_handlers_data_offset(0),
  47   // Set an initial hint. Don't use set_hint_di() because
  48   // first_di() may be out of bounds if data_size is 0.
  49   _hint_di(first_di()),
  50   _state(empty_state),
  51   _saw_free_extra_data(false),
  52   // Initialize the escape information (to "don't know.");
  53   _eflags(0), _arg_local(0), _arg_stack(0), _arg_returned(0),
  54   _invocation_counter(0),
  55   _orig() {}
  56 
  57 // Check for entries that reference an unloaded method
  58 class PrepareExtraDataClosure : public CleanExtraDataClosure {
  59   MethodData*            _mdo;
  60   SafepointStateTracker  _safepoint_tracker;
  61   GrowableArray<Method*> _uncached_methods;
  62 
  63 public:
  64   PrepareExtraDataClosure(MethodData* mdo)
  65     : _mdo(mdo),
  66       _safepoint_tracker(SafepointSynchronize::safepoint_state_tracker()),
  67       _uncached_methods()
  68   { }
  69 
  70   bool is_live(Method* m) {
  71     if (!m->method_holder()->is_loader_alive()) {
  72       return false;
  73     }
  74     if (CURRENT_ENV->cached_metadata(m) == nullptr) {
  75       // Uncached entries need to be pre-populated.
  76       _uncached_methods.append(m);
  77     }
  78     return true;
  79   }
  80 
  81   bool has_safepointed() {
  82     return _safepoint_tracker.safepoint_state_changed();
  83   }
  84 
  85   bool finish() {
  86     if (_uncached_methods.length() == 0) {
  87       // Preparation finished iff all Methods* were already cached.
  88       return true;
  89     }
  90     // Holding locks through safepoints is bad practice.
  91     MutexUnlocker mu(_mdo->extra_data_lock());
  92     for (int i = 0; i < _uncached_methods.length(); ++i) {
  93       if (has_safepointed()) {
  94         // The metadata in the growable array might contain stale
  95         // entries after a safepoint.
  96         return false;
  97       }
  98       Method* method = _uncached_methods.at(i);
  99       // Populating ciEnv caches may cause safepoints due
 100       // to taking the Compile_lock with safepoint checks.
 101       (void)CURRENT_ENV->get_method(method);
 102     }
 103     return false;
 104   }
 105 };
 106 
 107 void ciMethodData::prepare_metadata() {
 108   MethodData* mdo = get_MethodData();
 109 
 110   for (;;) {
 111     ResourceMark rm;
 112     PrepareExtraDataClosure cl(mdo);
 113     mdo->clean_extra_data(&cl);
 114     if (cl.finish()) {
 115       // When encountering uncached metadata, the Compile_lock might be
 116       // acquired when creating ciMetadata handles, causing safepoints
 117       // which requires a new round of preparation to clean out potentially
 118       // new unloading metadata.
 119       return;
 120     }
 121   }
 122 }
 123 
 124 void ciMethodData::load_remaining_extra_data() {
 125   MethodData* mdo = get_MethodData();
 126   MutexLocker ml(mdo->extra_data_lock());
 127   // Deferred metadata cleaning due to concurrent class unloading.
 128   prepare_metadata();
 129   // After metadata preparation, there is no stale metadata,
 130   // and no safepoints can introduce more stale metadata.
 131   NoSafepointVerifier no_safepoint;
 132 
 133   assert((mdo->data_size() == _data_size) && (mdo->extra_data_size() == _extra_data_size), "sanity, unchanged");
 134   assert(extra_data_base() == (DataLayout*)((address) _data + _data_size), "sanity");
 135 
 136   // Copy the extra data once it is prepared (i.e. cache populated, no release of extra data lock anymore)
 137   Copy::disjoint_words_atomic((HeapWord*) mdo->extra_data_base(),
 138                               (HeapWord*) extra_data_base(),
 139                               // copy everything from extra_data_base() up to parameters_data_base()
 140                               pointer_delta(parameters_data_base(), extra_data_base(), HeapWordSize));
 141 
 142   // skip parameter data copying. Already done in 'load_data'
 143 
 144   // copy exception handler data
 145   Copy::disjoint_words_atomic((HeapWord*) mdo->exception_handler_data_base(),
 146                               (HeapWord*) exception_handler_data_base(),
 147                               exception_handler_data_size() / HeapWordSize);
 148 
 149   // speculative trap entries also hold a pointer to a Method so need to be translated
 150   DataLayout* dp_src  = mdo->extra_data_base();
 151   DataLayout* end_src = mdo->args_data_limit();
 152   DataLayout* dp_dst  = extra_data_base();
 153   for (;; dp_src = MethodData::next_extra(dp_src), dp_dst = MethodData::next_extra(dp_dst)) {
 154     assert(dp_src < end_src, "moved past end of extra data");
 155     assert(((intptr_t)dp_dst) - ((intptr_t)extra_data_base()) == ((intptr_t)dp_src) - ((intptr_t)mdo->extra_data_base()), "source and destination don't match");
 156 
 157     int tag = dp_src->tag();
 158     switch(tag) {
 159     case DataLayout::speculative_trap_data_tag: {
 160       ciSpeculativeTrapData data_dst(dp_dst);
 161       SpeculativeTrapData   data_src(dp_src);
 162       data_dst.translate_from(&data_src);
 163       break;
 164     }
 165     case DataLayout::bit_data_tag:
 166       break;
 167     case DataLayout::no_tag:
 168     case DataLayout::arg_info_data_tag:
 169       // An empty slot or ArgInfoData entry marks the end of the trap data
 170       {
 171         return; // Need a block to avoid SS compiler bug
 172       }
 173     default:
 174       fatal("bad tag = %d", tag);
 175     }
 176   }
 177 }
 178 
 179 bool ciMethodData::load_data() {
 180   MethodData* mdo = get_MethodData();
 181   if (mdo == nullptr) {
 182     return false;
 183   }
 184 
 185   // To do: don't copy the data if it is not "ripe" -- require a minimum #
 186   // of invocations.
 187 
 188   // Snapshot the data and extra parameter data first without the extra trap and arg info data.
 189   // Those are copied in a second step. Actually, an approximate snapshot of the data is taken.
 190   // Any concurrently executing threads may be changing the data as we copy it.
 191   //
 192   // The first snapshot step requires two copies (data entries and parameter data entries) since
 193   // the MDO is laid out as follows:
 194   //
 195   //  data_base:        ---------------------------
 196   //                    |       data entries      |
 197   //                    |           ...           |
 198   //  extra_data_base:  ---------------------------
 199   //                    |    trap data entries    |
 200   //                    |           ...           |
 201   //                    | one arg info data entry |
 202   //                    |    data for each arg    |
 203   //                    |           ...           |
 204   //  args_data_limit:  ---------------------------
 205   //                    |  parameter data entries |
 206   //                    |           ...           |
 207   //  param_data_limit: ---------------------------
 208   //                    | ex handler data entries |
 209   //                    |           ...           |
 210   //  extra_data_limit: ---------------------------
 211   //
 212   // _data_size = extra_data_base - data_base
 213   // _extra_data_size = extra_data_limit - extra_data_base
 214   // total_size = _data_size + _extra_data_size
 215   // args_data_limit = param_data_base
 216   // param_data_limit = exception_handler_data_base
 217   // extra_data_limit = extra_data_limit
 218 
 219 #ifndef ZERO
 220   // Some Zero platforms do not have expected alignment, and do not use
 221   // this code. static_assert would still fire and fail for them.
 222   static_assert(sizeof(_orig) % HeapWordSize == 0, "align");
 223 #endif
 224   Copy::disjoint_words_atomic((HeapWord*) &mdo->_compiler_counters,
 225                               (HeapWord*) &_orig,
 226                               sizeof(_orig) / HeapWordSize);
 227   Arena* arena = CURRENT_ENV->arena();
 228   _data_size = mdo->data_size();
 229   _extra_data_size = mdo->extra_data_size();
 230   int total_size = _data_size + _extra_data_size;
 231   _data = (intptr_t *) arena->Amalloc(total_size);
 232   Copy::disjoint_words_atomic((HeapWord*) mdo->data_base(),
 233                               (HeapWord*) _data,
 234                               _data_size / HeapWordSize);
 235   // Copy offsets. This is used below
 236   _parameters_data_offset = mdo->parameters_type_data_di();
 237   _exception_handlers_data_offset = mdo->exception_handlers_data_di();
 238 
 239   int parameters_data_size = mdo->parameters_size_in_bytes();
 240   if (parameters_data_size > 0) {
 241     // Snapshot the parameter data
 242     Copy::disjoint_words_atomic((HeapWord*) mdo->parameters_data_base(),
 243                                 (HeapWord*) parameters_data_base(),
 244                                 parameters_data_size / HeapWordSize);
 245   }
 246   // Traverse the profile data, translating any oops into their
 247   // ci equivalents.
 248   ResourceMark rm;
 249   ciProfileData* ci_data = first_data();
 250   ProfileData* data = mdo->first_data();
 251   while (is_valid(ci_data)) {
 252     ci_data->translate_from(data);
 253     ci_data = next_data(ci_data);
 254     data = mdo->next_data(data);
 255   }
 256   if (mdo->parameters_type_data() != nullptr) {
 257     DataLayout* parameters_data = data_layout_at(_parameters_data_offset);
 258     ciParametersTypeData* parameters = new ciParametersTypeData(parameters_data);
 259     parameters->translate_from(mdo->parameters_type_data());
 260   }
 261 
 262   assert((DataLayout*) ((address)_data + total_size - parameters_data_size - exception_handler_data_size()) == args_data_limit(),
 263       "sanity - parameter data starts after the argument data of the single ArgInfoData entry");
 264   load_remaining_extra_data();
 265 
 266   // Note:  Extra data are all BitData, and do not need translation.
 267   _invocation_counter = mdo->invocation_count();
 268   if (_invocation_counter == 0 && mdo->backedge_count() > 0) {
 269     // Avoid skewing counter data during OSR compilation.
 270     // Sometimes, MDO is allocated during the very first invocation and OSR compilation is triggered
 271     // solely by backedge counter while invocation counter stays zero. In such case, it's important
 272     // to observe non-zero invocation count to properly scale profile counts (see ciMethod::scale_count()).
 273     _invocation_counter = 1;
 274   }
 275 
 276   _state = mdo->is_mature() ? mature_state : immature_state;
 277   _eflags = mdo->eflags();
 278   _arg_local = mdo->arg_local();
 279   _arg_stack = mdo->arg_stack();
 280   _arg_returned  = mdo->arg_returned();
 281   if (ReplayCompiles) {
 282     ciReplay::initialize(this);
 283     if (is_empty()) {
 284       return false;
 285     }
 286   }
 287   return true;
 288 }
 289 
 290 void ciReceiverTypeData::translate_receiver_data_from(const ProfileData* data) {
 291   for (uint row = 0; row < row_limit(); row++) {
 292     Klass* k = data->as_ReceiverTypeData()->receiver(row);
 293     if (k != nullptr) {
 294       if (k->is_loader_alive()) {
 295         ciKlass* klass = CURRENT_ENV->get_klass(k);
 296         set_receiver(row, klass);
 297       } else {
 298         // With concurrent class unloading, the MDO could have stale metadata; override it
 299         clear_row(row);
 300       }
 301     } else {
 302       set_receiver(row, nullptr);
 303     }
 304   }
 305 }
 306 
 307 void ciTypeStackSlotEntries::translate_type_data_from(const TypeStackSlotEntries* entries) {
 308   for (int i = 0; i < number_of_entries(); i++) {
 309     intptr_t k = entries->type(i);
 310     Klass* klass = (Klass*)klass_part(k);
 311     if (klass != nullptr && !klass->is_loader_alive()) {
 312       // With concurrent class unloading, the MDO could have stale metadata; override it
 313       TypeStackSlotEntries::set_type(i, TypeStackSlotEntries::with_status((Klass*)nullptr, k));
 314     } else {
 315       TypeStackSlotEntries::set_type(i, translate_klass(k));
 316     }
 317   }
 318 }
 319 
 320 void ciSingleTypeEntry::translate_type_data_from(const SingleTypeEntry* ret) {
 321   intptr_t k = ret->type();
 322   Klass* klass = (Klass*)klass_part(k);
 323   if (klass != nullptr && !klass->is_loader_alive()) {
 324     // With concurrent class unloading, the MDO could have stale metadata; override it
 325     set_type(SingleTypeEntry::with_status((Klass*)nullptr, k));
 326   } else {
 327     set_type(translate_klass(k));
 328   }
 329 }
 330 
 331 void ciSpeculativeTrapData::translate_from(const ProfileData* data) {
 332   Method* m = data->as_SpeculativeTrapData()->method();
 333   ciMethod* ci_m = CURRENT_ENV->get_method(m);
 334   set_method(ci_m);
 335 }
 336 
 337 // Get the data at an arbitrary (sort of) data index.
 338 ciProfileData* ciMethodData::data_at(int data_index) {
 339   if (out_of_bounds(data_index)) {
 340     return nullptr;
 341   }
 342   DataLayout* data_layout = data_layout_at(data_index);
 343   return data_from(data_layout);
 344 }
 345 
 346 ciProfileData* ciMethodData::data_from(DataLayout* data_layout) {
 347   switch (data_layout->tag()) {
 348   case DataLayout::no_tag:
 349   default:
 350     ShouldNotReachHere();
 351     return nullptr;
 352   case DataLayout::bit_data_tag:
 353     return new ciBitData(data_layout);
 354   case DataLayout::counter_data_tag:
 355     return new ciCounterData(data_layout);
 356   case DataLayout::jump_data_tag:
 357     return new ciJumpData(data_layout);
 358   case DataLayout::receiver_type_data_tag:
 359     return new ciReceiverTypeData(data_layout);
 360   case DataLayout::virtual_call_data_tag:
 361     return new ciVirtualCallData(data_layout);
 362   case DataLayout::ret_data_tag:
 363     return new ciRetData(data_layout);
 364   case DataLayout::branch_data_tag:
 365     return new ciBranchData(data_layout);
 366   case DataLayout::multi_branch_data_tag:
 367     return new ciMultiBranchData(data_layout);
 368   case DataLayout::arg_info_data_tag:
 369     return new ciArgInfoData(data_layout);
 370   case DataLayout::call_type_data_tag:
 371     return new ciCallTypeData(data_layout);
 372   case DataLayout::virtual_call_type_data_tag:
 373     return new ciVirtualCallTypeData(data_layout);
 374   case DataLayout::parameters_type_data_tag:
 375     return new ciParametersTypeData(data_layout);
 376   case DataLayout::array_store_data_tag:
 377     return new ciArrayStoreData(data_layout);
 378   case DataLayout::array_load_data_tag:
 379     return new ciArrayLoadData(data_layout);
 380   case DataLayout::acmp_data_tag:
 381     return new ciACmpData(data_layout);
 382   };
 383 }
 384 
 385 // Iteration over data.
 386 ciProfileData* ciMethodData::next_data(ciProfileData* current) {
 387   int current_index = dp_to_di(current->dp());
 388   int next_index = current_index + current->size_in_bytes();
 389   ciProfileData* next = data_at(next_index);
 390   return next;
 391 }
 392 
 393 DataLayout* ciMethodData::next_data_layout_helper(DataLayout* current, bool extra) {
 394   int current_index = dp_to_di((address)current);
 395   int next_index = current_index + current->size_in_bytes();
 396   if (extra ? out_of_bounds_extra(next_index) : out_of_bounds(next_index)) {
 397     return nullptr;
 398   }
 399   DataLayout* next = data_layout_at(next_index);
 400   return next;
 401 }
 402 
 403 DataLayout* ciMethodData::next_data_layout(DataLayout* current) {
 404   return next_data_layout_helper(current, false);
 405 }
 406 
 407 DataLayout* ciMethodData::next_extra_data_layout(DataLayout* current) {
 408   return next_data_layout_helper(current, true);
 409 }
 410 
 411 ciProfileData* ciMethodData::bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots) {
 412   DataLayout* dp  = extra_data_base();
 413   DataLayout* end = args_data_limit();
 414   two_free_slots = false;
 415   for (;dp < end; dp = MethodData::next_extra(dp)) {
 416     switch(dp->tag()) {
 417     case DataLayout::no_tag:
 418       _saw_free_extra_data = true;  // observed an empty slot (common case)
 419       two_free_slots = (MethodData::next_extra(dp)->tag() == DataLayout::no_tag);
 420       return nullptr;
 421     case DataLayout::arg_info_data_tag:
 422       return nullptr; // ArgInfoData is after the trap data right before the parameter data.
 423     case DataLayout::bit_data_tag:
 424       if (m == nullptr && dp->bci() == bci) {
 425         return new ciBitData(dp);
 426       }
 427       break;
 428     case DataLayout::speculative_trap_data_tag: {
 429       ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
 430       // data->method() might be null if the MDO is snapshotted
 431       // concurrently with a trap
 432       if (m != nullptr && data->method() == m && dp->bci() == bci) {
 433         return data;
 434       }
 435       break;
 436     }
 437     default:
 438       fatal("bad tag = %d", dp->tag());
 439     }
 440   }
 441   return nullptr;
 442 }
 443 
 444 // Translate a bci to its corresponding data, or nullptr.
 445 ciProfileData* ciMethodData::bci_to_data(int bci, ciMethod* m) {
 446   // If m is not nullptr we look for a SpeculativeTrapData entry
 447   if (m == nullptr) {
 448     DataLayout* data_layout = data_layout_before(bci);
 449     for ( ; is_valid(data_layout); data_layout = next_data_layout(data_layout)) {
 450       if (data_layout->bci() == bci) {
 451         set_hint_di(dp_to_di((address)data_layout));
 452         return data_from(data_layout);
 453       } else if (data_layout->bci() > bci) {
 454         break;
 455       }
 456     }
 457   }
 458   bool two_free_slots = false;
 459   ciProfileData* result = bci_to_extra_data(bci, m, two_free_slots);
 460   if (result != nullptr) {
 461     return result;
 462   }
 463   if (m != nullptr && !two_free_slots) {
 464     // We were looking for a SpeculativeTrapData entry we didn't
 465     // find. Room is not available for more SpeculativeTrapData
 466     // entries, look in the non SpeculativeTrapData entries.
 467     return bci_to_data(bci, nullptr);
 468   }
 469   return nullptr;
 470 }
 471 
 472 ciBitData ciMethodData::exception_handler_bci_to_data(int bci) {
 473   assert(ProfileExceptionHandlers, "not profiling");
 474   assert(_data != nullptr, "must be initialized");
 475   for (DataLayout* data = exception_handler_data_base(); data < exception_handler_data_limit(); data = next_extra_data_layout(data)) {
 476     assert(data != nullptr, "out of bounds?");
 477     if (data->bci() == bci) {
 478       return ciBitData(data);
 479     }
 480   }
 481   // called with invalid bci or wrong Method/MethodData
 482   ShouldNotReachHere();
 483   return ciBitData(nullptr);
 484 }
 485 
 486 // Conservatively decode the trap_state of a ciProfileData.
 487 int ciMethodData::has_trap_at(ciProfileData* data, int reason) {
 488   typedef Deoptimization::DeoptReason DR_t;
 489   int per_bc_reason
 490     = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);
 491   if (trap_count(reason) == 0) {
 492     // Impossible for this trap to have occurred, regardless of trap_state.
 493     // Note:  This happens if the MDO is empty.
 494     return 0;
 495   } else if (per_bc_reason == Deoptimization::Reason_none) {
 496     // We cannot conclude anything; a trap happened somewhere, maybe here.
 497     return -1;
 498   } else if (data == nullptr) {
 499     // No profile here, not even an extra_data record allocated on the fly.
 500     // If there are empty extra_data records, and there had been a trap,
 501     // there would have been a non-null data pointer.  If there are no
 502     // free extra_data records, we must return a conservative -1.
 503     if (_saw_free_extra_data)
 504       return 0;                 // Q.E.D.
 505     else
 506       return -1;                // bail with a conservative answer
 507   } else {
 508     return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);
 509   }
 510 }
 511 
 512 int ciMethodData::trap_recompiled_at(ciProfileData* data) {
 513   if (data == nullptr) {
 514     return (_saw_free_extra_data? 0: -1);  // (see previous method)
 515   } else {
 516     return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;
 517   }
 518 }
 519 
 520 void ciMethodData::clear_escape_info() {
 521   VM_ENTRY_MARK;
 522   MethodData* mdo = get_MethodData();
 523   if (mdo != nullptr) {
 524     mdo->clear_escape_info();
 525     ArgInfoData *aid = arg_info();
 526     int arg_count = (aid == nullptr) ? 0 : aid->number_of_args();
 527     for (int i = 0; i < arg_count; i++) {
 528       set_arg_modified(i, 0);
 529     }
 530   }
 531   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
 532 }
 533 
 534 // copy our escape info to the MethodData* if it exists
 535 void ciMethodData::update_escape_info() {
 536   VM_ENTRY_MARK;
 537   MethodData* mdo = get_MethodData();
 538   if ( mdo != nullptr) {
 539     mdo->set_eflags(_eflags);
 540     mdo->set_arg_local(_arg_local);
 541     mdo->set_arg_stack(_arg_stack);
 542     mdo->set_arg_returned(_arg_returned);
 543     int arg_count = mdo->method()->size_of_parameters();
 544     for (int i = 0; i < arg_count; i++) {
 545       mdo->set_arg_modified(i, arg_modified(i));
 546     }
 547   }
 548 }
 549 
 550 void ciMethodData::set_compilation_stats(short loops, short blocks) {
 551   VM_ENTRY_MARK;
 552   MethodData* mdo = get_MethodData();
 553   if (mdo != nullptr) {
 554     mdo->set_num_loops(loops);
 555     mdo->set_num_blocks(blocks);
 556   }
 557 }
 558 
 559 void ciMethodData::set_would_profile(bool p) {
 560   VM_ENTRY_MARK;
 561   MethodData* mdo = get_MethodData();
 562   if (mdo != nullptr) {
 563     mdo->set_would_profile(p);
 564   }
 565 }
 566 
 567 void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) {
 568   VM_ENTRY_MARK;
 569   MethodData* mdo = get_MethodData();
 570   if (mdo != nullptr) {
 571     ProfileData* data = mdo->bci_to_data(bci);
 572     if (data != nullptr) {
 573       if (data->is_CallTypeData()) {
 574         data->as_CallTypeData()->set_argument_type(i, k->get_Klass());
 575       } else {
 576         assert(data->is_VirtualCallTypeData(), "no arguments!");
 577         data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass());
 578       }
 579     }
 580   }
 581 }
 582 
 583 void ciMethodData::set_parameter_type(int i, ciKlass* k) {
 584   VM_ENTRY_MARK;
 585   MethodData* mdo = get_MethodData();
 586   if (mdo != nullptr) {
 587     mdo->parameters_type_data()->set_type(i, k->get_Klass());
 588   }
 589 }
 590 
 591 void ciMethodData::set_return_type(int bci, ciKlass* k) {
 592   VM_ENTRY_MARK;
 593   MethodData* mdo = get_MethodData();
 594   if (mdo != nullptr) {
 595     ProfileData* data = mdo->bci_to_data(bci);
 596     if (data != nullptr) {
 597       if (data->is_CallTypeData()) {
 598         data->as_CallTypeData()->set_return_type(k->get_Klass());
 599       } else {
 600         assert(data->is_VirtualCallTypeData(), "no arguments!");
 601         data->as_VirtualCallTypeData()->set_return_type(k->get_Klass());
 602       }
 603     }
 604   }
 605 }
 606 
 607 bool ciMethodData::has_escape_info() {
 608   return eflag_set(MethodData::estimated);
 609 }
 610 
 611 void ciMethodData::set_eflag(MethodData::EscapeFlag f) {
 612   set_bits(_eflags, f);
 613 }
 614 
 615 bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const {
 616   return mask_bits(_eflags, f) != 0;
 617 }
 618 
 619 void ciMethodData::set_arg_local(int i) {
 620   set_nth_bit(_arg_local, i);
 621 }
 622 
 623 void ciMethodData::set_arg_stack(int i) {
 624   set_nth_bit(_arg_stack, i);
 625 }
 626 
 627 void ciMethodData::set_arg_returned(int i) {
 628   set_nth_bit(_arg_returned, i);
 629 }
 630 
 631 void ciMethodData::set_arg_modified(int arg, uint val) {
 632   ArgInfoData *aid = arg_info();
 633   if (aid == nullptr)
 634     return;
 635   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 636   aid->set_arg_modified(arg, val);
 637 }
 638 
 639 bool ciMethodData::is_arg_local(int i) const {
 640   return is_set_nth_bit(_arg_local, i);
 641 }
 642 
 643 bool ciMethodData::is_arg_stack(int i) const {
 644   return is_set_nth_bit(_arg_stack, i);
 645 }
 646 
 647 bool ciMethodData::is_arg_returned(int i) const {
 648   return is_set_nth_bit(_arg_returned, i);
 649 }
 650 
 651 uint ciMethodData::arg_modified(int arg) const {
 652   ArgInfoData *aid = arg_info();
 653   if (aid == nullptr)
 654     return 0;
 655   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
 656   return aid->arg_modified(arg);
 657 }
 658 
 659 ciParametersTypeData* ciMethodData::parameters_type_data() const {
 660   return parameter_data_size() != 0 ? new ciParametersTypeData(data_layout_at(_parameters_data_offset)) : nullptr;
 661 }
 662 
 663 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {
 664   // Get offset within MethodData* of the data array
 665   ByteSize data_offset = MethodData::data_offset();
 666 
 667   // Get cell offset of the ProfileData within data array
 668   int cell_offset = dp_to_di(data->dp());
 669 
 670   // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
 671   int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);
 672 
 673   return in_ByteSize(offset);
 674 }
 675 
 676 ciArgInfoData *ciMethodData::arg_info() const {
 677   // Should be last, have to skip all traps.
 678   DataLayout* dp  = extra_data_base();
 679   DataLayout* end = args_data_limit();
 680   for (; dp < end; dp = MethodData::next_extra(dp)) {
 681     if (dp->tag() == DataLayout::arg_info_data_tag)
 682       return new ciArgInfoData(dp);
 683   }
 684   return nullptr;
 685 }
 686 
 687 
 688 // Implementation of the print method.
 689 void ciMethodData::print_impl(outputStream* st) {
 690   ciMetadata::print_impl(st);
 691 }
 692 
 693 void ciMethodData::dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k) {
 694   if (k != nullptr) {
 695     if (round == 0) {
 696       count++;
 697     } else {
 698       out->print(" %d %s", (int)(dp_to_di(pdata->dp() + in_bytes(offset)) / sizeof(intptr_t)),
 699                            CURRENT_ENV->replay_name(k));
 700     }
 701   }
 702 }
 703 
 704 template<class T> void ciMethodData::dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* vdata) {
 705   for (uint i = 0; i < vdata->row_limit(); i++) {
 706     dump_replay_data_type_helper(out, round, count, vdata, vdata->receiver_offset(i), vdata->receiver(i));
 707   }
 708 }
 709 
 710 template<class T> void ciMethodData::dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data) {
 711   if (call_type_data->has_arguments()) {
 712     for (int i = 0; i < call_type_data->number_of_arguments(); i++) {
 713       dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->argument_type_offset(i), call_type_data->valid_argument_type(i));
 714     }
 715   }
 716   if (call_type_data->has_return()) {
 717     dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->return_type_offset(), call_type_data->valid_return_type());
 718   }
 719 }
 720 
 721 void ciMethodData::dump_replay_data_extra_data_helper(outputStream* out, int round, int& count) {
 722   DataLayout* dp  = extra_data_base();
 723   DataLayout* end = args_data_limit();
 724 
 725   for (;dp < end; dp = MethodData::next_extra(dp)) {
 726     switch(dp->tag()) {
 727     case DataLayout::no_tag:
 728     case DataLayout::arg_info_data_tag:
 729       return;
 730     case DataLayout::bit_data_tag:
 731       break;
 732     case DataLayout::speculative_trap_data_tag: {
 733       ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
 734       ciMethod* m = data->method();
 735       if (m != nullptr) {
 736         if (round == 0) {
 737           count++;
 738         } else {
 739           out->print(" %d ", (int)(dp_to_di(((address)dp) + in_bytes(ciSpeculativeTrapData::method_offset())) / sizeof(intptr_t)));
 740           m->dump_name_as_ascii(out);
 741         }
 742       }
 743       break;
 744     }
 745     default:
 746       fatal("bad tag = %d", dp->tag());
 747     }
 748   }
 749 }
 750 
 751 void ciMethodData::dump_replay_data(outputStream* out) {
 752   ResourceMark rm;
 753   MethodData* mdo = get_MethodData();
 754   Method* method = mdo->method();
 755   out->print("ciMethodData ");
 756   ciMethod::dump_name_as_ascii(out, method);
 757   out->print(" %d %d", _state, _invocation_counter);
 758 
 759   // dump the contents of the MDO header as raw data
 760   unsigned char* orig = (unsigned char*)&_orig;
 761   int length = sizeof(_orig);
 762   out->print(" orig %d", length);
 763   for (int i = 0; i < length; i++) {
 764     out->print(" %d", orig[i]);
 765   }
 766 
 767   // dump the MDO data as raw data
 768   int elements = (data_size() + extra_data_size()) / sizeof(intptr_t);
 769   out->print(" data %d", elements);
 770   for (int i = 0; i < elements; i++) {
 771     // We could use INTPTR_FORMAT here but that's zero justified
 772     // which makes comparing it with the SA version of this output
 773     // harder. data()'s element type is intptr_t.
 774     out->print(" " INTX_FORMAT_X, data()[i]);
 775   }
 776 
 777   // The MDO contained oop references as ciObjects, so scan for those
 778   // and emit pairs of offset and klass name so that they can be
 779   // reconstructed at runtime.  The first round counts the number of
 780   // oop references and the second actually emits them.
 781   ciParametersTypeData* parameters = parameters_type_data();
 782   for (int count = 0, round = 0; round < 2; round++) {
 783     if (round == 1) out->print(" oops %d", count);
 784     ProfileData* pdata = first_data();
 785     for ( ; is_valid(pdata); pdata = next_data(pdata)) {
 786       if (pdata->is_VirtualCallData()) {
 787         ciVirtualCallData* vdata = (ciVirtualCallData*)pdata;
 788         dump_replay_data_receiver_type_helper<ciVirtualCallData>(out, round, count, vdata);
 789         if (pdata->is_VirtualCallTypeData()) {
 790           ciVirtualCallTypeData* call_type_data = (ciVirtualCallTypeData*)pdata;
 791           dump_replay_data_call_type_helper<ciVirtualCallTypeData>(out, round, count, call_type_data);
 792         }
 793       } else if (pdata->is_ReceiverTypeData()) {
 794         ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata;
 795         dump_replay_data_receiver_type_helper<ciReceiverTypeData>(out, round, count, vdata);
 796       } else if (pdata->is_CallTypeData()) {
 797           ciCallTypeData* call_type_data = (ciCallTypeData*)pdata;
 798           dump_replay_data_call_type_helper<ciCallTypeData>(out, round, count, call_type_data);
 799       } else if (pdata->is_ArrayStoreData()) {
 800         ciArrayStoreData* array_store_data = (ciArrayStoreData*)pdata;
 801         dump_replay_data_type_helper(out, round, count, array_store_data, ciArrayStoreData::array_offset(),
 802                                      array_store_data->array()->valid_type());
 803         dump_replay_data_receiver_type_helper<ciArrayStoreData>(out, round, count, array_store_data);
 804       } else if (pdata->is_ArrayLoadData()) {
 805         ciArrayLoadData* array_load_data = (ciArrayLoadData*)pdata;
 806         dump_replay_data_type_helper(out, round, count, array_load_data, ciArrayLoadData::array_offset(),
 807                                      array_load_data->array()->valid_type());
 808         dump_replay_data_type_helper(out, round, count, array_load_data, ciArrayLoadData::element_offset(),
 809                                      array_load_data->element()->valid_type());
 810       } else if (pdata->is_ACmpData()) {
 811         ciACmpData* acmp_data = (ciACmpData*)pdata;
 812         dump_replay_data_type_helper(out, round, count, acmp_data, ciACmpData::left_offset(),
 813                                      acmp_data->left()->valid_type());
 814         dump_replay_data_type_helper(out, round, count, acmp_data, ciACmpData::right_offset(),
 815                                      acmp_data->right()->valid_type());
 816 
 817       }
 818     }
 819     if (parameters != nullptr) {
 820       for (int i = 0; i < parameters->number_of_parameters(); i++) {
 821         dump_replay_data_type_helper(out, round, count, parameters, ParametersTypeData::type_offset(i), parameters->valid_parameter_type(i));
 822       }
 823     }
 824   }
 825   for (int count = 0, round = 0; round < 2; round++) {
 826     if (round == 1) out->print(" methods %d", count);
 827     dump_replay_data_extra_data_helper(out, round, count);
 828   }
 829   out->cr();
 830 }
 831 
 832 #ifndef PRODUCT
 833 void ciMethodData::print() {
 834   print_data_on(tty);
 835 }
 836 
 837 void ciMethodData::print_data_on(outputStream* st) {
 838   ResourceMark rm;
 839   ciParametersTypeData* parameters = parameters_type_data();
 840   if (parameters != nullptr) {
 841     parameters->print_data_on(st);
 842   }
 843   ciProfileData* data;
 844   for (data = first_data(); is_valid(data); data = next_data(data)) {
 845     st->print("%d", dp_to_di(data->dp()));
 846     st->fill_to(6);
 847     data->print_data_on(st);
 848   }
 849   st->print_cr("--- Extra data:");
 850   DataLayout* dp  = extra_data_base();
 851   DataLayout* end = args_data_limit();
 852   for (;; dp = MethodData::next_extra(dp)) {
 853     assert(dp < end, "moved past end of extra data");
 854     switch (dp->tag()) {
 855     case DataLayout::no_tag:
 856       continue;
 857     case DataLayout::bit_data_tag:
 858       data = new BitData(dp);
 859       break;
 860     case DataLayout::arg_info_data_tag:
 861       data = new ciArgInfoData(dp);
 862       dp = end; // ArgInfoData is after the trap data right before the parameter data.
 863       break;
 864     case DataLayout::speculative_trap_data_tag:
 865       data = new ciSpeculativeTrapData(dp);
 866       break;
 867     default:
 868       fatal("unexpected tag %d", dp->tag());
 869     }
 870     st->print("%d", dp_to_di(data->dp()));
 871     st->fill_to(6);
 872     data->print_data_on(st);
 873     if (dp >= end) return;
 874   }
 875 }
 876 
 877 void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) {
 878   if (TypeEntries::is_type_none(k)) {
 879     st->print("none");
 880   } else if (TypeEntries::is_type_unknown(k)) {
 881     st->print("unknown");
 882   } else {
 883     valid_ciklass(k)->print_name_on(st);
 884   }
 885   if (TypeEntries::was_null_seen(k)) {
 886     st->print(" (null seen)");
 887   }
 888 }
 889 
 890 void ciTypeStackSlotEntries::print_data_on(outputStream* st) const {
 891   for (int i = 0; i < number_of_entries(); i++) {
 892     _pd->tab(st);
 893     st->print("%d: stack (%u) ", i, stack_slot(i));
 894     print_ciklass(st, type(i));
 895     st->cr();
 896   }
 897 }
 898 
 899 void ciSingleTypeEntry::print_data_on(outputStream* st) const {
 900   _pd->tab(st);
 901   st->print("ret ");
 902   print_ciklass(st, type());
 903   st->cr();
 904 }
 905 
 906 void ciCallTypeData::print_data_on(outputStream* st, const char* extra) const {
 907   print_shared(st, "ciCallTypeData", extra);
 908   if (has_arguments()) {
 909     tab(st, true);
 910     st->print_cr("argument types");
 911     args()->print_data_on(st);
 912   }
 913   if (has_return()) {
 914     tab(st, true);
 915     st->print_cr("return type");
 916     ret()->print_data_on(st);
 917   }
 918 }
 919 
 920 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const {
 921   uint row;
 922   int entries = 0;
 923   for (row = 0; row < row_limit(); row++) {
 924     if (receiver(row) != nullptr)  entries++;
 925   }
 926   st->print_cr("count(%u) entries(%u)", count(), entries);
 927   for (row = 0; row < row_limit(); row++) {
 928     if (receiver(row) != nullptr) {
 929       tab(st);
 930       receiver(row)->print_name_on(st);
 931       st->print_cr("(%u)", receiver_count(row));
 932     }
 933   }
 934 }
 935 
 936 void ciReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
 937   print_shared(st, "ciReceiverTypeData", extra);
 938   print_receiver_data_on(st);
 939 }
 940 
 941 void ciVirtualCallData::print_data_on(outputStream* st, const char* extra) const {
 942   print_shared(st, "ciVirtualCallData", extra);
 943   rtd_super()->print_receiver_data_on(st);
 944 }
 945 
 946 void ciVirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
 947   print_shared(st, "ciVirtualCallTypeData", extra);
 948   rtd_super()->print_receiver_data_on(st);
 949   if (has_arguments()) {
 950     tab(st, true);
 951     st->print("argument types");
 952     args()->print_data_on(st);
 953   }
 954   if (has_return()) {
 955     tab(st, true);
 956     st->print("return type");
 957     ret()->print_data_on(st);
 958   }
 959 }
 960 
 961 void ciParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
 962   st->print_cr("ciParametersTypeData");
 963   parameters()->print_data_on(st);
 964 }
 965 
 966 void ciSpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
 967   st->print_cr("ciSpeculativeTrapData");
 968   tab(st);
 969   method()->print_short_name(st);
 970   st->cr();
 971 }
 972 
 973 void ciArrayStoreData::print_data_on(outputStream* st, const char* extra) const {
 974   print_shared(st, "ciArrayLoadStoreData", extra);
 975   tab(st, true);
 976   st->print("array");
 977   array()->print_data_on(st);
 978   tab(st, true);
 979   st->print("element");
 980   print_receiver_data_on(st);
 981 }
 982 
 983 void ciArrayLoadData::print_data_on(outputStream* st, const char* extra) const {
 984   print_shared(st, "ciArrayLoadStoreData", extra);
 985   tab(st, true);
 986   st->print("array");
 987   array()->print_data_on(st);
 988   tab(st, true);
 989   st->print("element");
 990   element()->print_data_on(st);
 991 }
 992 
 993 void ciACmpData::print_data_on(outputStream* st, const char* extra) const {
 994   BranchData::print_data_on(st, extra);
 995   st->cr();
 996   tab(st, true);
 997   st->print("left");
 998   left()->print_data_on(st);
 999   tab(st, true);
1000   st->print("right");
1001   right()->print_data_on(st);
1002 }
1003 #endif