1 /*
   2  * Copyright (c) 2020, 2026, 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 #include "classfile/classFileParser.hpp"
  26 #include "classfile/fieldLayoutBuilder.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "jvm.h"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/array.hpp"
  32 #include "oops/fieldStreams.inline.hpp"
  33 #include "oops/inlineKlass.inline.hpp"
  34 #include "oops/instanceKlass.inline.hpp"
  35 #include "oops/instanceMirrorKlass.hpp"
  36 #include "oops/klass.inline.hpp"
  37 #include "runtime/fieldDescriptor.inline.hpp"
  38 #include "utilities/align.hpp"
  39 #include "utilities/powerOfTwo.hpp"
  40 
  41 static LayoutKind field_layout_selection(FieldInfo field_info, Array<InlineLayoutInfo>* inline_layout_info_array,
  42                                          bool can_use_atomic_flat) {
  43 
  44   // The can_use_atomic_flat argument indicates if an atomic flat layout can be used for this field.
  45   // This argument will be false if the container is a loosely consistent value class. Using an atomic layout
  46   // in a container that has no atomicity guarantee creates a risk to see this field's value be subject to
  47   // tearing even if the field's class was declared atomic (non loosely consistent).
  48 
  49   if (!UseFieldFlattening) {
  50     return LayoutKind::REFERENCE;
  51   }
  52 
  53   if (field_info.field_flags().is_injected()) {
  54     // don't flatten injected fields
  55     return LayoutKind::REFERENCE;
  56   }
  57 
  58   if (field_info.access_flags().is_volatile()) {
  59     // volatile is used as a keyword to prevent flattening
  60     return LayoutKind::REFERENCE;
  61   }
  62 
  63   if (field_info.access_flags().is_static()) {
  64     // don't flatten static fields
  65     return LayoutKind::REFERENCE;
  66   }
  67 
  68   if (inline_layout_info_array == nullptr || inline_layout_info_array->adr_at(field_info.index())->klass() == nullptr) {
  69     // field's type is not a known value class, using a reference
  70     return LayoutKind::REFERENCE;
  71   }
  72 
  73   InlineLayoutInfo* inline_field_info = inline_layout_info_array->adr_at(field_info.index());
  74   InlineKlass* vk = inline_field_info->klass();
  75 
  76   if (field_info.field_flags().is_null_free_inline_type()) {
  77     assert(field_info.access_flags().is_strict(), "null-free fields must be strict");
  78     if (vk->must_be_atomic()) {
  79       if (vk->is_naturally_atomic(true /* null-free */) && vk->has_null_free_non_atomic_layout()) {
  80         return LayoutKind::NULL_FREE_NON_ATOMIC_FLAT;
  81       }
  82       return (vk->has_null_free_atomic_layout() && can_use_atomic_flat) ? LayoutKind::NULL_FREE_ATOMIC_FLAT : LayoutKind::REFERENCE;
  83     } else {
  84       return vk->has_null_free_non_atomic_layout() ? LayoutKind::NULL_FREE_NON_ATOMIC_FLAT : LayoutKind::REFERENCE;
  85     }
  86   } else {
  87     // To preserve the consistency between the null-marker and the field content, the NULLABLE_NON_ATOMIC_FLAT
  88     // can only be used in containers that have atomicity guarantees (can_use_atomic_flat argument set to true)
  89     if (field_info.access_flags().is_strict() && field_info.access_flags().is_final() && can_use_atomic_flat) {
  90       if (vk->has_nullable_non_atomic_layout()) {
  91         return LayoutKind::NULLABLE_NON_ATOMIC_FLAT;
  92       }
  93     }
  94     // Another special case where NULLABLE_NON_ATOMIC_FLAT can be used: nullable empty values, because the
  95     // payload of those values contains only the null-marker
  96     if (vk->is_empty_inline_type() && vk->has_nullable_non_atomic_layout()) {
  97       return LayoutKind::NULLABLE_NON_ATOMIC_FLAT;
  98     }
  99     if (UseNullableAtomicValueFlattening && vk->has_nullable_atomic_layout()) {
 100       return can_use_atomic_flat ? LayoutKind::NULLABLE_ATOMIC_FLAT : LayoutKind::REFERENCE;
 101     } else {
 102       return LayoutKind::REFERENCE;
 103     }
 104   }
 105 }
 106 
 107 static LayoutKind adjust_with_budget(FieldInfo field_info, Array<InlineLayoutInfo>* inline_layout_info_array,
 108                                      LayoutKind lk, int& budget) {
 109   if (lk == LayoutKind::REFERENCE) return lk;
 110   assert(LayoutKindHelper::is_flat((lk)), "Must be");
 111   InlineLayoutInfo* inline_field_info = inline_layout_info_array->adr_at(field_info.index());
 112   InlineKlass* vk = inline_field_info->klass();
 113   int size = vk->layout_size_in_bytes(lk);
 114   if (size > budget) {
 115     return LayoutKind::REFERENCE;
 116   } else {
 117     budget -= size;
 118     return lk;
 119   }
 120 }
 121 
 122 static bool field_is_inlineable(FieldInfo fieldinfo, LayoutKind lk, Array<InlineLayoutInfo>* ili) {
 123   if (fieldinfo.field_flags().is_null_free_inline_type()) {
 124     // A null-free inline type is always inlineable
 125     return true;
 126   }
 127 
 128   if (lk != LayoutKind::REFERENCE) {
 129     assert(lk != LayoutKind::BUFFERED, "Sanity check");
 130     assert(lk != LayoutKind::UNKNOWN, "Sanity check");
 131     // We've chosen a layout that isn't a normal reference
 132     return true;
 133   }
 134 
 135   const int field_index = (int)fieldinfo.index();
 136   if (!fieldinfo.field_flags().is_injected() &&
 137       ili != nullptr &&
 138       ili->adr_at(field_index)->klass() != nullptr &&
 139       !ili->adr_at(field_index)->klass()->is_identity_class() &&
 140       !ili->adr_at(field_index)->klass()->is_abstract()) {
 141     // The field's klass is not an identity class or abstract
 142     return true;
 143   }
 144 
 145   return false;
 146 }
 147 
 148 LayoutRawBlock::LayoutRawBlock(Kind kind, int size) :
 149   _next_block(nullptr),
 150   _prev_block(nullptr),
 151   _inline_klass(nullptr),
 152   _block_kind(kind),
 153   _layout_kind(LayoutKind::UNKNOWN),
 154   _offset(-1),
 155   _alignment(1),
 156   _size(size),
 157   _field_index(-1) {
 158   assert(kind == EMPTY || kind == RESERVED || kind == PADDING || kind == INHERITED || kind == NULL_MARKER,
 159          "Otherwise, should use the constructor with a field index argument");
 160   assert(size > 0, "Sanity check");
 161 }
 162 
 163 
 164 LayoutRawBlock::LayoutRawBlock(int index, Kind kind, int size, int alignment) :
 165   _next_block(nullptr),
 166   _prev_block(nullptr),
 167   _inline_klass(nullptr),
 168   _block_kind(kind),
 169   _layout_kind(LayoutKind::UNKNOWN),
 170  _offset(-1),
 171  _alignment(alignment),
 172  _size(size),
 173  _field_index(index) {
 174   assert(kind == REGULAR || kind == FLAT || kind == INHERITED,
 175          "Other kind do not have a field index");
 176   assert(size > 0, "Sanity check");
 177   assert(alignment > 0, "Sanity check");
 178 }
 179 
 180 bool LayoutRawBlock::fit(int size, int alignment) {
 181   int adjustment = 0;
 182   if ((_offset % alignment) != 0) {
 183     adjustment = alignment - (_offset % alignment);
 184   }
 185   return _size >= size + adjustment;
 186 }
 187 
 188 FieldGroup::FieldGroup(int contended_group) :
 189   _next(nullptr),
 190   _small_primitive_fields(nullptr),
 191   _big_primitive_fields(nullptr),
 192   _oop_fields(nullptr),
 193   _contended_group(contended_group) {} // -1 means no contended group, 0 means default contended group
 194 
 195 void FieldGroup::add_primitive_field(int idx, BasicType type) {
 196   int size = type2aelembytes(type);
 197   LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for primitive types */);
 198   if (size >= heapOopSize) {
 199     add_to_big_primitive_list(block);
 200   } else {
 201     add_to_small_primitive_list(block);
 202   }
 203 }
 204 
 205 void FieldGroup::add_oop_field(int idx) {
 206   int size = type2aelembytes(T_OBJECT);
 207   LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for oops */);
 208   if (_oop_fields == nullptr) {
 209     _oop_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE);
 210   }
 211   _oop_fields->append(block);
 212 }
 213 
 214 void FieldGroup::add_flat_field(int idx, InlineKlass* vk, LayoutKind lk) {
 215   const int size = vk->layout_size_in_bytes(lk);
 216   const int alignment = vk->layout_alignment(lk);
 217 
 218   LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::FLAT, size, alignment);
 219   block->set_inline_klass(vk);
 220   block->set_layout_kind(lk);
 221   if (block->size() >= heapOopSize) {
 222     add_to_big_primitive_list(block);
 223   } else {
 224     assert(!vk->contains_oops(), "Size of Inline klass with oops should be >= heapOopSize");
 225     add_to_small_primitive_list(block);
 226   }
 227 }
 228 
 229 void FieldGroup::sort_by_size() {
 230   if (_small_primitive_fields != nullptr) {
 231     _small_primitive_fields->sort(LayoutRawBlock::compare_size_inverted);
 232   }
 233   if (_big_primitive_fields != nullptr) {
 234     _big_primitive_fields->sort(LayoutRawBlock::compare_size_inverted);
 235   }
 236 }
 237 
 238 void FieldGroup::add_to_small_primitive_list(LayoutRawBlock* block) {
 239   if (_small_primitive_fields == nullptr) {
 240     _small_primitive_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE);
 241   }
 242   _small_primitive_fields->append(block);
 243 }
 244 
 245 void FieldGroup::add_to_big_primitive_list(LayoutRawBlock* block) {
 246   if (_big_primitive_fields == nullptr) {
 247     _big_primitive_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE);
 248   }
 249   _big_primitive_fields->append(block);
 250 }
 251 
 252 FieldLayout::FieldLayout(GrowableArray<FieldInfo>* field_info, Array<InlineLayoutInfo>* inline_layout_info_array, ConstantPool* cp) :
 253   _field_info(field_info),
 254   _inline_layout_info_array(inline_layout_info_array),
 255   _cp(cp),
 256   _blocks(nullptr),
 257   _start(_blocks),
 258   _last(_blocks),
 259   _super_first_field_offset(-1),
 260   _super_alignment(-1),
 261   _super_min_align_required(-1),
 262   _null_reset_value_offset(-1),
 263   _acmp_maps_offset(-1),
 264   _super_has_nonstatic_fields(false),
 265   _has_inherited_fields(false) {}
 266 
 267 void FieldLayout::initialize_static_layout() {
 268   _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
 269   _blocks->set_offset(0);
 270   _last = _blocks;
 271   _start = _blocks;
 272   // Note: at this stage, InstanceMirrorKlass::offset_of_static_fields() could be zero, because
 273   // during bootstrapping, the size of the java.lang.Class is still not known when layout
 274   // of static field is computed. Field offsets are fixed later when the size is known
 275   // (see java_lang_Class::fixup_mirror())
 276   if (InstanceMirrorKlass::offset_of_static_fields() > 0) {
 277     insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, InstanceMirrorKlass::offset_of_static_fields()));
 278     _blocks->set_offset(0);
 279   }
 280 }
 281 
 282 void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass, bool& super_ends_with_oop) {
 283   if (super_klass == nullptr) {
 284     super_ends_with_oop = false;
 285     _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
 286     _blocks->set_offset(0);
 287     _last = _blocks;
 288     _start = _blocks;
 289     insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes()));
 290   } else {
 291     reconstruct_layout(super_klass, _super_has_nonstatic_fields, super_ends_with_oop);
 292     fill_holes(super_klass);
 293     if ((!super_klass->has_contended_annotations()) || !_super_has_nonstatic_fields) {
 294       _start = _blocks;  // start allocating fields from the first empty block
 295     } else {
 296       _start = _last;    // append fields at the end of the reconstructed layout
 297     }
 298   }
 299 }
 300 
 301 LayoutRawBlock* FieldLayout::first_field_block() {
 302   LayoutRawBlock* block = _blocks;
 303   while (block != nullptr
 304          && block->block_kind() != LayoutRawBlock::INHERITED
 305          && block->block_kind() != LayoutRawBlock::REGULAR
 306          && block->block_kind() != LayoutRawBlock::FLAT
 307          && block->block_kind() != LayoutRawBlock::NULL_MARKER) {
 308     block = block->next_block();
 309   }
 310   return block;
 311 }
 312 
 313 // Insert a set of fields into a layout.
 314 // For each field, search for an empty slot able to fit the field
 315 // (satisfying both size and alignment requirements), if none is found,
 316 // add the field at the end of the layout.
 317 // Fields cannot be inserted before the block specified in the "start" argument
 318 void FieldLayout::add(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) {
 319   if (list == nullptr) return;
 320   if (start == nullptr) start = this->_start;
 321   bool last_search_success = false;
 322   int last_size = 0;
 323   int last_alignment = 0;
 324   for (int i = 0; i < list->length(); i ++) {
 325     LayoutRawBlock* b = list->at(i);
 326     LayoutRawBlock* cursor = nullptr;
 327     LayoutRawBlock* candidate = nullptr;
 328     // if start is the last block, just append the field
 329     if (start == last_block()) {
 330       candidate = last_block();
 331     }
 332     // Before iterating over the layout to find an empty slot fitting the field's requirements,
 333     // check if the previous field had the same requirements and if the search for a fitting slot
 334     // was successful. If the requirements were the same but the search failed, a new search will
 335     // fail the same way, so just append the field at the of the layout.
 336     else  if (b->size() == last_size && b->alignment() == last_alignment && !last_search_success) {
 337       candidate = last_block();
 338     } else {
 339       // Iterate over the layout to find an empty slot fitting the field's requirements
 340       last_size = b->size();
 341       last_alignment = b->alignment();
 342       cursor = last_block()->prev_block();
 343       assert(cursor != nullptr, "Sanity check");
 344       last_search_success = true;
 345 
 346       assert(start->block_kind() != LayoutRawBlock::EMPTY, "");
 347       while (cursor != start) {
 348         if (cursor->block_kind() == LayoutRawBlock::EMPTY && cursor->fit(b->size(), b->alignment())) {
 349           if (candidate == nullptr || cursor->size() < candidate->size()) {
 350             candidate = cursor;
 351           }
 352         }
 353         cursor = cursor->prev_block();
 354       }
 355       if (candidate == nullptr) {
 356         candidate = last_block();
 357         last_search_success = false;
 358       }
 359       assert(candidate != nullptr, "Candidate must not be null");
 360       assert(candidate->block_kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block");
 361       assert(candidate->fit(b->size(), b->alignment()), "Candidate must be able to store the block");
 362     }
 363     insert_field_block(candidate, b);
 364   }
 365 }
 366 
 367 // Used for classes with hard coded field offsets, insert a field at the specified offset */
 368 void FieldLayout::add_field_at_offset(LayoutRawBlock* block, int offset, LayoutRawBlock* start) {
 369   assert(block != nullptr, "Sanity check");
 370   block->set_offset(offset);
 371   if (start == nullptr) {
 372     start = this->_start;
 373   }
 374   LayoutRawBlock* slot = start;
 375   while (slot != nullptr) {
 376     if ((slot->offset() <= block->offset() && (slot->offset() + slot->size()) > block->offset()) ||
 377         slot == _last){
 378       assert(slot->block_kind() == LayoutRawBlock::EMPTY, "Matching slot must be an empty slot");
 379       assert(slot->size() >= block->offset() - slot->offset() + block->size() ,"Matching slot must be big enough");
 380       if (slot->offset() < block->offset()) {
 381         int adjustment = block->offset() - slot->offset();
 382         LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment);
 383         insert(slot, adj);
 384       }
 385       insert(slot, block);
 386       if (slot->size() == 0) {
 387         remove(slot);
 388       }
 389       if (block->block_kind() == LayoutRawBlock::REGULAR || block->block_kind() == LayoutRawBlock::FLAT) {
 390         _field_info->adr_at(block->field_index())->set_offset(block->offset());
 391       }
 392       return;
 393     }
 394     slot = slot->next_block();
 395   }
 396   fatal("Should have found a matching slot above, corrupted layout or invalid offset");
 397 }
 398 
 399 // The allocation logic uses a best fit strategy: the set of fields is allocated
 400 // in the first empty slot big enough to contain the whole set ((including padding
 401 // to fit alignment constraints).
 402 void FieldLayout::add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) {
 403   if (list == nullptr) return;
 404   if (start == nullptr) {
 405     start = _start;
 406   }
 407   // This code assumes that if the first block is well aligned, the following
 408   // blocks would naturally be well aligned (no need for adjustment)
 409   int size = 0;
 410   for (int i = 0; i < list->length(); i++) {
 411     size += list->at(i)->size();
 412   }
 413 
 414   LayoutRawBlock* candidate = nullptr;
 415   if (start == last_block()) {
 416     candidate = last_block();
 417   } else {
 418     LayoutRawBlock* first = list->at(0);
 419     candidate = last_block()->prev_block();
 420     while (candidate->block_kind() != LayoutRawBlock::EMPTY || !candidate->fit(size, first->alignment())) {
 421       if (candidate == start) {
 422         candidate = last_block();
 423         break;
 424       }
 425       candidate = candidate->prev_block();
 426     }
 427     assert(candidate != nullptr, "Candidate must not be null");
 428     assert(candidate->block_kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block");
 429     assert(candidate->fit(size, first->alignment()), "Candidate must be able to store the whole contiguous block");
 430   }
 431 
 432   for (int i = 0; i < list->length(); i++) {
 433     LayoutRawBlock* b = list->at(i);
 434     insert_field_block(candidate, b);
 435     assert((candidate->offset() % b->alignment() == 0), "Contiguous blocks must be naturally well aligned");
 436   }
 437 }
 438 
 439 LayoutRawBlock* FieldLayout::insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block) {
 440   assert(slot->block_kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks");
 441   if (slot->offset() % block->alignment() != 0) {
 442     int adjustment = block->alignment() - (slot->offset() % block->alignment());
 443     LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment);
 444     insert(slot, adj);
 445   }
 446   assert(slot->size() >= block->size(), "Enough space must remain after adjustment");
 447   insert(slot, block);
 448   if (slot->size() == 0) {
 449     remove(slot);
 450   }
 451   // NULL_MARKER blocks are not real fields, so they don't have an entry in the FieldInfo array
 452   if (block->block_kind() != LayoutRawBlock::NULL_MARKER) {
 453     _field_info->adr_at(block->field_index())->set_offset(block->offset());
 454     if (_field_info->adr_at(block->field_index())->name(_cp) == vmSymbols::null_reset_value_name()) {
 455       _null_reset_value_offset = block->offset();
 456     }
 457     if (_field_info->adr_at(block->field_index())->name(_cp) == vmSymbols::acmp_maps_name()) {
 458       _acmp_maps_offset = block->offset();
 459     }
 460   }
 461   if (LayoutKindHelper::is_nullable_flat(block->layout_kind())) {
 462     int nm_offset = block->inline_klass()->null_marker_offset() - block->inline_klass()->payload_offset() + block->offset();
 463     _field_info->adr_at(block->field_index())->set_null_marker_offset(nm_offset);
 464     _inline_layout_info_array->adr_at(block->field_index())->set_null_marker_offset(nm_offset);
 465   }
 466 
 467   return block;
 468 }
 469 
 470 void FieldLayout::reconstruct_layout(const InstanceKlass* ik, bool& has_nonstatic_fields, bool& ends_with_oop) {
 471   has_nonstatic_fields = ends_with_oop = false;
 472   if (ik->is_abstract() && !ik->is_identity_class()) {
 473     _super_alignment = type2aelembytes(BasicType::T_LONG);
 474   }
 475   GrowableArray<LayoutRawBlock*>* all_fields = new GrowableArray<LayoutRawBlock*>(32);
 476   BasicType last_type;
 477   int last_offset = -1;
 478   while (ik != nullptr) {
 479     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
 480       BasicType type = Signature::basic_type(fs.signature());
 481       // distinction between static and non-static fields is missing
 482       if (fs.access_flags().is_static()) continue;
 483       has_nonstatic_fields = true;
 484       _has_inherited_fields = true;
 485       if (_super_first_field_offset == -1 || fs.offset() < _super_first_field_offset) {
 486         _super_first_field_offset = fs.offset();
 487       }
 488       LayoutRawBlock* block;
 489       if (fs.is_flat()) {
 490         InlineLayoutInfo layout_info = ik->inline_layout_info(fs.index());
 491         InlineKlass* vk = layout_info.klass();
 492         block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED,
 493                                    vk->layout_size_in_bytes(layout_info.kind()),
 494                                    vk->layout_alignment(layout_info.kind()));
 495         assert(_super_alignment == -1 || _super_alignment >=  vk->payload_alignment(), "Invalid value alignment");
 496         _super_min_align_required = _super_min_align_required > vk->payload_alignment() ? _super_min_align_required : vk->payload_alignment();
 497       } else {
 498         int size = type2aelembytes(type);
 499         // INHERITED blocks are marked as non-reference because oop_maps are handled by their holder class
 500         block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED, size, size);
 501         // For primitive types, the alignment is equal to the size
 502         assert(_super_alignment == -1 || _super_alignment >=  size, "Invalid value alignment");
 503         _super_min_align_required = _super_min_align_required > size ? _super_min_align_required : size;
 504       }
 505       if (fs.offset() > last_offset) {
 506         last_offset = fs.offset();
 507         last_type = type;
 508       }
 509       block->set_offset(fs.offset());
 510       all_fields->append(block);
 511     }
 512     ik = ik->super() == nullptr ? nullptr : ik->super();
 513   }
 514   assert(last_offset == -1 || last_offset > 0, "Sanity");
 515   if (last_offset > 0 &&
 516       (last_type == BasicType::T_ARRAY || last_type == BasicType::T_OBJECT)) {
 517     ends_with_oop = true;
 518   }
 519 
 520   all_fields->sort(LayoutRawBlock::compare_offset);
 521   _blocks = new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes());
 522   _blocks->set_offset(0);
 523   _last = _blocks;
 524   for(int i = 0; i < all_fields->length(); i++) {
 525     LayoutRawBlock* b = all_fields->at(i);
 526     _last->set_next_block(b);
 527     b->set_prev_block(_last);
 528     _last = b;
 529   }
 530   _start = _blocks;
 531 }
 532 
 533 // Called during the reconstruction of a layout, after fields from super
 534 // classes have been inserted. It fills unused slots between inserted fields
 535 // with EMPTY blocks, so the regular field insertion methods would work.
 536 // This method handles classes with @Contended annotations differently
 537 // by inserting PADDING blocks instead of EMPTY block to prevent subclasses'
 538 // fields to interfere with contended fields/classes.
 539 void FieldLayout::fill_holes(const InstanceKlass* super_klass) {
 540   assert(_blocks != nullptr, "Sanity check");
 541   assert(_blocks->offset() == 0, "first block must be at offset zero");
 542   LayoutRawBlock::Kind filling_type = super_klass->has_contended_annotations() ? LayoutRawBlock::PADDING: LayoutRawBlock::EMPTY;
 543   LayoutRawBlock* b = _blocks;
 544   while (b->next_block() != nullptr) {
 545     if (b->next_block()->offset() > (b->offset() + b->size())) {
 546       int size = b->next_block()->offset() - (b->offset() + b->size());
 547       // FIXME it would be better if initial empty blocks were tagged as PADDING for value classes
 548       // Tracked by JDK-8383383
 549       LayoutRawBlock* empty = new LayoutRawBlock(filling_type, size);
 550       empty->set_offset(b->offset() + b->size());
 551       empty->set_next_block(b->next_block());
 552       b->next_block()->set_prev_block(empty);
 553       b->set_next_block(empty);
 554       empty->set_prev_block(b);
 555     }
 556     b = b->next_block();
 557   }
 558   assert(b->next_block() == nullptr, "Invariant at this point");
 559   assert(b->block_kind() != LayoutRawBlock::EMPTY, "Sanity check");
 560   // If the super class has @Contended annotation, a padding block is
 561   // inserted at the end to ensure that fields from the subclasses won't share
 562   // the cache line of the last field of the contended class
 563   if (super_klass->has_contended_annotations() && ContendedPaddingWidth > 0) {
 564     LayoutRawBlock* p = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth);
 565     p->set_offset(b->offset() + b->size());
 566     b->set_next_block(p);
 567     p->set_prev_block(b);
 568     b = p;
 569   }
 570 
 571   LayoutRawBlock* last = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
 572   last->set_offset(b->offset() + b->size());
 573   assert(last->offset() > 0, "Sanity check");
 574   b->set_next_block(last);
 575   last->set_prev_block(b);
 576   _last = last;
 577 }
 578 
 579 LayoutRawBlock* FieldLayout::insert(LayoutRawBlock* slot, LayoutRawBlock* block) {
 580   assert(slot->block_kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks");
 581   assert(slot->offset() % block->alignment() == 0, "Incompatible alignment");
 582   block->set_offset(slot->offset());
 583   slot->set_offset(slot->offset() + block->size());
 584   assert((slot->size() - block->size()) < slot->size(), "underflow checking");
 585   assert(slot->size() - block->size() >= 0, "no negative size allowed");
 586   slot->set_size(slot->size() - block->size());
 587   block->set_prev_block(slot->prev_block());
 588   block->set_next_block(slot);
 589   slot->set_prev_block(block);
 590   if (block->prev_block() != nullptr) {
 591     block->prev_block()->set_next_block(block);
 592   }
 593   if (_blocks == slot) {
 594     _blocks = block;
 595   }
 596   if (_start == slot) {
 597     _start = block;
 598   }
 599   return block;
 600 }
 601 
 602 void FieldLayout::remove(LayoutRawBlock* block) {
 603   assert(block != nullptr, "Sanity check");
 604   assert(block != _last, "Sanity check");
 605   if (_blocks == block) {
 606     _blocks = block->next_block();
 607     if (_blocks != nullptr) {
 608       _blocks->set_prev_block(nullptr);
 609     }
 610   } else {
 611     assert(block->prev_block() != nullptr, "_prev should be set for non-head blocks");
 612     block->prev_block()->set_next_block(block->next_block());
 613     block->next_block()->set_prev_block(block->prev_block());
 614   }
 615   if (block == _start) {
 616     _start = block->prev_block();
 617   }
 618 }
 619 
 620 void FieldLayout::shift_fields(int shift) {
 621   LayoutRawBlock* b = first_field_block();
 622   assert(b != nullptr, "shift_fields must not be called if layout has no fields");
 623   LayoutRawBlock* previous = b->prev_block();
 624   if (previous->block_kind() == LayoutRawBlock::EMPTY) {
 625     previous->set_size(previous->size() + shift);
 626   } else {
 627     LayoutRawBlock* nb = new LayoutRawBlock(LayoutRawBlock::PADDING, shift);
 628     nb->set_offset(b->offset());
 629     previous->set_next_block(nb);
 630     nb->set_prev_block(previous);
 631     b->set_prev_block(nb);
 632     nb->set_next_block(b);
 633   }
 634   while (b != nullptr) {
 635     b->set_offset(b->offset() + shift);
 636     if (b->block_kind() == LayoutRawBlock::REGULAR || b->block_kind() == LayoutRawBlock::FLAT) {
 637       _field_info->adr_at(b->field_index())->set_offset(b->offset());
 638       if (LayoutKindHelper::is_nullable_flat(b->layout_kind())) {
 639         int new_nm_offset = _field_info->adr_at(b->field_index())->null_marker_offset() + shift;
 640         _field_info->adr_at(b->field_index())->set_null_marker_offset(new_nm_offset);
 641         _inline_layout_info_array->adr_at(b->field_index())->set_null_marker_offset(new_nm_offset);
 642       }
 643     }
 644     assert(b->block_kind() == LayoutRawBlock::EMPTY || b->offset() % b->alignment() == 0, "Must still be correctly aligned");
 645     b = b->next_block();
 646   }
 647 }
 648 
 649 LayoutRawBlock* FieldLayout::find_null_marker() {
 650   LayoutRawBlock* b = _blocks;
 651   while (b != nullptr) {
 652     if (b->block_kind() == LayoutRawBlock::NULL_MARKER) {
 653       return b;
 654     }
 655     b = b->next_block();
 656   }
 657   ShouldNotReachHere();
 658   return nullptr;
 659 }
 660 
 661 void FieldLayout::remove_null_marker() {
 662   LayoutRawBlock* b = first_field_block();
 663   while (b != nullptr) {
 664     if (b->block_kind() == LayoutRawBlock::NULL_MARKER) {
 665       if (b->next_block()->block_kind() == LayoutRawBlock::EMPTY) {
 666         LayoutRawBlock* n = b->next_block();
 667         remove(b);
 668         n->set_offset(b->offset());
 669         n->set_size(n->size() + b->size());
 670       } else {
 671         b->set_block_kind(LayoutRawBlock::EMPTY);
 672       }
 673       return;
 674     }
 675     b = b->next_block();
 676   }
 677   ShouldNotReachHere(); // if we reach this point, the null marker was not found!
 678 }
 679 
 680 void FieldLayout::print(outputStream* output, bool is_static, const InstanceKlass* super, Array<InlineLayoutInfo>* inline_fields, bool dummy_field_is_reused_as_null_marker) {
 681   ResourceMark rm;
 682   LayoutRawBlock* b = _blocks;
 683   while(b != _last) {
 684     switch(b->block_kind()) {
 685       case LayoutRawBlock::REGULAR: {
 686         FieldInfo* fi = _field_info->adr_at(b->field_index());
 687         output->print(" @%d %s %d/%d \"%s\" %s",
 688                       b->offset(),
 689                       "REGULAR",
 690                       b->size(),
 691                       b->alignment(),
 692                       fi->name(_cp)->as_C_string(),
 693                       fi->signature(_cp)->as_C_string());
 694 
 695         if (dummy_field_is_reused_as_null_marker) {
 696           const bool is_dummy_field = fi->name(_cp)->fast_compare(vmSymbols::symbol_at(VM_SYMBOL_ENUM_NAME(empty_marker_name))) == 0;
 697           if (is_dummy_field) {
 698             output->print(" (reused as null-marker)");
 699           }
 700         }
 701 
 702         output->cr();
 703         break;
 704       }
 705       case LayoutRawBlock::FLAT: {
 706         FieldInfo* fi = _field_info->adr_at(b->field_index());
 707         InlineKlass* ik = inline_fields->adr_at(fi->index())->klass();
 708         assert(ik != nullptr, "");
 709         output->print_cr(" @%d %s %d/%d \"%s\" %s %s@%p %s",
 710                          b->offset(),
 711                          "FLAT",
 712                          b->size(),
 713                          b->alignment(),
 714                          fi->name(_cp)->as_C_string(),
 715                          fi->signature(_cp)->as_C_string(),
 716                          ik->name()->as_C_string(),
 717                          ik->class_loader_data(),
 718                          LayoutKindHelper::layout_kind_as_string(b->layout_kind()));
 719         break;
 720       }
 721       case LayoutRawBlock::RESERVED: {
 722         output->print_cr(" @%d %s %d/-",
 723                          b->offset(),
 724                          "RESERVED",
 725                          b->size());
 726         break;
 727       }
 728       case LayoutRawBlock::INHERITED: {
 729         assert(!is_static, "Static fields are not inherited in layouts");
 730         assert(super != nullptr, "super klass must be provided to retrieve inherited fields info");
 731         bool found = false;
 732         const InstanceKlass* ik = super;
 733         while (!found && ik != nullptr) {
 734           for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
 735             if (fs.offset() == b->offset() && fs.access_flags().is_static() == is_static) {
 736               output->print_cr(" @%d %s %d/%d \"%s\" %s",
 737                   b->offset(),
 738                   "INHERITED",
 739                   b->size(),
 740                   b->alignment(),
 741                   fs.name()->as_C_string(),
 742                   fs.signature()->as_C_string());
 743               found = true;
 744               break;
 745             }
 746         }
 747         ik = ik->super();
 748       }
 749       break;
 750     }
 751     case LayoutRawBlock::EMPTY:
 752       output->print_cr(" @%d %s %d/1",
 753                        b->offset(),
 754                       "EMPTY",
 755                        b->size());
 756       break;
 757     case LayoutRawBlock::PADDING:
 758       output->print_cr(" @%d %s %d/1",
 759                       b->offset(),
 760                       "PADDING",
 761                       b->size());
 762       break;
 763     case LayoutRawBlock::NULL_MARKER:
 764     {
 765       output->print_cr(" @%d %s %d/1 ",
 766                       b->offset(),
 767                       "NULL_MARKER",
 768                       b->size());
 769       break;
 770     }
 771     default:
 772       fatal("Unknown block type");
 773     }
 774     b = b->next_block();
 775   }
 776 }
 777 
 778 FieldLayoutBuilder::FieldLayoutBuilder(const Symbol* classname, ClassLoaderData* loader_data, const InstanceKlass* super_klass, ConstantPool* constant_pool,
 779                                        GrowableArray<FieldInfo>* field_info, bool is_contended, bool is_inline_type,bool is_abstract_value,
 780                                        bool must_be_atomic, FieldLayoutInfo* info, Array<InlineLayoutInfo>* inline_layout_info_array) :
 781   _classname(classname),
 782   _loader_data(loader_data),
 783   _super_klass(super_klass),
 784   _constant_pool(constant_pool),
 785   _field_info(field_info),
 786   _info(info),
 787   _inline_layout_info_array(inline_layout_info_array),
 788   _root_group(nullptr),
 789   _contended_groups(GrowableArray<FieldGroup*>(8)),
 790   _static_fields(nullptr),
 791   _layout(nullptr),
 792   _static_layout(nullptr),
 793   _nonstatic_oopmap_count(0),
 794   _payload_alignment(-1),
 795   _payload_offset(-1),
 796   _null_marker_offset(-1),
 797   _payload_size_in_bytes(-1),
 798   _null_free_non_atomic_layout_size_in_bytes(-1),
 799   _null_free_non_atomic_layout_alignment(-1),
 800   _null_free_atomic_layout_size_in_bytes(-1),
 801   _nullable_atomic_layout_size_in_bytes(-1),
 802   _nullable_non_atomic_layout_size_in_bytes(-1),
 803   _fields_size_sum(0),
 804   _declared_nonstatic_fields_count(0),
 805   _flattening_budget((int)FlatteningBudget),  // uint -> int convertion but FlatteningBudget value has
 806                                               // been validated in VM flags parsing (range [0, 1024 * 1024]).
 807   _has_non_naturally_atomic_fields(false),
 808   _is_naturally_atomic(false),
 809   _must_be_atomic(must_be_atomic),
 810   _has_nonstatic_fields(false),
 811   _has_inlineable_fields(false),
 812   _has_inlined_fields(false),
 813   _is_contended(is_contended),
 814   _is_inline_type(is_inline_type),
 815   _is_abstract_value(is_abstract_value),
 816   _is_empty_inline_class(false) {}
 817 
 818 FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) {
 819   assert(g > 0, "must only be called for named contended groups");
 820   FieldGroup* fg = nullptr;
 821   for (int i = 0; i < _contended_groups.length(); i++) {
 822     fg = _contended_groups.at(i);
 823     if (fg->contended_group() == g) return fg;
 824   }
 825   fg = new FieldGroup(g);
 826   _contended_groups.append(fg);
 827   return fg;
 828 }
 829 
 830 void FieldLayoutBuilder::prologue() {
 831   _layout = new FieldLayout(_field_info, _inline_layout_info_array, _constant_pool);
 832   const InstanceKlass* super_klass = _super_klass;
 833   _layout->initialize_instance_layout(super_klass, _super_ends_with_oop);
 834   _nonstatic_oopmap_count = super_klass == nullptr ? 0 : super_klass->nonstatic_oop_map_count();
 835   if (super_klass != nullptr) {
 836     _has_nonstatic_fields = super_klass->has_nonstatic_fields();
 837   }
 838   _static_layout = new FieldLayout(_field_info, _inline_layout_info_array, _constant_pool);
 839   _static_layout->initialize_static_layout();
 840   _static_fields = new FieldGroup();
 841   _root_group = new FieldGroup();
 842 }
 843 
 844 // Field sorting for regular (non-inline) classes:
 845 //   - fields are sorted in static and non-static fields
 846 //   - non-static fields are also sorted according to their contention group
 847 //     (support of the @Contended annotation)
 848 //   - @Contended annotation is ignored for static fields
 849 //   - field flattening decisions are taken in this method
 850 void FieldLayoutBuilder::regular_field_sorting() {
 851   int idx = 0;
 852   for (GrowableArrayIterator<FieldInfo> it = _field_info->begin(); it != _field_info->end(); ++it, ++idx) {
 853     FieldGroup* group = nullptr;
 854     FieldInfo fieldinfo = *it;
 855     if (fieldinfo.access_flags().is_static()) {
 856       group = _static_fields;
 857     } else {
 858       _has_nonstatic_fields = true;
 859       if (fieldinfo.field_flags().is_contended()) {
 860         int g = fieldinfo.contended_group();
 861         if (g == 0) {
 862           group = new FieldGroup(true);
 863           _contended_groups.append(group);
 864         } else {
 865           group = get_or_create_contended_group(g);
 866         }
 867       } else {
 868         group = _root_group;
 869       }
 870     }
 871     assert(group != nullptr, "invariant");
 872     BasicType type = Signature::basic_type(fieldinfo.signature(_constant_pool));
 873     switch(type) {
 874     case T_BYTE:
 875     case T_CHAR:
 876     case T_DOUBLE:
 877     case T_FLOAT:
 878     case T_INT:
 879     case T_LONG:
 880     case T_SHORT:
 881     case T_BOOLEAN:
 882       group->add_primitive_field(idx, type);
 883       break;
 884     case T_OBJECT:
 885     case T_ARRAY:
 886     {
 887       LayoutKind lk = field_layout_selection(fieldinfo, _inline_layout_info_array, true);
 888       lk = adjust_with_budget(fieldinfo, _inline_layout_info_array, lk, _flattening_budget);
 889       if (field_is_inlineable(fieldinfo, lk, _inline_layout_info_array)) {
 890         _has_inlineable_fields = true;
 891       }
 892 
 893       if (lk == LayoutKind::REFERENCE) {
 894         if (group != _static_fields) _nonstatic_oopmap_count++;
 895         group->add_oop_field(idx);
 896       } else {
 897         assert(group != _static_fields, "Static fields are not flattened");
 898         assert(lk != LayoutKind::BUFFERED && lk != LayoutKind::UNKNOWN,
 899                "Invalid layout kind for flat field: %s", LayoutKindHelper::layout_kind_as_string(lk));
 900 
 901         const int field_index = (int)fieldinfo.index();
 902         assert(_inline_layout_info_array != nullptr, "Array must have been created");
 903         assert(_inline_layout_info_array->adr_at(field_index)->klass() != nullptr, "Klass must have been set");
 904         _has_inlined_fields = true;
 905         InlineKlass* vk = _inline_layout_info_array->adr_at(field_index)->klass();
 906         group->add_flat_field(idx, vk, lk);
 907         _inline_layout_info_array->adr_at(field_index)->set_kind(lk);
 908         _nonstatic_oopmap_count += vk->nonstatic_oop_map_count();
 909         _field_info->adr_at(idx)->field_flags_addr()->update_flat(true);
 910         _field_info->adr_at(idx)->set_layout_kind(lk);
 911         // no need to update _must_be_atomic if vk->must_be_atomic() is true because current class is not an inline class
 912       }
 913       break;
 914     }
 915     default:
 916       fatal("Something wrong?");
 917     }
 918   }
 919   _root_group->sort_by_size();
 920   _static_fields->sort_by_size();
 921   if (!_contended_groups.is_empty()) {
 922     for (int i = 0; i < _contended_groups.length(); i++) {
 923       _contended_groups.at(i)->sort_by_size();
 924     }
 925   }
 926 }
 927 
 928 /* Field sorting for inline classes:
 929  *   - because inline classes are immutable, the @Contended annotation is ignored
 930  *     when computing their layout (with only read operation, there's no false
 931  *     sharing issue)
 932  *   - this method also records the alignment of the field with the most
 933  *     constraining alignment, this value is then used as the alignment
 934  *     constraint when flattening this inline type into another container
 935  *   - field flattening decisions are taken in this method (those decisions are
 936  *     currently only based in the size of the fields to be flattened, the size
 937  *     of the resulting instance is not considered)
 938  */
 939 void FieldLayoutBuilder::inline_class_field_sorting() {
 940   assert(_is_inline_type || _is_abstract_value, "Should only be used for inline classes");
 941   int alignment = -1;
 942   int idx = 0;
 943   for (GrowableArrayIterator<FieldInfo> it = _field_info->begin(); it != _field_info->end(); ++it, ++idx) {
 944     FieldGroup* group = nullptr;
 945     FieldInfo fieldinfo = *it;
 946     int field_alignment = 1;
 947     if (fieldinfo.access_flags().is_static()) {
 948       group = _static_fields;
 949     } else {
 950       _has_nonstatic_fields = true;
 951       _declared_nonstatic_fields_count++;
 952       group = _root_group;
 953     }
 954     assert(group != nullptr, "invariant");
 955     BasicType type = Signature::basic_type(fieldinfo.signature(_constant_pool));
 956     switch(type) {
 957     case T_BYTE:
 958     case T_CHAR:
 959     case T_DOUBLE:
 960     case T_FLOAT:
 961     case T_INT:
 962     case T_LONG:
 963     case T_SHORT:
 964     case T_BOOLEAN:
 965       if (group != _static_fields) {
 966         field_alignment = type2aelembytes(type); // alignment == size for primitive types
 967       }
 968       group->add_primitive_field(idx, type);
 969       break;
 970     case T_OBJECT:
 971     case T_ARRAY:
 972     {
 973       bool use_atomic_flat = _must_be_atomic; // flatten atomic fields only if the container is itself atomic
 974       LayoutKind lk = field_layout_selection(fieldinfo, _inline_layout_info_array, use_atomic_flat);
 975       lk = adjust_with_budget(fieldinfo, _inline_layout_info_array, lk, _flattening_budget);
 976       if (field_is_inlineable(fieldinfo, lk, _inline_layout_info_array)) {
 977         _has_inlineable_fields = true;
 978       }
 979 
 980       if (lk == LayoutKind::REFERENCE) {
 981         if (group != _static_fields) {
 982           _nonstatic_oopmap_count++;
 983           field_alignment = type2aelembytes(type); // alignment == size for oops
 984         }
 985         group->add_oop_field(idx);
 986       } else {
 987         assert(group != _static_fields, "Static fields are not flattened");
 988         assert(lk != LayoutKind::BUFFERED && lk != LayoutKind::UNKNOWN,
 989                "Invalid layout kind for flat field: %s", LayoutKindHelper::layout_kind_as_string(lk));
 990 
 991         const int field_index = (int)fieldinfo.index();
 992         assert(_inline_layout_info_array != nullptr, "Array must have been created");
 993         assert(_inline_layout_info_array->adr_at(field_index)->klass() != nullptr, "Klass must have been set");
 994         _has_inlined_fields = true;
 995         InlineKlass* vk = _inline_layout_info_array->adr_at(field_index)->klass();
 996         if (!vk->is_naturally_atomic(LayoutKindHelper::is_null_free_flat(lk))) _has_non_naturally_atomic_fields = true;
 997         group->add_flat_field(idx, vk, lk);
 998         _inline_layout_info_array->adr_at(field_index)->set_kind(lk);
 999         _nonstatic_oopmap_count += vk->nonstatic_oop_map_count();
1000         field_alignment = vk->layout_alignment(lk);
1001         _field_info->adr_at(idx)->field_flags_addr()->update_flat(true);
1002         _field_info->adr_at(idx)->set_layout_kind(lk);
1003       }
1004       break;
1005     }
1006     default:
1007       fatal("Unexpected BasicType");
1008     }
1009     if (!fieldinfo.access_flags().is_static() && field_alignment > alignment) alignment = field_alignment;
1010   }
1011   _root_group->sort_by_size();
1012   _static_fields->sort_by_size();
1013   _payload_alignment = alignment;
1014   assert(_has_nonstatic_fields || _is_abstract_value, "Concrete value types do not support zero instance size yet");
1015 }
1016 
1017 LayoutRawBlock* FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) {
1018   LayoutRawBlock* padding = nullptr;
1019   if (ContendedPaddingWidth > 0) {
1020     padding = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth);
1021     _layout->insert(slot, padding);
1022   }
1023   return padding;
1024 }
1025 
1026 // Computation of regular classes layout is an evolution of the previous default layout
1027 // (FieldAllocationStyle 1):
1028 //   - primitive fields (both primitive types and flat inline types) are allocated
1029 //     first (from the biggest to the smallest)
1030 //   - oop fields are allocated, either in existing gaps or at the end of
1031 //     the layout. We allocate oops in a single block to have a single oop map entry.
1032 //   - if the super class ended with an oop, we lead with oops. That will cause the
1033 //     trailing oop map entry of the super class and the oop map entry of this class
1034 //     to be folded into a single entry later. Correspondingly, if the super class
1035 //     ends with a primitive field, we gain nothing by leading with oops; therefore
1036 //     we let oop fields trail, thus giving future derived classes the chance to apply
1037 //     the same trick.
1038 void FieldLayoutBuilder::compute_regular_layout() {
1039   bool need_tail_padding = false;
1040   prologue();
1041   regular_field_sorting();
1042   if (_is_contended) {
1043     // insertion is currently easy because the current strategy doesn't try to fill holes
1044     // in super classes layouts => the _start block is by consequence the _last_block
1045     _layout->set_start(_layout->last_block());
1046     LayoutRawBlock* padding = insert_contended_padding(_layout->start());
1047     if (padding != nullptr) {
1048       // Setting the padding block as start ensures we do not insert past it.
1049       _layout->set_start(padding);
1050     }
1051     need_tail_padding = true;
1052   }
1053 
1054   if (_super_ends_with_oop) {
1055     _layout->add(_root_group->oop_fields());
1056     _layout->add(_root_group->big_primitive_fields());
1057     _layout->add(_root_group->small_primitive_fields());
1058   } else {
1059     _layout->add(_root_group->big_primitive_fields());
1060     _layout->add(_root_group->small_primitive_fields());
1061     _layout->add(_root_group->oop_fields());
1062   }
1063 
1064   if (!_contended_groups.is_empty()) {
1065     for (int i = 0; i < _contended_groups.length(); i++) {
1066       FieldGroup* cg = _contended_groups.at(i);
1067       LayoutRawBlock* start = _layout->last_block();
1068       LayoutRawBlock* padding = insert_contended_padding(start);
1069 
1070       // Do not insert fields past the padding block.
1071       if (padding != nullptr) {
1072         start = padding;
1073       }
1074 
1075       _layout->add(cg->big_primitive_fields(), start);
1076       _layout->add(cg->small_primitive_fields(), start);
1077       _layout->add(cg->oop_fields(), start);
1078       need_tail_padding = true;
1079     }
1080   }
1081 
1082   if (need_tail_padding) {
1083     insert_contended_padding(_layout->last_block());
1084   }
1085 
1086   // Warning: IntanceMirrorKlass expects static oops to be allocated first
1087   _static_layout->add_contiguously(_static_fields->oop_fields());
1088   _static_layout->add(_static_fields->big_primitive_fields());
1089   _static_layout->add(_static_fields->small_primitive_fields());
1090 
1091   epilogue();
1092 }
1093 
1094 /* Computation of inline classes has a slightly different strategy than for
1095  * regular classes. Regular classes have their oop fields allocated at the end
1096  * of the layout to increase GC performance. Unfortunately, this strategy
1097  * increases the number of empty slots inside an instance. Because the purpose
1098  * of inline classes is to be embedded into other containers, it is critical
1099  * to keep their size as small as possible. For this reason, the allocation
1100  * strategy is:
1101  *   - big primitive fields (primitive types and flat inline types larger
1102  *     than an oop) are allocated first (from the biggest to the smallest)
1103  *   - then oop fields
1104  *   - then small primitive fields (from the biggest to the smallest)
1105  */
1106 void FieldLayoutBuilder::compute_inline_class_layout() {
1107 
1108   // Test if the concrete inline class is an empty class (no instance fields)
1109   // and insert a dummy field if needed
1110   if (!_is_abstract_value) {
1111     bool declares_nonstatic_fields = false;
1112     for (FieldInfo fieldinfo : *_field_info) {
1113       if (!fieldinfo.access_flags().is_static()) {
1114         declares_nonstatic_fields = true;
1115         break;
1116       }
1117     }
1118 
1119     if (!declares_nonstatic_fields) {
1120       bool has_inherited_fields = _super_klass != nullptr && _super_klass->has_nonstatic_fields();
1121       if (!has_inherited_fields) {
1122         // Inject ".empty" dummy field
1123         _is_empty_inline_class = true;
1124         FieldInfo::FieldFlags fflags(0);
1125         fflags.update_injected(true);
1126         AccessFlags aflags;
1127         FieldInfo fi(aflags,
1128                     (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(empty_marker_name)),
1129                     (u2)vmSymbols::as_int(VM_SYMBOL_ENUM_NAME(byte_signature)),
1130                     0,
1131                     fflags);
1132         int idx = _field_info->append(fi);
1133         _field_info->adr_at(idx)->set_index(idx);
1134       }
1135     }
1136   }
1137 
1138   prologue();
1139   inline_class_field_sorting();
1140 
1141   assert(_layout->start()->block_kind() == LayoutRawBlock::RESERVED, "Unexpected");
1142 
1143   if (!_layout->super_has_nonstatic_fields()) {
1144     // No inherited fields, the layout must be empty except for the RESERVED block
1145     // PADDING is inserted if needed to ensure the correct alignment of the payload.
1146     if (_is_abstract_value && _has_nonstatic_fields) {
1147       // Non-static fields of the abstract class must be laid out without knowing
1148       // the alignment constraints of the fields of the sub-classes, so the worst
1149       // case scenario is assumed, which is currently the alignment of T_LONG.
1150       // PADDING is added if needed to ensure the payload will respect this alignment.
1151       _payload_alignment = type2aelembytes(BasicType::T_LONG);
1152     }
1153     assert(_layout->start()->next_block()->block_kind() == LayoutRawBlock::EMPTY, "Unexpected");
1154     LayoutRawBlock* first_empty = _layout->start()->next_block();
1155     if (first_empty->offset() % _payload_alignment != 0) {
1156       LayoutRawBlock* padding = new LayoutRawBlock(LayoutRawBlock::PADDING, _payload_alignment - (first_empty->offset() % _payload_alignment));
1157       _layout->insert(first_empty, padding);
1158       if (first_empty->size() == 0) {
1159         _layout->remove(first_empty);
1160       }
1161       _layout->set_start(padding);
1162     }
1163   } else { // the class has inherited some fields from its super(s)
1164     if (!_is_abstract_value) {
1165       // This is the step where the layout of the final concrete value class' layout
1166       // is computed. Super abstract value classes might have been too conservative
1167       // regarding alignment constraints, but now that the full set of non-static fields is
1168       // known, compute which alignment to use, then set first allowed field offset.
1169 
1170       assert(_has_nonstatic_fields, "Concrete value classes must have at least one field");
1171       if (_payload_alignment == -1) { // current class declares no local nonstatic fields
1172         _payload_alignment = _layout->super_min_align_required();
1173       }
1174 
1175       assert(_layout->super_alignment() >= _payload_alignment, "Incompatible alignment");
1176       assert(_layout->super_alignment() % _payload_alignment == 0, "Incompatible alignment");
1177 
1178       if (_payload_alignment < _layout->super_alignment()) {
1179         int new_alignment = _payload_alignment > _layout->super_min_align_required() ? _payload_alignment : _layout->super_min_align_required();
1180         assert(new_alignment % _payload_alignment == 0, "Must be");
1181         assert(new_alignment % _layout->super_min_align_required() == 0, "Must be");
1182         _payload_alignment = new_alignment;
1183       }
1184       _layout->set_start(_layout->first_field_block());
1185     } else {
1186       // Abstract value class inheriting fields, restore the pessimistic alignment
1187       // constraint (see comment above) and ensure no field will be inserted before
1188       // the first inherited field.
1189       _payload_alignment = type2aelembytes(BasicType::T_LONG);
1190       _layout->set_start(_layout->first_field_block());
1191     }
1192   }
1193 
1194   _layout->add(_root_group->big_primitive_fields());
1195   _layout->add(_root_group->oop_fields());
1196   _layout->add(_root_group->small_primitive_fields());
1197 
1198   LayoutRawBlock* first_field = _layout->first_field_block();
1199   if (first_field != nullptr) {
1200     _payload_offset = _layout->first_field_block()->offset();
1201     _payload_size_in_bytes = _layout->last_block()->offset() - _layout->first_field_block()->offset();
1202   } else {
1203     assert(_is_abstract_value, "Concrete inline types must have at least one field");
1204     _payload_offset = _layout->blocks()->size();
1205     _payload_size_in_bytes = 0;
1206   }
1207 
1208   // Determining if the value class is naturally atomic:
1209   if (_declared_nonstatic_fields_count == 0) {
1210     _is_naturally_atomic = _super_klass == vmClasses::Object_klass() || _super_klass->is_naturally_atomic(true /* null-free */);
1211   } else if (_declared_nonstatic_fields_count == 1) {
1212     _is_naturally_atomic = !_layout->super_has_nonstatic_fields() && !_has_non_naturally_atomic_fields;
1213   } else {
1214     _is_naturally_atomic = false;
1215   }
1216 
1217   // At this point, the characteristics of the raw layout (used in standalone instances) are known.
1218   // From this, additional layouts will be computed: atomic and nullable layouts.
1219   // Once those additional layouts are computed, the raw layout might need some adjustments.
1220 
1221   bool vm_uses_flattening = UseFieldFlattening || UseArrayFlattening;
1222 
1223   if (!_is_abstract_value && vm_uses_flattening) { // Flat layouts are only for concrete value classes
1224     // Validation of the non atomic layout
1225     if (UseNullFreeNonAtomicValueFlattening && (!_must_be_atomic || _is_naturally_atomic)) {
1226       _null_free_non_atomic_layout_size_in_bytes = _payload_size_in_bytes;
1227       _null_free_non_atomic_layout_alignment = _payload_alignment;
1228     }
1229 
1230     // Next step is to compute the characteristics for a layout enabling atomic updates
1231     if (UseNullFreeAtomicValueFlattening) {
1232       int atomic_size = _payload_size_in_bytes == 0 ? 0 : round_up_power_of_2(_payload_size_in_bytes);
1233       if (atomic_size <= (int)MAX_ATOMIC_OP_SIZE) {
1234         _null_free_atomic_layout_size_in_bytes = atomic_size;
1235       }
1236     }
1237 
1238     // Next step is the nullable layouts: they must include a null marker.
1239     // Note about the special case of j.l.Double and j.l.Long: the introduction of
1240     // the NULLABLE_NON_ATOMIC_FLAT layout caused an increase of the size of their
1241     // instances which causes performance regression (see JDK-8379145).
1242     // The temporary solution is to simply disable nullable layouts for these classes
1243     // until a better fix is implemented (see JDK-8382361).
1244     if ((UseNullableAtomicValueFlattening || UseNullableNonAtomicValueFlattening)
1245          && _classname != vmSymbols::java_lang_Double() && _classname != vmSymbols::java_lang_Long()) {
1246       // Looking if there's an empty slot inside the layout that could be used to store a null marker
1247       LayoutRawBlock* b = _layout->first_field_block();
1248       assert(b != nullptr, "A concrete value class must have at least one (possible dummy) field");
1249       int null_marker_offset = -1;
1250       if (_is_empty_inline_class) {
1251         // Reusing the dummy field as a field marker
1252         assert(_field_info->adr_at(b->field_index())->name(_constant_pool) == vmSymbols::empty_marker_name(), "b must be the dummy field");
1253         null_marker_offset = b->offset();
1254       } else {
1255         while (b != _layout->last_block()) {
1256           if (b->block_kind() == LayoutRawBlock::EMPTY) {
1257             break;
1258           }
1259           b = b->next_block();
1260         }
1261         if (b != _layout->last_block()) {
1262           // found an empty slot, register its offset from the beginning of the payload
1263           null_marker_offset = b->offset();
1264           LayoutRawBlock* marker = new LayoutRawBlock(LayoutRawBlock::NULL_MARKER, 1);
1265           _layout->add_field_at_offset(marker, b->offset());
1266         }
1267         if (null_marker_offset == -1) { // no empty slot available to store the null marker, need to inject one
1268           int last_offset = _layout->last_block()->offset();
1269           LayoutRawBlock* marker = new LayoutRawBlock(LayoutRawBlock::NULL_MARKER, 1);
1270           _layout->insert_field_block(_layout->last_block(), marker);
1271           assert(marker->offset() == last_offset, "Null marker should have been inserted at the end");
1272           null_marker_offset = marker->offset();
1273         }
1274       }
1275       assert(null_marker_offset != -1, "Sanity check");
1276       // Now that the null marker is there, the size of the nullable layout must be computed
1277       int new_raw_size = _layout->last_block()->offset() - _layout->first_field_block()->offset();
1278       if (UseNullableNonAtomicValueFlattening) {
1279         _nullable_non_atomic_layout_size_in_bytes = new_raw_size;
1280         _null_marker_offset = null_marker_offset;
1281         _null_free_non_atomic_layout_alignment = _payload_alignment;
1282       }
1283       if (UseNullableAtomicValueFlattening) {
1284         // For the nullable atomic layout, the size must be compatible with the platform capabilities
1285         int nullable_atomic_size = round_up_power_of_2(new_raw_size);
1286         if (nullable_atomic_size <= (int)MAX_ATOMIC_OP_SIZE) {
1287           _nullable_atomic_layout_size_in_bytes = nullable_atomic_size;
1288           _null_marker_offset = null_marker_offset;
1289         }
1290       }
1291       if (_null_marker_offset == -1) { // No nullable layout has been accepted
1292         // If the nullable layout is rejected, the NULL_MARKER block should be removed
1293         // from the layout, otherwise it will appear anyway if the layout is printer
1294         if (!_is_empty_inline_class) {  // empty values don't have a dedicated NULL_MARKER block
1295           _layout->remove_null_marker();
1296         }
1297       }
1298     }
1299     // If the inline class has an atomic or nullable atomic layout,
1300     // we want the raw layout to have the same alignment as those atomic layouts so access codes
1301     // could remain simple (single instruction without intermediate copy). This might require
1302     // shifting all fields in the raw layout, but this operation is possible only if the class
1303     // doesn't have inherited fields (offsets of inherited fields cannot be changed). If a
1304     // field shift is needed but not possible, all atomic layouts are disabled and only reference
1305     // and loosely consistent are supported.
1306     int required_alignment = _payload_alignment;
1307     if (has_null_free_atomic_layout() && required_alignment < null_free_atomic_layout_size_in_bytes()) {
1308       required_alignment = null_free_atomic_layout_size_in_bytes();
1309     }
1310     if (has_nullable_atomic_layout() && required_alignment < nullable_atomic_layout_size_in_bytes()) {
1311       required_alignment = nullable_atomic_layout_size_in_bytes();
1312     }
1313     int shift = (required_alignment - (first_field->offset() % required_alignment)) % required_alignment;
1314     if (shift != 0) {
1315       if (required_alignment > _payload_alignment && !_layout->has_inherited_fields()) {
1316         assert(_layout->first_field_block() != nullptr, "A concrete value class must have at least one (possible dummy) field");
1317         _layout->shift_fields(shift);
1318         _payload_offset = _layout->first_field_block()->offset();
1319         assert(is_aligned(_payload_offset, required_alignment), "Fields should have been shifted to respect the required alignment");
1320         if (has_nullable_atomic_layout() || has_nullable_non_atomic_layout()) {
1321           assert(!_is_empty_inline_class, "Should not get here with empty values");
1322           _null_marker_offset = _layout->find_null_marker()->offset();
1323         }
1324         _payload_alignment = required_alignment;
1325       } else {
1326         _null_free_atomic_layout_size_in_bytes = -1;
1327         if (has_nullable_atomic_layout() && !has_nullable_non_atomic_layout() && !_is_empty_inline_class) {  // empty values don't have a dedicated NULL_MARKER block
1328           _layout->remove_null_marker();
1329           _null_marker_offset = -1;
1330         }
1331         _nullable_atomic_layout_size_in_bytes = -1;
1332       }
1333     } else {
1334       _payload_alignment = required_alignment;
1335     }
1336 
1337     // If the inline class has a nullable layout, the layout used in heap allocated standalone
1338     // instances must also be the nullable layout, in order to be able to set the null marker to
1339     // non-null before copying the payload to other containers.
1340     if (has_nullable_atomic_layout() && payload_layout_size_in_bytes() < nullable_atomic_layout_size_in_bytes()) {
1341       _payload_size_in_bytes = nullable_atomic_layout_size_in_bytes();
1342     }
1343     if (has_nullable_non_atomic_layout() && payload_layout_size_in_bytes() < nullable_non_atomic_layout_size_in_bytes()) {
1344       _payload_size_in_bytes = nullable_non_atomic_layout_size_in_bytes();
1345     }
1346 
1347     // If the inline class has a null-free atomic layout, then the layout used in heap allocated standalone
1348     // instances must have at least equal to the atomic layout to allow safe read/write atomic
1349     // operation.
1350     if (has_null_free_atomic_layout() && payload_layout_size_in_bytes() < null_free_atomic_layout_size_in_bytes()) {
1351       _payload_size_in_bytes = null_free_atomic_layout_size_in_bytes();
1352     }
1353   }
1354   // Warning:: InstanceMirrorKlass expects static oops to be allocated first
1355   _static_layout->add_contiguously(_static_fields->oop_fields());
1356   _static_layout->add(_static_fields->big_primitive_fields());
1357   _static_layout->add(_static_fields->small_primitive_fields());
1358 
1359   generate_acmp_maps();
1360   epilogue();
1361 }
1362 
1363 void FieldLayoutBuilder::add_flat_field_oopmap(OopMapBlocksBuilder* nonstatic_oop_maps, InlineKlass* vklass, int offset) {
1364   int diff = offset - vklass->payload_offset();
1365   const OopMapBlock* map = vklass->start_of_nonstatic_oop_maps();
1366   const OopMapBlock* last_map = map + vklass->nonstatic_oop_map_count();
1367   while (map < last_map) {
1368     nonstatic_oop_maps->add(map->offset() + diff, map->count());
1369     map++;
1370   }
1371 }
1372 
1373 void FieldLayoutBuilder::register_embedded_oops_from_list(OopMapBlocksBuilder* nonstatic_oop_maps, GrowableArray<LayoutRawBlock*>* list) {
1374   if (list == nullptr) {
1375     return;
1376   }
1377 
1378   for (int i = 0; i < list->length(); i++) {
1379     LayoutRawBlock* f = list->at(i);
1380     if (f->block_kind() == LayoutRawBlock::FLAT) {
1381       InlineKlass* vk = f->inline_klass();
1382       assert(vk != nullptr, "Should have been initialized");
1383       if (vk->contains_oops()) {
1384         add_flat_field_oopmap(nonstatic_oop_maps, vk, f->offset());
1385       }
1386     }
1387   }
1388 }
1389 
1390 void FieldLayoutBuilder::register_embedded_oops(OopMapBlocksBuilder* nonstatic_oop_maps, FieldGroup* group) {
1391   if (group->oop_fields() != nullptr) {
1392     for (int i = 0; i < group->oop_fields()->length(); i++) {
1393       LayoutRawBlock* b = group->oop_fields()->at(i);
1394       nonstatic_oop_maps->add(b->offset(), 1);
1395     }
1396   }
1397   register_embedded_oops_from_list(nonstatic_oop_maps, group->big_primitive_fields());
1398 }
1399 
1400 static int insert_segment(GrowableArray<AcmpMapSegment>* map, int offset, int size, int last_idx) {
1401   if (map->is_empty()) {
1402     return map->append(AcmpMapSegment(offset, size));
1403   }
1404   int start = map->adr_at(last_idx)->_offset > offset ? 0 : last_idx;
1405   bool inserted = false;
1406   for (int c = start; c < map->length(); c++) {
1407     if (offset == (map->adr_at(c)->_offset + map->adr_at(c)->_size)) {
1408       //contiguous to the last field, can be coalesced
1409       map->adr_at(c)->_size = map->adr_at(c)->_size + size;
1410       inserted = true;
1411       break;  // break out of the for loop
1412     }
1413     if (offset < (map->adr_at(c)->_offset)) {
1414       map->insert_before(c, AcmpMapSegment(offset, size));
1415       last_idx = c;
1416       inserted = true;
1417       break;  // break out of the for loop
1418     }
1419   }
1420   if (!inserted) {
1421     last_idx = map->append(AcmpMapSegment(offset, size));
1422   }
1423   return last_idx;
1424 }
1425 
1426 static int insert_map_at_offset(GrowableArray<AcmpMapSegment>* nonoop_map, GrowableArray<int>* oop_map,
1427                                 const InstanceKlass* ik, int field_offset, int last_idx) {
1428   Array<int>* super_map = ik->acmp_maps_array();
1429   assert(super_map != nullptr, "super class must have an acmp map");
1430   int num_nonoop_field = super_map->at(0);
1431   for (int i = 0; i < num_nonoop_field; i++) {
1432     last_idx = insert_segment(nonoop_map,
1433                               field_offset + super_map->at( i * 2 + 1),
1434                               super_map->at( i * 2 + 2), last_idx);
1435   }
1436   int len = super_map->length();
1437   for (int i = num_nonoop_field * 2 + 1; i < len; i++) {
1438       oop_map->append(field_offset + super_map->at(i));
1439   }
1440   return last_idx;
1441 }
1442 
1443 static void split_after(GrowableArray<AcmpMapSegment>* map, int idx, int head) {
1444   int offset = map->adr_at(idx)->_offset;
1445   int size = map->adr_at(idx)->_size;
1446   if (size <= head) return;
1447   map->adr_at(idx)->_offset = offset + head;
1448   map->adr_at(idx)->_size = size - head;
1449   map->insert_before(idx, AcmpMapSegment(offset, head));
1450 
1451 }
1452 
1453 void FieldLayoutBuilder::generate_acmp_maps() {
1454   assert(_is_inline_type || _is_abstract_value, "Must be done only for value classes (abstract or not)");
1455 
1456   // create/initialize current class' maps
1457   _nonoop_acmp_map = new GrowableArray<AcmpMapSegment>();
1458   _oop_acmp_map = new GrowableArray<int>();
1459   if (_is_empty_inline_class) return;
1460   // last_idx remembers the position of the last insertion in order to speed up the next insertion.
1461   // Local fields are processed in ascending offset order, so an insertion is very likely be performed
1462   // next to the previous insertion. However, in some cases local fields and inherited fields can be
1463   // interleaved, in which case the search of the insertion position cannot depend on the previous insertion.
1464   int last_idx = 0;
1465   if (_super_klass != nullptr && _super_klass != vmClasses::Object_klass()) {  // Assumes j.l.Object cannot have fields
1466     last_idx = insert_map_at_offset(_nonoop_acmp_map, _oop_acmp_map, _super_klass, 0, last_idx);
1467   }
1468 
1469   // Processing local fields
1470   LayoutRawBlock* b = _layout->blocks();
1471   while(b != _layout->last_block()) {
1472     switch(b->block_kind()) {
1473       case LayoutRawBlock::RESERVED:
1474       case LayoutRawBlock::EMPTY:
1475       case LayoutRawBlock::PADDING:
1476       case LayoutRawBlock::NULL_MARKER:
1477       case LayoutRawBlock::INHERITED: // inherited fields are handled during maps creation/initialization
1478         // skip
1479         break;
1480 
1481       case LayoutRawBlock::REGULAR:
1482         {
1483           FieldInfo* fi = _field_info->adr_at(b->field_index());
1484           if (fi->signature(_constant_pool)->starts_with("L") || fi->signature(_constant_pool)->starts_with("[")) {
1485             _oop_acmp_map->append(b->offset());
1486           } else {
1487             // Non-oop case
1488             last_idx = insert_segment(_nonoop_acmp_map, b->offset(), b->size(), last_idx);
1489           }
1490           break;
1491        }
1492       case LayoutRawBlock::FLAT:
1493         {
1494           InlineKlass* vk = b->inline_klass();
1495           int field_offset = b->offset() - vk->payload_offset();
1496           last_idx = insert_map_at_offset(_nonoop_acmp_map, _oop_acmp_map, vk, field_offset, last_idx);
1497           if (LayoutKindHelper::is_nullable_flat(b->layout_kind())) {
1498             int null_marker_offset = b->offset() + vk->null_marker_offset_in_payload();
1499             last_idx = insert_segment(_nonoop_acmp_map, null_marker_offset, 1, last_idx);
1500             // Important note: the implementation assumes that for nullable flat fields, if the
1501             // null marker is zero (field is null), then all the fields of the flat field are also
1502             // zeroed. So, nullable flat field are not encoded different than null-free flat fields,
1503             // all fields are included in the map, plus the null marker.
1504             // If it happens that the assumption above is wrong, then nullable flat fields would
1505             // require a dedicated section in the acmp map, and be handled differently: null_marker
1506             // comparison first, and if null markers are identical and non-zero, then conditional
1507             // comparison of the other fields.
1508           }
1509         }
1510         break;
1511 
1512     }
1513     b = b->next_block();
1514   }
1515 
1516   // split segments into well-aligned blocks
1517   int idx = 0;
1518   while (idx < _nonoop_acmp_map->length()) {
1519     int offset = _nonoop_acmp_map->adr_at(idx)->_offset;
1520     int size = _nonoop_acmp_map->adr_at(idx)->_size;
1521     int mod = offset % 8;
1522     switch (mod) {
1523       case 0:
1524         break;
1525       case 4:
1526         split_after(_nonoop_acmp_map, idx, 4);
1527         break;
1528       case 2:
1529       case 6:
1530         split_after(_nonoop_acmp_map, idx, 2);
1531         break;
1532       case 1:
1533       case 3:
1534       case 5:
1535       case 7:
1536         split_after(_nonoop_acmp_map, idx, 1);
1537         break;
1538       default:
1539         ShouldNotReachHere();
1540     }
1541     idx++;
1542   }
1543 }
1544 
1545 void FieldLayoutBuilder::epilogue() {
1546   // Computing oopmaps
1547   OopMapBlocksBuilder* nonstatic_oop_maps =
1548       new OopMapBlocksBuilder(_nonstatic_oopmap_count);
1549   int super_oop_map_count = (_super_klass == nullptr) ? 0 :_super_klass->nonstatic_oop_map_count();
1550   if (super_oop_map_count > 0) {
1551     nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(),
1552     _super_klass->nonstatic_oop_map_count());
1553   }
1554   register_embedded_oops(nonstatic_oop_maps, _root_group);
1555   if (!_contended_groups.is_empty()) {
1556     for (int i = 0; i < _contended_groups.length(); i++) {
1557       FieldGroup* cg = _contended_groups.at(i);
1558       register_embedded_oops(nonstatic_oop_maps, cg);
1559     }
1560   }
1561   nonstatic_oop_maps->compact();
1562 
1563   int instance_end = align_up(_layout->last_block()->offset(), wordSize);
1564   int static_fields_end = align_up(_static_layout->last_block()->offset(), wordSize);
1565   int static_fields_size = (static_fields_end -
1566       InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
1567   int nonstatic_field_end = align_up(_layout->last_block()->offset(), heapOopSize);
1568 
1569   // Pass back information needed for InstanceKlass creation
1570 
1571   _info->oop_map_blocks = nonstatic_oop_maps;
1572   _info->_instance_size = align_object_size(instance_end / wordSize);
1573   _info->_static_field_size = static_fields_size;
1574   _info->_nonstatic_field_size = (nonstatic_field_end - instanceOopDesc::base_offset_in_bytes()) / heapOopSize;
1575   _info->_has_nonstatic_fields = _has_nonstatic_fields;
1576   _info->_has_inlined_fields = _has_inlined_fields;
1577   _info->_is_naturally_atomic = _is_naturally_atomic;
1578   if (_is_inline_type) {
1579     _info->_must_be_atomic = _must_be_atomic;
1580     _info->_payload_alignment = _payload_alignment;
1581     _info->_payload_offset = _payload_offset;
1582     _info->_payload_size_in_bytes = _payload_size_in_bytes;
1583     _info->_null_free_non_atomic_size_in_bytes = _null_free_non_atomic_layout_size_in_bytes;
1584     _info->_null_free_non_atomic_alignment = _null_free_non_atomic_layout_alignment;
1585     _info->_null_free_atomic_layout_size_in_bytes = _null_free_atomic_layout_size_in_bytes;
1586     _info->_nullable_atomic_layout_size_in_bytes = _nullable_atomic_layout_size_in_bytes;
1587     _info->_nullable_non_atomic_layout_size_in_bytes = _nullable_non_atomic_layout_size_in_bytes;
1588     _info->_null_marker_offset = _null_marker_offset;
1589     _info->_null_reset_value_offset = _static_layout->null_reset_value_offset();
1590     _info->_is_empty_inline_klass = _is_empty_inline_class;
1591   }
1592 
1593   // Acmp maps are needed for both concrete and abstract value classes
1594   if (_is_inline_type || _is_abstract_value) {
1595     _info->_acmp_maps_offset = _static_layout->acmp_maps_offset();
1596     _info->_nonoop_acmp_map = _nonoop_acmp_map;
1597     _info->_oop_acmp_map = _oop_acmp_map;
1598   }
1599 
1600   // This may be too restrictive, since if all the fields fit in 64
1601   // bits we could make the decision to align instances of this class
1602   // to 64-bit boundaries, and load and store them as single words.
1603   // And on machines which supported larger atomics we could similarly
1604   // allow larger values to be atomic, if properly aligned.
1605 
1606 #ifdef ASSERT
1607   // Tests verifying integrity of field layouts are using the output of -XX:+PrintFieldLayout
1608   // which prints the details of LayoutRawBlocks used to compute the layout.
1609   // The code below checks that offsets in the _field_info meta-data match offsets
1610   // in the LayoutRawBlocks.
1611   LayoutRawBlock* b = _layout->blocks();
1612   while(b != _layout->last_block()) {
1613     if (b->block_kind() == LayoutRawBlock::REGULAR || b->block_kind() == LayoutRawBlock::FLAT) {
1614       if (_field_info->adr_at(b->field_index())->offset() != (u4)b->offset()) {
1615         tty->print_cr("Offset from field info = %d, offset from block = %d", (int)_field_info->adr_at(b->field_index())->offset(), b->offset());
1616       }
1617       assert(_field_info->adr_at(b->field_index())->offset() == (u4)b->offset()," Must match");
1618     }
1619     b = b->next_block();
1620   }
1621   b = _static_layout->blocks();
1622   while(b != _static_layout->last_block()) {
1623     if (b->block_kind() == LayoutRawBlock::REGULAR || b->block_kind() == LayoutRawBlock::FLAT) {
1624       assert(_field_info->adr_at(b->field_index())->offset() == (u4)b->offset()," Must match");
1625     }
1626     b = b->next_block();
1627   }
1628 #endif // ASSERT
1629 
1630   static bool first_layout_print = true;
1631 
1632   if (PrintFieldLayout || (PrintInlineLayout && (_has_inlineable_fields || _is_inline_type || _is_abstract_value))) {
1633     ResourceMark rm;
1634     stringStream st;
1635     if (first_layout_print) {
1636       st.print_cr("Field layout log format: @offset size/alignment [name] [signature] [comment]");
1637       st.print_cr("Heap oop size = %d", heapOopSize);
1638       first_layout_print = false;
1639     }
1640     if (_super_klass != nullptr) {
1641       st.print_cr("Layout of class %s@%p extends %s@%p", _classname->as_C_string(),
1642                     _loader_data, _super_klass->name()->as_C_string(), _super_klass->class_loader_data());
1643     } else {
1644       st.print_cr("Layout of class %s@%p", _classname->as_C_string(), _loader_data);
1645     }
1646     st.print_cr("Instance fields:");
1647     const bool dummy_field_is_reused_as_null_marker = _is_empty_inline_class && _null_marker_offset != -1;
1648     _layout->print(&st, false, _super_klass, _inline_layout_info_array, dummy_field_is_reused_as_null_marker);
1649     st.print_cr("Static fields:");
1650     _static_layout->print(&st, true, nullptr, _inline_layout_info_array, false);
1651     st.print_cr("Instance size = %d bytes", _info->_instance_size * wordSize);
1652     if (_is_inline_type) {
1653       st.print_cr("First field offset = %d", _payload_offset);
1654       st.print_cr("%s layout: %d/%d", LayoutKindHelper::layout_kind_as_string(LayoutKind::BUFFERED),
1655                   _payload_size_in_bytes, _payload_alignment);
1656       if (has_null_free_non_atomic_flat_layout()) {
1657         st.print_cr("%s layout: %d/%d",
1658                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULL_FREE_NON_ATOMIC_FLAT),
1659                     _null_free_non_atomic_layout_size_in_bytes, _null_free_non_atomic_layout_alignment);
1660       } else {
1661         st.print_cr("%s layout: -/-",
1662                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULL_FREE_NON_ATOMIC_FLAT));
1663       }
1664       if (has_null_free_atomic_layout()) {
1665         st.print_cr("%s layout: %d/%d",
1666                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULL_FREE_ATOMIC_FLAT),
1667                     _null_free_atomic_layout_size_in_bytes, _null_free_atomic_layout_size_in_bytes);
1668       } else {
1669         st.print_cr("%s layout: -/-",
1670                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULL_FREE_ATOMIC_FLAT));
1671       }
1672       if (has_nullable_atomic_layout()) {
1673         st.print_cr("%s layout: %d/%d",
1674                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULLABLE_ATOMIC_FLAT),
1675                     _nullable_atomic_layout_size_in_bytes, _nullable_atomic_layout_size_in_bytes);
1676       } else {
1677         st.print_cr("%s layout: -/-",
1678                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULLABLE_ATOMIC_FLAT));
1679       }
1680       if (has_nullable_non_atomic_layout()) {
1681         st.print_cr("%s layout: %d/%d",
1682                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULLABLE_NON_ATOMIC_FLAT),
1683                     _nullable_non_atomic_layout_size_in_bytes, _null_free_non_atomic_layout_alignment);
1684       } else {
1685         st.print_cr("%s layout: -/-",
1686                     LayoutKindHelper::layout_kind_as_string(LayoutKind::NULLABLE_NON_ATOMIC_FLAT));
1687       }
1688       if (_null_marker_offset != -1) {
1689         st.print_cr("Null marker offset = %d", _null_marker_offset);
1690       }
1691       st.print("Non-oop acmp map <offset,size>: ");
1692       for (int i = 0 ; i < _nonoop_acmp_map->length(); i++) {
1693         st.print("<%d,%d> ", _nonoop_acmp_map->at(i)._offset,  _nonoop_acmp_map->at(i)._size);
1694       }
1695       st.print_cr("");
1696       st.print("oop acmp map: ");
1697       for (int i = 0 ; i < _oop_acmp_map->length(); i++) {
1698         st.print("%d ", _oop_acmp_map->at(i));
1699       }
1700       st.print_cr("");
1701     }
1702     st.print_cr("---");
1703     // Print output all together.
1704     tty->print_raw(st.as_string());
1705   }
1706 }
1707 
1708 void FieldLayoutBuilder::build_layout() {
1709   if (_is_inline_type || _is_abstract_value) {
1710     compute_inline_class_layout();
1711   } else {
1712     compute_regular_layout();
1713   }
1714 }