1 /*
   2  * Copyright (c) 2001, 2015, 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_VM_CI_CIMETHODDATA_HPP
  26 #define SHARE_VM_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 != NULL) {
  72       ciKlass* klass = CURRENT_ENV->get_klass(v);
  73       CURRENT_ENV->ensure_metadata_alive(klass);
  74       return with_status(klass, k);
  75     }
  76     return with_status(NULL, k);
  77   }
  78 
  79 public:
  80   static ciKlass* valid_ciklass(intptr_t k) {
  81     if (!TypeEntries::is_type_none(k) &&
  82         !TypeEntries::is_type_unknown(k)) {
  83       ciKlass* res = (ciKlass*)TypeEntries::klass_part(k);
  84       assert(res != NULL, "invalid");
  85       return res;
  86     } else {
  87       return NULL;
  88     }
  89   }
  90 
  91   static ProfilePtrKind ptr_kind(intptr_t v) {
  92     bool maybe_null = TypeEntries::was_null_seen(v);
  93     if (!maybe_null) {
  94       return ProfileNeverNull;
  95     } else if (TypeEntries::is_type_none(v)) {
  96       return ProfileAlwaysNull;
  97     } else {
  98       return ProfileMaybeNull;
  99     }
 100   }
 101 
 102   static intptr_t with_status(ciKlass* k, intptr_t in) {
 103     return TypeEntries::with_status((intptr_t)k, in);
 104   }
 105 
 106 #ifndef PRODUCT
 107   static void print_ciklass(outputStream* st, intptr_t k);
 108 #endif
 109 };
 110 
 111 class ciTypeStackSlotEntries : public TypeStackSlotEntries, ciTypeEntries {
 112 public:
 113   void translate_type_data_from(const TypeStackSlotEntries* args);
 114 
 115   ciKlass* valid_type(int i) const {
 116     return valid_ciklass(type(i));
 117   }
 118 
 119   ProfilePtrKind ptr_kind(int i) const {
 120     return ciTypeEntries::ptr_kind(type(i));
 121   }
 122 
 123 #ifndef PRODUCT
 124   void print_data_on(outputStream* st) const;
 125 #endif
 126 };
 127 
 128 class ciReturnTypeEntry : public ReturnTypeEntry, ciTypeEntries {
 129 public:
 130   void translate_type_data_from(const ReturnTypeEntry* ret);
 131 
 132   ciKlass* valid_type() const {
 133     return valid_ciklass(type());
 134   }
 135 
 136   ProfilePtrKind ptr_kind() const {
 137     return ciTypeEntries::ptr_kind(type());
 138   }
 139 
 140 #ifndef PRODUCT
 141   void print_data_on(outputStream* st) const;
 142 #endif
 143 };
 144 
 145 class ciCallTypeData : public CallTypeData {
 146 public:
 147   ciCallTypeData(DataLayout* layout) : CallTypeData(layout) {}
 148 
 149   ciTypeStackSlotEntries* args() const { return (ciTypeStackSlotEntries*)CallTypeData::args(); }
 150   ciReturnTypeEntry* ret() const { return (ciReturnTypeEntry*)CallTypeData::ret(); }
 151 
 152   void translate_from(const ProfileData* data) {
 153     if (has_arguments()) {
 154       args()->translate_type_data_from(data->as_CallTypeData()->args());
 155     }
 156     if (has_return()) {
 157       ret()->translate_type_data_from(data->as_CallTypeData()->ret());
 158     }
 159   }
 160 
 161   intptr_t argument_type(int i) const {
 162     assert(has_arguments(), "no arg type profiling data");
 163     return args()->type(i);
 164   }
 165 
 166   ciKlass* valid_argument_type(int i) const {
 167     assert(has_arguments(), "no arg type profiling data");
 168     return args()->valid_type(i);
 169   }
 170 
 171   intptr_t return_type() const {
 172     assert(has_return(), "no ret type profiling data");
 173     return ret()->type();
 174   }
 175 
 176   ciKlass* valid_return_type() const {
 177     assert(has_return(), "no ret type profiling data");
 178     return ret()->valid_type();
 179   }
 180 
 181   ProfilePtrKind argument_ptr_kind(int i) const {
 182     return args()->ptr_kind(i);
 183   }
 184 
 185   ProfilePtrKind return_ptr_kind() const {
 186     return ret()->ptr_kind();
 187   }
 188 
 189 #ifndef PRODUCT
 190   void print_data_on(outputStream* st, const char* extra = NULL) const;
 191 #endif
 192 };
 193 
 194 class ciReceiverTypeData : public ReceiverTypeData {
 195 public:
 196   ciReceiverTypeData(DataLayout* layout) : ReceiverTypeData(layout) {};
 197 
 198   void set_receiver(uint row, ciKlass* recv) {
 199     assert((uint)row < row_limit(), "oob");
 200     set_intptr_at(receiver0_offset + row * receiver_type_row_cell_count,
 201                   (intptr_t) recv);
 202   }
 203 
 204   ciKlass* receiver(uint row) const {
 205     assert((uint)row < row_limit(), "oob");
 206     ciKlass* recv = (ciKlass*)intptr_at(receiver0_offset + row * receiver_type_row_cell_count);
 207     assert(recv == NULL || recv->is_klass(), "wrong type");
 208     return recv;
 209   }
 210 
 211   // Copy & translate from oop based ReceiverTypeData
 212   virtual void translate_from(const ProfileData* data) {
 213     translate_receiver_data_from(data);
 214   }
 215   void translate_receiver_data_from(const ProfileData* data);
 216 #ifndef PRODUCT
 217   void print_data_on(outputStream* st, const char* extra = NULL) const;
 218   void print_receiver_data_on(outputStream* st) const;
 219 #endif
 220 };
 221 
 222 class ciVirtualCallData : public VirtualCallData {
 223   // Fake multiple inheritance...  It's a ciReceiverTypeData also.
 224   ciReceiverTypeData* rtd_super() const { return (ciReceiverTypeData*) this; }
 225 
 226 public:
 227   ciVirtualCallData(DataLayout* layout) : VirtualCallData(layout) {};
 228 
 229   void set_receiver(uint row, ciKlass* recv) {
 230     rtd_super()->set_receiver(row, recv);
 231   }
 232 
 233   ciKlass* receiver(uint row) {
 234     return rtd_super()->receiver(row);
 235   }
 236 
 237   // Copy & translate from oop based VirtualCallData
 238   virtual void translate_from(const ProfileData* data) {
 239     rtd_super()->translate_receiver_data_from(data);
 240   }
 241 #ifndef PRODUCT
 242   void print_data_on(outputStream* st, const char* extra = NULL) const;
 243 #endif
 244 };
 245 
 246 class ciVirtualCallTypeData : public VirtualCallTypeData {
 247 private:
 248   // Fake multiple inheritance...  It's a ciReceiverTypeData also.
 249   ciReceiverTypeData* rtd_super() const { return (ciReceiverTypeData*) this; }
 250 public:
 251   ciVirtualCallTypeData(DataLayout* layout) : VirtualCallTypeData(layout) {}
 252 
 253   void set_receiver(uint row, ciKlass* recv) {
 254     rtd_super()->set_receiver(row, recv);
 255   }
 256 
 257   ciKlass* receiver(uint row) const {
 258     return rtd_super()->receiver(row);
 259   }
 260 
 261   ciTypeStackSlotEntries* args() const { return (ciTypeStackSlotEntries*)VirtualCallTypeData::args(); }
 262   ciReturnTypeEntry* ret() const { return (ciReturnTypeEntry*)VirtualCallTypeData::ret(); }
 263 
 264   // Copy & translate from oop based VirtualCallData
 265   virtual void translate_from(const ProfileData* data) {
 266     rtd_super()->translate_receiver_data_from(data);
 267     if (has_arguments()) {
 268       args()->translate_type_data_from(data->as_VirtualCallTypeData()->args());
 269     }
 270     if (has_return()) {
 271       ret()->translate_type_data_from(data->as_VirtualCallTypeData()->ret());
 272     }
 273   }
 274 
 275   intptr_t argument_type(int i) const {
 276     assert(has_arguments(), "no arg type profiling data");
 277     return args()->type(i);
 278   }
 279 
 280   ciKlass* valid_argument_type(int i) const {
 281     assert(has_arguments(), "no arg type profiling data");
 282     return args()->valid_type(i);
 283   }
 284 
 285   intptr_t return_type() const {
 286     assert(has_return(), "no ret type profiling data");
 287     return ret()->type();
 288   }
 289 
 290   ciKlass* valid_return_type() const {
 291     assert(has_return(), "no ret type profiling data");
 292     return ret()->valid_type();
 293   }
 294 
 295   ProfilePtrKind argument_ptr_kind(int i) const {
 296     return args()->ptr_kind(i);
 297   }
 298 
 299   ProfilePtrKind return_ptr_kind() const {
 300     return ret()->ptr_kind();
 301   }
 302 
 303 #ifndef PRODUCT
 304   void print_data_on(outputStream* st, const char* extra = NULL) const;
 305 #endif
 306 };
 307 
 308 
 309 class ciRetData : public RetData {
 310 public:
 311   ciRetData(DataLayout* layout) : RetData(layout) {};
 312 };
 313 
 314 class ciBranchData : public BranchData {
 315 public:
 316   ciBranchData(DataLayout* layout) : BranchData(layout) {};
 317 };
 318 
 319 class ciArrayData : public ArrayData {
 320 public:
 321   ciArrayData(DataLayout* layout) : ArrayData(layout) {};
 322 };
 323 
 324 class ciMultiBranchData : public MultiBranchData {
 325 public:
 326   ciMultiBranchData(DataLayout* layout) : MultiBranchData(layout) {};
 327 };
 328 
 329 class ciArgInfoData : public ArgInfoData {
 330 public:
 331   ciArgInfoData(DataLayout* layout) : ArgInfoData(layout) {};
 332 };
 333 
 334 class ciParametersTypeData : public ParametersTypeData {
 335 public:
 336   ciParametersTypeData(DataLayout* layout) : ParametersTypeData(layout) {}
 337 
 338   virtual void translate_from(const ProfileData* data) {
 339     parameters()->translate_type_data_from(data->as_ParametersTypeData()->parameters());
 340   }
 341 
 342   ciTypeStackSlotEntries* parameters() const { return (ciTypeStackSlotEntries*)ParametersTypeData::parameters(); }
 343 
 344   ciKlass* valid_parameter_type(int i) const {
 345     return parameters()->valid_type(i);
 346   }
 347   
 348   ProfilePtrKind parameter_ptr_kind(int i) const {
 349     return parameters()->ptr_kind(i);
 350   }
 351 
 352 #ifndef PRODUCT
 353   void print_data_on(outputStream* st, const char* extra = NULL) const;
 354 #endif
 355 };
 356 
 357 class ciSpeculativeTrapData : public SpeculativeTrapData {
 358 public:
 359   ciSpeculativeTrapData(DataLayout* layout) : SpeculativeTrapData(layout) {}
 360 
 361   virtual void translate_from(const ProfileData* data);
 362 
 363   ciMethod* method() const {
 364     return (ciMethod*)intptr_at(speculative_trap_method);
 365   }
 366 
 367   void set_method(ciMethod* m) {
 368     set_intptr_at(speculative_trap_method, (intptr_t)m);
 369   }
 370 
 371 #ifndef PRODUCT
 372   void print_data_on(outputStream* st, const char* extra = NULL) const;
 373 #endif
 374 };
 375 
 376 // ciMethodData
 377 //
 378 // This class represents a MethodData* in the HotSpot virtual
 379 // machine.
 380 
 381 class ciMethodData : public ciMetadata {
 382   CI_PACKAGE_ACCESS
 383   friend class ciReplay;
 384 
 385 private:
 386   // Size in bytes
 387   int _data_size;
 388   int _extra_data_size;
 389 
 390   // Data entries
 391   intptr_t* _data;
 392 
 393   // Cached hint for data_before()
 394   int _hint_di;
 395 
 396   // Is data attached?  And is it mature?
 397   enum { empty_state, immature_state, mature_state };
 398   u_char _state;
 399 
 400   // Set this true if empty extra_data slots are ever witnessed.
 401   u_char _saw_free_extra_data;
 402 
 403   // Support for interprocedural escape analysis
 404   intx              _eflags;          // flags on escape information
 405   intx              _arg_local;       // bit set of non-escaping arguments
 406   intx              _arg_stack;       // bit set of stack-allocatable arguments
 407   intx              _arg_returned;    // bit set of returned arguments
 408 
 409   // Maturity of the oop when the snapshot is taken.
 410   int _current_mileage;
 411 
 412   // These counters hold the age of MDO in tiered. In tiered we can have the same method
 413   // running at different compilation levels concurrently. So, in order to precisely measure
 414   // its maturity we need separate counters.
 415   int _invocation_counter;
 416   int _backedge_counter;
 417 
 418   // Coherent snapshot of original header.
 419   MethodData _orig;
 420 
 421   // Area dedicated to parameters. NULL if no parameter profiling for
 422   // this method.
 423   DataLayout* _parameters;
 424   int parameters_size() const {
 425     return _parameters == NULL ? 0 : parameters_type_data()->size_in_bytes();
 426   }
 427 
 428   ciMethodData(MethodData* md);
 429   ciMethodData();
 430 
 431   // Accessors
 432   int data_size() const { return _data_size; }
 433   int extra_data_size() const { return _extra_data_size; }
 434   intptr_t * data() const { return _data; }
 435 
 436   MethodData* get_MethodData() const {
 437     return (MethodData*)_metadata;
 438   }
 439 
 440   const char* type_string()                      { return "ciMethodData"; }
 441 
 442   void print_impl(outputStream* st);
 443 
 444   DataLayout* data_layout_at(int data_index) const {
 445     assert(data_index % sizeof(intptr_t) == 0, "unaligned");
 446     return (DataLayout*) (((address)_data) + data_index);
 447   }
 448 
 449   bool out_of_bounds(int data_index) {
 450     return data_index >= data_size();
 451   }
 452 
 453   // hint accessors
 454   int      hint_di() const  { return _hint_di; }
 455   void set_hint_di(int di)  {
 456     assert(!out_of_bounds(di), "hint_di out of bounds");
 457     _hint_di = di;
 458   }
 459   ciProfileData* data_before(int bci) {
 460     // avoid SEGV on this edge case
 461     if (data_size() == 0)
 462       return NULL;
 463     int hint = hint_di();
 464     if (data_layout_at(hint)->bci() <= bci)
 465       return data_at(hint);
 466     return first_data();
 467   }
 468 
 469 
 470   // What is the index of the first data entry?
 471   int first_di() { return 0; }
 472 
 473   ciArgInfoData *arg_info() const;
 474 
 475   address data_base() const {
 476     return (address) _data;
 477   }
 478 
 479   void load_extra_data();
 480   ciProfileData* bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots);
 481 
 482   void dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k);
 483   template<class T> void dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data);
 484   template<class T> void dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* call_type_data);
 485   void dump_replay_data_extra_data_helper(outputStream* out, int round, int& count);
 486 
 487 public:
 488   bool is_method_data() const { return true; }
 489 
 490   bool is_empty()  { return _state == empty_state; }
 491   bool is_mature() { return _state == mature_state; }
 492 
 493   int creation_mileage() { return _orig.creation_mileage(); }
 494   int current_mileage()  { return _current_mileage; }
 495 
 496   int invocation_count() { return _invocation_counter; }
 497   int backedge_count()   { return _backedge_counter;   }
 498 
 499 #if INCLUDE_RTM_OPT
 500   // return cached value
 501   int rtm_state() {
 502     if (is_empty()) {
 503       return NoRTM;
 504     } else {
 505       return get_MethodData()->rtm_state();
 506     }
 507   }
 508 #endif
 509 
 510   // Transfer information about the method to MethodData*.
 511   // would_profile means we would like to profile this method,
 512   // meaning it's not trivial.
 513   void set_would_profile(bool p);
 514   // Also set the numer of loops and blocks in the method.
 515   // Again, this is used to determine if a method is trivial.
 516   void set_compilation_stats(short loops, short blocks);
 517   // If the compiler finds a profiled type that is known statically
 518   // for sure, set it in the MethodData
 519   void set_argument_type(int bci, int i, ciKlass* k);
 520   void set_parameter_type(int i, ciKlass* k);
 521   void set_return_type(int bci, ciKlass* k);
 522 
 523   void load_data();
 524 
 525   // Convert a dp (data pointer) to a di (data index).
 526   int dp_to_di(address dp) {
 527     return dp - ((address)_data);
 528   }
 529 
 530   // Get the data at an arbitrary (sort of) data index.
 531   ciProfileData* data_at(int data_index);
 532 
 533   // Walk through the data in order.
 534   ciProfileData* first_data() { return data_at(first_di()); }
 535   ciProfileData* next_data(ciProfileData* current);
 536   bool is_valid(ciProfileData* current) { return current != NULL; }
 537 
 538   DataLayout* extra_data_base() const  { return data_layout_at(data_size()); }
 539   DataLayout* args_data_limit() const  { return data_layout_at(data_size() + extra_data_size() -
 540                                                                parameters_size()); }
 541 
 542   // Get the data at an arbitrary bci, or NULL if there is none. If m
 543   // is not NULL look for a SpeculativeTrapData if any first.
 544   ciProfileData* bci_to_data(int bci, ciMethod* m = NULL);
 545 
 546   uint overflow_trap_count() const {
 547     return _orig.overflow_trap_count();
 548   }
 549   uint overflow_recompile_count() const {
 550     return _orig.overflow_recompile_count();
 551   }
 552   uint decompile_count() const {
 553     return _orig.decompile_count();
 554   }
 555   uint trap_count(int reason) const {
 556     return _orig.trap_count(reason);
 557   }
 558   uint trap_reason_limit() const { return _orig.trap_reason_limit(); }
 559   uint trap_count_limit()  const { return _orig.trap_count_limit(); }
 560 
 561   // Helpful query functions that decode trap_state.
 562   int has_trap_at(ciProfileData* data, int reason);
 563   int has_trap_at(int bci, ciMethod* m, int reason) {
 564     assert((m != NULL) == Deoptimization::reason_is_speculate(reason), "inconsistent method/reason");
 565     return has_trap_at(bci_to_data(bci, m), reason);
 566   }
 567   int trap_recompiled_at(ciProfileData* data);
 568   int trap_recompiled_at(int bci, ciMethod* m) {
 569     return trap_recompiled_at(bci_to_data(bci, m));
 570   }
 571 
 572   void clear_escape_info();
 573   bool has_escape_info();
 574   void update_escape_info();
 575 
 576   void set_eflag(MethodData::EscapeFlag f);
 577   void clear_eflag(MethodData::EscapeFlag f);
 578   bool eflag_set(MethodData::EscapeFlag f) const;
 579 
 580   void set_arg_local(int i);
 581   void set_arg_stack(int i);
 582   void set_arg_returned(int i);
 583   void set_arg_modified(int arg, uint val);
 584 
 585   bool is_arg_local(int i) const;
 586   bool is_arg_stack(int i) const;
 587   bool is_arg_returned(int i) const;
 588   uint arg_modified(int arg) const;
 589 
 590   ciParametersTypeData* parameters_type_data() const {
 591     return _parameters != NULL ? new ciParametersTypeData(_parameters) : NULL;
 592   }
 593 
 594   // Code generation helper
 595   ByteSize offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data);
 596   int      byte_offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) { return in_bytes(offset_of_slot(data, slot_offset_in_data)); }
 597 
 598 #ifndef PRODUCT
 599   // printing support for method data
 600   void print();
 601   void print_data_on(outputStream* st);
 602 #endif
 603   void dump_replay_data(outputStream* out);
 604 };
 605 
 606 #endif // SHARE_VM_CI_CIMETHODDATA_HPP