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