< prev index next >

src/hotspot/share/ci/ciInstance.cpp

Print this page

  1 /*
  2  * Copyright (c) 1999, 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 "ci/ciConstant.hpp"
 26 #include "ci/ciField.hpp"

 27 #include "ci/ciInstance.hpp"
 28 #include "ci/ciInstanceKlass.hpp"
 29 #include "ci/ciNullObject.hpp"
 30 #include "ci/ciUtilities.inline.hpp"
 31 #include "classfile/javaClasses.inline.hpp"
 32 #include "classfile/vmClasses.hpp"


 33 #include "oops/oop.inline.hpp"

 34 
 35 // ciInstance
 36 //
 37 // This class represents an instanceOop in the HotSpot virtual
 38 // machine.
 39 
 40 // ------------------------------------------------------------------
 41 // ciObject::java_mirror_type
 42 ciType* ciInstance::java_mirror_type() {
 43   VM_ENTRY_MARK;
 44   oop m = get_oop();
 45   // Return null if it is not java.lang.Class.
 46   if (m == nullptr || m->klass() != vmClasses::Class_klass()) {
 47     return nullptr;
 48   }
 49   // Return either a primitive type or a klass.
 50   if (java_lang_Class::is_primitive(m)) {
 51     return ciType::make(java_lang_Class::primitive_type(m));
 52   } else {
 53     Klass* k = java_lang_Class::as_Klass(m);
 54     assert(k != nullptr, "");
 55     return CURRENT_THREAD_ENV->get_klass(k);
 56   }
 57 }
 58 
 59 // ------------------------------------------------------------------
 60 // ciInstance::field_value_impl
 61 ciConstant ciInstance::field_value_impl(BasicType field_btype, int offset) {
 62   ciConstant value = check_constant_value_cache(offset, field_btype);


 63   if (value.is_valid()) {
 64     return value;
 65   }
 66   VM_ENTRY_MARK;
 67   oop obj = get_oop();
 68   assert(obj != nullptr, "bad oop");
 69   switch(field_btype) {
 70     case T_BYTE:    value = ciConstant(field_btype, obj->byte_field(offset)); break;
 71     case T_CHAR:    value = ciConstant(field_btype, obj->char_field(offset)); break;
 72     case T_SHORT:   value = ciConstant(field_btype, obj->short_field(offset)); break;
 73     case T_BOOLEAN: value = ciConstant(field_btype, obj->bool_field(offset)); break;
 74     case T_INT:     value = ciConstant(field_btype, obj->int_field(offset)); break;
 75     case T_FLOAT:   value = ciConstant(obj->float_field(offset)); break;
 76     case T_DOUBLE:  value = ciConstant(obj->double_field(offset)); break;
 77     case T_LONG:    value = ciConstant(obj->long_field(offset)); break;
 78     case T_OBJECT:  // fall through
 79     case T_ARRAY: {
 80       oop o = obj->obj_field(offset);











 81 
 82       // A field will be "constant" if it is known always to be
 83       // a non-null reference to an instance of a particular class,
 84       // or to a particular array.  This can happen even if the instance
 85       // or array is not perm.  In such a case, an "unloaded" ciArray
 86       // or ciInstance is created.  The compiler may be able to use
 87       // information about the object's class (which is exact) or length.
 88 
 89       if (o == nullptr) {
 90         value = ciConstant(field_btype, ciNullObject::make());
 91       } else {
 92         value = ciConstant(field_btype, CURRENT_ENV->get_object(o));

 93       }
 94       break;
 95     }
 96     default:
 97       fatal("no field value: %s", type2name(field_btype));
 98   }
 99   add_to_constant_value_cache(offset, value);
100   return value;
101 }
102 
103 // ------------------------------------------------------------------
104 // ciInstance::field_value
105 //
106 // Constant value of a field.













107 ciConstant ciInstance::field_value(ciField* field) {
108   assert(is_loaded(), "invalid access - must be loaded");
109   assert(field->holder()->is_loaded(), "invalid access - holder must be loaded");
110   assert(field->is_static() || klass()->is_subclass_of(field->holder()), "invalid access - must be subclass");
111   return field_value_impl(field->type()->basic_type(), field->offset_in_bytes());

















































































112 }
113 
114 // ------------------------------------------------------------------
115 // ciInstance::field_value_by_offset
116 //
117 // Constant value of a field at the specified offset.
118 ciConstant ciInstance::field_value_by_offset(int field_offset) {
119   ciInstanceKlass* ik = klass()->as_instance_klass();
120   ciField* field = ik->get_field_by_offset(field_offset, false);
121   if (field == nullptr)
122     return ciConstant();  // T_ILLEGAL
123   return field_value(field);
124 }
125 
126 // ------------------------------------------------------------------
127 // ciInstance::print_impl
128 //
129 // Implementation of the print method.
130 void ciInstance::print_impl(outputStream* st) {
131   st->print(" type=");

  1 /*
  2  * Copyright (c) 1999, 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 "ci/ciConstant.hpp"
 26 #include "ci/ciField.hpp"
 27 #include "ci/ciInlineKlass.hpp"
 28 #include "ci/ciInstance.hpp"
 29 #include "ci/ciInstanceKlass.hpp"
 30 #include "ci/ciNullObject.hpp"
 31 #include "ci/ciUtilities.inline.hpp"
 32 #include "classfile/javaClasses.inline.hpp"
 33 #include "classfile/vmClasses.hpp"
 34 #include "oops/fieldStreams.hpp"
 35 #include "oops/fieldStreams.inline.hpp"
 36 #include "oops/oop.inline.hpp"
 37 #include "oops/valuePayload.inline.hpp"
 38 
 39 // ciInstance
 40 //
 41 // This class represents an instanceOop in the HotSpot virtual
 42 // machine.
 43 
 44 // ------------------------------------------------------------------
 45 // ciObject::java_mirror_type
 46 ciType* ciInstance::java_mirror_type() {
 47   VM_ENTRY_MARK;
 48   oop m = get_oop();
 49   // Return null if it is not java.lang.Class.
 50   if (m == nullptr || m->klass() != vmClasses::Class_klass()) {
 51     return nullptr;
 52   }
 53   // Return either a primitive type or a klass.
 54   if (java_lang_Class::is_primitive(m)) {
 55     return ciType::make(java_lang_Class::primitive_type(m));
 56   } else {
 57     Klass* k = java_lang_Class::as_Klass(m);
 58     assert(k != nullptr, "");
 59     return CURRENT_THREAD_ENV->get_klass(k);
 60   }
 61 }
 62 
 63 // ------------------------------------------------------------------
 64 // ciInstance::field_value_impl
 65 ciConstant ciInstance::field_value_impl(ciField* field) {
 66   BasicType field_bt = field->type()->basic_type();
 67   int offset = field->offset_in_bytes();
 68   ciConstant value = check_constant_value_cache(offset, field_bt);
 69   if (value.is_valid()) {
 70     return value;
 71   }
 72   VM_ENTRY_MARK;
 73   oop obj = get_oop();
 74   assert(obj != nullptr, "bad oop");
 75   switch(field_bt) {
 76     case T_BYTE:    value = ciConstant(field_bt, obj->byte_field(offset)); break;
 77     case T_CHAR:    value = ciConstant(field_bt, obj->char_field(offset)); break;
 78     case T_SHORT:   value = ciConstant(field_bt, obj->short_field(offset)); break;
 79     case T_BOOLEAN: value = ciConstant(field_bt, obj->bool_field(offset)); break;
 80     case T_INT:     value = ciConstant(field_bt, obj->int_field(offset)); break;
 81     case T_FLOAT:   value = ciConstant(obj->float_field(offset)); break;
 82     case T_DOUBLE:  value = ciConstant(obj->double_field(offset)); break;
 83     case T_LONG:    value = ciConstant(obj->long_field(offset)); break;
 84     case T_OBJECT:  // fall through
 85     case T_ARRAY: {
 86       if (field->is_flat()) {
 87         assert(field->is_atomic(), "do not query atomically a non-atomic flat field");
 88         InlineKlass* vk = field->type()->as_inline_klass()->get_InlineKlass();
 89         FlatValuePayload payload = FlatValuePayload::construct_from_parts(obj, offset, vk, field->layout_kind());
 90         oop res = payload.read(THREAD);
 91         if (HAS_PENDING_EXCEPTION) {
 92           CLEAR_PENDING_EXCEPTION;
 93           return ciConstant();
 94         }
 95         value = ciConstant(field_bt, CURRENT_ENV->get_object(res));
 96       } else {
 97         oop o = obj->obj_field(offset);
 98 
 99         // A field will be "constant" if it is known always to be
100         // a non-null reference to an instance of a particular class,
101         // or to a particular array.  This can happen even if the instance
102         // or array is not perm.  In such a case, an "unloaded" ciArray
103         // or ciInstance is created.  The compiler may be able to use
104         // information about the object's class (which is exact) or length.
105 
106         if (o == nullptr) {
107           value = ciConstant(field_bt, ciNullObject::make());
108         } else {
109           value = ciConstant(field_bt, CURRENT_ENV->get_object(o));
110         }
111       }
112       break;
113     }
114     default:
115       fatal("no field value: %s", type2name(field_bt));
116   }
117   add_to_constant_value_cache(offset, value);
118   return value;
119 }
120 
121 // ------------------------------------------------------------------
122 // ciInstance::field_value
123 //
124 // Constant value of a field of any kind: a declared field, or a leaf field.
125 // For a flat declared field, a cached copy of the value object is returned.
126 //
127 // Since stable fields can be treated as "constant" but are not really, we need
128 // to cache the value of fields so that the compiler will observe only one value
129 // per field. We also need to ensure that leaf fields from a single stable
130 // flat declared field will be observed to be consistent with each other.
131 //
132 // To do so, we need to always fetch the whole declared field containing the
133 // desired field. If we want a sub-field of a flat field, we then extract the field
134 // out of the cached copy, using sub_field_value.
135 //
136 // In the case we request a non-flat field, or a declared field (possibly flat), there
137 // is no sub-field to extract and sub_field_value will not be called.
138 ciConstant ciInstance::field_value(ciField* field) {
139   assert(is_loaded(), "invalid access - must be loaded");
140   assert(field->holder()->is_loaded(), "invalid access - holder must be loaded");
141   assert(field->is_static() || field->holder()->is_inlinetype() || klass()->is_subclass_of(field->holder()),
142          "invalid access - must be subclass");
143   ciInstanceKlass* klass = this->klass()->as_instance_klass();
144   int containing_field_idx = klass->field_index_by_offset(field->offset_in_bytes());
145   ciField* containing_field = klass->declared_nonstatic_field_at(containing_field_idx);
146   if (containing_field->is_flat() && !containing_field->is_atomic()) {
147     assert(field != containing_field, "do not ask for a non atomic declared field");
148     return field_value_impl(field);
149   }
150   ciConstant containing_field_value = field_value_impl(containing_field);
151   if (!containing_field_value.is_valid()) {
152     return ciConstant();
153   }
154   if (field->original_holder() == nullptr) {
155     return containing_field_value;
156   }
157   ciObject* obj = containing_field_value.as_object();
158   if (obj->is_instance()) {
159     ciInstance* inst = obj->as_instance();
160     // inst->klass() must be an inline klass since it is the value of a flat field.
161     ciInlineKlass* inst_klass = inst->klass()->as_inline_klass();
162     ciField* field_in_value_klass = inst_klass->get_field_by_offset(inst_klass->payload_offset() + field->offset_in_bytes() - containing_field->offset_in_bytes(), false);
163     return inst->sub_field_value(field_in_value_klass);
164   } else if (obj->is_null_object()) {
165     return ciConstant::make_zero_or_null(field->type()->basic_type());
166   }
167   // obj should not be an array since we are trying to get a field inside it
168   ShouldNotReachHere();
169   return ciConstant();
170 }
171 
172 // Extract a leaf field from a value object.
173 //
174 // This is used by field_value when getting the value of a sub-field. field_value
175 // will take care of getting the value of the declared field containing the requested
176 // field, and of caching (see the comment on field_value for why). But if we want the
177 // value of a sub-field, we need to extract it from the value of the declared field
178 // containing the said sub-field. This is what this function does.
179 //
180 // This is meant for internal used only. In particular, this function does not cache
181 // the result and must be called only on already cached values (to ensure consistency).
182 // field_value takes care of that.
183 ciConstant ciInstance::sub_field_value(ciField* field) {
184   precond(klass()->is_inlinetype());
185   precond(!field->is_flat());
186   int offset = field->offset_in_bytes();
187   BasicType field_btype = field->type()->basic_type();
188 
189   ciConstant value;
190   VM_ENTRY_MARK;
191   oop obj = get_oop();
192   assert(obj != nullptr, "bad oop");
193   switch(field_btype) {
194   case T_BYTE:    value = ciConstant(field_btype, obj->byte_field(offset)); break;
195   case T_CHAR:    value = ciConstant(field_btype, obj->char_field(offset)); break;
196   case T_SHORT:   value = ciConstant(field_btype, obj->short_field(offset)); break;
197   case T_BOOLEAN: value = ciConstant(field_btype, obj->bool_field(offset)); break;
198   case T_INT:     value = ciConstant(field_btype, obj->int_field(offset)); break;
199   case T_FLOAT:   value = ciConstant(obj->float_field(offset)); break;
200   case T_DOUBLE:  value = ciConstant(obj->double_field(offset)); break;
201   case T_LONG:    value = ciConstant(obj->long_field(offset)); break;
202   case T_OBJECT:  // fall through
203   case T_ARRAY: {
204     oop o = obj->obj_field(offset);
205 
206     // A field will be "constant" if it is known always to be
207     // a non-null reference to an instance of a particular class,
208     // or to a particular array.  This can happen even if the instance
209     // or array is not perm.  In such a case, an "unloaded" ciArray
210     // or ciInstance is created.  The compiler may be able to use
211     // information about the object's class (which is exact) or length.
212 
213     if (o == nullptr) {
214       value = ciConstant(field_btype, ciNullObject::make());
215     } else {
216       value = ciConstant(field_btype, CURRENT_ENV->get_object(o));
217     }
218     break;
219   }
220   default:
221     fatal("no field value: %s", type2name(field_btype));
222   }
223   return value;
224 }
225 
226 // ------------------------------------------------------------------
227 // ciInstance::field_value_by_offset
228 //
229 // Constant value of a field at the specified offset.
230 ciConstant ciInstance::field_value_by_offset(int field_offset) {
231   ciInstanceKlass* ik = klass()->as_instance_klass();
232   ciField* field = ik->get_field_by_offset(field_offset, false);
233   if (field == nullptr)
234     return ciConstant();  // T_ILLEGAL
235   return field_value(field);
236 }
237 
238 // ------------------------------------------------------------------
239 // ciInstance::print_impl
240 //
241 // Implementation of the print method.
242 void ciInstance::print_impl(outputStream* st) {
243   st->print(" type=");
< prev index next >