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