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