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