184 return CompiledIC_at(nm, call_site);
185 }
186
187 CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
188 CompiledIC* c_ic = new CompiledIC(reloc_iter);
189 c_ic->verify();
190 return c_ic;
191 }
192
193 void CompiledIC::ensure_initialized(CallInfo* call_info, Klass* receiver_klass) {
194 if (!_data->is_initialized()) {
195 _data->initialize(call_info, receiver_klass);
196 }
197 }
198
199 void CompiledIC::set_to_clean() {
200 log_debug(inlinecache)("IC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
201 _call->set_destination_mt_safe(SharedRuntime::get_resolve_virtual_call_stub());
202 }
203
204 void CompiledIC::set_to_monomorphic() {
205 assert(data()->is_initialized(), "must be initialized");
206 Method* method = data()->speculated_method();
207 nmethod* code = method->code();
208 address entry;
209 bool to_compiled = code != nullptr && code->is_in_use() && !code->is_unloading();
210
211 if (to_compiled) {
212 entry = code->entry_point();
213 } else {
214 entry = method->get_c2i_unverified_entry();
215 }
216
217 log_trace(inlinecache)("IC@" INTPTR_FORMAT ": monomorphic to %s: %s",
218 p2i(_call->instruction_address()),
219 to_compiled ? "compiled" : "interpreter",
220 method->print_value_string());
221
222 _call->set_destination_mt_safe(entry);
223 }
224
225 void CompiledIC::set_to_megamorphic(CallInfo* call_info) {
226 assert(data()->is_initialized(), "must be initialized");
227
228 address entry;
229 if (call_info->call_kind() == CallInfo::direct_call) {
230 // C1 sometimes compiles a callsite before the target method is loaded, resulting in
231 // dynamically bound callsites that should really be statically bound. However, the
232 // target method might not have a vtable or itable. We just wait for better code to arrive
233 return;
234 } else if (call_info->call_kind() == CallInfo::itable_call) {
235 int itable_index = call_info->itable_index();
236 entry = VtableStubs::find_itable_stub(itable_index);
237 if (entry == nullptr) {
238 return;
239 }
240 #ifdef ASSERT
241 assert(call_info->resolved_method() != nullptr, "virtual or interface method must be found");
242 int index = call_info->resolved_method()->itable_index();
243 assert(index == itable_index, "CallInfo pre-computes this");
244 InstanceKlass* k = call_info->resolved_method()->method_holder();
245 assert(k->verify_itable_index(itable_index), "sanity check");
246 #endif //ASSERT
247 } else {
248 assert(call_info->call_kind() == CallInfo::vtable_call, "what else?");
249 // Can be different than selected_method->vtable_index(), due to package-private etc.
250 int vtable_index = call_info->vtable_index();
251 assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
252 entry = VtableStubs::find_vtable_stub(vtable_index);
253 if (entry == nullptr) {
254 return;
255 }
256 }
257
258 assert(call_info->selected_method() != nullptr, "virtual or interface method must be found");
259 log_trace(inlinecache)("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
260 p2i(_call->instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
261
262 _call->set_destination_mt_safe(entry);
263 assert(is_megamorphic(), "sanity check");
264 }
265
266 void CompiledIC::update(CallInfo* call_info, Klass* receiver_klass) {
267 // If this is the first time we fix the inline cache, we ensure it's initialized
268 ensure_initialized(call_info, receiver_klass);
269
270 if (is_megamorphic()) {
271 // Terminal state for the inline cache
272 return;
273 }
274
275 if (is_speculated_klass(receiver_klass)) {
276 // If the speculated class matches the receiver klass, we can speculate that will
277 // continue to be the case with a monomorphic inline cache
278 set_to_monomorphic();
279 } else {
280 // If the dynamic type speculation fails, we try to transform to a megamorphic state
281 // for the inline cache using stubs to dispatch in tables
282 set_to_megamorphic(call_info);
283 }
284 }
285
286 bool CompiledIC::is_clean() const {
287 return destination() == SharedRuntime::get_resolve_virtual_call_stub();
288 }
289
290 bool CompiledIC::is_monomorphic() const {
291 return !is_clean() && !is_megamorphic();
292 }
293
294 bool CompiledIC::is_megamorphic() const {
295 return VtableStubs::entry_point(destination()) != nullptr;
296 }
297
298 bool CompiledIC::is_speculated_klass(Klass* receiver_klass) {
299 return data()->speculated_klass() == receiver_klass;
300 }
301
302 // GC support
327 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
328 // Reset call site
329 RelocIterator iter((nmethod*)nullptr, instruction_address(), instruction_address() + 1);
330 while (iter.next()) {
331 switch(iter.type()) {
332 case relocInfo::static_call_type:
333 _call->set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub());
334 break;
335 case relocInfo::opt_virtual_call_type:
336 _call->set_destination_mt_safe(SharedRuntime::get_resolve_opt_virtual_call_stub());
337 break;
338 default:
339 ShouldNotReachHere();
340 }
341 }
342 assert(is_clean(), "should be clean after cleaning");
343
344 log_debug(inlinecache)("DC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
345 }
346
347 void CompiledDirectCall::set(const methodHandle& callee_method) {
348 nmethod* code = callee_method->code();
349 nmethod* caller = CodeCache::find_nmethod(instruction_address());
350 assert(caller != nullptr, "did not find caller nmethod");
351
352 bool to_interp_cont_enter = caller->method()->is_continuation_enter_intrinsic() &&
353 ContinuationEntry::is_interpreted_call(instruction_address());
354
355 bool to_compiled = !to_interp_cont_enter && code != nullptr && code->is_in_use() && !code->is_unloading();
356
357 if (to_compiled) {
358 _call->set_destination_mt_safe(code->verified_entry_point());
359 assert(is_call_to_compiled(), "should be compiled after set to compiled");
360 } else {
361 // Patch call site to C2I adapter if code is deoptimized or unloaded.
362 // We also need to patch the static call stub to set the rmethod register
363 // to the callee_method so the c2i adapter knows how to build the frame
364 set_to_interpreted(callee_method, callee_method->get_c2i_entry());
365 assert(is_call_to_interpreted(), "should be interpreted after set to interpreted");
366 }
367
368 log_trace(inlinecache)("DC@" INTPTR_FORMAT ": set to %s: %s: " INTPTR_FORMAT,
369 p2i(_call->instruction_address()),
370 to_compiled ? "compiled" : "interpreter",
371 callee_method->print_value_string(),
372 p2i(_call->destination()));
373 }
374
375 bool CompiledDirectCall::is_clean() const {
376 return destination() == SharedRuntime::get_resolve_static_call_stub() ||
377 destination() == SharedRuntime::get_resolve_opt_virtual_call_stub();
378 }
379
380 bool CompiledDirectCall::is_call_to_interpreted() const {
381 // It is a call to interpreted, if it calls to a stub. Hence, the destination
382 // must be in the stub part of the nmethod that contains the call
383 nmethod* nm = CodeCache::find_nmethod(instruction_address());
384 assert(nm != nullptr, "did not find nmethod");
|
184 return CompiledIC_at(nm, call_site);
185 }
186
187 CompiledIC* CompiledIC_at(RelocIterator* reloc_iter) {
188 CompiledIC* c_ic = new CompiledIC(reloc_iter);
189 c_ic->verify();
190 return c_ic;
191 }
192
193 void CompiledIC::ensure_initialized(CallInfo* call_info, Klass* receiver_klass) {
194 if (!_data->is_initialized()) {
195 _data->initialize(call_info, receiver_klass);
196 }
197 }
198
199 void CompiledIC::set_to_clean() {
200 log_debug(inlinecache)("IC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
201 _call->set_destination_mt_safe(SharedRuntime::get_resolve_virtual_call_stub());
202 }
203
204 void CompiledIC::set_to_monomorphic(bool caller_is_c1) {
205 assert(data()->is_initialized(), "must be initialized");
206 Method* method = data()->speculated_method();
207 nmethod* code = method->code();
208 address entry;
209 bool to_compiled = code != nullptr && code->is_in_use() && !code->is_unloading();
210
211 if (to_compiled) {
212 entry = caller_is_c1 ? code->inline_entry_point() : code->entry_point();
213 } else {
214 entry = caller_is_c1 ? method->get_c2i_unverified_inline_entry() : method->get_c2i_unverified_entry();
215 }
216
217 log_trace(inlinecache)("IC@" INTPTR_FORMAT ": monomorphic to %s: %s",
218 p2i(_call->instruction_address()),
219 to_compiled ? "compiled" : "interpreter",
220 method->print_value_string());
221
222 _call->set_destination_mt_safe(entry);
223 }
224
225 void CompiledIC::set_to_megamorphic(CallInfo* call_info, bool caller_is_c1) {
226 assert(data()->is_initialized(), "must be initialized");
227
228 address entry;
229 if (call_info->call_kind() == CallInfo::direct_call) {
230 // C1 sometimes compiles a callsite before the target method is loaded, resulting in
231 // dynamically bound callsites that should really be statically bound. However, the
232 // target method might not have a vtable or itable. We just wait for better code to arrive
233 return;
234 } else if (call_info->call_kind() == CallInfo::itable_call) {
235 int itable_index = call_info->itable_index();
236 entry = VtableStubs::find_itable_stub(itable_index, caller_is_c1);
237 if (entry == nullptr) {
238 return;
239 }
240 #ifdef ASSERT
241 assert(call_info->resolved_method() != nullptr, "virtual or interface method must be found");
242 int index = call_info->resolved_method()->itable_index();
243 assert(index == itable_index, "CallInfo pre-computes this");
244 InstanceKlass* k = call_info->resolved_method()->method_holder();
245 assert(k->verify_itable_index(itable_index), "sanity check");
246 #endif //ASSERT
247 } else {
248 assert(call_info->call_kind() == CallInfo::vtable_call, "what else?");
249 // Can be different than selected_method->vtable_index(), due to package-private etc.
250 int vtable_index = call_info->vtable_index();
251 assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
252 entry = VtableStubs::find_vtable_stub(vtable_index, caller_is_c1);
253 if (entry == nullptr) {
254 return;
255 }
256 }
257
258 assert(call_info->selected_method() != nullptr, "virtual or interface method must be found");
259 log_trace(inlinecache)("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
260 p2i(_call->instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
261
262 _call->set_destination_mt_safe(entry);
263 assert(is_megamorphic(), "sanity check");
264 }
265
266 void CompiledIC::update(CallInfo* call_info, Klass* receiver_klass, bool caller_is_c1) {
267 // If this is the first time we fix the inline cache, we ensure it's initialized
268 ensure_initialized(call_info, receiver_klass);
269
270 if (is_megamorphic()) {
271 // Terminal state for the inline cache
272 return;
273 }
274
275 if (is_speculated_klass(receiver_klass)) {
276 // If the speculated class matches the receiver klass, we can speculate that will
277 // continue to be the case with a monomorphic inline cache
278 set_to_monomorphic(caller_is_c1);
279 } else {
280 // If the dynamic type speculation fails, we try to transform to a megamorphic state
281 // for the inline cache using stubs to dispatch in tables
282 set_to_megamorphic(call_info, caller_is_c1);
283 }
284 }
285
286 bool CompiledIC::is_clean() const {
287 return destination() == SharedRuntime::get_resolve_virtual_call_stub();
288 }
289
290 bool CompiledIC::is_monomorphic() const {
291 return !is_clean() && !is_megamorphic();
292 }
293
294 bool CompiledIC::is_megamorphic() const {
295 return VtableStubs::entry_point(destination()) != nullptr;
296 }
297
298 bool CompiledIC::is_speculated_klass(Klass* receiver_klass) {
299 return data()->speculated_klass() == receiver_klass;
300 }
301
302 // GC support
327 assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
328 // Reset call site
329 RelocIterator iter((nmethod*)nullptr, instruction_address(), instruction_address() + 1);
330 while (iter.next()) {
331 switch(iter.type()) {
332 case relocInfo::static_call_type:
333 _call->set_destination_mt_safe(SharedRuntime::get_resolve_static_call_stub());
334 break;
335 case relocInfo::opt_virtual_call_type:
336 _call->set_destination_mt_safe(SharedRuntime::get_resolve_opt_virtual_call_stub());
337 break;
338 default:
339 ShouldNotReachHere();
340 }
341 }
342 assert(is_clean(), "should be clean after cleaning");
343
344 log_debug(inlinecache)("DC@" INTPTR_FORMAT ": set to clean", p2i(_call->instruction_address()));
345 }
346
347 void CompiledDirectCall::set(const methodHandle& callee_method, bool caller_is_c1) {
348 nmethod* code = callee_method->code();
349 nmethod* caller = CodeCache::find_nmethod(instruction_address());
350 assert(caller != nullptr, "did not find caller nmethod");
351
352 bool to_interp_cont_enter = caller->method()->is_continuation_enter_intrinsic() &&
353 ContinuationEntry::is_interpreted_call(instruction_address());
354
355 bool to_compiled = !to_interp_cont_enter && code != nullptr && code->is_in_use() && !code->is_unloading();
356
357 if (to_compiled) {
358 _call->set_destination_mt_safe(caller_is_c1 ? code->verified_inline_entry_point() : code->verified_entry_point());
359 assert(is_call_to_compiled(), "should be compiled after set to compiled");
360 } else {
361 // Patch call site to C2I adapter if code is deoptimized or unloaded.
362 // We also need to patch the static call stub to set the rmethod register
363 // to the callee_method so the c2i adapter knows how to build the frame
364 set_to_interpreted(callee_method, caller_is_c1 ? callee_method->get_c2i_inline_entry() : callee_method->get_c2i_entry());
365 assert(is_call_to_interpreted(), "should be interpreted after set to interpreted");
366 }
367
368 log_trace(inlinecache)("DC@" INTPTR_FORMAT ": set to %s: %s: " INTPTR_FORMAT,
369 p2i(_call->instruction_address()),
370 to_compiled ? "compiled" : "interpreter",
371 callee_method->print_value_string(),
372 p2i(_call->destination()));
373 }
374
375 bool CompiledDirectCall::is_clean() const {
376 return destination() == SharedRuntime::get_resolve_static_call_stub() ||
377 destination() == SharedRuntime::get_resolve_opt_virtual_call_stub();
378 }
379
380 bool CompiledDirectCall::is_call_to_interpreted() const {
381 // It is a call to interpreted, if it calls to a stub. Hence, the destination
382 // must be in the stub part of the nmethod that contains the call
383 nmethod* nm = CodeCache::find_nmethod(instruction_address());
384 assert(nm != nullptr, "did not find nmethod");
|