24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "interpreter/bytecode.hpp"
28 #include "interpreter/bytecodeStream.hpp"
29 #include "interpreter/linkResolver.hpp"
30 #include "memory/heapInspection.hpp"
31 #include "oops/methodData.hpp"
32 #include "prims/jvmtiRedefineClasses.hpp"
33 #include "runtime/compilationPolicy.hpp"
34 #include "runtime/deoptimization.hpp"
35 #include "runtime/handles.inline.hpp"
36
37 // ==================================================================
38 // DataLayout
39 //
40 // Overlay for generic profiling data.
41
42 // Some types of data layouts need a length field.
43 bool DataLayout::needs_array_len(u1 tag) {
44 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag);
45 }
46
47 // Perform generic initialization of the data. More specific
48 // initialization occurs in overrides of ProfileData::post_initialize.
49 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
50 _header._bits = (intptr_t)0;
51 _header._struct._tag = tag;
52 _header._struct._bci = bci;
53 for (int i = 0; i < cell_count; i++) {
54 set_cell_at(i, (intptr_t)0);
55 }
56 if (needs_array_len(tag)) {
57 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
58 }
59 if (tag == call_type_data_tag) {
60 CallTypeData::initialize(this, cell_count);
61 } else if (tag == virtual_call_type_data_tag) {
62 VirtualCallTypeData::initialize(this, cell_count);
63 }
64 }
139 int target;
140 Bytecodes::Code c = stream->code();
141 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
142 target = stream->dest_w();
143 } else {
144 target = stream->dest();
145 }
146 int my_di = mdo->dp_to_di(dp());
147 int target_di = mdo->bci_to_di(target);
148 int offset = target_di - my_di;
149 set_displacement(offset);
150 }
151
152 #ifndef PRODUCT
153 void JumpData::print_data_on(outputStream* st) const {
154 print_shared(st, "JumpData");
155 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
156 }
157 #endif // !PRODUCT
158
159 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, int max) {
160 ResourceMark rm;
161 SignatureStream ss(signature);
162 int args_count = MIN2(ss.reference_parameter_count(), max);
163 return args_count * per_arg_cell_count;
164 }
165
166 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
167 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
168 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
169 Bytecode_invoke inv(stream->method(), stream->bci());
170 int args_cell = 0;
171 if (arguments_profiling_enabled()) {
172 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), TypeProfileArgsLimit);
173 }
174 int ret_cell = 0;
175 if (return_profiling_enabled() && (inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY)) {
176 ret_cell = ReturnTypeEntry::static_cell_count();
177 }
178 int header_cell = 0;
179 if (args_cell + ret_cell > 0) {
180 header_cell = header_cell_count();
181 }
182
183 return header_cell + args_cell + ret_cell;
184 }
185
186 class ArgumentOffsetComputer : public SignatureInfo {
187 private:
188 int _max;
189 GrowableArray<int> _offsets;
190
191 void set(int size, BasicType type) { _size += size; }
192 void do_object(int begin, int end) {
195 }
196 SignatureInfo::do_object(begin, end);
197 }
198 void do_array (int begin, int end) {
199 if (_offsets.length() < _max) {
200 _offsets.push(_size);
201 }
202 SignatureInfo::do_array(begin, end);
203 }
204
205 public:
206 ArgumentOffsetComputer(Symbol* signature, int max)
207 : SignatureInfo(signature), _max(max), _offsets(Thread::current(), max) {
208 }
209
210 int total() { lazy_iterate_parameters(); return _size; }
211
212 int off_at(int i) const { return _offsets.at(i); }
213 };
214
215 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver) {
216 ResourceMark rm;
217 ArgumentOffsetComputer aos(signature, _number_of_entries);
218 aos.total();
219 for (int i = 0; i < _number_of_entries; i++) {
220 set_stack_slot(i, aos.off_at(i) + (has_receiver ? 1 : 0));
221 set_type(i, type_none());
222 }
223 }
224
225 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
226 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
227 Bytecode_invoke inv(stream->method(), stream->bci());
228
229 SignatureStream ss(inv.signature());
230 if (has_arguments()) {
231 #ifdef ASSERT
232 ResourceMark rm;
233 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
234 assert(count > 0, "room for args type but none found?");
235 check_number_of_arguments(count);
236 #endif
237 _args.post_initialize(inv.signature(), inv.has_receiver());
238 }
239
240 if (has_return()) {
241 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
242 _ret.post_initialize();
243 }
244 }
245
246 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
247 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
248 Bytecode_invoke inv(stream->method(), stream->bci());
249
250 if (has_arguments()) {
251 #ifdef ASSERT
252 ResourceMark rm;
253 SignatureStream ss(inv.signature());
254 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
255 assert(count > 0, "room for args type but none found?");
256 check_number_of_arguments(count);
257 #endif
258 _args.post_initialize(inv.signature(), inv.has_receiver());
259 }
260
261 if (has_return()) {
262 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
263 _ret.post_initialize();
264 }
265 }
266
267 bool TypeEntries::is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p) {
268 return !is_type_none(p) &&
269 !((Klass*)klass_part(p))->is_loader_alive(is_alive_cl);
270 }
271
272 void TypeStackSlotEntries::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
273 for (int i = 0; i < _number_of_entries; i++) {
274 intptr_t p = type(i);
275 if (is_loader_alive(is_alive_cl, p)) {
276 set_type(i, type_none());
277 }
278 }
562 int cases = number_of_cases();
563 for (int i = 0; i < cases; i++) {
564 tab(st);
565 st->print_cr("count(%u) displacement(%d)",
566 count_at(i), displacement_at(i));
567 }
568 }
569 #endif
570
571 #ifndef PRODUCT
572 void ArgInfoData::print_data_on(outputStream* st) const {
573 print_shared(st, "ArgInfoData");
574 int nargs = number_of_args();
575 for (int i = 0; i < nargs; i++) {
576 st->print(" 0x%x", arg_modified(i));
577 }
578 st->cr();
579 }
580
581 #endif
582 // ==================================================================
583 // MethodData*
584 //
585 // A MethodData* holds information which has been collected about
586 // a method.
587
588 MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) {
589 int size = MethodData::compute_allocation_size_in_words(method);
590
591 return new (loader_data, size, false, MetaspaceObj::MethodDataType, THREAD)
592 MethodData(method(), size, CHECK_NULL);
593 }
594
595 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
596 #if defined(COMPILER1) && !defined(COMPILER2)
597 return no_profile_data;
598 #else
599 switch (code) {
600 case Bytecodes::_checkcast:
601 case Bytecodes::_instanceof:
724 int MethodData::compute_allocation_size_in_bytes(methodHandle method) {
725 int data_size = 0;
726 BytecodeStream stream(method);
727 Bytecodes::Code c;
728 int empty_bc_count = 0; // number of bytecodes lacking data
729 while ((c = stream.next()) >= 0) {
730 int size_in_bytes = compute_data_size(&stream);
731 data_size += size_in_bytes;
732 if (size_in_bytes == 0) empty_bc_count += 1;
733 }
734 int object_size = in_bytes(data_offset()) + data_size;
735
736 // Add some extra DataLayout cells (at least one) to track stray traps.
737 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
738 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
739
740 // Add a cell to record information about modified arguments.
741 int arg_size = method->size_of_parameters();
742 object_size += DataLayout::compute_size_in_bytes(arg_size+1);
743
744 return object_size;
745 }
746
747 // Compute the size of the MethodData* necessary to store
748 // profiling information about a given method. Size is in words
749 int MethodData::compute_allocation_size_in_words(methodHandle method) {
750 int byte_size = compute_allocation_size_in_bytes(method);
751 int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
752 return align_object_size(word_size);
753 }
754
755 // Initialize an individual data segment. Returns the size of
756 // the segment in bytes.
757 int MethodData::initialize_data(BytecodeStream* stream,
758 int data_index) {
759 #if defined(COMPILER1) && !defined(COMPILER2)
760 return 0;
761 #else
762 int cell_count = -1;
763 int tag = DataLayout::no_tag;
898 case DataLayout::counter_data_tag:
899 return new CounterData(this);
900 case DataLayout::jump_data_tag:
901 return new JumpData(this);
902 case DataLayout::receiver_type_data_tag:
903 return new ReceiverTypeData(this);
904 case DataLayout::virtual_call_data_tag:
905 return new VirtualCallData(this);
906 case DataLayout::ret_data_tag:
907 return new RetData(this);
908 case DataLayout::branch_data_tag:
909 return new BranchData(this);
910 case DataLayout::multi_branch_data_tag:
911 return new MultiBranchData(this);
912 case DataLayout::arg_info_data_tag:
913 return new ArgInfoData(this);
914 case DataLayout::call_type_data_tag:
915 return new CallTypeData(this);
916 case DataLayout::virtual_call_type_data_tag:
917 return new VirtualCallTypeData(this);
918 };
919 }
920
921 // Iteration over data.
922 ProfileData* MethodData::next_data(ProfileData* current) const {
923 int current_index = dp_to_di(current->dp());
924 int next_index = current_index + current->size_in_bytes();
925 ProfileData* next = data_at(next_index);
926 return next;
927 }
928
929 // Give each of the data entries a chance to perform specific
930 // data initialization.
931 void MethodData::post_initialize(BytecodeStream* stream) {
932 ResourceMark rm;
933 ProfileData* data;
934 for (data = first_data(); is_valid(data); data = next_data(data)) {
935 stream->set_start(data->bci());
936 stream->next();
937 data->post_initialize(stream, this);
938 }
939 }
940
941 // Initialize the MethodData* corresponding to a given method.
942 MethodData::MethodData(methodHandle method, int size, TRAPS) {
943 No_Safepoint_Verifier no_safepoint; // init function atomic wrt GC
944 ResourceMark rm;
945 // Set the method back-pointer.
946 _method = method();
947
948 init();
949 set_creation_mileage(mileage_of(method()));
950
951 // Go through the bytecodes and allocate and initialize the
952 // corresponding data cells.
953 int data_size = 0;
954 int empty_bc_count = 0; // number of bytecodes lacking data
955 _data[0] = 0; // apparently not set below.
956 BytecodeStream stream(method);
957 Bytecodes::Code c;
958 while ((c = stream.next()) >= 0) {
959 int size_in_bytes = initialize_data(&stream, data_size);
960 data_size += size_in_bytes;
961 if (size_in_bytes == 0) empty_bc_count += 1;
962 }
963 _data_size = data_size;
964 int object_size = in_bytes(data_offset()) + data_size;
965
966 // Add some extra DataLayout cells (at least one) to track stray traps.
967 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
968 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
969
970 // Add a cell to record information about modified arguments.
971 // Set up _args_modified array after traps cells so that
972 // the code for traps cells works.
973 DataLayout *dp = data_layout_at(data_size + extra_size);
974
975 int arg_size = method->size_of_parameters();
976 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
977
978 object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
979
980 // Set an initial hint. Don't use set_hint_di() because
981 // first_di() may be out of bounds if data_size is 0.
982 // In that situation, _hint_di is never used, but at
983 // least well-defined.
984 _hint_di = first_di();
985
986 post_initialize(&stream);
987
988 set_size(object_size);
989 }
990
991 void MethodData::init() {
992 _invocation_counter.init();
993 _backedge_counter.init();
994 _invocation_counter_start = 0;
995 _backedge_counter_start = 0;
996 _num_loops = 0;
997 _num_blocks = 0;
998 _highest_comp_level = 0;
1117 void MethodData::print_on(outputStream* st) const {
1118 assert(is_methodData(), "should be method data");
1119 st->print("method data for ");
1120 method()->print_value_on(st);
1121 st->cr();
1122 print_data_on(st);
1123 }
1124
1125 #endif //PRODUCT
1126
1127 void MethodData::print_value_on(outputStream* st) const {
1128 assert(is_methodData(), "should be method data");
1129 st->print("method data for ");
1130 method()->print_value_on(st);
1131 }
1132
1133 #ifndef PRODUCT
1134 void MethodData::print_data_on(outputStream* st) const {
1135 ResourceMark rm;
1136 ProfileData* data = first_data();
1137 for ( ; is_valid(data); data = next_data(data)) {
1138 st->print("%d", dp_to_di(data->dp()));
1139 st->fill_to(6);
1140 data->print_data_on(st);
1141 }
1142 st->print_cr("--- Extra data:");
1143 DataLayout* dp = extra_data_base();
1144 DataLayout* end = extra_data_limit();
1145 for (; dp < end; dp = next_extra(dp)) {
1146 // No need for "OrderAccess::load_acquire" ops,
1147 // since the data structure is monotonic.
1148 if (dp->tag() == DataLayout::no_tag) continue;
1149 if (dp->tag() == DataLayout::bit_data_tag) {
1150 data = new BitData(dp);
1151 } else {
1152 assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
1153 data = new ArgInfoData(dp);
1154 dp = end; // ArgInfoData is at the end of extra data section.
1155 }
1156 st->print("%d", dp_to_di(data->dp()));
1205 }
1206
1207 bool MethodData::profile_all_arguments() {
1208 return profile_arguments_flag() == type_profile_all;
1209 }
1210
1211 bool MethodData::profile_arguments_for_invoke(methodHandle m, int bci) {
1212 if (!profile_arguments()) {
1213 return false;
1214 }
1215
1216 if (profile_all_arguments()) {
1217 return true;
1218 }
1219
1220 assert(profile_arguments_jsr292_only(), "inconsistent");
1221 return profile_jsr292(m, bci);
1222 }
1223
1224 int MethodData::profile_return_flag() {
1225 return TypeProfileLevel / 10;
1226 }
1227
1228 bool MethodData::profile_return() {
1229 return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1230 }
1231
1232 bool MethodData::profile_return_jsr292_only() {
1233 return profile_return_flag() == type_profile_jsr292;
1234 }
1235
1236 bool MethodData::profile_all_return() {
1237 return profile_return_flag() == type_profile_all;
1238 }
1239
1240 bool MethodData::profile_return_for_invoke(methodHandle m, int bci) {
1241 if (!profile_return()) {
1242 return false;
1243 }
1244
1245 if (profile_all_return()) {
1246 return true;
1247 }
1248
1249 assert(profile_return_jsr292_only(), "inconsistent");
1250 return profile_jsr292(m, bci);
1251 }
|
24
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "interpreter/bytecode.hpp"
28 #include "interpreter/bytecodeStream.hpp"
29 #include "interpreter/linkResolver.hpp"
30 #include "memory/heapInspection.hpp"
31 #include "oops/methodData.hpp"
32 #include "prims/jvmtiRedefineClasses.hpp"
33 #include "runtime/compilationPolicy.hpp"
34 #include "runtime/deoptimization.hpp"
35 #include "runtime/handles.inline.hpp"
36
37 // ==================================================================
38 // DataLayout
39 //
40 // Overlay for generic profiling data.
41
42 // Some types of data layouts need a length field.
43 bool DataLayout::needs_array_len(u1 tag) {
44 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag);
45 }
46
47 // Perform generic initialization of the data. More specific
48 // initialization occurs in overrides of ProfileData::post_initialize.
49 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
50 _header._bits = (intptr_t)0;
51 _header._struct._tag = tag;
52 _header._struct._bci = bci;
53 for (int i = 0; i < cell_count; i++) {
54 set_cell_at(i, (intptr_t)0);
55 }
56 if (needs_array_len(tag)) {
57 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
58 }
59 if (tag == call_type_data_tag) {
60 CallTypeData::initialize(this, cell_count);
61 } else if (tag == virtual_call_type_data_tag) {
62 VirtualCallTypeData::initialize(this, cell_count);
63 }
64 }
139 int target;
140 Bytecodes::Code c = stream->code();
141 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
142 target = stream->dest_w();
143 } else {
144 target = stream->dest();
145 }
146 int my_di = mdo->dp_to_di(dp());
147 int target_di = mdo->bci_to_di(target);
148 int offset = target_di - my_di;
149 set_displacement(offset);
150 }
151
152 #ifndef PRODUCT
153 void JumpData::print_data_on(outputStream* st) const {
154 print_shared(st, "JumpData");
155 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
156 }
157 #endif // !PRODUCT
158
159 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
160 int args_count = include_receiver ? 1 : 0;
161 ResourceMark rm;
162 SignatureStream ss(signature);
163 args_count += ss.reference_parameter_count();
164 args_count = MIN2(args_count, max);
165 return args_count * per_arg_cell_count;
166 }
167
168 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
169 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
170 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
171 Bytecode_invoke inv(stream->method(), stream->bci());
172 int args_cell = 0;
173 if (arguments_profiling_enabled()) {
174 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
175 }
176 int ret_cell = 0;
177 if (return_profiling_enabled() && (inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY)) {
178 ret_cell = ReturnTypeEntry::static_cell_count();
179 }
180 int header_cell = 0;
181 if (args_cell + ret_cell > 0) {
182 header_cell = header_cell_count();
183 }
184
185 return header_cell + args_cell + ret_cell;
186 }
187
188 class ArgumentOffsetComputer : public SignatureInfo {
189 private:
190 int _max;
191 GrowableArray<int> _offsets;
192
193 void set(int size, BasicType type) { _size += size; }
194 void do_object(int begin, int end) {
197 }
198 SignatureInfo::do_object(begin, end);
199 }
200 void do_array (int begin, int end) {
201 if (_offsets.length() < _max) {
202 _offsets.push(_size);
203 }
204 SignatureInfo::do_array(begin, end);
205 }
206
207 public:
208 ArgumentOffsetComputer(Symbol* signature, int max)
209 : SignatureInfo(signature), _max(max), _offsets(Thread::current(), max) {
210 }
211
212 int total() { lazy_iterate_parameters(); return _size; }
213
214 int off_at(int i) const { return _offsets.at(i); }
215 };
216
217 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) {
218 ResourceMark rm;
219 int start = 0;
220 if (include_receiver && has_receiver) {
221 set_stack_slot(0, 0);
222 set_type(0, type_none());
223 start += 1;
224 }
225 ArgumentOffsetComputer aos(signature, _number_of_entries-start);
226 aos.total();
227 for (int i = start; i < _number_of_entries; i++) {
228 set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
229 set_type(i, type_none());
230 }
231 }
232
233 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
234 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
235 Bytecode_invoke inv(stream->method(), stream->bci());
236
237 SignatureStream ss(inv.signature());
238 if (has_arguments()) {
239 #ifdef ASSERT
240 ResourceMark rm;
241 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
242 assert(count > 0, "room for args type but none found?");
243 check_number_of_arguments(count);
244 #endif
245 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
246 }
247
248 if (has_return()) {
249 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
250 _ret.post_initialize();
251 }
252 }
253
254 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
255 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
256 Bytecode_invoke inv(stream->method(), stream->bci());
257
258 if (has_arguments()) {
259 #ifdef ASSERT
260 ResourceMark rm;
261 SignatureStream ss(inv.signature());
262 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
263 assert(count > 0, "room for args type but none found?");
264 check_number_of_arguments(count);
265 #endif
266 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
267 }
268
269 if (has_return()) {
270 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
271 _ret.post_initialize();
272 }
273 }
274
275 bool TypeEntries::is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p) {
276 return !is_type_none(p) &&
277 !((Klass*)klass_part(p))->is_loader_alive(is_alive_cl);
278 }
279
280 void TypeStackSlotEntries::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
281 for (int i = 0; i < _number_of_entries; i++) {
282 intptr_t p = type(i);
283 if (is_loader_alive(is_alive_cl, p)) {
284 set_type(i, type_none());
285 }
286 }
570 int cases = number_of_cases();
571 for (int i = 0; i < cases; i++) {
572 tab(st);
573 st->print_cr("count(%u) displacement(%d)",
574 count_at(i), displacement_at(i));
575 }
576 }
577 #endif
578
579 #ifndef PRODUCT
580 void ArgInfoData::print_data_on(outputStream* st) const {
581 print_shared(st, "ArgInfoData");
582 int nargs = number_of_args();
583 for (int i = 0; i < nargs; i++) {
584 st->print(" 0x%x", arg_modified(i));
585 }
586 st->cr();
587 }
588
589 #endif
590
591 int ParametersTypeData::compute_cell_count(Method* m) {
592 if (!MethodData::profile_parameters_for_method(m)) {
593 return 0;
594 }
595 int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit;
596 int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max);
597 if (obj_args > 0) {
598 return obj_args + 1; // 1 cell for array len
599 }
600 return 0;
601 }
602
603 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
604 _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
605 }
606
607 bool ParametersTypeData::profiling_enabled() {
608 return MethodData::profile_parameters();
609 }
610
611 #ifndef PRODUCT
612 void ParametersTypeData::print_data_on(outputStream* st) const {
613 st->print("parameter types");
614 _parameters.print_data_on(st);
615 }
616 #endif
617
618 // ==================================================================
619 // MethodData*
620 //
621 // A MethodData* holds information which has been collected about
622 // a method.
623
624 MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) {
625 int size = MethodData::compute_allocation_size_in_words(method);
626
627 return new (loader_data, size, false, MetaspaceObj::MethodDataType, THREAD)
628 MethodData(method(), size, CHECK_NULL);
629 }
630
631 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
632 #if defined(COMPILER1) && !defined(COMPILER2)
633 return no_profile_data;
634 #else
635 switch (code) {
636 case Bytecodes::_checkcast:
637 case Bytecodes::_instanceof:
760 int MethodData::compute_allocation_size_in_bytes(methodHandle method) {
761 int data_size = 0;
762 BytecodeStream stream(method);
763 Bytecodes::Code c;
764 int empty_bc_count = 0; // number of bytecodes lacking data
765 while ((c = stream.next()) >= 0) {
766 int size_in_bytes = compute_data_size(&stream);
767 data_size += size_in_bytes;
768 if (size_in_bytes == 0) empty_bc_count += 1;
769 }
770 int object_size = in_bytes(data_offset()) + data_size;
771
772 // Add some extra DataLayout cells (at least one) to track stray traps.
773 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
774 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
775
776 // Add a cell to record information about modified arguments.
777 int arg_size = method->size_of_parameters();
778 object_size += DataLayout::compute_size_in_bytes(arg_size+1);
779
780 int args_cell = ParametersTypeData::compute_cell_count(method());
781 if (args_cell > 0) {
782 object_size += DataLayout::compute_size_in_bytes(args_cell);
783 }
784 return object_size;
785 }
786
787 // Compute the size of the MethodData* necessary to store
788 // profiling information about a given method. Size is in words
789 int MethodData::compute_allocation_size_in_words(methodHandle method) {
790 int byte_size = compute_allocation_size_in_bytes(method);
791 int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
792 return align_object_size(word_size);
793 }
794
795 // Initialize an individual data segment. Returns the size of
796 // the segment in bytes.
797 int MethodData::initialize_data(BytecodeStream* stream,
798 int data_index) {
799 #if defined(COMPILER1) && !defined(COMPILER2)
800 return 0;
801 #else
802 int cell_count = -1;
803 int tag = DataLayout::no_tag;
938 case DataLayout::counter_data_tag:
939 return new CounterData(this);
940 case DataLayout::jump_data_tag:
941 return new JumpData(this);
942 case DataLayout::receiver_type_data_tag:
943 return new ReceiverTypeData(this);
944 case DataLayout::virtual_call_data_tag:
945 return new VirtualCallData(this);
946 case DataLayout::ret_data_tag:
947 return new RetData(this);
948 case DataLayout::branch_data_tag:
949 return new BranchData(this);
950 case DataLayout::multi_branch_data_tag:
951 return new MultiBranchData(this);
952 case DataLayout::arg_info_data_tag:
953 return new ArgInfoData(this);
954 case DataLayout::call_type_data_tag:
955 return new CallTypeData(this);
956 case DataLayout::virtual_call_type_data_tag:
957 return new VirtualCallTypeData(this);
958 case DataLayout::parameters_type_data_tag:
959 return new ParametersTypeData(this);
960 };
961 }
962
963 // Iteration over data.
964 ProfileData* MethodData::next_data(ProfileData* current) const {
965 int current_index = dp_to_di(current->dp());
966 int next_index = current_index + current->size_in_bytes();
967 ProfileData* next = data_at(next_index);
968 return next;
969 }
970
971 // Give each of the data entries a chance to perform specific
972 // data initialization.
973 void MethodData::post_initialize(BytecodeStream* stream) {
974 ResourceMark rm;
975 ProfileData* data;
976 for (data = first_data(); is_valid(data); data = next_data(data)) {
977 stream->set_start(data->bci());
978 stream->next();
979 data->post_initialize(stream, this);
980 }
981 if (_parameters_type_data_di != -1) {
982 parameters_type_data()->post_initialize(NULL, this);
983 }
984 }
985
986 // Initialize the MethodData* corresponding to a given method.
987 MethodData::MethodData(methodHandle method, int size, TRAPS) {
988 No_Safepoint_Verifier no_safepoint; // init function atomic wrt GC
989 ResourceMark rm;
990 // Set the method back-pointer.
991 _method = method();
992
993 init();
994 set_creation_mileage(mileage_of(method()));
995
996 // Go through the bytecodes and allocate and initialize the
997 // corresponding data cells.
998 int data_size = 0;
999 int empty_bc_count = 0; // number of bytecodes lacking data
1000 _data[0] = 0; // apparently not set below.
1001 BytecodeStream stream(method);
1002 Bytecodes::Code c;
1003 while ((c = stream.next()) >= 0) {
1004 int size_in_bytes = initialize_data(&stream, data_size);
1005 data_size += size_in_bytes;
1006 if (size_in_bytes == 0) empty_bc_count += 1;
1007 }
1008 _data_size = data_size;
1009 int object_size = in_bytes(data_offset()) + data_size;
1010
1011 // Add some extra DataLayout cells (at least one) to track stray traps.
1012 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
1013 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
1014
1015 // Add a cell to record information about modified arguments.
1016 // Set up _args_modified array after traps cells so that
1017 // the code for traps cells works.
1018 DataLayout *dp = data_layout_at(data_size + extra_size);
1019
1020 int arg_size = method->size_of_parameters();
1021 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
1022
1023 int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1);
1024 object_size += extra_size + arg_data_size;
1025
1026 int args_cell = ParametersTypeData::compute_cell_count(method());
1027 if (args_cell > 0) {
1028 object_size += DataLayout::compute_size_in_bytes(args_cell);
1029 _parameters_type_data_di = data_size + extra_size + arg_data_size;
1030 DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size);
1031 dp->initialize(DataLayout::parameters_type_data_tag, 0, args_cell);
1032 } else {
1033 _parameters_type_data_di = -1;
1034 }
1035
1036 // Set an initial hint. Don't use set_hint_di() because
1037 // first_di() may be out of bounds if data_size is 0.
1038 // In that situation, _hint_di is never used, but at
1039 // least well-defined.
1040 _hint_di = first_di();
1041
1042 post_initialize(&stream);
1043
1044 set_size(object_size);
1045 }
1046
1047 void MethodData::init() {
1048 _invocation_counter.init();
1049 _backedge_counter.init();
1050 _invocation_counter_start = 0;
1051 _backedge_counter_start = 0;
1052 _num_loops = 0;
1053 _num_blocks = 0;
1054 _highest_comp_level = 0;
1173 void MethodData::print_on(outputStream* st) const {
1174 assert(is_methodData(), "should be method data");
1175 st->print("method data for ");
1176 method()->print_value_on(st);
1177 st->cr();
1178 print_data_on(st);
1179 }
1180
1181 #endif //PRODUCT
1182
1183 void MethodData::print_value_on(outputStream* st) const {
1184 assert(is_methodData(), "should be method data");
1185 st->print("method data for ");
1186 method()->print_value_on(st);
1187 }
1188
1189 #ifndef PRODUCT
1190 void MethodData::print_data_on(outputStream* st) const {
1191 ResourceMark rm;
1192 ProfileData* data = first_data();
1193 if (_parameters_type_data_di != -1) {
1194 parameters_type_data()->print_data_on(st);
1195 }
1196 for ( ; is_valid(data); data = next_data(data)) {
1197 st->print("%d", dp_to_di(data->dp()));
1198 st->fill_to(6);
1199 data->print_data_on(st);
1200 }
1201 st->print_cr("--- Extra data:");
1202 DataLayout* dp = extra_data_base();
1203 DataLayout* end = extra_data_limit();
1204 for (; dp < end; dp = next_extra(dp)) {
1205 // No need for "OrderAccess::load_acquire" ops,
1206 // since the data structure is monotonic.
1207 if (dp->tag() == DataLayout::no_tag) continue;
1208 if (dp->tag() == DataLayout::bit_data_tag) {
1209 data = new BitData(dp);
1210 } else {
1211 assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
1212 data = new ArgInfoData(dp);
1213 dp = end; // ArgInfoData is at the end of extra data section.
1214 }
1215 st->print("%d", dp_to_di(data->dp()));
1264 }
1265
1266 bool MethodData::profile_all_arguments() {
1267 return profile_arguments_flag() == type_profile_all;
1268 }
1269
1270 bool MethodData::profile_arguments_for_invoke(methodHandle m, int bci) {
1271 if (!profile_arguments()) {
1272 return false;
1273 }
1274
1275 if (profile_all_arguments()) {
1276 return true;
1277 }
1278
1279 assert(profile_arguments_jsr292_only(), "inconsistent");
1280 return profile_jsr292(m, bci);
1281 }
1282
1283 int MethodData::profile_return_flag() {
1284 return (TypeProfileLevel % 100) / 10;
1285 }
1286
1287 bool MethodData::profile_return() {
1288 return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1289 }
1290
1291 bool MethodData::profile_return_jsr292_only() {
1292 return profile_return_flag() == type_profile_jsr292;
1293 }
1294
1295 bool MethodData::profile_all_return() {
1296 return profile_return_flag() == type_profile_all;
1297 }
1298
1299 bool MethodData::profile_return_for_invoke(methodHandle m, int bci) {
1300 if (!profile_return()) {
1301 return false;
1302 }
1303
1304 if (profile_all_return()) {
1305 return true;
1306 }
1307
1308 assert(profile_return_jsr292_only(), "inconsistent");
1309 return profile_jsr292(m, bci);
1310 }
1311
1312 int MethodData::profile_parameters_flag() {
1313 return TypeProfileLevel / 100;
1314 }
1315
1316 bool MethodData::profile_parameters() {
1317 return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all;
1318 }
1319
1320 bool MethodData::profile_parameters_jsr292_only() {
1321 return profile_parameters_flag() == type_profile_jsr292;
1322 }
1323
1324 bool MethodData::profile_all_parameters() {
1325 return profile_parameters_flag() == type_profile_all;
1326 }
1327
1328 bool MethodData::profile_parameters_for_method(methodHandle m) {
1329 if (!profile_parameters()) {
1330 return false;
1331 }
1332
1333 if (profile_all_parameters()) {
1334 return true;
1335 }
1336
1337 assert(profile_parameters_jsr292_only(), "inconsistent");
1338 return m->is_compiled_lambda_form();
1339 }
|