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