1 /*
  2  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CLASSFILE_FIELDLAYOUTBUILDER_HPP
 26 #define SHARE_CLASSFILE_FIELDLAYOUTBUILDER_HPP
 27 
 28 #include "classfile/classFileParser.hpp"
 29 #include "classfile/classLoaderData.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "oops/fieldStreams.hpp"
 32 #include "utilities/growableArray.hpp"
 33 
 34 // Classes below are used to compute the field layout of classes.
 35 
 36 
 37 // A LayoutRawBlock describes an element of a layout.
 38 // Each field is represented by a LayoutRawBlock.
 39 // LayoutRawBlocks can also represent elements injected by the JVM:
 40 // padding, empty blocks, inherited fields, etc.
 41 // All LayoutRawBlocks must have a size and an alignment. The size is the
 42 // exact size of the field expressed in bytes. The alignment is
 43 // the alignment constraint of the field (1 for byte, 2 for short,
 44 // 4 for int, 8 for long, etc.)
 45 //
 46 // LayoutRawBlock are designed to be used in two data structures:
 47 //   - a linked list in a layout (using _next_block, _prev_block)
 48 //   - a GrowableArray in field group (the growable array contains pointers to LayoutRawBlocks)
 49 //
 50 //  next/prev pointers are included in the LayoutRawBlock class to narrow
 51 //  the number of allocation required during the computation of a layout.
 52 //
 53 class LayoutRawBlock : public ResourceObj {
 54  public:
 55   // Some code relies on the order of values below.
 56   enum Kind {
 57     EMPTY,         // empty slot, space is taken from this to allocate fields
 58     RESERVED,      // reserved for JVM usage (for instance object header)
 59     PADDING,       // padding (because of alignment constraints or @Contended)
 60     REGULAR,       // primitive or oop field (including non-flattened inline fields)
 61     FLATTENED,     // flattened field
 62     INHERITED      // field(s) inherited from super classes
 63   };
 64 
 65  private:
 66   LayoutRawBlock* _next_block;
 67   LayoutRawBlock* _prev_block;
 68   Kind _kind;
 69   int _offset;
 70   int _alignment;
 71   int _size;
 72   int _field_index;
 73   bool _is_reference;
 74 
 75  public:
 76   LayoutRawBlock(Kind kind, int size);
 77   LayoutRawBlock(int index, Kind kind, int size, int alignment, bool is_reference = false);
 78   LayoutRawBlock* next_block() const { return _next_block; }
 79   void set_next_block(LayoutRawBlock* next) { _next_block = next; }
 80   LayoutRawBlock* prev_block() const { return _prev_block; }
 81   void set_prev_block(LayoutRawBlock* prev) { _prev_block = prev; }
 82   Kind kind() const { return _kind; }
 83   int offset() const {
 84     assert(_offset >= 0, "Must be initialized");
 85     return _offset;
 86   }
 87   void set_offset(int offset) { _offset = offset; }
 88   int alignment() const { return _alignment; }
 89   int size() const { return _size; }
 90   void set_size(int size) { _size = size; }
 91   int field_index() const {
 92     assert(_field_index != -1, "Must be initialized");
 93     return _field_index;
 94   }
 95   bool is_reference() const { return _is_reference; }
 96 
 97   bool fit(int size, int alignment);
 98 
 99   static int compare_offset(LayoutRawBlock** x, LayoutRawBlock** y)  { return (*x)->offset() - (*y)->offset(); }
100   // compare_size_inverted() returns the opposite of a regular compare method in order to
101   // sort fields in decreasing order.
102   // Note: with line types, the comparison should include alignment constraint if sizes are equals
103   static int compare_size_inverted(LayoutRawBlock** x, LayoutRawBlock** y)  {
104     int diff = (*y)->size() - (*x)->size();
105     // qsort() may reverse the order of fields with the same size.
106     // The extension is to ensure stable sort.
107     if (diff == 0) {
108       diff = (*x)->field_index() - (*y)->field_index();
109     }
110     return diff;
111   }
112 
113 };
114 
115 // A Field group represents a set of fields that have to be allocated together,
116 // this is the way the @Contended annotation is supported.
117 // Inside a FieldGroup, fields are sorted based on their kind: primitive,
118 // oop, or flattened.
119 //
120 class FieldGroup : public ResourceObj {
121 
122  private:
123   FieldGroup* _next;
124   GrowableArray<LayoutRawBlock*>* _primitive_fields;
125   GrowableArray<LayoutRawBlock*>* _oop_fields;
126   int _contended_group;
127   int _oop_count;
128   static const int INITIAL_LIST_SIZE = 16;
129 
130  public:
131   FieldGroup(int contended_group = -1);
132 
133   FieldGroup* next() const { return _next; }
134   void set_next(FieldGroup* next) { _next = next; }
135   GrowableArray<LayoutRawBlock*>* primitive_fields() const { return _primitive_fields; }
136   GrowableArray<LayoutRawBlock*>* oop_fields() const { return _oop_fields; }
137   int contended_group() const { return _contended_group; }
138   int oop_count() const { return _oop_count; }
139 
140   void add_primitive_field(int idx, BasicType type);
141   void add_oop_field(int idx);
142   void sort_by_size();
143 };
144 
145 // The FieldLayout class represents a set of fields organized
146 // in a layout.
147 // An instance of FieldLayout can either represent the layout
148 // of non-static fields (used in an instance object) or the
149 // layout of static fields (to be included in the class mirror).
150 //
151 // _block is a pointer to a list of LayoutRawBlock ordered by increasing
152 // offsets.
153 // _start points to the LayoutRawBlock with the first offset that can
154 // be used to allocate fields of the current class
155 // _last points to the last LayoutRawBlock of the list. In order to
156 // simplify the code, the LayoutRawBlock list always ends with an
157 // EMPTY block (the kind of LayoutRawBlock from which space is taken
158 // to allocate fields) with a size big enough to satisfy all
159 // field allocations.
160 //
161 class FieldLayout : public ResourceObj {
162  private:
163   GrowableArray<FieldInfo>* _field_info;
164   ConstantPool* _cp;
165   LayoutRawBlock* _blocks;  // the layout being computed
166   LayoutRawBlock* _start;   // points to the first block where a field can be inserted
167   LayoutRawBlock* _last;    // points to the last block of the layout (big empty block)
168 
169  public:
170   FieldLayout(GrowableArray<FieldInfo>* field_info, ConstantPool* cp);
171   void initialize_static_layout();
172   void initialize_instance_layout(const InstanceKlass* ik);
173 
174   LayoutRawBlock* first_empty_block() {
175     LayoutRawBlock* block = _start;
176     while (block->kind() != LayoutRawBlock::EMPTY) {
177       block = block->next_block();
178     }
179     return block;
180   }
181 
182   LayoutRawBlock* start() { return _start; }
183   void set_start(LayoutRawBlock* start) { _start = start; }
184   LayoutRawBlock* last_block() { return _last; }
185 
186   LayoutRawBlock* first_field_block();
187   void add(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start = nullptr);
188   void add_field_at_offset(LayoutRawBlock* blocks, int offset, LayoutRawBlock* start = nullptr);
189   void add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start = nullptr);
190   LayoutRawBlock* insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block);
191   bool reconstruct_layout(const InstanceKlass* ik);
192   void fill_holes(const InstanceKlass* ik);
193   LayoutRawBlock* insert(LayoutRawBlock* slot, LayoutRawBlock* block);
194   void remove(LayoutRawBlock* block);
195   void print(outputStream* output, bool is_static, const InstanceKlass* super);
196 };
197 
198 
199 // FieldLayoutBuilder is the main entry point for layout computation.
200 // This class has three methods to generate layout: one for regular classes
201 // and two for classes with hard coded offsets (java,lang.ref.Reference
202 // and the boxing classes). The rationale for having multiple methods
203 // is that each kind of class has a different set goals regarding
204 // its layout, so instead of mixing several layout strategies into a
205 // single method, each kind has its own method (see comments below
206 // for more details about the allocation strategies).
207 //
208 // Computing the layout of a class always goes through 4 steps:
209 //   1 - Prologue: preparation of data structure and gathering of
210 //       layout information inherited from super classes
211 //   2 - Field sorting: fields are sorted according to their
212 //       kind (oop, primitive, inline class) and their contention
213 //       annotation (if any)
214 //   3 - Layout is computed from the set of lists generated during
215 //       step 2
216 //   4 - Epilogue: oopmaps are generated, layout information is
217 //       prepared so other VM components can use it (instance size,
218 //       static field size, non-static field size, etc.)
219 //
220 //  Steps 1 and 4 are common to all layout computations. Step 2 and 3
221 //  can vary with the allocation strategy.
222 //
223 class FieldLayoutBuilder : public ResourceObj {
224  private:
225 
226   const Symbol* _classname;
227   const InstanceKlass* _super_klass;
228   ConstantPool* _constant_pool;
229   GrowableArray<FieldInfo>* _field_info;
230   FieldLayoutInfo* _info;
231   FieldGroup* _root_group;
232   GrowableArray<FieldGroup*> _contended_groups;
233   FieldGroup* _static_fields;
234   FieldLayout* _layout;
235   FieldLayout* _static_layout;
236   int _nonstatic_oopmap_count;
237   int _alignment;
238   bool _has_nonstatic_fields;
239   bool _is_contended; // is a contended class?
240 
241  public:
242   FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool,
243                      GrowableArray<FieldInfo>* field_info, bool is_contended, FieldLayoutInfo* info);
244 
245   int get_alignment() {
246     assert(_alignment != -1, "Uninitialized");
247     return _alignment;
248   }
249 
250   void build_layout();
251   void compute_regular_layout();
252   void insert_contended_padding(LayoutRawBlock* slot);
253 
254  private:
255   void prologue();
256   void epilogue();
257   void regular_field_sorting();
258   FieldGroup* get_or_create_contended_group(int g);
259 };
260 
261 #endif // SHARE_CLASSFILE_FIELDLAYOUTBUILDER_HPP