< prev index next >

src/hotspot/share/code/compiledIC.cpp

Print this page

227 CompiledIC::CompiledIC(RelocIterator* iter)
228   : _method(iter->code())
229 {
230   _call = _method->call_wrapper_at(iter->addr());
231   address ic_call = _call->instruction_address();
232 
233   CompiledMethod* nm = iter->code();
234   assert(ic_call != NULL, "ic_call address must be set");
235   assert(nm != NULL, "must pass compiled method");
236   assert(nm->contains(ic_call), "must be in compiled method");
237 
238   initialize_from_iter(iter);
239 }
240 
241 // This function may fail for two reasons: either due to running out of vtable
242 // stubs, or due to running out of IC stubs in an attempted transition to a
243 // transitional state. The needs_ic_stub_refill value will be set if the failure
244 // was due to running out of IC stubs, in which case the caller will refill IC
245 // stubs and retry.
246 bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode,
247                                     bool& needs_ic_stub_refill, TRAPS) {
248   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
249   assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
250   assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");
251 
252   address entry;
253   if (call_info->call_kind() == CallInfo::itable_call) {
254     assert(bytecode == Bytecodes::_invokeinterface, "");
255     int itable_index = call_info->itable_index();
256     entry = VtableStubs::find_itable_stub(itable_index);
257     if (entry == NULL) {
258       return false;
259     }
260 #ifdef ASSERT
261     int index = call_info->resolved_method()->itable_index();
262     assert(index == itable_index, "CallInfo pre-computes this");
263     InstanceKlass* k = call_info->resolved_method()->method_holder();
264     assert(k->verify_itable_index(itable_index), "sanity check");
265 #endif //ASSERT
266     CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(),
267                                                     call_info->resolved_klass(), false);
268     holder->claim();
269     if (!InlineCacheBuffer::create_transition_stub(this, holder, entry)) {
270       delete holder;
271       needs_ic_stub_refill = true;
272       return false;
273     }
274   } else {
275     assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable");
276     // Can be different than selected_method->vtable_index(), due to package-private etc.
277     int vtable_index = call_info->vtable_index();
278     assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
279     entry = VtableStubs::find_vtable_stub(vtable_index);
280     if (entry == NULL) {
281       return false;
282     }
283     if (!InlineCacheBuffer::create_transition_stub(this, NULL, entry)) {
284       needs_ic_stub_refill = true;
285       return false;
286     }
287   }
288 
289   if (TraceICs) {
290     ResourceMark rm;
291     assert(call_info->selected_method() != NULL, "Unexpected null selected method");
292     tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
293                    p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
294   }
295 
296   // We can't check this anymore. With lazy deopt we could have already
297   // cleaned this IC entry before we even return. This is possible if
298   // we ran out of space in the inline cache buffer trying to do the
299   // set_next and we safepointed to free up space. This is a benign

493   // cleaned this IC entry before we even return. This is possible if
494   // we ran out of space in the inline cache buffer trying to do the
495   // set_next and we safepointed to free up space. This is a benign
496   // race because the IC entry was complete when we safepointed so
497   // cleaning it immediately is harmless.
498   // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
499   return true;
500 }
501 
502 
503 // is_optimized: Compiler has generated an optimized call (i.e. fixed, no inline cache)
504 // static_bound: The call can be static bound. If it isn't also optimized, the property
505 // wasn't provable at time of compilation. An optimized call will have any necessary
506 // null check, while a static_bound won't. A static_bound (but not optimized) must
507 // therefore use the unverified entry point.
508 void CompiledIC::compute_monomorphic_entry(const methodHandle& method,
509                                            Klass* receiver_klass,
510                                            bool is_optimized,
511                                            bool static_bound,
512                                            bool caller_is_nmethod,

513                                            CompiledICInfo& info,
514                                            TRAPS) {
515   CompiledMethod* method_code = method->code();
516 
517   address entry = NULL;
518   if (method_code != NULL && method_code->is_in_use() && !method_code->is_unloading()) {
519     assert(method_code->is_compiled(), "must be compiled");
520     // Call to compiled code
521     //
522     // Note: the following problem exists with Compiler1:
523     //   - at compile time we may or may not know if the destination is final
524     //   - if we know that the destination is final (is_optimized), we will emit
525     //     an optimized virtual call (no inline cache), and need a Method* to make
526     //     a call to the interpreter
527     //   - if we don't know if the destination is final, we emit a standard
528     //     virtual call, and use CompiledICHolder to call interpreted code
529     //     (no static call stub has been generated)
530     //   - In the case that we here notice the call is static bound we
531     //     convert the call into what looks to be an optimized virtual call,
532     //     but we must use the unverified entry point (since there will be no
533     //     null check on a call when the target isn't loaded).
534     //     This causes problems when verifying the IC because
535     //     it looks vanilla but is optimized. Code in is_call_to_interpreted
536     //     is aware of this and weakens its asserts.
537     if (is_optimized) {
538       entry      = method_code->verified_entry_point();
539     } else {
540       entry      = method_code->entry_point();
541     }
542   }
543   if (entry != NULL) {
544     // Call to near compiled code.
545     info.set_compiled_entry(entry, is_optimized ? NULL : receiver_klass, is_optimized);
546   } else {
547     if (is_optimized) {
548       // Use stub entry
549       info.set_interpreter_entry(method()->get_c2i_entry(), method());

550     } else {
551       // Use icholder entry
552       assert(method_code == NULL || method_code->is_compiled(), "must be compiled");
553       CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass);
554       info.set_icholder_entry(method()->get_c2i_unverified_entry(), holder);

555     }
556   }
557   assert(info.is_optimized() == is_optimized, "must agree");
558 }
559 
560 
561 bool CompiledIC::is_icholder_entry(address entry) {
562   CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
563   if (cb != NULL && cb->is_adapter_blob()) {
564     return true;
565   }
566   // itable stubs also use CompiledICHolder
567   if (cb != NULL && cb->is_vtable_blob()) {
568     VtableStub* s = VtableStubs::entry_point(entry);
569     return (s != NULL) && s->is_itable_stub();
570   }
571 
572   return false;
573 }
574 

621 }
622 
623 void CompiledStaticCall::set(const StaticCallInfo& info) {
624   assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
625   // Updating a cache to the wrong entry can cause bugs that are very hard
626   // to track down - if cache entry gets invalid - we just clean it. In
627   // this way it is always the same code path that is responsible for
628   // updating and resolving an inline cache
629   assert(is_clean(), "do not update a call entry - use clean");
630 
631   if (info._to_interpreter) {
632     // Call to interpreted code
633     set_to_interpreted(info.callee(), info.entry());
634   } else {
635     set_to_compiled(info.entry());
636   }
637 }
638 
639 // Compute settings for a CompiledStaticCall. Since we might have to set
640 // the stub when calling to the interpreter, we need to return arguments.
641 void CompiledStaticCall::compute_entry(const methodHandle& m, bool caller_is_nmethod, StaticCallInfo& info) {

642   CompiledMethod* m_code = m->code();
643   info._callee = m;
644   if (m_code != NULL && m_code->is_in_use() && !m_code->is_unloading()) {
645     info._to_interpreter = false;
646     info._entry  = m_code->verified_entry_point();




647   } else {
648     // Callee is interpreted code.  In any case entering the interpreter
649     // puts a converter-frame on the stack to save arguments.
650     assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics");
651     info._to_interpreter = true;
652     info._entry      = m()->get_c2i_entry();







653   }
654 }
655 
656 address CompiledDirectStaticCall::find_stub_for(address instruction) {
657   // Find reloc. information containing this call-site
658   RelocIterator iter((nmethod*)NULL, instruction);
659   while (iter.next()) {
660     if (iter.addr() == instruction) {
661       switch(iter.type()) {
662         case relocInfo::static_call_type:
663           return iter.static_call_reloc()->static_stub();
664         // We check here for opt_virtual_call_type, since we reuse the code
665         // from the CompiledIC implementation
666         case relocInfo::opt_virtual_call_type:
667           return iter.opt_virtual_call_reloc()->static_stub();
668         case relocInfo::poll_type:
669         case relocInfo::poll_return_type: // A safepoint can't overlap a call.
670         default:
671           ShouldNotReachHere();
672       }

227 CompiledIC::CompiledIC(RelocIterator* iter)
228   : _method(iter->code())
229 {
230   _call = _method->call_wrapper_at(iter->addr());
231   address ic_call = _call->instruction_address();
232 
233   CompiledMethod* nm = iter->code();
234   assert(ic_call != NULL, "ic_call address must be set");
235   assert(nm != NULL, "must pass compiled method");
236   assert(nm->contains(ic_call), "must be in compiled method");
237 
238   initialize_from_iter(iter);
239 }
240 
241 // This function may fail for two reasons: either due to running out of vtable
242 // stubs, or due to running out of IC stubs in an attempted transition to a
243 // transitional state. The needs_ic_stub_refill value will be set if the failure
244 // was due to running out of IC stubs, in which case the caller will refill IC
245 // stubs and retry.
246 bool CompiledIC::set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode,
247                                     bool& needs_ic_stub_refill, bool caller_is_c1, TRAPS) {
248   assert(CompiledICLocker::is_safe(_method), "mt unsafe call");
249   assert(!is_optimized(), "cannot set an optimized virtual call to megamorphic");
250   assert(is_call_to_compiled() || is_call_to_interpreted(), "going directly to megamorphic?");
251 
252   address entry;
253   if (call_info->call_kind() == CallInfo::itable_call) {
254     assert(bytecode == Bytecodes::_invokeinterface, "");
255     int itable_index = call_info->itable_index();
256     entry = VtableStubs::find_itable_stub(itable_index, caller_is_c1);
257     if (entry == NULL) {
258       return false;
259     }
260 #ifdef ASSERT
261     int index = call_info->resolved_method()->itable_index();
262     assert(index == itable_index, "CallInfo pre-computes this");
263     InstanceKlass* k = call_info->resolved_method()->method_holder();
264     assert(k->verify_itable_index(itable_index), "sanity check");
265 #endif //ASSERT
266     CompiledICHolder* holder = new CompiledICHolder(call_info->resolved_method()->method_holder(),
267                                                     call_info->resolved_klass(), false);
268     holder->claim();
269     if (!InlineCacheBuffer::create_transition_stub(this, holder, entry)) {
270       delete holder;
271       needs_ic_stub_refill = true;
272       return false;
273     }
274   } else {
275     assert(call_info->call_kind() == CallInfo::vtable_call, "either itable or vtable");
276     // Can be different than selected_method->vtable_index(), due to package-private etc.
277     int vtable_index = call_info->vtable_index();
278     assert(call_info->resolved_klass()->verify_vtable_index(vtable_index), "sanity check");
279     entry = VtableStubs::find_vtable_stub(vtable_index, caller_is_c1);
280     if (entry == NULL) {
281       return false;
282     }
283     if (!InlineCacheBuffer::create_transition_stub(this, NULL, entry)) {
284       needs_ic_stub_refill = true;
285       return false;
286     }
287   }
288 
289   if (TraceICs) {
290     ResourceMark rm;
291     assert(call_info->selected_method() != NULL, "Unexpected null selected method");
292     tty->print_cr ("IC@" INTPTR_FORMAT ": to megamorphic %s entry: " INTPTR_FORMAT,
293                    p2i(instruction_address()), call_info->selected_method()->print_value_string(), p2i(entry));
294   }
295 
296   // We can't check this anymore. With lazy deopt we could have already
297   // cleaned this IC entry before we even return. This is possible if
298   // we ran out of space in the inline cache buffer trying to do the
299   // set_next and we safepointed to free up space. This is a benign

493   // cleaned this IC entry before we even return. This is possible if
494   // we ran out of space in the inline cache buffer trying to do the
495   // set_next and we safepointed to free up space. This is a benign
496   // race because the IC entry was complete when we safepointed so
497   // cleaning it immediately is harmless.
498   // assert(is_call_to_compiled() || is_call_to_interpreted(), "sanity check");
499   return true;
500 }
501 
502 
503 // is_optimized: Compiler has generated an optimized call (i.e. fixed, no inline cache)
504 // static_bound: The call can be static bound. If it isn't also optimized, the property
505 // wasn't provable at time of compilation. An optimized call will have any necessary
506 // null check, while a static_bound won't. A static_bound (but not optimized) must
507 // therefore use the unverified entry point.
508 void CompiledIC::compute_monomorphic_entry(const methodHandle& method,
509                                            Klass* receiver_klass,
510                                            bool is_optimized,
511                                            bool static_bound,
512                                            bool caller_is_nmethod,
513                                            bool caller_is_c1,
514                                            CompiledICInfo& info,
515                                            TRAPS) {
516   CompiledMethod* method_code = method->code();
517 
518   address entry = NULL;
519   if (method_code != NULL && method_code->is_in_use() && !method_code->is_unloading()) {
520     assert(method_code->is_compiled(), "must be compiled");
521     // Call to compiled code
522     //
523     // Note: the following problem exists with Compiler1:
524     //   - at compile time we may or may not know if the destination is final
525     //   - if we know that the destination is final (is_optimized), we will emit
526     //     an optimized virtual call (no inline cache), and need a Method* to make
527     //     a call to the interpreter
528     //   - if we don't know if the destination is final, we emit a standard
529     //     virtual call, and use CompiledICHolder to call interpreted code
530     //     (no static call stub has been generated)
531     //   - In the case that we here notice the call is static bound we
532     //     convert the call into what looks to be an optimized virtual call,
533     //     but we must use the unverified entry point (since there will be no
534     //     null check on a call when the target isn't loaded).
535     //     This causes problems when verifying the IC because
536     //     it looks vanilla but is optimized. Code in is_call_to_interpreted
537     //     is aware of this and weakens its asserts.
538     if (is_optimized) {
539       entry      = caller_is_c1 ? method_code->verified_inline_entry_point() : method_code->verified_entry_point();
540     } else {
541       entry      = caller_is_c1 ? method_code->inline_entry_point() : method_code->entry_point();
542     }
543   }
544   if (entry != NULL) {
545     // Call to near compiled code.
546     info.set_compiled_entry(entry, is_optimized ? NULL : receiver_klass, is_optimized);
547   } else {
548     if (is_optimized) {
549       // Use stub entry
550       address entry = caller_is_c1 ? method()->get_c2i_inline_entry() : method()->get_c2i_entry();
551       info.set_interpreter_entry(entry, method());
552     } else {
553       // Use icholder entry
554       assert(method_code == NULL || method_code->is_compiled(), "must be compiled");
555       CompiledICHolder* holder = new CompiledICHolder(method(), receiver_klass);
556       entry = (caller_is_c1)? method()->get_c2i_unverified_inline_entry() : method()->get_c2i_unverified_entry();
557       info.set_icholder_entry(entry, holder);
558     }
559   }
560   assert(info.is_optimized() == is_optimized, "must agree");
561 }
562 
563 
564 bool CompiledIC::is_icholder_entry(address entry) {
565   CodeBlob* cb = CodeCache::find_blob_unsafe(entry);
566   if (cb != NULL && cb->is_adapter_blob()) {
567     return true;
568   }
569   // itable stubs also use CompiledICHolder
570   if (cb != NULL && cb->is_vtable_blob()) {
571     VtableStub* s = VtableStubs::entry_point(entry);
572     return (s != NULL) && s->is_itable_stub();
573   }
574 
575   return false;
576 }
577 

624 }
625 
626 void CompiledStaticCall::set(const StaticCallInfo& info) {
627   assert(CompiledICLocker::is_safe(instruction_address()), "mt unsafe call");
628   // Updating a cache to the wrong entry can cause bugs that are very hard
629   // to track down - if cache entry gets invalid - we just clean it. In
630   // this way it is always the same code path that is responsible for
631   // updating and resolving an inline cache
632   assert(is_clean(), "do not update a call entry - use clean");
633 
634   if (info._to_interpreter) {
635     // Call to interpreted code
636     set_to_interpreted(info.callee(), info.entry());
637   } else {
638     set_to_compiled(info.entry());
639   }
640 }
641 
642 // Compute settings for a CompiledStaticCall. Since we might have to set
643 // the stub when calling to the interpreter, we need to return arguments.
644 void CompiledStaticCall::compute_entry(const methodHandle& m, CompiledMethod* caller_nm, StaticCallInfo& info) {
645   bool caller_is_nmethod = caller_nm->is_nmethod();
646   CompiledMethod* m_code = m->code();
647   info._callee = m;
648   if (m_code != NULL && m_code->is_in_use() && !m_code->is_unloading()) {
649     info._to_interpreter = false;
650     if (caller_nm->is_compiled_by_c1()) {
651       info._entry = m_code->verified_inline_entry_point();
652     } else {
653       info._entry = m_code->verified_entry_point();
654     }
655   } else {
656     // Callee is interpreted code.  In any case entering the interpreter
657     // puts a converter-frame on the stack to save arguments.
658     assert(!m->is_method_handle_intrinsic(), "Compiled code should never call interpreter MH intrinsics");
659     info._to_interpreter = true;
660 
661     if (caller_nm->is_compiled_by_c1()) {
662       // C1 -> interp: values passed as oops
663       info._entry = m()->get_c2i_inline_entry();
664     } else {
665       // C2 -> interp: values passed fields
666       info._entry = m()->get_c2i_entry();
667     }
668   }
669 }
670 
671 address CompiledDirectStaticCall::find_stub_for(address instruction) {
672   // Find reloc. information containing this call-site
673   RelocIterator iter((nmethod*)NULL, instruction);
674   while (iter.next()) {
675     if (iter.addr() == instruction) {
676       switch(iter.type()) {
677         case relocInfo::static_call_type:
678           return iter.static_call_reloc()->static_stub();
679         // We check here for opt_virtual_call_type, since we reuse the code
680         // from the CompiledIC implementation
681         case relocInfo::opt_virtual_call_type:
682           return iter.opt_virtual_call_reloc()->static_stub();
683         case relocInfo::poll_type:
684         case relocInfo::poll_return_type: // A safepoint can't overlap a call.
685         default:
686           ShouldNotReachHere();
687       }
< prev index next >