1 /*
  2  * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CI_CIMETHODDATA_HPP
 26 #define SHARE_CI_CIMETHODDATA_HPP
 27 
 28 #include "ci/ciClassList.hpp"
 29 #include "ci/ciKlass.hpp"
 30 #include "ci/ciObject.hpp"
 31 #include "ci/ciUtilities.hpp"
 32 #include "oops/methodData.hpp"
 33 #include "oops/oop.hpp"
 34 #include "runtime/deoptimization.hpp"
 35 
 36 class ciBitData;
 37 class ciCounterData;
 38 class ciJumpData;
 39 class ciReceiverTypeData;
 40 class ciRetData;
 41 class ciBranchData;
 42 class ciArrayData;
 43 class ciMultiBranchData;
 44 class ciArgInfoData;
 45 class ciCallTypeData;
 46 class ciVirtualCallTypeData;
 47 class ciParametersTypeData;
 48 class ciSpeculativeTrapData;
 49 
 50 typedef ProfileData ciProfileData;
 51 
 52 class ciBitData : public BitData {
 53 public:
 54   ciBitData(DataLayout* layout) : BitData(layout) {};
 55 };
 56 
 57 class ciCounterData : public CounterData {
 58 public:
 59   ciCounterData(DataLayout* layout) : CounterData(layout) {};
 60 };
 61 
 62 class ciJumpData : public JumpData {
 63 public:
 64   ciJumpData(DataLayout* layout) : JumpData(layout) {};
 65 };
 66 
 67 class ciTypeEntries {
 68 protected:
 69   static intptr_t translate_klass(intptr_t k) {
 70     Klass* v = TypeEntries::valid_klass(k);
 71     if (v != nullptr) {
 72       ciKlass* klass = CURRENT_ENV->get_klass(v);
 73       return with_status(klass, k);
 74     }
 75     return with_status(nullptr, k);
 76   }
 77 
 78 public:
 79   static ciKlass* valid_ciklass(intptr_t k) {
 80     if (!TypeEntries::is_type_none(k) &&
 81         !TypeEntries::is_type_unknown(k)) {
 82       ciKlass* res = (ciKlass*)TypeEntries::klass_part(k);
 83       assert(res != nullptr, "invalid");
 84       return res;
 85     } else {
 86       return nullptr;
 87     }
 88   }
 89 
 90   static ProfilePtrKind ptr_kind(intptr_t v) {
 91     bool maybe_null = TypeEntries::was_null_seen(v);
 92     if (!maybe_null) {
 93       return ProfileNeverNull;
 94     } else if (TypeEntries::is_type_none(v)) {
 95       return ProfileAlwaysNull;
 96     } else {
 97       return ProfileMaybeNull;
 98     }
 99   }
100 
101   static intptr_t with_status(ciKlass* k, intptr_t in) {
102     return TypeEntries::with_status((intptr_t)k, in);
103   }
104 
105 #ifndef PRODUCT
106   static void print_ciklass(outputStream* st, intptr_t k);
107 #endif
108 };
109 
110 class ciTypeStackSlotEntries : public TypeStackSlotEntries, ciTypeEntries {
111 public:
112   void translate_type_data_from(const TypeStackSlotEntries* args);
113 
114   ciKlass* valid_type(int i) const {
115     return valid_ciklass(type(i));
116   }
117 
118   ProfilePtrKind ptr_kind(int i) const {
119     return ciTypeEntries::ptr_kind(type(i));
120   }
121 
122 #ifndef PRODUCT
123   void print_data_on(outputStream* st) const;
124 #endif
125 };
126 
127 class ciReturnTypeEntry : public ReturnTypeEntry, ciTypeEntries {
128 public:
129   void translate_type_data_from(const ReturnTypeEntry* ret);
130 
131   ciKlass* valid_type() const {
132     return valid_ciklass(type());
133   }
134 
135   ProfilePtrKind ptr_kind() const {
136     return ciTypeEntries::ptr_kind(type());
137   }
138 
139 #ifndef PRODUCT
140   void print_data_on(outputStream* st) const;
141 #endif
142 };
143 
144 class ciCallTypeData : public CallTypeData {
145 public:
146   ciCallTypeData(DataLayout* layout) : CallTypeData(layout) {}
147 
148   ciTypeStackSlotEntries* args() const { return (ciTypeStackSlotEntries*)CallTypeData::args(); }
149   ciReturnTypeEntry* ret() const { return (ciReturnTypeEntry*)CallTypeData::ret(); }
150 
151   void translate_from(const ProfileData* data) {
152     if (has_arguments()) {
153       args()->translate_type_data_from(data->as_CallTypeData()->args());
154     }
155     if (has_return()) {
156       ret()->translate_type_data_from(data->as_CallTypeData()->ret());
157     }
158   }
159 
160   intptr_t argument_type(int i) const {
161     assert(has_arguments(), "no arg type profiling data");
162     return args()->type(i);
163   }
164 
165   ciKlass* valid_argument_type(int i) const {
166     assert(has_arguments(), "no arg type profiling data");
167     return args()->valid_type(i);
168   }
169 
170   intptr_t return_type() const {
171     assert(has_return(), "no ret type profiling data");
172     return ret()->type();
173   }
174 
175   ciKlass* valid_return_type() const {
176     assert(has_return(), "no ret type profiling data");
177     return ret()->valid_type();
178   }
179 
180   ProfilePtrKind argument_ptr_kind(int i) const {
181     return args()->ptr_kind(i);
182   }
183 
184   ProfilePtrKind return_ptr_kind() const {
185     return ret()->ptr_kind();
186   }
187 
188 #ifndef PRODUCT
189   void print_data_on(outputStream* st, const char* extra = nullptr) const;
190 #endif
191 };
192 
193 class ciReceiverTypeData : public ReceiverTypeData {
194 public:
195   ciReceiverTypeData(DataLayout* layout) : ReceiverTypeData(layout) {};
196 
197   void set_receiver(uint row, ciKlass* recv) {
198     assert((uint)row < row_limit(), "oob");
199     set_intptr_at(receiver0_offset + row * receiver_type_row_cell_count,
200                   (intptr_t) recv);
201   }
202 
203   ciKlass* receiver(uint row) const {
204     assert((uint)row < row_limit(), "oob");
205     ciKlass* recv = (ciKlass*)intptr_at(receiver0_offset + row * receiver_type_row_cell_count);
206     assert(recv == nullptr || recv->is_klass(), "wrong type");
207     return recv;
208   }
209 
210   // Copy & translate from oop based ReceiverTypeData
211   virtual void translate_from(const ProfileData* data) {
212     translate_receiver_data_from(data);
213   }
214   void translate_receiver_data_from(const ProfileData* data);
215 #ifndef PRODUCT
216   void print_data_on(outputStream* st, const char* extra = nullptr) const;
217   void print_receiver_data_on(outputStream* st) const;
218 #endif
219 };
220 
221 class ciVirtualCallData : public VirtualCallData {
222   // Fake multiple inheritance...  It's a ciReceiverTypeData also.
223   ciReceiverTypeData* rtd_super() const { return (ciReceiverTypeData*) this; }
224 
225 public:
226   ciVirtualCallData(DataLayout* layout) : VirtualCallData(layout) {};
227 
228   void set_receiver(uint row, ciKlass* recv) {
229     rtd_super()->set_receiver(row, recv);
230   }
231 
232   ciKlass* receiver(uint row) {
233     return rtd_super()->receiver(row);
234   }
235 
236   // Copy & translate from oop based VirtualCallData
237   virtual void translate_from(const ProfileData* data) {
238     rtd_super()->translate_receiver_data_from(data);
239   }
240 #ifndef PRODUCT
241   void print_data_on(outputStream* st, const char* extra = nullptr) const;
242 #endif
243 };
244 
245 class ciVirtualCallTypeData : public VirtualCallTypeData {
246 private:
247   // Fake multiple inheritance...  It's a ciReceiverTypeData also.
248   ciReceiverTypeData* rtd_super() const { return (ciReceiverTypeData*) this; }
249 public:
250   ciVirtualCallTypeData(DataLayout* layout) : VirtualCallTypeData(layout) {}
251 
252   void set_receiver(uint row, ciKlass* recv) {
253     rtd_super()->set_receiver(row, recv);
254   }
255 
256   ciKlass* receiver(uint row) const {
257     return rtd_super()->receiver(row);
258   }
259 
260   ciTypeStackSlotEntries* args() const { return (ciTypeStackSlotEntries*)VirtualCallTypeData::args(); }
261   ciReturnTypeEntry* ret() const { return (ciReturnTypeEntry*)VirtualCallTypeData::ret(); }
262 
263   // Copy & translate from oop based VirtualCallData
264   virtual void translate_from(const ProfileData* data) {
265     rtd_super()->translate_receiver_data_from(data);
266     if (has_arguments()) {
267       args()->translate_type_data_from(data->as_VirtualCallTypeData()->args());
268     }
269     if (has_return()) {
270       ret()->translate_type_data_from(data->as_VirtualCallTypeData()->ret());
271     }
272   }
273 
274   ciKlass* valid_argument_type(int i) const {
275     assert(has_arguments(), "no arg type profiling data");
276     return args()->valid_type(i);
277   }
278 
279   intptr_t return_type() const {
280     assert(has_return(), "no ret type profiling data");
281     return ret()->type();
282   }
283 
284   ciKlass* valid_return_type() const {
285     assert(has_return(), "no ret type profiling data");
286     return ret()->valid_type();
287   }
288 
289   ProfilePtrKind argument_ptr_kind(int i) const {
290     return args()->ptr_kind(i);
291   }
292 
293   ProfilePtrKind return_ptr_kind() const {
294     return ret()->ptr_kind();
295   }
296 
297 #ifndef PRODUCT
298   void print_data_on(outputStream* st, const char* extra = nullptr) const;
299 #endif
300 };
301 
302 
303 class ciRetData : public RetData {
304 public:
305   ciRetData(DataLayout* layout) : RetData(layout) {};
306 };
307 
308 class ciBranchData : public BranchData {
309 public:
310   ciBranchData(DataLayout* layout) : BranchData(layout) {};
311 };
312 
313 class ciMultiBranchData : public MultiBranchData {
314 public:
315   ciMultiBranchData(DataLayout* layout) : MultiBranchData(layout) {};
316 };
317 
318 class ciArgInfoData : public ArgInfoData {
319 public:
320   ciArgInfoData(DataLayout* layout) : ArgInfoData(layout) {};
321 };
322 
323 class ciParametersTypeData : public ParametersTypeData {
324 public:
325   ciParametersTypeData(DataLayout* layout) : ParametersTypeData(layout) {}
326 
327   virtual void translate_from(const ProfileData* data) {
328     parameters()->translate_type_data_from(data->as_ParametersTypeData()->parameters());
329   }
330 
331   ciTypeStackSlotEntries* parameters() const { return (ciTypeStackSlotEntries*)ParametersTypeData::parameters(); }
332 
333   ciKlass* valid_parameter_type(int i) const {
334     return parameters()->valid_type(i);
335   }
336 
337   ProfilePtrKind parameter_ptr_kind(int i) const {
338     return parameters()->ptr_kind(i);
339   }
340 
341 #ifndef PRODUCT
342   void print_data_on(outputStream* st, const char* extra = nullptr) const;
343 #endif
344 };
345 
346 class ciSpeculativeTrapData : public SpeculativeTrapData {
347 public:
348   ciSpeculativeTrapData(DataLayout* layout) : SpeculativeTrapData(layout) {}
349 
350   virtual void translate_from(const ProfileData* data);
351 
352   ciMethod* method() const {
353     return (ciMethod*)intptr_at(speculative_trap_method);
354   }
355 
356   void set_method(ciMethod* m) {
357     set_intptr_at(speculative_trap_method, (intptr_t)m);
358   }
359 
360 #ifndef PRODUCT
361   void print_data_on(outputStream* st, const char* extra = nullptr) const;
362 #endif
363 };
364 
365 // ciMethodData
366 //
367 // This class represents a MethodData* in the HotSpot virtual
368 // machine.
369 
370 class ciMethodData : public ciMetadata {
371   CI_PACKAGE_ACCESS
372   friend class ciReplay;
373 
374 private:
375   // Size in bytes
376   int _data_size;
377   int _extra_data_size;
378 
379   // Data entries
380   intptr_t* _data;
381 
382   // layout of _data
383   int _parameters_data_offset;
384   int _exception_handlers_data_offset;
385 
386   // Cached hint for data_layout_before()
387   int _hint_di;
388 
389   // Is data attached?  And is it mature?
390   enum { empty_state, immature_state, mature_state };
391   u_char _state;
392 
393   // Set this true if empty extra_data slots are ever witnessed.
394   u_char _saw_free_extra_data;
395 
396   // Support for interprocedural escape analysis
397   intx _eflags;       // flags on escape information
398   intx _arg_local;    // bit set of non-escaping arguments
399   intx _arg_stack;    // bit set of stack-allocatable arguments
400   intx _arg_returned; // bit set of returned arguments
401 
402   // These counters hold the age of MDO in tiered. In tiered we can have the same method
403   // running at different compilation levels concurrently. So, in order to precisely measure
404   // its maturity we need separate counters.
405   int _invocation_counter;
406 
407   // Coherent snapshot of original header.
408   MethodData::CompilerCounters _orig;
409 
410   ciMethodData(MethodData* md = nullptr);
411 
412   // Accessors
413   int data_size() const { return _data_size; }
414   int extra_data_size() const { return _extra_data_size; }
415   int parameter_data_size() const { return _exception_handlers_data_offset - _parameters_data_offset; }
416   int exception_handler_data_size() const { return dp_to_di((address) exception_handler_data_limit()) - _exception_handlers_data_offset; }
417   intptr_t * data() const { return _data; }
418 
419   MethodData* get_MethodData() const {
420     return (MethodData*)_metadata;
421   }
422 
423   const char* type_string()                      { return "ciMethodData"; }
424 
425   void print_impl(outputStream* st);
426 
427   DataLayout* data_layout_at(int data_index) const {
428     assert(data_index % sizeof(intptr_t) == 0, "unaligned: %d", data_index);
429     return (DataLayout*) (((address)_data) + data_index);
430   }
431 
432   bool out_of_bounds(int data_index) {
433     return data_index >= data_size();
434   }
435 
436   bool out_of_bounds_extra(int data_index) {
437     return data_index < data_size() || data_index >= data_size() + extra_data_size();
438   }
439 
440   DataLayout* next_data_layout_helper(DataLayout* current, bool extra);
441 
442   // hint accessors
443   int      hint_di() const  { return _hint_di; }
444   void set_hint_di(int di)  {
445     assert(!out_of_bounds(di), "hint_di out of bounds");
446     _hint_di = di;
447   }
448 
449   DataLayout* data_layout_before(int bci) {
450     // avoid SEGV on this edge case
451     if (data_size() == 0)
452       return nullptr;
453     DataLayout* layout = data_layout_at(hint_di());
454     if (layout->bci() <= bci)
455       return layout;
456     return data_layout_at(first_di());
457   }
458 
459   // What is the index of the first data entry?
460   int first_di() { return 0; }
461 
462   ciArgInfoData *arg_info() const;
463 
464   void prepare_metadata();
465   void load_remaining_extra_data();
466   ciProfileData* bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots);
467 
468   void dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k);
469   template<class T> void dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data);
470   template<class T> void dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* call_type_data);
471   void dump_replay_data_extra_data_helper(outputStream* out, int round, int& count);
472   ciProfileData* data_from(DataLayout* data_layout);
473 
474 public:
475   bool is_method_data() const { return true; }
476 
477   bool is_empty()  { return _state == empty_state; }
478   bool is_mature() { return _state == mature_state; }
479 
480   int invocation_count() { return _invocation_counter; }
481 
482 #if INCLUDE_RTM_OPT
483   // return cached value
484   int rtm_state() {
485     if (is_empty()) {
486       return NoRTM;
487     } else {
488       return get_MethodData()->rtm_state();
489     }
490   }
491 #endif
492 
493   // Transfer information about the method to MethodData*.
494   // would_profile means we would like to profile this method,
495   // meaning it's not trivial.
496   void set_would_profile(bool p);
497   // Also set the number of loops and blocks in the method.
498   // Again, this is used to determine if a method is trivial.
499   void set_compilation_stats(short loops, short blocks);
500   // If the compiler finds a profiled type that is known statically
501   // for sure, set it in the MethodData
502   void set_argument_type(int bci, int i, ciKlass* k);
503   void set_parameter_type(int i, ciKlass* k);
504   void set_return_type(int bci, ciKlass* k);
505 
506   bool load_data();
507 
508   // Convert a dp (data pointer) to a di (data index).
509   int dp_to_di(address dp) const {
510     return pointer_delta_as_int(dp, ((address)_data));
511   }
512 
513   // Get the data at an arbitrary (sort of) data index.
514   ciProfileData* data_at(int data_index);
515 
516   // Walk through the data in order.
517   ciProfileData* first_data() { return data_at(first_di()); }
518   ciProfileData* next_data(ciProfileData* current);
519   DataLayout* next_data_layout(DataLayout* current);
520   DataLayout* next_extra_data_layout(DataLayout* current);
521   bool is_valid(ciProfileData* current) { return current != nullptr; }
522   bool is_valid(DataLayout* current)    { return current != nullptr; }
523 
524   // pointers to sections in _data
525   // NOTE: these may be called before ciMethodData::load_data
526   //       this works out since everything is initialized to 0 (i.e. there will appear to be no data)
527   DataLayout* extra_data_base() const  { return data_layout_at(data_size()); }
528   DataLayout* extra_data_limit() const { return data_layout_at(data_size() + extra_data_size()); }
529   // pointers to sections in extra data
530   DataLayout* args_data_limit() const  { return parameters_data_base(); }
531   DataLayout* parameters_data_base() const { return data_layout_at(_parameters_data_offset); }
532   DataLayout* parameters_data_limit() const { return exception_handler_data_base(); }
533   DataLayout* exception_handler_data_base() const { return data_layout_at(_exception_handlers_data_offset); }
534   DataLayout* exception_handler_data_limit() const { return extra_data_limit(); }
535 
536   // Get the data at an arbitrary bci, or null if there is none. If m
537   // is not null look for a SpeculativeTrapData if any first.
538   ciProfileData* bci_to_data(int bci, ciMethod* m = nullptr);
539 
540   ciBitData exception_handler_bci_to_data(int bci);
541 
542   uint overflow_trap_count() const {
543     return _orig.overflow_trap_count();
544   }
545   uint overflow_recompile_count() const {
546     return _orig.overflow_recompile_count();
547   }
548   uint decompile_count() const {
549     return _orig.decompile_count();
550   }
551   uint trap_count(int reason) const {
552     return _orig.trap_count(reason);
553   }
554   uint trap_reason_limit() const { return MethodData::trap_reason_limit(); }
555   uint trap_count_limit()  const { return MethodData::trap_count_limit(); }
556 
557   // Helpful query functions that decode trap_state.
558   int has_trap_at(ciProfileData* data, int reason);
559   int has_trap_at(int bci, ciMethod* m, int reason) {
560     assert((m != nullptr) == Deoptimization::reason_is_speculate(reason), "inconsistent method/reason");
561     return has_trap_at(bci_to_data(bci, m), reason);
562   }
563   int trap_recompiled_at(ciProfileData* data);
564   int trap_recompiled_at(int bci, ciMethod* m) {
565     return trap_recompiled_at(bci_to_data(bci, m));
566   }
567 
568   void clear_escape_info();
569   bool has_escape_info();
570   void update_escape_info();
571 
572   void set_eflag(MethodData::EscapeFlag f);
573   bool eflag_set(MethodData::EscapeFlag f) const;
574 
575   void set_arg_local(int i);
576   void set_arg_stack(int i);
577   void set_arg_returned(int i);
578   void set_arg_modified(int arg, uint val);
579 
580   bool is_arg_local(int i) const;
581   bool is_arg_stack(int i) const;
582   bool is_arg_returned(int i) const;
583   uint arg_modified(int arg) const;
584 
585   ciParametersTypeData* parameters_type_data() const;
586 
587   // Code generation helper
588   ByteSize offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data);
589   int      byte_offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) { return in_bytes(offset_of_slot(data, slot_offset_in_data)); }
590 
591 #ifndef PRODUCT
592   // printing support for method data
593   void print();
594   void print_data_on(outputStream* st);
595 #endif
596   void dump_replay_data(outputStream* out);
597 };
598 
599 #endif // SHARE_CI_CIMETHODDATA_HPP