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