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 ciReturnTypeEntry::translate_type_data_from(const ReturnTypeEntry* 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(ReturnTypeEntry::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   };
361 }
362 
363 // Iteration over data.
364 ciProfileData* ciMethodData::next_data(ciProfileData* current) {
365   int current_index = dp_to_di(current->dp());
366   int next_index = current_index + current->size_in_bytes();
367   ciProfileData* next = data_at(next_index);
368   return next;
369 }
370 
371 DataLayout* ciMethodData::next_data_layout(DataLayout* current) {
372   int current_index = dp_to_di((address)current);
373   int next_index = current_index + current->size_in_bytes();
374   if (out_of_bounds(next_index)) {
375     return NULL;
376   }
377   DataLayout* next = data_layout_at(next_index);
378   return next;
379 }
380 
381 ciProfileData* ciMethodData::bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots) {
382   DataLayout* dp  = extra_data_base();
383   DataLayout* end = args_data_limit();
384   two_free_slots = false;
385   for (;dp < end; dp = MethodData::next_extra(dp)) {
386     switch(dp->tag()) {
387     case DataLayout::no_tag:
388       _saw_free_extra_data = true;  // observed an empty slot (common case)
389       two_free_slots = (MethodData::next_extra(dp)->tag() == DataLayout::no_tag);
390       return NULL;
391     case DataLayout::arg_info_data_tag:
392       return NULL; // ArgInfoData is after the trap data right before the parameter data.
393     case DataLayout::bit_data_tag:
394       if (m == NULL && dp->bci() == bci) {
395         return new ciBitData(dp);
396       }
397       break;
398     case DataLayout::speculative_trap_data_tag: {
399       ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
400       // data->method() might be null if the MDO is snapshotted
401       // concurrently with a trap
402       if (m != NULL && data->method() == m && dp->bci() == bci) {
403         return data;
404       }
405       break;
406     }
407     default:
408       fatal("bad tag = %d", dp->tag());
409     }
410   }
411   return NULL;
412 }
413 
414 // Translate a bci to its corresponding data, or NULL.
415 ciProfileData* ciMethodData::bci_to_data(int bci, ciMethod* m) {
416   // If m is not NULL we look for a SpeculativeTrapData entry
417   if (m == NULL) {
418     DataLayout* data_layout = data_layout_before(bci);
419     for ( ; is_valid(data_layout); data_layout = next_data_layout(data_layout)) {
420       if (data_layout->bci() == bci) {
421         set_hint_di(dp_to_di((address)data_layout));
422         return data_from(data_layout);
423       } else if (data_layout->bci() > bci) {
424         break;
425       }
426     }
427   }
428   bool two_free_slots = false;
429   ciProfileData* result = bci_to_extra_data(bci, m, two_free_slots);
430   if (result != NULL) {
431     return result;
432   }
433   if (m != NULL && !two_free_slots) {
434     // We were looking for a SpeculativeTrapData entry we didn't
435     // find. Room is not available for more SpeculativeTrapData
436     // entries, look in the non SpeculativeTrapData entries.
437     return bci_to_data(bci, NULL);
438   }
439   return NULL;
440 }
441 
442 // Conservatively decode the trap_state of a ciProfileData.
443 int ciMethodData::has_trap_at(ciProfileData* data, int reason) {
444   typedef Deoptimization::DeoptReason DR_t;
445   int per_bc_reason
446     = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);
447   if (trap_count(reason) == 0) {
448     // Impossible for this trap to have occurred, regardless of trap_state.
449     // Note:  This happens if the MDO is empty.
450     return 0;
451   } else if (per_bc_reason == Deoptimization::Reason_none) {
452     // We cannot conclude anything; a trap happened somewhere, maybe here.
453     return -1;
454   } else if (data == NULL) {
455     // No profile here, not even an extra_data record allocated on the fly.
456     // If there are empty extra_data records, and there had been a trap,
457     // there would have been a non-null data pointer.  If there are no
458     // free extra_data records, we must return a conservative -1.
459     if (_saw_free_extra_data)
460       return 0;                 // Q.E.D.
461     else
462       return -1;                // bail with a conservative answer
463   } else {
464     return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);
465   }
466 }
467 
468 int ciMethodData::trap_recompiled_at(ciProfileData* data) {
469   if (data == NULL) {
470     return (_saw_free_extra_data? 0: -1);  // (see previous method)
471   } else {
472     return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;
473   }
474 }
475 
476 void ciMethodData::clear_escape_info() {
477   VM_ENTRY_MARK;
478   MethodData* mdo = get_MethodData();
479   if (mdo != NULL) {
480     mdo->clear_escape_info();
481     ArgInfoData *aid = arg_info();
482     int arg_count = (aid == NULL) ? 0 : aid->number_of_args();
483     for (int i = 0; i < arg_count; i++) {
484       set_arg_modified(i, 0);
485     }
486   }
487   _eflags = _arg_local = _arg_stack = _arg_returned = 0;
488 }
489 
490 // copy our escape info to the MethodData* if it exists
491 void ciMethodData::update_escape_info() {
492   VM_ENTRY_MARK;
493   MethodData* mdo = get_MethodData();
494   if ( mdo != NULL) {
495     mdo->set_eflags(_eflags);
496     mdo->set_arg_local(_arg_local);
497     mdo->set_arg_stack(_arg_stack);
498     mdo->set_arg_returned(_arg_returned);
499     int arg_count = mdo->method()->size_of_parameters();
500     for (int i = 0; i < arg_count; i++) {
501       mdo->set_arg_modified(i, arg_modified(i));
502     }
503   }
504 }
505 
506 void ciMethodData::set_compilation_stats(short loops, short blocks) {
507   VM_ENTRY_MARK;
508   MethodData* mdo = get_MethodData();
509   if (mdo != NULL) {
510     mdo->set_num_loops(loops);
511     mdo->set_num_blocks(blocks);
512   }
513 }
514 
515 void ciMethodData::set_would_profile(bool p) {
516   VM_ENTRY_MARK;
517   MethodData* mdo = get_MethodData();
518   if (mdo != NULL) {
519     mdo->set_would_profile(p);
520   }
521 }
522 
523 void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) {
524   VM_ENTRY_MARK;
525   MethodData* mdo = get_MethodData();
526   if (mdo != NULL) {
527     ProfileData* data = mdo->bci_to_data(bci);
528     if (data != NULL) {
529       if (data->is_CallTypeData()) {
530         data->as_CallTypeData()->set_argument_type(i, k->get_Klass());
531       } else {
532         assert(data->is_VirtualCallTypeData(), "no arguments!");
533         data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass());
534       }
535     }
536   }
537 }
538 
539 void ciMethodData::set_parameter_type(int i, ciKlass* k) {
540   VM_ENTRY_MARK;
541   MethodData* mdo = get_MethodData();
542   if (mdo != NULL) {
543     mdo->parameters_type_data()->set_type(i, k->get_Klass());
544   }
545 }
546 
547 void ciMethodData::set_return_type(int bci, ciKlass* k) {
548   VM_ENTRY_MARK;
549   MethodData* mdo = get_MethodData();
550   if (mdo != NULL) {
551     ProfileData* data = mdo->bci_to_data(bci);
552     if (data != NULL) {
553       if (data->is_CallTypeData()) {
554         data->as_CallTypeData()->set_return_type(k->get_Klass());
555       } else {
556         assert(data->is_VirtualCallTypeData(), "no arguments!");
557         data->as_VirtualCallTypeData()->set_return_type(k->get_Klass());
558       }
559     }
560   }
561 }
562 
563 bool ciMethodData::has_escape_info() {
564   return eflag_set(MethodData::estimated);
565 }
566 
567 void ciMethodData::set_eflag(MethodData::EscapeFlag f) {
568   set_bits(_eflags, f);
569 }
570 
571 bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const {
572   return mask_bits(_eflags, f) != 0;
573 }
574 
575 void ciMethodData::set_arg_local(int i) {
576   set_nth_bit(_arg_local, i);
577 }
578 
579 void ciMethodData::set_arg_stack(int i) {
580   set_nth_bit(_arg_stack, i);
581 }
582 
583 void ciMethodData::set_arg_returned(int i) {
584   set_nth_bit(_arg_returned, i);
585 }
586 
587 void ciMethodData::set_arg_modified(int arg, uint val) {
588   ArgInfoData *aid = arg_info();
589   if (aid == NULL)
590     return;
591   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
592   aid->set_arg_modified(arg, val);
593 }
594 
595 bool ciMethodData::is_arg_local(int i) const {
596   return is_set_nth_bit(_arg_local, i);
597 }
598 
599 bool ciMethodData::is_arg_stack(int i) const {
600   return is_set_nth_bit(_arg_stack, i);
601 }
602 
603 bool ciMethodData::is_arg_returned(int i) const {
604   return is_set_nth_bit(_arg_returned, i);
605 }
606 
607 uint ciMethodData::arg_modified(int arg) const {
608   ArgInfoData *aid = arg_info();
609   if (aid == NULL)
610     return 0;
611   assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
612   return aid->arg_modified(arg);
613 }
614 
615 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {
616   // Get offset within MethodData* of the data array
617   ByteSize data_offset = MethodData::data_offset();
618 
619   // Get cell offset of the ProfileData within data array
620   int cell_offset = dp_to_di(data->dp());
621 
622   // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
623   int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);
624 
625   return in_ByteSize(offset);
626 }
627 
628 ciArgInfoData *ciMethodData::arg_info() const {
629   // Should be last, have to skip all traps.
630   DataLayout* dp  = extra_data_base();
631   DataLayout* end = args_data_limit();
632   for (; dp < end; dp = MethodData::next_extra(dp)) {
633     if (dp->tag() == DataLayout::arg_info_data_tag)
634       return new ciArgInfoData(dp);
635   }
636   return NULL;
637 }
638 
639 
640 // Implementation of the print method.
641 void ciMethodData::print_impl(outputStream* st) {
642   ciMetadata::print_impl(st);
643 }
644 
645 void ciMethodData::dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k) {
646   if (k != NULL) {
647     if (round == 0) {
648       count++;
649     } else {
650       out->print(" %d %s", (int)(dp_to_di(pdata->dp() + in_bytes(offset)) / sizeof(intptr_t)),
651                            CURRENT_ENV->replay_name(k));
652     }
653   }
654 }
655 
656 template<class T> void ciMethodData::dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* vdata) {
657   for (uint i = 0; i < vdata->row_limit(); i++) {
658     dump_replay_data_type_helper(out, round, count, vdata, vdata->receiver_offset(i), vdata->receiver(i));
659   }
660 }
661 
662 template<class T> void ciMethodData::dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data) {
663   if (call_type_data->has_arguments()) {
664     for (int i = 0; i < call_type_data->number_of_arguments(); i++) {
665       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));
666     }
667   }
668   if (call_type_data->has_return()) {
669     dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->return_type_offset(), call_type_data->valid_return_type());
670   }
671 }
672 
673 void ciMethodData::dump_replay_data_extra_data_helper(outputStream* out, int round, int& count) {
674   DataLayout* dp  = extra_data_base();
675   DataLayout* end = args_data_limit();
676 
677   for (;dp < end; dp = MethodData::next_extra(dp)) {
678     switch(dp->tag()) {
679     case DataLayout::no_tag:
680     case DataLayout::arg_info_data_tag:
681       return;
682     case DataLayout::bit_data_tag:
683       break;
684     case DataLayout::speculative_trap_data_tag: {
685       ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
686       ciMethod* m = data->method();
687       if (m != NULL) {
688         if (round == 0) {
689           count++;
690         } else {
691           out->print(" %d ", (int)(dp_to_di(((address)dp) + in_bytes(ciSpeculativeTrapData::method_offset())) / sizeof(intptr_t)));
692           m->dump_name_as_ascii(out);
693         }
694       }
695       break;
696     }
697     default:
698       fatal("bad tag = %d", dp->tag());
699     }
700   }
701 }
702 
703 void ciMethodData::dump_replay_data(outputStream* out) {
704   ResourceMark rm;
705   MethodData* mdo = get_MethodData();
706   Method* method = mdo->method();
707   out->print("ciMethodData ");
708   ciMethod::dump_name_as_ascii(out, method);
709   out->print(" %d %d", _state, current_mileage());
710 
711   // dump the contents of the MDO header as raw data
712   unsigned char* orig = (unsigned char*)&_orig;
713   int length = sizeof(_orig);
714   out->print(" orig %d", length);
715   for (int i = 0; i < length; i++) {
716     out->print(" %d", orig[i]);
717   }
718 
719   // dump the MDO data as raw data
720   int elements = (data_size() + extra_data_size()) / sizeof(intptr_t);
721   out->print(" data %d", elements);
722   for (int i = 0; i < elements; i++) {
723     // We could use INTPTR_FORMAT here but that's zero justified
724     // which makes comparing it with the SA version of this output
725     // harder. data()'s element type is intptr_t.
726     out->print(" " INTPTRNZ_FORMAT, data()[i]);
727   }
728 
729   // The MDO contained oop references as ciObjects, so scan for those
730   // and emit pairs of offset and klass name so that they can be
731   // reconstructed at runtime.  The first round counts the number of
732   // oop references and the second actually emits them.
733   ciParametersTypeData* parameters = parameters_type_data();
734   for (int count = 0, round = 0; round < 2; round++) {
735     if (round == 1) out->print(" oops %d", count);
736     ProfileData* pdata = first_data();
737     for ( ; is_valid(pdata); pdata = next_data(pdata)) {
738       if (pdata->is_VirtualCallData()) {
739         ciVirtualCallData* vdata = (ciVirtualCallData*)pdata;
740         dump_replay_data_receiver_type_helper<ciVirtualCallData>(out, round, count, vdata);
741         if (pdata->is_VirtualCallTypeData()) {
742           ciVirtualCallTypeData* call_type_data = (ciVirtualCallTypeData*)pdata;
743           dump_replay_data_call_type_helper<ciVirtualCallTypeData>(out, round, count, call_type_data);
744         }
745       } else if (pdata->is_ReceiverTypeData()) {
746         ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata;
747         dump_replay_data_receiver_type_helper<ciReceiverTypeData>(out, round, count, vdata);
748       } else if (pdata->is_CallTypeData()) {
749           ciCallTypeData* call_type_data = (ciCallTypeData*)pdata;
750           dump_replay_data_call_type_helper<ciCallTypeData>(out, round, count, call_type_data);
751       }
752     }
753     if (parameters != NULL) {
754       for (int i = 0; i < parameters->number_of_parameters(); i++) {
755         dump_replay_data_type_helper(out, round, count, parameters, ParametersTypeData::type_offset(i), parameters->valid_parameter_type(i));
756       }
757     }
758   }
759   for (int count = 0, round = 0; round < 2; round++) {
760     if (round == 1) out->print(" methods %d", count);
761     dump_replay_data_extra_data_helper(out, round, count);
762   }
763   out->cr();
764 }
765 
766 #ifndef PRODUCT
767 void ciMethodData::print() {
768   print_data_on(tty);
769 }
770 
771 void ciMethodData::print_data_on(outputStream* st) {
772   ResourceMark rm;
773   ciParametersTypeData* parameters = parameters_type_data();
774   if (parameters != NULL) {
775     parameters->print_data_on(st);
776   }
777   ciProfileData* data;
778   for (data = first_data(); is_valid(data); data = next_data(data)) {
779     st->print("%d", dp_to_di(data->dp()));
780     st->fill_to(6);
781     data->print_data_on(st);
782   }
783   st->print_cr("--- Extra data:");
784   DataLayout* dp  = extra_data_base();
785   DataLayout* end = args_data_limit();
786   for (;; dp = MethodData::next_extra(dp)) {
787     assert(dp < end, "moved past end of extra data");
788     switch (dp->tag()) {
789     case DataLayout::no_tag:
790       continue;
791     case DataLayout::bit_data_tag:
792       data = new BitData(dp);
793       break;
794     case DataLayout::arg_info_data_tag:
795       data = new ciArgInfoData(dp);
796       dp = end; // ArgInfoData is after the trap data right before the parameter data.
797       break;
798     case DataLayout::speculative_trap_data_tag:
799       data = new ciSpeculativeTrapData(dp);
800       break;
801     default:
802       fatal("unexpected tag %d", dp->tag());
803     }
804     st->print("%d", dp_to_di(data->dp()));
805     st->fill_to(6);
806     data->print_data_on(st);
807     if (dp >= end) return;
808   }
809 }
810 
811 void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) {
812   if (TypeEntries::is_type_none(k)) {
813     st->print("none");
814   } else if (TypeEntries::is_type_unknown(k)) {
815     st->print("unknown");
816   } else {
817     valid_ciklass(k)->print_name_on(st);
818   }
819   if (TypeEntries::was_null_seen(k)) {
820     st->print(" (null seen)");
821   }
822 }
823 
824 void ciTypeStackSlotEntries::print_data_on(outputStream* st) const {
825   for (int i = 0; i < number_of_entries(); i++) {
826     _pd->tab(st);
827     st->print("%d: stack (%u) ", i, stack_slot(i));
828     print_ciklass(st, type(i));
829     st->cr();
830   }
831 }
832 
833 void ciReturnTypeEntry::print_data_on(outputStream* st) const {
834   _pd->tab(st);
835   st->print("ret ");
836   print_ciklass(st, type());
837   st->cr();
838 }
839 
840 void ciCallTypeData::print_data_on(outputStream* st, const char* extra) const {
841   print_shared(st, "ciCallTypeData", extra);
842   if (has_arguments()) {
843     tab(st, true);
844     st->print_cr("argument types");
845     args()->print_data_on(st);
846   }
847   if (has_return()) {
848     tab(st, true);
849     st->print_cr("return type");
850     ret()->print_data_on(st);
851   }
852 }
853 
854 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const {
855   uint row;
856   int entries = 0;
857   for (row = 0; row < row_limit(); row++) {
858     if (receiver(row) != NULL)  entries++;
859   }
860   st->print_cr("count(%u) entries(%u)", count(), entries);
861   for (row = 0; row < row_limit(); row++) {
862     if (receiver(row) != NULL) {
863       tab(st);
864       receiver(row)->print_name_on(st);
865       st->print_cr("(%u)", receiver_count(row));
866     }
867   }
868 }
869 
870 void ciReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
871   print_shared(st, "ciReceiverTypeData", extra);
872   print_receiver_data_on(st);
873 }
874 
875 void ciVirtualCallData::print_data_on(outputStream* st, const char* extra) const {
876   print_shared(st, "ciVirtualCallData", extra);
877   rtd_super()->print_receiver_data_on(st);
878 }
879 
880 void ciVirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
881   print_shared(st, "ciVirtualCallTypeData", extra);
882   rtd_super()->print_receiver_data_on(st);
883   if (has_arguments()) {
884     tab(st, true);
885     st->print("argument types");
886     args()->print_data_on(st);
887   }
888   if (has_return()) {
889     tab(st, true);
890     st->print("return type");
891     ret()->print_data_on(st);
892   }
893 }
894 
895 void ciParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
896   st->print_cr("ciParametersTypeData");
897   parameters()->print_data_on(st);
898 }
899 
900 void ciSpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
901   st->print_cr("ciSpeculativeTrapData");
902   tab(st);
903   method()->print_short_name(st);
904   st->cr();
905 }
906 #endif