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