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