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