Tue, 11 Mar 2008 11:25:13 -0700
6667620: (Escape Analysis) fix deoptimization for scalar replaced objects
Summary: Deoptimization code for reallocation and relocking scalar replaced objects has to be fixed.
Reviewed-by: rasbold, never
1 /*
2 * Copyright 1999-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_ciInstanceKlass.cpp.incl"
28 // ciInstanceKlass
29 //
30 // This class represents a klassOop in the HotSpot virtual machine
31 // whose Klass part in an instanceKlass.
33 // ------------------------------------------------------------------
34 // ciInstanceKlass::ciInstanceKlass
35 //
36 // Loaded instance klass.
37 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) :
38 ciKlass(h_k), _non_static_fields(NULL)
39 {
40 assert(get_Klass()->oop_is_instance(), "wrong type");
41 instanceKlass* ik = get_instanceKlass();
43 AccessFlags access_flags = ik->access_flags();
44 _flags = ciFlags(access_flags);
45 _has_finalizer = access_flags.has_finalizer();
46 _has_subklass = ik->subklass() != NULL;
47 _is_initialized = ik->is_initialized();
48 // Next line must follow and use the result of the previous line:
49 _is_linked = _is_initialized || ik->is_linked();
50 _nonstatic_field_size = ik->nonstatic_field_size();
51 _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
53 _nof_implementors = ik->nof_implementors();
54 for (int i = 0; i < implementors_limit; i++) {
55 _implementors[i] = NULL; // we will fill these lazily
56 }
58 Thread *thread = Thread::current();
59 if (ciObjectFactory::is_initialized()) {
60 _loader = JNIHandles::make_local(thread, ik->class_loader());
61 _protection_domain = JNIHandles::make_local(thread,
62 ik->protection_domain());
63 _is_shared = false;
64 } else {
65 Handle h_loader(thread, ik->class_loader());
66 Handle h_protection_domain(thread, ik->protection_domain());
67 _loader = JNIHandles::make_global(h_loader);
68 _protection_domain = JNIHandles::make_global(h_protection_domain);
69 _is_shared = true;
70 }
72 // Lazy fields get filled in only upon request.
73 _super = NULL;
74 _java_mirror = NULL;
76 if (is_shared()) {
77 if (h_k() != SystemDictionary::object_klass()) {
78 super();
79 }
80 java_mirror();
81 //compute_nonstatic_fields(); // done outside of constructor
82 }
84 _field_cache = NULL;
85 }
87 // Version for unloaded classes:
88 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
89 jobject loader, jobject protection_domain)
90 : ciKlass(name, ciInstanceKlassKlass::make())
91 {
92 assert(name->byte_at(0) != '[', "not an instance klass");
93 _is_initialized = false;
94 _is_linked = false;
95 _nonstatic_field_size = -1;
96 _nonstatic_fields = NULL;
97 _nof_implementors = -1;
98 _loader = loader;
99 _protection_domain = protection_domain;
100 _is_shared = false;
101 _super = NULL;
102 _java_mirror = NULL;
103 _field_cache = NULL;
104 }
108 // ------------------------------------------------------------------
109 // ciInstanceKlass::compute_shared_is_initialized
110 bool ciInstanceKlass::compute_shared_is_initialized() {
111 GUARDED_VM_ENTRY(
112 instanceKlass* ik = get_instanceKlass();
113 _is_initialized = ik->is_initialized();
114 return _is_initialized;
115 )
116 }
118 // ------------------------------------------------------------------
119 // ciInstanceKlass::compute_shared_is_linked
120 bool ciInstanceKlass::compute_shared_is_linked() {
121 GUARDED_VM_ENTRY(
122 instanceKlass* ik = get_instanceKlass();
123 _is_linked = ik->is_linked();
124 return _is_linked;
125 )
126 }
128 // ------------------------------------------------------------------
129 // ciInstanceKlass::compute_shared_has_subklass
130 bool ciInstanceKlass::compute_shared_has_subklass() {
131 GUARDED_VM_ENTRY(
132 instanceKlass* ik = get_instanceKlass();
133 _has_subklass = ik->subklass() != NULL;
134 return _has_subklass;
135 )
136 }
138 // ------------------------------------------------------------------
139 // ciInstanceKlass::compute_shared_nof_implementors
140 int ciInstanceKlass::compute_shared_nof_implementors() {
141 // We requery this property, since it is a very old ciObject.
142 GUARDED_VM_ENTRY(
143 instanceKlass* ik = get_instanceKlass();
144 _nof_implementors = ik->nof_implementors();
145 return _nof_implementors;
146 )
147 }
149 // ------------------------------------------------------------------
150 // ciInstanceKlass::loader
151 oop ciInstanceKlass::loader() {
152 ASSERT_IN_VM;
153 return JNIHandles::resolve(_loader);
154 }
156 // ------------------------------------------------------------------
157 // ciInstanceKlass::loader_handle
158 jobject ciInstanceKlass::loader_handle() {
159 return _loader;
160 }
162 // ------------------------------------------------------------------
163 // ciInstanceKlass::protection_domain
164 oop ciInstanceKlass::protection_domain() {
165 ASSERT_IN_VM;
166 return JNIHandles::resolve(_protection_domain);
167 }
169 // ------------------------------------------------------------------
170 // ciInstanceKlass::protection_domain_handle
171 jobject ciInstanceKlass::protection_domain_handle() {
172 return _protection_domain;
173 }
175 // ------------------------------------------------------------------
176 // ciInstanceKlass::field_cache
177 //
178 // Get the field cache associated with this klass.
179 ciConstantPoolCache* ciInstanceKlass::field_cache() {
180 if (is_shared()) {
181 return NULL;
182 }
183 if (_field_cache == NULL) {
184 assert(!is_java_lang_Object(), "Object has no fields");
185 Arena* arena = CURRENT_ENV->arena();
186 _field_cache = new (arena) ciConstantPoolCache(arena, 5);
187 }
188 return _field_cache;
189 }
191 // ------------------------------------------------------------------
192 // ciInstanceKlass::get_canonical_holder
193 //
194 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
195 #ifdef ASSERT
196 if (!(offset >= 0 && offset < layout_helper())) {
197 tty->print("*** get_canonical_holder(%d) on ", offset);
198 this->print();
199 tty->print_cr(" ***");
200 };
201 assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
202 #endif
204 if (offset < (instanceOopDesc::header_size() * wordSize)) {
205 // All header offsets belong properly to java/lang/Object.
206 return CURRENT_ENV->Object_klass();
207 }
209 ciInstanceKlass* self = this;
210 for (;;) {
211 assert(self->is_loaded(), "must be loaded to have size");
212 ciInstanceKlass* super = self->super();
213 if (super == NULL || !super->contains_field_offset(offset)) {
214 return self;
215 } else {
216 self = super; // return super->get_canonical_holder(offset)
217 }
218 }
219 }
221 // ------------------------------------------------------------------
222 // ciInstanceKlass::is_java_lang_Object
223 //
224 // Is this klass java.lang.Object?
225 bool ciInstanceKlass::is_java_lang_Object() {
226 return equals(CURRENT_ENV->Object_klass());
227 }
229 // ------------------------------------------------------------------
230 // ciInstanceKlass::uses_default_loader
231 bool ciInstanceKlass::uses_default_loader() {
232 VM_ENTRY_MARK;
233 return loader() == NULL;
234 }
236 // ------------------------------------------------------------------
237 // ciInstanceKlass::print_impl
238 //
239 // Implementation of the print method.
240 void ciInstanceKlass::print_impl(outputStream* st) {
241 ciKlass::print_impl(st);
242 GUARDED_VM_ENTRY(st->print(" loader=0x%x", (address)loader());)
243 if (is_loaded()) {
244 st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
245 bool_to_str(is_initialized()),
246 bool_to_str(has_finalizer()),
247 bool_to_str(has_subklass()),
248 layout_helper());
250 _flags.print_klass_flags();
252 if (_super) {
253 st->print(" super=");
254 _super->print_name();
255 }
256 if (_java_mirror) {
257 st->print(" mirror=PRESENT");
258 }
259 } else {
260 st->print(" loaded=false");
261 }
262 }
264 // ------------------------------------------------------------------
265 // ciInstanceKlass::super
266 //
267 // Get the superklass of this klass.
268 ciInstanceKlass* ciInstanceKlass::super() {
269 assert(is_loaded(), "must be loaded");
270 if (_super == NULL && !is_java_lang_Object()) {
271 GUARDED_VM_ENTRY(
272 klassOop super_klass = get_instanceKlass()->super();
273 _super = CURRENT_ENV->get_object(super_klass)->as_instance_klass();
274 )
275 }
276 return _super;
277 }
279 // ------------------------------------------------------------------
280 // ciInstanceKlass::java_mirror
281 //
282 // Get the instance of java.lang.Class corresponding to this klass.
283 ciInstance* ciInstanceKlass::java_mirror() {
284 assert(is_loaded(), "must be loaded");
285 if (_java_mirror == NULL) {
286 _java_mirror = ciKlass::java_mirror();
287 }
288 return _java_mirror;
289 }
291 // ------------------------------------------------------------------
292 // ciInstanceKlass::unique_concrete_subklass
293 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
294 if (!is_loaded()) return NULL; // No change if class is not loaded
295 if (!is_abstract()) return NULL; // Only applies to abstract classes.
296 if (!has_subklass()) return NULL; // Must have at least one subklass.
297 VM_ENTRY_MARK;
298 instanceKlass* ik = get_instanceKlass();
299 Klass* up = ik->up_cast_abstract();
300 assert(up->oop_is_instance(), "must be instanceKlass");
301 if (ik == up) {
302 return NULL;
303 }
304 return CURRENT_THREAD_ENV->get_object(up->as_klassOop())->as_instance_klass();
305 }
307 // ------------------------------------------------------------------
308 // ciInstanceKlass::has_finalizable_subclass
309 bool ciInstanceKlass::has_finalizable_subclass() {
310 if (!is_loaded()) return true;
311 VM_ENTRY_MARK;
312 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
313 }
315 // ------------------------------------------------------------------
316 // ciInstanceKlass::get_field_by_offset
317 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
318 if (!is_static) {
319 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
320 ciField* field = _nonstatic_fields->at(i);
321 int field_off = field->offset_in_bytes();
322 if (field_off == field_offset)
323 return field;
324 if (field_off > field_offset)
325 break;
326 // could do binary search or check bins, but probably not worth it
327 }
328 return NULL;
329 }
330 VM_ENTRY_MARK;
331 instanceKlass* k = get_instanceKlass();
332 fieldDescriptor fd;
333 if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
334 return NULL;
335 }
336 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
337 return field;
338 }
340 // ------------------------------------------------------------------
341 // ciInstanceKlass::non_static_fields.
343 class NonStaticFieldFiller: public FieldClosure {
344 GrowableArray<ciField*>* _arr;
345 ciEnv* _curEnv;
346 public:
347 NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) :
348 _curEnv(curEnv), _arr(arr)
349 {}
350 void do_field(fieldDescriptor* fd) {
351 ciField* field = new (_curEnv->arena()) ciField(fd);
352 _arr->append(field);
353 }
354 };
356 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() {
357 if (_non_static_fields == NULL) {
358 VM_ENTRY_MARK;
359 ciEnv* curEnv = ciEnv::current();
360 instanceKlass* ik = get_instanceKlass();
361 int max_n_fields = ik->fields()->length()/instanceKlass::next_offset;
363 _non_static_fields =
364 new (curEnv->arena()) GrowableArray<ciField*>(max_n_fields);
365 NonStaticFieldFiller filler(curEnv, _non_static_fields);
366 ik->do_nonstatic_fields(&filler);
367 }
368 return _non_static_fields;
369 }
371 static int sort_field_by_offset(ciField** a, ciField** b) {
372 return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
373 // (no worries about 32-bit overflow...)
374 }
376 // ------------------------------------------------------------------
377 // ciInstanceKlass::compute_nonstatic_fields
378 int ciInstanceKlass::compute_nonstatic_fields() {
379 assert(is_loaded(), "must be loaded");
381 if (_nonstatic_fields != NULL)
382 return _nonstatic_fields->length();
384 // Size in bytes of my fields, including inherited fields.
385 // About equal to size_helper() - sizeof(oopDesc).
386 int fsize = nonstatic_field_size() * wordSize;
387 if (fsize == 0) { // easy shortcut
388 Arena* arena = CURRENT_ENV->arena();
389 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
390 return 0;
391 }
392 assert(!is_java_lang_Object(), "bootstrap OK");
394 ciInstanceKlass* super = this->super();
395 int super_fsize = 0;
396 int super_flen = 0;
397 GrowableArray<ciField*>* super_fields = NULL;
398 if (super != NULL) {
399 super_fsize = super->nonstatic_field_size() * wordSize;
400 super_flen = super->nof_nonstatic_fields();
401 super_fields = super->_nonstatic_fields;
402 assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
403 }
405 // See if I am no larger than my super; if so, I can use his fields.
406 if (fsize == super_fsize) {
407 _nonstatic_fields = super_fields;
408 return super_fields->length();
409 }
411 GrowableArray<ciField*>* fields = NULL;
412 GUARDED_VM_ENTRY({
413 fields = compute_nonstatic_fields_impl(super_fields);
414 });
416 if (fields == NULL) {
417 // This can happen if this class (java.lang.Class) has invisible fields.
418 _nonstatic_fields = super_fields;
419 return super_fields->length();
420 }
422 int flen = fields->length();
424 // Now sort them by offset, ascending.
425 // (In principle, they could mix with superclass fields.)
426 fields->sort(sort_field_by_offset);
427 #ifdef ASSERT
428 int last_offset = sizeof(oopDesc);
429 for (int i = 0; i < fields->length(); i++) {
430 ciField* field = fields->at(i);
431 int offset = field->offset_in_bytes();
432 int size = (field->_type == NULL) ? oopSize : field->size_in_bytes();
433 assert(last_offset <= offset, "no field overlap");
434 if (last_offset > (int)sizeof(oopDesc))
435 assert((offset - last_offset) < BytesPerLong, "no big holes");
436 // Note: Two consecutive T_BYTE fields will be separated by wordSize-1
437 // padding bytes if one of them is declared by a superclass.
438 // This is a minor inefficiency classFileParser.cpp.
439 last_offset = offset + size;
440 }
441 assert(last_offset <= (int)sizeof(oopDesc) + fsize, "no overflow");
442 #endif
444 _nonstatic_fields = fields;
445 return flen;
446 }
448 GrowableArray<ciField*>*
449 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
450 super_fields) {
451 ASSERT_IN_VM;
452 Arena* arena = CURRENT_ENV->arena();
453 int flen = 0;
454 GrowableArray<ciField*>* fields = NULL;
455 instanceKlass* k = get_instanceKlass();
456 typeArrayOop fields_array = k->fields();
457 for (int pass = 0; pass <= 1; pass++) {
458 for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
459 fieldDescriptor fd;
460 fd.initialize(k->as_klassOop(), i);
461 if (fd.is_static()) continue;
462 if (pass == 0) {
463 flen += 1;
464 } else {
465 ciField* field = new (arena) ciField(&fd);
466 fields->append(field);
467 }
468 }
470 // Between passes, allocate the array:
471 if (pass == 0) {
472 if (flen == 0) {
473 return NULL; // return nothing if none are locally declared
474 }
475 if (super_fields != NULL) {
476 flen += super_fields->length();
477 }
478 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
479 if (super_fields != NULL) {
480 fields->appendAll(super_fields);
481 }
482 }
483 }
484 assert(fields->length() == flen, "sanity");
485 return fields;
486 }
488 // ------------------------------------------------------------------
489 // ciInstanceKlass::find_method
490 //
491 // Find a method in this klass.
492 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
493 VM_ENTRY_MARK;
494 instanceKlass* k = get_instanceKlass();
495 symbolOop name_sym = name->get_symbolOop();
496 symbolOop sig_sym= signature->get_symbolOop();
498 methodOop m = k->find_method(name_sym, sig_sym);
499 if (m == NULL) return NULL;
501 return CURRENT_THREAD_ENV->get_object(m)->as_method();
502 }
504 // ------------------------------------------------------------------
505 // ciInstanceKlass::is_leaf_type
506 bool ciInstanceKlass::is_leaf_type() {
507 assert(is_loaded(), "must be loaded");
508 if (is_shared()) {
509 return is_final(); // approximately correct
510 } else {
511 return !_has_subklass && (_nof_implementors == 0);
512 }
513 }
515 // ------------------------------------------------------------------
516 // ciInstanceKlass::implementor
517 //
518 // Report an implementor of this interface.
519 // Returns NULL if exact information is not available.
520 // Note that there are various races here, since my copy
521 // of _nof_implementors might be out of date with respect
522 // to results returned by instanceKlass::implementor.
523 // This is OK, since any dependencies we decide to assert
524 // will be checked later under the Compile_lock.
525 ciInstanceKlass* ciInstanceKlass::implementor(int n) {
526 if (n > implementors_limit) {
527 return NULL;
528 }
529 ciInstanceKlass* impl = _implementors[n];
530 if (impl == NULL) {
531 if (_nof_implementors > implementors_limit) {
532 return NULL;
533 }
534 // Go into the VM to fetch the implementor.
535 {
536 VM_ENTRY_MARK;
537 klassOop k = get_instanceKlass()->implementor(n);
538 if (k != NULL) {
539 impl = CURRENT_THREAD_ENV->get_object(k)->as_instance_klass();
540 }
541 }
542 // Memoize this result.
543 if (!is_shared()) {
544 _implementors[n] = (impl == NULL)? this: impl;
545 }
546 } else if (impl == this) {
547 impl = NULL; // memoized null result from a VM query
548 }
549 return impl;
550 }