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