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