1 /*
   2  * Copyright (c) 2000, 2018, 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_OOPS_METHODDATAOOP_HPP
  26 #define SHARE_VM_OOPS_METHODDATAOOP_HPP
  27 
  28 #include "interpreter/bytecodes.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/metadata.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/oop.hpp"
  33 #include "runtime/atomic.hpp"
  34 #include "utilities/align.hpp"
  35 #if INCLUDE_JVMCI
  36 #include "jvmci/jvmci_globals.hpp"
  37 #endif
  38 
  39 class BytecodeStream;
  40 class KlassSizeStats;
  41 
  42 // The MethodData object collects counts and other profile information
  43 // during zeroth-tier (interpretive) and first-tier execution.
  44 // The profile is used later by compilation heuristics.  Some heuristics
  45 // enable use of aggressive (or "heroic") optimizations.  An aggressive
  46 // optimization often has a down-side, a corner case that it handles
  47 // poorly, but which is thought to be rare.  The profile provides
  48 // evidence of this rarity for a given method or even BCI.  It allows
  49 // the compiler to back out of the optimization at places where it
  50 // has historically been a poor choice.  Other heuristics try to use
  51 // specific information gathered about types observed at a given site.
  52 //
  53 // All data in the profile is approximate.  It is expected to be accurate
  54 // on the whole, but the system expects occasional inaccuraces, due to
  55 // counter overflow, multiprocessor races during data collection, space
  56 // limitations, missing MDO blocks, etc.  Bad or missing data will degrade
  57 // optimization quality but will not affect correctness.  Also, each MDO
  58 // is marked with its birth-date ("creation_mileage") which can be used
  59 // to assess the quality ("maturity") of its data.
  60 //
  61 // Short (<32-bit) counters are designed to overflow to a known "saturated"
  62 // state.  Also, certain recorded per-BCI events are given one-bit counters
  63 // which overflow to a saturated state which applied to all counters at
  64 // that BCI.  In other words, there is a small lattice which approximates
  65 // the ideal of an infinite-precision counter for each event at each BCI,
  66 // and the lattice quickly "bottoms out" in a state where all counters
  67 // are taken to be indefinitely large.
  68 //
  69 // The reader will find many data races in profile gathering code, starting
  70 // with invocation counter incrementation.  None of these races harm correct
  71 // execution of the compiled code.
  72 
  73 // forward decl
  74 class ProfileData;
  75 
  76 // DataLayout
  77 //
  78 // Overlay for generic profiling data.
  79 class DataLayout {
  80   friend class VMStructs;
  81   friend class JVMCIVMStructs;
  82 
  83 private:
  84   // Every data layout begins with a header.  This header
  85   // contains a tag, which is used to indicate the size/layout
  86   // of the data, 4 bits of flags, which can be used in any way,
  87   // 4 bits of trap history (none/one reason/many reasons),
  88   // and a bci, which is used to tie this piece of data to a
  89   // specific bci in the bytecodes.
  90   union {
  91     intptr_t _bits;
  92     struct {
  93       u1 _tag;
  94       u1 _flags;
  95       u2 _bci;
  96     } _struct;
  97   } _header;
  98 
  99   // The data layout has an arbitrary number of cells, each sized
 100   // to accomodate a pointer or an integer.
 101   intptr_t _cells[1];
 102 
 103   // Some types of data layouts need a length field.
 104   static bool needs_array_len(u1 tag);
 105 
 106 public:
 107   enum {
 108     counter_increment = 1
 109   };
 110 
 111   enum {
 112     cell_size = sizeof(intptr_t)
 113   };
 114 
 115   // Tag values
 116   enum {
 117     no_tag,
 118     bit_data_tag,
 119     counter_data_tag,
 120     jump_data_tag,
 121     receiver_type_data_tag,
 122     virtual_call_data_tag,
 123     ret_data_tag,
 124     branch_data_tag,
 125     multi_branch_data_tag,
 126     arg_info_data_tag,
 127     call_type_data_tag,
 128     virtual_call_type_data_tag,
 129     parameters_type_data_tag,
 130     speculative_trap_data_tag
 131   };
 132 
 133   enum {
 134     // The _struct._flags word is formatted as [trap_state:4 | flags:4].
 135     // The trap state breaks down further as [recompile:1 | reason:3].
 136     // This further breakdown is defined in deoptimization.cpp.
 137     // See Deoptimization::trap_state_reason for an assert that
 138     // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
 139     //
 140     // The trap_state is collected only if ProfileTraps is true.
 141     trap_bits = 1+3,  // 3: enough to distinguish [0..Reason_RECORDED_LIMIT].
 142     trap_shift = BitsPerByte - trap_bits,
 143     trap_mask = right_n_bits(trap_bits),
 144     trap_mask_in_place = (trap_mask << trap_shift),
 145     flag_limit = trap_shift,
 146     flag_mask = right_n_bits(flag_limit),
 147     first_flag = 0
 148   };
 149 
 150   // Size computation
 151   static int header_size_in_bytes() {
 152     return cell_size;
 153   }
 154   static int header_size_in_cells() {
 155     return 1;
 156   }
 157 
 158   static int compute_size_in_bytes(int cell_count) {
 159     return header_size_in_bytes() + cell_count * cell_size;
 160   }
 161 
 162   // Initialization
 163   void initialize(u1 tag, u2 bci, int cell_count);
 164 
 165   // Accessors
 166   u1 tag() {
 167     return _header._struct._tag;
 168   }
 169 
 170   // Return a few bits of trap state.  Range is [0..trap_mask].
 171   // The state tells if traps with zero, one, or many reasons have occurred.
 172   // It also tells whether zero or many recompilations have occurred.
 173   // The associated trap histogram in the MDO itself tells whether
 174   // traps are common or not.  If a BCI shows that a trap X has
 175   // occurred, and the MDO shows N occurrences of X, we make the
 176   // simplifying assumption that all N occurrences can be blamed
 177   // on that BCI.
 178   int trap_state() const {
 179     return ((_header._struct._flags >> trap_shift) & trap_mask);
 180   }
 181 
 182   void set_trap_state(int new_state) {
 183     assert(ProfileTraps, "used only under +ProfileTraps");
 184     uint old_flags = (_header._struct._flags & flag_mask);
 185     _header._struct._flags = (new_state << trap_shift) | old_flags;
 186   }
 187 
 188   u1 flags() const {
 189     return _header._struct._flags;
 190   }
 191 
 192   u2 bci() const {
 193     return _header._struct._bci;
 194   }
 195 
 196   void set_header(intptr_t value) {
 197     _header._bits = value;
 198   }
 199   intptr_t header() {
 200     return _header._bits;
 201   }
 202   void set_cell_at(int index, intptr_t value) {
 203     _cells[index] = value;
 204   }
 205   void release_set_cell_at(int index, intptr_t value);
 206   intptr_t cell_at(int index) const {
 207     return _cells[index];
 208   }
 209 
 210   void set_flag_at(int flag_number) {
 211     assert(flag_number < flag_limit, "oob");
 212     _header._struct._flags |= (0x1 << flag_number);
 213   }
 214   bool flag_at(int flag_number) const {
 215     assert(flag_number < flag_limit, "oob");
 216     return (_header._struct._flags & (0x1 << flag_number)) != 0;
 217   }
 218 
 219   // Low-level support for code generation.
 220   static ByteSize header_offset() {
 221     return byte_offset_of(DataLayout, _header);
 222   }
 223   static ByteSize tag_offset() {
 224     return byte_offset_of(DataLayout, _header._struct._tag);
 225   }
 226   static ByteSize flags_offset() {
 227     return byte_offset_of(DataLayout, _header._struct._flags);
 228   }
 229   static ByteSize bci_offset() {
 230     return byte_offset_of(DataLayout, _header._struct._bci);
 231   }
 232   static ByteSize cell_offset(int index) {
 233     return byte_offset_of(DataLayout, _cells) + in_ByteSize(index * cell_size);
 234   }
 235 #ifdef CC_INTERP
 236   static int cell_offset_in_bytes(int index) {
 237     return (int)offset_of(DataLayout, _cells[index]);
 238   }
 239 #endif // CC_INTERP
 240   // Return a value which, when or-ed as a byte into _flags, sets the flag.
 241   static int flag_number_to_byte_constant(int flag_number) {
 242     assert(0 <= flag_number && flag_number < flag_limit, "oob");
 243     DataLayout temp; temp.set_header(0);
 244     temp.set_flag_at(flag_number);
 245     return temp._header._struct._flags;
 246   }
 247   // Return a value which, when or-ed as a word into _header, sets the flag.
 248   static intptr_t flag_mask_to_header_mask(int byte_constant) {
 249     DataLayout temp; temp.set_header(0);
 250     temp._header._struct._flags = byte_constant;
 251     return temp._header._bits;
 252   }
 253 
 254   ProfileData* data_in();
 255 
 256   // GC support
 257   void clean_weak_klass_links(bool always_clean);
 258 
 259   // Redefinition support
 260   void clean_weak_method_links();
 261   DEBUG_ONLY(void verify_clean_weak_method_links();)
 262 };
 263 
 264 
 265 // ProfileData class hierarchy
 266 class ProfileData;
 267 class   BitData;
 268 class     CounterData;
 269 class       ReceiverTypeData;
 270 class         VirtualCallData;
 271 class           VirtualCallTypeData;
 272 class       RetData;
 273 class       CallTypeData;
 274 class   JumpData;
 275 class     BranchData;
 276 class   ArrayData;
 277 class     MultiBranchData;
 278 class     ArgInfoData;
 279 class     ParametersTypeData;
 280 class   SpeculativeTrapData;
 281 
 282 // ProfileData
 283 //
 284 // A ProfileData object is created to refer to a section of profiling
 285 // data in a structured way.
 286 class ProfileData : public ResourceObj {
 287   friend class TypeEntries;
 288   friend class ReturnTypeEntry;
 289   friend class TypeStackSlotEntries;
 290 private:
 291   enum {
 292     tab_width_one = 16,
 293     tab_width_two = 36
 294   };
 295 
 296   // This is a pointer to a section of profiling data.
 297   DataLayout* _data;
 298 
 299   char* print_data_on_helper(const MethodData* md) const;
 300 
 301 protected:
 302   DataLayout* data() { return _data; }
 303   const DataLayout* data() const { return _data; }
 304 
 305   enum {
 306     cell_size = DataLayout::cell_size
 307   };
 308 
 309 public:
 310   // How many cells are in this?
 311   virtual int cell_count() const {
 312     ShouldNotReachHere();
 313     return -1;
 314   }
 315 
 316   // Return the size of this data.
 317   int size_in_bytes() {
 318     return DataLayout::compute_size_in_bytes(cell_count());
 319   }
 320 
 321 protected:
 322   // Low-level accessors for underlying data
 323   void set_intptr_at(int index, intptr_t value) {
 324     assert(0 <= index && index < cell_count(), "oob");
 325     data()->set_cell_at(index, value);
 326   }
 327   void release_set_intptr_at(int index, intptr_t value);
 328   intptr_t intptr_at(int index) const {
 329     assert(0 <= index && index < cell_count(), "oob");
 330     return data()->cell_at(index);
 331   }
 332   void set_uint_at(int index, uint value) {
 333     set_intptr_at(index, (intptr_t) value);
 334   }
 335   void release_set_uint_at(int index, uint value);
 336   uint uint_at(int index) const {
 337     return (uint)intptr_at(index);
 338   }
 339   void set_int_at(int index, int value) {
 340     set_intptr_at(index, (intptr_t) value);
 341   }
 342   void release_set_int_at(int index, int value);
 343   int int_at(int index) const {
 344     return (int)intptr_at(index);
 345   }
 346   int int_at_unchecked(int index) const {
 347     return (int)data()->cell_at(index);
 348   }
 349   void set_oop_at(int index, oop value) {
 350     set_intptr_at(index, cast_from_oop<intptr_t>(value));
 351   }
 352   oop oop_at(int index) const {
 353     return cast_to_oop(intptr_at(index));
 354   }
 355 
 356   void set_flag_at(int flag_number) {
 357     data()->set_flag_at(flag_number);
 358   }
 359   bool flag_at(int flag_number) const {
 360     return data()->flag_at(flag_number);
 361   }
 362 
 363   // two convenient imports for use by subclasses:
 364   static ByteSize cell_offset(int index) {
 365     return DataLayout::cell_offset(index);
 366   }
 367   static int flag_number_to_byte_constant(int flag_number) {
 368     return DataLayout::flag_number_to_byte_constant(flag_number);
 369   }
 370 
 371   ProfileData(DataLayout* data) {
 372     _data = data;
 373   }
 374 
 375 #ifdef CC_INTERP
 376   // Static low level accessors for DataLayout with ProfileData's semantics.
 377 
 378   static int cell_offset_in_bytes(int index) {
 379     return DataLayout::cell_offset_in_bytes(index);
 380   }
 381 
 382   static void increment_uint_at_no_overflow(DataLayout* layout, int index,
 383                                             int inc = DataLayout::counter_increment) {
 384     uint count = ((uint)layout->cell_at(index)) + inc;
 385     if (count == 0) return;
 386     layout->set_cell_at(index, (intptr_t) count);
 387   }
 388 
 389   static int int_at(DataLayout* layout, int index) {
 390     return (int)layout->cell_at(index);
 391   }
 392 
 393   static int uint_at(DataLayout* layout, int index) {
 394     return (uint)layout->cell_at(index);
 395   }
 396 
 397   static oop oop_at(DataLayout* layout, int index) {
 398     return cast_to_oop(layout->cell_at(index));
 399   }
 400 
 401   static void set_intptr_at(DataLayout* layout, int index, intptr_t value) {
 402     layout->set_cell_at(index, (intptr_t) value);
 403   }
 404 
 405   static void set_flag_at(DataLayout* layout, int flag_number) {
 406     layout->set_flag_at(flag_number);
 407   }
 408 #endif // CC_INTERP
 409 
 410 public:
 411   // Constructor for invalid ProfileData.
 412   ProfileData();
 413 
 414   u2 bci() const {
 415     return data()->bci();
 416   }
 417 
 418   address dp() {
 419     return (address)_data;
 420   }
 421 
 422   int trap_state() const {
 423     return data()->trap_state();
 424   }
 425   void set_trap_state(int new_state) {
 426     data()->set_trap_state(new_state);
 427   }
 428 
 429   // Type checking
 430   virtual bool is_BitData()         const { return false; }
 431   virtual bool is_CounterData()     const { return false; }
 432   virtual bool is_JumpData()        const { return false; }
 433   virtual bool is_ReceiverTypeData()const { return false; }
 434   virtual bool is_VirtualCallData() const { return false; }
 435   virtual bool is_RetData()         const { return false; }
 436   virtual bool is_BranchData()      const { return false; }
 437   virtual bool is_ArrayData()       const { return false; }
 438   virtual bool is_MultiBranchData() const { return false; }
 439   virtual bool is_ArgInfoData()     const { return false; }
 440   virtual bool is_CallTypeData()    const { return false; }
 441   virtual bool is_VirtualCallTypeData()const { return false; }
 442   virtual bool is_ParametersTypeData() const { return false; }
 443   virtual bool is_SpeculativeTrapData()const { return false; }
 444 
 445 
 446   BitData* as_BitData() const {
 447     assert(is_BitData(), "wrong type");
 448     return is_BitData()         ? (BitData*)        this : NULL;
 449   }
 450   CounterData* as_CounterData() const {
 451     assert(is_CounterData(), "wrong type");
 452     return is_CounterData()     ? (CounterData*)    this : NULL;
 453   }
 454   JumpData* as_JumpData() const {
 455     assert(is_JumpData(), "wrong type");
 456     return is_JumpData()        ? (JumpData*)       this : NULL;
 457   }
 458   ReceiverTypeData* as_ReceiverTypeData() const {
 459     assert(is_ReceiverTypeData(), "wrong type");
 460     return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL;
 461   }
 462   VirtualCallData* as_VirtualCallData() const {
 463     assert(is_VirtualCallData(), "wrong type");
 464     return is_VirtualCallData() ? (VirtualCallData*)this : NULL;
 465   }
 466   RetData* as_RetData() const {
 467     assert(is_RetData(), "wrong type");
 468     return is_RetData()         ? (RetData*)        this : NULL;
 469   }
 470   BranchData* as_BranchData() const {
 471     assert(is_BranchData(), "wrong type");
 472     return is_BranchData()      ? (BranchData*)     this : NULL;
 473   }
 474   ArrayData* as_ArrayData() const {
 475     assert(is_ArrayData(), "wrong type");
 476     return is_ArrayData()       ? (ArrayData*)      this : NULL;
 477   }
 478   MultiBranchData* as_MultiBranchData() const {
 479     assert(is_MultiBranchData(), "wrong type");
 480     return is_MultiBranchData() ? (MultiBranchData*)this : NULL;
 481   }
 482   ArgInfoData* as_ArgInfoData() const {
 483     assert(is_ArgInfoData(), "wrong type");
 484     return is_ArgInfoData() ? (ArgInfoData*)this : NULL;
 485   }
 486   CallTypeData* as_CallTypeData() const {
 487     assert(is_CallTypeData(), "wrong type");
 488     return is_CallTypeData() ? (CallTypeData*)this : NULL;
 489   }
 490   VirtualCallTypeData* as_VirtualCallTypeData() const {
 491     assert(is_VirtualCallTypeData(), "wrong type");
 492     return is_VirtualCallTypeData() ? (VirtualCallTypeData*)this : NULL;
 493   }
 494   ParametersTypeData* as_ParametersTypeData() const {
 495     assert(is_ParametersTypeData(), "wrong type");
 496     return is_ParametersTypeData() ? (ParametersTypeData*)this : NULL;
 497   }
 498   SpeculativeTrapData* as_SpeculativeTrapData() const {
 499     assert(is_SpeculativeTrapData(), "wrong type");
 500     return is_SpeculativeTrapData() ? (SpeculativeTrapData*)this : NULL;
 501   }
 502 
 503 
 504   // Subclass specific initialization
 505   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {}
 506 
 507   // GC support
 508   virtual void clean_weak_klass_links(bool always_clean) {}
 509 
 510   // Redefinition support
 511   virtual void clean_weak_method_links() {}
 512   DEBUG_ONLY(virtual void verify_clean_weak_method_links() {})
 513 
 514   // CI translation: ProfileData can represent both MethodDataOop data
 515   // as well as CIMethodData data. This function is provided for translating
 516   // an oop in a ProfileData to the ci equivalent. Generally speaking,
 517   // most ProfileData don't require any translation, so we provide the null
 518   // translation here, and the required translators are in the ci subclasses.
 519   virtual void translate_from(const ProfileData* data) {}
 520 
 521   virtual void print_data_on(outputStream* st, const char* extra = NULL) const {
 522     ShouldNotReachHere();
 523   }
 524 
 525   void print_data_on(outputStream* st, const MethodData* md) const;
 526 
 527   void print_shared(outputStream* st, const char* name, const char* extra) const;
 528   void tab(outputStream* st, bool first = false) const;
 529 };
 530 
 531 // BitData
 532 //
 533 // A BitData holds a flag or two in its header.
 534 class BitData : public ProfileData {
 535   friend class VMStructs;
 536   friend class JVMCIVMStructs;
 537 protected:
 538   enum {
 539     // null_seen:
 540     //  saw a null operand (cast/aastore/instanceof)
 541       null_seen_flag              = DataLayout::first_flag + 0
 542 #if INCLUDE_JVMCI
 543     // bytecode threw any exception
 544     , exception_seen_flag         = null_seen_flag + 1
 545 #endif
 546   };
 547   enum { bit_cell_count = 0 };  // no additional data fields needed.
 548 public:
 549   BitData(DataLayout* layout) : ProfileData(layout) {
 550   }
 551 
 552   virtual bool is_BitData() const { return true; }
 553 
 554   static int static_cell_count() {
 555     return bit_cell_count;
 556   }
 557 
 558   virtual int cell_count() const {
 559     return static_cell_count();
 560   }
 561 
 562   // Accessor
 563 
 564   // The null_seen flag bit is specially known to the interpreter.
 565   // Consulting it allows the compiler to avoid setting up null_check traps.
 566   bool null_seen()     { return flag_at(null_seen_flag); }
 567   void set_null_seen()    { set_flag_at(null_seen_flag); }
 568 
 569 #if INCLUDE_JVMCI
 570   // true if an exception was thrown at the specific BCI
 571   bool exception_seen() { return flag_at(exception_seen_flag); }
 572   void set_exception_seen() { set_flag_at(exception_seen_flag); }
 573 #endif
 574 
 575   // Code generation support
 576   static int null_seen_byte_constant() {
 577     return flag_number_to_byte_constant(null_seen_flag);
 578   }
 579 
 580   static ByteSize bit_data_size() {
 581     return cell_offset(bit_cell_count);
 582   }
 583 
 584 #ifdef CC_INTERP
 585   static int bit_data_size_in_bytes() {
 586     return cell_offset_in_bytes(bit_cell_count);
 587   }
 588 
 589   static void set_null_seen(DataLayout* layout) {
 590     set_flag_at(layout, null_seen_flag);
 591   }
 592 
 593   static DataLayout* advance(DataLayout* layout) {
 594     return (DataLayout*) (((address)layout) + (ssize_t)BitData::bit_data_size_in_bytes());
 595   }
 596 #endif // CC_INTERP
 597 
 598   void print_data_on(outputStream* st, const char* extra = NULL) const;
 599 };
 600 
 601 // CounterData
 602 //
 603 // A CounterData corresponds to a simple counter.
 604 class CounterData : public BitData {
 605   friend class VMStructs;
 606   friend class JVMCIVMStructs;
 607 protected:
 608   enum {
 609     count_off,
 610     counter_cell_count
 611   };
 612 public:
 613   CounterData(DataLayout* layout) : BitData(layout) {}
 614 
 615   virtual bool is_CounterData() const { return true; }
 616 
 617   static int static_cell_count() {
 618     return counter_cell_count;
 619   }
 620 
 621   virtual int cell_count() const {
 622     return static_cell_count();
 623   }
 624 
 625   // Direct accessor
 626   uint count() const {
 627     return uint_at(count_off);
 628   }
 629 
 630   // Code generation support
 631   static ByteSize count_offset() {
 632     return cell_offset(count_off);
 633   }
 634   static ByteSize counter_data_size() {
 635     return cell_offset(counter_cell_count);
 636   }
 637 
 638   void set_count(uint count) {
 639     set_uint_at(count_off, count);
 640   }
 641 
 642 #ifdef CC_INTERP
 643   static int counter_data_size_in_bytes() {
 644     return cell_offset_in_bytes(counter_cell_count);
 645   }
 646 
 647   static void increment_count_no_overflow(DataLayout* layout) {
 648     increment_uint_at_no_overflow(layout, count_off);
 649   }
 650 
 651   // Support counter decrementation at checkcast / subtype check failed.
 652   static void decrement_count(DataLayout* layout) {
 653     increment_uint_at_no_overflow(layout, count_off, -1);
 654   }
 655 
 656   static DataLayout* advance(DataLayout* layout) {
 657     return (DataLayout*) (((address)layout) + (ssize_t)CounterData::counter_data_size_in_bytes());
 658   }
 659 #endif // CC_INTERP
 660 
 661   void print_data_on(outputStream* st, const char* extra = NULL) const;
 662 };
 663 
 664 // JumpData
 665 //
 666 // A JumpData is used to access profiling information for a direct
 667 // branch.  It is a counter, used for counting the number of branches,
 668 // plus a data displacement, used for realigning the data pointer to
 669 // the corresponding target bci.
 670 class JumpData : public ProfileData {
 671   friend class VMStructs;
 672   friend class JVMCIVMStructs;
 673 protected:
 674   enum {
 675     taken_off_set,
 676     displacement_off_set,
 677     jump_cell_count
 678   };
 679 
 680   void set_displacement(int displacement) {
 681     set_int_at(displacement_off_set, displacement);
 682   }
 683 
 684 public:
 685   JumpData(DataLayout* layout) : ProfileData(layout) {
 686     assert(layout->tag() == DataLayout::jump_data_tag ||
 687       layout->tag() == DataLayout::branch_data_tag, "wrong type");
 688   }
 689 
 690   virtual bool is_JumpData() const { return true; }
 691 
 692   static int static_cell_count() {
 693     return jump_cell_count;
 694   }
 695 
 696   virtual int cell_count() const {
 697     return static_cell_count();
 698   }
 699 
 700   // Direct accessor
 701   uint taken() const {
 702     return uint_at(taken_off_set);
 703   }
 704 
 705   void set_taken(uint cnt) {
 706     set_uint_at(taken_off_set, cnt);
 707   }
 708 
 709   // Saturating counter
 710   uint inc_taken() {
 711     uint cnt = taken() + 1;
 712     // Did we wrap? Will compiler screw us??
 713     if (cnt == 0) cnt--;
 714     set_uint_at(taken_off_set, cnt);
 715     return cnt;
 716   }
 717 
 718   int displacement() const {
 719     return int_at(displacement_off_set);
 720   }
 721 
 722   // Code generation support
 723   static ByteSize taken_offset() {
 724     return cell_offset(taken_off_set);
 725   }
 726 
 727   static ByteSize displacement_offset() {
 728     return cell_offset(displacement_off_set);
 729   }
 730 
 731 #ifdef CC_INTERP
 732   static void increment_taken_count_no_overflow(DataLayout* layout) {
 733     increment_uint_at_no_overflow(layout, taken_off_set);
 734   }
 735 
 736   static DataLayout* advance_taken(DataLayout* layout) {
 737     return (DataLayout*) (((address)layout) + (ssize_t)int_at(layout, displacement_off_set));
 738   }
 739 
 740   static uint taken_count(DataLayout* layout) {
 741     return (uint) uint_at(layout, taken_off_set);
 742   }
 743 #endif // CC_INTERP
 744 
 745   // Specific initialization.
 746   void post_initialize(BytecodeStream* stream, MethodData* mdo);
 747 
 748   void print_data_on(outputStream* st, const char* extra = NULL) const;
 749 };
 750 
 751 // Entries in a ProfileData object to record types: it can either be
 752 // none (no profile), unknown (conflicting profile data) or a klass if
 753 // a single one is seen. Whether a null reference was seen is also
 754 // recorded. No counter is associated with the type and a single type
 755 // is tracked (unlike VirtualCallData).
 756 class TypeEntries {
 757 
 758 public:
 759 
 760   // A single cell is used to record information for a type:
 761   // - the cell is initialized to 0
 762   // - when a type is discovered it is stored in the cell
 763   // - bit zero of the cell is used to record whether a null reference
 764   // was encountered or not
 765   // - bit 1 is set to record a conflict in the type information
 766 
 767   enum {
 768     null_seen = 1,
 769     type_mask = ~null_seen,
 770     type_unknown = 2,
 771     status_bits = null_seen | type_unknown,
 772     type_klass_mask = ~status_bits
 773   };
 774 
 775   // what to initialize a cell to
 776   static intptr_t type_none() {
 777     return 0;
 778   }
 779 
 780   // null seen = bit 0 set?
 781   static bool was_null_seen(intptr_t v) {
 782     return (v & null_seen) != 0;
 783   }
 784 
 785   // conflicting type information = bit 1 set?
 786   static bool is_type_unknown(intptr_t v) {
 787     return (v & type_unknown) != 0;
 788   }
 789 
 790   // not type information yet = all bits cleared, ignoring bit 0?
 791   static bool is_type_none(intptr_t v) {
 792     return (v & type_mask) == 0;
 793   }
 794 
 795   // recorded type: cell without bit 0 and 1
 796   static intptr_t klass_part(intptr_t v) {
 797     intptr_t r = v & type_klass_mask;
 798     return r;
 799   }
 800 
 801   // type recorded
 802   static Klass* valid_klass(intptr_t k) {
 803     if (!is_type_none(k) &&
 804         !is_type_unknown(k)) {
 805       Klass* res = (Klass*)klass_part(k);
 806       assert(res != NULL, "invalid");
 807       return res;
 808     } else {
 809       return NULL;
 810     }
 811   }
 812 
 813   static intptr_t with_status(intptr_t k, intptr_t in) {
 814     return k | (in & status_bits);
 815   }
 816 
 817   static intptr_t with_status(Klass* k, intptr_t in) {
 818     return with_status((intptr_t)k, in);
 819   }
 820 
 821   static void print_klass(outputStream* st, intptr_t k);
 822 
 823 protected:
 824   // ProfileData object these entries are part of
 825   ProfileData* _pd;
 826   // offset within the ProfileData object where the entries start
 827   const int _base_off;
 828 
 829   TypeEntries(int base_off)
 830     : _base_off(base_off), _pd(NULL) {}
 831 
 832   void set_intptr_at(int index, intptr_t value) {
 833     _pd->set_intptr_at(index, value);
 834   }
 835 
 836   intptr_t intptr_at(int index) const {
 837     return _pd->intptr_at(index);
 838   }
 839 
 840 public:
 841   void set_profile_data(ProfileData* pd) {
 842     _pd = pd;
 843   }
 844 };
 845 
 846 // Type entries used for arguments passed at a call and parameters on
 847 // method entry. 2 cells per entry: one for the type encoded as in
 848 // TypeEntries and one initialized with the stack slot where the
 849 // profiled object is to be found so that the interpreter can locate
 850 // it quickly.
 851 class TypeStackSlotEntries : public TypeEntries {
 852 
 853 private:
 854   enum {
 855     stack_slot_entry,
 856     type_entry,
 857     per_arg_cell_count
 858   };
 859 
 860   // offset of cell for stack slot for entry i within ProfileData object
 861   int stack_slot_offset(int i) const {
 862     return _base_off + stack_slot_local_offset(i);
 863   }
 864 
 865   const int _number_of_entries;
 866 
 867   // offset of cell for type for entry i within ProfileData object
 868   int type_offset_in_cells(int i) const {
 869     return _base_off + type_local_offset(i);
 870   }
 871 
 872 public:
 873 
 874   TypeStackSlotEntries(int base_off, int nb_entries)
 875     : TypeEntries(base_off), _number_of_entries(nb_entries) {}
 876 
 877   static int compute_cell_count(Symbol* signature, bool include_receiver, int max);
 878 
 879   void post_initialize(Symbol* signature, bool has_receiver, bool include_receiver);
 880 
 881   int number_of_entries() const { return _number_of_entries; }
 882 
 883   // offset of cell for stack slot for entry i within this block of cells for a TypeStackSlotEntries
 884   static int stack_slot_local_offset(int i) {
 885     return i * per_arg_cell_count + stack_slot_entry;
 886   }
 887 
 888   // offset of cell for type for entry i within this block of cells for a TypeStackSlotEntries
 889   static int type_local_offset(int i) {
 890     return i * per_arg_cell_count + type_entry;
 891   }
 892 
 893   // stack slot for entry i
 894   uint stack_slot(int i) const {
 895     assert(i >= 0 && i < _number_of_entries, "oob");
 896     return _pd->uint_at(stack_slot_offset(i));
 897   }
 898 
 899   // set stack slot for entry i
 900   void set_stack_slot(int i, uint num) {
 901     assert(i >= 0 && i < _number_of_entries, "oob");
 902     _pd->set_uint_at(stack_slot_offset(i), num);
 903   }
 904 
 905   // type for entry i
 906   intptr_t type(int i) const {
 907     assert(i >= 0 && i < _number_of_entries, "oob");
 908     return _pd->intptr_at(type_offset_in_cells(i));
 909   }
 910 
 911   // set type for entry i
 912   void set_type(int i, intptr_t k) {
 913     assert(i >= 0 && i < _number_of_entries, "oob");
 914     _pd->set_intptr_at(type_offset_in_cells(i), k);
 915   }
 916 
 917   static ByteSize per_arg_size() {
 918     return in_ByteSize(per_arg_cell_count * DataLayout::cell_size);
 919   }
 920 
 921   static int per_arg_count() {
 922     return per_arg_cell_count;
 923   }
 924 
 925   ByteSize type_offset(int i) const {
 926     return DataLayout::cell_offset(type_offset_in_cells(i));
 927   }
 928 
 929   // GC support
 930   void clean_weak_klass_links(bool always_clean);
 931 
 932   void print_data_on(outputStream* st) const;
 933 };
 934 
 935 // Type entry used for return from a call. A single cell to record the
 936 // type.
 937 class ReturnTypeEntry : public TypeEntries {
 938 
 939 private:
 940   enum {
 941     cell_count = 1
 942   };
 943 
 944 public:
 945   ReturnTypeEntry(int base_off)
 946     : TypeEntries(base_off) {}
 947 
 948   void post_initialize() {
 949     set_type(type_none());
 950   }
 951 
 952   intptr_t type() const {
 953     return _pd->intptr_at(_base_off);
 954   }
 955 
 956   void set_type(intptr_t k) {
 957     _pd->set_intptr_at(_base_off, k);
 958   }
 959 
 960   static int static_cell_count() {
 961     return cell_count;
 962   }
 963 
 964   static ByteSize size() {
 965     return in_ByteSize(cell_count * DataLayout::cell_size);
 966   }
 967 
 968   ByteSize type_offset() {
 969     return DataLayout::cell_offset(_base_off);
 970   }
 971 
 972   // GC support
 973   void clean_weak_klass_links(bool always_clean);
 974 
 975   void print_data_on(outputStream* st) const;
 976 };
 977 
 978 // Entries to collect type information at a call: contains arguments
 979 // (TypeStackSlotEntries), a return type (ReturnTypeEntry) and a
 980 // number of cells. Because the number of cells for the return type is
 981 // smaller than the number of cells for the type of an arguments, the
 982 // number of cells is used to tell how many arguments are profiled and
 983 // whether a return value is profiled. See has_arguments() and
 984 // has_return().
 985 class TypeEntriesAtCall {
 986 private:
 987   static int stack_slot_local_offset(int i) {
 988     return header_cell_count() + TypeStackSlotEntries::stack_slot_local_offset(i);
 989   }
 990 
 991   static int argument_type_local_offset(int i) {
 992     return header_cell_count() + TypeStackSlotEntries::type_local_offset(i);
 993   }
 994 
 995 public:
 996 
 997   static int header_cell_count() {
 998     return 1;
 999   }
1000 
1001   static int cell_count_local_offset() {
1002     return 0;
1003   }
1004 
1005   static int compute_cell_count(BytecodeStream* stream);
1006 
1007   static void initialize(DataLayout* dl, int base, int cell_count) {
1008     int off = base + cell_count_local_offset();
1009     dl->set_cell_at(off, cell_count - base - header_cell_count());
1010   }
1011 
1012   static bool arguments_profiling_enabled();
1013   static bool return_profiling_enabled();
1014 
1015   // Code generation support
1016   static ByteSize cell_count_offset() {
1017     return in_ByteSize(cell_count_local_offset() * DataLayout::cell_size);
1018   }
1019 
1020   static ByteSize args_data_offset() {
1021     return in_ByteSize(header_cell_count() * DataLayout::cell_size);
1022   }
1023 
1024   static ByteSize stack_slot_offset(int i) {
1025     return in_ByteSize(stack_slot_local_offset(i) * DataLayout::cell_size);
1026   }
1027 
1028   static ByteSize argument_type_offset(int i) {
1029     return in_ByteSize(argument_type_local_offset(i) * DataLayout::cell_size);
1030   }
1031 
1032   static ByteSize return_only_size() {
1033     return ReturnTypeEntry::size() + in_ByteSize(header_cell_count() * DataLayout::cell_size);
1034   }
1035 
1036 };
1037 
1038 // CallTypeData
1039 //
1040 // A CallTypeData is used to access profiling information about a non
1041 // virtual call for which we collect type information about arguments
1042 // and return value.
1043 class CallTypeData : public CounterData {
1044 private:
1045   // entries for arguments if any
1046   TypeStackSlotEntries _args;
1047   // entry for return type if any
1048   ReturnTypeEntry _ret;
1049 
1050   int cell_count_global_offset() const {
1051     return CounterData::static_cell_count() + TypeEntriesAtCall::cell_count_local_offset();
1052   }
1053 
1054   // number of cells not counting the header
1055   int cell_count_no_header() const {
1056     return uint_at(cell_count_global_offset());
1057   }
1058 
1059   void check_number_of_arguments(int total) {
1060     assert(number_of_arguments() == total, "should be set in DataLayout::initialize");
1061   }
1062 
1063 public:
1064   CallTypeData(DataLayout* layout) :
1065     CounterData(layout),
1066     _args(CounterData::static_cell_count()+TypeEntriesAtCall::header_cell_count(), number_of_arguments()),
1067     _ret(cell_count() - ReturnTypeEntry::static_cell_count())
1068   {
1069     assert(layout->tag() == DataLayout::call_type_data_tag, "wrong type");
1070     // Some compilers (VC++) don't want this passed in member initialization list
1071     _args.set_profile_data(this);
1072     _ret.set_profile_data(this);
1073   }
1074 
1075   const TypeStackSlotEntries* args() const {
1076     assert(has_arguments(), "no profiling of arguments");
1077     return &_args;
1078   }
1079 
1080   const ReturnTypeEntry* ret() const {
1081     assert(has_return(), "no profiling of return value");
1082     return &_ret;
1083   }
1084 
1085   virtual bool is_CallTypeData() const { return true; }
1086 
1087   static int static_cell_count() {
1088     return -1;
1089   }
1090 
1091   static int compute_cell_count(BytecodeStream* stream) {
1092     return CounterData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream);
1093   }
1094 
1095   static void initialize(DataLayout* dl, int cell_count) {
1096     TypeEntriesAtCall::initialize(dl, CounterData::static_cell_count(), cell_count);
1097   }
1098 
1099   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
1100 
1101   virtual int cell_count() const {
1102     return CounterData::static_cell_count() +
1103       TypeEntriesAtCall::header_cell_count() +
1104       int_at_unchecked(cell_count_global_offset());
1105   }
1106 
1107   int number_of_arguments() const {
1108     return cell_count_no_header() / TypeStackSlotEntries::per_arg_count();
1109   }
1110 
1111   void set_argument_type(int i, Klass* k) {
1112     assert(has_arguments(), "no arguments!");
1113     intptr_t current = _args.type(i);
1114     _args.set_type(i, TypeEntries::with_status(k, current));
1115   }
1116 
1117   void set_return_type(Klass* k) {
1118     assert(has_return(), "no return!");
1119     intptr_t current = _ret.type();
1120     _ret.set_type(TypeEntries::with_status(k, current));
1121   }
1122 
1123   // An entry for a return value takes less space than an entry for an
1124   // argument so if the number of cells exceeds the number of cells
1125   // needed for an argument, this object contains type information for
1126   // at least one argument.
1127   bool has_arguments() const {
1128     bool res = cell_count_no_header() >= TypeStackSlotEntries::per_arg_count();
1129     assert (!res || TypeEntriesAtCall::arguments_profiling_enabled(), "no profiling of arguments");
1130     return res;
1131   }
1132 
1133   // An entry for a return value takes less space than an entry for an
1134   // argument, so if the remainder of the number of cells divided by
1135   // the number of cells for an argument is not null, a return value
1136   // is profiled in this object.
1137   bool has_return() const {
1138     bool res = (cell_count_no_header() % TypeStackSlotEntries::per_arg_count()) != 0;
1139     assert (!res || TypeEntriesAtCall::return_profiling_enabled(), "no profiling of return values");
1140     return res;
1141   }
1142 
1143   // Code generation support
1144   static ByteSize args_data_offset() {
1145     return cell_offset(CounterData::static_cell_count()) + TypeEntriesAtCall::args_data_offset();
1146   }
1147 
1148   ByteSize argument_type_offset(int i) {
1149     return _args.type_offset(i);
1150   }
1151 
1152   ByteSize return_type_offset() {
1153     return _ret.type_offset();
1154   }
1155 
1156   // GC support
1157   virtual void clean_weak_klass_links(bool always_clean) {
1158     if (has_arguments()) {
1159       _args.clean_weak_klass_links(always_clean);
1160     }
1161     if (has_return()) {
1162       _ret.clean_weak_klass_links(always_clean);
1163     }
1164   }
1165 
1166   virtual void print_data_on(outputStream* st, const char* extra = NULL) const;
1167 };
1168 
1169 // ReceiverTypeData
1170 //
1171 // A ReceiverTypeData is used to access profiling information about a
1172 // dynamic type check.  It consists of a counter which counts the total times
1173 // that the check is reached, and a series of (Klass*, count) pairs
1174 // which are used to store a type profile for the receiver of the check.
1175 class ReceiverTypeData : public CounterData {
1176   friend class VMStructs;
1177   friend class JVMCIVMStructs;
1178 protected:
1179   enum {
1180 #if INCLUDE_JVMCI
1181     // Description of the different counters
1182     // ReceiverTypeData for instanceof/checkcast/aastore:
1183     //   count is decremented for failed type checks
1184     //   JVMCI only: nonprofiled_count is incremented on type overflow
1185     // VirtualCallData for invokevirtual/invokeinterface:
1186     //   count is incremented on type overflow
1187     //   JVMCI only: nonprofiled_count is incremented on method overflow
1188 
1189     // JVMCI is interested in knowing the percentage of type checks involving a type not explicitly in the profile
1190     nonprofiled_count_off_set = counter_cell_count,
1191     receiver0_offset,
1192 #else
1193     receiver0_offset = counter_cell_count,
1194 #endif
1195     count0_offset,
1196     receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
1197   };
1198 
1199 public:
1200   ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
1201     assert(layout->tag() == DataLayout::receiver_type_data_tag ||
1202            layout->tag() == DataLayout::virtual_call_data_tag ||
1203            layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1204   }
1205 
1206   virtual bool is_ReceiverTypeData() const { return true; }
1207 
1208   static int static_cell_count() {
1209     return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count JVMCI_ONLY(+ 1);
1210   }
1211 
1212   virtual int cell_count() const {
1213     return static_cell_count();
1214   }
1215 
1216   // Direct accessors
1217   static uint row_limit() {
1218     return TypeProfileWidth;
1219   }
1220   static int receiver_cell_index(uint row) {
1221     return receiver0_offset + row * receiver_type_row_cell_count;
1222   }
1223   static int receiver_count_cell_index(uint row) {
1224     return count0_offset + row * receiver_type_row_cell_count;
1225   }
1226 
1227   Klass* receiver(uint row) const {
1228     assert(row < row_limit(), "oob");
1229 
1230     Klass* recv = (Klass*)intptr_at(receiver_cell_index(row));
1231     assert(recv == NULL || recv->is_klass(), "wrong type");
1232     return recv;
1233   }
1234 
1235   void set_receiver(uint row, Klass* k) {
1236     assert((uint)row < row_limit(), "oob");
1237     set_intptr_at(receiver_cell_index(row), (uintptr_t)k);
1238   }
1239 
1240   uint receiver_count(uint row) const {
1241     assert(row < row_limit(), "oob");
1242     return uint_at(receiver_count_cell_index(row));
1243   }
1244 
1245   void set_receiver_count(uint row, uint count) {
1246     assert(row < row_limit(), "oob");
1247     set_uint_at(receiver_count_cell_index(row), count);
1248   }
1249 
1250   void clear_row(uint row) {
1251     assert(row < row_limit(), "oob");
1252     // Clear total count - indicator of polymorphic call site.
1253     // The site may look like as monomorphic after that but
1254     // it allow to have more accurate profiling information because
1255     // there was execution phase change since klasses were unloaded.
1256     // If the site is still polymorphic then MDO will be updated
1257     // to reflect it. But it could be the case that the site becomes
1258     // only bimorphic. Then keeping total count not 0 will be wrong.
1259     // Even if we use monomorphic (when it is not) for compilation
1260     // we will only have trap, deoptimization and recompile again
1261     // with updated MDO after executing method in Interpreter.
1262     // An additional receiver will be recorded in the cleaned row
1263     // during next call execution.
1264     //
1265     // Note: our profiling logic works with empty rows in any slot.
1266     // We do sorting a profiling info (ciCallProfile) for compilation.
1267     //
1268     set_count(0);
1269     set_receiver(row, NULL);
1270     set_receiver_count(row, 0);
1271 #if INCLUDE_JVMCI
1272     if (!this->is_VirtualCallData()) {
1273       // if this is a ReceiverTypeData for JVMCI, the nonprofiled_count
1274       // must also be reset (see "Description of the different counters" above)
1275       set_nonprofiled_count(0);
1276     }
1277 #endif
1278   }
1279 
1280   // Code generation support
1281   static ByteSize receiver_offset(uint row) {
1282     return cell_offset(receiver_cell_index(row));
1283   }
1284   static ByteSize receiver_count_offset(uint row) {
1285     return cell_offset(receiver_count_cell_index(row));
1286   }
1287 #if INCLUDE_JVMCI
1288   static ByteSize nonprofiled_receiver_count_offset() {
1289     return cell_offset(nonprofiled_count_off_set);
1290   }
1291   uint nonprofiled_count() const {
1292     return uint_at(nonprofiled_count_off_set);
1293   }
1294   void set_nonprofiled_count(uint count) {
1295     set_uint_at(nonprofiled_count_off_set, count);
1296   }
1297 #endif // INCLUDE_JVMCI
1298   static ByteSize receiver_type_data_size() {
1299     return cell_offset(static_cell_count());
1300   }
1301 
1302   // GC support
1303   virtual void clean_weak_klass_links(bool always_clean);
1304 
1305 #ifdef CC_INTERP
1306   static int receiver_type_data_size_in_bytes() {
1307     return cell_offset_in_bytes(static_cell_count());
1308   }
1309 
1310   static Klass *receiver_unchecked(DataLayout* layout, uint row) {
1311     Klass* recv = (Klass*)layout->cell_at(receiver_cell_index(row));
1312     return recv;
1313   }
1314 
1315   static void increment_receiver_count_no_overflow(DataLayout* layout, Klass *rcvr) {
1316     const int num_rows = row_limit();
1317     // Receiver already exists?
1318     for (int row = 0; row < num_rows; row++) {
1319       if (receiver_unchecked(layout, row) == rcvr) {
1320         increment_uint_at_no_overflow(layout, receiver_count_cell_index(row));
1321         return;
1322       }
1323     }
1324     // New receiver, find a free slot.
1325     for (int row = 0; row < num_rows; row++) {
1326       if (receiver_unchecked(layout, row) == NULL) {
1327         set_intptr_at(layout, receiver_cell_index(row), (intptr_t)rcvr);
1328         increment_uint_at_no_overflow(layout, receiver_count_cell_index(row));
1329         return;
1330       }
1331     }
1332     // Receiver did not match any saved receiver and there is no empty row for it.
1333     // Increment total counter to indicate polymorphic case.
1334     increment_count_no_overflow(layout);
1335   }
1336 
1337   static DataLayout* advance(DataLayout* layout) {
1338     return (DataLayout*) (((address)layout) + (ssize_t)ReceiverTypeData::receiver_type_data_size_in_bytes());
1339   }
1340 #endif // CC_INTERP
1341 
1342   void print_receiver_data_on(outputStream* st) const;
1343   void print_data_on(outputStream* st, const char* extra = NULL) const;
1344 };
1345 
1346 // VirtualCallData
1347 //
1348 // A VirtualCallData is used to access profiling information about a
1349 // virtual call.  For now, it has nothing more than a ReceiverTypeData.
1350 class VirtualCallData : public ReceiverTypeData {
1351 public:
1352   VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
1353     assert(layout->tag() == DataLayout::virtual_call_data_tag ||
1354            layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1355   }
1356 
1357   virtual bool is_VirtualCallData() const { return true; }
1358 
1359   static int static_cell_count() {
1360     // At this point we could add more profile state, e.g., for arguments.
1361     // But for now it's the same size as the base record type.
1362     return ReceiverTypeData::static_cell_count() JVMCI_ONLY(+ (uint) MethodProfileWidth * receiver_type_row_cell_count);
1363   }
1364 
1365   virtual int cell_count() const {
1366     return static_cell_count();
1367   }
1368 
1369   // Direct accessors
1370   static ByteSize virtual_call_data_size() {
1371     return cell_offset(static_cell_count());
1372   }
1373 
1374 #ifdef CC_INTERP
1375   static int virtual_call_data_size_in_bytes() {
1376     return cell_offset_in_bytes(static_cell_count());
1377   }
1378 
1379   static DataLayout* advance(DataLayout* layout) {
1380     return (DataLayout*) (((address)layout) + (ssize_t)VirtualCallData::virtual_call_data_size_in_bytes());
1381   }
1382 #endif // CC_INTERP
1383 
1384 #if INCLUDE_JVMCI
1385   static ByteSize method_offset(uint row) {
1386     return cell_offset(method_cell_index(row));
1387   }
1388   static ByteSize method_count_offset(uint row) {
1389     return cell_offset(method_count_cell_index(row));
1390   }
1391   static int method_cell_index(uint row) {
1392     return receiver0_offset + (row + TypeProfileWidth) * receiver_type_row_cell_count;
1393   }
1394   static int method_count_cell_index(uint row) {
1395     return count0_offset + (row + TypeProfileWidth) * receiver_type_row_cell_count;
1396   }
1397   static uint method_row_limit() {
1398     return MethodProfileWidth;
1399   }
1400 
1401   Method* method(uint row) const {
1402     assert(row < method_row_limit(), "oob");
1403 
1404     Method* method = (Method*)intptr_at(method_cell_index(row));
1405     assert(method == NULL || method->is_method(), "must be");
1406     return method;
1407   }
1408 
1409   uint method_count(uint row) const {
1410     assert(row < method_row_limit(), "oob");
1411     return uint_at(method_count_cell_index(row));
1412   }
1413 
1414   void set_method(uint row, Method* m) {
1415     assert((uint)row < method_row_limit(), "oob");
1416     set_intptr_at(method_cell_index(row), (uintptr_t)m);
1417   }
1418 
1419   void set_method_count(uint row, uint count) {
1420     assert(row < method_row_limit(), "oob");
1421     set_uint_at(method_count_cell_index(row), count);
1422   }
1423 
1424   void clear_method_row(uint row) {
1425     assert(row < method_row_limit(), "oob");
1426     // Clear total count - indicator of polymorphic call site (see comment for clear_row() in ReceiverTypeData).
1427     set_nonprofiled_count(0);
1428     set_method(row, NULL);
1429     set_method_count(row, 0);
1430   }
1431 
1432   // GC support
1433   virtual void clean_weak_klass_links(bool always_clean);
1434 
1435   // Redefinition support
1436   virtual void clean_weak_method_links();
1437 #endif // INCLUDE_JVMCI
1438 
1439   void print_method_data_on(outputStream* st) const NOT_JVMCI_RETURN;
1440   void print_data_on(outputStream* st, const char* extra = NULL) const;
1441 };
1442 
1443 // VirtualCallTypeData
1444 //
1445 // A VirtualCallTypeData is used to access profiling information about
1446 // a virtual call for which we collect type information about
1447 // arguments and return value.
1448 class VirtualCallTypeData : public VirtualCallData {
1449 private:
1450   // entries for arguments if any
1451   TypeStackSlotEntries _args;
1452   // entry for return type if any
1453   ReturnTypeEntry _ret;
1454 
1455   int cell_count_global_offset() const {
1456     return VirtualCallData::static_cell_count() + TypeEntriesAtCall::cell_count_local_offset();
1457   }
1458 
1459   // number of cells not counting the header
1460   int cell_count_no_header() const {
1461     return uint_at(cell_count_global_offset());
1462   }
1463 
1464   void check_number_of_arguments(int total) {
1465     assert(number_of_arguments() == total, "should be set in DataLayout::initialize");
1466   }
1467 
1468 public:
1469   VirtualCallTypeData(DataLayout* layout) :
1470     VirtualCallData(layout),
1471     _args(VirtualCallData::static_cell_count()+TypeEntriesAtCall::header_cell_count(), number_of_arguments()),
1472     _ret(cell_count() - ReturnTypeEntry::static_cell_count())
1473   {
1474     assert(layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1475     // Some compilers (VC++) don't want this passed in member initialization list
1476     _args.set_profile_data(this);
1477     _ret.set_profile_data(this);
1478   }
1479 
1480   const TypeStackSlotEntries* args() const {
1481     assert(has_arguments(), "no profiling of arguments");
1482     return &_args;
1483   }
1484 
1485   const ReturnTypeEntry* ret() const {
1486     assert(has_return(), "no profiling of return value");
1487     return &_ret;
1488   }
1489 
1490   virtual bool is_VirtualCallTypeData() const { return true; }
1491 
1492   static int static_cell_count() {
1493     return -1;
1494   }
1495 
1496   static int compute_cell_count(BytecodeStream* stream) {
1497     return VirtualCallData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream);
1498   }
1499 
1500   static void initialize(DataLayout* dl, int cell_count) {
1501     TypeEntriesAtCall::initialize(dl, VirtualCallData::static_cell_count(), cell_count);
1502   }
1503 
1504   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
1505 
1506   virtual int cell_count() const {
1507     return VirtualCallData::static_cell_count() +
1508       TypeEntriesAtCall::header_cell_count() +
1509       int_at_unchecked(cell_count_global_offset());
1510   }
1511 
1512   int number_of_arguments() const {
1513     return cell_count_no_header() / TypeStackSlotEntries::per_arg_count();
1514   }
1515 
1516   void set_argument_type(int i, Klass* k) {
1517     assert(has_arguments(), "no arguments!");
1518     intptr_t current = _args.type(i);
1519     _args.set_type(i, TypeEntries::with_status(k, current));
1520   }
1521 
1522   void set_return_type(Klass* k) {
1523     assert(has_return(), "no return!");
1524     intptr_t current = _ret.type();
1525     _ret.set_type(TypeEntries::with_status(k, current));
1526   }
1527 
1528   // An entry for a return value takes less space than an entry for an
1529   // argument, so if the remainder of the number of cells divided by
1530   // the number of cells for an argument is not null, a return value
1531   // is profiled in this object.
1532   bool has_return() const {
1533     bool res = (cell_count_no_header() % TypeStackSlotEntries::per_arg_count()) != 0;
1534     assert (!res || TypeEntriesAtCall::return_profiling_enabled(), "no profiling of return values");
1535     return res;
1536   }
1537 
1538   // An entry for a return value takes less space than an entry for an
1539   // argument so if the number of cells exceeds the number of cells
1540   // needed for an argument, this object contains type information for
1541   // at least one argument.
1542   bool has_arguments() const {
1543     bool res = cell_count_no_header() >= TypeStackSlotEntries::per_arg_count();
1544     assert (!res || TypeEntriesAtCall::arguments_profiling_enabled(), "no profiling of arguments");
1545     return res;
1546   }
1547 
1548   // Code generation support
1549   static ByteSize args_data_offset() {
1550     return cell_offset(VirtualCallData::static_cell_count()) + TypeEntriesAtCall::args_data_offset();
1551   }
1552 
1553   ByteSize argument_type_offset(int i) {
1554     return _args.type_offset(i);
1555   }
1556 
1557   ByteSize return_type_offset() {
1558     return _ret.type_offset();
1559   }
1560 
1561   // GC support
1562   virtual void clean_weak_klass_links(bool always_clean) {
1563     ReceiverTypeData::clean_weak_klass_links(always_clean);
1564     if (has_arguments()) {
1565       _args.clean_weak_klass_links(always_clean);
1566     }
1567     if (has_return()) {
1568       _ret.clean_weak_klass_links(always_clean);
1569     }
1570   }
1571 
1572   virtual void print_data_on(outputStream* st, const char* extra = NULL) const;
1573 };
1574 
1575 // RetData
1576 //
1577 // A RetData is used to access profiling information for a ret bytecode.
1578 // It is composed of a count of the number of times that the ret has
1579 // been executed, followed by a series of triples of the form
1580 // (bci, count, di) which count the number of times that some bci was the
1581 // target of the ret and cache a corresponding data displacement.
1582 class RetData : public CounterData {
1583 protected:
1584   enum {
1585     bci0_offset = counter_cell_count,
1586     count0_offset,
1587     displacement0_offset,
1588     ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
1589   };
1590 
1591   void set_bci(uint row, int bci) {
1592     assert((uint)row < row_limit(), "oob");
1593     set_int_at(bci0_offset + row * ret_row_cell_count, bci);
1594   }
1595   void release_set_bci(uint row, int bci);
1596   void set_bci_count(uint row, uint count) {
1597     assert((uint)row < row_limit(), "oob");
1598     set_uint_at(count0_offset + row * ret_row_cell_count, count);
1599   }
1600   void set_bci_displacement(uint row, int disp) {
1601     set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
1602   }
1603 
1604 public:
1605   RetData(DataLayout* layout) : CounterData(layout) {
1606     assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
1607   }
1608 
1609   virtual bool is_RetData() const { return true; }
1610 
1611   enum {
1612     no_bci = -1 // value of bci when bci1/2 are not in use.
1613   };
1614 
1615   static int static_cell_count() {
1616     return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
1617   }
1618 
1619   virtual int cell_count() const {
1620     return static_cell_count();
1621   }
1622 
1623   static uint row_limit() {
1624     return BciProfileWidth;
1625   }
1626   static int bci_cell_index(uint row) {
1627     return bci0_offset + row * ret_row_cell_count;
1628   }
1629   static int bci_count_cell_index(uint row) {
1630     return count0_offset + row * ret_row_cell_count;
1631   }
1632   static int bci_displacement_cell_index(uint row) {
1633     return displacement0_offset + row * ret_row_cell_count;
1634   }
1635 
1636   // Direct accessors
1637   int bci(uint row) const {
1638     return int_at(bci_cell_index(row));
1639   }
1640   uint bci_count(uint row) const {
1641     return uint_at(bci_count_cell_index(row));
1642   }
1643   int bci_displacement(uint row) const {
1644     return int_at(bci_displacement_cell_index(row));
1645   }
1646 
1647   // Interpreter Runtime support
1648   address fixup_ret(int return_bci, MethodData* mdo);
1649 
1650   // Code generation support
1651   static ByteSize bci_offset(uint row) {
1652     return cell_offset(bci_cell_index(row));
1653   }
1654   static ByteSize bci_count_offset(uint row) {
1655     return cell_offset(bci_count_cell_index(row));
1656   }
1657   static ByteSize bci_displacement_offset(uint row) {
1658     return cell_offset(bci_displacement_cell_index(row));
1659   }
1660 
1661 #ifdef CC_INTERP
1662   static DataLayout* advance(MethodData *md, int bci);
1663 #endif // CC_INTERP
1664 
1665   // Specific initialization.
1666   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1667 
1668   void print_data_on(outputStream* st, const char* extra = NULL) const;
1669 };
1670 
1671 // BranchData
1672 //
1673 // A BranchData is used to access profiling data for a two-way branch.
1674 // It consists of taken and not_taken counts as well as a data displacement
1675 // for the taken case.
1676 class BranchData : public JumpData {
1677   friend class VMStructs;
1678   friend class JVMCIVMStructs;
1679 protected:
1680   enum {
1681     not_taken_off_set = jump_cell_count,
1682     branch_cell_count
1683   };
1684 
1685   void set_displacement(int displacement) {
1686     set_int_at(displacement_off_set, displacement);
1687   }
1688 
1689 public:
1690   BranchData(DataLayout* layout) : JumpData(layout) {
1691     assert(layout->tag() == DataLayout::branch_data_tag, "wrong type");
1692   }
1693 
1694   virtual bool is_BranchData() const { return true; }
1695 
1696   static int static_cell_count() {
1697     return branch_cell_count;
1698   }
1699 
1700   virtual int cell_count() const {
1701     return static_cell_count();
1702   }
1703 
1704   // Direct accessor
1705   uint not_taken() const {
1706     return uint_at(not_taken_off_set);
1707   }
1708 
1709   void set_not_taken(uint cnt) {
1710     set_uint_at(not_taken_off_set, cnt);
1711   }
1712 
1713   uint inc_not_taken() {
1714     uint cnt = not_taken() + 1;
1715     // Did we wrap? Will compiler screw us??
1716     if (cnt == 0) cnt--;
1717     set_uint_at(not_taken_off_set, cnt);
1718     return cnt;
1719   }
1720 
1721   // Code generation support
1722   static ByteSize not_taken_offset() {
1723     return cell_offset(not_taken_off_set);
1724   }
1725   static ByteSize branch_data_size() {
1726     return cell_offset(branch_cell_count);
1727   }
1728 
1729 #ifdef CC_INTERP
1730   static int branch_data_size_in_bytes() {
1731     return cell_offset_in_bytes(branch_cell_count);
1732   }
1733 
1734   static void increment_not_taken_count_no_overflow(DataLayout* layout) {
1735     increment_uint_at_no_overflow(layout, not_taken_off_set);
1736   }
1737 
1738   static DataLayout* advance_not_taken(DataLayout* layout) {
1739     return (DataLayout*) (((address)layout) + (ssize_t)BranchData::branch_data_size_in_bytes());
1740   }
1741 #endif // CC_INTERP
1742 
1743   // Specific initialization.
1744   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1745 
1746   void print_data_on(outputStream* st, const char* extra = NULL) const;
1747 };
1748 
1749 // ArrayData
1750 //
1751 // A ArrayData is a base class for accessing profiling data which does
1752 // not have a statically known size.  It consists of an array length
1753 // and an array start.
1754 class ArrayData : public ProfileData {
1755   friend class VMStructs;
1756   friend class JVMCIVMStructs;
1757 protected:
1758   friend class DataLayout;
1759 
1760   enum {
1761     array_len_off_set,
1762     array_start_off_set
1763   };
1764 
1765   uint array_uint_at(int index) const {
1766     int aindex = index + array_start_off_set;
1767     return uint_at(aindex);
1768   }
1769   int array_int_at(int index) const {
1770     int aindex = index + array_start_off_set;
1771     return int_at(aindex);
1772   }
1773   oop array_oop_at(int index) const {
1774     int aindex = index + array_start_off_set;
1775     return oop_at(aindex);
1776   }
1777   void array_set_int_at(int index, int value) {
1778     int aindex = index + array_start_off_set;
1779     set_int_at(aindex, value);
1780   }
1781 
1782 #ifdef CC_INTERP
1783   // Static low level accessors for DataLayout with ArrayData's semantics.
1784 
1785   static void increment_array_uint_at_no_overflow(DataLayout* layout, int index) {
1786     int aindex = index + array_start_off_set;
1787     increment_uint_at_no_overflow(layout, aindex);
1788   }
1789 
1790   static int array_int_at(DataLayout* layout, int index) {
1791     int aindex = index + array_start_off_set;
1792     return int_at(layout, aindex);
1793   }
1794 #endif // CC_INTERP
1795 
1796   // Code generation support for subclasses.
1797   static ByteSize array_element_offset(int index) {
1798     return cell_offset(array_start_off_set + index);
1799   }
1800 
1801 public:
1802   ArrayData(DataLayout* layout) : ProfileData(layout) {}
1803 
1804   virtual bool is_ArrayData() const { return true; }
1805 
1806   static int static_cell_count() {
1807     return -1;
1808   }
1809 
1810   int array_len() const {
1811     return int_at_unchecked(array_len_off_set);
1812   }
1813 
1814   virtual int cell_count() const {
1815     return array_len() + 1;
1816   }
1817 
1818   // Code generation support
1819   static ByteSize array_len_offset() {
1820     return cell_offset(array_len_off_set);
1821   }
1822   static ByteSize array_start_offset() {
1823     return cell_offset(array_start_off_set);
1824   }
1825 };
1826 
1827 // MultiBranchData
1828 //
1829 // A MultiBranchData is used to access profiling information for
1830 // a multi-way branch (*switch bytecodes).  It consists of a series
1831 // of (count, displacement) pairs, which count the number of times each
1832 // case was taken and specify the data displacment for each branch target.
1833 class MultiBranchData : public ArrayData {
1834   friend class VMStructs;
1835   friend class JVMCIVMStructs;
1836 protected:
1837   enum {
1838     default_count_off_set,
1839     default_disaplacement_off_set,
1840     case_array_start
1841   };
1842   enum {
1843     relative_count_off_set,
1844     relative_displacement_off_set,
1845     per_case_cell_count
1846   };
1847 
1848   void set_default_displacement(int displacement) {
1849     array_set_int_at(default_disaplacement_off_set, displacement);
1850   }
1851   void set_displacement_at(int index, int displacement) {
1852     array_set_int_at(case_array_start +
1853                      index * per_case_cell_count +
1854                      relative_displacement_off_set,
1855                      displacement);
1856   }
1857 
1858 public:
1859   MultiBranchData(DataLayout* layout) : ArrayData(layout) {
1860     assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
1861   }
1862 
1863   virtual bool is_MultiBranchData() const { return true; }
1864 
1865   static int compute_cell_count(BytecodeStream* stream);
1866 
1867   int number_of_cases() const {
1868     int alen = array_len() - 2; // get rid of default case here.
1869     assert(alen % per_case_cell_count == 0, "must be even");
1870     return (alen / per_case_cell_count);
1871   }
1872 
1873   uint default_count() const {
1874     return array_uint_at(default_count_off_set);
1875   }
1876   int default_displacement() const {
1877     return array_int_at(default_disaplacement_off_set);
1878   }
1879 
1880   uint count_at(int index) const {
1881     return array_uint_at(case_array_start +
1882                          index * per_case_cell_count +
1883                          relative_count_off_set);
1884   }
1885   int displacement_at(int index) const {
1886     return array_int_at(case_array_start +
1887                         index * per_case_cell_count +
1888                         relative_displacement_off_set);
1889   }
1890 
1891   // Code generation support
1892   static ByteSize default_count_offset() {
1893     return array_element_offset(default_count_off_set);
1894   }
1895   static ByteSize default_displacement_offset() {
1896     return array_element_offset(default_disaplacement_off_set);
1897   }
1898   static ByteSize case_count_offset(int index) {
1899     return case_array_offset() +
1900            (per_case_size() * index) +
1901            relative_count_offset();
1902   }
1903   static ByteSize case_array_offset() {
1904     return array_element_offset(case_array_start);
1905   }
1906   static ByteSize per_case_size() {
1907     return in_ByteSize(per_case_cell_count) * cell_size;
1908   }
1909   static ByteSize relative_count_offset() {
1910     return in_ByteSize(relative_count_off_set) * cell_size;
1911   }
1912   static ByteSize relative_displacement_offset() {
1913     return in_ByteSize(relative_displacement_off_set) * cell_size;
1914   }
1915 
1916 #ifdef CC_INTERP
1917   static void increment_count_no_overflow(DataLayout* layout, int index) {
1918     if (index == -1) {
1919       increment_array_uint_at_no_overflow(layout, default_count_off_set);
1920     } else {
1921       increment_array_uint_at_no_overflow(layout, case_array_start +
1922                                                   index * per_case_cell_count +
1923                                                   relative_count_off_set);
1924     }
1925   }
1926 
1927   static DataLayout* advance(DataLayout* layout, int index) {
1928     if (index == -1) {
1929       return (DataLayout*) (((address)layout) + (ssize_t)array_int_at(layout, default_disaplacement_off_set));
1930     } else {
1931       return (DataLayout*) (((address)layout) + (ssize_t)array_int_at(layout, case_array_start +
1932                                                                               index * per_case_cell_count +
1933                                                                               relative_displacement_off_set));
1934     }
1935   }
1936 #endif // CC_INTERP
1937 
1938   // Specific initialization.
1939   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1940 
1941   void print_data_on(outputStream* st, const char* extra = NULL) const;
1942 };
1943 
1944 class ArgInfoData : public ArrayData {
1945 
1946 public:
1947   ArgInfoData(DataLayout* layout) : ArrayData(layout) {
1948     assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type");
1949   }
1950 
1951   virtual bool is_ArgInfoData() const { return true; }
1952 
1953 
1954   int number_of_args() const {
1955     return array_len();
1956   }
1957 
1958   uint arg_modified(int arg) const {
1959     return array_uint_at(arg);
1960   }
1961 
1962   void set_arg_modified(int arg, uint val) {
1963     array_set_int_at(arg, val);
1964   }
1965 
1966   void print_data_on(outputStream* st, const char* extra = NULL) const;
1967 };
1968 
1969 // ParametersTypeData
1970 //
1971 // A ParametersTypeData is used to access profiling information about
1972 // types of parameters to a method
1973 class ParametersTypeData : public ArrayData {
1974 
1975 private:
1976   TypeStackSlotEntries _parameters;
1977 
1978   static int stack_slot_local_offset(int i) {
1979     assert_profiling_enabled();
1980     return array_start_off_set + TypeStackSlotEntries::stack_slot_local_offset(i);
1981   }
1982 
1983   static int type_local_offset(int i) {
1984     assert_profiling_enabled();
1985     return array_start_off_set + TypeStackSlotEntries::type_local_offset(i);
1986   }
1987 
1988   static bool profiling_enabled();
1989   static void assert_profiling_enabled() {
1990     assert(profiling_enabled(), "method parameters profiling should be on");
1991   }
1992 
1993 public:
1994   ParametersTypeData(DataLayout* layout) : ArrayData(layout), _parameters(1, number_of_parameters()) {
1995     assert(layout->tag() == DataLayout::parameters_type_data_tag, "wrong type");
1996     // Some compilers (VC++) don't want this passed in member initialization list
1997     _parameters.set_profile_data(this);
1998   }
1999 
2000   static int compute_cell_count(Method* m);
2001 
2002   virtual bool is_ParametersTypeData() const { return true; }
2003 
2004   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
2005 
2006   int number_of_parameters() const {
2007     return array_len() / TypeStackSlotEntries::per_arg_count();
2008   }
2009 
2010   const TypeStackSlotEntries* parameters() const { return &_parameters; }
2011 
2012   uint stack_slot(int i) const {
2013     return _parameters.stack_slot(i);
2014   }
2015 
2016   void set_type(int i, Klass* k) {
2017     intptr_t current = _parameters.type(i);
2018     _parameters.set_type(i, TypeEntries::with_status((intptr_t)k, current));
2019   }
2020 
2021   virtual void clean_weak_klass_links(bool always_clean) {
2022     _parameters.clean_weak_klass_links(always_clean);
2023   }
2024 
2025   virtual void print_data_on(outputStream* st, const char* extra = NULL) const;
2026 
2027   static ByteSize stack_slot_offset(int i) {
2028     return cell_offset(stack_slot_local_offset(i));
2029   }
2030 
2031   static ByteSize type_offset(int i) {
2032     return cell_offset(type_local_offset(i));
2033   }
2034 };
2035 
2036 // SpeculativeTrapData
2037 //
2038 // A SpeculativeTrapData is used to record traps due to type
2039 // speculation. It records the root of the compilation: that type
2040 // speculation is wrong in the context of one compilation (for
2041 // method1) doesn't mean it's wrong in the context of another one (for
2042 // method2). Type speculation could have more/different data in the
2043 // context of the compilation of method2 and it's worthwhile to try an
2044 // optimization that failed for compilation of method1 in the context
2045 // of compilation of method2.
2046 // Space for SpeculativeTrapData entries is allocated from the extra
2047 // data space in the MDO. If we run out of space, the trap data for
2048 // the ProfileData at that bci is updated.
2049 class SpeculativeTrapData : public ProfileData {
2050 protected:
2051   enum {
2052     speculative_trap_method,
2053     speculative_trap_cell_count
2054   };
2055 public:
2056   SpeculativeTrapData(DataLayout* layout) : ProfileData(layout) {
2057     assert(layout->tag() == DataLayout::speculative_trap_data_tag, "wrong type");
2058   }
2059 
2060   virtual bool is_SpeculativeTrapData() const { return true; }
2061 
2062   static int static_cell_count() {
2063     return speculative_trap_cell_count;
2064   }
2065 
2066   virtual int cell_count() const {
2067     return static_cell_count();
2068   }
2069 
2070   // Direct accessor
2071   Method* method() const {
2072     return (Method*)intptr_at(speculative_trap_method);
2073   }
2074 
2075   void set_method(Method* m) {
2076     assert(!m->is_old(), "cannot add old methods");
2077     set_intptr_at(speculative_trap_method, (intptr_t)m);
2078   }
2079 
2080   static ByteSize method_offset() {
2081     return cell_offset(speculative_trap_method);
2082   }
2083 
2084   virtual void print_data_on(outputStream* st, const char* extra = NULL) const;
2085 };
2086 
2087 // MethodData*
2088 //
2089 // A MethodData* holds information which has been collected about
2090 // a method.  Its layout looks like this:
2091 //
2092 // -----------------------------
2093 // | header                    |
2094 // | klass                     |
2095 // -----------------------------
2096 // | method                    |
2097 // | size of the MethodData* |
2098 // -----------------------------
2099 // | Data entries...           |
2100 // |   (variable size)         |
2101 // |                           |
2102 // .                           .
2103 // .                           .
2104 // .                           .
2105 // |                           |
2106 // -----------------------------
2107 //
2108 // The data entry area is a heterogeneous array of DataLayouts. Each
2109 // DataLayout in the array corresponds to a specific bytecode in the
2110 // method.  The entries in the array are sorted by the corresponding
2111 // bytecode.  Access to the data is via resource-allocated ProfileData,
2112 // which point to the underlying blocks of DataLayout structures.
2113 //
2114 // During interpretation, if profiling in enabled, the interpreter
2115 // maintains a method data pointer (mdp), which points at the entry
2116 // in the array corresponding to the current bci.  In the course of
2117 // intepretation, when a bytecode is encountered that has profile data
2118 // associated with it, the entry pointed to by mdp is updated, then the
2119 // mdp is adjusted to point to the next appropriate DataLayout.  If mdp
2120 // is NULL to begin with, the interpreter assumes that the current method
2121 // is not (yet) being profiled.
2122 //
2123 // In MethodData* parlance, "dp" is a "data pointer", the actual address
2124 // of a DataLayout element.  A "di" is a "data index", the offset in bytes
2125 // from the base of the data entry array.  A "displacement" is the byte offset
2126 // in certain ProfileData objects that indicate the amount the mdp must be
2127 // adjusted in the event of a change in control flow.
2128 //
2129 
2130 CC_INTERP_ONLY(class BytecodeInterpreter;)
2131 class CleanExtraDataClosure;
2132 
2133 class MethodData : public Metadata {
2134   friend class VMStructs;
2135   friend class JVMCIVMStructs;
2136   CC_INTERP_ONLY(friend class BytecodeInterpreter;)
2137 private:
2138   friend class ProfileData;
2139   friend class TypeEntriesAtCall;
2140 
2141   // If you add a new field that points to any metaspace object, you
2142   // must add this field to MethodData::metaspace_pointers_do().
2143 
2144   // Back pointer to the Method*
2145   Method* _method;
2146 
2147   // Size of this oop in bytes
2148   int _size;
2149 
2150   // Cached hint for bci_to_dp and bci_to_data
2151   int _hint_di;
2152 
2153   Mutex _extra_data_lock;
2154 
2155   MethodData(const methodHandle& method, int size, TRAPS);
2156 public:
2157   static MethodData* allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS);
2158   MethodData() : _extra_data_lock(Monitor::leaf, "MDO extra data lock") {}; // For ciMethodData
2159 
2160   bool is_methodData() const volatile { return true; }
2161   void initialize();
2162 
2163   // Whole-method sticky bits and flags
2164   enum {
2165     _trap_hist_limit    = 23 JVMCI_ONLY(+5),   // decoupled from Deoptimization::Reason_LIMIT
2166     _trap_hist_mask     = max_jubyte,
2167     _extra_data_count   = 4     // extra DataLayout headers, for trap history
2168   }; // Public flag values
2169 private:
2170   uint _nof_decompiles;             // count of all nmethod removals
2171   uint _nof_overflow_recompiles;    // recompile count, excluding recomp. bits
2172   uint _nof_overflow_traps;         // trap count, excluding _trap_hist
2173   union {
2174     intptr_t _align;
2175     u1 _array[JVMCI_ONLY(2 *) _trap_hist_limit];
2176   } _trap_hist;
2177 
2178   // Support for interprocedural escape analysis, from Thomas Kotzmann.
2179   intx              _eflags;          // flags on escape information
2180   intx              _arg_local;       // bit set of non-escaping arguments
2181   intx              _arg_stack;       // bit set of stack-allocatable arguments
2182   intx              _arg_returned;    // bit set of returned arguments
2183 
2184   int _creation_mileage;              // method mileage at MDO creation
2185 
2186   // How many invocations has this MDO seen?
2187   // These counters are used to determine the exact age of MDO.
2188   // We need those because in tiered a method can be concurrently
2189   // executed at different levels.
2190   InvocationCounter _invocation_counter;
2191   // Same for backedges.
2192   InvocationCounter _backedge_counter;
2193   // Counter values at the time profiling started.
2194   int               _invocation_counter_start;
2195   int               _backedge_counter_start;
2196   uint              _tenure_traps;
2197   int               _invoke_mask;      // per-method Tier0InvokeNotifyFreqLog
2198   int               _backedge_mask;    // per-method Tier0BackedgeNotifyFreqLog
2199 
2200 #if INCLUDE_RTM_OPT
2201   // State of RTM code generation during compilation of the method
2202   int               _rtm_state;
2203 #endif
2204 
2205   // Number of loops and blocks is computed when compiling the first
2206   // time with C1. It is used to determine if method is trivial.
2207   short             _num_loops;
2208   short             _num_blocks;
2209   // Does this method contain anything worth profiling?
2210   enum WouldProfile {unknown, no_profile, profile};
2211   WouldProfile      _would_profile;
2212 
2213 #if INCLUDE_JVMCI
2214   // Support for HotSpotMethodData.setCompiledIRSize(int)
2215   int               _jvmci_ir_size;
2216 #endif
2217 
2218   // Size of _data array in bytes.  (Excludes header and extra_data fields.)
2219   int _data_size;
2220 
2221   // data index for the area dedicated to parameters. -1 if no
2222   // parameter profiling.
2223   enum { no_parameters = -2, parameters_uninitialized = -1 };
2224   int _parameters_type_data_di;
2225   int parameters_size_in_bytes() const {
2226     ParametersTypeData* param = parameters_type_data();
2227     return param == NULL ? 0 : param->size_in_bytes();
2228   }
2229 
2230   // Beginning of the data entries
2231   intptr_t _data[1];
2232 
2233   // Helper for size computation
2234   static int compute_data_size(BytecodeStream* stream);
2235   static int bytecode_cell_count(Bytecodes::Code code);
2236   static bool is_speculative_trap_bytecode(Bytecodes::Code code);
2237   enum { no_profile_data = -1, variable_cell_count = -2 };
2238 
2239   // Helper for initialization
2240   DataLayout* data_layout_at(int data_index) const {
2241     assert(data_index % sizeof(intptr_t) == 0, "unaligned");
2242     return (DataLayout*) (((address)_data) + data_index);
2243   }
2244 
2245   // Initialize an individual data segment.  Returns the size of
2246   // the segment in bytes.
2247   int initialize_data(BytecodeStream* stream, int data_index);
2248 
2249   // Helper for data_at
2250   DataLayout* limit_data_position() const {
2251     return data_layout_at(_data_size);
2252   }
2253   bool out_of_bounds(int data_index) const {
2254     return data_index >= data_size();
2255   }
2256 
2257   // Give each of the data entries a chance to perform specific
2258   // data initialization.
2259   void post_initialize(BytecodeStream* stream);
2260 
2261   // hint accessors
2262   int      hint_di() const  { return _hint_di; }
2263   void set_hint_di(int di)  {
2264     assert(!out_of_bounds(di), "hint_di out of bounds");
2265     _hint_di = di;
2266   }
2267   ProfileData* data_before(int bci) {
2268     // avoid SEGV on this edge case
2269     if (data_size() == 0)
2270       return NULL;
2271     int hint = hint_di();
2272     if (data_layout_at(hint)->bci() <= bci)
2273       return data_at(hint);
2274     return first_data();
2275   }
2276 
2277   // What is the index of the first data entry?
2278   int first_di() const { return 0; }
2279 
2280   ProfileData* bci_to_extra_data_helper(int bci, Method* m, DataLayout*& dp, bool concurrent);
2281   // Find or create an extra ProfileData:
2282   ProfileData* bci_to_extra_data(int bci, Method* m, bool create_if_missing);
2283 
2284   // return the argument info cell
2285   ArgInfoData *arg_info();
2286 
2287   enum {
2288     no_type_profile = 0,
2289     type_profile_jsr292 = 1,
2290     type_profile_all = 2
2291   };
2292 
2293   static bool profile_jsr292(const methodHandle& m, int bci);
2294   static bool profile_unsafe(const methodHandle& m, int bci);
2295   static int profile_arguments_flag();
2296   static bool profile_all_arguments();
2297   static bool profile_arguments_for_invoke(const methodHandle& m, int bci);
2298   static int profile_return_flag();
2299   static bool profile_all_return();
2300   static bool profile_return_for_invoke(const methodHandle& m, int bci);
2301   static int profile_parameters_flag();
2302   static bool profile_parameters_jsr292_only();
2303   static bool profile_all_parameters();
2304 
2305   void clean_extra_data(CleanExtraDataClosure* cl);
2306   void clean_extra_data_helper(DataLayout* dp, int shift, bool reset = false);
2307   void verify_extra_data_clean(CleanExtraDataClosure* cl);
2308 
2309 public:
2310   static int header_size() {
2311     return sizeof(MethodData)/wordSize;
2312   }
2313 
2314   // Compute the size of a MethodData* before it is created.
2315   static int compute_allocation_size_in_bytes(const methodHandle& method);
2316   static int compute_allocation_size_in_words(const methodHandle& method);
2317   static int compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps);
2318 
2319   // Determine if a given bytecode can have profile information.
2320   static bool bytecode_has_profile(Bytecodes::Code code) {
2321     return bytecode_cell_count(code) != no_profile_data;
2322   }
2323 
2324   // reset into original state
2325   void init();
2326 
2327   // My size
2328   int size_in_bytes() const { return _size; }
2329   int size() const    { return align_metadata_size(align_up(_size, BytesPerWord)/BytesPerWord); }
2330 #if INCLUDE_SERVICES
2331   void collect_statistics(KlassSizeStats *sz) const;
2332 #endif
2333 
2334   int      creation_mileage() const  { return _creation_mileage; }
2335   void set_creation_mileage(int x)   { _creation_mileage = x; }
2336 
2337   int invocation_count() {
2338     if (invocation_counter()->carry()) {
2339       return InvocationCounter::count_limit;
2340     }
2341     return invocation_counter()->count();
2342   }
2343   int backedge_count() {
2344     if (backedge_counter()->carry()) {
2345       return InvocationCounter::count_limit;
2346     }
2347     return backedge_counter()->count();
2348   }
2349 
2350   int invocation_count_start() {
2351     if (invocation_counter()->carry()) {
2352       return 0;
2353     }
2354     return _invocation_counter_start;
2355   }
2356 
2357   int backedge_count_start() {
2358     if (backedge_counter()->carry()) {
2359       return 0;
2360     }
2361     return _backedge_counter_start;
2362   }
2363 
2364   int invocation_count_delta() { return invocation_count() - invocation_count_start(); }
2365   int backedge_count_delta()   { return backedge_count()   - backedge_count_start();   }
2366 
2367   void reset_start_counters() {
2368     _invocation_counter_start = invocation_count();
2369     _backedge_counter_start = backedge_count();
2370   }
2371 
2372   InvocationCounter* invocation_counter()     { return &_invocation_counter; }
2373   InvocationCounter* backedge_counter()       { return &_backedge_counter;   }
2374 
2375 #if INCLUDE_RTM_OPT
2376   int rtm_state() const {
2377     return _rtm_state;
2378   }
2379   void set_rtm_state(RTMState rstate) {
2380     _rtm_state = (int)rstate;
2381   }
2382   void atomic_set_rtm_state(RTMState rstate) {
2383     Atomic::store((int)rstate, &_rtm_state);
2384   }
2385 
2386   static int rtm_state_offset_in_bytes() {
2387     return offset_of(MethodData, _rtm_state);
2388   }
2389 #endif
2390 
2391   void set_would_profile(bool p)              { _would_profile = p ? profile : no_profile; }
2392   bool would_profile() const                  { return _would_profile != no_profile; }
2393 
2394   int num_loops() const                       { return _num_loops;  }
2395   void set_num_loops(int n)                   { _num_loops = n;     }
2396   int num_blocks() const                      { return _num_blocks; }
2397   void set_num_blocks(int n)                  { _num_blocks = n;    }
2398 
2399   bool is_mature() const;  // consult mileage and ProfileMaturityPercentage
2400   static int mileage_of(Method* m);
2401 
2402   // Support for interprocedural escape analysis, from Thomas Kotzmann.
2403   enum EscapeFlag {
2404     estimated    = 1 << 0,
2405     return_local = 1 << 1,
2406     return_allocated = 1 << 2,
2407     allocated_escapes = 1 << 3,
2408     unknown_modified = 1 << 4
2409   };
2410 
2411   intx eflags()                                  { return _eflags; }
2412   intx arg_local()                               { return _arg_local; }
2413   intx arg_stack()                               { return _arg_stack; }
2414   intx arg_returned()                            { return _arg_returned; }
2415   uint arg_modified(int a)                       { ArgInfoData *aid = arg_info();
2416                                                    assert(aid != NULL, "arg_info must be not null");
2417                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
2418                                                    return aid->arg_modified(a); }
2419 
2420   void set_eflags(intx v)                        { _eflags = v; }
2421   void set_arg_local(intx v)                     { _arg_local = v; }
2422   void set_arg_stack(intx v)                     { _arg_stack = v; }
2423   void set_arg_returned(intx v)                  { _arg_returned = v; }
2424   void set_arg_modified(int a, uint v)           { ArgInfoData *aid = arg_info();
2425                                                    assert(aid != NULL, "arg_info must be not null");
2426                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
2427                                                    aid->set_arg_modified(a, v); }
2428 
2429   void clear_escape_info()                       { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
2430 
2431   // Location and size of data area
2432   address data_base() const {
2433     return (address) _data;
2434   }
2435   int data_size() const {
2436     return _data_size;
2437   }
2438 
2439   // Accessors
2440   Method* method() const { return _method; }
2441 
2442   // Get the data at an arbitrary (sort of) data index.
2443   ProfileData* data_at(int data_index) const;
2444 
2445   // Walk through the data in order.
2446   ProfileData* first_data() const { return data_at(first_di()); }
2447   ProfileData* next_data(ProfileData* current) const;
2448   bool is_valid(ProfileData* current) const { return current != NULL; }
2449 
2450   // Convert a dp (data pointer) to a di (data index).
2451   int dp_to_di(address dp) const {
2452     return dp - ((address)_data);
2453   }
2454 
2455   // bci to di/dp conversion.
2456   address bci_to_dp(int bci);
2457   int bci_to_di(int bci) {
2458     return dp_to_di(bci_to_dp(bci));
2459   }
2460 
2461   // Get the data at an arbitrary bci, or NULL if there is none.
2462   ProfileData* bci_to_data(int bci);
2463 
2464   // Same, but try to create an extra_data record if one is needed:
2465   ProfileData* allocate_bci_to_data(int bci, Method* m) {
2466     ProfileData* data = NULL;
2467     // If m not NULL, try to allocate a SpeculativeTrapData entry
2468     if (m == NULL) {
2469       data = bci_to_data(bci);
2470     }
2471     if (data != NULL) {
2472       return data;
2473     }
2474     data = bci_to_extra_data(bci, m, true);
2475     if (data != NULL) {
2476       return data;
2477     }
2478     // If SpeculativeTrapData allocation fails try to allocate a
2479     // regular entry
2480     data = bci_to_data(bci);
2481     if (data != NULL) {
2482       return data;
2483     }
2484     return bci_to_extra_data(bci, NULL, true);
2485   }
2486 
2487   // Add a handful of extra data records, for trap tracking.
2488   DataLayout* extra_data_base() const  { return limit_data_position(); }
2489   DataLayout* extra_data_limit() const { return (DataLayout*)((address)this + size_in_bytes()); }
2490   DataLayout* args_data_limit() const  { return (DataLayout*)((address)this + size_in_bytes() -
2491                                                               parameters_size_in_bytes()); }
2492   int extra_data_size() const          { return (address)extra_data_limit() - (address)extra_data_base(); }
2493   static DataLayout* next_extra(DataLayout* dp);
2494 
2495   // Return (uint)-1 for overflow.
2496   uint trap_count(int reason) const {
2497     assert((uint)reason < JVMCI_ONLY(2*) _trap_hist_limit, "oob");
2498     return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
2499   }
2500   // For loops:
2501   static uint trap_reason_limit() { return _trap_hist_limit; }
2502   static uint trap_count_limit()  { return _trap_hist_mask; }
2503   uint inc_trap_count(int reason) {
2504     // Count another trap, anywhere in this method.
2505     assert(reason >= 0, "must be single trap");
2506     assert((uint)reason < JVMCI_ONLY(2*) _trap_hist_limit, "oob");
2507     uint cnt1 = 1 + _trap_hist._array[reason];
2508     if ((cnt1 & _trap_hist_mask) != 0) {  // if no counter overflow...
2509       _trap_hist._array[reason] = cnt1;
2510       return cnt1;
2511     } else {
2512       return _trap_hist_mask + (++_nof_overflow_traps);
2513     }
2514   }
2515 
2516   uint overflow_trap_count() const {
2517     return _nof_overflow_traps;
2518   }
2519   uint overflow_recompile_count() const {
2520     return _nof_overflow_recompiles;
2521   }
2522   void inc_overflow_recompile_count() {
2523     _nof_overflow_recompiles += 1;
2524   }
2525   uint decompile_count() const {
2526     return _nof_decompiles;
2527   }
2528   void inc_decompile_count() {
2529     _nof_decompiles += 1;
2530     if (decompile_count() > (uint)PerMethodRecompilationCutoff) {
2531       method()->set_not_compilable(CompLevel_full_optimization, true, "decompile_count > PerMethodRecompilationCutoff");
2532     }
2533   }
2534   uint tenure_traps() const {
2535     return _tenure_traps;
2536   }
2537   void inc_tenure_traps() {
2538     _tenure_traps += 1;
2539   }
2540 
2541   // Return pointer to area dedicated to parameters in MDO
2542   ParametersTypeData* parameters_type_data() const {
2543     assert(_parameters_type_data_di != parameters_uninitialized, "called too early");
2544     return _parameters_type_data_di != no_parameters ? data_layout_at(_parameters_type_data_di)->data_in()->as_ParametersTypeData() : NULL;
2545   }
2546 
2547   int parameters_type_data_di() const {
2548     assert(_parameters_type_data_di != parameters_uninitialized && _parameters_type_data_di != no_parameters, "no args type data");
2549     return _parameters_type_data_di;
2550   }
2551 
2552   // Support for code generation
2553   static ByteSize data_offset() {
2554     return byte_offset_of(MethodData, _data[0]);
2555   }
2556 
2557   static ByteSize trap_history_offset() {
2558     return byte_offset_of(MethodData, _trap_hist._array);
2559   }
2560 
2561   static ByteSize invocation_counter_offset() {
2562     return byte_offset_of(MethodData, _invocation_counter);
2563   }
2564 
2565   static ByteSize backedge_counter_offset() {
2566     return byte_offset_of(MethodData, _backedge_counter);
2567   }
2568 
2569   static ByteSize invoke_mask_offset() {
2570     return byte_offset_of(MethodData, _invoke_mask);
2571   }
2572 
2573   static ByteSize backedge_mask_offset() {
2574     return byte_offset_of(MethodData, _backedge_mask);
2575   }
2576 
2577   static ByteSize parameters_type_data_di_offset() {
2578     return byte_offset_of(MethodData, _parameters_type_data_di);
2579   }
2580 
2581   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
2582   virtual MetaspaceObj::Type type() const { return MethodDataType; }
2583 
2584   // Deallocation support - no pointer fields to deallocate
2585   void deallocate_contents(ClassLoaderData* loader_data) {}
2586 
2587   // GC support
2588   void set_size(int object_size_in_bytes) { _size = object_size_in_bytes; }
2589 
2590   // Printing
2591   void print_on      (outputStream* st) const;
2592   void print_value_on(outputStream* st) const;
2593 
2594   // printing support for method data
2595   void print_data_on(outputStream* st) const;
2596 
2597   const char* internal_name() const { return "{method data}"; }
2598 
2599   // verification
2600   void verify_on(outputStream* st);
2601   void verify_data_on(outputStream* st);
2602 
2603   static bool profile_parameters_for_method(const methodHandle& m);
2604   static bool profile_arguments();
2605   static bool profile_arguments_jsr292_only();
2606   static bool profile_return();
2607   static bool profile_parameters();
2608   static bool profile_return_jsr292_only();
2609 
2610   void clean_method_data(bool always_clean);
2611   void clean_weak_method_links();
2612   DEBUG_ONLY(void verify_clean_weak_method_links();)
2613   Mutex* extra_data_lock() { return &_extra_data_lock; }
2614 };
2615 
2616 #endif // SHARE_VM_OOPS_METHODDATAOOP_HPP