forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodData.cpp
More file actions
1832 lines (1654 loc) · 60 KB
/
Copy pathmethodData.cpp
File metadata and controls
1832 lines (1654 loc) · 60 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "ci/ciMethodData.hpp"
#include "classfile/vmSymbols.hpp"
#include "compiler/compilationPolicy.hpp"
#include "compiler/compilerOracle.hpp"
#include "interpreter/bytecode.hpp"
#include "interpreter/bytecodeStream.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/metaspaceClosure.hpp"
#include "memory/resourceArea.hpp"
#include "oops/klass.inline.hpp"
#include "oops/methodData.inline.hpp"
#include "prims/jvmtiRedefineClasses.hpp"
#include "runtime/atomic.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/orderAccess.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/signature.hpp"
#include "utilities/align.hpp"
#include "utilities/copy.hpp"
// ==================================================================
// DataLayout
//
// Overlay for generic profiling data.
// Some types of data layouts need a length field.
bool DataLayout::needs_array_len(u1 tag) {
return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag);
}
// Perform generic initialization of the data. More specific
// initialization occurs in overrides of ProfileData::post_initialize.
void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
_header._bits = (intptr_t)0;
_header._struct._tag = tag;
_header._struct._bci = bci;
for (int i = 0; i < cell_count; i++) {
set_cell_at(i, (intptr_t)0);
}
if (needs_array_len(tag)) {
set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
}
if (tag == call_type_data_tag) {
CallTypeData::initialize(this, cell_count);
} else if (tag == virtual_call_type_data_tag) {
VirtualCallTypeData::initialize(this, cell_count);
}
}
void DataLayout::clean_weak_klass_links(bool always_clean) {
ResourceMark m;
data_in()->clean_weak_klass_links(always_clean);
}
// ==================================================================
// ProfileData
//
// A ProfileData object is created to refer to a section of profiling
// data in a structured way.
// Constructor for invalid ProfileData.
ProfileData::ProfileData() {
_data = NULL;
}
char* ProfileData::print_data_on_helper(const MethodData* md) const {
DataLayout* dp = md->extra_data_base();
DataLayout* end = md->args_data_limit();
stringStream ss;
for (;; dp = MethodData::next_extra(dp)) {
assert(dp < end, "moved past end of extra data");
switch(dp->tag()) {
case DataLayout::speculative_trap_data_tag:
if (dp->bci() == bci()) {
SpeculativeTrapData* data = new SpeculativeTrapData(dp);
int trap = data->trap_state();
char buf[100];
ss.print("trap/");
data->method()->print_short_name(&ss);
ss.print("(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
}
break;
case DataLayout::bit_data_tag:
break;
case DataLayout::no_tag:
case DataLayout::arg_info_data_tag:
return ss.as_string();
break;
default:
fatal("unexpected tag %d", dp->tag());
}
}
return NULL;
}
void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
print_data_on(st, print_data_on_helper(md));
}
void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
st->print("bci: %d", bci());
st->fill_to(tab_width_one);
st->print("%s", name);
tab(st);
int trap = trap_state();
if (trap != 0) {
char buf[100];
st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
}
if (extra != NULL) {
st->print("%s", extra);
}
int flags = data()->flags();
if (flags != 0) {
st->print("flags(%d) ", flags);
}
}
void ProfileData::tab(outputStream* st, bool first) const {
st->fill_to(first ? tab_width_one : tab_width_two);
}
// ==================================================================
// BitData
//
// A BitData corresponds to a one-bit flag. This is used to indicate
// whether a checkcast bytecode has seen a null value.
void BitData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "BitData", extra);
st->cr();
}
// ==================================================================
// CounterData
//
// A CounterData corresponds to a simple counter.
void CounterData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "CounterData", extra);
st->print_cr("count(%u)", count());
}
// ==================================================================
// JumpData
//
// A JumpData is used to access profiling information for a direct
// branch. It is a counter, used for counting the number of branches,
// plus a data displacement, used for realigning the data pointer to
// the corresponding target bci.
void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
assert(stream->bci() == bci(), "wrong pos");
int target;
Bytecodes::Code c = stream->code();
if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
target = stream->dest_w();
} else {
target = stream->dest();
}
int my_di = mdo->dp_to_di(dp());
int target_di = mdo->bci_to_di(target);
int offset = target_di - my_di;
set_displacement(offset);
}
void JumpData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "JumpData", extra);
st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
}
int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
// Parameter profiling include the receiver
int args_count = include_receiver ? 1 : 0;
ResourceMark rm;
ReferenceArgumentCount rac(signature);
args_count += rac.count();
args_count = MIN2(args_count, max);
return args_count * per_arg_cell_count;
}
int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
const methodHandle m = stream->method();
int bci = stream->bci();
Bytecode_invoke inv(m, bci);
int args_cell = 0;
if (MethodData::profile_arguments_for_invoke(m, bci)) {
args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
}
int ret_cell = 0;
if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
ret_cell = ReturnTypeEntry::static_cell_count();
}
int header_cell = 0;
if (args_cell + ret_cell > 0) {
header_cell = header_cell_count();
}
return header_cell + args_cell + ret_cell;
}
class ArgumentOffsetComputer : public SignatureIterator {
private:
int _max;
int _offset;
GrowableArray<int> _offsets;
friend class SignatureIterator; // so do_parameters_on can call do_type
void do_type(BasicType type) {
if (is_reference_type(type) && _offsets.length() < _max) {
_offsets.push(_offset);
}
_offset += parameter_type_word_count(type);
}
public:
ArgumentOffsetComputer(Symbol* signature, int max)
: SignatureIterator(signature),
_max(max), _offset(0),
_offsets(max) {
do_parameters_on(this); // non-virtual template execution
}
int off_at(int i) const { return _offsets.at(i); }
};
void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) {
ResourceMark rm;
int start = 0;
// Parameter profiling include the receiver
if (include_receiver && has_receiver) {
set_stack_slot(0, 0);
set_type(0, type_none());
start += 1;
}
ArgumentOffsetComputer aos(signature, _number_of_entries-start);
for (int i = start; i < _number_of_entries; i++) {
set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
set_type(i, type_none());
}
}
void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
Bytecode_invoke inv(stream->method(), stream->bci());
if (has_arguments()) {
#ifdef ASSERT
ResourceMark rm;
ReferenceArgumentCount rac(inv.signature());
int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
assert(count > 0, "room for args type but none found?");
check_number_of_arguments(count);
#endif
_args.post_initialize(inv.signature(), inv.has_receiver(), false);
}
if (has_return()) {
assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
_ret.post_initialize();
}
}
void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
Bytecode_invoke inv(stream->method(), stream->bci());
if (has_arguments()) {
#ifdef ASSERT
ResourceMark rm;
ReferenceArgumentCount rac(inv.signature());
int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
assert(count > 0, "room for args type but none found?");
check_number_of_arguments(count);
#endif
_args.post_initialize(inv.signature(), inv.has_receiver(), false);
}
if (has_return()) {
assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
_ret.post_initialize();
}
}
void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
for (int i = 0; i < _number_of_entries; i++) {
intptr_t p = type(i);
Klass* k = (Klass*)klass_part(p);
if (k != NULL && (always_clean || !k->is_loader_alive())) {
set_type(i, with_status((Klass*)NULL, p));
}
}
}
void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
intptr_t p = type();
Klass* k = (Klass*)klass_part(p);
if (k != NULL && (always_clean || !k->is_loader_alive())) {
set_type(with_status((Klass*)NULL, p));
}
}
bool TypeEntriesAtCall::return_profiling_enabled() {
return MethodData::profile_return();
}
bool TypeEntriesAtCall::arguments_profiling_enabled() {
return MethodData::profile_arguments();
}
void TypeEntries::print_klass(outputStream* st, intptr_t k) {
if (is_type_none(k)) {
st->print("none");
} else if (is_type_unknown(k)) {
st->print("unknown");
} else {
valid_klass(k)->print_value_on(st);
}
if (was_null_seen(k)) {
st->print(" (null seen)");
}
}
void TypeStackSlotEntries::print_data_on(outputStream* st) const {
for (int i = 0; i < _number_of_entries; i++) {
_pd->tab(st);
st->print("%d: stack(%u) ", i, stack_slot(i));
print_klass(st, type(i));
st->cr();
}
}
void ReturnTypeEntry::print_data_on(outputStream* st) const {
_pd->tab(st);
print_klass(st, type());
st->cr();
}
void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
CounterData::print_data_on(st, extra);
if (has_arguments()) {
tab(st, true);
st->print("argument types");
_args.print_data_on(st);
}
if (has_return()) {
tab(st, true);
st->print("return type");
_ret.print_data_on(st);
}
}
void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
VirtualCallData::print_data_on(st, extra);
if (has_arguments()) {
tab(st, true);
st->print("argument types");
_args.print_data_on(st);
}
if (has_return()) {
tab(st, true);
st->print("return type");
_ret.print_data_on(st);
}
}
// ==================================================================
// ReceiverTypeData
//
// A ReceiverTypeData is used to access profiling information about a
// dynamic type check. It consists of a counter which counts the total times
// that the check is reached, and a series of (Klass*, count) pairs
// which are used to store a type profile for the receiver of the check.
void ReceiverTypeData::clean_weak_klass_links(bool always_clean) {
for (uint row = 0; row < row_limit(); row++) {
Klass* p = receiver(row);
if (p != NULL && (always_clean || !p->is_loader_alive())) {
clear_row(row);
}
}
}
void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
uint row;
int entries = 0;
for (row = 0; row < row_limit(); row++) {
if (receiver(row) != NULL) entries++;
}
#if INCLUDE_JVMCI
st->print_cr("count(%u) nonprofiled_count(%u) entries(%u)", count(), nonprofiled_count(), entries);
#else
st->print_cr("count(%u) entries(%u)", count(), entries);
#endif
int total = count();
for (row = 0; row < row_limit(); row++) {
if (receiver(row) != NULL) {
total += receiver_count(row);
}
}
for (row = 0; row < row_limit(); row++) {
if (receiver(row) != NULL) {
tab(st);
receiver(row)->print_value_on(st);
st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
}
}
}
void ReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "ReceiverTypeData", extra);
print_receiver_data_on(st);
}
void VirtualCallData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "VirtualCallData", extra);
print_receiver_data_on(st);
}
// ==================================================================
// RetData
//
// A RetData is used to access profiling information for a ret bytecode.
// It is composed of a count of the number of times that the ret has
// been executed, followed by a series of triples of the form
// (bci, count, di) which count the number of times that some bci was the
// target of the ret and cache a corresponding displacement.
void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
for (uint row = 0; row < row_limit(); row++) {
set_bci_displacement(row, -1);
set_bci(row, no_bci);
}
// release so other threads see a consistent state. bci is used as
// a valid flag for bci_displacement.
OrderAccess::release();
}
// This routine needs to atomically update the RetData structure, so the
// caller needs to hold the RetData_lock before it gets here. Since taking
// the lock can block (and allow GC) and since RetData is a ProfileData is a
// wrapper around a derived oop, taking the lock in _this_ method will
// basically cause the 'this' pointer's _data field to contain junk after the
// lock. We require the caller to take the lock before making the ProfileData
// structure. Currently the only caller is InterpreterRuntime::update_mdp_for_ret
address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
// First find the mdp which corresponds to the return bci.
address mdp = h_mdo->bci_to_dp(return_bci);
// Now check to see if any of the cache slots are open.
for (uint row = 0; row < row_limit(); row++) {
if (bci(row) == no_bci) {
set_bci_displacement(row, mdp - dp());
set_bci_count(row, DataLayout::counter_increment);
// Barrier to ensure displacement is written before the bci; allows
// the interpreter to read displacement without fear of race condition.
release_set_bci(row, return_bci);
break;
}
}
return mdp;
}
void RetData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "RetData", extra);
uint row;
int entries = 0;
for (row = 0; row < row_limit(); row++) {
if (bci(row) != no_bci) entries++;
}
st->print_cr("count(%u) entries(%u)", count(), entries);
for (row = 0; row < row_limit(); row++) {
if (bci(row) != no_bci) {
tab(st);
st->print_cr("bci(%d: count(%u) displacement(%d))",
bci(row), bci_count(row), bci_displacement(row));
}
}
}
// ==================================================================
// BranchData
//
// A BranchData is used to access profiling data for a two-way branch.
// It consists of taken and not_taken counts as well as a data displacement
// for the taken case.
void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
assert(stream->bci() == bci(), "wrong pos");
int target = stream->dest();
int my_di = mdo->dp_to_di(dp());
int target_di = mdo->bci_to_di(target);
int offset = target_di - my_di;
set_displacement(offset);
}
void BranchData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "BranchData", extra);
st->print_cr("taken(%u) displacement(%d)",
taken(), displacement());
tab(st);
st->print_cr("not taken(%u)", not_taken());
}
// ==================================================================
// MultiBranchData
//
// A MultiBranchData is used to access profiling information for
// a multi-way branch (*switch bytecodes). It consists of a series
// of (count, displacement) pairs, which count the number of times each
// case was taken and specify the data displacment for each branch target.
int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
int cell_count = 0;
if (stream->code() == Bytecodes::_tableswitch) {
Bytecode_tableswitch sw(stream->method()(), stream->bcp());
cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
} else {
Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
}
return cell_count;
}
void MultiBranchData::post_initialize(BytecodeStream* stream,
MethodData* mdo) {
assert(stream->bci() == bci(), "wrong pos");
int target;
int my_di;
int target_di;
int offset;
if (stream->code() == Bytecodes::_tableswitch) {
Bytecode_tableswitch sw(stream->method()(), stream->bcp());
int len = sw.length();
assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
for (int count = 0; count < len; count++) {
target = sw.dest_offset_at(count) + bci();
my_di = mdo->dp_to_di(dp());
target_di = mdo->bci_to_di(target);
offset = target_di - my_di;
set_displacement_at(count, offset);
}
target = sw.default_offset() + bci();
my_di = mdo->dp_to_di(dp());
target_di = mdo->bci_to_di(target);
offset = target_di - my_di;
set_default_displacement(offset);
} else {
Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
int npairs = sw.number_of_pairs();
assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
for (int count = 0; count < npairs; count++) {
LookupswitchPair pair = sw.pair_at(count);
target = pair.offset() + bci();
my_di = mdo->dp_to_di(dp());
target_di = mdo->bci_to_di(target);
offset = target_di - my_di;
set_displacement_at(count, offset);
}
target = sw.default_offset() + bci();
my_di = mdo->dp_to_di(dp());
target_di = mdo->bci_to_di(target);
offset = target_di - my_di;
set_default_displacement(offset);
}
}
void MultiBranchData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "MultiBranchData", extra);
st->print_cr("default_count(%u) displacement(%d)",
default_count(), default_displacement());
int cases = number_of_cases();
for (int i = 0; i < cases; i++) {
tab(st);
st->print_cr("count(%u) displacement(%d)",
count_at(i), displacement_at(i));
}
}
void ArgInfoData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "ArgInfoData", extra);
int nargs = number_of_args();
for (int i = 0; i < nargs; i++) {
st->print(" 0x%x", arg_modified(i));
}
st->cr();
}
int ParametersTypeData::compute_cell_count(Method* m) {
if (!MethodData::profile_parameters_for_method(methodHandle(Thread::current(), m))) {
return 0;
}
int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit;
int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max);
if (obj_args > 0) {
return obj_args + 1; // 1 cell for array len
}
return 0;
}
void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
_parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
}
bool ParametersTypeData::profiling_enabled() {
return MethodData::profile_parameters();
}
void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
st->print("parameter types"); // FIXME extra ignored?
_parameters.print_data_on(st);
}
void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
print_shared(st, "SpeculativeTrapData", extra);
tab(st);
method()->print_short_name(st);
st->cr();
}
// ==================================================================
// MethodData*
//
// A MethodData* holds information which has been collected about
// a method.
MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
int size = MethodData::compute_allocation_size_in_words(method);
return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
MethodData(method);
}
int MethodData::bytecode_cell_count(Bytecodes::Code code) {
if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) {
return no_profile_data;
}
switch (code) {
case Bytecodes::_checkcast:
case Bytecodes::_instanceof:
case Bytecodes::_aastore:
if (TypeProfileCasts) {
return ReceiverTypeData::static_cell_count();
} else {
return BitData::static_cell_count();
}
case Bytecodes::_invokespecial:
case Bytecodes::_invokestatic:
if (MethodData::profile_arguments() || MethodData::profile_return()) {
return variable_cell_count;
} else {
return CounterData::static_cell_count();
}
case Bytecodes::_goto:
case Bytecodes::_goto_w:
case Bytecodes::_jsr:
case Bytecodes::_jsr_w:
return JumpData::static_cell_count();
case Bytecodes::_invokevirtual:
case Bytecodes::_invokeinterface:
if (MethodData::profile_arguments() || MethodData::profile_return()) {
return variable_cell_count;
} else {
return VirtualCallData::static_cell_count();
}
case Bytecodes::_invokedynamic:
if (MethodData::profile_arguments() || MethodData::profile_return()) {
return variable_cell_count;
} else {
return CounterData::static_cell_count();
}
case Bytecodes::_ret:
return RetData::static_cell_count();
case Bytecodes::_ifeq:
case Bytecodes::_ifne:
case Bytecodes::_iflt:
case Bytecodes::_ifge:
case Bytecodes::_ifgt:
case Bytecodes::_ifle:
case Bytecodes::_if_icmpeq:
case Bytecodes::_if_icmpne:
case Bytecodes::_if_icmplt:
case Bytecodes::_if_icmpge:
case Bytecodes::_if_icmpgt:
case Bytecodes::_if_icmple:
case Bytecodes::_if_acmpeq:
case Bytecodes::_if_acmpne:
case Bytecodes::_ifnull:
case Bytecodes::_ifnonnull:
return BranchData::static_cell_count();
case Bytecodes::_lookupswitch:
case Bytecodes::_tableswitch:
return variable_cell_count;
default:
return no_profile_data;
}
}
// Compute the size of the profiling information corresponding to
// the current bytecode.
int MethodData::compute_data_size(BytecodeStream* stream) {
int cell_count = bytecode_cell_count(stream->code());
if (cell_count == no_profile_data) {
return 0;
}
if (cell_count == variable_cell_count) {
switch (stream->code()) {
case Bytecodes::_lookupswitch:
case Bytecodes::_tableswitch:
cell_count = MultiBranchData::compute_cell_count(stream);
break;
case Bytecodes::_invokespecial:
case Bytecodes::_invokestatic:
case Bytecodes::_invokedynamic:
assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
profile_return_for_invoke(stream->method(), stream->bci())) {
cell_count = CallTypeData::compute_cell_count(stream);
} else {
cell_count = CounterData::static_cell_count();
}
break;
case Bytecodes::_invokevirtual:
case Bytecodes::_invokeinterface: {
assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
profile_return_for_invoke(stream->method(), stream->bci())) {
cell_count = VirtualCallTypeData::compute_cell_count(stream);
} else {
cell_count = VirtualCallData::static_cell_count();
}
break;
}
default:
fatal("unexpected bytecode for var length profile data");
}
}
// Note: cell_count might be zero, meaning that there is just
// a DataLayout header, with no extra cells.
assert(cell_count >= 0, "sanity");
return DataLayout::compute_size_in_bytes(cell_count);
}
bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
// Bytecodes for which we may use speculation
switch (code) {
case Bytecodes::_checkcast:
case Bytecodes::_instanceof:
case Bytecodes::_aastore:
case Bytecodes::_invokevirtual:
case Bytecodes::_invokeinterface:
case Bytecodes::_if_acmpeq:
case Bytecodes::_if_acmpne:
case Bytecodes::_ifnull:
case Bytecodes::_ifnonnull:
case Bytecodes::_invokestatic:
#ifdef COMPILER2
if (CompilerConfig::is_c2_enabled()) {
return UseTypeSpeculation;
}
#endif
default:
return false;
}
return false;
}
#if INCLUDE_JVMCI
void* FailedSpeculation::operator new(size_t size, size_t fs_size) throw() {
return CHeapObj<mtCompiler>::operator new(fs_size, std::nothrow);
}
FailedSpeculation::FailedSpeculation(address speculation, int speculation_len) : _data_len(speculation_len), _next(NULL) {
memcpy(data(), speculation, speculation_len);
}
// A heuristic check to detect nmethods that outlive a failed speculations list.
static void guarantee_failed_speculations_alive(nmethod* nm, FailedSpeculation** failed_speculations_address) {
jlong head = (jlong)(address) *failed_speculations_address;
if ((head & 0x1) == 0x1) {
stringStream st;
if (nm != NULL) {
st.print("%d", nm->compile_id());
Method* method = nm->method();
st.print_raw("{");
if (method != NULL) {
method->print_name(&st);
} else {
const char* jvmci_name = nm->jvmci_name();
if (jvmci_name != NULL) {
st.print_raw(jvmci_name);
}
}
st.print_raw("}");
} else {
st.print("<unknown>");
}
fatal("Adding to failed speculations list that appears to have been freed. Source: %s", st.as_string());
}
}
bool FailedSpeculation::add_failed_speculation(nmethod* nm, FailedSpeculation** failed_speculations_address, address speculation, int speculation_len) {
assert(failed_speculations_address != NULL, "must be");
size_t fs_size = sizeof(FailedSpeculation) + speculation_len;
FailedSpeculation* fs = new (fs_size) FailedSpeculation(speculation, speculation_len);
if (fs == NULL) {
// no memory -> ignore failed speculation
return false;
}
guarantee(is_aligned(fs, sizeof(FailedSpeculation*)), "FailedSpeculation objects must be pointer aligned");
guarantee_failed_speculations_alive(nm, failed_speculations_address);
FailedSpeculation** cursor = failed_speculations_address;
do {
if (*cursor == NULL) {
FailedSpeculation* old_fs = Atomic::cmpxchg(cursor, (FailedSpeculation*) NULL, fs);
if (old_fs == NULL) {
// Successfully appended fs to end of the list
return true;
}
cursor = old_fs->next_adr();
} else {
cursor = (*cursor)->next_adr();
}
} while (true);
}
void FailedSpeculation::free_failed_speculations(FailedSpeculation** failed_speculations_address) {
assert(failed_speculations_address != NULL, "must be");
FailedSpeculation* fs = *failed_speculations_address;
while (fs != NULL) {
FailedSpeculation* next = fs->next();
delete fs;
fs = next;
}
// Write an unaligned value to failed_speculations_address to denote
// that it is no longer a valid pointer. This is allows for the check
// in add_failed_speculation against adding to a freed failed
// speculations list.
long* head = (long*) failed_speculations_address;
(*head) = (*head) | 0x1;
}
#endif // INCLUDE_JVMCI
int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) {
#if INCLUDE_JVMCI
if (ProfileTraps) {
// Assume that up to 30% of the possibly trapping BCIs with no MDP will need to allocate one.
int extra_data_count = MIN2(empty_bc_count, MAX2(4, (empty_bc_count * 30) / 100));
// Make sure we have a minimum number of extra data slots to
// allocate SpeculativeTrapData entries. We would want to have one
// entry per compilation that inlines this method and for which
// some type speculation assumption fails. So the room we need for
// the SpeculativeTrapData entries doesn't directly depend on the
// size of the method. Because it's hard to estimate, we reserve
// space for an arbitrary number of entries.
int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
(SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
return MAX2(extra_data_count, spec_data_count);
} else {
return 0;
}
#else // INCLUDE_JVMCI
if (ProfileTraps) {
// Assume that up to 3% of BCIs with no MDP will need to allocate one.
int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
// If the method is large, let the extra BCIs grow numerous (to ~1%).
int one_percent_of_data
= (uint)data_size / (DataLayout::header_size_in_bytes()*128);
if (extra_data_count < one_percent_of_data)
extra_data_count = one_percent_of_data;
if (extra_data_count > empty_bc_count)
extra_data_count = empty_bc_count; // no need for more
// Make sure we have a minimum number of extra data slots to
// allocate SpeculativeTrapData entries. We would want to have one
// entry per compilation that inlines this method and for which
// some type speculation assumption fails. So the room we need for
// the SpeculativeTrapData entries doesn't directly depend on the
// size of the method. Because it's hard to estimate, we reserve
// space for an arbitrary number of entries.
int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
(SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
return MAX2(extra_data_count, spec_data_count);
} else {
return 0;
}
#endif // INCLUDE_JVMCI
}
// Compute the size of the MethodData* necessary to store
// profiling information about a given method. Size is in bytes.
int MethodData::compute_allocation_size_in_bytes(const methodHandle& method) {
int data_size = 0;
BytecodeStream stream(method);
Bytecodes::Code c;
int empty_bc_count = 0; // number of bytecodes lacking data
bool needs_speculative_traps = false;
while ((c = stream.next()) >= 0) {
int size_in_bytes = compute_data_size(&stream);
data_size += size_in_bytes;
if (size_in_bytes == 0 JVMCI_ONLY(&& Bytecodes::can_trap(c))) empty_bc_count += 1;
needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
}
int object_size = in_bytes(data_offset()) + data_size;
// Add some extra DataLayout cells (at least one) to track stray traps.
int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
// Add a cell to record information about modified arguments.
int arg_size = method->size_of_parameters();
object_size += DataLayout::compute_size_in_bytes(arg_size+1);
// Reserve room for an area of the MDO dedicated to profiling of
// parameters
int args_cell = ParametersTypeData::compute_cell_count(method());
if (args_cell > 0) {
object_size += DataLayout::compute_size_in_bytes(args_cell);
}
return object_size;
}
// Compute the size of the MethodData* necessary to store
// profiling information about a given method. Size is in words
int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
int byte_size = compute_allocation_size_in_bytes(method);
int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
return align_metadata_size(word_size);
}
// Initialize an individual data segment. Returns the size of
// the segment in bytes.
int MethodData::initialize_data(BytecodeStream* stream,
int data_index) {
if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) {
return 0;
}
int cell_count = -1;
int tag = DataLayout::no_tag;
DataLayout* data_layout = data_layout_at(data_index);
Bytecodes::Code c = stream->code();
switch (c) {
case Bytecodes::_checkcast:
case Bytecodes::_instanceof:
case Bytecodes::_aastore:
if (TypeProfileCasts) {
cell_count = ReceiverTypeData::static_cell_count();
tag = DataLayout::receiver_type_data_tag;
} else {
cell_count = BitData::static_cell_count();
tag = DataLayout::bit_data_tag;
}
break;
case Bytecodes::_invokespecial:
case Bytecodes::_invokestatic: {
int counter_data_cell_count = CounterData::static_cell_count();
if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
profile_return_for_invoke(stream->method(), stream->bci())) {
cell_count = CallTypeData::compute_cell_count(stream);
} else {
cell_count = counter_data_cell_count;
}
if (cell_count > counter_data_cell_count) {