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