1 /*
2 * Copyright (c) 2001, 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/ciMetadata.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "ci/ciReplay.hpp"
28 #include "ci/ciUtilities.inline.hpp"
29 #include "compiler/compiler_globals.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/klass.inline.hpp"
33 #include "oops/methodData.inline.hpp"
34 #include "oops/trainingData.hpp"
35 #include "runtime/deoptimization.hpp"
36 #include "utilities/copy.hpp"
37
38 // ciMethodData
39
40 // ------------------------------------------------------------------
41 // ciMethodData::ciMethodData
42 //
43 ciMethodData::ciMethodData(MethodData* md)
44 : ciMetadata(md),
45 _data_size(0), _extra_data_size(0), _data(nullptr),
46 _parameters_data_offset(0),
47 _exception_handlers_data_offset(0),
48 // Set an initial hint. Don't use set_hint_di() because
49 // first_di() may be out of bounds if data_size is 0.
50 _hint_di(first_di()),
51 _state(empty_state),
52 _saw_free_extra_data(false),
53 // Initialize the escape information (to "don't know.");
54 _eflags(0), _arg_local(0), _arg_stack(0), _arg_returned(0),
55 _invocation_counter(0),
56 _orig() {}
57
58
59 static bool is_klass_loaded(Klass* k) {
60 return TrainingData::is_klass_loaded(k);
61 }
62
63 // Check for entries that reference an unloaded method
64 class PrepareExtraDataClosure : public CleanExtraDataClosure {
65 MethodData* _mdo;
66 SafepointStateTracker _safepoint_tracker;
67 GrowableArray<Method*> _uncached_methods;
68
69 public:
70 PrepareExtraDataClosure(MethodData* mdo)
71 : _mdo(mdo),
72 _safepoint_tracker(SafepointSynchronize::safepoint_state_tracker()),
73 _uncached_methods()
74 { }
75
76 bool is_live(Method* m) {
77 Klass* holder = m->method_holder();
78 if (holder == nullptr || !holder->is_loader_present_and_alive() || !is_klass_loaded(holder)) {
79 return false;
80 }
81 if (CURRENT_ENV->cached_metadata(m) == nullptr) {
82 // Uncached entries need to be pre-populated.
83 _uncached_methods.append(m);
84 }
85 return true;
86 }
87
88 bool has_safepointed() {
89 return _safepoint_tracker.safepoint_state_changed();
90 }
91
92 bool finish() {
93 if (_uncached_methods.length() == 0) {
94 // Preparation finished iff all Methods* were already cached.
95 return true;
96 }
97 // We are currently holding the extra_data_lock and ensuring
98 // no safepoint breaks the lock.
99 _mdo->check_extra_data_locked();
100
101 // We now want to cache some method data. This could cause a safepoint.
102 // We temporarily release the lock and allow safepoints, and revert that
103 // at the end of the scope. This is safe, since we currently do not hold
104 // any extra_method_data: finish is called only after clean_extra_data,
105 // and the outer scope that first aquired the lock should not hold any
106 // extra_method_data while cleaning is performed, as the offsets can change.
107 MutexUnlocker mu(_mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
108
109 for (int i = 0; i < _uncached_methods.length(); ++i) {
110 if (has_safepointed()) {
111 // The metadata in the growable array might contain stale
112 // entries after a safepoint.
113 return false;
114 }
115 Method* method = _uncached_methods.at(i);
116 // Populating ciEnv caches may cause safepoints due
117 // to taking the Compile_lock with safepoint checks.
118 (void)CURRENT_ENV->get_method(method);
119 }
120 return false;
121 }
122 };
123
124 void ciMethodData::prepare_metadata() {
125 MethodData* mdo = get_MethodData();
126
127 for (;;) {
128 ResourceMark rm;
129 PrepareExtraDataClosure cl(mdo);
130 mdo->clean_extra_data(&cl);
131 if (cl.finish()) {
132 // When encountering uncached metadata, the Compile_lock might be
133 // acquired when creating ciMetadata handles, causing safepoints
134 // which requires a new round of preparation to clean out potentially
135 // new unloading metadata.
136 return;
137 }
138 }
139 }
140
141 void ciMethodData::load_remaining_extra_data() {
142 MethodData* mdo = get_MethodData();
143
144 // Lock to read ProfileData, and ensure lock is not unintentionally broken by a safepoint
145 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
146
147 // Deferred metadata cleaning due to concurrent class unloading.
148 prepare_metadata();
149 // After metadata preparation, there is no stale metadata,
150 // and no safepoints can introduce more stale metadata.
151 NoSafepointVerifier no_safepoint;
152
153 assert((mdo->data_size() == _data_size) && (mdo->extra_data_size() == _extra_data_size), "sanity, unchanged");
154 assert(extra_data_base() == (DataLayout*)((address) _data + _data_size), "sanity");
155
156 // Copy the extra data once it is prepared (i.e. cache populated, no release of extra data lock anymore)
157 Copy::disjoint_words_atomic((HeapWord*) mdo->extra_data_base(),
158 (HeapWord*) extra_data_base(),
159 // copy everything from extra_data_base() up to parameters_data_base()
160 pointer_delta(parameters_data_base(), extra_data_base(), HeapWordSize));
161
162 // skip parameter data copying. Already done in 'load_data'
163
164 // copy exception handler data
165 Copy::disjoint_words_atomic((HeapWord*) mdo->exception_handler_data_base(),
166 (HeapWord*) exception_handler_data_base(),
167 exception_handler_data_size() / HeapWordSize);
168
169 // speculative trap entries also hold a pointer to a Method so need to be translated
170 DataLayout* dp_src = mdo->extra_data_base();
171 DataLayout* end_src = mdo->args_data_limit();
172 DataLayout* dp_dst = extra_data_base();
173 for (;; dp_src = MethodData::next_extra(dp_src), dp_dst = MethodData::next_extra(dp_dst)) {
174 assert(dp_src < end_src, "moved past end of extra data");
175 assert(((intptr_t)dp_dst) - ((intptr_t)extra_data_base()) == ((intptr_t)dp_src) - ((intptr_t)mdo->extra_data_base()), "source and destination don't match");
176
177 int tag = dp_src->tag();
178 switch(tag) {
179 case DataLayout::speculative_trap_data_tag: {
180 ciSpeculativeTrapData data_dst(dp_dst);
181 SpeculativeTrapData data_src(dp_src);
182 data_dst.translate_from(&data_src);
183 break;
184 }
185 case DataLayout::bit_data_tag:
186 break;
187 case DataLayout::no_tag:
188 case DataLayout::arg_info_data_tag:
189 // An empty slot or ArgInfoData entry marks the end of the trap data
190 {
191 return; // Need a block to avoid SS compiler bug
192 }
193 default:
194 fatal("bad tag = %d", tag);
195 }
196 }
197 }
198
199 bool ciMethodData::load_data() {
200 MethodData* mdo = get_MethodData();
201 if (mdo == nullptr) {
202 return false;
203 }
204
205 // To do: don't copy the data if it is not "ripe" -- require a minimum #
206 // of invocations.
207
208 // Snapshot the data and extra parameter data first without the extra trap and arg info data.
209 // Those are copied in a second step. Actually, an approximate snapshot of the data is taken.
210 // Any concurrently executing threads may be changing the data as we copy it.
211 //
212 // The first snapshot step requires two copies (data entries and parameter data entries) since
213 // the MDO is laid out as follows:
214 //
215 // data_base: ---------------------------
216 // | data entries |
217 // | ... |
218 // extra_data_base: ---------------------------
219 // | trap data entries |
220 // | ... |
221 // | one arg info data entry |
222 // | data for each arg |
223 // | ... |
224 // args_data_limit: ---------------------------
225 // | parameter data entries |
226 // | ... |
227 // param_data_limit: ---------------------------
228 // | ex handler data entries |
229 // | ... |
230 // extra_data_limit: ---------------------------
231 //
232 // _data_size = extra_data_base - data_base
233 // _extra_data_size = extra_data_limit - extra_data_base
234 // total_size = _data_size + _extra_data_size
235 // args_data_limit = param_data_base
236 // param_data_limit = exception_handler_data_base
237 // extra_data_limit = extra_data_limit
238
239 #ifndef ZERO
240 // Some Zero platforms do not have expected alignment, and do not use
241 // this code. static_assert would still fire and fail for them.
242 static_assert(sizeof(_orig) % HeapWordSize == 0, "align");
243 #endif
244 Copy::disjoint_words_atomic((HeapWord*) &mdo->_compiler_counters,
245 (HeapWord*) &_orig,
246 sizeof(_orig) / HeapWordSize);
247 Arena* arena = CURRENT_ENV->arena();
248 _data_size = mdo->data_size();
249 _extra_data_size = mdo->extra_data_size();
250 int total_size = _data_size + _extra_data_size;
251 _data = (intptr_t *) arena->Amalloc(total_size);
252 Copy::disjoint_words_atomic((HeapWord*) mdo->data_base(),
253 (HeapWord*) _data,
254 _data_size / HeapWordSize);
255 // Copy offsets. This is used below
256 _parameters_data_offset = mdo->parameters_type_data_di();
257 _exception_handlers_data_offset = mdo->exception_handlers_data_di();
258
259 int parameters_data_size = mdo->parameters_size_in_bytes();
260 if (parameters_data_size > 0) {
261 // Snapshot the parameter data
262 Copy::disjoint_words_atomic((HeapWord*) mdo->parameters_data_base(),
263 (HeapWord*) parameters_data_base(),
264 parameters_data_size / HeapWordSize);
265 }
266 // Traverse the profile data, translating any oops into their
267 // ci equivalents.
268 ResourceMark rm;
269 ciProfileData* ci_data = first_data();
270 ProfileData* data = mdo->first_data();
271 while (is_valid(ci_data)) {
272 ci_data->translate_from(data);
273 ci_data = next_data(ci_data);
274 data = mdo->next_data(data);
275 }
276 if (mdo->parameters_type_data() != nullptr) {
277 DataLayout* parameters_data = data_layout_at(_parameters_data_offset);
278 ciParametersTypeData* parameters = new ciParametersTypeData(parameters_data);
279 parameters->translate_from(mdo->parameters_type_data());
280 }
281
282 assert((DataLayout*) ((address)_data + total_size - parameters_data_size - exception_handler_data_size()) == args_data_limit(),
283 "sanity - parameter data starts after the argument data of the single ArgInfoData entry");
284 load_remaining_extra_data();
285
286 // Note: Extra data are all BitData, and do not need translation.
287 _invocation_counter = mdo->invocation_count();
288 if (_invocation_counter == 0 && mdo->backedge_count() > 0) {
289 // Avoid skewing counter data during OSR compilation.
290 // Sometimes, MDO is allocated during the very first invocation and OSR compilation is triggered
291 // solely by backedge counter while invocation counter stays zero. In such case, it's important
292 // to observe non-zero invocation count to properly scale profile counts (see ciMethod::scale_count()).
293 _invocation_counter = 1;
294 }
295
296 _state = mdo->is_mature() ? mature_state : immature_state;
297 _eflags = mdo->eflags();
298 _arg_local = mdo->arg_local();
299 _arg_stack = mdo->arg_stack();
300 _arg_returned = mdo->arg_returned();
301 if (ReplayCompiles) {
302 ciReplay::initialize(this);
303 if (is_empty()) {
304 return false;
305 }
306 }
307 return true;
308 }
309
310 void ciReceiverTypeData::translate_receiver_data_from(const ProfileData* data) {
311 for (uint row = 0; row < row_limit(); row++) {
312 Klass* k = data->as_ReceiverTypeData()->receiver(row);
313 if (k != nullptr && k->class_loader_data() != nullptr && is_klass_loaded(k)) {
314 if (k->is_loader_alive()) {
315 ciKlass* klass = CURRENT_ENV->get_klass(k);
316 set_receiver(row, klass);
317 } else {
318 // With concurrent class unloading, the MDO could have stale metadata; override it
319 clear_row(row);
320 }
321 } else {
322 set_receiver(row, nullptr);
323 }
324 }
325 }
326
327 void ciTypeStackSlotEntries::translate_type_data_from(const TypeStackSlotEntries* entries) {
328 for (int i = 0; i < number_of_entries(); i++) {
329 intptr_t k = entries->type(i);
330 Klass* klass = (Klass*)klass_part(k);
331 if (klass == nullptr || !klass->is_loader_present_and_alive() || !is_klass_loaded(klass)) {
332 // With concurrent class unloading, the MDO could have stale metadata; override it
333 TypeStackSlotEntries::set_type(i, TypeStackSlotEntries::with_status((Klass*)nullptr, k));
334 } else {
335 TypeStackSlotEntries::set_type(i, translate_klass(k));
336 }
337 }
338 }
339
340 void ciSingleTypeEntry::translate_type_data_from(const SingleTypeEntry* ret) {
341 intptr_t k = ret->type();
342 Klass* klass = (Klass*)klass_part(k);
343 if (klass == nullptr || !klass->is_loader_present_and_alive() || !is_klass_loaded(klass)) {
344 // With concurrent class unloading, the MDO could have stale metadata; override it
345 set_type(SingleTypeEntry::with_status((Klass*)nullptr, k));
346 } else {
347 set_type(translate_klass(k));
348 }
349 }
350
351 void ciSpeculativeTrapData::translate_from(const ProfileData* data) {
352 Method* m = data->as_SpeculativeTrapData()->method();
353 ciMethod* ci_m = CURRENT_ENV->get_method(m);
354 set_method(ci_m);
355 }
356
357 // Get the data at an arbitrary (sort of) data index.
358 ciProfileData* ciMethodData::data_at(int data_index) {
359 if (out_of_bounds(data_index)) {
360 return nullptr;
361 }
362 DataLayout* data_layout = data_layout_at(data_index);
363 return data_from(data_layout);
364 }
365
366 ciProfileData* ciMethodData::data_from(DataLayout* data_layout) {
367 switch (data_layout->tag()) {
368 case DataLayout::no_tag:
369 default:
370 ShouldNotReachHere();
371 return nullptr;
372 case DataLayout::bit_data_tag:
373 return new ciBitData(data_layout);
374 case DataLayout::counter_data_tag:
375 return new ciCounterData(data_layout);
376 case DataLayout::jump_data_tag:
377 return new ciJumpData(data_layout);
378 case DataLayout::receiver_type_data_tag:
379 return new ciReceiverTypeData(data_layout);
380 case DataLayout::virtual_call_data_tag:
381 return new ciVirtualCallData(data_layout);
382 case DataLayout::ret_data_tag:
383 return new ciRetData(data_layout);
384 case DataLayout::branch_data_tag:
385 return new ciBranchData(data_layout);
386 case DataLayout::multi_branch_data_tag:
387 return new ciMultiBranchData(data_layout);
388 case DataLayout::arg_info_data_tag:
389 return new ciArgInfoData(data_layout);
390 case DataLayout::call_type_data_tag:
391 return new ciCallTypeData(data_layout);
392 case DataLayout::virtual_call_type_data_tag:
393 return new ciVirtualCallTypeData(data_layout);
394 case DataLayout::parameters_type_data_tag:
395 return new ciParametersTypeData(data_layout);
396 case DataLayout::array_store_data_tag:
397 return new ciArrayStoreData(data_layout);
398 case DataLayout::array_load_data_tag:
399 return new ciArrayLoadData(data_layout);
400 case DataLayout::acmp_data_tag:
401 return new ciACmpData(data_layout);
402 };
403 }
404
405 // Iteration over data.
406 ciProfileData* ciMethodData::next_data(ciProfileData* current) {
407 int current_index = dp_to_di(current->dp());
408 int next_index = current_index + current->size_in_bytes();
409 ciProfileData* next = data_at(next_index);
410 return next;
411 }
412
413 DataLayout* ciMethodData::next_data_layout_helper(DataLayout* current, bool extra) {
414 int current_index = dp_to_di((address)current);
415 int next_index = current_index + current->size_in_bytes();
416 if (extra ? out_of_bounds_extra(next_index) : out_of_bounds(next_index)) {
417 return nullptr;
418 }
419 DataLayout* next = data_layout_at(next_index);
420 return next;
421 }
422
423 DataLayout* ciMethodData::next_data_layout(DataLayout* current) {
424 return next_data_layout_helper(current, false);
425 }
426
427 DataLayout* ciMethodData::next_extra_data_layout(DataLayout* current) {
428 return next_data_layout_helper(current, true);
429 }
430
431 ciProfileData* ciMethodData::bci_to_extra_data(int bci, ciMethod* m, bool& two_free_slots) {
432 DataLayout* dp = extra_data_base();
433 DataLayout* end = args_data_limit();
434 two_free_slots = false;
435 for (;dp < end; dp = MethodData::next_extra(dp)) {
436 switch(dp->tag()) {
437 case DataLayout::no_tag:
438 _saw_free_extra_data = true; // observed an empty slot (common case)
439 two_free_slots = (MethodData::next_extra(dp)->tag() == DataLayout::no_tag);
440 return nullptr;
441 case DataLayout::arg_info_data_tag:
442 return nullptr; // ArgInfoData is after the trap data right before the parameter data.
443 case DataLayout::bit_data_tag:
444 if (m == nullptr && dp->bci() == bci) {
445 return new ciBitData(dp);
446 }
447 break;
448 case DataLayout::speculative_trap_data_tag: {
449 ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
450 // data->method() might be null if the MDO is snapshotted
451 // concurrently with a trap
452 if (m != nullptr && data->method() == m && dp->bci() == bci) {
453 return data;
454 }
455 break;
456 }
457 default:
458 fatal("bad tag = %d", dp->tag());
459 }
460 }
461 return nullptr;
462 }
463
464 // Translate a bci to its corresponding data, or nullptr.
465 ciProfileData* ciMethodData::bci_to_data(int bci, ciMethod* m) {
466 // If m is not nullptr we look for a SpeculativeTrapData entry
467 if (m == nullptr) {
468 DataLayout* data_layout = data_layout_before(bci);
469 for ( ; is_valid(data_layout); data_layout = next_data_layout(data_layout)) {
470 if (data_layout->bci() == bci) {
471 set_hint_di(dp_to_di((address)data_layout));
472 return data_from(data_layout);
473 } else if (data_layout->bci() > bci) {
474 break;
475 }
476 }
477 }
478 bool two_free_slots = false;
479 ciProfileData* result = bci_to_extra_data(bci, m, two_free_slots);
480 if (result != nullptr) {
481 return result;
482 }
483 if (m != nullptr && !two_free_slots) {
484 // We were looking for a SpeculativeTrapData entry we didn't
485 // find. Room is not available for more SpeculativeTrapData
486 // entries, look in the non SpeculativeTrapData entries.
487 return bci_to_data(bci, nullptr);
488 }
489 return nullptr;
490 }
491
492 ciBitData ciMethodData::exception_handler_bci_to_data(int bci) {
493 assert(ProfileExceptionHandlers, "not profiling");
494 assert(_data != nullptr, "must be initialized");
495 for (DataLayout* data = exception_handler_data_base(); data < exception_handler_data_limit(); data = next_extra_data_layout(data)) {
496 assert(data != nullptr, "out of bounds?");
497 if (data->bci() == bci) {
498 return ciBitData(data);
499 }
500 }
501 // called with invalid bci or wrong Method/MethodData
502 ShouldNotReachHere();
503 return ciBitData(nullptr);
504 }
505
506 // Conservatively decode the trap_state of a ciProfileData.
507 int ciMethodData::has_trap_at(ciProfileData* data, int reason) {
508 typedef Deoptimization::DeoptReason DR_t;
509 int per_bc_reason
510 = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason);
511 if (trap_count(reason) == 0) {
512 // Impossible for this trap to have occurred, regardless of trap_state.
513 // Note: This happens if the MDO is empty.
514 return 0;
515 } else if (per_bc_reason == Deoptimization::Reason_none) {
516 // We cannot conclude anything; a trap happened somewhere, maybe here.
517 return -1;
518 } else if (data == nullptr) {
519 // No profile here, not even an extra_data record allocated on the fly.
520 // If there are empty extra_data records, and there had been a trap,
521 // there would have been a non-null data pointer. If there are no
522 // free extra_data records, we must return a conservative -1.
523 if (_saw_free_extra_data)
524 return 0; // Q.E.D.
525 else
526 return -1; // bail with a conservative answer
527 } else {
528 return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason);
529 }
530 }
531
532 int ciMethodData::trap_recompiled_at(ciProfileData* data) {
533 if (data == nullptr) {
534 return (_saw_free_extra_data? 0: -1); // (see previous method)
535 } else {
536 return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0;
537 }
538 }
539
540 void ciMethodData::clear_escape_info() {
541 VM_ENTRY_MARK;
542 MethodData* mdo = get_MethodData();
543 if (mdo != nullptr) {
544 mdo->clear_escape_info();
545 ArgInfoData *aid = arg_info();
546 int arg_count = (aid == nullptr) ? 0 : aid->number_of_args();
547 for (int i = 0; i < arg_count; i++) {
548 set_arg_modified(i, 0);
549 }
550 }
551 _eflags = _arg_local = _arg_stack = _arg_returned = 0;
552 }
553
554 // copy our escape info to the MethodData* if it exists
555 void ciMethodData::update_escape_info() {
556 VM_ENTRY_MARK;
557 MethodData* mdo = get_MethodData();
558 if ( mdo != nullptr) {
559 mdo->set_eflags(_eflags);
560 mdo->set_arg_local(_arg_local);
561 mdo->set_arg_stack(_arg_stack);
562 mdo->set_arg_returned(_arg_returned);
563 int arg_count = mdo->method()->size_of_parameters();
564 for (int i = 0; i < arg_count; i++) {
565 mdo->set_arg_modified(i, arg_modified(i));
566 }
567 }
568 }
569
570 void ciMethodData::set_compilation_stats(short loops, short blocks) {
571 VM_ENTRY_MARK;
572 MethodData* mdo = get_MethodData();
573 if (mdo != nullptr) {
574 mdo->set_num_loops(loops);
575 mdo->set_num_blocks(blocks);
576 }
577 }
578
579 void ciMethodData::set_would_profile(bool p) {
580 VM_ENTRY_MARK;
581 MethodData* mdo = get_MethodData();
582 if (mdo != nullptr) {
583 mdo->set_would_profile(p);
584 }
585 }
586
587 void ciMethodData::set_argument_type(int bci, int i, ciKlass* k) {
588 VM_ENTRY_MARK;
589 MethodData* mdo = get_MethodData();
590 if (mdo != nullptr) {
591 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
592 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
593
594 ProfileData* data = mdo->bci_to_data(bci);
595 if (data != nullptr) {
596 if (data->is_CallTypeData()) {
597 data->as_CallTypeData()->set_argument_type(i, k->get_Klass());
598 } else {
599 assert(data->is_VirtualCallTypeData(), "no arguments!");
600 data->as_VirtualCallTypeData()->set_argument_type(i, k->get_Klass());
601 }
602 }
603 }
604 }
605
606 void ciMethodData::set_parameter_type(int i, ciKlass* k) {
607 VM_ENTRY_MARK;
608 MethodData* mdo = get_MethodData();
609 if (mdo != nullptr) {
610 mdo->parameters_type_data()->set_type(i, k->get_Klass());
611 }
612 }
613
614 void ciMethodData::set_return_type(int bci, ciKlass* k) {
615 VM_ENTRY_MARK;
616 MethodData* mdo = get_MethodData();
617 if (mdo != nullptr) {
618 // Lock to read ProfileData, and ensure lock is not broken by a safepoint
619 MutexLocker ml(mdo->extra_data_lock(), Mutex::_no_safepoint_check_flag);
620
621 ProfileData* data = mdo->bci_to_data(bci);
622 if (data != nullptr) {
623 if (data->is_CallTypeData()) {
624 data->as_CallTypeData()->set_return_type(k->get_Klass());
625 } else {
626 assert(data->is_VirtualCallTypeData(), "no arguments!");
627 data->as_VirtualCallTypeData()->set_return_type(k->get_Klass());
628 }
629 }
630 }
631 }
632
633 bool ciMethodData::has_escape_info() {
634 return eflag_set(MethodData::estimated);
635 }
636
637 void ciMethodData::set_eflag(MethodData::EscapeFlag f) {
638 set_bits(_eflags, f);
639 }
640
641 bool ciMethodData::eflag_set(MethodData::EscapeFlag f) const {
642 return mask_bits(_eflags, f) != 0;
643 }
644
645 void ciMethodData::set_arg_local(int i) {
646 set_nth_bit(_arg_local, i);
647 }
648
649 void ciMethodData::set_arg_stack(int i) {
650 set_nth_bit(_arg_stack, i);
651 }
652
653 void ciMethodData::set_arg_returned(int i) {
654 set_nth_bit(_arg_returned, i);
655 }
656
657 void ciMethodData::set_arg_modified(int arg, uint val) {
658 ArgInfoData *aid = arg_info();
659 if (aid == nullptr)
660 return;
661 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
662 aid->set_arg_modified(arg, val);
663 }
664
665 bool ciMethodData::is_arg_local(int i) const {
666 return is_set_nth_bit(_arg_local, i);
667 }
668
669 bool ciMethodData::is_arg_stack(int i) const {
670 return is_set_nth_bit(_arg_stack, i);
671 }
672
673 bool ciMethodData::is_arg_returned(int i) const {
674 return is_set_nth_bit(_arg_returned, i);
675 }
676
677 uint ciMethodData::arg_modified(int arg) const {
678 ArgInfoData *aid = arg_info();
679 if (aid == nullptr)
680 return 0;
681 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number");
682 return aid->arg_modified(arg);
683 }
684
685 ciParametersTypeData* ciMethodData::parameters_type_data() const {
686 return parameter_data_size() != 0 ? new ciParametersTypeData(data_layout_at(_parameters_data_offset)) : nullptr;
687 }
688
689 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) {
690 // Get offset within MethodData* of the data array
691 ByteSize data_offset = MethodData::data_offset();
692
693 // Get cell offset of the ProfileData within data array
694 int cell_offset = dp_to_di(data->dp());
695
696 // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
697 int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data);
698
699 return in_ByteSize(offset);
700 }
701
702 ciArgInfoData *ciMethodData::arg_info() const {
703 // Should be last, have to skip all traps.
704 DataLayout* dp = extra_data_base();
705 DataLayout* end = args_data_limit();
706 for (; dp < end; dp = MethodData::next_extra(dp)) {
707 if (dp->tag() == DataLayout::arg_info_data_tag)
708 return new ciArgInfoData(dp);
709 }
710 return nullptr;
711 }
712
713
714 // Implementation of the print method.
715 void ciMethodData::print_impl(outputStream* st) {
716 ciMetadata::print_impl(st);
717 }
718
719 void ciMethodData::dump_replay_data_type_helper(outputStream* out, int round, int& count, ProfileData* pdata, ByteSize offset, ciKlass* k) {
720 if (k != nullptr) {
721 if (round == 0) {
722 count++;
723 } else {
724 out->print(" %d %s", (int)(dp_to_di(pdata->dp() + in_bytes(offset)) / sizeof(intptr_t)),
725 CURRENT_ENV->replay_name(k));
726 }
727 }
728 }
729
730 template<class T> void ciMethodData::dump_replay_data_receiver_type_helper(outputStream* out, int round, int& count, T* vdata) {
731 for (uint i = 0; i < vdata->row_limit(); i++) {
732 dump_replay_data_type_helper(out, round, count, vdata, vdata->receiver_offset(i), vdata->receiver(i));
733 }
734 }
735
736 template<class T> void ciMethodData::dump_replay_data_call_type_helper(outputStream* out, int round, int& count, T* call_type_data) {
737 if (call_type_data->has_arguments()) {
738 for (int i = 0; i < call_type_data->number_of_arguments(); i++) {
739 dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->argument_type_offset(i), call_type_data->valid_argument_type(i));
740 }
741 }
742 if (call_type_data->has_return()) {
743 dump_replay_data_type_helper(out, round, count, call_type_data, call_type_data->return_type_offset(), call_type_data->valid_return_type());
744 }
745 }
746
747 void ciMethodData::dump_replay_data_extra_data_helper(outputStream* out, int round, int& count) {
748 DataLayout* dp = extra_data_base();
749 DataLayout* end = args_data_limit();
750
751 for (;dp < end; dp = MethodData::next_extra(dp)) {
752 switch(dp->tag()) {
753 case DataLayout::no_tag:
754 case DataLayout::arg_info_data_tag:
755 return;
756 case DataLayout::bit_data_tag:
757 break;
758 case DataLayout::speculative_trap_data_tag: {
759 ciSpeculativeTrapData* data = new ciSpeculativeTrapData(dp);
760 ciMethod* m = data->method();
761 if (m != nullptr) {
762 if (round == 0) {
763 count++;
764 } else {
765 out->print(" %d ", (int)(dp_to_di(((address)dp) + in_bytes(ciSpeculativeTrapData::method_offset())) / sizeof(intptr_t)));
766 m->dump_name_as_ascii(out);
767 }
768 }
769 break;
770 }
771 default:
772 fatal("bad tag = %d", dp->tag());
773 }
774 }
775 }
776
777 void ciMethodData::dump_replay_data(outputStream* out) {
778 ResourceMark rm;
779 MethodData* mdo = get_MethodData();
780 Method* method = mdo->method();
781 out->print("ciMethodData ");
782 ciMethod::dump_name_as_ascii(out, method);
783 out->print(" %d %d", _state, _invocation_counter);
784
785 // dump the contents of the MDO header as raw data
786 unsigned char* orig = (unsigned char*)&_orig;
787 int length = sizeof(_orig);
788 out->print(" orig %d", length);
789 for (int i = 0; i < length; i++) {
790 out->print(" %d", orig[i]);
791 }
792
793 // dump the MDO data as raw data
794 int elements = (data_size() + extra_data_size()) / sizeof(intptr_t);
795 out->print(" data %d", elements);
796 for (int i = 0; i < elements; i++) {
797 // We could use INTPTR_FORMAT here but that's zero justified
798 // which makes comparing it with the SA version of this output
799 // harder. data()'s element type is intptr_t.
800 out->print(" 0x%zx", data()[i]);
801 }
802
803 // The MDO contained oop references as ciObjects, so scan for those
804 // and emit pairs of offset and klass name so that they can be
805 // reconstructed at runtime. The first round counts the number of
806 // oop references and the second actually emits them.
807 ciParametersTypeData* parameters = parameters_type_data();
808 for (int count = 0, round = 0; round < 2; round++) {
809 if (round == 1) out->print(" oops %d", count);
810 ProfileData* pdata = first_data();
811 for ( ; is_valid(pdata); pdata = next_data(pdata)) {
812 if (pdata->is_VirtualCallData()) {
813 ciVirtualCallData* vdata = (ciVirtualCallData*)pdata;
814 dump_replay_data_receiver_type_helper<ciVirtualCallData>(out, round, count, vdata);
815 if (pdata->is_VirtualCallTypeData()) {
816 ciVirtualCallTypeData* call_type_data = (ciVirtualCallTypeData*)pdata;
817 dump_replay_data_call_type_helper<ciVirtualCallTypeData>(out, round, count, call_type_data);
818 }
819 } else if (pdata->is_CallTypeData()) {
820 ciCallTypeData* call_type_data = (ciCallTypeData*)pdata;
821 dump_replay_data_call_type_helper<ciCallTypeData>(out, round, count, call_type_data);
822 } else if (pdata->is_ArrayStoreData()) {
823 ciArrayStoreData* array_store_data = (ciArrayStoreData*)pdata;
824 dump_replay_data_type_helper(out, round, count, array_store_data, ciArrayStoreData::array_offset(),
825 array_store_data->array()->valid_type());
826 dump_replay_data_receiver_type_helper<ciArrayStoreData>(out, round, count, array_store_data);
827 } else if (pdata->is_ArrayLoadData()) {
828 ciArrayLoadData* array_load_data = (ciArrayLoadData*)pdata;
829 dump_replay_data_type_helper(out, round, count, array_load_data, ciArrayLoadData::array_offset(),
830 array_load_data->array()->valid_type());
831 dump_replay_data_type_helper(out, round, count, array_load_data, ciArrayLoadData::element_offset(),
832 array_load_data->element()->valid_type());
833 } else if (pdata->is_ACmpData()) {
834 ciACmpData* acmp_data = (ciACmpData*)pdata;
835 dump_replay_data_type_helper(out, round, count, acmp_data, ciACmpData::left_offset(),
836 acmp_data->left()->valid_type());
837 dump_replay_data_type_helper(out, round, count, acmp_data, ciACmpData::right_offset(),
838 acmp_data->right()->valid_type());
839 } else if (pdata->is_ReceiverTypeData()) {
840 ciReceiverTypeData* vdata = (ciReceiverTypeData*)pdata;
841 dump_replay_data_receiver_type_helper<ciReceiverTypeData>(out, round, count, vdata);
842 }
843 }
844 if (parameters != nullptr) {
845 for (int i = 0; i < parameters->number_of_parameters(); i++) {
846 dump_replay_data_type_helper(out, round, count, parameters, ParametersTypeData::type_offset(i), parameters->valid_parameter_type(i));
847 }
848 }
849 }
850 for (int count = 0, round = 0; round < 2; round++) {
851 if (round == 1) out->print(" methods %d", count);
852 dump_replay_data_extra_data_helper(out, round, count);
853 }
854 out->cr();
855 }
856
857 #ifndef PRODUCT
858 void ciMethodData::print() {
859 print_data_on(tty);
860 }
861
862 void ciMethodData::print_data_on(outputStream* st) {
863 ResourceMark rm;
864 ciParametersTypeData* parameters = parameters_type_data();
865 if (parameters != nullptr) {
866 parameters->print_data_on(st);
867 }
868 ciProfileData* data;
869 for (data = first_data(); is_valid(data); data = next_data(data)) {
870 st->print("%d", dp_to_di(data->dp()));
871 st->fill_to(6);
872 data->print_data_on(st);
873 }
874 st->print_cr("--- Extra data:");
875 DataLayout* dp = extra_data_base();
876 DataLayout* end = args_data_limit();
877 for (;; dp = MethodData::next_extra(dp)) {
878 assert(dp < end, "moved past end of extra data");
879 switch (dp->tag()) {
880 case DataLayout::no_tag:
881 continue;
882 case DataLayout::bit_data_tag:
883 data = new BitData(dp);
884 break;
885 case DataLayout::arg_info_data_tag:
886 data = new ciArgInfoData(dp);
887 dp = end; // ArgInfoData is after the trap data right before the parameter data.
888 break;
889 case DataLayout::speculative_trap_data_tag:
890 data = new ciSpeculativeTrapData(dp);
891 break;
892 default:
893 fatal("unexpected tag %d", dp->tag());
894 }
895 st->print("%d", dp_to_di(data->dp()));
896 st->fill_to(6);
897 data->print_data_on(st);
898 if (dp >= end) return;
899 }
900 }
901
902 void ciTypeEntries::print_ciklass(outputStream* st, intptr_t k) {
903 if (TypeEntries::is_type_none(k)) {
904 st->print("none");
905 } else if (TypeEntries::is_type_unknown(k)) {
906 st->print("unknown");
907 } else {
908 valid_ciklass(k)->print_name_on(st);
909 }
910 if (TypeEntries::was_null_seen(k)) {
911 st->print(" (null seen)");
912 }
913 }
914
915 void ciTypeStackSlotEntries::print_data_on(outputStream* st) const {
916 for (int i = 0; i < number_of_entries(); i++) {
917 _pd->tab(st);
918 st->print("%d: stack (%u) ", i, stack_slot(i));
919 print_ciklass(st, type(i));
920 st->cr();
921 }
922 }
923
924 void ciSingleTypeEntry::print_data_on(outputStream* st) const {
925 _pd->tab(st);
926 st->print("ret ");
927 print_ciklass(st, type());
928 st->cr();
929 }
930
931 void ciCallTypeData::print_data_on(outputStream* st, const char* extra) const {
932 print_shared(st, "ciCallTypeData", extra);
933 if (has_arguments()) {
934 tab(st, true);
935 st->print_cr("argument types");
936 args()->print_data_on(st);
937 }
938 if (has_return()) {
939 tab(st, true);
940 st->print_cr("return type");
941 ret()->print_data_on(st);
942 }
943 }
944
945 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) const {
946 uint row;
947 int entries = 0;
948 for (row = 0; row < row_limit(); row++) {
949 if (receiver(row) != nullptr) entries++;
950 }
951 st->print_cr("count(%u) entries(%u)", count(), entries);
952 for (row = 0; row < row_limit(); row++) {
953 if (receiver(row) != nullptr) {
954 tab(st);
955 receiver(row)->print_name_on(st);
956 st->print_cr("(%u)", receiver_count(row));
957 }
958 }
959 }
960
961 void ciReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
962 print_shared(st, "ciReceiverTypeData", extra);
963 print_receiver_data_on(st);
964 }
965
966 void ciVirtualCallData::print_data_on(outputStream* st, const char* extra) const {
967 print_shared(st, "ciVirtualCallData", extra);
968 rtd_super()->print_receiver_data_on(st);
969 }
970
971 void ciVirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
972 print_shared(st, "ciVirtualCallTypeData", extra);
973 rtd_super()->print_receiver_data_on(st);
974 if (has_arguments()) {
975 tab(st, true);
976 st->print("argument types");
977 args()->print_data_on(st);
978 }
979 if (has_return()) {
980 tab(st, true);
981 st->print("return type");
982 ret()->print_data_on(st);
983 }
984 }
985
986 void ciParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
987 st->print_cr("ciParametersTypeData");
988 parameters()->print_data_on(st);
989 }
990
991 void ciSpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
992 st->print_cr("ciSpeculativeTrapData");
993 tab(st);
994 method()->print_short_name(st);
995 st->cr();
996 }
997
998 void ciArrayStoreData::print_data_on(outputStream* st, const char* extra) const {
999 print_shared(st, "ciArrayStoreData", extra);
1000 st->cr();
1001 tab(st, true);
1002 st->print("array");
1003 array()->print_data_on(st);
1004 tab(st, true);
1005 st->print("element");
1006 rtd_super()->print_receiver_data_on(st);
1007 }
1008
1009 void ciArrayLoadData::print_data_on(outputStream* st, const char* extra) const {
1010 print_shared(st, "ciArrayLoadData", extra);
1011 st->cr();
1012 tab(st, true);
1013 st->print("array");
1014 array()->print_data_on(st);
1015 tab(st, true);
1016 st->print("element");
1017 element()->print_data_on(st);
1018 }
1019
1020 void ciACmpData::print_data_on(outputStream* st, const char* extra) const {
1021 BranchData::print_data_on(st, extra);
1022 st->cr();
1023 tab(st, true);
1024 st->print("left");
1025 left()->print_data_on(st);
1026 tab(st, true);
1027 st->print("right");
1028 right()->print_data_on(st);
1029 }
1030 #endif