1 /*
  2  * Copyright (c) 2020, 2024, 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 "precompiled.hpp"
 26 #include "classfile/classFileParser.hpp"
 27 #include "classfile/fieldLayoutBuilder.hpp"
 28 #include "jvm.h"
 29 #include "memory/resourceArea.hpp"
 30 #include "oops/array.hpp"
 31 #include "oops/fieldStreams.inline.hpp"
 32 #include "oops/instanceMirrorKlass.hpp"
 33 #include "oops/instanceKlass.inline.hpp"
 34 #include "oops/klass.inline.hpp"
 35 #include "runtime/fieldDescriptor.inline.hpp"
 36 
 37 
 38 LayoutRawBlock::LayoutRawBlock(Kind kind, int size) :
 39   _next_block(nullptr),
 40   _prev_block(nullptr),
 41   _kind(kind),
 42   _offset(-1),
 43   _alignment(1),
 44   _size(size),
 45   _field_index(-1),
 46   _is_reference(false) {
 47   assert(kind == EMPTY || kind == RESERVED || kind == PADDING || kind == INHERITED,
 48          "Otherwise, should use the constructor with a field index argument");
 49   assert(size > 0, "Sanity check");
 50 }
 51 
 52 
 53 LayoutRawBlock::LayoutRawBlock(int index, Kind kind, int size, int alignment, bool is_reference) :
 54  _next_block(nullptr),
 55  _prev_block(nullptr),
 56  _kind(kind),
 57  _offset(-1),
 58  _alignment(alignment),
 59  _size(size),
 60  _field_index(index),
 61  _is_reference(is_reference) {
 62   assert(kind == REGULAR || kind == FLATTENED || kind == INHERITED,
 63          "Other kind do not have a field index");
 64   assert(size > 0, "Sanity check");
 65   assert(alignment > 0, "Sanity check");
 66 }
 67 
 68 bool LayoutRawBlock::fit(int size, int alignment) {
 69   int adjustment = 0;
 70   if ((_offset % alignment) != 0) {
 71     adjustment = alignment - (_offset % alignment);
 72   }
 73   return _size >= size + adjustment;
 74 }
 75 
 76 FieldGroup::FieldGroup(int contended_group) :
 77   _next(nullptr),
 78   _primitive_fields(nullptr),
 79   _oop_fields(nullptr),
 80   _contended_group(contended_group),  // -1 means no contended group, 0 means default contended group
 81   _oop_count(0) {}
 82 
 83 void FieldGroup::add_primitive_field(int idx, BasicType type) {
 84   int size = type2aelembytes(type);
 85   LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for primitive types */, false);
 86   if (_primitive_fields == nullptr) {
 87     _primitive_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE);
 88   }
 89   _primitive_fields->append(block);
 90 }
 91 
 92 void FieldGroup::add_oop_field(int idx) {
 93   int size = type2aelembytes(T_OBJECT);
 94   LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for oops */, true);
 95   if (_oop_fields == nullptr) {
 96     _oop_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE);
 97   }
 98   _oop_fields->append(block);
 99   _oop_count++;
100 }
101 
102 void FieldGroup::sort_by_size() {
103   if (_primitive_fields != nullptr) {
104     _primitive_fields->sort(LayoutRawBlock::compare_size_inverted);
105   }
106 }
107 
108 FieldLayout::FieldLayout(GrowableArray<FieldInfo>* field_info, ConstantPool* cp) :
109   _field_info(field_info),
110   _cp(cp),
111   _blocks(nullptr),
112   _start(_blocks),
113   _last(_blocks) {}
114 
115 void FieldLayout::initialize_static_layout() {
116   _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
117   _blocks->set_offset(0);
118   _last = _blocks;
119   _start = _blocks;
120   // Note: at this stage, InstanceMirrorKlass::offset_of_static_fields() could be zero, because
121   // during bootstrapping, the size of the java.lang.Class is still not known when layout
122   // of static field is computed. Field offsets are fixed later when the size is known
123   // (see java_lang_Class::fixup_mirror())
124   if (InstanceMirrorKlass::offset_of_static_fields() > 0) {
125     insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, InstanceMirrorKlass::offset_of_static_fields()));
126     _blocks->set_offset(0);
127   }
128 }
129 
130 void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass) {
131   if (super_klass == nullptr) {
132     _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
133     _blocks->set_offset(0);
134     _last = _blocks;
135     _start = _blocks;
136     insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes()));
137   } else {
138     bool has_fields = reconstruct_layout(super_klass);
139     fill_holes(super_klass);
140     if (!super_klass->has_contended_annotations() || !has_fields) {
141       _start = _blocks;  // start allocating fields from the first empty block
142     } else {
143       _start = _last;    // append fields at the end of the reconstructed layout
144     }
145   }
146 }
147 
148 LayoutRawBlock* FieldLayout::first_field_block() {
149   LayoutRawBlock* block = _start;
150   while (block->kind() != LayoutRawBlock::INHERITED && block->kind() != LayoutRawBlock::REGULAR
151       && block->kind() != LayoutRawBlock::FLATTENED && block->kind() != LayoutRawBlock::PADDING) {
152     block = block->next_block();
153   }
154   return block;
155 }
156 
157 
158 // Insert a set of fields into a layout using a best-fit strategy.
159 // For each field, search for the smallest empty slot able to fit the field
160 // (satisfying both size and alignment requirements), if none is found,
161 // add the field at the end of the layout.
162 // Fields cannot be inserted before the block specified in the "start" argument
163 void FieldLayout::add(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) {
164   if (list == nullptr) return;
165   if (start == nullptr) start = this->_start;
166   bool last_search_success = false;
167   int last_size = 0;
168   int last_alignment = 0;
169   for (int i = 0; i < list->length(); i ++) {
170     LayoutRawBlock* b = list->at(i);
171     LayoutRawBlock* cursor = nullptr;
172     LayoutRawBlock* candidate = nullptr;
173 
174     // if start is the last block, just append the field
175     if (start == last_block()) {
176       candidate = last_block();
177     }
178     // Before iterating over the layout to find an empty slot fitting the field's requirements,
179     // check if the previous field had the same requirements and if the search for a fitting slot
180     // was successful. If the requirements were the same but the search failed, a new search will
181     // fail the same way, so just append the field at the of the layout.
182     else  if (b->size() == last_size && b->alignment() == last_alignment && !last_search_success) {
183       candidate = last_block();
184     } else {
185       // Iterate over the layout to find an empty slot fitting the field's requirements
186       last_size = b->size();
187       last_alignment = b->alignment();
188       cursor = last_block()->prev_block();
189       assert(cursor != nullptr, "Sanity check");
190       last_search_success = true;
191       while (cursor != start) {
192         if (cursor->kind() == LayoutRawBlock::EMPTY && cursor->fit(b->size(), b->alignment())) {
193           if (candidate == nullptr || cursor->size() < candidate->size()) {
194             candidate = cursor;
195           }
196         }
197         cursor = cursor->prev_block();
198       }
199       if (candidate == nullptr) {
200         candidate = last_block();
201         last_search_success = false;
202       }
203       assert(candidate != nullptr, "Candidate must not be null");
204       assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block");
205       assert(candidate->fit(b->size(), b->alignment()), "Candidate must be able to store the block");
206     }
207 
208     insert_field_block(candidate, b);
209   }
210 }
211 
212 // Finds a slot for the identity hash-code.
213 // Same basic algorithm as above add() method, but simplified
214 // and does not actually insert the field.
215 int FieldLayout::find_hash_offset() {
216   LayoutRawBlock* start = this->_start;
217   LayoutRawBlock* last = last_block();
218   LayoutRawBlock* cursor = start;
219   while (cursor != last) {
220     assert(cursor != nullptr, "Sanity check");
221     if (cursor->kind() == LayoutRawBlock::EMPTY && cursor->fit(4, 1)) {
222       break;
223     }
224     cursor = cursor->next_block();
225   }
226   return cursor->offset();
227 }
228 
229 // Used for classes with hard coded field offsets, insert a field at the specified offset */
230 void FieldLayout::add_field_at_offset(LayoutRawBlock* block, int offset, LayoutRawBlock* start) {
231   assert(block != nullptr, "Sanity check");
232   block->set_offset(offset);
233   if (start == nullptr) {
234     start = this->_start;
235   }
236   LayoutRawBlock* slot = start;
237   while (slot != nullptr) {
238     if ((slot->offset() <= block->offset() && (slot->offset() + slot->size()) > block->offset()) ||
239         slot == _last){
240       assert(slot->kind() == LayoutRawBlock::EMPTY, "Matching slot must be an empty slot");
241       assert(slot->size() >= block->offset() + block->size() ,"Matching slot must be big enough");
242       if (slot->offset() < block->offset()) {
243         int adjustment = block->offset() - slot->offset();
244         LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment);
245         insert(slot, adj);
246       }
247       insert(slot, block);
248       if (slot->size() == 0) {
249         remove(slot);
250       }
251       _field_info->adr_at(block->field_index())->set_offset(block->offset());
252       return;
253     }
254     slot = slot->next_block();
255   }
256   fatal("Should have found a matching slot above, corrupted layout or invalid offset");
257 }
258 
259 // The allocation logic uses a best fit strategy: the set of fields is allocated
260 // in the first empty slot big enough to contain the whole set ((including padding
261 // to fit alignment constraints).
262 void FieldLayout::add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) {
263   if (list == nullptr) return;
264   if (start == nullptr) {
265     start = _start;
266   }
267   // This code assumes that if the first block is well aligned, the following
268   // blocks would naturally be well aligned (no need for adjustment)
269   int size = 0;
270   for (int i = 0; i < list->length(); i++) {
271     size += list->at(i)->size();
272   }
273 
274   LayoutRawBlock* candidate = nullptr;
275   if (start == last_block()) {
276     candidate = last_block();
277   } else {
278     LayoutRawBlock* first = list->at(0);
279     candidate = last_block()->prev_block();
280     while (candidate->kind() != LayoutRawBlock::EMPTY || !candidate->fit(size, first->alignment())) {
281       if (candidate == start) {
282         candidate = last_block();
283         break;
284       }
285       candidate = candidate->prev_block();
286     }
287     assert(candidate != nullptr, "Candidate must not be null");
288     assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block");
289     assert(candidate->fit(size, first->alignment()), "Candidate must be able to store the whole contiguous block");
290   }
291 
292   for (int i = 0; i < list->length(); i++) {
293     LayoutRawBlock* b = list->at(i);
294     insert_field_block(candidate, b);
295     assert((candidate->offset() % b->alignment() == 0), "Contiguous blocks must be naturally well aligned");
296   }
297 }
298 
299 LayoutRawBlock* FieldLayout::insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block) {
300   assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks");
301   if (slot->offset() % block->alignment() != 0) {
302     int adjustment = block->alignment() - (slot->offset() % block->alignment());
303     LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment);
304     insert(slot, adj);
305   }
306   insert(slot, block);
307   if (slot->size() == 0) {
308     remove(slot);
309   }
310   _field_info->adr_at(block->field_index())->set_offset(block->offset());
311   return block;
312 }
313 
314 bool FieldLayout::reconstruct_layout(const InstanceKlass* ik) {
315   bool has_instance_fields = false;
316   GrowableArray<LayoutRawBlock*>* all_fields = new GrowableArray<LayoutRawBlock*>(32);
317   while (ik != nullptr) {
318     for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) {
319       BasicType type = Signature::basic_type(fs.signature());
320       // distinction between static and non-static fields is missing
321       if (fs.access_flags().is_static()) continue;
322       has_instance_fields = true;
323       int size = type2aelembytes(type);
324       // INHERITED blocks are marked as non-reference because oop_maps are handled by their holder class
325       LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED, size, size, false);
326       block->set_offset(fs.offset());
327       all_fields->append(block);
328     }
329     ik = ik->super() == nullptr ? nullptr : InstanceKlass::cast(ik->super());
330   }
331 
332   all_fields->sort(LayoutRawBlock::compare_offset);
333   _blocks = new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes());
334   _blocks->set_offset(0);
335   _last = _blocks;
336 
337   for(int i = 0; i < all_fields->length(); i++) {
338     LayoutRawBlock* b = all_fields->at(i);
339     _last->set_next_block(b);
340     b->set_prev_block(_last);
341     _last = b;
342   }
343   _start = _blocks;
344   return has_instance_fields;
345 }
346 
347 // Called during the reconstruction of a layout, after fields from super
348 // classes have been inserted. It fills unused slots between inserted fields
349 // with EMPTY blocks, so the regular field insertion methods would work.
350 // This method handles classes with @Contended annotations differently
351 // by inserting PADDING blocks instead of EMPTY block to prevent subclasses'
352 // fields to interfere with contended fields/classes.
353 void FieldLayout::fill_holes(const InstanceKlass* super_klass) {
354   assert(_blocks != nullptr, "Sanity check");
355   assert(_blocks->offset() == 0, "first block must be at offset zero");
356   LayoutRawBlock::Kind filling_type = super_klass->has_contended_annotations() ? LayoutRawBlock::PADDING: LayoutRawBlock::EMPTY;
357   LayoutRawBlock* b = _blocks;
358   while (b->next_block() != nullptr) {
359     if (b->next_block()->offset() > (b->offset() + b->size())) {
360       int size = b->next_block()->offset() - (b->offset() + b->size());
361       LayoutRawBlock* empty = new LayoutRawBlock(filling_type, size);
362       empty->set_offset(b->offset() + b->size());
363       empty->set_next_block(b->next_block());
364       b->next_block()->set_prev_block(empty);
365       b->set_next_block(empty);
366       empty->set_prev_block(b);
367     }
368     b = b->next_block();
369   }
370   assert(b->next_block() == nullptr, "Invariant at this point");
371   assert(b->kind() != LayoutRawBlock::EMPTY, "Sanity check");
372 
373   // If the super class has @Contended annotation, a padding block is
374   // inserted at the end to ensure that fields from the subclasses won't share
375   // the cache line of the last field of the contended class
376   if (super_klass->has_contended_annotations() && ContendedPaddingWidth > 0) {
377     LayoutRawBlock* p = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth);
378     p->set_offset(b->offset() + b->size());
379     b->set_next_block(p);
380     p->set_prev_block(b);
381     b = p;
382   }
383 
384   LayoutRawBlock* last = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
385   last->set_offset(b->offset() + b->size());
386   assert(last->offset() > 0, "Sanity check");
387   b->set_next_block(last);
388   last->set_prev_block(b);
389   _last = last;
390 }
391 
392 LayoutRawBlock* FieldLayout::insert(LayoutRawBlock* slot, LayoutRawBlock* block) {
393   assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks");
394   assert(slot->offset() % block->alignment() == 0, "Incompatible alignment");
395   block->set_offset(slot->offset());
396   slot->set_offset(slot->offset() + block->size());
397   assert((slot->size() - block->size()) < slot->size(), "underflow checking");
398   assert(slot->size() - block->size() >= 0, "no negative size allowed");
399   slot->set_size(slot->size() - block->size());
400   block->set_prev_block(slot->prev_block());
401   block->set_next_block(slot);
402   slot->set_prev_block(block);
403   if (block->prev_block() != nullptr) {
404     block->prev_block()->set_next_block(block);
405   }
406   if (_blocks == slot) {
407     _blocks = block;
408   }
409   return block;
410 }
411 
412 void FieldLayout::remove(LayoutRawBlock* block) {
413   assert(block != nullptr, "Sanity check");
414   assert(block != _last, "Sanity check");
415   if (_blocks == block) {
416     _blocks = block->next_block();
417     if (_blocks != nullptr) {
418       _blocks->set_prev_block(nullptr);
419     }
420   } else {
421     assert(block->prev_block() != nullptr, "_prev should be set for non-head blocks");
422     block->prev_block()->set_next_block(block->next_block());
423     block->next_block()->set_prev_block(block->prev_block());
424   }
425   if (block == _start) {
426     _start = block->prev_block();
427   }
428 }
429 
430 void FieldLayout::print(outputStream* output, bool is_static, const InstanceKlass* super) {
431   ResourceMark rm;
432   LayoutRawBlock* b = _blocks;
433   while(b != _last) {
434     switch(b->kind()) {
435       case LayoutRawBlock::REGULAR: {
436         FieldInfo* fi = _field_info->adr_at(b->field_index());
437         output->print_cr(" @%d \"%s\" %s %d/%d %s",
438                          b->offset(),
439                          fi->name(_cp)->as_C_string(),
440                          fi->signature(_cp)->as_C_string(),
441                          b->size(),
442                          b->alignment(),
443                          "REGULAR");
444         break;
445       }
446       case LayoutRawBlock::FLATTENED: {
447         FieldInfo* fi = _field_info->adr_at(b->field_index());
448         output->print_cr(" @%d \"%s\" %s %d/%d %s",
449                          b->offset(),
450                          fi->name(_cp)->as_C_string(),
451                          fi->signature(_cp)->as_C_string(),
452                          b->size(),
453                          b->alignment(),
454                          "FLATTENED");
455         break;
456       }
457       case LayoutRawBlock::RESERVED: {
458         output->print_cr(" @%d %d/- %s",
459                          b->offset(),
460                          b->size(),
461                          "RESERVED");
462         break;
463       }
464       case LayoutRawBlock::INHERITED: {
465         assert(!is_static, "Static fields are not inherited in layouts");
466         assert(super != nullptr, "super klass must be provided to retrieve inherited fields info");
467         bool found = false;
468         const InstanceKlass* ik = super;
469         while (!found && ik != nullptr) {
470           for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) {
471             if (fs.offset() == b->offset()) {
472               output->print_cr(" @%d \"%s\" %s %d/%d %s",
473                   b->offset(),
474                   fs.name()->as_C_string(),
475                   fs.signature()->as_C_string(),
476                   b->size(),
477                   b->size(), // so far, alignment constraint == size, will change with Valhalla
478                   "INHERITED");
479               found = true;
480               break;
481             }
482           }
483           ik = ik->java_super();
484         }
485         break;
486       }
487       case LayoutRawBlock::EMPTY:
488         output->print_cr(" @%d %d/1 %s",
489                          b->offset(),
490                          b->size(),
491                         "EMPTY");
492         break;
493       case LayoutRawBlock::PADDING:
494         output->print_cr(" @%d %d/1 %s",
495                          b->offset(),
496                          b->size(),
497                         "PADDING");
498         break;
499     }
500     b = b->next_block();
501   }
502 }
503 
504 FieldLayoutBuilder::FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool,
505       GrowableArray<FieldInfo>* field_info, bool is_contended, FieldLayoutInfo* info) :
506   _classname(classname),
507   _super_klass(super_klass),
508   _constant_pool(constant_pool),
509   _field_info(field_info),
510   _info(info),
511   _root_group(nullptr),
512   _contended_groups(GrowableArray<FieldGroup*>(8)),
513   _static_fields(nullptr),
514   _layout(nullptr),
515   _static_layout(nullptr),
516   _nonstatic_oopmap_count(0),
517   _alignment(-1),
518   _has_nonstatic_fields(false),
519   _is_contended(is_contended) {}
520 
521 
522 FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) {
523   assert(g > 0, "must only be called for named contended groups");
524   FieldGroup* fg = nullptr;
525   for (int i = 0; i < _contended_groups.length(); i++) {
526     fg = _contended_groups.at(i);
527     if (fg->contended_group() == g) return fg;
528   }
529   fg = new FieldGroup(g);
530   _contended_groups.append(fg);
531   return fg;
532 }
533 
534 void FieldLayoutBuilder::prologue() {
535   _layout = new FieldLayout(_field_info, _constant_pool);
536   const InstanceKlass* super_klass = _super_klass;
537   _layout->initialize_instance_layout(super_klass);
538   if (super_klass != nullptr) {
539     _has_nonstatic_fields = super_klass->has_nonstatic_fields();
540   }
541   _static_layout = new FieldLayout(_field_info, _constant_pool);
542   _static_layout->initialize_static_layout();
543   _static_fields = new FieldGroup();
544   _root_group = new FieldGroup();
545 }
546 
547 // Field sorting for regular classes:
548 //   - fields are sorted in static and non-static fields
549 //   - non-static fields are also sorted according to their contention group
550 //     (support of the @Contended annotation)
551 //   - @Contended annotation is ignored for static fields
552 void FieldLayoutBuilder::regular_field_sorting() {
553   int idx = 0;
554   for (GrowableArrayIterator<FieldInfo> it = _field_info->begin(); it != _field_info->end(); ++it, ++idx) {
555     FieldInfo ctrl = _field_info->at(0);
556     FieldGroup* group = nullptr;
557     FieldInfo fieldinfo = *it;
558     if (fieldinfo.access_flags().is_static()) {
559       group = _static_fields;
560     } else {
561       _has_nonstatic_fields = true;
562       if (fieldinfo.field_flags().is_contended()) {
563         int g = fieldinfo.contended_group();
564         if (g == 0) {
565           group = new FieldGroup(true);
566           _contended_groups.append(group);
567         } else {
568           group = get_or_create_contended_group(g);
569         }
570       } else {
571         group = _root_group;
572       }
573     }
574     assert(group != nullptr, "invariant");
575     BasicType type = Signature::basic_type(fieldinfo.signature(_constant_pool));
576     switch(type) {
577       case T_BYTE:
578       case T_CHAR:
579       case T_DOUBLE:
580       case T_FLOAT:
581       case T_INT:
582       case T_LONG:
583       case T_SHORT:
584       case T_BOOLEAN:
585         group->add_primitive_field(idx, type);
586         break;
587       case T_OBJECT:
588       case T_ARRAY:
589         if (group != _static_fields) _nonstatic_oopmap_count++;
590         group->add_oop_field(idx);
591         break;
592       default:
593         fatal("Something wrong?");
594     }
595   }
596   _root_group->sort_by_size();
597   _static_fields->sort_by_size();
598   if (!_contended_groups.is_empty()) {
599     for (int i = 0; i < _contended_groups.length(); i++) {
600       _contended_groups.at(i)->sort_by_size();
601     }
602   }
603 }
604 
605 void FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) {
606   if (ContendedPaddingWidth > 0) {
607     LayoutRawBlock* padding = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth);
608     _layout->insert(slot, padding);
609   }
610 }
611 
612 // Computation of regular classes layout is an evolution of the previous default layout
613 // (FieldAllocationStyle 1):
614 //   - primitive fields are allocated first (from the biggest to the smallest)
615 //   - then oop fields are allocated, either in existing gaps or at the end of
616 //     the layout
617 void FieldLayoutBuilder::compute_regular_layout() {
618   bool need_tail_padding = false;
619   prologue();
620   regular_field_sorting();
621 
622   if (_is_contended) {
623     _layout->set_start(_layout->last_block());
624     // insertion is currently easy because the current strategy doesn't try to fill holes
625     // in super classes layouts => the _start block is by consequence the _last_block
626     insert_contended_padding(_layout->start());
627     need_tail_padding = true;
628   }
629   _layout->add(_root_group->primitive_fields());
630   _layout->add(_root_group->oop_fields());
631 
632   if (!_contended_groups.is_empty()) {
633     for (int i = 0; i < _contended_groups.length(); i++) {
634       FieldGroup* cg = _contended_groups.at(i);
635       LayoutRawBlock* start = _layout->last_block();
636       insert_contended_padding(start);
637       _layout->add(cg->primitive_fields(), start);
638       _layout->add(cg->oop_fields(), start);
639       need_tail_padding = true;
640     }
641   }
642 
643   if (need_tail_padding) {
644     insert_contended_padding(_layout->last_block());
645   }
646 
647   _static_layout->add_contiguously(this->_static_fields->oop_fields());
648   _static_layout->add(this->_static_fields->primitive_fields());
649 
650   epilogue();
651 }
652 
653 void FieldLayoutBuilder::epilogue() {
654   // Computing oopmaps
655   int super_oop_map_count = (_super_klass == nullptr) ? 0 :_super_klass->nonstatic_oop_map_count();
656   int max_oop_map_count = super_oop_map_count + _nonstatic_oopmap_count;
657 
658   OopMapBlocksBuilder* nonstatic_oop_maps =
659       new OopMapBlocksBuilder(max_oop_map_count);
660   if (super_oop_map_count > 0) {
661     nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(),
662     _super_klass->nonstatic_oop_map_count());
663   }
664 
665   if (_root_group->oop_fields() != nullptr) {
666     for (int i = 0; i < _root_group->oop_fields()->length(); i++) {
667       LayoutRawBlock* b = _root_group->oop_fields()->at(i);
668       nonstatic_oop_maps->add(b->offset(), 1);
669     }
670   }
671 
672   if (!_contended_groups.is_empty()) {
673     for (int i = 0; i < _contended_groups.length(); i++) {
674       FieldGroup* cg = _contended_groups.at(i);
675       if (cg->oop_count() > 0) {
676         assert(cg->oop_fields() != nullptr && cg->oop_fields()->at(0) != nullptr, "oop_count > 0 but no oop fields found");
677         nonstatic_oop_maps->add(cg->oop_fields()->at(0)->offset(), cg->oop_count());
678       }
679     }
680   }
681 
682   nonstatic_oop_maps->compact();
683 
684   int instance_end = align_up(_layout->last_block()->offset(), wordSize);
685   int static_fields_end = align_up(_static_layout->last_block()->offset(), wordSize);
686   int static_fields_size = (static_fields_end -
687       InstanceMirrorKlass::offset_of_static_fields()) / wordSize;
688   int nonstatic_field_end = align_up(_layout->last_block()->offset(), heapOopSize);
689 
690   // Pass back information needed for InstanceKlass creation
691 
692   _info->oop_map_blocks = nonstatic_oop_maps;
693   _info->_instance_size = align_object_size(instance_end / wordSize);
694   if (UseCompactObjectHeaders) {
695     _info->_hash_offset   = _layout->find_hash_offset();
696   }
697   _info->_static_field_size = static_fields_size;
698   _info->_nonstatic_field_size = (nonstatic_field_end - instanceOopDesc::base_offset_in_bytes()) / heapOopSize;
699   _info->_has_nonstatic_fields = _has_nonstatic_fields;
700 
701   if (PrintFieldLayout) {
702     ResourceMark rm;
703     tty->print_cr("Layout of class %s", _classname->as_C_string());
704     tty->print_cr("Instance fields:");
705     _layout->print(tty, false, _super_klass);
706     tty->print_cr("Static fields:");
707     _static_layout->print(tty, true, nullptr);
708     tty->print_cr("Instance size = %d bytes", _info->_instance_size * wordSize);
709     tty->print_cr("---");
710   }
711 }
712 
713 void FieldLayoutBuilder::build_layout() {
714   compute_regular_layout();
715 }